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