ptrace.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. * arch/score/kernel/ptrace.c
  3. *
  4. * Score Processor version.
  5. *
  6. * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
  7. * Chen Liqin <liqin.chen@sunplusct.com>
  8. * Lennox Wu <lennox.wu@sunplusct.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, see the file COPYING, or write
  22. * to the Free Software Foundation, Inc.,
  23. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <linux/elf.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/regset.h>
  30. #include <asm/uaccess.h>
  31. /*
  32. * retrieve the contents of SCORE userspace general registers
  33. */
  34. static int genregs_get(struct task_struct *target,
  35. const struct user_regset *regset,
  36. unsigned int pos, unsigned int count,
  37. void *kbuf, void __user *ubuf)
  38. {
  39. const struct pt_regs *regs = task_pt_regs(target);
  40. int ret;
  41. /* skip 9 * sizeof(unsigned long) not use for pt_regs */
  42. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  43. 0, offsetof(struct pt_regs, regs));
  44. /* r0 - r31, cel, ceh, sr0, sr1, sr2, epc, ema, psr, ecr, condition */
  45. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  46. regs->regs,
  47. offsetof(struct pt_regs, regs),
  48. offsetof(struct pt_regs, cp0_condition));
  49. if (!ret)
  50. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  51. sizeof(struct pt_regs), -1);
  52. return ret;
  53. }
  54. /*
  55. * update the contents of the SCORE userspace general registers
  56. */
  57. static int genregs_set(struct task_struct *target,
  58. const struct user_regset *regset,
  59. unsigned int pos, unsigned int count,
  60. const void *kbuf, const void __user *ubuf)
  61. {
  62. struct pt_regs *regs = task_pt_regs(target);
  63. int ret;
  64. /* skip 9 * sizeof(unsigned long) */
  65. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  66. 0, offsetof(struct pt_regs, regs));
  67. /* r0 - r31, cel, ceh, sr0, sr1, sr2, epc, ema, psr, ecr, condition */
  68. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  69. regs->regs,
  70. offsetof(struct pt_regs, regs),
  71. offsetof(struct pt_regs, cp0_condition));
  72. if (!ret)
  73. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  74. sizeof(struct pt_regs), -1);
  75. return ret;
  76. }
  77. /*
  78. * Define the register sets available on the score7 under Linux
  79. */
  80. enum score7_regset {
  81. REGSET_GENERAL,
  82. };
  83. static const struct user_regset score7_regsets[] = {
  84. [REGSET_GENERAL] = {
  85. .core_note_type = NT_PRSTATUS,
  86. .n = ELF_NGREG,
  87. .size = sizeof(long),
  88. .align = sizeof(long),
  89. .get = genregs_get,
  90. .set = genregs_set,
  91. },
  92. };
  93. static const struct user_regset_view user_score_native_view = {
  94. .name = "score7",
  95. .e_machine = EM_SCORE7,
  96. .regsets = score7_regsets,
  97. .n = ARRAY_SIZE(score7_regsets),
  98. };
  99. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  100. {
  101. return &user_score_native_view;
  102. }
  103. static int is_16bitinsn(unsigned long insn)
  104. {
  105. if ((insn & INSN32_MASK) == INSN32_MASK)
  106. return 0;
  107. else
  108. return 1;
  109. }
  110. int
  111. read_tsk_long(struct task_struct *child,
  112. unsigned long addr, unsigned long *res)
  113. {
  114. int copied;
  115. copied = access_process_vm(child, addr, res, sizeof(*res), 0);
  116. return copied != sizeof(*res) ? -EIO : 0;
  117. }
  118. int
  119. read_tsk_short(struct task_struct *child,
  120. unsigned long addr, unsigned short *res)
  121. {
  122. int copied;
  123. copied = access_process_vm(child, addr, res, sizeof(*res), 0);
  124. return copied != sizeof(*res) ? -EIO : 0;
  125. }
  126. static int
  127. write_tsk_short(struct task_struct *child,
  128. unsigned long addr, unsigned short val)
  129. {
  130. int copied;
  131. copied = access_process_vm(child, addr, &val, sizeof(val), 1);
  132. return copied != sizeof(val) ? -EIO : 0;
  133. }
  134. static int
  135. write_tsk_long(struct task_struct *child,
  136. unsigned long addr, unsigned long val)
  137. {
  138. int copied;
  139. copied = access_process_vm(child, addr, &val, sizeof(val), 1);
  140. return copied != sizeof(val) ? -EIO : 0;
  141. }
  142. void user_enable_single_step(struct task_struct *child)
  143. {
  144. /* far_epc is the target of branch */
  145. unsigned int epc, far_epc = 0;
  146. unsigned long epc_insn, far_epc_insn;
  147. int ninsn_type; /* next insn type 0=16b, 1=32b */
  148. unsigned int tmp, tmp2;
  149. struct pt_regs *regs = task_pt_regs(child);
  150. child->thread.single_step = 1;
  151. child->thread.ss_nextcnt = 1;
  152. epc = regs->cp0_epc;
  153. read_tsk_long(child, epc, &epc_insn);
  154. if (is_16bitinsn(epc_insn)) {
  155. if ((epc_insn & J16M) == J16) {
  156. tmp = epc_insn & 0xFFE;
  157. epc = (epc & 0xFFFFF000) | tmp;
  158. } else if ((epc_insn & B16M) == B16) {
  159. child->thread.ss_nextcnt = 2;
  160. tmp = (epc_insn & 0xFF) << 1;
  161. tmp = tmp << 23;
  162. tmp = (unsigned int)((int) tmp >> 23);
  163. far_epc = epc + tmp;
  164. epc += 2;
  165. } else if ((epc_insn & BR16M) == BR16) {
  166. child->thread.ss_nextcnt = 2;
  167. tmp = (epc_insn >> 4) & 0xF;
  168. far_epc = regs->regs[tmp];
  169. epc += 2;
  170. } else
  171. epc += 2;
  172. } else {
  173. if ((epc_insn & J32M) == J32) {
  174. tmp = epc_insn & 0x03FFFFFE;
  175. tmp2 = tmp & 0x7FFF;
  176. tmp = (((tmp >> 16) & 0x3FF) << 15) | tmp2;
  177. epc = (epc & 0xFFC00000) | tmp;
  178. } else if ((epc_insn & B32M) == B32) {
  179. child->thread.ss_nextcnt = 2;
  180. tmp = epc_insn & 0x03FFFFFE; /* discard LK bit */
  181. tmp2 = tmp & 0x3FF;
  182. tmp = (((tmp >> 16) & 0x3FF) << 10) | tmp2; /* 20bit */
  183. tmp = tmp << 12;
  184. tmp = (unsigned int)((int) tmp >> 12);
  185. far_epc = epc + tmp;
  186. epc += 4;
  187. } else if ((epc_insn & BR32M) == BR32) {
  188. child->thread.ss_nextcnt = 2;
  189. tmp = (epc_insn >> 16) & 0x1F;
  190. far_epc = regs->regs[tmp];
  191. epc += 4;
  192. } else
  193. epc += 4;
  194. }
  195. if (child->thread.ss_nextcnt == 1) {
  196. read_tsk_long(child, epc, &epc_insn);
  197. if (is_16bitinsn(epc_insn)) {
  198. write_tsk_short(child, epc, SINGLESTEP16_INSN);
  199. ninsn_type = 0;
  200. } else {
  201. write_tsk_long(child, epc, SINGLESTEP32_INSN);
  202. ninsn_type = 1;
  203. }
  204. if (ninsn_type == 0) { /* 16bits */
  205. child->thread.insn1_type = 0;
  206. child->thread.addr1 = epc;
  207. /* the insn may have 32bit data */
  208. child->thread.insn1 = (short)epc_insn;
  209. } else {
  210. child->thread.insn1_type = 1;
  211. child->thread.addr1 = epc;
  212. child->thread.insn1 = epc_insn;
  213. }
  214. } else {
  215. /* branch! have two target child->thread.ss_nextcnt=2 */
  216. read_tsk_long(child, epc, &epc_insn);
  217. read_tsk_long(child, far_epc, &far_epc_insn);
  218. if (is_16bitinsn(epc_insn)) {
  219. write_tsk_short(child, epc, SINGLESTEP16_INSN);
  220. ninsn_type = 0;
  221. } else {
  222. write_tsk_long(child, epc, SINGLESTEP32_INSN);
  223. ninsn_type = 1;
  224. }
  225. if (ninsn_type == 0) { /* 16bits */
  226. child->thread.insn1_type = 0;
  227. child->thread.addr1 = epc;
  228. /* the insn may have 32bit data */
  229. child->thread.insn1 = (short)epc_insn;
  230. } else {
  231. child->thread.insn1_type = 1;
  232. child->thread.addr1 = epc;
  233. child->thread.insn1 = epc_insn;
  234. }
  235. if (is_16bitinsn(far_epc_insn)) {
  236. write_tsk_short(child, far_epc, SINGLESTEP16_INSN);
  237. ninsn_type = 0;
  238. } else {
  239. write_tsk_long(child, far_epc, SINGLESTEP32_INSN);
  240. ninsn_type = 1;
  241. }
  242. if (ninsn_type == 0) { /* 16bits */
  243. child->thread.insn2_type = 0;
  244. child->thread.addr2 = far_epc;
  245. /* the insn may have 32bit data */
  246. child->thread.insn2 = (short)far_epc_insn;
  247. } else {
  248. child->thread.insn2_type = 1;
  249. child->thread.addr2 = far_epc;
  250. child->thread.insn2 = far_epc_insn;
  251. }
  252. }
  253. }
  254. void user_disable_single_step(struct task_struct *child)
  255. {
  256. if (child->thread.insn1_type == 0)
  257. write_tsk_short(child, child->thread.addr1,
  258. child->thread.insn1);
  259. if (child->thread.insn1_type == 1)
  260. write_tsk_long(child, child->thread.addr1,
  261. child->thread.insn1);
  262. if (child->thread.ss_nextcnt == 2) { /* branch */
  263. if (child->thread.insn1_type == 0)
  264. write_tsk_short(child, child->thread.addr1,
  265. child->thread.insn1);
  266. if (child->thread.insn1_type == 1)
  267. write_tsk_long(child, child->thread.addr1,
  268. child->thread.insn1);
  269. if (child->thread.insn2_type == 0)
  270. write_tsk_short(child, child->thread.addr2,
  271. child->thread.insn2);
  272. if (child->thread.insn2_type == 1)
  273. write_tsk_long(child, child->thread.addr2,
  274. child->thread.insn2);
  275. }
  276. child->thread.single_step = 0;
  277. child->thread.ss_nextcnt = 0;
  278. }
  279. void ptrace_disable(struct task_struct *child)
  280. {
  281. user_disable_single_step(child);
  282. }
  283. long
  284. arch_ptrace(struct task_struct *child, long request,
  285. unsigned long addr, unsigned long data)
  286. {
  287. int ret;
  288. unsigned long __user *datap = (void __user *)data;
  289. switch (request) {
  290. case PTRACE_GETREGS:
  291. ret = copy_regset_to_user(child, &user_score_native_view,
  292. REGSET_GENERAL,
  293. 0, sizeof(struct pt_regs),
  294. datap);
  295. break;
  296. case PTRACE_SETREGS:
  297. ret = copy_regset_from_user(child, &user_score_native_view,
  298. REGSET_GENERAL,
  299. 0, sizeof(struct pt_regs),
  300. datap);
  301. break;
  302. default:
  303. ret = ptrace_request(child, request, addr, data);
  304. break;
  305. }
  306. return ret;
  307. }
  308. /*
  309. * Notification of system call entry/exit
  310. * - triggered by current->work.syscall_trace
  311. */
  312. asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
  313. {
  314. if (!(current->ptrace & PT_PTRACED))
  315. return;
  316. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  317. return;
  318. /* The 0x80 provides a way for the tracing parent to distinguish
  319. between a syscall stop and SIGTRAP delivery. */
  320. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) ?
  321. 0x80 : 0));
  322. /*
  323. * this isn't the same as continuing with a signal, but it will do
  324. * for normal use. strace only continues with a signal if the
  325. * stopping signal is not SIGTRAP. -brl
  326. */
  327. if (current->exit_code) {
  328. send_sig(current->exit_code, current, 1);
  329. current->exit_code = 0;
  330. }
  331. }