sig_machdep.c revision 1.35
11.35Srmind/*	$NetBSD: sig_machdep.c,v 1.35 2009/11/21 17:40:29 rmind 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.35Srmind__KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.35 2009/11/21 17:40:29 rmind Exp $");
361.2Stsubai
371.8Sthorpej#include "opt_ppcarch.h"
381.22Smatt#include "opt_altivec.h"
391.1Skleink
401.1Skleink#include <sys/param.h>
411.1Skleink#include <sys/mount.h>
421.1Skleink#include <sys/proc.h>
431.1Skleink#include <sys/syscallargs.h>
441.1Skleink#include <sys/systm.h>
451.8Sthorpej#include <sys/ucontext.h>
461.1Skleink
471.22Smatt#include <powerpc/fpu.h>
481.22Smatt#include <powerpc/altivec.h>
491.8Sthorpej
501.1Skleink/*
511.1Skleink * Send a signal to process.
521.1Skleink */
531.1Skleinkvoid
541.34Shesendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
551.1Skleink{
561.13Smatt	struct lwp * const l = curlwp;
571.13Smatt	struct proc * const p = l->l_proc;
581.13Smatt	struct trapframe * const tf = trapframe(l);
591.27Sad	struct sigaltstack *ss = &l->l_sigstk;
601.13Smatt	const struct sigact_sigdesc *sd =
611.13Smatt	    &p->p_sigacts->sa_sigdesc[ksi->ksi_signo];
621.13Smatt	ucontext_t uc;
631.13Smatt	vaddr_t sp, sip, ucp;
641.27Sad	int onstack, error;
651.1Skleink
661.1Skleink	/* Do we need to jump onto the signal stack? */
671.13Smatt	onstack = (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
681.13Smatt	    (sd->sd_sigact.sa_flags & SA_ONSTACK) != 0;
691.1Skleink
701.13Smatt	/* Find top of stack.  */
711.13Smatt	sp = (onstack ? (vaddr_t)ss->ss_sp + ss->ss_size : tf->fixreg[1]);
721.13Smatt	sp &= ~(CALLFRAMELEN-1);
731.13Smatt
741.13Smatt	/* Allocate space for the ucontext.  */
751.13Smatt	sp -= sizeof(ucontext_t);
761.13Smatt	ucp = sp;
771.13Smatt
781.13Smatt	/* Allocate space for the siginfo.  */
791.13Smatt	sp -= sizeof(siginfo_t);
801.13Smatt	sip = sp;
811.13Smatt
821.13Smatt	sp &= ~(CALLFRAMELEN-1);
831.1Skleink
841.1Skleink	/* Save register context. */
851.13Smatt	uc.uc_flags = _UC_SIGMASK;
861.13Smatt	uc.uc_sigmask = *mask;
871.30Spooka	uc.uc_link = l->l_ctxlink;
881.13Smatt	memset(&uc.uc_stack, 0, sizeof(uc.uc_stack));
891.27Sad	sendsig_reset(l, ksi->ksi_signo);
901.32Sad	mutex_exit(p->p_lock);
911.13Smatt	cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags);
921.1Skleink
931.1Skleink	/*
941.13Smatt	 * Copy the siginfo and ucontext onto the user's stack.
951.1Skleink	 */
961.29Schristos	error = (copyout(&ksi->ksi_info, (void *)sip, sizeof(ksi->ksi_info)) != 0 ||
971.29Schristos	    copyout(&uc, (void *)ucp, sizeof(uc)) != 0);
981.32Sad	mutex_enter(p->p_lock);
991.27Sad
1001.27Sad	if (error) {
1011.1Skleink		/*
1021.1Skleink		 * Process has trashed its stack; give it an illegal
1031.14Smatt		 * instruction 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.13Smatt	switch (sd->sd_vers) {
1141.13Smatt	case 2:		/* siginfo sigtramp */
1151.14Smatt		tf->fixreg[1]  = (register_t)sp - CALLFRAMELEN;
1161.17Smatt		tf->fixreg[3]  = (register_t)ksi->ksi_signo;
1171.14Smatt		tf->fixreg[4]  = (register_t)sip;
1181.14Smatt		tf->fixreg[5]  = (register_t)ucp;
1191.14Smatt		/* Preserve ucp across call to signal function */
1201.14Smatt		tf->fixreg[30] = (register_t)ucp;
1211.14Smatt		tf->lr         = (register_t)sd->sd_tramp;
1221.14Smatt		tf->srr0       = (register_t)sd->sd_sigact.sa_handler;
1231.7Sthorpej		break;
1241.7Sthorpej
1251.7Sthorpej	default:
1261.14Smatt		goto nosupport;
1271.7Sthorpej	}
1281.1Skleink
1291.1Skleink	/* Remember that we're now on the signal stack. */
1301.1Skleink	if (onstack)
1311.13Smatt		ss->ss_flags |= SS_ONSTACK;
1321.14Smatt	return;
1331.14Smatt
1341.14Smatt nosupport:
1351.14Smatt	/* Don't know what trampoline version; kill it. */
1361.14Smatt	printf("sendsig_siginfo(sig %d): bad version %d\n",
1371.17Smatt	    ksi->ksi_signo, sd->sd_vers);
1381.14Smatt	sigexit(l, SIGILL);
1391.14Smatt	/* NOTREACHED */
1401.8Sthorpej}
1411.8Sthorpej
1421.8Sthorpejvoid
1431.13Smattcpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp)
1441.8Sthorpej{
1451.8Sthorpej	const struct trapframe *tf = trapframe(l);
1461.10Smatt	__greg_t *gr = mcp->__gregs;
1471.22Smatt#if defined(PPC_HAVE_FPU) || defined(ALTIVEC)
1481.35Srmind	struct pcb *pcb = lwp_getpcb(l);
1491.8Sthorpej#endif
1501.8Sthorpej
1511.8Sthorpej	/* Save GPR context. */
1521.10Smatt	(void)memcpy(gr, &tf->fixreg, 32 * sizeof (gr[0])); /* GR0-31 */
1531.10Smatt	gr[_REG_CR]  = tf->cr;
1541.10Smatt	gr[_REG_LR]  = tf->lr;
1551.10Smatt	gr[_REG_PC]  = tf->srr0;
1561.22Smatt	gr[_REG_MSR] = tf->srr1 & PSL_USERSRR1;
1571.21Smatt#ifdef PPC_HAVE_FPU
1581.21Smatt	gr[_REG_MSR] |= pcb->pcb_flags & (PCB_FE0|PCB_FE1);
1591.21Smatt#endif
1601.22Smatt#ifdef ALTIVEC
1611.22Smatt	gr[_REG_MSR] |= pcb->pcb_flags & PCB_ALTIVEC ? PSL_VEC : 0;
1621.22Smatt#endif
1631.10Smatt	gr[_REG_CTR] = tf->ctr;
1641.10Smatt	gr[_REG_XER] = tf->xer;
1651.11Smatt#ifdef PPC_OEA
1661.11Smatt	gr[_REG_MQ]  = tf->tf_xtra[TF_MQ];
1671.11Smatt#else
1681.11Smatt	gr[_REG_MQ]  = 0;
1691.11Smatt#endif
1701.8Sthorpej	*flagp |= _UC_CPU;
1711.8Sthorpej
1721.8Sthorpej#ifdef PPC_HAVE_FPU
1731.8Sthorpej	/* Save FPR context, if any. */
1741.8Sthorpej	if ((pcb->pcb_flags & PCB_FPU) != 0) {
1751.8Sthorpej		/* If we're the FPU owner, dump its context to the PCB first. */
1761.8Sthorpej		if (pcb->pcb_fpcpu)
1771.23Smatt			save_fpu_lwp(l, FPU_SAVE);
1781.23Smatt		(void)memcpy(mcp->__fpregs.__fpu_regs, pcb->pcb_fpu.fpreg,
1791.8Sthorpej		    sizeof (mcp->__fpregs.__fpu_regs));
1801.8Sthorpej		mcp->__fpregs.__fpu_fpscr =
1811.8Sthorpej		    ((int *)&pcb->pcb_fpu.fpscr)[_QUAD_LOWWORD];
1821.8Sthorpej		mcp->__fpregs.__fpu_valid = 1;
1831.8Sthorpej		*flagp |= _UC_FPU;
1841.8Sthorpej	} else
1851.8Sthorpej#endif
1861.13Smatt		memset(&mcp->__fpregs, 0, sizeof(mcp->__fpregs));
1871.8Sthorpej
1881.22Smatt#ifdef ALTIVEC
1891.22Smatt	/* Save AltiVec context, if any. */
1901.22Smatt	if ((pcb->pcb_flags & PCB_ALTIVEC) != 0) {
1911.22Smatt		/*
1921.22Smatt		 * If we're the AltiVec owner, dump its context
1931.22Smatt		 * to the PCB first.
1941.22Smatt		 */
1951.22Smatt		if (pcb->pcb_veccpu)
1961.23Smatt			save_vec_lwp(l, ALTIVEC_SAVE);
1971.22Smatt		(void)memcpy(mcp->__vrf.__vrs, pcb->pcb_vr.vreg,
1981.22Smatt		    sizeof (mcp->__vrf.__vrs));
1991.22Smatt		mcp->__vrf.__vscr = pcb->pcb_vr.vscr;
2001.22Smatt		mcp->__vrf.__vrsave = pcb->pcb_vr.vrsave;
2011.22Smatt		*flagp |= _UC_POWERPC_VEC;
2021.22Smatt	} else
2031.22Smatt#endif
2041.22Smatt		memset(&mcp->__vrf, 0, sizeof (mcp->__vrf));
2051.8Sthorpej}
2061.8Sthorpej
2071.8Sthorpejint
2081.13Smattcpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
2091.8Sthorpej{
2101.8Sthorpej	struct trapframe *tf = trapframe(l);
2111.26She	const __greg_t *gr = mcp->__gregs;
2121.8Sthorpej#ifdef PPC_HAVE_FPU
2131.35Srmind	struct pcb *pcb = lwp_getpcb(l);
2141.8Sthorpej#endif
2151.8Sthorpej
2161.8Sthorpej	/* Restore GPR context, if any. */
2171.8Sthorpej	if (flags & _UC_CPU) {
2181.21Smatt#ifdef PPC_HAVE_FPU
2191.21Smatt		/*
2201.21Smatt		 * Always save the FP exception mode in the PCB.
2211.21Smatt		 */
2221.21Smatt		pcb->pcb_flags &= ~(PCB_FE0|PCB_FE1);
2231.21Smatt		pcb->pcb_flags |= gr[_REG_MSR] & (PCB_FE0|PCB_FE1);
2241.21Smatt#endif
2251.21Smatt
2261.10Smatt		(void)memcpy(&tf->fixreg, gr, 32 * sizeof (gr[0]));
2271.10Smatt		tf->cr   = gr[_REG_CR];
2281.10Smatt		tf->lr   = gr[_REG_LR];
2291.10Smatt		tf->srr0 = gr[_REG_PC];
2301.26She		/*
2311.26She		 * Accept all user-settable bits without complaint;
2321.26She		 * userland should not need to know the machine-specific
2331.26She		 * MSR value.
2341.26She		 */
2351.26She		tf->srr1 = (gr[_REG_MSR] & PSL_USERMOD) | PSL_USERSET;
2361.10Smatt		tf->ctr  = gr[_REG_CTR];
2371.10Smatt		tf->xer  = gr[_REG_XER];
2381.11Smatt#ifdef PPC_OEA
2391.11Smatt		tf->tf_xtra[TF_MQ] = gr[_REG_MQ];
2401.11Smatt#endif
2411.8Sthorpej	}
2421.8Sthorpej
2431.22Smatt#ifdef PPC_HAVE_FPU /* Restore FPR context, if any. */
2441.8Sthorpej	if ((flags & _UC_FPU) && mcp->__fpregs.__fpu_valid != 0) {
2451.23Smatt		/* we don't need to save the state, just drop it */
2461.23Smatt		save_fpu_lwp(l, FPU_DISCARD);
2471.23Smatt		(void)memcpy(&pcb->pcb_fpu.fpreg, &mcp->__fpregs.__fpu_regs,
2481.23Smatt		    sizeof (pcb->pcb_fpu.fpreg));
2491.20Smatt		((int *)&pcb->pcb_fpu.fpscr)[_QUAD_LOWWORD] =
2501.20Smatt		    mcp->__fpregs.__fpu_fpscr;
2511.8Sthorpej	}
2521.8Sthorpej#endif
2531.8Sthorpej
2541.22Smatt#ifdef ALTIVEC
2551.22Smatt	/* Restore AltiVec context, if any. */
2561.22Smatt	if (flags & _UC_POWERPC_VEC) {
2571.23Smatt		/* we don't need to save the state, just drop it */
2581.23Smatt		save_vec_lwp(l, ALTIVEC_DISCARD);
2591.22Smatt		(void)memcpy(pcb->pcb_vr.vreg, &mcp->__vrf.__vrs,
2601.22Smatt		    sizeof (pcb->pcb_vr.vreg));
2611.22Smatt		pcb->pcb_vr.vscr = mcp->__vrf.__vscr;
2621.22Smatt		pcb->pcb_vr.vrsave = mcp->__vrf.__vrsave;
2631.22Smatt	}
2641.22Smatt#endif
2651.22Smatt
2661.8Sthorpej	return (0);
2671.1Skleink}
268