ptrace.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /*
  2. * Kernel support for the ptrace() and syscall tracing interfaces.
  3. *
  4. * Copyright (C) 2000 Hewlett-Packard Co, Linuxcare Inc.
  5. * Copyright (C) 2000 Matthew Wilcox <matthew@wil.cx>
  6. * Copyright (C) 2000 David Huggins-Daines <dhd@debian.org>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/mm.h>
  11. #include <linux/smp.h>
  12. #include <linux/errno.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/user.h>
  15. #include <linux/personality.h>
  16. #include <linux/security.h>
  17. #include <linux/compat.h>
  18. #include <linux/signal.h>
  19. #include <asm/uaccess.h>
  20. #include <asm/pgtable.h>
  21. #include <asm/system.h>
  22. #include <asm/processor.h>
  23. #include <asm/asm-offsets.h>
  24. /* PSW bits we allow the debugger to modify */
  25. #define USER_PSW_BITS (PSW_N | PSW_V | PSW_CB)
  26. #undef DEBUG_PTRACE
  27. #ifdef DEBUG_PTRACE
  28. #define DBG(x...) printk(x)
  29. #else
  30. #define DBG(x...)
  31. #endif
  32. #ifdef CONFIG_64BIT
  33. /* This function is needed to translate 32 bit pt_regs offsets in to
  34. * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel
  35. * will request offset 12 if it wants gr3, but the lower 32 bits of
  36. * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4).
  37. * This code relies on a 32 bit pt_regs being comprised of 32 bit values
  38. * except for the fp registers which (a) are 64 bits, and (b) follow
  39. * the gr registers at the start of pt_regs. The 32 bit pt_regs should
  40. * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[]
  41. * being 64 bit in both cases.
  42. */
  43. static long translate_usr_offset(long offset)
  44. {
  45. if (offset < 0)
  46. return -1;
  47. else if (offset <= 32*4) /* gr[0..31] */
  48. return offset * 2 + 4;
  49. else if (offset <= 32*4+32*8) /* gr[0..31] + fr[0..31] */
  50. return offset + 32*4;
  51. else if (offset < sizeof(struct pt_regs)/2 + 32*4)
  52. return offset * 2 + 4 - 32*8;
  53. else
  54. return -1;
  55. }
  56. #endif
  57. /*
  58. * Called by kernel/ptrace.c when detaching..
  59. *
  60. * Make sure single step bits etc are not set.
  61. */
  62. void ptrace_disable(struct task_struct *child)
  63. {
  64. /* make sure the trap bits are not set */
  65. pa_psw(child)->r = 0;
  66. pa_psw(child)->t = 0;
  67. pa_psw(child)->h = 0;
  68. pa_psw(child)->l = 0;
  69. }
  70. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  71. {
  72. long ret;
  73. #ifdef DEBUG_PTRACE
  74. long oaddr=addr, odata=data;
  75. #endif
  76. switch (request) {
  77. case PTRACE_PEEKTEXT: /* read word at location addr. */
  78. case PTRACE_PEEKDATA: {
  79. int copied;
  80. #ifdef CONFIG_64BIT
  81. if (__is_compat_task(child)) {
  82. unsigned int tmp;
  83. addr &= 0xffffffffL;
  84. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  85. ret = -EIO;
  86. if (copied != sizeof(tmp))
  87. goto out_tsk;
  88. ret = put_user(tmp,(unsigned int *) data);
  89. DBG("sys_ptrace(PEEK%s, %d, %lx, %lx) returning %ld, data %x\n",
  90. request == PTRACE_PEEKTEXT ? "TEXT" : "DATA",
  91. pid, oaddr, odata, ret, tmp);
  92. }
  93. else
  94. #endif
  95. {
  96. unsigned long tmp;
  97. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  98. ret = -EIO;
  99. if (copied != sizeof(tmp))
  100. goto out_tsk;
  101. ret = put_user(tmp,(unsigned long *) data);
  102. }
  103. goto out_tsk;
  104. }
  105. /* when I and D space are separate, this will have to be fixed. */
  106. case PTRACE_POKETEXT: /* write the word at location addr. */
  107. case PTRACE_POKEDATA:
  108. ret = 0;
  109. #ifdef CONFIG_64BIT
  110. if (__is_compat_task(child)) {
  111. unsigned int tmp = (unsigned int)data;
  112. DBG("sys_ptrace(POKE%s, %d, %lx, %lx)\n",
  113. request == PTRACE_POKETEXT ? "TEXT" : "DATA",
  114. pid, oaddr, odata);
  115. addr &= 0xffffffffL;
  116. if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1) == sizeof(tmp))
  117. goto out_tsk;
  118. }
  119. else
  120. #endif
  121. {
  122. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  123. goto out_tsk;
  124. }
  125. ret = -EIO;
  126. goto out_tsk;
  127. /* Read the word at location addr in the USER area. For ptraced
  128. processes, the kernel saves all regs on a syscall. */
  129. case PTRACE_PEEKUSR: {
  130. ret = -EIO;
  131. #ifdef CONFIG_64BIT
  132. if (__is_compat_task(child)) {
  133. unsigned int tmp;
  134. if (addr & (sizeof(int)-1))
  135. goto out_tsk;
  136. if ((addr = translate_usr_offset(addr)) < 0)
  137. goto out_tsk;
  138. tmp = *(unsigned int *) ((char *) task_regs(child) + addr);
  139. ret = put_user(tmp, (unsigned int *) data);
  140. DBG("sys_ptrace(PEEKUSR, %d, %lx, %lx) returning %ld, addr %lx, data %x\n",
  141. pid, oaddr, odata, ret, addr, tmp);
  142. }
  143. else
  144. #endif
  145. {
  146. unsigned long tmp;
  147. if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
  148. goto out_tsk;
  149. tmp = *(unsigned long *) ((char *) task_regs(child) + addr);
  150. ret = put_user(tmp, (unsigned long *) data);
  151. }
  152. goto out_tsk;
  153. }
  154. /* Write the word at location addr in the USER area. This will need
  155. to change when the kernel no longer saves all regs on a syscall.
  156. FIXME. There is a problem at the moment in that r3-r18 are only
  157. saved if the process is ptraced on syscall entry, and even then
  158. those values are overwritten by actual register values on syscall
  159. exit. */
  160. case PTRACE_POKEUSR:
  161. ret = -EIO;
  162. /* Some register values written here may be ignored in
  163. * entry.S:syscall_restore_rfi; e.g. iaoq is written with
  164. * r31/r31+4, and not with the values in pt_regs.
  165. */
  166. /* PT_PSW=0, so this is valid for 32 bit processes under 64
  167. * bit kernels.
  168. */
  169. if (addr == PT_PSW) {
  170. /* PT_PSW=0, so this is valid for 32 bit processes
  171. * under 64 bit kernels.
  172. *
  173. * Allow writing to Nullify, Divide-step-correction,
  174. * and carry/borrow bits.
  175. * BEWARE, if you set N, and then single step, it won't
  176. * stop on the nullified instruction.
  177. */
  178. DBG("sys_ptrace(POKEUSR, %d, %lx, %lx)\n",
  179. pid, oaddr, odata);
  180. data &= USER_PSW_BITS;
  181. task_regs(child)->gr[0] &= ~USER_PSW_BITS;
  182. task_regs(child)->gr[0] |= data;
  183. ret = 0;
  184. goto out_tsk;
  185. }
  186. #ifdef CONFIG_64BIT
  187. if (__is_compat_task(child)) {
  188. if (addr & (sizeof(int)-1))
  189. goto out_tsk;
  190. if ((addr = translate_usr_offset(addr)) < 0)
  191. goto out_tsk;
  192. DBG("sys_ptrace(POKEUSR, %d, %lx, %lx) addr %lx\n",
  193. pid, oaddr, odata, addr);
  194. if (addr >= PT_FR0 && addr <= PT_FR31 + 4) {
  195. /* Special case, fp regs are 64 bits anyway */
  196. *(unsigned int *) ((char *) task_regs(child) + addr) = data;
  197. ret = 0;
  198. }
  199. else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
  200. addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
  201. addr == PT_SAR+4) {
  202. /* Zero the top 32 bits */
  203. *(unsigned int *) ((char *) task_regs(child) + addr - 4) = 0;
  204. *(unsigned int *) ((char *) task_regs(child) + addr) = data;
  205. ret = 0;
  206. }
  207. goto out_tsk;
  208. }
  209. else
  210. #endif
  211. {
  212. if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
  213. goto out_tsk;
  214. if ((addr >= PT_GR1 && addr <= PT_GR31) ||
  215. addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
  216. (addr >= PT_FR0 && addr <= PT_FR31 + 4) ||
  217. addr == PT_SAR) {
  218. *(unsigned long *) ((char *) task_regs(child) + addr) = data;
  219. ret = 0;
  220. }
  221. goto out_tsk;
  222. }
  223. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  224. case PTRACE_CONT:
  225. ret = -EIO;
  226. DBG("sys_ptrace(%s)\n",
  227. request == PTRACE_SYSCALL ? "SYSCALL" : "CONT");
  228. if (!valid_signal(data))
  229. goto out_tsk;
  230. child->ptrace &= ~(PT_SINGLESTEP|PT_BLOCKSTEP);
  231. if (request == PTRACE_SYSCALL) {
  232. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  233. } else {
  234. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  235. }
  236. child->exit_code = data;
  237. goto out_wake_notrap;
  238. case PTRACE_KILL:
  239. /*
  240. * make the child exit. Best I can do is send it a
  241. * sigkill. perhaps it should be put in the status
  242. * that it wants to exit.
  243. */
  244. ret = 0;
  245. DBG("sys_ptrace(KILL)\n");
  246. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  247. goto out_tsk;
  248. child->exit_code = SIGKILL;
  249. goto out_wake_notrap;
  250. case PTRACE_SINGLEBLOCK:
  251. DBG("sys_ptrace(SINGLEBLOCK)\n");
  252. ret = -EIO;
  253. if (!valid_signal(data))
  254. goto out_tsk;
  255. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  256. child->ptrace &= ~PT_SINGLESTEP;
  257. child->ptrace |= PT_BLOCKSTEP;
  258. child->exit_code = data;
  259. /* Enable taken branch trap. */
  260. pa_psw(child)->r = 0;
  261. pa_psw(child)->t = 1;
  262. pa_psw(child)->h = 0;
  263. pa_psw(child)->l = 0;
  264. goto out_wake;
  265. case PTRACE_SINGLESTEP:
  266. DBG("sys_ptrace(SINGLESTEP)\n");
  267. ret = -EIO;
  268. if (!valid_signal(data))
  269. goto out_tsk;
  270. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  271. child->ptrace &= ~PT_BLOCKSTEP;
  272. child->ptrace |= PT_SINGLESTEP;
  273. child->exit_code = data;
  274. if (pa_psw(child)->n) {
  275. struct siginfo si;
  276. /* Nullified, just crank over the queue. */
  277. task_regs(child)->iaoq[0] = task_regs(child)->iaoq[1];
  278. task_regs(child)->iasq[0] = task_regs(child)->iasq[1];
  279. task_regs(child)->iaoq[1] = task_regs(child)->iaoq[0] + 4;
  280. pa_psw(child)->n = 0;
  281. pa_psw(child)->x = 0;
  282. pa_psw(child)->y = 0;
  283. pa_psw(child)->z = 0;
  284. pa_psw(child)->b = 0;
  285. ptrace_disable(child);
  286. /* Don't wake up the child, but let the
  287. parent know something happened. */
  288. si.si_code = TRAP_TRACE;
  289. si.si_addr = (void __user *) (task_regs(child)->iaoq[0] & ~3);
  290. si.si_signo = SIGTRAP;
  291. si.si_errno = 0;
  292. force_sig_info(SIGTRAP, &si, child);
  293. //notify_parent(child, SIGCHLD);
  294. //ret = 0;
  295. goto out_wake;
  296. }
  297. /* Enable recovery counter traps. The recovery counter
  298. * itself will be set to zero on a task switch. If the
  299. * task is suspended on a syscall then the syscall return
  300. * path will overwrite the recovery counter with a suitable
  301. * value such that it traps once back in user space. We
  302. * disable interrupts in the childs PSW here also, to avoid
  303. * interrupts while the recovery counter is decrementing.
  304. */
  305. pa_psw(child)->r = 1;
  306. pa_psw(child)->t = 0;
  307. pa_psw(child)->h = 0;
  308. pa_psw(child)->l = 0;
  309. /* give it a chance to run. */
  310. goto out_wake;
  311. case PTRACE_DETACH:
  312. ret = ptrace_detach(child, data);
  313. goto out_tsk;
  314. case PTRACE_GETEVENTMSG:
  315. ret = put_user(child->ptrace_message, (unsigned int __user *) data);
  316. goto out_tsk;
  317. default:
  318. ret = ptrace_request(child, request, addr, data);
  319. goto out_tsk;
  320. }
  321. out_wake_notrap:
  322. ptrace_disable(child);
  323. out_wake:
  324. wake_up_process(child);
  325. ret = 0;
  326. out_tsk:
  327. DBG("arch_ptrace(%ld, %d, %lx, %lx) returning %ld\n",
  328. request, pid, oaddr, odata, ret);
  329. return ret;
  330. }
  331. void syscall_trace(void)
  332. {
  333. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  334. return;
  335. if (!(current->ptrace & PT_PTRACED))
  336. return;
  337. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  338. ? 0x80 : 0));
  339. /*
  340. * this isn't the same as continuing with a signal, but it will do
  341. * for normal use. strace only continues with a signal if the
  342. * stopping signal is not SIGTRAP. -brl
  343. */
  344. if (current->exit_code) {
  345. send_sig(current->exit_code, current, 1);
  346. current->exit_code = 0;
  347. }
  348. }