ptrace_32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * SuperH process tracing
  3. *
  4. * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
  5. * Copyright (C) 2002 - 2009 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/sched/task_stack.h>
  16. #include <linux/mm.h>
  17. #include <linux/smp.h>
  18. #include <linux/errno.h>
  19. #include <linux/ptrace.h>
  20. #include <linux/user.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 <linux/hw_breakpoint.h>
  30. #include <linux/uaccess.h>
  31. #include <asm/pgtable.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 ptrace_triggered(struct perf_event *bp,
  61. struct perf_sample_data *data, struct pt_regs *regs)
  62. {
  63. struct perf_event_attr attr;
  64. /*
  65. * Disable the breakpoint request here since ptrace has defined a
  66. * one-shot behaviour for breakpoint exceptions.
  67. */
  68. attr = bp->attr;
  69. attr.disabled = true;
  70. modify_user_hw_breakpoint(bp, &attr);
  71. }
  72. static int set_single_step(struct task_struct *tsk, unsigned long addr)
  73. {
  74. struct thread_struct *thread = &tsk->thread;
  75. struct perf_event *bp;
  76. struct perf_event_attr attr;
  77. bp = thread->ptrace_bps[0];
  78. if (!bp) {
  79. ptrace_breakpoint_init(&attr);
  80. attr.bp_addr = addr;
  81. attr.bp_len = HW_BREAKPOINT_LEN_2;
  82. attr.bp_type = HW_BREAKPOINT_R;
  83. bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
  84. NULL, tsk);
  85. if (IS_ERR(bp))
  86. return PTR_ERR(bp);
  87. thread->ptrace_bps[0] = bp;
  88. } else {
  89. int err;
  90. attr = bp->attr;
  91. attr.bp_addr = addr;
  92. /* reenable breakpoint */
  93. attr.disabled = false;
  94. err = modify_user_hw_breakpoint(bp, &attr);
  95. if (unlikely(err))
  96. return err;
  97. }
  98. return 0;
  99. }
  100. void user_enable_single_step(struct task_struct *child)
  101. {
  102. unsigned long pc = get_stack_long(child, offsetof(struct pt_regs, pc));
  103. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  104. set_single_step(child, pc);
  105. }
  106. void user_disable_single_step(struct task_struct *child)
  107. {
  108. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  109. }
  110. /*
  111. * Called by kernel/ptrace.c when detaching..
  112. *
  113. * Make sure single step bits etc are not set.
  114. */
  115. void ptrace_disable(struct task_struct *child)
  116. {
  117. user_disable_single_step(child);
  118. }
  119. static int genregs_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. const struct pt_regs *regs = task_pt_regs(target);
  125. int ret;
  126. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  127. regs->regs,
  128. 0, 16 * sizeof(unsigned long));
  129. if (!ret)
  130. /* PC, PR, SR, GBR, MACH, MACL, TRA */
  131. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  132. &regs->pc,
  133. offsetof(struct pt_regs, pc),
  134. sizeof(struct pt_regs));
  135. if (!ret)
  136. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  137. sizeof(struct pt_regs), -1);
  138. return ret;
  139. }
  140. static int genregs_set(struct task_struct *target,
  141. const struct user_regset *regset,
  142. unsigned int pos, unsigned int count,
  143. const void *kbuf, const void __user *ubuf)
  144. {
  145. struct pt_regs *regs = task_pt_regs(target);
  146. int ret;
  147. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  148. regs->regs,
  149. 0, 16 * sizeof(unsigned long));
  150. if (!ret && count > 0)
  151. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  152. &regs->pc,
  153. offsetof(struct pt_regs, pc),
  154. sizeof(struct pt_regs));
  155. if (!ret)
  156. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  157. sizeof(struct pt_regs), -1);
  158. return ret;
  159. }
  160. #ifdef CONFIG_SH_FPU
  161. int fpregs_get(struct task_struct *target,
  162. const struct user_regset *regset,
  163. unsigned int pos, unsigned int count,
  164. void *kbuf, void __user *ubuf)
  165. {
  166. int ret;
  167. ret = init_fpu(target);
  168. if (ret)
  169. return ret;
  170. if ((boot_cpu_data.flags & CPU_HAS_FPU))
  171. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  172. &target->thread.xstate->hardfpu, 0, -1);
  173. return user_regset_copyout(&pos, &count, &kbuf, &ubuf,
  174. &target->thread.xstate->softfpu, 0, -1);
  175. }
  176. static int fpregs_set(struct task_struct *target,
  177. const struct user_regset *regset,
  178. unsigned int pos, unsigned int count,
  179. const void *kbuf, const void __user *ubuf)
  180. {
  181. int ret;
  182. ret = init_fpu(target);
  183. if (ret)
  184. return ret;
  185. set_stopped_child_used_math(target);
  186. if ((boot_cpu_data.flags & CPU_HAS_FPU))
  187. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  188. &target->thread.xstate->hardfpu, 0, -1);
  189. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  190. &target->thread.xstate->softfpu, 0, -1);
  191. }
  192. static int fpregs_active(struct task_struct *target,
  193. const struct user_regset *regset)
  194. {
  195. return tsk_used_math(target) ? regset->n : 0;
  196. }
  197. #endif
  198. #ifdef CONFIG_SH_DSP
  199. static int dspregs_get(struct task_struct *target,
  200. const struct user_regset *regset,
  201. unsigned int pos, unsigned int count,
  202. void *kbuf, void __user *ubuf)
  203. {
  204. const struct pt_dspregs *regs =
  205. (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
  206. int ret;
  207. ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs,
  208. 0, sizeof(struct pt_dspregs));
  209. if (!ret)
  210. ret = user_regset_copyout_zero(&pos, &count, &kbuf, &ubuf,
  211. sizeof(struct pt_dspregs), -1);
  212. return ret;
  213. }
  214. static int dspregs_set(struct task_struct *target,
  215. const struct user_regset *regset,
  216. unsigned int pos, unsigned int count,
  217. const void *kbuf, const void __user *ubuf)
  218. {
  219. struct pt_dspregs *regs =
  220. (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
  221. int ret;
  222. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs,
  223. 0, sizeof(struct pt_dspregs));
  224. if (!ret)
  225. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  226. sizeof(struct pt_dspregs), -1);
  227. return ret;
  228. }
  229. static int dspregs_active(struct task_struct *target,
  230. const struct user_regset *regset)
  231. {
  232. struct pt_regs *regs = task_pt_regs(target);
  233. return regs->sr & SR_DSP ? regset->n : 0;
  234. }
  235. #endif
  236. const struct pt_regs_offset regoffset_table[] = {
  237. REGS_OFFSET_NAME(0),
  238. REGS_OFFSET_NAME(1),
  239. REGS_OFFSET_NAME(2),
  240. REGS_OFFSET_NAME(3),
  241. REGS_OFFSET_NAME(4),
  242. REGS_OFFSET_NAME(5),
  243. REGS_OFFSET_NAME(6),
  244. REGS_OFFSET_NAME(7),
  245. REGS_OFFSET_NAME(8),
  246. REGS_OFFSET_NAME(9),
  247. REGS_OFFSET_NAME(10),
  248. REGS_OFFSET_NAME(11),
  249. REGS_OFFSET_NAME(12),
  250. REGS_OFFSET_NAME(13),
  251. REGS_OFFSET_NAME(14),
  252. REGS_OFFSET_NAME(15),
  253. REG_OFFSET_NAME(pc),
  254. REG_OFFSET_NAME(pr),
  255. REG_OFFSET_NAME(sr),
  256. REG_OFFSET_NAME(gbr),
  257. REG_OFFSET_NAME(mach),
  258. REG_OFFSET_NAME(macl),
  259. REG_OFFSET_NAME(tra),
  260. REG_OFFSET_END,
  261. };
  262. /*
  263. * These are our native regset flavours.
  264. */
  265. enum sh_regset {
  266. REGSET_GENERAL,
  267. #ifdef CONFIG_SH_FPU
  268. REGSET_FPU,
  269. #endif
  270. #ifdef CONFIG_SH_DSP
  271. REGSET_DSP,
  272. #endif
  273. };
  274. static const struct user_regset sh_regsets[] = {
  275. /*
  276. * Format is:
  277. * R0 --> R15
  278. * PC, PR, SR, GBR, MACH, MACL, TRA
  279. */
  280. [REGSET_GENERAL] = {
  281. .core_note_type = NT_PRSTATUS,
  282. .n = ELF_NGREG,
  283. .size = sizeof(long),
  284. .align = sizeof(long),
  285. .get = genregs_get,
  286. .set = genregs_set,
  287. },
  288. #ifdef CONFIG_SH_FPU
  289. [REGSET_FPU] = {
  290. .core_note_type = NT_PRFPREG,
  291. .n = sizeof(struct user_fpu_struct) / sizeof(long),
  292. .size = sizeof(long),
  293. .align = sizeof(long),
  294. .get = fpregs_get,
  295. .set = fpregs_set,
  296. .active = fpregs_active,
  297. },
  298. #endif
  299. #ifdef CONFIG_SH_DSP
  300. [REGSET_DSP] = {
  301. .n = sizeof(struct pt_dspregs) / sizeof(long),
  302. .size = sizeof(long),
  303. .align = sizeof(long),
  304. .get = dspregs_get,
  305. .set = dspregs_set,
  306. .active = dspregs_active,
  307. },
  308. #endif
  309. };
  310. static const struct user_regset_view user_sh_native_view = {
  311. .name = "sh",
  312. .e_machine = EM_SH,
  313. .regsets = sh_regsets,
  314. .n = ARRAY_SIZE(sh_regsets),
  315. };
  316. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  317. {
  318. return &user_sh_native_view;
  319. }
  320. long arch_ptrace(struct task_struct *child, long request,
  321. unsigned long addr, unsigned long data)
  322. {
  323. unsigned long __user *datap = (unsigned long __user *)data;
  324. int ret;
  325. switch (request) {
  326. /* read the word at location addr in the USER area. */
  327. case PTRACE_PEEKUSR: {
  328. unsigned long tmp;
  329. ret = -EIO;
  330. if ((addr & 3) || addr < 0 ||
  331. addr > sizeof(struct user) - 3)
  332. break;
  333. if (addr < sizeof(struct pt_regs))
  334. tmp = get_stack_long(child, addr);
  335. else if (addr >= offsetof(struct user, fpu) &&
  336. addr < offsetof(struct user, u_fpvalid)) {
  337. if (!tsk_used_math(child)) {
  338. if (addr == offsetof(struct user, fpu.fpscr))
  339. tmp = FPSCR_INIT;
  340. else
  341. tmp = 0;
  342. } else {
  343. unsigned long index;
  344. ret = init_fpu(child);
  345. if (ret)
  346. break;
  347. index = addr - offsetof(struct user, fpu);
  348. tmp = ((unsigned long *)child->thread.xstate)
  349. [index >> 2];
  350. }
  351. } else if (addr == offsetof(struct user, u_fpvalid))
  352. tmp = !!tsk_used_math(child);
  353. else if (addr == PT_TEXT_ADDR)
  354. tmp = child->mm->start_code;
  355. else if (addr == PT_DATA_ADDR)
  356. tmp = child->mm->start_data;
  357. else if (addr == PT_TEXT_END_ADDR)
  358. tmp = child->mm->end_code;
  359. else if (addr == PT_TEXT_LEN)
  360. tmp = child->mm->end_code - child->mm->start_code;
  361. else
  362. tmp = 0;
  363. ret = put_user(tmp, datap);
  364. break;
  365. }
  366. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  367. ret = -EIO;
  368. if ((addr & 3) || addr < 0 ||
  369. addr > sizeof(struct user) - 3)
  370. break;
  371. if (addr < sizeof(struct pt_regs))
  372. ret = put_stack_long(child, addr, data);
  373. else if (addr >= offsetof(struct user, fpu) &&
  374. addr < offsetof(struct user, u_fpvalid)) {
  375. unsigned long index;
  376. ret = init_fpu(child);
  377. if (ret)
  378. break;
  379. index = addr - offsetof(struct user, fpu);
  380. set_stopped_child_used_math(child);
  381. ((unsigned long *)child->thread.xstate)
  382. [index >> 2] = data;
  383. ret = 0;
  384. } else if (addr == offsetof(struct user, u_fpvalid)) {
  385. conditional_stopped_child_used_math(data, child);
  386. ret = 0;
  387. }
  388. break;
  389. case PTRACE_GETREGS:
  390. return copy_regset_to_user(child, &user_sh_native_view,
  391. REGSET_GENERAL,
  392. 0, sizeof(struct pt_regs),
  393. datap);
  394. case PTRACE_SETREGS:
  395. return copy_regset_from_user(child, &user_sh_native_view,
  396. REGSET_GENERAL,
  397. 0, sizeof(struct pt_regs),
  398. datap);
  399. #ifdef CONFIG_SH_FPU
  400. case PTRACE_GETFPREGS:
  401. return copy_regset_to_user(child, &user_sh_native_view,
  402. REGSET_FPU,
  403. 0, sizeof(struct user_fpu_struct),
  404. datap);
  405. case PTRACE_SETFPREGS:
  406. return copy_regset_from_user(child, &user_sh_native_view,
  407. REGSET_FPU,
  408. 0, sizeof(struct user_fpu_struct),
  409. datap);
  410. #endif
  411. #ifdef CONFIG_SH_DSP
  412. case PTRACE_GETDSPREGS:
  413. return copy_regset_to_user(child, &user_sh_native_view,
  414. REGSET_DSP,
  415. 0, sizeof(struct pt_dspregs),
  416. datap);
  417. case PTRACE_SETDSPREGS:
  418. return copy_regset_from_user(child, &user_sh_native_view,
  419. REGSET_DSP,
  420. 0, sizeof(struct pt_dspregs),
  421. datap);
  422. #endif
  423. default:
  424. ret = ptrace_request(child, request, addr, data);
  425. break;
  426. }
  427. return ret;
  428. }
  429. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  430. {
  431. long ret = 0;
  432. secure_computing_strict(regs->regs[0]);
  433. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  434. tracehook_report_syscall_entry(regs))
  435. /*
  436. * Tracing decided this syscall should not happen.
  437. * We'll return a bogus call number to get an ENOSYS
  438. * error, but leave the original number in regs->regs[0].
  439. */
  440. ret = -1L;
  441. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  442. trace_sys_enter(regs, regs->regs[0]);
  443. audit_syscall_entry(regs->regs[3], regs->regs[4], regs->regs[5],
  444. regs->regs[6], regs->regs[7]);
  445. return ret ?: regs->regs[0];
  446. }
  447. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  448. {
  449. int step;
  450. audit_syscall_exit(regs);
  451. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  452. trace_sys_exit(regs, regs->regs[0]);
  453. step = test_thread_flag(TIF_SINGLESTEP);
  454. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  455. tracehook_report_syscall_exit(regs, step);
  456. }