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/smp_lock.h>
16 #include <linux/kernel.h>
17 #include <linux/signal.h>
18 #include <linux/syscalls.h>
19 #include <linux/errno.h>
20 #include <linux/wait.h>
21 #include <linux/ptrace.h>
22 #include <linux/compat.h>
23 #include <linux/suspend.h>
24 #include <linux/compiler.h>
28 #include <asm/compat-signal.h>
29 #include <linux/bitops.h>
30 #include <asm/cacheflush.h>
32 #include <asm/uaccess.h>
33 #include <asm/ucontext.h>
34 #include <asm/system.h>
38 #include "signal-common.h"
40 #define SI_PAD_SIZE32 ((SI_MAX_SIZE/sizeof(int)) - 3)
42 typedef struct compat_siginfo {
48 int _pad[SI_PAD_SIZE32];
52 compat_pid_t _pid; /* sender's pid */
53 compat_uid_t _uid; /* sender's uid */
58 compat_pid_t _pid; /* which child */
59 compat_uid_t _uid; /* sender's uid */
60 int _status; /* exit code */
61 compat_clock_t _utime;
62 compat_clock_t _stime;
67 compat_pid_t _pid; /* which child */
68 compat_clock_t _utime;
69 int _status; /* exit code */
70 compat_clock_t _stime;
73 /* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
75 s32 _addr; /* faulting insn/memory ref. */
78 /* SIGPOLL, SIGXFSZ (To do ...) */
80 int _band; /* POLL_IN, POLL_OUT, POLL_MSG */
86 timer_t _tid; /* timer id */
87 int _overrun; /* overrun count */
88 compat_sigval_t _sigval;/* same as below */
89 int _sys_private; /* not to be passed to user */
92 /* POSIX.1b signals */
94 compat_pid_t _pid; /* sender's pid */
95 compat_uid_t _uid; /* sender's uid */
96 compat_sigval_t _sigval;
103 * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
105 #define __NR_O32_sigreturn 4119
106 #define __NR_O32_rt_sigreturn 4193
107 #define __NR_O32_restart_syscall 4253
109 /* 32-bit compatibility types */
111 typedef unsigned int __sighandler32_t;
112 typedef void (*vfptr_t)(void);
115 unsigned int sa_flags;
116 __sighandler32_t sa_handler;
117 compat_sigset_t sa_mask;
120 /* IRIX compatible stack_t */
121 typedef struct sigaltstack32 {
123 compat_size_t ss_size;
131 struct sigcontext32 uc_mcontext;
132 compat_sigset_t uc_sigmask; /* mask last for extensibility */
136 * Horribly complicated - with the bloody RM9000 workarounds enabled
137 * the signal trampolines is moving to the end of the structure so we can
138 * increase the alignment without breaking software compatibility.
140 #if ICACHE_REFILLS_WORKAROUND_WAR == 0
143 u32 sf_ass[4]; /* argument save space for o32 */
144 u32 sf_code[2]; /* signal trampoline */
145 struct sigcontext32 sf_sc;
146 compat_sigset_t sf_mask;
149 struct rt_sigframe32 {
150 u32 rs_ass[4]; /* argument save space for o32 */
151 u32 rs_code[2]; /* signal trampoline */
152 compat_siginfo_t rs_info;
153 struct ucontext32 rs_uc;
156 #else /* ICACHE_REFILLS_WORKAROUND_WAR */
159 u32 sf_ass[4]; /* argument save space for o32 */
161 struct sigcontext32 sf_sc; /* hw context */
162 compat_sigset_t sf_mask;
163 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
166 struct rt_sigframe32 {
167 u32 rs_ass[4]; /* argument save space for o32 */
169 compat_siginfo_t rs_info;
170 struct ucontext32 rs_uc;
171 u32 rs_code[8] __attribute__((aligned(32))); /* signal trampoline */
174 #endif /* !ICACHE_REFILLS_WORKAROUND_WAR */
177 * sigcontext handlers
179 static int setup_sigcontext32(struct pt_regs *regs,
180 struct sigcontext32 __user *sc)
185 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
187 err |= __put_user(0, &sc->sc_regs[0]);
188 for (i = 1; i < 32; i++)
189 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
191 err |= __put_user(regs->hi, &sc->sc_mdhi);
192 err |= __put_user(regs->lo, &sc->sc_mdlo);
194 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
195 err |= __put_user(mfhi1(), &sc->sc_hi1);
196 err |= __put_user(mflo1(), &sc->sc_lo1);
197 err |= __put_user(mfhi2(), &sc->sc_hi2);
198 err |= __put_user(mflo2(), &sc->sc_lo2);
199 err |= __put_user(mfhi3(), &sc->sc_hi3);
200 err |= __put_user(mflo3(), &sc->sc_lo3);
203 err |= __put_user(!!used_math(), &sc->sc_used_math);
207 * Save FPU state to signal context. Signal handler
208 * will "inherit" current FPU state.
212 if (!is_fpu_owner()) {
216 err |= save_fp_context32(sc);
224 check_and_restore_fp_context32(struct sigcontext32 __user *sc)
228 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
231 err |= restore_fp_context32(sc);
235 static int restore_sigcontext32(struct pt_regs *regs,
236 struct sigcontext32 __user *sc)
243 /* Always make any pending restarted system calls return -EINTR */
244 current_thread_info()->restart_block.fn = do_no_restart_syscall;
246 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
247 err |= __get_user(regs->hi, &sc->sc_mdhi);
248 err |= __get_user(regs->lo, &sc->sc_mdlo);
250 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
251 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
252 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
253 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
254 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
255 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
256 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
259 for (i = 1; i < 32; i++)
260 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
262 err |= __get_user(used_math, &sc->sc_used_math);
263 conditional_used_math(used_math);
268 /* restore fpu context if we have used it before */
271 err = check_and_restore_fp_context32(sc);
273 /* signal handler may have used FPU. Give it up. */
285 extern void __put_sigset_unknown_nsig(void);
286 extern void __get_sigset_unknown_nsig(void);
288 static inline int put_sigset(const sigset_t *kbuf, compat_sigset_t __user *ubuf)
292 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)))
295 switch (_NSIG_WORDS) {
297 __put_sigset_unknown_nsig();
299 err |= __put_user (kbuf->sig[1] >> 32, &ubuf->sig[3]);
300 err |= __put_user (kbuf->sig[1] & 0xffffffff, &ubuf->sig[2]);
302 err |= __put_user (kbuf->sig[0] >> 32, &ubuf->sig[1]);
303 err |= __put_user (kbuf->sig[0] & 0xffffffff, &ubuf->sig[0]);
309 static inline int get_sigset(sigset_t *kbuf, const compat_sigset_t __user *ubuf)
312 unsigned long sig[4];
314 if (!access_ok(VERIFY_READ, ubuf, sizeof(*ubuf)))
317 switch (_NSIG_WORDS) {
319 __get_sigset_unknown_nsig();
321 err |= __get_user (sig[3], &ubuf->sig[3]);
322 err |= __get_user (sig[2], &ubuf->sig[2]);
323 kbuf->sig[1] = sig[2] | (sig[3] << 32);
325 err |= __get_user (sig[1], &ubuf->sig[1]);
326 err |= __get_user (sig[0], &ubuf->sig[0]);
327 kbuf->sig[0] = sig[0] | (sig[1] << 32);
334 * Atomically swap in the new signal mask, and wait for a signal.
337 asmlinkage int sys32_sigsuspend(nabi_no_regargs struct pt_regs regs)
339 compat_sigset_t __user *uset;
342 uset = (compat_sigset_t __user *) regs.regs[4];
343 if (get_sigset(&newset, uset))
345 sigdelsetmask(&newset, ~_BLOCKABLE);
347 spin_lock_irq(¤t->sighand->siglock);
348 current->saved_sigmask = current->blocked;
349 current->blocked = newset;
351 spin_unlock_irq(¤t->sighand->siglock);
353 current->state = TASK_INTERRUPTIBLE;
355 set_thread_flag(TIF_RESTORE_SIGMASK);
356 return -ERESTARTNOHAND;
359 asmlinkage int sys32_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
361 compat_sigset_t __user *uset;
365 /* XXX Don't preclude handling different sized sigset_t's. */
366 sigsetsize = regs.regs[5];
367 if (sigsetsize != sizeof(compat_sigset_t))
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_sigaction(int sig, const struct sigaction32 __user *act,
388 struct sigaction32 __user *oact)
390 struct k_sigaction new_ka, old_ka;
398 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
400 err |= __get_user(handler, &act->sa_handler);
401 new_ka.sa.sa_handler = (void __user *)(s64)handler;
402 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
403 err |= __get_user(mask, &act->sa_mask.sig[0]);
407 siginitset(&new_ka.sa.sa_mask, mask);
410 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
413 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
415 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
416 err |= __put_user((u32)(u64)old_ka.sa.sa_handler,
418 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
419 err |= __put_user(0, &oact->sa_mask.sig[1]);
420 err |= __put_user(0, &oact->sa_mask.sig[2]);
421 err |= __put_user(0, &oact->sa_mask.sig[3]);
429 asmlinkage int sys32_sigaltstack(nabi_no_regargs struct pt_regs regs)
431 const stack32_t __user *uss = (const stack32_t __user *) regs.regs[4];
432 stack32_t __user *uoss = (stack32_t __user *) regs.regs[5];
433 unsigned long usp = regs.regs[29];
436 mm_segment_t old_fs = get_fs();
440 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
442 err |= __get_user(sp, &uss->ss_sp);
443 kss.ss_sp = (void __user *) (long) sp;
444 err |= __get_user(kss.ss_size, &uss->ss_size);
445 err |= __get_user(kss.ss_flags, &uss->ss_flags);
451 ret = do_sigaltstack(uss ? (stack_t __user *)&kss : NULL,
452 uoss ? (stack_t __user *)&koss : NULL, usp);
456 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
458 sp = (int) (unsigned long) koss.ss_sp;
459 err |= __put_user(sp, &uoss->ss_sp);
460 err |= __put_user(koss.ss_size, &uoss->ss_size);
461 err |= __put_user(koss.ss_flags, &uoss->ss_flags);
468 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
472 if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
475 /* If you change siginfo_t structure, please be sure
476 this code is fixed accordingly.
477 It should never copy any pad contained in the structure
478 to avoid security leaks, but must copy the generic
479 3 ints plus the relevant union member.
480 This routine must convert siginfo from 64bit to 32bit as well
482 err = __put_user(from->si_signo, &to->si_signo);
483 err |= __put_user(from->si_errno, &to->si_errno);
484 err |= __put_user((short)from->si_code, &to->si_code);
485 if (from->si_code < 0)
486 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
488 switch (from->si_code >> 16) {
489 case __SI_TIMER >> 16:
490 err |= __put_user(from->si_tid, &to->si_tid);
491 err |= __put_user(from->si_overrun, &to->si_overrun);
492 err |= __put_user(from->si_int, &to->si_int);
494 case __SI_CHLD >> 16:
495 err |= __put_user(from->si_utime, &to->si_utime);
496 err |= __put_user(from->si_stime, &to->si_stime);
497 err |= __put_user(from->si_status, &to->si_status);
499 err |= __put_user(from->si_pid, &to->si_pid);
500 err |= __put_user(from->si_uid, &to->si_uid);
502 case __SI_FAULT >> 16:
503 err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
505 case __SI_POLL >> 16:
506 err |= __put_user(from->si_band, &to->si_band);
507 err |= __put_user(from->si_fd, &to->si_fd);
509 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
510 case __SI_MESGQ >> 16:
511 err |= __put_user(from->si_pid, &to->si_pid);
512 err |= __put_user(from->si_uid, &to->si_uid);
513 err |= __put_user(from->si_int, &to->si_int);
520 asmlinkage void sys32_sigreturn(nabi_no_regargs struct pt_regs regs)
522 struct sigframe32 __user *frame;
526 frame = (struct sigframe32 __user *) regs.regs[29];
527 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
529 if (__copy_conv_sigset_from_user(&blocked, &frame->sf_mask))
532 sigdelsetmask(&blocked, ~_BLOCKABLE);
533 spin_lock_irq(¤t->sighand->siglock);
534 current->blocked = blocked;
536 spin_unlock_irq(¤t->sighand->siglock);
538 sig = restore_sigcontext32(®s, &frame->sf_sc);
542 force_sig(sig, current);
545 * Don't let your children do this ...
547 __asm__ __volatile__(
555 force_sig(SIGSEGV, current);
558 asmlinkage void sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
560 struct rt_sigframe32 __user *frame;
567 frame = (struct rt_sigframe32 __user *) regs.regs[29];
568 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
570 if (__copy_conv_sigset_from_user(&set, &frame->rs_uc.uc_sigmask))
573 sigdelsetmask(&set, ~_BLOCKABLE);
574 spin_lock_irq(¤t->sighand->siglock);
575 current->blocked = set;
577 spin_unlock_irq(¤t->sighand->siglock);
579 sig = restore_sigcontext32(®s, &frame->rs_uc.uc_mcontext);
583 force_sig(sig, current);
585 /* The ucontext contains a stack32_t, so we must convert! */
586 if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp))
588 st.ss_sp = (void __user *)(long) sp;
589 if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size))
591 if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags))
594 /* It is more difficult to avoid calling this function than to
595 call it and ignore errors. */
598 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
602 * Don't let your children do this ...
604 __asm__ __volatile__(
612 force_sig(SIGSEGV, current);
615 static int setup_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
616 int signr, sigset_t *set)
618 struct sigframe32 __user *frame;
621 frame = get_sigframe(ka, regs, sizeof(*frame));
622 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
625 err |= install_sigtramp(frame->sf_code, __NR_O32_sigreturn);
627 err |= setup_sigcontext32(regs, &frame->sf_sc);
628 err |= __copy_conv_sigset_to_user(&frame->sf_mask, set);
634 * Arguments to signal handler:
637 * a1 = 0 (should be cause)
638 * a2 = pointer to struct sigcontext
640 * $25 and c0_epc point to the signal handler, $29 points to the
643 regs->regs[ 4] = signr;
645 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
646 regs->regs[29] = (unsigned long) frame;
647 regs->regs[31] = (unsigned long) frame->sf_code;
648 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
650 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
651 current->comm, current->pid,
652 frame, regs->cp0_epc, regs->regs[31]);
657 force_sigsegv(signr, current);
661 static int setup_rt_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
662 int signr, sigset_t *set, siginfo_t *info)
664 struct rt_sigframe32 __user *frame;
668 frame = get_sigframe(ka, regs, sizeof(*frame));
669 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
672 err |= install_sigtramp(frame->rs_code, __NR_O32_rt_sigreturn);
674 /* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
675 err |= copy_siginfo_to_user32(&frame->rs_info, info);
677 /* Create the ucontext. */
678 err |= __put_user(0, &frame->rs_uc.uc_flags);
679 err |= __put_user(0, &frame->rs_uc.uc_link);
680 sp = (int) (long) current->sas_ss_sp;
681 err |= __put_user(sp,
682 &frame->rs_uc.uc_stack.ss_sp);
683 err |= __put_user(sas_ss_flags(regs->regs[29]),
684 &frame->rs_uc.uc_stack.ss_flags);
685 err |= __put_user(current->sas_ss_size,
686 &frame->rs_uc.uc_stack.ss_size);
687 err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
688 err |= __copy_conv_sigset_to_user(&frame->rs_uc.uc_sigmask, set);
694 * Arguments to signal handler:
697 * a1 = 0 (should be cause)
698 * a2 = pointer to ucontext
700 * $25 and c0_epc point to the signal handler, $29 points to
701 * the struct rt_sigframe32.
703 regs->regs[ 4] = signr;
704 regs->regs[ 5] = (unsigned long) &frame->rs_info;
705 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
706 regs->regs[29] = (unsigned long) frame;
707 regs->regs[31] = (unsigned long) frame->rs_code;
708 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
710 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
711 current->comm, current->pid,
712 frame, regs->cp0_epc, regs->regs[31]);
717 force_sigsegv(signr, current);
722 * o32 compatibility on 64-bit kernels, without DSP ASE
724 struct mips_abi mips_abi_32 = {
725 .setup_frame = setup_frame_32,
726 .setup_rt_frame = setup_rt_frame_32,
727 .restart = __NR_O32_restart_syscall
730 asmlinkage int sys32_rt_sigaction(int sig, const struct sigaction32 __user *act,
731 struct sigaction32 __user *oact,
732 unsigned int sigsetsize)
734 struct k_sigaction new_sa, old_sa;
737 /* XXX: Don't preclude handling different sized sigset_t's. */
738 if (sigsetsize != sizeof(sigset_t))
745 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
747 err |= __get_user(handler, &act->sa_handler);
748 new_sa.sa.sa_handler = (void __user *)(s64)handler;
749 err |= __get_user(new_sa.sa.sa_flags, &act->sa_flags);
750 err |= get_sigset(&new_sa.sa.sa_mask, &act->sa_mask);
755 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
760 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
763 err |= __put_user((u32)(u64)old_sa.sa.sa_handler,
765 err |= __put_user(old_sa.sa.sa_flags, &oact->sa_flags);
766 err |= put_sigset(&old_sa.sa.sa_mask, &oact->sa_mask);
774 asmlinkage int sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
775 compat_sigset_t __user *oset, unsigned int sigsetsize)
777 sigset_t old_set, new_set;
779 mm_segment_t old_fs = get_fs();
781 if (set && get_sigset(&new_set, set))
785 ret = sys_rt_sigprocmask(how, set ? (sigset_t __user *)&new_set : NULL,
786 oset ? (sigset_t __user *)&old_set : NULL,
790 if (!ret && oset && put_sigset(&old_set, oset))
796 asmlinkage int sys32_rt_sigpending(compat_sigset_t __user *uset,
797 unsigned int sigsetsize)
801 mm_segment_t old_fs = get_fs();
804 ret = sys_rt_sigpending((sigset_t __user *)&set, sigsetsize);
807 if (!ret && put_sigset(&set, uset))
813 asmlinkage int sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t __user *uinfo)
817 mm_segment_t old_fs = get_fs();
819 if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
820 copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE))
823 ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *)&info);
829 sys32_waitid(int which, compat_pid_t pid,
830 compat_siginfo_t __user *uinfo, int options,
831 struct compat_rusage __user *uru)
836 mm_segment_t old_fs = get_fs();
840 ret = sys_waitid(which, pid, (siginfo_t __user *) &info, options,
841 uru ? (struct rusage __user *) &ru : NULL);
844 if (ret < 0 || info.si_signo == 0)
847 if (uru && (ret = put_compat_rusage(&ru, uru)))
850 BUG_ON(info.si_code & __SI_MASK);
851 info.si_code |= __SI_CHLD;
852 return copy_siginfo_to_user32(uinfo, &info);