1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/signal.h>
23#include <linux/uaccess.h>
24#include <linux/syscalls.h>
25#include <linux/tracehook.h>
26#include <linux/linkage.h>
27
28#include <asm/ucontext.h>
29#include <asm/vdso.h>
30#include <asm/switch_to.h>
31#include <asm/csr.h>
32
33#define DEBUG_SIG 0
34
35struct rt_sigframe {
36 struct siginfo info;
37 struct ucontext uc;
38};
39
40#ifdef CONFIG_FPU
41static long restore_fp_state(struct pt_regs *regs,
42 union __riscv_fp_state *sc_fpregs)
43{
44 long err;
45 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
46 size_t i;
47
48 err = __copy_from_user(¤t->thread.fstate, state, sizeof(*state));
49 if (unlikely(err))
50 return err;
51
52 fstate_restore(current, regs);
53
54
55 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
56 u32 value;
57
58 err = __get_user(value, &sc_fpregs->q.reserved[i]);
59 if (unlikely(err))
60 break;
61 if (value != 0)
62 return -EINVAL;
63 }
64
65 return err;
66}
67
68static long save_fp_state(struct pt_regs *regs,
69 union __riscv_fp_state *sc_fpregs)
70{
71 long err;
72 struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
73 size_t i;
74
75 fstate_save(current, regs);
76 err = __copy_to_user(state, ¤t->thread.fstate, sizeof(*state));
77 if (unlikely(err))
78 return err;
79
80
81 for (i = 0; i < ARRAY_SIZE(sc_fpregs->q.reserved); i++) {
82 err = __put_user(0, &sc_fpregs->q.reserved[i]);
83 if (unlikely(err))
84 break;
85 }
86
87 return err;
88}
89#else
90#define save_fp_state(task, regs) (0)
91#define restore_fp_state(task, regs) (0)
92#endif
93
94static long restore_sigcontext(struct pt_regs *regs,
95 struct sigcontext __user *sc)
96{
97 long err;
98
99 err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
100
101 if (has_fpu)
102 err |= restore_fp_state(regs, &sc->sc_fpregs);
103 return err;
104}
105
106SYSCALL_DEFINE0(rt_sigreturn)
107{
108 struct pt_regs *regs = current_pt_regs();
109 struct rt_sigframe __user *frame;
110 struct task_struct *task;
111 sigset_t set;
112
113
114 current->restart_block.fn = do_no_restart_syscall;
115
116 frame = (struct rt_sigframe __user *)regs->sp;
117
118 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
119 goto badframe;
120
121 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
122 goto badframe;
123
124 set_current_blocked(&set);
125
126 if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
127 goto badframe;
128
129 if (restore_altstack(&frame->uc.uc_stack))
130 goto badframe;
131
132 return regs->a0;
133
134badframe:
135 task = current;
136 if (show_unhandled_signals) {
137 pr_info_ratelimited(
138 "%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
139 task->comm, task_pid_nr(task), __func__,
140 frame, (void *)regs->sepc, (void *)regs->sp);
141 }
142 force_sig(SIGSEGV, task);
143 return 0;
144}
145
146static long setup_sigcontext(struct rt_sigframe __user *frame,
147 struct pt_regs *regs)
148{
149 struct sigcontext __user *sc = &frame->uc.uc_mcontext;
150 long err;
151
152 err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
153
154 if (has_fpu)
155 err |= save_fp_state(regs, &sc->sc_fpregs);
156 return err;
157}
158
159static inline void __user *get_sigframe(struct ksignal *ksig,
160 struct pt_regs *regs, size_t framesize)
161{
162 unsigned long sp;
163
164 sp = regs->sp;
165
166
167
168
169
170 if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
171 return (void __user __force *)(-1UL);
172
173
174 sp = sigsp(sp, ksig) - framesize;
175
176
177 sp &= ~0xfUL;
178
179 return (void __user *)sp;
180}
181
182
183static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
184 struct pt_regs *regs)
185{
186 struct rt_sigframe __user *frame;
187 long err = 0;
188
189 frame = get_sigframe(ksig, regs, sizeof(*frame));
190 if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
191 return -EFAULT;
192
193 err |= copy_siginfo_to_user(&frame->info, &ksig->info);
194
195
196 err |= __put_user(0, &frame->uc.uc_flags);
197 err |= __put_user(NULL, &frame->uc.uc_link);
198 err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
199 err |= setup_sigcontext(frame, regs);
200 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
201 if (err)
202 return -EFAULT;
203
204
205 regs->ra = (unsigned long)VDSO_SYMBOL(
206 current->mm->context.vdso, rt_sigreturn);
207
208
209
210
211
212
213
214
215 regs->sepc = (unsigned long)ksig->ka.sa.sa_handler;
216 regs->sp = (unsigned long)frame;
217 regs->a0 = ksig->sig;
218 regs->a1 = (unsigned long)(&frame->info);
219 regs->a2 = (unsigned long)(&frame->uc);
220
221#if DEBUG_SIG
222 pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
223 current->comm, task_pid_nr(current), ksig->sig,
224 (void *)regs->sepc, (void *)regs->ra, frame);
225#endif
226
227 return 0;
228}
229
230static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
231{
232 sigset_t *oldset = sigmask_to_save();
233 int ret;
234
235
236 if (regs->scause == EXC_SYSCALL) {
237
238 switch (regs->a0) {
239 case -ERESTART_RESTARTBLOCK:
240 case -ERESTARTNOHAND:
241 regs->a0 = -EINTR;
242 break;
243
244 case -ERESTARTSYS:
245 if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
246 regs->a0 = -EINTR;
247 break;
248 }
249
250 case -ERESTARTNOINTR:
251 regs->a0 = regs->orig_a0;
252 regs->sepc -= 0x4;
253 break;
254 }
255 }
256
257
258 ret = setup_rt_frame(ksig, oldset, regs);
259
260 signal_setup_done(ret, ksig, 0);
261}
262
263static void do_signal(struct pt_regs *regs)
264{
265 struct ksignal ksig;
266
267 if (get_signal(&ksig)) {
268
269 handle_signal(&ksig, regs);
270 return;
271 }
272
273
274 if (regs->scause == EXC_SYSCALL) {
275
276 switch (regs->a0) {
277 case -ERESTARTNOHAND:
278 case -ERESTARTSYS:
279 case -ERESTARTNOINTR:
280 regs->a0 = regs->orig_a0;
281 regs->sepc -= 0x4;
282 break;
283 case -ERESTART_RESTARTBLOCK:
284 regs->a0 = regs->orig_a0;
285 regs->a7 = __NR_restart_syscall;
286 regs->sepc -= 0x4;
287 break;
288 }
289 }
290
291
292
293
294
295 restore_saved_sigmask();
296}
297
298
299
300
301
302asmlinkage void do_notify_resume(struct pt_regs *regs,
303 unsigned long thread_info_flags)
304{
305
306 if (thread_info_flags & _TIF_SIGPENDING)
307 do_signal(regs);
308
309 if (thread_info_flags & _TIF_NOTIFY_RESUME) {
310 clear_thread_flag(TIF_NOTIFY_RESUME);
311 tracehook_notify_resume(regs);
312 }
313}
314