xref: /illumos-gate/usr/src/uts/intel/os/archdep.c (revision ed093b41)
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	*/
26cff040f3SRobert Mustacchi /*	  All Rights Reserved	*/
27ebb8ac07SRobert Mustacchi /*
28a32a1f37SRobert Mustacchi  * Copyright (c) 2018, Joyent, Inc.
29ff512403SGarrett D'Amore  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
30*ed093b41SRobert Mustacchi  * Copyright 2023 Oxide Computer Company
31ebb8ac07SRobert Mustacchi  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/param.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
367c478bd9Sstevel@tonic-gate #include <sys/systm.h>
377c478bd9Sstevel@tonic-gate #include <sys/signal.h>
387c478bd9Sstevel@tonic-gate #include <sys/stack.h>
397c478bd9Sstevel@tonic-gate #include <sys/regset.h>
407c478bd9Sstevel@tonic-gate #include <sys/privregs.h>
417c478bd9Sstevel@tonic-gate #include <sys/frame.h>
427c478bd9Sstevel@tonic-gate #include <sys/proc.h>
437c478bd9Sstevel@tonic-gate #include <sys/psw.h>
447c478bd9Sstevel@tonic-gate #include <sys/siginfo.h>
457c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
467c478bd9Sstevel@tonic-gate #include <sys/asm_linkage.h>
477c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
487c478bd9Sstevel@tonic-gate #include <sys/errno.h>
497c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
507c478bd9Sstevel@tonic-gate #include <sys/archsystm.h>
517c478bd9Sstevel@tonic-gate #include <sys/debug.h>
527c478bd9Sstevel@tonic-gate #include <sys/elf.h>
537c478bd9Sstevel@tonic-gate #include <sys/spl.h>
547c478bd9Sstevel@tonic-gate #include <sys/time.h>
557c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
567c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
577c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
587c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
597c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
607c478bd9Sstevel@tonic-gate #include <sys/panic.h>
617c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
627c478bd9Sstevel@tonic-gate #include <sys/time.h>
637c478bd9Sstevel@tonic-gate #include <sys/fp.h>
647c478bd9Sstevel@tonic-gate #include <sys/x86_archext.h>
657c478bd9Sstevel@tonic-gate #include <sys/auxv.h>
667c478bd9Sstevel@tonic-gate #include <sys/auxv_386.h>
677c478bd9Sstevel@tonic-gate #include <sys/dtrace.h>
689acbbeafSnn #include <sys/brand.h>
699acbbeafSnn #include <sys/machbrand.h>
707af88ac7SKuriakose Kuruvilla #include <sys/cmn_err.h>
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * Map an fnsave-formatted save area into an fxsave-formatted save area.
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  * Most fields are the same width, content and semantics.  However
767c478bd9Sstevel@tonic-gate  * the tag word is compressed.
777c478bd9Sstevel@tonic-gate  */
787c478bd9Sstevel@tonic-gate static void
fnsave_to_fxsave(const struct fnsave_state * fn,struct fxsave_state * fx)797c478bd9Sstevel@tonic-gate fnsave_to_fxsave(const struct fnsave_state *fn, struct fxsave_state *fx)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	uint_t i, tagbits;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	fx->fx_fcw = fn->f_fcw;
847c478bd9Sstevel@tonic-gate 	fx->fx_fsw = fn->f_fsw;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * copy element by element (because of holes)
887c478bd9Sstevel@tonic-gate 	 */
897c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++)
907c478bd9Sstevel@tonic-gate 		bcopy(&fn->f_st[i].fpr_16[0], &fx->fx_st[i].fpr_16[0],
917c478bd9Sstevel@tonic-gate 		    sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	/*
947c478bd9Sstevel@tonic-gate 	 * synthesize compressed tag bits
957c478bd9Sstevel@tonic-gate 	 */
967c478bd9Sstevel@tonic-gate 	fx->fx_fctw = 0;
977c478bd9Sstevel@tonic-gate 	for (tagbits = fn->f_ftw, i = 0; i < 8; i++, tagbits >>= 2)
987c478bd9Sstevel@tonic-gate 		if ((tagbits & 3) != 3)
997c478bd9Sstevel@tonic-gate 			fx->fx_fctw |= (1 << i);
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	fx->fx_fop = fn->f_fop;
1027c478bd9Sstevel@tonic-gate 
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 }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Map from an fxsave-format save area to an fnsave-format save area.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate static void
fxsave_to_fnsave(const struct fxsave_state * fx,struct fnsave_state * fn)1117c478bd9Sstevel@tonic-gate fxsave_to_fnsave(const struct fxsave_state *fx, struct fnsave_state *fn)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	uint_t i, top, tagbits;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	fn->f_fcw = fx->fx_fcw;
1167c478bd9Sstevel@tonic-gate 	fn->__f_ign0 = 0;
1177c478bd9Sstevel@tonic-gate 	fn->f_fsw = fx->fx_fsw;
1187c478bd9Sstevel@tonic-gate 	fn->__f_ign1 = 0;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	top = (fx->fx_fsw & FPS_TOP) >> 11;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	/*
1237c478bd9Sstevel@tonic-gate 	 * copy element by element (because of holes)
1247c478bd9Sstevel@tonic-gate 	 */
1257c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++)
1267c478bd9Sstevel@tonic-gate 		bcopy(&fx->fx_st[i].fpr_16[0], &fn->f_st[i].fpr_16[0],
1277c478bd9Sstevel@tonic-gate 		    sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 	/*
1307c478bd9Sstevel@tonic-gate 	 * synthesize uncompressed tag bits
1317c478bd9Sstevel@tonic-gate 	 */
1327c478bd9Sstevel@tonic-gate 	fn->f_ftw = 0;
1337c478bd9Sstevel@tonic-gate 	for (tagbits = fx->fx_fctw, i = 0; i < 8; i++, tagbits >>= 1) {
1347c478bd9Sstevel@tonic-gate 		uint_t ibit, expo;
1357c478bd9Sstevel@tonic-gate 		const uint16_t *fpp;
1367c478bd9Sstevel@tonic-gate 		static const uint16_t zero[5] = { 0, 0, 0, 0, 0 };
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 		if ((tagbits & 1) == 0) {
1397c478bd9Sstevel@tonic-gate 			fn->f_ftw |= 3 << (i << 1);	/* empty */
1407c478bd9Sstevel@tonic-gate 			continue;
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		/*
1447c478bd9Sstevel@tonic-gate 		 * (tags refer to *physical* registers)
1457c478bd9Sstevel@tonic-gate 		 */
1467c478bd9Sstevel@tonic-gate 		fpp = &fx->fx_st[(i - top + 8) & 7].fpr_16[0];
1477c478bd9Sstevel@tonic-gate 		ibit = fpp[3] >> 15;
1487c478bd9Sstevel@tonic-gate 		expo = fpp[4] & 0x7fff;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		if (ibit && expo != 0 && expo != 0x7fff)
1517c478bd9Sstevel@tonic-gate 			continue;			/* valid fp number */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		if (bcmp(fpp, &zero, sizeof (zero)))
1547c478bd9Sstevel@tonic-gate 			fn->f_ftw |= 2 << (i << 1);	/* NaN */
1557c478bd9Sstevel@tonic-gate 		else
1567c478bd9Sstevel@tonic-gate 			fn->f_ftw |= 1 << (i << 1);	/* fp zero */
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	fn->f_fop = fx->fx_fop;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	fn->__f_ign2 = 0;
1627c478bd9Sstevel@tonic-gate 	fn->f_eip = (uint32_t)fx->fx_rip;
1637c478bd9Sstevel@tonic-gate 	fn->f_cs = U32CS_SEL;
1647c478bd9Sstevel@tonic-gate 	fn->f_dp = (uint32_t)fx->fx_rdp;
1657c478bd9Sstevel@tonic-gate 	fn->f_ds = UDS_SEL;
1667c478bd9Sstevel@tonic-gate 	fn->__f_ign3 = 0;
1677c478bd9Sstevel@tonic-gate }
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Map from an fpregset_t into an fxsave-format save area
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate static void
fpregset_to_fxsave(const fpregset_t * fp,struct fxsave_state * fx)1737c478bd9Sstevel@tonic-gate fpregset_to_fxsave(const fpregset_t *fp, struct fxsave_state *fx)
1747c478bd9Sstevel@tonic-gate {
1757c478bd9Sstevel@tonic-gate 	bcopy(fp, fx, sizeof (*fx));
1767c478bd9Sstevel@tonic-gate 	/*
1777c478bd9Sstevel@tonic-gate 	 * avoid useless #gp exceptions - mask reserved bits
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	fx->fx_mxcsr &= sse_mxcsr_mask;
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * Map from an fxsave-format save area into a fpregset_t
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate static void
fxsave_to_fpregset(const struct fxsave_state * fx,fpregset_t * fp)1867c478bd9Sstevel@tonic-gate fxsave_to_fpregset(const struct fxsave_state *fx, fpregset_t *fp)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	bcopy(fx, fp, sizeof (*fx));
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
1927c478bd9Sstevel@tonic-gate static void
fpregset32_to_fxsave(const fpregset32_t * fp,struct fxsave_state * fx)1937c478bd9Sstevel@tonic-gate fpregset32_to_fxsave(const fpregset32_t *fp, struct fxsave_state *fx)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	const struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	fnsave_to_fxsave((const struct fnsave_state *)fc, fx);
1987c478bd9Sstevel@tonic-gate 	/*
1997c478bd9Sstevel@tonic-gate 	 * avoid useless #gp exceptions - mask reserved bits
2007c478bd9Sstevel@tonic-gate 	 */
2017c478bd9Sstevel@tonic-gate 	fx->fx_mxcsr = sse_mxcsr_mask & fc->mxcsr;
2027c478bd9Sstevel@tonic-gate 	bcopy(&fc->xmm[0], &fx->fx_xmm[0], sizeof (fc->xmm));
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate static void
fxsave_to_fpregset32(const struct fxsave_state * fx,fpregset32_t * fp)2067c478bd9Sstevel@tonic-gate fxsave_to_fpregset32(const struct fxsave_state *fx, fpregset32_t *fp)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	fxsave_to_fnsave(fx, (struct fnsave_state *)fc);
2117c478bd9Sstevel@tonic-gate 	fc->mxcsr = fx->fx_mxcsr;
2127c478bd9Sstevel@tonic-gate 	bcopy(&fx->fx_xmm[0], &fc->xmm[0], sizeof (fc->xmm));
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate static void
fpregset_nto32(const fpregset_t * src,fpregset32_t * dst)2167c478bd9Sstevel@tonic-gate fpregset_nto32(const fpregset_t *src, fpregset32_t *dst)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	fxsave_to_fpregset32((struct fxsave_state *)src, dst);
2197c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.status =
2207c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.status;
2217c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.xstatus =
2227c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.xstatus;
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate static void
fpregset_32ton(const fpregset32_t * src,fpregset_t * dst)2267c478bd9Sstevel@tonic-gate fpregset_32ton(const fpregset32_t *src, fpregset_t *dst)
2277c478bd9Sstevel@tonic-gate {
2287c478bd9Sstevel@tonic-gate 	fpregset32_to_fxsave(src, (struct fxsave_state *)dst);
2297c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.status =
2307c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.status;
2317c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.xstatus =
2327c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.xstatus;
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate #endif
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate /*
2377c478bd9Sstevel@tonic-gate  * Set floating-point registers from a native fpregset_t.
2387c478bd9Sstevel@tonic-gate  */
2397c478bd9Sstevel@tonic-gate void
setfpregs(klwp_t * lwp,fpregset_t * fp)2407c478bd9Sstevel@tonic-gate setfpregs(klwp_t *lwp, fpregset_t *fp)
2417c478bd9Sstevel@tonic-gate {
242*ed093b41SRobert Mustacchi 	fpu_set_fpregset(lwp, fp);
2437c478bd9Sstevel@tonic-gate }
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate /*
2467c478bd9Sstevel@tonic-gate  * Get floating-point registers into a native fpregset_t.
2477c478bd9Sstevel@tonic-gate  */
2487c478bd9Sstevel@tonic-gate void
getfpregs(klwp_t * lwp,fpregset_t * fp)2497c478bd9Sstevel@tonic-gate getfpregs(klwp_t *lwp, fpregset_t *fp)
2507c478bd9Sstevel@tonic-gate {
251*ed093b41SRobert Mustacchi 	bzero(fp, sizeof (*fp));
252*ed093b41SRobert Mustacchi 	fpu_get_fpregset(lwp, fp);
2537c478bd9Sstevel@tonic-gate }
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate /*
2587c478bd9Sstevel@tonic-gate  * Set floating-point registers from an fpregset32_t.
2597c478bd9Sstevel@tonic-gate  */
2607c478bd9Sstevel@tonic-gate void
setfpregs32(klwp_t * lwp,fpregset32_t * fp)2617c478bd9Sstevel@tonic-gate setfpregs32(klwp_t *lwp, fpregset32_t *fp)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	fpregset_t fpregs;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	fpregset_32ton(fp, &fpregs);
2667c478bd9Sstevel@tonic-gate 	setfpregs(lwp, &fpregs);
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  * Get floating-point registers into an fpregset32_t.
2717c478bd9Sstevel@tonic-gate  */
2727c478bd9Sstevel@tonic-gate void
getfpregs32(klwp_t * lwp,fpregset32_t * fp)2737c478bd9Sstevel@tonic-gate getfpregs32(klwp_t *lwp, fpregset32_t *fp)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	fpregset_t fpregs;
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	getfpregs(lwp, &fpregs);
2787c478bd9Sstevel@tonic-gate 	fpregset_nto32(&fpregs, fp);
2797c478bd9Sstevel@tonic-gate }
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * Return the general registers
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate void
getgregs(klwp_t * lwp,gregset_t grp)2877c478bd9Sstevel@tonic-gate getgregs(klwp_t *lwp, gregset_t grp)
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
2907c478bd9Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
2917c478bd9Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	grp[REG_RDI] = rp->r_rdi;
2947c478bd9Sstevel@tonic-gate 	grp[REG_RSI] = rp->r_rsi;
2957c478bd9Sstevel@tonic-gate 	grp[REG_RDX] = rp->r_rdx;
2967c478bd9Sstevel@tonic-gate 	grp[REG_RCX] = rp->r_rcx;
2977c478bd9Sstevel@tonic-gate 	grp[REG_R8] = rp->r_r8;
2987c478bd9Sstevel@tonic-gate 	grp[REG_R9] = rp->r_r9;
2997c478bd9Sstevel@tonic-gate 	grp[REG_RAX] = rp->r_rax;
3007c478bd9Sstevel@tonic-gate 	grp[REG_RBX] = rp->r_rbx;
3017c478bd9Sstevel@tonic-gate 	grp[REG_RBP] = rp->r_rbp;
3027c478bd9Sstevel@tonic-gate 	grp[REG_R10] = rp->r_r10;
3037c478bd9Sstevel@tonic-gate 	grp[REG_R11] = rp->r_r11;
3047c478bd9Sstevel@tonic-gate 	grp[REG_R12] = rp->r_r12;
3057c478bd9Sstevel@tonic-gate 	grp[REG_R13] = rp->r_r13;
3067c478bd9Sstevel@tonic-gate 	grp[REG_R14] = rp->r_r14;
3077c478bd9Sstevel@tonic-gate 	grp[REG_R15] = rp->r_r15;
3087c478bd9Sstevel@tonic-gate 	grp[REG_FSBASE] = pcb->pcb_fsbase;
3097c478bd9Sstevel@tonic-gate 	grp[REG_GSBASE] = pcb->pcb_gsbase;
3107c478bd9Sstevel@tonic-gate 	if (thisthread)
3117c478bd9Sstevel@tonic-gate 		kpreempt_disable();
3124c28a617SRobert Mustacchi 	if (PCB_NEED_UPDATE_SEGS(pcb)) {
3137c478bd9Sstevel@tonic-gate 		grp[REG_DS] = pcb->pcb_ds;
3147c478bd9Sstevel@tonic-gate 		grp[REG_ES] = pcb->pcb_es;
3157c478bd9Sstevel@tonic-gate 		grp[REG_FS] = pcb->pcb_fs;
3167c478bd9Sstevel@tonic-gate 		grp[REG_GS] = pcb->pcb_gs;
3177c478bd9Sstevel@tonic-gate 	} else {
3187c478bd9Sstevel@tonic-gate 		grp[REG_DS] = rp->r_ds;
3197c478bd9Sstevel@tonic-gate 		grp[REG_ES] = rp->r_es;
3207c478bd9Sstevel@tonic-gate 		grp[REG_FS] = rp->r_fs;
3217c478bd9Sstevel@tonic-gate 		grp[REG_GS] = rp->r_gs;
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 	if (thisthread)
3247c478bd9Sstevel@tonic-gate 		kpreempt_enable();
3257c478bd9Sstevel@tonic-gate 	grp[REG_TRAPNO] = rp->r_trapno;
3267c478bd9Sstevel@tonic-gate 	grp[REG_ERR] = rp->r_err;
3277c478bd9Sstevel@tonic-gate 	grp[REG_RIP] = rp->r_rip;
3287c478bd9Sstevel@tonic-gate 	grp[REG_CS] = rp->r_cs;
3297c478bd9Sstevel@tonic-gate 	grp[REG_SS] = rp->r_ss;
3307c478bd9Sstevel@tonic-gate 	grp[REG_RFL] = rp->r_rfl;
3317c478bd9Sstevel@tonic-gate 	grp[REG_RSP] = rp->r_rsp;
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate void
getgregs32(klwp_t * lwp,gregset32_t grp)3377c478bd9Sstevel@tonic-gate getgregs32(klwp_t *lwp, gregset32_t grp)
3387c478bd9Sstevel@tonic-gate {
3397c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
3407c478bd9Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
3417c478bd9Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if (thisthread)
3447c478bd9Sstevel@tonic-gate 		kpreempt_disable();
3454c28a617SRobert Mustacchi 	if (PCB_NEED_UPDATE_SEGS(pcb)) {
3467c478bd9Sstevel@tonic-gate 		grp[GS] = (uint16_t)pcb->pcb_gs;
3477c478bd9Sstevel@tonic-gate 		grp[FS] = (uint16_t)pcb->pcb_fs;
3487c478bd9Sstevel@tonic-gate 		grp[DS] = (uint16_t)pcb->pcb_ds;
3497c478bd9Sstevel@tonic-gate 		grp[ES] = (uint16_t)pcb->pcb_es;
3507c478bd9Sstevel@tonic-gate 	} else {
3517c478bd9Sstevel@tonic-gate 		grp[GS] = (uint16_t)rp->r_gs;
3527c478bd9Sstevel@tonic-gate 		grp[FS] = (uint16_t)rp->r_fs;
3537c478bd9Sstevel@tonic-gate 		grp[DS] = (uint16_t)rp->r_ds;
3547c478bd9Sstevel@tonic-gate 		grp[ES] = (uint16_t)rp->r_es;
3557c478bd9Sstevel@tonic-gate 	}
3567c478bd9Sstevel@tonic-gate 	if (thisthread)
3577c478bd9Sstevel@tonic-gate 		kpreempt_enable();
3587c478bd9Sstevel@tonic-gate 	grp[EDI] = (greg32_t)rp->r_rdi;
3597c478bd9Sstevel@tonic-gate 	grp[ESI] = (greg32_t)rp->r_rsi;
3607c478bd9Sstevel@tonic-gate 	grp[EBP] = (greg32_t)rp->r_rbp;
3617c478bd9Sstevel@tonic-gate 	grp[ESP] = 0;
3627c478bd9Sstevel@tonic-gate 	grp[EBX] = (greg32_t)rp->r_rbx;
3637c478bd9Sstevel@tonic-gate 	grp[EDX] = (greg32_t)rp->r_rdx;
3647c478bd9Sstevel@tonic-gate 	grp[ECX] = (greg32_t)rp->r_rcx;
3657c478bd9Sstevel@tonic-gate 	grp[EAX] = (greg32_t)rp->r_rax;
3667c478bd9Sstevel@tonic-gate 	grp[TRAPNO] = (greg32_t)rp->r_trapno;
3677c478bd9Sstevel@tonic-gate 	grp[ERR] = (greg32_t)rp->r_err;
3687c478bd9Sstevel@tonic-gate 	grp[EIP] = (greg32_t)rp->r_rip;
3697c478bd9Sstevel@tonic-gate 	grp[CS] = (uint16_t)rp->r_cs;
3707c478bd9Sstevel@tonic-gate 	grp[EFL] = (greg32_t)rp->r_rfl;
3717c478bd9Sstevel@tonic-gate 	grp[UESP] = (greg32_t)rp->r_rsp;
3727c478bd9Sstevel@tonic-gate 	grp[SS] = (uint16_t)rp->r_ss;
3737c478bd9Sstevel@tonic-gate }
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate void
ucontext_32ton(const ucontext32_t * src,ucontext_t * dst)3767c478bd9Sstevel@tonic-gate ucontext_32ton(const ucontext32_t *src, ucontext_t *dst)
3777c478bd9Sstevel@tonic-gate {
3787c478bd9Sstevel@tonic-gate 	mcontext_t *dmc = &dst->uc_mcontext;
3797c478bd9Sstevel@tonic-gate 	const mcontext32_t *smc = &src->uc_mcontext;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	bzero(dst, sizeof (*dst));
3827c478bd9Sstevel@tonic-gate 	dst->uc_flags = src->uc_flags;
3837c478bd9Sstevel@tonic-gate 	dst->uc_link = (ucontext_t *)(uintptr_t)src->uc_link;
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	bcopy(&src->uc_sigmask, &dst->uc_sigmask, sizeof (dst->uc_sigmask));
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate 	dst->uc_stack.ss_sp = (void *)(uintptr_t)src->uc_stack.ss_sp;
3887c478bd9Sstevel@tonic-gate 	dst->uc_stack.ss_size = (size_t)src->uc_stack.ss_size;
3897c478bd9Sstevel@tonic-gate 	dst->uc_stack.ss_flags = src->uc_stack.ss_flags;
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_GS] = (greg_t)(uint32_t)smc->gregs[GS];
3927c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_FS] = (greg_t)(uint32_t)smc->gregs[FS];
3937c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_ES] = (greg_t)(uint32_t)smc->gregs[ES];
3947c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_DS] = (greg_t)(uint32_t)smc->gregs[DS];
3957c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RDI] = (greg_t)(uint32_t)smc->gregs[EDI];
3967c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RSI] = (greg_t)(uint32_t)smc->gregs[ESI];
3977c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RBP] = (greg_t)(uint32_t)smc->gregs[EBP];
3987c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RBX] = (greg_t)(uint32_t)smc->gregs[EBX];
3997c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RDX] = (greg_t)(uint32_t)smc->gregs[EDX];
4007c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RCX] = (greg_t)(uint32_t)smc->gregs[ECX];
4017c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RAX] = (greg_t)(uint32_t)smc->gregs[EAX];
4027c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_TRAPNO] = (greg_t)(uint32_t)smc->gregs[TRAPNO];
4037c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_ERR] = (greg_t)(uint32_t)smc->gregs[ERR];
4047c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RIP] = (greg_t)(uint32_t)smc->gregs[EIP];
4057c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_CS] = (greg_t)(uint32_t)smc->gregs[CS];
4067c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RFL] = (greg_t)(uint32_t)smc->gregs[EFL];
4077c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RSP] = (greg_t)(uint32_t)smc->gregs[UESP];
4087c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_SS] = (greg_t)(uint32_t)smc->gregs[SS];
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/*
4117c478bd9Sstevel@tonic-gate 	 * A valid fpregs is only copied in if uc.uc_flags has UC_FPU set
4127c478bd9Sstevel@tonic-gate 	 * otherwise there is no guarantee that anything in fpregs is valid.
4137c478bd9Sstevel@tonic-gate 	 */
4147c478bd9Sstevel@tonic-gate 	if (src->uc_flags & UC_FPU)
4157c478bd9Sstevel@tonic-gate 		fpregset_32ton(&src->uc_mcontext.fpregs,
4167c478bd9Sstevel@tonic-gate 		    &dst->uc_mcontext.fpregs);
417*ed093b41SRobert Mustacchi 
418*ed093b41SRobert Mustacchi 	if (src->uc_flags & UC_XSAVE) {
419*ed093b41SRobert Mustacchi 		dst->uc_xsave = (long)(uint32_t)src->uc_xsave;
420*ed093b41SRobert Mustacchi 	} else {
421*ed093b41SRobert Mustacchi 		dst->uc_xsave = 0;
422*ed093b41SRobert Mustacchi 	}
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate /*
4287c478bd9Sstevel@tonic-gate  * Return the user-level PC.
4297c478bd9Sstevel@tonic-gate  * If in a system call, return the address of the syscall trap.
4307c478bd9Sstevel@tonic-gate  */
4317c478bd9Sstevel@tonic-gate greg_t
getuserpc(void)432*ed093b41SRobert Mustacchi getuserpc(void)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	greg_t upc = lwptoregs(ttolwp(curthread))->r_pc;
4357c478bd9Sstevel@tonic-gate 	uint32_t insn;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	if (curthread->t_sysnum == 0)
4387c478bd9Sstevel@tonic-gate 		return (upc);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/*
4417c478bd9Sstevel@tonic-gate 	 * We might've gotten here from sysenter (0xf 0x34),
4427c478bd9Sstevel@tonic-gate 	 * syscall (0xf 0x5) or lcall (0x9a 0 0 0 0 0x27 0).
4437c478bd9Sstevel@tonic-gate 	 *
4447c478bd9Sstevel@tonic-gate 	 * Go peek at the binary to figure it out..
4457c478bd9Sstevel@tonic-gate 	 */
4467c478bd9Sstevel@tonic-gate 	if (fuword32((void *)(upc - 2), &insn) != -1 &&
4477c478bd9Sstevel@tonic-gate 	    (insn & 0xffff) == 0x340f || (insn & 0xffff) == 0x050f)
4487c478bd9Sstevel@tonic-gate 		return (upc - 2);
4497c478bd9Sstevel@tonic-gate 	return (upc - 7);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate /*
4537c478bd9Sstevel@tonic-gate  * Protect segment registers from non-user privilege levels and GDT selectors
4547c478bd9Sstevel@tonic-gate  * other than USER_CS, USER_DS and lwp FS and GS values.  If the segment
4557c478bd9Sstevel@tonic-gate  * selector is non-null and not USER_CS/USER_DS, we make sure that the
4567c478bd9Sstevel@tonic-gate  * TI bit is set to point into the LDT and that the RPL is set to 3.
4577c478bd9Sstevel@tonic-gate  *
4587c478bd9Sstevel@tonic-gate  * Since struct regs stores each 16-bit segment register as a 32-bit greg_t, we
4597c478bd9Sstevel@tonic-gate  * also explicitly zero the top 16 bits since they may be coming from the
4607c478bd9Sstevel@tonic-gate  * user's address space via setcontext(2) or /proc.
461843e1988Sjohnlev  *
462843e1988Sjohnlev  * Note about null selector. When running on the hypervisor if we allow a
463843e1988Sjohnlev  * process to set its %cs to null selector with RPL of 0 the hypervisor will
464843e1988Sjohnlev  * crash the domain. If running on bare metal we would get a #gp fault and
465843e1988Sjohnlev  * be able to kill the process and continue on. Therefore we make sure to
466843e1988Sjohnlev  * force RPL to SEL_UPL even for null selector when setting %cs.
4677c478bd9Sstevel@tonic-gate  */
4687c478bd9Sstevel@tonic-gate 
469843e1988Sjohnlev #if defined(IS_CS) || defined(IS_NOT_CS)
470843e1988Sjohnlev #error	"IS_CS and IS_NOT_CS already defined"
471843e1988Sjohnlev #endif
472843e1988Sjohnlev 
473843e1988Sjohnlev #define	IS_CS		1
474843e1988Sjohnlev #define	IS_NOT_CS	0
475843e1988Sjohnlev 
4767c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4777c478bd9Sstevel@tonic-gate static greg_t
fix_segreg(greg_t sr,int iscs,model_t datamodel)478843e1988Sjohnlev fix_segreg(greg_t sr, int iscs, model_t datamodel)
4797c478bd9Sstevel@tonic-gate {
4807c478bd9Sstevel@tonic-gate 	switch (sr &= 0xffff) {
481843e1988Sjohnlev 
482843e1988Sjohnlev 	case 0:
483843e1988Sjohnlev 		if (iscs == IS_CS)
484843e1988Sjohnlev 			return (0 | SEL_UPL);
485843e1988Sjohnlev 		else
486843e1988Sjohnlev 			return (0);
487843e1988Sjohnlev 
4887c478bd9Sstevel@tonic-gate 	/*
4897c478bd9Sstevel@tonic-gate 	 * If lwp attempts to switch data model then force their
4907c478bd9Sstevel@tonic-gate 	 * code selector to be null selector.
4917c478bd9Sstevel@tonic-gate 	 */
4927c478bd9Sstevel@tonic-gate 	case U32CS_SEL:
4937c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE)
494843e1988Sjohnlev 			return (0 | SEL_UPL);
4957c478bd9Sstevel@tonic-gate 		else
4967c478bd9Sstevel@tonic-gate 			return (sr);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	case UCS_SEL:
4997c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_ILP32)
500843e1988Sjohnlev 			return (0 | SEL_UPL);
5017c478bd9Sstevel@tonic-gate 	/*FALLTHROUGH*/
5027c478bd9Sstevel@tonic-gate 	case UDS_SEL:
5037c478bd9Sstevel@tonic-gate 	case LWPFS_SEL:
5047c478bd9Sstevel@tonic-gate 	case LWPGS_SEL:
505843e1988Sjohnlev 	case SEL_UPL:
5067c478bd9Sstevel@tonic-gate 		return (sr);
5077c478bd9Sstevel@tonic-gate 	default:
5087c478bd9Sstevel@tonic-gate 		break;
5097c478bd9Sstevel@tonic-gate 	}
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	/*
5120baeff3dSrab 	 * Force it into the LDT in ring 3 for 32-bit processes, which by
5130baeff3dSrab 	 * default do not have an LDT, so that any attempt to use an invalid
514843e1988Sjohnlev 	 * selector will reference the (non-existant) LDT, and cause a #gp
515843e1988Sjohnlev 	 * fault for the process.
5160baeff3dSrab 	 *
5177c478bd9Sstevel@tonic-gate 	 * 64-bit processes get the null gdt selector since they
5187c478bd9Sstevel@tonic-gate 	 * are not allowed to have a private LDT.
5197c478bd9Sstevel@tonic-gate 	 */
520843e1988Sjohnlev 	if (datamodel == DATAMODEL_ILP32) {
521843e1988Sjohnlev 		return (sr | SEL_TI_LDT | SEL_UPL);
522843e1988Sjohnlev 	} else {
523843e1988Sjohnlev 		if (iscs == IS_CS)
524843e1988Sjohnlev 			return (0 | SEL_UPL);
525843e1988Sjohnlev 		else
526843e1988Sjohnlev 			return (0);
527843e1988Sjohnlev 	}
528843e1988Sjohnlev 
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate /*
5327c478bd9Sstevel@tonic-gate  * Set general registers.
5337c478bd9Sstevel@tonic-gate  */
5347c478bd9Sstevel@tonic-gate void
setgregs(klwp_t * lwp,gregset_t grp)5357c478bd9Sstevel@tonic-gate setgregs(klwp_t *lwp, gregset_t grp)
5367c478bd9Sstevel@tonic-gate {
5377c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
5387c478bd9Sstevel@tonic-gate 	model_t	datamodel = lwp_getdatamodel(lwp);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
5417c478bd9Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	if (datamodel == DATAMODEL_NATIVE) {
5447c478bd9Sstevel@tonic-gate 		if (thisthread)
5457c478bd9Sstevel@tonic-gate 			(void) save_syscall_args();	/* copy the args */
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		rp->r_rdi = grp[REG_RDI];
5487c478bd9Sstevel@tonic-gate 		rp->r_rsi = grp[REG_RSI];
5497c478bd9Sstevel@tonic-gate 		rp->r_rdx = grp[REG_RDX];
5507c478bd9Sstevel@tonic-gate 		rp->r_rcx = grp[REG_RCX];
5517c478bd9Sstevel@tonic-gate 		rp->r_r8 = grp[REG_R8];
5527c478bd9Sstevel@tonic-gate 		rp->r_r9 = grp[REG_R9];
5537c478bd9Sstevel@tonic-gate 		rp->r_rax = grp[REG_RAX];
5547c478bd9Sstevel@tonic-gate 		rp->r_rbx = grp[REG_RBX];
5557c478bd9Sstevel@tonic-gate 		rp->r_rbp = grp[REG_RBP];
5567c478bd9Sstevel@tonic-gate 		rp->r_r10 = grp[REG_R10];
5577c478bd9Sstevel@tonic-gate 		rp->r_r11 = grp[REG_R11];
5587c478bd9Sstevel@tonic-gate 		rp->r_r12 = grp[REG_R12];
5597c478bd9Sstevel@tonic-gate 		rp->r_r13 = grp[REG_R13];
5607c478bd9Sstevel@tonic-gate 		rp->r_r14 = grp[REG_R14];
5617c478bd9Sstevel@tonic-gate 		rp->r_r15 = grp[REG_R15];
5627c478bd9Sstevel@tonic-gate 		rp->r_trapno = grp[REG_TRAPNO];
5637c478bd9Sstevel@tonic-gate 		rp->r_err = grp[REG_ERR];
5647c478bd9Sstevel@tonic-gate 		rp->r_rip = grp[REG_RIP];
5657c478bd9Sstevel@tonic-gate 		/*
5667c478bd9Sstevel@tonic-gate 		 * Setting %cs or %ss to anything else is quietly but
5677c478bd9Sstevel@tonic-gate 		 * quite definitely forbidden!
5687c478bd9Sstevel@tonic-gate 		 */
5697c478bd9Sstevel@tonic-gate 		rp->r_cs = UCS_SEL;
5707c478bd9Sstevel@tonic-gate 		rp->r_ss = UDS_SEL;
5717c478bd9Sstevel@tonic-gate 		rp->r_rsp = grp[REG_RSP];
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 		if (thisthread)
5747c478bd9Sstevel@tonic-gate 			kpreempt_disable();
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 		pcb->pcb_ds = UDS_SEL;
5777c478bd9Sstevel@tonic-gate 		pcb->pcb_es = UDS_SEL;
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 		/*
5807c478bd9Sstevel@tonic-gate 		 * 64-bit processes -are- allowed to set their fsbase/gsbase
5817c478bd9Sstevel@tonic-gate 		 * values directly, but only if they're using the segment
5827c478bd9Sstevel@tonic-gate 		 * selectors that allow that semantic.
5837c478bd9Sstevel@tonic-gate 		 *
5847c478bd9Sstevel@tonic-gate 		 * (32-bit processes must use lwp_set_private().)
5857c478bd9Sstevel@tonic-gate 		 */
5867c478bd9Sstevel@tonic-gate 		pcb->pcb_fsbase = grp[REG_FSBASE];
5877c478bd9Sstevel@tonic-gate 		pcb->pcb_gsbase = grp[REG_GSBASE];
588843e1988Sjohnlev 		pcb->pcb_fs = fix_segreg(grp[REG_FS], IS_NOT_CS, datamodel);
589843e1988Sjohnlev 		pcb->pcb_gs = fix_segreg(grp[REG_GS], IS_NOT_CS, datamodel);
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 		/*
5927c478bd9Sstevel@tonic-gate 		 * Ensure that we go out via update_sregs
5937c478bd9Sstevel@tonic-gate 		 */
5944c28a617SRobert Mustacchi 		PCB_SET_UPDATE_SEGS(pcb);
5957c478bd9Sstevel@tonic-gate 		lwptot(lwp)->t_post_sys = 1;
5967c478bd9Sstevel@tonic-gate 		if (thisthread)
5977c478bd9Sstevel@tonic-gate 			kpreempt_enable();
5987c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
5997c478bd9Sstevel@tonic-gate 	} else {
6007c478bd9Sstevel@tonic-gate 		rp->r_rdi = (uint32_t)grp[REG_RDI];
6017c478bd9Sstevel@tonic-gate 		rp->r_rsi = (uint32_t)grp[REG_RSI];
6027c478bd9Sstevel@tonic-gate 		rp->r_rdx = (uint32_t)grp[REG_RDX];
6037c478bd9Sstevel@tonic-gate 		rp->r_rcx = (uint32_t)grp[REG_RCX];
6047c478bd9Sstevel@tonic-gate 		rp->r_rax = (uint32_t)grp[REG_RAX];
6057c478bd9Sstevel@tonic-gate 		rp->r_rbx = (uint32_t)grp[REG_RBX];
6067c478bd9Sstevel@tonic-gate 		rp->r_rbp = (uint32_t)grp[REG_RBP];
6077c478bd9Sstevel@tonic-gate 		rp->r_trapno = (uint32_t)grp[REG_TRAPNO];
6087c478bd9Sstevel@tonic-gate 		rp->r_err = (uint32_t)grp[REG_ERR];
6097c478bd9Sstevel@tonic-gate 		rp->r_rip = (uint32_t)grp[REG_RIP];
6107c478bd9Sstevel@tonic-gate 
611843e1988Sjohnlev 		rp->r_cs = fix_segreg(grp[REG_CS], IS_CS, datamodel);
612843e1988Sjohnlev 		rp->r_ss = fix_segreg(grp[REG_DS], IS_NOT_CS, datamodel);
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		rp->r_rsp = (uint32_t)grp[REG_RSP];
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate 		if (thisthread)
6177c478bd9Sstevel@tonic-gate 			kpreempt_disable();
6187c478bd9Sstevel@tonic-gate 
619843e1988Sjohnlev 		pcb->pcb_ds = fix_segreg(grp[REG_DS], IS_NOT_CS, datamodel);
620843e1988Sjohnlev 		pcb->pcb_es = fix_segreg(grp[REG_ES], IS_NOT_CS, datamodel);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 		/*
6237c478bd9Sstevel@tonic-gate 		 * (See fsbase/gsbase commentary above)
6247c478bd9Sstevel@tonic-gate 		 */
625843e1988Sjohnlev 		pcb->pcb_fs = fix_segreg(grp[REG_FS], IS_NOT_CS, datamodel);
626843e1988Sjohnlev 		pcb->pcb_gs = fix_segreg(grp[REG_GS], IS_NOT_CS, datamodel);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 		/*
6297c478bd9Sstevel@tonic-gate 		 * Ensure that we go out via update_sregs
6307c478bd9Sstevel@tonic-gate 		 */
6314c28a617SRobert Mustacchi 		PCB_SET_UPDATE_SEGS(pcb);
6327c478bd9Sstevel@tonic-gate 		lwptot(lwp)->t_post_sys = 1;
6337c478bd9Sstevel@tonic-gate 		if (thisthread)
6347c478bd9Sstevel@tonic-gate 			kpreempt_enable();
6357c478bd9Sstevel@tonic-gate #endif
6367c478bd9Sstevel@tonic-gate 	}
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	/*
6397c478bd9Sstevel@tonic-gate 	 * Only certain bits of the flags register can be modified.
6407c478bd9Sstevel@tonic-gate 	 */
6417c478bd9Sstevel@tonic-gate 	rp->r_rfl = (rp->r_rfl & ~PSL_USERMASK) |
6427c478bd9Sstevel@tonic-gate 	    (grp[REG_RFL] & PSL_USERMASK);
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate /*
6467c478bd9Sstevel@tonic-gate  * Determine whether eip is likely to have an interrupt frame
6477c478bd9Sstevel@tonic-gate  * on the stack.  We do this by comparing the address to the
6487c478bd9Sstevel@tonic-gate  * range of addresses spanned by several well-known routines.
6497c478bd9Sstevel@tonic-gate  */
6507c478bd9Sstevel@tonic-gate extern void _interrupt();
6517c478bd9Sstevel@tonic-gate extern void _allsyscalls();
6527c478bd9Sstevel@tonic-gate extern void _cmntrap();
6537c478bd9Sstevel@tonic-gate extern void fakesoftint();
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate extern size_t _interrupt_size;
6567c478bd9Sstevel@tonic-gate extern size_t _allsyscalls_size;
6577c478bd9Sstevel@tonic-gate extern size_t _cmntrap_size;
6587c478bd9Sstevel@tonic-gate extern size_t _fakesoftint_size;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate /*
6617c478bd9Sstevel@tonic-gate  * Get a pc-only stacktrace.  Used for kmem_alloc() buffer ownership tracking.
6627c478bd9Sstevel@tonic-gate  * Returns MIN(current stack depth, pcstack_limit).
6637c478bd9Sstevel@tonic-gate  */
6647c478bd9Sstevel@tonic-gate int
getpcstack(pc_t * pcstack,int pcstack_limit)6657c478bd9Sstevel@tonic-gate getpcstack(pc_t *pcstack, int pcstack_limit)
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	struct frame *fp = (struct frame *)getfp();
6687c478bd9Sstevel@tonic-gate 	struct frame *nextfp, *minfp, *stacktop;
6697c478bd9Sstevel@tonic-gate 	int depth = 0;
6707c478bd9Sstevel@tonic-gate 	int on_intr;
6717c478bd9Sstevel@tonic-gate 	uintptr_t pc;
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
6747c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
6757c478bd9Sstevel@tonic-gate 	else
6767c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
6777c478bd9Sstevel@tonic-gate 	minfp = fp;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	pc = ((struct regs *)fp)->r_pc;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	while (depth < pcstack_limit) {
6827c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)fp->fr_savfp;
6837c478bd9Sstevel@tonic-gate 		pc = fp->fr_savpc;
6847c478bd9Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
6857c478bd9Sstevel@tonic-gate 			if (on_intr) {
6867c478bd9Sstevel@tonic-gate 				/*
6877c478bd9Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
6887c478bd9Sstevel@tonic-gate 				 */
6897c478bd9Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
6907c478bd9Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
6917c478bd9Sstevel@tonic-gate 				on_intr = 0;
6927c478bd9Sstevel@tonic-gate 				continue;
6937c478bd9Sstevel@tonic-gate 			}
6947c478bd9Sstevel@tonic-gate 			break;
6957c478bd9Sstevel@tonic-gate 		}
6967c478bd9Sstevel@tonic-gate 		pcstack[depth++] = (pc_t)pc;
6977c478bd9Sstevel@tonic-gate 		fp = nextfp;
6987c478bd9Sstevel@tonic-gate 		minfp = fp;
6997c478bd9Sstevel@tonic-gate 	}
7007c478bd9Sstevel@tonic-gate 	return (depth);
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate /*
7047c478bd9Sstevel@tonic-gate  * The following ELF header fields are defined as processor-specific
7057c478bd9Sstevel@tonic-gate  * in the V8 ABI:
7067c478bd9Sstevel@tonic-gate  *
7077c478bd9Sstevel@tonic-gate  *	e_ident[EI_DATA]	encoding of the processor-specific
7087c478bd9Sstevel@tonic-gate  *				data in the object file
7097c478bd9Sstevel@tonic-gate  *	e_machine		processor identification
7107c478bd9Sstevel@tonic-gate  *	e_flags			processor-specific flags associated
7117c478bd9Sstevel@tonic-gate  *				with the file
7127c478bd9Sstevel@tonic-gate  */
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate /*
7157c478bd9Sstevel@tonic-gate  * The value of at_flags reflects a platform's cpu module support.
7167c478bd9Sstevel@tonic-gate  * at_flags is used to check for allowing a binary to execute and
7177c478bd9Sstevel@tonic-gate  * is passed as the value of the AT_FLAGS auxiliary vector.
7187c478bd9Sstevel@tonic-gate  */
7197c478bd9Sstevel@tonic-gate int at_flags = 0;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate /*
7227c478bd9Sstevel@tonic-gate  * Check the processor-specific fields of an ELF header.
7237c478bd9Sstevel@tonic-gate  *
7247c478bd9Sstevel@tonic-gate  * returns 1 if the fields are valid, 0 otherwise
7257c478bd9Sstevel@tonic-gate  */
7267c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
7277c478bd9Sstevel@tonic-gate int
elfheadcheck(unsigned char e_data,Elf32_Half e_machine,Elf32_Word e_flags)7287c478bd9Sstevel@tonic-gate elfheadcheck(
7297c478bd9Sstevel@tonic-gate 	unsigned char e_data,
7307c478bd9Sstevel@tonic-gate 	Elf32_Half e_machine,
7317c478bd9Sstevel@tonic-gate 	Elf32_Word e_flags)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate 	if (e_data != ELFDATA2LSB)
7347c478bd9Sstevel@tonic-gate 		return (0);
7357c478bd9Sstevel@tonic-gate 	if (e_machine == EM_AMD64)
7367c478bd9Sstevel@tonic-gate 		return (1);
7377c478bd9Sstevel@tonic-gate 	return (e_machine == EM_386);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate uint_t auxv_hwcap_include = 0;	/* patch to enable unrecognized features */
741ebb8ac07SRobert Mustacchi uint_t auxv_hwcap_include_2 = 0;	/* second word */
7427c478bd9Sstevel@tonic-gate uint_t auxv_hwcap_exclude = 0;	/* patch for broken cpus, debugging */
743ebb8ac07SRobert Mustacchi uint_t auxv_hwcap_exclude_2 = 0;	/* second word */
7447c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
7457c478bd9Sstevel@tonic-gate uint_t auxv_hwcap32_include = 0;	/* ditto for 32-bit apps */
746ebb8ac07SRobert Mustacchi uint_t auxv_hwcap32_include_2 = 0;	/* ditto for 32-bit apps */
7477c478bd9Sstevel@tonic-gate uint_t auxv_hwcap32_exclude = 0;	/* ditto for 32-bit apps */
748ebb8ac07SRobert Mustacchi uint_t auxv_hwcap32_exclude_2 = 0;	/* ditto for 32-bit apps */
7497c478bd9Sstevel@tonic-gate #endif
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate /*
7527c478bd9Sstevel@tonic-gate  * Gather information about the processor and place it into auxv_hwcap
7537c478bd9Sstevel@tonic-gate  * so that it can be exported to the linker via the aux vector.
7547c478bd9Sstevel@tonic-gate  *
7557c478bd9Sstevel@tonic-gate  * We use this seemingly complicated mechanism so that we can ensure
7567c478bd9Sstevel@tonic-gate  * that /etc/system can be used to override what the system can or
75756726c7eSRobert Mustacchi  * cannot discover for itself. Due to a lack of use, this has not
75856726c7eSRobert Mustacchi  * been extended to the 3rd word.
7597c478bd9Sstevel@tonic-gate  */
7607c478bd9Sstevel@tonic-gate void
bind_hwcap(void)7617c478bd9Sstevel@tonic-gate bind_hwcap(void)
7627c478bd9Sstevel@tonic-gate {
76356726c7eSRobert Mustacchi 	uint_t cpu_hwcap_flags[3];
764ab5bb018SKeith M Wesolowski 	cpuid_execpass(NULL, CPUID_PASS_RESOLVE, cpu_hwcap_flags);
7657c478bd9Sstevel@tonic-gate 
766ebb8ac07SRobert Mustacchi 	auxv_hwcap = (auxv_hwcap_include | cpu_hwcap_flags[0]) &
7677c478bd9Sstevel@tonic-gate 	    ~auxv_hwcap_exclude;
768ebb8ac07SRobert Mustacchi 	auxv_hwcap_2 = (auxv_hwcap_include_2 | cpu_hwcap_flags[1]) &
769ebb8ac07SRobert Mustacchi 	    ~auxv_hwcap_exclude_2;
77056726c7eSRobert Mustacchi 	auxv_hwcap_3 = cpu_hwcap_flags[2];
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	/*
7737c478bd9Sstevel@tonic-gate 	 * On AMD processors, sysenter just doesn't work at all
7747c478bd9Sstevel@tonic-gate 	 * when the kernel is in long mode.  On IA-32e processors
7757c478bd9Sstevel@tonic-gate 	 * it does, but there's no real point in all the alternate
7767c478bd9Sstevel@tonic-gate 	 * mechanism when syscall works on both.
7777c478bd9Sstevel@tonic-gate 	 *
7787c478bd9Sstevel@tonic-gate 	 * Besides, the kernel's sysenter handler is expecting a
7797c478bd9Sstevel@tonic-gate 	 * 32-bit lwp ...
7807c478bd9Sstevel@tonic-gate 	 */
7817c478bd9Sstevel@tonic-gate 	auxv_hwcap &= ~AV_386_SEP;
7827c478bd9Sstevel@tonic-gate 
783ebb8ac07SRobert Mustacchi 	if (auxv_hwcap_include || auxv_hwcap_exclude || auxv_hwcap_include_2 ||
784ebb8ac07SRobert Mustacchi 	    auxv_hwcap_exclude_2) {
785a3853ec4SRobert Mustacchi 		/*
786a3853ec4SRobert Mustacchi 		 * The below assignment is regrettably required to get lint
787a3853ec4SRobert Mustacchi 		 * to accept the validity of our format string.  The format
788a3853ec4SRobert Mustacchi 		 * string is in fact valid, but whatever intelligence in lint
789a3853ec4SRobert Mustacchi 		 * understands the cmn_err()-specific %b appears to have an
790a3853ec4SRobert Mustacchi 		 * off-by-one error:  it (mistakenly) complains about bit
791a3853ec4SRobert Mustacchi 		 * number 32 (even though this is explicitly permitted).
792a3853ec4SRobert Mustacchi 		 * Normally, one would will away such warnings with a "LINTED"
793a3853ec4SRobert Mustacchi 		 * directive, but for reasons unclear and unknown, lint
794a3853ec4SRobert Mustacchi 		 * refuses to be assuaged in this case.  Fortunately, lint
795a3853ec4SRobert Mustacchi 		 * doesn't pretend to have solved the Halting Problem --
796a3853ec4SRobert Mustacchi 		 * and as soon as the format string is programmatic, it
797a3853ec4SRobert Mustacchi 		 * knows enough to shut up.
798a3853ec4SRobert Mustacchi 		 */
799ebb8ac07SRobert Mustacchi 		char *fmt = "?user ABI extensions: %b\n";
800a3853ec4SRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap, FMT_AV_386);
801ebb8ac07SRobert Mustacchi 		fmt = "?user ABI extensions (word 2): %b\n";
802ebb8ac07SRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap_2, FMT_AV_386_2);
80356726c7eSRobert Mustacchi 		fmt = "?user ABI extensions (word 2): %b\n";
80456726c7eSRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap_3, FMT_AV_386_3);
805a3853ec4SRobert Mustacchi 	}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
808ebb8ac07SRobert Mustacchi 	auxv_hwcap32 = (auxv_hwcap32_include | cpu_hwcap_flags[0]) &
809b5b48cc1Ssudheer 	    ~auxv_hwcap32_exclude;
810ebb8ac07SRobert Mustacchi 	auxv_hwcap32_2 = (auxv_hwcap32_include_2 | cpu_hwcap_flags[1]) &
811ebb8ac07SRobert Mustacchi 	    ~auxv_hwcap32_exclude_2;
81256726c7eSRobert Mustacchi 	auxv_hwcap32_3 = auxv_hwcap_3;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	/*
8157c478bd9Sstevel@tonic-gate 	 * If this is an amd64 architecture machine from Intel, then
8167c478bd9Sstevel@tonic-gate 	 * syscall -doesn't- work in compatibility mode, only sysenter does.
8177c478bd9Sstevel@tonic-gate 	 *
8187c478bd9Sstevel@tonic-gate 	 * Sigh.
8197c478bd9Sstevel@tonic-gate 	 */
8207c478bd9Sstevel@tonic-gate 	if (!cpuid_syscall32_insn(NULL))
8217c478bd9Sstevel@tonic-gate 		auxv_hwcap32 &= ~AV_386_AMD_SYSC;
822ae115bc7Smrj 
823ae115bc7Smrj 	/*
824ae115bc7Smrj 	 * 32-bit processes can -always- use the lahf/sahf instructions
825ae115bc7Smrj 	 */
826ae115bc7Smrj 	auxv_hwcap32 |= AV_386_AHF;
827cff040f3SRobert Mustacchi 
828cff040f3SRobert Mustacchi 	/*
829cff040f3SRobert Mustacchi 	 * 32-bit processes can -never- use fsgsbase instructions.
830cff040f3SRobert Mustacchi 	 */
831cff040f3SRobert Mustacchi 	auxv_hwcap32_2 &= ~AV_386_2_FSGSBASE;
8327c478bd9Sstevel@tonic-gate 
833ebb8ac07SRobert Mustacchi 	if (auxv_hwcap32_include || auxv_hwcap32_exclude ||
834ebb8ac07SRobert Mustacchi 	    auxv_hwcap32_include_2 || auxv_hwcap32_exclude_2) {
835a3853ec4SRobert Mustacchi 		/*
836a3853ec4SRobert Mustacchi 		 * See the block comment in the cmn_err() of auxv_hwcap, above.
837a3853ec4SRobert Mustacchi 		 */
838ebb8ac07SRobert Mustacchi 		char *fmt = "?32-bit user ABI extensions: %b\n";
839a3853ec4SRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap32, FMT_AV_386);
840ebb8ac07SRobert Mustacchi 		fmt = "?32-bit user ABI extensions (word 2): %b\n";
841ebb8ac07SRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap32_2, FMT_AV_386_2);
84256726c7eSRobert Mustacchi 		fmt = "?32-bit user ABI extensions (word 3): %b\n";
84356726c7eSRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap32_3, FMT_AV_386_3);
844a3853ec4SRobert Mustacchi 	}
8457c478bd9Sstevel@tonic-gate #endif
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate /*
8497c478bd9Sstevel@tonic-gate  *	sync_icache() - this is called
8507c478bd9Sstevel@tonic-gate  *	in proc/fs/prusrio.c. x86 has an unified cache and therefore
8517c478bd9Sstevel@tonic-gate  *	this is a nop.
8527c478bd9Sstevel@tonic-gate  */
8537c478bd9Sstevel@tonic-gate /* ARGSUSED */
8547c478bd9Sstevel@tonic-gate void
sync_icache(caddr_t addr,uint_t len)8557c478bd9Sstevel@tonic-gate sync_icache(caddr_t addr, uint_t len)
8567c478bd9Sstevel@tonic-gate {
8577c478bd9Sstevel@tonic-gate 	/* Do nothing for now */
8587c478bd9Sstevel@tonic-gate }
8597c478bd9Sstevel@tonic-gate 
8607c478bd9Sstevel@tonic-gate /*ARGSUSED*/
8617c478bd9Sstevel@tonic-gate void
sync_data_memory(caddr_t va,size_t len)8627c478bd9Sstevel@tonic-gate sync_data_memory(caddr_t va, size_t len)
8637c478bd9Sstevel@tonic-gate {
8647c478bd9Sstevel@tonic-gate 	/* Not implemented for this platform */
8657c478bd9Sstevel@tonic-gate }
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate int
__ipltospl(int ipl)8687c478bd9Sstevel@tonic-gate __ipltospl(int ipl)
8697c478bd9Sstevel@tonic-gate {
8707c478bd9Sstevel@tonic-gate 	return (ipltospl(ipl));
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate /*
8747c478bd9Sstevel@tonic-gate  * The panic code invokes panic_saveregs() to record the contents of a
8757c478bd9Sstevel@tonic-gate  * regs structure into the specified panic_data structure for debuggers.
8767c478bd9Sstevel@tonic-gate  */
8777c478bd9Sstevel@tonic-gate void
panic_saveregs(panic_data_t * pdp,struct regs * rp)8787c478bd9Sstevel@tonic-gate panic_saveregs(panic_data_t *pdp, struct regs *rp)
8797c478bd9Sstevel@tonic-gate {
8807c478bd9Sstevel@tonic-gate 	panic_nv_t *pnv = PANICNVGET(pdp);
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	struct cregs	creg;
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	getcregs(&creg);
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rdi", rp->r_rdi);
8877c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rsi", rp->r_rsi);
8887c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rdx", rp->r_rdx);
8897c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rcx", rp->r_rcx);
8907c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r8", rp->r_r8);
8917c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r9", rp->r_r9);
8927c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rax", rp->r_rax);
8937c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rbx", rp->r_rbx);
8947c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rbp", rp->r_rbp);
8957c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r10", rp->r_r10);
8967c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r11", rp->r_r11);
8977c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r12", rp->r_r12);
8987c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r13", rp->r_r13);
8997c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r14", rp->r_r14);
9007c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r15", rp->r_r15);
901ae115bc7Smrj 	PANICNVADD(pnv, "fsbase", rdmsr(MSR_AMD_FSBASE));
902ae115bc7Smrj 	PANICNVADD(pnv, "gsbase", rdmsr(MSR_AMD_GSBASE));
9037c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "ds", rp->r_ds);
9047c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "es", rp->r_es);
9057c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "fs", rp->r_fs);
9067c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "gs", rp->r_gs);
9077c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "trapno", rp->r_trapno);
9087c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "err", rp->r_err);
9097c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rip", rp->r_rip);
9107c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "cs", rp->r_cs);
9117c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rflags", rp->r_rfl);
9127c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rsp", rp->r_rsp);
9137c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "ss", rp->r_ss);
9147c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "gdt_hi", (uint64_t)(creg.cr_gdt._l[3]));
9157c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "gdt_lo", (uint64_t)(creg.cr_gdt._l[0]));
9167c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "idt_hi", (uint64_t)(creg.cr_idt._l[3]));
9177c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "idt_lo", (uint64_t)(creg.cr_idt._l[0]));
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "ldt", creg.cr_ldt);
9207c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "task", creg.cr_task);
9217c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "cr0", creg.cr_cr0);
9227c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "cr2", creg.cr_cr2);
9237c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "cr3", creg.cr_cr3);
9247c478bd9Sstevel@tonic-gate 	if (creg.cr_cr4)
9257c478bd9Sstevel@tonic-gate 		PANICNVADD(pnv, "cr4", creg.cr_cr4);
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	PANICNVSET(pdp, pnv);
9287c478bd9Sstevel@tonic-gate }
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate #define	TR_ARG_MAX 6	/* Max args to print, same as SPARC */
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate  * Print a stack backtrace using the specified frame pointer.  We delay two
935f6e214c7SGavin Maltby  * seconds before continuing, unless this is the panic traceback.
936f6e214c7SGavin Maltby  * If we are in the process of panicking, we also attempt to write the
937f6e214c7SGavin Maltby  * stack backtrace to a staticly assigned buffer, to allow the panic
938f6e214c7SGavin Maltby  * code to find it and write it in to uncompressed pages within the
939f6e214c7SGavin Maltby  * system crash dump.
940f6e214c7SGavin Maltby  * Note that the frame for the starting stack pointer value is omitted because
9417c478bd9Sstevel@tonic-gate  * the corresponding %eip is not known.
9427c478bd9Sstevel@tonic-gate  */
943f6e214c7SGavin Maltby 
944f6e214c7SGavin Maltby extern char *dump_stack_scratch;
945f6e214c7SGavin Maltby 
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate void
traceback(caddr_t fpreg)9487c478bd9Sstevel@tonic-gate traceback(caddr_t fpreg)
9497c478bd9Sstevel@tonic-gate {
9507c478bd9Sstevel@tonic-gate 	struct frame	*fp = (struct frame *)fpreg;
9517c478bd9Sstevel@tonic-gate 	struct frame	*nextfp;
9527c478bd9Sstevel@tonic-gate 	uintptr_t	pc, nextpc;
9537c478bd9Sstevel@tonic-gate 	ulong_t		off;
9547c478bd9Sstevel@tonic-gate 	char		args[TR_ARG_MAX * 2 + 16], *sym;
955f6e214c7SGavin Maltby 	uint_t	  offset = 0;
956f6e214c7SGavin Maltby 	uint_t	  next_offset = 0;
957f6e214c7SGavin Maltby 	char	    stack_buffer[1024];
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 	if (!panicstr)
9607c478bd9Sstevel@tonic-gate 		printf("traceback: %%fp = %p\n", (void *)fp);
9617c478bd9Sstevel@tonic-gate 
962f6e214c7SGavin Maltby 	if (panicstr && !dump_stack_scratch) {
963f6e214c7SGavin Maltby 		printf("Warning - stack not written to the dump buffer\n");
964f6e214c7SGavin Maltby 	}
965f6e214c7SGavin Maltby 
966843e1988Sjohnlev 	fp = (struct frame *)plat_traceback(fpreg);
9677c478bd9Sstevel@tonic-gate 	if ((uintptr_t)fp < KERNELBASE)
9687c478bd9Sstevel@tonic-gate 		goto out;
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	pc = fp->fr_savpc;
9717c478bd9Sstevel@tonic-gate 	fp = (struct frame *)fp->fr_savfp;
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	while ((uintptr_t)fp >= KERNELBASE) {
9747c478bd9Sstevel@tonic-gate 		/*
9757c478bd9Sstevel@tonic-gate 		 * XX64 Until port is complete tolerate 8-byte aligned
9767c478bd9Sstevel@tonic-gate 		 * frame pointers but flag with a warning so they can
9777c478bd9Sstevel@tonic-gate 		 * be fixed.
9787c478bd9Sstevel@tonic-gate 		 */
9797c478bd9Sstevel@tonic-gate 		if (((uintptr_t)fp & (STACK_ALIGN - 1)) != 0) {
9807c478bd9Sstevel@tonic-gate 			if (((uintptr_t)fp & (8 - 1)) == 0) {
9817c478bd9Sstevel@tonic-gate 				printf("  >> warning! 8-byte"
9827c478bd9Sstevel@tonic-gate 				    " aligned %%fp = %p\n", (void *)fp);
9837c478bd9Sstevel@tonic-gate 			} else {
9847c478bd9Sstevel@tonic-gate 				printf(
9857c478bd9Sstevel@tonic-gate 				    "  >> mis-aligned %%fp = %p\n", (void *)fp);
9867c478bd9Sstevel@tonic-gate 				break;
9877c478bd9Sstevel@tonic-gate 			}
9887c478bd9Sstevel@tonic-gate 		}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 		args[0] = '\0';
9917c478bd9Sstevel@tonic-gate 		nextpc = (uintptr_t)fp->fr_savpc;
9927c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)fp->fr_savfp;
9937c478bd9Sstevel@tonic-gate 		if ((sym = kobj_getsymname(pc, &off)) != NULL) {
9947c478bd9Sstevel@tonic-gate 			printf("%016lx %s:%s+%lx (%s)\n", (uintptr_t)fp,
9957c478bd9Sstevel@tonic-gate 			    mod_containing_pc((caddr_t)pc), sym, off, args);
996f6e214c7SGavin Maltby 			(void) snprintf(stack_buffer, sizeof (stack_buffer),
997f6e214c7SGavin Maltby 			    "%s:%s+%lx (%s) | ",
998f6e214c7SGavin Maltby 			    mod_containing_pc((caddr_t)pc), sym, off, args);
9997c478bd9Sstevel@tonic-gate 		} else {
10007c478bd9Sstevel@tonic-gate 			printf("%016lx %lx (%s)\n",
10017c478bd9Sstevel@tonic-gate 			    (uintptr_t)fp, pc, args);
1002f6e214c7SGavin Maltby 			(void) snprintf(stack_buffer, sizeof (stack_buffer),
1003f6e214c7SGavin Maltby 			    "%lx (%s) | ", pc, args);
1004f6e214c7SGavin Maltby 		}
1005f6e214c7SGavin Maltby 
1006f6e214c7SGavin Maltby 		if (panicstr && dump_stack_scratch) {
1007f6e214c7SGavin Maltby 			next_offset = offset + strlen(stack_buffer);
1008f6e214c7SGavin Maltby 			if (next_offset < STACK_BUF_SIZE) {
1009f6e214c7SGavin Maltby 				bcopy(stack_buffer, dump_stack_scratch + offset,
1010f6e214c7SGavin Maltby 				    strlen(stack_buffer));
1011f6e214c7SGavin Maltby 				offset = next_offset;
1012f6e214c7SGavin Maltby 			} else {
1013f6e214c7SGavin Maltby 				/*
1014f6e214c7SGavin Maltby 				 * In attempting to save the panic stack
1015f6e214c7SGavin Maltby 				 * to the dumpbuf we have overflowed that area.
1016f6e214c7SGavin Maltby 				 * Print a warning and continue to printf the
1017f6e214c7SGavin Maltby 				 * stack to the msgbuf
1018f6e214c7SGavin Maltby 				 */
1019f6e214c7SGavin Maltby 				printf("Warning: stack in the dump buffer"
1020f6e214c7SGavin Maltby 				    " may be incomplete\n");
1021f6e214c7SGavin Maltby 				offset = next_offset;
1022f6e214c7SGavin Maltby 			}
10237c478bd9Sstevel@tonic-gate 		}
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 		pc = nextpc;
10267c478bd9Sstevel@tonic-gate 		fp = nextfp;
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate out:
10297c478bd9Sstevel@tonic-gate 	if (!panicstr) {
10307c478bd9Sstevel@tonic-gate 		printf("end of traceback\n");
10317c478bd9Sstevel@tonic-gate 		DELAY(2 * MICROSEC);
1032f6e214c7SGavin Maltby 	} else if (dump_stack_scratch) {
1033f6e214c7SGavin Maltby 		dump_stack_scratch[offset] = '\0';
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate }
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate /*
10397c478bd9Sstevel@tonic-gate  * Generate a stack backtrace from a saved register set.
10407c478bd9Sstevel@tonic-gate  */
10417c478bd9Sstevel@tonic-gate void
traceregs(struct regs * rp)10427c478bd9Sstevel@tonic-gate traceregs(struct regs *rp)
10437c478bd9Sstevel@tonic-gate {
10447c478bd9Sstevel@tonic-gate 	traceback((caddr_t)rp->r_fp);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate 
10477c478bd9Sstevel@tonic-gate void
exec_set_sp(size_t stksize)10487c478bd9Sstevel@tonic-gate exec_set_sp(size_t stksize)
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	lwptoregs(lwp)->r_sp = (uintptr_t)curproc->p_usrstack - stksize;
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate hrtime_t
gethrtime_waitfree(void)10567c478bd9Sstevel@tonic-gate gethrtime_waitfree(void)
10577c478bd9Sstevel@tonic-gate {
10587c478bd9Sstevel@tonic-gate 	return (dtrace_gethrtime());
10597c478bd9Sstevel@tonic-gate }
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate hrtime_t
gethrtime(void)10627c478bd9Sstevel@tonic-gate gethrtime(void)
10637c478bd9Sstevel@tonic-gate {
10647c478bd9Sstevel@tonic-gate 	return (gethrtimef());
10657c478bd9Sstevel@tonic-gate }
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate hrtime_t
gethrtime_unscaled(void)10687c478bd9Sstevel@tonic-gate gethrtime_unscaled(void)
10697c478bd9Sstevel@tonic-gate {
10707c478bd9Sstevel@tonic-gate 	return (gethrtimeunscaledf());
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate void
scalehrtime(hrtime_t * hrt)10747c478bd9Sstevel@tonic-gate scalehrtime(hrtime_t *hrt)
10757c478bd9Sstevel@tonic-gate {
10767c478bd9Sstevel@tonic-gate 	scalehrtimef(hrt);
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate 
1079113b131bSEric Saxe uint64_t
unscalehrtime(hrtime_t nsecs)1080113b131bSEric Saxe unscalehrtime(hrtime_t nsecs)
1081113b131bSEric Saxe {
1082113b131bSEric Saxe 	return (unscalehrtimef(nsecs));
1083113b131bSEric Saxe }
1084113b131bSEric Saxe 
10857c478bd9Sstevel@tonic-gate void
gethrestime(timespec_t * tp)10867c478bd9Sstevel@tonic-gate gethrestime(timespec_t *tp)
10877c478bd9Sstevel@tonic-gate {
10887c478bd9Sstevel@tonic-gate 	gethrestimef(tp);
10897c478bd9Sstevel@tonic-gate }
10907c478bd9Sstevel@tonic-gate 
10917c478bd9Sstevel@tonic-gate /*
10927c478bd9Sstevel@tonic-gate  * Part of the implementation of hres_tick(); this routine is
10937c478bd9Sstevel@tonic-gate  * easier in C than assembler .. called with the hres_lock held.
10947c478bd9Sstevel@tonic-gate  *
10957c478bd9Sstevel@tonic-gate  * XX64	Many of these timekeeping variables need to be extern'ed in a header
10967c478bd9Sstevel@tonic-gate  */
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate #include <sys/time.h>
10997c478bd9Sstevel@tonic-gate #include <sys/machlock.h>
11007c478bd9Sstevel@tonic-gate 
11017c478bd9Sstevel@tonic-gate extern int one_sec;
11027c478bd9Sstevel@tonic-gate extern int max_hres_adj;
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate void
__adj_hrestime(void)11057c478bd9Sstevel@tonic-gate __adj_hrestime(void)
11067c478bd9Sstevel@tonic-gate {
11077c478bd9Sstevel@tonic-gate 	long long adj;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	if (hrestime_adj == 0)
11107c478bd9Sstevel@tonic-gate 		adj = 0;
11117c478bd9Sstevel@tonic-gate 	else if (hrestime_adj > 0) {
11127c478bd9Sstevel@tonic-gate 		if (hrestime_adj < max_hres_adj)
11137c478bd9Sstevel@tonic-gate 			adj = hrestime_adj;
11147c478bd9Sstevel@tonic-gate 		else
11157c478bd9Sstevel@tonic-gate 			adj = max_hres_adj;
11167c478bd9Sstevel@tonic-gate 	} else {
11177c478bd9Sstevel@tonic-gate 		if (hrestime_adj < -max_hres_adj)
11187c478bd9Sstevel@tonic-gate 			adj = -max_hres_adj;
11197c478bd9Sstevel@tonic-gate 		else
11207c478bd9Sstevel@tonic-gate 			adj = hrestime_adj;
11217c478bd9Sstevel@tonic-gate 	}
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 	timedelta -= adj;
11247c478bd9Sstevel@tonic-gate 	hrestime_adj = timedelta;
11257c478bd9Sstevel@tonic-gate 	hrestime.tv_nsec += adj;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	while (hrestime.tv_nsec >= NANOSEC) {
11287c478bd9Sstevel@tonic-gate 		one_sec++;
11297c478bd9Sstevel@tonic-gate 		hrestime.tv_sec++;
11307c478bd9Sstevel@tonic-gate 		hrestime.tv_nsec -= NANOSEC;
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate /*
11357c478bd9Sstevel@tonic-gate  * Wrapper functions to maintain backwards compability
11367c478bd9Sstevel@tonic-gate  */
11377c478bd9Sstevel@tonic-gate int
xcopyin(const void * uaddr,void * kaddr,size_t count)11387c478bd9Sstevel@tonic-gate xcopyin(const void *uaddr, void *kaddr, size_t count)
11397c478bd9Sstevel@tonic-gate {
11407c478bd9Sstevel@tonic-gate 	return (xcopyin_nta(uaddr, kaddr, count, UIO_COPY_CACHED));
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate int
xcopyout(const void * kaddr,void * uaddr,size_t count)11447c478bd9Sstevel@tonic-gate xcopyout(const void *kaddr, void *uaddr, size_t count)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	return (xcopyout_nta(kaddr, uaddr, count, UIO_COPY_CACHED));
11477c478bd9Sstevel@tonic-gate }
1148