sig_machdep.c revision 1.38
11.38Smatt/*	$NetBSD: sig_machdep.c,v 1.38 2011/01/18 01:02:55 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.12Slukem
341.12Slukem#include <sys/cdefs.h>
351.38Smatt__KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.38 2011/01/18 01:02:55 matt 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.36Srmind#include <uvm/uvm_extern.h>
481.36Srmind
491.22Smatt#include <powerpc/fpu.h>
501.22Smatt#include <powerpc/altivec.h>
511.37Srmind#include <powerpc/pcb.h>
521.8Sthorpej
531.1Skleink/*
541.1Skleink * Send a signal to process.
551.1Skleink */
561.1Skleinkvoid
571.34Shesendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
581.1Skleink{
591.13Smatt	struct lwp * const l = curlwp;
601.13Smatt	struct proc * const p = l->l_proc;
611.13Smatt	struct trapframe * const tf = trapframe(l);
621.27Sad	struct sigaltstack *ss = &l->l_sigstk;
631.13Smatt	const struct sigact_sigdesc *sd =
641.13Smatt	    &p->p_sigacts->sa_sigdesc[ksi->ksi_signo];
651.13Smatt	ucontext_t uc;
661.13Smatt	vaddr_t sp, sip, ucp;
671.27Sad	int onstack, error;
681.1Skleink
691.1Skleink	/* Do we need to jump onto the signal stack? */
701.13Smatt	onstack = (ss->ss_flags & (SS_DISABLE | SS_ONSTACK)) == 0 &&
711.13Smatt	    (sd->sd_sigact.sa_flags & SA_ONSTACK) != 0;
721.1Skleink
731.13Smatt	/* Find top of stack.  */
741.38Smatt	sp = (onstack ? (vaddr_t)ss->ss_sp + ss->ss_size : tf->tf_fixreg[1]);
751.13Smatt	sp &= ~(CALLFRAMELEN-1);
761.13Smatt
771.13Smatt	/* Allocate space for the ucontext.  */
781.13Smatt	sp -= sizeof(ucontext_t);
791.13Smatt	ucp = sp;
801.13Smatt
811.13Smatt	/* Allocate space for the siginfo.  */
821.13Smatt	sp -= sizeof(siginfo_t);
831.13Smatt	sip = sp;
841.13Smatt
851.13Smatt	sp &= ~(CALLFRAMELEN-1);
861.1Skleink
871.1Skleink	/* Save register context. */
881.13Smatt	uc.uc_flags = _UC_SIGMASK;
891.13Smatt	uc.uc_sigmask = *mask;
901.30Spooka	uc.uc_link = l->l_ctxlink;
911.13Smatt	memset(&uc.uc_stack, 0, sizeof(uc.uc_stack));
921.27Sad	sendsig_reset(l, ksi->ksi_signo);
931.32Sad	mutex_exit(p->p_lock);
941.13Smatt	cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags);
951.1Skleink
961.1Skleink	/*
971.13Smatt	 * Copy the siginfo and ucontext onto the user's stack.
981.1Skleink	 */
991.29Schristos	error = (copyout(&ksi->ksi_info, (void *)sip, sizeof(ksi->ksi_info)) != 0 ||
1001.29Schristos	    copyout(&uc, (void *)ucp, sizeof(uc)) != 0);
1011.32Sad	mutex_enter(p->p_lock);
1021.27Sad
1031.27Sad	if (error) {
1041.1Skleink		/*
1051.1Skleink		 * Process has trashed its stack; give it an illegal
1061.14Smatt		 * instruction to halt it in its tracks.
1071.1Skleink		 */
1081.8Sthorpej		sigexit(l, SIGILL);
1091.1Skleink		/* NOTREACHED */
1101.1Skleink	}
1111.1Skleink
1121.1Skleink	/*
1131.7Sthorpej	 * Build context to run handler in.  Note the trampoline version
1141.7Sthorpej	 * numbers are coordinated with machine-dependent code in libc.
1151.1Skleink	 */
1161.13Smatt	switch (sd->sd_vers) {
1171.13Smatt	case 2:		/* siginfo sigtramp */
1181.38Smatt		tf->tf_fixreg[1]  = (register_t)sp - CALLFRAMELEN;
1191.38Smatt		tf->tf_fixreg[3]  = (register_t)ksi->ksi_signo;
1201.38Smatt		tf->tf_fixreg[4]  = (register_t)sip;
1211.38Smatt		tf->tf_fixreg[5]  = (register_t)ucp;
1221.14Smatt		/* Preserve ucp across call to signal function */
1231.38Smatt		tf->tf_fixreg[30] = (register_t)ucp;
1241.38Smatt		tf->tf_lr         = (register_t)sd->sd_tramp;
1251.38Smatt		tf->tf_srr0       = (register_t)sd->sd_sigact.sa_handler;
1261.7Sthorpej		break;
1271.7Sthorpej
1281.7Sthorpej	default:
1291.14Smatt		goto nosupport;
1301.7Sthorpej	}
1311.1Skleink
1321.1Skleink	/* Remember that we're now on the signal stack. */
1331.1Skleink	if (onstack)
1341.13Smatt		ss->ss_flags |= SS_ONSTACK;
1351.14Smatt	return;
1361.14Smatt
1371.14Smatt nosupport:
1381.14Smatt	/* Don't know what trampoline version; kill it. */
1391.14Smatt	printf("sendsig_siginfo(sig %d): bad version %d\n",
1401.17Smatt	    ksi->ksi_signo, sd->sd_vers);
1411.14Smatt	sigexit(l, SIGILL);
1421.14Smatt	/* NOTREACHED */
1431.8Sthorpej}
1441.8Sthorpej
1451.8Sthorpejvoid
1461.13Smattcpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp)
1471.8Sthorpej{
1481.38Smatt	const struct trapframe * const tf = trapframe(l);
1491.38Smatt	__greg_t * const gr = mcp->__gregs;
1501.38Smatt#if defined(PPC_HAVE_FPU)
1511.38Smatt	struct pcb * const pcb = lwp_getpcb(l);
1521.8Sthorpej#endif
1531.8Sthorpej
1541.8Sthorpej	/* Save GPR context. */
1551.38Smatt	(void)memcpy(gr, &tf->tf_fixreg, 32 * sizeof (gr[0])); /* GR0-31 */
1561.38Smatt	gr[_REG_CR]  = tf->tf_cr;
1571.38Smatt	gr[_REG_LR]  = tf->tf_lr;
1581.38Smatt	gr[_REG_PC]  = tf->tf_srr0;
1591.38Smatt	gr[_REG_MSR] = tf->tf_srr1 & PSL_USERSRR1;
1601.21Smatt#ifdef PPC_HAVE_FPU
1611.21Smatt	gr[_REG_MSR] |= pcb->pcb_flags & (PCB_FE0|PCB_FE1);
1621.21Smatt#endif
1631.38Smatt	gr[_REG_CTR] = tf->tf_ctr;
1641.38Smatt	gr[_REG_XER] = tf->tf_xer;
1651.11Smatt#ifdef PPC_OEA
1661.38Smatt	gr[_REG_MQ]  = tf->tf_mq;
1671.11Smatt#else
1681.11Smatt	gr[_REG_MQ]  = 0;
1691.11Smatt#endif
1701.38Smatt
1711.8Sthorpej	*flagp |= _UC_CPU;
1721.8Sthorpej
1731.8Sthorpej#ifdef PPC_HAVE_FPU
1741.38Smatt	/* Save FPU context, if any. */
1751.38Smatt	if (!fpu_save_to_mcontext(l, mcp, flagp))
1761.8Sthorpej#endif
1771.13Smatt		memset(&mcp->__fpregs, 0, sizeof(mcp->__fpregs));
1781.8Sthorpej
1791.38Smatt#if defined(ALTIVEC) || defined(PPC_HAVE_SPE)
1801.38Smatt	/* Save vector context, if any. */
1811.38Smatt	if (!vec_save_to_mcontext(l, mcp, flagp))
1821.22Smatt#endif
1831.22Smatt		memset(&mcp->__vrf, 0, sizeof (mcp->__vrf));
1841.8Sthorpej}
1851.8Sthorpej
1861.8Sthorpejint
1871.13Smattcpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
1881.8Sthorpej{
1891.38Smatt	struct trapframe * const tf = trapframe(l);
1901.38Smatt	const __greg_t * const gr = mcp->__gregs;
1911.8Sthorpej
1921.8Sthorpej	/* Restore GPR context, if any. */
1931.8Sthorpej	if (flags & _UC_CPU) {
1941.21Smatt#ifdef PPC_HAVE_FPU
1951.21Smatt		/*
1961.21Smatt		 * Always save the FP exception mode in the PCB.
1971.21Smatt		 */
1981.38Smatt		struct pcb * const pcb = lwp_getpcb(l);
1991.21Smatt		pcb->pcb_flags &= ~(PCB_FE0|PCB_FE1);
2001.21Smatt		pcb->pcb_flags |= gr[_REG_MSR] & (PCB_FE0|PCB_FE1);
2011.21Smatt#endif
2021.21Smatt
2031.38Smatt		(void)memcpy(&tf->tf_fixreg, gr, 32 * sizeof (gr[0]));
2041.38Smatt		tf->tf_cr   = gr[_REG_CR];
2051.38Smatt		tf->tf_lr   = gr[_REG_LR];
2061.38Smatt		tf->tf_srr0 = gr[_REG_PC];
2071.26She		/*
2081.26She		 * Accept all user-settable bits without complaint;
2091.26She		 * userland should not need to know the machine-specific
2101.26She		 * MSR value.
2111.26She		 */
2121.38Smatt		tf->tf_srr1 = (gr[_REG_MSR] & PSL_USERMOD) | PSL_USERSET;
2131.38Smatt		tf->tf_ctr  = gr[_REG_CTR];
2141.38Smatt		tf->tf_xer  = gr[_REG_XER];
2151.11Smatt#ifdef PPC_OEA
2161.38Smatt		tf->tf_mq = gr[_REG_MQ];
2171.11Smatt#endif
2181.8Sthorpej	}
2191.8Sthorpej
2201.38Smatt#ifdef PPC_HAVE_FPU
2211.38Smatt	/* Restore FPU context, if any. */
2221.38Smatt	if (flags & _UC_FPU)
2231.38Smatt		fpu_restore_from_mcontext(l, mcp);
2241.8Sthorpej#endif
2251.8Sthorpej
2261.22Smatt#ifdef ALTIVEC
2271.22Smatt	/* Restore AltiVec context, if any. */
2281.38Smatt	if (flags & _UC_POWERPC_VEC)
2291.38Smatt		vec_restore_from_mcontext(l, mcp);
2301.38Smatt#endif
2311.38Smatt
2321.38Smatt#ifdef PPC_HAVE_SPE
2331.38Smatt	/* Restore SPE context, if any. */
2341.38Smatt	if (flags & _UC_POWERPC_SPE)
2351.38Smatt		vec_restore_from_mcontext(l, mcp);
2361.22Smatt#endif
2371.22Smatt
2381.8Sthorpej	return (0);
2391.1Skleink}
240