ptrace.c 8.5 KB

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