ptrace.c 6.4 KB

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