Linux 5.6.13
[linux/fpc-iii.git] / arch / riscv / kernel / ptrace.c
blob444dc7b0fd78c5cdc04887ffa4f6fea50b99a992
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
8 */
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>
24 enum riscv_regset {
25 REGSET_X,
26 #ifdef CONFIG_FPU
27 REGSET_F,
28 #endif
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)
36 struct pt_regs *regs;
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)
47 int ret;
48 struct pt_regs *regs;
50 regs = task_pt_regs(target);
51 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
52 return ret;
55 #ifdef CONFIG_FPU
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)
61 int ret;
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));
66 if (!ret) {
67 ret = user_regset_copyout(&pos, &count, &kbuf, &ubuf, fstate, 0,
68 offsetof(struct __riscv_d_ext_state, fcsr) +
69 sizeof(fstate->fcsr));
72 return ret;
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)
80 int ret;
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));
85 if (!ret) {
86 ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, fstate, 0,
87 offsetof(struct __riscv_d_ext_state, fcsr) +
88 sizeof(fstate->fcsr));
91 return ret;
93 #endif
95 static const struct user_regset riscv_user_regset[] = {
96 [REGSET_X] = {
97 .core_note_type = NT_PRSTATUS,
98 .n = ELF_NGREG,
99 .size = sizeof(elf_greg_t),
100 .align = sizeof(elf_greg_t),
101 .get = &riscv_gpr_get,
102 .set = &riscv_gpr_set,
104 #ifdef CONFIG_FPU
105 [REGSET_F] = {
106 .core_note_type = NT_PRFPREG,
107 .n = ELF_NFPREG,
108 .size = sizeof(elf_fpreg_t),
109 .align = sizeof(elf_fpreg_t),
110 .get = &riscv_fpr_get,
111 .set = &riscv_fpr_set,
113 #endif
116 static const struct user_regset_view riscv_user_native_view = {
117 .name = "riscv",
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)
136 long ret = -EIO;
138 switch (request) {
139 default:
140 ret = ptrace_request(child, request, addr, data);
141 break;
144 return ret;
148 * Allows PTRACE_SYSCALL to work. These are called from entry.S in
149 * {handle,ret_from}_syscall.
151 __visible int do_syscall_trace_enter(struct pt_regs *regs)
153 if (test_thread_flag(TIF_SYSCALL_TRACE))
154 if (tracehook_report_syscall_entry(regs))
155 return -1;
158 * Do the secure computing after ptrace; failures should be fast.
159 * If this fails we might have return value in a0 from seccomp
160 * (via SECCOMP_RET_ERRNO/TRACE).
162 if (secure_computing() == -1)
163 return -1;
165 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
166 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
167 trace_sys_enter(regs, syscall_get_nr(current, regs));
168 #endif
170 audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3);
171 return 0;
174 __visible void do_syscall_trace_exit(struct pt_regs *regs)
176 audit_syscall_exit(regs);
178 if (test_thread_flag(TIF_SYSCALL_TRACE))
179 tracehook_report_syscall_exit(regs, 0);
181 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
182 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
183 trace_sys_exit(regs, regs_return_value(regs));
184 #endif