ptrace_32.c 7.4 KB

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