ptrace.c 11 KB

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