ptrace_32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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 =
  177. (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
  178. int ret;
  179. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
  180. 0, sizeof(struct pt_dspregs));
  181. if (!ret)
  182. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  183. sizeof(struct pt_dspregs), -1);
  184. return ret;
  185. }
  186. static int dspregs_set(struct task_struct *target,
  187. const struct user_regset *regset,
  188. unsigned int pos, unsigned int count,
  189. const void *kbuf, const void __user *ubuf)
  190. {
  191. struct pt_dspregs *regs =
  192. (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
  193. int ret;
  194. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
  195. 0, sizeof(struct pt_dspregs));
  196. if (!ret)
  197. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  198. sizeof(struct pt_dspregs), -1);
  199. return ret;
  200. }
  201. static int dspregs_active(struct task_struct *target,
  202. const struct user_regset *regset)
  203. {
  204. struct pt_regs *regs = task_pt_regs(target);
  205. return regs->sr & SR_DSP ? regset->n : 0;
  206. }
  207. #endif
  208. /*
  209. * These are our native regset flavours.
  210. */
  211. enum sh_regset {
  212. REGSET_GENERAL,
  213. #ifdef CONFIG_SH_FPU
  214. REGSET_FPU,
  215. #endif
  216. #ifdef CONFIG_SH_DSP
  217. REGSET_DSP,
  218. #endif
  219. };
  220. static const struct user_regset sh_regsets[] = {
  221. /*
  222. * Format is:
  223. * R0 --> R15
  224. * PC, PR, SR, GBR, MACH, MACL, TRA
  225. */
  226. [REGSET_GENERAL] = {
  227. .core_note_type = NT_PRSTATUS,
  228. .n = ELF_NGREG,
  229. .size = sizeof(long),
  230. .align = sizeof(long),
  231. .get = genregs_get,
  232. .set = genregs_set,
  233. },
  234. #ifdef CONFIG_SH_FPU
  235. [REGSET_FPU] = {
  236. .core_note_type = NT_PRFPREG,
  237. .n = sizeof(struct user_fpu_struct) / sizeof(long),
  238. .size = sizeof(long),
  239. .align = sizeof(long),
  240. .get = fpregs_get,
  241. .set = fpregs_set,
  242. .active = fpregs_active,
  243. },
  244. #endif
  245. #ifdef CONFIG_SH_DSP
  246. [REGSET_DSP] = {
  247. .n = sizeof(struct pt_dspregs) / sizeof(long),
  248. .size = sizeof(long),
  249. .align = sizeof(long),
  250. .get = dspregs_get,
  251. .set = dspregs_set,
  252. .active = dspregs_active,
  253. },
  254. #endif
  255. };
  256. static const struct user_regset_view user_sh_native_view = {
  257. .name = "sh",
  258. .e_machine = EM_SH,
  259. .regsets = sh_regsets,
  260. .n = ARRAY_SIZE(sh_regsets),
  261. };
  262. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  263. {
  264. return &user_sh_native_view;
  265. }
  266. long arch_ptrace(struct task_struct *child, long request, long addr, long data)
  267. {
  268. struct user * dummy = NULL;
  269. unsigned long __user *datap = (unsigned long __user *)data;
  270. int ret;
  271. switch (request) {
  272. /* read the word at location addr in the USER area. */
  273. case PTRACE_PEEKUSR: {
  274. unsigned long tmp;
  275. ret = -EIO;
  276. if ((addr & 3) || addr < 0 ||
  277. addr > sizeof(struct user) - 3)
  278. break;
  279. if (addr < sizeof(struct pt_regs))
  280. tmp = get_stack_long(child, addr);
  281. else if (addr >= (long) &dummy->fpu &&
  282. addr < (long) &dummy->u_fpvalid) {
  283. if (!tsk_used_math(child)) {
  284. if (addr == (long)&dummy->fpu.fpscr)
  285. tmp = FPSCR_INIT;
  286. else
  287. tmp = 0;
  288. } else
  289. tmp = ((long *)&child->thread.fpu)
  290. [(addr - (long)&dummy->fpu) >> 2];
  291. } else if (addr == (long) &dummy->u_fpvalid)
  292. tmp = !!tsk_used_math(child);
  293. else
  294. tmp = 0;
  295. ret = put_user(tmp, datap);
  296. break;
  297. }
  298. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  299. ret = -EIO;
  300. if ((addr & 3) || addr < 0 ||
  301. addr > sizeof(struct user) - 3)
  302. break;
  303. if (addr < sizeof(struct pt_regs))
  304. ret = put_stack_long(child, addr, data);
  305. else if (addr >= (long) &dummy->fpu &&
  306. addr < (long) &dummy->u_fpvalid) {
  307. set_stopped_child_used_math(child);
  308. ((long *)&child->thread.fpu)
  309. [(addr - (long)&dummy->fpu) >> 2] = data;
  310. ret = 0;
  311. } else if (addr == (long) &dummy->u_fpvalid) {
  312. conditional_stopped_child_used_math(data, child);
  313. ret = 0;
  314. }
  315. break;
  316. case PTRACE_GETREGS:
  317. return copy_regset_to_user(child, &user_sh_native_view,
  318. REGSET_GENERAL,
  319. 0, sizeof(struct pt_regs),
  320. (void __user *)data);
  321. case PTRACE_SETREGS:
  322. return copy_regset_from_user(child, &user_sh_native_view,
  323. REGSET_GENERAL,
  324. 0, sizeof(struct pt_regs),
  325. (const void __user *)data);
  326. #ifdef CONFIG_SH_FPU
  327. case PTRACE_GETFPREGS:
  328. return copy_regset_to_user(child, &user_sh_native_view,
  329. REGSET_FPU,
  330. 0, sizeof(struct user_fpu_struct),
  331. (void __user *)data);
  332. case PTRACE_SETFPREGS:
  333. return copy_regset_from_user(child, &user_sh_native_view,
  334. REGSET_FPU,
  335. 0, sizeof(struct user_fpu_struct),
  336. (const void __user *)data);
  337. #endif
  338. #ifdef CONFIG_SH_DSP
  339. case PTRACE_GETDSPREGS:
  340. return copy_regset_to_user(child, &user_sh_native_view,
  341. REGSET_DSP,
  342. 0, sizeof(struct pt_dspregs),
  343. (void __user *)data);
  344. case PTRACE_SETDSPREGS:
  345. return copy_regset_from_user(child, &user_sh_native_view,
  346. REGSET_DSP,
  347. 0, sizeof(struct pt_dspregs),
  348. (const void __user *)data);
  349. #endif
  350. #ifdef CONFIG_BINFMT_ELF_FDPIC
  351. case PTRACE_GETFDPIC: {
  352. unsigned long tmp = 0;
  353. switch (addr) {
  354. case PTRACE_GETFDPIC_EXEC:
  355. tmp = child->mm->context.exec_fdpic_loadmap;
  356. break;
  357. case PTRACE_GETFDPIC_INTERP:
  358. tmp = child->mm->context.interp_fdpic_loadmap;
  359. break;
  360. default:
  361. break;
  362. }
  363. ret = 0;
  364. if (put_user(tmp, datap)) {
  365. ret = -EFAULT;
  366. break;
  367. }
  368. break;
  369. }
  370. #endif
  371. default:
  372. ret = ptrace_request(child, request, addr, data);
  373. break;
  374. }
  375. return ret;
  376. }
  377. static inline int audit_arch(void)
  378. {
  379. int arch = EM_SH;
  380. #ifdef CONFIG_CPU_LITTLE_ENDIAN
  381. arch |= __AUDIT_ARCH_LE;
  382. #endif
  383. return arch;
  384. }
  385. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  386. {
  387. long ret = 0;
  388. secure_computing(regs->regs[0]);
  389. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  390. tracehook_report_syscall_entry(regs))
  391. /*
  392. * Tracing decided this syscall should not happen.
  393. * We'll return a bogus call number to get an ENOSYS
  394. * error, but leave the original number in regs->regs[0].
  395. */
  396. ret = -1L;
  397. if (unlikely(current->audit_context))
  398. audit_syscall_entry(audit_arch(), regs->regs[3],
  399. regs->regs[4], regs->regs[5],
  400. regs->regs[6], regs->regs[7]);
  401. return ret ?: regs->regs[0];
  402. }
  403. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  404. {
  405. int step;
  406. if (unlikely(current->audit_context))
  407. audit_syscall_exit(AUDITSC_RESULT(regs->regs[0]),
  408. regs->regs[0]);
  409. step = test_thread_flag(TIF_SINGLESTEP);
  410. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  411. tracehook_report_syscall_exit(regs, step);
  412. }