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