xref: /openbmc/linux/arch/s390/kernel/signal.c (revision 1eb4c977)
1 /*
2  *  arch/s390/kernel/signal.c
3  *
4  *    Copyright (C) IBM Corp. 1999,2006
5  *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
6  *
7  *    Based on Intel version
8  *
9  *  Copyright (C) 1991, 1992  Linus Torvalds
10  *
11  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
12  */
13 
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/smp.h>
17 #include <linux/kernel.h>
18 #include <linux/signal.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/unistd.h>
23 #include <linux/stddef.h>
24 #include <linux/tty.h>
25 #include <linux/personality.h>
26 #include <linux/binfmts.h>
27 #include <linux/tracehook.h>
28 #include <linux/syscalls.h>
29 #include <linux/compat.h>
30 #include <asm/ucontext.h>
31 #include <asm/uaccess.h>
32 #include <asm/lowcore.h>
33 #include "entry.h"
34 
35 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
36 
37 
38 typedef struct
39 {
40 	__u8 callee_used_stack[__SIGNAL_FRAMESIZE];
41 	struct sigcontext sc;
42 	_sigregs sregs;
43 	int signo;
44 	__u8 retcode[S390_SYSCALL_SIZE];
45 } sigframe;
46 
47 typedef struct
48 {
49 	__u8 callee_used_stack[__SIGNAL_FRAMESIZE];
50 	__u8 retcode[S390_SYSCALL_SIZE];
51 	struct siginfo info;
52 	struct ucontext uc;
53 } rt_sigframe;
54 
55 /*
56  * Atomically swap in the new signal mask, and wait for a signal.
57  */
58 SYSCALL_DEFINE3(sigsuspend, int, history0, int, history1, old_sigset_t, mask)
59 {
60 	sigset_t blocked;
61 
62 	current->saved_sigmask = current->blocked;
63 	mask &= _BLOCKABLE;
64 	siginitset(&blocked, mask);
65 	set_current_blocked(&blocked);
66 	set_current_state(TASK_INTERRUPTIBLE);
67 	schedule();
68 	set_restore_sigmask();
69 	return -ERESTARTNOHAND;
70 }
71 
72 SYSCALL_DEFINE3(sigaction, int, sig, const struct old_sigaction __user *, act,
73 		struct old_sigaction __user *, oact)
74 {
75 	struct k_sigaction new_ka, old_ka;
76 	int ret;
77 
78 	if (act) {
79 		old_sigset_t mask;
80 		if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
81 		    __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
82 		    __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
83 		    __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
84 		    __get_user(mask, &act->sa_mask))
85 			return -EFAULT;
86 		siginitset(&new_ka.sa.sa_mask, mask);
87 	}
88 
89 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
90 
91 	if (!ret && oact) {
92 		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
93 		    __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
94 		    __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
95 		    __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
96 		    __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
97 			return -EFAULT;
98 	}
99 
100 	return ret;
101 }
102 
103 SYSCALL_DEFINE2(sigaltstack, const stack_t __user *, uss,
104 		stack_t __user *, uoss)
105 {
106 	struct pt_regs *regs = task_pt_regs(current);
107 	return do_sigaltstack(uss, uoss, regs->gprs[15]);
108 }
109 
110 /* Returns non-zero on fault. */
111 static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
112 {
113 	_sigregs user_sregs;
114 
115 	save_access_regs(current->thread.acrs);
116 
117 	/* Copy a 'clean' PSW mask to the user to avoid leaking
118 	   information about whether PER is currently on.  */
119 	user_sregs.regs.psw.mask = psw_user_bits |
120 		(regs->psw.mask & PSW_MASK_USER);
121 	user_sregs.regs.psw.addr = regs->psw.addr;
122 	memcpy(&user_sregs.regs.gprs, &regs->gprs, sizeof(sregs->regs.gprs));
123 	memcpy(&user_sregs.regs.acrs, current->thread.acrs,
124 	       sizeof(sregs->regs.acrs));
125 	/*
126 	 * We have to store the fp registers to current->thread.fp_regs
127 	 * to merge them with the emulated registers.
128 	 */
129 	save_fp_regs(&current->thread.fp_regs);
130 	memcpy(&user_sregs.fpregs, &current->thread.fp_regs,
131 	       sizeof(s390_fp_regs));
132 	return __copy_to_user(sregs, &user_sregs, sizeof(_sigregs));
133 }
134 
135 /* Returns positive number on error */
136 static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
137 {
138 	int err;
139 	_sigregs user_sregs;
140 
141 	/* Alwys make any pending restarted system call return -EINTR */
142 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
143 
144 	err = __copy_from_user(&user_sregs, sregs, sizeof(_sigregs));
145 	if (err)
146 		return err;
147 	/* Use regs->psw.mask instead of psw_user_bits to preserve PER bit. */
148 	regs->psw.mask = (regs->psw.mask & ~PSW_MASK_USER) |
149 		(user_sregs.regs.psw.mask & PSW_MASK_USER);
150 	/* Check for invalid amode */
151 	if (regs->psw.mask & PSW_MASK_EA)
152 		regs->psw.mask |= PSW_MASK_BA;
153 	regs->psw.addr = user_sregs.regs.psw.addr;
154 	memcpy(&regs->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
155 	memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
156 	       sizeof(sregs->regs.acrs));
157 	restore_access_regs(current->thread.acrs);
158 
159 	memcpy(&current->thread.fp_regs, &user_sregs.fpregs,
160 	       sizeof(s390_fp_regs));
161 	current->thread.fp_regs.fpc &= FPC_VALID_MASK;
162 
163 	restore_fp_regs(&current->thread.fp_regs);
164 	clear_thread_flag(TIF_SYSCALL);	/* No longer in a system call */
165 	return 0;
166 }
167 
168 SYSCALL_DEFINE0(sigreturn)
169 {
170 	struct pt_regs *regs = task_pt_regs(current);
171 	sigframe __user *frame = (sigframe __user *)regs->gprs[15];
172 	sigset_t set;
173 
174 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
175 		goto badframe;
176 	if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
177 		goto badframe;
178 	sigdelsetmask(&set, ~_BLOCKABLE);
179 	set_current_blocked(&set);
180 	if (restore_sigregs(regs, &frame->sregs))
181 		goto badframe;
182 	return regs->gprs[2];
183 badframe:
184 	force_sig(SIGSEGV, current);
185 	return 0;
186 }
187 
188 SYSCALL_DEFINE0(rt_sigreturn)
189 {
190 	struct pt_regs *regs = task_pt_regs(current);
191 	rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
192 	sigset_t set;
193 
194 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
195 		goto badframe;
196 	if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
197 		goto badframe;
198 	sigdelsetmask(&set, ~_BLOCKABLE);
199 	set_current_blocked(&set);
200 	if (restore_sigregs(regs, &frame->uc.uc_mcontext))
201 		goto badframe;
202 	if (do_sigaltstack(&frame->uc.uc_stack, NULL,
203 			   regs->gprs[15]) == -EFAULT)
204 		goto badframe;
205 	return regs->gprs[2];
206 badframe:
207 	force_sig(SIGSEGV, current);
208 	return 0;
209 }
210 
211 /*
212  * Set up a signal frame.
213  */
214 
215 
216 /*
217  * Determine which stack to use..
218  */
219 static inline void __user *
220 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
221 {
222 	unsigned long sp;
223 
224 	/* Default to using normal stack */
225 	sp = regs->gprs[15];
226 
227 	/* Overflow on alternate signal stack gives SIGSEGV. */
228 	if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
229 		return (void __user *) -1UL;
230 
231 	/* This is the X/Open sanctioned signal stack switching.  */
232 	if (ka->sa.sa_flags & SA_ONSTACK) {
233 		if (! sas_ss_flags(sp))
234 			sp = current->sas_ss_sp + current->sas_ss_size;
235 	}
236 
237 	/* This is the legacy signal stack switching. */
238 	else if (!user_mode(regs) &&
239 		 !(ka->sa.sa_flags & SA_RESTORER) &&
240 		 ka->sa.sa_restorer) {
241 		sp = (unsigned long) ka->sa.sa_restorer;
242 	}
243 
244 	return (void __user *)((sp - frame_size) & -8ul);
245 }
246 
247 static inline int map_signal(int sig)
248 {
249 	if (current_thread_info()->exec_domain
250 	    && current_thread_info()->exec_domain->signal_invmap
251 	    && sig < 32)
252 		return current_thread_info()->exec_domain->signal_invmap[sig];
253 	else
254 		return sig;
255 }
256 
257 static int setup_frame(int sig, struct k_sigaction *ka,
258 		       sigset_t *set, struct pt_regs * regs)
259 {
260 	sigframe __user *frame;
261 
262 	frame = get_sigframe(ka, regs, sizeof(sigframe));
263 	if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
264 		goto give_sigsegv;
265 
266 	if (frame == (void __user *) -1UL)
267 		goto give_sigsegv;
268 
269 	if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
270 		goto give_sigsegv;
271 
272 	if (save_sigregs(regs, &frame->sregs))
273 		goto give_sigsegv;
274 	if (__put_user(&frame->sregs, &frame->sc.sregs))
275 		goto give_sigsegv;
276 
277 	/* Set up to return from userspace.  If provided, use a stub
278 	   already in userspace.  */
279 	if (ka->sa.sa_flags & SA_RESTORER) {
280                 regs->gprs[14] = (unsigned long)
281 			ka->sa.sa_restorer | PSW_ADDR_AMODE;
282 	} else {
283                 regs->gprs[14] = (unsigned long)
284 			frame->retcode | PSW_ADDR_AMODE;
285 		if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
286 	                       (u16 __user *)(frame->retcode)))
287 			goto give_sigsegv;
288 	}
289 
290 	/* Set up backchain. */
291 	if (__put_user(regs->gprs[15], (addr_t __user *) frame))
292 		goto give_sigsegv;
293 
294 	/* Set up registers for signal handler */
295 	regs->gprs[15] = (unsigned long) frame;
296 	regs->psw.mask |= PSW_MASK_EA | PSW_MASK_BA;	/* 64 bit amode */
297 	regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
298 
299 	regs->gprs[2] = map_signal(sig);
300 	regs->gprs[3] = (unsigned long) &frame->sc;
301 
302 	/* We forgot to include these in the sigcontext.
303 	   To avoid breaking binary compatibility, they are passed as args. */
304 	if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
305 	    sig == SIGTRAP || sig == SIGFPE) {
306 		/* set extra registers only for synchronous signals */
307 		regs->gprs[4] = regs->int_code & 127;
308 		regs->gprs[5] = regs->int_parm_long;
309 		regs->gprs[6] = task_thread_info(current)->last_break;
310 	}
311 
312 	/* Place signal number on stack to allow backtrace from handler.  */
313 	if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
314 		goto give_sigsegv;
315 	return 0;
316 
317 give_sigsegv:
318 	force_sigsegv(sig, current);
319 	return -EFAULT;
320 }
321 
322 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
323 			   sigset_t *set, struct pt_regs * regs)
324 {
325 	int err = 0;
326 	rt_sigframe __user *frame;
327 
328 	frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
329 	if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
330 		goto give_sigsegv;
331 
332 	if (frame == (void __user *) -1UL)
333 		goto give_sigsegv;
334 
335 	if (copy_siginfo_to_user(&frame->info, info))
336 		goto give_sigsegv;
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 |= __put_user((void __user *)current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
342 	err |= __put_user(sas_ss_flags(regs->gprs[15]),
343 			  &frame->uc.uc_stack.ss_flags);
344 	err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
345 	err |= save_sigregs(regs, &frame->uc.uc_mcontext);
346 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
347 	if (err)
348 		goto give_sigsegv;
349 
350 	/* Set up to return from userspace.  If provided, use a stub
351 	   already in userspace.  */
352 	if (ka->sa.sa_flags & SA_RESTORER) {
353                 regs->gprs[14] = (unsigned long)
354 			ka->sa.sa_restorer | PSW_ADDR_AMODE;
355 	} else {
356                 regs->gprs[14] = (unsigned long)
357 			frame->retcode | PSW_ADDR_AMODE;
358 		if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
359 			       (u16 __user *)(frame->retcode)))
360 			goto give_sigsegv;
361 	}
362 
363 	/* Set up backchain. */
364 	if (__put_user(regs->gprs[15], (addr_t __user *) frame))
365 		goto give_sigsegv;
366 
367 	/* Set up registers for signal handler */
368 	regs->gprs[15] = (unsigned long) frame;
369 	regs->psw.mask |= PSW_MASK_EA | PSW_MASK_BA;	/* 64 bit amode */
370 	regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
371 
372 	regs->gprs[2] = map_signal(sig);
373 	regs->gprs[3] = (unsigned long) &frame->info;
374 	regs->gprs[4] = (unsigned long) &frame->uc;
375 	regs->gprs[5] = task_thread_info(current)->last_break;
376 	return 0;
377 
378 give_sigsegv:
379 	force_sigsegv(sig, current);
380 	return -EFAULT;
381 }
382 
383 static int handle_signal(unsigned long sig, struct k_sigaction *ka,
384 			 siginfo_t *info, sigset_t *oldset,
385 			 struct pt_regs *regs)
386 {
387 	int ret;
388 
389 	/* Set up the stack frame */
390 	if (ka->sa.sa_flags & SA_SIGINFO)
391 		ret = setup_rt_frame(sig, ka, info, oldset, regs);
392 	else
393 		ret = setup_frame(sig, ka, oldset, regs);
394 	if (ret)
395 		return ret;
396 	block_sigmask(ka, sig);
397 	return 0;
398 }
399 
400 /*
401  * Note that 'init' is a special process: it doesn't get signals it doesn't
402  * want to handle. Thus you cannot kill init even with a SIGKILL even by
403  * mistake.
404  *
405  * Note that we go through the signals twice: once to check the signals that
406  * the kernel can handle, and then we build all the user-level signal handling
407  * stack-frames in one go after that.
408  */
409 void do_signal(struct pt_regs *regs)
410 {
411 	siginfo_t info;
412 	int signr;
413 	struct k_sigaction ka;
414 	sigset_t *oldset;
415 
416 	/*
417 	 * We want the common case to go fast, which
418 	 * is why we may in certain cases get here from
419 	 * kernel mode. Just return without doing anything
420 	 * if so.
421 	 */
422 	if (!user_mode(regs))
423 		return;
424 
425 	if (test_thread_flag(TIF_RESTORE_SIGMASK))
426 		oldset = &current->saved_sigmask;
427 	else
428 		oldset = &current->blocked;
429 
430 	/*
431 	 * Get signal to deliver. When running under ptrace, at this point
432 	 * the debugger may change all our registers, including the system
433 	 * call information.
434 	 */
435 	current_thread_info()->system_call =
436 		test_thread_flag(TIF_SYSCALL) ? regs->int_code : 0;
437 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
438 
439 	if (signr > 0) {
440 		/* Whee!  Actually deliver the signal.  */
441 		if (current_thread_info()->system_call) {
442 			regs->int_code = current_thread_info()->system_call;
443 			/* Check for system call restarting. */
444 			switch (regs->gprs[2]) {
445 			case -ERESTART_RESTARTBLOCK:
446 			case -ERESTARTNOHAND:
447 				regs->gprs[2] = -EINTR;
448 				break;
449 			case -ERESTARTSYS:
450 				if (!(ka.sa.sa_flags & SA_RESTART)) {
451 					regs->gprs[2] = -EINTR;
452 					break;
453 				}
454 			/* fallthrough */
455 			case -ERESTARTNOINTR:
456 				regs->gprs[2] = regs->orig_gpr2;
457 				regs->psw.addr =
458 					__rewind_psw(regs->psw,
459 						     regs->int_code >> 16);
460 				break;
461 			}
462 		}
463 		/* No longer in a system call */
464 		clear_thread_flag(TIF_SYSCALL);
465 
466 		if ((is_compat_task() ?
467 		     handle_signal32(signr, &ka, &info, oldset, regs) :
468 		     handle_signal(signr, &ka, &info, oldset, regs)) == 0) {
469 			/*
470 			 * A signal was successfully delivered; the saved
471 			 * sigmask will have been stored in the signal frame,
472 			 * and will be restored by sigreturn, so we can simply
473 			 * clear the TIF_RESTORE_SIGMASK flag.
474 			 */
475 			if (test_thread_flag(TIF_RESTORE_SIGMASK))
476 				clear_thread_flag(TIF_RESTORE_SIGMASK);
477 
478 			/*
479 			 * Let tracing know that we've done the handler setup.
480 			 */
481 			tracehook_signal_handler(signr, &info, &ka, regs,
482 					 test_thread_flag(TIF_SINGLE_STEP));
483 		}
484 		return;
485 	}
486 
487 	/* No handlers present - check for system call restart */
488 	clear_thread_flag(TIF_SYSCALL);
489 	if (current_thread_info()->system_call) {
490 		regs->int_code = current_thread_info()->system_call;
491 		switch (regs->gprs[2]) {
492 		case -ERESTART_RESTARTBLOCK:
493 			/* Restart with sys_restart_syscall */
494 			regs->int_code = __NR_restart_syscall;
495 		/* fallthrough */
496 		case -ERESTARTNOHAND:
497 		case -ERESTARTSYS:
498 		case -ERESTARTNOINTR:
499 			/* Restart system call with magic TIF bit. */
500 			regs->gprs[2] = regs->orig_gpr2;
501 			set_thread_flag(TIF_SYSCALL);
502 			break;
503 		}
504 	}
505 
506 	/*
507 	 * If there's no signal to deliver, we just put the saved sigmask back.
508 	 */
509 	if (test_thread_flag(TIF_RESTORE_SIGMASK)) {
510 		clear_thread_flag(TIF_RESTORE_SIGMASK);
511 		sigprocmask(SIG_SETMASK, &current->saved_sigmask, NULL);
512 	}
513 }
514 
515 void do_notify_resume(struct pt_regs *regs)
516 {
517 	clear_thread_flag(TIF_NOTIFY_RESUME);
518 	tracehook_notify_resume(regs);
519 	if (current->replacement_session_keyring)
520 		key_replace_session_keyring();
521 }
522