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 <linux/audit.h>
14 #include <linux/ptrace.h>
15 #include <linux/elf.h>
16 #include <linux/regset.h>
17 #include <linux/sched.h>
18 #include <linux/sched/task_stack.h>
19 #include <linux/tracehook.h>
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/syscalls.h>
31 static int riscv_gpr_get(struct task_struct *target,
32 const struct user_regset *regset,
35 return membuf_write(&to, task_pt_regs(target),
36 sizeof(struct user_regs_struct));
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)
47 regs = task_pt_regs(target);
48 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
53 static int riscv_fpr_get(struct task_struct *target,
54 const struct user_regset *regset,
57 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
59 membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr));
60 membuf_store(&to, fstate->fcsr);
61 return membuf_zero(&to, 4); // explicitly pad
64 static int riscv_fpr_set(struct task_struct *target,
65 const struct user_regset *regset,
66 unsigned int pos, unsigned int count,
67 const void *kbuf, const void __user *ubuf)
70 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
72 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
73 offsetof(struct __riscv_d_ext_state, fcsr));
75 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
76 offsetof(struct __riscv_d_ext_state, fcsr) +
77 sizeof(fstate->fcsr));
84 static const struct user_regset riscv_user_regset[] = {
86 .core_note_type = NT_PRSTATUS,
88 .size = sizeof(elf_greg_t),
89 .align = sizeof(elf_greg_t),
90 .regset_get = riscv_gpr_get,
95 .core_note_type = NT_PRFPREG,
97 .size = sizeof(elf_fpreg_t),
98 .align = sizeof(elf_fpreg_t),
99 .regset_get = riscv_fpr_get,
100 .set = riscv_fpr_set,
105 static const struct user_regset_view riscv_user_native_view = {
107 .e_machine = EM_RISCV,
108 .regsets = riscv_user_regset,
109 .n = ARRAY_SIZE(riscv_user_regset),
112 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
114 return &riscv_user_native_view;
117 struct pt_regs_offset {
122 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
123 #define REG_OFFSET_END {.name = NULL, .offset = 0}
125 static const struct pt_regs_offset regoffset_table[] = {
126 REG_OFFSET_NAME(epc),
152 REG_OFFSET_NAME(s10),
153 REG_OFFSET_NAME(s11),
158 REG_OFFSET_NAME(status),
159 REG_OFFSET_NAME(badaddr),
160 REG_OFFSET_NAME(cause),
161 REG_OFFSET_NAME(orig_a0),
166 * regs_query_register_offset() - query register offset from its name
167 * @name: the name of a register
169 * regs_query_register_offset() returns the offset of a register in struct
170 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
172 int regs_query_register_offset(const char *name)
174 const struct pt_regs_offset *roff;
176 for (roff = regoffset_table; roff->name != NULL; roff++)
177 if (!strcmp(roff->name, name))
183 * regs_within_kernel_stack() - check the address in the stack
184 * @regs: pt_regs which contains kernel stack pointer.
185 * @addr: address which is checked.
187 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
188 * If @addr is within the kernel stack, it returns true. If not, returns false.
190 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
192 return (addr & ~(THREAD_SIZE - 1)) ==
193 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1));
197 * regs_get_kernel_stack_nth() - get Nth entry of the stack
198 * @regs: pt_regs which contains kernel stack pointer.
199 * @n: stack entry number.
201 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
202 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
205 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
207 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
210 if (regs_within_kernel_stack(regs, (unsigned long)addr))
216 void ptrace_disable(struct task_struct *child)
218 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
221 long arch_ptrace(struct task_struct *child, long request,
222 unsigned long addr, unsigned long data)
228 ret = ptrace_request(child, request, addr, data);
236 * Allows PTRACE_SYSCALL to work. These are called from entry.S in
237 * {handle,ret_from}_syscall.
239 __visible int do_syscall_trace_enter(struct pt_regs *regs)
241 if (test_thread_flag(TIF_SYSCALL_TRACE))
242 if (tracehook_report_syscall_entry(regs))
246 * Do the secure computing after ptrace; failures should be fast.
247 * If this fails we might have return value in a0 from seccomp
248 * (via SECCOMP_RET_ERRNO/TRACE).
250 if (secure_computing() == -1)
253 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
254 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
255 trace_sys_enter(regs, syscall_get_nr(current, regs));
258 audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3);
262 __visible void do_syscall_trace_exit(struct pt_regs *regs)
264 audit_syscall_exit(regs);
266 if (test_thread_flag(TIF_SYSCALL_TRACE))
267 tracehook_report_syscall_exit(regs, 0);
269 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
270 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
271 trace_sys_exit(regs, regs_return_value(regs));