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,
33 unsigned int pos, unsigned int count,
34 void *kbuf, void __user *ubuf)
38 regs = task_pt_regs(target);
39 return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
42 static int riscv_gpr_set(struct task_struct *target,
43 const struct user_regset *regset,
44 unsigned int pos, unsigned int count,
45 const void *kbuf, const void __user *ubuf)
50 regs = task_pt_regs(target);
51 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
56 static int riscv_fpr_get(struct task_struct *target,
57 const struct user_regset *regset,
58 unsigned int pos, unsigned int count,
59 void *kbuf, void __user *ubuf)
62 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
64 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0,
65 offsetof(struct __riscv_d_ext_state, fcsr));
67 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0,
68 offsetof(struct __riscv_d_ext_state, fcsr) +
69 sizeof(fstate->fcsr));
75 static int riscv_fpr_set(struct task_struct *target,
76 const struct user_regset *regset,
77 unsigned int pos, unsigned int count,
78 const void *kbuf, const void __user *ubuf)
81 struct __riscv_d_ext_state *fstate = &target->thread.fstate;
83 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
84 offsetof(struct __riscv_d_ext_state, fcsr));
86 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
87 offsetof(struct __riscv_d_ext_state, fcsr) +
88 sizeof(fstate->fcsr));
95 static const struct user_regset riscv_user_regset[] = {
97 .core_note_type = NT_PRSTATUS,
99 .size = sizeof(elf_greg_t),
100 .align = sizeof(elf_greg_t),
101 .get = &riscv_gpr_get,
102 .set = &riscv_gpr_set,
106 .core_note_type = NT_PRFPREG,
108 .size = sizeof(elf_fpreg_t),
109 .align = sizeof(elf_fpreg_t),
110 .get = &riscv_fpr_get,
111 .set = &riscv_fpr_set,
116 static const struct user_regset_view riscv_user_native_view = {
118 .e_machine = EM_RISCV,
119 .regsets = riscv_user_regset,
120 .n = ARRAY_SIZE(riscv_user_regset),
123 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
125 return &riscv_user_native_view;
128 void ptrace_disable(struct task_struct *child)
130 clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
133 long arch_ptrace(struct task_struct *child, long request,
134 unsigned long addr, unsigned long data)
140 ret = ptrace_request(child, request, addr, data);
148 * Allows PTRACE_SYSCALL to work. These are called from entry.S in
149 * {handle,ret_from}_syscall.
151 __visible void do_syscall_trace_enter(struct pt_regs *regs)
153 if (test_thread_flag(TIF_SYSCALL_TRACE))
154 if (tracehook_report_syscall_entry(regs))
155 syscall_set_nr(current, regs, -1);
157 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
158 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
159 trace_sys_enter(regs, syscall_get_nr(current, regs));
162 audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3);
165 __visible void do_syscall_trace_exit(struct pt_regs *regs)
167 audit_syscall_exit(regs);
169 if (test_thread_flag(TIF_SYSCALL_TRACE))
170 tracehook_report_syscall_exit(regs, 0);
172 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
173 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
174 trace_sys_exit(regs, regs_return_value(regs));