2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Copyright (C) 1994 - 2000, 2006 Ralf Baechle
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
10 #include <linux/cache.h>
11 #include <linux/compat.h>
12 #include <linux/sched.h>
14 #include <linux/smp.h>
15 #include <linux/kernel.h>
16 #include <linux/signal.h>
17 #include <linux/syscalls.h>
18 #include <linux/errno.h>
19 #include <linux/wait.h>
20 #include <linux/ptrace.h>
21 #include <linux/compat.h>
22 #include <linux/suspend.h>
23 #include <linux/compiler.h>
24 #include <linux/uaccess.h>
28 #include <asm/compat-signal.h>
29 #include <linux/bitops.h>
30 #include <asm/cacheflush.h>
32 #include <asm/ucontext.h>
33 #include <asm/system.h>
37 #include "signal-common.h"
39 #define SI_PAD_SIZE32 ((SI_MAX_SIZE/sizeof(int)) - 3)
41 typedef struct compat_siginfo {
47 int _pad[SI_PAD_SIZE32];
51 compat_pid_t _pid; /* sender's pid */
52 compat_uid_t _uid; /* sender's uid */
57 compat_pid_t _pid; /* which child */
58 compat_uid_t _uid; /* sender's uid */
59 int _status; /* exit code */
60 compat_clock_t _utime;
61 compat_clock_t _stime;
66 compat_pid_t _pid; /* which child */
67 compat_clock_t _utime;
68 int _status; /* exit code */
69 compat_clock_t _stime;
72 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
74 s32 _addr; /* faulting insn/memory ref. */
77 /* SIGPOLL, SIGXFSZ (To do ...) */
79 int _band; /* POLL_IN, POLL_OUT, POLL_MSG */
85 timer_t _tid; /* timer id */
86 int _overrun; /* overrun count */
87 compat_sigval_t _sigval;/* same as below */
88 int _sys_private; /* not to be passed to user */
91 /* POSIX.1b signals */
93 compat_pid_t _pid; /* sender's pid */
94 compat_uid_t _uid; /* sender's uid */
95 compat_sigval_t _sigval;
102 * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
104 #define __NR_O32_sigreturn 4119
105 #define __NR_O32_rt_sigreturn 4193
106 #define __NR_O32_restart_syscall 4253
108 /* 32-bit compatibility types */
110 typedef unsigned int __sighandler32_t;
111 typedef void (*vfptr_t)(void);
114 unsigned int sa_flags;
115 __sighandler32_t sa_handler;
116 compat_sigset_t sa_mask;
119 /* IRIX compatible stack_t */
120 typedef struct sigaltstack32 {
122 compat_size_t ss_size;
130 struct sigcontext32 uc_mcontext;
131 compat_sigset_t uc_sigmask; /* mask last for extensibility */
135 * Horribly complicated - with the bloody RM9000 workarounds enabled
136 * the signal trampolines is moving to the end of the structure so we can
137 * increase the alignment without breaking software compatibility.
139 #if ICACHE_REFILLS_WORKAROUND_WAR == 0
142 u32 sf_ass[4]; /* argument save space for o32 */
143 u32 sf_code[2]; /* signal trampoline */
144 struct sigcontext32 sf_sc;
145 compat_sigset_t sf_mask;
148 struct rt_sigframe32 {
149 u32 rs_ass[4]; /* argument save space for o32 */
150 u32 rs_code[2]; /* signal trampoline */
151 compat_siginfo_t rs_info;
152 struct ucontext32 rs_uc;
155 #else /* ICACHE_REFILLS_WORKAROUND_WAR */
158 u32 sf_ass[4]; /* argument save space for o32 */
160 struct sigcontext32 sf_sc; /* hw context */
161 compat_sigset_t sf_mask;
162 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
165 struct rt_sigframe32 {
166 u32 rs_ass[4]; /* argument save space for o32 */
168 compat_siginfo_t rs_info;
169 struct ucontext32 rs_uc;
170 u32 rs_code[8] __attribute__((aligned(32))); /* signal trampoline */
173 #endif /* !ICACHE_REFILLS_WORKAROUND_WAR */
176 * sigcontext handlers
178 static int protected_save_fp_context32(struct sigcontext32 __user *sc)
184 err = save_fp_context32(sc); /* this might fail */
188 /* touch the sigcontext and try again */
189 err = __put_user(0, &sc->sc_fpregs[0]) |
190 __put_user(0, &sc->sc_fpregs[31]) |
191 __put_user(0, &sc->sc_fpc_csr);
193 break; /* really bad sigcontext */
198 static int protected_restore_fp_context32(struct sigcontext32 __user *sc)
204 err = restore_fp_context32(sc); /* this might fail */
208 /* touch the sigcontext and try again */
209 err = __get_user(tmp, &sc->sc_fpregs[0]) |
210 __get_user(tmp, &sc->sc_fpregs[31]) |
211 __get_user(tmp, &sc->sc_fpc_csr);
213 break; /* really bad sigcontext */
218 static int setup_sigcontext32(struct pt_regs *regs,
219 struct sigcontext32 __user *sc)
225 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
227 err |= __put_user(0, &sc->sc_regs[0]);
228 for (i = 1; i < 32; i++)
229 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
231 err |= __put_user(regs->hi, &sc->sc_mdhi);
232 err |= __put_user(regs->lo, &sc->sc_mdlo);
234 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
235 err |= __put_user(mfhi1(), &sc->sc_hi1);
236 err |= __put_user(mflo1(), &sc->sc_lo1);
237 err |= __put_user(mfhi2(), &sc->sc_hi2);
238 err |= __put_user(mflo2(), &sc->sc_lo2);
239 err |= __put_user(mfhi3(), &sc->sc_hi3);
240 err |= __put_user(mflo3(), &sc->sc_lo3);
243 used_math = !!used_math();
244 err |= __put_user(used_math, &sc->sc_used_math);
248 * Save FPU state to signal context. Signal handler
249 * will "inherit" current FPU state.
251 err |= protected_save_fp_context32(sc);
257 check_and_restore_fp_context32(struct sigcontext32 __user *sc)
261 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
264 err |= protected_restore_fp_context32(sc);
268 static int restore_sigcontext32(struct pt_regs *regs,
269 struct sigcontext32 __user *sc)
276 /* Always make any pending restarted system calls return -EINTR */
277 current_thread_info()->restart_block.fn = do_no_restart_syscall;
279 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
280 err |= __get_user(regs->hi, &sc->sc_mdhi);
281 err |= __get_user(regs->lo, &sc->sc_mdlo);
283 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
284 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
285 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
286 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
287 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
288 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
289 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
292 for (i = 1; i < 32; i++)
293 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
295 err |= __get_user(used_math, &sc->sc_used_math);
296 conditional_used_math(used_math);
299 /* restore fpu context if we have used it before */
301 err = check_and_restore_fp_context32(sc);
303 /* signal handler may have used FPU. Give it up. */
313 extern void __put_sigset_unknown_nsig(void);
314 extern void __get_sigset_unknown_nsig(void);
316 static inline int put_sigset(const sigset_t *kbuf, compat_sigset_t __user *ubuf)
320 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)))
323 switch (_NSIG_WORDS) {
325 __put_sigset_unknown_nsig();
327 err |= __put_user (kbuf->sig[1] >> 32, &ubuf->sig[3]);
328 err |= __put_user (kbuf->sig[1] & 0xffffffff, &ubuf->sig[2]);
330 err |= __put_user (kbuf->sig[0] >> 32, &ubuf->sig[1]);
331 err |= __put_user (kbuf->sig[0] & 0xffffffff, &ubuf->sig[0]);
337 static inline int get_sigset(sigset_t *kbuf, const compat_sigset_t __user *ubuf)
340 unsigned long sig[4];
342 if (!access_ok(VERIFY_READ, ubuf, sizeof(*ubuf)))
345 switch (_NSIG_WORDS) {
347 __get_sigset_unknown_nsig();
349 err |= __get_user (sig[3], &ubuf->sig[3]);
350 err |= __get_user (sig[2], &ubuf->sig[2]);
351 kbuf->sig[1] = sig[2] | (sig[3] << 32);
353 err |= __get_user (sig[1], &ubuf->sig[1]);
354 err |= __get_user (sig[0], &ubuf->sig[0]);
355 kbuf->sig[0] = sig[0] | (sig[1] << 32);
362 * Atomically swap in the new signal mask, and wait for a signal.
365 asmlinkage int sys32_sigsuspend(nabi_no_regargs struct pt_regs regs)
367 compat_sigset_t __user *uset;
370 uset = (compat_sigset_t __user *) regs.regs[4];
371 if (get_sigset(&newset, uset))
373 sigdelsetmask(&newset, ~_BLOCKABLE);
375 spin_lock_irq(¤t->sighand->siglock);
376 current->saved_sigmask = current->blocked;
377 current->blocked = newset;
379 spin_unlock_irq(¤t->sighand->siglock);
381 current->state = TASK_INTERRUPTIBLE;
383 set_thread_flag(TIF_RESTORE_SIGMASK);
384 return -ERESTARTNOHAND;
387 asmlinkage int sys32_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
389 compat_sigset_t __user *uset;
393 /* XXX Don't preclude handling different sized sigset_t's. */
394 sigsetsize = regs.regs[5];
395 if (sigsetsize != sizeof(compat_sigset_t))
398 uset = (compat_sigset_t __user *) regs.regs[4];
399 if (get_sigset(&newset, uset))
401 sigdelsetmask(&newset, ~_BLOCKABLE);
403 spin_lock_irq(¤t->sighand->siglock);
404 current->saved_sigmask = current->blocked;
405 current->blocked = newset;
407 spin_unlock_irq(¤t->sighand->siglock);
409 current->state = TASK_INTERRUPTIBLE;
411 set_thread_flag(TIF_RESTORE_SIGMASK);
412 return -ERESTARTNOHAND;
415 asmlinkage int sys32_sigaction(int sig, const struct sigaction32 __user *act,
416 struct sigaction32 __user *oact)
418 struct k_sigaction new_ka, old_ka;
426 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
428 err |= __get_user(handler, &act->sa_handler);
429 new_ka.sa.sa_handler = (void __user *)(s64)handler;
430 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
431 err |= __get_user(mask, &act->sa_mask.sig[0]);
435 siginitset(&new_ka.sa.sa_mask, mask);
438 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
441 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
443 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
444 err |= __put_user((u32)(u64)old_ka.sa.sa_handler,
446 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
447 err |= __put_user(0, &oact->sa_mask.sig[1]);
448 err |= __put_user(0, &oact->sa_mask.sig[2]);
449 err |= __put_user(0, &oact->sa_mask.sig[3]);
457 asmlinkage int sys32_sigaltstack(nabi_no_regargs struct pt_regs regs)
459 const stack32_t __user *uss = (const stack32_t __user *) regs.regs[4];
460 stack32_t __user *uoss = (stack32_t __user *) regs.regs[5];
461 unsigned long usp = regs.regs[29];
464 mm_segment_t old_fs = get_fs();
468 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
470 err |= __get_user(sp, &uss->ss_sp);
471 kss.ss_sp = (void __user *) (long) sp;
472 err |= __get_user(kss.ss_size, &uss->ss_size);
473 err |= __get_user(kss.ss_flags, &uss->ss_flags);
479 ret = do_sigaltstack(uss ? (stack_t __user *)&kss : NULL,
480 uoss ? (stack_t __user *)&koss : NULL, usp);
484 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
486 sp = (int) (unsigned long) koss.ss_sp;
487 err |= __put_user(sp, &uoss->ss_sp);
488 err |= __put_user(koss.ss_size, &uoss->ss_size);
489 err |= __put_user(koss.ss_flags, &uoss->ss_flags);
496 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
500 if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
503 /* If you change siginfo_t structure, please be sure
504 this code is fixed accordingly.
505 It should never copy any pad contained in the structure
506 to avoid security leaks, but must copy the generic
507 3 ints plus the relevant union member.
508 This routine must convert siginfo from 64bit to 32bit as well
510 err = __put_user(from->si_signo, &to->si_signo);
511 err |= __put_user(from->si_errno, &to->si_errno);
512 err |= __put_user((short)from->si_code, &to->si_code);
513 if (from->si_code < 0)
514 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
516 switch (from->si_code >> 16) {
517 case __SI_TIMER >> 16:
518 err |= __put_user(from->si_tid, &to->si_tid);
519 err |= __put_user(from->si_overrun, &to->si_overrun);
520 err |= __put_user(from->si_int, &to->si_int);
522 case __SI_CHLD >> 16:
523 err |= __put_user(from->si_utime, &to->si_utime);
524 err |= __put_user(from->si_stime, &to->si_stime);
525 err |= __put_user(from->si_status, &to->si_status);
527 err |= __put_user(from->si_pid, &to->si_pid);
528 err |= __put_user(from->si_uid, &to->si_uid);
530 case __SI_FAULT >> 16:
531 err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
533 case __SI_POLL >> 16:
534 err |= __put_user(from->si_band, &to->si_band);
535 err |= __put_user(from->si_fd, &to->si_fd);
537 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
538 case __SI_MESGQ >> 16:
539 err |= __put_user(from->si_pid, &to->si_pid);
540 err |= __put_user(from->si_uid, &to->si_uid);
541 err |= __put_user(from->si_int, &to->si_int);
548 asmlinkage void sys32_sigreturn(nabi_no_regargs struct pt_regs regs)
550 struct sigframe32 __user *frame;
554 frame = (struct sigframe32 __user *) regs.regs[29];
555 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
557 if (__copy_conv_sigset_from_user(&blocked, &frame->sf_mask))
560 sigdelsetmask(&blocked, ~_BLOCKABLE);
561 spin_lock_irq(¤t->sighand->siglock);
562 current->blocked = blocked;
564 spin_unlock_irq(¤t->sighand->siglock);
566 sig = restore_sigcontext32(®s, &frame->sf_sc);
570 force_sig(sig, current);
573 * Don't let your children do this ...
575 __asm__ __volatile__(
583 force_sig(SIGSEGV, current);
586 asmlinkage void sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
588 struct rt_sigframe32 __user *frame;
595 frame = (struct rt_sigframe32 __user *) regs.regs[29];
596 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
598 if (__copy_conv_sigset_from_user(&set, &frame->rs_uc.uc_sigmask))
601 sigdelsetmask(&set, ~_BLOCKABLE);
602 spin_lock_irq(¤t->sighand->siglock);
603 current->blocked = set;
605 spin_unlock_irq(¤t->sighand->siglock);
607 sig = restore_sigcontext32(®s, &frame->rs_uc.uc_mcontext);
611 force_sig(sig, current);
613 /* The ucontext contains a stack32_t, so we must convert! */
614 if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp))
616 st.ss_sp = (void __user *)(long) sp;
617 if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size))
619 if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags))
622 /* It is more difficult to avoid calling this function than to
623 call it and ignore errors. */
626 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
630 * Don't let your children do this ...
632 __asm__ __volatile__(
640 force_sig(SIGSEGV, current);
643 static int setup_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
644 int signr, sigset_t *set)
646 struct sigframe32 __user *frame;
649 frame = get_sigframe(ka, regs, sizeof(*frame));
650 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
653 err |= install_sigtramp(frame->sf_code, __NR_O32_sigreturn);
655 err |= setup_sigcontext32(regs, &frame->sf_sc);
656 err |= __copy_conv_sigset_to_user(&frame->sf_mask, set);
662 * Arguments to signal handler:
665 * a1 = 0 (should be cause)
666 * a2 = pointer to struct sigcontext
668 * $25 and c0_epc point to the signal handler, $29 points to the
671 regs->regs[ 4] = signr;
673 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
674 regs->regs[29] = (unsigned long) frame;
675 regs->regs[31] = (unsigned long) frame->sf_code;
676 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
678 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
679 current->comm, current->pid,
680 frame, regs->cp0_epc, regs->regs[31]);
685 force_sigsegv(signr, current);
689 static int setup_rt_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
690 int signr, sigset_t *set, siginfo_t *info)
692 struct rt_sigframe32 __user *frame;
696 frame = get_sigframe(ka, regs, sizeof(*frame));
697 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
700 err |= install_sigtramp(frame->rs_code, __NR_O32_rt_sigreturn);
702 /* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
703 err |= copy_siginfo_to_user32(&frame->rs_info, info);
705 /* Create the ucontext. */
706 err |= __put_user(0, &frame->rs_uc.uc_flags);
707 err |= __put_user(0, &frame->rs_uc.uc_link);
708 sp = (int) (long) current->sas_ss_sp;
709 err |= __put_user(sp,
710 &frame->rs_uc.uc_stack.ss_sp);
711 err |= __put_user(sas_ss_flags(regs->regs[29]),
712 &frame->rs_uc.uc_stack.ss_flags);
713 err |= __put_user(current->sas_ss_size,
714 &frame->rs_uc.uc_stack.ss_size);
715 err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
716 err |= __copy_conv_sigset_to_user(&frame->rs_uc.uc_sigmask, set);
722 * Arguments to signal handler:
725 * a1 = 0 (should be cause)
726 * a2 = pointer to ucontext
728 * $25 and c0_epc point to the signal handler, $29 points to
729 * the struct rt_sigframe32.
731 regs->regs[ 4] = signr;
732 regs->regs[ 5] = (unsigned long) &frame->rs_info;
733 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
734 regs->regs[29] = (unsigned long) frame;
735 regs->regs[31] = (unsigned long) frame->rs_code;
736 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
738 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
739 current->comm, current->pid,
740 frame, regs->cp0_epc, regs->regs[31]);
745 force_sigsegv(signr, current);
750 * o32 compatibility on 64-bit kernels, without DSP ASE
752 struct mips_abi mips_abi_32 = {
753 .setup_frame = setup_frame_32,
754 .setup_rt_frame = setup_rt_frame_32,
755 .restart = __NR_O32_restart_syscall
758 asmlinkage int sys32_rt_sigaction(int sig, const struct sigaction32 __user *act,
759 struct sigaction32 __user *oact,
760 unsigned int sigsetsize)
762 struct k_sigaction new_sa, old_sa;
765 /* XXX: Don't preclude handling different sized sigset_t's. */
766 if (sigsetsize != sizeof(sigset_t))
773 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
775 err |= __get_user(handler, &act->sa_handler);
776 new_sa.sa.sa_handler = (void __user *)(s64)handler;
777 err |= __get_user(new_sa.sa.sa_flags, &act->sa_flags);
778 err |= get_sigset(&new_sa.sa.sa_mask, &act->sa_mask);
783 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
788 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
791 err |= __put_user((u32)(u64)old_sa.sa.sa_handler,
793 err |= __put_user(old_sa.sa.sa_flags, &oact->sa_flags);
794 err |= put_sigset(&old_sa.sa.sa_mask, &oact->sa_mask);
802 asmlinkage int sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
803 compat_sigset_t __user *oset, unsigned int sigsetsize)
805 sigset_t old_set, new_set;
807 mm_segment_t old_fs = get_fs();
809 if (set && get_sigset(&new_set, set))
813 ret = sys_rt_sigprocmask(how, set ? (sigset_t __user *)&new_set : NULL,
814 oset ? (sigset_t __user *)&old_set : NULL,
818 if (!ret && oset && put_sigset(&old_set, oset))
824 asmlinkage int sys32_rt_sigpending(compat_sigset_t __user *uset,
825 unsigned int sigsetsize)
829 mm_segment_t old_fs = get_fs();
832 ret = sys_rt_sigpending((sigset_t __user *)&set, sigsetsize);
835 if (!ret && put_sigset(&set, uset))
841 asmlinkage int sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t __user *uinfo)
845 mm_segment_t old_fs = get_fs();
847 if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
848 copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE))
851 ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *)&info);
857 sys32_waitid(int which, compat_pid_t pid,
858 compat_siginfo_t __user *uinfo, int options,
859 struct compat_rusage __user *uru)
864 mm_segment_t old_fs = get_fs();
868 ret = sys_waitid(which, pid, (siginfo_t __user *) &info, options,
869 uru ? (struct rusage __user *) &ru : NULL);
872 if (ret < 0 || info.si_signo == 0)
875 if (uru && (ret = put_compat_rusage(&ru, uru)))
878 BUG_ON(info.si_code & __SI_MASK);
879 info.si_code |= __SI_CHLD;
880 return copy_siginfo_to_user32(uinfo, &info);