sig_machdep.c revision 1.10
11.10Smatt/*	$NetBSD: sig_machdep.c,v 1.10 2003/01/20 05:26:47 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.1Skleink	struct trapframe *tf;
621.1Skleink	struct sigframe *fp, frame;
631.1Skleink	int onstack;
641.7Sthorpej	sig_t catcher = SIGACTION(p, sig).sa_handler;
651.1Skleink
661.8Sthorpej	tf = trapframe(l);
671.1Skleink
681.1Skleink	/* Do we need to jump onto the signal stack? */
691.1Skleink	onstack =
701.4Sjdolecek	    (p->p_sigctx.ps_sigstk.ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
711.4Sjdolecek	    (SIGACTION(p, sig).sa_flags & SA_ONSTACK) != 0;
721.1Skleink
731.1Skleink	/* Allocate space for the signal handler context. */
741.1Skleink	if (onstack)
751.4Sjdolecek		fp = (struct sigframe *)((caddr_t)p->p_sigctx.ps_sigstk.ss_sp +
761.4Sjdolecek						p->p_sigctx.ps_sigstk.ss_size);
771.1Skleink	else
781.1Skleink		fp = (struct sigframe *)tf->fixreg[1];
791.9Smatt	fp = (struct sigframe *)((uintptr_t)(fp - 1) & ~0xf);
801.1Skleink
811.1Skleink	/* Save register context. */
821.5Smatt	frame.sf_sc.sc_frame = *tf;
831.1Skleink
841.1Skleink	/* Save signal stack. */
851.4Sjdolecek	frame.sf_sc.sc_onstack = p->p_sigctx.ps_sigstk.ss_flags & SS_ONSTACK;
861.1Skleink
871.1Skleink	/* Save signal mask. */
881.1Skleink	frame.sf_sc.sc_mask = *mask;
891.1Skleink
901.1Skleink#ifdef COMPAT_13
911.1Skleink	/*
921.1Skleink	 * XXX We always have to save an old style signal mask because
931.1Skleink	 * XXX we might be delivering a signal to a process which will
941.1Skleink	 * XXX escape from the signal in a non-standard way and invoke
951.1Skleink	 * XXX sigreturn() directly.
961.1Skleink	 */
971.1Skleink	native_sigset_to_sigset13(mask, &frame.sf_sc.__sc_mask13);
981.1Skleink#endif
991.1Skleink
1001.1Skleink	if (copyout(&frame, fp, sizeof frame) != 0) {
1011.1Skleink		/*
1021.1Skleink		 * Process has trashed its stack; give it an illegal
1031.1Skleink		 * instructoin to halt it in its tracks.
1041.1Skleink		 */
1051.8Sthorpej		sigexit(l, SIGILL);
1061.1Skleink		/* NOTREACHED */
1071.1Skleink	}
1081.1Skleink
1091.1Skleink	/*
1101.7Sthorpej	 * Build context to run handler in.  Note the trampoline version
1111.7Sthorpej	 * numbers are coordinated with machine-dependent code in libc.
1121.1Skleink	 */
1131.7Sthorpej	switch (ps->sa_sigdesc[sig].sd_vers) {
1141.7Sthorpej#if 1 /* COMPAT_16 */
1151.7Sthorpej	case 0:		/* legacy on-stack sigtramp */
1161.9Smatt		tf->fixreg[1] = (register_t)fp;
1171.9Smatt		tf->lr = (register_t)catcher;
1181.9Smatt		tf->fixreg[3] = (register_t)sig;
1191.9Smatt		tf->fixreg[4] = (register_t)code;
1201.9Smatt		tf->fixreg[5] = (register_t)&fp->sf_sc;
1211.9Smatt		tf->srr0 = (register_t)p->p_sigctx.ps_sigcode;
1221.7Sthorpej		break;
1231.7Sthorpej#endif /* COMPAT_16 */
1241.7Sthorpej
1251.7Sthorpej	case 1:
1261.9Smatt		tf->fixreg[1] = (register_t)fp;
1271.9Smatt		tf->lr = (register_t)catcher;
1281.9Smatt		tf->fixreg[3] = (register_t)sig;
1291.9Smatt		tf->fixreg[4] = (register_t)code;
1301.9Smatt		tf->fixreg[5] = (register_t)&fp->sf_sc;
1311.9Smatt		tf->srr0 = (register_t)ps->sa_sigdesc[sig].sd_tramp;
1321.7Sthorpej		break;
1331.7Sthorpej
1341.7Sthorpej	default:
1351.7Sthorpej		/* Don't know what trampoline version; kill it. */
1361.8Sthorpej		sigexit(l, SIGILL);
1371.7Sthorpej	}
1381.1Skleink
1391.1Skleink	/* Remember that we're now on the signal stack. */
1401.1Skleink	if (onstack)
1411.4Sjdolecek		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
1421.1Skleink}
1431.1Skleink
1441.1Skleink/*
1451.1Skleink * System call to cleanup state after a signal handler returns.
1461.1Skleink */
1471.1Skleinkint
1481.8Sthorpejsys___sigreturn14(l, v, retval)
1491.8Sthorpej	struct lwp *l;
1501.1Skleink	void *v;
1511.1Skleink	register_t *retval;
1521.1Skleink{
1531.1Skleink	struct sys___sigreturn14_args /* {
1541.1Skleink		syscallarg(struct sigcontext *) sigcntxp;
1551.1Skleink	} */ *uap = v;
1561.8Sthorpej	struct proc *p = l->l_proc;
1571.1Skleink	struct sigcontext sc;
1581.1Skleink	struct trapframe *tf;
1591.1Skleink	int error;
1601.1Skleink
1611.1Skleink	/*
1621.1Skleink	 * The trampoline hands us the context.
1631.1Skleink	 * It is unsafe to keep track of it ourselves, in the event that a
1641.1Skleink	 * program jumps out of a signal hander.
1651.1Skleink	 */
1661.1Skleink	if ((error = copyin(SCARG(uap, sigcntxp), &sc, sizeof sc)) != 0)
1671.1Skleink		return (error);
1681.1Skleink
1691.1Skleink	/* Restore the register context. */
1701.8Sthorpej	tf = trapframe(l);
1711.1Skleink	if ((sc.sc_frame.srr1 & PSL_USERSTATIC) != (tf->srr1 & PSL_USERSTATIC))
1721.1Skleink		return (EINVAL);
1731.5Smatt	*tf = sc.sc_frame;
1741.1Skleink
1751.1Skleink	/* Restore signal stack. */
1761.1Skleink	if (sc.sc_onstack & SS_ONSTACK)
1771.4Sjdolecek		p->p_sigctx.ps_sigstk.ss_flags |= SS_ONSTACK;
1781.1Skleink	else
1791.4Sjdolecek		p->p_sigctx.ps_sigstk.ss_flags &= ~SS_ONSTACK;
1801.1Skleink
1811.1Skleink	/* Restore signal mask. */
1821.1Skleink	(void) sigprocmask1(p, SIG_SETMASK, &sc.sc_mask, 0);
1831.1Skleink
1841.1Skleink	return (EJUSTRETURN);
1851.8Sthorpej}
1861.8Sthorpej
1871.8Sthorpejvoid
1881.8Sthorpejcpu_getmcontext(l, mcp, flagp)
1891.8Sthorpej	struct lwp *l;
1901.8Sthorpej	mcontext_t *mcp;
1911.8Sthorpej	unsigned int *flagp;
1921.8Sthorpej{
1931.8Sthorpej	const struct trapframe *tf = trapframe(l);
1941.10Smatt	__greg_t *gr = mcp->__gregs;
1951.8Sthorpej#ifdef PPC_HAVE_FPU
1961.8Sthorpej	struct pcb *pcb = &l->l_addr->u_pcb;
1971.8Sthorpej#endif
1981.8Sthorpej
1991.8Sthorpej	/* Save GPR context. */
2001.10Smatt	(void)memcpy(gr, &tf->fixreg, 32 * sizeof (gr[0])); /* GR0-31 */
2011.10Smatt	gr[_REG_CR]  = tf->cr;
2021.10Smatt	gr[_REG_LR]  = tf->lr;
2031.10Smatt	gr[_REG_PC]  = tf->srr0;
2041.10Smatt	gr[_REG_MSR] = tf->srr1;
2051.10Smatt	gr[_REG_CTR] = tf->ctr;
2061.10Smatt	gr[_REG_XER] = tf->xer;
2071.10Smatt	gr[_REG_MQ]  = 0;				/* For now. */
2081.8Sthorpej	*flagp |= _UC_CPU;
2091.8Sthorpej
2101.8Sthorpej#ifdef PPC_HAVE_FPU
2111.8Sthorpej	/* Save FPR context, if any. */
2121.8Sthorpej	if ((pcb->pcb_flags & PCB_FPU) != 0) {
2131.8Sthorpej		/* If we're the FPU owner, dump its context to the PCB first. */
2141.8Sthorpej		if (pcb->pcb_fpcpu)
2151.8Sthorpej			save_fpu_lwp(l);
2161.8Sthorpej		(void)memcpy(mcp->__fpregs.__fpu_regs, pcb->pcb_fpu.fpr,
2171.8Sthorpej		    sizeof (mcp->__fpregs.__fpu_regs));
2181.8Sthorpej		mcp->__fpregs.__fpu_fpscr =
2191.8Sthorpej		    ((int *)&pcb->pcb_fpu.fpscr)[_QUAD_LOWWORD];
2201.8Sthorpej		mcp->__fpregs.__fpu_valid = 1;
2211.8Sthorpej		*flagp |= _UC_FPU;
2221.8Sthorpej	} else
2231.8Sthorpej#endif
2241.8Sthorpej		mcp->__fpregs.__fpu_valid = 0;
2251.8Sthorpej
2261.8Sthorpej	/* No AltiVec support, for now. */
2271.8Sthorpej	memset(&mcp->__vrf, 0, sizeof (mcp->__vrf));
2281.8Sthorpej}
2291.8Sthorpej
2301.8Sthorpejint
2311.8Sthorpejcpu_setmcontext(l, mcp, flags)
2321.8Sthorpej	struct lwp *l;
2331.8Sthorpej	const mcontext_t *mcp;
2341.8Sthorpej	unsigned int flags;
2351.8Sthorpej{
2361.8Sthorpej	struct trapframe *tf = trapframe(l);
2371.10Smatt	__greg_t *gr = mcp->__gregs;
2381.8Sthorpej#ifdef PPC_HAVE_FPU
2391.8Sthorpej	struct pcb *pcb = &l->l_addr->u_pcb;
2401.8Sthorpej#endif
2411.8Sthorpej
2421.8Sthorpej	/* Restore GPR context, if any. */
2431.8Sthorpej	if (flags & _UC_CPU) {
2441.10Smatt		if ((gr[_REG_MSR] & PSL_USERSTATIC) !=
2451.8Sthorpej		    (tf->srr1 & PSL_USERSTATIC))
2461.8Sthorpej			return (EINVAL);
2471.8Sthorpej
2481.10Smatt		(void)memcpy(&tf->fixreg, gr, 32 * sizeof (gr[0]));
2491.10Smatt		tf->cr   = gr[_REG_CR];
2501.10Smatt		tf->lr   = gr[_REG_LR];
2511.10Smatt		tf->srr0 = gr[_REG_PC];
2521.10Smatt		tf->srr1 = gr[_REG_MSR];
2531.10Smatt		tf->ctr  = gr[_REG_CTR];
2541.10Smatt		tf->xer  = gr[_REG_XER];
2551.10Smatt		/* unused = gr[_REG_MQ]; */
2561.8Sthorpej	}
2571.8Sthorpej
2581.8Sthorpej#ifdef PPC_HAVE_FPU
2591.8Sthorpej	/* Restore FPR context, if any. */
2601.8Sthorpej	if ((flags & _UC_FPU) && mcp->__fpregs.__fpu_valid != 0) {
2611.8Sthorpej		/* XXX we don't need to save the state, just to drop it */
2621.8Sthorpej		save_fpu_lwp(l);
2631.8Sthorpej		(void)memcpy(&pcb->pcb_fpu.fpr, &mcp->__fpregs.__fpu_regs,
2641.8Sthorpej		    sizeof (pcb->pcb_fpu.fpr));
2651.8Sthorpej		pcb->pcb_fpu.fpscr = *(double *)&mcp->__fpregs.__fpu_fpscr;
2661.8Sthorpej	}
2671.8Sthorpej#endif
2681.8Sthorpej
2691.8Sthorpej	return (0);
2701.1Skleink}
271