ptrace_32.c 6.9 KB

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