ptrace.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  4. * Copyright 2015 Regents of the University of California
  5. * Copyright 2017 SiFive
  6. *
  7. * Copied from arch/tile/kernel/ptrace.c
  8. */
  9. #include <asm/ptrace.h>
  10. #include <asm/syscall.h>
  11. #include <asm/thread_info.h>
  12. #include <asm/switch_to.h>
  13. #include <linux/audit.h>
  14. #include <linux/compat.h>
  15. #include <linux/ptrace.h>
  16. #include <linux/elf.h>
  17. #include <linux/regset.h>
  18. #include <linux/sched.h>
  19. #include <linux/sched/task_stack.h>
  20. #include <linux/tracehook.h>
  21. #define CREATE_TRACE_POINTS
  22. #include <trace/events/syscalls.h>
  23. enum riscv_regset {
  24. REGSET_X,
  25. #ifdef CONFIG_FPU
  26. REGSET_F,
  27. #endif
  28. #ifdef CONFIG_VECTOR
  29. REGSET_V,
  30. #endif
  31. };
  32. static int riscv_gpr_get(struct task_struct *target,
  33. const struct user_regset *regset,
  34. struct membuf to)
  35. {
  36. return membuf_write(&to, task_pt_regs(target),
  37. sizeof(struct user_regs_struct));
  38. }
  39. static int riscv_gpr_set(struct task_struct *target,
  40. const struct user_regset *regset,
  41. unsigned int pos, unsigned int count,
  42. const void *kbuf, const void __user *ubuf)
  43. {
  44. int ret;
  45. struct pt_regs *regs;
  46. regs = task_pt_regs(target);
  47. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
  48. return ret;
  49. }
  50. #ifdef CONFIG_FPU
  51. static int riscv_fpr_get(struct task_struct *target,
  52. const struct user_regset *regset,
  53. struct membuf to)
  54. {
  55. struct __riscv_d_ext_state *fstate = &target->thread.fstate;
  56. if (target == current)
  57. fstate_save(current, task_pt_regs(current));
  58. membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr));
  59. membuf_store(&to, fstate->fcsr);
  60. return membuf_zero(&to, 4); // explicitly pad
  61. }
  62. static int riscv_fpr_set(struct task_struct *target,
  63. const struct user_regset *regset,
  64. unsigned int pos, unsigned int count,
  65. const void *kbuf, const void __user *ubuf)
  66. {
  67. int ret;
  68. struct __riscv_d_ext_state *fstate = &target->thread.fstate;
  69. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
  70. offsetof(struct __riscv_d_ext_state, fcsr));
  71. if (!ret) {
  72. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
  73. offsetof(struct __riscv_d_ext_state, fcsr) +
  74. sizeof(fstate->fcsr));
  75. }
  76. return ret;
  77. }
  78. #endif
  79. #ifdef CONFIG_VECTOR
  80. static int riscv_vr_get(struct task_struct *target,
  81. const struct user_regset *regset,
  82. struct membuf to)
  83. {
  84. struct __riscv_v_state *vstate = &target->thread.vstate;
  85. membuf_write(&to, vstate, offsetof(struct __riscv_v_state, vtype));
  86. return membuf_zero(&to, 4); // explicitly pad
  87. }
  88. static int riscv_vr_set(struct task_struct *target,
  89. const struct user_regset *regset,
  90. unsigned int pos, unsigned int count,
  91. const void *kbuf, const void __user *ubuf)
  92. {
  93. int ret;
  94. struct __riscv_v_state *vstate = &target->thread.vstate;
  95. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate, 0,
  96. offsetof(struct __riscv_v_state, vtype));
  97. return ret;
  98. }
  99. #endif
  100. static const struct user_regset riscv_user_regset[] = {
  101. [REGSET_X] = {
  102. .core_note_type = NT_PRSTATUS,
  103. .n = ELF_NGREG,
  104. .size = sizeof(elf_greg_t),
  105. .align = sizeof(elf_greg_t),
  106. .regset_get = riscv_gpr_get,
  107. .set = riscv_gpr_set,
  108. },
  109. #ifdef CONFIG_FPU
  110. [REGSET_F] = {
  111. .core_note_type = NT_PRFPREG,
  112. .n = ELF_NFPREG,
  113. .size = sizeof(elf_fpreg_t),
  114. .align = sizeof(elf_fpreg_t),
  115. .regset_get = riscv_fpr_get,
  116. .set = riscv_fpr_set,
  117. },
  118. #endif
  119. #ifdef CONFIG_VECTOR
  120. [REGSET_V] = {
  121. .core_note_type = NT_RISCV_VECTOR,
  122. .n = ELF_NVREG,
  123. .size = sizeof(elf_greg_t),
  124. .align = sizeof(elf_greg_t),
  125. .regset_get = riscv_vr_get,
  126. .set = riscv_vr_set,
  127. },
  128. #endif
  129. };
  130. static const struct user_regset_view riscv_user_native_view = {
  131. .name = "riscv",
  132. .e_machine = EM_RISCV,
  133. .regsets = riscv_user_regset,
  134. .n = ARRAY_SIZE(riscv_user_regset),
  135. };
  136. struct pt_regs_offset {
  137. const char *name;
  138. int offset;
  139. };
  140. #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
  141. #define REG_OFFSET_END {.name = NULL, .offset = 0}
  142. static const struct pt_regs_offset regoffset_table[] = {
  143. REG_OFFSET_NAME(epc),
  144. REG_OFFSET_NAME(ra),
  145. REG_OFFSET_NAME(sp),
  146. REG_OFFSET_NAME(gp),
  147. REG_OFFSET_NAME(tp),
  148. REG_OFFSET_NAME(t0),
  149. REG_OFFSET_NAME(t1),
  150. REG_OFFSET_NAME(t2),
  151. REG_OFFSET_NAME(s0),
  152. REG_OFFSET_NAME(s1),
  153. REG_OFFSET_NAME(a0),
  154. REG_OFFSET_NAME(a1),
  155. REG_OFFSET_NAME(a2),
  156. REG_OFFSET_NAME(a3),
  157. REG_OFFSET_NAME(a4),
  158. REG_OFFSET_NAME(a5),
  159. REG_OFFSET_NAME(a6),
  160. REG_OFFSET_NAME(a7),
  161. REG_OFFSET_NAME(s2),
  162. REG_OFFSET_NAME(s3),
  163. REG_OFFSET_NAME(s4),
  164. REG_OFFSET_NAME(s5),
  165. REG_OFFSET_NAME(s6),
  166. REG_OFFSET_NAME(s7),
  167. REG_OFFSET_NAME(s8),
  168. REG_OFFSET_NAME(s9),
  169. REG_OFFSET_NAME(s10),
  170. REG_OFFSET_NAME(s11),
  171. REG_OFFSET_NAME(t3),
  172. REG_OFFSET_NAME(t4),
  173. REG_OFFSET_NAME(t5),
  174. REG_OFFSET_NAME(t6),
  175. REG_OFFSET_NAME(status),
  176. REG_OFFSET_NAME(badaddr),
  177. REG_OFFSET_NAME(cause),
  178. REG_OFFSET_NAME(orig_a0),
  179. REG_OFFSET_END,
  180. };
  181. /**
  182. * regs_query_register_offset() - query register offset from its name
  183. * @name: the name of a register
  184. *
  185. * regs_query_register_offset() returns the offset of a register in struct
  186. * pt_regs from its name. If the name is invalid, this returns -EINVAL;
  187. */
  188. int regs_query_register_offset(const char *name)
  189. {
  190. const struct pt_regs_offset *roff;
  191. for (roff = regoffset_table; roff->name != NULL; roff++)
  192. if (!strcmp(roff->name, name))
  193. return roff->offset;
  194. return -EINVAL;
  195. }
  196. /**
  197. * regs_within_kernel_stack() - check the address in the stack
  198. * @regs: pt_regs which contains kernel stack pointer.
  199. * @addr: address which is checked.
  200. *
  201. * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
  202. * If @addr is within the kernel stack, it returns true. If not, returns false.
  203. */
  204. static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
  205. {
  206. return (addr & ~(THREAD_SIZE - 1)) ==
  207. (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1));
  208. }
  209. /**
  210. * regs_get_kernel_stack_nth() - get Nth entry of the stack
  211. * @regs: pt_regs which contains kernel stack pointer.
  212. * @n: stack entry number.
  213. *
  214. * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
  215. * is specified by @regs. If the @n th entry is NOT in the kernel stack,
  216. * this returns 0.
  217. */
  218. unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
  219. {
  220. unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
  221. addr += n;
  222. if (regs_within_kernel_stack(regs, (unsigned long)addr))
  223. return *addr;
  224. else
  225. return 0;
  226. }
  227. void ptrace_disable(struct task_struct *child)
  228. {
  229. clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
  230. }
  231. long arch_ptrace(struct task_struct *child, long request,
  232. unsigned long addr, unsigned long data)
  233. {
  234. long ret = -EIO;
  235. switch (request) {
  236. default:
  237. ret = ptrace_request(child, request, addr, data);
  238. break;
  239. }
  240. return ret;
  241. }
  242. /*
  243. * Allows PTRACE_SYSCALL to work. These are called from entry.S in
  244. * {handle,ret_from}_syscall.
  245. */
  246. __visible int do_syscall_trace_enter(struct pt_regs *regs)
  247. {
  248. if (test_thread_flag(TIF_SYSCALL_TRACE))
  249. if (tracehook_report_syscall_entry(regs))
  250. return -1;
  251. /*
  252. * Do the secure computing after ptrace; failures should be fast.
  253. * If this fails we might have return value in a0 from seccomp
  254. * (via SECCOMP_RET_ERRNO/TRACE).
  255. */
  256. if (secure_computing() == -1)
  257. return -1;
  258. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  259. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  260. trace_sys_enter(regs, syscall_get_nr(current, regs));
  261. #endif
  262. audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3);
  263. return 0;
  264. }
  265. __visible void do_syscall_trace_exit(struct pt_regs *regs)
  266. {
  267. audit_syscall_exit(regs);
  268. if (test_thread_flag(TIF_SYSCALL_TRACE))
  269. tracehook_report_syscall_exit(regs, 0);
  270. #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
  271. if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
  272. trace_sys_exit(regs, regs_return_value(regs));
  273. #endif
  274. }
  275. #ifdef CONFIG_COMPAT
  276. static int compat_riscv_gpr_get(struct task_struct *target,
  277. const struct user_regset *regset,
  278. struct membuf to)
  279. {
  280. struct compat_user_regs_struct cregs;
  281. regs_to_cregs(&cregs, task_pt_regs(target));
  282. return membuf_write(&to, &cregs,
  283. sizeof(struct compat_user_regs_struct));
  284. }
  285. static int compat_riscv_gpr_set(struct task_struct *target,
  286. const struct user_regset *regset,
  287. unsigned int pos, unsigned int count,
  288. const void *kbuf, const void __user *ubuf)
  289. {
  290. int ret;
  291. struct compat_user_regs_struct cregs;
  292. ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &cregs, 0, -1);
  293. cregs_to_regs(&cregs, task_pt_regs(target));
  294. return ret;
  295. }
  296. static const struct user_regset compat_riscv_user_regset[] = {
  297. [REGSET_X] = {
  298. .core_note_type = NT_PRSTATUS,
  299. .n = ELF_NGREG,
  300. .size = sizeof(compat_elf_greg_t),
  301. .align = sizeof(compat_elf_greg_t),
  302. .regset_get = compat_riscv_gpr_get,
  303. .set = compat_riscv_gpr_set,
  304. },
  305. #ifdef CONFIG_FPU
  306. [REGSET_F] = {
  307. .core_note_type = NT_PRFPREG,
  308. .n = ELF_NFPREG,
  309. .size = sizeof(elf_fpreg_t),
  310. .align = sizeof(elf_fpreg_t),
  311. .regset_get = riscv_fpr_get,
  312. .set = riscv_fpr_set,
  313. },
  314. #endif
  315. #ifdef CONFIG_VECTOR
  316. [REGSET_V] = {
  317. .core_note_type = NT_RISCV_VECTOR,
  318. .n = ELF_NVREG,
  319. .size = sizeof(elf_greg_t),
  320. .align = sizeof(elf_greg_t),
  321. .regset_get = riscv_vr_get,
  322. .set = riscv_vr_set,
  323. },
  324. #endif
  325. };
  326. static const struct user_regset_view compat_riscv_user_native_view = {
  327. .name = "riscv",
  328. .e_machine = EM_RISCV,
  329. .regsets = compat_riscv_user_regset,
  330. .n = ARRAY_SIZE(compat_riscv_user_regset),
  331. };
  332. long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
  333. compat_ulong_t caddr, compat_ulong_t cdata)
  334. {
  335. long ret = -EIO;
  336. switch (request) {
  337. default:
  338. ret = compat_ptrace_request(child, request, caddr, cdata);
  339. break;
  340. }
  341. return ret;
  342. }
  343. #endif /* CONFIG_COMPAT */
  344. const struct user_regset_view *task_user_regset_view(struct task_struct *task)
  345. {
  346. #ifdef CONFIG_COMPAT
  347. if (test_tsk_thread_flag(task, TIF_32BIT))
  348. return &compat_riscv_user_native_view;
  349. else
  350. #endif
  351. return &riscv_user_native_view;
  352. }