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