ptrace_32.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <linux/tracehook.h>
  25. #include <asm/uaccess.h>
  26. #include <asm/pgtable.h>
  27. #include <asm/system.h>
  28. #include <asm/processor.h>
  29. #include <asm/mmu_context.h>
  30. /*
  31. * does not yet catch signals sent when the child dies.
  32. * in exit.c or in signal.c.
  33. */
  34. /*
  35. * This routine will get a word off of the process kernel stack.
  36. */
  37. static inline int get_stack_long(struct task_struct *task, int offset)
  38. {
  39. unsigned char *stack;
  40. stack = (unsigned char *)task_pt_regs(task);
  41. stack += offset;
  42. return (*((int *)stack));
  43. }
  44. /*
  45. * This routine will put a word on the process kernel stack.
  46. */
  47. static inline int put_stack_long(struct task_struct *task, int offset,
  48. unsigned long data)
  49. {
  50. unsigned char *stack;
  51. stack = (unsigned char *)task_pt_regs(task);
  52. stack += offset;
  53. *(unsigned long *) stack = data;
  54. return 0;
  55. }
  56. void user_enable_single_step(struct task_struct *child)
  57. {
  58. struct pt_regs *regs = task_pt_regs(child);
  59. long pc;
  60. pc = get_stack_long(child, (long)&regs->pc);
  61. /* Next scheduling will set up UBC */
  62. if (child->thread.ubc_pc == 0)
  63. ubc_usercnt += 1;
  64. child->thread.ubc_pc = pc;
  65. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  66. }
  67. void user_disable_single_step(struct task_struct *child)
  68. {
  69. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  70. /*
  71. * Ensure the UBC is not programmed at the next context switch.
  72. *
  73. * Normally this is not needed but there are sequences such as
  74. * singlestep, signal delivery, and continue that leave the
  75. * ubc_pc non-zero leading to spurious SIGTRAPs.
  76. */
  77. if (child->thread.ubc_pc != 0) {
  78. ubc_usercnt -= 1;
  79. child->thread.ubc_pc = 0;
  80. }
  81. }
  82. /*
  83. * Called by kernel/ptrace.c when detaching..
  84. *
  85. * Make sure single step bits etc are not set.
  86. */
  87. void ptrace_disable(struct task_struct *child)
  88. {
  89. user_disable_single_step(child);
  90. }
  91. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  92. {
  93. struct user * dummy = NULL;
  94. int ret;
  95. switch (request) {
  96. /* read the word at location addr in the USER area. */
  97. case PTRACE_PEEKUSR: {
  98. unsigned long tmp;
  99. ret = -EIO;
  100. if ((addr & 3) || addr < 0 ||
  101. addr > sizeof(struct user) - 3)
  102. break;
  103. if (addr < sizeof(struct pt_regs))
  104. tmp = get_stack_long(child, addr);
  105. else if (addr >= (long) &dummy->fpu &&
  106. addr < (long) &dummy->u_fpvalid) {
  107. if (!tsk_used_math(child)) {
  108. if (addr == (long)&dummy->fpu.fpscr)
  109. tmp = FPSCR_INIT;
  110. else
  111. tmp = 0;
  112. } else
  113. tmp = ((long *)&child->thread.fpu)
  114. [(addr - (long)&dummy->fpu) >> 2];
  115. } else if (addr == (long) &dummy->u_fpvalid)
  116. tmp = !!tsk_used_math(child);
  117. else
  118. tmp = 0;
  119. ret = put_user(tmp, (unsigned long __user *)data);
  120. break;
  121. }
  122. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  123. ret = -EIO;
  124. if ((addr & 3) || addr < 0 ||
  125. addr > sizeof(struct user) - 3)
  126. break;
  127. if (addr < sizeof(struct pt_regs))
  128. ret = put_stack_long(child, addr, data);
  129. else if (addr >= (long) &dummy->fpu &&
  130. addr < (long) &dummy->u_fpvalid) {
  131. set_stopped_child_used_math(child);
  132. ((long *)&child->thread.fpu)
  133. [(addr - (long)&dummy->fpu) >> 2] = data;
  134. ret = 0;
  135. } else if (addr == (long) &dummy->u_fpvalid) {
  136. conditional_stopped_child_used_math(data, child);
  137. ret = 0;
  138. }
  139. break;
  140. #ifdef CONFIG_SH_DSP
  141. case PTRACE_GETDSPREGS: {
  142. unsigned long dp;
  143. ret = -EIO;
  144. dp = ((unsigned long) child) + THREAD_SIZE -
  145. sizeof(struct pt_dspregs);
  146. if (*((int *) (dp - 4)) == SR_FD) {
  147. copy_to_user((void *)addr, (void *) dp,
  148. sizeof(struct pt_dspregs));
  149. ret = 0;
  150. }
  151. break;
  152. }
  153. case PTRACE_SETDSPREGS: {
  154. unsigned long dp;
  155. ret = -EIO;
  156. dp = ((unsigned long) child) + THREAD_SIZE -
  157. sizeof(struct pt_dspregs);
  158. if (*((int *) (dp - 4)) == SR_FD) {
  159. copy_from_user((void *) dp, (void *)addr,
  160. sizeof(struct pt_dspregs));
  161. ret = 0;
  162. }
  163. break;
  164. }
  165. #endif
  166. #ifdef CONFIG_BINFMT_ELF_FDPIC
  167. case PTRACE_GETFDPIC: {
  168. unsigned long tmp = 0;
  169. switch (addr) {
  170. case PTRACE_GETFDPIC_EXEC:
  171. tmp = child->mm->context.exec_fdpic_loadmap;
  172. break;
  173. case PTRACE_GETFDPIC_INTERP:
  174. tmp = child->mm->context.interp_fdpic_loadmap;
  175. break;
  176. default:
  177. break;
  178. }
  179. ret = 0;
  180. if (put_user(tmp, (unsigned long *) data)) {
  181. ret = -EFAULT;
  182. break;
  183. }
  184. break;
  185. }
  186. #endif
  187. default:
  188. ret = ptrace_request(child, request, addr, data);
  189. break;
  190. }
  191. return ret;
  192. }
  193. static inline int audit_arch(void)
  194. {
  195. int arch = EM_SH;
  196. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  197. arch |= __AUDIT_ARCH_LE;
  198. #endif
  199. return arch;
  200. }
  201. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  202. {
  203. long ret = 0;
  204. secure_computing(regs->regs[0]);
  205. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  206. tracehook_report_syscall_entry(regs))
  207. /*
  208. * Tracing decided this syscall should not happen.
  209. * We'll return a bogus call number to get an ENOSYS
  210. * error, but leave the original number in regs->regs[0].
  211. */
  212. ret = -1L;
  213. if (unlikely(current->audit_context))
  214. audit_syscall_entry(audit_arch(), regs->regs[3],
  215. regs->regs[4], regs->regs[5],
  216. regs->regs[6], regs->regs[7]);
  217. return ret ?: regs->regs[0];
  218. }
  219. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  220. {
  221. int step;
  222. if (unlikely(current->audit_context))
  223. audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
  224. regs->regs[0]);
  225. step = test_thread_flag(TIF_SINGLESTEP);
  226. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  227. tracehook_report_syscall_exit(regs, step);
  228. }