sig_machdep.c revision 1.26
11.26She/*	$NetBSD: sig_machdep.c,v 1.26 2006/10/24 16:53:01 he 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.12Slukem
341.12Slukem#include <sys/cdefs.h>
351.26She__KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.26 2006/10/24 16:53:01 he Exp $");
361.2Stsubai
371.2Stsubai#include "opt_compat_netbsd.h"
381.8Sthorpej#include "opt_ppcarch.h"
391.22Smatt#include "opt_altivec.h"
401.1Skleink
411.1Skleink#include <sys/param.h>
421.1Skleink#include <sys/mount.h>
431.1Skleink#include <sys/proc.h>
441.8Sthorpej#include <sys/sa.h>
451.8Sthorpej#include <sys/savar.h>
461.1Skleink#include <sys/syscallargs.h>
471.1Skleink#include <sys/systm.h>
481.8Sthorpej#include <sys/ucontext.h>
491.1Skleink#include <sys/user.h>
501.1Skleink
511.22Smatt#include <powerpc/fpu.h>
521.22Smatt#include <powerpc/altivec.h>
531.8Sthorpej
541.1Skleink/*
551.1Skleink * Send a signal to process.
561.1Skleink */
571.1Skleinkvoid
581.15Schristossendsig(const ksiginfo_t *ksi, const sigset_t *mask)
591.1Skleink{
601.13Smatt	struct lwp * const l = curlwp;
611.13Smatt	struct proc * const p = l->l_proc;
621.13Smatt	struct trapframe * const tf = trapframe(l);
631.13Smatt	struct sigaltstack *ss = &p->p_sigctx.ps_sigstk;
641.13Smatt	const struct sigact_sigdesc *sd =
651.13Smatt	    &p->p_sigacts->sa_sigdesc[ksi->ksi_signo];
661.13Smatt	ucontext_t uc;
671.13Smatt	vaddr_t sp, sip, ucp;
681.1Skleink	int onstack;
691.1Skleink
701.14Smatt	if (sd->sd_vers < 2) {
711.13Smatt#ifdef COMPAT_16
721.18Sthorpej		sendsig_sigcontext(ksi->ksi_signo, mask, KSI_TRAPCODE(ksi));
731.13Smatt		return;
741.14Smatt#else
751.14Smatt		goto nosupport;
761.14Smatt#endif
771.13Smatt	}
781.1Skleink
791.1Skleink	/* Do we need to jump onto the signal stack? */
801.13Smatt	onstack = (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
811.13Smatt	    (sd->sd_sigact.sa_flags & SA_ONSTACK) != 0;
821.1Skleink
831.13Smatt	/* Find top of stack.  */
841.13Smatt	sp = (onstack ? (vaddr_t)ss->ss_sp + ss->ss_size : tf->fixreg[1]);
851.13Smatt	sp &= ~(CALLFRAMELEN-1);
861.13Smatt
871.13Smatt	/* Allocate space for the ucontext.  */
881.13Smatt	sp -= sizeof(ucontext_t);
891.13Smatt	ucp = sp;
901.13Smatt
911.13Smatt	/* Allocate space for the siginfo.  */
921.13Smatt	sp -= sizeof(siginfo_t);
931.13Smatt	sip = sp;
941.13Smatt
951.13Smatt	sp &= ~(CALLFRAMELEN-1);
961.1Skleink
971.1Skleink	/* Save register context. */
981.13Smatt	uc.uc_flags = _UC_SIGMASK;
991.13Smatt	uc.uc_sigmask = *mask;
1001.13Smatt	uc.uc_link = NULL;
1011.13Smatt	memset(&uc.uc_stack, 0, sizeof(uc.uc_stack));
1021.13Smatt	cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags);
1031.1Skleink
1041.1Skleink	/*
1051.13Smatt	 * Copy the siginfo and ucontext onto the user's stack.
1061.1Skleink	 */
1071.19Smatt	if (copyout(&ksi->ksi_info, (caddr_t)sip, sizeof(ksi->ksi_info)) != 0 ||
1081.13Smatt	    copyout(&uc, (caddr_t)ucp, sizeof(uc)) != 0) {
1091.1Skleink		/*
1101.1Skleink		 * Process has trashed its stack; give it an illegal
1111.14Smatt		 * instruction to halt it in its tracks.
1121.1Skleink		 */
1131.8Sthorpej		sigexit(l, SIGILL);
1141.1Skleink		/* NOTREACHED */
1151.1Skleink	}
1161.1Skleink
1171.1Skleink	/*
1181.7Sthorpej	 * Build context to run handler in.  Note the trampoline version
1191.7Sthorpej	 * numbers are coordinated with machine-dependent code in libc.
1201.1Skleink	 */
1211.13Smatt	switch (sd->sd_vers) {
1221.13Smatt	case 2:		/* siginfo sigtramp */
1231.14Smatt		tf->fixreg[1]  = (register_t)sp - CALLFRAMELEN;
1241.17Smatt		tf->fixreg[3]  = (register_t)ksi->ksi_signo;
1251.14Smatt		tf->fixreg[4]  = (register_t)sip;
1261.14Smatt		tf->fixreg[5]  = (register_t)ucp;
1271.14Smatt		/* Preserve ucp across call to signal function */
1281.14Smatt		tf->fixreg[30] = (register_t)ucp;
1291.14Smatt		tf->lr         = (register_t)sd->sd_tramp;
1301.14Smatt		tf->srr0       = (register_t)sd->sd_sigact.sa_handler;
1311.7Sthorpej		break;
1321.7Sthorpej
1331.7Sthorpej	default:
1341.14Smatt		goto nosupport;
1351.7Sthorpej	}
1361.1Skleink
1371.1Skleink	/* Remember that we're now on the signal stack. */
1381.1Skleink	if (onstack)
1391.13Smatt		ss->ss_flags |= SS_ONSTACK;
1401.14Smatt	return;
1411.14Smatt
1421.14Smatt nosupport:
1431.14Smatt	/* Don't know what trampoline version; kill it. */
1441.14Smatt	printf("sendsig_siginfo(sig %d): bad version %d\n",
1451.17Smatt	    ksi->ksi_signo, sd->sd_vers);
1461.14Smatt	sigexit(l, SIGILL);
1471.14Smatt	/* NOTREACHED */
1481.8Sthorpej}
1491.8Sthorpej
1501.8Sthorpejvoid
1511.13Smattcpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp)
1521.8Sthorpej{
1531.8Sthorpej	const struct trapframe *tf = trapframe(l);
1541.10Smatt	__greg_t *gr = mcp->__gregs;
1551.22Smatt#if defined(PPC_HAVE_FPU) || defined(ALTIVEC)
1561.8Sthorpej	struct pcb *pcb = &l->l_addr->u_pcb;
1571.8Sthorpej#endif
1581.8Sthorpej
1591.8Sthorpej	/* Save GPR context. */
1601.10Smatt	(void)memcpy(gr, &tf->fixreg, 32 * sizeof (gr[0])); /* GR0-31 */
1611.10Smatt	gr[_REG_CR]  = tf->cr;
1621.10Smatt	gr[_REG_LR]  = tf->lr;
1631.10Smatt	gr[_REG_PC]  = tf->srr0;
1641.22Smatt	gr[_REG_MSR] = tf->srr1 & PSL_USERSRR1;
1651.21Smatt#ifdef PPC_HAVE_FPU
1661.21Smatt	gr[_REG_MSR] |= pcb->pcb_flags & (PCB_FE0|PCB_FE1);
1671.21Smatt#endif
1681.22Smatt#ifdef ALTIVEC
1691.22Smatt	gr[_REG_MSR] |= pcb->pcb_flags & PCB_ALTIVEC ? PSL_VEC : 0;
1701.22Smatt#endif
1711.10Smatt	gr[_REG_CTR] = tf->ctr;
1721.10Smatt	gr[_REG_XER] = tf->xer;
1731.11Smatt#ifdef PPC_OEA
1741.11Smatt	gr[_REG_MQ]  = tf->tf_xtra[TF_MQ];
1751.11Smatt#else
1761.11Smatt	gr[_REG_MQ]  = 0;
1771.11Smatt#endif
1781.8Sthorpej	*flagp |= _UC_CPU;
1791.8Sthorpej
1801.8Sthorpej#ifdef PPC_HAVE_FPU
1811.8Sthorpej	/* Save FPR context, if any. */
1821.8Sthorpej	if ((pcb->pcb_flags & PCB_FPU) != 0) {
1831.8Sthorpej		/* If we're the FPU owner, dump its context to the PCB first. */
1841.8Sthorpej		if (pcb->pcb_fpcpu)
1851.23Smatt			save_fpu_lwp(l, FPU_SAVE);
1861.23Smatt		(void)memcpy(mcp->__fpregs.__fpu_regs, pcb->pcb_fpu.fpreg,
1871.8Sthorpej		    sizeof (mcp->__fpregs.__fpu_regs));
1881.8Sthorpej		mcp->__fpregs.__fpu_fpscr =
1891.8Sthorpej		    ((int *)&pcb->pcb_fpu.fpscr)[_QUAD_LOWWORD];
1901.8Sthorpej		mcp->__fpregs.__fpu_valid = 1;
1911.8Sthorpej		*flagp |= _UC_FPU;
1921.8Sthorpej	} else
1931.8Sthorpej#endif
1941.13Smatt		memset(&mcp->__fpregs, 0, sizeof(mcp->__fpregs));
1951.8Sthorpej
1961.22Smatt#ifdef ALTIVEC
1971.22Smatt	/* Save AltiVec context, if any. */
1981.22Smatt	if ((pcb->pcb_flags & PCB_ALTIVEC) != 0) {
1991.22Smatt		/*
2001.22Smatt		 * If we're the AltiVec owner, dump its context
2011.22Smatt		 * to the PCB first.
2021.22Smatt		 */
2031.22Smatt		if (pcb->pcb_veccpu)
2041.23Smatt			save_vec_lwp(l, ALTIVEC_SAVE);
2051.22Smatt		(void)memcpy(mcp->__vrf.__vrs, pcb->pcb_vr.vreg,
2061.22Smatt		    sizeof (mcp->__vrf.__vrs));
2071.22Smatt		mcp->__vrf.__vscr = pcb->pcb_vr.vscr;
2081.22Smatt		mcp->__vrf.__vrsave = pcb->pcb_vr.vrsave;
2091.22Smatt		*flagp |= _UC_POWERPC_VEC;
2101.22Smatt	} else
2111.22Smatt#endif
2121.22Smatt		memset(&mcp->__vrf, 0, sizeof (mcp->__vrf));
2131.8Sthorpej}
2141.8Sthorpej
2151.8Sthorpejint
2161.13Smattcpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
2171.8Sthorpej{
2181.8Sthorpej	struct trapframe *tf = trapframe(l);
2191.26She	const __greg_t *gr = mcp->__gregs;
2201.8Sthorpej#ifdef PPC_HAVE_FPU
2211.8Sthorpej	struct pcb *pcb = &l->l_addr->u_pcb;
2221.8Sthorpej#endif
2231.8Sthorpej
2241.8Sthorpej	/* Restore GPR context, if any. */
2251.8Sthorpej	if (flags & _UC_CPU) {
2261.21Smatt#ifdef PPC_HAVE_FPU
2271.21Smatt		/*
2281.21Smatt		 * Always save the FP exception mode in the PCB.
2291.21Smatt		 */
2301.21Smatt		pcb->pcb_flags &= ~(PCB_FE0|PCB_FE1);
2311.21Smatt		pcb->pcb_flags |= gr[_REG_MSR] & (PCB_FE0|PCB_FE1);
2321.21Smatt#endif
2331.21Smatt
2341.10Smatt		(void)memcpy(&tf->fixreg, gr, 32 * sizeof (gr[0]));
2351.10Smatt		tf->cr   = gr[_REG_CR];
2361.10Smatt		tf->lr   = gr[_REG_LR];
2371.10Smatt		tf->srr0 = gr[_REG_PC];
2381.26She		/*
2391.26She		 * Accept all user-settable bits without complaint;
2401.26She		 * userland should not need to know the machine-specific
2411.26She		 * MSR value.
2421.26She		 */
2431.26She		tf->srr1 = (gr[_REG_MSR] & PSL_USERMOD) | PSL_USERSET;
2441.10Smatt		tf->ctr  = gr[_REG_CTR];
2451.10Smatt		tf->xer  = gr[_REG_XER];
2461.11Smatt#ifdef PPC_OEA
2471.11Smatt		tf->tf_xtra[TF_MQ] = gr[_REG_MQ];
2481.11Smatt#endif
2491.8Sthorpej	}
2501.8Sthorpej
2511.22Smatt#ifdef PPC_HAVE_FPU /* Restore FPR context, if any. */
2521.8Sthorpej	if ((flags & _UC_FPU) && mcp->__fpregs.__fpu_valid != 0) {
2531.23Smatt		/* we don't need to save the state, just drop it */
2541.23Smatt		save_fpu_lwp(l, FPU_DISCARD);
2551.23Smatt		(void)memcpy(&pcb->pcb_fpu.fpreg, &mcp->__fpregs.__fpu_regs,
2561.23Smatt		    sizeof (pcb->pcb_fpu.fpreg));
2571.20Smatt		((int *)&pcb->pcb_fpu.fpscr)[_QUAD_LOWWORD] =
2581.20Smatt		    mcp->__fpregs.__fpu_fpscr;
2591.8Sthorpej	}
2601.8Sthorpej#endif
2611.8Sthorpej
2621.22Smatt#ifdef ALTIVEC
2631.22Smatt	/* Restore AltiVec context, if any. */
2641.22Smatt	if (flags & _UC_POWERPC_VEC) {
2651.23Smatt		/* we don't need to save the state, just drop it */
2661.23Smatt		save_vec_lwp(l, ALTIVEC_DISCARD);
2671.22Smatt		(void)memcpy(pcb->pcb_vr.vreg, &mcp->__vrf.__vrs,
2681.22Smatt		    sizeof (pcb->pcb_vr.vreg));
2691.22Smatt		pcb->pcb_vr.vscr = mcp->__vrf.__vscr;
2701.22Smatt		pcb->pcb_vr.vrsave = mcp->__vrf.__vrsave;
2711.22Smatt	}
2721.22Smatt#endif
2731.22Smatt
2741.8Sthorpej	return (0);
2751.1Skleink}
276