ptrace_32.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SuperH process tracing
  4. *
  5. * Copyright (C) 1999, 2000 Kaz Kojima & Niibe Yutaka
  6. * Copyright (C) 2002 - 2009 Paul Mundt
  7. *
  8. * Audit support by Yuichi Nakamura <[email protected]>
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/sched.h>
  12. #include <linux/sched/task_stack.h>
  13. #include <linux/mm.h>
  14. #include <linux/smp.h>
  15. #include <linux/errno.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/user.h>
  18. #include <linux/security.h>
  19. #include <linux/signal.h>
  20. #include <linux/io.h>
  21. #include <linux/audit.h>
  22. #include <linux/seccomp.h>
  23. #include <linux/elf.h>
  24. #include <linux/regset.h>
  25. #include <linux/hw_breakpoint.h>
  26. #include <linux/uaccess.h>
  27. #include <asm/processor.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/syscalls.h>
  30. #include <asm/fpu.h>
  31. #define CREATE_TRACE_POINTS
  32. #include <trace/events/syscalls.h>
  33. /*
  34. * This routine will get a word off of the process kernel stack.
  35. */
  36. static inline int get_stack_long(struct task_struct *task, int offset)
  37. {
  38. unsigned char *stack;
  39. stack = (unsigned char *)task_pt_regs(task);
  40. stack += offset;
  41. return (*((int *)stack));
  42. }
  43. /*
  44. * This routine will put a word on the process kernel stack.
  45. */
  46. static inline int put_stack_long(struct task_struct *task, int offset,
  47. unsigned long data)
  48. {
  49. unsigned char *stack;
  50. stack = (unsigned char *)task_pt_regs(task);
  51. stack += offset;
  52. *(unsigned long *) stack = data;
  53. return 0;
  54. }
  55. void ptrace_triggered(struct perf_event *bp,
  56. struct perf_sample_data *data, struct pt_regs *regs)
  57. {
  58. struct perf_event_attr attr;
  59. /*
  60. * Disable the breakpoint request here since ptrace has defined a
  61. * one-shot behaviour for breakpoint exceptions.
  62. */
  63. attr = bp->attr;
  64. attr.disabled = true;
  65. modify_user_hw_breakpoint(bp, &attr);
  66. }
  67. static int set_single_step(struct task_struct *tsk, unsigned long addr)
  68. {
  69. struct thread_struct *thread = &tsk->thread;
  70. struct perf_event *bp;
  71. struct perf_event_attr attr;
  72. bp = thread->ptrace_bps[0];
  73. if (!bp) {
  74. ptrace_breakpoint_init(&attr);
  75. attr.bp_addr = addr;
  76. attr.bp_len = HW_BREAKPOINT_LEN_2;
  77. attr.bp_type = HW_BREAKPOINT_R;
  78. bp = register_user_hw_breakpoint(&attr, ptrace_triggered,
  79. NULL, tsk);
  80. if (IS_ERR(bp))
  81. return PTR_ERR(bp);
  82. thread->ptrace_bps[0] = bp;
  83. } else {
  84. int err;
  85. attr = bp->attr;
  86. attr.bp_addr = addr;
  87. /* reenable breakpoint */
  88. attr.disabled = false;
  89. err = modify_user_hw_breakpoint(bp, &attr);
  90. if (unlikely(err))
  91. return err;
  92. }
  93. return 0;
  94. }
  95. void user_enable_single_step(struct task_struct *child)
  96. {
  97. unsigned long pc = get_stack_long(child, offsetof(struct pt_regs, pc));
  98. set_tsk_thread_flag(child, TIF_SINGLESTEP);
  99. set_single_step(child, pc);
  100. }
  101. void user_disable_single_step(struct task_struct *child)
  102. {
  103. clear_tsk_thread_flag(child, TIF_SINGLESTEP);
  104. }
  105. /*
  106. * Called by kernel/ptrace.c when detaching..
  107. *
  108. * Make sure single step bits etc are not set.
  109. */
  110. void ptrace_disable(struct task_struct *child)
  111. {
  112. user_disable_single_step(child);
  113. }
  114. static int genregs_get(struct task_struct *target,
  115. const struct user_regset *regset,
  116. struct membuf to)
  117. {
  118. const struct pt_regs *regs = task_pt_regs(target);
  119. return membuf_write(&to, regs, sizeof(struct pt_regs));
  120. }
  121. static int genregs_set(struct task_struct *target,
  122. const struct user_regset *regset,
  123. unsigned int pos, unsigned int count,
  124. const void *kbuf, const void __user *ubuf)
  125. {
  126. struct pt_regs *regs = task_pt_regs(target);
  127. int ret;
  128. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  129. regs->regs,
  130. 0, 16 * sizeof(unsigned long));
  131. if (!ret && count > 0)
  132. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  133. &regs->pc,
  134. offsetof(struct pt_regs, pc),
  135. sizeof(struct pt_regs));
  136. if (!ret)
  137. ret = user_regset_copyin_ignore(&pos, &count, &kbuf, &ubuf,
  138. sizeof(struct pt_regs), -1);
  139. return ret;
  140. }
  141. #ifdef CONFIG_SH_FPU
  142. static int fpregs_get(struct task_struct *target,
  143. const struct user_regset *regset,
  144. struct membuf to)
  145. {
  146. int ret;
  147. ret = init_fpu(target);
  148. if (ret)
  149. return ret;
  150. return membuf_write(&to, target->thread.xstate,
  151. sizeof(struct user_fpu_struct));
  152. }
  153. static int fpregs_set(struct task_struct *target,
  154. const struct user_regset *regset,
  155. unsigned int pos, unsigned int count,
  156. const void *kbuf, const void __user *ubuf)
  157. {
  158. int ret;
  159. ret = init_fpu(target);
  160. if (ret)
  161. return ret;
  162. set_stopped_child_used_math(target);
  163. if ((boot_cpu_data.flags & CPU_HAS_FPU))
  164. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  165. &target->thread.xstate->hardfpu, 0, -1);
  166. return user_regset_copyin(&pos, &count, &kbuf, &ubuf,
  167. &target->thread.xstate->softfpu, 0, -1);
  168. }
  169. static int fpregs_active(struct task_struct *target,
  170. const struct user_regset *regset)
  171. {
  172. return tsk_used_math(target) ? regset->n : 0;
  173. }
  174. #endif
  175. #ifdef CONFIG_SH_DSP
  176. static int dspregs_get(struct task_struct *target,
  177. const struct user_regset *regset,
  178. struct membuf to)
  179. {
  180. const struct pt_dspregs *regs =
  181. (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
  182. return membuf_write(&to, regs, sizeof(struct pt_dspregs));
  183. }
  184. static int dspregs_set(struct task_struct *target,
  185. const struct user_regset *regset,
  186. unsigned int pos, unsigned int count,
  187. const void *kbuf, const void __user *ubuf)
  188. {
  189. struct pt_dspregs *regs =
  190. (struct pt_dspregs *)&target->thread.dsp_status.dsp_regs;
  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. const struct pt_regs_offset regoffset_table[] = {
  207. REGS_OFFSET_NAME(0),
  208. REGS_OFFSET_NAME(1),
  209. REGS_OFFSET_NAME(2),
  210. REGS_OFFSET_NAME(3),
  211. REGS_OFFSET_NAME(4),
  212. REGS_OFFSET_NAME(5),
  213. REGS_OFFSET_NAME(6),
  214. REGS_OFFSET_NAME(7),
  215. REGS_OFFSET_NAME(8),
  216. REGS_OFFSET_NAME(9),
  217. REGS_OFFSET_NAME(10),
  218. REGS_OFFSET_NAME(11),
  219. REGS_OFFSET_NAME(12),
  220. REGS_OFFSET_NAME(13),
  221. REGS_OFFSET_NAME(14),
  222. REGS_OFFSET_NAME(15),
  223. REG_OFFSET_NAME(pc),
  224. REG_OFFSET_NAME(pr),
  225. REG_OFFSET_NAME(sr),
  226. REG_OFFSET_NAME(gbr),
  227. REG_OFFSET_NAME(mach),
  228. REG_OFFSET_NAME(macl),
  229. REG_OFFSET_NAME(tra),
  230. REG_OFFSET_END,
  231. };
  232. /*
  233. * These are our native regset flavours.
  234. */
  235. enum sh_regset {
  236. REGSET_GENERAL,
  237. #ifdef CONFIG_SH_FPU
  238. REGSET_FPU,
  239. #endif
  240. #ifdef CONFIG_SH_DSP
  241. REGSET_DSP,
  242. #endif
  243. };
  244. static const struct user_regset sh_regsets[] = {
  245. /*
  246. * Format is:
  247. * R0 --> R15
  248. * PC, PR, SR, GBR, MACH, MACL, TRA
  249. */
  250. [REGSET_GENERAL] = {
  251. .core_note_type = NT_PRSTATUS,
  252. .n = ELF_NGREG,
  253. .size = sizeof(long),
  254. .align = sizeof(long),
  255. .regset_get = genregs_get,
  256. .set = genregs_set,
  257. },
  258. #ifdef CONFIG_SH_FPU
  259. [REGSET_FPU] = {
  260. .core_note_type = NT_PRFPREG,
  261. .n = sizeof(struct user_fpu_struct) / sizeof(long),
  262. .size = sizeof(long),
  263. .align = sizeof(long),
  264. .regset_get = fpregs_get,
  265. .set = fpregs_set,
  266. .active = fpregs_active,
  267. },
  268. #endif
  269. #ifdef CONFIG_SH_DSP
  270. [REGSET_DSP] = {
  271. .n = sizeof(struct pt_dspregs) / sizeof(long),
  272. .size = sizeof(long),
  273. .align = sizeof(long),
  274. .regset_get = dspregs_get,
  275. .set = dspregs_set,
  276. .active = dspregs_active,
  277. },
  278. #endif
  279. };
  280. static const struct user_regset_view user_sh_native_view = {
  281. .name = "sh",
  282. .e_machine = EM_SH,
  283. .regsets = sh_regsets,
  284. .n = ARRAY_SIZE(sh_regsets),
  285. };
  286. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  287. {
  288. return &user_sh_native_view;
  289. }
  290. long arch_ptrace(struct task_struct *child, long request,
  291. unsigned long addr, unsigned long data)
  292. {
  293. unsigned long __user *datap = (unsigned long __user *)data;
  294. int ret;
  295. switch (request) {
  296. /* read the word at location addr in the USER area. */
  297. case PTRACE_PEEKUSR: {
  298. unsigned long tmp;
  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. tmp = get_stack_long(child, addr);
  305. else if (addr >= offsetof(struct user, fpu) &&
  306. addr < offsetof(struct user, u_fpvalid)) {
  307. if (!tsk_used_math(child)) {
  308. if (addr == offsetof(struct user, fpu.fpscr))
  309. tmp = FPSCR_INIT;
  310. else
  311. tmp = 0;
  312. } else {
  313. unsigned long index;
  314. ret = init_fpu(child);
  315. if (ret)
  316. break;
  317. index = addr - offsetof(struct user, fpu);
  318. tmp = ((unsigned long *)child->thread.xstate)
  319. [index >> 2];
  320. }
  321. } else if (addr == offsetof(struct user, u_fpvalid))
  322. tmp = !!tsk_used_math(child);
  323. else if (addr == PT_TEXT_ADDR)
  324. tmp = child->mm->start_code;
  325. else if (addr == PT_DATA_ADDR)
  326. tmp = child->mm->start_data;
  327. else if (addr == PT_TEXT_END_ADDR)
  328. tmp = child->mm->end_code;
  329. else if (addr == PT_TEXT_LEN)
  330. tmp = child->mm->end_code - child->mm->start_code;
  331. else
  332. tmp = 0;
  333. ret = put_user(tmp, datap);
  334. break;
  335. }
  336. case PTRACE_POKEUSR: /* write the word at location addr in the USER area */
  337. ret = -EIO;
  338. if ((addr & 3) || addr < 0 ||
  339. addr > sizeof(struct user) - 3)
  340. break;
  341. if (addr < sizeof(struct pt_regs))
  342. ret = put_stack_long(child, addr, data);
  343. else if (addr >= offsetof(struct user, fpu) &&
  344. addr < offsetof(struct user, u_fpvalid)) {
  345. unsigned long index;
  346. ret = init_fpu(child);
  347. if (ret)
  348. break;
  349. index = addr - offsetof(struct user, fpu);
  350. set_stopped_child_used_math(child);
  351. ((unsigned long *)child->thread.xstate)
  352. [index >> 2] = data;
  353. ret = 0;
  354. } else if (addr == offsetof(struct user, u_fpvalid)) {
  355. conditional_stopped_child_used_math(data, child);
  356. ret = 0;
  357. }
  358. break;
  359. case PTRACE_GETREGS:
  360. return copy_regset_to_user(child, &user_sh_native_view,
  361. REGSET_GENERAL,
  362. 0, sizeof(struct pt_regs),
  363. datap);
  364. case PTRACE_SETREGS:
  365. return copy_regset_from_user(child, &user_sh_native_view,
  366. REGSET_GENERAL,
  367. 0, sizeof(struct pt_regs),
  368. datap);
  369. #ifdef CONFIG_SH_FPU
  370. case PTRACE_GETFPREGS:
  371. return copy_regset_to_user(child, &user_sh_native_view,
  372. REGSET_FPU,
  373. 0, sizeof(struct user_fpu_struct),
  374. datap);
  375. case PTRACE_SETFPREGS:
  376. return copy_regset_from_user(child, &user_sh_native_view,
  377. REGSET_FPU,
  378. 0, sizeof(struct user_fpu_struct),
  379. datap);
  380. #endif
  381. #ifdef CONFIG_SH_DSP
  382. case PTRACE_GETDSPREGS:
  383. return copy_regset_to_user(child, &user_sh_native_view,
  384. REGSET_DSP,
  385. 0, sizeof(struct pt_dspregs),
  386. datap);
  387. case PTRACE_SETDSPREGS:
  388. return copy_regset_from_user(child, &user_sh_native_view,
  389. REGSET_DSP,
  390. 0, sizeof(struct pt_dspregs),
  391. datap);
  392. #endif
  393. default:
  394. ret = ptrace_request(child, request, addr, data);
  395. break;
  396. }
  397. return ret;
  398. }
  399. asmlinkage long do_syscall_trace_enter(struct pt_regs *regs)
  400. {
  401. if (test_thread_flag(TIF_SYSCALL_TRACE) &&
  402. ptrace_report_syscall_entry(regs)) {
  403. regs->regs[0] = -ENOSYS;
  404. return -1;
  405. }
  406. if (secure_computing() == -1)
  407. return -1;
  408. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  409. trace_sys_enter(regs, regs->regs[0]);
  410. audit_syscall_entry(regs->regs[3], regs->regs[4], regs->regs[5],
  411. regs->regs[6], regs->regs[7]);
  412. return 0;
  413. }
  414. asmlinkage void do_syscall_trace_leave(struct pt_regs *regs)
  415. {
  416. int step;
  417. audit_syscall_exit(regs);
  418. if (unlikely(test_thread_flag(TIF_SYSCALL_TRACEPOINT)))
  419. trace_sys_exit(regs, regs->regs[0]);
  420. step = test_thread_flag(TIF_SINGLESTEP);
  421. if (step || test_thread_flag(TIF_SYSCALL_TRACE))
  422. ptrace_report_syscall_exit(regs, step);
  423. }