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