ptrace_32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. #include <asm/fpu.h>
  36. /*
  37. * This routine will get a word off of the process kernel stack.
  38. */
  39. static inline int get_stack_long(struct task_struct *task, int offset)
  40. {
  41. unsigned char *stack;
  42. stack = (unsigned char *)task_pt_regs(task);
  43. stack += offset;
  44. return (*((int *)stack));
  45. }
  46. /*
  47. * This routine will put a word on the process kernel stack.
  48. */
  49. static inline int put_stack_long(struct task_struct *task, int offset,
  50. unsigned long data)
  51. {
  52. unsigned char *stack;
  53. stack = (unsigned char *)task_pt_regs(task);
  54. stack += offset;
  55. *(unsigned long *) stack = data;
  56. return 0;
  57. }
  58. void user_enable_single_step(struct task_struct *child)
  59. {
  60. /* Next scheduling will set up UBC */
  61. if (child->thread.ubc_pc == 0)
  62. ubc_usercnt += 1;
  63. child->thread.ubc_pc = get_stack_long(child,
  64. offsetof(struct pt_regs, 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. static int genregs_get(struct task_struct *target,
  92. const struct user_regset *regset,
  93. unsigned int pos, unsigned int count,
  94. void *kbuf, void __user *ubuf)
  95. {
  96. const struct pt_regs *regs = task_pt_regs(target);
  97. int ret;
  98. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  99. regs->regs,
  100. 0, 16 * sizeof(unsigned long));
  101. if (!ret)
  102. /* PC, PR, SR, GBR, MACH, MACL, TRA */
  103. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  104. &regs->pc,
  105. offsetof(struct pt_regs, pc),
  106. sizeof(struct pt_regs));
  107. if (!ret)
  108. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  109. sizeof(struct pt_regs), -1);
  110. return ret;
  111. }
  112. static int genregs_set(struct task_struct *target,
  113. const struct user_regset *regset,
  114. unsigned int pos, unsigned int count,
  115. const void *kbuf, const void __user *ubuf)
  116. {
  117. struct pt_regs *regs = task_pt_regs(target);
  118. int ret;
  119. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  120. regs->regs,
  121. 0, 16 * sizeof(unsigned long));
  122. if (!ret && count > 0)
  123. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  124. &regs->pc,
  125. offsetof(struct pt_regs, pc),
  126. sizeof(struct pt_regs));
  127. if (!ret)
  128. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  129. sizeof(struct pt_regs), -1);
  130. return ret;
  131. }
  132. #ifdef CONFIG_SH_FPU
  133. int fpregs_get(struct task_struct *target,
  134. const struct user_regset *regset,
  135. unsigned int pos, unsigned int count,
  136. void *kbuf, void __user *ubuf)
  137. {
  138. int ret;
  139. ret = init_fpu(target);
  140. if (ret)
  141. return ret;
  142. if ((boot_cpu_data.flags & CPU_HAS_FPU))
  143. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  144. &target->thread.fpu.hard, 0, -1);
  145. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  146. &target->thread.fpu.soft, 0, -1);
  147. }
  148. static int fpregs_set(struct task_struct *target,
  149. const struct user_regset *regset,
  150. unsigned int pos, unsigned int count,
  151. const void *kbuf, const void __user *ubuf)
  152. {
  153. int ret;
  154. ret = init_fpu(target);
  155. if (ret)
  156. return ret;
  157. set_stopped_child_used_math(target);
  158. if ((boot_cpu_data.flags & CPU_HAS_FPU))
  159. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  160. &target->thread.fpu.hard, 0, -1);
  161. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  162. &target->thread.fpu.soft, 0, -1);
  163. }
  164. static int fpregs_active(struct task_struct *target,
  165. const struct user_regset *regset)
  166. {
  167. return tsk_used_math(target) ? regset->n : 0;
  168. }
  169. #endif
  170. #ifdef CONFIG_SH_DSP
  171. static int dspregs_get(struct task_struct *target,
  172. const struct user_regset *regset,
  173. unsigned int pos, unsigned int count,
  174. void *kbuf, void __user *ubuf)
  175. {
  176. const struct pt_dspregs *regs = task_pt_dspregs(target);
  177. int ret;
  178. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
  179. 0, sizeof(struct pt_dspregs));
  180. if (!ret)
  181. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  182. sizeof(struct pt_dspregs), -1);
  183. return ret;
  184. }
  185. static int dspregs_set(struct task_struct *target,
  186. const struct user_regset *regset,
  187. unsigned int pos, unsigned int count,
  188. const void *kbuf, const void __user *ubuf)
  189. {
  190. struct pt_dspregs *regs = task_pt_dspregs(target);
  191. int ret;
  192. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
  193. 0, sizeof(struct pt_dspregs));
  194. if (!ret)
  195. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  196. sizeof(struct pt_dspregs), -1);
  197. return ret;
  198. }
  199. static int dspregs_active(struct task_struct *target,
  200. const struct user_regset *regset)
  201. {
  202. struct pt_regs *regs = task_pt_regs(target);
  203. return regs->sr & SR_DSP ? regset->n : 0;
  204. }
  205. #endif
  206. /*
  207. * These are our native regset flavours.
  208. */
  209. enum sh_regset {
  210. REGSET_GENERAL,
  211. #ifdef CONFIG_SH_FPU
  212. REGSET_FPU,
  213. #endif
  214. #ifdef CONFIG_SH_DSP
  215. REGSET_DSP,
  216. #endif
  217. };
  218. static const struct user_regset sh_regsets[] = {
  219. /*
  220. * Format is:
  221. * R0 --> R15
  222. * PC, PR, SR, GBR, MACH, MACL, TRA
  223. */
  224. [REGSET_GENERAL] = {
  225. .core_note_type = NT_PRSTATUS,
  226. .n = ELF_NGREG,
  227. .size = sizeof(long),
  228. .align = sizeof(long),
  229. .get = genregs_get,
  230. .set = genregs_set,
  231. },
  232. #ifdef CONFIG_SH_FPU
  233. [REGSET_FPU] = {
  234. .core_note_type = NT_PRFPREG,
  235. .n = sizeof(struct user_fpu_struct) / sizeof(long),
  236. .size = sizeof(long),
  237. .align = sizeof(long),
  238. .get = fpregs_get,
  239. .set = fpregs_set,
  240. .active = fpregs_active,
  241. },
  242. #endif
  243. #ifdef CONFIG_SH_DSP
  244. [REGSET_DSP] = {
  245. .n = sizeof(struct pt_dspregs) / sizeof(long),
  246. .size = sizeof(long),
  247. .align = sizeof(long),
  248. .get = dspregs_get,
  249. .set = dspregs_set,
  250. .active = dspregs_active,
  251. },
  252. #endif
  253. };
  254. static const struct user_regset_view user_sh_native_view = {
  255. .name = "sh",
  256. .e_machine = EM_SH,
  257. .regsets = sh_regsets,
  258. .n = ARRAY_SIZE(sh_regsets),
  259. };
  260. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  261. {
  262. return &user_sh_native_view;
  263. }
  264. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  265. {
  266. struct user * dummy = NULL;
  267. unsigned long __user *datap = (unsigned long __user *)data;
  268. int ret;
  269. switch (request) {
  270. /* read the word at location addr in the USER area. */
  271. case PTRACE_PEEKUSR: {
  272. unsigned long tmp;
  273. ret = -EIO;
  274. if ((addr & 3) || addr < 0 ||
  275. addr > sizeof(struct user) - 3)
  276. break;
  277. if (addr < sizeof(struct pt_regs))
  278. tmp = get_stack_long(child, addr);
  279. else if (addr >= (long) &dummy->fpu &&
  280. addr < (long) &dummy->u_fpvalid) {
  281. if (!tsk_used_math(child)) {
  282. if (addr == (long)&dummy->fpu.fpscr)
  283. tmp = FPSCR_INIT;
  284. else
  285. tmp = 0;
  286. } else
  287. tmp = ((long *)&child->thread.fpu)
  288. [(addr - (long)&dummy->fpu) >> 2];
  289. } else if (addr == (long) &dummy->u_fpvalid)
  290. tmp = !!tsk_used_math(child);
  291. else
  292. tmp = 0;
  293. ret = put_user(tmp, datap);
  294. break;
  295. }
  296. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  297. ret = -EIO;
  298. if ((addr & 3) || addr < 0 ||
  299. addr > sizeof(struct user) - 3)
  300. break;
  301. if (addr < sizeof(struct pt_regs))
  302. ret = put_stack_long(child, addr, data);
  303. else if (addr >= (long) &dummy->fpu &&
  304. addr < (long) &dummy->u_fpvalid) {
  305. set_stopped_child_used_math(child);
  306. ((long *)&child->thread.fpu)
  307. [(addr - (long)&dummy->fpu) >> 2] = data;
  308. ret = 0;
  309. } else if (addr == (long) &dummy->u_fpvalid) {
  310. conditional_stopped_child_used_math(data, child);
  311. ret = 0;
  312. }
  313. break;
  314. case PTRACE_GETREGS:
  315. return copy_regset_to_user(child, &user_sh_native_view,
  316. REGSET_GENERAL,
  317. 0, sizeof(struct pt_regs),
  318. (void __user *)data);
  319. case PTRACE_SETREGS:
  320. return copy_regset_from_user(child, &user_sh_native_view,
  321. REGSET_GENERAL,
  322. 0, sizeof(struct pt_regs),
  323. (const void __user *)data);
  324. #ifdef CONFIG_SH_FPU
  325. case PTRACE_GETFPREGS:
  326. return copy_regset_to_user(child, &user_sh_native_view,
  327. REGSET_FPU,
  328. 0, sizeof(struct user_fpu_struct),
  329. (void __user *)data);
  330. case PTRACE_SETFPREGS:
  331. return copy_regset_from_user(child, &user_sh_native_view,
  332. REGSET_FPU,
  333. 0, sizeof(struct user_fpu_struct),
  334. (const void __user *)data);
  335. #endif
  336. #ifdef CONFIG_SH_DSP
  337. case PTRACE_GETDSPREGS:
  338. return copy_regset_to_user(child, &user_sh_native_view,
  339. REGSET_DSP,
  340. 0, sizeof(struct pt_dspregs),
  341. (void __user *)data);
  342. case PTRACE_SETDSPREGS:
  343. return copy_regset_from_user(child, &user_sh_native_view,
  344. REGSET_DSP,
  345. 0, sizeof(struct pt_dspregs),
  346. (const void __user *)data);
  347. #endif
  348. #ifdef CONFIG_BINFMT_ELF_FDPIC
  349. case PTRACE_GETFDPIC: {
  350. unsigned long tmp = 0;
  351. switch (addr) {
  352. case PTRACE_GETFDPIC_EXEC:
  353. tmp = child->mm->context.exec_fdpic_loadmap;
  354. break;
  355. case PTRACE_GETFDPIC_INTERP:
  356. tmp = child->mm->context.interp_fdpic_loadmap;
  357. break;
  358. default:
  359. break;
  360. }
  361. ret = 0;
  362. if (put_user(tmp, datap)) {
  363. ret = -EFAULT;
  364. break;
  365. }
  366. break;
  367. }
  368. #endif
  369. default:
  370. ret = ptrace_request(child, request, addr, data);
  371. break;
  372. }
  373. return ret;
  374. }
  375. static inline int audit_arch(void)
  376. {
  377. int arch = EM_SH;
  378. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  379. arch |= __AUDIT_ARCH_LE;
  380. #endif
  381. return arch;
  382. }
  383. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  384. {
  385. long ret = 0;
  386. secure_computing(regs->regs[0]);
  387. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  388. tracehook_report_syscall_entry(regs))
  389. /*
  390. * Tracing decided this syscall should not happen.
  391. * We'll return a bogus call number to get an ENOSYS
  392. * error, but leave the original number in regs->regs[0].
  393. */
  394. ret = -1L;
  395. if (unlikely(current->audit_context))
  396. audit_syscall_entry(audit_arch(), regs->regs[3],
  397. regs->regs[4], regs->regs[5],
  398. regs->regs[6], regs->regs[7]);
  399. return ret ?: regs->regs[0];
  400. }
  401. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  402. {
  403. int step;
  404. if (unlikely(current->audit_context))
  405. audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
  406. regs->regs[0]);
  407. step = test_thread_flag(TIF_SINGLESTEP);
  408. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  409. tracehook_report_syscall_exit(regs, step);
  410. }