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/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>
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)
48 regs = task_pt_regs(target);
49 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
54 static int riscv_fpr_get(struct task_struct *target,
55 const struct user_regset *regset,
58 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
60 if (target == current)
61 fstate_save(current, task_pt_regs(current));
63 membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr));
64 membuf_store(&to, fstate->fcsr);
65 return membuf_zero(&to, 4); // explicitly pad
68 static int riscv_fpr_set(struct task_struct *target,
69 const struct user_regset *regset,
70 unsigned int pos, unsigned int count,
71 const void *kbuf, const void __user *ubuf)
74 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
76 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
77 offsetof(struct __riscv_d_ext_state, fcsr));
79 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
80 offsetof(struct __riscv_d_ext_state, fcsr) +
81 sizeof(fstate->fcsr));
88 static const struct user_regset riscv_user_regset[] = {
90 .core_note_type = NT_PRSTATUS,
92 .size = sizeof(elf_greg_t),
93 .align = sizeof(elf_greg_t),
94 .regset_get = riscv_gpr_get,
99 .core_note_type = NT_PRFPREG,
101 .size = sizeof(elf_fpreg_t),
102 .align = sizeof(elf_fpreg_t),
103 .regset_get = riscv_fpr_get,
104 .set = riscv_fpr_set,
109 static const struct user_regset_view riscv_user_native_view = {
111 .e_machine = EM_RISCV,
112 .regsets = riscv_user_regset,
113 .n = ARRAY_SIZE(riscv_user_regset),
116 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
118 return &riscv_user_native_view;
121 struct pt_regs_offset {
126 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
127 #define REG_OFFSET_END {.name = NULL, .offset = 0}
129 static const struct pt_regs_offset regoffset_table[] = {
130 REG_OFFSET_NAME(epc),
156 REG_OFFSET_NAME(s10),
157 REG_OFFSET_NAME(s11),
162 REG_OFFSET_NAME(status),
163 REG_OFFSET_NAME(badaddr),
164 REG_OFFSET_NAME(cause),
165 REG_OFFSET_NAME(orig_a0),
170 * regs_query_register_offset() - query register offset from its name
171 * @name: the name of a register
173 * regs_query_register_offset() returns the offset of a register in struct
174 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
176 int regs_query_register_offset(const char *name)
178 const struct pt_regs_offset *roff;
180 for (roff = regoffset_table; roff->name != NULL; roff++)
181 if (!strcmp(roff->name, name))
187 * regs_within_kernel_stack() - check the address in the stack
188 * @regs: pt_regs which contains kernel stack pointer.
189 * @addr: address which is checked.
191 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
192 * If @addr is within the kernel stack, it returns true. If not, returns false.
194 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
196 return (addr & ~(THREAD_SIZE - 1)) ==
197 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1));
201 * regs_get_kernel_stack_nth() - get Nth entry of the stack
202 * @regs: pt_regs which contains kernel stack pointer.
203 * @n: stack entry number.
205 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
206 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
209 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
211 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
214 if (regs_within_kernel_stack(regs, (unsigned long)addr))
220 void ptrace_disable(struct task_struct *child)
222 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
225 long arch_ptrace(struct task_struct *child, long request,
226 unsigned long addr, unsigned long data)
232 ret = ptrace_request(child, request, addr, data);
240 * Allows PTRACE_SYSCALL to work. These are called from entry.S in
241 * {handle,ret_from}_syscall.
243 __visible int do_syscall_trace_enter(struct pt_regs *regs)
245 if (test_thread_flag(TIF_SYSCALL_TRACE))
246 if (tracehook_report_syscall_entry(regs))
250 * Do the secure computing after ptrace; failures should be fast.
251 * If this fails we might have return value in a0 from seccomp
252 * (via SECCOMP_RET_ERRNO/TRACE).
254 if (secure_computing() == -1)
257 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
258 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
259 trace_sys_enter(regs, syscall_get_nr(current, regs));
262 audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3);
266 __visible void do_syscall_trace_exit(struct pt_regs *regs)
268 audit_syscall_exit(regs);
270 if (test_thread_flag(TIF_SYSCALL_TRACE))
271 tracehook_report_syscall_exit(regs, 0);
273 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
274 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
275 trace_sys_exit(regs, regs_return_value(regs));