xref: /linux/arch/s390/kernel/compat_signal.c (revision 4b660dbd9ee2059850fd30e0df420ca7a38a1856)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2000, 2006
4  *    Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
5  *               Gerhard Tonn (ton@de.ibm.com)
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/compat.h>
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/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/unistd.h>
23 #include <linux/stddef.h>
24 #include <linux/tty.h>
25 #include <linux/personality.h>
26 #include <linux/binfmts.h>
27 #include <asm/access-regs.h>
28 #include <asm/ucontext.h>
29 #include <linux/uaccess.h>
30 #include <asm/lowcore.h>
31 #include <asm/vdso.h>
32 #include <asm/fpu.h>
33 #include "compat_linux.h"
34 #include "compat_ptrace.h"
35 #include "entry.h"
36 
37 typedef struct
38 {
39 	__u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
40 	struct sigcontext32 sc;
41 	_sigregs32 sregs;
42 	int signo;
43 	_sigregs_ext32 sregs_ext;
44 	__u16 svc_insn;		/* Offset of svc_insn is NOT fixed! */
45 } sigframe32;
46 
47 typedef struct
48 {
49 	__u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
50 	__u16 svc_insn;
51 	compat_siginfo_t info;
52 	struct ucontext32 uc;
53 } rt_sigframe32;
54 
55 /* Store registers needed to create the signal frame */
56 static void store_sigregs(void)
57 {
58 	save_access_regs(current->thread.acrs);
59 	save_user_fpu_regs();
60 }
61 
62 /* Load registers after signal return */
63 static void load_sigregs(void)
64 {
65 	restore_access_regs(current->thread.acrs);
66 }
67 
68 static int save_sigregs32(struct pt_regs *regs, _sigregs32 __user *sregs)
69 {
70 	_sigregs32 user_sregs;
71 	int i;
72 
73 	user_sregs.regs.psw.mask = (__u32)(regs->psw.mask >> 32);
74 	user_sregs.regs.psw.mask &= PSW32_MASK_USER | PSW32_MASK_RI;
75 	user_sregs.regs.psw.mask |= PSW32_USER_BITS;
76 	user_sregs.regs.psw.addr = (__u32) regs->psw.addr |
77 		(__u32)(regs->psw.mask & PSW_MASK_BA);
78 	for (i = 0; i < NUM_GPRS; i++)
79 		user_sregs.regs.gprs[i] = (__u32) regs->gprs[i];
80 	memcpy(&user_sregs.regs.acrs, current->thread.acrs,
81 	       sizeof(user_sregs.regs.acrs));
82 	fpregs_store((_s390_fp_regs *) &user_sregs.fpregs, &current->thread.ufpu);
83 	if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs32)))
84 		return -EFAULT;
85 	return 0;
86 }
87 
88 static int restore_sigregs32(struct pt_regs *regs,_sigregs32 __user *sregs)
89 {
90 	_sigregs32 user_sregs;
91 	int i;
92 
93 	/* Always make any pending restarted system call return -EINTR */
94 	current->restart_block.fn = do_no_restart_syscall;
95 
96 	if (__copy_from_user(&user_sregs, &sregs->regs, sizeof(user_sregs)))
97 		return -EFAULT;
98 
99 	if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW32_MASK_RI))
100 		return -EINVAL;
101 
102 	/* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
103 	regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) |
104 		(__u64)(user_sregs.regs.psw.mask & PSW32_MASK_USER) << 32 |
105 		(__u64)(user_sregs.regs.psw.mask & PSW32_MASK_RI) << 32 |
106 		(__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_AMODE);
107 	/* Check for invalid user address space control. */
108 	if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
109 		regs->psw.mask = PSW_ASC_PRIMARY |
110 			(regs->psw.mask & ~PSW_MASK_ASC);
111 	regs->psw.addr = (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_INSN);
112 	for (i = 0; i < NUM_GPRS; i++)
113 		regs->gprs[i] = (__u64) user_sregs.regs.gprs[i];
114 	memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
115 	       sizeof(current->thread.acrs));
116 	fpregs_load((_s390_fp_regs *)&user_sregs.fpregs, &current->thread.ufpu);
117 
118 	clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */
119 	return 0;
120 }
121 
122 static int save_sigregs_ext32(struct pt_regs *regs,
123 			      _sigregs_ext32 __user *sregs_ext)
124 {
125 	__u32 gprs_high[NUM_GPRS];
126 	__u64 vxrs[__NUM_VXRS_LOW];
127 	int i;
128 
129 	/* Save high gprs to signal stack */
130 	for (i = 0; i < NUM_GPRS; i++)
131 		gprs_high[i] = regs->gprs[i] >> 32;
132 	if (__copy_to_user(&sregs_ext->gprs_high, &gprs_high,
133 			   sizeof(sregs_ext->gprs_high)))
134 		return -EFAULT;
135 
136 	/* Save vector registers to signal stack */
137 	if (cpu_has_vx()) {
138 		for (i = 0; i < __NUM_VXRS_LOW; i++)
139 			vxrs[i] = current->thread.ufpu.vxrs[i].low;
140 		if (__copy_to_user(&sregs_ext->vxrs_low, vxrs,
141 				   sizeof(sregs_ext->vxrs_low)) ||
142 		    __copy_to_user(&sregs_ext->vxrs_high,
143 				   current->thread.ufpu.vxrs + __NUM_VXRS_LOW,
144 				   sizeof(sregs_ext->vxrs_high)))
145 			return -EFAULT;
146 	}
147 	return 0;
148 }
149 
150 static int restore_sigregs_ext32(struct pt_regs *regs,
151 				 _sigregs_ext32 __user *sregs_ext)
152 {
153 	__u32 gprs_high[NUM_GPRS];
154 	__u64 vxrs[__NUM_VXRS_LOW];
155 	int i;
156 
157 	/* Restore high gprs from signal stack */
158 	if (__copy_from_user(&gprs_high, &sregs_ext->gprs_high,
159 			     sizeof(sregs_ext->gprs_high)))
160 		return -EFAULT;
161 	for (i = 0; i < NUM_GPRS; i++)
162 		*(__u32 *)&regs->gprs[i] = gprs_high[i];
163 
164 	/* Restore vector registers from signal stack */
165 	if (cpu_has_vx()) {
166 		if (__copy_from_user(vxrs, &sregs_ext->vxrs_low,
167 				     sizeof(sregs_ext->vxrs_low)) ||
168 		    __copy_from_user(current->thread.ufpu.vxrs + __NUM_VXRS_LOW,
169 				     &sregs_ext->vxrs_high,
170 				     sizeof(sregs_ext->vxrs_high)))
171 			return -EFAULT;
172 		for (i = 0; i < __NUM_VXRS_LOW; i++)
173 			current->thread.ufpu.vxrs[i].low = vxrs[i];
174 	}
175 	return 0;
176 }
177 
178 COMPAT_SYSCALL_DEFINE0(sigreturn)
179 {
180 	struct pt_regs *regs = task_pt_regs(current);
181 	sigframe32 __user *frame = (sigframe32 __user *)regs->gprs[15];
182 	sigset_t set;
183 
184 	if (get_compat_sigset(&set, (compat_sigset_t __user *)frame->sc.oldmask))
185 		goto badframe;
186 	set_current_blocked(&set);
187 	save_user_fpu_regs();
188 	if (restore_sigregs32(regs, &frame->sregs))
189 		goto badframe;
190 	if (restore_sigregs_ext32(regs, &frame->sregs_ext))
191 		goto badframe;
192 	load_sigregs();
193 	return regs->gprs[2];
194 badframe:
195 	force_sig(SIGSEGV);
196 	return 0;
197 }
198 
199 COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
200 {
201 	struct pt_regs *regs = task_pt_regs(current);
202 	rt_sigframe32 __user *frame = (rt_sigframe32 __user *)regs->gprs[15];
203 	sigset_t set;
204 
205 	if (get_compat_sigset(&set, &frame->uc.uc_sigmask))
206 		goto badframe;
207 	set_current_blocked(&set);
208 	if (compat_restore_altstack(&frame->uc.uc_stack))
209 		goto badframe;
210 	save_user_fpu_regs();
211 	if (restore_sigregs32(regs, &frame->uc.uc_mcontext))
212 		goto badframe;
213 	if (restore_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
214 		goto badframe;
215 	load_sigregs();
216 	return regs->gprs[2];
217 badframe:
218 	force_sig(SIGSEGV);
219 	return 0;
220 }
221 
222 /*
223  * Set up a signal frame.
224  */
225 
226 
227 /*
228  * Determine which stack to use..
229  */
230 static inline void __user *
231 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
232 {
233 	unsigned long sp;
234 
235 	/* Default to using normal stack */
236 	sp = (unsigned long) A(regs->gprs[15]);
237 
238 	/* Overflow on alternate signal stack gives SIGSEGV. */
239 	if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
240 		return (void __user *) -1UL;
241 
242 	/* This is the X/Open sanctioned signal stack switching.  */
243 	if (ka->sa.sa_flags & SA_ONSTACK) {
244 		if (! sas_ss_flags(sp))
245 			sp = current->sas_ss_sp + current->sas_ss_size;
246 	}
247 
248 	return (void __user *)((sp - frame_size) & -8ul);
249 }
250 
251 static int setup_frame32(struct ksignal *ksig, sigset_t *set,
252 			 struct pt_regs *regs)
253 {
254 	int sig = ksig->sig;
255 	sigframe32 __user *frame;
256 	unsigned long restorer;
257 	size_t frame_size;
258 
259 	/*
260 	 * gprs_high are always present for 31-bit compat tasks.
261 	 * The space for vector registers is only allocated if
262 	 * the machine supports it
263 	 */
264 	frame_size = sizeof(*frame) - sizeof(frame->sregs_ext.__reserved);
265 	if (!cpu_has_vx())
266 		frame_size -= sizeof(frame->sregs_ext.vxrs_low) +
267 			      sizeof(frame->sregs_ext.vxrs_high);
268 	frame = get_sigframe(&ksig->ka, regs, frame_size);
269 	if (frame == (void __user *) -1UL)
270 		return -EFAULT;
271 
272 	/* Set up backchain. */
273 	if (__put_user(regs->gprs[15], (unsigned int __user *) frame))
274 		return -EFAULT;
275 
276 	/* Create struct sigcontext32 on the signal stack */
277 	if (put_compat_sigset((compat_sigset_t __user *)frame->sc.oldmask,
278 			      set, sizeof(compat_sigset_t)))
279 		return -EFAULT;
280 	if (__put_user(ptr_to_compat(&frame->sregs), &frame->sc.sregs))
281 		return -EFAULT;
282 
283 	/* Store registers needed to create the signal frame */
284 	store_sigregs();
285 
286 	/* Create _sigregs32 on the signal stack */
287 	if (save_sigregs32(regs, &frame->sregs))
288 		return -EFAULT;
289 
290 	/* Place signal number on stack to allow backtrace from handler.  */
291 	if (__put_user(regs->gprs[2], (int __force __user *) &frame->signo))
292 		return -EFAULT;
293 
294 	/* Create _sigregs_ext32 on the signal stack */
295 	if (save_sigregs_ext32(regs, &frame->sregs_ext))
296 		return -EFAULT;
297 
298 	/* Set up to return from userspace.  If provided, use a stub
299 	   already in userspace.  */
300 	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
301 		restorer = (unsigned long __force)
302 			ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
303 	} else {
304 		restorer = VDSO32_SYMBOL(current, sigreturn);
305         }
306 
307 	/* Set up registers for signal handler */
308 	regs->gprs[14] = restorer;
309 	regs->gprs[15] = (__force __u64) frame;
310 	/* Force 31 bit amode and default user address space control. */
311 	regs->psw.mask = PSW_MASK_BA |
312 		(PSW_USER_BITS & PSW_MASK_ASC) |
313 		(regs->psw.mask & ~PSW_MASK_ASC);
314 	regs->psw.addr = (__force __u64) ksig->ka.sa.sa_handler;
315 
316 	regs->gprs[2] = sig;
317 	regs->gprs[3] = (__force __u64) &frame->sc;
318 
319 	/* We forgot to include these in the sigcontext.
320 	   To avoid breaking binary compatibility, they are passed as args. */
321 	if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
322 	    sig == SIGTRAP || sig == SIGFPE) {
323 		/* set extra registers only for synchronous signals */
324 		regs->gprs[4] = regs->int_code & 127;
325 		regs->gprs[5] = regs->int_parm_long;
326 		regs->gprs[6] = current->thread.last_break;
327 	}
328 
329 	return 0;
330 }
331 
332 static int setup_rt_frame32(struct ksignal *ksig, sigset_t *set,
333 			    struct pt_regs *regs)
334 {
335 	rt_sigframe32 __user *frame;
336 	unsigned long restorer;
337 	size_t frame_size;
338 	u32 uc_flags;
339 
340 	frame_size = sizeof(*frame) -
341 		     sizeof(frame->uc.uc_mcontext_ext.__reserved);
342 	/*
343 	 * gprs_high are always present for 31-bit compat tasks.
344 	 * The space for vector registers is only allocated if
345 	 * the machine supports it
346 	 */
347 	uc_flags = UC_GPRS_HIGH;
348 	if (cpu_has_vx()) {
349 		uc_flags |= UC_VXRS;
350 	} else {
351 		frame_size -= sizeof(frame->uc.uc_mcontext_ext.vxrs_low) +
352 			      sizeof(frame->uc.uc_mcontext_ext.vxrs_high);
353 	}
354 	frame = get_sigframe(&ksig->ka, regs, frame_size);
355 	if (frame == (void __user *) -1UL)
356 		return -EFAULT;
357 
358 	/* Set up backchain. */
359 	if (__put_user(regs->gprs[15], (unsigned int __force __user *) frame))
360 		return -EFAULT;
361 
362 	/* Set up to return from userspace.  If provided, use a stub
363 	   already in userspace.  */
364 	if (ksig->ka.sa.sa_flags & SA_RESTORER) {
365 		restorer = (unsigned long __force)
366 			ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
367 	} else {
368 		restorer = VDSO32_SYMBOL(current, rt_sigreturn);
369 	}
370 
371 	/* Create siginfo on the signal stack */
372 	if (copy_siginfo_to_user32(&frame->info, &ksig->info))
373 		return -EFAULT;
374 
375 	/* Store registers needed to create the signal frame */
376 	store_sigregs();
377 
378 	/* Create ucontext on the signal stack. */
379 	if (__put_user(uc_flags, &frame->uc.uc_flags) ||
380 	    __put_user(0, &frame->uc.uc_link) ||
381 	    __compat_save_altstack(&frame->uc.uc_stack, regs->gprs[15]) ||
382 	    save_sigregs32(regs, &frame->uc.uc_mcontext) ||
383 	    put_compat_sigset(&frame->uc.uc_sigmask, set, sizeof(compat_sigset_t)) ||
384 	    save_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
385 		return -EFAULT;
386 
387 	/* Set up registers for signal handler */
388 	regs->gprs[14] = restorer;
389 	regs->gprs[15] = (__force __u64) frame;
390 	/* Force 31 bit amode and default user address space control. */
391 	regs->psw.mask = PSW_MASK_BA |
392 		(PSW_USER_BITS & PSW_MASK_ASC) |
393 		(regs->psw.mask & ~PSW_MASK_ASC);
394 	regs->psw.addr = (__u64 __force) ksig->ka.sa.sa_handler;
395 
396 	regs->gprs[2] = ksig->sig;
397 	regs->gprs[3] = (__force __u64) &frame->info;
398 	regs->gprs[4] = (__force __u64) &frame->uc;
399 	regs->gprs[5] = current->thread.last_break;
400 	return 0;
401 }
402 
403 /*
404  * OK, we're invoking a handler
405  */
406 
407 void handle_signal32(struct ksignal *ksig, sigset_t *oldset,
408 		     struct pt_regs *regs)
409 {
410 	int ret;
411 
412 	/* Set up the stack frame */
413 	if (ksig->ka.sa.sa_flags & SA_SIGINFO)
414 		ret = setup_rt_frame32(ksig, oldset, regs);
415 	else
416 		ret = setup_frame32(ksig, oldset, regs);
417 
418 	signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP));
419 }
420 
421