ptrace.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * linux/arch/sh/kernel/ptrace.c
  3. *
  4. * Original x86 implementation:
  5. * By Ross Biro 1/23/92
  6. * edited by Linus Torvalds
  7. *
  8. * SuperH version: Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
  9. *
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/errno.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/user.h>
  18. #include <linux/slab.h>
  19. #include <linux/security.h>
  20. #include <linux/signal.h>
  21. #include <linux/io.h>
  22. #include <asm/uaccess.h>
  23. #include <asm/pgtable.h>
  24. #include <asm/system.h>
  25. #include <asm/processor.h>
  26. #include <asm/mmu_context.h>
  27. /*
  28. * does not yet catch signals sent when the child dies.
  29. * in exit.c or in signal.c.
  30. */
  31. /*
  32. * This routine will get a word off of the process kernel stack.
  33. */
  34. static inline int get_stack_long(struct task_struct *task, int offset)
  35. {
  36. unsigned char *stack;
  37. stack = (unsigned char *)task_pt_regs(task);
  38. stack += offset;
  39. return (*((int *)stack));
  40. }
  41. /*
  42. * This routine will put a word on the process kernel stack.
  43. */
  44. static inline int put_stack_long(struct task_struct *task, int offset,
  45. unsigned long data)
  46. {
  47. unsigned char *stack;
  48. stack = (unsigned char *)task_pt_regs(task);
  49. stack += offset;
  50. *(unsigned long *) stack = data;
  51. return 0;
  52. }
  53. static void ptrace_disable_singlestep(struct task_struct *child)
  54. {
  55. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  56. /*
  57. * Ensure the UBC is not programmed at the next context switch.
  58. *
  59. * Normally this is not needed but there are sequences such as
  60. * singlestep, signal delivery, and continue that leave the
  61. * ubc_pc non-zero leading to spurious SIGTRAPs.
  62. */
  63. if (child->thread.ubc_pc != 0) {
  64. ubc_usercnt -= 1;
  65. child->thread.ubc_pc = 0;
  66. }
  67. }
  68. /*
  69. * Called by kernel/ptrace.c when detaching..
  70. *
  71. * Make sure single step bits etc are not set.
  72. */
  73. void ptrace_disable(struct task_struct *child)
  74. {
  75. ptrace_disable_singlestep(child);
  76. }
  77. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  78. {
  79. struct user * dummy = NULL;
  80. int ret;
  81. switch (request) {
  82. /* when I and D space are separate, these will need to be fixed. */
  83. case PTRACE_PEEKTEXT: /* read word at location addr. */
  84. case PTRACE_PEEKDATA: {
  85. unsigned long tmp;
  86. int copied;
  87. copied = access_process_vm(child, addr, &tmp, sizeof(tmp), 0);
  88. ret = -EIO;
  89. if (copied != sizeof(tmp))
  90. break;
  91. ret = put_user(tmp,(unsigned long *) data);
  92. break;
  93. }
  94. /* read the word at location addr in the USER area. */
  95. case PTRACE_PEEKUSR: {
  96. unsigned long tmp;
  97. ret = -EIO;
  98. if ((addr & 3) || addr < 0 ||
  99. addr > sizeof(struct user) - 3)
  100. break;
  101. if (addr < sizeof(struct pt_regs))
  102. tmp = get_stack_long(child, addr);
  103. else if (addr >= (long) &dummy->fpu &&
  104. addr < (long) &dummy->u_fpvalid) {
  105. if (!tsk_used_math(child)) {
  106. if (addr == (long)&dummy->fpu.fpscr)
  107. tmp = FPSCR_INIT;
  108. else
  109. tmp = 0;
  110. } else
  111. tmp = ((long *)&child->thread.fpu)
  112. [(addr - (long)&dummy->fpu) >> 2];
  113. } else if (addr == (long) &dummy->u_fpvalid)
  114. tmp = !!tsk_used_math(child);
  115. else
  116. tmp = 0;
  117. ret = put_user(tmp, (unsigned long *)data);
  118. break;
  119. }
  120. /* when I and D space are separate, this will have to be fixed. */
  121. case PTRACE_POKETEXT: /* write the word at location addr. */
  122. case PTRACE_POKEDATA:
  123. ret = 0;
  124. if (access_process_vm(child, addr, &data, sizeof(data), 1) == sizeof(data))
  125. break;
  126. ret = -EIO;
  127. break;
  128. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  129. ret = -EIO;
  130. if ((addr & 3) || addr < 0 ||
  131. addr > sizeof(struct user) - 3)
  132. break;
  133. if (addr < sizeof(struct pt_regs))
  134. ret = put_stack_long(child, addr, data);
  135. else if (addr >= (long) &dummy->fpu &&
  136. addr < (long) &dummy->u_fpvalid) {
  137. set_stopped_child_used_math(child);
  138. ((long *)&child->thread.fpu)
  139. [(addr - (long)&dummy->fpu) >> 2] = data;
  140. ret = 0;
  141. } else if (addr == (long) &dummy->u_fpvalid) {
  142. conditional_stopped_child_used_math(data, child);
  143. ret = 0;
  144. }
  145. break;
  146. case PTRACE_SYSCALL: /* continue and stop at next (return from) syscall */
  147. case PTRACE_CONT: { /* restart after signal. */
  148. ret = -EIO;
  149. if (!valid_signal(data))
  150. break;
  151. if (request == PTRACE_SYSCALL)
  152. set_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  153. else
  154. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  155. ptrace_disable_singlestep(child);
  156. child->exit_code = data;
  157. wake_up_process(child);
  158. ret = 0;
  159. break;
  160. }
  161. /*
  162. * make the child exit. Best I can do is send it a sigkill.
  163. * perhaps it should be put in the status that it wants to
  164. * exit.
  165. */
  166. case PTRACE_KILL: {
  167. ret = 0;
  168. if (child->exit_state == EXIT_ZOMBIE) /* already dead */
  169. break;
  170. ptrace_disable_singlestep(child);
  171. child->exit_code = SIGKILL;
  172. wake_up_process(child);
  173. break;
  174. }
  175. case PTRACE_SINGLESTEP: { /* set the trap flag. */
  176. long pc;
  177. struct pt_regs *dummy = NULL;
  178. ret = -EIO;
  179. if (!valid_signal(data))
  180. break;
  181. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  182. if ((child->ptrace & PT_DTRACE) == 0) {
  183. /* Spurious delayed TF traps may occur */
  184. child->ptrace |= PT_DTRACE;
  185. }
  186. pc = get_stack_long(child, (long)&dummy->pc);
  187. /* Next scheduling will set up UBC */
  188. if (child->thread.ubc_pc == 0)
  189. ubc_usercnt += 1;
  190. child->thread.ubc_pc = pc;
  191. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  192. child->exit_code = data;
  193. /* give it a chance to run. */
  194. wake_up_process(child);
  195. ret = 0;
  196. break;
  197. }
  198. case PTRACE_DETACH: /* detach a process that was attached. */
  199. ret = ptrace_detach(child, data);
  200. break;
  201. #ifdef CONFIG_SH_DSP
  202. case PTRACE_GETDSPREGS: {
  203. unsigned long dp;
  204. ret = -EIO;
  205. dp = ((unsigned long) child) + THREAD_SIZE -
  206. sizeof(struct pt_dspregs);
  207. if (*((int *) (dp - 4)) == SR_FD) {
  208. copy_to_user(addr, (void *) dp,
  209. sizeof(struct pt_dspregs));
  210. ret = 0;
  211. }
  212. break;
  213. }
  214. case PTRACE_SETDSPREGS: {
  215. unsigned long dp;
  216. ret = -EIO;
  217. dp = ((unsigned long) child) + THREAD_SIZE -
  218. sizeof(struct pt_dspregs);
  219. if (*((int *) (dp - 4)) == SR_FD) {
  220. copy_from_user((void *) dp, addr,
  221. sizeof(struct pt_dspregs));
  222. ret = 0;
  223. }
  224. break;
  225. }
  226. #endif
  227. default:
  228. ret = ptrace_request(child, request, addr, data);
  229. break;
  230. }
  231. return ret;
  232. }
  233. asmlinkage void do_syscall_trace(void)
  234. {
  235. struct task_struct *tsk = current;
  236. if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
  237. !test_thread_flag(TIF_SINGLESTEP))
  238. return;
  239. if (!(tsk->ptrace & PT_PTRACED))
  240. return;
  241. /* the 0x80 provides a way for the tracing parent to distinguish
  242. between a syscall stop and SIGTRAP delivery */
  243. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
  244. !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
  245. /*
  246. * this isn't the same as continuing with a signal, but it will do
  247. * for normal use. strace only continues with a signal if the
  248. * stopping signal is not SIGTRAP. -brl
  249. */
  250. if (tsk->exit_code) {
  251. send_sig(tsk->exit_code, tsk, 1);
  252. tsk->exit_code = 0;
  253. }
  254. }