sig_machdep.c revision 1.11
11.11Smatt/*	$NetBSD: sig_machdep.c,v 1.11 2003/02/03 21:48:02 matt Exp $	*/
21.1Skleink
31.1Skleink/*
41.1Skleink * Copyright (C) 1995, 1996 Wolfgang Solfrank.
51.1Skleink * Copyright (C) 1995, 1996 TooLs GmbH.
61.1Skleink * All rights reserved.
71.1Skleink *
81.1Skleink * Redistribution and use in source and binary forms, with or without
91.1Skleink * modification, are permitted provided that the following conditions
101.1Skleink * are met:
111.1Skleink * 1. Redistributions of source code must retain the above copyright
121.1Skleink *    notice, this list of conditions and the following disclaimer.
131.1Skleink * 2. Redistributions in binary form must reproduce the above copyright
141.1Skleink *    notice, this list of conditions and the following disclaimer in the
151.1Skleink *    documentation and/or other materials provided with the distribution.
161.1Skleink * 3. All advertising materials mentioning features or use of this software
171.1Skleink *    must display the following acknowledgement:
181.1Skleink *	This product includes software developed by TooLs GmbH.
191.1Skleink * 4. The name of TooLs GmbH may not be used to endorse or promote products
201.1Skleink *    derived from this software without specific prior written permission.
211.1Skleink *
221.1Skleink * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
231.1Skleink * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
241.1Skleink * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
251.1Skleink * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
261.1Skleink * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
271.1Skleink * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
281.1Skleink * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
291.1Skleink * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
301.1Skleink * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
311.1Skleink * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321.1Skleink */
331.2Stsubai
341.2Stsubai#include "opt_compat_netbsd.h"
351.8Sthorpej#include "opt_ppcarch.h"
361.1Skleink
371.1Skleink#include <sys/param.h>
381.1Skleink#include <sys/mount.h>
391.1Skleink#include <sys/proc.h>
401.8Sthorpej#include <sys/sa.h>
411.8Sthorpej#include <sys/savar.h>
421.1Skleink#include <sys/syscallargs.h>
431.1Skleink#include <sys/systm.h>
441.8Sthorpej#include <sys/ucontext.h>
451.1Skleink#include <sys/user.h>
461.1Skleink
471.8Sthorpej#include <machine/fpu.h>
481.8Sthorpej
491.1Skleink/*
501.1Skleink * Send a signal to process.
511.1Skleink */
521.1Skleinkvoid
531.7Sthorpejsendsig(sig, mask, code)
541.1Skleink	int sig;
551.1Skleink	sigset_t *mask;
561.1Skleink	u_long code;
571.1Skleink{
581.8Sthorpej	struct lwp *l = curlwp;
591.8Sthorpej	struct proc *p = l->l_proc;
601.7Sthorpej	struct sigacts *ps = p->p_sigacts;
611.11Smatt	struct sigframe *fp, frame;
621.1Skleink	struct trapframe *tf;
631.11Smatt	struct utrapframe *utf = &frame.sf_sc.sc_frame;
641.1Skleink	int onstack;
651.7Sthorpej	sig_t catcher = SIGACTION(p, sig).sa_handler;
661.1Skleink
671.8Sthorpej	tf = trapframe(l);
681.1Skleink
691.1Skleink	/* Do we need to jump onto the signal stack? */
701.1Skleink	onstack =
711.4Sjdolecek	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
721.4Sjdolecek	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
731.1Skleink
741.1Skleink	/* Allocate space for the signal handler context. */
751.1Skleink	if (onstack)
761.4Sjdolecek		fp = (struct sigframe *)((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
771.4Sjdolecek						p->p_sigctx.ps_sigstk.ss_size);
781.1Skleink	else
791.1Skleink		fp = (struct sigframe *)tf->fixreg[1];
801.9Smatt	fp = (struct sigframe *)((uintptr_t)(fp - 1) & ~0xf);
811.1Skleink
821.1Skleink	/* Save register context. */
831.11Smatt	memcpy(utf->fixreg, tf->fixreg, sizeof(utf->fixreg));
841.11Smatt	utf->lr   = tf->lr;
851.11Smatt	utf->cr   = tf->cr;
861.11Smatt	utf->xer  = tf->xer;
871.11Smatt	utf->ctr  = tf->ctr;
881.11Smatt	utf->srr0 = tf->srr0;
891.11Smatt	utf->srr1 = tf->srr1;
901.11Smatt#ifdef PPC_OEA
911.11Smatt	utf->vrsave = tf->tf_xtra[TF_VRSAVE];
921.11Smatt	utf->mq = tf->tf_xtra[TF_MQ];
931.11Smatt#endif
941.1Skleink
951.1Skleink	/* Save signal stack. */
961.4Sjdolecek	frame.sf_sc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK;
971.1Skleink
981.1Skleink	/* Save signal mask. */
991.1Skleink	frame.sf_sc.sc_mask = *mask;
1001.1Skleink
1011.1Skleink#ifdef COMPAT_13
1021.1Skleink	/*
1031.1Skleink	 * XXX We always have to save an old style signal mask because
1041.1Skleink	 * XXX we might be delivering a signal to a process which will
1051.1Skleink	 * XXX escape from the signal in a non-standard way and invoke
1061.1Skleink	 * XXX sigreturn() directly.
1071.1Skleink	 */
1081.1Skleink	native_sigset_to_sigset13(mask, &frame.sf_sc.__sc_mask13);
1091.1Skleink#endif
1101.1Skleink
1111.1Skleink	if (copyout(&frame, fp, sizeof frame) != 0) {
1121.1Skleink		/*
1131.1Skleink		 * Process has trashed its stack; give it an illegal
1141.1Skleink		 * instructoin to halt it in its tracks.
1151.1Skleink		 */
1161.8Sthorpej		sigexit(l, SIGILL);
1171.1Skleink		/* NOTREACHED */
1181.1Skleink	}
1191.1Skleink
1201.1Skleink	/*
1211.7Sthorpej	 * Build context to run handler in.  Note the trampoline version
1221.7Sthorpej	 * numbers are coordinated with machine-dependent code in libc.
1231.1Skleink	 */
1241.7Sthorpej	switch (ps->sa_sigdesc[sig].sd_vers) {
1251.7Sthorpej#if 1 /* COMPAT_16 */
1261.7Sthorpej	case 0:		/* legacy on-stack sigtramp */
1271.9Smatt		tf->fixreg[1] = (register_t)fp;
1281.9Smatt		tf->lr = (register_t)catcher;
1291.9Smatt		tf->fixreg[3] = (register_t)sig;
1301.9Smatt		tf->fixreg[4] = (register_t)code;
1311.9Smatt		tf->fixreg[5] = (register_t)&fp->sf_sc;
1321.9Smatt		tf->srr0 = (register_t)p->p_sigctx.ps_sigcode;
1331.7Sthorpej		break;
1341.7Sthorpej#endif /* COMPAT_16 */
1351.7Sthorpej
1361.7Sthorpej	case 1:
1371.9Smatt		tf->fixreg[1] = (register_t)fp;
1381.9Smatt		tf->lr = (register_t)catcher;
1391.9Smatt		tf->fixreg[3] = (register_t)sig;
1401.9Smatt		tf->fixreg[4] = (register_t)code;
1411.9Smatt		tf->fixreg[5] = (register_t)&fp->sf_sc;
1421.9Smatt		tf->srr0 = (register_t)ps->sa_sigdesc[sig].sd_tramp;
1431.7Sthorpej		break;
1441.7Sthorpej
1451.7Sthorpej	default:
1461.7Sthorpej		/* Don't know what trampoline version; kill it. */
1471.8Sthorpej		sigexit(l, SIGILL);
1481.7Sthorpej	}
1491.1Skleink
1501.1Skleink	/* Remember that we're now on the signal stack. */
1511.1Skleink	if (onstack)
1521.4Sjdolecek		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
1531.1Skleink}
1541.1Skleink
1551.1Skleink/*
1561.1Skleink * System call to cleanup state after a signal handler returns.
1571.1Skleink */
1581.1Skleinkint
1591.8Sthorpejsys___sigreturn14(l, v, retval)
1601.8Sthorpej	struct lwp *l;
1611.1Skleink	void *v;
1621.1Skleink	register_t *retval;
1631.1Skleink{
1641.1Skleink	struct sys___sigreturn14_args /* {
1651.1Skleink		syscallarg(struct sigcontext *) sigcntxp;
1661.1Skleink	} */ *uap = v;
1671.8Sthorpej	struct proc *p = l->l_proc;
1681.1Skleink	struct sigcontext sc;
1691.1Skleink	struct trapframe *tf;
1701.11Smatt	struct utrapframe * const utf = &sc.sc_frame;
1711.1Skleink	int error;
1721.1Skleink
1731.1Skleink	/*
1741.1Skleink	 * The trampoline hands us the context.
1751.1Skleink	 * It is unsafe to keep track of it ourselves, in the event that a
1761.1Skleink	 * program jumps out of a signal hander.
1771.1Skleink	 */
1781.1Skleink	if ((error = copyin(SCARG(uap, sigcntxp), &sc, sizeof sc)) != 0)
1791.1Skleink		return (error);
1801.1Skleink
1811.1Skleink	/* Restore the register context. */
1821.8Sthorpej	tf = trapframe(l);
1831.1Skleink	if ((sc.sc_frame.srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
1841.1Skleink		return (EINVAL);
1851.11Smatt
1861.11Smatt	/* Restore register context. */
1871.11Smatt	memcpy(tf->fixreg, utf->fixreg, sizeof(tf->fixreg));
1881.11Smatt	tf->lr   = utf->lr;
1891.11Smatt	tf->cr   = utf->cr;
1901.11Smatt	tf->xer  = utf->xer;
1911.11Smatt	tf->ctr  = utf->ctr;
1921.11Smatt	tf->srr0 = utf->srr0;
1931.11Smatt	tf->srr1 = utf->srr1;
1941.11Smatt#ifdef PPC_OEA
1951.11Smatt	tf->tf_xtra[TF_VRSAVE] = utf->vrsave;
1961.11Smatt	tf->tf_xtra[TF_MQ] = utf->mq;
1971.11Smatt#endif
1981.1Skleink
1991.1Skleink	/* Restore signal stack. */
2001.1Skleink	if (sc.sc_onstack & SS_ONSTACK)
2011.4Sjdolecek		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
2021.1Skleink	else
2031.4Sjdolecek		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
2041.1Skleink
2051.1Skleink	/* Restore signal mask. */
2061.1Skleink	(void) sigprocmask1(p, SIG_SETMASK, &sc.sc_mask, 0);
2071.1Skleink
2081.1Skleink	return (EJUSTRETURN);
2091.8Sthorpej}
2101.8Sthorpej
2111.8Sthorpejvoid
2121.8Sthorpejcpu_getmcontext(l, mcp, flagp)
2131.8Sthorpej	struct lwp *l;
2141.8Sthorpej	mcontext_t *mcp;
2151.8Sthorpej	unsigned int *flagp;
2161.8Sthorpej{
2171.8Sthorpej	const struct trapframe *tf = trapframe(l);
2181.10Smatt	__greg_t *gr = mcp->__gregs;
2191.8Sthorpej#ifdef PPC_HAVE_FPU
2201.8Sthorpej	struct pcb *pcb = &l->l_addr->u_pcb;
2211.8Sthorpej#endif
2221.8Sthorpej
2231.8Sthorpej	/* Save GPR context. */
2241.10Smatt	(void)memcpy(gr, &tf->fixreg, 32 * sizeof (gr[0])); /* GR0-31 */
2251.10Smatt	gr[_REG_CR]  = tf->cr;
2261.10Smatt	gr[_REG_LR]  = tf->lr;
2271.10Smatt	gr[_REG_PC]  = tf->srr0;
2281.10Smatt	gr[_REG_MSR] = tf->srr1;
2291.10Smatt	gr[_REG_CTR] = tf->ctr;
2301.10Smatt	gr[_REG_XER] = tf->xer;
2311.11Smatt#ifdef PPC_OEA
2321.11Smatt	gr[_REG_MQ]  = tf->tf_xtra[TF_MQ];
2331.11Smatt#else
2341.11Smatt	gr[_REG_MQ]  = 0;
2351.11Smatt#endif
2361.8Sthorpej	*flagp |= _UC_CPU;
2371.8Sthorpej
2381.8Sthorpej#ifdef PPC_HAVE_FPU
2391.8Sthorpej	/* Save FPR context, if any. */
2401.8Sthorpej	if ((pcb->pcb_flags & PCB_FPU) != 0) {
2411.8Sthorpej		/* If we're the FPU owner, dump its context to the PCB first. */
2421.8Sthorpej		if (pcb->pcb_fpcpu)
2431.8Sthorpej			save_fpu_lwp(l);
2441.8Sthorpej		(void)memcpy(mcp->__fpregs.__fpu_regs, pcb->pcb_fpu.fpr,
2451.8Sthorpej		    sizeof (mcp->__fpregs.__fpu_regs));
2461.8Sthorpej		mcp->__fpregs.__fpu_fpscr =
2471.8Sthorpej		    ((int *)&pcb->pcb_fpu.fpscr)[_QUAD_LOWWORD];
2481.8Sthorpej		mcp->__fpregs.__fpu_valid = 1;
2491.8Sthorpej		*flagp |= _UC_FPU;
2501.8Sthorpej	} else
2511.8Sthorpej#endif
2521.8Sthorpej		mcp->__fpregs.__fpu_valid = 0;
2531.8Sthorpej
2541.8Sthorpej	/* No AltiVec support, for now. */
2551.8Sthorpej	memset(&mcp->__vrf, 0, sizeof (mcp->__vrf));
2561.8Sthorpej}
2571.8Sthorpej
2581.8Sthorpejint
2591.8Sthorpejcpu_setmcontext(l, mcp, flags)
2601.8Sthorpej	struct lwp *l;
2611.8Sthorpej	const mcontext_t *mcp;
2621.8Sthorpej	unsigned int flags;
2631.8Sthorpej{
2641.8Sthorpej	struct trapframe *tf = trapframe(l);
2651.10Smatt	__greg_t *gr = mcp->__gregs;
2661.8Sthorpej#ifdef PPC_HAVE_FPU
2671.8Sthorpej	struct pcb *pcb = &l->l_addr->u_pcb;
2681.8Sthorpej#endif
2691.8Sthorpej
2701.8Sthorpej	/* Restore GPR context, if any. */
2711.8Sthorpej	if (flags & _UC_CPU) {
2721.10Smatt		if ((gr[_REG_MSR] & PSL_USERSTATIC) !=
2731.8Sthorpej		    (tf->srr1 & PSL_USERSTATIC))
2741.8Sthorpej			return (EINVAL);
2751.8Sthorpej
2761.10Smatt		(void)memcpy(&tf->fixreg, gr, 32 * sizeof (gr[0]));
2771.10Smatt		tf->cr   = gr[_REG_CR];
2781.10Smatt		tf->lr   = gr[_REG_LR];
2791.10Smatt		tf->srr0 = gr[_REG_PC];
2801.10Smatt		tf->srr1 = gr[_REG_MSR];
2811.10Smatt		tf->ctr  = gr[_REG_CTR];
2821.10Smatt		tf->xer  = gr[_REG_XER];
2831.11Smatt#ifdef PPC_OEA
2841.11Smatt		tf->tf_xtra[TF_MQ] = gr[_REG_MQ];
2851.11Smatt#endif
2861.8Sthorpej	}
2871.8Sthorpej
2881.8Sthorpej#ifdef PPC_HAVE_FPU
2891.8Sthorpej	/* Restore FPR context, if any. */
2901.8Sthorpej	if ((flags & _UC_FPU) && mcp->__fpregs.__fpu_valid != 0) {
2911.8Sthorpej		/* XXX we don't need to save the state, just to drop it */
2921.8Sthorpej		save_fpu_lwp(l);
2931.8Sthorpej		(void)memcpy(&pcb->pcb_fpu.fpr, &mcp->__fpregs.__fpu_regs,
2941.8Sthorpej		    sizeof (pcb->pcb_fpu.fpr));
2951.8Sthorpej		pcb->pcb_fpu.fpscr = *(double *)&mcp->__fpregs.__fpu_fpscr;
2961.8Sthorpej	}
2971.8Sthorpej#endif
2981.8Sthorpej
2991.8Sthorpej	return (0);
3001.1Skleink}
301