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