xref: /openbmc/linux/arch/alpha/kernel/signal.c (revision 82e6fdd6)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  linux/arch/alpha/kernel/signal.c
4  *
5  *  Copyright (C) 1995  Linus Torvalds
6  *
7  *  1997-11-02  Modified for POSIX.1b signals by Richard Henderson
8  */
9 
10 #include <linux/sched/signal.h>
11 #include <linux/sched/task_stack.h>
12 #include <linux/kernel.h>
13 #include <linux/signal.h>
14 #include <linux/errno.h>
15 #include <linux/wait.h>
16 #include <linux/ptrace.h>
17 #include <linux/unistd.h>
18 #include <linux/mm.h>
19 #include <linux/smp.h>
20 #include <linux/stddef.h>
21 #include <linux/tty.h>
22 #include <linux/binfmts.h>
23 #include <linux/bitops.h>
24 #include <linux/syscalls.h>
25 #include <linux/tracehook.h>
26 
27 #include <linux/uaccess.h>
28 #include <asm/sigcontext.h>
29 #include <asm/ucontext.h>
30 
31 #include "proto.h"
32 
33 
34 #define DEBUG_SIG 0
35 
36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
37 
38 asmlinkage void ret_from_sys_call(void);
39 
40 /*
41  * The OSF/1 sigprocmask calling sequence is different from the
42  * C sigprocmask() sequence..
43  */
44 SYSCALL_DEFINE2(osf_sigprocmask, int, how, unsigned long, newmask)
45 {
46 	sigset_t oldmask;
47 	sigset_t mask;
48 	unsigned long res;
49 
50 	siginitset(&mask, newmask & _BLOCKABLE);
51 	res = sigprocmask(how, &mask, &oldmask);
52 	if (!res) {
53 		force_successful_syscall_return();
54 		res = oldmask.sig[0];
55 	}
56 	return res;
57 }
58 
59 SYSCALL_DEFINE3(osf_sigaction, int, sig,
60 		const struct osf_sigaction __user *, act,
61 		struct osf_sigaction __user *, oact)
62 {
63 	struct k_sigaction new_ka, old_ka;
64 	int ret;
65 
66 	if (act) {
67 		old_sigset_t mask;
68 		if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
69 		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
70 		    __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
71 		    __get_user(mask, &act->sa_mask))
72 			return -EFAULT;
73 		siginitset(&new_ka.sa.sa_mask, mask);
74 		new_ka.ka_restorer = NULL;
75 	}
76 
77 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
78 
79 	if (!ret && oact) {
80 		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
81 		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
82 		    __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
83 		    __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
84 			return -EFAULT;
85 	}
86 
87 	return ret;
88 }
89 
90 SYSCALL_DEFINE5(rt_sigaction, int, sig, const struct sigaction __user *, act,
91 		struct sigaction __user *, oact,
92 		size_t, sigsetsize, void __user *, restorer)
93 {
94 	struct k_sigaction new_ka, old_ka;
95 	int ret;
96 
97 	/* XXX: Don't preclude handling different sized sigset_t's.  */
98 	if (sigsetsize != sizeof(sigset_t))
99 		return -EINVAL;
100 
101 	if (act) {
102 		new_ka.ka_restorer = restorer;
103 		if (copy_from_user(&new_ka.sa, act, sizeof(*act)))
104 			return -EFAULT;
105 	}
106 
107 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
108 
109 	if (!ret && oact) {
110 		if (copy_to_user(oact, &old_ka.sa, sizeof(*oact)))
111 			return -EFAULT;
112 	}
113 
114 	return ret;
115 }
116 
117 /*
118  * Do a signal return; undo the signal stack.
119  */
120 
121 #if _NSIG_WORDS > 1
122 # error "Non SA_SIGINFO frame needs rearranging"
123 #endif
124 
125 struct sigframe
126 {
127 	struct sigcontext sc;
128 	unsigned int retcode[3];
129 };
130 
131 struct rt_sigframe
132 {
133 	struct siginfo info;
134 	struct ucontext uc;
135 	unsigned int retcode[3];
136 };
137 
138 /* If this changes, userland unwinders that Know Things about our signal
139    frame will break.  Do not undertake lightly.  It also implies an ABI
140    change wrt the size of siginfo_t, which may cause some pain.  */
141 extern char compile_time_assert
142         [offsetof(struct rt_sigframe, uc.uc_mcontext) == 176 ? 1 : -1];
143 
144 #define INSN_MOV_R30_R16	0x47fe0410
145 #define INSN_LDI_R0		0x201f0000
146 #define INSN_CALLSYS		0x00000083
147 
148 static long
149 restore_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs)
150 {
151 	unsigned long usp;
152 	struct switch_stack *sw = (struct switch_stack *)regs - 1;
153 	long i, err = __get_user(regs->pc, &sc->sc_pc);
154 
155 	current->restart_block.fn = do_no_restart_syscall;
156 
157 	sw->r26 = (unsigned long) ret_from_sys_call;
158 
159 	err |= __get_user(regs->r0, sc->sc_regs+0);
160 	err |= __get_user(regs->r1, sc->sc_regs+1);
161 	err |= __get_user(regs->r2, sc->sc_regs+2);
162 	err |= __get_user(regs->r3, sc->sc_regs+3);
163 	err |= __get_user(regs->r4, sc->sc_regs+4);
164 	err |= __get_user(regs->r5, sc->sc_regs+5);
165 	err |= __get_user(regs->r6, sc->sc_regs+6);
166 	err |= __get_user(regs->r7, sc->sc_regs+7);
167 	err |= __get_user(regs->r8, sc->sc_regs+8);
168 	err |= __get_user(sw->r9, sc->sc_regs+9);
169 	err |= __get_user(sw->r10, sc->sc_regs+10);
170 	err |= __get_user(sw->r11, sc->sc_regs+11);
171 	err |= __get_user(sw->r12, sc->sc_regs+12);
172 	err |= __get_user(sw->r13, sc->sc_regs+13);
173 	err |= __get_user(sw->r14, sc->sc_regs+14);
174 	err |= __get_user(sw->r15, sc->sc_regs+15);
175 	err |= __get_user(regs->r16, sc->sc_regs+16);
176 	err |= __get_user(regs->r17, sc->sc_regs+17);
177 	err |= __get_user(regs->r18, sc->sc_regs+18);
178 	err |= __get_user(regs->r19, sc->sc_regs+19);
179 	err |= __get_user(regs->r20, sc->sc_regs+20);
180 	err |= __get_user(regs->r21, sc->sc_regs+21);
181 	err |= __get_user(regs->r22, sc->sc_regs+22);
182 	err |= __get_user(regs->r23, sc->sc_regs+23);
183 	err |= __get_user(regs->r24, sc->sc_regs+24);
184 	err |= __get_user(regs->r25, sc->sc_regs+25);
185 	err |= __get_user(regs->r26, sc->sc_regs+26);
186 	err |= __get_user(regs->r27, sc->sc_regs+27);
187 	err |= __get_user(regs->r28, sc->sc_regs+28);
188 	err |= __get_user(regs->gp, sc->sc_regs+29);
189 	err |= __get_user(usp, sc->sc_regs+30);
190 	wrusp(usp);
191 
192 	for (i = 0; i < 31; i++)
193 		err |= __get_user(sw->fp[i], sc->sc_fpregs+i);
194 	err |= __get_user(sw->fp[31], &sc->sc_fpcr);
195 
196 	return err;
197 }
198 
199 /* Note that this syscall is also used by setcontext(3) to install
200    a given sigcontext.  This because it's impossible to set *all*
201    registers and transfer control from userland.  */
202 
203 asmlinkage void
204 do_sigreturn(struct sigcontext __user *sc)
205 {
206 	struct pt_regs *regs = current_pt_regs();
207 	sigset_t set;
208 
209 	/* Verify that it's a good sigcontext before using it */
210 	if (!access_ok(VERIFY_READ, sc, sizeof(*sc)))
211 		goto give_sigsegv;
212 	if (__get_user(set.sig[0], &sc->sc_mask))
213 		goto give_sigsegv;
214 
215 	set_current_blocked(&set);
216 
217 	if (restore_sigcontext(sc, regs))
218 		goto give_sigsegv;
219 
220 	/* Send SIGTRAP if we're single-stepping: */
221 	if (ptrace_cancel_bpt (current)) {
222 		siginfo_t info;
223 
224 		info.si_signo = SIGTRAP;
225 		info.si_errno = 0;
226 		info.si_code = TRAP_BRKPT;
227 		info.si_addr = (void __user *) regs->pc;
228 		info.si_trapno = 0;
229 		send_sig_info(SIGTRAP, &info, current);
230 	}
231 	return;
232 
233 give_sigsegv:
234 	force_sig(SIGSEGV, current);
235 }
236 
237 asmlinkage void
238 do_rt_sigreturn(struct rt_sigframe __user *frame)
239 {
240 	struct pt_regs *regs = current_pt_regs();
241 	sigset_t set;
242 
243 	/* Verify that it's a good ucontext_t before using it */
244 	if (!access_ok(VERIFY_READ, &frame->uc, sizeof(frame->uc)))
245 		goto give_sigsegv;
246 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
247 		goto give_sigsegv;
248 
249 	set_current_blocked(&set);
250 
251 	if (restore_sigcontext(&frame->uc.uc_mcontext, regs))
252 		goto give_sigsegv;
253 
254 	/* Send SIGTRAP if we're single-stepping: */
255 	if (ptrace_cancel_bpt (current)) {
256 		siginfo_t info;
257 
258 		info.si_signo = SIGTRAP;
259 		info.si_errno = 0;
260 		info.si_code = TRAP_BRKPT;
261 		info.si_addr = (void __user *) regs->pc;
262 		info.si_trapno = 0;
263 		send_sig_info(SIGTRAP, &info, current);
264 	}
265 	return;
266 
267 give_sigsegv:
268 	force_sig(SIGSEGV, current);
269 }
270 
271 
272 /*
273  * Set up a signal frame.
274  */
275 
276 static inline void __user *
277 get_sigframe(struct ksignal *ksig, unsigned long sp, size_t frame_size)
278 {
279 	return (void __user *)((sigsp(sp, ksig) - frame_size) & -32ul);
280 }
281 
282 static long
283 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
284 		 unsigned long mask, unsigned long sp)
285 {
286 	struct switch_stack *sw = (struct switch_stack *)regs - 1;
287 	long i, err = 0;
288 
289 	err |= __put_user(on_sig_stack((unsigned long)sc), &sc->sc_onstack);
290 	err |= __put_user(mask, &sc->sc_mask);
291 	err |= __put_user(regs->pc, &sc->sc_pc);
292 	err |= __put_user(8, &sc->sc_ps);
293 
294 	err |= __put_user(regs->r0 , sc->sc_regs+0);
295 	err |= __put_user(regs->r1 , sc->sc_regs+1);
296 	err |= __put_user(regs->r2 , sc->sc_regs+2);
297 	err |= __put_user(regs->r3 , sc->sc_regs+3);
298 	err |= __put_user(regs->r4 , sc->sc_regs+4);
299 	err |= __put_user(regs->r5 , sc->sc_regs+5);
300 	err |= __put_user(regs->r6 , sc->sc_regs+6);
301 	err |= __put_user(regs->r7 , sc->sc_regs+7);
302 	err |= __put_user(regs->r8 , sc->sc_regs+8);
303 	err |= __put_user(sw->r9   , sc->sc_regs+9);
304 	err |= __put_user(sw->r10  , sc->sc_regs+10);
305 	err |= __put_user(sw->r11  , sc->sc_regs+11);
306 	err |= __put_user(sw->r12  , sc->sc_regs+12);
307 	err |= __put_user(sw->r13  , sc->sc_regs+13);
308 	err |= __put_user(sw->r14  , sc->sc_regs+14);
309 	err |= __put_user(sw->r15  , sc->sc_regs+15);
310 	err |= __put_user(regs->r16, sc->sc_regs+16);
311 	err |= __put_user(regs->r17, sc->sc_regs+17);
312 	err |= __put_user(regs->r18, sc->sc_regs+18);
313 	err |= __put_user(regs->r19, sc->sc_regs+19);
314 	err |= __put_user(regs->r20, sc->sc_regs+20);
315 	err |= __put_user(regs->r21, sc->sc_regs+21);
316 	err |= __put_user(regs->r22, sc->sc_regs+22);
317 	err |= __put_user(regs->r23, sc->sc_regs+23);
318 	err |= __put_user(regs->r24, sc->sc_regs+24);
319 	err |= __put_user(regs->r25, sc->sc_regs+25);
320 	err |= __put_user(regs->r26, sc->sc_regs+26);
321 	err |= __put_user(regs->r27, sc->sc_regs+27);
322 	err |= __put_user(regs->r28, sc->sc_regs+28);
323 	err |= __put_user(regs->gp , sc->sc_regs+29);
324 	err |= __put_user(sp, sc->sc_regs+30);
325 	err |= __put_user(0, sc->sc_regs+31);
326 
327 	for (i = 0; i < 31; i++)
328 		err |= __put_user(sw->fp[i], sc->sc_fpregs+i);
329 	err |= __put_user(0, sc->sc_fpregs+31);
330 	err |= __put_user(sw->fp[31], &sc->sc_fpcr);
331 
332 	err |= __put_user(regs->trap_a0, &sc->sc_traparg_a0);
333 	err |= __put_user(regs->trap_a1, &sc->sc_traparg_a1);
334 	err |= __put_user(regs->trap_a2, &sc->sc_traparg_a2);
335 
336 	return err;
337 }
338 
339 static int
340 setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
341 {
342 	unsigned long oldsp, r26, err = 0;
343 	struct sigframe __user *frame;
344 
345 	oldsp = rdusp();
346 	frame = get_sigframe(ksig, oldsp, sizeof(*frame));
347 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
348 		return -EFAULT;
349 
350 	err |= setup_sigcontext(&frame->sc, regs, set->sig[0], oldsp);
351 	if (err)
352 		return -EFAULT;
353 
354 	/* Set up to return from userspace.  If provided, use a stub
355 	   already in userspace.  */
356 	r26 = (unsigned long) ksig->ka.ka_restorer;
357 	if (!r26) {
358 		err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0);
359 		err |= __put_user(INSN_LDI_R0+__NR_sigreturn, frame->retcode+1);
360 		err |= __put_user(INSN_CALLSYS, frame->retcode+2);
361 		imb();
362 		r26 = (unsigned long) frame->retcode;
363 	}
364 
365 	/* Check that everything was written properly.  */
366 	if (err)
367 		return err;
368 
369 	/* "Return" to the handler */
370 	regs->r26 = r26;
371 	regs->r27 = regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
372 	regs->r16 = ksig->sig;			/* a0: signal number */
373 	regs->r17 = 0;				/* a1: exception code */
374 	regs->r18 = (unsigned long) &frame->sc;	/* a2: sigcontext pointer */
375 	wrusp((unsigned long) frame);
376 
377 #if DEBUG_SIG
378 	printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
379 		current->comm, current->pid, frame, regs->pc, regs->r26);
380 #endif
381 	return 0;
382 }
383 
384 static int
385 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
386 {
387 	unsigned long oldsp, r26, err = 0;
388 	struct rt_sigframe __user *frame;
389 
390 	oldsp = rdusp();
391 	frame = get_sigframe(ksig, oldsp, sizeof(*frame));
392 	if (!access_ok(VERIFY_WRITE, frame, sizeof(*frame)))
393 		return -EFAULT;
394 
395 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
396 
397 	/* Create the ucontext.  */
398 	err |= __put_user(0, &frame->uc.uc_flags);
399 	err |= __put_user(0, &frame->uc.uc_link);
400 	err |= __put_user(set->sig[0], &frame->uc.uc_osf_sigmask);
401 	err |= __save_altstack(&frame->uc.uc_stack, oldsp);
402 	err |= setup_sigcontext(&frame->uc.uc_mcontext, regs,
403 				set->sig[0], oldsp);
404 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
405 	if (err)
406 		return -EFAULT;
407 
408 	/* Set up to return from userspace.  If provided, use a stub
409 	   already in userspace.  */
410 	r26 = (unsigned long) ksig->ka.ka_restorer;
411 	if (!r26) {
412 		err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0);
413 		err |= __put_user(INSN_LDI_R0+__NR_rt_sigreturn,
414 				  frame->retcode+1);
415 		err |= __put_user(INSN_CALLSYS, frame->retcode+2);
416 		imb();
417 		r26 = (unsigned long) frame->retcode;
418 	}
419 
420 	if (err)
421 		return -EFAULT;
422 
423 	/* "Return" to the handler */
424 	regs->r26 = r26;
425 	regs->r27 = regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
426 	regs->r16 = ksig->sig;			  /* a0: signal number */
427 	regs->r17 = (unsigned long) &frame->info; /* a1: siginfo pointer */
428 	regs->r18 = (unsigned long) &frame->uc;	  /* a2: ucontext pointer */
429 	wrusp((unsigned long) frame);
430 
431 #if DEBUG_SIG
432 	printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
433 		current->comm, current->pid, frame, regs->pc, regs->r26);
434 #endif
435 
436 	return 0;
437 }
438 
439 
440 /*
441  * OK, we're invoking a handler.
442  */
443 static inline void
444 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
445 {
446 	sigset_t *oldset = sigmask_to_save();
447 	int ret;
448 
449 	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
450 		ret = setup_rt_frame(ksig, oldset, regs);
451 	else
452 		ret = setup_frame(ksig, oldset, regs);
453 
454 	signal_setup_done(ret, ksig, 0);
455 }
456 
457 static inline void
458 syscall_restart(unsigned long r0, unsigned long r19,
459 		struct pt_regs *regs, struct k_sigaction *ka)
460 {
461 	switch (regs->r0) {
462 	case ERESTARTSYS:
463 		if (!(ka->sa.sa_flags & SA_RESTART)) {
464 		case ERESTARTNOHAND:
465 			regs->r0 = EINTR;
466 			break;
467 		}
468 		/* fallthrough */
469 	case ERESTARTNOINTR:
470 		regs->r0 = r0;	/* reset v0 and a3 and replay syscall */
471 		regs->r19 = r19;
472 		regs->pc -= 4;
473 		break;
474 	case ERESTART_RESTARTBLOCK:
475 		regs->r0 = EINTR;
476 		break;
477 	}
478 }
479 
480 
481 /*
482  * Note that 'init' is a special process: it doesn't get signals it doesn't
483  * want to handle. Thus you cannot kill init even with a SIGKILL even by
484  * mistake.
485  *
486  * Note that we go through the signals twice: once to check the signals that
487  * the kernel can handle, and then we build all the user-level signal handling
488  * stack-frames in one go after that.
489  *
490  * "r0" and "r19" are the registers we need to restore for system call
491  * restart. "r0" is also used as an indicator whether we can restart at
492  * all (if we get here from anything but a syscall return, it will be 0)
493  */
494 static void
495 do_signal(struct pt_regs *regs, unsigned long r0, unsigned long r19)
496 {
497 	unsigned long single_stepping = ptrace_cancel_bpt(current);
498 	struct ksignal ksig;
499 
500 	/* This lets the debugger run, ... */
501 	if (get_signal(&ksig)) {
502 		/* ... so re-check the single stepping. */
503 		single_stepping |= ptrace_cancel_bpt(current);
504 		/* Whee!  Actually deliver the signal.  */
505 		if (r0)
506 			syscall_restart(r0, r19, regs, &ksig.ka);
507 		handle_signal(&ksig, regs);
508 	} else {
509 		single_stepping |= ptrace_cancel_bpt(current);
510 		if (r0) {
511 			switch (regs->r0) {
512 			case ERESTARTNOHAND:
513 			case ERESTARTSYS:
514 			case ERESTARTNOINTR:
515 				/* Reset v0 and a3 and replay syscall.  */
516 				regs->r0 = r0;
517 				regs->r19 = r19;
518 				regs->pc -= 4;
519 				break;
520 			case ERESTART_RESTARTBLOCK:
521 				/* Set v0 to the restart_syscall and replay */
522 				regs->r0 = __NR_restart_syscall;
523 				regs->pc -= 4;
524 				break;
525 			}
526 		}
527 		restore_saved_sigmask();
528 	}
529 	if (single_stepping)
530 		ptrace_set_bpt(current);	/* re-set breakpoint */
531 }
532 
533 void
534 do_work_pending(struct pt_regs *regs, unsigned long thread_flags,
535 		 unsigned long r0, unsigned long r19)
536 {
537 	do {
538 		if (thread_flags & _TIF_NEED_RESCHED) {
539 			schedule();
540 		} else {
541 			local_irq_enable();
542 			if (thread_flags & _TIF_SIGPENDING) {
543 				do_signal(regs, r0, r19);
544 				r0 = 0;
545 			} else {
546 				clear_thread_flag(TIF_NOTIFY_RESUME);
547 				tracehook_notify_resume(regs);
548 			}
549 		}
550 		local_irq_disable();
551 		thread_flags = current_thread_info()->flags;
552 	} while (thread_flags & _TIF_WORK_MASK);
553 }
554