ptrace_32.c 12 KB

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