xref: /openbmc/linux/arch/mips/kernel/signal32.c (revision 54f2da755b7f0bf022ea204240cba824af4d80ad)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * This file is subject to the terms and conditions of the GNU General Public
31da177e4SLinus Torvalds  * License.  See the file "COPYING" in the main directory of this archive
41da177e4SLinus Torvalds  * for more details.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * Copyright (C) 1991, 1992  Linus Torvalds
71da177e4SLinus Torvalds  * Copyright (C) 1994 - 2000  Ralf Baechle
81da177e4SLinus Torvalds  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds #include <linux/sched.h>
111da177e4SLinus Torvalds #include <linux/mm.h>
121da177e4SLinus Torvalds #include <linux/smp.h>
131da177e4SLinus Torvalds #include <linux/smp_lock.h>
141da177e4SLinus Torvalds #include <linux/kernel.h>
151da177e4SLinus Torvalds #include <linux/signal.h>
161da177e4SLinus Torvalds #include <linux/syscalls.h>
171da177e4SLinus Torvalds #include <linux/errno.h>
181da177e4SLinus Torvalds #include <linux/wait.h>
191da177e4SLinus Torvalds #include <linux/ptrace.h>
201da177e4SLinus Torvalds #include <linux/compat.h>
211da177e4SLinus Torvalds #include <linux/suspend.h>
221da177e4SLinus Torvalds #include <linux/compiler.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <asm/asm.h>
251da177e4SLinus Torvalds #include <linux/bitops.h>
261da177e4SLinus Torvalds #include <asm/cacheflush.h>
271da177e4SLinus Torvalds #include <asm/sim.h>
281da177e4SLinus Torvalds #include <asm/uaccess.h>
291da177e4SLinus Torvalds #include <asm/ucontext.h>
301da177e4SLinus Torvalds #include <asm/system.h>
311da177e4SLinus Torvalds #include <asm/fpu.h>
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds #define SI_PAD_SIZE32   ((SI_MAX_SIZE/sizeof(int)) - 3)
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds typedef struct compat_siginfo {
361da177e4SLinus Torvalds 	int si_signo;
371da177e4SLinus Torvalds 	int si_code;
381da177e4SLinus Torvalds 	int si_errno;
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds 	union {
411da177e4SLinus Torvalds 		int _pad[SI_PAD_SIZE32];
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds 		/* kill() */
441da177e4SLinus Torvalds 		struct {
451da177e4SLinus Torvalds 			compat_pid_t _pid;	/* sender's pid */
461da177e4SLinus Torvalds 			compat_uid_t _uid;	/* sender's uid */
471da177e4SLinus Torvalds 		} _kill;
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds 		/* SIGCHLD */
501da177e4SLinus Torvalds 		struct {
511da177e4SLinus Torvalds 			compat_pid_t _pid;	/* which child */
521da177e4SLinus Torvalds 			compat_uid_t _uid;	/* sender's uid */
531da177e4SLinus Torvalds 			int _status;		/* exit code */
541da177e4SLinus Torvalds 			compat_clock_t _utime;
551da177e4SLinus Torvalds 			compat_clock_t _stime;
561da177e4SLinus Torvalds 		} _sigchld;
571da177e4SLinus Torvalds 
581da177e4SLinus Torvalds 		/* IRIX SIGCHLD */
591da177e4SLinus Torvalds 		struct {
601da177e4SLinus Torvalds 			compat_pid_t _pid;	/* which child */
611da177e4SLinus Torvalds 			compat_clock_t _utime;
621da177e4SLinus Torvalds 			int _status;		/* exit code */
631da177e4SLinus Torvalds 			compat_clock_t _stime;
641da177e4SLinus Torvalds 		} _irix_sigchld;
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 		/* SIGILL, SIGFPE, SIGSEGV, SIGBUS */
671da177e4SLinus Torvalds 		struct {
681da177e4SLinus Torvalds 			s32 _addr; /* faulting insn/memory ref. */
691da177e4SLinus Torvalds 		} _sigfault;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 		/* SIGPOLL, SIGXFSZ (To do ...)  */
721da177e4SLinus Torvalds 		struct {
731da177e4SLinus Torvalds 			int _band;	/* POLL_IN, POLL_OUT, POLL_MSG */
741da177e4SLinus Torvalds 			int _fd;
751da177e4SLinus Torvalds 		} _sigpoll;
761da177e4SLinus Torvalds 
771da177e4SLinus Torvalds 		/* POSIX.1b timers */
781da177e4SLinus Torvalds 		struct {
791da177e4SLinus Torvalds 			unsigned int _timer1;
801da177e4SLinus Torvalds 			unsigned int _timer2;
811da177e4SLinus Torvalds 		} _timer;
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 		/* POSIX.1b signals */
841da177e4SLinus Torvalds 		struct {
851da177e4SLinus Torvalds 			compat_pid_t _pid;	/* sender's pid */
861da177e4SLinus Torvalds 			compat_uid_t _uid;	/* sender's uid */
871da177e4SLinus Torvalds 			compat_sigval_t _sigval;
881da177e4SLinus Torvalds 		} _rt;
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds 	} _sifields;
911da177e4SLinus Torvalds } compat_siginfo_t;
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds /*
941da177e4SLinus Torvalds  * Including <asm/unistd.h> would give use the 64-bit syscall numbers ...
951da177e4SLinus Torvalds  */
961da177e4SLinus Torvalds #define __NR_O32_sigreturn		4119
971da177e4SLinus Torvalds #define __NR_O32_rt_sigreturn		4193
981da177e4SLinus Torvalds #define __NR_O32_restart_syscall	4253
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds #define DEBUG_SIG 0
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds #define _BLOCKABLE (~(sigmask(SIGKILL) | sigmask(SIGSTOP)))
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds extern int do_signal32(sigset_t *oldset, struct pt_regs *regs);
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds /* 32-bit compatibility types */
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds #define _NSIG_BPW32	32
1091da177e4SLinus Torvalds #define _NSIG_WORDS32	(_NSIG / _NSIG_BPW32)
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds typedef struct {
1121da177e4SLinus Torvalds 	unsigned int sig[_NSIG_WORDS32];
1131da177e4SLinus Torvalds } sigset_t32;
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds typedef unsigned int __sighandler32_t;
1161da177e4SLinus Torvalds typedef void (*vfptr_t)(void);
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds struct sigaction32 {
1191da177e4SLinus Torvalds 	unsigned int		sa_flags;
1201da177e4SLinus Torvalds 	__sighandler32_t	sa_handler;
1211da177e4SLinus Torvalds 	compat_sigset_t		sa_mask;
1221da177e4SLinus Torvalds };
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /* IRIX compatible stack_t  */
1251da177e4SLinus Torvalds typedef struct sigaltstack32 {
1261da177e4SLinus Torvalds 	s32 ss_sp;
1271da177e4SLinus Torvalds 	compat_size_t ss_size;
1281da177e4SLinus Torvalds 	int ss_flags;
1291da177e4SLinus Torvalds } stack32_t;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds struct ucontext32 {
1321da177e4SLinus Torvalds 	u32                 uc_flags;
1331da177e4SLinus Torvalds 	s32                 uc_link;
1341da177e4SLinus Torvalds 	stack32_t           uc_stack;
1351da177e4SLinus Torvalds 	struct sigcontext32 uc_mcontext;
1361da177e4SLinus Torvalds 	sigset_t32          uc_sigmask;   /* mask last for extensibility */
1371da177e4SLinus Torvalds };
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds extern void __put_sigset_unknown_nsig(void);
1401da177e4SLinus Torvalds extern void __get_sigset_unknown_nsig(void);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds static inline int put_sigset(const sigset_t *kbuf, compat_sigset_t *ubuf)
1431da177e4SLinus Torvalds {
1441da177e4SLinus Torvalds 	int err = 0;
1451da177e4SLinus Torvalds 
1461da177e4SLinus Torvalds 	if (!access_ok(VERIFY_WRITE, ubuf, sizeof(*ubuf)))
1471da177e4SLinus Torvalds 		return -EFAULT;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 	switch (_NSIG_WORDS) {
1501da177e4SLinus Torvalds 	default:
1511da177e4SLinus Torvalds 		__put_sigset_unknown_nsig();
1521da177e4SLinus Torvalds 	case 2:
1531da177e4SLinus Torvalds 		err |= __put_user (kbuf->sig[1] >> 32, &ubuf->sig[3]);
1541da177e4SLinus Torvalds 		err |= __put_user (kbuf->sig[1] & 0xffffffff, &ubuf->sig[2]);
1551da177e4SLinus Torvalds 	case 1:
1561da177e4SLinus Torvalds 		err |= __put_user (kbuf->sig[0] >> 32, &ubuf->sig[1]);
1571da177e4SLinus Torvalds 		err |= __put_user (kbuf->sig[0] & 0xffffffff, &ubuf->sig[0]);
1581da177e4SLinus Torvalds 	}
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	return err;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds static inline int get_sigset(sigset_t *kbuf, const compat_sigset_t *ubuf)
1641da177e4SLinus Torvalds {
1651da177e4SLinus Torvalds 	int err = 0;
1661da177e4SLinus Torvalds 	unsigned long sig[4];
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, ubuf, sizeof(*ubuf)))
1691da177e4SLinus Torvalds 		return -EFAULT;
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	switch (_NSIG_WORDS) {
1721da177e4SLinus Torvalds 	default:
1731da177e4SLinus Torvalds 		__get_sigset_unknown_nsig();
1741da177e4SLinus Torvalds 	case 2:
1751da177e4SLinus Torvalds 		err |= __get_user (sig[3], &ubuf->sig[3]);
1761da177e4SLinus Torvalds 		err |= __get_user (sig[2], &ubuf->sig[2]);
1771da177e4SLinus Torvalds 		kbuf->sig[1] = sig[2] | (sig[3] << 32);
1781da177e4SLinus Torvalds 	case 1:
1791da177e4SLinus Torvalds 		err |= __get_user (sig[1], &ubuf->sig[1]);
1801da177e4SLinus Torvalds 		err |= __get_user (sig[0], &ubuf->sig[0]);
1811da177e4SLinus Torvalds 		kbuf->sig[0] = sig[0] | (sig[1] << 32);
1821da177e4SLinus Torvalds 	}
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds 	return err;
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds /*
1881da177e4SLinus Torvalds  * Atomically swap in the new signal mask, and wait for a signal.
1891da177e4SLinus Torvalds  */
1901da177e4SLinus Torvalds 
1911da177e4SLinus Torvalds save_static_function(sys32_sigsuspend);
1921da177e4SLinus Torvalds __attribute_used__ noinline static int
1931da177e4SLinus Torvalds _sys32_sigsuspend(nabi_no_regargs struct pt_regs regs)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	compat_sigset_t *uset;
1961da177e4SLinus Torvalds 	sigset_t newset, saveset;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	uset = (compat_sigset_t *) regs.regs[4];
1991da177e4SLinus Torvalds 	if (get_sigset(&newset, uset))
2001da177e4SLinus Torvalds 		return -EFAULT;
2011da177e4SLinus Torvalds 	sigdelsetmask(&newset, ~_BLOCKABLE);
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 	spin_lock_irq(&current->sighand->siglock);
2041da177e4SLinus Torvalds 	saveset = current->blocked;
2051da177e4SLinus Torvalds 	current->blocked = newset;
2061da177e4SLinus Torvalds 	recalc_sigpending();
2071da177e4SLinus Torvalds 	spin_unlock_irq(&current->sighand->siglock);
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	regs.regs[2] = EINTR;
2101da177e4SLinus Torvalds 	regs.regs[7] = 1;
2111da177e4SLinus Torvalds 	while (1) {
2121da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
2131da177e4SLinus Torvalds 		schedule();
2141da177e4SLinus Torvalds 		if (do_signal32(&saveset, &regs))
2151da177e4SLinus Torvalds 			return -EINTR;
2161da177e4SLinus Torvalds 	}
2171da177e4SLinus Torvalds }
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds save_static_function(sys32_rt_sigsuspend);
2201da177e4SLinus Torvalds __attribute_used__ noinline static int
2211da177e4SLinus Torvalds _sys32_rt_sigsuspend(nabi_no_regargs struct pt_regs regs)
2221da177e4SLinus Torvalds {
2231da177e4SLinus Torvalds 	compat_sigset_t *uset;
2241da177e4SLinus Torvalds 	sigset_t newset, saveset;
2251da177e4SLinus Torvalds         size_t sigsetsize;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	/* XXX Don't preclude handling different sized sigset_t's.  */
2281da177e4SLinus Torvalds 	sigsetsize = regs.regs[5];
2291da177e4SLinus Torvalds 	if (sigsetsize != sizeof(compat_sigset_t))
2301da177e4SLinus Torvalds 		return -EINVAL;
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	uset = (compat_sigset_t *) regs.regs[4];
2331da177e4SLinus Torvalds 	if (get_sigset(&newset, uset))
2341da177e4SLinus Torvalds 		return -EFAULT;
2351da177e4SLinus Torvalds 	sigdelsetmask(&newset, ~_BLOCKABLE);
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	spin_lock_irq(&current->sighand->siglock);
2381da177e4SLinus Torvalds 	saveset = current->blocked;
2391da177e4SLinus Torvalds 	current->blocked = newset;
2401da177e4SLinus Torvalds         recalc_sigpending();
2411da177e4SLinus Torvalds 	spin_unlock_irq(&current->sighand->siglock);
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds 	regs.regs[2] = EINTR;
2441da177e4SLinus Torvalds 	regs.regs[7] = 1;
2451da177e4SLinus Torvalds 	while (1) {
2461da177e4SLinus Torvalds 		current->state = TASK_INTERRUPTIBLE;
2471da177e4SLinus Torvalds 		schedule();
2481da177e4SLinus Torvalds 		if (do_signal32(&saveset, &regs))
2491da177e4SLinus Torvalds 			return -EINTR;
2501da177e4SLinus Torvalds 	}
2511da177e4SLinus Torvalds }
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds asmlinkage int sys32_sigaction(int sig, const struct sigaction32 *act,
2541da177e4SLinus Torvalds                                struct sigaction32 *oact)
2551da177e4SLinus Torvalds {
2561da177e4SLinus Torvalds 	struct k_sigaction new_ka, old_ka;
2571da177e4SLinus Torvalds 	int ret;
2581da177e4SLinus Torvalds 	int err = 0;
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds 	if (act) {
2611da177e4SLinus Torvalds 		old_sigset_t mask;
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 		if (!access_ok(VERIFY_READ, act, sizeof(*act)))
2641da177e4SLinus Torvalds 			return -EFAULT;
2651da177e4SLinus Torvalds 		err |= __get_user((u32)(u64)new_ka.sa.sa_handler,
2661da177e4SLinus Torvalds 		                  &act->sa_handler);
2671da177e4SLinus Torvalds 		err |= __get_user(new_ka.sa.sa_flags, &act->sa_flags);
2681da177e4SLinus Torvalds 		err |= __get_user(mask, &act->sa_mask.sig[0]);
2691da177e4SLinus Torvalds 		if (err)
2701da177e4SLinus Torvalds 			return -EFAULT;
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 		siginitset(&new_ka.sa.sa_mask, mask);
2731da177e4SLinus Torvalds 	}
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds 	ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	if (!ret && oact) {
2781da177e4SLinus Torvalds 		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
2791da177e4SLinus Torvalds                         return -EFAULT;
2801da177e4SLinus Torvalds 		err |= __put_user(old_ka.sa.sa_flags, &oact->sa_flags);
2811da177e4SLinus Torvalds 		err |= __put_user((u32)(u64)old_ka.sa.sa_handler,
2821da177e4SLinus Torvalds 		                  &oact->sa_handler);
2831da177e4SLinus Torvalds 		err |= __put_user(old_ka.sa.sa_mask.sig[0], oact->sa_mask.sig);
2841da177e4SLinus Torvalds                 err |= __put_user(0, &oact->sa_mask.sig[1]);
2851da177e4SLinus Torvalds                 err |= __put_user(0, &oact->sa_mask.sig[2]);
2861da177e4SLinus Torvalds                 err |= __put_user(0, &oact->sa_mask.sig[3]);
2871da177e4SLinus Torvalds                 if (err)
2881da177e4SLinus Torvalds 			return -EFAULT;
2891da177e4SLinus Torvalds 	}
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	return ret;
2921da177e4SLinus Torvalds }
2931da177e4SLinus Torvalds 
2941da177e4SLinus Torvalds asmlinkage int sys32_sigaltstack(nabi_no_regargs struct pt_regs regs)
2951da177e4SLinus Torvalds {
2961da177e4SLinus Torvalds 	const stack32_t *uss = (const stack32_t *) regs.regs[4];
2971da177e4SLinus Torvalds 	stack32_t *uoss = (stack32_t *) regs.regs[5];
2981da177e4SLinus Torvalds 	unsigned long usp = regs.regs[29];
2991da177e4SLinus Torvalds 	stack_t kss, koss;
3001da177e4SLinus Torvalds 	int ret, err = 0;
3011da177e4SLinus Torvalds 	mm_segment_t old_fs = get_fs();
3021da177e4SLinus Torvalds 	s32 sp;
3031da177e4SLinus Torvalds 
3041da177e4SLinus Torvalds 	if (uss) {
3051da177e4SLinus Torvalds 		if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
3061da177e4SLinus Torvalds 			return -EFAULT;
3071da177e4SLinus Torvalds 		err |= __get_user(sp, &uss->ss_sp);
3081da177e4SLinus Torvalds 		kss.ss_sp = (void *) (long) sp;
3091da177e4SLinus Torvalds 		err |= __get_user(kss.ss_size, &uss->ss_size);
3101da177e4SLinus Torvalds 		err |= __get_user(kss.ss_flags, &uss->ss_flags);
3111da177e4SLinus Torvalds 		if (err)
3121da177e4SLinus Torvalds 			return -EFAULT;
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	set_fs (KERNEL_DS);
3161da177e4SLinus Torvalds 	ret = do_sigaltstack(uss ? &kss : NULL , uoss ? &koss : NULL, usp);
3171da177e4SLinus Torvalds 	set_fs (old_fs);
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	if (!ret && uoss) {
3201da177e4SLinus Torvalds 		if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
3211da177e4SLinus Torvalds 			return -EFAULT;
3221da177e4SLinus Torvalds 		sp = (int) (long) koss.ss_sp;
3231da177e4SLinus Torvalds 		err |= __put_user(sp, &uoss->ss_sp);
3241da177e4SLinus Torvalds 		err |= __put_user(koss.ss_size, &uoss->ss_size);
3251da177e4SLinus Torvalds 		err |= __put_user(koss.ss_flags, &uoss->ss_flags);
3261da177e4SLinus Torvalds 		if (err)
3271da177e4SLinus Torvalds 			return -EFAULT;
3281da177e4SLinus Torvalds 	}
3291da177e4SLinus Torvalds 	return ret;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds static int restore_sigcontext32(struct pt_regs *regs, struct sigcontext32 *sc)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	int err = 0;
3351da177e4SLinus Torvalds 	__u32 used_math;
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	/* Always make any pending restarted system calls return -EINTR */
3381da177e4SLinus Torvalds 	current_thread_info()->restart_block.fn = do_no_restart_syscall;
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds 	err |= __get_user(regs->cp0_epc, &sc->sc_pc);
3411da177e4SLinus Torvalds 	err |= __get_user(regs->hi, &sc->sc_mdhi);
3421da177e4SLinus Torvalds 	err |= __get_user(regs->lo, &sc->sc_mdlo);
3431da177e4SLinus Torvalds 
3441da177e4SLinus Torvalds #define restore_gp_reg(i) do {						\
3451da177e4SLinus Torvalds 	err |= __get_user(regs->regs[i], &sc->sc_regs[i]);		\
3461da177e4SLinus Torvalds } while(0)
3471da177e4SLinus Torvalds 	restore_gp_reg( 1); restore_gp_reg( 2); restore_gp_reg( 3);
3481da177e4SLinus Torvalds 	restore_gp_reg( 4); restore_gp_reg( 5); restore_gp_reg( 6);
3491da177e4SLinus Torvalds 	restore_gp_reg( 7); restore_gp_reg( 8); restore_gp_reg( 9);
3501da177e4SLinus Torvalds 	restore_gp_reg(10); restore_gp_reg(11); restore_gp_reg(12);
3511da177e4SLinus Torvalds 	restore_gp_reg(13); restore_gp_reg(14); restore_gp_reg(15);
3521da177e4SLinus Torvalds 	restore_gp_reg(16); restore_gp_reg(17); restore_gp_reg(18);
3531da177e4SLinus Torvalds 	restore_gp_reg(19); restore_gp_reg(20); restore_gp_reg(21);
3541da177e4SLinus Torvalds 	restore_gp_reg(22); restore_gp_reg(23); restore_gp_reg(24);
3551da177e4SLinus Torvalds 	restore_gp_reg(25); restore_gp_reg(26); restore_gp_reg(27);
3561da177e4SLinus Torvalds 	restore_gp_reg(28); restore_gp_reg(29); restore_gp_reg(30);
3571da177e4SLinus Torvalds 	restore_gp_reg(31);
3581da177e4SLinus Torvalds #undef restore_gp_reg
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	err |= __get_user(used_math, &sc->sc_used_math);
3611da177e4SLinus Torvalds 	conditional_used_math(used_math);
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	preempt_disable();
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	if (used_math()) {
3661da177e4SLinus Torvalds 		/* restore fpu context if we have used it before */
3671da177e4SLinus Torvalds 		own_fpu();
3681da177e4SLinus Torvalds 		err |= restore_fp_context32(sc);
3691da177e4SLinus Torvalds 	} else {
3701da177e4SLinus Torvalds 		/* signal handler may have used FPU.  Give it up. */
3711da177e4SLinus Torvalds 		lose_fpu();
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	preempt_enable();
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	return err;
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds struct sigframe {
3801da177e4SLinus Torvalds 	u32 sf_ass[4];			/* argument save space for o32 */
3811da177e4SLinus Torvalds 	u32 sf_code[2];			/* signal trampoline */
3821da177e4SLinus Torvalds 	struct sigcontext32 sf_sc;
3831da177e4SLinus Torvalds 	sigset_t sf_mask;
3841da177e4SLinus Torvalds };
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds struct rt_sigframe32 {
3871da177e4SLinus Torvalds 	u32 rs_ass[4];			/* argument save space for o32 */
3881da177e4SLinus Torvalds 	u32 rs_code[2];			/* signal trampoline */
3891da177e4SLinus Torvalds 	compat_siginfo_t rs_info;
3901da177e4SLinus Torvalds 	struct ucontext32 rs_uc;
3911da177e4SLinus Torvalds };
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds int copy_siginfo_to_user32(compat_siginfo_t *to, siginfo_t *from)
3941da177e4SLinus Torvalds {
3951da177e4SLinus Torvalds 	int err;
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds 	if (!access_ok (VERIFY_WRITE, to, sizeof(compat_siginfo_t)))
3981da177e4SLinus Torvalds 		return -EFAULT;
3991da177e4SLinus Torvalds 
4001da177e4SLinus Torvalds 	/* If you change siginfo_t structure, please be sure
4011da177e4SLinus Torvalds 	   this code is fixed accordingly.
4021da177e4SLinus Torvalds 	   It should never copy any pad contained in the structure
4031da177e4SLinus Torvalds 	   to avoid security leaks, but must copy the generic
4041da177e4SLinus Torvalds 	   3 ints plus the relevant union member.
4051da177e4SLinus Torvalds 	   This routine must convert siginfo from 64bit to 32bit as well
4061da177e4SLinus Torvalds 	   at the same time.  */
4071da177e4SLinus Torvalds 	err = __put_user(from->si_signo, &to->si_signo);
4081da177e4SLinus Torvalds 	err |= __put_user(from->si_errno, &to->si_errno);
4091da177e4SLinus Torvalds 	err |= __put_user((short)from->si_code, &to->si_code);
4101da177e4SLinus Torvalds 	if (from->si_code < 0)
4111da177e4SLinus Torvalds 		err |= __copy_to_user(&to->_sifields._pad, &from->_sifields._pad, SI_PAD_SIZE);
4121da177e4SLinus Torvalds 	else {
4131da177e4SLinus Torvalds 		switch (from->si_code >> 16) {
4141da177e4SLinus Torvalds 		case __SI_CHLD >> 16:
4151da177e4SLinus Torvalds 			err |= __put_user(from->si_utime, &to->si_utime);
4161da177e4SLinus Torvalds 			err |= __put_user(from->si_stime, &to->si_stime);
4171da177e4SLinus Torvalds 			err |= __put_user(from->si_status, &to->si_status);
4181da177e4SLinus Torvalds 		default:
4191da177e4SLinus Torvalds 			err |= __put_user(from->si_pid, &to->si_pid);
4201da177e4SLinus Torvalds 			err |= __put_user(from->si_uid, &to->si_uid);
4211da177e4SLinus Torvalds 			break;
4221da177e4SLinus Torvalds 		case __SI_FAULT >> 16:
4231da177e4SLinus Torvalds 			err |= __put_user((long)from->si_addr, &to->si_addr);
4241da177e4SLinus Torvalds 			break;
4251da177e4SLinus Torvalds 		case __SI_POLL >> 16:
4261da177e4SLinus Torvalds 			err |= __put_user(from->si_band, &to->si_band);
4271da177e4SLinus Torvalds 			err |= __put_user(from->si_fd, &to->si_fd);
4281da177e4SLinus Torvalds 			break;
4291da177e4SLinus Torvalds 		case __SI_RT >> 16: /* This is not generated by the kernel as of now.  */
4301da177e4SLinus Torvalds 		case __SI_MESGQ >> 16:
4311da177e4SLinus Torvalds 			err |= __put_user(from->si_pid, &to->si_pid);
4321da177e4SLinus Torvalds 			err |= __put_user(from->si_uid, &to->si_uid);
4331da177e4SLinus Torvalds 			err |= __put_user(from->si_int, &to->si_int);
4341da177e4SLinus Torvalds 			break;
4351da177e4SLinus Torvalds 		}
4361da177e4SLinus Torvalds 	}
4371da177e4SLinus Torvalds 	return err;
4381da177e4SLinus Torvalds }
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds save_static_function(sys32_sigreturn);
4411da177e4SLinus Torvalds __attribute_used__ noinline static void
4421da177e4SLinus Torvalds _sys32_sigreturn(nabi_no_regargs struct pt_regs regs)
4431da177e4SLinus Torvalds {
4441da177e4SLinus Torvalds 	struct sigframe *frame;
4451da177e4SLinus Torvalds 	sigset_t blocked;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	frame = (struct sigframe *) regs.regs[29];
4481da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
4491da177e4SLinus Torvalds 		goto badframe;
4501da177e4SLinus Torvalds 	if (__copy_from_user(&blocked, &frame->sf_mask, sizeof(blocked)))
4511da177e4SLinus Torvalds 		goto badframe;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	sigdelsetmask(&blocked, ~_BLOCKABLE);
4541da177e4SLinus Torvalds 	spin_lock_irq(&current->sighand->siglock);
4551da177e4SLinus Torvalds 	current->blocked = blocked;
4561da177e4SLinus Torvalds 	recalc_sigpending();
4571da177e4SLinus Torvalds 	spin_unlock_irq(&current->sighand->siglock);
4581da177e4SLinus Torvalds 
4591da177e4SLinus Torvalds 	if (restore_sigcontext32(&regs, &frame->sf_sc))
4601da177e4SLinus Torvalds 		goto badframe;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	/*
4631da177e4SLinus Torvalds 	 * Don't let your children do this ...
4641da177e4SLinus Torvalds 	 */
4651da177e4SLinus Torvalds 	if (current_thread_info()->flags & TIF_SYSCALL_TRACE)
4661da177e4SLinus Torvalds 		do_syscall_trace(&regs, 1);
4671da177e4SLinus Torvalds 	__asm__ __volatile__(
4681da177e4SLinus Torvalds 		"move\t$29, %0\n\t"
4691da177e4SLinus Torvalds 		"j\tsyscall_exit"
4701da177e4SLinus Torvalds 		:/* no outputs */
4711da177e4SLinus Torvalds 		:"r" (&regs));
4721da177e4SLinus Torvalds 	/* Unreached */
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds badframe:
4751da177e4SLinus Torvalds 	force_sig(SIGSEGV, current);
4761da177e4SLinus Torvalds }
4771da177e4SLinus Torvalds 
4781da177e4SLinus Torvalds save_static_function(sys32_rt_sigreturn);
4791da177e4SLinus Torvalds __attribute_used__ noinline static void
4801da177e4SLinus Torvalds _sys32_rt_sigreturn(nabi_no_regargs struct pt_regs regs)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	struct rt_sigframe32 *frame;
4831da177e4SLinus Torvalds 	sigset_t set;
4841da177e4SLinus Torvalds 	stack_t st;
4851da177e4SLinus Torvalds 	s32 sp;
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	frame = (struct rt_sigframe32 *) regs.regs[29];
4881da177e4SLinus Torvalds 	if (!access_ok(VERIFY_READ, frame, sizeof(*frame)))
4891da177e4SLinus Torvalds 		goto badframe;
4901da177e4SLinus Torvalds 	if (__copy_from_user(&set, &frame->rs_uc.uc_sigmask, sizeof(set)))
4911da177e4SLinus Torvalds 		goto badframe;
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	sigdelsetmask(&set, ~_BLOCKABLE);
4941da177e4SLinus Torvalds 	spin_lock_irq(&current->sighand->siglock);
4951da177e4SLinus Torvalds 	current->blocked = set;
4961da177e4SLinus Torvalds 	recalc_sigpending();
4971da177e4SLinus Torvalds 	spin_unlock_irq(&current->sighand->siglock);
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	if (restore_sigcontext32(&regs, &frame->rs_uc.uc_mcontext))
5001da177e4SLinus Torvalds 		goto badframe;
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds 	/* The ucontext contains a stack32_t, so we must convert!  */
5031da177e4SLinus Torvalds 	if (__get_user(sp, &frame->rs_uc.uc_stack.ss_sp))
5041da177e4SLinus Torvalds 		goto badframe;
5051da177e4SLinus Torvalds 	st.ss_size = (long) sp;
5061da177e4SLinus Torvalds 	if (__get_user(st.ss_size, &frame->rs_uc.uc_stack.ss_size))
5071da177e4SLinus Torvalds 		goto badframe;
5081da177e4SLinus Torvalds 	if (__get_user(st.ss_flags, &frame->rs_uc.uc_stack.ss_flags))
5091da177e4SLinus Torvalds 		goto badframe;
5101da177e4SLinus Torvalds 
5111da177e4SLinus Torvalds 	/* It is more difficult to avoid calling this function than to
5121da177e4SLinus Torvalds 	   call it and ignore errors.  */
5131da177e4SLinus Torvalds 	do_sigaltstack(&st, NULL, regs.regs[29]);
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	/*
5161da177e4SLinus Torvalds 	 * Don't let your children do this ...
5171da177e4SLinus Torvalds 	 */
5181da177e4SLinus Torvalds 	__asm__ __volatile__(
5191da177e4SLinus Torvalds 		"move\t$29, %0\n\t"
5201da177e4SLinus Torvalds 		"j\tsyscall_exit"
5211da177e4SLinus Torvalds 		:/* no outputs */
5221da177e4SLinus Torvalds 		:"r" (&regs));
5231da177e4SLinus Torvalds 	/* Unreached */
5241da177e4SLinus Torvalds 
5251da177e4SLinus Torvalds badframe:
5261da177e4SLinus Torvalds 	force_sig(SIGSEGV, current);
5271da177e4SLinus Torvalds }
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds static inline int setup_sigcontext32(struct pt_regs *regs,
5301da177e4SLinus Torvalds 				     struct sigcontext32 *sc)
5311da177e4SLinus Torvalds {
5321da177e4SLinus Torvalds 	int err = 0;
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	err |= __put_user(regs->cp0_epc, &sc->sc_pc);
5351da177e4SLinus Torvalds 	err |= __put_user(regs->cp0_status, &sc->sc_status);
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds #define save_gp_reg(i) {						\
5381da177e4SLinus Torvalds 	err |= __put_user(regs->regs[i], &sc->sc_regs[i]);		\
5391da177e4SLinus Torvalds } while(0)
5401da177e4SLinus Torvalds 	__put_user(0, &sc->sc_regs[0]); save_gp_reg(1); save_gp_reg(2);
5411da177e4SLinus Torvalds 	save_gp_reg(3); save_gp_reg(4); save_gp_reg(5); save_gp_reg(6);
5421da177e4SLinus Torvalds 	save_gp_reg(7); save_gp_reg(8); save_gp_reg(9); save_gp_reg(10);
5431da177e4SLinus Torvalds 	save_gp_reg(11); save_gp_reg(12); save_gp_reg(13); save_gp_reg(14);
5441da177e4SLinus Torvalds 	save_gp_reg(15); save_gp_reg(16); save_gp_reg(17); save_gp_reg(18);
5451da177e4SLinus Torvalds 	save_gp_reg(19); save_gp_reg(20); save_gp_reg(21); save_gp_reg(22);
5461da177e4SLinus Torvalds 	save_gp_reg(23); save_gp_reg(24); save_gp_reg(25); save_gp_reg(26);
5471da177e4SLinus Torvalds 	save_gp_reg(27); save_gp_reg(28); save_gp_reg(29); save_gp_reg(30);
5481da177e4SLinus Torvalds 	save_gp_reg(31);
5491da177e4SLinus Torvalds #undef save_gp_reg
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	err |= __put_user(regs->hi, &sc->sc_mdhi);
5521da177e4SLinus Torvalds 	err |= __put_user(regs->lo, &sc->sc_mdlo);
5531da177e4SLinus Torvalds 	err |= __put_user(regs->cp0_cause, &sc->sc_cause);
5541da177e4SLinus Torvalds 	err |= __put_user(regs->cp0_badvaddr, &sc->sc_badvaddr);
5551da177e4SLinus Torvalds 
5561da177e4SLinus Torvalds 	err |= __put_user(!!used_math(), &sc->sc_used_math);
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 	if (!used_math())
5591da177e4SLinus Torvalds 		goto out;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	/*
5621da177e4SLinus Torvalds 	 * Save FPU state to signal context.  Signal handler will "inherit"
5631da177e4SLinus Torvalds 	 * current FPU state.
5641da177e4SLinus Torvalds 	 */
5651da177e4SLinus Torvalds 	preempt_disable();
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds 	if (!is_fpu_owner()) {
5681da177e4SLinus Torvalds 		own_fpu();
5691da177e4SLinus Torvalds 		restore_fp(current);
5701da177e4SLinus Torvalds 	}
5711da177e4SLinus Torvalds 	err |= save_fp_context32(sc);
5721da177e4SLinus Torvalds 
5731da177e4SLinus Torvalds 	preempt_enable();
5741da177e4SLinus Torvalds 
5751da177e4SLinus Torvalds out:
5761da177e4SLinus Torvalds 	return err;
5771da177e4SLinus Torvalds }
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds /*
5801da177e4SLinus Torvalds  * Determine which stack to use..
5811da177e4SLinus Torvalds  */
5821da177e4SLinus Torvalds static inline void *get_sigframe(struct k_sigaction *ka, struct pt_regs *regs,
5831da177e4SLinus Torvalds 				 size_t frame_size)
5841da177e4SLinus Torvalds {
5851da177e4SLinus Torvalds 	unsigned long sp;
5861da177e4SLinus Torvalds 
5871da177e4SLinus Torvalds 	/* Default to using normal stack */
5881da177e4SLinus Torvalds 	sp = regs->regs[29];
5891da177e4SLinus Torvalds 
5901da177e4SLinus Torvalds 	/*
5911da177e4SLinus Torvalds  	 * FPU emulator may have it's own trampoline active just
5921da177e4SLinus Torvalds  	 * above the user stack, 16-bytes before the next lowest
5931da177e4SLinus Torvalds  	 * 16 byte boundary.  Try to avoid trashing it.
5941da177e4SLinus Torvalds  	 */
5951da177e4SLinus Torvalds  	sp -= 32;
5961da177e4SLinus Torvalds 
5971da177e4SLinus Torvalds 	/* This is the X/Open sanctioned signal stack switching.  */
5981da177e4SLinus Torvalds 	if ((ka->sa.sa_flags & SA_ONSTACK) && (sas_ss_flags (sp) == 0))
5991da177e4SLinus Torvalds 		sp = current->sas_ss_sp + current->sas_ss_size;
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 	return (void *)((sp - frame_size) & ALMASK);
6021da177e4SLinus Torvalds }
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds static inline void setup_frame(struct k_sigaction * ka, struct pt_regs *regs,
6051da177e4SLinus Torvalds 			       int signr, sigset_t *set)
6061da177e4SLinus Torvalds {
6071da177e4SLinus Torvalds 	struct sigframe *frame;
6081da177e4SLinus Torvalds 	int err = 0;
6091da177e4SLinus Torvalds 
6101da177e4SLinus Torvalds 	frame = get_sigframe(ka, regs, sizeof(*frame));
6111da177e4SLinus Torvalds 	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
6121da177e4SLinus Torvalds 		goto give_sigsegv;
6131da177e4SLinus Torvalds 
6141da177e4SLinus Torvalds 	/*
6151da177e4SLinus Torvalds 	 * Set up the return code ...
6161da177e4SLinus Torvalds 	 *
6171da177e4SLinus Torvalds 	 *         li      v0, __NR_O32_sigreturn
6181da177e4SLinus Torvalds 	 *         syscall
6191da177e4SLinus Torvalds 	 */
6201da177e4SLinus Torvalds 	err |= __put_user(0x24020000 + __NR_O32_sigreturn, frame->sf_code + 0);
6211da177e4SLinus Torvalds 	err |= __put_user(0x0000000c                     , frame->sf_code + 1);
6221da177e4SLinus Torvalds 	flush_cache_sigtramp((unsigned long) frame->sf_code);
6231da177e4SLinus Torvalds 
6241da177e4SLinus Torvalds 	err |= setup_sigcontext32(regs, &frame->sf_sc);
6251da177e4SLinus Torvalds 	err |= __copy_to_user(&frame->sf_mask, set, sizeof(*set));
6261da177e4SLinus Torvalds 	if (err)
6271da177e4SLinus Torvalds 		goto give_sigsegv;
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds 	/*
6301da177e4SLinus Torvalds 	 * Arguments to signal handler:
6311da177e4SLinus Torvalds 	 *
6321da177e4SLinus Torvalds 	 *   a0 = signal number
6331da177e4SLinus Torvalds 	 *   a1 = 0 (should be cause)
6341da177e4SLinus Torvalds 	 *   a2 = pointer to struct sigcontext
6351da177e4SLinus Torvalds 	 *
6361da177e4SLinus Torvalds 	 * $25 and c0_epc point to the signal handler, $29 points to the
6371da177e4SLinus Torvalds 	 * struct sigframe.
6381da177e4SLinus Torvalds 	 */
6391da177e4SLinus Torvalds 	regs->regs[ 4] = signr;
6401da177e4SLinus Torvalds 	regs->regs[ 5] = 0;
6411da177e4SLinus Torvalds 	regs->regs[ 6] = (unsigned long) &frame->sf_sc;
6421da177e4SLinus Torvalds 	regs->regs[29] = (unsigned long) frame;
6431da177e4SLinus Torvalds 	regs->regs[31] = (unsigned long) frame->sf_code;
6441da177e4SLinus Torvalds 	regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
6451da177e4SLinus Torvalds 
6461da177e4SLinus Torvalds #if DEBUG_SIG
6471da177e4SLinus Torvalds 	printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
6481da177e4SLinus Torvalds 	       current->comm, current->pid,
6491da177e4SLinus Torvalds 	       frame, regs->cp0_epc, frame->sf_code);
6501da177e4SLinus Torvalds #endif
6511da177e4SLinus Torvalds         return;
6521da177e4SLinus Torvalds 
6531da177e4SLinus Torvalds give_sigsegv:
6541da177e4SLinus Torvalds 	force_sigsegv(signr, current);
6551da177e4SLinus Torvalds }
6561da177e4SLinus Torvalds 
6571da177e4SLinus Torvalds static inline void setup_rt_frame(struct k_sigaction * ka,
6581da177e4SLinus Torvalds 				  struct pt_regs *regs, int signr,
6591da177e4SLinus Torvalds 				  sigset_t *set, siginfo_t *info)
6601da177e4SLinus Torvalds {
6611da177e4SLinus Torvalds 	struct rt_sigframe32 *frame;
6621da177e4SLinus Torvalds 	int err = 0;
6631da177e4SLinus Torvalds 	s32 sp;
6641da177e4SLinus Torvalds 
6651da177e4SLinus Torvalds 	frame = get_sigframe(ka, regs, sizeof(*frame));
6661da177e4SLinus Torvalds 	if (!access_ok(VERIFY_WRITE, frame, sizeof (*frame)))
6671da177e4SLinus Torvalds 		goto give_sigsegv;
6681da177e4SLinus Torvalds 
6691da177e4SLinus Torvalds 	/* Set up to return from userspace.  If provided, use a stub already
6701da177e4SLinus Torvalds 	   in userspace.  */
6711da177e4SLinus Torvalds 	/*
6721da177e4SLinus Torvalds 	 * Set up the return code ...
6731da177e4SLinus Torvalds 	 *
6741da177e4SLinus Torvalds 	 *         li      v0, __NR_O32_rt_sigreturn
6751da177e4SLinus Torvalds 	 *         syscall
6761da177e4SLinus Torvalds 	 */
6771da177e4SLinus Torvalds 	err |= __put_user(0x24020000 + __NR_O32_rt_sigreturn, frame->rs_code + 0);
6781da177e4SLinus Torvalds 	err |= __put_user(0x0000000c                      , frame->rs_code + 1);
6791da177e4SLinus Torvalds 	flush_cache_sigtramp((unsigned long) frame->rs_code);
6801da177e4SLinus Torvalds 
6811da177e4SLinus Torvalds 	/* Convert (siginfo_t -> compat_siginfo_t) and copy to user. */
6821da177e4SLinus Torvalds 	err |= copy_siginfo_to_user32(&frame->rs_info, info);
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	/* Create the ucontext.  */
6851da177e4SLinus Torvalds 	err |= __put_user(0, &frame->rs_uc.uc_flags);
6861da177e4SLinus Torvalds 	err |= __put_user(0, &frame->rs_uc.uc_link);
6871da177e4SLinus Torvalds 	sp = (int) (long) current->sas_ss_sp;
6881da177e4SLinus Torvalds 	err |= __put_user(sp,
6891da177e4SLinus Torvalds 	                  &frame->rs_uc.uc_stack.ss_sp);
6901da177e4SLinus Torvalds 	err |= __put_user(sas_ss_flags(regs->regs[29]),
6911da177e4SLinus Torvalds 	                  &frame->rs_uc.uc_stack.ss_flags);
6921da177e4SLinus Torvalds 	err |= __put_user(current->sas_ss_size,
6931da177e4SLinus Torvalds 	                  &frame->rs_uc.uc_stack.ss_size);
6941da177e4SLinus Torvalds 	err |= setup_sigcontext32(regs, &frame->rs_uc.uc_mcontext);
6951da177e4SLinus Torvalds 	err |= __copy_to_user(&frame->rs_uc.uc_sigmask, set, sizeof(*set));
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds 	if (err)
6981da177e4SLinus Torvalds 		goto give_sigsegv;
6991da177e4SLinus Torvalds 
7001da177e4SLinus Torvalds 	/*
7011da177e4SLinus Torvalds 	 * Arguments to signal handler:
7021da177e4SLinus Torvalds 	 *
7031da177e4SLinus Torvalds 	 *   a0 = signal number
7041da177e4SLinus Torvalds 	 *   a1 = 0 (should be cause)
7051da177e4SLinus Torvalds 	 *   a2 = pointer to ucontext
7061da177e4SLinus Torvalds 	 *
7071da177e4SLinus Torvalds 	 * $25 and c0_epc point to the signal handler, $29 points to
7081da177e4SLinus Torvalds 	 * the struct rt_sigframe32.
7091da177e4SLinus Torvalds 	 */
7101da177e4SLinus Torvalds 	regs->regs[ 4] = signr;
7111da177e4SLinus Torvalds 	regs->regs[ 5] = (unsigned long) &frame->rs_info;
7121da177e4SLinus Torvalds 	regs->regs[ 6] = (unsigned long) &frame->rs_uc;
7131da177e4SLinus Torvalds 	regs->regs[29] = (unsigned long) frame;
7141da177e4SLinus Torvalds 	regs->regs[31] = (unsigned long) frame->rs_code;
7151da177e4SLinus Torvalds 	regs->cp0_epc = regs->regs[25] = (unsigned long) ka->sa.sa_handler;
7161da177e4SLinus Torvalds 
7171da177e4SLinus Torvalds #if DEBUG_SIG
7181da177e4SLinus Torvalds 	printk("SIG deliver (%s:%d): sp=0x%p pc=0x%lx ra=0x%p\n",
7191da177e4SLinus Torvalds 	       current->comm, current->pid,
7201da177e4SLinus Torvalds 	       frame, regs->cp0_epc, frame->rs_code);
7211da177e4SLinus Torvalds #endif
7221da177e4SLinus Torvalds 	return;
7231da177e4SLinus Torvalds 
7241da177e4SLinus Torvalds give_sigsegv:
7251da177e4SLinus Torvalds 	force_sigsegv(signr, current);
7261da177e4SLinus Torvalds }
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds static inline void handle_signal(unsigned long sig, siginfo_t *info,
7291da177e4SLinus Torvalds 	struct k_sigaction *ka, sigset_t *oldset, struct pt_regs * regs)
7301da177e4SLinus Torvalds {
7311da177e4SLinus Torvalds 	switch (regs->regs[0]) {
7321da177e4SLinus Torvalds 	case ERESTART_RESTARTBLOCK:
7331da177e4SLinus Torvalds 	case ERESTARTNOHAND:
7341da177e4SLinus Torvalds 		regs->regs[2] = EINTR;
7351da177e4SLinus Torvalds 		break;
7361da177e4SLinus Torvalds 	case ERESTARTSYS:
7371da177e4SLinus Torvalds 		if(!(ka->sa.sa_flags & SA_RESTART)) {
7381da177e4SLinus Torvalds 			regs->regs[2] = EINTR;
7391da177e4SLinus Torvalds 			break;
7401da177e4SLinus Torvalds 		}
7411da177e4SLinus Torvalds 	/* fallthrough */
7421da177e4SLinus Torvalds 	case ERESTARTNOINTR:		/* Userland will reload $v0.  */
7431da177e4SLinus Torvalds 		regs->regs[7] = regs->regs[26];
7441da177e4SLinus Torvalds 		regs->cp0_epc -= 8;
7451da177e4SLinus Torvalds 	}
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	regs->regs[0] = 0;		/* Don't deal with this again.  */
7481da177e4SLinus Torvalds 
7491da177e4SLinus Torvalds 	if (ka->sa.sa_flags & SA_SIGINFO)
7501da177e4SLinus Torvalds 		setup_rt_frame(ka, regs, sig, oldset, info);
7511da177e4SLinus Torvalds 	else
7521da177e4SLinus Torvalds 		setup_frame(ka, regs, sig, oldset);
7531da177e4SLinus Torvalds 
7541da177e4SLinus Torvalds 	spin_lock_irq(&current->sighand->siglock);
7551da177e4SLinus Torvalds 	sigorsets(&current->blocked,&current->blocked,&ka->sa.sa_mask);
75669be8f18SSteven Rostedt 	if (!(ka->sa.sa_flags & SA_NODEFER))
7571da177e4SLinus Torvalds 		sigaddset(&current->blocked,sig);
7581da177e4SLinus Torvalds 	recalc_sigpending();
7591da177e4SLinus Torvalds 	spin_unlock_irq(&current->sighand->siglock);
7601da177e4SLinus Torvalds }
7611da177e4SLinus Torvalds 
7621da177e4SLinus Torvalds int do_signal32(sigset_t *oldset, struct pt_regs *regs)
7631da177e4SLinus Torvalds {
7641da177e4SLinus Torvalds 	struct k_sigaction ka;
7651da177e4SLinus Torvalds 	siginfo_t info;
7661da177e4SLinus Torvalds 	int signr;
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	/*
7691da177e4SLinus Torvalds 	 * We want the common case to go fast, which is why we may in certain
7701da177e4SLinus Torvalds 	 * cases get here from kernel mode. Just return without doing anything
7711da177e4SLinus Torvalds 	 * if so.
7721da177e4SLinus Torvalds 	 */
7731da177e4SLinus Torvalds 	if (!user_mode(regs))
7741da177e4SLinus Torvalds 		return 1;
7751da177e4SLinus Torvalds 
7760e6c1f5fSNigel Cunningham 	if (try_to_freeze())
7771da177e4SLinus Torvalds 		goto no_signal;
7781da177e4SLinus Torvalds 
7791da177e4SLinus Torvalds 	if (!oldset)
7801da177e4SLinus Torvalds 		oldset = &current->blocked;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	signr = get_signal_to_deliver(&info, &ka, regs, NULL);
7831da177e4SLinus Torvalds 	if (signr > 0) {
7841da177e4SLinus Torvalds 		handle_signal(signr, &info, &ka, oldset, regs);
7851da177e4SLinus Torvalds 		return 1;
7861da177e4SLinus Torvalds 	}
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds no_signal:
7891da177e4SLinus Torvalds 	/*
7901da177e4SLinus Torvalds 	 * Who's code doesn't conform to the restartable syscall convention
7911da177e4SLinus Torvalds 	 * dies here!!!  The li instruction, a single machine instruction,
7921da177e4SLinus Torvalds 	 * must directly be followed by the syscall instruction.
7931da177e4SLinus Torvalds 	 */
7941da177e4SLinus Torvalds 	if (regs->regs[0]) {
7951da177e4SLinus Torvalds 		if (regs->regs[2] == ERESTARTNOHAND ||
7961da177e4SLinus Torvalds 		    regs->regs[2] == ERESTARTSYS ||
7971da177e4SLinus Torvalds 		    regs->regs[2] == ERESTARTNOINTR) {
7981da177e4SLinus Torvalds 			regs->regs[7] = regs->regs[26];
7991da177e4SLinus Torvalds 			regs->cp0_epc -= 8;
8001da177e4SLinus Torvalds 		}
8011da177e4SLinus Torvalds 		if (regs->regs[2] == ERESTART_RESTARTBLOCK) {
8021da177e4SLinus Torvalds 			regs->regs[2] = __NR_O32_restart_syscall;
8031da177e4SLinus Torvalds 			regs->regs[7] = regs->regs[26];
8041da177e4SLinus Torvalds 			regs->cp0_epc -= 4;
8051da177e4SLinus Torvalds 		}
8061da177e4SLinus Torvalds 	}
8071da177e4SLinus Torvalds 	return 0;
8081da177e4SLinus Torvalds }
8091da177e4SLinus Torvalds 
8101da177e4SLinus Torvalds asmlinkage int sys32_rt_sigaction(int sig, const struct sigaction32 *act,
8111da177e4SLinus Torvalds 				  struct sigaction32 *oact,
8121da177e4SLinus Torvalds 				  unsigned int sigsetsize)
8131da177e4SLinus Torvalds {
8141da177e4SLinus Torvalds 	struct k_sigaction new_sa, old_sa;
8151da177e4SLinus Torvalds 	int ret = -EINVAL;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	/* XXX: Don't preclude handling different sized sigset_t's.  */
8181da177e4SLinus Torvalds 	if (sigsetsize != sizeof(sigset_t))
8191da177e4SLinus Torvalds 		goto out;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	if (act) {
8221da177e4SLinus Torvalds 		int err = 0;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 		if (!access_ok(VERIFY_READ, act, sizeof(*act)))
8251da177e4SLinus Torvalds 			return -EFAULT;
8261da177e4SLinus Torvalds 		err |= __get_user((u32)(u64)new_sa.sa.sa_handler,
8271da177e4SLinus Torvalds 		                  &act->sa_handler);
8281da177e4SLinus Torvalds 		err |= __get_user(new_sa.sa.sa_flags, &act->sa_flags);
8291da177e4SLinus Torvalds 		err |= get_sigset(&new_sa.sa.sa_mask, &act->sa_mask);
8301da177e4SLinus Torvalds 		if (err)
8311da177e4SLinus Torvalds 			return -EFAULT;
8321da177e4SLinus Torvalds 	}
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
8351da177e4SLinus Torvalds 
8361da177e4SLinus Torvalds 	if (!ret && oact) {
8371da177e4SLinus Torvalds 		int err = 0;
8381da177e4SLinus Torvalds 
8391da177e4SLinus Torvalds 		if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)))
8401da177e4SLinus Torvalds 			return -EFAULT;
8411da177e4SLinus Torvalds 
8421da177e4SLinus Torvalds 		err |= __put_user((u32)(u64)old_sa.sa.sa_handler,
8431da177e4SLinus Torvalds 		                   &oact->sa_handler);
8441da177e4SLinus Torvalds 		err |= __put_user(old_sa.sa.sa_flags, &oact->sa_flags);
8451da177e4SLinus Torvalds 		err |= put_sigset(&old_sa.sa.sa_mask, &oact->sa_mask);
8461da177e4SLinus Torvalds 		if (err)
8471da177e4SLinus Torvalds 			return -EFAULT;
8481da177e4SLinus Torvalds 	}
8491da177e4SLinus Torvalds out:
8501da177e4SLinus Torvalds 	return ret;
8511da177e4SLinus Torvalds }
8521da177e4SLinus Torvalds 
8531da177e4SLinus Torvalds asmlinkage int sys32_rt_sigprocmask(int how, compat_sigset_t *set,
8541da177e4SLinus Torvalds 	compat_sigset_t *oset, unsigned int sigsetsize)
8551da177e4SLinus Torvalds {
8561da177e4SLinus Torvalds 	sigset_t old_set, new_set;
8571da177e4SLinus Torvalds 	int ret;
8581da177e4SLinus Torvalds 	mm_segment_t old_fs = get_fs();
8591da177e4SLinus Torvalds 
8601da177e4SLinus Torvalds 	if (set && get_sigset(&new_set, set))
8611da177e4SLinus Torvalds 		return -EFAULT;
8621da177e4SLinus Torvalds 
8631da177e4SLinus Torvalds 	set_fs (KERNEL_DS);
8641da177e4SLinus Torvalds 	ret = sys_rt_sigprocmask(how, set ? &new_set : NULL,
8651da177e4SLinus Torvalds 				 oset ? &old_set : NULL, sigsetsize);
8661da177e4SLinus Torvalds 	set_fs (old_fs);
8671da177e4SLinus Torvalds 
8681da177e4SLinus Torvalds 	if (!ret && oset && put_sigset(&old_set, oset))
8691da177e4SLinus Torvalds 		return -EFAULT;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	return ret;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds 
8741da177e4SLinus Torvalds asmlinkage int sys32_rt_sigpending(compat_sigset_t *uset,
8751da177e4SLinus Torvalds 	unsigned int sigsetsize)
8761da177e4SLinus Torvalds {
8771da177e4SLinus Torvalds 	int ret;
8781da177e4SLinus Torvalds 	sigset_t set;
8791da177e4SLinus Torvalds 	mm_segment_t old_fs = get_fs();
8801da177e4SLinus Torvalds 
8811da177e4SLinus Torvalds 	set_fs (KERNEL_DS);
8821da177e4SLinus Torvalds 	ret = sys_rt_sigpending(&set, sigsetsize);
8831da177e4SLinus Torvalds 	set_fs (old_fs);
8841da177e4SLinus Torvalds 
8851da177e4SLinus Torvalds 	if (!ret && put_sigset(&set, uset))
8861da177e4SLinus Torvalds 		return -EFAULT;
8871da177e4SLinus Torvalds 
8881da177e4SLinus Torvalds 	return ret;
8891da177e4SLinus Torvalds }
8901da177e4SLinus Torvalds 
8911da177e4SLinus Torvalds asmlinkage int sys32_rt_sigqueueinfo(int pid, int sig, compat_siginfo_t *uinfo)
8921da177e4SLinus Torvalds {
8931da177e4SLinus Torvalds 	siginfo_t info;
8941da177e4SLinus Torvalds 	int ret;
8951da177e4SLinus Torvalds 	mm_segment_t old_fs = get_fs();
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	if (copy_from_user (&info, uinfo, 3*sizeof(int)) ||
8981da177e4SLinus Torvalds 	    copy_from_user (info._sifields._pad, uinfo->_sifields._pad, SI_PAD_SIZE))
8991da177e4SLinus Torvalds 		return -EFAULT;
9001da177e4SLinus Torvalds 	set_fs (KERNEL_DS);
9011da177e4SLinus Torvalds 	ret = sys_rt_sigqueueinfo(pid, sig, &info);
9021da177e4SLinus Torvalds 	set_fs (old_fs);
9031da177e4SLinus Torvalds 	return ret;
9041da177e4SLinus Torvalds }
905*54f2da75SRalf Baechle 
906*54f2da75SRalf Baechle asmlinkage long
907*54f2da75SRalf Baechle sys32_waitid(int which, compat_pid_t pid,
908*54f2da75SRalf Baechle 	     compat_siginfo_t __user *uinfo, int options,
909*54f2da75SRalf Baechle 	     struct compat_rusage __user *uru)
910*54f2da75SRalf Baechle {
911*54f2da75SRalf Baechle 	siginfo_t info;
912*54f2da75SRalf Baechle 	struct rusage ru;
913*54f2da75SRalf Baechle 	long ret;
914*54f2da75SRalf Baechle 	mm_segment_t old_fs = get_fs();
915*54f2da75SRalf Baechle 
916*54f2da75SRalf Baechle 	info.si_signo = 0;
917*54f2da75SRalf Baechle 	set_fs (KERNEL_DS);
918*54f2da75SRalf Baechle 	ret = sys_waitid(which, pid, (siginfo_t __user *) &info, options,
919*54f2da75SRalf Baechle 			 uru ? (struct rusage __user *) &ru : NULL);
920*54f2da75SRalf Baechle 	set_fs (old_fs);
921*54f2da75SRalf Baechle 
922*54f2da75SRalf Baechle 	if (ret < 0 || info.si_signo == 0)
923*54f2da75SRalf Baechle 		return ret;
924*54f2da75SRalf Baechle 
925*54f2da75SRalf Baechle 	if (uru && (ret = put_compat_rusage(&ru, uru)))
926*54f2da75SRalf Baechle 		return ret;
927*54f2da75SRalf Baechle 
928*54f2da75SRalf Baechle 	BUG_ON(info.si_code & __SI_MASK);
929*54f2da75SRalf Baechle 	info.si_code |= __SI_CHLD;
930*54f2da75SRalf Baechle 	return copy_siginfo_to_user32(uinfo, &info);
931*54f2da75SRalf Baechle }
932