xref: /openbmc/linux/arch/riscv/kernel/signal.c (revision 88a6f899)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2009 Sunplus Core Technology Co., Ltd.
4  *  Chen Liqin <liqin.chen@sunplusct.com>
5  *  Lennox Wu <lennox.wu@sunplusct.com>
6  * Copyright (C) 2012 Regents of the University of California
7  */
8 
9 #include <linux/compat.h>
10 #include <linux/signal.h>
11 #include <linux/uaccess.h>
12 #include <linux/syscalls.h>
13 #include <linux/resume_user_mode.h>
14 #include <linux/linkage.h>
15 #include <linux/entry-common.h>
16 
17 #include <asm/ucontext.h>
18 #include <asm/vdso.h>
19 #include <asm/signal.h>
20 #include <asm/signal32.h>
21 #include <asm/switch_to.h>
22 #include <asm/vector.h>
23 #include <asm/csr.h>
24 #include <asm/cacheflush.h>
25 
26 unsigned long signal_minsigstksz __ro_after_init;
27 
28 extern u32 __user_rt_sigreturn[2];
29 static size_t riscv_v_sc_size __ro_after_init;
30 
31 #define DEBUG_SIG 0
32 
33 struct rt_sigframe {
34 	struct siginfo info;
35 	struct ucontext uc;
36 #ifndef CONFIG_MMU
37 	u32 sigreturn_code[2];
38 #endif
39 };
40 
41 #ifdef CONFIG_FPU
42 static long restore_fp_state(struct pt_regs *regs,
43 			     union __riscv_fp_state __user *sc_fpregs)
44 {
45 	long err;
46 	struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
47 
48 	err = __copy_from_user(&current->thread.fstate, state, sizeof(*state));
49 	if (unlikely(err))
50 		return err;
51 
52 	fstate_restore(current, regs);
53 	return 0;
54 }
55 
56 static long save_fp_state(struct pt_regs *regs,
57 			  union __riscv_fp_state __user *sc_fpregs)
58 {
59 	long err;
60 	struct __riscv_d_ext_state __user *state = &sc_fpregs->d;
61 
62 	fstate_save(current, regs);
63 	err = __copy_to_user(state, &current->thread.fstate, sizeof(*state));
64 	return err;
65 }
66 #else
67 #define save_fp_state(task, regs) (0)
68 #define restore_fp_state(task, regs) (0)
69 #endif
70 
71 #ifdef CONFIG_RISCV_ISA_V
72 
73 static long save_v_state(struct pt_regs *regs, void __user **sc_vec)
74 {
75 	struct __riscv_ctx_hdr __user *hdr;
76 	struct __sc_riscv_v_state __user *state;
77 	void __user *datap;
78 	long err;
79 
80 	hdr = *sc_vec;
81 	/* Place state to the user's signal context space after the hdr */
82 	state = (struct __sc_riscv_v_state __user *)(hdr + 1);
83 	/* Point datap right after the end of __sc_riscv_v_state */
84 	datap = state + 1;
85 
86 	/* datap is designed to be 16 byte aligned for better performance */
87 	WARN_ON(unlikely(!IS_ALIGNED((unsigned long)datap, 16)));
88 
89 	riscv_v_vstate_save(current, regs);
90 	/* Copy everything of vstate but datap. */
91 	err = __copy_to_user(&state->v_state, &current->thread.vstate,
92 			     offsetof(struct __riscv_v_ext_state, datap));
93 	/* Copy the pointer datap itself. */
94 	err |= __put_user(datap, &state->v_state.datap);
95 	/* Copy the whole vector content to user space datap. */
96 	err |= __copy_to_user(datap, current->thread.vstate.datap, riscv_v_vsize);
97 	/* Copy magic to the user space after saving  all vector conetext */
98 	err |= __put_user(RISCV_V_MAGIC, &hdr->magic);
99 	err |= __put_user(riscv_v_sc_size, &hdr->size);
100 	if (unlikely(err))
101 		return err;
102 
103 	/* Only progress the sv_vec if everything has done successfully  */
104 	*sc_vec += riscv_v_sc_size;
105 	return 0;
106 }
107 
108 /*
109  * Restore Vector extension context from the user's signal frame. This function
110  * assumes a valid extension header. So magic and size checking must be done by
111  * the caller.
112  */
113 static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec)
114 {
115 	long err;
116 	struct __sc_riscv_v_state __user *state = sc_vec;
117 	void __user *datap;
118 
119 	/* Copy everything of __sc_riscv_v_state except datap. */
120 	err = __copy_from_user(&current->thread.vstate, &state->v_state,
121 			       offsetof(struct __riscv_v_ext_state, datap));
122 	if (unlikely(err))
123 		return err;
124 
125 	/* Copy the pointer datap itself. */
126 	err = __get_user(datap, &state->v_state.datap);
127 	if (unlikely(err))
128 		return err;
129 	/*
130 	 * Copy the whole vector content from user space datap. Use
131 	 * copy_from_user to prevent information leak.
132 	 */
133 	err = copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize);
134 	if (unlikely(err))
135 		return err;
136 
137 	riscv_v_vstate_restore(current, regs);
138 
139 	return err;
140 }
141 #else
142 #define save_v_state(task, regs) (0)
143 #define __restore_v_state(task, regs) (0)
144 #endif
145 
146 static long restore_sigcontext(struct pt_regs *regs,
147 	struct sigcontext __user *sc)
148 {
149 	void __user *sc_ext_ptr = &sc->sc_extdesc.hdr;
150 	__u32 rsvd;
151 	long err;
152 	/* sc_regs is structured the same as the start of pt_regs */
153 	err = __copy_from_user(regs, &sc->sc_regs, sizeof(sc->sc_regs));
154 	if (unlikely(err))
155 		return err;
156 
157 	/* Restore the floating-point state. */
158 	if (has_fpu()) {
159 		err = restore_fp_state(regs, &sc->sc_fpregs);
160 		if (unlikely(err))
161 			return err;
162 	}
163 
164 	/* Check the reserved word before extensions parsing */
165 	err = __get_user(rsvd, &sc->sc_extdesc.reserved);
166 	if (unlikely(err))
167 		return err;
168 	if (unlikely(rsvd))
169 		return -EINVAL;
170 
171 	while (!err) {
172 		__u32 magic, size;
173 		struct __riscv_ctx_hdr __user *head = sc_ext_ptr;
174 
175 		err |= __get_user(magic, &head->magic);
176 		err |= __get_user(size, &head->size);
177 		if (unlikely(err))
178 			return err;
179 
180 		sc_ext_ptr += sizeof(*head);
181 		switch (magic) {
182 		case END_MAGIC:
183 			if (size != END_HDR_SIZE)
184 				return -EINVAL;
185 
186 			return 0;
187 		case RISCV_V_MAGIC:
188 			if (!has_vector() || !riscv_v_vstate_query(regs) ||
189 			    size != riscv_v_sc_size)
190 				return -EINVAL;
191 
192 			err = __restore_v_state(regs, sc_ext_ptr);
193 			break;
194 		default:
195 			return -EINVAL;
196 		}
197 		sc_ext_ptr = (void __user *)head + size;
198 	}
199 	return err;
200 }
201 
202 static size_t get_rt_frame_size(bool cal_all)
203 {
204 	struct rt_sigframe __user *frame;
205 	size_t frame_size;
206 	size_t total_context_size = 0;
207 
208 	frame_size = sizeof(*frame);
209 
210 	if (has_vector()) {
211 		if (cal_all || riscv_v_vstate_query(task_pt_regs(current)))
212 			total_context_size += riscv_v_sc_size;
213 	}
214 	/*
215 	 * Preserved a __riscv_ctx_hdr for END signal context header if an
216 	 * extension uses __riscv_extra_ext_header
217 	 */
218 	if (total_context_size)
219 		total_context_size += sizeof(struct __riscv_ctx_hdr);
220 
221 	frame_size += total_context_size;
222 
223 	frame_size = round_up(frame_size, 16);
224 	return frame_size;
225 }
226 
227 SYSCALL_DEFINE0(rt_sigreturn)
228 {
229 	struct pt_regs *regs = current_pt_regs();
230 	struct rt_sigframe __user *frame;
231 	struct task_struct *task;
232 	sigset_t set;
233 	size_t frame_size = get_rt_frame_size(false);
234 
235 	/* Always make any pending restarted system calls return -EINTR */
236 	current->restart_block.fn = do_no_restart_syscall;
237 
238 	frame = (struct rt_sigframe __user *)regs->sp;
239 
240 	if (!access_ok(frame, frame_size))
241 		goto badframe;
242 
243 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
244 		goto badframe;
245 
246 	set_current_blocked(&set);
247 
248 	if (restore_sigcontext(regs, &frame->uc.uc_mcontext))
249 		goto badframe;
250 
251 	if (restore_altstack(&frame->uc.uc_stack))
252 		goto badframe;
253 
254 	regs->cause = -1UL;
255 
256 	return regs->a0;
257 
258 badframe:
259 	task = current;
260 	if (show_unhandled_signals) {
261 		pr_info_ratelimited(
262 			"%s[%d]: bad frame in %s: frame=%p pc=%p sp=%p\n",
263 			task->comm, task_pid_nr(task), __func__,
264 			frame, (void *)regs->epc, (void *)regs->sp);
265 	}
266 	force_sig(SIGSEGV);
267 	return 0;
268 }
269 
270 static long setup_sigcontext(struct rt_sigframe __user *frame,
271 	struct pt_regs *regs)
272 {
273 	struct sigcontext __user *sc = &frame->uc.uc_mcontext;
274 	struct __riscv_ctx_hdr __user *sc_ext_ptr = &sc->sc_extdesc.hdr;
275 	long err;
276 
277 	/* sc_regs is structured the same as the start of pt_regs */
278 	err = __copy_to_user(&sc->sc_regs, regs, sizeof(sc->sc_regs));
279 	/* Save the floating-point state. */
280 	if (has_fpu())
281 		err |= save_fp_state(regs, &sc->sc_fpregs);
282 	/* Save the vector state. */
283 	if (has_vector() && riscv_v_vstate_query(regs))
284 		err |= save_v_state(regs, (void __user **)&sc_ext_ptr);
285 	/* Write zero to fp-reserved space and check it on restore_sigcontext */
286 	err |= __put_user(0, &sc->sc_extdesc.reserved);
287 	/* And put END __riscv_ctx_hdr at the end. */
288 	err |= __put_user(END_MAGIC, &sc_ext_ptr->magic);
289 	err |= __put_user(END_HDR_SIZE, &sc_ext_ptr->size);
290 
291 	return err;
292 }
293 
294 static inline void __user *get_sigframe(struct ksignal *ksig,
295 	struct pt_regs *regs, size_t framesize)
296 {
297 	unsigned long sp;
298 	/* Default to using normal stack */
299 	sp = regs->sp;
300 
301 	/*
302 	 * If we are on the alternate signal stack and would overflow it, don't.
303 	 * Return an always-bogus address instead so we will die with SIGSEGV.
304 	 */
305 	if (on_sig_stack(sp) && !likely(on_sig_stack(sp - framesize)))
306 		return (void __user __force *)(-1UL);
307 
308 	/* This is the X/Open sanctioned signal stack switching. */
309 	sp = sigsp(sp, ksig) - framesize;
310 
311 	/* Align the stack frame. */
312 	sp &= ~0xfUL;
313 
314 	/*
315 	 * Fail if the size of the altstack is not large enough for the
316 	 * sigframe construction.
317 	 */
318 	if (current->sas_ss_size && sp < current->sas_ss_sp)
319 		return (void __user __force *)-1UL;
320 
321 	return (void __user *)sp;
322 }
323 
324 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
325 	struct pt_regs *regs)
326 {
327 	struct rt_sigframe __user *frame;
328 	long err = 0;
329 	unsigned long __maybe_unused addr;
330 	size_t frame_size = get_rt_frame_size(false);
331 
332 	frame = get_sigframe(ksig, regs, frame_size);
333 	if (!access_ok(frame, frame_size))
334 		return -EFAULT;
335 
336 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
337 
338 	/* Create the ucontext. */
339 	err |= __put_user(0, &frame->uc.uc_flags);
340 	err |= __put_user(NULL, &frame->uc.uc_link);
341 	err |= __save_altstack(&frame->uc.uc_stack, regs->sp);
342 	err |= setup_sigcontext(frame, regs);
343 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
344 	if (err)
345 		return -EFAULT;
346 
347 	/* Set up to return from userspace. */
348 #ifdef CONFIG_MMU
349 	regs->ra = (unsigned long)VDSO_SYMBOL(
350 		current->mm->context.vdso, rt_sigreturn);
351 #else
352 	/*
353 	 * For the nommu case we don't have a VDSO.  Instead we push two
354 	 * instructions to call the rt_sigreturn syscall onto the user stack.
355 	 */
356 	if (copy_to_user(&frame->sigreturn_code, __user_rt_sigreturn,
357 			 sizeof(frame->sigreturn_code)))
358 		return -EFAULT;
359 
360 	addr = (unsigned long)&frame->sigreturn_code;
361 	/* Make sure the two instructions are pushed to icache. */
362 	flush_icache_range(addr, addr + sizeof(frame->sigreturn_code));
363 
364 	regs->ra = addr;
365 #endif /* CONFIG_MMU */
366 
367 	/*
368 	 * Set up registers for signal handler.
369 	 * Registers that we don't modify keep the value they had from
370 	 * user-space at the time we took the signal.
371 	 * We always pass siginfo and mcontext, regardless of SA_SIGINFO,
372 	 * since some things rely on this (e.g. glibc's debug/segfault.c).
373 	 */
374 	regs->epc = (unsigned long)ksig->ka.sa.sa_handler;
375 	regs->sp = (unsigned long)frame;
376 	regs->a0 = ksig->sig;                     /* a0: signal number */
377 	regs->a1 = (unsigned long)(&frame->info); /* a1: siginfo pointer */
378 	regs->a2 = (unsigned long)(&frame->uc);   /* a2: ucontext pointer */
379 
380 #if DEBUG_SIG
381 	pr_info("SIG deliver (%s:%d): sig=%d pc=%p ra=%p sp=%p\n",
382 		current->comm, task_pid_nr(current), ksig->sig,
383 		(void *)regs->epc, (void *)regs->ra, frame);
384 #endif
385 
386 	return 0;
387 }
388 
389 static void handle_signal(struct ksignal *ksig, struct pt_regs *regs)
390 {
391 	sigset_t *oldset = sigmask_to_save();
392 	int ret;
393 
394 	/* Are we from a system call? */
395 	if (regs->cause == EXC_SYSCALL) {
396 		/* Avoid additional syscall restarting via ret_from_exception */
397 		regs->cause = -1UL;
398 		/* If so, check system call restarting.. */
399 		switch (regs->a0) {
400 		case -ERESTART_RESTARTBLOCK:
401 		case -ERESTARTNOHAND:
402 			regs->a0 = -EINTR;
403 			break;
404 
405 		case -ERESTARTSYS:
406 			if (!(ksig->ka.sa.sa_flags & SA_RESTART)) {
407 				regs->a0 = -EINTR;
408 				break;
409 			}
410 			fallthrough;
411 		case -ERESTARTNOINTR:
412                         regs->a0 = regs->orig_a0;
413 			regs->epc -= 0x4;
414 			break;
415 		}
416 	}
417 
418 	rseq_signal_deliver(ksig, regs);
419 
420 	/* Set up the stack frame */
421 	if (is_compat_task())
422 		ret = compat_setup_rt_frame(ksig, oldset, regs);
423 	else
424 		ret = setup_rt_frame(ksig, oldset, regs);
425 
426 	signal_setup_done(ret, ksig, 0);
427 }
428 
429 void arch_do_signal_or_restart(struct pt_regs *regs)
430 {
431 	struct ksignal ksig;
432 
433 	if (get_signal(&ksig)) {
434 		/* Actually deliver the signal */
435 		handle_signal(&ksig, regs);
436 		return;
437 	}
438 
439 	/* Did we come from a system call? */
440 	if (regs->cause == EXC_SYSCALL) {
441 		/* Avoid additional syscall restarting via ret_from_exception */
442 		regs->cause = -1UL;
443 
444 		/* Restart the system call - no handlers present */
445 		switch (regs->a0) {
446 		case -ERESTARTNOHAND:
447 		case -ERESTARTSYS:
448 		case -ERESTARTNOINTR:
449                         regs->a0 = regs->orig_a0;
450 			regs->epc -= 0x4;
451 			break;
452 		case -ERESTART_RESTARTBLOCK:
453                         regs->a0 = regs->orig_a0;
454 			regs->a7 = __NR_restart_syscall;
455 			regs->epc -= 0x4;
456 			break;
457 		}
458 	}
459 
460 	/*
461 	 * If there is no signal to deliver, we just put the saved
462 	 * sigmask back.
463 	 */
464 	restore_saved_sigmask();
465 }
466 
467 void init_rt_signal_env(void);
468 void __init init_rt_signal_env(void)
469 {
470 	riscv_v_sc_size = sizeof(struct __riscv_ctx_hdr) +
471 			  sizeof(struct __sc_riscv_v_state) + riscv_v_vsize;
472 	/*
473 	 * Determine the stack space required for guaranteed signal delivery.
474 	 * The signal_minsigstksz will be populated into the AT_MINSIGSTKSZ entry
475 	 * in the auxiliary array at process startup.
476 	 */
477 	signal_minsigstksz = get_rt_frame_size(true);
478 }
479 
480 #ifdef CONFIG_DYNAMIC_SIGFRAME
481 bool sigaltstack_size_valid(size_t ss_size)
482 {
483 	return ss_size > get_rt_frame_size(false);
484 }
485 #endif /* CONFIG_DYNAMIC_SIGFRAME */
486