xref: /illumos-gate/usr/src/uts/sparc/v9/os/v9dep.c (revision aad98a6d8e89f8f5a62a1793da807d4bc5e5b159)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
237c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/stack.h>
387c478bd9Sstevel@tonic-gate #include <sys/frame.h>
397c478bd9Sstevel@tonic-gate #include <sys/proc.h>
407c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
417c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
427c478bd9Sstevel@tonic-gate #include <sys/asm_linkage.h>
437c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
447c478bd9Sstevel@tonic-gate #include <sys/errno.h>
457c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
467c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
477c478bd9Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
487c478bd9Sstevel@tonic-gate #include <sys/debug.h>
497c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
507c478bd9Sstevel@tonic-gate #include <sys/machpcb.h>
517c478bd9Sstevel@tonic-gate #include <sys/psr_compat.h>
527c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
537c478bd9Sstevel@tonic-gate #include <sys/asi.h>
547c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
557c478bd9Sstevel@tonic-gate #include <sys/model.h>
567c478bd9Sstevel@tonic-gate #include <sys/panic.h>
577c478bd9Sstevel@tonic-gate #include <sys/exec.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * modify the lower 32bits of a uint64_t
617c478bd9Sstevel@tonic-gate  */
627c478bd9Sstevel@tonic-gate #define	SET_LOWER_32(all, lower)	\
637c478bd9Sstevel@tonic-gate 	(((uint64_t)(all) & 0xffffffff00000000) | (uint32_t)(lower))
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #define	MEMCPY_FPU_EN		2	/* fprs on and fpu_en == 0 */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static uint_t mkpsr(uint64_t tstate, uint32_t fprs);
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
707c478bd9Sstevel@tonic-gate static void fpuregset_32ton(const fpregset32_t *src, fpregset_t *dest,
717c478bd9Sstevel@tonic-gate     const struct fq32 *sfq, struct fq *dfq);
727c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Set floating-point registers.
767c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
777c478bd9Sstevel@tonic-gate  * called from code in /proc to set the registers of another lwp.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate void
807c478bd9Sstevel@tonic-gate setfpregs(klwp_t *lwp, fpregset_t *fp)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
837c478bd9Sstevel@tonic-gate 	kfpu_t *pfp;
847c478bd9Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
857c478bd9Sstevel@tonic-gate 	model_t model = lwp_getdatamodel(lwp);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	mpcb = lwptompcb(lwp);
887c478bd9Sstevel@tonic-gate 	pfp = lwptofpu(lwp);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/*
917c478bd9Sstevel@tonic-gate 	 * This is always true for both "real" fp programs and memcpy fp
927c478bd9Sstevel@tonic-gate 	 * programs, because we force fpu_en to MEMCPY_FPU_EN in getfpregs,
937c478bd9Sstevel@tonic-gate 	 * for the memcpy and threads cases where (fpu_en == 0) &&
947c478bd9Sstevel@tonic-gate 	 * (fpu_fprs & FPRS_FEF), if setfpregs is called after getfpregs.
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 	if (fp->fpu_en) {
977c478bd9Sstevel@tonic-gate 		kpreempt_disable();
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 		if (!(pfp->fpu_en) && (!(pfp->fpu_fprs & FPRS_FEF)) &&
1007c478bd9Sstevel@tonic-gate 		    fpu_exists) {
1017c478bd9Sstevel@tonic-gate 			/*
1027c478bd9Sstevel@tonic-gate 			 * He's not currently using the FPU but wants to in his
1037c478bd9Sstevel@tonic-gate 			 * new context - arrange for this on return to userland.
1047c478bd9Sstevel@tonic-gate 			 */
1057c478bd9Sstevel@tonic-gate 			pfp->fpu_fprs = (uint32_t)fprs;
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 		/*
1087c478bd9Sstevel@tonic-gate 		 * Get setfpregs to restore fpu_en to zero
1097c478bd9Sstevel@tonic-gate 		 * for the memcpy/threads case (where pfp->fpu_en == 0 &&
1107c478bd9Sstevel@tonic-gate 		 * (pfp->fp_fprs & FPRS_FEF) == FPRS_FEF).
1117c478bd9Sstevel@tonic-gate 		 */
1127c478bd9Sstevel@tonic-gate 		if (fp->fpu_en == MEMCPY_FPU_EN)
1137c478bd9Sstevel@tonic-gate 			fp->fpu_en = 0;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 		/*
1167c478bd9Sstevel@tonic-gate 		 * Load up a user's floating point context.
1177c478bd9Sstevel@tonic-gate 		 */
1187c478bd9Sstevel@tonic-gate 		if (fp->fpu_qcnt > MAXFPQ) 	/* plug security holes */
1197c478bd9Sstevel@tonic-gate 			fp->fpu_qcnt = MAXFPQ;
1207c478bd9Sstevel@tonic-gate 		fp->fpu_q_entrysize = sizeof (struct fq);
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 		/*
1237c478bd9Sstevel@tonic-gate 		 * For v9 kernel, copy all of the fp regs.
1247c478bd9Sstevel@tonic-gate 		 * For v8 kernel, copy v8 fp regs (lower half of v9 fp regs).
1257c478bd9Sstevel@tonic-gate 		 * Restore entire fsr for v9, only lower half for v8.
1267c478bd9Sstevel@tonic-gate 		 */
1277c478bd9Sstevel@tonic-gate 		(void) kcopy(fp, pfp, sizeof (fp->fpu_fr));
1287c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_LP64)
1297c478bd9Sstevel@tonic-gate 			pfp->fpu_fsr = fp->fpu_fsr;
1307c478bd9Sstevel@tonic-gate 		else
1317c478bd9Sstevel@tonic-gate 			pfp->fpu_fsr = SET_LOWER_32(pfp->fpu_fsr, fp->fpu_fsr);
1327c478bd9Sstevel@tonic-gate 		pfp->fpu_qcnt = fp->fpu_qcnt;
1337c478bd9Sstevel@tonic-gate 		pfp->fpu_q_entrysize = fp->fpu_q_entrysize;
1347c478bd9Sstevel@tonic-gate 		pfp->fpu_en = fp->fpu_en;
1357c478bd9Sstevel@tonic-gate 		pfp->fpu_q = mpcb->mpcb_fpu_q;
1367c478bd9Sstevel@tonic-gate 		if (fp->fpu_qcnt)
1377c478bd9Sstevel@tonic-gate 			(void) kcopy(fp->fpu_q, pfp->fpu_q,
1387c478bd9Sstevel@tonic-gate 			    fp->fpu_qcnt * fp->fpu_q_entrysize);
1397c478bd9Sstevel@tonic-gate 		/* FSR ignores these bits on load, so they can not be set */
1407c478bd9Sstevel@tonic-gate 		pfp->fpu_fsr &= ~(FSR_QNE|FSR_FTT);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 		/*
1437c478bd9Sstevel@tonic-gate 		 * If not the current process then resume() will handle it.
1447c478bd9Sstevel@tonic-gate 		 */
1457c478bd9Sstevel@tonic-gate 		if (lwp != ttolwp(curthread)) {
1467c478bd9Sstevel@tonic-gate 			/* force resume to reload fp regs */
1477c478bd9Sstevel@tonic-gate 			pfp->fpu_fprs |= FPRS_FEF;
1487c478bd9Sstevel@tonic-gate 			kpreempt_enable();
1497c478bd9Sstevel@tonic-gate 			return;
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		/*
1537c478bd9Sstevel@tonic-gate 		 * Load up FPU with new floating point context.
1547c478bd9Sstevel@tonic-gate 		 */
1557c478bd9Sstevel@tonic-gate 		if (fpu_exists) {
1567c478bd9Sstevel@tonic-gate 			pfp->fpu_fprs = _fp_read_fprs();
1577c478bd9Sstevel@tonic-gate 			if ((pfp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
1587c478bd9Sstevel@tonic-gate 				_fp_write_fprs(fprs);
1597c478bd9Sstevel@tonic-gate 				pfp->fpu_fprs = (uint32_t)fprs;
1607c478bd9Sstevel@tonic-gate #ifdef DEBUG
1617c478bd9Sstevel@tonic-gate 				if (fpdispr)
1627c478bd9Sstevel@tonic-gate 					cmn_err(CE_NOTE,
1637c478bd9Sstevel@tonic-gate 					    "setfpregs with fp disabled!\n");
1647c478bd9Sstevel@tonic-gate #endif
1657c478bd9Sstevel@tonic-gate 			}
1667c478bd9Sstevel@tonic-gate 			/*
1677c478bd9Sstevel@tonic-gate 			 * Load all fp regs for v9 user programs, but only
1687c478bd9Sstevel@tonic-gate 			 * load the lower half for v8[plus] programs.
1697c478bd9Sstevel@tonic-gate 			 */
1707c478bd9Sstevel@tonic-gate 			if (model == DATAMODEL_LP64)
1717c478bd9Sstevel@tonic-gate 				fp_restore(pfp);
1727c478bd9Sstevel@tonic-gate 			else
1737c478bd9Sstevel@tonic-gate 				fp_v8_load(pfp);
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 		kpreempt_enable();
1777c478bd9Sstevel@tonic-gate 	} else {
1787c478bd9Sstevel@tonic-gate 		if ((pfp->fpu_en) ||	/* normal fp case */
1797c478bd9Sstevel@tonic-gate 		    (pfp->fpu_fprs & FPRS_FEF)) { /* memcpy/threads case */
1807c478bd9Sstevel@tonic-gate 			/*
1817c478bd9Sstevel@tonic-gate 			 * Currently the lwp has floating point enabled.
1827c478bd9Sstevel@tonic-gate 			 * Turn off FPRS_FEF in user's fprs, saved and
1837c478bd9Sstevel@tonic-gate 			 * real copies thereof.
1847c478bd9Sstevel@tonic-gate 			 */
1857c478bd9Sstevel@tonic-gate 			pfp->fpu_en = 0;
1867c478bd9Sstevel@tonic-gate 			if (fpu_exists) {
1877c478bd9Sstevel@tonic-gate 				fprs = 0;
1887c478bd9Sstevel@tonic-gate 				if (lwp == ttolwp(curthread))
1897c478bd9Sstevel@tonic-gate 					_fp_write_fprs(fprs);
1907c478bd9Sstevel@tonic-gate 				pfp->fpu_fprs = (uint32_t)fprs;
1917c478bd9Sstevel@tonic-gate 			}
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
1977c478bd9Sstevel@tonic-gate void
1987c478bd9Sstevel@tonic-gate setfpregs32(klwp_t *lwp, fpregset32_t *fp)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	fpregset_t fpregs;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	fpuregset_32ton(fp, &fpregs, NULL, NULL);
2037c478bd9Sstevel@tonic-gate 	setfpregs(lwp, &fpregs);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate /*
2087c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
2097c478bd9Sstevel@tonic-gate  * called from code in /proc to set the registers of another lwp.
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate void
2127c478bd9Sstevel@tonic-gate run_fpq(klwp_t *lwp, fpregset_t *fp)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate 	/*
2157c478bd9Sstevel@tonic-gate 	 * If the context being loaded up includes a floating queue,
2167c478bd9Sstevel@tonic-gate 	 * we need to simulate those instructions (since we can't reload
2177c478bd9Sstevel@tonic-gate 	 * the fpu) and pass the process any appropriate signals
2187c478bd9Sstevel@tonic-gate 	 */
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	if (lwp == ttolwp(curthread)) {
2217c478bd9Sstevel@tonic-gate 		if (fpu_exists) {
2227c478bd9Sstevel@tonic-gate 			if (fp->fpu_qcnt)
2237c478bd9Sstevel@tonic-gate 				fp_runq(lwp->lwp_regs);
2247c478bd9Sstevel@tonic-gate 		}
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate }
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * Get floating-point registers.
2307c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
2317c478bd9Sstevel@tonic-gate  * called from code in /proc to set the registers of another lwp.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate void
2347c478bd9Sstevel@tonic-gate getfpregs(klwp_t *lwp, fpregset_t *fp)
2357c478bd9Sstevel@tonic-gate {
2367c478bd9Sstevel@tonic-gate 	kfpu_t *pfp;
2377c478bd9Sstevel@tonic-gate 	model_t model = lwp_getdatamodel(lwp);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	pfp = lwptofpu(lwp);
2407c478bd9Sstevel@tonic-gate 	kpreempt_disable();
2417c478bd9Sstevel@tonic-gate 	if (fpu_exists && ttolwp(curthread) == lwp)
2427c478bd9Sstevel@tonic-gate 		pfp->fpu_fprs = _fp_read_fprs();
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/*
2457c478bd9Sstevel@tonic-gate 	 * First check the fpu_en case, for normal fp programs.
2467c478bd9Sstevel@tonic-gate 	 * Next check the fprs case, for fp use by memcpy/threads.
2477c478bd9Sstevel@tonic-gate 	 */
2487c478bd9Sstevel@tonic-gate 	if (((fp->fpu_en = pfp->fpu_en) != 0) ||
2497c478bd9Sstevel@tonic-gate 	    (pfp->fpu_fprs & FPRS_FEF)) {
2507c478bd9Sstevel@tonic-gate 		/*
2517c478bd9Sstevel@tonic-gate 		 * Force setfpregs to restore the fp context in
2527c478bd9Sstevel@tonic-gate 		 * setfpregs for the memcpy and threads cases (where
2537c478bd9Sstevel@tonic-gate 		 * pfp->fpu_en == 0 && (pfp->fp_fprs & FPRS_FEF) == FPRS_FEF).
2547c478bd9Sstevel@tonic-gate 		 */
2557c478bd9Sstevel@tonic-gate 		if (pfp->fpu_en == 0)
2567c478bd9Sstevel@tonic-gate 			fp->fpu_en = MEMCPY_FPU_EN;
2577c478bd9Sstevel@tonic-gate 		/*
2587c478bd9Sstevel@tonic-gate 		 * If we have an fpu and the current thread owns the fp
2597c478bd9Sstevel@tonic-gate 		 * context, flush fp * registers into the pcb. Save all
2607c478bd9Sstevel@tonic-gate 		 * the fp regs for v9, xregs_getfpregs saves the upper half
2617c478bd9Sstevel@tonic-gate 		 * for v8plus. Save entire fsr for v9, only lower half for v8.
2627c478bd9Sstevel@tonic-gate 		 */
2637c478bd9Sstevel@tonic-gate 		if (fpu_exists && ttolwp(curthread) == lwp) {
2647c478bd9Sstevel@tonic-gate 			if ((pfp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
2657c478bd9Sstevel@tonic-gate 				uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 				_fp_write_fprs(fprs);
2687c478bd9Sstevel@tonic-gate 				pfp->fpu_fprs = fprs;
2697c478bd9Sstevel@tonic-gate #ifdef DEBUG
2707c478bd9Sstevel@tonic-gate 				if (fpdispr)
2717c478bd9Sstevel@tonic-gate 					cmn_err(CE_NOTE,
2727c478bd9Sstevel@tonic-gate 					    "getfpregs with fp disabled!\n");
2737c478bd9Sstevel@tonic-gate #endif
2747c478bd9Sstevel@tonic-gate 			}
2757c478bd9Sstevel@tonic-gate 			if (model == DATAMODEL_LP64)
2767c478bd9Sstevel@tonic-gate 				fp_fksave(pfp);
2777c478bd9Sstevel@tonic-gate 			else
2787c478bd9Sstevel@tonic-gate 				fp_v8_fksave(pfp);
2797c478bd9Sstevel@tonic-gate 		}
2807c478bd9Sstevel@tonic-gate 		(void) kcopy(pfp, fp, sizeof (fp->fpu_fr));
2817c478bd9Sstevel@tonic-gate 		fp->fpu_q = pfp->fpu_q;
2827c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_LP64)
2837c478bd9Sstevel@tonic-gate 			fp->fpu_fsr = pfp->fpu_fsr;
2847c478bd9Sstevel@tonic-gate 		else
2857c478bd9Sstevel@tonic-gate 			fp->fpu_fsr = (uint32_t)pfp->fpu_fsr;
2867c478bd9Sstevel@tonic-gate 		fp->fpu_qcnt = pfp->fpu_qcnt;
2877c478bd9Sstevel@tonic-gate 		fp->fpu_q_entrysize = pfp->fpu_q_entrysize;
2887c478bd9Sstevel@tonic-gate 	} else {
2897c478bd9Sstevel@tonic-gate 		int i;
2907c478bd9Sstevel@tonic-gate 		for (i = 0; i < 32; i++)		/* NaN */
2917c478bd9Sstevel@tonic-gate 			((uint32_t *)fp->fpu_fr.fpu_regs)[i] = (uint32_t)-1;
2927c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_LP64) {
2937c478bd9Sstevel@tonic-gate 			for (i = 16; i < 32; i++)	/* NaN */
2947c478bd9Sstevel@tonic-gate 				((uint64_t *)fp->fpu_fr.fpu_dregs)[i] =
2957c478bd9Sstevel@tonic-gate 				    (uint64_t)-1;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 		fp->fpu_fsr = 0;
2987c478bd9Sstevel@tonic-gate 		fp->fpu_qcnt = 0;
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	kpreempt_enable();
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
3047c478bd9Sstevel@tonic-gate void
3057c478bd9Sstevel@tonic-gate getfpregs32(klwp_t *lwp, fpregset32_t *fp)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	fpregset_t fpregs;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	getfpregs(lwp, &fpregs);
3107c478bd9Sstevel@tonic-gate 	fpuregset_nto32(&fpregs, fp, NULL);
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate  * Set general registers.
3167c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
3177c478bd9Sstevel@tonic-gate  * called from code in /proc to set the registers of another lwp.
3187c478bd9Sstevel@tonic-gate  */
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /* 64-bit gregset_t */
3217c478bd9Sstevel@tonic-gate void
3227c478bd9Sstevel@tonic-gate setgregs(klwp_t *lwp, gregset_t grp)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
3257c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
3267c478bd9Sstevel@tonic-gate 	uint64_t tbits;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	int current = (lwp == curthread->t_lwp);
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	if (current)
3317c478bd9Sstevel@tonic-gate 		(void) save_syscall_args();	/* copy the args first */
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	tbits = (((grp[REG_CCR] & TSTATE_CCR_MASK) << TSTATE_CCR_SHIFT) |
3347c478bd9Sstevel@tonic-gate 		((grp[REG_ASI] & TSTATE_ASI_MASK) << TSTATE_ASI_SHIFT));
3357c478bd9Sstevel@tonic-gate 	rp->r_tstate &= ~(((uint64_t)TSTATE_CCR_MASK << TSTATE_CCR_SHIFT) |
3367c478bd9Sstevel@tonic-gate 		((uint64_t)TSTATE_ASI_MASK << TSTATE_ASI_SHIFT));
3377c478bd9Sstevel@tonic-gate 	rp->r_tstate |= tbits;
3387c478bd9Sstevel@tonic-gate 	kpreempt_disable();
3397c478bd9Sstevel@tonic-gate 	fp->fpu_fprs = (uint32_t)grp[REG_FPRS];
3407c478bd9Sstevel@tonic-gate 	if (fpu_exists && (current) && (fp->fpu_fprs & FPRS_FEF))
3417c478bd9Sstevel@tonic-gate 		_fp_write_fprs(fp->fpu_fprs);
3427c478bd9Sstevel@tonic-gate 	kpreempt_enable();
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	 * pc and npc must be 4-byte aligned on sparc.
3467c478bd9Sstevel@tonic-gate 	 * We silently make it so to avoid a watchdog reset.
3477c478bd9Sstevel@tonic-gate 	 */
3487c478bd9Sstevel@tonic-gate 	rp->r_pc = grp[REG_PC] & ~03L;
3497c478bd9Sstevel@tonic-gate 	rp->r_npc = grp[REG_nPC] & ~03L;
3507c478bd9Sstevel@tonic-gate 	rp->r_y = grp[REG_Y];
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	rp->r_g1 = grp[REG_G1];
3537c478bd9Sstevel@tonic-gate 	rp->r_g2 = grp[REG_G2];
3547c478bd9Sstevel@tonic-gate 	rp->r_g3 = grp[REG_G3];
3557c478bd9Sstevel@tonic-gate 	rp->r_g4 = grp[REG_G4];
3567c478bd9Sstevel@tonic-gate 	rp->r_g5 = grp[REG_G5];
3577c478bd9Sstevel@tonic-gate 	rp->r_g6 = grp[REG_G6];
3587c478bd9Sstevel@tonic-gate 	rp->r_g7 = grp[REG_G7];
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	rp->r_o0 = grp[REG_O0];
3617c478bd9Sstevel@tonic-gate 	rp->r_o1 = grp[REG_O1];
3627c478bd9Sstevel@tonic-gate 	rp->r_o2 = grp[REG_O2];
3637c478bd9Sstevel@tonic-gate 	rp->r_o3 = grp[REG_O3];
3647c478bd9Sstevel@tonic-gate 	rp->r_o4 = grp[REG_O4];
3657c478bd9Sstevel@tonic-gate 	rp->r_o5 = grp[REG_O5];
3667c478bd9Sstevel@tonic-gate 	rp->r_o6 = grp[REG_O6];
3677c478bd9Sstevel@tonic-gate 	rp->r_o7 = grp[REG_O7];
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	if (current) {
3707c478bd9Sstevel@tonic-gate 		/*
3717c478bd9Sstevel@tonic-gate 		 * This was called from a system call, but we
3727c478bd9Sstevel@tonic-gate 		 * do not want to return via the shared window;
3737c478bd9Sstevel@tonic-gate 		 * restoring the CPU context changes everything.
3747c478bd9Sstevel@tonic-gate 		 */
3757c478bd9Sstevel@tonic-gate 		lwp->lwp_eosys = JUSTRETURN;
3767c478bd9Sstevel@tonic-gate 		curthread->t_post_sys = 1;
3777c478bd9Sstevel@tonic-gate 	}
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate /*
3817c478bd9Sstevel@tonic-gate  * Return the general registers.
3827c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
3837c478bd9Sstevel@tonic-gate  * called from code in /proc to get the registers of another lwp.
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate void
3867c478bd9Sstevel@tonic-gate getgregs(klwp_t *lwp, gregset_t grp)
3877c478bd9Sstevel@tonic-gate {
3887c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
3897c478bd9Sstevel@tonic-gate 	uint32_t fprs;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	kpreempt_disable();
3927c478bd9Sstevel@tonic-gate 	if (fpu_exists && ttolwp(curthread) == lwp) {
3937c478bd9Sstevel@tonic-gate 		fprs = _fp_read_fprs();
3947c478bd9Sstevel@tonic-gate 	} else {
3957c478bd9Sstevel@tonic-gate 		kfpu_t *fp = lwptofpu(lwp);
3967c478bd9Sstevel@tonic-gate 		fprs = fp->fpu_fprs;
3977c478bd9Sstevel@tonic-gate 	}
3987c478bd9Sstevel@tonic-gate 	kpreempt_enable();
3997c478bd9Sstevel@tonic-gate 	grp[REG_CCR] = (rp->r_tstate >> TSTATE_CCR_SHIFT) & TSTATE_CCR_MASK;
4007c478bd9Sstevel@tonic-gate 	grp[REG_PC] = rp->r_pc;
4017c478bd9Sstevel@tonic-gate 	grp[REG_nPC] = rp->r_npc;
4027c478bd9Sstevel@tonic-gate 	grp[REG_Y] = (uint32_t)rp->r_y;
4037c478bd9Sstevel@tonic-gate 	grp[REG_G1] = rp->r_g1;
4047c478bd9Sstevel@tonic-gate 	grp[REG_G2] = rp->r_g2;
4057c478bd9Sstevel@tonic-gate 	grp[REG_G3] = rp->r_g3;
4067c478bd9Sstevel@tonic-gate 	grp[REG_G4] = rp->r_g4;
4077c478bd9Sstevel@tonic-gate 	grp[REG_G5] = rp->r_g5;
4087c478bd9Sstevel@tonic-gate 	grp[REG_G6] = rp->r_g6;
4097c478bd9Sstevel@tonic-gate 	grp[REG_G7] = rp->r_g7;
4107c478bd9Sstevel@tonic-gate 	grp[REG_O0] = rp->r_o0;
4117c478bd9Sstevel@tonic-gate 	grp[REG_O1] = rp->r_o1;
4127c478bd9Sstevel@tonic-gate 	grp[REG_O2] = rp->r_o2;
4137c478bd9Sstevel@tonic-gate 	grp[REG_O3] = rp->r_o3;
4147c478bd9Sstevel@tonic-gate 	grp[REG_O4] = rp->r_o4;
4157c478bd9Sstevel@tonic-gate 	grp[REG_O5] = rp->r_o5;
4167c478bd9Sstevel@tonic-gate 	grp[REG_O6] = rp->r_o6;
4177c478bd9Sstevel@tonic-gate 	grp[REG_O7] = rp->r_o7;
4187c478bd9Sstevel@tonic-gate 	grp[REG_ASI] = (rp->r_tstate >> TSTATE_ASI_SHIFT) & TSTATE_ASI_MASK;
4197c478bd9Sstevel@tonic-gate 	grp[REG_FPRS] = fprs;
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate void
4237c478bd9Sstevel@tonic-gate getgregs32(klwp_t *lwp, gregset32_t grp)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
4267c478bd9Sstevel@tonic-gate 	uint32_t fprs;
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 	kpreempt_disable();
4297c478bd9Sstevel@tonic-gate 	if (fpu_exists && ttolwp(curthread) == lwp) {
4307c478bd9Sstevel@tonic-gate 		fprs = _fp_read_fprs();
4317c478bd9Sstevel@tonic-gate 	} else {
4327c478bd9Sstevel@tonic-gate 		kfpu_t *fp = lwptofpu(lwp);
4337c478bd9Sstevel@tonic-gate 		fprs = fp->fpu_fprs;
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate 	kpreempt_enable();
4367c478bd9Sstevel@tonic-gate 	grp[REG_PSR] = mkpsr(rp->r_tstate, fprs);
4377c478bd9Sstevel@tonic-gate 	grp[REG_PC] = rp->r_pc;
4387c478bd9Sstevel@tonic-gate 	grp[REG_nPC] = rp->r_npc;
4397c478bd9Sstevel@tonic-gate 	grp[REG_Y] = rp->r_y;
4407c478bd9Sstevel@tonic-gate 	grp[REG_G1] = rp->r_g1;
4417c478bd9Sstevel@tonic-gate 	grp[REG_G2] = rp->r_g2;
4427c478bd9Sstevel@tonic-gate 	grp[REG_G3] = rp->r_g3;
4437c478bd9Sstevel@tonic-gate 	grp[REG_G4] = rp->r_g4;
4447c478bd9Sstevel@tonic-gate 	grp[REG_G5] = rp->r_g5;
4457c478bd9Sstevel@tonic-gate 	grp[REG_G6] = rp->r_g6;
4467c478bd9Sstevel@tonic-gate 	grp[REG_G7] = rp->r_g7;
4477c478bd9Sstevel@tonic-gate 	grp[REG_O0] = rp->r_o0;
4487c478bd9Sstevel@tonic-gate 	grp[REG_O1] = rp->r_o1;
4497c478bd9Sstevel@tonic-gate 	grp[REG_O2] = rp->r_o2;
4507c478bd9Sstevel@tonic-gate 	grp[REG_O3] = rp->r_o3;
4517c478bd9Sstevel@tonic-gate 	grp[REG_O4] = rp->r_o4;
4527c478bd9Sstevel@tonic-gate 	grp[REG_O5] = rp->r_o5;
4537c478bd9Sstevel@tonic-gate 	grp[REG_O6] = rp->r_o6;
4547c478bd9Sstevel@tonic-gate 	grp[REG_O7] = rp->r_o7;
4557c478bd9Sstevel@tonic-gate }
4567c478bd9Sstevel@tonic-gate 
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate  * Return the user-level PC.
4597c478bd9Sstevel@tonic-gate  * If in a system call, return the address of the syscall trap.
4607c478bd9Sstevel@tonic-gate  */
4617c478bd9Sstevel@tonic-gate greg_t
4627c478bd9Sstevel@tonic-gate getuserpc()
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	return (lwptoregs(ttolwp(curthread))->r_pc);
4657c478bd9Sstevel@tonic-gate }
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate /*
4687c478bd9Sstevel@tonic-gate  * Set register windows.
4697c478bd9Sstevel@tonic-gate  */
4707c478bd9Sstevel@tonic-gate void
4717c478bd9Sstevel@tonic-gate setgwins(klwp_t *lwp, gwindows_t *gwins)
4727c478bd9Sstevel@tonic-gate {
4737c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
4747c478bd9Sstevel@tonic-gate 	int wbcnt = gwins->wbcnt;
4757c478bd9Sstevel@tonic-gate 	caddr_t sp;
4767c478bd9Sstevel@tonic-gate 	int i;
4777c478bd9Sstevel@tonic-gate 	struct rwindow32 *rwp;
4787c478bd9Sstevel@tonic-gate 	int wbuf_rwindow_size;
4797c478bd9Sstevel@tonic-gate 	int is64;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate == WSTATE_USER32) {
4827c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE32;
4837c478bd9Sstevel@tonic-gate 		is64 = 0;
4847c478bd9Sstevel@tonic-gate 	} else {
4857c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE64;
4867c478bd9Sstevel@tonic-gate 		is64 = 1;
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	ASSERT(wbcnt >= 0 && wbcnt <= SPARC_MAXREGWINDOW);
4897c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;
4907c478bd9Sstevel@tonic-gate 	for (i = 0; i < wbcnt; i++) {
4917c478bd9Sstevel@tonic-gate 		sp = (caddr_t)gwins->spbuf[i];
4927c478bd9Sstevel@tonic-gate 		mpcb->mpcb_spbuf[i] = sp;
4937c478bd9Sstevel@tonic-gate 		rwp = (struct rwindow32 *)
4947c478bd9Sstevel@tonic-gate 			(mpcb->mpcb_wbuf + (i * wbuf_rwindow_size));
4957c478bd9Sstevel@tonic-gate 		if (is64 && IS_V9STACK(sp))
4967c478bd9Sstevel@tonic-gate 			bcopy(&gwins->wbuf[i], rwp, sizeof (struct rwindow));
4977c478bd9Sstevel@tonic-gate 		else
4987c478bd9Sstevel@tonic-gate 			rwindow_nto32(&gwins->wbuf[i], rwp);
4997c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wbcnt++;
5007c478bd9Sstevel@tonic-gate 	}
5017c478bd9Sstevel@tonic-gate }
5027c478bd9Sstevel@tonic-gate 
5037c478bd9Sstevel@tonic-gate void
5047c478bd9Sstevel@tonic-gate setgwins32(klwp_t *lwp, gwindows32_t *gwins)
5057c478bd9Sstevel@tonic-gate {
5067c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
5077c478bd9Sstevel@tonic-gate 	int wbcnt = gwins->wbcnt;
5087c478bd9Sstevel@tonic-gate 	caddr_t sp;
5097c478bd9Sstevel@tonic-gate 	int i;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	struct rwindow *rwp;
5127c478bd9Sstevel@tonic-gate 	int wbuf_rwindow_size;
5137c478bd9Sstevel@tonic-gate 	int is64;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate == WSTATE_USER32) {
5167c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE32;
5177c478bd9Sstevel@tonic-gate 		is64 = 0;
5187c478bd9Sstevel@tonic-gate 	} else {
5197c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE64;
5207c478bd9Sstevel@tonic-gate 		is64 = 1;
5217c478bd9Sstevel@tonic-gate 	}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	ASSERT(wbcnt >= 0 && wbcnt <= SPARC_MAXREGWINDOW);
5247c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;
5257c478bd9Sstevel@tonic-gate 	for (i = 0; i < wbcnt; i++) {
526*aad98a6dSmathue 		sp = (caddr_t)(uintptr_t)gwins->spbuf[i];
5277c478bd9Sstevel@tonic-gate 		mpcb->mpcb_spbuf[i] = sp;
5287c478bd9Sstevel@tonic-gate 		rwp = (struct rwindow *)
5297c478bd9Sstevel@tonic-gate 			(mpcb->mpcb_wbuf + (i * wbuf_rwindow_size));
5307c478bd9Sstevel@tonic-gate 		if (is64 && IS_V9STACK(sp))
5317c478bd9Sstevel@tonic-gate 			rwindow_32ton(&gwins->wbuf[i], rwp);
5327c478bd9Sstevel@tonic-gate 		else
5337c478bd9Sstevel@tonic-gate 			bcopy(&gwins->wbuf[i], rwp, sizeof (struct rwindow32));
5347c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wbcnt++;
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  * Get register windows.
5407c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
5417c478bd9Sstevel@tonic-gate  * called from code in /proc to set the registers of another lwp.
5427c478bd9Sstevel@tonic-gate  */
5437c478bd9Sstevel@tonic-gate void
5447c478bd9Sstevel@tonic-gate getgwins(klwp_t *lwp, gwindows_t *gwp)
5457c478bd9Sstevel@tonic-gate {
5467c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
5477c478bd9Sstevel@tonic-gate 	int wbcnt = mpcb->mpcb_wbcnt;
5487c478bd9Sstevel@tonic-gate 	caddr_t sp;
5497c478bd9Sstevel@tonic-gate 	int i;
5507c478bd9Sstevel@tonic-gate 	struct rwindow32 *rwp;
5517c478bd9Sstevel@tonic-gate 	int wbuf_rwindow_size;
5527c478bd9Sstevel@tonic-gate 	int is64;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate == WSTATE_USER32) {
5557c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE32;
5567c478bd9Sstevel@tonic-gate 		is64 = 0;
5577c478bd9Sstevel@tonic-gate 	} else {
5587c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE64;
5597c478bd9Sstevel@tonic-gate 		is64 = 1;
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 	ASSERT(wbcnt >= 0 && wbcnt <= SPARC_MAXREGWINDOW);
5627c478bd9Sstevel@tonic-gate 	gwp->wbcnt = wbcnt;
5637c478bd9Sstevel@tonic-gate 	for (i = 0; i < wbcnt; i++) {
5647c478bd9Sstevel@tonic-gate 		sp = mpcb->mpcb_spbuf[i];
5657c478bd9Sstevel@tonic-gate 		gwp->spbuf[i] = (greg_t *)sp;
5667c478bd9Sstevel@tonic-gate 		rwp = (struct rwindow32 *)
5677c478bd9Sstevel@tonic-gate 			(mpcb->mpcb_wbuf + (i * wbuf_rwindow_size));
5687c478bd9Sstevel@tonic-gate 		if (is64 && IS_V9STACK(sp))
5697c478bd9Sstevel@tonic-gate 			bcopy(rwp, &gwp->wbuf[i], sizeof (struct rwindow));
5707c478bd9Sstevel@tonic-gate 		else
5717c478bd9Sstevel@tonic-gate 			rwindow_32ton(rwp, &gwp->wbuf[i]);
5727c478bd9Sstevel@tonic-gate 	}
5737c478bd9Sstevel@tonic-gate }
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate void
5767c478bd9Sstevel@tonic-gate getgwins32(klwp_t *lwp, gwindows32_t *gwp)
5777c478bd9Sstevel@tonic-gate {
5787c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
5797c478bd9Sstevel@tonic-gate 	int wbcnt = mpcb->mpcb_wbcnt;
5807c478bd9Sstevel@tonic-gate 	int i;
5817c478bd9Sstevel@tonic-gate 	struct rwindow *rwp;
5827c478bd9Sstevel@tonic-gate 	int wbuf_rwindow_size;
5837c478bd9Sstevel@tonic-gate 	caddr_t sp;
5847c478bd9Sstevel@tonic-gate 	int is64;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate == WSTATE_USER32) {
5877c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE32;
5887c478bd9Sstevel@tonic-gate 		is64 = 0;
5897c478bd9Sstevel@tonic-gate 	} else {
5907c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE64;
5917c478bd9Sstevel@tonic-gate 		is64 = 1;
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	ASSERT(wbcnt >= 0 && wbcnt <= SPARC_MAXREGWINDOW);
5957c478bd9Sstevel@tonic-gate 	gwp->wbcnt = wbcnt;
5967c478bd9Sstevel@tonic-gate 	for (i = 0; i < wbcnt; i++) {
5977c478bd9Sstevel@tonic-gate 		sp = mpcb->mpcb_spbuf[i];
5987c478bd9Sstevel@tonic-gate 		rwp = (struct rwindow *)
5997c478bd9Sstevel@tonic-gate 			(mpcb->mpcb_wbuf + (i * wbuf_rwindow_size));
600*aad98a6dSmathue 		gwp->spbuf[i] = (caddr32_t)(uintptr_t)sp;
6017c478bd9Sstevel@tonic-gate 		if (is64 && IS_V9STACK(sp))
6027c478bd9Sstevel@tonic-gate 			rwindow_nto32(rwp, &gwp->wbuf[i]);
6037c478bd9Sstevel@tonic-gate 		else
6047c478bd9Sstevel@tonic-gate 			bcopy(rwp, &gwp->wbuf[i], sizeof (struct rwindow32));
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate }
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate /*
6097c478bd9Sstevel@tonic-gate  * For things that depend on register state being on the stack,
6107c478bd9Sstevel@tonic-gate  * copy any register windows that get saved into the window buffer
6117c478bd9Sstevel@tonic-gate  * (in the pcb) onto the stack.  This normally gets fixed up
6127c478bd9Sstevel@tonic-gate  * before returning to a user program.  Callers of this routine
6137c478bd9Sstevel@tonic-gate  * require this to happen immediately because a later kernel
6147c478bd9Sstevel@tonic-gate  * operation depends on window state (like instruction simulation).
6157c478bd9Sstevel@tonic-gate  */
6167c478bd9Sstevel@tonic-gate int
6177c478bd9Sstevel@tonic-gate flush_user_windows_to_stack(caddr_t *psp)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	int j, k;
6207c478bd9Sstevel@tonic-gate 	caddr_t sp;
6217c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(ttolwp(curthread));
6227c478bd9Sstevel@tonic-gate 	int err;
6237c478bd9Sstevel@tonic-gate 	int error = 0;
6247c478bd9Sstevel@tonic-gate 	int wbuf_rwindow_size;
6257c478bd9Sstevel@tonic-gate 	int rwindow_size;
6267c478bd9Sstevel@tonic-gate 	int stack_align;
6277c478bd9Sstevel@tonic-gate 	int watched;
6287c478bd9Sstevel@tonic-gate 
6297c478bd9Sstevel@tonic-gate 	flush_user_windows();
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate != WSTATE_USER32)
6327c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE64;
6337c478bd9Sstevel@tonic-gate 	else
6347c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE32;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	j = mpcb->mpcb_wbcnt;
6377c478bd9Sstevel@tonic-gate 	while (j > 0) {
6387c478bd9Sstevel@tonic-gate 		sp = mpcb->mpcb_spbuf[--j];
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		if ((mpcb->mpcb_wstate != WSTATE_USER32) &&
6417c478bd9Sstevel@tonic-gate 		    IS_V9STACK(sp)) {
6427c478bd9Sstevel@tonic-gate 			sp += V9BIAS64;
6437c478bd9Sstevel@tonic-gate 			stack_align = STACK_ALIGN64;
6447c478bd9Sstevel@tonic-gate 			rwindow_size = WINDOWSIZE64;
6457c478bd9Sstevel@tonic-gate 		} else {
646*aad98a6dSmathue 			/*
647*aad98a6dSmathue 			 * Reduce sp to a 32 bit value.  This was originally
648*aad98a6dSmathue 			 * done by casting down to uint32_t and back up to
649*aad98a6dSmathue 			 * caddr_t, but one compiler didn't like that, so the
650*aad98a6dSmathue 			 * uintptr_t casts were added.  The temporary 32 bit
651*aad98a6dSmathue 			 * variable was introduced to avoid depending on all
652*aad98a6dSmathue 			 * compilers to generate the desired assembly code for a
653*aad98a6dSmathue 			 * quadruple cast in a single expression.
654*aad98a6dSmathue 			 */
655*aad98a6dSmathue 			caddr32_t sp32 = (uint32_t)(uintptr_t)sp;
656*aad98a6dSmathue 			sp = (caddr_t)(uintptr_t)sp32;
657*aad98a6dSmathue 
6587c478bd9Sstevel@tonic-gate 			stack_align = STACK_ALIGN32;
6597c478bd9Sstevel@tonic-gate 			rwindow_size = WINDOWSIZE32;
6607c478bd9Sstevel@tonic-gate 		}
6617c478bd9Sstevel@tonic-gate 		if (((uintptr_t)sp & (stack_align - 1)) != 0)
6627c478bd9Sstevel@tonic-gate 			continue;
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 		watched = watch_disable_addr(sp, rwindow_size, S_WRITE);
6657c478bd9Sstevel@tonic-gate 		err = xcopyout(mpcb->mpcb_wbuf +
6667c478bd9Sstevel@tonic-gate 		    (j * wbuf_rwindow_size), sp, rwindow_size);
6677c478bd9Sstevel@tonic-gate 		if (err != 0) {
6687c478bd9Sstevel@tonic-gate 			if (psp != NULL) {
6697c478bd9Sstevel@tonic-gate 				/*
6707c478bd9Sstevel@tonic-gate 				 * Determine the offending address.
6717c478bd9Sstevel@tonic-gate 				 * It may not be the stack pointer itself.
6727c478bd9Sstevel@tonic-gate 				 */
6737c478bd9Sstevel@tonic-gate 				uint_t *kaddr = (uint_t *)(mpcb->mpcb_wbuf +
6747c478bd9Sstevel@tonic-gate 				    (j * wbuf_rwindow_size));
6757c478bd9Sstevel@tonic-gate 				uint_t *uaddr = (uint_t *)sp;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 				for (k = 0;
6787c478bd9Sstevel@tonic-gate 				    k < rwindow_size / sizeof (int);
6797c478bd9Sstevel@tonic-gate 				    k++, kaddr++, uaddr++) {
6807c478bd9Sstevel@tonic-gate 					if (suword32(uaddr, *kaddr))
6817c478bd9Sstevel@tonic-gate 						break;
6827c478bd9Sstevel@tonic-gate 				}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 				/* can't happen? */
6857c478bd9Sstevel@tonic-gate 				if (k == rwindow_size / sizeof (int))
6867c478bd9Sstevel@tonic-gate 					uaddr = (uint_t *)sp;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 				*psp = (caddr_t)uaddr;
6897c478bd9Sstevel@tonic-gate 			}
6907c478bd9Sstevel@tonic-gate 			error = err;
6917c478bd9Sstevel@tonic-gate 		} else {
6927c478bd9Sstevel@tonic-gate 			/*
6937c478bd9Sstevel@tonic-gate 			 * stack was aligned and copyout succeeded;
6947c478bd9Sstevel@tonic-gate 			 * move other windows down.
6957c478bd9Sstevel@tonic-gate 			 */
6967c478bd9Sstevel@tonic-gate 			mpcb->mpcb_wbcnt--;
6977c478bd9Sstevel@tonic-gate 			for (k = j; k < mpcb->mpcb_wbcnt; k++) {
6987c478bd9Sstevel@tonic-gate 				mpcb->mpcb_spbuf[k] = mpcb->mpcb_spbuf[k+1];
6997c478bd9Sstevel@tonic-gate 				bcopy(
7007c478bd9Sstevel@tonic-gate 				    mpcb->mpcb_wbuf +
7017c478bd9Sstevel@tonic-gate 					((k+1) * wbuf_rwindow_size),
7027c478bd9Sstevel@tonic-gate 				    mpcb->mpcb_wbuf +
7037c478bd9Sstevel@tonic-gate 					(k * wbuf_rwindow_size),
7047c478bd9Sstevel@tonic-gate 				    wbuf_rwindow_size);
7057c478bd9Sstevel@tonic-gate 			}
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 		if (watched)
7087c478bd9Sstevel@tonic-gate 			watch_enable_addr(sp, rwindow_size, S_WRITE);
7097c478bd9Sstevel@tonic-gate 	} /* while there are windows in the wbuf */
7107c478bd9Sstevel@tonic-gate 	return (error);
7117c478bd9Sstevel@tonic-gate }
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate static int
7147c478bd9Sstevel@tonic-gate copy_return_window32(int dotwo)
7157c478bd9Sstevel@tonic-gate {
7167c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
7177c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
7187c478bd9Sstevel@tonic-gate 	struct rwindow32 rwindow32;
7197c478bd9Sstevel@tonic-gate 	caddr_t sp1;
7207c478bd9Sstevel@tonic-gate 	caddr_t sp2;
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
7237c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_rsp[0] == NULL) {
724*aad98a6dSmathue 		/*
725*aad98a6dSmathue 		 * Reduce r_sp to a 32 bit value before storing it in sp1.  This
726*aad98a6dSmathue 		 * was originally done by casting down to uint32_t and back up
727*aad98a6dSmathue 		 * to caddr_t, but that generated complaints under one compiler.
728*aad98a6dSmathue 		 * The uintptr_t cast was added to address that, and the
729*aad98a6dSmathue 		 * temporary 32 bit variable was introduced to avoid depending
730*aad98a6dSmathue 		 * on all compilers to generate the desired assembly code for a
731*aad98a6dSmathue 		 * triple cast in a single expression.
732*aad98a6dSmathue 		 */
733*aad98a6dSmathue 		caddr32_t sp1_32 = (uint32_t)lwptoregs(lwp)->r_sp;
734*aad98a6dSmathue 		sp1 = (caddr_t)(uintptr_t)sp1_32;
735*aad98a6dSmathue 
7367c478bd9Sstevel@tonic-gate 		if ((copyin_nowatch(sp1, &rwindow32,
7377c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow32))) == 0)
7387c478bd9Sstevel@tonic-gate 			mpcb->mpcb_rsp[0] = sp1;
7397c478bd9Sstevel@tonic-gate 		rwindow_32ton(&rwindow32, &mpcb->mpcb_rwin[0]);
7407c478bd9Sstevel@tonic-gate 	}
7417c478bd9Sstevel@tonic-gate 	mpcb->mpcb_rsp[1] = NULL;
7427c478bd9Sstevel@tonic-gate 	if (dotwo && mpcb->mpcb_rsp[0] != NULL &&
7437c478bd9Sstevel@tonic-gate 	    (sp2 = (caddr_t)mpcb->mpcb_rwin[0].rw_fp) != NULL) {
7447c478bd9Sstevel@tonic-gate 		if ((copyin_nowatch(sp2, &rwindow32,
7457c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow32)) == 0))
7467c478bd9Sstevel@tonic-gate 			mpcb->mpcb_rsp[1] = sp2;
7477c478bd9Sstevel@tonic-gate 		rwindow_32ton(&rwindow32, &mpcb->mpcb_rwin[1]);
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 	return (mpcb->mpcb_rsp[0] != NULL);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate int
7537c478bd9Sstevel@tonic-gate copy_return_window(int dotwo)
7547c478bd9Sstevel@tonic-gate {
7557c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
7567c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
7577c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
7587c478bd9Sstevel@tonic-gate 	caddr_t sp1;
7597c478bd9Sstevel@tonic-gate 	caddr_t sp2;
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_ILP32)
7627c478bd9Sstevel@tonic-gate 		return (copy_return_window32(dotwo));
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	lwp = ttolwp(curthread);
7657c478bd9Sstevel@tonic-gate 	mpcb = lwptompcb(lwp);
7667c478bd9Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
7677c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_rsp[0] == NULL) {
7687c478bd9Sstevel@tonic-gate 		sp1 = (caddr_t)lwptoregs(lwp)->r_sp + STACK_BIAS;
7697c478bd9Sstevel@tonic-gate 		if ((copyin_nowatch(sp1, &mpcb->mpcb_rwin[0],
7707c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow)) == 0))
7717c478bd9Sstevel@tonic-gate 			mpcb->mpcb_rsp[0] = sp1 - STACK_BIAS;
7727c478bd9Sstevel@tonic-gate 	}
7737c478bd9Sstevel@tonic-gate 	mpcb->mpcb_rsp[1] = NULL;
7747c478bd9Sstevel@tonic-gate 	if (dotwo && mpcb->mpcb_rsp[0] != NULL &&
7757c478bd9Sstevel@tonic-gate 	    (sp2 = (caddr_t)mpcb->mpcb_rwin[0].rw_fp) != NULL) {
7767c478bd9Sstevel@tonic-gate 		sp2 += STACK_BIAS;
7777c478bd9Sstevel@tonic-gate 		if ((copyin_nowatch(sp2, &mpcb->mpcb_rwin[1],
7787c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow)) == 0))
7797c478bd9Sstevel@tonic-gate 			mpcb->mpcb_rsp[1] = sp2 - STACK_BIAS;
7807c478bd9Sstevel@tonic-gate 	}
7817c478bd9Sstevel@tonic-gate 	return (mpcb->mpcb_rsp[0] != NULL);
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate /*
7857c478bd9Sstevel@tonic-gate  * Clear registers on exec(2).
7867c478bd9Sstevel@tonic-gate  */
7877c478bd9Sstevel@tonic-gate void
7887c478bd9Sstevel@tonic-gate setregs(uarg_t *args)
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate 	struct regs *rp;
7917c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
7927c478bd9Sstevel@tonic-gate 	kfpu_t *fpp = lwptofpu(lwp);
7937c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
7947c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	/*
7977c478bd9Sstevel@tonic-gate 	 * Initialize user registers.
7987c478bd9Sstevel@tonic-gate 	 */
7997c478bd9Sstevel@tonic-gate 	(void) save_syscall_args();	/* copy args from registers first */
8007c478bd9Sstevel@tonic-gate 	rp = lwptoregs(lwp);
8017c478bd9Sstevel@tonic-gate 	rp->r_g1 = rp->r_g2 = rp->r_g3 = rp->r_g4 = rp->r_g5 =
8027c478bd9Sstevel@tonic-gate 	    rp->r_g6 = rp->r_o0 = rp->r_o1 = rp->r_o2 =
8037c478bd9Sstevel@tonic-gate 	    rp->r_o3 = rp->r_o4 = rp->r_o5 = rp->r_o7 = 0;
8047c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_ILP32)
8057c478bd9Sstevel@tonic-gate 		rp->r_tstate = TSTATE_USER32;
8067c478bd9Sstevel@tonic-gate 	else
8077c478bd9Sstevel@tonic-gate 		rp->r_tstate = TSTATE_USER64;
8087c478bd9Sstevel@tonic-gate 	if (!fpu_exists)
8097c478bd9Sstevel@tonic-gate 		rp->r_tstate &= ~TSTATE_PEF;
8107c478bd9Sstevel@tonic-gate 	rp->r_g7 = args->thrptr;
8117c478bd9Sstevel@tonic-gate 	rp->r_pc = args->entry;
8127c478bd9Sstevel@tonic-gate 	rp->r_npc = args->entry + 4;
8137c478bd9Sstevel@tonic-gate 	rp->r_y = 0;
8147c478bd9Sstevel@tonic-gate 	curthread->t_post_sys = 1;
8157c478bd9Sstevel@tonic-gate 	lwp->lwp_eosys = JUSTRETURN;
8167c478bd9Sstevel@tonic-gate 	lwp->lwp_pcb.pcb_trap0addr = NULL;	/* no trap 0 handler */
8177c478bd9Sstevel@tonic-gate 	/*
8187c478bd9Sstevel@tonic-gate 	 * Clear the fixalignment flag
8197c478bd9Sstevel@tonic-gate 	 */
8207c478bd9Sstevel@tonic-gate 	p->p_fixalignment = 0;
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate 	/*
8237c478bd9Sstevel@tonic-gate 	 * Throw out old user windows, init window buf.
8247c478bd9Sstevel@tonic-gate 	 */
8257c478bd9Sstevel@tonic-gate 	trash_user_windows();
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_LP64 &&
8287c478bd9Sstevel@tonic-gate 	    mpcb->mpcb_wstate != WSTATE_USER64) {
8297c478bd9Sstevel@tonic-gate 		ASSERT(mpcb->mpcb_wbcnt == 0);
8307c478bd9Sstevel@tonic-gate 		kmem_free(mpcb->mpcb_wbuf, MAXWIN * sizeof (struct rwindow32));
8317c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wbuf = kmem_alloc(MAXWIN *
8327c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow64), KM_SLEEP);
8337c478bd9Sstevel@tonic-gate 		ASSERT(((uintptr_t)mpcb->mpcb_wbuf & 7) == 0);
8347c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wstate = WSTATE_USER64;
8357c478bd9Sstevel@tonic-gate 	} else if (p->p_model == DATAMODEL_ILP32 &&
8367c478bd9Sstevel@tonic-gate 	    mpcb->mpcb_wstate != WSTATE_USER32) {
8377c478bd9Sstevel@tonic-gate 		ASSERT(mpcb->mpcb_wbcnt == 0);
8387c478bd9Sstevel@tonic-gate 		kmem_free(mpcb->mpcb_wbuf, MAXWIN * sizeof (struct rwindow64));
8397c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wbuf = kmem_alloc(MAXWIN *
8407c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow32), KM_SLEEP);
8417c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wstate = WSTATE_USER32;
8427c478bd9Sstevel@tonic-gate 	}
8437c478bd9Sstevel@tonic-gate 	mpcb->mpcb_pa = va_to_pa(mpcb);
8447c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = va_to_pa(mpcb->mpcb_wbuf);
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	/*
8477c478bd9Sstevel@tonic-gate 	 * Here we initialize minimal fpu state.
8487c478bd9Sstevel@tonic-gate 	 * The rest is done at the first floating
8497c478bd9Sstevel@tonic-gate 	 * point instruction that a process executes
8507c478bd9Sstevel@tonic-gate 	 * or by the lib_psr memcpy routines.
8517c478bd9Sstevel@tonic-gate 	 */
8527c478bd9Sstevel@tonic-gate 	if (fpu_exists) {
8537c478bd9Sstevel@tonic-gate 		extern void _fp_write_fprs(unsigned);
8547c478bd9Sstevel@tonic-gate 		_fp_write_fprs(0);
8557c478bd9Sstevel@tonic-gate 	}
8567c478bd9Sstevel@tonic-gate 	fpp->fpu_en = 0;
8577c478bd9Sstevel@tonic-gate 	fpp->fpu_fprs = 0;
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate void
8617c478bd9Sstevel@tonic-gate lwp_swapin(kthread_t *tp)
8627c478bd9Sstevel@tonic-gate {
8637c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(ttolwp(tp));
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	mpcb->mpcb_pa = va_to_pa(mpcb);
8667c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = va_to_pa(mpcb->mpcb_wbuf);
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /*
8707c478bd9Sstevel@tonic-gate  * Construct the execution environment for the user's signal
8717c478bd9Sstevel@tonic-gate  * handler and arrange for control to be given to it on return
8727c478bd9Sstevel@tonic-gate  * to userland.  The library code now calls setcontext() to
8737c478bd9Sstevel@tonic-gate  * clean up after the signal handler, so sigret() is no longer
8747c478bd9Sstevel@tonic-gate  * needed.
8757c478bd9Sstevel@tonic-gate  */
8767c478bd9Sstevel@tonic-gate int
8777c478bd9Sstevel@tonic-gate sendsig(int sig, k_siginfo_t *sip, void (*hdlr)())
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 	/*
8807c478bd9Sstevel@tonic-gate 	 * 'volatile' is needed to ensure that values are
8817c478bd9Sstevel@tonic-gate 	 * correct on the error return from on_fault().
8827c478bd9Sstevel@tonic-gate 	 */
8837c478bd9Sstevel@tonic-gate 	volatile int minstacksz; /* min stack required to catch signal */
8847c478bd9Sstevel@tonic-gate 	int newstack = 0;	/* if true, switching to altstack */
8857c478bd9Sstevel@tonic-gate 	label_t ljb;
8867c478bd9Sstevel@tonic-gate 	caddr_t sp;
8877c478bd9Sstevel@tonic-gate 	struct regs *volatile rp;
8887c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
8897c478bd9Sstevel@tonic-gate 	proc_t *volatile p = ttoproc(curthread);
8907c478bd9Sstevel@tonic-gate 	int fpq_size = 0;
8917c478bd9Sstevel@tonic-gate 	struct sigframe {
8927c478bd9Sstevel@tonic-gate 		struct frame frwin;
8937c478bd9Sstevel@tonic-gate 		ucontext_t uc;
8947c478bd9Sstevel@tonic-gate 	};
8957c478bd9Sstevel@tonic-gate 	siginfo_t *sip_addr;
8967c478bd9Sstevel@tonic-gate 	struct sigframe *volatile fp;
8977c478bd9Sstevel@tonic-gate 	ucontext_t *volatile tuc = NULL;
8987c478bd9Sstevel@tonic-gate 	char *volatile xregs = NULL;
8997c478bd9Sstevel@tonic-gate 	volatile size_t xregs_size = 0;
9007c478bd9Sstevel@tonic-gate 	gwindows_t *volatile gwp = NULL;
9017c478bd9Sstevel@tonic-gate 	volatile int gwin_size = 0;
9027c478bd9Sstevel@tonic-gate 	kfpu_t *fpp;
9037c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
9047c478bd9Sstevel@tonic-gate 	volatile int watched = 0;
9057c478bd9Sstevel@tonic-gate 	volatile int watched2 = 0;
9067c478bd9Sstevel@tonic-gate 	caddr_t tos;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	/*
9097c478bd9Sstevel@tonic-gate 	 * Make sure the current last user window has been flushed to
9107c478bd9Sstevel@tonic-gate 	 * the stack save area before we change the sp.
9117c478bd9Sstevel@tonic-gate 	 * Restore register window if a debugger modified it.
9127c478bd9Sstevel@tonic-gate 	 */
9137c478bd9Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
9147c478bd9Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_xregstat != XREGNONE)
9157c478bd9Sstevel@tonic-gate 		xregrestore(lwp, 0);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	mpcb = lwptompcb(lwp);
9187c478bd9Sstevel@tonic-gate 	rp = lwptoregs(lwp);
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 	/*
9217c478bd9Sstevel@tonic-gate 	 * Clear the watchpoint return stack pointers.
9227c478bd9Sstevel@tonic-gate 	 */
9237c478bd9Sstevel@tonic-gate 	mpcb->mpcb_rsp[0] = NULL;
9247c478bd9Sstevel@tonic-gate 	mpcb->mpcb_rsp[1] = NULL;
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 	minstacksz = sizeof (struct sigframe);
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	/*
9297c478bd9Sstevel@tonic-gate 	 * We know that sizeof (siginfo_t) is stack-aligned:
9307c478bd9Sstevel@tonic-gate 	 * 128 bytes for ILP32, 256 bytes for LP64.
9317c478bd9Sstevel@tonic-gate 	 */
9327c478bd9Sstevel@tonic-gate 	if (sip != NULL)
9337c478bd9Sstevel@tonic-gate 		minstacksz += sizeof (siginfo_t);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	/*
9367c478bd9Sstevel@tonic-gate 	 * These two fields are pointed to by ABI structures and may
9377c478bd9Sstevel@tonic-gate 	 * be of arbitrary length. Size them now so we know how big
9387c478bd9Sstevel@tonic-gate 	 * the signal frame has to be.
9397c478bd9Sstevel@tonic-gate 	 */
9407c478bd9Sstevel@tonic-gate 	fpp = lwptofpu(lwp);
9417c478bd9Sstevel@tonic-gate 	fpp->fpu_fprs = _fp_read_fprs();
9427c478bd9Sstevel@tonic-gate 	if ((fpp->fpu_en) || (fpp->fpu_fprs & FPRS_FEF)) {
9437c478bd9Sstevel@tonic-gate 		fpq_size = fpp->fpu_q_entrysize * fpp->fpu_qcnt;
9447c478bd9Sstevel@tonic-gate 		minstacksz += SA(fpq_size);
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	mpcb = lwptompcb(lwp);
9487c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wbcnt != 0) {
9497c478bd9Sstevel@tonic-gate 		gwin_size = (mpcb->mpcb_wbcnt * sizeof (struct rwindow)) +
9507c478bd9Sstevel@tonic-gate 		    (SPARC_MAXREGWINDOW * sizeof (caddr_t)) + sizeof (long);
9517c478bd9Sstevel@tonic-gate 		minstacksz += SA(gwin_size);
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	/*
9557c478bd9Sstevel@tonic-gate 	 * Extra registers, if support by this platform, may be of arbitrary
9567c478bd9Sstevel@tonic-gate 	 * length. Size them now so we know how big the signal frame has to be.
9577c478bd9Sstevel@tonic-gate 	 * For sparcv9 _LP64 user programs, use asrs instead of the xregs.
9587c478bd9Sstevel@tonic-gate 	 */
9597c478bd9Sstevel@tonic-gate 	minstacksz += SA(xregs_size);
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 	/*
9627c478bd9Sstevel@tonic-gate 	 * Figure out whether we will be handling this signal on
9637c478bd9Sstevel@tonic-gate 	 * an alternate stack specified by the user. Then allocate
9647c478bd9Sstevel@tonic-gate 	 * and validate the stack requirements for the signal handler
9657c478bd9Sstevel@tonic-gate 	 * context. on_fault will catch any faults.
9667c478bd9Sstevel@tonic-gate 	 */
9677c478bd9Sstevel@tonic-gate 	newstack = (sigismember(&u.u_sigonstack, sig) &&
9687c478bd9Sstevel@tonic-gate 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE)));
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	tos = (caddr_t)rp->r_sp + STACK_BIAS;
9717c478bd9Sstevel@tonic-gate 	if (newstack != 0) {
9727c478bd9Sstevel@tonic-gate 		fp = (struct sigframe *)
9737c478bd9Sstevel@tonic-gate 		    (SA((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
9747c478bd9Sstevel@tonic-gate 			SA((int)lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN -
9757c478bd9Sstevel@tonic-gate 			SA(minstacksz));
9767c478bd9Sstevel@tonic-gate 	} else {
9777c478bd9Sstevel@tonic-gate 		/*
9787c478bd9Sstevel@tonic-gate 		 * If we were unable to flush all register windows to
9797c478bd9Sstevel@tonic-gate 		 * the stack and we are not now on an alternate stack,
9807c478bd9Sstevel@tonic-gate 		 * just dump core with a SIGSEGV back in psig().
9817c478bd9Sstevel@tonic-gate 		 */
9827c478bd9Sstevel@tonic-gate 		if (sig == SIGSEGV &&
9837c478bd9Sstevel@tonic-gate 		    mpcb->mpcb_wbcnt != 0 &&
9847c478bd9Sstevel@tonic-gate 		    !(lwp->lwp_sigaltstack.ss_flags & SS_ONSTACK))
9857c478bd9Sstevel@tonic-gate 			return (0);
9867c478bd9Sstevel@tonic-gate 		fp = (struct sigframe *)(tos - SA(minstacksz));
9877c478bd9Sstevel@tonic-gate 		/*
9887c478bd9Sstevel@tonic-gate 		 * Could call grow here, but stack growth now handled below
9897c478bd9Sstevel@tonic-gate 		 * in code protected by on_fault().
9907c478bd9Sstevel@tonic-gate 		 */
9917c478bd9Sstevel@tonic-gate 	}
9927c478bd9Sstevel@tonic-gate 	sp = (caddr_t)fp + sizeof (struct sigframe);
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate 	/*
9957c478bd9Sstevel@tonic-gate 	 * Make sure process hasn't trashed its stack.
9967c478bd9Sstevel@tonic-gate 	 */
9977c478bd9Sstevel@tonic-gate 	if (((uintptr_t)fp & (STACK_ALIGN - 1)) != 0 ||
9987c478bd9Sstevel@tonic-gate 	    (caddr_t)fp >= p->p_usrstack ||
9997c478bd9Sstevel@tonic-gate 	    (caddr_t)fp + SA(minstacksz) >= p->p_usrstack) {
10007c478bd9Sstevel@tonic-gate #ifdef DEBUG
10017c478bd9Sstevel@tonic-gate 		printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
10027c478bd9Sstevel@tonic-gate 		    PTOU(p)->u_comm, p->p_pid, sig);
10037c478bd9Sstevel@tonic-gate 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
10047c478bd9Sstevel@tonic-gate 		    (void *)fp, (void *)hdlr, rp->r_pc);
10057c478bd9Sstevel@tonic-gate 
10067c478bd9Sstevel@tonic-gate 		if (((uintptr_t)fp & (STACK_ALIGN - 1)) != 0)
10077c478bd9Sstevel@tonic-gate 			printf("bad stack alignment\n");
10087c478bd9Sstevel@tonic-gate 		else
10097c478bd9Sstevel@tonic-gate 			printf("fp above USRSTACK\n");
10107c478bd9Sstevel@tonic-gate #endif
10117c478bd9Sstevel@tonic-gate 		return (0);
10127c478bd9Sstevel@tonic-gate 	}
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate 	watched = watch_disable_addr((caddr_t)fp, SA(minstacksz), S_WRITE);
10157c478bd9Sstevel@tonic-gate 	if (on_fault(&ljb))
10167c478bd9Sstevel@tonic-gate 		goto badstack;
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate 	tuc = kmem_alloc(sizeof (ucontext_t), KM_SLEEP);
10197c478bd9Sstevel@tonic-gate 	savecontext(tuc, lwp->lwp_sigoldmask);
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	/*
10227c478bd9Sstevel@tonic-gate 	 * save extra register state if it exists
10237c478bd9Sstevel@tonic-gate 	 */
10247c478bd9Sstevel@tonic-gate 	if (xregs_size != 0) {
10257c478bd9Sstevel@tonic-gate 		xregs_setptr(lwp, tuc, sp);
10267c478bd9Sstevel@tonic-gate 		xregs = kmem_alloc(xregs_size, KM_SLEEP);
10277c478bd9Sstevel@tonic-gate 		xregs_get(lwp, xregs);
10287c478bd9Sstevel@tonic-gate 		copyout_noerr(xregs, sp, xregs_size);
10297c478bd9Sstevel@tonic-gate 		kmem_free(xregs, xregs_size);
10307c478bd9Sstevel@tonic-gate 		xregs = NULL;
10317c478bd9Sstevel@tonic-gate 		sp += SA(xregs_size);
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 	copyout_noerr(tuc, &fp->uc, sizeof (*tuc));
10357c478bd9Sstevel@tonic-gate 	kmem_free(tuc, sizeof (*tuc));
10367c478bd9Sstevel@tonic-gate 	tuc = NULL;
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	if (sip != NULL) {
10397c478bd9Sstevel@tonic-gate 		zoneid_t zoneid;
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 		uzero(sp, sizeof (siginfo_t));
10427c478bd9Sstevel@tonic-gate 		if (SI_FROMUSER(sip) &&
10437c478bd9Sstevel@tonic-gate 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
10447c478bd9Sstevel@tonic-gate 		    zoneid != sip->si_zoneid) {
10457c478bd9Sstevel@tonic-gate 			k_siginfo_t sani_sip = *sip;
10467c478bd9Sstevel@tonic-gate 			sani_sip.si_pid = p->p_zone->zone_zsched->p_pid;
10477c478bd9Sstevel@tonic-gate 			sani_sip.si_uid = 0;
10487c478bd9Sstevel@tonic-gate 			sani_sip.si_ctid = -1;
10497c478bd9Sstevel@tonic-gate 			sani_sip.si_zoneid = zoneid;
10507c478bd9Sstevel@tonic-gate 			copyout_noerr(&sani_sip, sp, sizeof (sani_sip));
10517c478bd9Sstevel@tonic-gate 		} else {
10527c478bd9Sstevel@tonic-gate 			copyout_noerr(sip, sp, sizeof (*sip));
10537c478bd9Sstevel@tonic-gate 		}
10547c478bd9Sstevel@tonic-gate 		sip_addr = (siginfo_t *)sp;
10557c478bd9Sstevel@tonic-gate 		sp += sizeof (siginfo_t);
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 		if (sig == SIGPROF &&
10587c478bd9Sstevel@tonic-gate 		    curthread->t_rprof != NULL &&
10597c478bd9Sstevel@tonic-gate 		    curthread->t_rprof->rp_anystate) {
10607c478bd9Sstevel@tonic-gate 			/*
10617c478bd9Sstevel@tonic-gate 			 * We stand on our head to deal with
10627c478bd9Sstevel@tonic-gate 			 * the real time profiling signal.
10637c478bd9Sstevel@tonic-gate 			 * Fill in the stuff that doesn't fit
10647c478bd9Sstevel@tonic-gate 			 * in a normal k_siginfo structure.
10657c478bd9Sstevel@tonic-gate 			 */
10667c478bd9Sstevel@tonic-gate 			int i = sip->si_nsysarg;
10677c478bd9Sstevel@tonic-gate 			while (--i >= 0) {
10687c478bd9Sstevel@tonic-gate 				sulword_noerr(
10697c478bd9Sstevel@tonic-gate 				    (ulong_t *)&sip_addr->si_sysarg[i],
10707c478bd9Sstevel@tonic-gate 				    (ulong_t)lwp->lwp_arg[i]);
10717c478bd9Sstevel@tonic-gate 			}
10727c478bd9Sstevel@tonic-gate 			copyout_noerr(curthread->t_rprof->rp_state,
10737c478bd9Sstevel@tonic-gate 			    sip_addr->si_mstate,
10747c478bd9Sstevel@tonic-gate 			    sizeof (curthread->t_rprof->rp_state));
10757c478bd9Sstevel@tonic-gate 		}
10767c478bd9Sstevel@tonic-gate 	} else {
10777c478bd9Sstevel@tonic-gate 		sip_addr = (siginfo_t *)NULL;
10787c478bd9Sstevel@tonic-gate 	}
10797c478bd9Sstevel@tonic-gate 
10807c478bd9Sstevel@tonic-gate 	/*
10817c478bd9Sstevel@tonic-gate 	 * When flush_user_windows_to_stack() can't save all the
10827c478bd9Sstevel@tonic-gate 	 * windows to the stack, it puts them in the lwp's pcb.
10837c478bd9Sstevel@tonic-gate 	 */
10847c478bd9Sstevel@tonic-gate 	if (gwin_size != 0) {
10857c478bd9Sstevel@tonic-gate 		gwp = kmem_alloc(gwin_size, KM_SLEEP);
10867c478bd9Sstevel@tonic-gate 		getgwins(lwp, gwp);
10877c478bd9Sstevel@tonic-gate 		sulword_noerr(&fp->uc.uc_mcontext.gwins, (ulong_t)sp);
10887c478bd9Sstevel@tonic-gate 		copyout_noerr(gwp, sp, gwin_size);
10897c478bd9Sstevel@tonic-gate 		kmem_free(gwp, gwin_size);
10907c478bd9Sstevel@tonic-gate 		gwp = NULL;
10917c478bd9Sstevel@tonic-gate 		sp += SA(gwin_size);
10927c478bd9Sstevel@tonic-gate 	} else
10937c478bd9Sstevel@tonic-gate 		sulword_noerr(&fp->uc.uc_mcontext.gwins, (ulong_t)NULL);
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 	if (fpq_size != 0) {
10967c478bd9Sstevel@tonic-gate 		struct fq *fqp = (struct fq *)sp;
10977c478bd9Sstevel@tonic-gate 		sulword_noerr(&fp->uc.uc_mcontext.fpregs.fpu_q, (ulong_t)fqp);
10987c478bd9Sstevel@tonic-gate 		copyout_noerr(mpcb->mpcb_fpu_q, fqp, fpq_size);
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		/*
11017c478bd9Sstevel@tonic-gate 		 * forget the fp queue so that the signal handler can run
11027c478bd9Sstevel@tonic-gate 		 * without being harrassed--it will do a setcontext that will
11037c478bd9Sstevel@tonic-gate 		 * re-establish the queue if there still is one
11047c478bd9Sstevel@tonic-gate 		 *
11057c478bd9Sstevel@tonic-gate 		 * NOTE: fp_runq() relies on the qcnt field being zeroed here
11067c478bd9Sstevel@tonic-gate 		 *	to terminate its processing of the queue after signal
11077c478bd9Sstevel@tonic-gate 		 *	delivery.
11087c478bd9Sstevel@tonic-gate 		 */
11097c478bd9Sstevel@tonic-gate 		mpcb->mpcb_fpu->fpu_qcnt = 0;
11107c478bd9Sstevel@tonic-gate 		sp += SA(fpq_size);
11117c478bd9Sstevel@tonic-gate 
11127c478bd9Sstevel@tonic-gate 		/* Also, syscall needs to know about this */
11137c478bd9Sstevel@tonic-gate 		mpcb->mpcb_flags |= FP_TRAPPED;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	} else {
11167c478bd9Sstevel@tonic-gate 		sulword_noerr(&fp->uc.uc_mcontext.fpregs.fpu_q, (ulong_t)NULL);
11177c478bd9Sstevel@tonic-gate 		suword8_noerr(&fp->uc.uc_mcontext.fpregs.fpu_qcnt, 0);
11187c478bd9Sstevel@tonic-gate 	}
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	/*
11227c478bd9Sstevel@tonic-gate 	 * Since we flushed the user's windows and we are changing his
11237c478bd9Sstevel@tonic-gate 	 * stack pointer, the window that the user will return to will
11247c478bd9Sstevel@tonic-gate 	 * be restored from the save area in the frame we are setting up.
11257c478bd9Sstevel@tonic-gate 	 * We copy in save area for old stack pointer so that debuggers
11267c478bd9Sstevel@tonic-gate 	 * can do a proper stack backtrace from the signal handler.
11277c478bd9Sstevel@tonic-gate 	 */
11287c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wbcnt == 0) {
11297c478bd9Sstevel@tonic-gate 		watched2 = watch_disable_addr(tos, sizeof (struct rwindow),
11307c478bd9Sstevel@tonic-gate 		    S_READ);
11317c478bd9Sstevel@tonic-gate 		ucopy(tos, &fp->frwin, sizeof (struct rwindow));
11327c478bd9Sstevel@tonic-gate 	}
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	lwp->lwp_oldcontext = (uintptr_t)&fp->uc;
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	if (newstack != 0) {
11377c478bd9Sstevel@tonic-gate 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 		if (lwp->lwp_ustack) {
11407c478bd9Sstevel@tonic-gate 			copyout_noerr(&lwp->lwp_sigaltstack,
11417c478bd9Sstevel@tonic-gate 			    (stack_t *)lwp->lwp_ustack, sizeof (stack_t));
11427c478bd9Sstevel@tonic-gate 		}
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate 	no_fault();
11467c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;		/* let user go on */
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	if (watched2)
11497c478bd9Sstevel@tonic-gate 		watch_enable_addr(tos, sizeof (struct rwindow), S_READ);
11507c478bd9Sstevel@tonic-gate 	if (watched)
11517c478bd9Sstevel@tonic-gate 		watch_enable_addr((caddr_t)fp, SA(minstacksz), S_WRITE);
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	/*
11547c478bd9Sstevel@tonic-gate 	 * Set up user registers for execution of signal handler.
11557c478bd9Sstevel@tonic-gate 	 */
11567c478bd9Sstevel@tonic-gate 	rp->r_sp = (uintptr_t)fp - STACK_BIAS;
11577c478bd9Sstevel@tonic-gate 	rp->r_pc = (uintptr_t)hdlr;
11587c478bd9Sstevel@tonic-gate 	rp->r_npc = (uintptr_t)hdlr + 4;
11597c478bd9Sstevel@tonic-gate 	/* make sure %asi is ASI_PNF */
11607c478bd9Sstevel@tonic-gate 	rp->r_tstate &= ~((uint64_t)TSTATE_ASI_MASK << TSTATE_ASI_SHIFT);
11617c478bd9Sstevel@tonic-gate 	rp->r_tstate |= ((uint64_t)ASI_PNF << TSTATE_ASI_SHIFT);
11627c478bd9Sstevel@tonic-gate 	rp->r_o0 = sig;
11637c478bd9Sstevel@tonic-gate 	rp->r_o1 = (uintptr_t)sip_addr;
11647c478bd9Sstevel@tonic-gate 	rp->r_o2 = (uintptr_t)&fp->uc;
11657c478bd9Sstevel@tonic-gate 	/*
11667c478bd9Sstevel@tonic-gate 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
11677c478bd9Sstevel@tonic-gate 	 * lwp_eosys is handled, so setting it here would affect the next
11687c478bd9Sstevel@tonic-gate 	 * system call.
11697c478bd9Sstevel@tonic-gate 	 */
11707c478bd9Sstevel@tonic-gate 	return (1);
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate badstack:
11737c478bd9Sstevel@tonic-gate 	no_fault();
11747c478bd9Sstevel@tonic-gate 	if (watched2)
11757c478bd9Sstevel@tonic-gate 		watch_enable_addr(tos, sizeof (struct rwindow), S_READ);
11767c478bd9Sstevel@tonic-gate 	if (watched)
11777c478bd9Sstevel@tonic-gate 		watch_enable_addr((caddr_t)fp, SA(minstacksz), S_WRITE);
11787c478bd9Sstevel@tonic-gate 	if (tuc)
11797c478bd9Sstevel@tonic-gate 		kmem_free(tuc, sizeof (ucontext_t));
11807c478bd9Sstevel@tonic-gate 	if (xregs)
11817c478bd9Sstevel@tonic-gate 		kmem_free(xregs, xregs_size);
11827c478bd9Sstevel@tonic-gate 	if (gwp)
11837c478bd9Sstevel@tonic-gate 		kmem_free(gwp, gwin_size);
11847c478bd9Sstevel@tonic-gate #ifdef DEBUG
11857c478bd9Sstevel@tonic-gate 	printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
11867c478bd9Sstevel@tonic-gate 	    PTOU(p)->u_comm, p->p_pid, sig);
11877c478bd9Sstevel@tonic-gate 	printf("on fault, sigsp = %p, action = %p, upc = 0x%lx\n",
11887c478bd9Sstevel@tonic-gate 	    (void *)fp, (void *)hdlr, rp->r_pc);
11897c478bd9Sstevel@tonic-gate #endif
11907c478bd9Sstevel@tonic-gate 	return (0);
11917c478bd9Sstevel@tonic-gate }
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 
11947c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate /*
11977c478bd9Sstevel@tonic-gate  * Construct the execution environment for the user's signal
11987c478bd9Sstevel@tonic-gate  * handler and arrange for control to be given to it on return
11997c478bd9Sstevel@tonic-gate  * to userland.  The library code now calls setcontext() to
12007c478bd9Sstevel@tonic-gate  * clean up after the signal handler, so sigret() is no longer
12017c478bd9Sstevel@tonic-gate  * needed.
12027c478bd9Sstevel@tonic-gate  */
12037c478bd9Sstevel@tonic-gate int
12047c478bd9Sstevel@tonic-gate sendsig32(int sig, k_siginfo_t *sip, void (*hdlr)())
12057c478bd9Sstevel@tonic-gate {
12067c478bd9Sstevel@tonic-gate 	/*
12077c478bd9Sstevel@tonic-gate 	 * 'volatile' is needed to ensure that values are
12087c478bd9Sstevel@tonic-gate 	 * correct on the error return from on_fault().
12097c478bd9Sstevel@tonic-gate 	 */
12107c478bd9Sstevel@tonic-gate 	volatile int minstacksz; /* min stack required to catch signal */
12117c478bd9Sstevel@tonic-gate 	int newstack = 0;	/* if true, switching to altstack */
12127c478bd9Sstevel@tonic-gate 	label_t ljb;
12137c478bd9Sstevel@tonic-gate 	caddr_t sp;
12147c478bd9Sstevel@tonic-gate 	struct regs *volatile rp;
12157c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
12167c478bd9Sstevel@tonic-gate 	proc_t *volatile p = ttoproc(curthread);
12177c478bd9Sstevel@tonic-gate 	struct fq32 fpu_q[MAXFPQ]; /* to hold floating queue */
12187c478bd9Sstevel@tonic-gate 	struct fq32 *dfq = NULL;
12197c478bd9Sstevel@tonic-gate 	size_t fpq_size = 0;
12207c478bd9Sstevel@tonic-gate 	struct sigframe32 {
12217c478bd9Sstevel@tonic-gate 		struct frame32 frwin;
12227c478bd9Sstevel@tonic-gate 		ucontext32_t uc;
12237c478bd9Sstevel@tonic-gate 	};
12247c478bd9Sstevel@tonic-gate 	struct sigframe32 *volatile fp;
12257c478bd9Sstevel@tonic-gate 	siginfo32_t *sip_addr;
12267c478bd9Sstevel@tonic-gate 	ucontext32_t *volatile tuc = NULL;
12277c478bd9Sstevel@tonic-gate 	char *volatile xregs = NULL;
12287c478bd9Sstevel@tonic-gate 	volatile int xregs_size = 0;
12297c478bd9Sstevel@tonic-gate 	gwindows32_t *volatile gwp = NULL;
12307c478bd9Sstevel@tonic-gate 	volatile size_t gwin_size = 0;
12317c478bd9Sstevel@tonic-gate 	kfpu_t *fpp;
12327c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
12337c478bd9Sstevel@tonic-gate 	volatile int watched = 0;
12347c478bd9Sstevel@tonic-gate 	volatile int watched2 = 0;
12357c478bd9Sstevel@tonic-gate 	caddr_t tos;
1236*aad98a6dSmathue 	caddr32_t tos32;
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate 	/*
12397c478bd9Sstevel@tonic-gate 	 * Make sure the current last user window has been flushed to
12407c478bd9Sstevel@tonic-gate 	 * the stack save area before we change the sp.
12417c478bd9Sstevel@tonic-gate 	 * Restore register window if a debugger modified it.
12427c478bd9Sstevel@tonic-gate 	 */
12437c478bd9Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
12447c478bd9Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_xregstat != XREGNONE)
12457c478bd9Sstevel@tonic-gate 		xregrestore(lwp, 0);
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate 	mpcb = lwptompcb(lwp);
12487c478bd9Sstevel@tonic-gate 	rp = lwptoregs(lwp);
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	/*
12517c478bd9Sstevel@tonic-gate 	 * Clear the watchpoint return stack pointers.
12527c478bd9Sstevel@tonic-gate 	 */
12537c478bd9Sstevel@tonic-gate 	mpcb->mpcb_rsp[0] = NULL;
12547c478bd9Sstevel@tonic-gate 	mpcb->mpcb_rsp[1] = NULL;
12557c478bd9Sstevel@tonic-gate 
12567c478bd9Sstevel@tonic-gate 	minstacksz = sizeof (struct sigframe32);
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 	if (sip != NULL)
12597c478bd9Sstevel@tonic-gate 		minstacksz += sizeof (siginfo32_t);
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 	/*
12627c478bd9Sstevel@tonic-gate 	 * These two fields are pointed to by ABI structures and may
12637c478bd9Sstevel@tonic-gate 	 * be of arbitrary length. Size them now so we know how big
12647c478bd9Sstevel@tonic-gate 	 * the signal frame has to be.
12657c478bd9Sstevel@tonic-gate 	 */
12667c478bd9Sstevel@tonic-gate 	fpp = lwptofpu(lwp);
12677c478bd9Sstevel@tonic-gate 	fpp->fpu_fprs = _fp_read_fprs();
12687c478bd9Sstevel@tonic-gate 	if ((fpp->fpu_en) || (fpp->fpu_fprs & FPRS_FEF)) {
12697c478bd9Sstevel@tonic-gate 		fpq_size = sizeof (struct fpq32) * fpp->fpu_qcnt;
12707c478bd9Sstevel@tonic-gate 		minstacksz += fpq_size;
12717c478bd9Sstevel@tonic-gate 		dfq = fpu_q;
12727c478bd9Sstevel@tonic-gate 	}
12737c478bd9Sstevel@tonic-gate 
12747c478bd9Sstevel@tonic-gate 	mpcb = lwptompcb(lwp);
12757c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wbcnt != 0) {
12767c478bd9Sstevel@tonic-gate 		gwin_size = (mpcb->mpcb_wbcnt * sizeof (struct rwindow32)) +
12777c478bd9Sstevel@tonic-gate 		    (SPARC_MAXREGWINDOW * sizeof (caddr32_t)) +
12787c478bd9Sstevel@tonic-gate 		    sizeof (int32_t);
12797c478bd9Sstevel@tonic-gate 		minstacksz += gwin_size;
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate 	/*
12837c478bd9Sstevel@tonic-gate 	 * Extra registers, if supported by this platform, may be of arbitrary
12847c478bd9Sstevel@tonic-gate 	 * length. Size them now so we know how big the signal frame has to be.
12857c478bd9Sstevel@tonic-gate 	 */
12867c478bd9Sstevel@tonic-gate 	xregs_size = xregs_getsize(p);
12877c478bd9Sstevel@tonic-gate 	minstacksz += SA32(xregs_size);
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	/*
12907c478bd9Sstevel@tonic-gate 	 * Figure out whether we will be handling this signal on
12917c478bd9Sstevel@tonic-gate 	 * an alternate stack specified by the user. Then allocate
12927c478bd9Sstevel@tonic-gate 	 * and validate the stack requirements for the signal handler
12937c478bd9Sstevel@tonic-gate 	 * context. on_fault will catch any faults.
12947c478bd9Sstevel@tonic-gate 	 */
12957c478bd9Sstevel@tonic-gate 	newstack = (sigismember(&u.u_sigonstack, sig) &&
12967c478bd9Sstevel@tonic-gate 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE)));
12977c478bd9Sstevel@tonic-gate 
1298*aad98a6dSmathue 	/*
1299*aad98a6dSmathue 	 * Reduce r_sp to a 32 bit value before storing it in tos.  This was
1300*aad98a6dSmathue 	 * originally done by casting down to uint32_t and back up to void *,
1301*aad98a6dSmathue 	 * but that generated complaints under one compiler.  The uintptr_t cast
1302*aad98a6dSmathue 	 * was added to address that, and the temporary 32 bit variable was
1303*aad98a6dSmathue 	 * introduced to avoid depending on all compilers to generate the
1304*aad98a6dSmathue 	 * desired assembly code for a triple cast in a single expression.
1305*aad98a6dSmathue 	 */
1306*aad98a6dSmathue 	tos32 = (uint32_t)rp->r_sp;
1307*aad98a6dSmathue 	tos = (void *)(uintptr_t)tos32;
1308*aad98a6dSmathue 
13097c478bd9Sstevel@tonic-gate 	if (newstack != 0) {
13107c478bd9Sstevel@tonic-gate 		fp = (struct sigframe32 *)
13117c478bd9Sstevel@tonic-gate 		    (SA32((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
13127c478bd9Sstevel@tonic-gate 			SA32((int)lwp->lwp_sigaltstack.ss_size) -
13137c478bd9Sstevel@tonic-gate 			STACK_ALIGN32 -
13147c478bd9Sstevel@tonic-gate 			SA32(minstacksz));
13157c478bd9Sstevel@tonic-gate 	} else {
13167c478bd9Sstevel@tonic-gate 		/*
13177c478bd9Sstevel@tonic-gate 		 * If we were unable to flush all register windows to
13187c478bd9Sstevel@tonic-gate 		 * the stack and we are not now on an alternate stack,
13197c478bd9Sstevel@tonic-gate 		 * just dump core with a SIGSEGV back in psig().
13207c478bd9Sstevel@tonic-gate 		 */
13217c478bd9Sstevel@tonic-gate 		if (sig == SIGSEGV &&
13227c478bd9Sstevel@tonic-gate 		    mpcb->mpcb_wbcnt != 0 &&
13237c478bd9Sstevel@tonic-gate 		    !(lwp->lwp_sigaltstack.ss_flags & SS_ONSTACK))
13247c478bd9Sstevel@tonic-gate 			return (0);
13257c478bd9Sstevel@tonic-gate 		fp = (struct sigframe32 *)(tos - SA32(minstacksz));
13267c478bd9Sstevel@tonic-gate 		/*
13277c478bd9Sstevel@tonic-gate 		 * Could call grow here, but stack growth now handled below
13287c478bd9Sstevel@tonic-gate 		 * in code protected by on_fault().
13297c478bd9Sstevel@tonic-gate 		 */
13307c478bd9Sstevel@tonic-gate 	}
13317c478bd9Sstevel@tonic-gate 	sp = (caddr_t)fp + sizeof (struct sigframe32);
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	/*
13347c478bd9Sstevel@tonic-gate 	 * Make sure process hasn't trashed its stack.
13357c478bd9Sstevel@tonic-gate 	 */
13367c478bd9Sstevel@tonic-gate 	if (((uintptr_t)fp & (STACK_ALIGN32 - 1)) != 0 ||
13377c478bd9Sstevel@tonic-gate 	    (caddr_t)fp >= p->p_usrstack ||
13387c478bd9Sstevel@tonic-gate 	    (caddr_t)fp + SA32(minstacksz) >= p->p_usrstack) {
13397c478bd9Sstevel@tonic-gate #ifdef DEBUG
13407c478bd9Sstevel@tonic-gate 		printf("sendsig32: bad signal stack cmd=%s, pid=%d, sig=%d\n",
13417c478bd9Sstevel@tonic-gate 		    PTOU(p)->u_comm, p->p_pid, sig);
13427c478bd9Sstevel@tonic-gate 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
13437c478bd9Sstevel@tonic-gate 		    (void *)fp, (void *)hdlr, rp->r_pc);
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 		if (((uintptr_t)fp & (STACK_ALIGN32 - 1)) != 0)
13467c478bd9Sstevel@tonic-gate 			printf("bad stack alignment\n");
13477c478bd9Sstevel@tonic-gate 		else
13487c478bd9Sstevel@tonic-gate 			printf("fp above USRSTACK32\n");
13497c478bd9Sstevel@tonic-gate #endif
13507c478bd9Sstevel@tonic-gate 		return (0);
13517c478bd9Sstevel@tonic-gate 	}
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate 	watched = watch_disable_addr((caddr_t)fp, SA32(minstacksz), S_WRITE);
13547c478bd9Sstevel@tonic-gate 	if (on_fault(&ljb))
13557c478bd9Sstevel@tonic-gate 		goto badstack;
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	tuc = kmem_alloc(sizeof (ucontext32_t), KM_SLEEP);
13587c478bd9Sstevel@tonic-gate 	savecontext32(tuc, lwp->lwp_sigoldmask, dfq);
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	/*
13617c478bd9Sstevel@tonic-gate 	 * save extra register state if it exists
13627c478bd9Sstevel@tonic-gate 	 */
13637c478bd9Sstevel@tonic-gate 	if (xregs_size != 0) {
1364*aad98a6dSmathue 		xregs_setptr32(lwp, tuc, (caddr32_t)(uintptr_t)sp);
13657c478bd9Sstevel@tonic-gate 		xregs = kmem_alloc(xregs_size, KM_SLEEP);
13667c478bd9Sstevel@tonic-gate 		xregs_get(lwp, xregs);
13677c478bd9Sstevel@tonic-gate 		copyout_noerr(xregs, sp, xregs_size);
13687c478bd9Sstevel@tonic-gate 		kmem_free(xregs, xregs_size);
13697c478bd9Sstevel@tonic-gate 		xregs = NULL;
13707c478bd9Sstevel@tonic-gate 		sp += SA32(xregs_size);
13717c478bd9Sstevel@tonic-gate 	}
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate 	copyout_noerr(tuc, &fp->uc, sizeof (*tuc));
13747c478bd9Sstevel@tonic-gate 	kmem_free(tuc, sizeof (*tuc));
13757c478bd9Sstevel@tonic-gate 	tuc = NULL;
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 	if (sip != NULL) {
13787c478bd9Sstevel@tonic-gate 		siginfo32_t si32;
13797c478bd9Sstevel@tonic-gate 		zoneid_t zoneid;
13807c478bd9Sstevel@tonic-gate 
13817c478bd9Sstevel@tonic-gate 		siginfo_kto32(sip, &si32);
13827c478bd9Sstevel@tonic-gate 		if (SI_FROMUSER(sip) &&
13837c478bd9Sstevel@tonic-gate 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
13847c478bd9Sstevel@tonic-gate 		    zoneid != sip->si_zoneid) {
13857c478bd9Sstevel@tonic-gate 			si32.si_pid = p->p_zone->zone_zsched->p_pid;
13867c478bd9Sstevel@tonic-gate 			si32.si_uid = 0;
13877c478bd9Sstevel@tonic-gate 			si32.si_ctid = -1;
13887c478bd9Sstevel@tonic-gate 			si32.si_zoneid = zoneid;
13897c478bd9Sstevel@tonic-gate 		}
13907c478bd9Sstevel@tonic-gate 		uzero(sp, sizeof (siginfo32_t));
13917c478bd9Sstevel@tonic-gate 		copyout_noerr(&si32, sp, sizeof (siginfo32_t));
13927c478bd9Sstevel@tonic-gate 		sip_addr = (siginfo32_t *)sp;
13937c478bd9Sstevel@tonic-gate 		sp += sizeof (siginfo32_t);
13947c478bd9Sstevel@tonic-gate 
13957c478bd9Sstevel@tonic-gate 		if (sig == SIGPROF &&
13967c478bd9Sstevel@tonic-gate 		    curthread->t_rprof != NULL &&
13977c478bd9Sstevel@tonic-gate 		    curthread->t_rprof->rp_anystate) {
13987c478bd9Sstevel@tonic-gate 			/*
13997c478bd9Sstevel@tonic-gate 			 * We stand on our head to deal with
14007c478bd9Sstevel@tonic-gate 			 * the real time profiling signal.
14017c478bd9Sstevel@tonic-gate 			 * Fill in the stuff that doesn't fit
14027c478bd9Sstevel@tonic-gate 			 * in a normal k_siginfo structure.
14037c478bd9Sstevel@tonic-gate 			 */
14047c478bd9Sstevel@tonic-gate 			int i = sip->si_nsysarg;
14057c478bd9Sstevel@tonic-gate 			while (--i >= 0) {
14067c478bd9Sstevel@tonic-gate 				suword32_noerr(&sip_addr->si_sysarg[i],
14077c478bd9Sstevel@tonic-gate 				    (uint32_t)lwp->lwp_arg[i]);
14087c478bd9Sstevel@tonic-gate 			}
14097c478bd9Sstevel@tonic-gate 			copyout_noerr(curthread->t_rprof->rp_state,
14107c478bd9Sstevel@tonic-gate 			    sip_addr->si_mstate,
14117c478bd9Sstevel@tonic-gate 			    sizeof (curthread->t_rprof->rp_state));
14127c478bd9Sstevel@tonic-gate 		}
14137c478bd9Sstevel@tonic-gate 	} else {
14147c478bd9Sstevel@tonic-gate 		sip_addr = NULL;
14157c478bd9Sstevel@tonic-gate 	}
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 	/*
14187c478bd9Sstevel@tonic-gate 	 * When flush_user_windows_to_stack() can't save all the
14197c478bd9Sstevel@tonic-gate 	 * windows to the stack, it puts them in the lwp's pcb.
14207c478bd9Sstevel@tonic-gate 	 */
14217c478bd9Sstevel@tonic-gate 	if (gwin_size != 0) {
14227c478bd9Sstevel@tonic-gate 		gwp = kmem_alloc(gwin_size, KM_SLEEP);
14237c478bd9Sstevel@tonic-gate 		getgwins32(lwp, gwp);
1424*aad98a6dSmathue 		suword32_noerr(&fp->uc.uc_mcontext.gwins,
1425*aad98a6dSmathue 		    (uint32_t)(uintptr_t)sp);
14267c478bd9Sstevel@tonic-gate 		copyout_noerr(gwp, sp, gwin_size);
14277c478bd9Sstevel@tonic-gate 		kmem_free(gwp, gwin_size);
14287c478bd9Sstevel@tonic-gate 		gwp = NULL;
14297c478bd9Sstevel@tonic-gate 		sp += gwin_size;
14307c478bd9Sstevel@tonic-gate 	} else {
14317c478bd9Sstevel@tonic-gate 		suword32_noerr(&fp->uc.uc_mcontext.gwins, (uint32_t)NULL);
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	if (fpq_size != 0) {
14357c478bd9Sstevel@tonic-gate 		/*
14367c478bd9Sstevel@tonic-gate 		 * Update the (already copied out) fpu32.fpu_q pointer
14377c478bd9Sstevel@tonic-gate 		 * from NULL to the 32-bit address on the user's stack
14387c478bd9Sstevel@tonic-gate 		 * where we then copyout the fq32 to.
14397c478bd9Sstevel@tonic-gate 		 */
14407c478bd9Sstevel@tonic-gate 		struct fq32 *fqp = (struct fq32 *)sp;
1441*aad98a6dSmathue 		suword32_noerr(&fp->uc.uc_mcontext.fpregs.fpu_q,
1442*aad98a6dSmathue 		    (uint32_t)(uintptr_t)fqp);
14437c478bd9Sstevel@tonic-gate 		copyout_noerr(dfq, fqp, fpq_size);
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 		/*
14467c478bd9Sstevel@tonic-gate 		 * forget the fp queue so that the signal handler can run
14477c478bd9Sstevel@tonic-gate 		 * without being harrassed--it will do a setcontext that will
14487c478bd9Sstevel@tonic-gate 		 * re-establish the queue if there still is one
14497c478bd9Sstevel@tonic-gate 		 *
14507c478bd9Sstevel@tonic-gate 		 * NOTE: fp_runq() relies on the qcnt field being zeroed here
14517c478bd9Sstevel@tonic-gate 		 *	to terminate its processing of the queue after signal
14527c478bd9Sstevel@tonic-gate 		 *	delivery.
14537c478bd9Sstevel@tonic-gate 		 */
14547c478bd9Sstevel@tonic-gate 		mpcb->mpcb_fpu->fpu_qcnt = 0;
14557c478bd9Sstevel@tonic-gate 		sp += fpq_size;
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate 		/* Also, syscall needs to know about this */
14587c478bd9Sstevel@tonic-gate 		mpcb->mpcb_flags |= FP_TRAPPED;
14597c478bd9Sstevel@tonic-gate 
14607c478bd9Sstevel@tonic-gate 	} else {
14617c478bd9Sstevel@tonic-gate 		suword32_noerr(&fp->uc.uc_mcontext.fpregs.fpu_q,
14627c478bd9Sstevel@tonic-gate 		    (uint32_t)NULL);
14637c478bd9Sstevel@tonic-gate 		suword8_noerr(&fp->uc.uc_mcontext.fpregs.fpu_qcnt, 0);
14647c478bd9Sstevel@tonic-gate 	}
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	/*
14687c478bd9Sstevel@tonic-gate 	 * Since we flushed the user's windows and we are changing his
14697c478bd9Sstevel@tonic-gate 	 * stack pointer, the window that the user will return to will
14707c478bd9Sstevel@tonic-gate 	 * be restored from the save area in the frame we are setting up.
14717c478bd9Sstevel@tonic-gate 	 * We copy in save area for old stack pointer so that debuggers
14727c478bd9Sstevel@tonic-gate 	 * can do a proper stack backtrace from the signal handler.
14737c478bd9Sstevel@tonic-gate 	 */
14747c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wbcnt == 0) {
14757c478bd9Sstevel@tonic-gate 		watched2 = watch_disable_addr(tos, sizeof (struct rwindow32),
14767c478bd9Sstevel@tonic-gate 		    S_READ);
14777c478bd9Sstevel@tonic-gate 		ucopy(tos, &fp->frwin, sizeof (struct rwindow32));
14787c478bd9Sstevel@tonic-gate 	}
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	lwp->lwp_oldcontext = (uintptr_t)&fp->uc;
14817c478bd9Sstevel@tonic-gate 
14827c478bd9Sstevel@tonic-gate 	if (newstack != 0) {
14837c478bd9Sstevel@tonic-gate 		lwp->lwp_sigaltstack.ss_flags |= SS_ONSTACK;
14847c478bd9Sstevel@tonic-gate 		if (lwp->lwp_ustack) {
14857c478bd9Sstevel@tonic-gate 			stack32_t stk32;
14867c478bd9Sstevel@tonic-gate 
1487*aad98a6dSmathue 			stk32.ss_sp =
1488*aad98a6dSmathue 			    (caddr32_t)(uintptr_t)lwp->lwp_sigaltstack.ss_sp;
14897c478bd9Sstevel@tonic-gate 			stk32.ss_size = (size32_t)lwp->lwp_sigaltstack.ss_size;
14907c478bd9Sstevel@tonic-gate 			stk32.ss_flags = (int32_t)lwp->lwp_sigaltstack.ss_flags;
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate 			copyout_noerr(&stk32, (stack32_t *)lwp->lwp_ustack,
14937c478bd9Sstevel@tonic-gate 			    sizeof (stack32_t));
14947c478bd9Sstevel@tonic-gate 		}
14957c478bd9Sstevel@tonic-gate 	}
14967c478bd9Sstevel@tonic-gate 
14977c478bd9Sstevel@tonic-gate 	no_fault();
14987c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;		/* let user go on */
14997c478bd9Sstevel@tonic-gate 
15007c478bd9Sstevel@tonic-gate 	if (watched2)
15017c478bd9Sstevel@tonic-gate 		watch_enable_addr(tos, sizeof (struct rwindow32), S_READ);
15027c478bd9Sstevel@tonic-gate 	if (watched)
15037c478bd9Sstevel@tonic-gate 		watch_enable_addr((caddr_t)fp, SA32(minstacksz), S_WRITE);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	/*
15067c478bd9Sstevel@tonic-gate 	 * Set up user registers for execution of signal handler.
15077c478bd9Sstevel@tonic-gate 	 */
15087c478bd9Sstevel@tonic-gate 	rp->r_sp = (uintptr_t)fp;
15097c478bd9Sstevel@tonic-gate 	rp->r_pc = (uintptr_t)hdlr;
15107c478bd9Sstevel@tonic-gate 	rp->r_npc = (uintptr_t)hdlr + 4;
15117c478bd9Sstevel@tonic-gate 	/* make sure %asi is ASI_PNF */
15127c478bd9Sstevel@tonic-gate 	rp->r_tstate &= ~((uint64_t)TSTATE_ASI_MASK << TSTATE_ASI_SHIFT);
15137c478bd9Sstevel@tonic-gate 	rp->r_tstate |= ((uint64_t)ASI_PNF << TSTATE_ASI_SHIFT);
15147c478bd9Sstevel@tonic-gate 	rp->r_o0 = sig;
15157c478bd9Sstevel@tonic-gate 	rp->r_o1 = (uintptr_t)sip_addr;
15167c478bd9Sstevel@tonic-gate 	rp->r_o2 = (uintptr_t)&fp->uc;
15177c478bd9Sstevel@tonic-gate 	/*
15187c478bd9Sstevel@tonic-gate 	 * Don't set lwp_eosys here.  sendsig() is called via psig() after
15197c478bd9Sstevel@tonic-gate 	 * lwp_eosys is handled, so setting it here would affect the next
15207c478bd9Sstevel@tonic-gate 	 * system call.
15217c478bd9Sstevel@tonic-gate 	 */
15227c478bd9Sstevel@tonic-gate 	return (1);
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate badstack:
15257c478bd9Sstevel@tonic-gate 	no_fault();
15267c478bd9Sstevel@tonic-gate 	if (watched2)
15277c478bd9Sstevel@tonic-gate 		watch_enable_addr(tos, sizeof (struct rwindow32), S_READ);
15287c478bd9Sstevel@tonic-gate 	if (watched)
15297c478bd9Sstevel@tonic-gate 		watch_enable_addr((caddr_t)fp, SA32(minstacksz), S_WRITE);
15307c478bd9Sstevel@tonic-gate 	if (tuc)
15317c478bd9Sstevel@tonic-gate 		kmem_free(tuc, sizeof (*tuc));
15327c478bd9Sstevel@tonic-gate 	if (xregs)
15337c478bd9Sstevel@tonic-gate 		kmem_free(xregs, xregs_size);
15347c478bd9Sstevel@tonic-gate 	if (gwp)
15357c478bd9Sstevel@tonic-gate 		kmem_free(gwp, gwin_size);
15367c478bd9Sstevel@tonic-gate #ifdef DEBUG
15377c478bd9Sstevel@tonic-gate 	printf("sendsig32: bad signal stack cmd=%s, pid=%d, sig=%d\n",
15387c478bd9Sstevel@tonic-gate 	    PTOU(p)->u_comm, p->p_pid, sig);
15397c478bd9Sstevel@tonic-gate 	printf("on fault, sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
15407c478bd9Sstevel@tonic-gate 	    (void *)fp, (void *)hdlr, rp->r_pc);
15417c478bd9Sstevel@tonic-gate #endif
15427c478bd9Sstevel@tonic-gate 	return (0);
15437c478bd9Sstevel@tonic-gate }
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
15467c478bd9Sstevel@tonic-gate 
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate /*
15497c478bd9Sstevel@tonic-gate  * load user registers into lwp.
15507c478bd9Sstevel@tonic-gate  * thrptr ignored for sparc.
15517c478bd9Sstevel@tonic-gate  */
15527c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
15537c478bd9Sstevel@tonic-gate void
15547c478bd9Sstevel@tonic-gate lwp_load(klwp_t *lwp, gregset_t grp, uintptr_t thrptr)
15557c478bd9Sstevel@tonic-gate {
15567c478bd9Sstevel@tonic-gate 	setgregs(lwp, grp);
15577c478bd9Sstevel@tonic-gate 	if (lwptoproc(lwp)->p_model == DATAMODEL_ILP32)
15587c478bd9Sstevel@tonic-gate 		lwptoregs(lwp)->r_tstate = TSTATE_USER32;
15597c478bd9Sstevel@tonic-gate 	else
15607c478bd9Sstevel@tonic-gate 		lwptoregs(lwp)->r_tstate = TSTATE_USER64;
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	if (!fpu_exists)
15637c478bd9Sstevel@tonic-gate 		lwptoregs(lwp)->r_tstate &= ~TSTATE_PEF;
15647c478bd9Sstevel@tonic-gate 	lwp->lwp_eosys = JUSTRETURN;
15657c478bd9Sstevel@tonic-gate 	lwptot(lwp)->t_post_sys = 1;
15667c478bd9Sstevel@tonic-gate }
15677c478bd9Sstevel@tonic-gate 
15687c478bd9Sstevel@tonic-gate /*
15697c478bd9Sstevel@tonic-gate  * set syscall()'s return values for a lwp.
15707c478bd9Sstevel@tonic-gate  */
15717c478bd9Sstevel@tonic-gate void
15727c478bd9Sstevel@tonic-gate lwp_setrval(klwp_t *lwp, int v1, int v2)
15737c478bd9Sstevel@tonic-gate {
15747c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
15757c478bd9Sstevel@tonic-gate 
15767c478bd9Sstevel@tonic-gate 	rp->r_tstate &= ~TSTATE_IC;
15777c478bd9Sstevel@tonic-gate 	rp->r_o0 = v1;
15787c478bd9Sstevel@tonic-gate 	rp->r_o1 = v2;
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate /*
15827c478bd9Sstevel@tonic-gate  * set stack pointer for a lwp
15837c478bd9Sstevel@tonic-gate  */
15847c478bd9Sstevel@tonic-gate void
15857c478bd9Sstevel@tonic-gate lwp_setsp(klwp_t *lwp, caddr_t sp)
15867c478bd9Sstevel@tonic-gate {
15877c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
15887c478bd9Sstevel@tonic-gate 	rp->r_sp = (uintptr_t)sp;
15897c478bd9Sstevel@tonic-gate }
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate /*
15927c478bd9Sstevel@tonic-gate  * Take any PCB specific actions that are required or flagged in the PCB.
15937c478bd9Sstevel@tonic-gate  */
15947c478bd9Sstevel@tonic-gate extern void trap_async_hwerr(void);
15957c478bd9Sstevel@tonic-gate #pragma	weak trap_async_hwerr
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate void
15987c478bd9Sstevel@tonic-gate lwp_pcb_exit(void)
15997c478bd9Sstevel@tonic-gate {
16007c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
16017c478bd9Sstevel@tonic-gate 
16027c478bd9Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_flags & ASYNC_HWERR) {
16037c478bd9Sstevel@tonic-gate 		trap_async_hwerr();
16047c478bd9Sstevel@tonic-gate 	}
16057c478bd9Sstevel@tonic-gate }
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate /*
16087c478bd9Sstevel@tonic-gate  * Invalidate the saved user register windows in the pcb struct
16097c478bd9Sstevel@tonic-gate  * for the current thread. They will no longer be preserved.
16107c478bd9Sstevel@tonic-gate  */
16117c478bd9Sstevel@tonic-gate void
16127c478bd9Sstevel@tonic-gate lwp_clear_uwin(void)
16137c478bd9Sstevel@tonic-gate {
16147c478bd9Sstevel@tonic-gate 	struct machpcb *m = lwptompcb(ttolwp(curthread));
16157c478bd9Sstevel@tonic-gate 
16167c478bd9Sstevel@tonic-gate 	/*
16177c478bd9Sstevel@tonic-gate 	 * This has the effect of invalidating all (any) of the
16187c478bd9Sstevel@tonic-gate 	 * user level windows that are currently sitting in the
16197c478bd9Sstevel@tonic-gate 	 * kernel buffer.
16207c478bd9Sstevel@tonic-gate 	 */
16217c478bd9Sstevel@tonic-gate 	m->mpcb_wbcnt = 0;
16227c478bd9Sstevel@tonic-gate }
16237c478bd9Sstevel@tonic-gate 
16247c478bd9Sstevel@tonic-gate static uint_t
16257c478bd9Sstevel@tonic-gate mkpsr(uint64_t tstate, uint_t fprs)
16267c478bd9Sstevel@tonic-gate {
16277c478bd9Sstevel@tonic-gate 	uint_t psr, icc;
16287c478bd9Sstevel@tonic-gate 
16297c478bd9Sstevel@tonic-gate 	psr = tstate & TSTATE_CWP_MASK;
16307c478bd9Sstevel@tonic-gate 	if (tstate & TSTATE_PRIV)
16317c478bd9Sstevel@tonic-gate 		psr |= PSR_PS;
16327c478bd9Sstevel@tonic-gate 	if (fprs & FPRS_FEF)
16337c478bd9Sstevel@tonic-gate 		psr |= PSR_EF;
16347c478bd9Sstevel@tonic-gate 	icc = (uint_t)(tstate >> PSR_TSTATE_CC_SHIFT) & PSR_ICC;
16357c478bd9Sstevel@tonic-gate 	psr |= icc;
16367c478bd9Sstevel@tonic-gate 	psr |= V9_PSR_IMPLVER;
16377c478bd9Sstevel@tonic-gate 	return (psr);
16387c478bd9Sstevel@tonic-gate }
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate void
16417c478bd9Sstevel@tonic-gate sync_icache(caddr_t va, uint_t len)
16427c478bd9Sstevel@tonic-gate {
16437c478bd9Sstevel@tonic-gate 	caddr_t end;
16447c478bd9Sstevel@tonic-gate 
16457c478bd9Sstevel@tonic-gate 	end = va + len;
16467c478bd9Sstevel@tonic-gate 	va = (caddr_t)((uintptr_t)va & -8l);	/* sparc needs 8-byte align */
16477c478bd9Sstevel@tonic-gate 	while (va < end) {
16487c478bd9Sstevel@tonic-gate 		doflush(va);
16497c478bd9Sstevel@tonic-gate 		va += 8;
16507c478bd9Sstevel@tonic-gate 	}
16517c478bd9Sstevel@tonic-gate }
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate /*
16567c478bd9Sstevel@tonic-gate  * Copy the floating point queue if and only if there is a queue and a place
16577c478bd9Sstevel@tonic-gate  * to copy it to. Let xregs take care of the other fp regs, for v8plus.
16587c478bd9Sstevel@tonic-gate  * The issue is that while we are handling the fq32 in sendsig, we
16597c478bd9Sstevel@tonic-gate  * still need a 64-bit pointer to it, and the caddr32_t in fpregset32_t
16607c478bd9Sstevel@tonic-gate  * will not suffice, so we have the third parameter to this function.
16617c478bd9Sstevel@tonic-gate  */
16627c478bd9Sstevel@tonic-gate void
16637c478bd9Sstevel@tonic-gate fpuregset_nto32(const fpregset_t *src, fpregset32_t *dest, struct fq32 *dfq)
16647c478bd9Sstevel@tonic-gate {
16657c478bd9Sstevel@tonic-gate 	int i;
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	bzero(dest, sizeof (*dest));
16687c478bd9Sstevel@tonic-gate 	for (i = 0; i < 32; i++)
16697c478bd9Sstevel@tonic-gate 		dest->fpu_fr.fpu_regs[i] = src->fpu_fr.fpu_regs[i];
16707c478bd9Sstevel@tonic-gate 	dest->fpu_q = NULL;
16717c478bd9Sstevel@tonic-gate 	dest->fpu_fsr = (uint32_t)src->fpu_fsr;
16727c478bd9Sstevel@tonic-gate 	dest->fpu_qcnt = src->fpu_qcnt;
16737c478bd9Sstevel@tonic-gate 	dest->fpu_q_entrysize = sizeof (struct fpq32);
16747c478bd9Sstevel@tonic-gate 	dest->fpu_en = src->fpu_en;
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	if ((src->fpu_qcnt) && (dfq != NULL)) {
16777c478bd9Sstevel@tonic-gate 		struct fq *sfq = src->fpu_q;
16787c478bd9Sstevel@tonic-gate 		for (i = 0; i < src->fpu_qcnt; i++, dfq++, sfq++) {
16797c478bd9Sstevel@tonic-gate 			dfq->FQu.fpq.fpq_addr =
1680*aad98a6dSmathue 			    (caddr32_t)(uintptr_t)sfq->FQu.fpq.fpq_addr;
16817c478bd9Sstevel@tonic-gate 			dfq->FQu.fpq.fpq_instr = sfq->FQu.fpq.fpq_instr;
16827c478bd9Sstevel@tonic-gate 		}
16837c478bd9Sstevel@tonic-gate 	}
16847c478bd9Sstevel@tonic-gate }
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate /*
16877c478bd9Sstevel@tonic-gate  * Copy the floating point queue if and only if there is a queue and a place
16887c478bd9Sstevel@tonic-gate  * to copy it to. Let xregs take care of the other fp regs, for v8plus.
16897c478bd9Sstevel@tonic-gate  * The *dfq is required to escape the bzero in both this function and in
16907c478bd9Sstevel@tonic-gate  * ucontext_32ton. The *sfq is required because once the fq32 is copied
16917c478bd9Sstevel@tonic-gate  * into the kernel, in setcontext, then we need a 64-bit pointer to it.
16927c478bd9Sstevel@tonic-gate  */
16937c478bd9Sstevel@tonic-gate static void
16947c478bd9Sstevel@tonic-gate fpuregset_32ton(const fpregset32_t *src, fpregset_t *dest,
16957c478bd9Sstevel@tonic-gate     const struct fq32 *sfq, struct fq *dfq)
16967c478bd9Sstevel@tonic-gate {
16977c478bd9Sstevel@tonic-gate 	int i;
16987c478bd9Sstevel@tonic-gate 
16997c478bd9Sstevel@tonic-gate 	bzero(dest, sizeof (*dest));
17007c478bd9Sstevel@tonic-gate 	for (i = 0; i < 32; i++)
17017c478bd9Sstevel@tonic-gate 		dest->fpu_fr.fpu_regs[i] = src->fpu_fr.fpu_regs[i];
17027c478bd9Sstevel@tonic-gate 	dest->fpu_q = dfq;
17037c478bd9Sstevel@tonic-gate 	dest->fpu_fsr = (uint64_t)src->fpu_fsr;
17047c478bd9Sstevel@tonic-gate 	if ((dest->fpu_qcnt = src->fpu_qcnt) > 0)
17057c478bd9Sstevel@tonic-gate 		dest->fpu_q_entrysize = sizeof (struct fpq);
17067c478bd9Sstevel@tonic-gate 	else
17077c478bd9Sstevel@tonic-gate 		dest->fpu_q_entrysize = 0;
17087c478bd9Sstevel@tonic-gate 	dest->fpu_en = src->fpu_en;
17097c478bd9Sstevel@tonic-gate 
17107c478bd9Sstevel@tonic-gate 	if ((src->fpu_qcnt) && (sfq) && (dfq)) {
17117c478bd9Sstevel@tonic-gate 		for (i = 0; i < src->fpu_qcnt; i++, dfq++, sfq++) {
17127c478bd9Sstevel@tonic-gate 			dfq->FQu.fpq.fpq_addr =
1713*aad98a6dSmathue 			    (unsigned int *)(uintptr_t)sfq->FQu.fpq.fpq_addr;
17147c478bd9Sstevel@tonic-gate 			dfq->FQu.fpq.fpq_instr = sfq->FQu.fpq.fpq_instr;
17157c478bd9Sstevel@tonic-gate 		}
17167c478bd9Sstevel@tonic-gate 	}
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate void
17207c478bd9Sstevel@tonic-gate ucontext_32ton(const ucontext32_t *src, ucontext_t *dest,
17217c478bd9Sstevel@tonic-gate     const struct fq32 *sfq, struct fq *dfq)
17227c478bd9Sstevel@tonic-gate {
17237c478bd9Sstevel@tonic-gate 	int i;
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate 	bzero(dest, sizeof (*dest));
17267c478bd9Sstevel@tonic-gate 
17277c478bd9Sstevel@tonic-gate 	dest->uc_flags = src->uc_flags;
1728*aad98a6dSmathue 	dest->uc_link = (ucontext_t *)(uintptr_t)src->uc_link;
17297c478bd9Sstevel@tonic-gate 
17307c478bd9Sstevel@tonic-gate 	for (i = 0; i < 4; i++) {
17317c478bd9Sstevel@tonic-gate 		dest->uc_sigmask.__sigbits[i] = src->uc_sigmask.__sigbits[i];
17327c478bd9Sstevel@tonic-gate 	}
17337c478bd9Sstevel@tonic-gate 
1734*aad98a6dSmathue 	dest->uc_stack.ss_sp = (void *)(uintptr_t)src->uc_stack.ss_sp;
17357c478bd9Sstevel@tonic-gate 	dest->uc_stack.ss_size = (size_t)src->uc_stack.ss_size;
17367c478bd9Sstevel@tonic-gate 	dest->uc_stack.ss_flags = src->uc_stack.ss_flags;
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 	/* REG_CCR is 0, skip over it and handle it after this loop */
17397c478bd9Sstevel@tonic-gate 	for (i = 1; i < _NGREG32; i++)
17407c478bd9Sstevel@tonic-gate 		dest->uc_mcontext.gregs[i] =
17417c478bd9Sstevel@tonic-gate 		    (greg_t)(uint32_t)src->uc_mcontext.gregs[i];
17427c478bd9Sstevel@tonic-gate 	dest->uc_mcontext.gregs[REG_CCR] =
17437c478bd9Sstevel@tonic-gate 	    (src->uc_mcontext.gregs[REG_PSR] & PSR_ICC) >> PSR_ICC_SHIFT;
17447c478bd9Sstevel@tonic-gate 	dest->uc_mcontext.gregs[REG_ASI] = ASI_PNF;
17457c478bd9Sstevel@tonic-gate 	/*
17467c478bd9Sstevel@tonic-gate 	 * A valid fpregs is only copied in if (uc.uc_flags & UC_FPU),
17477c478bd9Sstevel@tonic-gate 	 * otherwise there is no guarantee that anything in fpregs is valid.
17487c478bd9Sstevel@tonic-gate 	 */
17497c478bd9Sstevel@tonic-gate 	if (src->uc_flags & UC_FPU) {
17507c478bd9Sstevel@tonic-gate 		dest->uc_mcontext.gregs[REG_FPRS] =
17517c478bd9Sstevel@tonic-gate 		    ((src->uc_mcontext.fpregs.fpu_en) ?
17527c478bd9Sstevel@tonic-gate 		    (FPRS_DU|FPRS_DL|FPRS_FEF) : 0);
17537c478bd9Sstevel@tonic-gate 	} else {
17547c478bd9Sstevel@tonic-gate 		dest->uc_mcontext.gregs[REG_FPRS] = 0;
17557c478bd9Sstevel@tonic-gate 	}
1756*aad98a6dSmathue 	dest->uc_mcontext.gwins =
1757*aad98a6dSmathue 	    (gwindows_t *)(uintptr_t)src->uc_mcontext.gwins;
17587c478bd9Sstevel@tonic-gate 	if (src->uc_flags & UC_FPU) {
17597c478bd9Sstevel@tonic-gate 		fpuregset_32ton(&src->uc_mcontext.fpregs,
17607c478bd9Sstevel@tonic-gate 		    &dest->uc_mcontext.fpregs, sfq, dfq);
17617c478bd9Sstevel@tonic-gate 	}
17627c478bd9Sstevel@tonic-gate }
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate void
17657c478bd9Sstevel@tonic-gate rwindow_nto32(struct rwindow *src, struct rwindow32 *dest)
17667c478bd9Sstevel@tonic-gate {
17677c478bd9Sstevel@tonic-gate 	greg_t *s = (greg_t *)src;
17687c478bd9Sstevel@tonic-gate 	greg32_t *d = (greg32_t *)dest;
17697c478bd9Sstevel@tonic-gate 	int i;
17707c478bd9Sstevel@tonic-gate 
17717c478bd9Sstevel@tonic-gate 	for (i = 0; i < 16; i++)
17727c478bd9Sstevel@tonic-gate 		*d++ = (greg32_t)*s++;
17737c478bd9Sstevel@tonic-gate }
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate void
17767c478bd9Sstevel@tonic-gate rwindow_32ton(struct rwindow32 *src, struct rwindow *dest)
17777c478bd9Sstevel@tonic-gate {
17787c478bd9Sstevel@tonic-gate 	greg32_t *s = (greg32_t *)src;
17797c478bd9Sstevel@tonic-gate 	greg_t *d = (greg_t *)dest;
17807c478bd9Sstevel@tonic-gate 	int i;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	for (i = 0; i < 16; i++)
17837c478bd9Sstevel@tonic-gate 		*d++ = (uint32_t)*s++;
17847c478bd9Sstevel@tonic-gate }
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate /*
17897c478bd9Sstevel@tonic-gate  * The panic code invokes panic_saveregs() to record the contents of a
17907c478bd9Sstevel@tonic-gate  * regs structure into the specified panic_data structure for debuggers.
17917c478bd9Sstevel@tonic-gate  */
17927c478bd9Sstevel@tonic-gate void
17937c478bd9Sstevel@tonic-gate panic_saveregs(panic_data_t *pdp, struct regs *rp)
17947c478bd9Sstevel@tonic-gate {
17957c478bd9Sstevel@tonic-gate 	panic_nv_t *pnv = PANICNVGET(pdp);
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "tstate", rp->r_tstate);
17987c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "g1", rp->r_g1);
17997c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "g2", rp->r_g2);
18007c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "g3", rp->r_g3);
18017c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "g4", rp->r_g4);
18027c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "g5", rp->r_g5);
18037c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "g6", rp->r_g6);
18047c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "g7", rp->r_g7);
18057c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "o0", rp->r_o0);
18067c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "o1", rp->r_o1);
18077c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "o2", rp->r_o2);
18087c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "o3", rp->r_o3);
18097c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "o4", rp->r_o4);
18107c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "o5", rp->r_o5);
18117c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "o6", rp->r_o6);
18127c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "o7", rp->r_o7);
18137c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "pc", (ulong_t)rp->r_pc);
18147c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "npc", (ulong_t)rp->r_npc);
18157c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "y", (uint32_t)rp->r_y);
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate 	PANICNVSET(pdp, pnv);
18187c478bd9Sstevel@tonic-gate }
1819