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>
29 static int riscv_gpr_get(struct task_struct *target,
30 const struct user_regset *regset,
33 return membuf_write(&to, task_pt_regs(target),
34 sizeof(struct user_regs_struct));
37 static int riscv_gpr_set(struct task_struct *target,
38 const struct user_regset *regset,
39 unsigned int pos, unsigned int count,
40 const void *kbuf, const void __user *ubuf)
44 regs = task_pt_regs(target);
45 return user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
49 static int riscv_fpr_get(struct task_struct *target,
50 const struct user_regset *regset,
53 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
55 if (target == current)
56 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
63 static int riscv_fpr_set(struct task_struct *target,
64 const struct user_regset *regset,
65 unsigned int pos, unsigned int count,
66 const void *kbuf, const void __user *ubuf)
69 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
71 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
72 offsetof(struct __riscv_d_ext_state, fcsr));
74 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
75 offsetof(struct __riscv_d_ext_state, fcsr) +
76 sizeof(fstate->fcsr));
83 static const struct user_regset riscv_user_regset[] = {
85 .core_note_type = NT_PRSTATUS,
87 .size = sizeof(elf_greg_t),
88 .align = sizeof(elf_greg_t),
89 .regset_get = riscv_gpr_get,
94 .core_note_type = NT_PRFPREG,
96 .size = sizeof(elf_fpreg_t),
97 .align = sizeof(elf_fpreg_t),
98 .regset_get = riscv_fpr_get,
104 static const struct user_regset_view riscv_user_native_view = {
106 .e_machine = EM_RISCV,
107 .regsets = riscv_user_regset,
108 .n = ARRAY_SIZE(riscv_user_regset),
111 struct pt_regs_offset {
116 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
117 #define REG_OFFSET_END {.name = NULL, .offset = 0}
119 static const struct pt_regs_offset regoffset_table[] = {
120 REG_OFFSET_NAME(epc),
146 REG_OFFSET_NAME(s10),
147 REG_OFFSET_NAME(s11),
152 REG_OFFSET_NAME(status),
153 REG_OFFSET_NAME(badaddr),
154 REG_OFFSET_NAME(cause),
155 REG_OFFSET_NAME(orig_a0),
160 * regs_query_register_offset() - query register offset from its name
161 * @name: the name of a register
163 * regs_query_register_offset() returns the offset of a register in struct
164 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
166 int regs_query_register_offset(const char *name)
168 const struct pt_regs_offset *roff;
170 for (roff = regoffset_table; roff->name != NULL; roff++)
171 if (!strcmp(roff->name, name))
177 * regs_within_kernel_stack() - check the address in the stack
178 * @regs: pt_regs which contains kernel stack pointer.
179 * @addr: address which is checked.
181 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
182 * If @addr is within the kernel stack, it returns true. If not, returns false.
184 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
186 return (addr & ~(THREAD_SIZE - 1)) ==
187 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1));
191 * regs_get_kernel_stack_nth() - get Nth entry of the stack
192 * @regs: pt_regs which contains kernel stack pointer.
193 * @n: stack entry number.
195 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
196 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
199 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
201 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
204 if (regs_within_kernel_stack(regs, (unsigned long)addr))
210 void ptrace_disable(struct task_struct *child)
214 long arch_ptrace(struct task_struct *child, long request,
215 unsigned long addr, unsigned long data)
221 ret = ptrace_request(child, request, addr, data);
229 static int compat_riscv_gpr_get(struct task_struct *target,
230 const struct user_regset *regset,
233 struct compat_user_regs_struct cregs;
235 regs_to_cregs(&cregs, task_pt_regs(target));
237 return membuf_write(&to, &cregs,
238 sizeof(struct compat_user_regs_struct));
241 static int compat_riscv_gpr_set(struct task_struct *target,
242 const struct user_regset *regset,
243 unsigned int pos, unsigned int count,
244 const void *kbuf, const void __user *ubuf)
247 struct compat_user_regs_struct cregs;
249 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &cregs, 0, -1);
251 cregs_to_regs(&cregs, task_pt_regs(target));
256 static const struct user_regset compat_riscv_user_regset[] = {
258 .core_note_type = NT_PRSTATUS,
260 .size = sizeof(compat_elf_greg_t),
261 .align = sizeof(compat_elf_greg_t),
262 .regset_get = compat_riscv_gpr_get,
263 .set = compat_riscv_gpr_set,
267 .core_note_type = NT_PRFPREG,
269 .size = sizeof(elf_fpreg_t),
270 .align = sizeof(elf_fpreg_t),
271 .regset_get = riscv_fpr_get,
272 .set = riscv_fpr_set,
277 static const struct user_regset_view compat_riscv_user_native_view = {
279 .e_machine = EM_RISCV,
280 .regsets = compat_riscv_user_regset,
281 .n = ARRAY_SIZE(compat_riscv_user_regset),
284 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
285 compat_ulong_t caddr, compat_ulong_t cdata)
291 ret = compat_ptrace_request(child, request, caddr, cdata);
297 #endif /* CONFIG_COMPAT */
299 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
302 if (test_tsk_thread_flag(task, TIF_32BIT))
303 return &compat_riscv_user_native_view;
306 return &riscv_user_native_view;