ptrace.c 11 KB

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