2 * arch/s390/kernel/signal32.c
5 * Copyright (C) 2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
6 * Author(s): Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
7 * Gerhard Tonn (ton@de.ibm.com)
9 * Copyright (C) 1991, 1992 Linus Torvalds
11 * 1997-11-28 Modified for POSIX.1b signals by Richard Henderson
14 #include <linux/config.h>
15 #include <linux/compat.h>
16 #include <linux/sched.h>
18 #include <linux/smp.h>
19 #include <linux/smp_lock.h>
20 #include <linux/kernel.h>
21 #include <linux/signal.h>
22 #include <linux/errno.h>
23 #include <linux/wait.h>
24 #include <linux/ptrace.h>
25 #include <linux/unistd.h>
26 #include <linux/stddef.h>
27 #include <linux/tty.h>
28 #include <linux/personality.h>
29 #include <linux/binfmts.h>
30 #include <asm/ucontext.h>
31 #include <asm/uaccess.h>
32 #include <asm/lowcore.h>
33 #include "compat_linux.h"
34 #include "compat_ptrace.h"
36 #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
40 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
41 struct sigcontext32 sc;
44 __u8 retcode[S390_SYSCALL_SIZE];
49 __u8 callee_used_stack[__SIGNAL_FRAMESIZE32];
50 __u8 retcode[S390_SYSCALL_SIZE];
51 compat_siginfo_t info;
55 asmlinkage int FASTCALL(do_signal(struct pt_regs *regs, sigset_t *oldset));
57 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
61 if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
64 /* If you change siginfo_t structure, please be sure
65 this code is fixed accordingly.
66 It should never copy any pad contained in the structure
67 to avoid security leaks, but must copy the generic
68 3 ints plus the relevant union member.
69 This routine must convert siginfo from 64bit to 32bit as well
71 err = __put_user(from->si_signo, &to->si_signo);
72 err |= __put_user(from->si_errno, &to->si_errno);
73 err |= __put_user((short)from->si_code, &to->si_code);
74 if (from->si_code < 0)
75 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
77 switch (from->si_code >> 16) {
78 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
79 case __SI_MESGQ >> 16:
80 err |= __put_user(from->si_int, &to->si_int);
83 err |= __put_user(from->si_pid, &to->si_pid);
84 err |= __put_user(from->si_uid, &to->si_uid);
87 err |= __put_user(from->si_pid, &to->si_pid);
88 err |= __put_user(from->si_uid, &to->si_uid);
89 err |= __put_user(from->si_utime, &to->si_utime);
90 err |= __put_user(from->si_stime, &to->si_stime);
91 err |= __put_user(from->si_status, &to->si_status);
93 case __SI_FAULT >> 16:
94 err |= __put_user((unsigned long) from->si_addr,
98 err |= __put_user(from->si_band, &to->si_band);
99 err |= __put_user(from->si_fd, &to->si_fd);
101 case __SI_TIMER >> 16:
102 err |= __put_user(from->si_tid, &to->si_tid);
103 err |= __put_user(from->si_overrun, &to->si_overrun);
104 err |= __put_user(from->si_int, &to->si_int);
113 int copy_siginfo_from_user32(siginfo_t *to, compat_siginfo_t __user *from)
118 if (!access_ok (VERIFY_READ, from, sizeof(compat_siginfo_t)))
121 err = __get_user(to->si_signo, &from->si_signo);
122 err |= __get_user(to->si_errno, &from->si_errno);
123 err |= __get_user(to->si_code, &from->si_code);
126 err |= __copy_from_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
128 switch (to->si_code >> 16) {
129 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
130 case __SI_MESGQ >> 16:
131 err |= __get_user(to->si_int, &from->si_int);
133 case __SI_KILL >> 16:
134 err |= __get_user(to->si_pid, &from->si_pid);
135 err |= __get_user(to->si_uid, &from->si_uid);
137 case __SI_CHLD >> 16:
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);
144 case __SI_FAULT >> 16:
145 err |= __get_user(tmp, &from->si_addr);
146 to->si_addr = (void *)(u64) (tmp & PSW32_ADDR_INSN);
148 case __SI_POLL >> 16:
149 err |= __get_user(to->si_band, &from->si_band);
150 err |= __get_user(to->si_fd, &from->si_fd);
152 case __SI_TIMER >> 16:
153 err |= __get_user(to->si_tid, &from->si_tid);
154 err |= __get_user(to->si_overrun, &from->si_overrun);
155 err |= __get_user(to->si_int, &from->si_int);
165 * Atomically swap in the new signal mask, and wait for a signal.
168 sys32_sigsuspend(struct pt_regs * regs,int history0, int history1, old_sigset_t mask)
173 spin_lock_irq(¤t->sighand->siglock);
174 saveset = current->blocked;
175 siginitset(¤t->blocked, mask);
177 spin_unlock_irq(¤t->sighand->siglock);
178 regs->gprs[2] = -EINTR;
181 set_current_state(TASK_INTERRUPTIBLE);
183 if (do_signal(regs, &saveset))
189 sys32_rt_sigsuspend(struct pt_regs * regs, compat_sigset_t __user *unewset,
192 sigset_t saveset, newset;
193 compat_sigset_t set32;
195 /* XXX: Don't preclude handling different sized sigset_t's. */
196 if (sigsetsize != sizeof(sigset_t))
199 if (copy_from_user(&set32, unewset, sizeof(set32)))
201 switch (_NSIG_WORDS) {
202 case 4: newset.sig[3] = set32.sig[6] + (((long)set32.sig[7]) << 32);
203 case 3: newset.sig[2] = set32.sig[4] + (((long)set32.sig[5]) << 32);
204 case 2: newset.sig[1] = set32.sig[2] + (((long)set32.sig[3]) << 32);
205 case 1: newset.sig[0] = set32.sig[0] + (((long)set32.sig[1]) << 32);
207 sigdelsetmask(&newset, ~_BLOCKABLE);
209 spin_lock_irq(¤t->sighand->siglock);
210 saveset = current->blocked;
211 current->blocked = newset;
213 spin_unlock_irq(¤t->sighand->siglock);
214 regs->gprs[2] = -EINTR;
217 set_current_state(TASK_INTERRUPTIBLE);
219 if (do_signal(regs, &saveset))
225 sys32_sigaction(int sig, const struct old_sigaction32 __user *act,
226 struct old_sigaction32 __user *oact)
228 struct k_sigaction new_ka, old_ka;
229 unsigned long sa_handler, sa_restorer;
233 compat_old_sigset_t mask;
234 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
235 __get_user(sa_handler, &act->sa_handler) ||
236 __get_user(sa_restorer, &act->sa_restorer))
238 new_ka.sa.sa_handler = (__sighandler_t) sa_handler;
239 new_ka.sa.sa_restorer = (void (*)(void)) sa_restorer;
240 __get_user(new_ka.sa.sa_flags, &act->sa_flags);
241 __get_user(mask, &act->sa_mask);
242 siginitset(&new_ka.sa.sa_mask, mask);
245 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
248 sa_handler = (unsigned long) old_ka.sa.sa_handler;
249 sa_restorer = (unsigned long) old_ka.sa.sa_restorer;
250 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
251 __put_user(sa_handler, &oact->sa_handler) ||
252 __put_user(sa_restorer, &oact->sa_restorer))
254 __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
255 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask);
262 do_sigaction(int sig, const struct k_sigaction *act, struct k_sigaction *oact);
265 sys32_rt_sigaction(int sig, const struct sigaction32 __user *act,
266 struct sigaction32 __user *oact, size_t sigsetsize)
268 struct k_sigaction new_ka, old_ka;
269 unsigned long sa_handler;
271 compat_sigset_t set32;
273 /* XXX: Don't preclude handling different sized sigset_t's. */
274 if (sigsetsize != sizeof(compat_sigset_t))
278 ret = get_user(sa_handler, &act->sa_handler);
279 ret |= __copy_from_user(&set32, &act->sa_mask,
280 sizeof(compat_sigset_t));
281 switch (_NSIG_WORDS) {
282 case 4: new_ka.sa.sa_mask.sig[3] = set32.sig[6]
283 | (((long)set32.sig[7]) << 32);
284 case 3: new_ka.sa.sa_mask.sig[2] = set32.sig[4]
285 | (((long)set32.sig[5]) << 32);
286 case 2: new_ka.sa.sa_mask.sig[1] = set32.sig[2]
287 | (((long)set32.sig[3]) << 32);
288 case 1: new_ka.sa.sa_mask.sig[0] = set32.sig[0]
289 | (((long)set32.sig[1]) << 32);
291 ret |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
295 new_ka.sa.sa_handler = (__sighandler_t) sa_handler;
298 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
301 switch (_NSIG_WORDS) {
303 set32.sig[7] = (old_ka.sa.sa_mask.sig[3] >> 32);
304 set32.sig[6] = old_ka.sa.sa_mask.sig[3];
306 set32.sig[5] = (old_ka.sa.sa_mask.sig[2] >> 32);
307 set32.sig[4] = old_ka.sa.sa_mask.sig[2];
309 set32.sig[3] = (old_ka.sa.sa_mask.sig[1] >> 32);
310 set32.sig[2] = old_ka.sa.sa_mask.sig[1];
312 set32.sig[1] = (old_ka.sa.sa_mask.sig[0] >> 32);
313 set32.sig[0] = old_ka.sa.sa_mask.sig[0];
315 ret = put_user((unsigned long)old_ka.sa.sa_handler, &oact->sa_handler);
316 ret |= __copy_to_user(&oact->sa_mask, &set32,
317 sizeof(compat_sigset_t));
318 ret |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
325 sys32_sigaltstack(const stack_t32 __user *uss, stack_t32 __user *uoss,
326 struct pt_regs *regs)
331 mm_segment_t old_fs = get_fs();
334 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
336 err |= __get_user(ss_sp, &uss->ss_sp);
337 err |= __get_user(kss.ss_size, &uss->ss_size);
338 err |= __get_user(kss.ss_flags, &uss->ss_flags);
341 kss.ss_sp = (void *) ss_sp;
345 ret = do_sigaltstack((stack_t __user *) (uss ? &kss : NULL),
346 (stack_t __user *) (uoss ? &koss : NULL),
351 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
353 ss_sp = (unsigned long) koss.ss_sp;
354 err |= __put_user(ss_sp, &uoss->ss_sp);
355 err |= __put_user(koss.ss_size, &uoss->ss_size);
356 err |= __put_user(koss.ss_flags, &uoss->ss_flags);
363 static int save_sigregs32(struct pt_regs *regs, _sigregs32 __user *sregs)
365 _s390_regs_common32 regs32;
368 regs32.psw.mask = PSW32_MASK_MERGE(PSW32_USER_BITS,
369 (__u32)(regs->psw.mask >> 32));
370 regs32.psw.addr = PSW32_ADDR_AMODE31 | (__u32) regs->psw.addr;
371 for (i = 0; i < NUM_GPRS; i++)
372 regs32.gprs[i] = (__u32) regs->gprs[i];
373 save_access_regs(current->thread.acrs);
374 memcpy(regs32.acrs, current->thread.acrs, sizeof(regs32.acrs));
375 err = __copy_to_user(&sregs->regs, ®s32, sizeof(regs32));
378 save_fp_regs(¤t->thread.fp_regs);
379 /* s390_fp_regs and _s390_fp_regs32 are the same ! */
380 return __copy_to_user(&sregs->fpregs, ¤t->thread.fp_regs,
381 sizeof(_s390_fp_regs32));
384 static int restore_sigregs32(struct pt_regs *regs,_sigregs32 __user *sregs)
386 _s390_regs_common32 regs32;
389 /* Alwys make any pending restarted system call return -EINTR */
390 current_thread_info()->restart_block.fn = do_no_restart_syscall;
392 err = __copy_from_user(®s32, &sregs->regs, sizeof(regs32));
395 regs->psw.mask = PSW_MASK_MERGE(regs->psw.mask,
396 (__u64)regs32.psw.mask << 32);
397 regs->psw.addr = (__u64)(regs32.psw.addr & PSW32_ADDR_INSN);
398 for (i = 0; i < NUM_GPRS; i++)
399 regs->gprs[i] = (__u64) regs32.gprs[i];
400 memcpy(current->thread.acrs, regs32.acrs, sizeof(current->thread.acrs));
401 restore_access_regs(current->thread.acrs);
403 err = __copy_from_user(¤t->thread.fp_regs, &sregs->fpregs,
404 sizeof(_s390_fp_regs32));
405 current->thread.fp_regs.fpc &= FPC_VALID_MASK;
409 restore_fp_regs(¤t->thread.fp_regs);
410 regs->trap = -1; /* disable syscall checks */
414 asmlinkage long sys32_sigreturn(struct pt_regs *regs)
416 sigframe32 __user *frame = (sigframe32 __user *)regs->gprs[15];
419 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
421 if (__copy_from_user(&set.sig, &frame->sc.oldmask, _SIGMASK_COPY_SIZE32))
424 sigdelsetmask(&set, ~_BLOCKABLE);
425 spin_lock_irq(¤t->sighand->siglock);
426 current->blocked = set;
428 spin_unlock_irq(¤t->sighand->siglock);
430 if (restore_sigregs32(regs, &frame->sregs))
433 return regs->gprs[2];
436 force_sig(SIGSEGV, current);
440 asmlinkage long sys32_rt_sigreturn(struct pt_regs *regs)
442 rt_sigframe32 __user *frame = (rt_sigframe32 __user *)regs->gprs[15];
447 mm_segment_t old_fs = get_fs();
449 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
451 if (__copy_from_user(&set, &frame->uc.uc_sigmask, sizeof(set)))
454 sigdelsetmask(&set, ~_BLOCKABLE);
455 spin_lock_irq(¤t->sighand->siglock);
456 current->blocked = set;
458 spin_unlock_irq(¤t->sighand->siglock);
460 if (restore_sigregs32(regs, &frame->uc.uc_mcontext))
463 err = __get_user(ss_sp, &frame->uc.uc_stack.ss_sp);
464 st.ss_sp = (void *) A((unsigned long)ss_sp);
465 err |= __get_user(st.ss_size, &frame->uc.uc_stack.ss_size);
466 err |= __get_user(st.ss_flags, &frame->uc.uc_stack.ss_flags);
470 /* It is more difficult to avoid calling this function than to
471 call it and ignore errors. */
473 do_sigaltstack((stack_t __user *)&st, NULL, regs->gprs[15]);
476 return regs->gprs[2];
479 force_sig(SIGSEGV, current);
484 * Set up a signal frame.
489 * Determine which stack to use..
491 static inline void __user *
492 get_sigframe(struct k_sigaction *ka, struct pt_regs * regs, size_t frame_size)
496 /* Default to using normal stack */
497 sp = (unsigned long) A(regs->gprs[15]);
499 /* This is the X/Open sanctioned signal stack switching. */
500 if (ka->sa.sa_flags & SA_ONSTACK) {
501 if (! on_sig_stack(sp))
502 sp = current->sas_ss_sp + current->sas_ss_size;
505 /* This is the legacy signal stack switching. */
506 else if (!user_mode(regs) &&
507 !(ka->sa.sa_flags & SA_RESTORER) &&
508 ka->sa.sa_restorer) {
509 sp = (unsigned long) ka->sa.sa_restorer;
512 return (void __user *)((sp - frame_size) & -8ul);
515 static inline int map_signal(int sig)
517 if (current_thread_info()->exec_domain
518 && current_thread_info()->exec_domain->signal_invmap
520 return current_thread_info()->exec_domain->signal_invmap[sig];
525 static void setup_frame32(int sig, struct k_sigaction *ka,
526 sigset_t *set, struct pt_regs * regs)
528 sigframe32 __user *frame = get_sigframe(ka, regs, sizeof(sigframe32));
529 if (!access_ok(VERIFY_WRITE, frame, sizeof(sigframe32)))
532 if (__copy_to_user(&frame->sc.oldmask, &set->sig, _SIGMASK_COPY_SIZE32))
535 if (save_sigregs32(regs, &frame->sregs))
537 if (__put_user((unsigned long) &frame->sregs, &frame->sc.sregs))
540 /* Set up to return from userspace. If provided, use a stub
541 already in userspace. */
542 if (ka->sa.sa_flags & SA_RESTORER) {
543 regs->gprs[14] = (__u64) ka->sa.sa_restorer;
545 regs->gprs[14] = (__u64) frame->retcode;
546 if (__put_user(S390_SYSCALL_OPCODE | __NR_sigreturn,
547 (u16 __user *)(frame->retcode)))
551 /* Set up backchain. */
552 if (__put_user(regs->gprs[15], (unsigned int __user *) frame))
555 /* Set up registers for signal handler */
556 regs->gprs[15] = (__u64) frame;
557 regs->psw.addr = (__u64) ka->sa.sa_handler;
559 regs->gprs[2] = map_signal(sig);
560 regs->gprs[3] = (__u64) &frame->sc;
562 /* We forgot to include these in the sigcontext.
563 To avoid breaking binary compatibility, they are passed as args. */
564 regs->gprs[4] = current->thread.trap_no;
565 regs->gprs[5] = current->thread.prot_addr;
567 /* Place signal number on stack to allow backtrace from handler. */
568 if (__put_user(regs->gprs[2], (int __user *) &frame->signo))
573 force_sigsegv(sig, current);
576 static void setup_rt_frame32(int sig, struct k_sigaction *ka, siginfo_t *info,
577 sigset_t *set, struct pt_regs * regs)
580 rt_sigframe32 __user *frame = get_sigframe(ka, regs, sizeof(rt_sigframe32));
581 if (!access_ok(VERIFY_WRITE, frame, sizeof(rt_sigframe32)))
584 if (copy_siginfo_to_user32(&frame->info, info))
587 /* Create the ucontext. */
588 err |= __put_user(0, &frame->uc.uc_flags);
589 err |= __put_user(0, &frame->uc.uc_link);
590 err |= __put_user(current->sas_ss_sp, &frame->uc.uc_stack.ss_sp);
591 err |= __put_user(sas_ss_flags(regs->gprs[15]),
592 &frame->uc.uc_stack.ss_flags);
593 err |= __put_user(current->sas_ss_size, &frame->uc.uc_stack.ss_size);
594 err |= save_sigregs32(regs, &frame->uc.uc_mcontext);
595 err |= __copy_to_user(&frame->uc.uc_sigmask, set, sizeof(*set));
599 /* Set up to return from userspace. If provided, use a stub
600 already in userspace. */
601 if (ka->sa.sa_flags & SA_RESTORER) {
602 regs->gprs[14] = (__u64) ka->sa.sa_restorer;
604 regs->gprs[14] = (__u64) frame->retcode;
605 err |= __put_user(S390_SYSCALL_OPCODE | __NR_rt_sigreturn,
606 (u16 __user *)(frame->retcode));
609 /* Set up backchain. */
610 if (__put_user(regs->gprs[15], (unsigned int __user *) frame))
613 /* Set up registers for signal handler */
614 regs->gprs[15] = (__u64) frame;
615 regs->psw.addr = (__u64) ka->sa.sa_handler;
617 regs->gprs[2] = map_signal(sig);
618 regs->gprs[3] = (__u64) &frame->info;
619 regs->gprs[4] = (__u64) &frame->uc;
623 force_sigsegv(sig, current);
627 * OK, we're invoking a handler
631 handle_signal32(unsigned long sig, struct k_sigaction *ka,
632 siginfo_t *info, sigset_t *oldset, struct pt_regs * regs)
634 /* Set up the stack frame */
635 if (ka->sa.sa_flags & SA_SIGINFO)
636 setup_rt_frame32(sig, ka, info, oldset, regs);
638 setup_frame32(sig, ka, oldset, regs);
640 if (!(ka->sa.sa_flags & SA_NODEFER)) {
641 spin_lock_irq(¤t->sighand->siglock);
642 sigorsets(¤t->blocked,¤t->blocked,&ka->sa.sa_mask);
643 sigaddset(¤t->blocked,sig);
645 spin_unlock_irq(¤t->sighand->siglock);