ptrace_32.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. void user_enable_single_step(struct task_struct *child)
  56. {
  57. struct pt_regs *regs = task_pt_regs(child);
  58. long pc;
  59. pc = get_stack_long(child, (long)&regs->pc);
  60. /* Next scheduling will set up UBC */
  61. if (child->thread.ubc_pc == 0)
  62. ubc_usercnt += 1;
  63. child->thread.ubc_pc = pc;
  64. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  65. }
  66. void user_disable_single_step(struct task_struct *child)
  67. {
  68. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  69. /*
  70. * Ensure the UBC is not programmed at the next context switch.
  71. *
  72. * Normally this is not needed but there are sequences such as
  73. * singlestep, signal delivery, and continue that leave the
  74. * ubc_pc non-zero leading to spurious SIGTRAPs.
  75. */
  76. if (child->thread.ubc_pc != 0) {
  77. ubc_usercnt -= 1;
  78. child->thread.ubc_pc = 0;
  79. }
  80. }
  81. /*
  82. * Called by kernel/ptrace.c when detaching..
  83. *
  84. * Make sure single step bits etc are not set.
  85. */
  86. void ptrace_disable(struct task_struct *child)
  87. {
  88. user_disable_single_step(child);
  89. }
  90. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  91. {
  92. struct user * dummy = NULL;
  93. int ret;
  94. switch (request) {
  95. /* read the word at location addr in the USER area. */
  96. case PTRACE_PEEKUSR: {
  97. unsigned long tmp;
  98. ret = -EIO;
  99. if ((addr & 3) || addr < 0 ||
  100. addr > sizeof(struct user) - 3)
  101. break;
  102. if (addr < sizeof(struct pt_regs))
  103. tmp = get_stack_long(child, addr);
  104. else if (addr >= (long) &dummy->fpu &&
  105. addr < (long) &dummy->u_fpvalid) {
  106. if (!tsk_used_math(child)) {
  107. if (addr == (long)&dummy->fpu.fpscr)
  108. tmp = FPSCR_INIT;
  109. else
  110. tmp = 0;
  111. } else
  112. tmp = ((long *)&child->thread.fpu)
  113. [(addr - (long)&dummy->fpu) >> 2];
  114. } else if (addr == (long) &dummy->u_fpvalid)
  115. tmp = !!tsk_used_math(child);
  116. else
  117. tmp = 0;
  118. ret = put_user(tmp, (unsigned long __user *)data);
  119. break;
  120. }
  121. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  122. ret = -EIO;
  123. if ((addr & 3) || addr < 0 ||
  124. addr > sizeof(struct user) - 3)
  125. break;
  126. if (addr < sizeof(struct pt_regs))
  127. ret = put_stack_long(child, addr, data);
  128. else if (addr >= (long) &dummy->fpu &&
  129. addr < (long) &dummy->u_fpvalid) {
  130. set_stopped_child_used_math(child);
  131. ((long *)&child->thread.fpu)
  132. [(addr - (long)&dummy->fpu) >> 2] = data;
  133. ret = 0;
  134. } else if (addr == (long) &dummy->u_fpvalid) {
  135. conditional_stopped_child_used_math(data, child);
  136. ret = 0;
  137. }
  138. break;
  139. #ifdef CONFIG_SH_DSP
  140. case PTRACE_GETDSPREGS: {
  141. unsigned long dp;
  142. ret = -EIO;
  143. dp = ((unsigned long) child) + THREAD_SIZE -
  144. sizeof(struct pt_dspregs);
  145. if (*((int *) (dp - 4)) == SR_FD) {
  146. copy_to_user((void *)addr, (void *) dp,
  147. sizeof(struct pt_dspregs));
  148. ret = 0;
  149. }
  150. break;
  151. }
  152. case PTRACE_SETDSPREGS: {
  153. unsigned long dp;
  154. ret = -EIO;
  155. dp = ((unsigned long) child) + THREAD_SIZE -
  156. sizeof(struct pt_dspregs);
  157. if (*((int *) (dp - 4)) == SR_FD) {
  158. copy_from_user((void *) dp, (void *)addr,
  159. sizeof(struct pt_dspregs));
  160. ret = 0;
  161. }
  162. break;
  163. }
  164. #endif
  165. #ifdef CONFIG_BINFMT_ELF_FDPIC
  166. case PTRACE_GETFDPIC: {
  167. unsigned long tmp = 0;
  168. switch (addr) {
  169. case PTRACE_GETFDPIC_EXEC:
  170. tmp = child->mm->context.exec_fdpic_loadmap;
  171. break;
  172. case PTRACE_GETFDPIC_INTERP:
  173. tmp = child->mm->context.interp_fdpic_loadmap;
  174. break;
  175. default:
  176. break;
  177. }
  178. ret = 0;
  179. if (put_user(tmp, (unsigned long *) data)) {
  180. ret = -EFAULT;
  181. break;
  182. }
  183. break;
  184. }
  185. #endif
  186. default:
  187. ret = ptrace_request(child, request, addr, data);
  188. break;
  189. }
  190. return ret;
  191. }
  192. asmlinkage void do_syscall_trace(struct pt_regs *regs, int entryexit)
  193. {
  194. struct task_struct *tsk = current;
  195. secure_computing(regs->regs[0]);
  196. if (unlikely(current->audit_context) && entryexit)
  197. audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
  198. regs->regs[0]);
  199. if (!test_thread_flag(TIF_SYSCALL_TRACE) &&
  200. !test_thread_flag(TIF_SINGLESTEP))
  201. goto out;
  202. if (!(tsk->ptrace & PT_PTRACED))
  203. goto out;
  204. /* the 0x80 provides a way for the tracing parent to distinguish
  205. between a syscall stop and SIGTRAP delivery */
  206. ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD) &&
  207. !test_thread_flag(TIF_SINGLESTEP) ? 0x80 : 0));
  208. /*
  209. * this isn't the same as continuing with a signal, but it will do
  210. * for normal use. strace only continues with a signal if the
  211. * stopping signal is not SIGTRAP. -brl
  212. */
  213. if (tsk->exit_code) {
  214. send_sig(tsk->exit_code, tsk, 1);
  215. tsk->exit_code = 0;
  216. }
  217. out:
  218. if (unlikely(current->audit_context) && !entryexit)
  219. audit_syscall_entry(AUDIT_ARCH_SH, regs->regs[3],
  220. regs->regs[4], regs->regs[5],
  221. regs->regs[6], regs->regs[7]);
  222. }