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);
223 static int restore_sigcontext32(struct pt_regs *regs,
224 struct sigcontext32 __user *sc)
231 /* Always make any pending restarted system calls return -EINTR */
232 current_thread_info()->restart_block.fn = do_no_restart_syscall;
234 err |= __get_user(regs->cp0_epc, &sc->sc_pc);
235 err |= __get_user(regs->hi, &sc->sc_mdhi);
236 err |= __get_user(regs->lo, &sc->sc_mdlo);
238 err |= __get_user(treg, &sc->sc_hi1); mthi1(treg);
239 err |= __get_user(treg, &sc->sc_lo1); mtlo1(treg);
240 err |= __get_user(treg, &sc->sc_hi2); mthi2(treg);
241 err |= __get_user(treg, &sc->sc_lo2); mtlo2(treg);
242 err |= __get_user(treg, &sc->sc_hi3); mthi3(treg);
243 err |= __get_user(treg, &sc->sc_lo3); mtlo3(treg);
244 err |= __get_user(treg, &sc->sc_dsp); wrdsp(treg, DSP_MASK);
247 for (i = 1; i < 32; i++)
248 err |= __get_user(regs->regs[i], &sc->sc_regs[i]);
250 err |= __get_user(used_math, &sc->sc_used_math);
251 conditional_used_math(used_math);
256 /* restore fpu context if we have used it before */
258 err |= restore_fp_context32(sc);
260 /* signal handler may have used FPU. Give it up. */
272 extern void __put_sigset_unknown_nsig(void);
273 extern void __get_sigset_unknown_nsig(void);
275 static inline int put_sigset(const sigset_t *kbuf, compat_sigset_t __user *ubuf)
279 if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)))
282 switch (_NSIG_WORDS) {
284 __put_sigset_unknown_nsig();
286 err |= __put_user (kbuf->sig[1] >> 32, &ubuf->sig[3]);
287 err |= __put_user (kbuf->sig[1] & 0xffffffff, &ubuf->sig[2]);
289 err |= __put_user (kbuf->sig[0] >> 32, &ubuf->sig[1]);
290 err |= __put_user (kbuf->sig[0] & 0xffffffff, &ubuf->sig[0]);
296 static inline int get_sigset(sigset_t *kbuf, const compat_sigset_t __user *ubuf)
299 unsigned long sig[4];
301 if (!access_ok(VERIFY_READ, ubuf, sizeof(*ubuf)))
304 switch (_NSIG_WORDS) {
306 __get_sigset_unknown_nsig();
308 err |= __get_user (sig[3], &ubuf->sig[3]);
309 err |= __get_user (sig[2], &ubuf->sig[2]);
310 kbuf->sig[1] = sig[2] | (sig[3] << 32);
312 err |= __get_user (sig[1], &ubuf->sig[1]);
313 err |= __get_user (sig[0], &ubuf->sig[0]);
314 kbuf->sig[0] = sig[0] | (sig[1] << 32);
321 * Atomically swap in the new signal mask, and wait for a signal.
324 asmlinkage int sys32_sigsuspend(nabi_no_regargs struct pt_regs regs)
326 compat_sigset_t __user *uset;
329 uset = (compat_sigset_t __user *) regs.regs[4];
330 if (get_sigset(&newset, uset))
332 sigdelsetmask(&newset, ~_BLOCKABLE);
334 spin_lock_irq(¤t->sighand->siglock);
335 current->saved_sigmask = current->blocked;
336 current->blocked = newset;
338 spin_unlock_irq(¤t->sighand->siglock);
340 current->state = TASK_INTERRUPTIBLE;
342 set_thread_flag(TIF_RESTORE_SIGMASK);
343 return -ERESTARTNOHAND;
346 asmlinkage int sys32_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
348 compat_sigset_t __user *uset;
352 /* XXX Don't preclude handling different sized sigset_t's. */
353 sigsetsize = regs.regs[5];
354 if (sigsetsize != sizeof(compat_sigset_t))
357 uset = (compat_sigset_t __user *) regs.regs[4];
358 if (get_sigset(&newset, uset))
360 sigdelsetmask(&newset, ~_BLOCKABLE);
362 spin_lock_irq(¤t->sighand->siglock);
363 current->saved_sigmask = current->blocked;
364 current->blocked = newset;
366 spin_unlock_irq(¤t->sighand->siglock);
368 current->state = TASK_INTERRUPTIBLE;
370 set_thread_flag(TIF_RESTORE_SIGMASK);
371 return -ERESTARTNOHAND;
374 asmlinkage int sys32_sigaction(int sig, const struct sigaction32 __user *act,
375 struct sigaction32 __user *oact)
377 struct k_sigaction new_ka, old_ka;
385 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
387 err |= __get_user(handler, &act->sa_handler);
388 new_ka.sa.sa_handler = (void __user *)(s64)handler;
389 err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
390 err |= __get_user(mask, &act->sa_mask.sig[0]);
394 siginitset(&new_ka.sa.sa_mask, mask);
397 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
400 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
402 err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
403 err |= __put_user((u32)(u64)old_ka.sa.sa_handler,
405 err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
406 err |= __put_user(0, &oact->sa_mask.sig[1]);
407 err |= __put_user(0, &oact->sa_mask.sig[2]);
408 err |= __put_user(0, &oact->sa_mask.sig[3]);
416 asmlinkage int sys32_sigaltstack(nabi_no_regargs struct pt_regs regs)
418 const stack32_t __user *uss = (const stack32_t __user *) regs.regs[4];
419 stack32_t __user *uoss = (stack32_t __user *) regs.regs[5];
420 unsigned long usp = regs.regs[29];
423 mm_segment_t old_fs = get_fs();
427 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
429 err |= __get_user(sp, &uss->ss_sp);
430 kss.ss_sp = (void __user *) (long) sp;
431 err |= __get_user(kss.ss_size, &uss->ss_size);
432 err |= __get_user(kss.ss_flags, &uss->ss_flags);
438 ret = do_sigaltstack(uss ? (stack_t __user *)&kss : NULL,
439 uoss ? (stack_t __user *)&koss : NULL, usp);
443 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
445 sp = (int) (unsigned long) koss.ss_sp;
446 err |= __put_user(sp, &uoss->ss_sp);
447 err |= __put_user(koss.ss_size, &uoss->ss_size);
448 err |= __put_user(koss.ss_flags, &uoss->ss_flags);
455 int copy_siginfo_to_user32(compat_siginfo_t __user *to, siginfo_t *from)
459 if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
462 /* If you change siginfo_t structure, please be sure
463 this code is fixed accordingly.
464 It should never copy any pad contained in the structure
465 to avoid security leaks, but must copy the generic
466 3 ints plus the relevant union member.
467 This routine must convert siginfo from 64bit to 32bit as well
469 err = __put_user(from->si_signo, &to->si_signo);
470 err |= __put_user(from->si_errno, &to->si_errno);
471 err |= __put_user((short)from->si_code, &to->si_code);
472 if (from->si_code < 0)
473 err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
475 switch (from->si_code >> 16) {
476 case __SI_TIMER >> 16:
477 err |= __put_user(from->si_tid, &to->si_tid);
478 err |= __put_user(from->si_overrun, &to->si_overrun);
479 err |= __put_user(from->si_int, &to->si_int);
481 case __SI_CHLD >> 16:
482 err |= __put_user(from->si_utime, &to->si_utime);
483 err |= __put_user(from->si_stime, &to->si_stime);
484 err |= __put_user(from->si_status, &to->si_status);
486 err |= __put_user(from->si_pid, &to->si_pid);
487 err |= __put_user(from->si_uid, &to->si_uid);
489 case __SI_FAULT >> 16:
490 err |= __put_user((unsigned long)from->si_addr, &to->si_addr);
492 case __SI_POLL >> 16:
493 err |= __put_user(from->si_band, &to->si_band);
494 err |= __put_user(from->si_fd, &to->si_fd);
496 case __SI_RT >> 16: /* This is not generated by the kernel as of now. */
497 case __SI_MESGQ >> 16:
498 err |= __put_user(from->si_pid, &to->si_pid);
499 err |= __put_user(from->si_uid, &to->si_uid);
500 err |= __put_user(from->si_int, &to->si_int);
507 asmlinkage void sys32_sigreturn(nabi_no_regargs struct pt_regs regs)
509 struct sigframe32 __user *frame;
512 frame = (struct sigframe32 __user *) regs.regs[29];
513 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
515 if (__copy_conv_sigset_from_user(&blocked, &frame->sf_mask))
518 sigdelsetmask(&blocked, ~_BLOCKABLE);
519 spin_lock_irq(¤t->sighand->siglock);
520 current->blocked = blocked;
522 spin_unlock_irq(¤t->sighand->siglock);
524 if (restore_sigcontext32(®s, &frame->sf_sc))
528 * Don't let your children do this ...
530 __asm__ __volatile__(
538 force_sig(SIGSEGV, current);
541 asmlinkage void sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
543 struct rt_sigframe32 __user *frame;
549 frame = (struct rt_sigframe32 __user *) regs.regs[29];
550 if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
552 if (__copy_conv_sigset_from_user(&set, &frame->rs_uc.uc_sigmask))
555 sigdelsetmask(&set, ~_BLOCKABLE);
556 spin_lock_irq(¤t->sighand->siglock);
557 current->blocked = set;
559 spin_unlock_irq(¤t->sighand->siglock);
561 if (restore_sigcontext32(®s, &frame->rs_uc.uc_mcontext))
564 /* The ucontext contains a stack32_t, so we must convert! */
565 if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp))
567 st.ss_sp = (void __user *)(long) sp;
568 if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size))
570 if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags))
573 /* It is more difficult to avoid calling this function than to
574 call it and ignore errors. */
577 do_sigaltstack((stack_t __user *)&st, NULL, regs.regs[29]);
581 * Don't let your children do this ...
583 __asm__ __volatile__(
591 force_sig(SIGSEGV, current);
594 static int setup_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
595 int signr, sigset_t *set)
597 struct sigframe32 __user *frame;
600 frame = get_sigframe(ka, regs, sizeof(*frame));
601 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
604 err |= install_sigtramp(frame->sf_code, __NR_O32_sigreturn);
606 err |= setup_sigcontext32(regs, &frame->sf_sc);
607 err |= __copy_conv_sigset_to_user(&frame->sf_mask, set);
613 * Arguments to signal handler:
616 * a1 = 0 (should be cause)
617 * a2 = pointer to struct sigcontext
619 * $25 and c0_epc point to the signal handler, $29 points to the
622 regs->regs[ 4] = signr;
624 regs->regs[ 6] = (unsigned long) &frame->sf_sc;
625 regs->regs[29] = (unsigned long) frame;
626 regs->regs[31] = (unsigned long) frame->sf_code;
627 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
629 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
630 current->comm, current->pid,
631 frame, regs->cp0_epc, regs->regs[31]);
636 force_sigsegv(signr, current);
640 static int setup_rt_frame_32(struct k_sigaction * ka, struct pt_regs *regs,
641 int signr, sigset_t *set, siginfo_t *info)
643 struct rt_sigframe32 __user *frame;
647 frame = get_sigframe(ka, regs, sizeof(*frame));
648 if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
651 err |= install_sigtramp(frame->rs_code, __NR_O32_rt_sigreturn);
653 /* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
654 err |= copy_siginfo_to_user32(&frame->rs_info, info);
656 /* Create the ucontext. */
657 err |= __put_user(0, &frame->rs_uc.uc_flags);
658 err |= __put_user(0, &frame->rs_uc.uc_link);
659 sp = (int) (long) current->sas_ss_sp;
660 err |= __put_user(sp,
661 &frame->rs_uc.uc_stack.ss_sp);
662 err |= __put_user(sas_ss_flags(regs->regs[29]),
663 &frame->rs_uc.uc_stack.ss_flags);
664 err |= __put_user(current->sas_ss_size,
665 &frame->rs_uc.uc_stack.ss_size);
666 err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
667 err |= __copy_conv_sigset_to_user(&frame->rs_uc.uc_sigmask, set);
673 * Arguments to signal handler:
676 * a1 = 0 (should be cause)
677 * a2 = pointer to ucontext
679 * $25 and c0_epc point to the signal handler, $29 points to
680 * the struct rt_sigframe32.
682 regs->regs[ 4] = signr;
683 regs->regs[ 5] = (unsigned long) &frame->rs_info;
684 regs->regs[ 6] = (unsigned long) &frame->rs_uc;
685 regs->regs[29] = (unsigned long) frame;
686 regs->regs[31] = (unsigned long) frame->rs_code;
687 regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
689 DEBUGP("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%lx\n",
690 current->comm, current->pid,
691 frame, regs->cp0_epc, regs->regs[31]);
696 force_sigsegv(signr, current);
701 * o32 compatibility on 64-bit kernels, without DSP ASE
703 struct mips_abi mips_abi_32 = {
704 .setup_frame = setup_frame_32,
705 .setup_rt_frame = setup_rt_frame_32,
706 .restart = __NR_O32_restart_syscall
709 asmlinkage int sys32_rt_sigaction(int sig, const struct sigaction32 __user *act,
710 struct sigaction32 __user *oact,
711 unsigned int sigsetsize)
713 struct k_sigaction new_sa, old_sa;
716 /* XXX: Don't preclude handling different sized sigset_t's. */
717 if (sigsetsize != sizeof(sigset_t))
724 if (!access_ok(VERIFY_READ, act, sizeof(*act)))
726 err |= __get_user(handler, &act->sa_handler);
727 new_sa.sa.sa_handler = (void __user *)(s64)handler;
728 err |= __get_user(new_sa.sa.sa_flags, &act->sa_flags);
729 err |= get_sigset(&new_sa.sa.sa_mask, &act->sa_mask);
734 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
739 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
742 err |= __put_user((u32)(u64)old_sa.sa.sa_handler,
744 err |= __put_user(old_sa.sa.sa_flags, &oact->sa_flags);
745 err |= put_sigset(&old_sa.sa.sa_mask, &oact->sa_mask);
753 asmlinkage int sys32_rt_sigprocmask(int how, compat_sigset_t __user *set,
754 compat_sigset_t __user *oset, unsigned int sigsetsize)
756 sigset_t old_set, new_set;
758 mm_segment_t old_fs = get_fs();
760 if (set && get_sigset(&new_set, set))
764 ret = sys_rt_sigprocmask(how, set ? (sigset_t __user *)&new_set : NULL,
765 oset ? (sigset_t __user *)&old_set : NULL,
769 if (!ret && oset && put_sigset(&old_set, oset))
775 asmlinkage int sys32_rt_sigpending(compat_sigset_t __user *uset,
776 unsigned int sigsetsize)
780 mm_segment_t old_fs = get_fs();
783 ret = sys_rt_sigpending((sigset_t __user *)&set, sigsetsize);
786 if (!ret && put_sigset(&set, uset))
792 asmlinkage int sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t __user *uinfo)
796 mm_segment_t old_fs = get_fs();
798 if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
799 copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE))
802 ret = sys_rt_sigqueueinfo(pid, sig, (siginfo_t __user *)&info);
808 sys32_waitid(int which, compat_pid_t pid,
809 compat_siginfo_t __user *uinfo, int options,
810 struct compat_rusage __user *uru)
815 mm_segment_t old_fs = get_fs();
819 ret = sys_waitid(which, pid, (siginfo_t __user *) &info, options,
820 uru ? (struct rusage __user *) &ru : NULL);
823 if (ret < 0 || info.si_signo == 0)
826 if (uru && (ret = put_compat_rusage(&ru, uru)))
829 BUG_ON(info.si_code & __SI_MASK);
830 info.si_code |= __SI_CHLD;
831 return copy_siginfo_to_user32(uinfo, &info);