xref: /titanic_44/usr/src/uts/intel/ia32/os/archdep.c (revision c5a5e6f47e8f40ef4f4a14b199b09585e3ecf9a0)
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
59acbbeafSnn35248  * Common Development and Distribution License (the "License").
69acbbeafSnn35248  * 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	*/
267c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
27ebb8ac07SRobert Mustacchi /*
28ebb8ac07SRobert Mustacchi  * Copyright (c) 2012, Joyent, Inc.  All rights reserved.
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>
679acbbeafSnn35248 #include <sys/brand.h>
689acbbeafSnn35248 #include <sys/machbrand.h>
697af88ac7SKuriakose Kuruvilla #include <sys/cmn_err.h>
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate extern const struct fnsave_state x87_initial;
727c478bd9Sstevel@tonic-gate extern const struct fxsave_state sse_initial;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Map an fnsave-formatted save area into an fxsave-formatted save area.
767c478bd9Sstevel@tonic-gate  *
777c478bd9Sstevel@tonic-gate  * Most fields are the same width, content and semantics.  However
787c478bd9Sstevel@tonic-gate  * the tag word is compressed.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate static void
fnsave_to_fxsave(const struct fnsave_state * fn,struct fxsave_state * fx)817c478bd9Sstevel@tonic-gate fnsave_to_fxsave(const struct fnsave_state *fn, struct fxsave_state *fx)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	uint_t i, tagbits;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	fx->fx_fcw = fn->f_fcw;
867c478bd9Sstevel@tonic-gate 	fx->fx_fsw = fn->f_fsw;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * copy element by element (because of holes)
907c478bd9Sstevel@tonic-gate 	 */
917c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++)
927c478bd9Sstevel@tonic-gate 		bcopy(&fn->f_st[i].fpr_16[0], &fx->fx_st[i].fpr_16[0],
937c478bd9Sstevel@tonic-gate 		    sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	/*
967c478bd9Sstevel@tonic-gate 	 * synthesize compressed tag bits
977c478bd9Sstevel@tonic-gate 	 */
987c478bd9Sstevel@tonic-gate 	fx->fx_fctw = 0;
997c478bd9Sstevel@tonic-gate 	for (tagbits = fn->f_ftw, i = 0; i < 8; i++, tagbits >>= 2)
1007c478bd9Sstevel@tonic-gate 		if ((tagbits & 3) != 3)
1017c478bd9Sstevel@tonic-gate 			fx->fx_fctw |= (1 << i);
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	fx->fx_fop = fn->f_fop;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate #if defined(__amd64)
1067c478bd9Sstevel@tonic-gate 	fx->fx_rip = (uint64_t)fn->f_eip;
1077c478bd9Sstevel@tonic-gate 	fx->fx_rdp = (uint64_t)fn->f_dp;
1087c478bd9Sstevel@tonic-gate #else
1097c478bd9Sstevel@tonic-gate 	fx->fx_eip = fn->f_eip;
1107c478bd9Sstevel@tonic-gate 	fx->fx_cs = fn->f_cs;
1117c478bd9Sstevel@tonic-gate 	fx->__fx_ign0 = 0;
1127c478bd9Sstevel@tonic-gate 	fx->fx_dp = fn->f_dp;
1137c478bd9Sstevel@tonic-gate 	fx->fx_ds = fn->f_ds;
1147c478bd9Sstevel@tonic-gate 	fx->__fx_ign1 = 0;
1157c478bd9Sstevel@tonic-gate #endif
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * Map from an fxsave-format save area to an fnsave-format save area.
1207c478bd9Sstevel@tonic-gate  */
1217c478bd9Sstevel@tonic-gate static void
fxsave_to_fnsave(const struct fxsave_state * fx,struct fnsave_state * fn)1227c478bd9Sstevel@tonic-gate fxsave_to_fnsave(const struct fxsave_state *fx, struct fnsave_state *fn)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	uint_t i, top, tagbits;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	fn->f_fcw = fx->fx_fcw;
1277c478bd9Sstevel@tonic-gate 	fn->__f_ign0 = 0;
1287c478bd9Sstevel@tonic-gate 	fn->f_fsw = fx->fx_fsw;
1297c478bd9Sstevel@tonic-gate 	fn->__f_ign1 = 0;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	top = (fx->fx_fsw & FPS_TOP) >> 11;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * copy element by element (because of holes)
1357c478bd9Sstevel@tonic-gate 	 */
1367c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; i++)
1377c478bd9Sstevel@tonic-gate 		bcopy(&fx->fx_st[i].fpr_16[0], &fn->f_st[i].fpr_16[0],
1387c478bd9Sstevel@tonic-gate 		    sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * synthesize uncompressed tag bits
1427c478bd9Sstevel@tonic-gate 	 */
1437c478bd9Sstevel@tonic-gate 	fn->f_ftw = 0;
1447c478bd9Sstevel@tonic-gate 	for (tagbits = fx->fx_fctw, i = 0; i < 8; i++, tagbits >>= 1) {
1457c478bd9Sstevel@tonic-gate 		uint_t ibit, expo;
1467c478bd9Sstevel@tonic-gate 		const uint16_t *fpp;
1477c478bd9Sstevel@tonic-gate 		static const uint16_t zero[5] = { 0, 0, 0, 0, 0 };
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		if ((tagbits & 1) == 0) {
1507c478bd9Sstevel@tonic-gate 			fn->f_ftw |= 3 << (i << 1);	/* empty */
1517c478bd9Sstevel@tonic-gate 			continue;
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 		/*
1557c478bd9Sstevel@tonic-gate 		 * (tags refer to *physical* registers)
1567c478bd9Sstevel@tonic-gate 		 */
1577c478bd9Sstevel@tonic-gate 		fpp = &fx->fx_st[(i - top + 8) & 7].fpr_16[0];
1587c478bd9Sstevel@tonic-gate 		ibit = fpp[3] >> 15;
1597c478bd9Sstevel@tonic-gate 		expo = fpp[4] & 0x7fff;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 		if (ibit && expo != 0 && expo != 0x7fff)
1627c478bd9Sstevel@tonic-gate 			continue;			/* valid fp number */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		if (bcmp(fpp, &zero, sizeof (zero)))
1657c478bd9Sstevel@tonic-gate 			fn->f_ftw |= 2 << (i << 1);	/* NaN */
1667c478bd9Sstevel@tonic-gate 		else
1677c478bd9Sstevel@tonic-gate 			fn->f_ftw |= 1 << (i << 1);	/* fp zero */
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	fn->f_fop = fx->fx_fop;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	fn->__f_ign2 = 0;
1737c478bd9Sstevel@tonic-gate #if defined(__amd64)
1747c478bd9Sstevel@tonic-gate 	fn->f_eip = (uint32_t)fx->fx_rip;
1757c478bd9Sstevel@tonic-gate 	fn->f_cs = U32CS_SEL;
1767c478bd9Sstevel@tonic-gate 	fn->f_dp = (uint32_t)fx->fx_rdp;
1777c478bd9Sstevel@tonic-gate 	fn->f_ds = UDS_SEL;
1787c478bd9Sstevel@tonic-gate #else
1797c478bd9Sstevel@tonic-gate 	fn->f_eip = fx->fx_eip;
1807c478bd9Sstevel@tonic-gate 	fn->f_cs = fx->fx_cs;
1817c478bd9Sstevel@tonic-gate 	fn->f_dp = fx->fx_dp;
1827c478bd9Sstevel@tonic-gate 	fn->f_ds = fx->fx_ds;
1837c478bd9Sstevel@tonic-gate #endif
1847c478bd9Sstevel@tonic-gate 	fn->__f_ign3 = 0;
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate  * Map from an fpregset_t into an fxsave-format save area
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate static void
fpregset_to_fxsave(const fpregset_t * fp,struct fxsave_state * fx)1917c478bd9Sstevel@tonic-gate fpregset_to_fxsave(const fpregset_t *fp, struct fxsave_state *fx)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate #if defined(__amd64)
1947c478bd9Sstevel@tonic-gate 	bcopy(fp, fx, sizeof (*fx));
1957c478bd9Sstevel@tonic-gate #else
196*c5a5e6f4SGordon Ross 	const struct _fpchip_state *fc = &fp->fp_reg_set.fpchip_state;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	fnsave_to_fxsave((const struct fnsave_state *)fc, fx);
1997c478bd9Sstevel@tonic-gate 	fx->fx_mxcsr = fc->mxcsr;
2007c478bd9Sstevel@tonic-gate 	bcopy(&fc->xmm[0], &fx->fx_xmm[0], sizeof (fc->xmm));
2017c478bd9Sstevel@tonic-gate #endif
2027c478bd9Sstevel@tonic-gate 	/*
2037c478bd9Sstevel@tonic-gate 	 * avoid useless #gp exceptions - mask reserved bits
2047c478bd9Sstevel@tonic-gate 	 */
2057c478bd9Sstevel@tonic-gate 	fx->fx_mxcsr &= sse_mxcsr_mask;
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate /*
2097c478bd9Sstevel@tonic-gate  * Map from an fxsave-format save area into a fpregset_t
2107c478bd9Sstevel@tonic-gate  */
2117c478bd9Sstevel@tonic-gate static void
fxsave_to_fpregset(const struct fxsave_state * fx,fpregset_t * fp)2127c478bd9Sstevel@tonic-gate fxsave_to_fpregset(const struct fxsave_state *fx, fpregset_t *fp)
2137c478bd9Sstevel@tonic-gate {
2147c478bd9Sstevel@tonic-gate #if defined(__amd64)
2157c478bd9Sstevel@tonic-gate 	bcopy(fx, fp, sizeof (*fx));
2167c478bd9Sstevel@tonic-gate #else
217*c5a5e6f4SGordon Ross 	struct _fpchip_state *fc = &fp->fp_reg_set.fpchip_state;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	fxsave_to_fnsave(fx, (struct fnsave_state *)fc);
2207c478bd9Sstevel@tonic-gate 	fc->mxcsr = fx->fx_mxcsr;
2217c478bd9Sstevel@tonic-gate 	bcopy(&fx->fx_xmm[0], &fc->xmm[0], sizeof (fc->xmm));
2227c478bd9Sstevel@tonic-gate #endif
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
2267c478bd9Sstevel@tonic-gate static void
fpregset32_to_fxsave(const fpregset32_t * fp,struct fxsave_state * fx)2277c478bd9Sstevel@tonic-gate fpregset32_to_fxsave(const fpregset32_t *fp, struct fxsave_state *fx)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate 	const struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	fnsave_to_fxsave((const struct fnsave_state *)fc, fx);
2327c478bd9Sstevel@tonic-gate 	/*
2337c478bd9Sstevel@tonic-gate 	 * avoid useless #gp exceptions - mask reserved bits
2347c478bd9Sstevel@tonic-gate 	 */
2357c478bd9Sstevel@tonic-gate 	fx->fx_mxcsr = sse_mxcsr_mask & fc->mxcsr;
2367c478bd9Sstevel@tonic-gate 	bcopy(&fc->xmm[0], &fx->fx_xmm[0], sizeof (fc->xmm));
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate static void
fxsave_to_fpregset32(const struct fxsave_state * fx,fpregset32_t * fp)2407c478bd9Sstevel@tonic-gate fxsave_to_fpregset32(const struct fxsave_state *fx, fpregset32_t *fp)
2417c478bd9Sstevel@tonic-gate {
2427c478bd9Sstevel@tonic-gate 	struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	fxsave_to_fnsave(fx, (struct fnsave_state *)fc);
2457c478bd9Sstevel@tonic-gate 	fc->mxcsr = fx->fx_mxcsr;
2467c478bd9Sstevel@tonic-gate 	bcopy(&fx->fx_xmm[0], &fc->xmm[0], sizeof (fc->xmm));
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate static void
fpregset_nto32(const fpregset_t * src,fpregset32_t * dst)2507c478bd9Sstevel@tonic-gate fpregset_nto32(const fpregset_t *src, fpregset32_t *dst)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	fxsave_to_fpregset32((struct fxsave_state *)src, dst);
2537c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.status =
2547c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.status;
2557c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.xstatus =
2567c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.xstatus;
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate static void
fpregset_32ton(const fpregset32_t * src,fpregset_t * dst)2607c478bd9Sstevel@tonic-gate fpregset_32ton(const fpregset32_t *src, fpregset_t *dst)
2617c478bd9Sstevel@tonic-gate {
2627c478bd9Sstevel@tonic-gate 	fpregset32_to_fxsave(src, (struct fxsave_state *)dst);
2637c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.status =
2647c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.status;
2657c478bd9Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.xstatus =
2667c478bd9Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.xstatus;
2677c478bd9Sstevel@tonic-gate }
2687c478bd9Sstevel@tonic-gate #endif
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate /*
2717c478bd9Sstevel@tonic-gate  * Set floating-point registers from a native fpregset_t.
2727c478bd9Sstevel@tonic-gate  */
2737c478bd9Sstevel@tonic-gate void
setfpregs(klwp_t * lwp,fpregset_t * fp)2747c478bd9Sstevel@tonic-gate setfpregs(klwp_t *lwp, fpregset_t *fp)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	struct fpu_ctx *fpu = &lwp->lwp_pcb.pcb_fpu;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	if (fpu->fpu_flags & FPU_EN) {
2797c478bd9Sstevel@tonic-gate 		if (!(fpu->fpu_flags & FPU_VALID)) {
2807c478bd9Sstevel@tonic-gate 			/*
2817c478bd9Sstevel@tonic-gate 			 * FPU context is still active, release the
2827c478bd9Sstevel@tonic-gate 			 * ownership.
2837c478bd9Sstevel@tonic-gate 			 */
2847c478bd9Sstevel@tonic-gate 			fp_free(fpu, 0);
2857c478bd9Sstevel@tonic-gate 		}
2867af88ac7SKuriakose Kuruvilla 	}
2877c478bd9Sstevel@tonic-gate 	/*
2887af88ac7SKuriakose Kuruvilla 	 * Else: if we are trying to change the FPU state of a thread which
2897c478bd9Sstevel@tonic-gate 	 * hasn't yet initialized floating point, store the state in
2907c478bd9Sstevel@tonic-gate 	 * the pcb and indicate that the state is valid.  When the
2917c478bd9Sstevel@tonic-gate 	 * thread enables floating point, it will use this state instead
2927c478bd9Sstevel@tonic-gate 	 * of the default state.
2937c478bd9Sstevel@tonic-gate 	 */
2947af88ac7SKuriakose Kuruvilla 
2957af88ac7SKuriakose Kuruvilla 	switch (fp_save_mech) {
2967af88ac7SKuriakose Kuruvilla #if defined(__i386)
2977af88ac7SKuriakose Kuruvilla 	case FP_FNSAVE:
2987af88ac7SKuriakose Kuruvilla 		bcopy(fp, &fpu->fpu_regs.kfpu_u.kfpu_fn,
2997af88ac7SKuriakose Kuruvilla 		    sizeof (fpu->fpu_regs.kfpu_u.kfpu_fn));
3007af88ac7SKuriakose Kuruvilla 		break;
3017c478bd9Sstevel@tonic-gate #endif
3027af88ac7SKuriakose Kuruvilla 	case FP_FXSAVE:
3037c478bd9Sstevel@tonic-gate 		fpregset_to_fxsave(fp, &fpu->fpu_regs.kfpu_u.kfpu_fx);
3047c478bd9Sstevel@tonic-gate 		fpu->fpu_regs.kfpu_xstatus =
3057c478bd9Sstevel@tonic-gate 		    fp->fp_reg_set.fpchip_state.xstatus;
3067af88ac7SKuriakose Kuruvilla 		break;
3077af88ac7SKuriakose Kuruvilla 
3087af88ac7SKuriakose Kuruvilla 	case FP_XSAVE:
3097af88ac7SKuriakose Kuruvilla 		fpregset_to_fxsave(fp,
3107af88ac7SKuriakose Kuruvilla 		    &fpu->fpu_regs.kfpu_u.kfpu_xs.xs_fxsave);
3117af88ac7SKuriakose Kuruvilla 		fpu->fpu_regs.kfpu_xstatus =
3127af88ac7SKuriakose Kuruvilla 		    fp->fp_reg_set.fpchip_state.xstatus;
3137af88ac7SKuriakose Kuruvilla 		fpu->fpu_regs.kfpu_u.kfpu_xs.xs_xstate_bv |=
3147af88ac7SKuriakose Kuruvilla 		    (XFEATURE_LEGACY_FP | XFEATURE_SSE);
3157af88ac7SKuriakose Kuruvilla 		break;
3167af88ac7SKuriakose Kuruvilla 	default:
3177af88ac7SKuriakose Kuruvilla 		panic("Invalid fp_save_mech");
3187af88ac7SKuriakose Kuruvilla 		/*NOTREACHED*/
3197af88ac7SKuriakose Kuruvilla 	}
3207af88ac7SKuriakose Kuruvilla 
3217c478bd9Sstevel@tonic-gate 	fpu->fpu_regs.kfpu_status = fp->fp_reg_set.fpchip_state.status;
3227c478bd9Sstevel@tonic-gate 	fpu->fpu_flags |= FPU_VALID;
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*
3267c478bd9Sstevel@tonic-gate  * Get floating-point registers into a native fpregset_t.
3277c478bd9Sstevel@tonic-gate  */
3287c478bd9Sstevel@tonic-gate void
getfpregs(klwp_t * lwp,fpregset_t * fp)3297c478bd9Sstevel@tonic-gate getfpregs(klwp_t *lwp, fpregset_t *fp)
3307c478bd9Sstevel@tonic-gate {
3317c478bd9Sstevel@tonic-gate 	struct fpu_ctx *fpu = &lwp->lwp_pcb.pcb_fpu;
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	kpreempt_disable();
3347c478bd9Sstevel@tonic-gate 	if (fpu->fpu_flags & FPU_EN) {
3357c478bd9Sstevel@tonic-gate 		/*
3367c478bd9Sstevel@tonic-gate 		 * If we have FPU hw and the thread's pcb doesn't have
3377c478bd9Sstevel@tonic-gate 		 * a valid FPU state then get the state from the hw.
3387c478bd9Sstevel@tonic-gate 		 */
3397c478bd9Sstevel@tonic-gate 		if (fpu_exists && ttolwp(curthread) == lwp &&
3407c478bd9Sstevel@tonic-gate 		    !(fpu->fpu_flags & FPU_VALID))
3417c478bd9Sstevel@tonic-gate 			fp_save(fpu); /* get the current FPU state */
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	 * There are 3 possible cases we have to be aware of here:
3467c478bd9Sstevel@tonic-gate 	 *
3477c478bd9Sstevel@tonic-gate 	 * 1. FPU is enabled.  FPU state is stored in the current LWP.
3487c478bd9Sstevel@tonic-gate 	 *
3497c478bd9Sstevel@tonic-gate 	 * 2. FPU is not enabled, and there have been no intervening /proc
3507c478bd9Sstevel@tonic-gate 	 *    modifications.  Return initial FPU state.
3517c478bd9Sstevel@tonic-gate 	 *
3527c478bd9Sstevel@tonic-gate 	 * 3. FPU is not enabled, but a /proc consumer has modified FPU state.
3537c478bd9Sstevel@tonic-gate 	 *    FPU state is stored in the current LWP.
3547c478bd9Sstevel@tonic-gate 	 */
3557c478bd9Sstevel@tonic-gate 	if ((fpu->fpu_flags & FPU_EN) || (fpu->fpu_flags & FPU_VALID)) {
3567c478bd9Sstevel@tonic-gate 		/*
3577c478bd9Sstevel@tonic-gate 		 * Cases 1 and 3.
3587c478bd9Sstevel@tonic-gate 		 */
3597af88ac7SKuriakose Kuruvilla 		switch (fp_save_mech) {
3607af88ac7SKuriakose Kuruvilla #if defined(__i386)
3617af88ac7SKuriakose Kuruvilla 		case FP_FNSAVE:
3627af88ac7SKuriakose Kuruvilla 			bcopy(&fpu->fpu_regs.kfpu_u.kfpu_fn, fp,
3637af88ac7SKuriakose Kuruvilla 			    sizeof (fpu->fpu_regs.kfpu_u.kfpu_fn));
3647af88ac7SKuriakose Kuruvilla 			break;
3657c478bd9Sstevel@tonic-gate #endif
3667af88ac7SKuriakose Kuruvilla 		case FP_FXSAVE:
3677c478bd9Sstevel@tonic-gate 			fxsave_to_fpregset(&fpu->fpu_regs.kfpu_u.kfpu_fx, fp);
3687c478bd9Sstevel@tonic-gate 			fp->fp_reg_set.fpchip_state.xstatus =
3697c478bd9Sstevel@tonic-gate 			    fpu->fpu_regs.kfpu_xstatus;
3707af88ac7SKuriakose Kuruvilla 			break;
3717af88ac7SKuriakose Kuruvilla 		case FP_XSAVE:
3727af88ac7SKuriakose Kuruvilla 			fxsave_to_fpregset(
3737af88ac7SKuriakose Kuruvilla 			    &fpu->fpu_regs.kfpu_u.kfpu_xs.xs_fxsave, fp);
3747af88ac7SKuriakose Kuruvilla 			fp->fp_reg_set.fpchip_state.xstatus =
3757af88ac7SKuriakose Kuruvilla 			    fpu->fpu_regs.kfpu_xstatus;
3767af88ac7SKuriakose Kuruvilla 			break;
3777af88ac7SKuriakose Kuruvilla 		default:
3787af88ac7SKuriakose Kuruvilla 			panic("Invalid fp_save_mech");
3797af88ac7SKuriakose Kuruvilla 			/*NOTREACHED*/
3807af88ac7SKuriakose Kuruvilla 		}
3817c478bd9Sstevel@tonic-gate 		fp->fp_reg_set.fpchip_state.status = fpu->fpu_regs.kfpu_status;
3827c478bd9Sstevel@tonic-gate 	} else {
3837c478bd9Sstevel@tonic-gate 		/*
3847c478bd9Sstevel@tonic-gate 		 * Case 2.
3857c478bd9Sstevel@tonic-gate 		 */
3867af88ac7SKuriakose Kuruvilla 		switch (fp_save_mech) {
3877af88ac7SKuriakose Kuruvilla #if defined(__i386)
3887af88ac7SKuriakose Kuruvilla 		case FP_FNSAVE:
3897af88ac7SKuriakose Kuruvilla 			bcopy(&x87_initial, fp, sizeof (x87_initial));
3907af88ac7SKuriakose Kuruvilla 			break;
3917c478bd9Sstevel@tonic-gate #endif
3927af88ac7SKuriakose Kuruvilla 		case FP_FXSAVE:
3937af88ac7SKuriakose Kuruvilla 		case FP_XSAVE:
3947af88ac7SKuriakose Kuruvilla 			/*
3957af88ac7SKuriakose Kuruvilla 			 * For now, we don't have any AVX specific field in ABI.
3967af88ac7SKuriakose Kuruvilla 			 * If we add any in the future, we need to initial them
3977af88ac7SKuriakose Kuruvilla 			 * as well.
3987af88ac7SKuriakose Kuruvilla 			 */
3997c478bd9Sstevel@tonic-gate 			fxsave_to_fpregset(&sse_initial, fp);
4007c478bd9Sstevel@tonic-gate 			fp->fp_reg_set.fpchip_state.xstatus =
4017c478bd9Sstevel@tonic-gate 			    fpu->fpu_regs.kfpu_xstatus;
4027af88ac7SKuriakose Kuruvilla 			break;
4037af88ac7SKuriakose Kuruvilla 		default:
4047af88ac7SKuriakose Kuruvilla 			panic("Invalid fp_save_mech");
4057af88ac7SKuriakose Kuruvilla 			/*NOTREACHED*/
4067af88ac7SKuriakose Kuruvilla 		}
4077c478bd9Sstevel@tonic-gate 		fp->fp_reg_set.fpchip_state.status = fpu->fpu_regs.kfpu_status;
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 	kpreempt_enable();
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate  * Set floating-point registers from an fpregset32_t.
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate void
setfpregs32(klwp_t * lwp,fpregset32_t * fp)4187c478bd9Sstevel@tonic-gate setfpregs32(klwp_t *lwp, fpregset32_t *fp)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	fpregset_t fpregs;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	fpregset_32ton(fp, &fpregs);
4237c478bd9Sstevel@tonic-gate 	setfpregs(lwp, &fpregs);
4247c478bd9Sstevel@tonic-gate }
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * Get floating-point registers into an fpregset32_t.
4287c478bd9Sstevel@tonic-gate  */
4297c478bd9Sstevel@tonic-gate void
getfpregs32(klwp_t * lwp,fpregset32_t * fp)4307c478bd9Sstevel@tonic-gate getfpregs32(klwp_t *lwp, fpregset32_t *fp)
4317c478bd9Sstevel@tonic-gate {
4327c478bd9Sstevel@tonic-gate 	fpregset_t fpregs;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	getfpregs(lwp, &fpregs);
4357c478bd9Sstevel@tonic-gate 	fpregset_nto32(&fpregs, fp);
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate  * Return the general registers
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate void
getgregs(klwp_t * lwp,gregset_t grp)4447c478bd9Sstevel@tonic-gate getgregs(klwp_t *lwp, gregset_t grp)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
4477c478bd9Sstevel@tonic-gate #if defined(__amd64)
4487c478bd9Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
4497c478bd9Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	grp[REG_RDI] = rp->r_rdi;
4527c478bd9Sstevel@tonic-gate 	grp[REG_RSI] = rp->r_rsi;
4537c478bd9Sstevel@tonic-gate 	grp[REG_RDX] = rp->r_rdx;
4547c478bd9Sstevel@tonic-gate 	grp[REG_RCX] = rp->r_rcx;
4557c478bd9Sstevel@tonic-gate 	grp[REG_R8] = rp->r_r8;
4567c478bd9Sstevel@tonic-gate 	grp[REG_R9] = rp->r_r9;
4577c478bd9Sstevel@tonic-gate 	grp[REG_RAX] = rp->r_rax;
4587c478bd9Sstevel@tonic-gate 	grp[REG_RBX] = rp->r_rbx;
4597c478bd9Sstevel@tonic-gate 	grp[REG_RBP] = rp->r_rbp;
4607c478bd9Sstevel@tonic-gate 	grp[REG_R10] = rp->r_r10;
4617c478bd9Sstevel@tonic-gate 	grp[REG_R11] = rp->r_r11;
4627c478bd9Sstevel@tonic-gate 	grp[REG_R12] = rp->r_r12;
4637c478bd9Sstevel@tonic-gate 	grp[REG_R13] = rp->r_r13;
4647c478bd9Sstevel@tonic-gate 	grp[REG_R14] = rp->r_r14;
4657c478bd9Sstevel@tonic-gate 	grp[REG_R15] = rp->r_r15;
4667c478bd9Sstevel@tonic-gate 	grp[REG_FSBASE] = pcb->pcb_fsbase;
4677c478bd9Sstevel@tonic-gate 	grp[REG_GSBASE] = pcb->pcb_gsbase;
4687c478bd9Sstevel@tonic-gate 	if (thisthread)
4697c478bd9Sstevel@tonic-gate 		kpreempt_disable();
4707712e92cSsudheer 	if (pcb->pcb_rupdate == 1) {
4717c478bd9Sstevel@tonic-gate 		grp[REG_DS] = pcb->pcb_ds;
4727c478bd9Sstevel@tonic-gate 		grp[REG_ES] = pcb->pcb_es;
4737c478bd9Sstevel@tonic-gate 		grp[REG_FS] = pcb->pcb_fs;
4747c478bd9Sstevel@tonic-gate 		grp[REG_GS] = pcb->pcb_gs;
4757c478bd9Sstevel@tonic-gate 	} else {
4767c478bd9Sstevel@tonic-gate 		grp[REG_DS] = rp->r_ds;
4777c478bd9Sstevel@tonic-gate 		grp[REG_ES] = rp->r_es;
4787c478bd9Sstevel@tonic-gate 		grp[REG_FS] = rp->r_fs;
4797c478bd9Sstevel@tonic-gate 		grp[REG_GS] = rp->r_gs;
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 	if (thisthread)
4827c478bd9Sstevel@tonic-gate 		kpreempt_enable();
4837c478bd9Sstevel@tonic-gate 	grp[REG_TRAPNO] = rp->r_trapno;
4847c478bd9Sstevel@tonic-gate 	grp[REG_ERR] = rp->r_err;
4857c478bd9Sstevel@tonic-gate 	grp[REG_RIP] = rp->r_rip;
4867c478bd9Sstevel@tonic-gate 	grp[REG_CS] = rp->r_cs;
4877c478bd9Sstevel@tonic-gate 	grp[REG_SS] = rp->r_ss;
4887c478bd9Sstevel@tonic-gate 	grp[REG_RFL] = rp->r_rfl;
4897c478bd9Sstevel@tonic-gate 	grp[REG_RSP] = rp->r_rsp;
4907c478bd9Sstevel@tonic-gate #else
4917c478bd9Sstevel@tonic-gate 	bcopy(&rp->r_gs, grp, sizeof (gregset_t));
4927c478bd9Sstevel@tonic-gate #endif
4937c478bd9Sstevel@tonic-gate }
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate void
getgregs32(klwp_t * lwp,gregset32_t grp)4987c478bd9Sstevel@tonic-gate getgregs32(klwp_t *lwp, gregset32_t grp)
4997c478bd9Sstevel@tonic-gate {
5007c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
5017c478bd9Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
5027c478bd9Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	if (thisthread)
5057c478bd9Sstevel@tonic-gate 		kpreempt_disable();
5067712e92cSsudheer 	if (pcb->pcb_rupdate == 1) {
5077c478bd9Sstevel@tonic-gate 		grp[GS] = (uint16_t)pcb->pcb_gs;
5087c478bd9Sstevel@tonic-gate 		grp[FS] = (uint16_t)pcb->pcb_fs;
5097c478bd9Sstevel@tonic-gate 		grp[DS] = (uint16_t)pcb->pcb_ds;
5107c478bd9Sstevel@tonic-gate 		grp[ES] = (uint16_t)pcb->pcb_es;
5117c478bd9Sstevel@tonic-gate 	} else {
5127c478bd9Sstevel@tonic-gate 		grp[GS] = (uint16_t)rp->r_gs;
5137c478bd9Sstevel@tonic-gate 		grp[FS] = (uint16_t)rp->r_fs;
5147c478bd9Sstevel@tonic-gate 		grp[DS] = (uint16_t)rp->r_ds;
5157c478bd9Sstevel@tonic-gate 		grp[ES] = (uint16_t)rp->r_es;
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	if (thisthread)
5187c478bd9Sstevel@tonic-gate 		kpreempt_enable();
5197c478bd9Sstevel@tonic-gate 	grp[EDI] = (greg32_t)rp->r_rdi;
5207c478bd9Sstevel@tonic-gate 	grp[ESI] = (greg32_t)rp->r_rsi;
5217c478bd9Sstevel@tonic-gate 	grp[EBP] = (greg32_t)rp->r_rbp;
5227c478bd9Sstevel@tonic-gate 	grp[ESP] = 0;
5237c478bd9Sstevel@tonic-gate 	grp[EBX] = (greg32_t)rp->r_rbx;
5247c478bd9Sstevel@tonic-gate 	grp[EDX] = (greg32_t)rp->r_rdx;
5257c478bd9Sstevel@tonic-gate 	grp[ECX] = (greg32_t)rp->r_rcx;
5267c478bd9Sstevel@tonic-gate 	grp[EAX] = (greg32_t)rp->r_rax;
5277c478bd9Sstevel@tonic-gate 	grp[TRAPNO] = (greg32_t)rp->r_trapno;
5287c478bd9Sstevel@tonic-gate 	grp[ERR] = (greg32_t)rp->r_err;
5297c478bd9Sstevel@tonic-gate 	grp[EIP] = (greg32_t)rp->r_rip;
5307c478bd9Sstevel@tonic-gate 	grp[CS] = (uint16_t)rp->r_cs;
5317c478bd9Sstevel@tonic-gate 	grp[EFL] = (greg32_t)rp->r_rfl;
5327c478bd9Sstevel@tonic-gate 	grp[UESP] = (greg32_t)rp->r_rsp;
5337c478bd9Sstevel@tonic-gate 	grp[SS] = (uint16_t)rp->r_ss;
5347c478bd9Sstevel@tonic-gate }
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate void
ucontext_32ton(const ucontext32_t * src,ucontext_t * dst)5377c478bd9Sstevel@tonic-gate ucontext_32ton(const ucontext32_t *src, ucontext_t *dst)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	mcontext_t *dmc = &dst->uc_mcontext;
5407c478bd9Sstevel@tonic-gate 	const mcontext32_t *smc = &src->uc_mcontext;
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 	bzero(dst, sizeof (*dst));
5437c478bd9Sstevel@tonic-gate 	dst->uc_flags = src->uc_flags;
5447c478bd9Sstevel@tonic-gate 	dst->uc_link = (ucontext_t *)(uintptr_t)src->uc_link;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	bcopy(&src->uc_sigmask, &dst->uc_sigmask, sizeof (dst->uc_sigmask));
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	dst->uc_stack.ss_sp = (void *)(uintptr_t)src->uc_stack.ss_sp;
5497c478bd9Sstevel@tonic-gate 	dst->uc_stack.ss_size = (size_t)src->uc_stack.ss_size;
5507c478bd9Sstevel@tonic-gate 	dst->uc_stack.ss_flags = src->uc_stack.ss_flags;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_GS] = (greg_t)(uint32_t)smc->gregs[GS];
5537c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_FS] = (greg_t)(uint32_t)smc->gregs[FS];
5547c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_ES] = (greg_t)(uint32_t)smc->gregs[ES];
5557c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_DS] = (greg_t)(uint32_t)smc->gregs[DS];
5567c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RDI] = (greg_t)(uint32_t)smc->gregs[EDI];
5577c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RSI] = (greg_t)(uint32_t)smc->gregs[ESI];
5587c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RBP] = (greg_t)(uint32_t)smc->gregs[EBP];
5597c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RBX] = (greg_t)(uint32_t)smc->gregs[EBX];
5607c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RDX] = (greg_t)(uint32_t)smc->gregs[EDX];
5617c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RCX] = (greg_t)(uint32_t)smc->gregs[ECX];
5627c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RAX] = (greg_t)(uint32_t)smc->gregs[EAX];
5637c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_TRAPNO] = (greg_t)(uint32_t)smc->gregs[TRAPNO];
5647c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_ERR] = (greg_t)(uint32_t)smc->gregs[ERR];
5657c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RIP] = (greg_t)(uint32_t)smc->gregs[EIP];
5667c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_CS] = (greg_t)(uint32_t)smc->gregs[CS];
5677c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RFL] = (greg_t)(uint32_t)smc->gregs[EFL];
5687c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_RSP] = (greg_t)(uint32_t)smc->gregs[UESP];
5697c478bd9Sstevel@tonic-gate 	dmc->gregs[REG_SS] = (greg_t)(uint32_t)smc->gregs[SS];
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	/*
5727c478bd9Sstevel@tonic-gate 	 * A valid fpregs is only copied in if uc.uc_flags has UC_FPU set
5737c478bd9Sstevel@tonic-gate 	 * otherwise there is no guarantee that anything in fpregs is valid.
5747c478bd9Sstevel@tonic-gate 	 */
5757c478bd9Sstevel@tonic-gate 	if (src->uc_flags & UC_FPU)
5767c478bd9Sstevel@tonic-gate 		fpregset_32ton(&src->uc_mcontext.fpregs,
5777c478bd9Sstevel@tonic-gate 		    &dst->uc_mcontext.fpregs);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate /*
5837c478bd9Sstevel@tonic-gate  * Return the user-level PC.
5847c478bd9Sstevel@tonic-gate  * If in a system call, return the address of the syscall trap.
5857c478bd9Sstevel@tonic-gate  */
5867c478bd9Sstevel@tonic-gate greg_t
getuserpc()5877c478bd9Sstevel@tonic-gate getuserpc()
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	greg_t upc = lwptoregs(ttolwp(curthread))->r_pc;
5907c478bd9Sstevel@tonic-gate 	uint32_t insn;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	if (curthread->t_sysnum == 0)
5937c478bd9Sstevel@tonic-gate 		return (upc);
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/*
5967c478bd9Sstevel@tonic-gate 	 * We might've gotten here from sysenter (0xf 0x34),
5977c478bd9Sstevel@tonic-gate 	 * syscall (0xf 0x5) or lcall (0x9a 0 0 0 0 0x27 0).
5987c478bd9Sstevel@tonic-gate 	 *
5997c478bd9Sstevel@tonic-gate 	 * Go peek at the binary to figure it out..
6007c478bd9Sstevel@tonic-gate 	 */
6017c478bd9Sstevel@tonic-gate 	if (fuword32((void *)(upc - 2), &insn) != -1 &&
6027c478bd9Sstevel@tonic-gate 	    (insn & 0xffff) == 0x340f || (insn & 0xffff) == 0x050f)
6037c478bd9Sstevel@tonic-gate 		return (upc - 2);
6047c478bd9Sstevel@tonic-gate 	return (upc - 7);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate /*
6087c478bd9Sstevel@tonic-gate  * Protect segment registers from non-user privilege levels and GDT selectors
6097c478bd9Sstevel@tonic-gate  * other than USER_CS, USER_DS and lwp FS and GS values.  If the segment
6107c478bd9Sstevel@tonic-gate  * selector is non-null and not USER_CS/USER_DS, we make sure that the
6117c478bd9Sstevel@tonic-gate  * TI bit is set to point into the LDT and that the RPL is set to 3.
6127c478bd9Sstevel@tonic-gate  *
6137c478bd9Sstevel@tonic-gate  * Since struct regs stores each 16-bit segment register as a 32-bit greg_t, we
6147c478bd9Sstevel@tonic-gate  * also explicitly zero the top 16 bits since they may be coming from the
6157c478bd9Sstevel@tonic-gate  * user's address space via setcontext(2) or /proc.
616843e1988Sjohnlev  *
617843e1988Sjohnlev  * Note about null selector. When running on the hypervisor if we allow a
618843e1988Sjohnlev  * process to set its %cs to null selector with RPL of 0 the hypervisor will
619843e1988Sjohnlev  * crash the domain. If running on bare metal we would get a #gp fault and
620843e1988Sjohnlev  * be able to kill the process and continue on. Therefore we make sure to
621843e1988Sjohnlev  * force RPL to SEL_UPL even for null selector when setting %cs.
6227c478bd9Sstevel@tonic-gate  */
6237c478bd9Sstevel@tonic-gate 
624843e1988Sjohnlev #if defined(IS_CS) || defined(IS_NOT_CS)
625843e1988Sjohnlev #error	"IS_CS and IS_NOT_CS already defined"
626843e1988Sjohnlev #endif
627843e1988Sjohnlev 
628843e1988Sjohnlev #define	IS_CS		1
629843e1988Sjohnlev #define	IS_NOT_CS	0
630843e1988Sjohnlev 
6317c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6327c478bd9Sstevel@tonic-gate static greg_t
fix_segreg(greg_t sr,int iscs,model_t datamodel)633843e1988Sjohnlev fix_segreg(greg_t sr, int iscs, model_t datamodel)
6347c478bd9Sstevel@tonic-gate {
6357c478bd9Sstevel@tonic-gate 	switch (sr &= 0xffff) {
636843e1988Sjohnlev 
637843e1988Sjohnlev 	case 0:
638843e1988Sjohnlev 		if (iscs == IS_CS)
639843e1988Sjohnlev 			return (0 | SEL_UPL);
640843e1988Sjohnlev 		else
641843e1988Sjohnlev 			return (0);
642843e1988Sjohnlev 
6437c478bd9Sstevel@tonic-gate #if defined(__amd64)
6447c478bd9Sstevel@tonic-gate 	/*
6457c478bd9Sstevel@tonic-gate 	 * If lwp attempts to switch data model then force their
6467c478bd9Sstevel@tonic-gate 	 * code selector to be null selector.
6477c478bd9Sstevel@tonic-gate 	 */
6487c478bd9Sstevel@tonic-gate 	case U32CS_SEL:
6497c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE)
650843e1988Sjohnlev 			return (0 | SEL_UPL);
6517c478bd9Sstevel@tonic-gate 		else
6527c478bd9Sstevel@tonic-gate 			return (sr);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 	case UCS_SEL:
6557c478bd9Sstevel@tonic-gate 		if (datamodel == DATAMODEL_ILP32)
656843e1988Sjohnlev 			return (0 | SEL_UPL);
6577c478bd9Sstevel@tonic-gate #elif defined(__i386)
6587c478bd9Sstevel@tonic-gate 	case UCS_SEL:
6597c478bd9Sstevel@tonic-gate #endif
6607c478bd9Sstevel@tonic-gate 	/*FALLTHROUGH*/
6617c478bd9Sstevel@tonic-gate 	case UDS_SEL:
6627c478bd9Sstevel@tonic-gate 	case LWPFS_SEL:
6637c478bd9Sstevel@tonic-gate 	case LWPGS_SEL:
664843e1988Sjohnlev 	case SEL_UPL:
6657c478bd9Sstevel@tonic-gate 		return (sr);
6667c478bd9Sstevel@tonic-gate 	default:
6677c478bd9Sstevel@tonic-gate 		break;
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/*
6710baeff3dSrab 	 * Force it into the LDT in ring 3 for 32-bit processes, which by
6720baeff3dSrab 	 * default do not have an LDT, so that any attempt to use an invalid
673843e1988Sjohnlev 	 * selector will reference the (non-existant) LDT, and cause a #gp
674843e1988Sjohnlev 	 * fault for the process.
6750baeff3dSrab 	 *
6767c478bd9Sstevel@tonic-gate 	 * 64-bit processes get the null gdt selector since they
6777c478bd9Sstevel@tonic-gate 	 * are not allowed to have a private LDT.
6787c478bd9Sstevel@tonic-gate 	 */
6797c478bd9Sstevel@tonic-gate #if defined(__amd64)
680843e1988Sjohnlev 	if (datamodel == DATAMODEL_ILP32) {
681843e1988Sjohnlev 		return (sr | SEL_TI_LDT | SEL_UPL);
682843e1988Sjohnlev 	} else {
683843e1988Sjohnlev 		if (iscs == IS_CS)
684843e1988Sjohnlev 			return (0 | SEL_UPL);
685843e1988Sjohnlev 		else
686843e1988Sjohnlev 			return (0);
687843e1988Sjohnlev 	}
688843e1988Sjohnlev 
6897c478bd9Sstevel@tonic-gate #elif defined(__i386)
6907c478bd9Sstevel@tonic-gate 	return (sr | SEL_TI_LDT | SEL_UPL);
6917c478bd9Sstevel@tonic-gate #endif
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate /*
6957c478bd9Sstevel@tonic-gate  * Set general registers.
6967c478bd9Sstevel@tonic-gate  */
6977c478bd9Sstevel@tonic-gate void
setgregs(klwp_t * lwp,gregset_t grp)6987c478bd9Sstevel@tonic-gate setgregs(klwp_t *lwp, gregset_t grp)
6997c478bd9Sstevel@tonic-gate {
7007c478bd9Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
7017c478bd9Sstevel@tonic-gate 	model_t	datamodel = lwp_getdatamodel(lwp);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate #if defined(__amd64)
7047c478bd9Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
7057c478bd9Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	if (datamodel == DATAMODEL_NATIVE) {
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		if (thisthread)
7107c478bd9Sstevel@tonic-gate 			(void) save_syscall_args();	/* copy the args */
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 		rp->r_rdi = grp[REG_RDI];
7137c478bd9Sstevel@tonic-gate 		rp->r_rsi = grp[REG_RSI];
7147c478bd9Sstevel@tonic-gate 		rp->r_rdx = grp[REG_RDX];
7157c478bd9Sstevel@tonic-gate 		rp->r_rcx = grp[REG_RCX];
7167c478bd9Sstevel@tonic-gate 		rp->r_r8 = grp[REG_R8];
7177c478bd9Sstevel@tonic-gate 		rp->r_r9 = grp[REG_R9];
7187c478bd9Sstevel@tonic-gate 		rp->r_rax = grp[REG_RAX];
7197c478bd9Sstevel@tonic-gate 		rp->r_rbx = grp[REG_RBX];
7207c478bd9Sstevel@tonic-gate 		rp->r_rbp = grp[REG_RBP];
7217c478bd9Sstevel@tonic-gate 		rp->r_r10 = grp[REG_R10];
7227c478bd9Sstevel@tonic-gate 		rp->r_r11 = grp[REG_R11];
7237c478bd9Sstevel@tonic-gate 		rp->r_r12 = grp[REG_R12];
7247c478bd9Sstevel@tonic-gate 		rp->r_r13 = grp[REG_R13];
7257c478bd9Sstevel@tonic-gate 		rp->r_r14 = grp[REG_R14];
7267c478bd9Sstevel@tonic-gate 		rp->r_r15 = grp[REG_R15];
7277c478bd9Sstevel@tonic-gate 		rp->r_trapno = grp[REG_TRAPNO];
7287c478bd9Sstevel@tonic-gate 		rp->r_err = grp[REG_ERR];
7297c478bd9Sstevel@tonic-gate 		rp->r_rip = grp[REG_RIP];
7307c478bd9Sstevel@tonic-gate 		/*
7317c478bd9Sstevel@tonic-gate 		 * Setting %cs or %ss to anything else is quietly but
7327c478bd9Sstevel@tonic-gate 		 * quite definitely forbidden!
7337c478bd9Sstevel@tonic-gate 		 */
7347c478bd9Sstevel@tonic-gate 		rp->r_cs = UCS_SEL;
7357c478bd9Sstevel@tonic-gate 		rp->r_ss = UDS_SEL;
7367c478bd9Sstevel@tonic-gate 		rp->r_rsp = grp[REG_RSP];
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 		if (thisthread)
7397c478bd9Sstevel@tonic-gate 			kpreempt_disable();
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 		pcb->pcb_ds = UDS_SEL;
7427c478bd9Sstevel@tonic-gate 		pcb->pcb_es = UDS_SEL;
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		/*
7457c478bd9Sstevel@tonic-gate 		 * 64-bit processes -are- allowed to set their fsbase/gsbase
7467c478bd9Sstevel@tonic-gate 		 * values directly, but only if they're using the segment
7477c478bd9Sstevel@tonic-gate 		 * selectors that allow that semantic.
7487c478bd9Sstevel@tonic-gate 		 *
7497c478bd9Sstevel@tonic-gate 		 * (32-bit processes must use lwp_set_private().)
7507c478bd9Sstevel@tonic-gate 		 */
7517c478bd9Sstevel@tonic-gate 		pcb->pcb_fsbase = grp[REG_FSBASE];
7527c478bd9Sstevel@tonic-gate 		pcb->pcb_gsbase = grp[REG_GSBASE];
753843e1988Sjohnlev 		pcb->pcb_fs = fix_segreg(grp[REG_FS], IS_NOT_CS, datamodel);
754843e1988Sjohnlev 		pcb->pcb_gs = fix_segreg(grp[REG_GS], IS_NOT_CS, datamodel);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 		/*
7577c478bd9Sstevel@tonic-gate 		 * Ensure that we go out via update_sregs
7587c478bd9Sstevel@tonic-gate 		 */
7597712e92cSsudheer 		pcb->pcb_rupdate = 1;
7607c478bd9Sstevel@tonic-gate 		lwptot(lwp)->t_post_sys = 1;
7617c478bd9Sstevel@tonic-gate 		if (thisthread)
7627c478bd9Sstevel@tonic-gate 			kpreempt_enable();
7637c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
7647c478bd9Sstevel@tonic-gate 	} else {
7657c478bd9Sstevel@tonic-gate 		rp->r_rdi = (uint32_t)grp[REG_RDI];
7667c478bd9Sstevel@tonic-gate 		rp->r_rsi = (uint32_t)grp[REG_RSI];
7677c478bd9Sstevel@tonic-gate 		rp->r_rdx = (uint32_t)grp[REG_RDX];
7687c478bd9Sstevel@tonic-gate 		rp->r_rcx = (uint32_t)grp[REG_RCX];
7697c478bd9Sstevel@tonic-gate 		rp->r_rax = (uint32_t)grp[REG_RAX];
7707c478bd9Sstevel@tonic-gate 		rp->r_rbx = (uint32_t)grp[REG_RBX];
7717c478bd9Sstevel@tonic-gate 		rp->r_rbp = (uint32_t)grp[REG_RBP];
7727c478bd9Sstevel@tonic-gate 		rp->r_trapno = (uint32_t)grp[REG_TRAPNO];
7737c478bd9Sstevel@tonic-gate 		rp->r_err = (uint32_t)grp[REG_ERR];
7747c478bd9Sstevel@tonic-gate 		rp->r_rip = (uint32_t)grp[REG_RIP];
7757c478bd9Sstevel@tonic-gate 
776843e1988Sjohnlev 		rp->r_cs = fix_segreg(grp[REG_CS], IS_CS, datamodel);
777843e1988Sjohnlev 		rp->r_ss = fix_segreg(grp[REG_DS], IS_NOT_CS, datamodel);
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 		rp->r_rsp = (uint32_t)grp[REG_RSP];
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 		if (thisthread)
7827c478bd9Sstevel@tonic-gate 			kpreempt_disable();
7837c478bd9Sstevel@tonic-gate 
784843e1988Sjohnlev 		pcb->pcb_ds = fix_segreg(grp[REG_DS], IS_NOT_CS, datamodel);
785843e1988Sjohnlev 		pcb->pcb_es = fix_segreg(grp[REG_ES], IS_NOT_CS, datamodel);
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 		/*
7887c478bd9Sstevel@tonic-gate 		 * (See fsbase/gsbase commentary above)
7897c478bd9Sstevel@tonic-gate 		 */
790843e1988Sjohnlev 		pcb->pcb_fs = fix_segreg(grp[REG_FS], IS_NOT_CS, datamodel);
791843e1988Sjohnlev 		pcb->pcb_gs = fix_segreg(grp[REG_GS], IS_NOT_CS, datamodel);
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 		/*
7947c478bd9Sstevel@tonic-gate 		 * Ensure that we go out via update_sregs
7957c478bd9Sstevel@tonic-gate 		 */
7967712e92cSsudheer 		pcb->pcb_rupdate = 1;
7977c478bd9Sstevel@tonic-gate 		lwptot(lwp)->t_post_sys = 1;
7987c478bd9Sstevel@tonic-gate 		if (thisthread)
7997c478bd9Sstevel@tonic-gate 			kpreempt_enable();
8007c478bd9Sstevel@tonic-gate #endif
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	/*
8047c478bd9Sstevel@tonic-gate 	 * Only certain bits of the flags register can be modified.
8057c478bd9Sstevel@tonic-gate 	 */
8067c478bd9Sstevel@tonic-gate 	rp->r_rfl = (rp->r_rfl & ~PSL_USERMASK) |
8077c478bd9Sstevel@tonic-gate 	    (grp[REG_RFL] & PSL_USERMASK);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate #elif defined(__i386)
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate 	/*
8127c478bd9Sstevel@tonic-gate 	 * Only certain bits of the flags register can be modified.
8137c478bd9Sstevel@tonic-gate 	 */
8147c478bd9Sstevel@tonic-gate 	grp[EFL] = (rp->r_efl & ~PSL_USERMASK) | (grp[EFL] & PSL_USERMASK);
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	/*
8177c478bd9Sstevel@tonic-gate 	 * Copy saved registers from user stack.
8187c478bd9Sstevel@tonic-gate 	 */
8197c478bd9Sstevel@tonic-gate 	bcopy(grp, &rp->r_gs, sizeof (gregset_t));
8207c478bd9Sstevel@tonic-gate 
821843e1988Sjohnlev 	rp->r_cs = fix_segreg(rp->r_cs, IS_CS, datamodel);
822843e1988Sjohnlev 	rp->r_ss = fix_segreg(rp->r_ss, IS_NOT_CS, datamodel);
823843e1988Sjohnlev 	rp->r_ds = fix_segreg(rp->r_ds, IS_NOT_CS, datamodel);
824843e1988Sjohnlev 	rp->r_es = fix_segreg(rp->r_es, IS_NOT_CS, datamodel);
825843e1988Sjohnlev 	rp->r_fs = fix_segreg(rp->r_fs, IS_NOT_CS, datamodel);
826843e1988Sjohnlev 	rp->r_gs = fix_segreg(rp->r_gs, IS_NOT_CS, datamodel);
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate #endif	/* __i386 */
8297c478bd9Sstevel@tonic-gate }
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate /*
8327c478bd9Sstevel@tonic-gate  * Determine whether eip is likely to have an interrupt frame
8337c478bd9Sstevel@tonic-gate  * on the stack.  We do this by comparing the address to the
8347c478bd9Sstevel@tonic-gate  * range of addresses spanned by several well-known routines.
8357c478bd9Sstevel@tonic-gate  */
8367c478bd9Sstevel@tonic-gate extern void _interrupt();
8377c478bd9Sstevel@tonic-gate extern void _allsyscalls();
8387c478bd9Sstevel@tonic-gate extern void _cmntrap();
8397c478bd9Sstevel@tonic-gate extern void fakesoftint();
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate extern size_t _interrupt_size;
8427c478bd9Sstevel@tonic-gate extern size_t _allsyscalls_size;
8437c478bd9Sstevel@tonic-gate extern size_t _cmntrap_size;
8447c478bd9Sstevel@tonic-gate extern size_t _fakesoftint_size;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate  * Get a pc-only stacktrace.  Used for kmem_alloc() buffer ownership tracking.
8487c478bd9Sstevel@tonic-gate  * Returns MIN(current stack depth, pcstack_limit).
8497c478bd9Sstevel@tonic-gate  */
8507c478bd9Sstevel@tonic-gate int
getpcstack(pc_t * pcstack,int pcstack_limit)8517c478bd9Sstevel@tonic-gate getpcstack(pc_t *pcstack, int pcstack_limit)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	struct frame *fp = (struct frame *)getfp();
8547c478bd9Sstevel@tonic-gate 	struct frame *nextfp, *minfp, *stacktop;
8557c478bd9Sstevel@tonic-gate 	int depth = 0;
8567c478bd9Sstevel@tonic-gate 	int on_intr;
8577c478bd9Sstevel@tonic-gate 	uintptr_t pc;
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
8607c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
8617c478bd9Sstevel@tonic-gate 	else
8627c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
8637c478bd9Sstevel@tonic-gate 	minfp = fp;
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	pc = ((struct regs *)fp)->r_pc;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	while (depth < pcstack_limit) {
8687c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)fp->fr_savfp;
8697c478bd9Sstevel@tonic-gate 		pc = fp->fr_savpc;
8707c478bd9Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
8717c478bd9Sstevel@tonic-gate 			if (on_intr) {
8727c478bd9Sstevel@tonic-gate 				/*
8737c478bd9Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
8747c478bd9Sstevel@tonic-gate 				 */
8757c478bd9Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
8767c478bd9Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
8777c478bd9Sstevel@tonic-gate 				on_intr = 0;
8787c478bd9Sstevel@tonic-gate 				continue;
8797c478bd9Sstevel@tonic-gate 			}
8807c478bd9Sstevel@tonic-gate 			break;
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 		pcstack[depth++] = (pc_t)pc;
8837c478bd9Sstevel@tonic-gate 		fp = nextfp;
8847c478bd9Sstevel@tonic-gate 		minfp = fp;
8857c478bd9Sstevel@tonic-gate 	}
8867c478bd9Sstevel@tonic-gate 	return (depth);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate /*
8907c478bd9Sstevel@tonic-gate  * The following ELF header fields are defined as processor-specific
8917c478bd9Sstevel@tonic-gate  * in the V8 ABI:
8927c478bd9Sstevel@tonic-gate  *
8937c478bd9Sstevel@tonic-gate  *	e_ident[EI_DATA]	encoding of the processor-specific
8947c478bd9Sstevel@tonic-gate  *				data in the object file
8957c478bd9Sstevel@tonic-gate  *	e_machine		processor identification
8967c478bd9Sstevel@tonic-gate  *	e_flags			processor-specific flags associated
8977c478bd9Sstevel@tonic-gate  *				with the file
8987c478bd9Sstevel@tonic-gate  */
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate /*
9017c478bd9Sstevel@tonic-gate  * The value of at_flags reflects a platform's cpu module support.
9027c478bd9Sstevel@tonic-gate  * at_flags is used to check for allowing a binary to execute and
9037c478bd9Sstevel@tonic-gate  * is passed as the value of the AT_FLAGS auxiliary vector.
9047c478bd9Sstevel@tonic-gate  */
9057c478bd9Sstevel@tonic-gate int at_flags = 0;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate /*
9087c478bd9Sstevel@tonic-gate  * Check the processor-specific fields of an ELF header.
9097c478bd9Sstevel@tonic-gate  *
9107c478bd9Sstevel@tonic-gate  * returns 1 if the fields are valid, 0 otherwise
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate /*ARGSUSED2*/
9137c478bd9Sstevel@tonic-gate int
elfheadcheck(unsigned char e_data,Elf32_Half e_machine,Elf32_Word e_flags)9147c478bd9Sstevel@tonic-gate elfheadcheck(
9157c478bd9Sstevel@tonic-gate 	unsigned char e_data,
9167c478bd9Sstevel@tonic-gate 	Elf32_Half e_machine,
9177c478bd9Sstevel@tonic-gate 	Elf32_Word e_flags)
9187c478bd9Sstevel@tonic-gate {
9197c478bd9Sstevel@tonic-gate 	if (e_data != ELFDATA2LSB)
9207c478bd9Sstevel@tonic-gate 		return (0);
9217c478bd9Sstevel@tonic-gate #if defined(__amd64)
9227c478bd9Sstevel@tonic-gate 	if (e_machine == EM_AMD64)
9237c478bd9Sstevel@tonic-gate 		return (1);
9247c478bd9Sstevel@tonic-gate #endif
9257c478bd9Sstevel@tonic-gate 	return (e_machine == EM_386);
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate uint_t auxv_hwcap_include = 0;	/* patch to enable unrecognized features */
929ebb8ac07SRobert Mustacchi uint_t auxv_hwcap_include_2 = 0;	/* second word */
9307c478bd9Sstevel@tonic-gate uint_t auxv_hwcap_exclude = 0;	/* patch for broken cpus, debugging */
931ebb8ac07SRobert Mustacchi uint_t auxv_hwcap_exclude_2 = 0;	/* second word */
9327c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
9337c478bd9Sstevel@tonic-gate uint_t auxv_hwcap32_include = 0;	/* ditto for 32-bit apps */
934ebb8ac07SRobert Mustacchi uint_t auxv_hwcap32_include_2 = 0;	/* ditto for 32-bit apps */
9357c478bd9Sstevel@tonic-gate uint_t auxv_hwcap32_exclude = 0;	/* ditto for 32-bit apps */
936ebb8ac07SRobert Mustacchi uint_t auxv_hwcap32_exclude_2 = 0;	/* ditto for 32-bit apps */
9377c478bd9Sstevel@tonic-gate #endif
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate /*
9407c478bd9Sstevel@tonic-gate  * Gather information about the processor and place it into auxv_hwcap
9417c478bd9Sstevel@tonic-gate  * so that it can be exported to the linker via the aux vector.
9427c478bd9Sstevel@tonic-gate  *
9437c478bd9Sstevel@tonic-gate  * We use this seemingly complicated mechanism so that we can ensure
9447c478bd9Sstevel@tonic-gate  * that /etc/system can be used to override what the system can or
9457c478bd9Sstevel@tonic-gate  * cannot discover for itself.
9467c478bd9Sstevel@tonic-gate  */
9477c478bd9Sstevel@tonic-gate void
bind_hwcap(void)9487c478bd9Sstevel@tonic-gate bind_hwcap(void)
9497c478bd9Sstevel@tonic-gate {
950ebb8ac07SRobert Mustacchi 	uint_t cpu_hwcap_flags[2];
951ebb8ac07SRobert Mustacchi 	cpuid_pass4(NULL, cpu_hwcap_flags);
9527c478bd9Sstevel@tonic-gate 
953ebb8ac07SRobert Mustacchi 	auxv_hwcap = (auxv_hwcap_include | cpu_hwcap_flags[0]) &
9547c478bd9Sstevel@tonic-gate 	    ~auxv_hwcap_exclude;
955ebb8ac07SRobert Mustacchi 	auxv_hwcap_2 = (auxv_hwcap_include_2 | cpu_hwcap_flags[1]) &
956ebb8ac07SRobert Mustacchi 	    ~auxv_hwcap_exclude_2;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate #if defined(__amd64)
9597c478bd9Sstevel@tonic-gate 	/*
9607c478bd9Sstevel@tonic-gate 	 * On AMD processors, sysenter just doesn't work at all
9617c478bd9Sstevel@tonic-gate 	 * when the kernel is in long mode.  On IA-32e processors
9627c478bd9Sstevel@tonic-gate 	 * it does, but there's no real point in all the alternate
9637c478bd9Sstevel@tonic-gate 	 * mechanism when syscall works on both.
9647c478bd9Sstevel@tonic-gate 	 *
9657c478bd9Sstevel@tonic-gate 	 * Besides, the kernel's sysenter handler is expecting a
9667c478bd9Sstevel@tonic-gate 	 * 32-bit lwp ...
9677c478bd9Sstevel@tonic-gate 	 */
9687c478bd9Sstevel@tonic-gate 	auxv_hwcap &= ~AV_386_SEP;
969ae115bc7Smrj #else
970ae115bc7Smrj 	/*
971ae115bc7Smrj 	 * 32-bit processes can -always- use the lahf/sahf instructions
972ae115bc7Smrj 	 */
973ae115bc7Smrj 	auxv_hwcap |= AV_386_AHF;
9747c478bd9Sstevel@tonic-gate #endif
9757c478bd9Sstevel@tonic-gate 
976ebb8ac07SRobert Mustacchi 	if (auxv_hwcap_include || auxv_hwcap_exclude || auxv_hwcap_include_2 ||
977ebb8ac07SRobert Mustacchi 	    auxv_hwcap_exclude_2) {
978a3853ec4SRobert Mustacchi 		/*
979a3853ec4SRobert Mustacchi 		 * The below assignment is regrettably required to get lint
980a3853ec4SRobert Mustacchi 		 * to accept the validity of our format string.  The format
981a3853ec4SRobert Mustacchi 		 * string is in fact valid, but whatever intelligence in lint
982a3853ec4SRobert Mustacchi 		 * understands the cmn_err()-specific %b appears to have an
983a3853ec4SRobert Mustacchi 		 * off-by-one error:  it (mistakenly) complains about bit
984a3853ec4SRobert Mustacchi 		 * number 32 (even though this is explicitly permitted).
985a3853ec4SRobert Mustacchi 		 * Normally, one would will away such warnings with a "LINTED"
986a3853ec4SRobert Mustacchi 		 * directive, but for reasons unclear and unknown, lint
987a3853ec4SRobert Mustacchi 		 * refuses to be assuaged in this case.  Fortunately, lint
988a3853ec4SRobert Mustacchi 		 * doesn't pretend to have solved the Halting Problem --
989a3853ec4SRobert Mustacchi 		 * and as soon as the format string is programmatic, it
990a3853ec4SRobert Mustacchi 		 * knows enough to shut up.
991a3853ec4SRobert Mustacchi 		 */
992ebb8ac07SRobert Mustacchi 		char *fmt = "?user ABI extensions: %b\n";
993a3853ec4SRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap, FMT_AV_386);
994ebb8ac07SRobert Mustacchi 		fmt = "?user ABI extensions (word 2): %b\n";
995ebb8ac07SRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap_2, FMT_AV_386_2);
996a3853ec4SRobert Mustacchi 	}
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
999ebb8ac07SRobert Mustacchi 	auxv_hwcap32 = (auxv_hwcap32_include | cpu_hwcap_flags[0]) &
10007c478bd9Sstevel@tonic-gate 	    ~auxv_hwcap32_exclude;
1001ebb8ac07SRobert Mustacchi 	auxv_hwcap32_2 = (auxv_hwcap32_include_2 | cpu_hwcap_flags[1]) &
1002ebb8ac07SRobert Mustacchi 	    ~auxv_hwcap32_exclude_2;
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate #if defined(__amd64)
10057c478bd9Sstevel@tonic-gate 	/*
10067c478bd9Sstevel@tonic-gate 	 * If this is an amd64 architecture machine from Intel, then
10077c478bd9Sstevel@tonic-gate 	 * syscall -doesn't- work in compatibility mode, only sysenter does.
10087c478bd9Sstevel@tonic-gate 	 *
10097c478bd9Sstevel@tonic-gate 	 * Sigh.
10107c478bd9Sstevel@tonic-gate 	 */
10117c478bd9Sstevel@tonic-gate 	if (!cpuid_syscall32_insn(NULL))
10127c478bd9Sstevel@tonic-gate 		auxv_hwcap32 &= ~AV_386_AMD_SYSC;
1013ae115bc7Smrj 
1014ae115bc7Smrj 	/*
1015ae115bc7Smrj 	 * 32-bit processes can -always- use the lahf/sahf instructions
1016ae115bc7Smrj 	 */
1017ae115bc7Smrj 	auxv_hwcap32 |= AV_386_AHF;
10187c478bd9Sstevel@tonic-gate #endif
10197c478bd9Sstevel@tonic-gate 
1020ebb8ac07SRobert Mustacchi 	if (auxv_hwcap32_include || auxv_hwcap32_exclude ||
1021ebb8ac07SRobert Mustacchi 	    auxv_hwcap32_include_2 || auxv_hwcap32_exclude_2) {
1022a3853ec4SRobert Mustacchi 		/*
1023a3853ec4SRobert Mustacchi 		 * See the block comment in the cmn_err() of auxv_hwcap, above.
1024a3853ec4SRobert Mustacchi 		 */
1025ebb8ac07SRobert Mustacchi 		char *fmt = "?32-bit user ABI extensions: %b\n";
1026a3853ec4SRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap32, FMT_AV_386);
1027ebb8ac07SRobert Mustacchi 		fmt = "?32-bit user ABI extensions (word 2): %b\n";
1028ebb8ac07SRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap32_2, FMT_AV_386_2);
1029a3853ec4SRobert Mustacchi 	}
10307c478bd9Sstevel@tonic-gate #endif
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate /*
10347c478bd9Sstevel@tonic-gate  *	sync_icache() - this is called
10357c478bd9Sstevel@tonic-gate  *	in proc/fs/prusrio.c. x86 has an unified cache and therefore
10367c478bd9Sstevel@tonic-gate  *	this is a nop.
10377c478bd9Sstevel@tonic-gate  */
10387c478bd9Sstevel@tonic-gate /* ARGSUSED */
10397c478bd9Sstevel@tonic-gate void
sync_icache(caddr_t addr,uint_t len)10407c478bd9Sstevel@tonic-gate sync_icache(caddr_t addr, uint_t len)
10417c478bd9Sstevel@tonic-gate {
10427c478bd9Sstevel@tonic-gate 	/* Do nothing for now */
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10467c478bd9Sstevel@tonic-gate void
sync_data_memory(caddr_t va,size_t len)10477c478bd9Sstevel@tonic-gate sync_data_memory(caddr_t va, size_t len)
10487c478bd9Sstevel@tonic-gate {
10497c478bd9Sstevel@tonic-gate 	/* Not implemented for this platform */
10507c478bd9Sstevel@tonic-gate }
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate int
__ipltospl(int ipl)10537c478bd9Sstevel@tonic-gate __ipltospl(int ipl)
10547c478bd9Sstevel@tonic-gate {
10557c478bd9Sstevel@tonic-gate 	return (ipltospl(ipl));
10567c478bd9Sstevel@tonic-gate }
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate /*
10597c478bd9Sstevel@tonic-gate  * The panic code invokes panic_saveregs() to record the contents of a
10607c478bd9Sstevel@tonic-gate  * regs structure into the specified panic_data structure for debuggers.
10617c478bd9Sstevel@tonic-gate  */
10627c478bd9Sstevel@tonic-gate void
panic_saveregs(panic_data_t * pdp,struct regs * rp)10637c478bd9Sstevel@tonic-gate panic_saveregs(panic_data_t *pdp, struct regs *rp)
10647c478bd9Sstevel@tonic-gate {
10657c478bd9Sstevel@tonic-gate 	panic_nv_t *pnv = PANICNVGET(pdp);
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	struct cregs	creg;
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	getcregs(&creg);
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate #if defined(__amd64)
10727c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rdi", rp->r_rdi);
10737c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rsi", rp->r_rsi);
10747c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rdx", rp->r_rdx);
10757c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rcx", rp->r_rcx);
10767c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r8", rp->r_r8);
10777c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r9", rp->r_r9);
10787c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rax", rp->r_rax);
10797c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rbx", rp->r_rbx);
10807c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rbp", rp->r_rbp);
10817c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r10", rp->r_r10);
10827c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r11", rp->r_r11);
10837c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r12", rp->r_r12);
10847c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r13", rp->r_r13);
10857c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r14", rp->r_r14);
10867c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "r15", rp->r_r15);
1087ae115bc7Smrj 	PANICNVADD(pnv, "fsbase", rdmsr(MSR_AMD_FSBASE));
1088ae115bc7Smrj 	PANICNVADD(pnv, "gsbase", rdmsr(MSR_AMD_GSBASE));
10897c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "ds", rp->r_ds);
10907c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "es", rp->r_es);
10917c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "fs", rp->r_fs);
10927c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "gs", rp->r_gs);
10937c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "trapno", rp->r_trapno);
10947c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "err", rp->r_err);
10957c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rip", rp->r_rip);
10967c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "cs", rp->r_cs);
10977c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rflags", rp->r_rfl);
10987c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "rsp", rp->r_rsp);
10997c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "ss", rp->r_ss);
11007c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "gdt_hi", (uint64_t)(creg.cr_gdt._l[3]));
11017c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "gdt_lo", (uint64_t)(creg.cr_gdt._l[0]));
11027c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "idt_hi", (uint64_t)(creg.cr_idt._l[3]));
11037c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "idt_lo", (uint64_t)(creg.cr_idt._l[0]));
11047c478bd9Sstevel@tonic-gate #elif defined(__i386)
11057c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "gs", (uint32_t)rp->r_gs);
11067c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "fs", (uint32_t)rp->r_fs);
11077c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "es", (uint32_t)rp->r_es);
11087c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "ds", (uint32_t)rp->r_ds);
11097c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "edi", (uint32_t)rp->r_edi);
11107c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "esi", (uint32_t)rp->r_esi);
11117c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "ebp", (uint32_t)rp->r_ebp);
11127c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "esp", (uint32_t)rp->r_esp);
11137c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "ebx", (uint32_t)rp->r_ebx);
11147c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "edx", (uint32_t)rp->r_edx);
11157c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "ecx", (uint32_t)rp->r_ecx);
11167c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "eax", (uint32_t)rp->r_eax);
11177c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "trapno", (uint32_t)rp->r_trapno);
11187c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "err", (uint32_t)rp->r_err);
11197c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "eip", (uint32_t)rp->r_eip);
11207c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "cs", (uint32_t)rp->r_cs);
11217c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "eflags", (uint32_t)rp->r_efl);
11227c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "uesp", (uint32_t)rp->r_uesp);
11237c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "ss", (uint32_t)rp->r_ss);
11247c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "gdt", creg.cr_gdt);
11257c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "idt", creg.cr_idt);
11267c478bd9Sstevel@tonic-gate #endif	/* __i386 */
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "ldt", creg.cr_ldt);
11297c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "task", creg.cr_task);
11307c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "cr0", creg.cr_cr0);
11317c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "cr2", creg.cr_cr2);
11327c478bd9Sstevel@tonic-gate 	PANICNVADD(pnv, "cr3", creg.cr_cr3);
11337c478bd9Sstevel@tonic-gate 	if (creg.cr_cr4)
11347c478bd9Sstevel@tonic-gate 		PANICNVADD(pnv, "cr4", creg.cr_cr4);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate 	PANICNVSET(pdp, pnv);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate #define	TR_ARG_MAX 6	/* Max args to print, same as SPARC */
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate #if !defined(__amd64)
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate /*
11447c478bd9Sstevel@tonic-gate  * Given a return address (%eip), determine the likely number of arguments
11457c478bd9Sstevel@tonic-gate  * that were pushed on the stack prior to its execution.  We do this by
11467c478bd9Sstevel@tonic-gate  * expecting that a typical call sequence consists of pushing arguments on
11477c478bd9Sstevel@tonic-gate  * the stack, executing a call instruction, and then performing an add
11487c478bd9Sstevel@tonic-gate  * on %esp to restore it to the value prior to pushing the arguments for
11497c478bd9Sstevel@tonic-gate  * the call.  We attempt to detect such an add, and divide the addend
11507c478bd9Sstevel@tonic-gate  * by the size of a word to determine the number of pushed arguments.
11517c478bd9Sstevel@tonic-gate  *
11527c478bd9Sstevel@tonic-gate  * If we do not find such an add, we punt and return TR_ARG_MAX. It is not
11537c478bd9Sstevel@tonic-gate  * possible to reliably determine if a function took no arguments (i.e. was
11547c478bd9Sstevel@tonic-gate  * void) because assembler routines do not reliably perform an add on %esp
11557c478bd9Sstevel@tonic-gate  * immediately upon returning (eg. _sys_call()), so returning TR_ARG_MAX is
11567c478bd9Sstevel@tonic-gate  * safer than returning 0.
11577c478bd9Sstevel@tonic-gate  */
11587c478bd9Sstevel@tonic-gate static ulong_t
argcount(uintptr_t eip)11597c478bd9Sstevel@tonic-gate argcount(uintptr_t eip)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate 	const uint8_t *ins = (const uint8_t *)eip;
11627c478bd9Sstevel@tonic-gate 	ulong_t n;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 	enum {
11657c478bd9Sstevel@tonic-gate 		M_MODRM_ESP = 0xc4,	/* Mod/RM byte indicates %esp */
11667c478bd9Sstevel@tonic-gate 		M_ADD_IMM32 = 0x81,	/* ADD imm32 to r/m32 */
11677c478bd9Sstevel@tonic-gate 		M_ADD_IMM8  = 0x83	/* ADD imm8 to r/m32 */
11687c478bd9Sstevel@tonic-gate 	};
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 	if (eip < KERNELBASE || ins[1] != M_MODRM_ESP)
11717c478bd9Sstevel@tonic-gate 		return (TR_ARG_MAX);
11727c478bd9Sstevel@tonic-gate 
11737c478bd9Sstevel@tonic-gate 	switch (ins[0]) {
11747c478bd9Sstevel@tonic-gate 	case M_ADD_IMM32:
11757c478bd9Sstevel@tonic-gate 		n = ins[2] + (ins[3] << 8) + (ins[4] << 16) + (ins[5] << 24);
11767c478bd9Sstevel@tonic-gate 		break;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 	case M_ADD_IMM8:
11797c478bd9Sstevel@tonic-gate 		n = ins[2];
11807c478bd9Sstevel@tonic-gate 		break;
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	default:
11837c478bd9Sstevel@tonic-gate 		return (TR_ARG_MAX);
11847c478bd9Sstevel@tonic-gate 	}
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	n /= sizeof (long);
11877c478bd9Sstevel@tonic-gate 	return (MIN(n, TR_ARG_MAX));
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate #endif	/* !__amd64 */
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate /*
11937c478bd9Sstevel@tonic-gate  * Print a stack backtrace using the specified frame pointer.  We delay two
1194f6e214c7SGavin Maltby  * seconds before continuing, unless this is the panic traceback.
1195f6e214c7SGavin Maltby  * If we are in the process of panicking, we also attempt to write the
1196f6e214c7SGavin Maltby  * stack backtrace to a staticly assigned buffer, to allow the panic
1197f6e214c7SGavin Maltby  * code to find it and write it in to uncompressed pages within the
1198f6e214c7SGavin Maltby  * system crash dump.
1199f6e214c7SGavin Maltby  * Note that the frame for the starting stack pointer value is omitted because
12007c478bd9Sstevel@tonic-gate  * the corresponding %eip is not known.
12017c478bd9Sstevel@tonic-gate  */
1202f6e214c7SGavin Maltby 
1203f6e214c7SGavin Maltby extern char *dump_stack_scratch;
1204f6e214c7SGavin Maltby 
12057c478bd9Sstevel@tonic-gate #if defined(__amd64)
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate void
traceback(caddr_t fpreg)12087c478bd9Sstevel@tonic-gate traceback(caddr_t fpreg)
12097c478bd9Sstevel@tonic-gate {
12107c478bd9Sstevel@tonic-gate 	struct frame	*fp = (struct frame *)fpreg;
12117c478bd9Sstevel@tonic-gate 	struct frame	*nextfp;
12127c478bd9Sstevel@tonic-gate 	uintptr_t	pc, nextpc;
12137c478bd9Sstevel@tonic-gate 	ulong_t		off;
12147c478bd9Sstevel@tonic-gate 	char		args[TR_ARG_MAX * 2 + 16], *sym;
1215f6e214c7SGavin Maltby 	uint_t	  offset = 0;
1216f6e214c7SGavin Maltby 	uint_t	  next_offset = 0;
1217f6e214c7SGavin Maltby 	char	    stack_buffer[1024];
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 	if (!panicstr)
12207c478bd9Sstevel@tonic-gate 		printf("traceback: %%fp = %p\n", (void *)fp);
12217c478bd9Sstevel@tonic-gate 
1222f6e214c7SGavin Maltby 	if (panicstr && !dump_stack_scratch) {
1223f6e214c7SGavin Maltby 		printf("Warning - stack not written to the dump buffer\n");
1224f6e214c7SGavin Maltby 	}
1225f6e214c7SGavin Maltby 
1226843e1988Sjohnlev 	fp = (struct frame *)plat_traceback(fpreg);
12277c478bd9Sstevel@tonic-gate 	if ((uintptr_t)fp < KERNELBASE)
12287c478bd9Sstevel@tonic-gate 		goto out;
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 	pc = fp->fr_savpc;
12317c478bd9Sstevel@tonic-gate 	fp = (struct frame *)fp->fr_savfp;
12327c478bd9Sstevel@tonic-gate 
12337c478bd9Sstevel@tonic-gate 	while ((uintptr_t)fp >= KERNELBASE) {
12347c478bd9Sstevel@tonic-gate 		/*
12357c478bd9Sstevel@tonic-gate 		 * XX64 Until port is complete tolerate 8-byte aligned
12367c478bd9Sstevel@tonic-gate 		 * frame pointers but flag with a warning so they can
12377c478bd9Sstevel@tonic-gate 		 * be fixed.
12387c478bd9Sstevel@tonic-gate 		 */
12397c478bd9Sstevel@tonic-gate 		if (((uintptr_t)fp & (STACK_ALIGN - 1)) != 0) {
12407c478bd9Sstevel@tonic-gate 			if (((uintptr_t)fp & (8 - 1)) == 0) {
12417c478bd9Sstevel@tonic-gate 				printf("  >> warning! 8-byte"
12427c478bd9Sstevel@tonic-gate 				    " aligned %%fp = %p\n", (void *)fp);
12437c478bd9Sstevel@tonic-gate 			} else {
12447c478bd9Sstevel@tonic-gate 				printf(
12457c478bd9Sstevel@tonic-gate 				    "  >> mis-aligned %%fp = %p\n", (void *)fp);
12467c478bd9Sstevel@tonic-gate 				break;
12477c478bd9Sstevel@tonic-gate 			}
12487c478bd9Sstevel@tonic-gate 		}
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 		args[0] = '\0';
12517c478bd9Sstevel@tonic-gate 		nextpc = (uintptr_t)fp->fr_savpc;
12527c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)fp->fr_savfp;
12537c478bd9Sstevel@tonic-gate 		if ((sym = kobj_getsymname(pc, &off)) != NULL) {
12547c478bd9Sstevel@tonic-gate 			printf("%016lx %s:%s+%lx (%s)\n", (uintptr_t)fp,
12557c478bd9Sstevel@tonic-gate 			    mod_containing_pc((caddr_t)pc), sym, off, args);
1256f6e214c7SGavin Maltby 			(void) snprintf(stack_buffer, sizeof (stack_buffer),
1257f6e214c7SGavin Maltby 			    "%s:%s+%lx (%s) | ",
1258f6e214c7SGavin Maltby 			    mod_containing_pc((caddr_t)pc), sym, off, args);
12597c478bd9Sstevel@tonic-gate 		} else {
12607c478bd9Sstevel@tonic-gate 			printf("%016lx %lx (%s)\n",
12617c478bd9Sstevel@tonic-gate 			    (uintptr_t)fp, pc, args);
1262f6e214c7SGavin Maltby 			(void) snprintf(stack_buffer, sizeof (stack_buffer),
1263f6e214c7SGavin Maltby 			    "%lx (%s) | ", pc, args);
1264f6e214c7SGavin Maltby 		}
1265f6e214c7SGavin Maltby 
1266f6e214c7SGavin Maltby 		if (panicstr && dump_stack_scratch) {
1267f6e214c7SGavin Maltby 			next_offset = offset + strlen(stack_buffer);
1268f6e214c7SGavin Maltby 			if (next_offset < STACK_BUF_SIZE) {
1269f6e214c7SGavin Maltby 				bcopy(stack_buffer, dump_stack_scratch + offset,
1270f6e214c7SGavin Maltby 				    strlen(stack_buffer));
1271f6e214c7SGavin Maltby 				offset = next_offset;
1272f6e214c7SGavin Maltby 			} else {
1273f6e214c7SGavin Maltby 				/*
1274f6e214c7SGavin Maltby 				 * In attempting to save the panic stack
1275f6e214c7SGavin Maltby 				 * to the dumpbuf we have overflowed that area.
1276f6e214c7SGavin Maltby 				 * Print a warning and continue to printf the
1277f6e214c7SGavin Maltby 				 * stack to the msgbuf
1278f6e214c7SGavin Maltby 				 */
1279f6e214c7SGavin Maltby 				printf("Warning: stack in the dump buffer"
1280f6e214c7SGavin Maltby 				    " may be incomplete\n");
1281f6e214c7SGavin Maltby 				offset = next_offset;
1282f6e214c7SGavin Maltby 			}
12837c478bd9Sstevel@tonic-gate 		}
12847c478bd9Sstevel@tonic-gate 
12857c478bd9Sstevel@tonic-gate 		pc = nextpc;
12867c478bd9Sstevel@tonic-gate 		fp = nextfp;
12877c478bd9Sstevel@tonic-gate 	}
12887c478bd9Sstevel@tonic-gate out:
12897c478bd9Sstevel@tonic-gate 	if (!panicstr) {
12907c478bd9Sstevel@tonic-gate 		printf("end of traceback\n");
12917c478bd9Sstevel@tonic-gate 		DELAY(2 * MICROSEC);
1292f6e214c7SGavin Maltby 	} else if (dump_stack_scratch) {
1293f6e214c7SGavin Maltby 		dump_stack_scratch[offset] = '\0';
12947c478bd9Sstevel@tonic-gate 	}
12957c478bd9Sstevel@tonic-gate }
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate #elif defined(__i386)
12987c478bd9Sstevel@tonic-gate 
12997c478bd9Sstevel@tonic-gate void
traceback(caddr_t fpreg)13007c478bd9Sstevel@tonic-gate traceback(caddr_t fpreg)
13017c478bd9Sstevel@tonic-gate {
13027c478bd9Sstevel@tonic-gate 	struct frame *fp = (struct frame *)fpreg;
13037c478bd9Sstevel@tonic-gate 	struct frame *nextfp, *minfp, *stacktop;
13047c478bd9Sstevel@tonic-gate 	uintptr_t pc, nextpc;
1305f6e214c7SGavin Maltby 	uint_t	  offset = 0;
1306f6e214c7SGavin Maltby 	uint_t	  next_offset = 0;
1307f6e214c7SGavin Maltby 	char	    stack_buffer[1024];
13087c478bd9Sstevel@tonic-gate 
13097c478bd9Sstevel@tonic-gate 	cpu_t *cpu;
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate 	/*
13127c478bd9Sstevel@tonic-gate 	 * args[] holds TR_ARG_MAX hex long args, plus ", " or '\0'.
13137c478bd9Sstevel@tonic-gate 	 */
13147c478bd9Sstevel@tonic-gate 	char args[TR_ARG_MAX * 2 + 8], *p;
13157c478bd9Sstevel@tonic-gate 
13167c478bd9Sstevel@tonic-gate 	int on_intr;
13177c478bd9Sstevel@tonic-gate 	ulong_t off;
13187c478bd9Sstevel@tonic-gate 	char *sym;
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	if (!panicstr)
13217c478bd9Sstevel@tonic-gate 		printf("traceback: %%fp = %p\n", (void *)fp);
13227c478bd9Sstevel@tonic-gate 
1323f6e214c7SGavin Maltby 	if (panicstr && !dump_stack_scratch) {
1324f6e214c7SGavin Maltby 		printf("Warning - stack not written to the dumpbuf\n");
1325f6e214c7SGavin Maltby 	}
1326f6e214c7SGavin Maltby 
13277c478bd9Sstevel@tonic-gate 	/*
13287c478bd9Sstevel@tonic-gate 	 * If we are panicking, all high-level interrupt information in
13297c478bd9Sstevel@tonic-gate 	 * CPU was overwritten.  panic_cpu has the correct values.
13307c478bd9Sstevel@tonic-gate 	 */
13317c478bd9Sstevel@tonic-gate 	kpreempt_disable();			/* prevent migration */
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 	cpu = (panicstr && CPU->cpu_id == panic_cpu.cpu_id)? &panic_cpu : CPU;
13347c478bd9Sstevel@tonic-gate 
13357c478bd9Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(cpu)) != 0)
13367c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)(cpu->cpu_intr_stack + SA(MINFRAME));
13377c478bd9Sstevel@tonic-gate 	else
13387c478bd9Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	kpreempt_enable();
13417c478bd9Sstevel@tonic-gate 
1342843e1988Sjohnlev 	fp = (struct frame *)plat_traceback(fpreg);
13437c478bd9Sstevel@tonic-gate 	if ((uintptr_t)fp < KERNELBASE)
13447c478bd9Sstevel@tonic-gate 		goto out;
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate 	minfp = fp;	/* Baseline minimum frame pointer */
13477c478bd9Sstevel@tonic-gate 	pc = fp->fr_savpc;
13487c478bd9Sstevel@tonic-gate 	fp = (struct frame *)fp->fr_savfp;
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	while ((uintptr_t)fp >= KERNELBASE) {
13517c478bd9Sstevel@tonic-gate 		ulong_t argc;
13527c478bd9Sstevel@tonic-gate 		long *argv;
13537c478bd9Sstevel@tonic-gate 
13547c478bd9Sstevel@tonic-gate 		if (fp <= minfp || fp >= stacktop) {
13557c478bd9Sstevel@tonic-gate 			if (on_intr) {
13567c478bd9Sstevel@tonic-gate 				/*
13577c478bd9Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
13587c478bd9Sstevel@tonic-gate 				 */
13597c478bd9Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
13607c478bd9Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
13617c478bd9Sstevel@tonic-gate 				on_intr = 0;
13627c478bd9Sstevel@tonic-gate 				continue;
13637c478bd9Sstevel@tonic-gate 			}
13647c478bd9Sstevel@tonic-gate 			break; /* we're outside of the expected range */
13657c478bd9Sstevel@tonic-gate 		}
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 		if ((uintptr_t)fp & (STACK_ALIGN - 1)) {
13687c478bd9Sstevel@tonic-gate 			printf("  >> mis-aligned %%fp = %p\n", (void *)fp);
13697c478bd9Sstevel@tonic-gate 			break;
13707c478bd9Sstevel@tonic-gate 		}
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 		nextpc = fp->fr_savpc;
13737c478bd9Sstevel@tonic-gate 		nextfp = (struct frame *)fp->fr_savfp;
13747c478bd9Sstevel@tonic-gate 		argc = argcount(nextpc);
13757c478bd9Sstevel@tonic-gate 		argv = (long *)((char *)fp + sizeof (struct frame));
13767c478bd9Sstevel@tonic-gate 
13777c478bd9Sstevel@tonic-gate 		args[0] = '\0';
13787c478bd9Sstevel@tonic-gate 		p = args;
13797c478bd9Sstevel@tonic-gate 		while (argc-- > 0 && argv < (long *)stacktop) {
13807c478bd9Sstevel@tonic-gate 			p += snprintf(p, args + sizeof (args) - p,
13817c478bd9Sstevel@tonic-gate 			    "%s%lx", (p == args) ? "" : ", ", *argv++);
13827c478bd9Sstevel@tonic-gate 		}
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 		if ((sym = kobj_getsymname(pc, &off)) != NULL) {
13857c478bd9Sstevel@tonic-gate 			printf("%08lx %s:%s+%lx (%s)\n", (uintptr_t)fp,
13867c478bd9Sstevel@tonic-gate 			    mod_containing_pc((caddr_t)pc), sym, off, args);
1387f6e214c7SGavin Maltby 			(void) snprintf(stack_buffer, sizeof (stack_buffer),
1388f6e214c7SGavin Maltby 			    "%s:%s+%lx (%s) | ",
1389f6e214c7SGavin Maltby 			    mod_containing_pc((caddr_t)pc), sym, off, args);
1390f6e214c7SGavin Maltby 
13917c478bd9Sstevel@tonic-gate 		} else {
13927c478bd9Sstevel@tonic-gate 			printf("%08lx %lx (%s)\n",
13937c478bd9Sstevel@tonic-gate 			    (uintptr_t)fp, pc, args);
1394f6e214c7SGavin Maltby 			(void) snprintf(stack_buffer, sizeof (stack_buffer),
1395f6e214c7SGavin Maltby 			    "%lx (%s) | ", pc, args);
1396f6e214c7SGavin Maltby 
1397f6e214c7SGavin Maltby 		}
1398f6e214c7SGavin Maltby 
1399f6e214c7SGavin Maltby 		if (panicstr && dump_stack_scratch) {
1400f6e214c7SGavin Maltby 			next_offset = offset + strlen(stack_buffer);
1401f6e214c7SGavin Maltby 			if (next_offset < STACK_BUF_SIZE) {
1402f6e214c7SGavin Maltby 				bcopy(stack_buffer, dump_stack_scratch + offset,
1403f6e214c7SGavin Maltby 				    strlen(stack_buffer));
1404f6e214c7SGavin Maltby 				offset = next_offset;
1405f6e214c7SGavin Maltby 			} else {
1406f6e214c7SGavin Maltby 				/*
1407f6e214c7SGavin Maltby 				 * In attempting to save the panic stack
1408f6e214c7SGavin Maltby 				 * to the dumpbuf we have overflowed that area.
1409f6e214c7SGavin Maltby 				 * Print a warning and continue to printf the
1410f6e214c7SGavin Maltby 				 * stack to the msgbuf
1411f6e214c7SGavin Maltby 				 */
1412f6e214c7SGavin Maltby 				printf("Warning: stack in the dumpbuf"
1413f6e214c7SGavin Maltby 				    " may be incomplete\n");
1414f6e214c7SGavin Maltby 				offset = next_offset;
1415f6e214c7SGavin Maltby 			}
14167c478bd9Sstevel@tonic-gate 		}
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 		minfp = fp;
14197c478bd9Sstevel@tonic-gate 		pc = nextpc;
14207c478bd9Sstevel@tonic-gate 		fp = nextfp;
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate out:
14237c478bd9Sstevel@tonic-gate 	if (!panicstr) {
14247c478bd9Sstevel@tonic-gate 		printf("end of traceback\n");
14257c478bd9Sstevel@tonic-gate 		DELAY(2 * MICROSEC);
1426f6e214c7SGavin Maltby 	} else if (dump_stack_scratch) {
1427f6e214c7SGavin Maltby 		dump_stack_scratch[offset] = '\0';
14287c478bd9Sstevel@tonic-gate 	}
1429f6e214c7SGavin Maltby 
14307c478bd9Sstevel@tonic-gate }
14317c478bd9Sstevel@tonic-gate 
14327c478bd9Sstevel@tonic-gate #endif	/* __i386 */
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate /*
14357c478bd9Sstevel@tonic-gate  * Generate a stack backtrace from a saved register set.
14367c478bd9Sstevel@tonic-gate  */
14377c478bd9Sstevel@tonic-gate void
traceregs(struct regs * rp)14387c478bd9Sstevel@tonic-gate traceregs(struct regs *rp)
14397c478bd9Sstevel@tonic-gate {
14407c478bd9Sstevel@tonic-gate 	traceback((caddr_t)rp->r_fp);
14417c478bd9Sstevel@tonic-gate }
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate void
exec_set_sp(size_t stksize)14447c478bd9Sstevel@tonic-gate exec_set_sp(size_t stksize)
14457c478bd9Sstevel@tonic-gate {
14467c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	lwptoregs(lwp)->r_sp = (uintptr_t)curproc->p_usrstack - stksize;
14497c478bd9Sstevel@tonic-gate }
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate hrtime_t
gethrtime_waitfree(void)14527c478bd9Sstevel@tonic-gate gethrtime_waitfree(void)
14537c478bd9Sstevel@tonic-gate {
14547c478bd9Sstevel@tonic-gate 	return (dtrace_gethrtime());
14557c478bd9Sstevel@tonic-gate }
14567c478bd9Sstevel@tonic-gate 
14577c478bd9Sstevel@tonic-gate hrtime_t
gethrtime(void)14587c478bd9Sstevel@tonic-gate gethrtime(void)
14597c478bd9Sstevel@tonic-gate {
14607c478bd9Sstevel@tonic-gate 	return (gethrtimef());
14617c478bd9Sstevel@tonic-gate }
14627c478bd9Sstevel@tonic-gate 
14637c478bd9Sstevel@tonic-gate hrtime_t
gethrtime_unscaled(void)14647c478bd9Sstevel@tonic-gate gethrtime_unscaled(void)
14657c478bd9Sstevel@tonic-gate {
14667c478bd9Sstevel@tonic-gate 	return (gethrtimeunscaledf());
14677c478bd9Sstevel@tonic-gate }
14687c478bd9Sstevel@tonic-gate 
14697c478bd9Sstevel@tonic-gate void
scalehrtime(hrtime_t * hrt)14707c478bd9Sstevel@tonic-gate scalehrtime(hrtime_t *hrt)
14717c478bd9Sstevel@tonic-gate {
14727c478bd9Sstevel@tonic-gate 	scalehrtimef(hrt);
14737c478bd9Sstevel@tonic-gate }
14747c478bd9Sstevel@tonic-gate 
1475113b131bSEric Saxe uint64_t
unscalehrtime(hrtime_t nsecs)1476113b131bSEric Saxe unscalehrtime(hrtime_t nsecs)
1477113b131bSEric Saxe {
1478113b131bSEric Saxe 	return (unscalehrtimef(nsecs));
1479113b131bSEric Saxe }
1480113b131bSEric Saxe 
14817c478bd9Sstevel@tonic-gate void
gethrestime(timespec_t * tp)14827c478bd9Sstevel@tonic-gate gethrestime(timespec_t *tp)
14837c478bd9Sstevel@tonic-gate {
14847c478bd9Sstevel@tonic-gate 	gethrestimef(tp);
14857c478bd9Sstevel@tonic-gate }
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate #if defined(__amd64)
14887c478bd9Sstevel@tonic-gate /*
14897c478bd9Sstevel@tonic-gate  * Part of the implementation of hres_tick(); this routine is
14907c478bd9Sstevel@tonic-gate  * easier in C than assembler .. called with the hres_lock held.
14917c478bd9Sstevel@tonic-gate  *
14927c478bd9Sstevel@tonic-gate  * XX64	Many of these timekeeping variables need to be extern'ed in a header
14937c478bd9Sstevel@tonic-gate  */
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate #include <sys/time.h>
14967c478bd9Sstevel@tonic-gate #include <sys/machlock.h>
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate extern int one_sec;
14997c478bd9Sstevel@tonic-gate extern int max_hres_adj;
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate void
__adj_hrestime(void)15027c478bd9Sstevel@tonic-gate __adj_hrestime(void)
15037c478bd9Sstevel@tonic-gate {
15047c478bd9Sstevel@tonic-gate 	long long adj;
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	if (hrestime_adj == 0)
15077c478bd9Sstevel@tonic-gate 		adj = 0;
15087c478bd9Sstevel@tonic-gate 	else if (hrestime_adj > 0) {
15097c478bd9Sstevel@tonic-gate 		if (hrestime_adj < max_hres_adj)
15107c478bd9Sstevel@tonic-gate 			adj = hrestime_adj;
15117c478bd9Sstevel@tonic-gate 		else
15127c478bd9Sstevel@tonic-gate 			adj = max_hres_adj;
15137c478bd9Sstevel@tonic-gate 	} else {
15147c478bd9Sstevel@tonic-gate 		if (hrestime_adj < -max_hres_adj)
15157c478bd9Sstevel@tonic-gate 			adj = -max_hres_adj;
15167c478bd9Sstevel@tonic-gate 		else
15177c478bd9Sstevel@tonic-gate 			adj = hrestime_adj;
15187c478bd9Sstevel@tonic-gate 	}
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	timedelta -= adj;
15217c478bd9Sstevel@tonic-gate 	hrestime_adj = timedelta;
15227c478bd9Sstevel@tonic-gate 	hrestime.tv_nsec += adj;
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 	while (hrestime.tv_nsec >= NANOSEC) {
15257c478bd9Sstevel@tonic-gate 		one_sec++;
15267c478bd9Sstevel@tonic-gate 		hrestime.tv_sec++;
15277c478bd9Sstevel@tonic-gate 		hrestime.tv_nsec -= NANOSEC;
15287c478bd9Sstevel@tonic-gate 	}
15297c478bd9Sstevel@tonic-gate }
15307c478bd9Sstevel@tonic-gate #endif
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate /*
15337c478bd9Sstevel@tonic-gate  * Wrapper functions to maintain backwards compability
15347c478bd9Sstevel@tonic-gate  */
15357c478bd9Sstevel@tonic-gate int
xcopyin(const void * uaddr,void * kaddr,size_t count)15367c478bd9Sstevel@tonic-gate xcopyin(const void *uaddr, void *kaddr, size_t count)
15377c478bd9Sstevel@tonic-gate {
15387c478bd9Sstevel@tonic-gate 	return (xcopyin_nta(uaddr, kaddr, count, UIO_COPY_CACHED));
15397c478bd9Sstevel@tonic-gate }
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate int
xcopyout(const void * kaddr,void * uaddr,size_t count)15427c478bd9Sstevel@tonic-gate xcopyout(const void *kaddr, void *uaddr, size_t count)
15437c478bd9Sstevel@tonic-gate {
15447c478bd9Sstevel@tonic-gate 	return (xcopyout_nta(kaddr, uaddr, count, UIO_COPY_CACHED));
15457c478bd9Sstevel@tonic-gate }
1546