xref: /linux-5.15/arch/s390/kernel/signal.c (revision 9e2d59ad580d590134285f361a0e80f0e98c0207)
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 (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
120 		goto badframe;
121 	if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE))
122 		goto badframe;
123 	set_current_blocked(&set);
124 	if (restore_sigregs(regs, &frame->sregs))
125 		goto badframe;
126 	return regs->gprs[2];
127 badframe:
128 	force_sig(SIGSEGV, current);
129 	return 0;
130 }
131 
132 SYSCALL_DEFINE0(rt_sigreturn)
133 {
134 	struct pt_regs *regs = task_pt_regs(current);
135 	rt_sigframe __user *frame = (rt_sigframe __user *)regs->gprs[15];
136 	sigset_t set;
137 
138 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
139 		goto badframe;
140 	if (__copy_from_user(&set.sig, &frame->uc.uc_sigmask, sizeof(set)))
141 		goto badframe;
142 	set_current_blocked(&set);
143 	if (restore_sigregs(regs, &frame->uc.uc_mcontext))
144 		goto badframe;
145 	if (restore_altstack(&frame->uc.uc_stack))
146 		goto badframe;
147 	return regs->gprs[2];
148 badframe:
149 	force_sig(SIGSEGV, current);
150 	return 0;
151 }
152 
153 /*
154  * Set up a signal frame.
155  */
156 
157 
158 /*
159  * Determine which stack to use..
160  */
161 static inline void __user *
162 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
163 {
164 	unsigned long sp;
165 
166 	/* Default to using normal stack */
167 	sp = regs->gprs[15];
168 
169 	/* Overflow on alternate signal stack gives SIGSEGV. */
170 	if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
171 		return (void __user *) -1UL;
172 
173 	/* This is the X/Open sanctioned signal stack switching.  */
174 	if (ka->sa.sa_flags & SA_ONSTACK) {
175 		if (! sas_ss_flags(sp))
176 			sp = current->sas_ss_sp + current->sas_ss_size;
177 	}
178 
179 	return (void __user *)((sp - frame_size) & -8ul);
180 }
181 
182 static inline int map_signal(int sig)
183 {
184 	if (current_thread_info()->exec_domain
185 	    && current_thread_info()->exec_domain->signal_invmap
186 	    && sig < 32)
187 		return current_thread_info()->exec_domain->signal_invmap[sig];
188 	else
189 		return sig;
190 }
191 
192 static int setup_frame(int sig, struct k_sigaction *ka,
193 		       sigset_t *set, struct pt_regs * regs)
194 {
195 	sigframe __user *frame;
196 
197 	frame = get_sigframe(ka, regs, sizeof(sigframe));
198 	if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe)))
199 		goto give_sigsegv;
200 
201 	if (frame == (void __user *) -1UL)
202 		goto give_sigsegv;
203 
204 	if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE))
205 		goto give_sigsegv;
206 
207 	if (save_sigregs(regs, &frame->sregs))
208 		goto give_sigsegv;
209 	if (__put_user(&frame->sregs, &frame->sc.sregs))
210 		goto give_sigsegv;
211 
212 	/* Set up to return from userspace.  If provided, use a stub
213 	   already in userspace.  */
214 	if (ka->sa.sa_flags & SA_RESTORER) {
215                 regs->gprs[14] = (unsigned long)
216 			ka->sa.sa_restorer | PSW_ADDR_AMODE;
217 	} else {
218                 regs->gprs[14] = (unsigned long)
219 			frame->retcode | PSW_ADDR_AMODE;
220 		if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
221 	                       (u16 __user *)(frame->retcode)))
222 			goto give_sigsegv;
223 	}
224 
225 	/* Set up backchain. */
226 	if (__put_user(regs->gprs[15], (addr_t __user *) frame))
227 		goto give_sigsegv;
228 
229 	/* Set up registers for signal handler */
230 	regs->gprs[15] = (unsigned long) frame;
231 	/* Force default amode and default user address space control. */
232 	regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
233 		(psw_user_bits & PSW_MASK_ASC) |
234 		(regs->psw.mask & ~PSW_MASK_ASC);
235 	regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
236 
237 	regs->gprs[2] = map_signal(sig);
238 	regs->gprs[3] = (unsigned long) &frame->sc;
239 
240 	/* We forgot to include these in the sigcontext.
241 	   To avoid breaking binary compatibility, they are passed as args. */
242 	if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
243 	    sig == SIGTRAP || sig == SIGFPE) {
244 		/* set extra registers only for synchronous signals */
245 		regs->gprs[4] = regs->int_code & 127;
246 		regs->gprs[5] = regs->int_parm_long;
247 		regs->gprs[6] = task_thread_info(current)->last_break;
248 	}
249 
250 	/* Place signal number on stack to allow backtrace from handler.  */
251 	if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
252 		goto give_sigsegv;
253 	return 0;
254 
255 give_sigsegv:
256 	force_sigsegv(sig, current);
257 	return -EFAULT;
258 }
259 
260 static int setup_rt_frame(int sig, struct k_sigaction *ka, siginfo_t *info,
261 			   sigset_t *set, struct pt_regs * regs)
262 {
263 	int err = 0;
264 	rt_sigframe __user *frame;
265 
266 	frame = get_sigframe(ka, regs, sizeof(rt_sigframe));
267 	if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe)))
268 		goto give_sigsegv;
269 
270 	if (frame == (void __user *) -1UL)
271 		goto give_sigsegv;
272 
273 	if (copy_siginfo_to_user(&frame->info, info))
274 		goto give_sigsegv;
275 
276 	/* Create the ucontext.  */
277 	err |= __put_user(0, &frame->uc.uc_flags);
278 	err |= __put_user(NULL, &frame->uc.uc_link);
279 	err |= __save_altstack(&frame->uc.uc_stack, regs->gprs[15]);
280 	err |= save_sigregs(regs, &frame->uc.uc_mcontext);
281 	err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
282 	if (err)
283 		goto give_sigsegv;
284 
285 	/* Set up to return from userspace.  If provided, use a stub
286 	   already in userspace.  */
287 	if (ka->sa.sa_flags & SA_RESTORER) {
288                 regs->gprs[14] = (unsigned long)
289 			ka->sa.sa_restorer | PSW_ADDR_AMODE;
290 	} else {
291                 regs->gprs[14] = (unsigned long)
292 			frame->retcode | PSW_ADDR_AMODE;
293 		if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
294 			       (u16 __user *)(frame->retcode)))
295 			goto give_sigsegv;
296 	}
297 
298 	/* Set up backchain. */
299 	if (__put_user(regs->gprs[15], (addr_t __user *) frame))
300 		goto give_sigsegv;
301 
302 	/* Set up registers for signal handler */
303 	regs->gprs[15] = (unsigned long) frame;
304 	/* Force default amode and default user address space control. */
305 	regs->psw.mask = PSW_MASK_EA | PSW_MASK_BA |
306 		(psw_user_bits & PSW_MASK_ASC) |
307 		(regs->psw.mask & ~PSW_MASK_ASC);
308 	regs->psw.addr = (unsigned long) ka->sa.sa_handler | PSW_ADDR_AMODE;
309 
310 	regs->gprs[2] = map_signal(sig);
311 	regs->gprs[3] = (unsigned long) &frame->info;
312 	regs->gprs[4] = (unsigned long) &frame->uc;
313 	regs->gprs[5] = task_thread_info(current)->last_break;
314 	return 0;
315 
316 give_sigsegv:
317 	force_sigsegv(sig, current);
318 	return -EFAULT;
319 }
320 
321 static void handle_signal(unsigned long sig, struct k_sigaction *ka,
322 			 siginfo_t *info, sigset_t *oldset,
323 			 struct pt_regs *regs)
324 {
325 	int ret;
326 
327 	/* Set up the stack frame */
328 	if (ka->sa.sa_flags & SA_SIGINFO)
329 		ret = setup_rt_frame(sig, ka, info, oldset, regs);
330 	else
331 		ret = setup_frame(sig, ka, oldset, regs);
332 	if (ret)
333 		return;
334 	signal_delivered(sig, info, ka, regs,
335 				 test_thread_flag(TIF_SINGLE_STEP));
336 }
337 
338 /*
339  * Note that 'init' is a special process: it doesn't get signals it doesn't
340  * want to handle. Thus you cannot kill init even with a SIGKILL even by
341  * mistake.
342  *
343  * Note that we go through the signals twice: once to check the signals that
344  * the kernel can handle, and then we build all the user-level signal handling
345  * stack-frames in one go after that.
346  */
347 void do_signal(struct pt_regs *regs)
348 {
349 	siginfo_t info;
350 	int signr;
351 	struct k_sigaction ka;
352 	sigset_t *oldset = sigmask_to_save();
353 
354 	/*
355 	 * Get signal to deliver. When running under ptrace, at this point
356 	 * the debugger may change all our registers, including the system
357 	 * call information.
358 	 */
359 	current_thread_info()->system_call =
360 		test_thread_flag(TIF_SYSCALL) ? regs->int_code : 0;
361 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
362 
363 	if (signr > 0) {
364 		/* Whee!  Actually deliver the signal.  */
365 		if (current_thread_info()->system_call) {
366 			regs->int_code = current_thread_info()->system_call;
367 			/* Check for system call restarting. */
368 			switch (regs->gprs[2]) {
369 			case -ERESTART_RESTARTBLOCK:
370 			case -ERESTARTNOHAND:
371 				regs->gprs[2] = -EINTR;
372 				break;
373 			case -ERESTARTSYS:
374 				if (!(ka.sa.sa_flags & SA_RESTART)) {
375 					regs->gprs[2] = -EINTR;
376 					break;
377 				}
378 			/* fallthrough */
379 			case -ERESTARTNOINTR:
380 				regs->gprs[2] = regs->orig_gpr2;
381 				regs->psw.addr =
382 					__rewind_psw(regs->psw,
383 						     regs->int_code >> 16);
384 				break;
385 			}
386 		}
387 		/* No longer in a system call */
388 		clear_thread_flag(TIF_SYSCALL);
389 
390 		if (is_compat_task())
391 			handle_signal32(signr, &ka, &info, oldset, regs);
392 		else
393 			handle_signal(signr, &ka, &info, oldset, regs);
394 		return;
395 	}
396 
397 	/* No handlers present - check for system call restart */
398 	clear_thread_flag(TIF_SYSCALL);
399 	if (current_thread_info()->system_call) {
400 		regs->int_code = current_thread_info()->system_call;
401 		switch (regs->gprs[2]) {
402 		case -ERESTART_RESTARTBLOCK:
403 			/* Restart with sys_restart_syscall */
404 			regs->int_code = __NR_restart_syscall;
405 		/* fallthrough */
406 		case -ERESTARTNOHAND:
407 		case -ERESTARTSYS:
408 		case -ERESTARTNOINTR:
409 			/* Restart system call with magic TIF bit. */
410 			regs->gprs[2] = regs->orig_gpr2;
411 			set_thread_flag(TIF_SYSCALL);
412 			if (test_thread_flag(TIF_SINGLE_STEP))
413 				set_thread_flag(TIF_PER_TRAP);
414 			break;
415 		}
416 	}
417 
418 	/*
419 	 * If there's no signal to deliver, we just put the saved sigmask back.
420 	 */
421 	restore_saved_sigmask();
422 }
423 
424 void do_notify_resume(struct pt_regs *regs)
425 {
426 	clear_thread_flag(TIF_NOTIFY_RESUME);
427 	tracehook_notify_resume(regs);
428 }
429