1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2010 Tilera Corporation. All Rights Reserved.
4 * Copyright 2015 Regents of the University of California
5 * Copyright 2017 SiFive
7 * Copied from arch/tile/kernel/ptrace.c
10 #include <asm/ptrace.h>
11 #include <asm/syscall.h>
12 #include <asm/thread_info.h>
13 #include <asm/switch_to.h>
14 #include <linux/audit.h>
15 #include <linux/compat.h>
16 #include <linux/ptrace.h>
17 #include <linux/elf.h>
18 #include <linux/regset.h>
19 #include <linux/sched.h>
20 #include <linux/sched/task_stack.h>
22 #define CREATE_TRACE_POINTS
23 #include <trace/events/syscalls.h>
32 static int riscv_gpr_get(struct task_struct *target,
33 const struct user_regset *regset,
36 return membuf_write(&to, task_pt_regs(target),
37 sizeof(struct user_regs_struct));
40 static int riscv_gpr_set(struct task_struct *target,
41 const struct user_regset *regset,
42 unsigned int pos, unsigned int count,
43 const void *kbuf, const void __user *ubuf)
47 regs = task_pt_regs(target);
48 return user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
52 static int riscv_fpr_get(struct task_struct *target,
53 const struct user_regset *regset,
56 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
58 if (target == current)
59 fstate_save(current, task_pt_regs(current));
61 membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr));
62 membuf_store(&to, fstate->fcsr);
63 return membuf_zero(&to, 4); // explicitly pad
66 static int riscv_fpr_set(struct task_struct *target,
67 const struct user_regset *regset,
68 unsigned int pos, unsigned int count,
69 const void *kbuf, const void __user *ubuf)
72 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
74 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
75 offsetof(struct __riscv_d_ext_state, fcsr));
77 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
78 offsetof(struct __riscv_d_ext_state, fcsr) +
79 sizeof(fstate->fcsr));
86 static const struct user_regset riscv_user_regset[] = {
88 .core_note_type = NT_PRSTATUS,
90 .size = sizeof(elf_greg_t),
91 .align = sizeof(elf_greg_t),
92 .regset_get = riscv_gpr_get,
97 .core_note_type = NT_PRFPREG,
99 .size = sizeof(elf_fpreg_t),
100 .align = sizeof(elf_fpreg_t),
101 .regset_get = riscv_fpr_get,
102 .set = riscv_fpr_set,
107 static const struct user_regset_view riscv_user_native_view = {
109 .e_machine = EM_RISCV,
110 .regsets = riscv_user_regset,
111 .n = ARRAY_SIZE(riscv_user_regset),
114 struct pt_regs_offset {
119 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
120 #define REG_OFFSET_END {.name = NULL, .offset = 0}
122 static const struct pt_regs_offset regoffset_table[] = {
123 REG_OFFSET_NAME(epc),
149 REG_OFFSET_NAME(s10),
150 REG_OFFSET_NAME(s11),
155 REG_OFFSET_NAME(status),
156 REG_OFFSET_NAME(badaddr),
157 REG_OFFSET_NAME(cause),
158 REG_OFFSET_NAME(orig_a0),
163 * regs_query_register_offset() - query register offset from its name
164 * @name: the name of a register
166 * regs_query_register_offset() returns the offset of a register in struct
167 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
169 int regs_query_register_offset(const char *name)
171 const struct pt_regs_offset *roff;
173 for (roff = regoffset_table; roff->name != NULL; roff++)
174 if (!strcmp(roff->name, name))
180 * regs_within_kernel_stack() - check the address in the stack
181 * @regs: pt_regs which contains kernel stack pointer.
182 * @addr: address which is checked.
184 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
185 * If @addr is within the kernel stack, it returns true. If not, returns false.
187 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
189 return (addr & ~(THREAD_SIZE - 1)) ==
190 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1));
194 * regs_get_kernel_stack_nth() - get Nth entry of the stack
195 * @regs: pt_regs which contains kernel stack pointer.
196 * @n: stack entry number.
198 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
199 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
202 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
204 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
207 if (regs_within_kernel_stack(regs, (unsigned long)addr))
213 void ptrace_disable(struct task_struct *child)
215 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
218 long arch_ptrace(struct task_struct *child, long request,
219 unsigned long addr, unsigned long data)
225 ret = ptrace_request(child, request, addr, data);
233 * Allows PTRACE_SYSCALL to work. These are called from entry.S in
234 * {handle,ret_from}_syscall.
236 __visible int do_syscall_trace_enter(struct pt_regs *regs)
238 if (test_thread_flag(TIF_SYSCALL_TRACE))
239 if (ptrace_report_syscall_entry(regs))
243 * Do the secure computing after ptrace; failures should be fast.
244 * If this fails we might have return value in a0 from seccomp
245 * (via SECCOMP_RET_ERRNO/TRACE).
247 if (secure_computing() == -1)
250 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
251 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
252 trace_sys_enter(regs, syscall_get_nr(current, regs));
255 audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3);
259 __visible void do_syscall_trace_exit(struct pt_regs *regs)
261 audit_syscall_exit(regs);
263 if (test_thread_flag(TIF_SYSCALL_TRACE))
264 ptrace_report_syscall_exit(regs, 0);
266 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
267 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
268 trace_sys_exit(regs, regs_return_value(regs));
273 static int compat_riscv_gpr_get(struct task_struct *target,
274 const struct user_regset *regset,
277 struct compat_user_regs_struct cregs;
279 regs_to_cregs(&cregs, task_pt_regs(target));
281 return membuf_write(&to, &cregs,
282 sizeof(struct compat_user_regs_struct));
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)
291 struct compat_user_regs_struct cregs;
293 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &cregs, 0, -1);
295 cregs_to_regs(&cregs, task_pt_regs(target));
300 static const struct user_regset compat_riscv_user_regset[] = {
302 .core_note_type = NT_PRSTATUS,
304 .size = sizeof(compat_elf_greg_t),
305 .align = sizeof(compat_elf_greg_t),
306 .regset_get = compat_riscv_gpr_get,
307 .set = compat_riscv_gpr_set,
311 .core_note_type = NT_PRFPREG,
313 .size = sizeof(elf_fpreg_t),
314 .align = sizeof(elf_fpreg_t),
315 .regset_get = riscv_fpr_get,
316 .set = riscv_fpr_set,
321 static const struct user_regset_view compat_riscv_user_native_view = {
323 .e_machine = EM_RISCV,
324 .regsets = compat_riscv_user_regset,
325 .n = ARRAY_SIZE(compat_riscv_user_regset),
328 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
329 compat_ulong_t caddr, compat_ulong_t cdata)
335 ret = compat_ptrace_request(child, request, caddr, cdata);
341 #endif /* CONFIG_COMPAT */
343 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
346 if (test_tsk_thread_flag(task, TIF_32BIT))
347 return &compat_riscv_user_native_view;
350 return &riscv_user_native_view;