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
11 #include <linux/compat.h>
12 #include <linux/sched.h>
13 #include <linux/sched/task_stack.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"
36 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
37 struct sigcontext32 sc;
40 _sigregs_ext32 sregs_ext;
41 __u16 svc_insn; /* Offset of svc_insn is NOT fixed! */
46 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
48 compat_siginfo_t info;
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,
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)
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
76 err = __put_user(from->si_signo, &to->si_signo);
77 err |= __put_user(from->si_errno, &to->si_errno);
78 err |= __put_user(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);
82 switch (siginfo_layout(from->si_signo, from->si_code)) {
84 err |= __put_user(from->si_int, &to->si_int);
87 err |= __put_user(from->si_pid, &to->si_pid);
88 err |= __put_user(from->si_uid, &to->si_uid);
91 err |= __put_user(from->si_pid, &to->si_pid);
92 err |= __put_user(from->si_uid, &to->si_uid);
93 err |= __put_user(from->si_utime, &to->si_utime);
94 err |= __put_user(from->si_stime, &to->si_stime);
95 err |= __put_user(from->si_status, &to->si_status);
98 err |= __put_user((unsigned long) from->si_addr,
102 err |= __put_user(from->si_band, &to->si_band);
103 err |= __put_user(from->si_fd, &to->si_fd);
106 err |= __put_user(from->si_tid, &to->si_tid);
107 err |= __put_user(from->si_overrun, &to->si_overrun);
108 err |= __put_user(from->si_int, &to->si_int);
114 return err ? -EFAULT : 0;
117 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
122 err = __get_user(to->si_signo, &from->si_signo);
123 err |= __get_user(to->si_errno, &from->si_errno);
124 err |= __get_user(to->si_code, &from->si_code);
127 err |= __copy_from_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
129 switch (siginfo_layout(to->si_signo, to->si_code)) {
131 err |= __get_user(to->si_int, &from->si_int);
134 err |= __get_user(to->si_pid, &from->si_pid);
135 err |= __get_user(to->si_uid, &from->si_uid);
138 err |= __get_user(to->si_pid, &from->si_pid);
139 err |= __get_user(to->si_uid, &from->si_uid);
140 err |= __get_user(to->si_utime, &from->si_utime);
141 err |= __get_user(to->si_stime, &from->si_stime);
142 err |= __get_user(to->si_status, &from->si_status);
145 err |= __get_user(tmp, &from->si_addr);
146 to->si_addr = (void __force __user *)
147 (u64) (tmp & PSW32_ADDR_INSN);
150 err |= __get_user(to->si_band, &from->si_band);
151 err |= __get_user(to->si_fd, &from->si_fd);
154 err |= __get_user(to->si_tid, &from->si_tid);
155 err |= __get_user(to->si_overrun, &from->si_overrun);
156 err |= __get_user(to->si_int, &from->si_int);
162 return err ? -EFAULT : 0;
165 /* Store registers needed to create the signal frame */
166 static void store_sigregs(void)
168 save_access_regs(current->thread.acrs);
172 /* Load registers after signal return */
173 static void load_sigregs(void)
175 restore_access_regs(current->thread.acrs);
178 static int save_sigregs32(struct pt_regs *regs, _sigregs32 __user *sregs)
180 _sigregs32 user_sregs;
183 user_sregs.regs.psw.mask = (__u32)(regs->psw.mask >> 32);
184 user_sregs.regs.psw.mask &= PSW32_MASK_USER | PSW32_MASK_RI;
185 user_sregs.regs.psw.mask |= PSW32_USER_BITS;
186 user_sregs.regs.psw.addr = (__u32) regs->psw.addr |
187 (__u32)(regs->psw.mask & PSW_MASK_BA);
188 for (i = 0; i < NUM_GPRS; i++)
189 user_sregs.regs.gprs[i] = (__u32) regs->gprs[i];
190 memcpy(&user_sregs.regs.acrs, current->thread.acrs,
191 sizeof(user_sregs.regs.acrs));
192 fpregs_store((_s390_fp_regs *) &user_sregs.fpregs, ¤t->thread.fpu);
193 if (__copy_to_user(sregs, &user_sregs, sizeof(_sigregs32)))
198 static int restore_sigregs32(struct pt_regs *regs,_sigregs32 __user *sregs)
200 _sigregs32 user_sregs;
203 /* Alwys make any pending restarted system call return -EINTR */
204 current->restart_block.fn = do_no_restart_syscall;
206 if (__copy_from_user(&user_sregs, &sregs->regs, sizeof(user_sregs)))
209 if (!is_ri_task(current) && (user_sregs.regs.psw.mask & PSW32_MASK_RI))
212 /* Test the floating-point-control word. */
213 if (test_fp_ctl(user_sregs.fpregs.fpc))
216 /* Use regs->psw.mask instead of PSW_USER_BITS to preserve PER bit. */
217 regs->psw.mask = (regs->psw.mask & ~(PSW_MASK_USER | PSW_MASK_RI)) |
218 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_USER) << 32 |
219 (__u64)(user_sregs.regs.psw.mask & PSW32_MASK_RI) << 32 |
220 (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_AMODE);
221 /* Check for invalid user address space control. */
222 if ((regs->psw.mask & PSW_MASK_ASC) == PSW_ASC_HOME)
223 regs->psw.mask = PSW_ASC_PRIMARY |
224 (regs->psw.mask & ~PSW_MASK_ASC);
225 regs->psw.addr = (__u64)(user_sregs.regs.psw.addr & PSW32_ADDR_INSN);
226 for (i = 0; i < NUM_GPRS; i++)
227 regs->gprs[i] = (__u64) user_sregs.regs.gprs[i];
228 memcpy(¤t->thread.acrs, &user_sregs.regs.acrs,
229 sizeof(current->thread.acrs));
230 fpregs_load((_s390_fp_regs *) &user_sregs.fpregs, ¤t->thread.fpu);
232 clear_pt_regs_flag(regs, PIF_SYSCALL); /* No longer in a system call */
236 static int save_sigregs_ext32(struct pt_regs *regs,
237 _sigregs_ext32 __user *sregs_ext)
239 __u32 gprs_high[NUM_GPRS];
240 __u64 vxrs[__NUM_VXRS_LOW];
243 /* Save high gprs to signal stack */
244 for (i = 0; i < NUM_GPRS; i++)
245 gprs_high[i] = regs->gprs[i] >> 32;
246 if (__copy_to_user(&sregs_ext->gprs_high, &gprs_high,
247 sizeof(sregs_ext->gprs_high)))
250 /* Save vector registers to signal stack */
251 if (MACHINE_HAS_VX) {
252 for (i = 0; i < __NUM_VXRS_LOW; i++)
253 vxrs[i] = *((__u64 *)(current->thread.fpu.vxrs + i) + 1);
254 if (__copy_to_user(&sregs_ext->vxrs_low, vxrs,
255 sizeof(sregs_ext->vxrs_low)) ||
256 __copy_to_user(&sregs_ext->vxrs_high,
257 current->thread.fpu.vxrs + __NUM_VXRS_LOW,
258 sizeof(sregs_ext->vxrs_high)))
264 static int restore_sigregs_ext32(struct pt_regs *regs,
265 _sigregs_ext32 __user *sregs_ext)
267 __u32 gprs_high[NUM_GPRS];
268 __u64 vxrs[__NUM_VXRS_LOW];
271 /* Restore high gprs from signal stack */
272 if (__copy_from_user(&gprs_high, &sregs_ext->gprs_high,
273 sizeof(sregs_ext->gprs_high)))
275 for (i = 0; i < NUM_GPRS; i++)
276 *(__u32 *)®s->gprs[i] = gprs_high[i];
278 /* Restore vector registers from signal stack */
279 if (MACHINE_HAS_VX) {
280 if (__copy_from_user(vxrs, &sregs_ext->vxrs_low,
281 sizeof(sregs_ext->vxrs_low)) ||
282 __copy_from_user(current->thread.fpu.vxrs + __NUM_VXRS_LOW,
283 &sregs_ext->vxrs_high,
284 sizeof(sregs_ext->vxrs_high)))
286 for (i = 0; i < __NUM_VXRS_LOW; i++)
287 *((__u64 *)(current->thread.fpu.vxrs + i) + 1) = vxrs[i];
292 COMPAT_SYSCALL_DEFINE0(sigreturn)
294 struct pt_regs *regs = task_pt_regs(current);
295 sigframe32 __user *frame = (sigframe32 __user *)regs->gprs[15];
296 compat_sigset_t cset;
299 if (__copy_from_user(&cset.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE32))
301 sigset32_to_sigset(cset.sig, set.sig);
302 set_current_blocked(&set);
304 if (restore_sigregs32(regs, &frame->sregs))
306 if (restore_sigregs_ext32(regs, &frame->sregs_ext))
309 return regs->gprs[2];
311 force_sig(SIGSEGV, current);
315 COMPAT_SYSCALL_DEFINE0(rt_sigreturn)
317 struct pt_regs *regs = task_pt_regs(current);
318 rt_sigframe32 __user *frame = (rt_sigframe32 __user *)regs->gprs[15];
319 compat_sigset_t cset;
322 if (__copy_from_user(&cset, &frame->uc.uc_sigmask, sizeof(cset)))
324 sigset32_to_sigset(cset.sig, set.sig);
325 set_current_blocked(&set);
326 if (compat_restore_altstack(&frame->uc.uc_stack))
329 if (restore_sigregs32(regs, &frame->uc.uc_mcontext))
331 if (restore_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
334 return regs->gprs[2];
336 force_sig(SIGSEGV, current);
341 * Set up a signal frame.
346 * Determine which stack to use..
348 static inline void __user *
349 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
353 /* Default to using normal stack */
354 sp = (unsigned long) A(regs->gprs[15]);
356 /* Overflow on alternate signal stack gives SIGSEGV. */
357 if (on_sig_stack(sp) && !on_sig_stack((sp - frame_size) & -8UL))
358 return (void __user *) -1UL;
360 /* This is the X/Open sanctioned signal stack switching. */
361 if (ka->sa.sa_flags & SA_ONSTACK) {
362 if (! sas_ss_flags(sp))
363 sp = current->sas_ss_sp + current->sas_ss_size;
366 return (void __user *)((sp - frame_size) & -8ul);
369 static int setup_frame32(struct ksignal *ksig, sigset_t *set,
370 struct pt_regs *regs)
373 sigframe32 __user *frame;
374 struct sigcontext32 sc;
375 unsigned long restorer;
379 * gprs_high are always present for 31-bit compat tasks.
380 * The space for vector registers is only allocated if
381 * the machine supports it
383 frame_size = sizeof(*frame) - sizeof(frame->sregs_ext.__reserved);
385 frame_size -= sizeof(frame->sregs_ext.vxrs_low) +
386 sizeof(frame->sregs_ext.vxrs_high);
387 frame = get_sigframe(&ksig->ka, regs, frame_size);
388 if (frame == (void __user *) -1UL)
391 /* Set up backchain. */
392 if (__put_user(regs->gprs[15], (unsigned int __user *) frame))
395 /* Create struct sigcontext32 on the signal stack */
396 sigset_to_sigset32(set->sig, sc.oldmask);
397 sc.sregs = (__u32)(unsigned long __force) &frame->sregs;
398 if (__copy_to_user(&frame->sc, &sc, sizeof(frame->sc)))
401 /* Store registers needed to create the signal frame */
404 /* Create _sigregs32 on the signal stack */
405 if (save_sigregs32(regs, &frame->sregs))
408 /* Place signal number on stack to allow backtrace from handler. */
409 if (__put_user(regs->gprs[2], (int __force __user *) &frame->signo))
412 /* Create _sigregs_ext32 on the signal stack */
413 if (save_sigregs_ext32(regs, &frame->sregs_ext))
416 /* Set up to return from userspace. If provided, use a stub
417 already in userspace. */
418 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
419 restorer = (unsigned long __force)
420 ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
422 /* Signal frames without vectors registers are short ! */
423 __u16 __user *svc = (void __user *) frame + frame_size - 2;
424 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn, svc))
426 restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE;
429 /* Set up registers for signal handler */
430 regs->gprs[14] = restorer;
431 regs->gprs[15] = (__force __u64) frame;
432 /* Force 31 bit amode and default user address space control. */
433 regs->psw.mask = PSW_MASK_BA |
434 (PSW_USER_BITS & PSW_MASK_ASC) |
435 (regs->psw.mask & ~PSW_MASK_ASC);
436 regs->psw.addr = (__force __u64) ksig->ka.sa.sa_handler;
439 regs->gprs[3] = (__force __u64) &frame->sc;
441 /* We forgot to include these in the sigcontext.
442 To avoid breaking binary compatibility, they are passed as args. */
443 if (sig == SIGSEGV || sig == SIGBUS || sig == SIGILL ||
444 sig == SIGTRAP || sig == SIGFPE) {
445 /* set extra registers only for synchronous signals */
446 regs->gprs[4] = regs->int_code & 127;
447 regs->gprs[5] = regs->int_parm_long;
448 regs->gprs[6] = current->thread.last_break;
454 static int setup_rt_frame32(struct ksignal *ksig, sigset_t *set,
455 struct pt_regs *regs)
457 compat_sigset_t cset;
458 rt_sigframe32 __user *frame;
459 unsigned long restorer;
463 frame_size = sizeof(*frame) -
464 sizeof(frame->uc.uc_mcontext_ext.__reserved);
466 * gprs_high are always present for 31-bit compat tasks.
467 * The space for vector registers is only allocated if
468 * the machine supports it
470 uc_flags = UC_GPRS_HIGH;
471 if (MACHINE_HAS_VX) {
474 frame_size -= sizeof(frame->uc.uc_mcontext_ext.vxrs_low) +
475 sizeof(frame->uc.uc_mcontext_ext.vxrs_high);
476 frame = get_sigframe(&ksig->ka, regs, frame_size);
477 if (frame == (void __user *) -1UL)
480 /* Set up backchain. */
481 if (__put_user(regs->gprs[15], (unsigned int __force __user *) frame))
484 /* Set up to return from userspace. If provided, use a stub
485 already in userspace. */
486 if (ksig->ka.sa.sa_flags & SA_RESTORER) {
487 restorer = (unsigned long __force)
488 ksig->ka.sa.sa_restorer | PSW32_ADDR_AMODE;
490 __u16 __user *svc = &frame->svc_insn;
491 if (__put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn, svc))
493 restorer = (unsigned long __force) svc | PSW32_ADDR_AMODE;
496 /* Create siginfo on the signal stack */
497 if (copy_siginfo_to_user32(&frame->info, &ksig->info))
500 /* Store registers needed to create the signal frame */
503 /* Create ucontext on the signal stack. */
504 sigset_to_sigset32(set->sig, cset.sig);
505 if (__put_user(uc_flags, &frame->uc.uc_flags) ||
506 __put_user(0, &frame->uc.uc_link) ||
507 __compat_save_altstack(&frame->uc.uc_stack, regs->gprs[15]) ||
508 save_sigregs32(regs, &frame->uc.uc_mcontext) ||
509 __copy_to_user(&frame->uc.uc_sigmask, &cset, sizeof(cset)) ||
510 save_sigregs_ext32(regs, &frame->uc.uc_mcontext_ext))
513 /* Set up registers for signal handler */
514 regs->gprs[14] = restorer;
515 regs->gprs[15] = (__force __u64) frame;
516 /* Force 31 bit amode and default user address space control. */
517 regs->psw.mask = PSW_MASK_BA |
518 (PSW_USER_BITS & PSW_MASK_ASC) |
519 (regs->psw.mask & ~PSW_MASK_ASC);
520 regs->psw.addr = (__u64 __force) ksig->ka.sa.sa_handler;
522 regs->gprs[2] = ksig->sig;
523 regs->gprs[3] = (__force __u64) &frame->info;
524 regs->gprs[4] = (__force __u64) &frame->uc;
525 regs->gprs[5] = current->thread.last_break;
530 * OK, we're invoking a handler
533 void handle_signal32(struct ksignal *ksig, sigset_t *oldset,
534 struct pt_regs *regs)
538 /* Set up the stack frame */
539 if (ksig->ka.sa.sa_flags & SA_SIGINFO)
540 ret = setup_rt_frame32(ksig, oldset, regs);
542 ret = setup_frame32(ksig, oldset, regs);
544 signal_setup_done(ret, ksig, test_thread_flag(TIF_SINGLE_STEP));