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