ptrace_32.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. #ifdef CONFIG_SH_DSP
  132. static int dspregs_get(struct task_struct *target,
  133. const struct user_regset *regset,
  134. unsigned int pos, unsigned int count,
  135. void *kbuf, void __user *ubuf)
  136. {
  137. const struct pt_dspregs *regs = task_pt_dspregs(target);
  138. int ret;
  139. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
  140. 0, sizeof(struct pt_dspregs));
  141. if (!ret)
  142. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  143. sizeof(struct pt_dspregs), -1);
  144. return ret;
  145. }
  146. static int dspregs_set(struct task_struct *target,
  147. const struct user_regset *regset,
  148. unsigned int pos, unsigned int count,
  149. const void *kbuf, const void __user *ubuf)
  150. {
  151. struct pt_dspregs *regs = task_pt_dspregs(target);
  152. int ret;
  153. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
  154. 0, sizeof(struct pt_dspregs));
  155. if (!ret)
  156. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  157. sizeof(struct pt_dspregs), -1);
  158. return ret;
  159. }
  160. #endif
  161. /*
  162. * These are our native regset flavours.
  163. */
  164. enum sh_regset {
  165. REGSET_GENERAL,
  166. #ifdef CONFIG_SH_DSP
  167. REGSET_DSP,
  168. #endif
  169. };
  170. static const struct user_regset sh_regsets[] = {
  171. /*
  172. * Format is:
  173. * R0 --> R15
  174. * PC, PR, SR, GBR, MACH, MACL, TRA
  175. */
  176. [REGSET_GENERAL] = {
  177. .core_note_type = NT_PRSTATUS,
  178. .n = ELF_NGREG,
  179. .size = sizeof(long),
  180. .align = sizeof(long),
  181. .get = genregs_get,
  182. .set = genregs_set,
  183. },
  184. #ifdef CONFIG_SH_DSP
  185. [REGSET_DSP] = {
  186. .n = sizeof(struct pt_dspregs) / sizeof(long),
  187. .size = sizeof(long),
  188. .align = sizeof(long),
  189. .get = dspregs_get,
  190. .set = dspregs_set,
  191. },
  192. #endif
  193. };
  194. static const struct user_regset_view user_sh_native_view = {
  195. .name = "sh",
  196. .e_machine = EM_SH,
  197. .regsets = sh_regsets,
  198. .n = ARRAY_SIZE(sh_regsets),
  199. };
  200. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  201. {
  202. return &user_sh_native_view;
  203. }
  204. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  205. {
  206. struct user * dummy = NULL;
  207. unsigned long __user *datap = (unsigned long __user *)data;
  208. int ret;
  209. switch (request) {
  210. /* read the word at location addr in the USER area. */
  211. case PTRACE_PEEKUSR: {
  212. unsigned long tmp;
  213. ret = -EIO;
  214. if ((addr & 3) || addr < 0 ||
  215. addr > sizeof(struct user) - 3)
  216. break;
  217. if (addr < sizeof(struct pt_regs))
  218. tmp = get_stack_long(child, addr);
  219. else if (addr >= (long) &dummy->fpu &&
  220. addr < (long) &dummy->u_fpvalid) {
  221. if (!tsk_used_math(child)) {
  222. if (addr == (long)&dummy->fpu.fpscr)
  223. tmp = FPSCR_INIT;
  224. else
  225. tmp = 0;
  226. } else
  227. tmp = ((long *)&child->thread.fpu)
  228. [(addr - (long)&dummy->fpu) >> 2];
  229. } else if (addr == (long) &dummy->u_fpvalid)
  230. tmp = !!tsk_used_math(child);
  231. else
  232. tmp = 0;
  233. ret = put_user(tmp, datap);
  234. break;
  235. }
  236. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  237. ret = -EIO;
  238. if ((addr & 3) || addr < 0 ||
  239. addr > sizeof(struct user) - 3)
  240. break;
  241. if (addr < sizeof(struct pt_regs))
  242. ret = put_stack_long(child, addr, data);
  243. else if (addr >= (long) &dummy->fpu &&
  244. addr < (long) &dummy->u_fpvalid) {
  245. set_stopped_child_used_math(child);
  246. ((long *)&child->thread.fpu)
  247. [(addr - (long)&dummy->fpu) >> 2] = data;
  248. ret = 0;
  249. } else if (addr == (long) &dummy->u_fpvalid) {
  250. conditional_stopped_child_used_math(data, child);
  251. ret = 0;
  252. }
  253. break;
  254. case PTRACE_GETREGS:
  255. return copy_regset_to_user(child, &user_sh_native_view,
  256. REGSET_GENERAL,
  257. 0, sizeof(struct pt_regs),
  258. (void __user *)data);
  259. case PTRACE_SETREGS:
  260. return copy_regset_from_user(child, &user_sh_native_view,
  261. REGSET_GENERAL,
  262. 0, sizeof(struct pt_regs),
  263. (const void __user *)data);
  264. #ifdef CONFIG_SH_DSP
  265. case PTRACE_GETDSPREGS:
  266. return copy_regset_to_user(child, &user_sh_native_view,
  267. REGSET_DSP,
  268. 0, sizeof(struct pt_dspregs),
  269. (void __user *)data);
  270. case PTRACE_SETDSPREGS:
  271. return copy_regset_from_user(child, &user_sh_native_view,
  272. REGSET_DSP,
  273. 0, sizeof(struct pt_dspregs),
  274. (const void __user *)data);
  275. #endif
  276. #ifdef CONFIG_BINFMT_ELF_FDPIC
  277. case PTRACE_GETFDPIC: {
  278. unsigned long tmp = 0;
  279. switch (addr) {
  280. case PTRACE_GETFDPIC_EXEC:
  281. tmp = child->mm->context.exec_fdpic_loadmap;
  282. break;
  283. case PTRACE_GETFDPIC_INTERP:
  284. tmp = child->mm->context.interp_fdpic_loadmap;
  285. break;
  286. default:
  287. break;
  288. }
  289. ret = 0;
  290. if (put_user(tmp, datap)) {
  291. ret = -EFAULT;
  292. break;
  293. }
  294. break;
  295. }
  296. #endif
  297. default:
  298. ret = ptrace_request(child, request, addr, data);
  299. break;
  300. }
  301. return ret;
  302. }
  303. static inline int audit_arch(void)
  304. {
  305. int arch = EM_SH;
  306. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  307. arch |= __AUDIT_ARCH_LE;
  308. #endif
  309. return arch;
  310. }
  311. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  312. {
  313. long ret = 0;
  314. secure_computing(regs->regs[0]);
  315. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  316. tracehook_report_syscall_entry(regs))
  317. /*
  318. * Tracing decided this syscall should not happen.
  319. * We'll return a bogus call number to get an ENOSYS
  320. * error, but leave the original number in regs->regs[0].
  321. */
  322. ret = -1L;
  323. if (unlikely(current->audit_context))
  324. audit_syscall_entry(audit_arch(), regs->regs[3],
  325. regs->regs[4], regs->regs[5],
  326. regs->regs[6], regs->regs[7]);
  327. return ret ?: regs->regs[0];
  328. }
  329. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  330. {
  331. int step;
  332. if (unlikely(current->audit_context))
  333. audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
  334. regs->regs[0]);
  335. step = test_thread_flag(TIF_SINGLESTEP);
  336. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  337. tracehook_report_syscall_exit(regs, step);
  338. }