ptrace.c 11 KB

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