xref: /openbmc/linux/arch/riscv/kernel/ptrace.c (revision 22d55f02)
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
6  *
7  * Copied from arch/tile/kernel/ptrace.c
8  */
9 
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>
20 
21 #define CREATE_TRACE_POINTS
22 #include <trace/events/syscalls.h>
23 
24 enum riscv_regset {
25 	REGSET_X,
26 #ifdef CONFIG_FPU
27 	REGSET_F,
28 #endif
29 };
30 
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)
35 {
36 	struct pt_regs *regs;
37 
38 	regs = task_pt_regs(target);
39 	return user_regset_copyout(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
40 }
41 
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)
46 {
47 	int ret;
48 	struct pt_regs *regs;
49 
50 	regs = task_pt_regs(target);
51 	ret = user_regset_copyin(&pos, &count, &kbuf, &ubuf, regs, 0, -1);
52 	return ret;
53 }
54 
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)
60 {
61 	int ret;
62 	struct __riscv_d_ext_state *fstate = &target->thread.fstate;
63 
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));
70 	}
71 
72 	return ret;
73 }
74 
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)
79 {
80 	int ret;
81 	struct __riscv_d_ext_state *fstate = &target->thread.fstate;
82 
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));
89 	}
90 
91 	return ret;
92 }
93 #endif
94 
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,
103 	},
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,
112 	},
113 #endif
114 };
115 
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),
121 };
122 
123 const struct user_regset_view *task_user_regset_view(struct task_struct *task)
124 {
125 	return &riscv_user_native_view;
126 }
127 
128 void ptrace_disable(struct task_struct *child)
129 {
130 	clear_tsk_thread_flag(child, TIF_SYSCALL_TRACE);
131 }
132 
133 long arch_ptrace(struct task_struct *child, long request,
134 		 unsigned long addr, unsigned long data)
135 {
136 	long ret = -EIO;
137 
138 	switch (request) {
139 	default:
140 		ret = ptrace_request(child, request, addr, data);
141 		break;
142 	}
143 
144 	return ret;
145 }
146 
147 /*
148  * Allows PTRACE_SYSCALL to work.  These are called from entry.S in
149  * {handle,ret_from}_syscall.
150  */
151 void do_syscall_trace_enter(struct pt_regs *regs)
152 {
153 	if (test_thread_flag(TIF_SYSCALL_TRACE))
154 		if (tracehook_report_syscall_entry(regs))
155 			syscall_set_nr(current, regs, -1);
156 
157 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
158 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
159 		trace_sys_enter(regs, syscall_get_nr(current, regs));
160 #endif
161 
162 	audit_syscall_entry(regs->a7, regs->a0, regs->a1, regs->a2, regs->a3);
163 }
164 
165 void do_syscall_trace_exit(struct pt_regs *regs)
166 {
167 	audit_syscall_exit(regs);
168 
169 	if (test_thread_flag(TIF_SYSCALL_TRACE))
170 		tracehook_report_syscall_exit(regs, 0);
171 
172 #ifdef CONFIG_HAVE_SYSCALL_TRACEPOINTS
173 	if (test_thread_flag(TIF_SYSCALL_TRACEPOINT))
174 		trace_sys_exit(regs, regs_return_value(regs));
175 #endif
176 }
177