ptrace.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. * Copyright (C) 2008 Helge Deller <deller@gmx.de>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/smp.h>
  13. #include <linux/errno.h>
  14. #include <linux/ptrace.h>
  15. #include <linux/tracehook.h>
  16. #include <linux/user.h>
  17. #include <linux/personality.h>
  18. #include <linux/security.h>
  19. #include <linux/seccomp.h>
  20. #include <linux/compat.h>
  21. #include <linux/signal.h>
  22. #include <linux/audit.h>
  23. #include <asm/uaccess.h>
  24. #include <asm/pgtable.h>
  25. #include <asm/processor.h>
  26. #include <asm/asm-offsets.h>
  27. /* PSW bits we allow the debugger to modify */
  28. #define USER_PSW_BITS (PSW_N | PSW_B | PSW_V | PSW_CB)
  29. /*
  30. * Called by kernel/ptrace.c when detaching..
  31. *
  32. * Make sure single step bits etc are not set.
  33. */
  34. void ptrace_disable(struct task_struct *task)
  35. {
  36. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  37. clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
  38. /* make sure the trap bits are not set */
  39. pa_psw(task)->r = 0;
  40. pa_psw(task)->t = 0;
  41. pa_psw(task)->h = 0;
  42. pa_psw(task)->l = 0;
  43. }
  44. /*
  45. * The following functions are called by ptrace_resume() when
  46. * enabling or disabling single/block tracing.
  47. */
  48. void user_disable_single_step(struct task_struct *task)
  49. {
  50. ptrace_disable(task);
  51. }
  52. void user_enable_single_step(struct task_struct *task)
  53. {
  54. clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
  55. set_tsk_thread_flag(task, TIF_SINGLESTEP);
  56. if (pa_psw(task)->n) {
  57. struct siginfo si;
  58. /* Nullified, just crank over the queue. */
  59. task_regs(task)->iaoq[0] = task_regs(task)->iaoq[1];
  60. task_regs(task)->iasq[0] = task_regs(task)->iasq[1];
  61. task_regs(task)->iaoq[1] = task_regs(task)->iaoq[0] + 4;
  62. pa_psw(task)->n = 0;
  63. pa_psw(task)->x = 0;
  64. pa_psw(task)->y = 0;
  65. pa_psw(task)->z = 0;
  66. pa_psw(task)->b = 0;
  67. ptrace_disable(task);
  68. /* Don't wake up the task, but let the
  69. parent know something happened. */
  70. si.si_code = TRAP_TRACE;
  71. si.si_addr = (void __user *) (task_regs(task)->iaoq[0] & ~3);
  72. si.si_signo = SIGTRAP;
  73. si.si_errno = 0;
  74. force_sig_info(SIGTRAP, &si, task);
  75. /* notify_parent(task, SIGCHLD); */
  76. return;
  77. }
  78. /* Enable recovery counter traps. The recovery counter
  79. * itself will be set to zero on a task switch. If the
  80. * task is suspended on a syscall then the syscall return
  81. * path will overwrite the recovery counter with a suitable
  82. * value such that it traps once back in user space. We
  83. * disable interrupts in the tasks PSW here also, to avoid
  84. * interrupts while the recovery counter is decrementing.
  85. */
  86. pa_psw(task)->r = 1;
  87. pa_psw(task)->t = 0;
  88. pa_psw(task)->h = 0;
  89. pa_psw(task)->l = 0;
  90. }
  91. void user_enable_block_step(struct task_struct *task)
  92. {
  93. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  94. set_tsk_thread_flag(task, TIF_BLOCKSTEP);
  95. /* Enable taken branch trap. */
  96. pa_psw(task)->r = 0;
  97. pa_psw(task)->t = 1;
  98. pa_psw(task)->h = 0;
  99. pa_psw(task)->l = 0;
  100. }
  101. long arch_ptrace(struct task_struct *child, long request,
  102. unsigned long addr, unsigned long data)
  103. {
  104. unsigned long tmp;
  105. long ret = -EIO;
  106. switch (request) {
  107. /* Read the word at location addr in the USER area. For ptraced
  108. processes, the kernel saves all regs on a syscall. */
  109. case PTRACE_PEEKUSR:
  110. if ((addr & (sizeof(unsigned long)-1)) ||
  111. addr >= sizeof(struct pt_regs))
  112. break;
  113. tmp = *(unsigned long *) ((char *) task_regs(child) + addr);
  114. ret = put_user(tmp, (unsigned long __user *) data);
  115. break;
  116. /* Write the word at location addr in the USER area. This will need
  117. to change when the kernel no longer saves all regs on a syscall.
  118. FIXME. There is a problem at the moment in that r3-r18 are only
  119. saved if the process is ptraced on syscall entry, and even then
  120. those values are overwritten by actual register values on syscall
  121. exit. */
  122. case PTRACE_POKEUSR:
  123. /* Some register values written here may be ignored in
  124. * entry.S:syscall_restore_rfi; e.g. iaoq is written with
  125. * r31/r31+4, and not with the values in pt_regs.
  126. */
  127. if (addr == PT_PSW) {
  128. /* Allow writing to Nullify, Divide-step-correction,
  129. * and carry/borrow bits.
  130. * BEWARE, if you set N, and then single step, it won't
  131. * stop on the nullified instruction.
  132. */
  133. data &= USER_PSW_BITS;
  134. task_regs(child)->gr[0] &= ~USER_PSW_BITS;
  135. task_regs(child)->gr[0] |= data;
  136. ret = 0;
  137. break;
  138. }
  139. if ((addr & (sizeof(unsigned long)-1)) ||
  140. addr >= sizeof(struct pt_regs))
  141. break;
  142. if ((addr >= PT_GR1 && addr <= PT_GR31) ||
  143. addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
  144. (addr >= PT_FR0 && addr <= PT_FR31 + 4) ||
  145. addr == PT_SAR) {
  146. *(unsigned long *) ((char *) task_regs(child) + addr) = data;
  147. ret = 0;
  148. }
  149. break;
  150. default:
  151. ret = ptrace_request(child, request, addr, data);
  152. break;
  153. }
  154. return ret;
  155. }
  156. #ifdef CONFIG_COMPAT
  157. /* This function is needed to translate 32 bit pt_regs offsets in to
  158. * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel
  159. * will request offset 12 if it wants gr3, but the lower 32 bits of
  160. * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4).
  161. * This code relies on a 32 bit pt_regs being comprised of 32 bit values
  162. * except for the fp registers which (a) are 64 bits, and (b) follow
  163. * the gr registers at the start of pt_regs. The 32 bit pt_regs should
  164. * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[]
  165. * being 64 bit in both cases.
  166. */
  167. static compat_ulong_t translate_usr_offset(compat_ulong_t offset)
  168. {
  169. if (offset < 0)
  170. return sizeof(struct pt_regs);
  171. else if (offset <= 32*4) /* gr[0..31] */
  172. return offset * 2 + 4;
  173. else if (offset <= 32*4+32*8) /* gr[0..31] + fr[0..31] */
  174. return offset + 32*4;
  175. else if (offset < sizeof(struct pt_regs)/2 + 32*4)
  176. return offset * 2 + 4 - 32*8;
  177. else
  178. return sizeof(struct pt_regs);
  179. }
  180. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  181. compat_ulong_t addr, compat_ulong_t data)
  182. {
  183. compat_uint_t tmp;
  184. long ret = -EIO;
  185. switch (request) {
  186. case PTRACE_PEEKUSR:
  187. if (addr & (sizeof(compat_uint_t)-1))
  188. break;
  189. addr = translate_usr_offset(addr);
  190. if (addr >= sizeof(struct pt_regs))
  191. break;
  192. tmp = *(compat_uint_t *) ((char *) task_regs(child) + addr);
  193. ret = put_user(tmp, (compat_uint_t *) (unsigned long) data);
  194. break;
  195. /* Write the word at location addr in the USER area. This will need
  196. to change when the kernel no longer saves all regs on a syscall.
  197. FIXME. There is a problem at the moment in that r3-r18 are only
  198. saved if the process is ptraced on syscall entry, and even then
  199. those values are overwritten by actual register values on syscall
  200. exit. */
  201. case PTRACE_POKEUSR:
  202. /* Some register values written here may be ignored in
  203. * entry.S:syscall_restore_rfi; e.g. iaoq is written with
  204. * r31/r31+4, and not with the values in pt_regs.
  205. */
  206. if (addr == PT_PSW) {
  207. /* Since PT_PSW==0, it is valid for 32 bit processes
  208. * under 64 bit kernels as well.
  209. */
  210. ret = arch_ptrace(child, request, addr, data);
  211. } else {
  212. if (addr & (sizeof(compat_uint_t)-1))
  213. break;
  214. addr = translate_usr_offset(addr);
  215. if (addr >= sizeof(struct pt_regs))
  216. break;
  217. if (addr >= PT_FR0 && addr <= PT_FR31 + 4) {
  218. /* Special case, fp regs are 64 bits anyway */
  219. *(__u64 *) ((char *) task_regs(child) + addr) = data;
  220. ret = 0;
  221. }
  222. else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
  223. addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
  224. addr == PT_SAR+4) {
  225. /* Zero the top 32 bits */
  226. *(__u32 *) ((char *) task_regs(child) + addr - 4) = 0;
  227. *(__u32 *) ((char *) task_regs(child) + addr) = data;
  228. ret = 0;
  229. }
  230. }
  231. break;
  232. default:
  233. ret = compat_ptrace_request(child, request, addr, data);
  234. break;
  235. }
  236. return ret;
  237. }
  238. #endif
  239. long do_syscall_trace_enter(struct pt_regs *regs)
  240. {
  241. long ret = 0;
  242. /* Do the secure computing check first. */
  243. secure_computing_strict(regs->gr[20]);
  244. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  245. tracehook_report_syscall_entry(regs))
  246. ret = -1L;
  247. #ifdef CONFIG_64BIT
  248. if (!is_compat_task())
  249. audit_syscall_entry(regs->gr[20], regs->gr[26], regs->gr[25],
  250. regs->gr[24], regs->gr[23]);
  251. else
  252. #endif
  253. audit_syscall_entry(regs->gr[20] & 0xffffffff,
  254. regs->gr[26] & 0xffffffff,
  255. regs->gr[25] & 0xffffffff,
  256. regs->gr[24] & 0xffffffff,
  257. regs->gr[23] & 0xffffffff);
  258. return ret ? : regs->gr[20];
  259. }
  260. void do_syscall_trace_exit(struct pt_regs *regs)
  261. {
  262. int stepping = test_thread_flag(TIF_SINGLESTEP) ||
  263. test_thread_flag(TIF_BLOCKSTEP);
  264. audit_syscall_exit(regs);
  265. if (stepping || test_thread_flag(TIF_SYSCALL_TRACE))
  266. tracehook_report_syscall_exit(regs, stepping);
  267. }