Linux 4.13.16
[linux/fpc-iii.git] / arch / s390 / kernel / compat_signal.c
blobc620049c61f2dfcd0e8824e28f3c329c077f8644
1 /*
2 * Copyright IBM Corp. 2000, 2006
3 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
4 * Gerhard Tonn (ton@de.ibm.com)
6 * Copyright (C) 1991, 1992 Linus Torvalds
8 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
9 */
11 #include <linux/compat.h>
12 #include <linux/sched.h>
13 #include <linux/sched/task_stack.h>
14 #include <linux/mm.h>
15 #include <linux/smp.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/unistd.h>
22 #include <linux/stddef.h>
23 #include <linux/tty.h>
24 #include <linux/personality.h>
25 #include <linux/binfmts.h>
26 #include <asm/ucontext.h>
27 #include <linux/uaccess.h>
28 #include <asm/lowcore.h>
29 #include <asm/switch_to.h>
30 #include "compat_linux.h"
31 #include "compat_ptrace.h"
32 #include "entry.h"
34 typedef struct
36 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
37 struct sigcontext32 sc;
38 _sigregs32 sregs;
39 int signo;
40 _sigregs_ext32 sregs_ext;
41 __u16 svc_insn; /* Offset of svc_insn is NOT fixed! */
42 } sigframe32;
44 typedef struct
46 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
47 __u16 svc_insn;
48 compat_siginfo_t info;
49 struct ucontext32 uc;
50 } rt_sigframe32;
52 static inline void sigset_to_sigset32(unsigned long *set64,
53 compat_sigset_word *set32)
55 set32[0] = (compat_sigset_word) set64[0];
56 set32[1] = (compat_sigset_word)(set64[0] >> 32);
59 static inline void sigset32_to_sigset(compat_sigset_word *set32,
60 unsigned long *set64)
62 set64[0] = (unsigned long) set32[0] | ((unsigned long) set32[1] << 32);
65 int copy_siginfo_to_user32(compat_siginfo_t __user *to, const siginfo_t *from)
67 int err;
69 /* If you change siginfo_t structure, please be sure
70 this code is fixed accordingly.
71 It should never copy any pad contained in the structure
72 to avoid security leaks, but must copy the generic
73 3 ints plus the relevant union member.
74 This routine must convert siginfo from 64bit to 32bit as well
75 at the same time. */
76 err = __put_user(from->si_signo, &to->si_signo);
77 err |= __put_user(from->si_errno, &to->si_errno);
78 err |= __put_user((short)from->si_code, &to->si_code);
79 if (from->si_code < 0)
80 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
81 else {
82 switch (from->si_code >> 16) {
83 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
84 case __SI_MESGQ >> 16:
85 err |= __put_user(from->si_int, &to->si_int);
86 /* fallthrough */
87 case __SI_KILL >> 16:
88 err |= __put_user(from->si_pid, &to->si_pid);
89 err |= __put_user(from->si_uid, &to->si_uid);
90 break;
91 case __SI_CHLD >> 16:
92 err |= __put_user(from->si_pid, &to->si_pid);
93 err |= __put_user(from->si_uid, &to->si_uid);
94 err |= __put_user(from->si_utime, &to->si_utime);
95 err |= __put_user(from->si_stime, &to->si_stime);
96 err |= __put_user(from->si_status, &to->si_status);
97 break;
98 case __SI_FAULT >> 16:
99 err |= __put_user((unsigned long) from->si_addr,
100 &to->si_addr);
101 break;
102 case __SI_POLL >> 16:
103 err |= __put_user(from->si_band, &to->si_band);
104 err |= __put_user(from->si_fd, &to->si_fd);
105 break;
106 case __SI_TIMER >> 16:
107 err |= __put_user(from->si_tid, &to->si_tid);
108 err |= __put_user(from->si_overrun, &to->si_overrun);
109 err |= __put_user(from->si_int, &to->si_int);
110 break;
111 default:
112 break;
115 return err ? -EFAULT : 0;
118 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
120 int err;
121 u32 tmp;
123 err = __get_user(to->si_signo, &from->si_signo);
124 err |= __get_user(to->si_errno, &from->si_errno);
125 err |= __get_user(to->si_code, &from->si_code);
127 if (to->si_code < 0)
128 err |= __copy_from_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
129 else {
130 switch (to->si_code >> 16) {
131 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
132 case __SI_MESGQ >> 16:
133 err |= __get_user(to->si_int, &from->si_int);
134 /* fallthrough */
135 case __SI_KILL >> 16:
136 err |= __get_user(to->si_pid, &from->si_pid);
137 err |= __get_user(to->si_uid, &from->si_uid);
138 break;
139 case __SI_CHLD >> 16:
140 err |= __get_user(to->si_pid, &from->si_pid);
141 err |= __get_user(to->si_uid, &from->si_uid);
142 err |= __get_user(to->si_utime, &from->si_utime);
143 err |= __get_user(to->si_stime, &from->si_stime);
144 err |= __get_user(to->si_status, &from->si_status);
145 break;
146 case __SI_FAULT >> 16:
147 err |= __get_user(tmp, &from->si_addr);
148 to->si_addr = (void __force __user *)
149 (u64) (tmp & PSW32_ADDR_INSN);
150 break;
151 case __SI_POLL >> 16:
152 err |= __get_user(to->si_band, &from->si_band);
153 err |= __get_user(to->si_fd, &from->si_fd);
154 break;
155 case __SI_TIMER >> 16:
156 err |= __get_user(to->si_tid, &from->si_tid);
157 err |= __get_user(to->si_overrun, &from->si_overrun);
158 err |= __get_user(to->si_int, &from->si_int);
159 break;
160 default:
161 break;
164 return err ? -EFAULT : 0;
167 /* Store registers needed to create the signal frame */
168 static void store_sigregs(void)
170 save_access_regs(current->thread.acrs);
171 save_fpu_regs();
174 /* Load registers after signal return */
175 static void load_sigregs(void)
177 restore_access_regs(current->thread.acrs);
180 static int save_sigregs32(struct pt_regs *regs, _sigregs32 __user *sregs)
182 _sigregs32 user_sregs;
183 int i;
185 user_sregs.regs.psw.mask = (__u32)(regs->psw.mask >> 32);
186 user_sregs.regs.psw.mask &= PSW32_MASK_USER | PSW32_MASK_RI;
187 user_sregs.regs.psw.mask |= PSW32_USER_BITS;
188 user_sregs.regs.psw.addr = (__u32) regs->psw.addr |
189 (__u32)(regs->psw.mask & PSW_MASK_BA);
190 for (i = 0; i < NUM_GPRS; i++)
191 user_sregs.regs.gprs[i] = (__u32) regs->gprs[i];
192 memcpy(&user_sregs.regs.acrs, current->thread.acrs,
193 sizeof(user_sregs.regs.acrs));
194 fpregs_store((_s390_fp_regs *) &user_sregs.fpregs, &current->thread.fpu);
195 if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs32)))
196 return -EFAULT;
197 return 0;
200 static int restore_sigregs32(struct pt_regs *regs,_sigregs32 __user *sregs)
202 _sigregs32 user_sregs;
203 int i;
205 /* Alwys make any pending restarted system call return -EINTR */
206 current->restart_block.fn = do_no_restart_syscall;
208 if (__copy_from_user(&user_sregs, &sregs->regs, sizeof(user_sregs)))
209 return -EFAULT;
211 if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW32_MASK_RI))
212 return -EINVAL;
214 /* Test the floating-point-control word. */
215 if (test_fp_ctl(user_sregs.fpregs.fpc))
216 return -EINVAL;
218 /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
219 regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) |
220 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_USER) << 32 |
221 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_RI) << 32 |
222 (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_AMODE);
223 /* Check for invalid user address space control. */
224 if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
225 regs->psw.mask = PSW_ASC_PRIMARY |
226 (regs->psw.mask & ~PSW_MASK_ASC);
227 regs->psw.addr = (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_INSN);
228 for (i = 0; i < NUM_GPRS; i++)
229 regs->gprs[i] = (__u64) user_sregs.regs.gprs[i];
230 memcpy(&current->thread.acrs, &user_sregs.regs.acrs,
231 sizeof(current->thread.acrs));
232 fpregs_load((_s390_fp_regs *) &user_sregs.fpregs, &current->thread.fpu);
234 clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */
235 return 0;
238 static int save_sigregs_ext32(struct pt_regs *regs,
239 _sigregs_ext32 __user *sregs_ext)
241 __u32 gprs_high[NUM_GPRS];
242 __u64 vxrs[__NUM_VXRS_LOW];
243 int i;
245 /* Save high gprs to signal stack */
246 for (i = 0; i < NUM_GPRS; i++)
247 gprs_high[i] = regs->gprs[i] >> 32;
248 if (__copy_to_user(&sregs_ext->gprs_high, &gprs_high,
249 sizeof(sregs_ext->gprs_high)))
250 return -EFAULT;
252 /* Save vector registers to signal stack */
253 if (MACHINE_HAS_VX) {
254 for (i = 0; i < __NUM_VXRS_LOW; i++)
255 vxrs[i] = *((__u64 *)(current->thread.fpu.vxrs + i) + 1);
256 if (__copy_to_user(&sregs_ext->vxrs_low, vxrs,
257 sizeof(sregs_ext->vxrs_low)) ||
258 __copy_to_user(&sregs_ext->vxrs_high,
259 current->thread.fpu.vxrs + __NUM_VXRS_LOW,
260 sizeof(sregs_ext->vxrs_high)))
261 return -EFAULT;
263 return 0;
266 static int restore_sigregs_ext32(struct pt_regs *regs,
267 _sigregs_ext32 __user *sregs_ext)
269 __u32 gprs_high[NUM_GPRS];
270 __u64 vxrs[__NUM_VXRS_LOW];
271 int i;
273 /* Restore high gprs from signal stack */
274 if (__copy_from_user(&gprs_high, &sregs_ext->gprs_high,
275 sizeof(sregs_ext->gprs_high)))
276 return -EFAULT;
277 for (i = 0; i < NUM_GPRS; i++)
278 *(__u32 *)&regs->gprs[i] = gprs_high[i];
280 /* Restore vector registers from signal stack */
281 if (MACHINE_HAS_VX) {
282 if (__copy_from_user(vxrs, &sregs_ext->vxrs_low,
283 sizeof(sregs_ext->vxrs_low)) ||
284 __copy_from_user(current->thread.fpu.vxrs + __NUM_VXRS_LOW,
285 &sregs_ext->vxrs_high,
286 sizeof(sregs_ext->vxrs_high)))
287 return -EFAULT;
288 for (i = 0; i < __NUM_VXRS_LOW; i++)
289 *((__u64 *)(current->thread.fpu.vxrs + i) + 1) = vxrs[i];
291 return 0;
294 COMPAT_SYSCALL_DEFINE0(sigreturn)
296 struct pt_regs *regs = task_pt_regs(current);
297 sigframe32 __user *frame = (sigframe32 __user *)regs->gprs[15];
298 compat_sigset_t cset;
299 sigset_t set;
301 if (__copy_from_user(&cset.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE32))
302 goto badframe;
303 sigset32_to_sigset(cset.sig, set.sig);
304 set_current_blocked(&set);
305 save_fpu_regs();
306 if (restore_sigregs32(regs, &frame->sregs))
307 goto badframe;
308 if (restore_sigregs_ext32(regs, &frame->sregs_ext))
309 goto badframe;
310 load_sigregs();
311 return regs->gprs[2];
312 badframe:
313 force_sig(SIGSEGV, current);
314 return 0;
317 COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
319 struct pt_regs *regs = task_pt_regs(current);
320 rt_sigframe32 __user *frame = (rt_sigframe32 __user *)regs->gprs[15];
321 compat_sigset_t cset;
322 sigset_t set;
324 if (__copy_from_user(&cset, &frame->uc.uc_sigmask, sizeof(cset)))
325 goto badframe;
326 sigset32_to_sigset(cset.sig, set.sig);
327 set_current_blocked(&set);
328 if (compat_restore_altstack(&frame->uc.uc_stack))
329 goto badframe;
330 save_fpu_regs();
331 if (restore_sigregs32(regs, &frame->uc.uc_mcontext))
332 goto badframe;
333 if (restore_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
334 goto badframe;
335 load_sigregs();
336 return regs->gprs[2];
337 badframe:
338 force_sig(SIGSEGV, current);
339 return 0;
343 * Set up a signal frame.
348 * Determine which stack to use..
350 static inline void __user *
351 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
353 unsigned long sp;
355 /* Default to using normal stack */
356 sp = (unsigned long) A(regs->gprs[15]);
358 /* Overflow on alternate signal stack gives SIGSEGV. */
359 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
360 return (void __user *) -1UL;
362 /* This is the X/Open sanctioned signal stack switching. */
363 if (ka->sa.sa_flags & SA_ONSTACK) {
364 if (! sas_ss_flags(sp))
365 sp = current->sas_ss_sp + current->sas_ss_size;
368 return (void __user *)((sp - frame_size) & -8ul);
371 static int setup_frame32(struct ksignal *ksig, sigset_t *set,
372 struct pt_regs *regs)
374 int sig = ksig->sig;
375 sigframe32 __user *frame;
376 struct sigcontext32 sc;
377 unsigned long restorer;
378 size_t frame_size;
381 * gprs_high are always present for 31-bit compat tasks.
382 * The space for vector registers is only allocated if
383 * the machine supports it
385 frame_size = sizeof(*frame) - sizeof(frame->sregs_ext.__reserved);
386 if (!MACHINE_HAS_VX)
387 frame_size -= sizeof(frame->sregs_ext.vxrs_low) +
388 sizeof(frame->sregs_ext.vxrs_high);
389 frame = get_sigframe(&ksig->ka, regs, frame_size);
390 if (frame == (void __user *) -1UL)
391 return -EFAULT;
393 /* Set up backchain. */
394 if (__put_user(regs->gprs[15], (unsigned int __user *) frame))
395 return -EFAULT;
397 /* Create struct sigcontext32 on the signal stack */
398 sigset_to_sigset32(set->sig, sc.oldmask);
399 sc.sregs = (__u32)(unsigned long __force) &frame->sregs;
400 if (__copy_to_user(&frame->sc, &sc, sizeof(frame->sc)))
401 return -EFAULT;
403 /* Store registers needed to create the signal frame */
404 store_sigregs();
406 /* Create _sigregs32 on the signal stack */
407 if (save_sigregs32(regs, &frame->sregs))
408 return -EFAULT;
410 /* Place signal number on stack to allow backtrace from handler. */
411 if (__put_user(regs->gprs[2], (int __force __user *) &frame->signo))
412 return -EFAULT;
414 /* Create _sigregs_ext32 on the signal stack */
415 if (save_sigregs_ext32(regs, &frame->sregs_ext))
416 return -EFAULT;
418 /* Set up to return from userspace. If provided, use a stub
419 already in userspace. */
420 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
421 restorer = (unsigned long __force)
422 ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
423 } else {
424 /* Signal frames without vectors registers are short ! */
425 __u16 __user *svc = (void __user *) frame + frame_size - 2;
426 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, svc))
427 return -EFAULT;
428 restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE;
431 /* Set up registers for signal handler */
432 regs->gprs[14] = restorer;
433 regs->gprs[15] = (__force __u64) frame;
434 /* Force 31 bit amode and default user address space control. */
435 regs->psw.mask = PSW_MASK_BA |
436 (PSW_USER_BITS & PSW_MASK_ASC) |
437 (regs->psw.mask & ~PSW_MASK_ASC);
438 regs->psw.addr = (__force __u64) ksig->ka.sa.sa_handler;
440 regs->gprs[2] = sig;
441 regs->gprs[3] = (__force __u64) &frame->sc;
443 /* We forgot to include these in the sigcontext.
444 To avoid breaking binary compatibility, they are passed as args. */
445 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
446 sig == SIGTRAP || sig == SIGFPE) {
447 /* set extra registers only for synchronous signals */
448 regs->gprs[4] = regs->int_code & 127;
449 regs->gprs[5] = regs->int_parm_long;
450 regs->gprs[6] = current->thread.last_break;
453 return 0;
456 static int setup_rt_frame32(struct ksignal *ksig, sigset_t *set,
457 struct pt_regs *regs)
459 compat_sigset_t cset;
460 rt_sigframe32 __user *frame;
461 unsigned long restorer;
462 size_t frame_size;
463 u32 uc_flags;
465 frame_size = sizeof(*frame) -
466 sizeof(frame->uc.uc_mcontext_ext.__reserved);
468 * gprs_high are always present for 31-bit compat tasks.
469 * The space for vector registers is only allocated if
470 * the machine supports it
472 uc_flags = UC_GPRS_HIGH;
473 if (MACHINE_HAS_VX) {
474 uc_flags |= UC_VXRS;
475 } else
476 frame_size -= sizeof(frame->uc.uc_mcontext_ext.vxrs_low) +
477 sizeof(frame->uc.uc_mcontext_ext.vxrs_high);
478 frame = get_sigframe(&ksig->ka, regs, frame_size);
479 if (frame == (void __user *) -1UL)
480 return -EFAULT;
482 /* Set up backchain. */
483 if (__put_user(regs->gprs[15], (unsigned int __force __user *) frame))
484 return -EFAULT;
486 /* Set up to return from userspace. If provided, use a stub
487 already in userspace. */
488 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
489 restorer = (unsigned long __force)
490 ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
491 } else {
492 __u16 __user *svc = &frame->svc_insn;
493 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, svc))
494 return -EFAULT;
495 restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE;
498 /* Create siginfo on the signal stack */
499 if (copy_siginfo_to_user32(&frame->info, &ksig->info))
500 return -EFAULT;
502 /* Store registers needed to create the signal frame */
503 store_sigregs();
505 /* Create ucontext on the signal stack. */
506 sigset_to_sigset32(set->sig, cset.sig);
507 if (__put_user(uc_flags, &frame->uc.uc_flags) ||
508 __put_user(0, &frame->uc.uc_link) ||
509 __compat_save_altstack(&frame->uc.uc_stack, regs->gprs[15]) ||
510 save_sigregs32(regs, &frame->uc.uc_mcontext) ||
511 __copy_to_user(&frame->uc.uc_sigmask, &cset, sizeof(cset)) ||
512 save_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
513 return -EFAULT;
515 /* Set up registers for signal handler */
516 regs->gprs[14] = restorer;
517 regs->gprs[15] = (__force __u64) frame;
518 /* Force 31 bit amode and default user address space control. */
519 regs->psw.mask = PSW_MASK_BA |
520 (PSW_USER_BITS & PSW_MASK_ASC) |
521 (regs->psw.mask & ~PSW_MASK_ASC);
522 regs->psw.addr = (__u64 __force) ksig->ka.sa.sa_handler;
524 regs->gprs[2] = ksig->sig;
525 regs->gprs[3] = (__force __u64) &frame->info;
526 regs->gprs[4] = (__force __u64) &frame->uc;
527 regs->gprs[5] = current->thread.last_break;
528 return 0;
532 * OK, we're invoking a handler
535 void handle_signal32(struct ksignal *ksig, sigset_t *oldset,
536 struct pt_regs *regs)
538 int ret;
540 /* Set up the stack frame */
541 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
542 ret = setup_rt_frame32(ksig, oldset, regs);
543 else
544 ret = setup_frame32(ksig, oldset, regs);
546 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP));