xref: /illumos-gate/usr/src/uts/intel/ia32/os/archdep.c (revision cff040f3)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
59acbbeafSnn  * Common Development and Distribution License (the "License").
69acbbeafSnn  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22eb5a5c78SSurya Prakki  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
26*cff040f3SRobert Mustacchi /*	  All Rights Reserved	*/
27ebb8ac07SRobert Mustacchi /*
28a32a1f37SRobert Mustacchi  * Copyright (c) 2018, Joyent, Inc.
29ff512403SGarrett D'Amore  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
30ebb8ac07SRobert Mustacchi  */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
357c478bd9Sstevel@tonic-gate #include <sys/systm.h>
367c478bd9Sstevel@tonic-gate #include <sys/signal.h>
377c478bd9Sstevel@tonic-gate #include <sys/stack.h>
387c478bd9Sstevel@tonic-gate #include <sys/regset.h>
397c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
407c478bd9Sstevel@tonic-gate #include <sys/frame.h>
417c478bd9Sstevel@tonic-gate #include <sys/proc.h>
427c478bd9Sstevel@tonic-gate #include <sys/psw.h>
437c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
447c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
457c478bd9Sstevel@tonic-gate #include <sys/asm_linkage.h>
467c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
477c478bd9Sstevel@tonic-gate #include <sys/errno.h>
487c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
497c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
507c478bd9Sstevel@tonic-gate #include <sys/debug.h>
517c478bd9Sstevel@tonic-gate #include <sys/elf.h>
527c478bd9Sstevel@tonic-gate #include <sys/spl.h>
537c478bd9Sstevel@tonic-gate #include <sys/time.h>
547c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
557c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
567c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
577c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
587c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
597c478bd9Sstevel@tonic-gate #include <sys/panic.h>
607c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
617c478bd9Sstevel@tonic-gate #include <sys/time.h>
627c478bd9Sstevel@tonic-gate #include <sys/fp.h>
637c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
647c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
657c478bd9Sstevel@tonic-gate #include <sys/auxv_386.h>
667c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
679acbbeafSnn #include <sys/brand.h>
689acbbeafSnn #include <sys/machbrand.h>
697af88ac7SKuriakose Kuruvilla #include <sys/cmn_err.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate  * Map an fnsave-formatted save area into an fxsave-formatted save area.
737c478bd9Sstevel@tonic-gate  *
747c478bd9Sstevel@tonic-gate  * Most fields are the same width, content and semantics.  However
757c478bd9Sstevel@tonic-gate  * the tag word is compressed.
767c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate static void
fnsave_to_fxsave(const struct fnsave_state * fn,struct fxsave_state * fx)787c478bd9Sstevel@tonic-gate fnsave_to_fxsave(const struct fnsave_state *fn, struct fxsave_state *fx)
797c478bd9Sstevel@tonic-gate {
807c478bd9Sstevel@tonic-gate 	uint_t i, tagbits;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	fx->fx_fcw = fn->f_fcw;
837c478bd9Sstevel@tonic-gate 	fx->fx_fsw = fn->f_fsw;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	/*
867c478bd9Sstevel@tonic-gate 	 * copy element by element (because of holes)
877c478bd9Sstevel@tonic-gate 	 */
887c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++)
897c478bd9Sstevel@tonic-gate 		bcopy(&fn->f_st[i].fpr_16[0], &fx->fx_st[i].fpr_16[0],
907c478bd9Sstevel@tonic-gate 		    sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	/*
937c478bd9Sstevel@tonic-gate 	 * synthesize compressed tag bits
947c478bd9Sstevel@tonic-gate 	 */
957c478bd9Sstevel@tonic-gate 	fx->fx_fctw = 0;
967c478bd9Sstevel@tonic-gate 	for (tagbits = fn->f_ftw, i = 0; i < 8; i++, tagbits >>= 2)
977c478bd9Sstevel@tonic-gate 		if ((tagbits & 3) != 3)
987c478bd9Sstevel@tonic-gate 			fx->fx_fctw |= (1 << i);
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	fx->fx_fop = fn->f_fop;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate #if defined(__amd64)
1037c478bd9Sstevel@tonic-gate 	fx->fx_rip = (uint64_t)fn->f_eip;
1047c478bd9Sstevel@tonic-gate 	fx->fx_rdp = (uint64_t)fn->f_dp;
1057c478bd9Sstevel@tonic-gate #else
1067c478bd9Sstevel@tonic-gate 	fx->fx_eip = fn->f_eip;
1077c478bd9Sstevel@tonic-gate 	fx->fx_cs = fn->f_cs;
1087c478bd9Sstevel@tonic-gate 	fx->__fx_ign0 = 0;
1097c478bd9Sstevel@tonic-gate 	fx->fx_dp = fn->f_dp;
1107c478bd9Sstevel@tonic-gate 	fx->fx_ds = fn->f_ds;
1117c478bd9Sstevel@tonic-gate 	fx->__fx_ign1 = 0;
1127c478bd9Sstevel@tonic-gate #endif
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * Map from an fxsave-format save area to an fnsave-format save area.
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate static void
fxsave_to_fnsave(const struct fxsave_state * fx,struct fnsave_state * fn)1197c478bd9Sstevel@tonic-gate fxsave_to_fnsave(const struct fxsave_state *fx, struct fnsave_state *fn)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	uint_t i, top, tagbits;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	fn->f_fcw = fx->fx_fcw;
1247c478bd9Sstevel@tonic-gate 	fn->__f_ign0 = 0;
1257c478bd9Sstevel@tonic-gate 	fn->f_fsw = fx->fx_fsw;
1267c478bd9Sstevel@tonic-gate 	fn->__f_ign1 = 0;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	top = (fx->fx_fsw & FPS_TOP) >> 11;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * copy element by element (because of holes)
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++)
1347c478bd9Sstevel@tonic-gate 		bcopy(&fx->fx_st[i].fpr_16[0], &fn->f_st[i].fpr_16[0],
1357c478bd9Sstevel@tonic-gate 		    sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/*
1387c478bd9Sstevel@tonic-gate 	 * synthesize uncompressed tag bits
1397c478bd9Sstevel@tonic-gate 	 */
1407c478bd9Sstevel@tonic-gate 	fn->f_ftw = 0;
1417c478bd9Sstevel@tonic-gate 	for (tagbits = fx->fx_fctw, i = 0; i < 8; i++, tagbits >>= 1) {
1427c478bd9Sstevel@tonic-gate 		uint_t ibit, expo;
1437c478bd9Sstevel@tonic-gate 		const uint16_t *fpp;
1447c478bd9Sstevel@tonic-gate 		static const uint16_t zero[5] = { 0, 0, 0, 0, 0 };
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		if ((tagbits & 1) == 0) {
1477c478bd9Sstevel@tonic-gate 			fn->f_ftw |= 3 << (i << 1);	/* empty */
1487c478bd9Sstevel@tonic-gate 			continue;
1497c478bd9Sstevel@tonic-gate 		}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 		/*
1527c478bd9Sstevel@tonic-gate 		 * (tags refer to *physical* registers)
1537c478bd9Sstevel@tonic-gate 		 */
1547c478bd9Sstevel@tonic-gate 		fpp = &fx->fx_st[(i - top + 8) & 7].fpr_16[0];
1557c478bd9Sstevel@tonic-gate 		ibit = fpp[3] >> 15;
1567c478bd9Sstevel@tonic-gate 		expo = fpp[4] & 0x7fff;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		if (ibit && expo != 0 && expo != 0x7fff)
1597c478bd9Sstevel@tonic-gate 			continue;			/* valid fp number */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		if (bcmp(fpp, &zero, sizeof (zero)))
1627c478bd9Sstevel@tonic-gate 			fn->f_ftw |= 2 << (i << 1);	/* NaN */
1637c478bd9Sstevel@tonic-gate 		else
1647c478bd9Sstevel@tonic-gate 			fn->f_ftw |= 1 << (i << 1);	/* fp zero */
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	fn->f_fop = fx->fx_fop;
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	fn->__f_ign2 = 0;
1707c478bd9Sstevel@tonic-gate #if defined(__amd64)
1717c478bd9Sstevel@tonic-gate 	fn->f_eip = (uint32_t)fx->fx_rip;
1727c478bd9Sstevel@tonic-gate 	fn->f_cs = U32CS_SEL;
1737c478bd9Sstevel@tonic-gate 	fn->f_dp = (uint32_t)fx->fx_rdp;
1747c478bd9Sstevel@tonic-gate 	fn->f_ds = UDS_SEL;
1757c478bd9Sstevel@tonic-gate #else
1767c478bd9Sstevel@tonic-gate 	fn->f_eip = fx->fx_eip;
1777c478bd9Sstevel@tonic-gate 	fn->f_cs = fx->fx_cs;
1787c478bd9Sstevel@tonic-gate 	fn->f_dp = fx->fx_dp;
1797c478bd9Sstevel@tonic-gate 	fn->f_ds = fx->fx_ds;
1807c478bd9Sstevel@tonic-gate #endif
1817c478bd9Sstevel@tonic-gate 	fn->__f_ign3 = 0;
1827c478bd9Sstevel@tonic-gate }
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate  * Map from an fpregset_t into an fxsave-format save area
1867c478bd9Sstevel@tonic-gate  */
1877c478bd9Sstevel@tonic-gate static void
fpregset_to_fxsave(const fpregset_t * fp,struct fxsave_state * fx)1887c478bd9Sstevel@tonic-gate fpregset_to_fxsave(const fpregset_t *fp, struct fxsave_state *fx)
1897c478bd9Sstevel@tonic-gate {
1907c478bd9Sstevel@tonic-gate #if defined(__amd64)
1917c478bd9Sstevel@tonic-gate 	bcopy(fp, fx, sizeof (*fx));
1927c478bd9Sstevel@tonic-gate #else
193bc0e9132SGordon Ross 	const struct _fpchip_state *fc = &fp->fp_reg_set.fpchip_state;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	fnsave_to_fxsave((const struct fnsave_state *)fc, fx);
1967c478bd9Sstevel@tonic-gate 	fx->fx_mxcsr = fc->mxcsr;
1977c478bd9Sstevel@tonic-gate 	bcopy(&fc->xmm[0], &fx->fx_xmm[0], sizeof (fc->xmm));
1987c478bd9Sstevel@tonic-gate #endif
1997c478bd9Sstevel@tonic-gate 	/*
2007c478bd9Sstevel@tonic-gate 	 * avoid useless #gp exceptions - mask reserved bits
2017c478bd9Sstevel@tonic-gate 	 */
2027c478bd9Sstevel@tonic-gate 	fx->fx_mxcsr &= sse_mxcsr_mask;
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /*
2067c478bd9Sstevel@tonic-gate  * Map from an fxsave-format save area into a fpregset_t
2077c478bd9Sstevel@tonic-gate  */
2087c478bd9Sstevel@tonic-gate static void
fxsave_to_fpregset(const struct fxsave_state * fx,fpregset_t * fp)2097c478bd9Sstevel@tonic-gate fxsave_to_fpregset(const struct fxsave_state *fx, fpregset_t *fp)
2107c478bd9Sstevel@tonic-gate {
2117c478bd9Sstevel@tonic-gate #if defined(__amd64)
2127c478bd9Sstevel@tonic-gate 	bcopy(fx, fp, sizeof (*fx));
2137c478bd9Sstevel@tonic-gate #else
214bc0e9132SGordon Ross 	struct _fpchip_state *fc = &fp->fp_reg_set.fpchip_state;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	fxsave_to_fnsave(fx, (struct fnsave_state *)fc);
2177c478bd9Sstevel@tonic-gate 	fc->mxcsr = fx->fx_mxcsr;
2187c478bd9Sstevel@tonic-gate 	bcopy(&fx->fx_xmm[0], &fc->xmm[0], sizeof (fc->xmm));
2197c478bd9Sstevel@tonic-gate #endif
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2237c478bd9Sstevel@tonic-gate static void
fpregset32_to_fxsave(const fpregset32_t * fp,struct fxsave_state * fx)2247c478bd9Sstevel@tonic-gate fpregset32_to_fxsave(const fpregset32_t *fp, struct fxsave_state *fx)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	const struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	fnsave_to_fxsave((const struct fnsave_state *)fc, fx);
2297c478bd9Sstevel@tonic-gate 	/*
2307c478bd9Sstevel@tonic-gate 	 * avoid useless #gp exceptions - mask reserved bits
2317c478bd9Sstevel@tonic-gate 	 */
2327c478bd9Sstevel@tonic-gate 	fx->fx_mxcsr = sse_mxcsr_mask & fc->mxcsr;
2337c478bd9Sstevel@tonic-gate 	bcopy(&fc->xmm[0], &fx->fx_xmm[0], sizeof (fc->xmm));
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate static void
fxsave_to_fpregset32(const struct fxsave_state * fx,fpregset32_t * fp)2377c478bd9Sstevel@tonic-gate fxsave_to_fpregset32(const struct fxsave_state *fx, fpregset32_t *fp)
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	fxsave_to_fnsave(fx, (struct fnsave_state *)fc);
2427c478bd9Sstevel@tonic-gate 	fc->mxcsr = fx->fx_mxcsr;
2437c478bd9Sstevel@tonic-gate 	bcopy(&fx->fx_xmm[0], &fc->xmm[0], sizeof (fc->xmm));
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate static void
fpregset_nto32(const fpregset_t * src,fpregset32_t * dst)2477c478bd9Sstevel@tonic-gate fpregset_nto32(const fpregset_t *src, fpregset32_t *dst)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 	fxsave_to_fpregset32((struct fxsave_state *)src, dst);
2507c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.status =
2517c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.status;
2527c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.xstatus =
2537c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.xstatus;
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static void
fpregset_32ton(const fpregset32_t * src,fpregset_t * dst)2577c478bd9Sstevel@tonic-gate fpregset_32ton(const fpregset32_t *src, fpregset_t *dst)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	fpregset32_to_fxsave(src, (struct fxsave_state *)dst);
2607c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.status =
2617c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.status;
2627c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.xstatus =
2637c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.xstatus;
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate #endif
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate /*
2687c478bd9Sstevel@tonic-gate  * Set floating-point registers from a native fpregset_t.
2697c478bd9Sstevel@tonic-gate  */
2707c478bd9Sstevel@tonic-gate void
setfpregs(klwp_t * lwp,fpregset_t * fp)2717c478bd9Sstevel@tonic-gate setfpregs(klwp_t *lwp, fpregset_t *fp)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate 	struct fpu_ctx *fpu = &lwp->lwp_pcb.pcb_fpu;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (fpu->fpu_flags & FPU_EN) {
2767c478bd9Sstevel@tonic-gate 		if (!(fpu->fpu_flags & FPU_VALID)) {
2777c478bd9Sstevel@tonic-gate 			/*
2787c478bd9Sstevel@tonic-gate 			 * FPU context is still active, release the
2797c478bd9Sstevel@tonic-gate 			 * ownership.
2807c478bd9Sstevel@tonic-gate 			 */
2817c478bd9Sstevel@tonic-gate 			fp_free(fpu, 0);
2827c478bd9Sstevel@tonic-gate 		}
2837af88ac7SKuriakose Kuruvilla 	}
2847af88ac7SKuriakose Kuruvilla 	/*
2857af88ac7SKuriakose Kuruvilla 	 * Else: if we are trying to change the FPU state of a thread which
2867af88ac7SKuriakose Kuruvilla 	 * hasn't yet initialized floating point, store the state in
2877af88ac7SKuriakose Kuruvilla 	 * the pcb and indicate that the state is valid.  When the
2887af88ac7SKuriakose Kuruvilla 	 * thread enables floating point, it will use this state instead
2897af88ac7SKuriakose Kuruvilla 	 * of the default state.
2907af88ac7SKuriakose Kuruvilla 	 */
2917af88ac7SKuriakose Kuruvilla 
2927af88ac7SKuriakose Kuruvilla 	switch (fp_save_mech) {
2937af88ac7SKuriakose Kuruvilla #if defined(__i386)
2947af88ac7SKuriakose Kuruvilla 	case FP_FNSAVE:
295088d69f8SJerry Jelinek 		bcopy(fp, fpu->fpu_regs.kfpu_u.kfpu_fn,
296088d69f8SJerry Jelinek 		    sizeof (*fpu->fpu_regs.kfpu_u.kfpu_fn));
2977af88ac7SKuriakose Kuruvilla 		break;
2987c478bd9Sstevel@tonic-gate #endif
2997af88ac7SKuriakose Kuruvilla 	case FP_FXSAVE:
300088d69f8SJerry Jelinek 		fpregset_to_fxsave(fp, fpu->fpu_regs.kfpu_u.kfpu_fx);
3017af88ac7SKuriakose Kuruvilla 		fpu->fpu_regs.kfpu_xstatus =
3027af88ac7SKuriakose Kuruvilla 		    fp->fp_reg_set.fpchip_state.xstatus;
3037af88ac7SKuriakose Kuruvilla 		break;
3047af88ac7SKuriakose Kuruvilla 
3057af88ac7SKuriakose Kuruvilla 	case FP_XSAVE:
3067af88ac7SKuriakose Kuruvilla 		fpregset_to_fxsave(fp,
307088d69f8SJerry Jelinek 		    &fpu->fpu_regs.kfpu_u.kfpu_xs->xs_fxsave);
3087af88ac7SKuriakose Kuruvilla 		fpu->fpu_regs.kfpu_xstatus =
3097af88ac7SKuriakose Kuruvilla 		    fp->fp_reg_set.fpchip_state.xstatus;
310088d69f8SJerry Jelinek 		fpu->fpu_regs.kfpu_u.kfpu_xs->xs_xstate_bv |=
3117af88ac7SKuriakose Kuruvilla 		    (XFEATURE_LEGACY_FP | XFEATURE_SSE);
3127af88ac7SKuriakose Kuruvilla 		break;
3137af88ac7SKuriakose Kuruvilla 	default:
3147af88ac7SKuriakose Kuruvilla 		panic("Invalid fp_save_mech");
3157af88ac7SKuriakose Kuruvilla 		/*NOTREACHED*/
3167c478bd9Sstevel@tonic-gate 	}
3177af88ac7SKuriakose Kuruvilla 
3187af88ac7SKuriakose Kuruvilla 	fpu->fpu_regs.kfpu_status = fp->fp_reg_set.fpchip_state.status;
3197af88ac7SKuriakose Kuruvilla 	fpu->fpu_flags |= FPU_VALID;
3204c28a617SRobert Mustacchi 	PCB_SET_UPDATE_FPU(&lwp->lwp_pcb);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate  * Get floating-point registers into a native fpregset_t.
3257c478bd9Sstevel@tonic-gate  */
3267c478bd9Sstevel@tonic-gate void
getfpregs(klwp_t * lwp,fpregset_t * fp)3277c478bd9Sstevel@tonic-gate getfpregs(klwp_t *lwp, fpregset_t *fp)
3287c478bd9Sstevel@tonic-gate {
3297c478bd9Sstevel@tonic-gate 	struct fpu_ctx *fpu = &lwp->lwp_pcb.pcb_fpu;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	kpreempt_disable();
3327c478bd9Sstevel@tonic-gate 	if (fpu->fpu_flags & FPU_EN) {
3337c478bd9Sstevel@tonic-gate 		/*
3347c478bd9Sstevel@tonic-gate 		 * If we have FPU hw and the thread's pcb doesn't have
3357c478bd9Sstevel@tonic-gate 		 * a valid FPU state then get the state from the hw.
3367c478bd9Sstevel@tonic-gate 		 */
3377c478bd9Sstevel@tonic-gate 		if (fpu_exists && ttolwp(curthread) == lwp &&
3387c478bd9Sstevel@tonic-gate 		    !(fpu->fpu_flags & FPU_VALID))
3397c478bd9Sstevel@tonic-gate 			fp_save(fpu); /* get the current FPU state */
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * There are 3 possible cases we have to be aware of here:
3447c478bd9Sstevel@tonic-gate 	 *
3457c478bd9Sstevel@tonic-gate 	 * 1. FPU is enabled.  FPU state is stored in the current LWP.
3467c478bd9Sstevel@tonic-gate 	 *
3477c478bd9Sstevel@tonic-gate 	 * 2. FPU is not enabled, and there have been no intervening /proc
3487c478bd9Sstevel@tonic-gate 	 *    modifications.  Return initial FPU state.
3497c478bd9Sstevel@tonic-gate 	 *
3507c478bd9Sstevel@tonic-gate 	 * 3. FPU is not enabled, but a /proc consumer has modified FPU state.
3517c478bd9Sstevel@tonic-gate 	 *    FPU state is stored in the current LWP.
3527c478bd9Sstevel@tonic-gate 	 */
3537c478bd9Sstevel@tonic-gate 	if ((fpu->fpu_flags & FPU_EN) || (fpu->fpu_flags & FPU_VALID)) {
3547c478bd9Sstevel@tonic-gate 		/*
3557c478bd9Sstevel@tonic-gate 		 * Cases 1 and 3.
3567c478bd9Sstevel@tonic-gate 		 */
3577af88ac7SKuriakose Kuruvilla 		switch (fp_save_mech) {
3587af88ac7SKuriakose Kuruvilla #if defined(__i386)
3597af88ac7SKuriakose Kuruvilla 		case FP_FNSAVE:
360088d69f8SJerry Jelinek 			bcopy(fpu->fpu_regs.kfpu_u.kfpu_fn, fp,
361088d69f8SJerry Jelinek 			    sizeof (*fpu->fpu_regs.kfpu_u.kfpu_fn));
3627af88ac7SKuriakose Kuruvilla 			break;
3637c478bd9Sstevel@tonic-gate #endif
3647af88ac7SKuriakose Kuruvilla 		case FP_FXSAVE:
365088d69f8SJerry Jelinek 			fxsave_to_fpregset(fpu->fpu_regs.kfpu_u.kfpu_fx, fp);
3667c478bd9Sstevel@tonic-gate 			fp->fp_reg_set.fpchip_state.xstatus =
3677c478bd9Sstevel@tonic-gate 			    fpu->fpu_regs.kfpu_xstatus;
3687af88ac7SKuriakose Kuruvilla 			break;
3697af88ac7SKuriakose Kuruvilla 		case FP_XSAVE:
3707af88ac7SKuriakose Kuruvilla 			fxsave_to_fpregset(
371088d69f8SJerry Jelinek 			    &fpu->fpu_regs.kfpu_u.kfpu_xs->xs_fxsave, fp);
3727af88ac7SKuriakose Kuruvilla 			fp->fp_reg_set.fpchip_state.xstatus =
3737af88ac7SKuriakose Kuruvilla 			    fpu->fpu_regs.kfpu_xstatus;
3747af88ac7SKuriakose Kuruvilla 			break;
3757af88ac7SKuriakose Kuruvilla 		default:
3767af88ac7SKuriakose Kuruvilla 			panic("Invalid fp_save_mech");
3777af88ac7SKuriakose Kuruvilla 			/*NOTREACHED*/
3787af88ac7SKuriakose Kuruvilla 		}
3797c478bd9Sstevel@tonic-gate 		fp->fp_reg_set.fpchip_state.status = fpu->fpu_regs.kfpu_status;
3807c478bd9Sstevel@tonic-gate 	} else {
3817c478bd9Sstevel@tonic-gate 		/*
3827c478bd9Sstevel@tonic-gate 		 * Case 2.
3837c478bd9Sstevel@tonic-gate 		 */
3847af88ac7SKuriakose Kuruvilla 		switch (fp_save_mech) {
3857af88ac7SKuriakose Kuruvilla #if defined(__i386)
3867af88ac7SKuriakose Kuruvilla 		case FP_FNSAVE:
3877af88ac7SKuriakose Kuruvilla 			bcopy(&x87_initial, fp, sizeof (x87_initial));
3887af88ac7SKuriakose Kuruvilla 			break;
3897c478bd9Sstevel@tonic-gate #endif
3907af88ac7SKuriakose Kuruvilla 		case FP_FXSAVE:
3917af88ac7SKuriakose Kuruvilla 		case FP_XSAVE:
3927af88ac7SKuriakose Kuruvilla 			/*
3937af88ac7SKuriakose Kuruvilla 			 * For now, we don't have any AVX specific field in ABI.
3947af88ac7SKuriakose Kuruvilla 			 * If we add any in the future, we need to initial them
3957af88ac7SKuriakose Kuruvilla 			 * as well.
3967af88ac7SKuriakose Kuruvilla 			 */
3977c478bd9Sstevel@tonic-gate 			fxsave_to_fpregset(&sse_initial, fp);
3987c478bd9Sstevel@tonic-gate 			fp->fp_reg_set.fpchip_state.xstatus =
3997c478bd9Sstevel@tonic-gate 			    fpu->fpu_regs.kfpu_xstatus;
4007af88ac7SKuriakose Kuruvilla 			break;
4017af88ac7SKuriakose Kuruvilla 		default:
4027af88ac7SKuriakose Kuruvilla 			panic("Invalid fp_save_mech");
4037af88ac7SKuriakose Kuruvilla 			/*NOTREACHED*/
4047af88ac7SKuriakose Kuruvilla 		}
4057c478bd9Sstevel@tonic-gate 		fp->fp_reg_set.fpchip_state.status = fpu->fpu_regs.kfpu_status;
4067c478bd9Sstevel@tonic-gate 	}
4077c478bd9Sstevel@tonic-gate 	kpreempt_enable();
4087c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * Set floating-point registers from an fpregset32_t.
4147c478bd9Sstevel@tonic-gate  */
4157c478bd9Sstevel@tonic-gate void
setfpregs32(klwp_t * lwp,fpregset32_t * fp)4167c478bd9Sstevel@tonic-gate setfpregs32(klwp_t *lwp, fpregset32_t *fp)
4177c478bd9Sstevel@tonic-gate {
4187c478bd9Sstevel@tonic-gate 	fpregset_t fpregs;
4197c478bd9Sstevel@tonic-gate 
4207c478bd9Sstevel@tonic-gate 	fpregset_32ton(fp, &fpregs);
4217c478bd9Sstevel@tonic-gate 	setfpregs(lwp, &fpregs);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate /*
4257c478bd9Sstevel@tonic-gate  * Get floating-point registers into an fpregset32_t.
4267c478bd9Sstevel@tonic-gate  */
4277c478bd9Sstevel@tonic-gate void
getfpregs32(klwp_t * lwp,fpregset32_t * fp)4287c478bd9Sstevel@tonic-gate getfpregs32(klwp_t *lwp, fpregset32_t *fp)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 	fpregset_t fpregs;
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	getfpregs(lwp, &fpregs);
4337c478bd9Sstevel@tonic-gate 	fpregset_nto32(&fpregs, fp);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate /*
4397c478bd9Sstevel@tonic-gate  * Return the general registers
4407c478bd9Sstevel@tonic-gate  */
4417c478bd9Sstevel@tonic-gate void
getgregs(klwp_t * lwp,gregset_t grp)4427c478bd9Sstevel@tonic-gate getgregs(klwp_t *lwp, gregset_t grp)
4437c478bd9Sstevel@tonic-gate {
4447c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
4457c478bd9Sstevel@tonic-gate #if defined(__amd64)
4467c478bd9Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
4477c478bd9Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	grp[REG_RDI] = rp->r_rdi;
4507c478bd9Sstevel@tonic-gate 	grp[REG_RSI] = rp->r_rsi;
4517c478bd9Sstevel@tonic-gate 	grp[REG_RDX] = rp->r_rdx;
4527c478bd9Sstevel@tonic-gate 	grp[REG_RCX] = rp->r_rcx;
4537c478bd9Sstevel@tonic-gate 	grp[REG_R8] = rp->r_r8;
4547c478bd9Sstevel@tonic-gate 	grp[REG_R9] = rp->r_r9;
4557c478bd9Sstevel@tonic-gate 	grp[REG_RAX] = rp->r_rax;
4567c478bd9Sstevel@tonic-gate 	grp[REG_RBX] = rp->r_rbx;
4577c478bd9Sstevel@tonic-gate 	grp[REG_RBP] = rp->r_rbp;
4587c478bd9Sstevel@tonic-gate 	grp[REG_R10] = rp->r_r10;
4597c478bd9Sstevel@tonic-gate 	grp[REG_R11] = rp->r_r11;
4607c478bd9Sstevel@tonic-gate 	grp[REG_R12] = rp->r_r12;
4617c478bd9Sstevel@tonic-gate 	grp[REG_R13] = rp->r_r13;
4627c478bd9Sstevel@tonic-gate 	grp[REG_R14] = rp->r_r14;
4637c478bd9Sstevel@tonic-gate 	grp[REG_R15] = rp->r_r15;
4647c478bd9Sstevel@tonic-gate 	grp[REG_FSBASE] = pcb->pcb_fsbase;
4657c478bd9Sstevel@tonic-gate 	grp[REG_GSBASE] = pcb->pcb_gsbase;
4667c478bd9Sstevel@tonic-gate 	if (thisthread)
4677c478bd9Sstevel@tonic-gate 		kpreempt_disable();
4684c28a617SRobert Mustacchi 	if (PCB_NEED_UPDATE_SEGS(pcb)) {
4697c478bd9Sstevel@tonic-gate 		grp[REG_DS] = pcb->pcb_ds;
4707c478bd9Sstevel@tonic-gate 		grp[REG_ES] = pcb->pcb_es;
4717c478bd9Sstevel@tonic-gate 		grp[REG_FS] = pcb->pcb_fs;
4727c478bd9Sstevel@tonic-gate 		grp[REG_GS] = pcb->pcb_gs;
4737c478bd9Sstevel@tonic-gate 	} else {
4747c478bd9Sstevel@tonic-gate 		grp[REG_DS] = rp->r_ds;
4757c478bd9Sstevel@tonic-gate 		grp[REG_ES] = rp->r_es;
4767c478bd9Sstevel@tonic-gate 		grp[REG_FS] = rp->r_fs;
4777c478bd9Sstevel@tonic-gate 		grp[REG_GS] = rp->r_gs;
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 	if (thisthread)
4807c478bd9Sstevel@tonic-gate 		kpreempt_enable();
4817c478bd9Sstevel@tonic-gate 	grp[REG_TRAPNO] = rp->r_trapno;
4827c478bd9Sstevel@tonic-gate 	grp[REG_ERR] = rp->r_err;
4837c478bd9Sstevel@tonic-gate 	grp[REG_RIP] = rp->r_rip;
4847c478bd9Sstevel@tonic-gate 	grp[REG_CS] = rp->r_cs;
4857c478bd9Sstevel@tonic-gate 	grp[REG_SS] = rp->r_ss;
4867c478bd9Sstevel@tonic-gate 	grp[REG_RFL] = rp->r_rfl;
4877c478bd9Sstevel@tonic-gate 	grp[REG_RSP] = rp->r_rsp;
4887c478bd9Sstevel@tonic-gate #else
4897c478bd9Sstevel@tonic-gate 	bcopy(&rp->r_gs, grp, sizeof (gregset_t));
4907c478bd9Sstevel@tonic-gate #endif
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate void
getgregs32(klwp_t * lwp,gregset32_t grp)4967c478bd9Sstevel@tonic-gate getgregs32(klwp_t *lwp, gregset32_t grp)
4977c478bd9Sstevel@tonic-gate {
4987c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
4997c478bd9Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
5007c478bd9Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	if (thisthread)
5037c478bd9Sstevel@tonic-gate 		kpreempt_disable();
5044c28a617SRobert Mustacchi 	if (PCB_NEED_UPDATE_SEGS(pcb)) {
5057c478bd9Sstevel@tonic-gate 		grp[GS] = (uint16_t)pcb->pcb_gs;
5067c478bd9Sstevel@tonic-gate 		grp[FS] = (uint16_t)pcb->pcb_fs;
5077c478bd9Sstevel@tonic-gate 		grp[DS] = (uint16_t)pcb->pcb_ds;
5087c478bd9Sstevel@tonic-gate 		grp[ES] = (uint16_t)pcb->pcb_es;
5097c478bd9Sstevel@tonic-gate 	} else {
5107c478bd9Sstevel@tonic-gate 		grp[GS] = (uint16_t)rp->r_gs;
5117c478bd9Sstevel@tonic-gate 		grp[FS] = (uint16_t)rp->r_fs;
5127c478bd9Sstevel@tonic-gate 		grp[DS] = (uint16_t)rp->r_ds;
5137c478bd9Sstevel@tonic-gate 		grp[ES] = (uint16_t)rp->r_es;
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 	if (thisthread)
5167c478bd9Sstevel@tonic-gate 		kpreempt_enable();
5177c478bd9Sstevel@tonic-gate 	grp[EDI] = (greg32_t)rp->r_rdi;
5187c478bd9Sstevel@tonic-gate 	grp[ESI] = (greg32_t)rp->r_rsi;
5197c478bd9Sstevel@tonic-gate 	grp[EBP] = (greg32_t)rp->r_rbp;
5207c478bd9Sstevel@tonic-gate 	grp[ESP] = 0;
5217c478bd9Sstevel@tonic-gate 	grp[EBX] = (greg32_t)rp->r_rbx;
5227c478bd9Sstevel@tonic-gate 	grp[EDX] = (greg32_t)rp->r_rdx;
5237c478bd9Sstevel@tonic-gate 	grp[ECX] = (greg32_t)rp->r_rcx;
5247c478bd9Sstevel@tonic-gate 	grp[EAX] = (greg32_t)rp->r_rax;
5257c478bd9Sstevel@tonic-gate 	grp[TRAPNO] = (greg32_t)rp->r_trapno;
5267c478bd9Sstevel@tonic-gate 	grp[ERR] = (greg32_t)rp->r_err;
5277c478bd9Sstevel@tonic-gate 	grp[EIP] = (greg32_t)rp->r_rip;
5287c478bd9Sstevel@tonic-gate 	grp[CS] = (uint16_t)rp->r_cs;
5297c478bd9Sstevel@tonic-gate 	grp[EFL] = (greg32_t)rp->r_rfl;
5307c478bd9Sstevel@tonic-gate 	grp[UESP] = (greg32_t)rp->r_rsp;
5317c478bd9Sstevel@tonic-gate 	grp[SS] = (uint16_t)rp->r_ss;
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate void
ucontext_32ton(const ucontext32_t * src,ucontext_t * dst)5357c478bd9Sstevel@tonic-gate ucontext_32ton(const ucontext32_t *src, ucontext_t *dst)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate 	mcontext_t *dmc = &dst->uc_mcontext;
5387c478bd9Sstevel@tonic-gate 	const mcontext32_t *smc = &src->uc_mcontext;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	bzero(dst, sizeof (*dst));
5417c478bd9Sstevel@tonic-gate 	dst->uc_flags = src->uc_flags;
5427c478bd9Sstevel@tonic-gate 	dst->uc_link = (ucontext_t *)(uintptr_t)src->uc_link;
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	bcopy(&src->uc_sigmask, &dst->uc_sigmask, sizeof (dst->uc_sigmask));
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	dst->uc_stack.ss_sp = (void *)(uintptr_t)src->uc_stack.ss_sp;
5477c478bd9Sstevel@tonic-gate 	dst->uc_stack.ss_size = (size_t)src->uc_stack.ss_size;
5487c478bd9Sstevel@tonic-gate 	dst->uc_stack.ss_flags = src->uc_stack.ss_flags;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_GS] = (greg_t)(uint32_t)smc->gregs[GS];
5517c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_FS] = (greg_t)(uint32_t)smc->gregs[FS];
5527c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_ES] = (greg_t)(uint32_t)smc->gregs[ES];
5537c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_DS] = (greg_t)(uint32_t)smc->gregs[DS];
5547c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RDI] = (greg_t)(uint32_t)smc->gregs[EDI];
5557c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RSI] = (greg_t)(uint32_t)smc->gregs[ESI];
5567c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RBP] = (greg_t)(uint32_t)smc->gregs[EBP];
5577c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RBX] = (greg_t)(uint32_t)smc->gregs[EBX];
5587c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RDX] = (greg_t)(uint32_t)smc->gregs[EDX];
5597c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RCX] = (greg_t)(uint32_t)smc->gregs[ECX];
5607c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RAX] = (greg_t)(uint32_t)smc->gregs[EAX];
5617c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_TRAPNO] = (greg_t)(uint32_t)smc->gregs[TRAPNO];
5627c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_ERR] = (greg_t)(uint32_t)smc->gregs[ERR];
5637c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RIP] = (greg_t)(uint32_t)smc->gregs[EIP];
5647c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_CS] = (greg_t)(uint32_t)smc->gregs[CS];
5657c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RFL] = (greg_t)(uint32_t)smc->gregs[EFL];
5667c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RSP] = (greg_t)(uint32_t)smc->gregs[UESP];
5677c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_SS] = (greg_t)(uint32_t)smc->gregs[SS];
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	/*
5707c478bd9Sstevel@tonic-gate 	 * A valid fpregs is only copied in if uc.uc_flags has UC_FPU set
5717c478bd9Sstevel@tonic-gate 	 * otherwise there is no guarantee that anything in fpregs is valid.
5727c478bd9Sstevel@tonic-gate 	 */
5737c478bd9Sstevel@tonic-gate 	if (src->uc_flags & UC_FPU)
5747c478bd9Sstevel@tonic-gate 		fpregset_32ton(&src->uc_mcontext.fpregs,
5757c478bd9Sstevel@tonic-gate 		    &dst->uc_mcontext.fpregs);
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate  * Return the user-level PC.
5827c478bd9Sstevel@tonic-gate  * If in a system call, return the address of the syscall trap.
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate greg_t
getuserpc()5857c478bd9Sstevel@tonic-gate getuserpc()
5867c478bd9Sstevel@tonic-gate {
5877c478bd9Sstevel@tonic-gate 	greg_t upc = lwptoregs(ttolwp(curthread))->r_pc;
5887c478bd9Sstevel@tonic-gate 	uint32_t insn;
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	if (curthread->t_sysnum == 0)
5917c478bd9Sstevel@tonic-gate 		return (upc);
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	/*
5947c478bd9Sstevel@tonic-gate 	 * We might've gotten here from sysenter (0xf 0x34),
5957c478bd9Sstevel@tonic-gate 	 * syscall (0xf 0x5) or lcall (0x9a 0 0 0 0 0x27 0).
5967c478bd9Sstevel@tonic-gate 	 *
5977c478bd9Sstevel@tonic-gate 	 * Go peek at the binary to figure it out..
5987c478bd9Sstevel@tonic-gate 	 */
5997c478bd9Sstevel@tonic-gate 	if (fuword32((void *)(upc - 2), &insn) != -1 &&
6007c478bd9Sstevel@tonic-gate 	    (insn & 0xffff) == 0x340f || (insn & 0xffff) == 0x050f)
6017c478bd9Sstevel@tonic-gate 		return (upc - 2);
6027c478bd9Sstevel@tonic-gate 	return (upc - 7);
6037c478bd9Sstevel@tonic-gate }
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate /*
6067c478bd9Sstevel@tonic-gate  * Protect segment registers from non-user privilege levels and GDT selectors
6077c478bd9Sstevel@tonic-gate  * other than USER_CS, USER_DS and lwp FS and GS values.  If the segment
6087c478bd9Sstevel@tonic-gate  * selector is non-null and not USER_CS/USER_DS, we make sure that the
6097c478bd9Sstevel@tonic-gate  * TI bit is set to point into the LDT and that the RPL is set to 3.
6107c478bd9Sstevel@tonic-gate  *
6117c478bd9Sstevel@tonic-gate  * Since struct regs stores each 16-bit segment register as a 32-bit greg_t, we
6127c478bd9Sstevel@tonic-gate  * also explicitly zero the top 16 bits since they may be coming from the
6137c478bd9Sstevel@tonic-gate  * user's address space via setcontext(2) or /proc.
614843e1988Sjohnlev  *
615843e1988Sjohnlev  * Note about null selector. When running on the hypervisor if we allow a
616843e1988Sjohnlev  * process to set its %cs to null selector with RPL of 0 the hypervisor will
617843e1988Sjohnlev  * crash the domain. If running on bare metal we would get a #gp fault and
618843e1988Sjohnlev  * be able to kill the process and continue on. Therefore we make sure to
619843e1988Sjohnlev  * force RPL to SEL_UPL even for null selector when setting %cs.
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate 
622843e1988Sjohnlev #if defined(IS_CS) || defined(IS_NOT_CS)
623843e1988Sjohnlev #error	"IS_CS and IS_NOT_CS already defined"
624843e1988Sjohnlev #endif
625843e1988Sjohnlev 
626843e1988Sjohnlev #define	IS_CS		1
627843e1988Sjohnlev #define	IS_NOT_CS	0
628843e1988Sjohnlev 
6297c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6307c478bd9Sstevel@tonic-gate static greg_t
fix_segreg(greg_t sr,int iscs,model_t datamodel)631843e1988Sjohnlev fix_segreg(greg_t sr, int iscs, model_t datamodel)
6327c478bd9Sstevel@tonic-gate {
6337c478bd9Sstevel@tonic-gate 	switch (sr &= 0xffff) {
634843e1988Sjohnlev 
635843e1988Sjohnlev 	case 0:
636843e1988Sjohnlev 		if (iscs == IS_CS)
637843e1988Sjohnlev 			return (0 | SEL_UPL);
638843e1988Sjohnlev 		else
639843e1988Sjohnlev 			return (0);
640843e1988Sjohnlev 
6417c478bd9Sstevel@tonic-gate #if defined(__amd64)
6427c478bd9Sstevel@tonic-gate 	/*
6437c478bd9Sstevel@tonic-gate 	 * If lwp attempts to switch data model then force their
6447c478bd9Sstevel@tonic-gate 	 * code selector to be null selector.
6457c478bd9Sstevel@tonic-gate 	 */
6467c478bd9Sstevel@tonic-gate 	case U32CS_SEL:
6477c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE)
648843e1988Sjohnlev 			return (0 | SEL_UPL);
6497c478bd9Sstevel@tonic-gate 		else
6507c478bd9Sstevel@tonic-gate 			return (sr);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	case UCS_SEL:
6537c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_ILP32)
654843e1988Sjohnlev 			return (0 | SEL_UPL);
6557c478bd9Sstevel@tonic-gate #elif defined(__i386)
6567c478bd9Sstevel@tonic-gate 	case UCS_SEL:
6577c478bd9Sstevel@tonic-gate #endif
6587c478bd9Sstevel@tonic-gate 	/*FALLTHROUGH*/
6597c478bd9Sstevel@tonic-gate 	case UDS_SEL:
6607c478bd9Sstevel@tonic-gate 	case LWPFS_SEL:
6617c478bd9Sstevel@tonic-gate 	case LWPGS_SEL:
662843e1988Sjohnlev 	case SEL_UPL:
6637c478bd9Sstevel@tonic-gate 		return (sr);
6647c478bd9Sstevel@tonic-gate 	default:
6657c478bd9Sstevel@tonic-gate 		break;
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	/*
6690baeff3dSrab 	 * Force it into the LDT in ring 3 for 32-bit processes, which by
6700baeff3dSrab 	 * default do not have an LDT, so that any attempt to use an invalid
671843e1988Sjohnlev 	 * selector will reference the (non-existant) LDT, and cause a #gp
672843e1988Sjohnlev 	 * fault for the process.
6730baeff3dSrab 	 *
6747c478bd9Sstevel@tonic-gate 	 * 64-bit processes get the null gdt selector since they
6757c478bd9Sstevel@tonic-gate 	 * are not allowed to have a private LDT.
6767c478bd9Sstevel@tonic-gate 	 */
6777c478bd9Sstevel@tonic-gate #if defined(__amd64)
678843e1988Sjohnlev 	if (datamodel == DATAMODEL_ILP32) {
679843e1988Sjohnlev 		return (sr | SEL_TI_LDT | SEL_UPL);
680843e1988Sjohnlev 	} else {
681843e1988Sjohnlev 		if (iscs == IS_CS)
682843e1988Sjohnlev 			return (0 | SEL_UPL);
683843e1988Sjohnlev 		else
684843e1988Sjohnlev 			return (0);
685843e1988Sjohnlev 	}
686843e1988Sjohnlev 
6877c478bd9Sstevel@tonic-gate #elif defined(__i386)
6887c478bd9Sstevel@tonic-gate 	return (sr | SEL_TI_LDT | SEL_UPL);
6897c478bd9Sstevel@tonic-gate #endif
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate /*
6937c478bd9Sstevel@tonic-gate  * Set general registers.
6947c478bd9Sstevel@tonic-gate  */
6957c478bd9Sstevel@tonic-gate void
setgregs(klwp_t * lwp,gregset_t grp)6967c478bd9Sstevel@tonic-gate setgregs(klwp_t *lwp, gregset_t grp)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
6997c478bd9Sstevel@tonic-gate 	model_t	datamodel = lwp_getdatamodel(lwp);
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate #if defined(__amd64)
7027c478bd9Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
7037c478bd9Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 	if (datamodel == DATAMODEL_NATIVE) {
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 		if (thisthread)
7087c478bd9Sstevel@tonic-gate 			(void) save_syscall_args();	/* copy the args */
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 		rp->r_rdi = grp[REG_RDI];
7117c478bd9Sstevel@tonic-gate 		rp->r_rsi = grp[REG_RSI];
7127c478bd9Sstevel@tonic-gate 		rp->r_rdx = grp[REG_RDX];
7137c478bd9Sstevel@tonic-gate 		rp->r_rcx = grp[REG_RCX];
7147c478bd9Sstevel@tonic-gate 		rp->r_r8 = grp[REG_R8];
7157c478bd9Sstevel@tonic-gate 		rp->r_r9 = grp[REG_R9];
7167c478bd9Sstevel@tonic-gate 		rp->r_rax = grp[REG_RAX];
7177c478bd9Sstevel@tonic-gate 		rp->r_rbx = grp[REG_RBX];
7187c478bd9Sstevel@tonic-gate 		rp->r_rbp = grp[REG_RBP];
7197c478bd9Sstevel@tonic-gate 		rp->r_r10 = grp[REG_R10];
7207c478bd9Sstevel@tonic-gate 		rp->r_r11 = grp[REG_R11];
7217c478bd9Sstevel@tonic-gate 		rp->r_r12 = grp[REG_R12];
7227c478bd9Sstevel@tonic-gate 		rp->r_r13 = grp[REG_R13];
7237c478bd9Sstevel@tonic-gate 		rp->r_r14 = grp[REG_R14];
7247c478bd9Sstevel@tonic-gate 		rp->r_r15 = grp[REG_R15];
7257c478bd9Sstevel@tonic-gate 		rp->r_trapno = grp[REG_TRAPNO];
7267c478bd9Sstevel@tonic-gate 		rp->r_err = grp[REG_ERR];
7277c478bd9Sstevel@tonic-gate 		rp->r_rip = grp[REG_RIP];
7287c478bd9Sstevel@tonic-gate 		/*
7297c478bd9Sstevel@tonic-gate 		 * Setting %cs or %ss to anything else is quietly but
7307c478bd9Sstevel@tonic-gate 		 * quite definitely forbidden!
7317c478bd9Sstevel@tonic-gate 		 */
7327c478bd9Sstevel@tonic-gate 		rp->r_cs = UCS_SEL;
7337c478bd9Sstevel@tonic-gate 		rp->r_ss = UDS_SEL;
7347c478bd9Sstevel@tonic-gate 		rp->r_rsp = grp[REG_RSP];
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 		if (thisthread)
7377c478bd9Sstevel@tonic-gate 			kpreempt_disable();
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 		pcb->pcb_ds = UDS_SEL;
7407c478bd9Sstevel@tonic-gate 		pcb->pcb_es = UDS_SEL;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		/*
7437c478bd9Sstevel@tonic-gate 		 * 64-bit processes -are- allowed to set their fsbase/gsbase
7447c478bd9Sstevel@tonic-gate 		 * values directly, but only if they're using the segment
7457c478bd9Sstevel@tonic-gate 		 * selectors that allow that semantic.
7467c478bd9Sstevel@tonic-gate 		 *
7477c478bd9Sstevel@tonic-gate 		 * (32-bit processes must use lwp_set_private().)
7487c478bd9Sstevel@tonic-gate 		 */
7497c478bd9Sstevel@tonic-gate 		pcb->pcb_fsbase = grp[REG_FSBASE];
7507c478bd9Sstevel@tonic-gate 		pcb->pcb_gsbase = grp[REG_GSBASE];
751843e1988Sjohnlev 		pcb->pcb_fs = fix_segreg(grp[REG_FS], IS_NOT_CS, datamodel);
752843e1988Sjohnlev 		pcb->pcb_gs = fix_segreg(grp[REG_GS], IS_NOT_CS, datamodel);
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 		/*
7557c478bd9Sstevel@tonic-gate 		 * Ensure that we go out via update_sregs
7567c478bd9Sstevel@tonic-gate 		 */
7574c28a617SRobert Mustacchi 		PCB_SET_UPDATE_SEGS(pcb);
7587c478bd9Sstevel@tonic-gate 		lwptot(lwp)->t_post_sys = 1;
7597c478bd9Sstevel@tonic-gate 		if (thisthread)
7607c478bd9Sstevel@tonic-gate 			kpreempt_enable();
7617c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
7627c478bd9Sstevel@tonic-gate 	} else {
7637c478bd9Sstevel@tonic-gate 		rp->r_rdi = (uint32_t)grp[REG_RDI];
7647c478bd9Sstevel@tonic-gate 		rp->r_rsi = (uint32_t)grp[REG_RSI];
7657c478bd9Sstevel@tonic-gate 		rp->r_rdx = (uint32_t)grp[REG_RDX];
7667c478bd9Sstevel@tonic-gate 		rp->r_rcx = (uint32_t)grp[REG_RCX];
7677c478bd9Sstevel@tonic-gate 		rp->r_rax = (uint32_t)grp[REG_RAX];
7687c478bd9Sstevel@tonic-gate 		rp->r_rbx = (uint32_t)grp[REG_RBX];
7697c478bd9Sstevel@tonic-gate 		rp->r_rbp = (uint32_t)grp[REG_RBP];
7707c478bd9Sstevel@tonic-gate 		rp->r_trapno = (uint32_t)grp[REG_TRAPNO];
7717c478bd9Sstevel@tonic-gate 		rp->r_err = (uint32_t)grp[REG_ERR];
7727c478bd9Sstevel@tonic-gate 		rp->r_rip = (uint32_t)grp[REG_RIP];
7737c478bd9Sstevel@tonic-gate 
774843e1988Sjohnlev 		rp->r_cs = fix_segreg(grp[REG_CS], IS_CS, datamodel);
775843e1988Sjohnlev 		rp->r_ss = fix_segreg(grp[REG_DS], IS_NOT_CS, datamodel);
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		rp->r_rsp = (uint32_t)grp[REG_RSP];
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 		if (thisthread)
7807c478bd9Sstevel@tonic-gate 			kpreempt_disable();
7817c478bd9Sstevel@tonic-gate 
782843e1988Sjohnlev 		pcb->pcb_ds = fix_segreg(grp[REG_DS], IS_NOT_CS, datamodel);
783843e1988Sjohnlev 		pcb->pcb_es = fix_segreg(grp[REG_ES], IS_NOT_CS, datamodel);
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate 		/*
7867c478bd9Sstevel@tonic-gate 		 * (See fsbase/gsbase commentary above)
7877c478bd9Sstevel@tonic-gate 		 */
788843e1988Sjohnlev 		pcb->pcb_fs = fix_segreg(grp[REG_FS], IS_NOT_CS, datamodel);
789843e1988Sjohnlev 		pcb->pcb_gs = fix_segreg(grp[REG_GS], IS_NOT_CS, datamodel);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		/*
7927c478bd9Sstevel@tonic-gate 		 * Ensure that we go out via update_sregs
7937c478bd9Sstevel@tonic-gate 		 */
7944c28a617SRobert Mustacchi 		PCB_SET_UPDATE_SEGS(pcb);
7957c478bd9Sstevel@tonic-gate 		lwptot(lwp)->t_post_sys = 1;
7967c478bd9Sstevel@tonic-gate 		if (thisthread)
7977c478bd9Sstevel@tonic-gate 			kpreempt_enable();
7987c478bd9Sstevel@tonic-gate #endif
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	/*
8027c478bd9Sstevel@tonic-gate 	 * Only certain bits of the flags register can be modified.
8037c478bd9Sstevel@tonic-gate 	 */
8047c478bd9Sstevel@tonic-gate 	rp->r_rfl = (rp->r_rfl & ~PSL_USERMASK) |
8057c478bd9Sstevel@tonic-gate 	    (grp[REG_RFL] & PSL_USERMASK);
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate #elif defined(__i386)
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	/*
8107c478bd9Sstevel@tonic-gate 	 * Only certain bits of the flags register can be modified.
8117c478bd9Sstevel@tonic-gate 	 */
8127c478bd9Sstevel@tonic-gate 	grp[EFL] = (rp->r_efl & ~PSL_USERMASK) | (grp[EFL] & PSL_USERMASK);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
8157c478bd9Sstevel@tonic-gate 	 * Copy saved registers from user stack.
8167c478bd9Sstevel@tonic-gate 	 */
8177c478bd9Sstevel@tonic-gate 	bcopy(grp, &rp->r_gs, sizeof (gregset_t));
8187c478bd9Sstevel@tonic-gate 
819843e1988Sjohnlev 	rp->r_cs = fix_segreg(rp->r_cs, IS_CS, datamodel);
820843e1988Sjohnlev 	rp->r_ss = fix_segreg(rp->r_ss, IS_NOT_CS, datamodel);
821843e1988Sjohnlev 	rp->r_ds = fix_segreg(rp->r_ds, IS_NOT_CS, datamodel);
822843e1988Sjohnlev 	rp->r_es = fix_segreg(rp->r_es, IS_NOT_CS, datamodel);
823843e1988Sjohnlev 	rp->r_fs = fix_segreg(rp->r_fs, IS_NOT_CS, datamodel);
824843e1988Sjohnlev 	rp->r_gs = fix_segreg(rp->r_gs, IS_NOT_CS, datamodel);
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate #endif	/* __i386 */
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate /*
8307c478bd9Sstevel@tonic-gate  * Determine whether eip is likely to have an interrupt frame
8317c478bd9Sstevel@tonic-gate  * on the stack.  We do this by comparing the address to the
8327c478bd9Sstevel@tonic-gate  * range of addresses spanned by several well-known routines.
8337c478bd9Sstevel@tonic-gate  */
8347c478bd9Sstevel@tonic-gate extern void _interrupt();
8357c478bd9Sstevel@tonic-gate extern void _allsyscalls();
8367c478bd9Sstevel@tonic-gate extern void _cmntrap();
8377c478bd9Sstevel@tonic-gate extern void fakesoftint();
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate extern size_t _interrupt_size;
8407c478bd9Sstevel@tonic-gate extern size_t _allsyscalls_size;
8417c478bd9Sstevel@tonic-gate extern size_t _cmntrap_size;
8427c478bd9Sstevel@tonic-gate extern size_t _fakesoftint_size;
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate /*
8457c478bd9Sstevel@tonic-gate  * Get a pc-only stacktrace.  Used for kmem_alloc() buffer ownership tracking.
8467c478bd9Sstevel@tonic-gate  * Returns MIN(current stack depth, pcstack_limit).
8477c478bd9Sstevel@tonic-gate  */
8487c478bd9Sstevel@tonic-gate int
getpcstack(pc_t * pcstack,int pcstack_limit)8497c478bd9Sstevel@tonic-gate getpcstack(pc_t *pcstack, int pcstack_limit)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	struct frame *fp = (struct frame *)getfp();
8527c478bd9Sstevel@tonic-gate 	struct frame *nextfp, *minfp, *stacktop;
8537c478bd9Sstevel@tonic-gate 	int depth = 0;
8547c478bd9Sstevel@tonic-gate 	int on_intr;
8557c478bd9Sstevel@tonic-gate 	uintptr_t pc;
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
8587c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
8597c478bd9Sstevel@tonic-gate 	else
8607c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
8617c478bd9Sstevel@tonic-gate 	minfp = fp;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	pc = ((struct regs *)fp)->r_pc;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	while (depth < pcstack_limit) {
8667c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)fp->fr_savfp;
8677c478bd9Sstevel@tonic-gate 		pc = fp->fr_savpc;
8687c478bd9Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
8697c478bd9Sstevel@tonic-gate 			if (on_intr) {
8707c478bd9Sstevel@tonic-gate 				/*
8717c478bd9Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
8727c478bd9Sstevel@tonic-gate 				 */
8737c478bd9Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
8747c478bd9Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
8757c478bd9Sstevel@tonic-gate 				on_intr = 0;
8767c478bd9Sstevel@tonic-gate 				continue;
8777c478bd9Sstevel@tonic-gate 			}
8787c478bd9Sstevel@tonic-gate 			break;
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 		pcstack[depth++] = (pc_t)pc;
8817c478bd9Sstevel@tonic-gate 		fp = nextfp;
8827c478bd9Sstevel@tonic-gate 		minfp = fp;
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 	return (depth);
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate /*
8887c478bd9Sstevel@tonic-gate  * The following ELF header fields are defined as processor-specific
8897c478bd9Sstevel@tonic-gate  * in the V8 ABI:
8907c478bd9Sstevel@tonic-gate  *
8917c478bd9Sstevel@tonic-gate  *	e_ident[EI_DATA]	encoding of the processor-specific
8927c478bd9Sstevel@tonic-gate  *				data in the object file
8937c478bd9Sstevel@tonic-gate  *	e_machine		processor identification
8947c478bd9Sstevel@tonic-gate  *	e_flags			processor-specific flags associated
8957c478bd9Sstevel@tonic-gate  *				with the file
8967c478bd9Sstevel@tonic-gate  */
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate /*
8997c478bd9Sstevel@tonic-gate  * The value of at_flags reflects a platform's cpu module support.
9007c478bd9Sstevel@tonic-gate  * at_flags is used to check for allowing a binary to execute and
9017c478bd9Sstevel@tonic-gate  * is passed as the value of the AT_FLAGS auxiliary vector.
9027c478bd9Sstevel@tonic-gate  */
9037c478bd9Sstevel@tonic-gate int at_flags = 0;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate /*
9067c478bd9Sstevel@tonic-gate  * Check the processor-specific fields of an ELF header.
9077c478bd9Sstevel@tonic-gate  *
9087c478bd9Sstevel@tonic-gate  * returns 1 if the fields are valid, 0 otherwise
9097c478bd9Sstevel@tonic-gate  */
9107c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
9117c478bd9Sstevel@tonic-gate int
elfheadcheck(unsigned char e_data,Elf32_Half e_machine,Elf32_Word e_flags)9127c478bd9Sstevel@tonic-gate elfheadcheck(
9137c478bd9Sstevel@tonic-gate 	unsigned char e_data,
9147c478bd9Sstevel@tonic-gate 	Elf32_Half e_machine,
9157c478bd9Sstevel@tonic-gate 	Elf32_Word e_flags)
9167c478bd9Sstevel@tonic-gate {
9177c478bd9Sstevel@tonic-gate 	if (e_data != ELFDATA2LSB)
9187c478bd9Sstevel@tonic-gate 		return (0);
9197c478bd9Sstevel@tonic-gate #if defined(__amd64)
9207c478bd9Sstevel@tonic-gate 	if (e_machine == EM_AMD64)
9217c478bd9Sstevel@tonic-gate 		return (1);
9227c478bd9Sstevel@tonic-gate #endif
9237c478bd9Sstevel@tonic-gate 	return (e_machine == EM_386);
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate uint_t auxv_hwcap_include = 0;	/* patch to enable unrecognized features */
927ebb8ac07SRobert Mustacchi uint_t auxv_hwcap_include_2 = 0;	/* second word */
9287c478bd9Sstevel@tonic-gate uint_t auxv_hwcap_exclude = 0;	/* patch for broken cpus, debugging */
929ebb8ac07SRobert Mustacchi uint_t auxv_hwcap_exclude_2 = 0;	/* second word */
9307c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
9317c478bd9Sstevel@tonic-gate uint_t auxv_hwcap32_include = 0;	/* ditto for 32-bit apps */
932ebb8ac07SRobert Mustacchi uint_t auxv_hwcap32_include_2 = 0;	/* ditto for 32-bit apps */
9337c478bd9Sstevel@tonic-gate uint_t auxv_hwcap32_exclude = 0;	/* ditto for 32-bit apps */
934ebb8ac07SRobert Mustacchi uint_t auxv_hwcap32_exclude_2 = 0;	/* ditto for 32-bit apps */
9357c478bd9Sstevel@tonic-gate #endif
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*
9387c478bd9Sstevel@tonic-gate  * Gather information about the processor and place it into auxv_hwcap
9397c478bd9Sstevel@tonic-gate  * so that it can be exported to the linker via the aux vector.
9407c478bd9Sstevel@tonic-gate  *
9417c478bd9Sstevel@tonic-gate  * We use this seemingly complicated mechanism so that we can ensure
9427c478bd9Sstevel@tonic-gate  * that /etc/system can be used to override what the system can or
943