ptrace_32.c 5.9 KB

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