xref: /openbmc/linux/arch/s390/kernel/signal.c (revision b78412b8)
1 /*
2  *    Copyright IBM Corp. 1999, 2006
3  *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
4  *
5  *    Based on Intel version
6  *
7  *  Copyright (C) 1991, 1992  Linus Torvalds
8  *
9  *  1997-11-28  Modified for POSIX.1b signals by Richard Henderson
10  */
11 
12 #include <linux/sched.h>
13 #include <linux/sched/task_stack.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/stddef.h>
23 #include <linux/tty.h>
24 #include <linux/personality.h>
25 #include <linux/binfmts.h>
26 #include <linux/tracehook.h>
27 #include <linux/syscalls.h>
28 #include <linux/compat.h>
29 #include <asm/ucontext.h>
30 #include <linux/uaccess.h>
31 #include <asm/lowcore.h>
32 #include <asm/switch_to.h>
33 #include "entry.h"
34 
35 /*
36  * Layout of an old-style signal-frame:
37  *	-----------------------------------------
38  *	| save area (_SIGNAL_FRAMESIZE)		|
39  *	-----------------------------------------
40  *	| struct sigcontext			|
41  *	|	oldmask				|
42  *	|	_sigregs *			|
43  *	-----------------------------------------
44  *	| _sigregs with				|
45  *	|	_s390_regs_common		|
46  *	|	_s390_fp_regs			|
47  *	-----------------------------------------
48  *	| int signo				|
49  *	-----------------------------------------
50  *	| _sigregs_ext with			|
51  *	|	gprs_high 64 byte (opt)		|
52  *	|	vxrs_low 128 byte (opt)		|
53  *	|	vxrs_high 256 byte (opt)	|
54  *	|	reserved 128 byte (opt)		|
55  *	-----------------------------------------
56  *	| __u16 svc_insn			|
57  *	-----------------------------------------
58  * The svc_insn entry with the sigreturn system call opcode does not
59  * have a fixed position and moves if gprs_high or vxrs exist.
60  * Future extensions will be added to _sigregs_ext.
61  */
62 struct sigframe
63 {
64 	__u8 callee_used_stack[__SIGNAL_FRAMESIZE];
65 	struct sigcontext sc;
66 	_sigregs sregs;
67 	int signo;
68 	_sigregs_ext sregs_ext;
69 	__u16 svc_insn;		/* Offset of svc_insn is NOT fixed! */
70 };
71 
72 /*
73  * Layout of an rt signal-frame:
74  *	-----------------------------------------
75  *	| save area (_SIGNAL_FRAMESIZE)		|
76  *	-----------------------------------------
77  *	| svc __NR_rt_sigreturn 2 byte		|
78  *	-----------------------------------------
79  *	| struct siginfo			|
80  *	-----------------------------------------
81  *	| struct ucontext_extended with		|
82  *	|	unsigned long uc_flags		|
83  *	|	struct ucontext *uc_link	|
84  *	|	stack_t uc_stack		|
85  *	|	_sigregs uc_mcontext with	|
86  *	|		_s390_regs_common	|
87  *	|		_s390_fp_regs		|
88  *	|	sigset_t uc_sigmask		|
89  *	|	_sigregs_ext uc_mcontext_ext	|
90  *	|		gprs_high 64 byte (opt)	|
91  *	|		vxrs_low 128 byte (opt)	|
92  *	|		vxrs_high 256 byte (opt)|
93  *	|		reserved 128 byte (opt)	|
94  *	-----------------------------------------
95  * Future extensions will be added to _sigregs_ext.
96  */
97 struct rt_sigframe
98 {
99 	__u8 callee_used_stack[__SIGNAL_FRAMESIZE];
100 	__u16 svc_insn;
101 	struct siginfo info;
102 	struct ucontext_extended uc;
103 };
104 
105 /* Store registers needed to create the signal frame */
106 static void store_sigregs(void)
107 {
108 	save_access_regs(current->thread.acrs);
109 	save_fpu_regs();
110 }
111 
112 /* Load registers after signal return */
113 static void load_sigregs(void)
114 {
115 	restore_access_regs(current->thread.acrs);
116 }
117 
118 /* Returns non-zero on fault. */
119 static int save_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
120 {
121 	_sigregs user_sregs;
122 
123 	/* Copy a 'clean' PSW mask to the user to avoid leaking
124 	   information about whether PER is currently on.  */
125 	user_sregs.regs.psw.mask = PSW_USER_BITS |
126 		(regs->psw.mask & (PSW_MASK_USER | PSW_MASK_RI));
127 	user_sregs.regs.psw.addr = regs->psw.addr;
128 	memcpy(&user_sregs.regs.gprs, &regs->gprs, sizeof(sregs->regs.gprs));
129 	memcpy(&user_sregs.regs.acrs, current->thread.acrs,
130 	       sizeof(user_sregs.regs.acrs));
131 	fpregs_store(&user_sregs.fpregs, &current->thread.fpu);
132 	if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs)))
133 		return -EFAULT;
134 	return 0;
135 }
136 
137 static int restore_sigregs(struct pt_regs *regs, _sigregs __user *sregs)
138 {
139 	_sigregs user_sregs;
140 
141 	/* Alwys make any pending restarted system call return -EINTR */
142 	current->restart_block.fn = do_no_restart_syscall;
143 
144 	if (__copy_from_user(&user_sregs, sregs, sizeof(user_sregs)))
145 		return -EFAULT;
146 
147 	if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW_MASK_RI))
148 		return -EINVAL;
149 
150 	/* Test the floating-point-control word. */
151 	if (test_fp_ctl(user_sregs.fpregs.fpc))
152 		return -EINVAL;
153 
154 	/* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
155 	regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) |
156 		(user_sregs.regs.psw.mask & (PSW_MASK_USER | PSW_MASK_RI));
157 	/* Check for invalid user address space control. */
158 	if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
159 		regs->psw.mask = PSW_ASC_PRIMARY |
160 			(regs->psw.mask & ~PSW_MASK_ASC);
161 	/* Check for invalid amode */
162 	if (regs->psw.mask & PSW_MASK_EA)
163 		regs->psw.mask |= PSW_MASK_BA;
164 	regs->psw.addr = user_sregs.regs.psw.addr;
165 	memcpy(&regs->gprs, &user_sregs.regs.gprs, sizeof(sregs->regs.gprs));
166 	memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
167 	       sizeof(current->thread.acrs));
168 
169 	fpregs_load(&user_sregs.fpregs, &current->thread.fpu);
170 
171 	clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */
172 	return 0;
173 }
174 
175 /* Returns non-zero on fault. */
176 static int save_sigregs_ext(struct pt_regs *regs,
177 			    _sigregs_ext __user *sregs_ext)
178 {
179 	__u64 vxrs[__NUM_VXRS_LOW];
180 	int i;
181 
182 	/* Save vector registers to signal stack */
183 	if (MACHINE_HAS_VX) {
184 		for (i = 0; i < __NUM_VXRS_LOW; i++)
185 			vxrs[i] = *((__u64 *)(current->thread.fpu.vxrs + i) + 1);
186 		if (__copy_to_user(&sregs_ext->vxrs_low, vxrs,
187 				   sizeof(sregs_ext->vxrs_low)) ||
188 		    __copy_to_user(&sregs_ext->vxrs_high,
189 				   current->thread.fpu.vxrs + __NUM_VXRS_LOW,
190 				   sizeof(sregs_ext->vxrs_high)))
191 			return -EFAULT;
192 	}
193 	return 0;
194 }
195 
196 static int restore_sigregs_ext(struct pt_regs *regs,
197 			       _sigregs_ext __user *sregs_ext)
198 {
199 	__u64 vxrs[__NUM_VXRS_LOW];
200 	int i;
201 
202 	/* Restore vector registers from signal stack */
203 	if (MACHINE_HAS_VX) {
204 		if (__copy_from_user(vxrs, &sregs_ext->vxrs_low,
205 				     sizeof(sregs_ext->vxrs_low)) ||
206 		    __copy_from_user(current->thread.fpu.vxrs + __NUM_VXRS_LOW,
207 				     &sregs_ext->vxrs_high,
208 				     sizeof(sregs_ext->vxrs_high)))
209 			return -EFAULT;
210 		for (i = 0; i < __NUM_VXRS_LOW; i++)
211 			*((__u64 *)(current->thread.fpu.vxrs + i) + 1) = vxrs[i];
212 	}
213 	return 0;
214 }
215 
216 SYSCALL_DEFINE0(sigreturn)
217 {
218 	struct pt_regs *regs = task_pt_regs(current);
219 	struct sigframe __user *frame =
220 		(struct sigframe __user *) regs->gprs[15];
221 	sigset_t set;
222 
223 	if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
224 		goto badframe;
225 	set_current_blocked(&set);
226 	save_fpu_regs();
227 	if (restore_sigregs(regs, &frame->sregs))
228 		goto badframe;
229 	if (restore_sigregs_ext(regs, &frame->sregs_ext))
230 		goto badframe;
231 	load_sigregs();
232 	return regs->gprs[2];
233 badframe:
234 	force_sig(SIGSEGV, current);
235 	return 0;
236 }
237 
238 SYSCALL_DEFINE0(rt_sigreturn)
239 {
240 	struct pt_regs *regs = task_pt_regs(current);
241 	struct rt_sigframe __user *frame =
242 		(struct rt_sigframe __user *)regs->gprs[15];
243 	sigset_t set;
244 
245 	if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
246 		goto badframe;
247 	set_current_blocked(&set);
248 	if (restore_altstack(&frame->uc.uc_stack))
249 		goto badframe;
250 	save_fpu_regs();
251 	if (restore_sigregs(regs, &frame->uc.uc_mcontext))
252 		goto badframe;
253 	if (restore_sigregs_ext(regs, &frame->uc.uc_mcontext_ext))
254 		goto badframe;
255 	load_sigregs();
256 	return regs->gprs[2];
257 badframe:
258 	force_sig(SIGSEGV, current);
259 	return 0;
260 }
261 
262 /*
263  * Determine which stack to use..
264  */
265 static inline void __user *
266 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
267 {
268 	unsigned long sp;
269 
270 	/* Default to using normal stack */
271 	sp = regs->gprs[15];
272 
273 	/* Overflow on alternate signal stack gives SIGSEGV. */
274 	if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
275 		return (void __user *) -1UL;
276 
277 	/* This is the X/Open sanctioned signal stack switching.  */
278 	if (ka->sa.sa_flags & SA_ONSTACK) {
279 		if (! sas_ss_flags(sp))
280 			sp = current->sas_ss_sp + current->sas_ss_size;
281 	}
282 
283 	return (void __user *)((sp - frame_size) & -8ul);
284 }
285 
286 static int setup_frame(int sig, struct k_sigaction *ka,
287 		       sigset_t *set, struct pt_regs * regs)
288 {
289 	struct sigframe __user *frame;
290 	struct sigcontext sc;
291 	unsigned long restorer;
292 	size_t frame_size;
293 
294 	/*
295 	 * gprs_high are only present for a 31-bit task running on
296 	 * a 64-bit kernel (see compat_signal.c) but the space for
297 	 * gprs_high need to be allocated if vector registers are
298 	 * included in the signal frame on a 31-bit system.
299 	 */
300 	frame_size = sizeof(*frame) - sizeof(frame->sregs_ext);
301 	if (MACHINE_HAS_VX)
302 		frame_size += sizeof(frame->sregs_ext);
303 	frame = get_sigframe(ka, regs, frame_size);
304 	if (frame == (void __user *) -1UL)
305 		return -EFAULT;
306 
307 	/* Set up backchain. */
308 	if (__put_user(regs->gprs[15], (addr_t __user *) frame))
309 		return -EFAULT;
310 
311 	/* Create struct sigcontext on the signal stack */
312 	memcpy(&sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE);
313 	sc.sregs = (_sigregs __user __force *) &frame->sregs;
314 	if (__copy_to_user(&frame->sc, &sc, sizeof(frame->sc)))
315 		return -EFAULT;
316 
317 	/* Store registers needed to create the signal frame */
318 	store_sigregs();
319 
320 	/* Create _sigregs on the signal stack */
321 	if (save_sigregs(regs, &frame->sregs))
322 		return -EFAULT;
323 
324 	/* Place signal number on stack to allow backtrace from handler.  */
325 	if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
326 		return -EFAULT;
327 
328 	/* Create _sigregs_ext on the signal stack */
329 	if (save_sigregs_ext(regs, &frame->sregs_ext))
330 		return -EFAULT;
331 
332 	/* Set up to return from userspace.  If provided, use a stub
333 	   already in userspace.  */
334 	if (ka->sa.sa_flags & SA_RESTORER) {
335 		restorer = (unsigned long) ka->sa.sa_restorer;
336 	} else {
337 		/* Signal frame without vector registers are short ! */
338 		__u16 __user *svc = (void __user *) frame + frame_size - 2;
339 		if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, svc))
340 			return -EFAULT;
341 		restorer = (unsigned long) svc;
342 	}
343 
344 	/* Set up registers for signal handler */
345 	regs->gprs[14] = restorer;
346 	regs->gprs[15] = (unsigned long) frame;
347 	/* Force default amode and default user address space control. */
348 	regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
349 		(PSW_USER_BITS & PSW_MASK_ASC) |
350 		(regs->psw.mask & ~PSW_MASK_ASC);
351 	regs->psw.addr = (unsigned long) ka->sa.sa_handler;
352 
353 	regs->gprs[2] = sig;
354 	regs->gprs[3] = (unsigned long) &frame->sc;
355 
356 	/* We forgot to include these in the sigcontext.
357 	   To avoid breaking binary compatibility, they are passed as args. */
358 	if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
359 	    sig == SIGTRAP || sig == SIGFPE) {
360 		/* set extra registers only for synchronous signals */
361 		regs->gprs[4] = regs->int_code & 127;
362 		regs->gprs[5] = regs->int_parm_long;
363 		regs->gprs[6] = current->thread.last_break;
364 	}
365 	return 0;
366 }
367 
368 static int setup_rt_frame(struct ksignal *ksig, sigset_t *set,
369 			  struct pt_regs *regs)
370 {
371 	struct rt_sigframe __user *frame;
372 	unsigned long uc_flags, restorer;
373 	size_t frame_size;
374 
375 	frame_size = sizeof(struct rt_sigframe) - sizeof(_sigregs_ext);
376 	/*
377 	 * gprs_high are only present for a 31-bit task running on
378 	 * a 64-bit kernel (see compat_signal.c) but the space for
379 	 * gprs_high need to be allocated if vector registers are
380 	 * included in the signal frame on a 31-bit system.
381 	 */
382 	uc_flags = 0;
383 	if (MACHINE_HAS_VX) {
384 		frame_size += sizeof(_sigregs_ext);
385 		uc_flags |= UC_VXRS;
386 	}
387 	frame = get_sigframe(&ksig->ka, regs, frame_size);
388 	if (frame == (void __user *) -1UL)
389 		return -EFAULT;
390 
391 	/* Set up backchain. */
392 	if (__put_user(regs->gprs[15], (addr_t __user *) frame))
393 		return -EFAULT;
394 
395 	/* Set up to return from userspace.  If provided, use a stub
396 	   already in userspace.  */
397 	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
398 		restorer = (unsigned long) ksig->ka.sa.sa_restorer;
399 	} else {
400 		__u16 __user *svc = &frame->svc_insn;
401 		if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, svc))
402 			return -EFAULT;
403 		restorer = (unsigned long) svc;
404 	}
405 
406 	/* Create siginfo on the signal stack */
407 	if (copy_siginfo_to_user(&frame->info, &ksig->info))
408 		return -EFAULT;
409 
410 	/* Store registers needed to create the signal frame */
411 	store_sigregs();
412 
413 	/* Create ucontext on the signal stack. */
414 	if (__put_user(uc_flags, &frame->uc.uc_flags) ||
415 	    __put_user(NULL, &frame->uc.uc_link) ||
416 	    __save_altstack(&frame->uc.uc_stack, regs->gprs[15]) ||
417 	    save_sigregs(regs, &frame->uc.uc_mcontext) ||
418 	    __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set)) ||
419 	    save_sigregs_ext(regs, &frame->uc.uc_mcontext_ext))
420 		return -EFAULT;
421 
422 	/* Set up registers for signal handler */
423 	regs->gprs[14] = restorer;
424 	regs->gprs[15] = (unsigned long) frame;
425 	/* Force default amode and default user address space control. */
426 	regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
427 		(PSW_USER_BITS & PSW_MASK_ASC) |
428 		(regs->psw.mask & ~PSW_MASK_ASC);
429 	regs->psw.addr = (unsigned long) ksig->ka.sa.sa_handler;
430 
431 	regs->gprs[2] = ksig->sig;
432 	regs->gprs[3] = (unsigned long) &frame->info;
433 	regs->gprs[4] = (unsigned long) &frame->uc;
434 	regs->gprs[5] = current->thread.last_break;
435 	return 0;
436 }
437 
438 static void handle_signal(struct ksignal *ksig, sigset_t *oldset,
439 			  struct pt_regs *regs)
440 {
441 	int ret;
442 
443 	/* Set up the stack frame */
444 	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
445 		ret = setup_rt_frame(ksig, oldset, regs);
446 	else
447 		ret = setup_frame(ksig->sig, &ksig->ka, oldset, regs);
448 
449 	signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP));
450 }
451 
452 /*
453  * Note that 'init' is a special process: it doesn't get signals it doesn't
454  * want to handle. Thus you cannot kill init even with a SIGKILL even by
455  * mistake.
456  *
457  * Note that we go through the signals twice: once to check the signals that
458  * the kernel can handle, and then we build all the user-level signal handling
459  * stack-frames in one go after that.
460  */
461 void do_signal(struct pt_regs *regs)
462 {
463 	struct ksignal ksig;
464 	sigset_t *oldset = sigmask_to_save();
465 
466 	/*
467 	 * Get signal to deliver. When running under ptrace, at this point
468 	 * the debugger may change all our registers, including the system
469 	 * call information.
470 	 */
471 	current->thread.system_call =
472 		test_pt_regs_flag(regs, PIF_SYSCALL) ? regs->int_code : 0;
473 
474 	if (get_signal(&ksig)) {
475 		/* Whee!  Actually deliver the signal.  */
476 		if (current->thread.system_call) {
477 			regs->int_code = current->thread.system_call;
478 			/* Check for system call restarting. */
479 			switch (regs->gprs[2]) {
480 			case -ERESTART_RESTARTBLOCK:
481 			case -ERESTARTNOHAND:
482 				regs->gprs[2] = -EINTR;
483 				break;
484 			case -ERESTARTSYS:
485 				if (!(ksig.ka.sa.sa_flags & SA_RESTART)) {
486 					regs->gprs[2] = -EINTR;
487 					break;
488 				}
489 			/* fallthrough */
490 			case -ERESTARTNOINTR:
491 				regs->gprs[2] = regs->orig_gpr2;
492 				regs->psw.addr =
493 					__rewind_psw(regs->psw,
494 						     regs->int_code >> 16);
495 				break;
496 			}
497 		}
498 		/* No longer in a system call */
499 		clear_pt_regs_flag(regs, PIF_SYSCALL);
500 
501 		if (is_compat_task())
502 			handle_signal32(&ksig, oldset, regs);
503 		else
504 			handle_signal(&ksig, oldset, regs);
505 		return;
506 	}
507 
508 	/* No handlers present - check for system call restart */
509 	clear_pt_regs_flag(regs, PIF_SYSCALL);
510 	if (current->thread.system_call) {
511 		regs->int_code = current->thread.system_call;
512 		switch (regs->gprs[2]) {
513 		case -ERESTART_RESTARTBLOCK:
514 			/* Restart with sys_restart_syscall */
515 			regs->int_code = __NR_restart_syscall;
516 		/* fallthrough */
517 		case -ERESTARTNOHAND:
518 		case -ERESTARTSYS:
519 		case -ERESTARTNOINTR:
520 			/* Restart system call with magic TIF bit. */
521 			regs->gprs[2] = regs->orig_gpr2;
522 			set_pt_regs_flag(regs, PIF_SYSCALL);
523 			if (test_thread_flag(TIF_SINGLE_STEP))
524 				clear_pt_regs_flag(regs, PIF_PER_TRAP);
525 			break;
526 		}
527 	}
528 
529 	/*
530 	 * If there's no signal to deliver, we just put the saved sigmask back.
531 	 */
532 	restore_saved_sigmask();
533 }
534 
535 void do_notify_resume(struct pt_regs *regs)
536 {
537 	clear_thread_flag(TIF_NOTIFY_RESUME);
538 	tracehook_notify_resume(regs);
539 }
540