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