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"
40 * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
42 #define __NR_O32_sigreturn 4119
43 #define __NR_O32_rt_sigreturn 4193
44 #define __NR_O32_restart_syscall 4253
46 /* 32-bit compatibility types */
48 typedef unsigned int __sighandler32_t;
49 typedef void (*vfptr_t)(void);
52 unsigned int sa_flags;
53 __sighandler32_t sa_handler;
54 compat_sigset_t sa_mask;
57 /* IRIX compatible stack_t */
58 typedef struct sigaltstack32 {
60 compat_size_t ss_size;
68 struct sigcontext32 uc_mcontext;
69 compat_sigset_t uc_sigmask; /* mask last for extensibility */
73 * Horribly complicated - with the bloody RM9000 workarounds enabled
74 * the signal trampolines is moving to the end of the structure so we can
75 * increase the alignment without breaking software compatibility.
77 #if ICACHE_REFILLS_WORKAROUND_WAR == 0
80 u32 sf_ass[4]; /* argument save space for o32 */
81 u32 sf_code[2]; /* signal trampoline */
82 struct sigcontext32 sf_sc;
83 compat_sigset_t sf_mask;
86 struct rt_sigframe32 {
87 u32 rs_ass[4]; /* argument save space for o32 */
88 u32 rs_code[2]; /* signal trampoline */
89 compat_siginfo_t rs_info;
90 struct ucontext32 rs_uc;
93 #else /* ICACHE_REFILLS_WORKAROUND_WAR */
96 u32 sf_ass[4]; /* argument save space for o32 */
98 struct sigcontext32 sf_sc; /* hw context */
99 compat_sigset_t sf_mask;
100 u32 sf_code[8] ____cacheline_aligned; /* signal trampoline */
103 struct rt_sigframe32 {
104 u32 rs_ass[4]; /* argument save space for o32 */
106 compat_siginfo_t rs_info;
107 struct ucontext32 rs_uc;
108 u32 rs_code[8] __attribute__((aligned(32))); /* signal trampoline */
111 #endif /* !ICACHE_REFILLS_WORKAROUND_WAR */
114 * sigcontext handlers
116 static int protected_save_fp_context32(struct sigcontext32 __user *sc)
122 err = save_fp_context32(sc); /* this might fail */
126 /* touch the sigcontext and try again */
127 err = __put_user(0, &sc->sc_fpregs[0]) |
128 __put_user(0, &sc->sc_fpregs[31]) |
129 __put_user(0, &sc->sc_fpc_csr);
131 break; /* really bad sigcontext */
136 static int protected_restore_fp_context32(struct sigcontext32 __user *sc)
142 err = restore_fp_context32(sc); /* this might fail */
146 /* touch the sigcontext and try again */
147 err = __get_user(tmp, &sc->sc_fpregs[0]) |
148 __get_user(tmp, &sc->sc_fpregs[31]) |
149 __get_user(tmp, &sc->sc_fpc_csr);
151 break; /* really bad sigcontext */
156 static int setup_sigcontext32(struct pt_regs *regs,
157 struct sigcontext32 __user *sc)
163 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
165 err |= __put_user(0, &sc->sc_regs[0]);
166 for (i = 1; i < 32; i++)
167 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
169 err |= __put_user(regs->hi, &sc->sc_mdhi);
170 err |= __put_user(regs->lo, &sc->sc_mdlo);
172 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
173 err |= __put_user(mfhi1(), &sc->sc_hi1);
174 err |= __put_user(mflo1(), &sc->sc_lo1);
175 err |= __put_user(mfhi2(), &sc->sc_hi2);
176 err |= __put_user(mflo2(), &sc->sc_lo2);
177 err |= __put_user(mfhi3(), &sc->sc_hi3);
178 err |= __put_user(mflo3(), &sc->sc_lo3);
181 used_math = !!used_math();
182 err |= __put_user(used_math, &sc->sc_used_math);
186 * Save FPU state to signal context. Signal handler
187 * will "inherit" current FPU state.
189 err |= protected_save_fp_context32(sc);
195 check_and_restore_fp_context32(struct sigcontext32 __user *sc)
199 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
202 err |= protected_restore_fp_context32(sc);
206 static int restore_sigcontext32(struct pt_regs *regs,
207 struct sigcontext32 __user *sc)
214 /* Always make any pending restarted system calls return -EINTR */
215 current_thread_info()->restart_block.fn = do_no_restart_syscall;
217 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
218 err |= __get_user(regs->hi, &sc->sc_mdhi);
219 err |= __get_user(regs->lo, &sc->sc_mdlo);
221 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
222 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
223 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
224 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
225 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
226 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
227 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
230 for (i = 1; i < 32; i++)
231 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
233 err |= __get_user(used_math, &sc->sc_used_math);
234 conditional_used_math(used_math);
237 /* restore fpu context if we have used it before */
239 err = check_and_restore_fp_context32(sc);
241 /* signal handler may have used FPU. Give it up. */
251 extern void __put_sigset_unknown_nsig(void);
252 extern void __get_sigset_unknown_nsig(void);
254 static inline int put_sigset(const sigset_t *kbuf, compat_sigset_t __user *ubuf)
258 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)))
261 switch (_NSIG_WORDS) {
263 __put_sigset_unknown_nsig();
265 err |= __put_user (kbuf->sig[1] >> 32, &ubuf->sig[3]);
266 err |= __put_user (kbuf->sig[1] & 0xffffffff, &ubuf->sig[2]);
268 err |= __put_user (kbuf->sig[0] >> 32, &ubuf->sig[1]);
269 err |= __put_user (kbuf->sig[0] & 0xffffffff, &ubuf->sig[0]);
275 static inline int get_sigset(sigset_t *kbuf, const compat_sigset_t __user *ubuf)
278 unsigned long sig[4];
280 if (!access_ok(VERIFY_READ, ubuf, sizeof(*ubuf)))
283 switch (_NSIG_WORDS) {
285 __get_sigset_unknown_nsig();
287 err |= __get_user (sig[3], &ubuf->sig[3]);
288 err |= __get_user (sig[2], &ubuf->sig[2]);
289 kbuf->sig[1] = sig[2] | (sig[3] << 32);
291 err |= __get_user (sig[1], &ubuf->sig[1]);
292 err |= __get_user (sig[0], &ubuf->sig[0]);
293 kbuf->sig[0] = sig[0] | (sig[1] << 32);
300 * Atomically swap in the new signal mask, and wait for a signal.
303 asmlinkage int sys32_sigsuspend(nabi_no_regargs struct pt_regs regs)
305 compat_sigset_t __user *uset;
308 uset = (compat_sigset_t __user *) regs.regs[4];
309 if (get_sigset(&newset, uset))
311 sigdelsetmask(&newset, ~_BLOCKABLE);
313 spin_lock_irq(¤t->sighand->siglock);
314 current->saved_sigmask = current->blocked;
315 current->blocked = newset;
317 spin_unlock_irq(¤t->sighand->siglock);
319 current->state = TASK_INTERRUPTIBLE;
321 set_thread_flag(TIF_RESTORE_SIGMASK);
322 return -ERESTARTNOHAND;
325 asmlinkage int sys32_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
327 compat_sigset_t __user *uset;
331 /* XXX Don't preclude handling different sized sigset_t's. */
332 sigsetsize = regs.regs[5];
333 if (sigsetsize != sizeof(compat_sigset_t))
336 uset = (compat_sigset_t __user *) regs.regs[4];
337 if (get_sigset(&newset, uset))
339 sigdelsetmask(&newset, ~_BLOCKABLE);
341 spin_lock_irq(¤t->sighand->siglock);
342 current->saved_sigmask = current->blocked;
343 current->blocked = newset;
345 spin_unlock_irq(¤t->sighand->siglock);
347 current->state = TASK_INTERRUPTIBLE;
349 set_thread_flag(TIF_RESTORE_SIGMASK);
350 return -ERESTARTNOHAND;
353 asmlinkage int sys32_sigaction(int sig, const struct sigaction32 __user *act,
354 struct sigaction32 __user *oact)
356 struct k_sigaction new_ka, old_ka;
364 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
366 err |= __get_user(handler, &act->sa_handler);
367 new_ka.sa.sa_handler = (void __user *)(s64)handler;
368 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
369 err |= __get_user(mask, &act->sa_mask.sig[0]);
373 siginitset(&new_ka.sa.sa_mask, mask);
376 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
379 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
381 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
382 err |= __put_user((u32)(u64)old_ka.sa.sa_handler,
384 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
385 err |= __put_user(0, &oact->sa_mask.sig[1]);
386 err |= __put_user(0, &oact->sa_mask.sig[2]);
387 err |= __put_user(0, &oact->sa_mask.sig[3]);
395 asmlinkage int sys32_sigaltstack(nabi_no_regargs struct pt_regs regs)
397 const stack32_t __user *uss = (const stack32_t __user *) regs.regs[4];
398 stack32_t __user *uoss = (stack32_t __user *) regs.regs[5];
399 unsigned long usp = regs.regs[29];
402 mm_segment_t old_fs = get_fs();
406 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
408 err |= __get_user(sp, &uss->ss_sp);
409 kss.ss_sp = (void __user *) (long) sp;
410 err |= __get_user(kss.ss_size, &uss->ss_size);
411 err |= __get_user(kss.ss_flags, &uss->ss_flags);
417 ret = do_sigaltstack(uss ? (stack_t __user *)&kss : NULL,
418 uoss ? (stack_t __user *)&koss : NULL, usp);
422 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
424 sp = (int) (unsigned long) koss.ss_sp;
425 err |= __put_user(sp, &uoss->ss_sp);
426 err |= __put_user(koss.ss_size, &uoss->ss_size);
427 err |= __put_user(koss.ss_flags, &uoss->ss_flags);
434 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
438 if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
441 /* If you change siginfo_t structure, please be sure
442 this code is fixed accordingly.
443 It should never copy any pad contained in the structure
444 to avoid security leaks, but must copy the generic
445 3 ints plus the relevant union member.
446 This routine must convert siginfo from 64bit to 32bit as well
448 err = __put_user(from->si_signo, &to->si_signo);
449 err |= __put_user(from->si_errno, &to->si_errno);
450 err |= __put_user((short)from->si_code, &to->si_code);
451 if (from->si_code < 0)
452 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
454 switch (from->si_code >> 16) {
455 case __SI_TIMER >> 16:
456 err |= __put_user(from->si_tid, &to->si_tid);
457 err |= __put_user(from->si_overrun, &to->si_overrun);
458 err |= __put_user(from->si_int, &to->si_int);
460 case __SI_CHLD >> 16:
461 err |= __put_user(from->si_utime, &to->si_utime);
462 err |= __put_user(from->si_stime, &to->si_stime);
463 err |= __put_user(from->si_status, &to->si_status);
465 err |= __put_user(from->si_pid, &to->si_pid);
466 err |= __put_user(from->si_uid, &to->si_uid);
468 case __SI_FAULT >> 16:
469 err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
471 case __SI_POLL >> 16:
472 err |= __put_user(from->si_band, &to->si_band);
473 err |= __put_user(from->si_fd, &to->si_fd);
475 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
476 case __SI_MESGQ >> 16:
477 err |= __put_user(from->si_pid, &to->si_pid);
478 err |= __put_user(from->si_uid, &to->si_uid);
479 err |= __put_user(from->si_int, &to->si_int);
486 asmlinkage void sys32_sigreturn(nabi_no_regargs struct pt_regs regs)
488 struct sigframe32 __user *frame;
492 frame = (struct sigframe32 __user *) regs.regs[29];
493 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
495 if (__copy_conv_sigset_from_user(&blocked, &frame->sf_mask))
498 sigdelsetmask(&blocked, ~_BLOCKABLE);
499 spin_lock_irq(¤t->sighand->siglock);
500 current->blocked = blocked;
502 spin_unlock_irq(¤t->sighand->siglock);
504 sig = restore_sigcontext32(®s, &frame->sf_sc);
508 force_sig(sig, current);
511 * Don't let your children do this ...
513 __asm__ __volatile__(
521 force_sig(SIGSEGV, current);
524 asmlinkage void sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
526 struct rt_sigframe32 __user *frame;
533 frame = (struct rt_sigframe32 __user *) regs.regs[29];
534 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
536 if (__copy_conv_sigset_from_user(&set, &frame->rs_uc.uc_sigmask))
539 sigdelsetmask(&set, ~_BLOCKABLE);
540 spin_lock_irq(¤t->sighand->siglock);
541 current->blocked = set;
543 spin_unlock_irq(¤t->sighand->siglock);
545 sig = restore_sigcontext32(®s, &frame->rs_uc.uc_mcontext);
549 force_sig(sig, current);
551 /* The ucontext contains a stack32_t, so we must convert! */
552 if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp))
554 st.ss_sp = (void __user *)(long) sp;
555 if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size))
557 if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags))
560 /* It is more difficult to avoid calling this function than to
561 call it and ignore errors. */
564 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
568 * Don't let your children do this ...
570 __asm__ __volatile__(
578 force_sig(SIGSEGV, current);
581 static int setup_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
582 int signr, sigset_t *set)
584 struct sigframe32 __user *frame;
587 frame = get_sigframe(ka, regs, sizeof(*frame));
588 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
591 err |= install_sigtramp(frame->sf_code, __NR_O32_sigreturn);
593 err |= setup_sigcontext32(regs, &frame->sf_sc);
594 err |= __copy_conv_sigset_to_user(&frame->sf_mask, set);
600 * Arguments to signal handler:
603 * a1 = 0 (should be cause)
604 * a2 = pointer to struct sigcontext
606 * $25 and c0_epc point to the signal handler, $29 points to the
609 regs->regs[ 4] = signr;
611 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
612 regs->regs[29] = (unsigned long) frame;
613 regs->regs[31] = (unsigned long) frame->sf_code;
614 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
616 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
617 current->comm, current->pid,
618 frame, regs->cp0_epc, regs->regs[31]);
623 force_sigsegv(signr, current);
627 static int setup_rt_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
628 int signr, sigset_t *set, siginfo_t *info)
630 struct rt_sigframe32 __user *frame;
634 frame = get_sigframe(ka, regs, sizeof(*frame));
635 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
638 err |= install_sigtramp(frame->rs_code, __NR_O32_rt_sigreturn);
640 /* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
641 err |= copy_siginfo_to_user32(&frame->rs_info, info);
643 /* Create the ucontext. */
644 err |= __put_user(0, &frame->rs_uc.uc_flags);
645 err |= __put_user(0, &frame->rs_uc.uc_link);
646 sp = (int) (long) current->sas_ss_sp;
647 err |= __put_user(sp,
648 &frame->rs_uc.uc_stack.ss_sp);
649 err |= __put_user(sas_ss_flags(regs->regs[29]),
650 &frame->rs_uc.uc_stack.ss_flags);
651 err |= __put_user(current->sas_ss_size,
652 &frame->rs_uc.uc_stack.ss_size);
653 err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
654 err |= __copy_conv_sigset_to_user(&frame->rs_uc.uc_sigmask, set);
660 * Arguments to signal handler:
663 * a1 = 0 (should be cause)
664 * a2 = pointer to ucontext
666 * $25 and c0_epc point to the signal handler, $29 points to
667 * the struct rt_sigframe32.
669 regs->regs[ 4] = signr;
670 regs->regs[ 5] = (unsigned long) &frame->rs_info;
671 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
672 regs->regs[29] = (unsigned long) frame;
673 regs->regs[31] = (unsigned long) frame->rs_code;
674 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
676 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
677 current->comm, current->pid,
678 frame, regs->cp0_epc, regs->regs[31]);
683 force_sigsegv(signr, current);
688 * o32 compatibility on 64-bit kernels, without DSP ASE
690 struct mips_abi mips_abi_32 = {
691 .setup_frame = setup_frame_32,
692 .setup_rt_frame = setup_rt_frame_32,
693 .restart = __NR_O32_restart_syscall
696 asmlinkage int sys32_rt_sigaction(int sig, const struct sigaction32 __user *act,
697 struct sigaction32 __user *oact,
698 unsigned int sigsetsize)
700 struct k_sigaction new_sa, old_sa;
703 /* XXX: Don't preclude handling different sized sigset_t's. */
704 if (sigsetsize != sizeof(sigset_t))
711 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
713 err |= __get_user(handler, &act->sa_handler);
714 new_sa.sa.sa_handler = (void __user *)(s64)handler;
715 err |= __get_user(new_sa.sa.sa_flags, &act->sa_flags);
716 err |= get_sigset(&new_sa.sa.sa_mask, &act->sa_mask);
721 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
726 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
729 err |= __put_user((u32)(u64)old_sa.sa.sa_handler,
731 err |= __put_user(old_sa.sa.sa_flags, &oact->sa_flags);
732 err |= put_sigset(&old_sa.sa.sa_mask, &oact->sa_mask);
740 asmlinkage int sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
741 compat_sigset_t __user *oset, unsigned int sigsetsize)
743 sigset_t old_set, new_set;
745 mm_segment_t old_fs = get_fs();
747 if (set && get_sigset(&new_set, set))
751 ret = sys_rt_sigprocmask(how, set ? (sigset_t __user *)&new_set : NULL,
752 oset ? (sigset_t __user *)&old_set : NULL,
756 if (!ret && oset && put_sigset(&old_set, oset))
762 asmlinkage int sys32_rt_sigpending(compat_sigset_t __user *uset,
763 unsigned int sigsetsize)
767 mm_segment_t old_fs = get_fs();
770 ret = sys_rt_sigpending((sigset_t __user *)&set, sigsetsize);
773 if (!ret && put_sigset(&set, uset))
779 asmlinkage int sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t __user *uinfo)
783 mm_segment_t old_fs = get_fs();
785 if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
786 copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE))
789 ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *)&info);
795 sys32_waitid(int which, compat_pid_t pid,
796 compat_siginfo_t __user *uinfo, int options,
797 struct compat_rusage __user *uru)
802 mm_segment_t old_fs = get_fs();
806 ret = sys_waitid(which, pid, (siginfo_t __user *) &info, options,
807 uru ? (struct rusage __user *) &ru : NULL);
810 if (ret < 0 || info.si_signo == 0)
813 if (uru && (ret = put_compat_rusage(&ru, uru)))
816 BUG_ON(info.si_code & __SI_MASK);
817 info.si_code |= __SI_CHLD;
818 return copy_siginfo_to_user32(uinfo, &info);