ptrace.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. #ifdef CONFIG_64BIT
  80. if (__is_compat_task(child)) {
  81. int copied;
  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. ret = generic_ptrace_peekdata(child, addr, data);
  96. goto out_tsk;
  97. }
  98. /* when I and D space are separate, this will have to be fixed. */
  99. case PTRACE_POKETEXT: /* write the word at location addr. */
  100. case PTRACE_POKEDATA:
  101. ret = 0;
  102. #ifdef CONFIG_64BIT
  103. if (__is_compat_task(child)) {
  104. unsigned int tmp = (unsigned int)data;
  105. DBG("sys_ptrace(POKE%s, %d, %lx, %lx)\n",
  106. request == PTRACE_POKETEXT ? "TEXT" : "DATA",
  107. pid, oaddr, odata);
  108. addr &= 0xffffffffL;
  109. if (access_process_vm(child, addr, &tmp, sizeof(tmp), 1) == sizeof(tmp))
  110. goto out_tsk;
  111. }
  112. else
  113. #endif
  114. {
  115. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  116. goto out_tsk;
  117. }
  118. ret = -EIO;
  119. goto out_tsk;
  120. /* Read the word at location addr in the USER area. For ptraced
  121. processes, the kernel saves all regs on a syscall. */
  122. case PTRACE_PEEKUSR: {
  123. ret = -EIO;
  124. #ifdef CONFIG_64BIT
  125. if (__is_compat_task(child)) {
  126. unsigned int tmp;
  127. if (addr & (sizeof(int)-1))
  128. goto out_tsk;
  129. if ((addr = translate_usr_offset(addr)) < 0)
  130. goto out_tsk;
  131. tmp = *(unsigned int *) ((char *) task_regs(child) + addr);
  132. ret = put_user(tmp, (unsigned int *) data);
  133. DBG("sys_ptrace(PEEKUSR, %d, %lx, %lx) returning %ld, addr %lx, data %x\n",
  134. pid, oaddr, odata, ret, addr, tmp);
  135. }
  136. else
  137. #endif
  138. {
  139. unsigned long tmp;
  140. if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
  141. goto out_tsk;
  142. tmp = *(unsigned long *) ((char *) task_regs(child) + addr);
  143. ret = put_user(tmp, (unsigned long *) data);
  144. }
  145. goto out_tsk;
  146. }
  147. /* Write the word at location addr in the USER area. This will need
  148. to change when the kernel no longer saves all regs on a syscall.
  149. FIXME. There is a problem at the moment in that r3-r18 are only
  150. saved if the process is ptraced on syscall entry, and even then
  151. those values are overwritten by actual register values on syscall
  152. exit. */
  153. case PTRACE_POKEUSR:
  154. ret = -EIO;
  155. /* Some register values written here may be ignored in
  156. * entry.S:syscall_restore_rfi; e.g. iaoq is written with
  157. * r31/r31+4, and not with the values in pt_regs.
  158. */
  159. /* PT_PSW=0, so this is valid for 32 bit processes under 64
  160. * bit kernels.
  161. */
  162. if (addr == PT_PSW) {
  163. /* PT_PSW=0, so this is valid for 32 bit processes
  164. * under 64 bit kernels.
  165. *
  166. * Allow writing to Nullify, Divide-step-correction,
  167. * and carry/borrow bits.
  168. * BEWARE, if you set N, and then single step, it won't
  169. * stop on the nullified instruction.
  170. */
  171. DBG("sys_ptrace(POKEUSR, %d, %lx, %lx)\n",
  172. pid, oaddr, odata);
  173. data &= USER_PSW_BITS;
  174. task_regs(child)->gr[0] &= ~USER_PSW_BITS;
  175. task_regs(child)->gr[0] |= data;
  176. ret = 0;
  177. goto out_tsk;
  178. }
  179. #ifdef CONFIG_64BIT
  180. if (__is_compat_task(child)) {
  181. if (addr & (sizeof(int)-1))
  182. goto out_tsk;
  183. if ((addr = translate_usr_offset(addr)) < 0)
  184. goto out_tsk;
  185. DBG("sys_ptrace(POKEUSR, %d, %lx, %lx) addr %lx\n",
  186. pid, oaddr, odata, addr);
  187. if (addr >= PT_FR0 && addr <= PT_FR31 + 4) {
  188. /* Special case, fp regs are 64 bits anyway */
  189. *(unsigned int *) ((char *) task_regs(child) + addr) = data;
  190. ret = 0;
  191. }
  192. else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
  193. addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
  194. addr == PT_SAR+4) {
  195. /* Zero the top 32 bits */
  196. *(unsigned int *) ((char *) task_regs(child) + addr - 4) = 0;
  197. *(unsigned int *) ((char *) task_regs(child) + addr) = data;
  198. ret = 0;
  199. }
  200. goto out_tsk;
  201. }
  202. else
  203. #endif
  204. {
  205. if ((addr & (sizeof(long)-1)) || (unsigned long) addr >= sizeof(struct pt_regs))
  206. goto out_tsk;
  207. if ((addr >= PT_GR1 && addr <= PT_GR31) ||
  208. addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
  209. (addr >= PT_FR0 && addr <= PT_FR31 + 4) ||
  210. addr == PT_SAR) {
  211. *(unsigned long *) ((char *) task_regs(child) + addr) = data;
  212. ret = 0;
  213. }
  214. goto out_tsk;
  215. }
  216. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  217. case PTRACE_CONT:
  218. ret = -EIO;
  219. DBG("sys_ptrace(%s)\n",
  220. request == PTRACE_SYSCALL ? "SYSCALL" : "CONT");
  221. if (!valid_signal(data))
  222. goto out_tsk;
  223. child->ptrace &= ~(PT_SINGLESTEP|PT_BLOCKSTEP);
  224. if (request == PTRACE_SYSCALL) {
  225. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  226. } else {
  227. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  228. }
  229. child->exit_code = data;
  230. goto out_wake_notrap;
  231. case PTRACE_KILL:
  232. /*
  233. * make the child exit. Best I can do is send it a
  234. * sigkill. perhaps it should be put in the status
  235. * that it wants to exit.
  236. */
  237. ret = 0;
  238. DBG("sys_ptrace(KILL)\n");
  239. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  240. goto out_tsk;
  241. child->exit_code = SIGKILL;
  242. goto out_wake_notrap;
  243. case PTRACE_SINGLEBLOCK:
  244. DBG("sys_ptrace(SINGLEBLOCK)\n");
  245. ret = -EIO;
  246. if (!valid_signal(data))
  247. goto out_tsk;
  248. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  249. child->ptrace &= ~PT_SINGLESTEP;
  250. child->ptrace |= PT_BLOCKSTEP;
  251. child->exit_code = data;
  252. /* Enable taken branch trap. */
  253. pa_psw(child)->r = 0;
  254. pa_psw(child)->t = 1;
  255. pa_psw(child)->h = 0;
  256. pa_psw(child)->l = 0;
  257. goto out_wake;
  258. case PTRACE_SINGLESTEP:
  259. DBG("sys_ptrace(SINGLESTEP)\n");
  260. ret = -EIO;
  261. if (!valid_signal(data))
  262. goto out_tsk;
  263. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  264. child->ptrace &= ~PT_BLOCKSTEP;
  265. child->ptrace |= PT_SINGLESTEP;
  266. child->exit_code = data;
  267. if (pa_psw(child)->n) {
  268. struct siginfo si;
  269. /* Nullified, just crank over the queue. */
  270. task_regs(child)->iaoq[0] = task_regs(child)->iaoq[1];
  271. task_regs(child)->iasq[0] = task_regs(child)->iasq[1];
  272. task_regs(child)->iaoq[1] = task_regs(child)->iaoq[0] + 4;
  273. pa_psw(child)->n = 0;
  274. pa_psw(child)->x = 0;
  275. pa_psw(child)->y = 0;
  276. pa_psw(child)->z = 0;
  277. pa_psw(child)->b = 0;
  278. ptrace_disable(child);
  279. /* Don't wake up the child, but let the
  280. parent know something happened. */
  281. si.si_code = TRAP_TRACE;
  282. si.si_addr = (void __user *) (task_regs(child)->iaoq[0] & ~3);
  283. si.si_signo = SIGTRAP;
  284. si.si_errno = 0;
  285. force_sig_info(SIGTRAP, &si, child);
  286. //notify_parent(child, SIGCHLD);
  287. //ret = 0;
  288. goto out_wake;
  289. }
  290. /* Enable recovery counter traps. The recovery counter
  291. * itself will be set to zero on a task switch. If the
  292. * task is suspended on a syscall then the syscall return
  293. * path will overwrite the recovery counter with a suitable
  294. * value such that it traps once back in user space. We
  295. * disable interrupts in the childs PSW here also, to avoid
  296. * interrupts while the recovery counter is decrementing.
  297. */
  298. pa_psw(child)->r = 1;
  299. pa_psw(child)->t = 0;
  300. pa_psw(child)->h = 0;
  301. pa_psw(child)->l = 0;
  302. /* give it a chance to run. */
  303. goto out_wake;
  304. case PTRACE_GETEVENTMSG:
  305. ret = put_user(child->ptrace_message, (unsigned int __user *) data);
  306. goto out_tsk;
  307. default:
  308. ret = ptrace_request(child, request, addr, data);
  309. goto out_tsk;
  310. }
  311. out_wake_notrap:
  312. ptrace_disable(child);
  313. out_wake:
  314. wake_up_process(child);
  315. ret = 0;
  316. out_tsk:
  317. DBG("arch_ptrace(%ld, %d, %lx, %lx) returning %ld\n",
  318. request, pid, oaddr, odata, ret);
  319. return ret;
  320. }
  321. void syscall_trace(void)
  322. {
  323. if (!test_thread_flag(TIF_SYSCALL_TRACE))
  324. return;
  325. if (!(current->ptrace & PT_PTRACED))
  326. return;
  327. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
  328. ? 0x80 : 0));
  329. /*
  330. * this isn't the same as continuing with a signal, but it will do
  331. * for normal use. strace only continues with a signal if the
  332. * stopping signal is not SIGTRAP. -brl
  333. */
  334. if (current->exit_code) {
  335. send_sig(current->exit_code, current, 1);
  336. current->exit_code = 0;
  337. }
  338. }