xref: /openbmc/linux/arch/alpha/kernel/signal.c (revision bbaf1ff0)
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/resume_user_mode.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(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(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 err = __get_user(regs->pc, &sc->sc_pc);
154 
155 	current->restart_block.fn = do_no_restart_syscall;
156 	current_thread_info()->status |= TS_SAVED_FP | TS_RESTORE_FP;
157 
158 	sw->r26 = (unsigned long) ret_from_sys_call;
159 
160 	err |= __get_user(regs->r0, sc->sc_regs+0);
161 	err |= __get_user(regs->r1, sc->sc_regs+1);
162 	err |= __get_user(regs->r2, sc->sc_regs+2);
163 	err |= __get_user(regs->r3, sc->sc_regs+3);
164 	err |= __get_user(regs->r4, sc->sc_regs+4);
165 	err |= __get_user(regs->r5, sc->sc_regs+5);
166 	err |= __get_user(regs->r6, sc->sc_regs+6);
167 	err |= __get_user(regs->r7, sc->sc_regs+7);
168 	err |= __get_user(regs->r8, sc->sc_regs+8);
169 	err |= __get_user(sw->r9, sc->sc_regs+9);
170 	err |= __get_user(sw->r10, sc->sc_regs+10);
171 	err |= __get_user(sw->r11, sc->sc_regs+11);
172 	err |= __get_user(sw->r12, sc->sc_regs+12);
173 	err |= __get_user(sw->r13, sc->sc_regs+13);
174 	err |= __get_user(sw->r14, sc->sc_regs+14);
175 	err |= __get_user(sw->r15, sc->sc_regs+15);
176 	err |= __get_user(regs->r16, sc->sc_regs+16);
177 	err |= __get_user(regs->r17, sc->sc_regs+17);
178 	err |= __get_user(regs->r18, sc->sc_regs+18);
179 	err |= __get_user(regs->r19, sc->sc_regs+19);
180 	err |= __get_user(regs->r20, sc->sc_regs+20);
181 	err |= __get_user(regs->r21, sc->sc_regs+21);
182 	err |= __get_user(regs->r22, sc->sc_regs+22);
183 	err |= __get_user(regs->r23, sc->sc_regs+23);
184 	err |= __get_user(regs->r24, sc->sc_regs+24);
185 	err |= __get_user(regs->r25, sc->sc_regs+25);
186 	err |= __get_user(regs->r26, sc->sc_regs+26);
187 	err |= __get_user(regs->r27, sc->sc_regs+27);
188 	err |= __get_user(regs->r28, sc->sc_regs+28);
189 	err |= __get_user(regs->gp, sc->sc_regs+29);
190 	err |= __get_user(usp, sc->sc_regs+30);
191 	wrusp(usp);
192 
193 	err |= __copy_from_user(current_thread_info()->fp,
194 				sc->sc_fpregs, 31 * 8);
195 	err |= __get_user(current_thread_info()->fp[31], &sc->sc_fpcr);
196 
197 	return err;
198 }
199 
200 /* Note that this syscall is also used by setcontext(3) to install
201    a given sigcontext.  This because it's impossible to set *all*
202    registers and transfer control from userland.  */
203 
204 asmlinkage void
205 do_sigreturn(struct sigcontext __user *sc)
206 {
207 	struct pt_regs *regs = current_pt_regs();
208 	sigset_t set;
209 
210 	/* Verify that it's a good sigcontext before using it */
211 	if (!access_ok(sc, sizeof(*sc)))
212 		goto give_sigsegv;
213 	if (__get_user(set.sig[0], &sc->sc_mask))
214 		goto give_sigsegv;
215 
216 	set_current_blocked(&set);
217 
218 	if (restore_sigcontext(sc, regs))
219 		goto give_sigsegv;
220 
221 	/* Send SIGTRAP if we're single-stepping: */
222 	if (ptrace_cancel_bpt (current)) {
223 		send_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *) regs->pc,
224 			       current);
225 	}
226 	return;
227 
228 give_sigsegv:
229 	force_sig(SIGSEGV);
230 }
231 
232 asmlinkage void
233 do_rt_sigreturn(struct rt_sigframe __user *frame)
234 {
235 	struct pt_regs *regs = current_pt_regs();
236 	sigset_t set;
237 
238 	/* Verify that it's a good ucontext_t before using it */
239 	if (!access_ok(&frame->uc, sizeof(frame->uc)))
240 		goto give_sigsegv;
241 	if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
242 		goto give_sigsegv;
243 
244 	set_current_blocked(&set);
245 
246 	if (restore_sigcontext(&frame->uc.uc_mcontext, regs))
247 		goto give_sigsegv;
248 
249 	/* Send SIGTRAP if we're single-stepping: */
250 	if (ptrace_cancel_bpt (current)) {
251 		send_sig_fault(SIGTRAP, TRAP_BRKPT, (void __user *) regs->pc,
252 			       current);
253 	}
254 	return;
255 
256 give_sigsegv:
257 	force_sig(SIGSEGV);
258 }
259 
260 
261 /*
262  * Set up a signal frame.
263  */
264 
265 static inline void __user *
266 get_sigframe(struct ksignal *ksig, unsigned long sp, size_t frame_size)
267 {
268 	return (void __user *)((sigsp(sp, ksig) - frame_size) & -32ul);
269 }
270 
271 static long
272 setup_sigcontext(struct sigcontext __user *sc, struct pt_regs *regs,
273 		 unsigned long mask, unsigned long sp)
274 {
275 	struct switch_stack *sw = (struct switch_stack *)regs - 1;
276 	long err = 0;
277 
278 	err |= __put_user(on_sig_stack((unsigned long)sc), &sc->sc_onstack);
279 	err |= __put_user(mask, &sc->sc_mask);
280 	err |= __put_user(regs->pc, &sc->sc_pc);
281 	err |= __put_user(8, &sc->sc_ps);
282 
283 	err |= __put_user(regs->r0 , sc->sc_regs+0);
284 	err |= __put_user(regs->r1 , sc->sc_regs+1);
285 	err |= __put_user(regs->r2 , sc->sc_regs+2);
286 	err |= __put_user(regs->r3 , sc->sc_regs+3);
287 	err |= __put_user(regs->r4 , sc->sc_regs+4);
288 	err |= __put_user(regs->r5 , sc->sc_regs+5);
289 	err |= __put_user(regs->r6 , sc->sc_regs+6);
290 	err |= __put_user(regs->r7 , sc->sc_regs+7);
291 	err |= __put_user(regs->r8 , sc->sc_regs+8);
292 	err |= __put_user(sw->r9   , sc->sc_regs+9);
293 	err |= __put_user(sw->r10  , sc->sc_regs+10);
294 	err |= __put_user(sw->r11  , sc->sc_regs+11);
295 	err |= __put_user(sw->r12  , sc->sc_regs+12);
296 	err |= __put_user(sw->r13  , sc->sc_regs+13);
297 	err |= __put_user(sw->r14  , sc->sc_regs+14);
298 	err |= __put_user(sw->r15  , sc->sc_regs+15);
299 	err |= __put_user(regs->r16, sc->sc_regs+16);
300 	err |= __put_user(regs->r17, sc->sc_regs+17);
301 	err |= __put_user(regs->r18, sc->sc_regs+18);
302 	err |= __put_user(regs->r19, sc->sc_regs+19);
303 	err |= __put_user(regs->r20, sc->sc_regs+20);
304 	err |= __put_user(regs->r21, sc->sc_regs+21);
305 	err |= __put_user(regs->r22, sc->sc_regs+22);
306 	err |= __put_user(regs->r23, sc->sc_regs+23);
307 	err |= __put_user(regs->r24, sc->sc_regs+24);
308 	err |= __put_user(regs->r25, sc->sc_regs+25);
309 	err |= __put_user(regs->r26, sc->sc_regs+26);
310 	err |= __put_user(regs->r27, sc->sc_regs+27);
311 	err |= __put_user(regs->r28, sc->sc_regs+28);
312 	err |= __put_user(regs->gp , sc->sc_regs+29);
313 	err |= __put_user(sp, sc->sc_regs+30);
314 	err |= __put_user(0, sc->sc_regs+31);
315 
316 	err |= __copy_to_user(sc->sc_fpregs,
317 			      current_thread_info()->fp, 31 * 8);
318 	err |= __put_user(0, sc->sc_fpregs+31);
319 	err |= __put_user(current_thread_info()->fp[31], &sc->sc_fpcr);
320 
321 	err |= __put_user(regs->trap_a0, &sc->sc_traparg_a0);
322 	err |= __put_user(regs->trap_a1, &sc->sc_traparg_a1);
323 	err |= __put_user(regs->trap_a2, &sc->sc_traparg_a2);
324 
325 	return err;
326 }
327 
328 static int
329 setup_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
330 {
331 	unsigned long oldsp, r26, err = 0;
332 	struct sigframe __user *frame;
333 
334 	oldsp = rdusp();
335 	frame = get_sigframe(ksig, oldsp, sizeof(*frame));
336 	if (!access_ok(frame, sizeof(*frame)))
337 		return -EFAULT;
338 
339 	err |= setup_sigcontext(&frame->sc, regs, set->sig[0], oldsp);
340 	if (err)
341 		return -EFAULT;
342 
343 	/* Set up to return from userspace.  If provided, use a stub
344 	   already in userspace.  */
345 	r26 = (unsigned long) ksig->ka.ka_restorer;
346 	if (!r26) {
347 		err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0);
348 		err |= __put_user(INSN_LDI_R0+__NR_sigreturn, frame->retcode+1);
349 		err |= __put_user(INSN_CALLSYS, frame->retcode+2);
350 		imb();
351 		r26 = (unsigned long) frame->retcode;
352 	}
353 
354 	/* Check that everything was written properly.  */
355 	if (err)
356 		return err;
357 
358 	/* "Return" to the handler */
359 	regs->r26 = r26;
360 	regs->r27 = regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
361 	regs->r16 = ksig->sig;			/* a0: signal number */
362 	regs->r17 = 0;				/* a1: exception code */
363 	regs->r18 = (unsigned long) &frame->sc;	/* a2: sigcontext pointer */
364 	wrusp((unsigned long) frame);
365 
366 #if DEBUG_SIG
367 	printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
368 		current->comm, current->pid, frame, regs->pc, regs->r26);
369 #endif
370 	return 0;
371 }
372 
373 static int
374 setup_rt_frame(struct ksignal *ksig, sigset_t *set, struct pt_regs *regs)
375 {
376 	unsigned long oldsp, r26, err = 0;
377 	struct rt_sigframe __user *frame;
378 
379 	oldsp = rdusp();
380 	frame = get_sigframe(ksig, oldsp, sizeof(*frame));
381 	if (!access_ok(frame, sizeof(*frame)))
382 		return -EFAULT;
383 
384 	err |= copy_siginfo_to_user(&frame->info, &ksig->info);
385 
386 	/* Create the ucontext.  */
387 	err |= __put_user(0, &frame->uc.uc_flags);
388 	err |= __put_user(0, &frame->uc.uc_link);
389 	err |= __put_user(set->sig[0], &frame->uc.uc_osf_sigmask);
390 	err |= __save_altstack(&frame->uc.uc_stack, oldsp);
391 	err |= setup_sigcontext(&frame->uc.uc_mcontext, regs,
392 				set->sig[0], oldsp);
393 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
394 	if (err)
395 		return -EFAULT;
396 
397 	/* Set up to return from userspace.  If provided, use a stub
398 	   already in userspace.  */
399 	r26 = (unsigned long) ksig->ka.ka_restorer;
400 	if (!r26) {
401 		err |= __put_user(INSN_MOV_R30_R16, frame->retcode+0);
402 		err |= __put_user(INSN_LDI_R0+__NR_rt_sigreturn,
403 				  frame->retcode+1);
404 		err |= __put_user(INSN_CALLSYS, frame->retcode+2);
405 		imb();
406 		r26 = (unsigned long) frame->retcode;
407 	}
408 
409 	if (err)
410 		return -EFAULT;
411 
412 	/* "Return" to the handler */
413 	regs->r26 = r26;
414 	regs->r27 = regs->pc = (unsigned long) ksig->ka.sa.sa_handler;
415 	regs->r16 = ksig->sig;			  /* a0: signal number */
416 	regs->r17 = (unsigned long) &frame->info; /* a1: siginfo pointer */
417 	regs->r18 = (unsigned long) &frame->uc;	  /* a2: ucontext pointer */
418 	wrusp((unsigned long) frame);
419 
420 #if DEBUG_SIG
421 	printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
422 		current->comm, current->pid, frame, regs->pc, regs->r26);
423 #endif
424 
425 	return 0;
426 }
427 
428 
429 /*
430  * OK, we're invoking a handler.
431  */
432 static inline void
433 handle_signal(struct ksignal *ksig, struct pt_regs *regs)
434 {
435 	sigset_t *oldset = sigmask_to_save();
436 	int ret;
437 
438 	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
439 		ret = setup_rt_frame(ksig, oldset, regs);
440 	else
441 		ret = setup_frame(ksig, oldset, regs);
442 
443 	signal_setup_done(ret, ksig, 0);
444 }
445 
446 static inline void
447 syscall_restart(unsigned long r0, unsigned long r19,
448 		struct pt_regs *regs, struct k_sigaction *ka)
449 {
450 	switch (regs->r0) {
451 	case ERESTARTSYS:
452 		if (!(ka->sa.sa_flags & SA_RESTART)) {
453 		case ERESTARTNOHAND:
454 			regs->r0 = EINTR;
455 			break;
456 		}
457 		fallthrough;
458 	case ERESTARTNOINTR:
459 		regs->r0 = r0;	/* reset v0 and a3 and replay syscall */
460 		regs->r19 = r19;
461 		regs->pc -= 4;
462 		break;
463 	case ERESTART_RESTARTBLOCK:
464 		regs->r0 = EINTR;
465 		break;
466 	}
467 }
468 
469 
470 /*
471  * Note that 'init' is a special process: it doesn't get signals it doesn't
472  * want to handle. Thus you cannot kill init even with a SIGKILL even by
473  * mistake.
474  *
475  * Note that we go through the signals twice: once to check the signals that
476  * the kernel can handle, and then we build all the user-level signal handling
477  * stack-frames in one go after that.
478  *
479  * "r0" and "r19" are the registers we need to restore for system call
480  * restart. "r0" is also used as an indicator whether we can restart at
481  * all (if we get here from anything but a syscall return, it will be 0)
482  */
483 static void
484 do_signal(struct pt_regs *regs, unsigned long r0, unsigned long r19)
485 {
486 	unsigned long single_stepping = ptrace_cancel_bpt(current);
487 	struct ksignal ksig;
488 
489 	/* This lets the debugger run, ... */
490 	if (get_signal(&ksig)) {
491 		/* ... so re-check the single stepping. */
492 		single_stepping |= ptrace_cancel_bpt(current);
493 		/* Whee!  Actually deliver the signal.  */
494 		if (r0)
495 			syscall_restart(r0, r19, regs, &ksig.ka);
496 		handle_signal(&ksig, regs);
497 	} else {
498 		single_stepping |= ptrace_cancel_bpt(current);
499 		if (r0) {
500 			switch (regs->r0) {
501 			case ERESTARTNOHAND:
502 			case ERESTARTSYS:
503 			case ERESTARTNOINTR:
504 				/* Reset v0 and a3 and replay syscall.  */
505 				regs->r0 = r0;
506 				regs->r19 = r19;
507 				regs->pc -= 4;
508 				break;
509 			case ERESTART_RESTARTBLOCK:
510 				/* Set v0 to the restart_syscall and replay */
511 				regs->r0 = __NR_restart_syscall;
512 				regs->pc -= 4;
513 				break;
514 			}
515 		}
516 		restore_saved_sigmask();
517 	}
518 	if (single_stepping)
519 		ptrace_set_bpt(current);	/* re-set breakpoint */
520 }
521 
522 void
523 do_work_pending(struct pt_regs *regs, unsigned long thread_flags,
524 		 unsigned long r0, unsigned long r19)
525 {
526 	do {
527 		if (thread_flags & _TIF_NEED_RESCHED) {
528 			schedule();
529 		} else {
530 			local_irq_enable();
531 			if (thread_flags & (_TIF_SIGPENDING|_TIF_NOTIFY_SIGNAL)) {
532 				preempt_disable();
533 				save_fpu();
534 				preempt_enable();
535 				do_signal(regs, r0, r19);
536 				r0 = 0;
537 			} else {
538 				resume_user_mode_work(regs);
539 			}
540 		}
541 		local_irq_disable();
542 		thread_flags = read_thread_flags();
543 	} while (thread_flags & _TIF_WORK_MASK);
544 }
545