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
,
35 return membuf_write(&to
, task_pt_regs(target
),
36 sizeof(struct user_regs_struct
));
39 static int riscv_gpr_set(struct task_struct
*target
,
40 const struct user_regset
*regset
,
41 unsigned int pos
, unsigned int count
,
42 const void *kbuf
, const void __user
*ubuf
)
47 regs
= task_pt_regs(target
);
48 ret
= 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 membuf_write(&to
, fstate
, offsetof(struct __riscv_d_ext_state
, fcsr
));
60 membuf_store(&to
, fstate
->fcsr
);
61 return membuf_zero(&to
, 4); // explicitly pad
64 static int riscv_fpr_set(struct task_struct
*target
,
65 const struct user_regset
*regset
,
66 unsigned int pos
, unsigned int count
,
67 const void *kbuf
, const void __user
*ubuf
)
70 struct __riscv_d_ext_state
*fstate
= &target
->thread
.fstate
;
72 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, fstate
, 0,
73 offsetof(struct __riscv_d_ext_state
, fcsr
));
75 ret
= user_regset_copyin(&pos
, &count
, &kbuf
, &ubuf
, fstate
, 0,
76 offsetof(struct __riscv_d_ext_state
, fcsr
) +
77 sizeof(fstate
->fcsr
));
84 static const struct user_regset riscv_user_regset
[] = {
86 .core_note_type
= NT_PRSTATUS
,
88 .size
= sizeof(elf_greg_t
),
89 .align
= sizeof(elf_greg_t
),
90 .regset_get
= riscv_gpr_get
,
95 .core_note_type
= NT_PRFPREG
,
97 .size
= sizeof(elf_fpreg_t
),
98 .align
= sizeof(elf_fpreg_t
),
99 .regset_get
= riscv_fpr_get
,
100 .set
= riscv_fpr_set
,
105 static const struct user_regset_view riscv_user_native_view
= {
107 .e_machine
= EM_RISCV
,
108 .regsets
= riscv_user_regset
,
109 .n
= ARRAY_SIZE(riscv_user_regset
),
112 const struct user_regset_view
*task_user_regset_view(struct task_struct
*task
)
114 return &riscv_user_native_view
;
117 void ptrace_disable(struct task_struct
*child
)
119 clear_tsk_thread_flag(child
, TIF_SYSCALL_TRACE
);
122 long arch_ptrace(struct task_struct
*child
, long request
,
123 unsigned long addr
, unsigned long data
)
129 ret
= ptrace_request(child
, request
, addr
, data
);
137 * Allows PTRACE_SYSCALL to work. These are called from entry.S in
138 * {handle,ret_from}_syscall.
140 __visible
int do_syscall_trace_enter(struct pt_regs
*regs
)
142 if (test_thread_flag(TIF_SYSCALL_TRACE
))
143 if (tracehook_report_syscall_entry(regs
))
147 * Do the secure computing after ptrace; failures should be fast.
148 * If this fails we might have return value in a0 from seccomp
149 * (via SECCOMP_RET_ERRNO/TRACE).
151 if (secure_computing() == -1)
154 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
155 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT
))
156 trace_sys_enter(regs
, syscall_get_nr(current
, regs
));
159 audit_syscall_entry(regs
->a7
, regs
->a0
, regs
->a1
, regs
->a2
, regs
->a3
);
163 __visible
void do_syscall_trace_exit(struct pt_regs
*regs
)
165 audit_syscall_exit(regs
);
167 if (test_thread_flag(TIF_SYSCALL_TRACE
))
168 tracehook_report_syscall_exit(regs
, 0);
170 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
171 if (test_thread_flag(TIF_SYSCALL_TRACEPOINT
))
172 trace_sys_exit(regs
, regs_return_value(regs
));