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/vector.h>
11 #include <asm/ptrace.h>
12 #include <asm/syscall.h>
13 #include <asm/thread_info.h>
14 #include <asm/switch_to.h>
15 #include <linux/audit.h>
16 #include <linux/compat.h>
17 #include <linux/ptrace.h>
18 #include <linux/elf.h>
19 #include <linux/regset.h>
20 #include <linux/sched.h>
21 #include <linux/sched/task_stack.h>
28 #ifdef CONFIG_RISCV_ISA_V
33 static int riscv_gpr_get(struct task_struct *target,
34 const struct user_regset *regset,
37 return membuf_write(&to, task_pt_regs(target),
38 sizeof(struct user_regs_struct));
41 static int riscv_gpr_set(struct task_struct *target,
42 const struct user_regset *regset,
43 unsigned int pos, unsigned int count,
44 const void *kbuf, const void __user *ubuf)
48 regs = task_pt_regs(target);
49 return 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 if (target == current)
60 fstate_save(current, task_pt_regs(current));
62 membuf_write(&to, fstate, offsetof(struct __riscv_d_ext_state, fcsr));
63 membuf_store(&to, fstate->fcsr);
64 return membuf_zero(&to, 4); // explicitly pad
67 static int riscv_fpr_set(struct task_struct *target,
68 const struct user_regset *regset,
69 unsigned int pos, unsigned int count,
70 const void *kbuf, const void __user *ubuf)
73 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
75 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
76 offsetof(struct __riscv_d_ext_state, fcsr));
78 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
79 offsetof(struct __riscv_d_ext_state, fcsr) +
80 sizeof(fstate->fcsr));
87 #ifdef CONFIG_RISCV_ISA_V
88 static int riscv_vr_get(struct task_struct *target,
89 const struct user_regset *regset,
92 struct __riscv_v_ext_state *vstate = &target->thread.vstate;
93 struct __riscv_v_regset_state ptrace_vstate;
95 if (!riscv_v_vstate_query(task_pt_regs(target)))
99 * Ensure the vector registers have been saved to the memory before
100 * copying them to membuf.
102 if (target == current)
103 riscv_v_vstate_save(current, task_pt_regs(current));
105 ptrace_vstate.vstart = vstate->vstart;
106 ptrace_vstate.vl = vstate->vl;
107 ptrace_vstate.vtype = vstate->vtype;
108 ptrace_vstate.vcsr = vstate->vcsr;
109 ptrace_vstate.vlenb = vstate->vlenb;
111 /* Copy vector header from vstate. */
112 membuf_write(&to, &ptrace_vstate, sizeof(struct __riscv_v_regset_state));
114 /* Copy all the vector registers from vstate. */
115 return membuf_write(&to, vstate->datap, riscv_v_vsize);
118 static int riscv_vr_set(struct task_struct *target,
119 const struct user_regset *regset,
120 unsigned int pos, unsigned int count,
121 const void *kbuf, const void __user *ubuf)
124 struct __riscv_v_ext_state *vstate = &target->thread.vstate;
125 struct __riscv_v_regset_state ptrace_vstate;
127 if (!riscv_v_vstate_query(task_pt_regs(target)))
130 /* Copy rest of the vstate except datap */
131 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &ptrace_vstate, 0,
132 sizeof(struct __riscv_v_regset_state));
136 if (vstate->vlenb != ptrace_vstate.vlenb)
139 vstate->vstart = ptrace_vstate.vstart;
140 vstate->vl = ptrace_vstate.vl;
141 vstate->vtype = ptrace_vstate.vtype;
142 vstate->vcsr = ptrace_vstate.vcsr;
144 /* Copy all the vector registers. */
146 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, vstate->datap,
152 static const struct user_regset riscv_user_regset[] = {
154 .core_note_type = NT_PRSTATUS,
156 .size = sizeof(elf_greg_t),
157 .align = sizeof(elf_greg_t),
158 .regset_get = riscv_gpr_get,
159 .set = riscv_gpr_set,
163 .core_note_type = NT_PRFPREG,
165 .size = sizeof(elf_fpreg_t),
166 .align = sizeof(elf_fpreg_t),
167 .regset_get = riscv_fpr_get,
168 .set = riscv_fpr_set,
171 #ifdef CONFIG_RISCV_ISA_V
173 .core_note_type = NT_RISCV_VECTOR,
175 .n = ((32 * RISCV_MAX_VLENB) +
176 sizeof(struct __riscv_v_regset_state)) / sizeof(__u32),
177 .size = sizeof(__u32),
178 .regset_get = riscv_vr_get,
184 static const struct user_regset_view riscv_user_native_view = {
186 .e_machine = EM_RISCV,
187 .regsets = riscv_user_regset,
188 .n = ARRAY_SIZE(riscv_user_regset),
191 struct pt_regs_offset {
196 #define REG_OFFSET_NAME(r) {.name = #r, .offset = offsetof(struct pt_regs, r)}
197 #define REG_OFFSET_END {.name = NULL, .offset = 0}
199 static const struct pt_regs_offset regoffset_table[] = {
200 REG_OFFSET_NAME(epc),
226 REG_OFFSET_NAME(s10),
227 REG_OFFSET_NAME(s11),
232 REG_OFFSET_NAME(status),
233 REG_OFFSET_NAME(badaddr),
234 REG_OFFSET_NAME(cause),
235 REG_OFFSET_NAME(orig_a0),
240 * regs_query_register_offset() - query register offset from its name
241 * @name: the name of a register
243 * regs_query_register_offset() returns the offset of a register in struct
244 * pt_regs from its name. If the name is invalid, this returns -EINVAL;
246 int regs_query_register_offset(const char *name)
248 const struct pt_regs_offset *roff;
250 for (roff = regoffset_table; roff->name != NULL; roff++)
251 if (!strcmp(roff->name, name))
257 * regs_within_kernel_stack() - check the address in the stack
258 * @regs: pt_regs which contains kernel stack pointer.
259 * @addr: address which is checked.
261 * regs_within_kernel_stack() checks @addr is within the kernel stack page(s).
262 * If @addr is within the kernel stack, it returns true. If not, returns false.
264 static bool regs_within_kernel_stack(struct pt_regs *regs, unsigned long addr)
266 return (addr & ~(THREAD_SIZE - 1)) ==
267 (kernel_stack_pointer(regs) & ~(THREAD_SIZE - 1));
271 * regs_get_kernel_stack_nth() - get Nth entry of the stack
272 * @regs: pt_regs which contains kernel stack pointer.
273 * @n: stack entry number.
275 * regs_get_kernel_stack_nth() returns @n th entry of the kernel stack which
276 * is specified by @regs. If the @n th entry is NOT in the kernel stack,
279 unsigned long regs_get_kernel_stack_nth(struct pt_regs *regs, unsigned int n)
281 unsigned long *addr = (unsigned long *)kernel_stack_pointer(regs);
284 if (regs_within_kernel_stack(regs, (unsigned long)addr))
290 void ptrace_disable(struct task_struct *child)
294 long arch_ptrace(struct task_struct *child, long request,
295 unsigned long addr, unsigned long data)
301 ret = ptrace_request(child, request, addr, data);
309 static int compat_riscv_gpr_get(struct task_struct *target,
310 const struct user_regset *regset,
313 struct compat_user_regs_struct cregs;
315 regs_to_cregs(&cregs, task_pt_regs(target));
317 return membuf_write(&to, &cregs,
318 sizeof(struct compat_user_regs_struct));
321 static int compat_riscv_gpr_set(struct task_struct *target,
322 const struct user_regset *regset,
323 unsigned int pos, unsigned int count,
324 const void *kbuf, const void __user *ubuf)
327 struct compat_user_regs_struct cregs;
329 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, &cregs, 0, -1);
331 cregs_to_regs(&cregs, task_pt_regs(target));
336 static const struct user_regset compat_riscv_user_regset[] = {
338 .core_note_type = NT_PRSTATUS,
340 .size = sizeof(compat_elf_greg_t),
341 .align = sizeof(compat_elf_greg_t),
342 .regset_get = compat_riscv_gpr_get,
343 .set = compat_riscv_gpr_set,
347 .core_note_type = NT_PRFPREG,
349 .size = sizeof(elf_fpreg_t),
350 .align = sizeof(elf_fpreg_t),
351 .regset_get = riscv_fpr_get,
352 .set = riscv_fpr_set,
357 static const struct user_regset_view compat_riscv_user_native_view = {
359 .e_machine = EM_RISCV,
360 .regsets = compat_riscv_user_regset,
361 .n = ARRAY_SIZE(compat_riscv_user_regset),
364 long compat_arch_ptrace(struct task_struct *child, compat_long_t request,
365 compat_ulong_t caddr, compat_ulong_t cdata)
371 ret = compat_ptrace_request(child, request, caddr, cdata);
377 #endif /* CONFIG_COMPAT */
379 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
382 if (test_tsk_thread_flag(task, TIF_32BIT))
383 return &compat_riscv_user_native_view;
386 return &riscv_user_native_view;