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)
186 err |= __put_user(regs->cp0_epc, &sc->sc_pc);
188 err |= __put_user(0, &sc->sc_regs[0]);
189 for (i = 1; i < 32; i++)
190 err |= __put_user(regs->regs[i], &sc->sc_regs[i]);
192 err |= __put_user(regs->hi, &sc->sc_mdhi);
193 err |= __put_user(regs->lo, &sc->sc_mdlo);
195 err |= __put_user(rddsp(DSP_MASK), &sc->sc_dsp);
196 err |= __put_user(mfhi1(), &sc->sc_hi1);
197 err |= __put_user(mflo1(), &sc->sc_lo1);
198 err |= __put_user(mfhi2(), &sc->sc_hi2);
199 err |= __put_user(mflo2(), &sc->sc_lo2);
200 err |= __put_user(mfhi3(), &sc->sc_hi3);
201 err |= __put_user(mflo3(), &sc->sc_lo3);
204 used_math = !!used_math();
205 err |= __put_user(used_math, &sc->sc_used_math);
209 * Save FPU state to signal context. Signal handler
210 * will "inherit" current FPU state.
213 enable_fp_in_kernel();
214 err |= save_fp_context32(sc);
215 disable_fp_in_kernel();
221 check_and_restore_fp_context32(struct sigcontext32 __user *sc)
225 err = sig = fpcsr_pending(&sc->sc_fpc_csr);
228 err |= restore_fp_context32(sc);
232 static int restore_sigcontext32(struct pt_regs *regs,
233 struct sigcontext32 __user *sc)
240 /* Always make any pending restarted system calls return -EINTR */
241 current_thread_info()->restart_block.fn = do_no_restart_syscall;
243 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
244 err |= __get_user(regs->hi, &sc->sc_mdhi);
245 err |= __get_user(regs->lo, &sc->sc_mdlo);
247 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
248 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
249 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
250 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
251 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
252 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
253 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
256 for (i = 1; i < 32; i++)
257 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
259 err |= __get_user(used_math, &sc->sc_used_math);
260 conditional_used_math(used_math);
263 /* restore fpu context if we have used it before */
265 enable_fp_in_kernel();
267 err = check_and_restore_fp_context32(sc);
268 disable_fp_in_kernel();
270 /* signal handler may have used FPU. Give it up. */
280 extern void __put_sigset_unknown_nsig(void);
281 extern void __get_sigset_unknown_nsig(void);
283 static inline int put_sigset(const sigset_t *kbuf, compat_sigset_t __user *ubuf)
287 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)))
290 switch (_NSIG_WORDS) {
292 __put_sigset_unknown_nsig();
294 err |= __put_user (kbuf->sig[1] >> 32, &ubuf->sig[3]);
295 err |= __put_user (kbuf->sig[1] & 0xffffffff, &ubuf->sig[2]);
297 err |= __put_user (kbuf->sig[0] >> 32, &ubuf->sig[1]);
298 err |= __put_user (kbuf->sig[0] & 0xffffffff, &ubuf->sig[0]);
304 static inline int get_sigset(sigset_t *kbuf, const compat_sigset_t __user *ubuf)
307 unsigned long sig[4];
309 if (!access_ok(VERIFY_READ, ubuf, sizeof(*ubuf)))
312 switch (_NSIG_WORDS) {
314 __get_sigset_unknown_nsig();
316 err |= __get_user (sig[3], &ubuf->sig[3]);
317 err |= __get_user (sig[2], &ubuf->sig[2]);
318 kbuf->sig[1] = sig[2] | (sig[3] << 32);
320 err |= __get_user (sig[1], &ubuf->sig[1]);
321 err |= __get_user (sig[0], &ubuf->sig[0]);
322 kbuf->sig[0] = sig[0] | (sig[1] << 32);
329 * Atomically swap in the new signal mask, and wait for a signal.
332 asmlinkage int sys32_sigsuspend(nabi_no_regargs struct pt_regs regs)
334 compat_sigset_t __user *uset;
337 uset = (compat_sigset_t __user *) regs.regs[4];
338 if (get_sigset(&newset, uset))
340 sigdelsetmask(&newset, ~_BLOCKABLE);
342 spin_lock_irq(¤t->sighand->siglock);
343 current->saved_sigmask = current->blocked;
344 current->blocked = newset;
346 spin_unlock_irq(¤t->sighand->siglock);
348 current->state = TASK_INTERRUPTIBLE;
350 set_thread_flag(TIF_RESTORE_SIGMASK);
351 return -ERESTARTNOHAND;
354 asmlinkage int sys32_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
356 compat_sigset_t __user *uset;
360 /* XXX Don't preclude handling different sized sigset_t's. */
361 sigsetsize = regs.regs[5];
362 if (sigsetsize != sizeof(compat_sigset_t))
365 uset = (compat_sigset_t __user *) regs.regs[4];
366 if (get_sigset(&newset, uset))
368 sigdelsetmask(&newset, ~_BLOCKABLE);
370 spin_lock_irq(¤t->sighand->siglock);
371 current->saved_sigmask = current->blocked;
372 current->blocked = newset;
374 spin_unlock_irq(¤t->sighand->siglock);
376 current->state = TASK_INTERRUPTIBLE;
378 set_thread_flag(TIF_RESTORE_SIGMASK);
379 return -ERESTARTNOHAND;
382 asmlinkage int sys32_sigaction(int sig, const struct sigaction32 __user *act,
383 struct sigaction32 __user *oact)
385 struct k_sigaction new_ka, old_ka;
393 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
395 err |= __get_user(handler, &act->sa_handler);
396 new_ka.sa.sa_handler = (void __user *)(s64)handler;
397 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
398 err |= __get_user(mask, &act->sa_mask.sig[0]);
402 siginitset(&new_ka.sa.sa_mask, mask);
405 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
408 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
410 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
411 err |= __put_user((u32)(u64)old_ka.sa.sa_handler,
413 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
414 err |= __put_user(0, &oact->sa_mask.sig[1]);
415 err |= __put_user(0, &oact->sa_mask.sig[2]);
416 err |= __put_user(0, &oact->sa_mask.sig[3]);
424 asmlinkage int sys32_sigaltstack(nabi_no_regargs struct pt_regs regs)
426 const stack32_t __user *uss = (const stack32_t __user *) regs.regs[4];
427 stack32_t __user *uoss = (stack32_t __user *) regs.regs[5];
428 unsigned long usp = regs.regs[29];
431 mm_segment_t old_fs = get_fs();
435 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
437 err |= __get_user(sp, &uss->ss_sp);
438 kss.ss_sp = (void __user *) (long) sp;
439 err |= __get_user(kss.ss_size, &uss->ss_size);
440 err |= __get_user(kss.ss_flags, &uss->ss_flags);
446 ret = do_sigaltstack(uss ? (stack_t __user *)&kss : NULL,
447 uoss ? (stack_t __user *)&koss : NULL, usp);
451 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
453 sp = (int) (unsigned long) koss.ss_sp;
454 err |= __put_user(sp, &uoss->ss_sp);
455 err |= __put_user(koss.ss_size, &uoss->ss_size);
456 err |= __put_user(koss.ss_flags, &uoss->ss_flags);
463 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
467 if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
470 /* If you change siginfo_t structure, please be sure
471 this code is fixed accordingly.
472 It should never copy any pad contained in the structure
473 to avoid security leaks, but must copy the generic
474 3 ints plus the relevant union member.
475 This routine must convert siginfo from 64bit to 32bit as well
477 err = __put_user(from->si_signo, &to->si_signo);
478 err |= __put_user(from->si_errno, &to->si_errno);
479 err |= __put_user((short)from->si_code, &to->si_code);
480 if (from->si_code < 0)
481 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
483 switch (from->si_code >> 16) {
484 case __SI_TIMER >> 16:
485 err |= __put_user(from->si_tid, &to->si_tid);
486 err |= __put_user(from->si_overrun, &to->si_overrun);
487 err |= __put_user(from->si_int, &to->si_int);
489 case __SI_CHLD >> 16:
490 err |= __put_user(from->si_utime, &to->si_utime);
491 err |= __put_user(from->si_stime, &to->si_stime);
492 err |= __put_user(from->si_status, &to->si_status);
494 err |= __put_user(from->si_pid, &to->si_pid);
495 err |= __put_user(from->si_uid, &to->si_uid);
497 case __SI_FAULT >> 16:
498 err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
500 case __SI_POLL >> 16:
501 err |= __put_user(from->si_band, &to->si_band);
502 err |= __put_user(from->si_fd, &to->si_fd);
504 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
505 case __SI_MESGQ >> 16:
506 err |= __put_user(from->si_pid, &to->si_pid);
507 err |= __put_user(from->si_uid, &to->si_uid);
508 err |= __put_user(from->si_int, &to->si_int);
515 asmlinkage void sys32_sigreturn(nabi_no_regargs struct pt_regs regs)
517 struct sigframe32 __user *frame;
521 frame = (struct sigframe32 __user *) regs.regs[29];
522 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
524 if (__copy_conv_sigset_from_user(&blocked, &frame->sf_mask))
527 sigdelsetmask(&blocked, ~_BLOCKABLE);
528 spin_lock_irq(¤t->sighand->siglock);
529 current->blocked = blocked;
531 spin_unlock_irq(¤t->sighand->siglock);
533 sig = restore_sigcontext32(®s, &frame->sf_sc);
537 force_sig(sig, current);
540 * Don't let your children do this ...
542 __asm__ __volatile__(
550 force_sig(SIGSEGV, current);
553 asmlinkage void sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
555 struct rt_sigframe32 __user *frame;
562 frame = (struct rt_sigframe32 __user *) regs.regs[29];
563 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
565 if (__copy_conv_sigset_from_user(&set, &frame->rs_uc.uc_sigmask))
568 sigdelsetmask(&set, ~_BLOCKABLE);
569 spin_lock_irq(¤t->sighand->siglock);
570 current->blocked = set;
572 spin_unlock_irq(¤t->sighand->siglock);
574 sig = restore_sigcontext32(®s, &frame->rs_uc.uc_mcontext);
578 force_sig(sig, current);
580 /* The ucontext contains a stack32_t, so we must convert! */
581 if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp))
583 st.ss_sp = (void __user *)(long) sp;
584 if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size))
586 if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags))
589 /* It is more difficult to avoid calling this function than to
590 call it and ignore errors. */
593 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
597 * Don't let your children do this ...
599 __asm__ __volatile__(
607 force_sig(SIGSEGV, current);
610 static int setup_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
611 int signr, sigset_t *set)
613 struct sigframe32 __user *frame;
616 frame = get_sigframe(ka, regs, sizeof(*frame));
617 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
620 err |= install_sigtramp(frame->sf_code, __NR_O32_sigreturn);
622 err |= setup_sigcontext32(regs, &frame->sf_sc);
623 err |= __copy_conv_sigset_to_user(&frame->sf_mask, set);
629 * Arguments to signal handler:
632 * a1 = 0 (should be cause)
633 * a2 = pointer to struct sigcontext
635 * $25 and c0_epc point to the signal handler, $29 points to the
638 regs->regs[ 4] = signr;
640 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
641 regs->regs[29] = (unsigned long) frame;
642 regs->regs[31] = (unsigned long) frame->sf_code;
643 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
645 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
646 current->comm, current->pid,
647 frame, regs->cp0_epc, regs->regs[31]);
652 force_sigsegv(signr, current);
656 static int setup_rt_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
657 int signr, sigset_t *set, siginfo_t *info)
659 struct rt_sigframe32 __user *frame;
663 frame = get_sigframe(ka, regs, sizeof(*frame));
664 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
667 err |= install_sigtramp(frame->rs_code, __NR_O32_rt_sigreturn);
669 /* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
670 err |= copy_siginfo_to_user32(&frame->rs_info, info);
672 /* Create the ucontext. */
673 err |= __put_user(0, &frame->rs_uc.uc_flags);
674 err |= __put_user(0, &frame->rs_uc.uc_link);
675 sp = (int) (long) current->sas_ss_sp;
676 err |= __put_user(sp,
677 &frame->rs_uc.uc_stack.ss_sp);
678 err |= __put_user(sas_ss_flags(regs->regs[29]),
679 &frame->rs_uc.uc_stack.ss_flags);
680 err |= __put_user(current->sas_ss_size,
681 &frame->rs_uc.uc_stack.ss_size);
682 err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
683 err |= __copy_conv_sigset_to_user(&frame->rs_uc.uc_sigmask, set);
689 * Arguments to signal handler:
692 * a1 = 0 (should be cause)
693 * a2 = pointer to ucontext
695 * $25 and c0_epc point to the signal handler, $29 points to
696 * the struct rt_sigframe32.
698 regs->regs[ 4] = signr;
699 regs->regs[ 5] = (unsigned long) &frame->rs_info;
700 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
701 regs->regs[29] = (unsigned long) frame;
702 regs->regs[31] = (unsigned long) frame->rs_code;
703 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
705 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
706 current->comm, current->pid,
707 frame, regs->cp0_epc, regs->regs[31]);
712 force_sigsegv(signr, current);
717 * o32 compatibility on 64-bit kernels, without DSP ASE
719 struct mips_abi mips_abi_32 = {
720 .setup_frame = setup_frame_32,
721 .setup_rt_frame = setup_rt_frame_32,
722 .restart = __NR_O32_restart_syscall
725 asmlinkage int sys32_rt_sigaction(int sig, const struct sigaction32 __user *act,
726 struct sigaction32 __user *oact,
727 unsigned int sigsetsize)
729 struct k_sigaction new_sa, old_sa;
732 /* XXX: Don't preclude handling different sized sigset_t's. */
733 if (sigsetsize != sizeof(sigset_t))
740 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
742 err |= __get_user(handler, &act->sa_handler);
743 new_sa.sa.sa_handler = (void __user *)(s64)handler;
744 err |= __get_user(new_sa.sa.sa_flags, &act->sa_flags);
745 err |= get_sigset(&new_sa.sa.sa_mask, &act->sa_mask);
750 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
755 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
758 err |= __put_user((u32)(u64)old_sa.sa.sa_handler,
760 err |= __put_user(old_sa.sa.sa_flags, &oact->sa_flags);
761 err |= put_sigset(&old_sa.sa.sa_mask, &oact->sa_mask);
769 asmlinkage int sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
770 compat_sigset_t __user *oset, unsigned int sigsetsize)
772 sigset_t old_set, new_set;
774 mm_segment_t old_fs = get_fs();
776 if (set && get_sigset(&new_set, set))
780 ret = sys_rt_sigprocmask(how, set ? (sigset_t __user *)&new_set : NULL,
781 oset ? (sigset_t __user *)&old_set : NULL,
785 if (!ret && oset && put_sigset(&old_set, oset))
791 asmlinkage int sys32_rt_sigpending(compat_sigset_t __user *uset,
792 unsigned int sigsetsize)
796 mm_segment_t old_fs = get_fs();
799 ret = sys_rt_sigpending((sigset_t __user *)&set, sigsetsize);
802 if (!ret && put_sigset(&set, uset))
808 asmlinkage int sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t __user *uinfo)
812 mm_segment_t old_fs = get_fs();
814 if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
815 copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE))
818 ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *)&info);
824 sys32_waitid(int which, compat_pid_t pid,
825 compat_siginfo_t __user *uinfo, int options,
826 struct compat_rusage __user *uru)
831 mm_segment_t old_fs = get_fs();
835 ret = sys_waitid(which, pid, (siginfo_t __user *) &info, options,
836 uru ? (struct rusage __user *) &ru : NULL);
839 if (ret < 0 || info.si_signo == 0)
842 if (uru && (ret = put_compat_rusage(&ru, uru)))
845 BUG_ON(info.si_code & __SI_MASK);
846 info.si_code |= __SI_CHLD;
847 return copy_siginfo_to_user32(uinfo, &info);