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