11.55Sthorpej/*	$NetBSD: sig_machdep.c,v 1.55 2023/12/20 15:29:06 thorpej 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.55Sthorpej__KERNEL_RCSID(0, "$NetBSD: sig_machdep.c,v 1.55 2023/12/20 15:29:06 thorpej Exp $");
361.2Stsubai
371.52Srin#ifdef _KERNEL_OPT
381.52Srin#include "opt_altivec.h"
391.8Sthorpej#include "opt_ppcarch.h"
401.52Srin#endif
411.1Skleink
421.1Skleink#include <sys/param.h>
431.1Skleink#include <sys/mount.h>
441.1Skleink#include <sys/proc.h>
451.1Skleink#include <sys/syscallargs.h>
461.1Skleink#include <sys/systm.h>
471.8Sthorpej#include <sys/ucontext.h>
481.44Schs#include <sys/cpu.h>
491.1Skleink
501.36Srmind#include <uvm/uvm_extern.h>
511.36Srmind
521.22Smatt#include <powerpc/fpu.h>
531.22Smatt#include <powerpc/altivec.h>
541.37Srmind#include <powerpc/pcb.h>
551.41Smatt#include <powerpc/psl.h>
561.8Sthorpej
571.55Sthorpej/* Assert that the sizes of these two structures are multiples of 16. */
581.55SthorpejCTASSERT((sizeof(siginfo_t) & (CALLFRAMELEN-1)) == 0);
591.55SthorpejCTASSERT((sizeof(ucontext_t) & (CALLFRAMELEN-1)) == 0);
601.55Sthorpej
611.1Skleink/*
621.1Skleink * Send a signal to process.
631.1Skleink */
641.1Skleinkvoid
651.34Shesendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
661.1Skleink{
671.13Smatt	struct lwp * const l = curlwp;
681.13Smatt	struct proc * const p = l->l_proc;
691.39Smatt	struct trapframe * const tf = l->l_md.md_utf;
701.54Sthorpej	stack_t * const ss = &l->l_sigstk;
711.39Smatt	const struct sigact_sigdesc * const sd =
721.13Smatt	    &p->p_sigacts->sa_sigdesc[ksi->ksi_signo];
731.39Smatt	/* save handler before sendsig_reset trashes it! */
741.39Smatt	const void * const handler = sd->sd_sigact.sa_handler;
751.13Smatt	ucontext_t uc;
761.13Smatt	vaddr_t sp, sip, ucp;
771.27Sad	int onstack, error;
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.38Smatt	sp = (onstack ? (vaddr_t)ss->ss_sp + ss->ss_size : tf->tf_fixreg[1]);
851.55Sthorpej
861.55Sthorpej	/* Ensure it is aligned. */
871.13Smatt	sp &= ~(CALLFRAMELEN-1);
881.13Smatt
891.13Smatt	/* Allocate space for the ucontext.  */
901.13Smatt	sp -= sizeof(ucontext_t);
911.13Smatt
921.13Smatt	/* Allocate space for the siginfo.  */
931.13Smatt	sp -= sizeof(siginfo_t);
941.55Sthorpej
951.55Sthorpej#if 0	/* Not needed; see CTASSERTs above. */
961.55Sthorpej	/* Align it again. */
971.55Sthorpej	sp &= ~(CALLFRAMELEN-1);
981.55Sthorpej#endif
991.55Sthorpej
1001.13Smatt	sip = sp;
1011.55Sthorpej	ucp = sp + sizeof(siginfo_t);
1021.55Sthorpej
1031.55Sthorpej	KASSERT((sip & (CALLFRAMELEN-1)) == 0);
1041.55Sthorpej	KASSERT((ucp & (CALLFRAMELEN-1)) == 0);
1051.13Smatt
1061.55Sthorpej	/*
1071.55Sthorpej	 * Now allocate space for a call frame, so that there's
1081.55Sthorpej	 * space for the ABI-mandated stack linkage area in the
1091.55Sthorpej	 * event the signal handler calls a another function.
1101.55Sthorpej	 */
1111.55Sthorpej	sp -= CALLFRAMELEN;
1121.1Skleink
1131.1Skleink	/* Save register context. */
1141.46Smaxv	memset(&uc, 0, sizeof(uc));
1151.13Smatt	uc.uc_flags = _UC_SIGMASK;
1161.49Srin	uc.uc_flags |= (ss->ss_flags & SS_ONSTACK) ?
1171.49Srin	    _UC_SETSTACK : _UC_CLRSTACK;
1181.13Smatt	uc.uc_sigmask = *mask;
1191.30Spooka	uc.uc_link = l->l_ctxlink;
1201.27Sad	sendsig_reset(l, ksi->ksi_signo);
1211.32Sad	mutex_exit(p->p_lock);
1221.13Smatt	cpu_getmcontext(l, &uc.uc_mcontext, &uc.uc_flags);
1231.1Skleink
1241.1Skleink	/*
1251.13Smatt	 * Copy the siginfo and ucontext onto the user's stack.
1261.1Skleink	 */
1271.29Schristos	error = (copyout(&ksi->ksi_info, (void *)sip, sizeof(ksi->ksi_info)) != 0 ||
1281.29Schristos	    copyout(&uc, (void *)ucp, sizeof(uc)) != 0);
1291.32Sad	mutex_enter(p->p_lock);
1301.27Sad
1311.27Sad	if (error) {
1321.1Skleink		/*
1331.1Skleink		 * Process has trashed its stack; give it an illegal
1341.14Smatt		 * instruction to halt it in its tracks.
1351.1Skleink		 */
1361.8Sthorpej		sigexit(l, SIGILL);
1371.1Skleink		/* NOTREACHED */
1381.1Skleink	}
1391.1Skleink
1401.1Skleink	/*
1411.7Sthorpej	 * Build context to run handler in.  Note the trampoline version
1421.7Sthorpej	 * numbers are coordinated with machine-dependent code in libc.
1431.1Skleink	 */
1441.13Smatt	switch (sd->sd_vers) {
1451.53Sthorpej	case __SIGTRAMP_SIGINFO_VERSION:	/* siginfo sigtramp */
1461.55Sthorpej		tf->tf_fixreg[1]  = (register_t)sp;
1471.38Smatt		tf->tf_fixreg[3]  = (register_t)ksi->ksi_signo;
1481.38Smatt		tf->tf_fixreg[4]  = (register_t)sip;
1491.38Smatt		tf->tf_fixreg[5]  = (register_t)ucp;
1501.14Smatt		/* Preserve ucp across call to signal function */
1511.38Smatt		tf->tf_fixreg[30] = (register_t)ucp;
1521.38Smatt		tf->tf_lr         = (register_t)sd->sd_tramp;
1531.39Smatt		tf->tf_srr0       = (register_t)handler;
1541.7Sthorpej		break;
1551.7Sthorpej
1561.7Sthorpej	default:
1571.14Smatt		goto nosupport;
1581.7Sthorpej	}
1591.1Skleink
1601.1Skleink	/* Remember that we're now on the signal stack. */
1611.1Skleink	if (onstack)
1621.13Smatt		ss->ss_flags |= SS_ONSTACK;
1631.14Smatt	return;
1641.14Smatt
1651.14Smatt nosupport:
1661.14Smatt	/* Don't know what trampoline version; kill it. */
1671.14Smatt	printf("sendsig_siginfo(sig %d): bad version %d\n",
1681.17Smatt	    ksi->ksi_signo, sd->sd_vers);
1691.14Smatt	sigexit(l, SIGILL);
1701.14Smatt	/* NOTREACHED */
1711.8Sthorpej}
1721.8Sthorpej
1731.8Sthorpejvoid
1741.13Smattcpu_getmcontext(struct lwp *l, mcontext_t *mcp, unsigned int *flagp)
1751.8Sthorpej{
1761.40Smatt	const struct trapframe * const tf = l->l_md.md_utf;
1771.38Smatt	__greg_t * const gr = mcp->__gregs;
1781.38Smatt#if defined(PPC_HAVE_FPU)
1791.38Smatt	struct pcb * const pcb = lwp_getpcb(l);
1801.8Sthorpej#endif
1811.8Sthorpej
1821.8Sthorpej	/* Save GPR context. */
1831.38Smatt	(void)memcpy(gr, &tf->tf_fixreg, 32 * sizeof (gr[0])); /* GR0-31 */
1841.38Smatt	gr[_REG_CR]  = tf->tf_cr;
1851.38Smatt	gr[_REG_LR]  = tf->tf_lr;
1861.38Smatt	gr[_REG_PC]  = tf->tf_srr0;
1871.38Smatt	gr[_REG_MSR] = tf->tf_srr1 & PSL_USERSRR1;
1881.21Smatt#ifdef PPC_HAVE_FPU
1891.21Smatt	gr[_REG_MSR] |= pcb->pcb_flags & (PCB_FE0|PCB_FE1);
1901.21Smatt#endif
1911.38Smatt	gr[_REG_CTR] = tf->tf_ctr;
1921.38Smatt	gr[_REG_XER] = tf->tf_xer;
1931.11Smatt#ifdef PPC_OEA
1941.38Smatt	gr[_REG_MQ]  = tf->tf_mq;
1951.11Smatt#else
1961.11Smatt	gr[_REG_MQ]  = 0;
1971.11Smatt#endif
1981.38Smatt
1991.8Sthorpej	*flagp |= _UC_CPU;
2001.44Schs	*flagp |= _UC_TLSBASE;
2011.8Sthorpej
2021.8Sthorpej#ifdef PPC_HAVE_FPU
2031.38Smatt	/* Save FPU context, if any. */
2041.38Smatt	if (!fpu_save_to_mcontext(l, mcp, flagp))
2051.8Sthorpej#endif
2061.13Smatt		memset(&mcp->__fpregs, 0, sizeof(mcp->__fpregs));
2071.8Sthorpej
2081.38Smatt#if defined(ALTIVEC) || defined(PPC_HAVE_SPE)
2091.38Smatt	/* Save vector context, if any. */
2101.38Smatt	if (!vec_save_to_mcontext(l, mcp, flagp))
2111.22Smatt#endif
2121.22Smatt		memset(&mcp->__vrf, 0, sizeof (mcp->__vrf));
2131.8Sthorpej}
2141.8Sthorpej
2151.8Sthorpejint
2161.42Smartincpu_mcontext_validate(struct lwp *l, const mcontext_t *mcp)
2171.42Smartin{
2181.42Smartin	return 0;
2191.42Smartin}
2201.42Smartin
2211.42Smartinint
2221.13Smattcpu_setmcontext(struct lwp *l, const mcontext_t *mcp, unsigned int flags)
2231.8Sthorpej{
2241.40Smatt	struct trapframe * const tf = l->l_md.md_utf;
2251.38Smatt	const __greg_t * const gr = mcp->__gregs;
2261.49Srin	struct proc * const p = l->l_proc;
2271.42Smartin	int error;
2281.8Sthorpej
2291.8Sthorpej	/* Restore GPR context, if any. */
2301.8Sthorpej	if (flags & _UC_CPU) {
2311.42Smartin		error = cpu_mcontext_validate(l, mcp);
2321.42Smartin		if (error)
2331.42Smartin			return error;
2341.42Smartin
2351.21Smatt#ifdef PPC_HAVE_FPU
2361.21Smatt		/*
2371.21Smatt		 * Always save the FP exception mode in the PCB.
2381.21Smatt		 */
2391.38Smatt		struct pcb * const pcb = lwp_getpcb(l);
2401.21Smatt		pcb->pcb_flags &= ~(PCB_FE0|PCB_FE1);
2411.21Smatt		pcb->pcb_flags |= gr[_REG_MSR] & (PCB_FE0|PCB_FE1);
2421.21Smatt#endif
2431.21Smatt
2441.45Schs		/*
2451.45Schs		 * R2 is the TLS register so avoid updating it here.
2461.45Schs		 */
2471.45Schs
2481.45Schs		__greg_t save_r2 = tf->tf_fixreg[_REG_R2];
2491.38Smatt		(void)memcpy(&tf->tf_fixreg, gr, 32 * sizeof (gr[0]));
2501.45Schs		tf->tf_fixreg[_REG_R2] = save_r2;
2511.38Smatt		tf->tf_cr   = gr[_REG_CR];
2521.38Smatt		tf->tf_lr   = gr[_REG_LR];
2531.38Smatt		tf->tf_srr0 = gr[_REG_PC];
2541.45Schs
2551.26She		/*
2561.26She		 * Accept all user-settable bits without complaint;
2571.26She		 * userland should not need to know the machine-specific
2581.26She		 * MSR value.
2591.26She		 */
2601.38Smatt		tf->tf_srr1 = (gr[_REG_MSR] & PSL_USERMOD) | PSL_USERSET;
2611.38Smatt		tf->tf_ctr  = gr[_REG_CTR];
2621.38Smatt		tf->tf_xer  = gr[_REG_XER];
2631.11Smatt#ifdef PPC_OEA
2641.38Smatt		tf->tf_mq = gr[_REG_MQ];
2651.11Smatt#endif
2661.8Sthorpej	}
2671.8Sthorpej
2681.45Schs	if (flags & _UC_TLSBASE)
2691.51Srin		lwp_setprivate(l, (void *)(uintptr_t)gr[_REG_R2]);
2701.45Schs
2711.38Smatt#ifdef PPC_HAVE_FPU
2721.38Smatt	/* Restore FPU context, if any. */
2731.38Smatt	if (flags & _UC_FPU)
2741.38Smatt		fpu_restore_from_mcontext(l, mcp);
2751.8Sthorpej#endif
2761.8Sthorpej
2771.22Smatt#ifdef ALTIVEC
2781.22Smatt	/* Restore AltiVec context, if any. */
2791.38Smatt	if (flags & _UC_POWERPC_VEC)
2801.38Smatt		vec_restore_from_mcontext(l, mcp);
2811.38Smatt#endif
2821.38Smatt
2831.38Smatt#ifdef PPC_HAVE_SPE
2841.38Smatt	/* Restore SPE context, if any. */
2851.38Smatt	if (flags & _UC_POWERPC_SPE)
2861.38Smatt		vec_restore_from_mcontext(l, mcp);
2871.22Smatt#endif
2881.22Smatt
2891.49Srin	mutex_enter(p->p_lock);
2901.49Srin	if (flags & _UC_SETSTACK)
2911.49Srin		l->l_sigstk.ss_flags |= SS_ONSTACK;
2921.49Srin	if (flags & _UC_CLRSTACK)
2931.49Srin		l->l_sigstk.ss_flags &= ~SS_ONSTACK;
2941.49Srin	mutex_exit(p->p_lock);
2951.49Srin
2961.8Sthorpej	return (0);
2971.1Skleink}
2981.44Schs
2991.44Schsint
3001.44Schscpu_lwp_setprivate(lwp_t *l, void *addr)
3011.44Schs{
3021.44Schs	struct trapframe * const tf = l->l_md.md_utf;
3031.44Schs
3041.51Srin	tf->tf_fixreg[_REG_R2] = (register_t)addr;
3051.50Srin
3061.44Schs	return 0;
3071.44Schs}
308