xref: /illumos-gate/usr/src/uts/sparc/v9/os/v9dep.c (revision 12551037)
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
5a3c55825Sraf  * Common Development and Distribution License (the "License").
6a3c55825Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21a3c55825Sraf 
227c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*12551037SToomas Soome /*	  All Rights Reserved	*/
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26061d7437SJakub Jermar  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
2848bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
347c478bd9Sstevel@tonic-gate #include <sys/systm.h>
357c478bd9Sstevel@tonic-gate #include <sys/stack.h>
367c478bd9Sstevel@tonic-gate #include <sys/frame.h>
377c478bd9Sstevel@tonic-gate #include <sys/proc.h>
387c478bd9Sstevel@tonic-gate #include <sys/ucontext.h>
397c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
407c478bd9Sstevel@tonic-gate #include <sys/asm_linkage.h>
417c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
447c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
457c478bd9Sstevel@tonic-gate #include <sys/fpu/fpusystm.h>
467c478bd9Sstevel@tonic-gate #include <sys/debug.h>
477c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
487c478bd9Sstevel@tonic-gate #include <sys/machpcb.h>
497c478bd9Sstevel@tonic-gate #include <sys/psr_compat.h>
507c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
517c478bd9Sstevel@tonic-gate #include <sys/asi.h>
527c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
537c478bd9Sstevel@tonic-gate #include <sys/model.h>
547c478bd9Sstevel@tonic-gate #include <sys/panic.h>
557c478bd9Sstevel@tonic-gate #include <sys/exec.h>
567c478bd9Sstevel@tonic-gate 
572c5124a1SPrashanth Sreenivasa /*
582c5124a1SPrashanth Sreenivasa  * By default, set the weakest model to TSO (Total Store Order)
592c5124a1SPrashanth Sreenivasa  * which is the default memory model on SPARC.
602c5124a1SPrashanth Sreenivasa  * If a platform does support a weaker model than TSO, this will be
612c5124a1SPrashanth Sreenivasa  * updated at runtime to reflect that.
622c5124a1SPrashanth Sreenivasa  */
632c5124a1SPrashanth Sreenivasa uint_t weakest_mem_model = TSTATE_MM_TSO;
642c5124a1SPrashanth Sreenivasa 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * modify the lower 32bits of a uint64_t
677c478bd9Sstevel@tonic-gate  */
687c478bd9Sstevel@tonic-gate #define	SET_LOWER_32(all, lower)	\
697c478bd9Sstevel@tonic-gate 	(((uint64_t)(all) & 0xffffffff00000000) | (uint32_t)(lower))
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	MEMCPY_FPU_EN		2	/* fprs on and fpu_en == 0 */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static uint_t mkpsr(uint64_t tstate, uint32_t fprs);
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
767c478bd9Sstevel@tonic-gate static void fpuregset_32ton(const fpregset32_t *src, fpregset_t *dest,
77bc0e9132SGordon Ross     const struct fq32 *sfq, struct _fq *dfq);
787c478bd9Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate  * Set floating-point registers.
827c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
837c478bd9Sstevel@tonic-gate  * called from code in /proc to set the registers of another lwp.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate void
setfpregs(klwp_t * lwp,fpregset_t * fp)867c478bd9Sstevel@tonic-gate setfpregs(klwp_t *lwp, fpregset_t *fp)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
897c478bd9Sstevel@tonic-gate 	kfpu_t *pfp;
907c478bd9Sstevel@tonic-gate 	uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
917c478bd9Sstevel@tonic-gate 	model_t model = lwp_getdatamodel(lwp);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	mpcb = lwptompcb(lwp);
947c478bd9Sstevel@tonic-gate 	pfp = lwptofpu(lwp);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	/*
977c478bd9Sstevel@tonic-gate 	 * This is always true for both "real" fp programs and memcpy fp
987c478bd9Sstevel@tonic-gate 	 * programs, because we force fpu_en to MEMCPY_FPU_EN in getfpregs,
997c478bd9Sstevel@tonic-gate 	 * for the memcpy and threads cases where (fpu_en == 0) &&
1007c478bd9Sstevel@tonic-gate 	 * (fpu_fprs & FPRS_FEF), if setfpregs is called after getfpregs.
1017c478bd9Sstevel@tonic-gate 	 */
1027c478bd9Sstevel@tonic-gate 	if (fp->fpu_en) {
1037c478bd9Sstevel@tonic-gate 		kpreempt_disable();
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		if (!(pfp->fpu_en) && (!(pfp->fpu_fprs & FPRS_FEF)) &&
1067c478bd9Sstevel@tonic-gate 		    fpu_exists) {
1077c478bd9Sstevel@tonic-gate 			/*
10848bbca81SDaniel Hoffman 			 * It's not currently using the FPU but wants to in its
1097c478bd9Sstevel@tonic-gate 			 * new context - arrange for this on return to userland.
1107c478bd9Sstevel@tonic-gate 			 */
1117c478bd9Sstevel@tonic-gate 			pfp->fpu_fprs = (uint32_t)fprs;
1127c478bd9Sstevel@tonic-gate 		}
1137c478bd9Sstevel@tonic-gate 		/*
1147c478bd9Sstevel@tonic-gate 		 * Get setfpregs to restore fpu_en to zero
1157c478bd9Sstevel@tonic-gate 		 * for the memcpy/threads case (where pfp->fpu_en == 0 &&
1167c478bd9Sstevel@tonic-gate 		 * (pfp->fp_fprs & FPRS_FEF) == FPRS_FEF).
1177c478bd9Sstevel@tonic-gate 		 */
1187c478bd9Sstevel@tonic-gate 		if (fp->fpu_en == MEMCPY_FPU_EN)
1197c478bd9Sstevel@tonic-gate 			fp->fpu_en = 0;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		/*
1227c478bd9Sstevel@tonic-gate 		 * Load up a user's floating point context.
1237c478bd9Sstevel@tonic-gate 		 */
124*12551037SToomas Soome 		if (fp->fpu_qcnt > MAXFPQ)	/* plug security holes */
1257c478bd9Sstevel@tonic-gate 			fp->fpu_qcnt = MAXFPQ;
126bc0e9132SGordon Ross 		fp->fpu_q_entrysize = sizeof (struct _fq);
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		/*
1297c478bd9Sstevel@tonic-gate 		 * For v9 kernel, copy all of the fp regs.
1307c478bd9Sstevel@tonic-gate 		 * For v8 kernel, copy v8 fp regs (lower half of v9 fp regs).
1317c478bd9Sstevel@tonic-gate 		 * Restore entire fsr for v9, only lower half for v8.
1327c478bd9Sstevel@tonic-gate 		 */
1337c478bd9Sstevel@tonic-gate 		(void) kcopy(fp, pfp, sizeof (fp->fpu_fr));
1347c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_LP64)
1357c478bd9Sstevel@tonic-gate 			pfp->fpu_fsr = fp->fpu_fsr;
1367c478bd9Sstevel@tonic-gate 		else
1377c478bd9Sstevel@tonic-gate 			pfp->fpu_fsr = SET_LOWER_32(pfp->fpu_fsr, fp->fpu_fsr);
1387c478bd9Sstevel@tonic-gate 		pfp->fpu_qcnt = fp->fpu_qcnt;
1397c478bd9Sstevel@tonic-gate 		pfp->fpu_q_entrysize = fp->fpu_q_entrysize;
1407c478bd9Sstevel@tonic-gate 		pfp->fpu_en = fp->fpu_en;
1417c478bd9Sstevel@tonic-gate 		pfp->fpu_q = mpcb->mpcb_fpu_q;
1427c478bd9Sstevel@tonic-gate 		if (fp->fpu_qcnt)
1437c478bd9Sstevel@tonic-gate 			(void) kcopy(fp->fpu_q, pfp->fpu_q,
1447c478bd9Sstevel@tonic-gate 			    fp->fpu_qcnt * fp->fpu_q_entrysize);
1457c478bd9Sstevel@tonic-gate 		/* FSR ignores these bits on load, so they can not be set */
1467c478bd9Sstevel@tonic-gate 		pfp->fpu_fsr &= ~(FSR_QNE|FSR_FTT);
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 		/*
1497c478bd9Sstevel@tonic-gate 		 * If not the current process then resume() will handle it.
1507c478bd9Sstevel@tonic-gate 		 */
1517c478bd9Sstevel@tonic-gate 		if (lwp != ttolwp(curthread)) {
1527c478bd9Sstevel@tonic-gate 			/* force resume to reload fp regs */
1537c478bd9Sstevel@tonic-gate 			pfp->fpu_fprs |= FPRS_FEF;
1547c478bd9Sstevel@tonic-gate 			kpreempt_enable();
1557c478bd9Sstevel@tonic-gate 			return;
1567c478bd9Sstevel@tonic-gate 		}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		/*
1597c478bd9Sstevel@tonic-gate 		 * Load up FPU with new floating point context.
1607c478bd9Sstevel@tonic-gate 		 */
1617c478bd9Sstevel@tonic-gate 		if (fpu_exists) {
1627c478bd9Sstevel@tonic-gate 			pfp->fpu_fprs = _fp_read_fprs();
1637c478bd9Sstevel@tonic-gate 			if ((pfp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
1647c478bd9Sstevel@tonic-gate 				_fp_write_fprs(fprs);
1657c478bd9Sstevel@tonic-gate 				pfp->fpu_fprs = (uint32_t)fprs;
1667c478bd9Sstevel@tonic-gate #ifdef DEBUG
1677c478bd9Sstevel@tonic-gate 				if (fpdispr)
1687c478bd9Sstevel@tonic-gate 					cmn_err(CE_NOTE,
1697c478bd9Sstevel@tonic-gate 					    "setfpregs with fp disabled!\n");
1707c478bd9Sstevel@tonic-gate #endif
1717c478bd9Sstevel@tonic-gate 			}
1727c478bd9Sstevel@tonic-gate 			/*
1737c478bd9Sstevel@tonic-gate 			 * Load all fp regs for v9 user programs, but only
1747c478bd9Sstevel@tonic-gate 			 * load the lower half for v8[plus] programs.
1757c478bd9Sstevel@tonic-gate 			 */
1767c478bd9Sstevel@tonic-gate 			if (model == DATAMODEL_LP64)
1777c478bd9Sstevel@tonic-gate 				fp_restore(pfp);
1787c478bd9Sstevel@tonic-gate 			else
1797c478bd9Sstevel@tonic-gate 				fp_v8_load(pfp);
1807c478bd9Sstevel@tonic-gate 		}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		kpreempt_enable();
1837c478bd9Sstevel@tonic-gate 	} else {
1847c478bd9Sstevel@tonic-gate 		if ((pfp->fpu_en) ||	/* normal fp case */
1857c478bd9Sstevel@tonic-gate 		    (pfp->fpu_fprs & FPRS_FEF)) { /* memcpy/threads case */
1867c478bd9Sstevel@tonic-gate 			/*
1877c478bd9Sstevel@tonic-gate 			 * Currently the lwp has floating point enabled.
1887c478bd9Sstevel@tonic-gate 			 * Turn off FPRS_FEF in user's fprs, saved and
1897c478bd9Sstevel@tonic-gate 			 * real copies thereof.
1907c478bd9Sstevel@tonic-gate 			 */
1917c478bd9Sstevel@tonic-gate 			pfp->fpu_en = 0;
1927c478bd9Sstevel@tonic-gate 			if (fpu_exists) {
1937c478bd9Sstevel@tonic-gate 				fprs = 0;
1947c478bd9Sstevel@tonic-gate 				if (lwp == ttolwp(curthread))
1957c478bd9Sstevel@tonic-gate 					_fp_write_fprs(fprs);
1967c478bd9Sstevel@tonic-gate 				pfp->fpu_fprs = (uint32_t)fprs;
1977c478bd9Sstevel@tonic-gate 			}
1987c478bd9Sstevel@tonic-gate 		}
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
2037c478bd9Sstevel@tonic-gate void
setfpregs32(klwp_t * lwp,fpregset32_t * fp)2047c478bd9Sstevel@tonic-gate setfpregs32(klwp_t *lwp, fpregset32_t *fp)
2057c478bd9Sstevel@tonic-gate {
2067c478bd9Sstevel@tonic-gate 	fpregset_t fpregs;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	fpuregset_32ton(fp, &fpregs, NULL, NULL);
2097c478bd9Sstevel@tonic-gate 	setfpregs(lwp, &fpregs);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
2157c478bd9Sstevel@tonic-gate  * called from code in /proc to set the registers of another lwp.
2167c478bd9Sstevel@tonic-gate  */
2177c478bd9Sstevel@tonic-gate void
run_fpq(klwp_t * lwp,fpregset_t * fp)2187c478bd9Sstevel@tonic-gate run_fpq(klwp_t *lwp, fpregset_t *fp)
2197c478bd9Sstevel@tonic-gate {
2207c478bd9Sstevel@tonic-gate 	/*
2217c478bd9Sstevel@tonic-gate 	 * If the context being loaded up includes a floating queue,
2227c478bd9Sstevel@tonic-gate 	 * we need to simulate those instructions (since we can't reload
2237c478bd9Sstevel@tonic-gate 	 * the fpu) and pass the process any appropriate signals
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (lwp == ttolwp(curthread)) {
2277c478bd9Sstevel@tonic-gate 		if (fpu_exists) {
2287c478bd9Sstevel@tonic-gate 			if (fp->fpu_qcnt)
2297c478bd9Sstevel@tonic-gate 				fp_runq(lwp->lwp_regs);
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate /*
2357c478bd9Sstevel@tonic-gate  * Get floating-point registers.
2367c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
2377c478bd9Sstevel@tonic-gate  * called from code in /proc to set the registers of another lwp.
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate void
getfpregs(klwp_t * lwp,fpregset_t * fp)2407c478bd9Sstevel@tonic-gate getfpregs(klwp_t *lwp, fpregset_t *fp)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	kfpu_t *pfp;
2437c478bd9Sstevel@tonic-gate 	model_t model = lwp_getdatamodel(lwp);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	pfp = lwptofpu(lwp);
2467c478bd9Sstevel@tonic-gate 	kpreempt_disable();
2477c478bd9Sstevel@tonic-gate 	if (fpu_exists && ttolwp(curthread) == lwp)
2487c478bd9Sstevel@tonic-gate 		pfp->fpu_fprs = _fp_read_fprs();
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/*
2517c478bd9Sstevel@tonic-gate 	 * First check the fpu_en case, for normal fp programs.
2527c478bd9Sstevel@tonic-gate 	 * Next check the fprs case, for fp use by memcpy/threads.
2537c478bd9Sstevel@tonic-gate 	 */
2547c478bd9Sstevel@tonic-gate 	if (((fp->fpu_en = pfp->fpu_en) != 0) ||
2557c478bd9Sstevel@tonic-gate 	    (pfp->fpu_fprs & FPRS_FEF)) {
2567c478bd9Sstevel@tonic-gate 		/*
2577c478bd9Sstevel@tonic-gate 		 * Force setfpregs to restore the fp context in
2587c478bd9Sstevel@tonic-gate 		 * setfpregs for the memcpy and threads cases (where
2597c478bd9Sstevel@tonic-gate 		 * pfp->fpu_en == 0 && (pfp->fp_fprs & FPRS_FEF) == FPRS_FEF).
2607c478bd9Sstevel@tonic-gate 		 */
2617c478bd9Sstevel@tonic-gate 		if (pfp->fpu_en == 0)
2627c478bd9Sstevel@tonic-gate 			fp->fpu_en = MEMCPY_FPU_EN;
2637c478bd9Sstevel@tonic-gate 		/*
2647c478bd9Sstevel@tonic-gate 		 * If we have an fpu and the current thread owns the fp
2657c478bd9Sstevel@tonic-gate 		 * context, flush fp * registers into the pcb. Save all
2667c478bd9Sstevel@tonic-gate 		 * the fp regs for v9, xregs_getfpregs saves the upper half
2677c478bd9Sstevel@tonic-gate 		 * for v8plus. Save entire fsr for v9, only lower half for v8.
2687c478bd9Sstevel@tonic-gate 		 */
2697c478bd9Sstevel@tonic-gate 		if (fpu_exists && ttolwp(curthread) == lwp) {
2707c478bd9Sstevel@tonic-gate 			if ((pfp->fpu_fprs & FPRS_FEF) != FPRS_FEF) {
2717c478bd9Sstevel@tonic-gate 				uint32_t fprs = (FPRS_FEF|FPRS_DU|FPRS_DL);
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 				_fp_write_fprs(fprs);
2747c478bd9Sstevel@tonic-gate 				pfp->fpu_fprs = fprs;
2757c478bd9Sstevel@tonic-gate #ifdef DEBUG
2767c478bd9Sstevel@tonic-gate 				if (fpdispr)
2777c478bd9Sstevel@tonic-gate 					cmn_err(CE_NOTE,
2787c478bd9Sstevel@tonic-gate 					    "getfpregs with fp disabled!\n");
2797c478bd9Sstevel@tonic-gate #endif
2807c478bd9Sstevel@tonic-gate 			}
2817c478bd9Sstevel@tonic-gate 			if (model == DATAMODEL_LP64)
2827c478bd9Sstevel@tonic-gate 				fp_fksave(pfp);
2837c478bd9Sstevel@tonic-gate 			else
2847c478bd9Sstevel@tonic-gate 				fp_v8_fksave(pfp);
2857c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		(void) kcopy(pfp, fp, sizeof (fp->fpu_fr));
2877c478bd9Sstevel@tonic-gate 		fp->fpu_q = pfp->fpu_q;
2887c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_LP64)
2897c478bd9Sstevel@tonic-gate 			fp->fpu_fsr = pfp->fpu_fsr;
2907c478bd9Sstevel@tonic-gate 		else
2917c478bd9Sstevel@tonic-gate 			fp->fpu_fsr = (uint32_t)pfp->fpu_fsr;
2927c478bd9Sstevel@tonic-gate 		fp->fpu_qcnt = pfp->fpu_qcnt;
2937c478bd9Sstevel@tonic-gate 		fp->fpu_q_entrysize = pfp->fpu_q_entrysize;
2947c478bd9Sstevel@tonic-gate 	} else {
2957c478bd9Sstevel@tonic-gate 		int i;
2967c478bd9Sstevel@tonic-gate 		for (i = 0; i < 32; i++)		/* NaN */
2977c478bd9Sstevel@tonic-gate 			((uint32_t *)fp->fpu_fr.fpu_regs)[i] = (uint32_t)-1;
2987c478bd9Sstevel@tonic-gate 		if (model == DATAMODEL_LP64) {
2997c478bd9Sstevel@tonic-gate 			for (i = 16; i < 32; i++)	/* NaN */
3007c478bd9Sstevel@tonic-gate 				((uint64_t *)fp->fpu_fr.fpu_dregs)[i] =
3017c478bd9Sstevel@tonic-gate 				    (uint64_t)-1;
3027c478bd9Sstevel@tonic-gate 		}
3037c478bd9Sstevel@tonic-gate 		fp->fpu_fsr = 0;
3047c478bd9Sstevel@tonic-gate 		fp->fpu_qcnt = 0;
3057c478bd9Sstevel@tonic-gate 	}
3067c478bd9Sstevel@tonic-gate 	kpreempt_enable();
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate #ifdef	_SYSCALL32_IMPL
3107c478bd9Sstevel@tonic-gate void
getfpregs32(klwp_t * lwp,fpregset32_t * fp)3117c478bd9Sstevel@tonic-gate getfpregs32(klwp_t *lwp, fpregset32_t *fp)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	fpregset_t fpregs;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	getfpregs(lwp, &fpregs);
3167c478bd9Sstevel@tonic-gate 	fpuregset_nto32(&fpregs, fp, NULL);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
3217c478bd9Sstevel@tonic-gate  * Set general registers.
3227c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
3237c478bd9Sstevel@tonic-gate  * called from code in /proc to set the registers of another lwp.
3247c478bd9Sstevel@tonic-gate  */
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /* 64-bit gregset_t */
3277c478bd9Sstevel@tonic-gate void
setgregs(klwp_t * lwp,gregset_t grp)3287c478bd9Sstevel@tonic-gate setgregs(klwp_t *lwp, gregset_t grp)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
3317c478bd9Sstevel@tonic-gate 	kfpu_t *fp = lwptofpu(lwp);
3327c478bd9Sstevel@tonic-gate 	uint64_t tbits;
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	int current = (lwp == curthread->t_lwp);
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if (current)
3377c478bd9Sstevel@tonic-gate 		(void) save_syscall_args();	/* copy the args first */
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	tbits = (((grp[REG_CCR] & TSTATE_CCR_MASK) << TSTATE_CCR_SHIFT) |
3402c5124a1SPrashanth Sreenivasa 	    ((grp[REG_ASI] & TSTATE_ASI_MASK) << TSTATE_ASI_SHIFT));
3417c478bd9Sstevel@tonic-gate 	rp->r_tstate &= ~(((uint64_t)TSTATE_CCR_MASK << TSTATE_CCR_SHIFT) |
3422c5124a1SPrashanth Sreenivasa 	    ((uint64_t)TSTATE_ASI_MASK << TSTATE_ASI_SHIFT));
3437c478bd9Sstevel@tonic-gate 	rp->r_tstate |= tbits;
3447c478bd9Sstevel@tonic-gate 	kpreempt_disable();
3457c478bd9Sstevel@tonic-gate 	fp->fpu_fprs = (uint32_t)grp[REG_FPRS];
3467c478bd9Sstevel@tonic-gate 	if (fpu_exists && (current) && (fp->fpu_fprs & FPRS_FEF))
3477c478bd9Sstevel@tonic-gate 		_fp_write_fprs(fp->fpu_fprs);
3487c478bd9Sstevel@tonic-gate 	kpreempt_enable();
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	/*
3517c478bd9Sstevel@tonic-gate 	 * pc and npc must be 4-byte aligned on sparc.
3527c478bd9Sstevel@tonic-gate 	 * We silently make it so to avoid a watchdog reset.
3537c478bd9Sstevel@tonic-gate 	 */
3547c478bd9Sstevel@tonic-gate 	rp->r_pc = grp[REG_PC] & ~03L;
3557c478bd9Sstevel@tonic-gate 	rp->r_npc = grp[REG_nPC] & ~03L;
3567c478bd9Sstevel@tonic-gate 	rp->r_y = grp[REG_Y];
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	rp->r_g1 = grp[REG_G1];
3597c478bd9Sstevel@tonic-gate 	rp->r_g2 = grp[REG_G2];
3607c478bd9Sstevel@tonic-gate 	rp->r_g3 = grp[REG_G3];
3617c478bd9Sstevel@tonic-gate 	rp->r_g4 = grp[REG_G4];
3627c478bd9Sstevel@tonic-gate 	rp->r_g5 = grp[REG_G5];
3637c478bd9Sstevel@tonic-gate 	rp->r_g6 = grp[REG_G6];
3647c478bd9Sstevel@tonic-gate 	rp->r_g7 = grp[REG_G7];
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	rp->r_o0 = grp[REG_O0];
3677c478bd9Sstevel@tonic-gate 	rp->r_o1 = grp[REG_O1];
3687c478bd9Sstevel@tonic-gate 	rp->r_o2 = grp[REG_O2];
3697c478bd9Sstevel@tonic-gate 	rp->r_o3 = grp[REG_O3];
3707c478bd9Sstevel@tonic-gate 	rp->r_o4 = grp[REG_O4];
3717c478bd9Sstevel@tonic-gate 	rp->r_o5 = grp[REG_O5];
3727c478bd9Sstevel@tonic-gate 	rp->r_o6 = grp[REG_O6];
3737c478bd9Sstevel@tonic-gate 	rp->r_o7 = grp[REG_O7];
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 	if (current) {
3767c478bd9Sstevel@tonic-gate 		/*
3777c478bd9Sstevel@tonic-gate 		 * This was called from a system call, but we
3787c478bd9Sstevel@tonic-gate 		 * do not want to return via the shared window;
3797c478bd9Sstevel@tonic-gate 		 * restoring the CPU context changes everything.
3807c478bd9Sstevel@tonic-gate 		 */
3817c478bd9Sstevel@tonic-gate 		lwp->lwp_eosys = JUSTRETURN;
3827c478bd9Sstevel@tonic-gate 		curthread->t_post_sys = 1;
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate }
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate /*
3877c478bd9Sstevel@tonic-gate  * Return the general registers.
3887c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
3897c478bd9Sstevel@tonic-gate  * called from code in /proc to get the registers of another lwp.
3907c478bd9Sstevel@tonic-gate  */
3917c478bd9Sstevel@tonic-gate void
getgregs(klwp_t * lwp,gregset_t grp)3927c478bd9Sstevel@tonic-gate getgregs(klwp_t *lwp, gregset_t grp)
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
3957c478bd9Sstevel@tonic-gate 	uint32_t fprs;
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	kpreempt_disable();
3987c478bd9Sstevel@tonic-gate 	if (fpu_exists && ttolwp(curthread) == lwp) {
3997c478bd9Sstevel@tonic-gate 		fprs = _fp_read_fprs();
4007c478bd9Sstevel@tonic-gate 	} else {
4017c478bd9Sstevel@tonic-gate 		kfpu_t *fp = lwptofpu(lwp);
4027c478bd9Sstevel@tonic-gate 		fprs = fp->fpu_fprs;
4037c478bd9Sstevel@tonic-gate 	}
4047c478bd9Sstevel@tonic-gate 	kpreempt_enable();
4057c478bd9Sstevel@tonic-gate 	grp[REG_CCR] = (rp->r_tstate >> TSTATE_CCR_SHIFT) & TSTATE_CCR_MASK;
4067c478bd9Sstevel@tonic-gate 	grp[REG_PC] = rp->r_pc;
4077c478bd9Sstevel@tonic-gate 	grp[REG_nPC] = rp->r_npc;
4087c478bd9Sstevel@tonic-gate 	grp[REG_Y] = (uint32_t)rp->r_y;
4097c478bd9Sstevel@tonic-gate 	grp[REG_G1] = rp->r_g1;
4107c478bd9Sstevel@tonic-gate 	grp[REG_G2] = rp->r_g2;
4117c478bd9Sstevel@tonic-gate 	grp[REG_G3] = rp->r_g3;
4127c478bd9Sstevel@tonic-gate 	grp[REG_G4] = rp->r_g4;
4137c478bd9Sstevel@tonic-gate 	grp[REG_G5] = rp->r_g5;
4147c478bd9Sstevel@tonic-gate 	grp[REG_G6] = rp->r_g6;
4157c478bd9Sstevel@tonic-gate 	grp[REG_G7] = rp->r_g7;
4167c478bd9Sstevel@tonic-gate 	grp[REG_O0] = rp->r_o0;
4177c478bd9Sstevel@tonic-gate 	grp[REG_O1] = rp->r_o1;
4187c478bd9Sstevel@tonic-gate 	grp[REG_O2] = rp->r_o2;
4197c478bd9Sstevel@tonic-gate 	grp[REG_O3] = rp->r_o3;
4207c478bd9Sstevel@tonic-gate 	grp[REG_O4] = rp->r_o4;
4217c478bd9Sstevel@tonic-gate 	grp[REG_O5] = rp->r_o5;
4227c478bd9Sstevel@tonic-gate 	grp[REG_O6] = rp->r_o6;
4237c478bd9Sstevel@tonic-gate 	grp[REG_O7] = rp->r_o7;
4247c478bd9Sstevel@tonic-gate 	grp[REG_ASI] = (rp->r_tstate >> TSTATE_ASI_SHIFT) & TSTATE_ASI_MASK;
4257c478bd9Sstevel@tonic-gate 	grp[REG_FPRS] = fprs;
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate void
getgregs32(klwp_t * lwp,gregset32_t grp)4297c478bd9Sstevel@tonic-gate getgregs32(klwp_t *lwp, gregset32_t grp)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
4327c478bd9Sstevel@tonic-gate 	uint32_t fprs;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	kpreempt_disable();
4357c478bd9Sstevel@tonic-gate 	if (fpu_exists && ttolwp(curthread) == lwp) {
4367c478bd9Sstevel@tonic-gate 		fprs = _fp_read_fprs();
4377c478bd9Sstevel@tonic-gate 	} else {
4387c478bd9Sstevel@tonic-gate 		kfpu_t *fp = lwptofpu(lwp);
4397c478bd9Sstevel@tonic-gate 		fprs = fp->fpu_fprs;
4407c478bd9Sstevel@tonic-gate 	}
4417c478bd9Sstevel@tonic-gate 	kpreempt_enable();
4427c478bd9Sstevel@tonic-gate 	grp[REG_PSR] = mkpsr(rp->r_tstate, fprs);
4437c478bd9Sstevel@tonic-gate 	grp[REG_PC] = rp->r_pc;
4447c478bd9Sstevel@tonic-gate 	grp[REG_nPC] = rp->r_npc;
4457c478bd9Sstevel@tonic-gate 	grp[REG_Y] = rp->r_y;
4467c478bd9Sstevel@tonic-gate 	grp[REG_G1] = rp->r_g1;
4477c478bd9Sstevel@tonic-gate 	grp[REG_G2] = rp->r_g2;
4487c478bd9Sstevel@tonic-gate 	grp[REG_G3] = rp->r_g3;
4497c478bd9Sstevel@tonic-gate 	grp[REG_G4] = rp->r_g4;
4507c478bd9Sstevel@tonic-gate 	grp[REG_G5] = rp->r_g5;
4517c478bd9Sstevel@tonic-gate 	grp[REG_G6] = rp->r_g6;
4527c478bd9Sstevel@tonic-gate 	grp[REG_G7] = rp->r_g7;
4537c478bd9Sstevel@tonic-gate 	grp[REG_O0] = rp->r_o0;
4547c478bd9Sstevel@tonic-gate 	grp[REG_O1] = rp->r_o1;
4557c478bd9Sstevel@tonic-gate 	grp[REG_O2] = rp->r_o2;
4567c478bd9Sstevel@tonic-gate 	grp[REG_O3] = rp->r_o3;
4577c478bd9Sstevel@tonic-gate 	grp[REG_O4] = rp->r_o4;
4587c478bd9Sstevel@tonic-gate 	grp[REG_O5] = rp->r_o5;
4597c478bd9Sstevel@tonic-gate 	grp[REG_O6] = rp->r_o6;
4607c478bd9Sstevel@tonic-gate 	grp[REG_O7] = rp->r_o7;
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate  * Return the user-level PC.
4657c478bd9Sstevel@tonic-gate  * If in a system call, return the address of the syscall trap.
4667c478bd9Sstevel@tonic-gate  */
4677c478bd9Sstevel@tonic-gate greg_t
getuserpc()4687c478bd9Sstevel@tonic-gate getuserpc()
4697c478bd9Sstevel@tonic-gate {
4707c478bd9Sstevel@tonic-gate 	return (lwptoregs(ttolwp(curthread))->r_pc);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate /*
4747c478bd9Sstevel@tonic-gate  * Set register windows.
4757c478bd9Sstevel@tonic-gate  */
4767c478bd9Sstevel@tonic-gate void
setgwins(klwp_t * lwp,gwindows_t * gwins)4777c478bd9Sstevel@tonic-gate setgwins(klwp_t *lwp, gwindows_t *gwins)
4787c478bd9Sstevel@tonic-gate {
4797c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
4807c478bd9Sstevel@tonic-gate 	int wbcnt = gwins->wbcnt;
4817c478bd9Sstevel@tonic-gate 	caddr_t sp;
4827c478bd9Sstevel@tonic-gate 	int i;
4837c478bd9Sstevel@tonic-gate 	struct rwindow32 *rwp;
4847c478bd9Sstevel@tonic-gate 	int wbuf_rwindow_size;
4857c478bd9Sstevel@tonic-gate 	int is64;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate == WSTATE_USER32) {
4887c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE32;
4897c478bd9Sstevel@tonic-gate 		is64 = 0;
4907c478bd9Sstevel@tonic-gate 	} else {
4917c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE64;
4927c478bd9Sstevel@tonic-gate 		is64 = 1;
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 	ASSERT(wbcnt >= 0 && wbcnt <= SPARC_MAXREGWINDOW);
4957c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;
4967c478bd9Sstevel@tonic-gate 	for (i = 0; i < wbcnt; i++) {
4977c478bd9Sstevel@tonic-gate 		sp = (caddr_t)gwins->spbuf[i];
4987c478bd9Sstevel@tonic-gate 		mpcb->mpcb_spbuf[i] = sp;
4997c478bd9Sstevel@tonic-gate 		rwp = (struct rwindow32 *)
5002c5124a1SPrashanth Sreenivasa 		    (mpcb->mpcb_wbuf + (i * wbuf_rwindow_size));
5017c478bd9Sstevel@tonic-gate 		if (is64 && IS_V9STACK(sp))
5027c478bd9Sstevel@tonic-gate 			bcopy(&gwins->wbuf[i], rwp, sizeof (struct rwindow));
5037c478bd9Sstevel@tonic-gate 		else
5047c478bd9Sstevel@tonic-gate 			rwindow_nto32(&gwins->wbuf[i], rwp);
5057c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wbcnt++;
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate void
setgwins32(klwp_t * lwp,gwindows32_t * gwins)5107c478bd9Sstevel@tonic-gate setgwins32(klwp_t *lwp, gwindows32_t *gwins)
5117c478bd9Sstevel@tonic-gate {
5127c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
5137c478bd9Sstevel@tonic-gate 	int wbcnt = gwins->wbcnt;
5147c478bd9Sstevel@tonic-gate 	caddr_t sp;
5157c478bd9Sstevel@tonic-gate 	int i;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	struct rwindow *rwp;
5187c478bd9Sstevel@tonic-gate 	int wbuf_rwindow_size;
5197c478bd9Sstevel@tonic-gate 	int is64;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate == WSTATE_USER32) {
5227c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE32;
5237c478bd9Sstevel@tonic-gate 		is64 = 0;
5247c478bd9Sstevel@tonic-gate 	} else {
5257c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE64;
5267c478bd9Sstevel@tonic-gate 		is64 = 1;
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	ASSERT(wbcnt >= 0 && wbcnt <= SPARC_MAXREGWINDOW);
5307c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbcnt = 0;
5317c478bd9Sstevel@tonic-gate 	for (i = 0; i < wbcnt; i++) {
532aad98a6dSmathue 		sp = (caddr_t)(uintptr_t)gwins->spbuf[i];
5337c478bd9Sstevel@tonic-gate 		mpcb->mpcb_spbuf[i] = sp;
5347c478bd9Sstevel@tonic-gate 		rwp = (struct rwindow *)
5352c5124a1SPrashanth Sreenivasa 		    (mpcb->mpcb_wbuf + (i * wbuf_rwindow_size));
5367c478bd9Sstevel@tonic-gate 		if (is64 && IS_V9STACK(sp))
5377c478bd9Sstevel@tonic-gate 			rwindow_32ton(&gwins->wbuf[i], rwp);
5387c478bd9Sstevel@tonic-gate 		else
5397c478bd9Sstevel@tonic-gate 			bcopy(&gwins->wbuf[i], rwp, sizeof (struct rwindow32));
5407c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wbcnt++;
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate }
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate /*
5457c478bd9Sstevel@tonic-gate  * Get register windows.
5467c478bd9Sstevel@tonic-gate  * NOTE:  'lwp' might not correspond to 'curthread' since this is
5477c478bd9Sstevel@tonic-gate  * called from code in /proc to set the registers of another lwp.
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate void
getgwins(klwp_t * lwp,gwindows_t * gwp)5507c478bd9Sstevel@tonic-gate getgwins(klwp_t *lwp, gwindows_t *gwp)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
5537c478bd9Sstevel@tonic-gate 	int wbcnt = mpcb->mpcb_wbcnt;
5547c478bd9Sstevel@tonic-gate 	caddr_t sp;
5557c478bd9Sstevel@tonic-gate 	int i;
5567c478bd9Sstevel@tonic-gate 	struct rwindow32 *rwp;
5577c478bd9Sstevel@tonic-gate 	int wbuf_rwindow_size;
5587c478bd9Sstevel@tonic-gate 	int is64;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate == WSTATE_USER32) {
5617c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE32;
5627c478bd9Sstevel@tonic-gate 		is64 = 0;
5637c478bd9Sstevel@tonic-gate 	} else {
5647c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE64;
5657c478bd9Sstevel@tonic-gate 		is64 = 1;
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate 	ASSERT(wbcnt >= 0 && wbcnt <= SPARC_MAXREGWINDOW);
5687c478bd9Sstevel@tonic-gate 	gwp->wbcnt = wbcnt;
5697c478bd9Sstevel@tonic-gate 	for (i = 0; i < wbcnt; i++) {
5707c478bd9Sstevel@tonic-gate 		sp = mpcb->mpcb_spbuf[i];
5717c478bd9Sstevel@tonic-gate 		gwp->spbuf[i] = (greg_t *)sp;
5727c478bd9Sstevel@tonic-gate 		rwp = (struct rwindow32 *)
5732c5124a1SPrashanth Sreenivasa 		    (mpcb->mpcb_wbuf + (i * wbuf_rwindow_size));
5747c478bd9Sstevel@tonic-gate 		if (is64 && IS_V9STACK(sp))
5757c478bd9Sstevel@tonic-gate 			bcopy(rwp, &gwp->wbuf[i], sizeof (struct rwindow));
5767c478bd9Sstevel@tonic-gate 		else
5777c478bd9Sstevel@tonic-gate 			rwindow_32ton(rwp, &gwp->wbuf[i]);
5787c478bd9Sstevel@tonic-gate 	}
5797c478bd9Sstevel@tonic-gate }
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate void
getgwins32(klwp_t * lwp,gwindows32_t * gwp)5827c478bd9Sstevel@tonic-gate getgwins32(klwp_t *lwp, gwindows32_t *gwp)
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
5857c478bd9Sstevel@tonic-gate 	int wbcnt = mpcb->mpcb_wbcnt;
5867c478bd9Sstevel@tonic-gate 	int i;
5877c478bd9Sstevel@tonic-gate 	struct rwindow *rwp;
5887c478bd9Sstevel@tonic-gate 	int wbuf_rwindow_size;
5897c478bd9Sstevel@tonic-gate 	caddr_t sp;
5907c478bd9Sstevel@tonic-gate 	int is64;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate == WSTATE_USER32) {
5937c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE32;
5947c478bd9Sstevel@tonic-gate 		is64 = 0;
5957c478bd9Sstevel@tonic-gate 	} else {
5967c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE64;
5977c478bd9Sstevel@tonic-gate 		is64 = 1;
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	ASSERT(wbcnt >= 0 && wbcnt <= SPARC_MAXREGWINDOW);
6017c478bd9Sstevel@tonic-gate 	gwp->wbcnt = wbcnt;
6027c478bd9Sstevel@tonic-gate 	for (i = 0; i < wbcnt; i++) {
6037c478bd9Sstevel@tonic-gate 		sp = mpcb->mpcb_spbuf[i];
6047c478bd9Sstevel@tonic-gate 		rwp = (struct rwindow *)
6052c5124a1SPrashanth Sreenivasa 		    (mpcb->mpcb_wbuf + (i * wbuf_rwindow_size));
606aad98a6dSmathue 		gwp->spbuf[i] = (caddr32_t)(uintptr_t)sp;
6077c478bd9Sstevel@tonic-gate 		if (is64 && IS_V9STACK(sp))
6087c478bd9Sstevel@tonic-gate 			rwindow_nto32(rwp, &gwp->wbuf[i]);
6097c478bd9Sstevel@tonic-gate 		else
6107c478bd9Sstevel@tonic-gate 			bcopy(rwp, &gwp->wbuf[i], sizeof (struct rwindow32));
6117c478bd9Sstevel@tonic-gate 	}
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate /*
6157c478bd9Sstevel@tonic-gate  * For things that depend on register state being on the stack,
6167c478bd9Sstevel@tonic-gate  * copy any register windows that get saved into the window buffer
6177c478bd9Sstevel@tonic-gate  * (in the pcb) onto the stack.  This normally gets fixed up
6187c478bd9Sstevel@tonic-gate  * before returning to a user program.  Callers of this routine
6197c478bd9Sstevel@tonic-gate  * require this to happen immediately because a later kernel
6207c478bd9Sstevel@tonic-gate  * operation depends on window state (like instruction simulation).
6217c478bd9Sstevel@tonic-gate  */
6227c478bd9Sstevel@tonic-gate int
flush_user_windows_to_stack(caddr_t * psp)6237c478bd9Sstevel@tonic-gate flush_user_windows_to_stack(caddr_t *psp)
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	int j, k;
6267c478bd9Sstevel@tonic-gate 	caddr_t sp;
6277c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(ttolwp(curthread));
6287c478bd9Sstevel@tonic-gate 	int err;
6297c478bd9Sstevel@tonic-gate 	int error = 0;
6307c478bd9Sstevel@tonic-gate 	int wbuf_rwindow_size;
6317c478bd9Sstevel@tonic-gate 	int rwindow_size;
6327c478bd9Sstevel@tonic-gate 	int stack_align;
6337c478bd9Sstevel@tonic-gate 	int watched;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	flush_user_windows();
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wstate != WSTATE_USER32)
6387c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE64;
6397c478bd9Sstevel@tonic-gate 	else
6407c478bd9Sstevel@tonic-gate 		wbuf_rwindow_size = WINDOWSIZE32;
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	j = mpcb->mpcb_wbcnt;
6437c478bd9Sstevel@tonic-gate 	while (j > 0) {
6447c478bd9Sstevel@tonic-gate 		sp = mpcb->mpcb_spbuf[--j];
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 		if ((mpcb->mpcb_wstate != WSTATE_USER32) &&
6477c478bd9Sstevel@tonic-gate 		    IS_V9STACK(sp)) {
6487c478bd9Sstevel@tonic-gate 			sp += V9BIAS64;
6497c478bd9Sstevel@tonic-gate 			stack_align = STACK_ALIGN64;
6507c478bd9Sstevel@tonic-gate 			rwindow_size = WINDOWSIZE64;
6517c478bd9Sstevel@tonic-gate 		} else {
652aad98a6dSmathue 			/*
653aad98a6dSmathue 			 * Reduce sp to a 32 bit value.  This was originally
654aad98a6dSmathue 			 * done by casting down to uint32_t and back up to
655aad98a6dSmathue 			 * caddr_t, but one compiler didn't like that, so the
656aad98a6dSmathue 			 * uintptr_t casts were added.  The temporary 32 bit
657aad98a6dSmathue 			 * variable was introduced to avoid depending on all
658aad98a6dSmathue 			 * compilers to generate the desired assembly code for a
659aad98a6dSmathue 			 * quadruple cast in a single expression.
660aad98a6dSmathue 			 */
661aad98a6dSmathue 			caddr32_t sp32 = (uint32_t)(uintptr_t)sp;
662aad98a6dSmathue 			sp = (caddr_t)(uintptr_t)sp32;
663aad98a6dSmathue 
6647c478bd9Sstevel@tonic-gate 			stack_align = STACK_ALIGN32;
6657c478bd9Sstevel@tonic-gate 			rwindow_size = WINDOWSIZE32;
6667c478bd9Sstevel@tonic-gate 		}
6677c478bd9Sstevel@tonic-gate 		if (((uintptr_t)sp & (stack_align - 1)) != 0)
6687c478bd9Sstevel@tonic-gate 			continue;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 		watched = watch_disable_addr(sp, rwindow_size, S_WRITE);
6717c478bd9Sstevel@tonic-gate 		err = xcopyout(mpcb->mpcb_wbuf +
6727c478bd9Sstevel@tonic-gate 		    (j * wbuf_rwindow_size), sp, rwindow_size);
6737c478bd9Sstevel@tonic-gate 		if (err != 0) {
6747c478bd9Sstevel@tonic-gate 			if (psp != NULL) {
6757c478bd9Sstevel@tonic-gate 				/*
6767c478bd9Sstevel@tonic-gate 				 * Determine the offending address.
6777c478bd9Sstevel@tonic-gate 				 * It may not be the stack pointer itself.
6787c478bd9Sstevel@tonic-gate 				 */
6797c478bd9Sstevel@tonic-gate 				uint_t *kaddr = (uint_t *)(mpcb->mpcb_wbuf +
6807c478bd9Sstevel@tonic-gate 				    (j * wbuf_rwindow_size));
6817c478bd9Sstevel@tonic-gate 				uint_t *uaddr = (uint_t *)sp;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 				for (k = 0;
6847c478bd9Sstevel@tonic-gate 				    k < rwindow_size / sizeof (int);
6857c478bd9Sstevel@tonic-gate 				    k++, kaddr++, uaddr++) {
6867c478bd9Sstevel@tonic-gate 					if (suword32(uaddr, *kaddr))
6877c478bd9Sstevel@tonic-gate 						break;
6887c478bd9Sstevel@tonic-gate 				}
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 				/* can't happen? */
6917c478bd9Sstevel@tonic-gate 				if (k == rwindow_size / sizeof (int))
6927c478bd9Sstevel@tonic-gate 					uaddr = (uint_t *)sp;
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 				*psp = (caddr_t)uaddr;
6957c478bd9Sstevel@tonic-gate 			}
6967c478bd9Sstevel@tonic-gate 			error = err;
6977c478bd9Sstevel@tonic-gate 		} else {
6987c478bd9Sstevel@tonic-gate 			/*
6997c478bd9Sstevel@tonic-gate 			 * stack was aligned and copyout succeeded;
7007c478bd9Sstevel@tonic-gate 			 * move other windows down.
7017c478bd9Sstevel@tonic-gate 			 */
7027c478bd9Sstevel@tonic-gate 			mpcb->mpcb_wbcnt--;
7037c478bd9Sstevel@tonic-gate 			for (k = j; k < mpcb->mpcb_wbcnt; k++) {
7047c478bd9Sstevel@tonic-gate 				mpcb->mpcb_spbuf[k] = mpcb->mpcb_spbuf[k+1];
7057c478bd9Sstevel@tonic-gate 				bcopy(
7067c478bd9Sstevel@tonic-gate 				    mpcb->mpcb_wbuf +
7072c5124a1SPrashanth Sreenivasa 				    ((k+1) * wbuf_rwindow_size),
7087c478bd9Sstevel@tonic-gate 				    mpcb->mpcb_wbuf +
7092c5124a1SPrashanth Sreenivasa 				    (k * wbuf_rwindow_size),
7107c478bd9Sstevel@tonic-gate 				    wbuf_rwindow_size);
7117c478bd9Sstevel@tonic-gate 			}
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 		if (watched)
7147c478bd9Sstevel@tonic-gate 			watch_enable_addr(sp, rwindow_size, S_WRITE);
7157c478bd9Sstevel@tonic-gate 	} /* while there are windows in the wbuf */
7167c478bd9Sstevel@tonic-gate 	return (error);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate static int
copy_return_window32(int dotwo)7207c478bd9Sstevel@tonic-gate copy_return_window32(int dotwo)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
7237c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
7247c478bd9Sstevel@tonic-gate 	struct rwindow32 rwindow32;
7257c478bd9Sstevel@tonic-gate 	caddr_t sp1;
7267c478bd9Sstevel@tonic-gate 	caddr_t sp2;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
7297c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_rsp[0] == NULL) {
730aad98a6dSmathue 		/*
731aad98a6dSmathue 		 * Reduce r_sp to a 32 bit value before storing it in sp1.  This
732aad98a6dSmathue 		 * was originally done by casting down to uint32_t and back up
733aad98a6dSmathue 		 * to caddr_t, but that generated complaints under one compiler.
734aad98a6dSmathue 		 * The uintptr_t cast was added to address that, and the
735aad98a6dSmathue 		 * temporary 32 bit variable was introduced to avoid depending
736aad98a6dSmathue 		 * on all compilers to generate the desired assembly code for a
737aad98a6dSmathue 		 * triple cast in a single expression.
738aad98a6dSmathue 		 */
739aad98a6dSmathue 		caddr32_t sp1_32 = (uint32_t)lwptoregs(lwp)->r_sp;
740aad98a6dSmathue 		sp1 = (caddr_t)(uintptr_t)sp1_32;
741aad98a6dSmathue 
7427c478bd9Sstevel@tonic-gate 		if ((copyin_nowatch(sp1, &rwindow32,
7437c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow32))) == 0)
7447c478bd9Sstevel@tonic-gate 			mpcb->mpcb_rsp[0] = sp1;
7457c478bd9Sstevel@tonic-gate 		rwindow_32ton(&rwindow32, &mpcb->mpcb_rwin[0]);
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 	mpcb->mpcb_rsp[1] = NULL;
7487c478bd9Sstevel@tonic-gate 	if (dotwo && mpcb->mpcb_rsp[0] != NULL &&
7497c478bd9Sstevel@tonic-gate 	    (sp2 = (caddr_t)mpcb->mpcb_rwin[0].rw_fp) != NULL) {
7507c478bd9Sstevel@tonic-gate 		if ((copyin_nowatch(sp2, &rwindow32,
7517c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow32)) == 0))
7527c478bd9Sstevel@tonic-gate 			mpcb->mpcb_rsp[1] = sp2;
7537c478bd9Sstevel@tonic-gate 		rwindow_32ton(&rwindow32, &mpcb->mpcb_rwin[1]);
7547c478bd9Sstevel@tonic-gate 	}
7557c478bd9Sstevel@tonic-gate 	return (mpcb->mpcb_rsp[0] != NULL);
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate int
copy_return_window(int dotwo)7597c478bd9Sstevel@tonic-gate copy_return_window(int dotwo)
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
7627c478bd9Sstevel@tonic-gate 	klwp_t *lwp;
7637c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
7647c478bd9Sstevel@tonic-gate 	caddr_t sp1;
7657c478bd9Sstevel@tonic-gate 	caddr_t sp2;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_ILP32)
7687c478bd9Sstevel@tonic-gate 		return (copy_return_window32(dotwo));
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	lwp = ttolwp(curthread);
7717c478bd9Sstevel@tonic-gate 	mpcb = lwptompcb(lwp);
7727c478bd9Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
7737c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_rsp[0] == NULL) {
7747c478bd9Sstevel@tonic-gate 		sp1 = (caddr_t)lwptoregs(lwp)->r_sp + STACK_BIAS;
7757c478bd9Sstevel@tonic-gate 		if ((copyin_nowatch(sp1, &mpcb->mpcb_rwin[0],
7767c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow)) == 0))
7777c478bd9Sstevel@tonic-gate 			mpcb->mpcb_rsp[0] = sp1 - STACK_BIAS;
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 	mpcb->mpcb_rsp[1] = NULL;
7807c478bd9Sstevel@tonic-gate 	if (dotwo && mpcb->mpcb_rsp[0] != NULL &&
7817c478bd9Sstevel@tonic-gate 	    (sp2 = (caddr_t)mpcb->mpcb_rwin[0].rw_fp) != NULL) {
7827c478bd9Sstevel@tonic-gate 		sp2 += STACK_BIAS;
7837c478bd9Sstevel@tonic-gate 		if ((copyin_nowatch(sp2, &mpcb->mpcb_rwin[1],
7847c478bd9Sstevel@tonic-gate 		    sizeof (struct rwindow)) == 0))
7857c478bd9Sstevel@tonic-gate 			mpcb->mpcb_rsp[1] = sp2 - STACK_BIAS;
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate 	return (mpcb->mpcb_rsp[0] != NULL);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * Clear registers on exec(2).
7927c478bd9Sstevel@tonic-gate  */
7937c478bd9Sstevel@tonic-gate void
setregs(uarg_t * args)7947c478bd9Sstevel@tonic-gate setregs(uarg_t *args)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	struct regs *rp;
7977c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
7987c478bd9Sstevel@tonic-gate 	kfpu_t *fpp = lwptofpu(lwp);
7997c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(lwp);
8007c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(curthread);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	/*
8037c478bd9Sstevel@tonic-gate 	 * Initialize user registers.
8047c478bd9Sstevel@tonic-gate 	 */
8057c478bd9Sstevel@tonic-gate 	(void) save_syscall_args();	/* copy args from registers first */
8067c478bd9Sstevel@tonic-gate 	rp = lwptoregs(lwp);
8077c478bd9Sstevel@tonic-gate 	rp->r_g1 = rp->r_g2 = rp->r_g3 = rp->r_g4 = rp->r_g5 =
8087c478bd9Sstevel@tonic-gate 	    rp->r_g6 = rp->r_o0 = rp->r_o1 = rp->r_o2 =
8097c478bd9Sstevel@tonic-gate 	    rp->r_o3 = rp->r_o4 = rp->r_o5 = rp->r_o7 = 0;
8107c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_ILP32)
8112c5124a1SPrashanth Sreenivasa 		rp->r_tstate = TSTATE_USER32 | weakest_mem_model;
8127c478bd9Sstevel@tonic-gate 	else
8132c5124a1SPrashanth Sreenivasa 		rp->r_tstate = TSTATE_USER64 | weakest_mem_model;
8147c478bd9Sstevel@tonic-gate 	if (!fpu_exists)
8157c478bd9Sstevel@tonic-gate 		rp->r_tstate &= ~TSTATE_PEF;
8167c478bd9Sstevel@tonic-gate 	rp->r_g7 = args->thrptr;
8177c478bd9Sstevel@tonic-gate 	rp->r_pc = args->entry;
8187c478bd9Sstevel@tonic-gate 	rp->r_npc = args->entry + 4;
8197c478bd9Sstevel@tonic-gate 	rp->r_y = 0;
8207c478bd9Sstevel@tonic-gate 	curthread->t_post_sys = 1;
8217c478bd9Sstevel@tonic-gate 	lwp->lwp_eosys = JUSTRETURN;
822*12551037SToomas Soome 	lwp->lwp_pcb.pcb_trap0addr = 0;	/* no trap 0 handler */
8237c478bd9Sstevel@tonic-gate 	/*
8247c478bd9Sstevel@tonic-gate 	 * Clear the fixalignment flag
8257c478bd9Sstevel@tonic-gate 	 */
8267c478bd9Sstevel@tonic-gate 	p->p_fixalignment = 0;
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate 	/*
8297c478bd9Sstevel@tonic-gate 	 * Throw out old user windows, init window buf.
8307c478bd9Sstevel@tonic-gate 	 */
8317c478bd9Sstevel@tonic-gate 	trash_user_windows();
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	if (p->p_model == DATAMODEL_LP64 &&
8347c478bd9Sstevel@tonic-gate 	    mpcb->mpcb_wstate != WSTATE_USER64) {
8357c478bd9Sstevel@tonic-gate 		ASSERT(mpcb->mpcb_wbcnt == 0);
836a8599265Selowe 		kmem_cache_free(wbuf32_cache, mpcb->mpcb_wbuf);
837a8599265Selowe 		mpcb->mpcb_wbuf = kmem_cache_alloc(wbuf64_cache, KM_SLEEP);
8387c478bd9Sstevel@tonic-gate 		ASSERT(((uintptr_t)mpcb->mpcb_wbuf & 7) == 0);
8397c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wstate = WSTATE_USER64;
8407c478bd9Sstevel@tonic-gate 	} else if (p->p_model == DATAMODEL_ILP32 &&
8417c478bd9Sstevel@tonic-gate 	    mpcb->mpcb_wstate != WSTATE_USER32) {
8427c478bd9Sstevel@tonic-gate 		ASSERT(mpcb->mpcb_wbcnt == 0);
843a8599265Selowe 		kmem_cache_free(wbuf64_cache, mpcb->mpcb_wbuf);
844a8599265Selowe 		mpcb->mpcb_wbuf = kmem_cache_alloc(wbuf32_cache, KM_SLEEP);
8457c478bd9Sstevel@tonic-gate 		mpcb->mpcb_wstate = WSTATE_USER32;
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate 	mpcb->mpcb_pa = va_to_pa(mpcb);
8487c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = va_to_pa(mpcb->mpcb_wbuf);
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/*
8517c478bd9Sstevel@tonic-gate 	 * Here we initialize minimal fpu state.
8527c478bd9Sstevel@tonic-gate 	 * The rest is done at the first floating
8537c478bd9Sstevel@tonic-gate 	 * point instruction that a process executes
8547c478bd9Sstevel@tonic-gate 	 * or by the lib_psr memcpy routines.
8557c478bd9Sstevel@tonic-gate 	 */
8567c478bd9Sstevel@tonic-gate 	if (fpu_exists) {
8577c478bd9Sstevel@tonic-gate 		extern void _fp_write_fprs(unsigned);
8587c478bd9Sstevel@tonic-gate 		_fp_write_fprs(0);
8597c478bd9Sstevel@tonic-gate 	}
8607c478bd9Sstevel@tonic-gate 	fpp->fpu_en = 0;
8617c478bd9Sstevel@tonic-gate 	fpp->fpu_fprs = 0;
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate void
lwp_swapin(kthread_t * tp)8657c478bd9Sstevel@tonic-gate lwp_swapin(kthread_t *tp)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb = lwptompcb(ttolwp(tp));
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 	mpcb->mpcb_pa = va_to_pa(mpcb);
8707c478bd9Sstevel@tonic-gate 	mpcb->mpcb_wbuf_pa = va_to_pa(mpcb->mpcb_wbuf);
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate /*
8747c478bd9Sstevel@tonic-gate  * Construct the execution environment for the user's signal
8757c478bd9Sstevel@tonic-gate  * handler and arrange for control to be given to it on return
8767c478bd9Sstevel@tonic-gate  * to userland.  The library code now calls setcontext() to
8777c478bd9Sstevel@tonic-gate  * clean up after the signal handler, so sigret() is no longer
8787c478bd9Sstevel@tonic-gate  * needed.
8797c478bd9Sstevel@tonic-gate  */
8807c478bd9Sstevel@tonic-gate int
sendsig(int sig,k_siginfo_t * sip,void (* hdlr)())8817c478bd9Sstevel@tonic-gate sendsig(int sig, k_siginfo_t *sip, void (*hdlr)())
8827c478bd9Sstevel@tonic-gate {
8837c478bd9Sstevel@tonic-gate 	/*
8847c478bd9Sstevel@tonic-gate 	 * 'volatile' is needed to ensure that values are
8857c478bd9Sstevel@tonic-gate 	 * correct on the error return from on_fault().
8867c478bd9Sstevel@tonic-gate 	 */
8877c478bd9Sstevel@tonic-gate 	volatile int minstacksz; /* min stack required to catch signal */
8887c478bd9Sstevel@tonic-gate 	int newstack = 0;	/* if true, switching to altstack */
8897c478bd9Sstevel@tonic-gate 	label_t ljb;
8907c478bd9Sstevel@tonic-gate 	caddr_t sp;
8917c478bd9Sstevel@tonic-gate 	struct regs *volatile rp;
8927c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
8937c478bd9Sstevel@tonic-gate 	proc_t *volatile p = ttoproc(curthread);
8947c478bd9Sstevel@tonic-gate 	int fpq_size = 0;
8957c478bd9Sstevel@tonic-gate 	struct sigframe {
8967c478bd9Sstevel@tonic-gate 		struct frame frwin;
8977c478bd9Sstevel@tonic-gate 		ucontext_t uc;
8987c478bd9Sstevel@tonic-gate 	};
8997c478bd9Sstevel@tonic-gate 	siginfo_t *sip_addr;
9007c478bd9Sstevel@tonic-gate 	struct sigframe *volatile fp;
9017c478bd9Sstevel@tonic-gate 	ucontext_t *volatile tuc = NULL;
9027c478bd9Sstevel@tonic-gate 	char *volatile xregs = NULL;
9037c478bd9Sstevel@tonic-gate 	volatile size_t xregs_size = 0;
9047c478bd9Sstevel@tonic-gate 	gwindows_t *volatile gwp = NULL;
9057c478bd9Sstevel@tonic-gate 	volatile int gwin_size = 0;
9067c478bd9Sstevel@tonic-gate 	kfpu_t *fpp;
9077c478bd9Sstevel@tonic-gate 	struct machpcb *mpcb;
9087c478bd9Sstevel@tonic-gate 	volatile int watched = 0;
9097c478bd9Sstevel@tonic-gate 	volatile int watched2 = 0;
9107c478bd9Sstevel@tonic-gate 	caddr_t tos;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	/*
9137c478bd9Sstevel@tonic-gate 	 * Make sure the current last user window has been flushed to
9147c478bd9Sstevel@tonic-gate 	 * the stack save area before we change the sp.
9157c478bd9Sstevel@tonic-gate 	 * Restore register window if a debugger modified it.
9167c478bd9Sstevel@tonic-gate 	 */
9177c478bd9Sstevel@tonic-gate 	(void) flush_user_windows_to_stack(NULL);
9187c478bd9Sstevel@tonic-gate 	if (lwp->lwp_pcb.pcb_xregstat != XREGNONE)
9197c478bd9Sstevel@tonic-gate 		xregrestore(lwp, 0);
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate 	mpcb = lwptompcb(lwp);
9227c478bd9Sstevel@tonic-gate 	rp = lwptoregs(lwp);
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/*
9257c478bd9Sstevel@tonic-gate 	 * Clear the watchpoint return stack pointers.
9267c478bd9Sstevel@tonic-gate 	 */
9277c478bd9Sstevel@tonic-gate 	mpcb->mpcb_rsp[0] = NULL;
9287c478bd9Sstevel@tonic-gate 	mpcb->mpcb_rsp[1] = NULL;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	minstacksz = sizeof (struct sigframe);
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	/*
9337c478bd9Sstevel@tonic-gate 	 * We know that sizeof (siginfo_t) is stack-aligned:
9347c478bd9Sstevel@tonic-gate 	 * 128 bytes for ILP32, 256 bytes for LP64.
9357c478bd9Sstevel@tonic-gate 	 */
9367c478bd9Sstevel@tonic-gate 	if (sip != NULL)
9377c478bd9Sstevel@tonic-gate 		minstacksz += sizeof (siginfo_t);
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	/*
9407c478bd9Sstevel@tonic-gate 	 * These two fields are pointed to by ABI structures and may
9417c478bd9Sstevel@tonic-gate 	 * be of arbitrary length. Size them now so we know how big
9427c478bd9Sstevel@tonic-gate 	 * the signal frame has to be.
9437c478bd9Sstevel@tonic-gate 	 */
9447c478bd9Sstevel@tonic-gate 	fpp = lwptofpu(lwp);
9457c478bd9Sstevel@tonic-gate 	fpp->fpu_fprs = _fp_read_fprs();
9467c478bd9Sstevel@tonic-gate 	if ((fpp->fpu_en) || (fpp->fpu_fprs & FPRS_FEF)) {
9477c478bd9Sstevel@tonic-gate 		fpq_size = fpp->fpu_q_entrysize * fpp->fpu_qcnt;
9487c478bd9Sstevel@tonic-gate 		minstacksz += SA(fpq_size);
9497c478bd9Sstevel@tonic-gate 	}
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	mpcb = lwptompcb(lwp);
9527c478bd9Sstevel@tonic-gate 	if (mpcb->mpcb_wbcnt != 0) {
9537c478bd9Sstevel@tonic-gate 		gwin_size = (mpcb->mpcb_wbcnt * sizeof (struct rwindow)) +
9547c478bd9Sstevel@tonic-gate 		    (SPARC_MAXREGWINDOW * sizeof (caddr_t)) + sizeof (long);
9557c478bd9Sstevel@tonic-gate 		minstacksz += SA(gwin_size);
9567c478bd9Sstevel@tonic-gate 	}
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 	/*
9597c478bd9Sstevel@tonic-gate 	 * Extra registers, if support by this platform, may be of arbitrary
9607c478bd9Sstevel@tonic-gate 	 * length. Size them now so we know how big the signal frame has to be.
9617c478bd9Sstevel@tonic-gate 	 * For sparcv9 _LP64 user programs, use asrs instead of the xregs.
9627c478bd9Sstevel@tonic-gate 	 */
9637c478bd9Sstevel@tonic-gate 	minstacksz += SA(xregs_size);
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate 	/*
9667c478bd9Sstevel@tonic-gate 	 * Figure out whether we will be handling this signal on
9677c478bd9Sstevel@tonic-gate 	 * an alternate stack specified by the user. Then allocate
9687c478bd9Sstevel@tonic-gate 	 * and validate the stack requirements for the signal handler
9697c478bd9Sstevel@tonic-gate 	 * context. on_fault will catch any faults.
9707c478bd9Sstevel@tonic-gate 	 */
971ae115bc7Smrj 	newstack = (sigismember(&PTOU(curproc)->u_sigonstack, sig) &&
9727c478bd9Sstevel@tonic-gate 	    !(lwp->lwp_sigaltstack.ss_flags & (SS_ONSTACK|SS_DISABLE)));
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate 	tos = (caddr_t)rp->r_sp + STACK_BIAS;
975a3c55825Sraf 	/*
976a3c55825Sraf 	 * Force proper stack pointer alignment, even in the face of a
977a3c55825Sraf 	 * misaligned stack pointer from user-level before the signal.
978a3c55825Sraf 	 * Don't use the SA() macro because that rounds up, not down.
979a3c55825Sraf 	 */
980a3c55825Sraf 	tos = (caddr_t)((uintptr_t)tos & ~(STACK_ALIGN - 1ul));
981a3c55825Sraf 
9827c478bd9Sstevel@tonic-gate 	if (newstack != 0) {
9837c478bd9Sstevel@tonic-gate 		fp = (struct sigframe *)
9847c478bd9Sstevel@tonic-gate 		    (SA((uintptr_t)lwp->lwp_sigaltstack.ss_sp) +
9852c5124a1SPrashanth Sreenivasa 		    SA((int)lwp->lwp_sigaltstack.ss_size) - STACK_ALIGN -
9862c5124a1SPrashanth Sreenivasa 		    SA(minstacksz));
9877c478bd9Sstevel@tonic-gate 	} else {
9887c478bd9Sstevel@tonic-gate 		/*
9897c478bd9Sstevel@tonic-gate 		 * If we were unable to flush all register windows to
9907c478bd9Sstevel@tonic-gate 		 * the stack and we are not now on an alternate stack,
9917c478bd9Sstevel@tonic-gate 		 * just dump core with a SIGSEGV back in psig().
9927c478bd9Sstevel@tonic-gate 		 */
9937c478bd9Sstevel@tonic-gate 		if (sig == SIGSEGV &&
9947c478bd9Sstevel@tonic-gate 		    mpcb->mpcb_wbcnt != 0 &&
9957c478bd9Sstevel@tonic-gate 		    !(lwp->lwp_sigaltstack.ss_flags & SS_ONSTACK))
9967c478bd9Sstevel@tonic-gate 			return (0);
9977c478bd9Sstevel@tonic-gate 		fp = (struct sigframe *)(tos - SA(minstacksz));
9987c478bd9Sstevel@tonic-gate 		/*
9997c478bd9Sstevel@tonic-gate 		 * Could call grow here, but stack growth now handled below
10007c478bd9Sstevel@tonic-gate 		 * in code protected by on_fault().
10017c478bd9Sstevel@tonic-gate 		 */
10027c478bd9Sstevel@tonic-gate 	}
10037c478bd9Sstevel@tonic-gate 	sp = (caddr_t)fp + sizeof (struct sigframe);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	/*
10067c478bd9Sstevel@tonic-gate 	 * Make sure process hasn't trashed its stack.
10077c478bd9Sstevel@tonic-gate 	 */
1008a3c55825Sraf 	if ((caddr_t)fp >= p->p_usrstack ||
10097c478bd9Sstevel@tonic-gate 	    (caddr_t)fp + SA(minstacksz) >= p->p_usrstack) {
10107c478bd9Sstevel@tonic-gate #ifdef DEBUG
10117c478bd9Sstevel@tonic-gate 		printf("sendsig: bad signal stack cmd=%s, pid=%d, sig=%d\n",
10127c478bd9Sstevel@tonic-gate 		    PTOU(p)->u_comm, p->p_pid, sig);
10137c478bd9Sstevel@tonic-gate 		printf("sigsp = 0x%p, action = 0x%p, upc = 0x%lx\n",
10147c478bd9Sstevel@tonic-gate 		    (void *)fp, (void *)hdlr, rp->r_pc);
1015a3c55825Sraf 		printf("fp above USRSTACK\n");
10167c478bd9Sstevel@tonic-gate #endif
10177c478bd9Sstevel@tonic-gate 		return (0);
10187c478bd9Sstevel@tonic-gate 	}
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate 	watched = watch_disable_addr((caddr_t)fp, SA(minstacksz), S_WRITE);
10217c478bd9Sstevel@tonic-gate 	if (on_fault(&ljb))
10227c478bd9Sstevel@tonic-gate 		goto badstack;
10237c478bd9Sstevel@tonic-gate 
10247c478bd9Sstevel@tonic-gate 	tuc = kmem_alloc(sizeof (ucontext_t), KM_SLEEP);
1025bdf0047cSRoger A. Faulkner 	savecontext(tuc, &lwp->lwp_sigoldmask);
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate 	/*
10287c478bd9Sstevel@tonic-gate 	 * save extra register state if it exists
10297c478bd9Sstevel@tonic-gate 	 */
10307c478bd9Sstevel@tonic-gate 	if (xregs_size != 0) {
10317c478bd9Sstevel@tonic-gate 		xregs_setptr(lwp, tuc, sp);
10327c478bd9Sstevel@tonic-gate 		xregs = kmem_alloc(xregs_size, KM_SLEEP);
10337c478bd9Sstevel@tonic-gate 		xregs_get(lwp, xregs);
10347c478bd9Sstevel@tonic-gate 		copyout_noerr(xregs, sp, xregs_size);
10357c478bd9Sstevel@tonic-gate 		kmem_free(xregs, xregs_size);
10367c478bd9Sstevel@tonic-gate 		xregs = NULL;
10377c478bd9Sstevel@tonic-gate 		sp += SA(xregs_size);
10387c478bd9Sstevel@tonic-gate 	}
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	copyout_noerr(tuc, &fp->uc, sizeof (*tuc));
10417c478bd9Sstevel@tonic-gate 	kmem_free(tuc, sizeof (*tuc));
10427c478bd9Sstevel@tonic-gate 	tuc = NULL;
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate 	if (sip != NULL) {
10457c478bd9Sstevel@tonic-gate 		zoneid_t zoneid;
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate 		uzero(sp, sizeof (siginfo_t));
10487c478bd9Sstevel@tonic-gate 		if (SI_FROMUSER(sip) &&
10497c478bd9Sstevel@tonic-gate 		    (zoneid = p->p_zone->zone_id) != GLOBAL_ZONEID &&
10507c478bd9Sstevel@tonic-gate 		    zoneid != sip->si_zoneid) {
1051