ptrace_32.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * SuperH process tracing
  3. *
  4. * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
  5. * Copyright (C) 2002 - 2008 Paul Mundt
  6. *
  7. * Audit support by Yuichi Nakamura <ynakam@hitachisoft.jp>
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/sched.h>
  15. #include <linux/mm.h>
  16. #include <linux/smp.h>
  17. #include <linux/errno.h>
  18. #include <linux/ptrace.h>
  19. #include <linux/user.h>
  20. #include <linux/slab.h>
  21. #include <linux/security.h>
  22. #include <linux/signal.h>
  23. #include <linux/io.h>
  24. #include <linux/audit.h>
  25. #include <linux/seccomp.h>
  26. #include <linux/tracehook.h>
  27. #include <linux/elf.h>
  28. #include <linux/regset.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/system.h>
  32. #include <asm/processor.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/syscalls.h>
  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. /* Next scheduling will set up UBC */
  60. if (child->thread.ubc_pc == 0)
  61. ubc_usercnt += 1;
  62. child->thread.ubc_pc = get_stack_long(child,
  63. offsetof(struct pt_regs, 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. static int genregs_get(struct task_struct *target,
  91. const struct user_regset *regset,
  92. unsigned int pos, unsigned int count,
  93. void *kbuf, void __user *ubuf)
  94. {
  95. const struct pt_regs *regs = task_pt_regs(target);
  96. int ret;
  97. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  98. regs->regs,
  99. 0, 16 * sizeof(unsigned long));
  100. if (!ret)
  101. /* PC, PR, SR, GBR, MACH, MACL, TRA */
  102. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  103. &regs->pc,
  104. offsetof(struct pt_regs, pc),
  105. sizeof(struct pt_regs));
  106. if (!ret)
  107. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  108. sizeof(struct pt_regs), -1);
  109. return ret;
  110. }
  111. static int genregs_set(struct task_struct *target,
  112. const struct user_regset *regset,
  113. unsigned int pos, unsigned int count,
  114. const void *kbuf, const void __user *ubuf)
  115. {
  116. struct pt_regs *regs = task_pt_regs(target);
  117. int ret;
  118. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  119. regs->regs,
  120. 0, 16 * sizeof(unsigned long));
  121. if (!ret && count > 0)
  122. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  123. &regs->pc,
  124. offsetof(struct pt_regs, pc),
  125. sizeof(struct pt_regs));
  126. if (!ret)
  127. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  128. sizeof(struct pt_regs), -1);
  129. return ret;
  130. }
  131. /*
  132. * These are our native regset flavours.
  133. */
  134. enum sh_regset {
  135. REGSET_GENERAL,
  136. };
  137. static const struct user_regset sh_regsets[] = {
  138. /*
  139. * Format is:
  140. * R0 --> R15
  141. * PC, PR, SR, GBR, MACH, MACL, TRA
  142. */
  143. [REGSET_GENERAL] = {
  144. .core_note_type = NT_PRSTATUS,
  145. .n = ELF_NGREG,
  146. .size = sizeof(long),
  147. .align = sizeof(long),
  148. .get = genregs_get,
  149. .set = genregs_set,
  150. },
  151. };
  152. static const struct user_regset_view user_sh_native_view = {
  153. .name = "sh",
  154. .e_machine = EM_SH,
  155. .regsets = sh_regsets,
  156. .n = ARRAY_SIZE(sh_regsets),
  157. };
  158. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  159. {
  160. struct user * dummy = NULL;
  161. unsigned long __user *datap = (unsigned long __user *)data;
  162. int ret;
  163. switch (request) {
  164. /* read the word at location addr in the USER area. */
  165. case PTRACE_PEEKUSR: {
  166. unsigned long tmp;
  167. ret = -EIO;
  168. if ((addr & 3) || addr < 0 ||
  169. addr > sizeof(struct user) - 3)
  170. break;
  171. if (addr < sizeof(struct pt_regs))
  172. tmp = get_stack_long(child, addr);
  173. else if (addr >= (long) &dummy->fpu &&
  174. addr < (long) &dummy->u_fpvalid) {
  175. if (!tsk_used_math(child)) {
  176. if (addr == (long)&dummy->fpu.fpscr)
  177. tmp = FPSCR_INIT;
  178. else
  179. tmp = 0;
  180. } else
  181. tmp = ((long *)&child->thread.fpu)
  182. [(addr - (long)&dummy->fpu) >> 2];
  183. } else if (addr == (long) &dummy->u_fpvalid)
  184. tmp = !!tsk_used_math(child);
  185. else
  186. tmp = 0;
  187. ret = put_user(tmp, datap);
  188. break;
  189. }
  190. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  191. ret = -EIO;
  192. if ((addr & 3) || addr < 0 ||
  193. addr > sizeof(struct user) - 3)
  194. break;
  195. if (addr < sizeof(struct pt_regs))
  196. ret = put_stack_long(child, addr, data);
  197. else if (addr >= (long) &dummy->fpu &&
  198. addr < (long) &dummy->u_fpvalid) {
  199. set_stopped_child_used_math(child);
  200. ((long *)&child->thread.fpu)
  201. [(addr - (long)&dummy->fpu) >> 2] = data;
  202. ret = 0;
  203. } else if (addr == (long) &dummy->u_fpvalid) {
  204. conditional_stopped_child_used_math(data, child);
  205. ret = 0;
  206. }
  207. break;
  208. case PTRACE_GETREGS:
  209. return copy_regset_to_user(child, &user_sh_native_view,
  210. REGSET_GENERAL,
  211. 0, sizeof(struct pt_regs),
  212. (void __user *)data);
  213. case PTRACE_SETREGS:
  214. return copy_regset_from_user(child, &user_sh_native_view,
  215. REGSET_GENERAL,
  216. 0, sizeof(struct pt_regs),
  217. (const void __user *)data);
  218. #ifdef CONFIG_SH_DSP
  219. case PTRACE_GETDSPREGS: {
  220. unsigned long dp;
  221. ret = -EIO;
  222. dp = ((unsigned long) child) + THREAD_SIZE -
  223. sizeof(struct pt_dspregs);
  224. if (*((int *) (dp - 4)) == SR_FD) {
  225. copy_to_user((void *)addr, (void *) dp,
  226. sizeof(struct pt_dspregs));
  227. ret = 0;
  228. }
  229. break;
  230. }
  231. case PTRACE_SETDSPREGS: {
  232. unsigned long dp;
  233. ret = -EIO;
  234. dp = ((unsigned long) child) + THREAD_SIZE -
  235. sizeof(struct pt_dspregs);
  236. if (*((int *) (dp - 4)) == SR_FD) {
  237. copy_from_user((void *) dp, (void *)addr,
  238. sizeof(struct pt_dspregs));
  239. ret = 0;
  240. }
  241. break;
  242. }
  243. #endif
  244. #ifdef CONFIG_BINFMT_ELF_FDPIC
  245. case PTRACE_GETFDPIC: {
  246. unsigned long tmp = 0;
  247. switch (addr) {
  248. case PTRACE_GETFDPIC_EXEC:
  249. tmp = child->mm->context.exec_fdpic_loadmap;
  250. break;
  251. case PTRACE_GETFDPIC_INTERP:
  252. tmp = child->mm->context.interp_fdpic_loadmap;
  253. break;
  254. default:
  255. break;
  256. }
  257. ret = 0;
  258. if (put_user(tmp, datap)) {
  259. ret = -EFAULT;
  260. break;
  261. }
  262. break;
  263. }
  264. #endif
  265. default:
  266. ret = ptrace_request(child, request, addr, data);
  267. break;
  268. }
  269. return ret;
  270. }
  271. static inline int audit_arch(void)
  272. {
  273. int arch = EM_SH;
  274. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  275. arch |= __AUDIT_ARCH_LE;
  276. #endif
  277. return arch;
  278. }
  279. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  280. {
  281. long ret = 0;
  282. secure_computing(regs->regs[0]);
  283. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  284. tracehook_report_syscall_entry(regs))
  285. /*
  286. * Tracing decided this syscall should not happen.
  287. * We'll return a bogus call number to get an ENOSYS
  288. * error, but leave the original number in regs->regs[0].
  289. */
  290. ret = -1L;
  291. if (unlikely(current->audit_context))
  292. audit_syscall_entry(audit_arch(), regs->regs[3],
  293. regs->regs[4], regs->regs[5],
  294. regs->regs[6], regs->regs[7]);
  295. return ret ?: regs->regs[0];
  296. }
  297. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  298. {
  299. int step;
  300. if (unlikely(current->audit_context))
  301. audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
  302. regs->regs[0]);
  303. step = test_thread_flag(TIF_SINGLESTEP);
  304. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  305. tracehook_report_syscall_exit(regs, step);
  306. }