ptrace.c 8.0 KB

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