ptrace.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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/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. /*
  28. * Called by kernel/ptrace.c when detaching..
  29. *
  30. * Make sure single step bits etc are not set.
  31. */
  32. void ptrace_disable(struct task_struct *task)
  33. {
  34. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  35. clear_tsk_thread_flag(task, TIF_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. clear_tsk_thread_flag(task, TIF_BLOCKSTEP);
  53. set_tsk_thread_flag(task, TIF_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. clear_tsk_thread_flag(task, TIF_SINGLESTEP);
  92. set_tsk_thread_flag(task, TIF_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,
  100. unsigned long addr, unsigned long data)
  101. {
  102. unsigned long tmp;
  103. long ret = -EIO;
  104. switch (request) {
  105. /* Read the word at location addr in the USER area. For ptraced
  106. processes, the kernel saves all regs on a syscall. */
  107. case PTRACE_PEEKUSR:
  108. if ((addr & (sizeof(unsigned long)-1)) ||
  109. addr >= sizeof(struct pt_regs))
  110. break;
  111. tmp = *(unsigned long *) ((char *) task_regs(child) + addr);
  112. ret = put_user(tmp, (unsigned long __user *) data);
  113. break;
  114. /* Write the word at location addr in the USER area. This will need
  115. to change when the kernel no longer saves all regs on a syscall.
  116. FIXME. There is a problem at the moment in that r3-r18 are only
  117. saved if the process is ptraced on syscall entry, and even then
  118. those values are overwritten by actual register values on syscall
  119. exit. */
  120. case PTRACE_POKEUSR:
  121. /* Some register values written here may be ignored in
  122. * entry.S:syscall_restore_rfi; e.g. iaoq is written with
  123. * r31/r31+4, and not with the values in pt_regs.
  124. */
  125. if (addr == PT_PSW) {
  126. /* Allow writing to Nullify, Divide-step-correction,
  127. * and carry/borrow bits.
  128. * BEWARE, if you set N, and then single step, it won't
  129. * stop on the nullified instruction.
  130. */
  131. data &= USER_PSW_BITS;
  132. task_regs(child)->gr[0] &= ~USER_PSW_BITS;
  133. task_regs(child)->gr[0] |= data;
  134. ret = 0;
  135. break;
  136. }
  137. if ((addr & (sizeof(unsigned long)-1)) ||
  138. addr >= sizeof(struct pt_regs))
  139. break;
  140. if ((addr >= PT_GR1 && addr <= PT_GR31) ||
  141. addr == PT_IAOQ0 || addr == PT_IAOQ1 ||
  142. (addr >= PT_FR0 && addr <= PT_FR31 + 4) ||
  143. addr == PT_SAR) {
  144. *(unsigned long *) ((char *) task_regs(child) + addr) = data;
  145. ret = 0;
  146. }
  147. break;
  148. default:
  149. ret = ptrace_request(child, request, addr, data);
  150. break;
  151. }
  152. return ret;
  153. }
  154. #ifdef CONFIG_COMPAT
  155. /* This function is needed to translate 32 bit pt_regs offsets in to
  156. * 64 bit pt_regs offsets. For example, a 32 bit gdb under a 64 bit kernel
  157. * will request offset 12 if it wants gr3, but the lower 32 bits of
  158. * the 64 bit kernels view of gr3 will be at offset 28 (3*8 + 4).
  159. * This code relies on a 32 bit pt_regs being comprised of 32 bit values
  160. * except for the fp registers which (a) are 64 bits, and (b) follow
  161. * the gr registers at the start of pt_regs. The 32 bit pt_regs should
  162. * be half the size of the 64 bit pt_regs, plus 32*4 to allow for fr[]
  163. * being 64 bit in both cases.
  164. */
  165. static compat_ulong_t translate_usr_offset(compat_ulong_t offset)
  166. {
  167. if (offset < 0)
  168. return sizeof(struct pt_regs);
  169. else if (offset <= 32*4) /* gr[0..31] */
  170. return offset * 2 + 4;
  171. else if (offset <= 32*4+32*8) /* gr[0..31] + fr[0..31] */
  172. return offset + 32*4;
  173. else if (offset < sizeof(struct pt_regs)/2 + 32*4)
  174. return offset * 2 + 4 - 32*8;
  175. else
  176. return sizeof(struct pt_regs);
  177. }
  178. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  179. compat_ulong_t addr, compat_ulong_t data)
  180. {
  181. compat_uint_t tmp;
  182. long ret = -EIO;
  183. switch (request) {
  184. case PTRACE_PEEKUSR:
  185. if (addr & (sizeof(compat_uint_t)-1))
  186. break;
  187. addr = translate_usr_offset(addr);
  188. if (addr >= sizeof(struct pt_regs))
  189. break;
  190. tmp = *(compat_uint_t *) ((char *) task_regs(child) + addr);
  191. ret = put_user(tmp, (compat_uint_t *) (unsigned long) data);
  192. break;
  193. /* Write the word at location addr in the USER area. This will need
  194. to change when the kernel no longer saves all regs on a syscall.
  195. FIXME. There is a problem at the moment in that r3-r18 are only
  196. saved if the process is ptraced on syscall entry, and even then
  197. those values are overwritten by actual register values on syscall
  198. exit. */
  199. case PTRACE_POKEUSR:
  200. /* Some register values written here may be ignored in
  201. * entry.S:syscall_restore_rfi; e.g. iaoq is written with
  202. * r31/r31+4, and not with the values in pt_regs.
  203. */
  204. if (addr == PT_PSW) {
  205. /* Since PT_PSW==0, it is valid for 32 bit processes
  206. * under 64 bit kernels as well.
  207. */
  208. ret = arch_ptrace(child, request, addr, data);
  209. } else {
  210. if (addr & (sizeof(compat_uint_t)-1))
  211. break;
  212. addr = translate_usr_offset(addr);
  213. if (addr >= sizeof(struct pt_regs))
  214. break;
  215. if (addr >= PT_FR0 && addr <= PT_FR31 + 4) {
  216. /* Special case, fp regs are 64 bits anyway */
  217. *(__u64 *) ((char *) task_regs(child) + addr) = data;
  218. ret = 0;
  219. }
  220. else if ((addr >= PT_GR1+4 && addr <= PT_GR31+4) ||
  221. addr == PT_IAOQ0+4 || addr == PT_IAOQ1+4 ||
  222. addr == PT_SAR+4) {
  223. /* Zero the top 32 bits */
  224. *(__u32 *) ((char *) task_regs(child) + addr - 4) = 0;
  225. *(__u32 *) ((char *) task_regs(child) + addr) = data;
  226. ret = 0;
  227. }
  228. }
  229. break;
  230. default:
  231. ret = compat_ptrace_request(child, request, addr, data);
  232. break;
  233. }
  234. return ret;
  235. }
  236. #endif
  237. long do_syscall_trace_enter(struct pt_regs *regs)
  238. {
  239. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  240. tracehook_report_syscall_entry(regs))
  241. return -1L;
  242. return regs->gr[20];
  243. }
  244. void do_syscall_trace_exit(struct pt_regs *regs)
  245. {
  246. int stepping = test_thread_flag(TIF_SINGLESTEP) ||
  247. test_thread_flag(TIF_BLOCKSTEP);
  248. if (stepping || test_thread_flag(TIF_SYSCALL_TRACE))
  249. tracehook_report_syscall_exit(regs, stepping);
  250. }