xref: /onnv-gate/usr/src/uts/intel/ia32/os/archdep.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include <sys/param.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <sys/vmparam.h>
36*0Sstevel@tonic-gate #include <sys/systm.h>
37*0Sstevel@tonic-gate #include <sys/signal.h>
38*0Sstevel@tonic-gate #include <sys/stack.h>
39*0Sstevel@tonic-gate #include <sys/regset.h>
40*0Sstevel@tonic-gate #include <sys/privregs.h>
41*0Sstevel@tonic-gate #include <sys/frame.h>
42*0Sstevel@tonic-gate #include <sys/proc.h>
43*0Sstevel@tonic-gate #include <sys/psw.h>
44*0Sstevel@tonic-gate #include <sys/siginfo.h>
45*0Sstevel@tonic-gate #include <sys/cpuvar.h>
46*0Sstevel@tonic-gate #include <sys/asm_linkage.h>
47*0Sstevel@tonic-gate #include <sys/kmem.h>
48*0Sstevel@tonic-gate #include <sys/errno.h>
49*0Sstevel@tonic-gate #include <sys/bootconf.h>
50*0Sstevel@tonic-gate #include <sys/archsystm.h>
51*0Sstevel@tonic-gate #include <sys/debug.h>
52*0Sstevel@tonic-gate #include <sys/elf.h>
53*0Sstevel@tonic-gate #include <sys/spl.h>
54*0Sstevel@tonic-gate #include <sys/time.h>
55*0Sstevel@tonic-gate #include <sys/atomic.h>
56*0Sstevel@tonic-gate #include <sys/sysmacros.h>
57*0Sstevel@tonic-gate #include <sys/cmn_err.h>
58*0Sstevel@tonic-gate #include <sys/modctl.h>
59*0Sstevel@tonic-gate #include <sys/kobj.h>
60*0Sstevel@tonic-gate #include <sys/panic.h>
61*0Sstevel@tonic-gate #include <sys/reboot.h>
62*0Sstevel@tonic-gate #include <sys/time.h>
63*0Sstevel@tonic-gate #include <sys/fp.h>
64*0Sstevel@tonic-gate #include <sys/x86_archext.h>
65*0Sstevel@tonic-gate #include <sys/auxv.h>
66*0Sstevel@tonic-gate #include <sys/auxv_386.h>
67*0Sstevel@tonic-gate #include <sys/dtrace.h>
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate extern const struct fnsave_state x87_initial;
70*0Sstevel@tonic-gate extern const struct fxsave_state sse_initial;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate /*
73*0Sstevel@tonic-gate  * Map an fnsave-formatted save area into an fxsave-formatted save area.
74*0Sstevel@tonic-gate  *
75*0Sstevel@tonic-gate  * Most fields are the same width, content and semantics.  However
76*0Sstevel@tonic-gate  * the tag word is compressed.
77*0Sstevel@tonic-gate  */
78*0Sstevel@tonic-gate static void
79*0Sstevel@tonic-gate fnsave_to_fxsave(const struct fnsave_state *fn, struct fxsave_state *fx)
80*0Sstevel@tonic-gate {
81*0Sstevel@tonic-gate 	uint_t i, tagbits;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	fx->fx_fcw = fn->f_fcw;
84*0Sstevel@tonic-gate 	fx->fx_fsw = fn->f_fsw;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 	/*
87*0Sstevel@tonic-gate 	 * copy element by element (because of holes)
88*0Sstevel@tonic-gate 	 */
89*0Sstevel@tonic-gate 	for (i = 0; i < 8; i++)
90*0Sstevel@tonic-gate 		bcopy(&fn->f_st[i].fpr_16[0], &fx->fx_st[i].fpr_16[0],
91*0Sstevel@tonic-gate 		    sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	/*
94*0Sstevel@tonic-gate 	 * synthesize compressed tag bits
95*0Sstevel@tonic-gate 	 */
96*0Sstevel@tonic-gate 	fx->fx_fctw = 0;
97*0Sstevel@tonic-gate 	for (tagbits = fn->f_ftw, i = 0; i < 8; i++, tagbits >>= 2)
98*0Sstevel@tonic-gate 		if ((tagbits & 3) != 3)
99*0Sstevel@tonic-gate 			fx->fx_fctw |= (1 << i);
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 	fx->fx_fop = fn->f_fop;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate #if defined(__amd64)
104*0Sstevel@tonic-gate 	fx->fx_rip = (uint64_t)fn->f_eip;
105*0Sstevel@tonic-gate 	fx->fx_rdp = (uint64_t)fn->f_dp;
106*0Sstevel@tonic-gate #else
107*0Sstevel@tonic-gate 	fx->fx_eip = fn->f_eip;
108*0Sstevel@tonic-gate 	fx->fx_cs = fn->f_cs;
109*0Sstevel@tonic-gate 	fx->__fx_ign0 = 0;
110*0Sstevel@tonic-gate 	fx->fx_dp = fn->f_dp;
111*0Sstevel@tonic-gate 	fx->fx_ds = fn->f_ds;
112*0Sstevel@tonic-gate 	fx->__fx_ign1 = 0;
113*0Sstevel@tonic-gate #endif
114*0Sstevel@tonic-gate }
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate /*
117*0Sstevel@tonic-gate  * Map from an fxsave-format save area to an fnsave-format save area.
118*0Sstevel@tonic-gate  */
119*0Sstevel@tonic-gate static void
120*0Sstevel@tonic-gate fxsave_to_fnsave(const struct fxsave_state *fx, struct fnsave_state *fn)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	uint_t i, top, tagbits;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	fn->f_fcw = fx->fx_fcw;
125*0Sstevel@tonic-gate 	fn->__f_ign0 = 0;
126*0Sstevel@tonic-gate 	fn->f_fsw = fx->fx_fsw;
127*0Sstevel@tonic-gate 	fn->__f_ign1 = 0;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	top = (fx->fx_fsw & FPS_TOP) >> 11;
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	/*
132*0Sstevel@tonic-gate 	 * copy element by element (because of holes)
133*0Sstevel@tonic-gate 	 */
134*0Sstevel@tonic-gate 	for (i = 0; i < 8; i++)
135*0Sstevel@tonic-gate 		bcopy(&fx->fx_st[i].fpr_16[0], &fn->f_st[i].fpr_16[0],
136*0Sstevel@tonic-gate 		    sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	/*
139*0Sstevel@tonic-gate 	 * synthesize uncompressed tag bits
140*0Sstevel@tonic-gate 	 */
141*0Sstevel@tonic-gate 	fn->f_ftw = 0;
142*0Sstevel@tonic-gate 	for (tagbits = fx->fx_fctw, i = 0; i < 8; i++, tagbits >>= 1) {
143*0Sstevel@tonic-gate 		uint_t ibit, expo;
144*0Sstevel@tonic-gate 		const uint16_t *fpp;
145*0Sstevel@tonic-gate 		static const uint16_t zero[5] = { 0, 0, 0, 0, 0 };
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 		if ((tagbits & 1) == 0) {
148*0Sstevel@tonic-gate 			fn->f_ftw |= 3 << (i << 1);	/* empty */
149*0Sstevel@tonic-gate 			continue;
150*0Sstevel@tonic-gate 		}
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 		/*
153*0Sstevel@tonic-gate 		 * (tags refer to *physical* registers)
154*0Sstevel@tonic-gate 		 */
155*0Sstevel@tonic-gate 		fpp = &fx->fx_st[(i - top + 8) & 7].fpr_16[0];
156*0Sstevel@tonic-gate 		ibit = fpp[3] >> 15;
157*0Sstevel@tonic-gate 		expo = fpp[4] & 0x7fff;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 		if (ibit && expo != 0 && expo != 0x7fff)
160*0Sstevel@tonic-gate 			continue;			/* valid fp number */
161*0Sstevel@tonic-gate 
162*0Sstevel@tonic-gate 		if (bcmp(fpp, &zero, sizeof (zero)))
163*0Sstevel@tonic-gate 			fn->f_ftw |= 2 << (i << 1);	/* NaN */
164*0Sstevel@tonic-gate 		else
165*0Sstevel@tonic-gate 			fn->f_ftw |= 1 << (i << 1);	/* fp zero */
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	fn->f_fop = fx->fx_fop;
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	fn->__f_ign2 = 0;
171*0Sstevel@tonic-gate #if defined(__amd64)
172*0Sstevel@tonic-gate 	fn->f_eip = (uint32_t)fx->fx_rip;
173*0Sstevel@tonic-gate 	fn->f_cs = U32CS_SEL;
174*0Sstevel@tonic-gate 	fn->f_dp = (uint32_t)fx->fx_rdp;
175*0Sstevel@tonic-gate 	fn->f_ds = UDS_SEL;
176*0Sstevel@tonic-gate #else
177*0Sstevel@tonic-gate 	fn->f_eip = fx->fx_eip;
178*0Sstevel@tonic-gate 	fn->f_cs = fx->fx_cs;
179*0Sstevel@tonic-gate 	fn->f_dp = fx->fx_dp;
180*0Sstevel@tonic-gate 	fn->f_ds = fx->fx_ds;
181*0Sstevel@tonic-gate #endif
182*0Sstevel@tonic-gate 	fn->__f_ign3 = 0;
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate  * Map from an fpregset_t into an fxsave-format save area
187*0Sstevel@tonic-gate  */
188*0Sstevel@tonic-gate static void
189*0Sstevel@tonic-gate fpregset_to_fxsave(const fpregset_t *fp, struct fxsave_state *fx)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate #if defined(__amd64)
192*0Sstevel@tonic-gate 	bcopy(fp, fx, sizeof (*fx));
193*0Sstevel@tonic-gate #else
194*0Sstevel@tonic-gate 	const struct fpchip_state *fc = &fp->fp_reg_set.fpchip_state;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 	fnsave_to_fxsave((const struct fnsave_state *)fc, fx);
197*0Sstevel@tonic-gate 	fx->fx_mxcsr = fc->mxcsr;
198*0Sstevel@tonic-gate 	bcopy(&fc->xmm[0], &fx->fx_xmm[0], sizeof (fc->xmm));
199*0Sstevel@tonic-gate #endif
200*0Sstevel@tonic-gate 	/*
201*0Sstevel@tonic-gate 	 * avoid useless #gp exceptions - mask reserved bits
202*0Sstevel@tonic-gate 	 */
203*0Sstevel@tonic-gate 	fx->fx_mxcsr &= sse_mxcsr_mask;
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate /*
207*0Sstevel@tonic-gate  * Map from an fxsave-format save area into a fpregset_t
208*0Sstevel@tonic-gate  */
209*0Sstevel@tonic-gate static void
210*0Sstevel@tonic-gate fxsave_to_fpregset(const struct fxsave_state *fx, fpregset_t *fp)
211*0Sstevel@tonic-gate {
212*0Sstevel@tonic-gate #if defined(__amd64)
213*0Sstevel@tonic-gate 	bcopy(fx, fp, sizeof (*fx));
214*0Sstevel@tonic-gate #else
215*0Sstevel@tonic-gate 	struct fpchip_state *fc = &fp->fp_reg_set.fpchip_state;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 	fxsave_to_fnsave(fx, (struct fnsave_state *)fc);
218*0Sstevel@tonic-gate 	fc->mxcsr = fx->fx_mxcsr;
219*0Sstevel@tonic-gate 	bcopy(&fx->fx_xmm[0], &fc->xmm[0], sizeof (fc->xmm));
220*0Sstevel@tonic-gate #endif
221*0Sstevel@tonic-gate }
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
224*0Sstevel@tonic-gate static void
225*0Sstevel@tonic-gate fpregset32_to_fxsave(const fpregset32_t *fp, struct fxsave_state *fx)
226*0Sstevel@tonic-gate {
227*0Sstevel@tonic-gate 	const struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state;
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate 	fnsave_to_fxsave((const struct fnsave_state *)fc, fx);
230*0Sstevel@tonic-gate 	/*
231*0Sstevel@tonic-gate 	 * avoid useless #gp exceptions - mask reserved bits
232*0Sstevel@tonic-gate 	 */
233*0Sstevel@tonic-gate 	fx->fx_mxcsr = sse_mxcsr_mask & fc->mxcsr;
234*0Sstevel@tonic-gate 	bcopy(&fc->xmm[0], &fx->fx_xmm[0], sizeof (fc->xmm));
235*0Sstevel@tonic-gate }
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate static void
238*0Sstevel@tonic-gate fxsave_to_fpregset32(const struct fxsave_state *fx, fpregset32_t *fp)
239*0Sstevel@tonic-gate {
240*0Sstevel@tonic-gate 	struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state;
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate 	fxsave_to_fnsave(fx, (struct fnsave_state *)fc);
243*0Sstevel@tonic-gate 	fc->mxcsr = fx->fx_mxcsr;
244*0Sstevel@tonic-gate 	bcopy(&fx->fx_xmm[0], &fc->xmm[0], sizeof (fc->xmm));
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate static void
248*0Sstevel@tonic-gate fpregset_nto32(const fpregset_t *src, fpregset32_t *dst)
249*0Sstevel@tonic-gate {
250*0Sstevel@tonic-gate 	fxsave_to_fpregset32((struct fxsave_state *)src, dst);
251*0Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.status =
252*0Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.status;
253*0Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.xstatus =
254*0Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.xstatus;
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate static void
258*0Sstevel@tonic-gate fpregset_32ton(const fpregset32_t *src, fpregset_t *dst)
259*0Sstevel@tonic-gate {
260*0Sstevel@tonic-gate 	fpregset32_to_fxsave(src, (struct fxsave_state *)dst);
261*0Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.status =
262*0Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.status;
263*0Sstevel@tonic-gate 	dst->fp_reg_set.fpchip_state.xstatus =
264*0Sstevel@tonic-gate 	    src->fp_reg_set.fpchip_state.xstatus;
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate #endif
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate /*
269*0Sstevel@tonic-gate  * Set floating-point registers from a native fpregset_t.
270*0Sstevel@tonic-gate  */
271*0Sstevel@tonic-gate void
272*0Sstevel@tonic-gate setfpregs(klwp_t *lwp, fpregset_t *fp)
273*0Sstevel@tonic-gate {
274*0Sstevel@tonic-gate 	struct fpu_ctx *fpu = &lwp->lwp_pcb.pcb_fpu;
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	if (fpu->fpu_flags & FPU_EN) {
277*0Sstevel@tonic-gate 		if (!(fpu->fpu_flags & FPU_VALID)) {
278*0Sstevel@tonic-gate 			/*
279*0Sstevel@tonic-gate 			 * FPU context is still active, release the
280*0Sstevel@tonic-gate 			 * ownership.
281*0Sstevel@tonic-gate 			 */
282*0Sstevel@tonic-gate 			fp_free(fpu, 0);
283*0Sstevel@tonic-gate 		}
284*0Sstevel@tonic-gate #if !defined(__amd64)
285*0Sstevel@tonic-gate 		if (fp_kind == __FP_SSE) {
286*0Sstevel@tonic-gate #endif
287*0Sstevel@tonic-gate 			fpregset_to_fxsave(fp, &fpu->fpu_regs.kfpu_u.kfpu_fx);
288*0Sstevel@tonic-gate 			fpu->fpu_regs.kfpu_xstatus =
289*0Sstevel@tonic-gate 			    fp->fp_reg_set.fpchip_state.xstatus;
290*0Sstevel@tonic-gate #if !defined(__amd64)
291*0Sstevel@tonic-gate 		} else
292*0Sstevel@tonic-gate 			bcopy(fp, &fpu->fpu_regs.kfpu_u.kfpu_fn,
293*0Sstevel@tonic-gate 			    sizeof (fpu->fpu_regs.kfpu_u.kfpu_fn));
294*0Sstevel@tonic-gate #endif
295*0Sstevel@tonic-gate 		fpu->fpu_regs.kfpu_status = fp->fp_reg_set.fpchip_state.status;
296*0Sstevel@tonic-gate 		fpu->fpu_flags |= FPU_VALID;
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 		/*
299*0Sstevel@tonic-gate 		 * If we are changing the fpu_flags in the current context,
300*0Sstevel@tonic-gate 		 * disable floating point (turn on CR0_TS bit) to track
301*0Sstevel@tonic-gate 		 * FPU_VALID after clearing any errors (frstor chokes
302*0Sstevel@tonic-gate 		 * otherwise)
303*0Sstevel@tonic-gate 		 */
304*0Sstevel@tonic-gate 		if (lwp == ttolwp(curthread)) {
305*0Sstevel@tonic-gate 			(void) fperr_reset();
306*0Sstevel@tonic-gate 			fpdisable();
307*0Sstevel@tonic-gate 		}
308*0Sstevel@tonic-gate 	} else {
309*0Sstevel@tonic-gate 		/*
310*0Sstevel@tonic-gate 		 * If we are trying to change the FPU state of a thread which
311*0Sstevel@tonic-gate 		 * hasn't yet initialized floating point, store the state in
312*0Sstevel@tonic-gate 		 * the pcb and indicate that the state is valid.  When the
313*0Sstevel@tonic-gate 		 * thread enables floating point, it will use this state instead
314*0Sstevel@tonic-gate 		 * of the default state.
315*0Sstevel@tonic-gate 		 */
316*0Sstevel@tonic-gate #if !defined(__amd64)
317*0Sstevel@tonic-gate 		if (fp_kind == __FP_SSE) {
318*0Sstevel@tonic-gate #endif
319*0Sstevel@tonic-gate 			fpregset_to_fxsave(fp, &fpu->fpu_regs.kfpu_u.kfpu_fx);
320*0Sstevel@tonic-gate 			fpu->fpu_regs.kfpu_xstatus =
321*0Sstevel@tonic-gate 			    fp->fp_reg_set.fpchip_state.xstatus;
322*0Sstevel@tonic-gate #if !defined(__amd64)
323*0Sstevel@tonic-gate 		} else
324*0Sstevel@tonic-gate 			bcopy(fp, &fpu->fpu_regs.kfpu_u.kfpu_fn,
325*0Sstevel@tonic-gate 			    sizeof (fpu->fpu_regs.kfpu_u.kfpu_fn));
326*0Sstevel@tonic-gate #endif
327*0Sstevel@tonic-gate 		fpu->fpu_regs.kfpu_status = fp->fp_reg_set.fpchip_state.status;
328*0Sstevel@tonic-gate 		fpu->fpu_flags |= FPU_VALID;
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate }
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate /*
333*0Sstevel@tonic-gate  * Get floating-point registers into a native fpregset_t.
334*0Sstevel@tonic-gate  */
335*0Sstevel@tonic-gate void
336*0Sstevel@tonic-gate getfpregs(klwp_t *lwp, fpregset_t *fp)
337*0Sstevel@tonic-gate {
338*0Sstevel@tonic-gate 	struct fpu_ctx *fpu = &lwp->lwp_pcb.pcb_fpu;
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	kpreempt_disable();
341*0Sstevel@tonic-gate 	if (fpu->fpu_flags & FPU_EN) {
342*0Sstevel@tonic-gate 		/*
343*0Sstevel@tonic-gate 		 * If we have FPU hw and the thread's pcb doesn't have
344*0Sstevel@tonic-gate 		 * a valid FPU state then get the state from the hw.
345*0Sstevel@tonic-gate 		 */
346*0Sstevel@tonic-gate 		if (fpu_exists && ttolwp(curthread) == lwp &&
347*0Sstevel@tonic-gate 		    !(fpu->fpu_flags & FPU_VALID))
348*0Sstevel@tonic-gate 			fp_save(fpu); /* get the current FPU state */
349*0Sstevel@tonic-gate 	}
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate 	/*
352*0Sstevel@tonic-gate 	 * There are 3 possible cases we have to be aware of here:
353*0Sstevel@tonic-gate 	 *
354*0Sstevel@tonic-gate 	 * 1. FPU is enabled.  FPU state is stored in the current LWP.
355*0Sstevel@tonic-gate 	 *
356*0Sstevel@tonic-gate 	 * 2. FPU is not enabled, and there have been no intervening /proc
357*0Sstevel@tonic-gate 	 *    modifications.  Return initial FPU state.
358*0Sstevel@tonic-gate 	 *
359*0Sstevel@tonic-gate 	 * 3. FPU is not enabled, but a /proc consumer has modified FPU state.
360*0Sstevel@tonic-gate 	 *    FPU state is stored in the current LWP.
361*0Sstevel@tonic-gate 	 */
362*0Sstevel@tonic-gate 	if ((fpu->fpu_flags & FPU_EN) || (fpu->fpu_flags & FPU_VALID)) {
363*0Sstevel@tonic-gate 		/*
364*0Sstevel@tonic-gate 		 * Cases 1 and 3.
365*0Sstevel@tonic-gate 		 */
366*0Sstevel@tonic-gate #if !defined(__amd64)
367*0Sstevel@tonic-gate 		if (fp_kind == __FP_SSE) {
368*0Sstevel@tonic-gate #endif
369*0Sstevel@tonic-gate 			fxsave_to_fpregset(&fpu->fpu_regs.kfpu_u.kfpu_fx, fp);
370*0Sstevel@tonic-gate 			fp->fp_reg_set.fpchip_state.xstatus =
371*0Sstevel@tonic-gate 			    fpu->fpu_regs.kfpu_xstatus;
372*0Sstevel@tonic-gate #if !defined(__amd64)
373*0Sstevel@tonic-gate 		} else
374*0Sstevel@tonic-gate 			bcopy(&fpu->fpu_regs.kfpu_u.kfpu_fn, fp,
375*0Sstevel@tonic-gate 			    sizeof (fpu->fpu_regs.kfpu_u.kfpu_fn));
376*0Sstevel@tonic-gate #endif
377*0Sstevel@tonic-gate 		fp->fp_reg_set.fpchip_state.status = fpu->fpu_regs.kfpu_status;
378*0Sstevel@tonic-gate 	} else {
379*0Sstevel@tonic-gate 		/*
380*0Sstevel@tonic-gate 		 * Case 2.
381*0Sstevel@tonic-gate 		 */
382*0Sstevel@tonic-gate #if !defined(__amd64)
383*0Sstevel@tonic-gate 		if (fp_kind == __FP_SSE) {
384*0Sstevel@tonic-gate #endif
385*0Sstevel@tonic-gate 			fxsave_to_fpregset(&sse_initial, fp);
386*0Sstevel@tonic-gate 			fp->fp_reg_set.fpchip_state.xstatus =
387*0Sstevel@tonic-gate 			    fpu->fpu_regs.kfpu_xstatus;
388*0Sstevel@tonic-gate #if !defined(__amd64)
389*0Sstevel@tonic-gate 		} else
390*0Sstevel@tonic-gate 			bcopy(&x87_initial, fp, sizeof (x87_initial));
391*0Sstevel@tonic-gate #endif
392*0Sstevel@tonic-gate 		fp->fp_reg_set.fpchip_state.status = fpu->fpu_regs.kfpu_status;
393*0Sstevel@tonic-gate 	}
394*0Sstevel@tonic-gate 	kpreempt_enable();
395*0Sstevel@tonic-gate }
396*0Sstevel@tonic-gate 
397*0Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate /*
400*0Sstevel@tonic-gate  * Set floating-point registers from an fpregset32_t.
401*0Sstevel@tonic-gate  */
402*0Sstevel@tonic-gate void
403*0Sstevel@tonic-gate setfpregs32(klwp_t *lwp, fpregset32_t *fp)
404*0Sstevel@tonic-gate {
405*0Sstevel@tonic-gate 	fpregset_t fpregs;
406*0Sstevel@tonic-gate 
407*0Sstevel@tonic-gate 	fpregset_32ton(fp, &fpregs);
408*0Sstevel@tonic-gate 	setfpregs(lwp, &fpregs);
409*0Sstevel@tonic-gate }
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate /*
412*0Sstevel@tonic-gate  * Get floating-point registers into an fpregset32_t.
413*0Sstevel@tonic-gate  */
414*0Sstevel@tonic-gate void
415*0Sstevel@tonic-gate getfpregs32(klwp_t *lwp, fpregset32_t *fp)
416*0Sstevel@tonic-gate {
417*0Sstevel@tonic-gate 	fpregset_t fpregs;
418*0Sstevel@tonic-gate 
419*0Sstevel@tonic-gate 	getfpregs(lwp, &fpregs);
420*0Sstevel@tonic-gate 	fpregset_nto32(&fpregs, fp);
421*0Sstevel@tonic-gate }
422*0Sstevel@tonic-gate 
423*0Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
424*0Sstevel@tonic-gate 
425*0Sstevel@tonic-gate /*
426*0Sstevel@tonic-gate  * Return the general registers
427*0Sstevel@tonic-gate  */
428*0Sstevel@tonic-gate void
429*0Sstevel@tonic-gate getgregs(klwp_t *lwp, gregset_t grp)
430*0Sstevel@tonic-gate {
431*0Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
432*0Sstevel@tonic-gate #if defined(__amd64)
433*0Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
434*0Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	grp[REG_RDI] = rp->r_rdi;
437*0Sstevel@tonic-gate 	grp[REG_RSI] = rp->r_rsi;
438*0Sstevel@tonic-gate 	grp[REG_RDX] = rp->r_rdx;
439*0Sstevel@tonic-gate 	grp[REG_RCX] = rp->r_rcx;
440*0Sstevel@tonic-gate 	grp[REG_R8] = rp->r_r8;
441*0Sstevel@tonic-gate 	grp[REG_R9] = rp->r_r9;
442*0Sstevel@tonic-gate 	grp[REG_RAX] = rp->r_rax;
443*0Sstevel@tonic-gate 	grp[REG_RBX] = rp->r_rbx;
444*0Sstevel@tonic-gate 	grp[REG_RBP] = rp->r_rbp;
445*0Sstevel@tonic-gate 	grp[REG_R10] = rp->r_r10;
446*0Sstevel@tonic-gate 	grp[REG_R11] = rp->r_r11;
447*0Sstevel@tonic-gate 	grp[REG_R12] = rp->r_r12;
448*0Sstevel@tonic-gate 	grp[REG_R13] = rp->r_r13;
449*0Sstevel@tonic-gate 	grp[REG_R14] = rp->r_r14;
450*0Sstevel@tonic-gate 	grp[REG_R15] = rp->r_r15;
451*0Sstevel@tonic-gate 	grp[REG_FSBASE] = pcb->pcb_fsbase;
452*0Sstevel@tonic-gate 	grp[REG_GSBASE] = pcb->pcb_gsbase;
453*0Sstevel@tonic-gate 	if (thisthread)
454*0Sstevel@tonic-gate 		kpreempt_disable();
455*0Sstevel@tonic-gate 	if (pcb->pcb_flags & RUPDATE_PENDING) {
456*0Sstevel@tonic-gate 		grp[REG_DS] = pcb->pcb_ds;
457*0Sstevel@tonic-gate 		grp[REG_ES] = pcb->pcb_es;
458*0Sstevel@tonic-gate 		grp[REG_FS] = pcb->pcb_fs;
459*0Sstevel@tonic-gate 		grp[REG_GS] = pcb->pcb_gs;
460*0Sstevel@tonic-gate 	} else {
461*0Sstevel@tonic-gate 		grp[REG_DS] = rp->r_ds;
462*0Sstevel@tonic-gate 		grp[REG_ES] = rp->r_es;
463*0Sstevel@tonic-gate 		grp[REG_FS] = rp->r_fs;
464*0Sstevel@tonic-gate 		grp[REG_GS] = rp->r_gs;
465*0Sstevel@tonic-gate 	}
466*0Sstevel@tonic-gate 	if (thisthread)
467*0Sstevel@tonic-gate 		kpreempt_enable();
468*0Sstevel@tonic-gate 	grp[REG_TRAPNO] = rp->r_trapno;
469*0Sstevel@tonic-gate 	grp[REG_ERR] = rp->r_err;
470*0Sstevel@tonic-gate 	grp[REG_RIP] = rp->r_rip;
471*0Sstevel@tonic-gate 	grp[REG_CS] = rp->r_cs;
472*0Sstevel@tonic-gate 	grp[REG_SS] = rp->r_ss;
473*0Sstevel@tonic-gate 	grp[REG_RFL] = rp->r_rfl;
474*0Sstevel@tonic-gate 	grp[REG_RSP] = rp->r_rsp;
475*0Sstevel@tonic-gate #else
476*0Sstevel@tonic-gate 	bcopy(&rp->r_gs, grp, sizeof (gregset_t));
477*0Sstevel@tonic-gate #endif
478*0Sstevel@tonic-gate }
479*0Sstevel@tonic-gate 
480*0Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate void
483*0Sstevel@tonic-gate getgregs32(klwp_t *lwp, gregset32_t grp)
484*0Sstevel@tonic-gate {
485*0Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
486*0Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
487*0Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 	if (thisthread)
490*0Sstevel@tonic-gate 		kpreempt_disable();
491*0Sstevel@tonic-gate 	if (pcb->pcb_flags & RUPDATE_PENDING) {
492*0Sstevel@tonic-gate 		grp[GS] = (uint16_t)pcb->pcb_gs;
493*0Sstevel@tonic-gate 		grp[FS] = (uint16_t)pcb->pcb_fs;
494*0Sstevel@tonic-gate 		grp[DS] = (uint16_t)pcb->pcb_ds;
495*0Sstevel@tonic-gate 		grp[ES] = (uint16_t)pcb->pcb_es;
496*0Sstevel@tonic-gate 	} else {
497*0Sstevel@tonic-gate 		grp[GS] = (uint16_t)rp->r_gs;
498*0Sstevel@tonic-gate 		grp[FS] = (uint16_t)rp->r_fs;
499*0Sstevel@tonic-gate 		grp[DS] = (uint16_t)rp->r_ds;
500*0Sstevel@tonic-gate 		grp[ES] = (uint16_t)rp->r_es;
501*0Sstevel@tonic-gate 	}
502*0Sstevel@tonic-gate 	if (thisthread)
503*0Sstevel@tonic-gate 		kpreempt_enable();
504*0Sstevel@tonic-gate 	grp[EDI] = (greg32_t)rp->r_rdi;
505*0Sstevel@tonic-gate 	grp[ESI] = (greg32_t)rp->r_rsi;
506*0Sstevel@tonic-gate 	grp[EBP] = (greg32_t)rp->r_rbp;
507*0Sstevel@tonic-gate 	grp[ESP] = 0;
508*0Sstevel@tonic-gate 	grp[EBX] = (greg32_t)rp->r_rbx;
509*0Sstevel@tonic-gate 	grp[EDX] = (greg32_t)rp->r_rdx;
510*0Sstevel@tonic-gate 	grp[ECX] = (greg32_t)rp->r_rcx;
511*0Sstevel@tonic-gate 	grp[EAX] = (greg32_t)rp->r_rax;
512*0Sstevel@tonic-gate 	grp[TRAPNO] = (greg32_t)rp->r_trapno;
513*0Sstevel@tonic-gate 	grp[ERR] = (greg32_t)rp->r_err;
514*0Sstevel@tonic-gate 	grp[EIP] = (greg32_t)rp->r_rip;
515*0Sstevel@tonic-gate 	grp[CS] = (uint16_t)rp->r_cs;
516*0Sstevel@tonic-gate 	grp[EFL] = (greg32_t)rp->r_rfl;
517*0Sstevel@tonic-gate 	grp[UESP] = (greg32_t)rp->r_rsp;
518*0Sstevel@tonic-gate 	grp[SS] = (uint16_t)rp->r_ss;
519*0Sstevel@tonic-gate }
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate void
522*0Sstevel@tonic-gate ucontext_32ton(const ucontext32_t *src, ucontext_t *dst)
523*0Sstevel@tonic-gate {
524*0Sstevel@tonic-gate 	mcontext_t *dmc = &dst->uc_mcontext;
525*0Sstevel@tonic-gate 	const mcontext32_t *smc = &src->uc_mcontext;
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate 	bzero(dst, sizeof (*dst));
528*0Sstevel@tonic-gate 	dst->uc_flags = src->uc_flags;
529*0Sstevel@tonic-gate 	dst->uc_link = (ucontext_t *)(uintptr_t)src->uc_link;
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 	bcopy(&src->uc_sigmask, &dst->uc_sigmask, sizeof (dst->uc_sigmask));
532*0Sstevel@tonic-gate 
533*0Sstevel@tonic-gate 	dst->uc_stack.ss_sp = (void *)(uintptr_t)src->uc_stack.ss_sp;
534*0Sstevel@tonic-gate 	dst->uc_stack.ss_size = (size_t)src->uc_stack.ss_size;
535*0Sstevel@tonic-gate 	dst->uc_stack.ss_flags = src->uc_stack.ss_flags;
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 	dmc->gregs[REG_GS] = (greg_t)(uint32_t)smc->gregs[GS];
538*0Sstevel@tonic-gate 	dmc->gregs[REG_FS] = (greg_t)(uint32_t)smc->gregs[FS];
539*0Sstevel@tonic-gate 	dmc->gregs[REG_ES] = (greg_t)(uint32_t)smc->gregs[ES];
540*0Sstevel@tonic-gate 	dmc->gregs[REG_DS] = (greg_t)(uint32_t)smc->gregs[DS];
541*0Sstevel@tonic-gate 	dmc->gregs[REG_RDI] = (greg_t)(uint32_t)smc->gregs[EDI];
542*0Sstevel@tonic-gate 	dmc->gregs[REG_RSI] = (greg_t)(uint32_t)smc->gregs[ESI];
543*0Sstevel@tonic-gate 	dmc->gregs[REG_RBP] = (greg_t)(uint32_t)smc->gregs[EBP];
544*0Sstevel@tonic-gate 	dmc->gregs[REG_RBX] = (greg_t)(uint32_t)smc->gregs[EBX];
545*0Sstevel@tonic-gate 	dmc->gregs[REG_RDX] = (greg_t)(uint32_t)smc->gregs[EDX];
546*0Sstevel@tonic-gate 	dmc->gregs[REG_RCX] = (greg_t)(uint32_t)smc->gregs[ECX];
547*0Sstevel@tonic-gate 	dmc->gregs[REG_RAX] = (greg_t)(uint32_t)smc->gregs[EAX];
548*0Sstevel@tonic-gate 	dmc->gregs[REG_TRAPNO] = (greg_t)(uint32_t)smc->gregs[TRAPNO];
549*0Sstevel@tonic-gate 	dmc->gregs[REG_ERR] = (greg_t)(uint32_t)smc->gregs[ERR];
550*0Sstevel@tonic-gate 	dmc->gregs[REG_RIP] = (greg_t)(uint32_t)smc->gregs[EIP];
551*0Sstevel@tonic-gate 	dmc->gregs[REG_CS] = (greg_t)(uint32_t)smc->gregs[CS];
552*0Sstevel@tonic-gate 	dmc->gregs[REG_RFL] = (greg_t)(uint32_t)smc->gregs[EFL];
553*0Sstevel@tonic-gate 	dmc->gregs[REG_RSP] = (greg_t)(uint32_t)smc->gregs[UESP];
554*0Sstevel@tonic-gate 	dmc->gregs[REG_SS] = (greg_t)(uint32_t)smc->gregs[SS];
555*0Sstevel@tonic-gate 
556*0Sstevel@tonic-gate 	/*
557*0Sstevel@tonic-gate 	 * A valid fpregs is only copied in if uc.uc_flags has UC_FPU set
558*0Sstevel@tonic-gate 	 * otherwise there is no guarantee that anything in fpregs is valid.
559*0Sstevel@tonic-gate 	 */
560*0Sstevel@tonic-gate 	if (src->uc_flags & UC_FPU)
561*0Sstevel@tonic-gate 		fpregset_32ton(&src->uc_mcontext.fpregs,
562*0Sstevel@tonic-gate 		    &dst->uc_mcontext.fpregs);
563*0Sstevel@tonic-gate }
564*0Sstevel@tonic-gate 
565*0Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
566*0Sstevel@tonic-gate 
567*0Sstevel@tonic-gate /*
568*0Sstevel@tonic-gate  * Return the user-level PC.
569*0Sstevel@tonic-gate  * If in a system call, return the address of the syscall trap.
570*0Sstevel@tonic-gate  */
571*0Sstevel@tonic-gate greg_t
572*0Sstevel@tonic-gate getuserpc()
573*0Sstevel@tonic-gate {
574*0Sstevel@tonic-gate 	greg_t upc = lwptoregs(ttolwp(curthread))->r_pc;
575*0Sstevel@tonic-gate 	uint32_t insn;
576*0Sstevel@tonic-gate 
577*0Sstevel@tonic-gate 	if (curthread->t_sysnum == 0)
578*0Sstevel@tonic-gate 		return (upc);
579*0Sstevel@tonic-gate 
580*0Sstevel@tonic-gate 	/*
581*0Sstevel@tonic-gate 	 * We might've gotten here from sysenter (0xf 0x34),
582*0Sstevel@tonic-gate 	 * syscall (0xf 0x5) or lcall (0x9a 0 0 0 0 0x27 0).
583*0Sstevel@tonic-gate 	 *
584*0Sstevel@tonic-gate 	 * Go peek at the binary to figure it out..
585*0Sstevel@tonic-gate 	 */
586*0Sstevel@tonic-gate 	if (fuword32((void *)(upc - 2), &insn) != -1 &&
587*0Sstevel@tonic-gate 	    (insn & 0xffff) == 0x340f || (insn & 0xffff) == 0x050f)
588*0Sstevel@tonic-gate 		return (upc - 2);
589*0Sstevel@tonic-gate 	return (upc - 7);
590*0Sstevel@tonic-gate }
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate /*
593*0Sstevel@tonic-gate  * Protect segment registers from non-user privilege levels and GDT selectors
594*0Sstevel@tonic-gate  * other than USER_CS, USER_DS and lwp FS and GS values.  If the segment
595*0Sstevel@tonic-gate  * selector is non-null and not USER_CS/USER_DS, we make sure that the
596*0Sstevel@tonic-gate  * TI bit is set to point into the LDT and that the RPL is set to 3.
597*0Sstevel@tonic-gate  *
598*0Sstevel@tonic-gate  * Since struct regs stores each 16-bit segment register as a 32-bit greg_t, we
599*0Sstevel@tonic-gate  * also explicitly zero the top 16 bits since they may be coming from the
600*0Sstevel@tonic-gate  * user's address space via setcontext(2) or /proc.
601*0Sstevel@tonic-gate  */
602*0Sstevel@tonic-gate 
603*0Sstevel@tonic-gate /*ARGSUSED*/
604*0Sstevel@tonic-gate static greg_t
605*0Sstevel@tonic-gate fix_segreg(greg_t sr, model_t datamodel)
606*0Sstevel@tonic-gate {
607*0Sstevel@tonic-gate 	switch (sr &= 0xffff) {
608*0Sstevel@tonic-gate #if defined(__amd64)
609*0Sstevel@tonic-gate 	/*
610*0Sstevel@tonic-gate 	 * If lwp attempts to switch data model then force their
611*0Sstevel@tonic-gate 	 * code selector to be null selector.
612*0Sstevel@tonic-gate 	 */
613*0Sstevel@tonic-gate 	case U32CS_SEL:
614*0Sstevel@tonic-gate 		if (datamodel == DATAMODEL_NATIVE)
615*0Sstevel@tonic-gate 			return (0);
616*0Sstevel@tonic-gate 		else
617*0Sstevel@tonic-gate 			return (sr);
618*0Sstevel@tonic-gate 
619*0Sstevel@tonic-gate 	case UCS_SEL:
620*0Sstevel@tonic-gate 		if (datamodel == DATAMODEL_ILP32)
621*0Sstevel@tonic-gate 			return (0);
622*0Sstevel@tonic-gate #elif defined(__i386)
623*0Sstevel@tonic-gate 	case UCS_SEL:
624*0Sstevel@tonic-gate #endif
625*0Sstevel@tonic-gate 	/*FALLTHROUGH*/
626*0Sstevel@tonic-gate 	case UDS_SEL:
627*0Sstevel@tonic-gate 	case LWPFS_SEL:
628*0Sstevel@tonic-gate 	case LWPGS_SEL:
629*0Sstevel@tonic-gate 	case 0:
630*0Sstevel@tonic-gate 		return (sr);
631*0Sstevel@tonic-gate 	default:
632*0Sstevel@tonic-gate 		break;
633*0Sstevel@tonic-gate 	}
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 	/*
636*0Sstevel@tonic-gate 	 * Force it into the LDT in ring 3 for 32-bit processes.
637*0Sstevel@tonic-gate 	 * 64-bit processes get the null gdt selector since they
638*0Sstevel@tonic-gate 	 * are not allowed to have a private LDT.
639*0Sstevel@tonic-gate 	 */
640*0Sstevel@tonic-gate #if defined(__amd64)
641*0Sstevel@tonic-gate 	return (datamodel == DATAMODEL_ILP32 ? (sr | SEL_TI_LDT | SEL_UPL) : 0);
642*0Sstevel@tonic-gate #elif defined(__i386)
643*0Sstevel@tonic-gate 	return (sr | SEL_TI_LDT | SEL_UPL);
644*0Sstevel@tonic-gate #endif
645*0Sstevel@tonic-gate }
646*0Sstevel@tonic-gate 
647*0Sstevel@tonic-gate /*
648*0Sstevel@tonic-gate  * Set general registers.
649*0Sstevel@tonic-gate  */
650*0Sstevel@tonic-gate void
651*0Sstevel@tonic-gate setgregs(klwp_t *lwp, gregset_t grp)
652*0Sstevel@tonic-gate {
653*0Sstevel@tonic-gate 	struct regs *rp = lwptoregs(lwp);
654*0Sstevel@tonic-gate 	model_t	datamodel = lwp_getdatamodel(lwp);
655*0Sstevel@tonic-gate 
656*0Sstevel@tonic-gate #if defined(__amd64)
657*0Sstevel@tonic-gate 	struct pcb *pcb = &lwp->lwp_pcb;
658*0Sstevel@tonic-gate 	int thisthread = lwptot(lwp) == curthread;
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate 	if (datamodel == DATAMODEL_NATIVE) {
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 		if (thisthread)
663*0Sstevel@tonic-gate 			(void) save_syscall_args();	/* copy the args */
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 		rp->r_rdi = grp[REG_RDI];
666*0Sstevel@tonic-gate 		rp->r_rsi = grp[REG_RSI];
667*0Sstevel@tonic-gate 		rp->r_rdx = grp[REG_RDX];
668*0Sstevel@tonic-gate 		rp->r_rcx = grp[REG_RCX];
669*0Sstevel@tonic-gate 		rp->r_r8 = grp[REG_R8];
670*0Sstevel@tonic-gate 		rp->r_r9 = grp[REG_R9];
671*0Sstevel@tonic-gate 		rp->r_rax = grp[REG_RAX];
672*0Sstevel@tonic-gate 		rp->r_rbx = grp[REG_RBX];
673*0Sstevel@tonic-gate 		rp->r_rbp = grp[REG_RBP];
674*0Sstevel@tonic-gate 		rp->r_r10 = grp[REG_R10];
675*0Sstevel@tonic-gate 		rp->r_r11 = grp[REG_R11];
676*0Sstevel@tonic-gate 		rp->r_r12 = grp[REG_R12];
677*0Sstevel@tonic-gate 		rp->r_r13 = grp[REG_R13];
678*0Sstevel@tonic-gate 		rp->r_r14 = grp[REG_R14];
679*0Sstevel@tonic-gate 		rp->r_r15 = grp[REG_R15];
680*0Sstevel@tonic-gate 		rp->r_trapno = grp[REG_TRAPNO];
681*0Sstevel@tonic-gate 		rp->r_err = grp[REG_ERR];
682*0Sstevel@tonic-gate 		rp->r_rip = grp[REG_RIP];
683*0Sstevel@tonic-gate 		/*
684*0Sstevel@tonic-gate 		 * Setting %cs or %ss to anything else is quietly but
685*0Sstevel@tonic-gate 		 * quite definitely forbidden!
686*0Sstevel@tonic-gate 		 */
687*0Sstevel@tonic-gate 		rp->r_cs = UCS_SEL;
688*0Sstevel@tonic-gate 		rp->r_ss = UDS_SEL;
689*0Sstevel@tonic-gate 		rp->r_rsp = grp[REG_RSP];
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 		if (thisthread)
692*0Sstevel@tonic-gate 			kpreempt_disable();
693*0Sstevel@tonic-gate 
694*0Sstevel@tonic-gate 		pcb->pcb_ds = UDS_SEL;
695*0Sstevel@tonic-gate 		pcb->pcb_es = UDS_SEL;
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 		/*
698*0Sstevel@tonic-gate 		 * 64-bit processes -are- allowed to set their fsbase/gsbase
699*0Sstevel@tonic-gate 		 * values directly, but only if they're using the segment
700*0Sstevel@tonic-gate 		 * selectors that allow that semantic.
701*0Sstevel@tonic-gate 		 *
702*0Sstevel@tonic-gate 		 * (32-bit processes must use lwp_set_private().)
703*0Sstevel@tonic-gate 		 */
704*0Sstevel@tonic-gate 		pcb->pcb_fsbase = grp[REG_FSBASE];
705*0Sstevel@tonic-gate 		pcb->pcb_gsbase = grp[REG_GSBASE];
706*0Sstevel@tonic-gate 		pcb->pcb_fs = fix_segreg(grp[REG_FS], datamodel);
707*0Sstevel@tonic-gate 		pcb->pcb_gs = fix_segreg(grp[REG_GS], datamodel);
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 		/*
710*0Sstevel@tonic-gate 		 * Ensure that we go out via update_sregs
711*0Sstevel@tonic-gate 		 */
712*0Sstevel@tonic-gate 		pcb->pcb_flags |= RUPDATE_PENDING;
713*0Sstevel@tonic-gate 		lwptot(lwp)->t_post_sys = 1;
714*0Sstevel@tonic-gate 		if (thisthread)
715*0Sstevel@tonic-gate 			kpreempt_enable();
716*0Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
717*0Sstevel@tonic-gate 	} else {
718*0Sstevel@tonic-gate 		rp->r_rdi = (uint32_t)grp[REG_RDI];
719*0Sstevel@tonic-gate 		rp->r_rsi = (uint32_t)grp[REG_RSI];
720*0Sstevel@tonic-gate 		rp->r_rdx = (uint32_t)grp[REG_RDX];
721*0Sstevel@tonic-gate 		rp->r_rcx = (uint32_t)grp[REG_RCX];
722*0Sstevel@tonic-gate 		rp->r_rax = (uint32_t)grp[REG_RAX];
723*0Sstevel@tonic-gate 		rp->r_rbx = (uint32_t)grp[REG_RBX];
724*0Sstevel@tonic-gate 		rp->r_rbp = (uint32_t)grp[REG_RBP];
725*0Sstevel@tonic-gate 		rp->r_trapno = (uint32_t)grp[REG_TRAPNO];
726*0Sstevel@tonic-gate 		rp->r_err = (uint32_t)grp[REG_ERR];
727*0Sstevel@tonic-gate 		rp->r_rip = (uint32_t)grp[REG_RIP];
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate 		/*
730*0Sstevel@tonic-gate 		 * The kernel uses %cs to determine if it is dealing with
731*0Sstevel@tonic-gate 		 * another part of the kernel or with a userland application.
732*0Sstevel@tonic-gate 		 * Specifically, it tests the privilege bits. For this reason,
733*0Sstevel@tonic-gate 		 * we must prevent user apps from ending up with a NULL selector
734*0Sstevel@tonic-gate 		 * in %cs. Instead, we'll use index 0 into the GDT but with the
735*0Sstevel@tonic-gate 		 * privilege bits set to usermode.
736*0Sstevel@tonic-gate 		 */
737*0Sstevel@tonic-gate 		rp->r_cs = fix_segreg(grp[REG_CS], datamodel) | SEL_UPL;
738*0Sstevel@tonic-gate 		rp->r_ss = fix_segreg(grp[REG_DS], datamodel);
739*0Sstevel@tonic-gate 
740*0Sstevel@tonic-gate 		rp->r_rsp = (uint32_t)grp[REG_RSP];
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 		if (thisthread)
743*0Sstevel@tonic-gate 			kpreempt_disable();
744*0Sstevel@tonic-gate 
745*0Sstevel@tonic-gate 		pcb->pcb_ds = fix_segreg(grp[REG_DS], datamodel);
746*0Sstevel@tonic-gate 		pcb->pcb_es = fix_segreg(grp[REG_ES], datamodel);
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate 		/*
749*0Sstevel@tonic-gate 		 * (See fsbase/gsbase commentary above)
750*0Sstevel@tonic-gate 		 */
751*0Sstevel@tonic-gate 		pcb->pcb_fs = fix_segreg(grp[REG_FS], datamodel);
752*0Sstevel@tonic-gate 		pcb->pcb_gs = fix_segreg(grp[REG_GS], datamodel);
753*0Sstevel@tonic-gate 
754*0Sstevel@tonic-gate 		/*
755*0Sstevel@tonic-gate 		 * Ensure that we go out via update_sregs
756*0Sstevel@tonic-gate 		 */
757*0Sstevel@tonic-gate 		pcb->pcb_flags |= RUPDATE_PENDING;
758*0Sstevel@tonic-gate 		lwptot(lwp)->t_post_sys = 1;
759*0Sstevel@tonic-gate 		if (thisthread)
760*0Sstevel@tonic-gate 			kpreempt_enable();
761*0Sstevel@tonic-gate #endif
762*0Sstevel@tonic-gate 	}
763*0Sstevel@tonic-gate 
764*0Sstevel@tonic-gate 	/*
765*0Sstevel@tonic-gate 	 * Only certain bits of the flags register can be modified.
766*0Sstevel@tonic-gate 	 */
767*0Sstevel@tonic-gate 	rp->r_rfl = (rp->r_rfl & ~PSL_USERMASK) |
768*0Sstevel@tonic-gate 	    (grp[REG_RFL] & PSL_USERMASK);
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate #elif defined(__i386)
771*0Sstevel@tonic-gate 
772*0Sstevel@tonic-gate 	/*
773*0Sstevel@tonic-gate 	 * Only certain bits of the flags register can be modified.
774*0Sstevel@tonic-gate 	 */
775*0Sstevel@tonic-gate 	grp[EFL] = (rp->r_efl & ~PSL_USERMASK) | (grp[EFL] & PSL_USERMASK);
776*0Sstevel@tonic-gate 
777*0Sstevel@tonic-gate 	/*
778*0Sstevel@tonic-gate 	 * Copy saved registers from user stack.
779*0Sstevel@tonic-gate 	 */
780*0Sstevel@tonic-gate 	bcopy(grp, &rp->r_gs, sizeof (gregset_t));
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate 	rp->r_cs = fix_segreg(rp->r_cs, datamodel);
783*0Sstevel@tonic-gate 	rp->r_ss = fix_segreg(rp->r_ss, datamodel);
784*0Sstevel@tonic-gate 	rp->r_ds = fix_segreg(rp->r_ds, datamodel);
785*0Sstevel@tonic-gate 	rp->r_es = fix_segreg(rp->r_es, datamodel);
786*0Sstevel@tonic-gate 	rp->r_fs = fix_segreg(rp->r_fs, datamodel);
787*0Sstevel@tonic-gate 	rp->r_gs = fix_segreg(rp->r_gs, datamodel);
788*0Sstevel@tonic-gate 
789*0Sstevel@tonic-gate #endif	/* __i386 */
790*0Sstevel@tonic-gate }
791*0Sstevel@tonic-gate 
792*0Sstevel@tonic-gate /*
793*0Sstevel@tonic-gate  * Determine whether eip is likely to have an interrupt frame
794*0Sstevel@tonic-gate  * on the stack.  We do this by comparing the address to the
795*0Sstevel@tonic-gate  * range of addresses spanned by several well-known routines.
796*0Sstevel@tonic-gate  */
797*0Sstevel@tonic-gate extern void _interrupt();
798*0Sstevel@tonic-gate extern void _allsyscalls();
799*0Sstevel@tonic-gate extern void _cmntrap();
800*0Sstevel@tonic-gate extern void fakesoftint();
801*0Sstevel@tonic-gate 
802*0Sstevel@tonic-gate extern size_t _interrupt_size;
803*0Sstevel@tonic-gate extern size_t _allsyscalls_size;
804*0Sstevel@tonic-gate extern size_t _cmntrap_size;
805*0Sstevel@tonic-gate extern size_t _fakesoftint_size;
806*0Sstevel@tonic-gate 
807*0Sstevel@tonic-gate /*
808*0Sstevel@tonic-gate  * Get a pc-only stacktrace.  Used for kmem_alloc() buffer ownership tracking.
809*0Sstevel@tonic-gate  * Returns MIN(current stack depth, pcstack_limit).
810*0Sstevel@tonic-gate  */
811*0Sstevel@tonic-gate int
812*0Sstevel@tonic-gate getpcstack(pc_t *pcstack, int pcstack_limit)
813*0Sstevel@tonic-gate {
814*0Sstevel@tonic-gate 	struct frame *fp = (struct frame *)getfp();
815*0Sstevel@tonic-gate 	struct frame *nextfp, *minfp, *stacktop;
816*0Sstevel@tonic-gate 	int depth = 0;
817*0Sstevel@tonic-gate 	int on_intr;
818*0Sstevel@tonic-gate 	uintptr_t pc;
819*0Sstevel@tonic-gate 
820*0Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
821*0Sstevel@tonic-gate 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
822*0Sstevel@tonic-gate 	else
823*0Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
824*0Sstevel@tonic-gate 	minfp = fp;
825*0Sstevel@tonic-gate 
826*0Sstevel@tonic-gate 	pc = ((struct regs *)fp)->r_pc;
827*0Sstevel@tonic-gate 
828*0Sstevel@tonic-gate 	while (depth < pcstack_limit) {
829*0Sstevel@tonic-gate 		nextfp = (struct frame *)fp->fr_savfp;
830*0Sstevel@tonic-gate 		pc = fp->fr_savpc;
831*0Sstevel@tonic-gate 		if (nextfp <= minfp || nextfp >= stacktop) {
832*0Sstevel@tonic-gate 			if (on_intr) {
833*0Sstevel@tonic-gate 				/*
834*0Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
835*0Sstevel@tonic-gate 				 */
836*0Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
837*0Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
838*0Sstevel@tonic-gate 				on_intr = 0;
839*0Sstevel@tonic-gate 				continue;
840*0Sstevel@tonic-gate 			}
841*0Sstevel@tonic-gate 			break;
842*0Sstevel@tonic-gate 		}
843*0Sstevel@tonic-gate 		pcstack[depth++] = (pc_t)pc;
844*0Sstevel@tonic-gate 		fp = nextfp;
845*0Sstevel@tonic-gate 		minfp = fp;
846*0Sstevel@tonic-gate 	}
847*0Sstevel@tonic-gate 	return (depth);
848*0Sstevel@tonic-gate }
849*0Sstevel@tonic-gate 
850*0Sstevel@tonic-gate /*
851*0Sstevel@tonic-gate  * The following ELF header fields are defined as processor-specific
852*0Sstevel@tonic-gate  * in the V8 ABI:
853*0Sstevel@tonic-gate  *
854*0Sstevel@tonic-gate  *	e_ident[EI_DATA]	encoding of the processor-specific
855*0Sstevel@tonic-gate  *				data in the object file
856*0Sstevel@tonic-gate  *	e_machine		processor identification
857*0Sstevel@tonic-gate  *	e_flags			processor-specific flags associated
858*0Sstevel@tonic-gate  *				with the file
859*0Sstevel@tonic-gate  */
860*0Sstevel@tonic-gate 
861*0Sstevel@tonic-gate /*
862*0Sstevel@tonic-gate  * The value of at_flags reflects a platform's cpu module support.
863*0Sstevel@tonic-gate  * at_flags is used to check for allowing a binary to execute and
864*0Sstevel@tonic-gate  * is passed as the value of the AT_FLAGS auxiliary vector.
865*0Sstevel@tonic-gate  */
866*0Sstevel@tonic-gate int at_flags = 0;
867*0Sstevel@tonic-gate 
868*0Sstevel@tonic-gate /*
869*0Sstevel@tonic-gate  * Check the processor-specific fields of an ELF header.
870*0Sstevel@tonic-gate  *
871*0Sstevel@tonic-gate  * returns 1 if the fields are valid, 0 otherwise
872*0Sstevel@tonic-gate  */
873*0Sstevel@tonic-gate /*ARGSUSED2*/
874*0Sstevel@tonic-gate int
875*0Sstevel@tonic-gate elfheadcheck(
876*0Sstevel@tonic-gate 	unsigned char e_data,
877*0Sstevel@tonic-gate 	Elf32_Half e_machine,
878*0Sstevel@tonic-gate 	Elf32_Word e_flags)
879*0Sstevel@tonic-gate {
880*0Sstevel@tonic-gate 	if (e_data != ELFDATA2LSB)
881*0Sstevel@tonic-gate 		return (0);
882*0Sstevel@tonic-gate #if defined(__amd64)
883*0Sstevel@tonic-gate 	if (e_machine == EM_AMD64)
884*0Sstevel@tonic-gate 		return (1);
885*0Sstevel@tonic-gate #endif
886*0Sstevel@tonic-gate 	return (e_machine == EM_386);
887*0Sstevel@tonic-gate }
888*0Sstevel@tonic-gate 
889*0Sstevel@tonic-gate uint_t auxv_hwcap_include = 0;	/* patch to enable unrecognized features */
890*0Sstevel@tonic-gate uint_t auxv_hwcap_exclude = 0;	/* patch for broken cpus, debugging */
891*0Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
892*0Sstevel@tonic-gate uint_t auxv_hwcap32_include = 0;	/* ditto for 32-bit apps */
893*0Sstevel@tonic-gate uint_t auxv_hwcap32_exclude = 0;	/* ditto for 32-bit apps */
894*0Sstevel@tonic-gate #endif
895*0Sstevel@tonic-gate 
896*0Sstevel@tonic-gate /*
897*0Sstevel@tonic-gate  * Gather information about the processor and place it into auxv_hwcap
898*0Sstevel@tonic-gate  * so that it can be exported to the linker via the aux vector.
899*0Sstevel@tonic-gate  *
900*0Sstevel@tonic-gate  * We use this seemingly complicated mechanism so that we can ensure
901*0Sstevel@tonic-gate  * that /etc/system can be used to override what the system can or
902*0Sstevel@tonic-gate  * cannot discover for itself.
903*0Sstevel@tonic-gate  */
904*0Sstevel@tonic-gate void
905*0Sstevel@tonic-gate bind_hwcap(void)
906*0Sstevel@tonic-gate {
907*0Sstevel@tonic-gate 	uint_t cpu_hwcap_flags = cpuid_pass4(NULL);
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 	auxv_hwcap = (auxv_hwcap_include | cpu_hwcap_flags) &
910*0Sstevel@tonic-gate 	    ~auxv_hwcap_exclude;
911*0Sstevel@tonic-gate 
912*0Sstevel@tonic-gate #if defined(__amd64)
913*0Sstevel@tonic-gate 	/*
914*0Sstevel@tonic-gate 	 * On AMD processors, sysenter just doesn't work at all
915*0Sstevel@tonic-gate 	 * when the kernel is in long mode.  On IA-32e processors
916*0Sstevel@tonic-gate 	 * it does, but there's no real point in all the alternate
917*0Sstevel@tonic-gate 	 * mechanism when syscall works on both.
918*0Sstevel@tonic-gate 	 *
919*0Sstevel@tonic-gate 	 * Besides, the kernel's sysenter handler is expecting a
920*0Sstevel@tonic-gate 	 * 32-bit lwp ...
921*0Sstevel@tonic-gate 	 */
922*0Sstevel@tonic-gate 	auxv_hwcap &= ~AV_386_SEP;
923*0Sstevel@tonic-gate #endif
924*0Sstevel@tonic-gate 
925*0Sstevel@tonic-gate 	if (auxv_hwcap_include || auxv_hwcap_exclude)
926*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "?user ABI extensions: %b\n",
927*0Sstevel@tonic-gate 		    auxv_hwcap, FMT_AV_386);
928*0Sstevel@tonic-gate 
929*0Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL)
930*0Sstevel@tonic-gate 	auxv_hwcap32 = (auxv_hwcap32_include | cpu_hwcap_flags) &
931*0Sstevel@tonic-gate 		~auxv_hwcap32_exclude;
932*0Sstevel@tonic-gate 
933*0Sstevel@tonic-gate #if defined(__amd64)
934*0Sstevel@tonic-gate 	/*
935*0Sstevel@tonic-gate 	 * If this is an amd64 architecture machine from Intel, then
936*0Sstevel@tonic-gate 	 * syscall -doesn't- work in compatibility mode, only sysenter does.
937*0Sstevel@tonic-gate 	 *
938*0Sstevel@tonic-gate 	 * Sigh.
939*0Sstevel@tonic-gate 	 */
940*0Sstevel@tonic-gate 	if (!cpuid_syscall32_insn(NULL))
941*0Sstevel@tonic-gate 		auxv_hwcap32 &= ~AV_386_AMD_SYSC;
942*0Sstevel@tonic-gate #endif
943*0Sstevel@tonic-gate 
944*0Sstevel@tonic-gate 	if (auxv_hwcap32_include || auxv_hwcap32_exclude)
945*0Sstevel@tonic-gate 		cmn_err(CE_CONT, "?32-bit user ABI extensions: %b\n",
946*0Sstevel@tonic-gate 		    auxv_hwcap32, FMT_AV_386);
947*0Sstevel@tonic-gate #endif
948*0Sstevel@tonic-gate }
949*0Sstevel@tonic-gate 
950*0Sstevel@tonic-gate /*
951*0Sstevel@tonic-gate  *	sync_icache() - this is called
952*0Sstevel@tonic-gate  *	in proc/fs/prusrio.c. x86 has an unified cache and therefore
953*0Sstevel@tonic-gate  *	this is a nop.
954*0Sstevel@tonic-gate  */
955*0Sstevel@tonic-gate /* ARGSUSED */
956*0Sstevel@tonic-gate void
957*0Sstevel@tonic-gate sync_icache(caddr_t addr, uint_t len)
958*0Sstevel@tonic-gate {
959*0Sstevel@tonic-gate 	/* Do nothing for now */
960*0Sstevel@tonic-gate }
961*0Sstevel@tonic-gate 
962*0Sstevel@tonic-gate /*ARGSUSED*/
963*0Sstevel@tonic-gate void
964*0Sstevel@tonic-gate sync_data_memory(caddr_t va, size_t len)
965*0Sstevel@tonic-gate {
966*0Sstevel@tonic-gate 	/* Not implemented for this platform */
967*0Sstevel@tonic-gate }
968*0Sstevel@tonic-gate 
969*0Sstevel@tonic-gate int
970*0Sstevel@tonic-gate __ipltospl(int ipl)
971*0Sstevel@tonic-gate {
972*0Sstevel@tonic-gate 	return (ipltospl(ipl));
973*0Sstevel@tonic-gate }
974*0Sstevel@tonic-gate 
975*0Sstevel@tonic-gate /*
976*0Sstevel@tonic-gate  * The panic code invokes panic_saveregs() to record the contents of a
977*0Sstevel@tonic-gate  * regs structure into the specified panic_data structure for debuggers.
978*0Sstevel@tonic-gate  */
979*0Sstevel@tonic-gate void
980*0Sstevel@tonic-gate panic_saveregs(panic_data_t *pdp, struct regs *rp)
981*0Sstevel@tonic-gate {
982*0Sstevel@tonic-gate 	panic_nv_t *pnv = PANICNVGET(pdp);
983*0Sstevel@tonic-gate 
984*0Sstevel@tonic-gate 	struct cregs	creg;
985*0Sstevel@tonic-gate 
986*0Sstevel@tonic-gate 	getcregs(&creg);
987*0Sstevel@tonic-gate 
988*0Sstevel@tonic-gate #if defined(__amd64)
989*0Sstevel@tonic-gate 	PANICNVADD(pnv, "rdi", rp->r_rdi);
990*0Sstevel@tonic-gate 	PANICNVADD(pnv, "rsi", rp->r_rsi);
991*0Sstevel@tonic-gate 	PANICNVADD(pnv, "rdx", rp->r_rdx);
992*0Sstevel@tonic-gate 	PANICNVADD(pnv, "rcx", rp->r_rcx);
993*0Sstevel@tonic-gate 	PANICNVADD(pnv, "r8", rp->r_r8);
994*0Sstevel@tonic-gate 	PANICNVADD(pnv, "r9", rp->r_r9);
995*0Sstevel@tonic-gate 	PANICNVADD(pnv, "rax", rp->r_rax);
996*0Sstevel@tonic-gate 	PANICNVADD(pnv, "rbx", rp->r_rbx);
997*0Sstevel@tonic-gate 	PANICNVADD(pnv, "rbp", rp->r_rbp);
998*0Sstevel@tonic-gate 	PANICNVADD(pnv, "r10", rp->r_r10);
999*0Sstevel@tonic-gate 	PANICNVADD(pnv, "r10", rp->r_r10);
1000*0Sstevel@tonic-gate 	PANICNVADD(pnv, "r11", rp->r_r11);
1001*0Sstevel@tonic-gate 	PANICNVADD(pnv, "r12", rp->r_r12);
1002*0Sstevel@tonic-gate 	PANICNVADD(pnv, "r13", rp->r_r13);
1003*0Sstevel@tonic-gate 	PANICNVADD(pnv, "r14", rp->r_r14);
1004*0Sstevel@tonic-gate 	PANICNVADD(pnv, "r15", rp->r_r15);
1005*0Sstevel@tonic-gate 	PANICNVADD(pnv, "fsbase", rp->r_fsbase);
1006*0Sstevel@tonic-gate 	PANICNVADD(pnv, "gsbase", rp->r_gsbase);
1007*0Sstevel@tonic-gate 	PANICNVADD(pnv, "ds", rp->r_ds);
1008*0Sstevel@tonic-gate 	PANICNVADD(pnv, "es", rp->r_es);
1009*0Sstevel@tonic-gate 	PANICNVADD(pnv, "fs", rp->r_fs);
1010*0Sstevel@tonic-gate 	PANICNVADD(pnv, "gs", rp->r_gs);
1011*0Sstevel@tonic-gate 	PANICNVADD(pnv, "trapno", rp->r_trapno);
1012*0Sstevel@tonic-gate 	PANICNVADD(pnv, "err", rp->r_err);
1013*0Sstevel@tonic-gate 	PANICNVADD(pnv, "rip", rp->r_rip);
1014*0Sstevel@tonic-gate 	PANICNVADD(pnv, "cs", rp->r_cs);
1015*0Sstevel@tonic-gate 	PANICNVADD(pnv, "rflags", rp->r_rfl);
1016*0Sstevel@tonic-gate 	PANICNVADD(pnv, "rsp", rp->r_rsp);
1017*0Sstevel@tonic-gate 	PANICNVADD(pnv, "ss", rp->r_ss);
1018*0Sstevel@tonic-gate 	PANICNVADD(pnv, "gdt_hi", (uint64_t)(creg.cr_gdt._l[3]));
1019*0Sstevel@tonic-gate 	PANICNVADD(pnv, "gdt_lo", (uint64_t)(creg.cr_gdt._l[0]));
1020*0Sstevel@tonic-gate 	PANICNVADD(pnv, "idt_hi", (uint64_t)(creg.cr_idt._l[3]));
1021*0Sstevel@tonic-gate 	PANICNVADD(pnv, "idt_lo", (uint64_t)(creg.cr_idt._l[0]));
1022*0Sstevel@tonic-gate #elif defined(__i386)
1023*0Sstevel@tonic-gate 	PANICNVADD(pnv, "gs", (uint32_t)rp->r_gs);
1024*0Sstevel@tonic-gate 	PANICNVADD(pnv, "fs", (uint32_t)rp->r_fs);
1025*0Sstevel@tonic-gate 	PANICNVADD(pnv, "es", (uint32_t)rp->r_es);
1026*0Sstevel@tonic-gate 	PANICNVADD(pnv, "ds", (uint32_t)rp->r_ds);
1027*0Sstevel@tonic-gate 	PANICNVADD(pnv, "edi", (uint32_t)rp->r_edi);
1028*0Sstevel@tonic-gate 	PANICNVADD(pnv, "esi", (uint32_t)rp->r_esi);
1029*0Sstevel@tonic-gate 	PANICNVADD(pnv, "ebp", (uint32_t)rp->r_ebp);
1030*0Sstevel@tonic-gate 	PANICNVADD(pnv, "esp", (uint32_t)rp->r_esp);
1031*0Sstevel@tonic-gate 	PANICNVADD(pnv, "ebx", (uint32_t)rp->r_ebx);
1032*0Sstevel@tonic-gate 	PANICNVADD(pnv, "edx", (uint32_t)rp->r_edx);
1033*0Sstevel@tonic-gate 	PANICNVADD(pnv, "ecx", (uint32_t)rp->r_ecx);
1034*0Sstevel@tonic-gate 	PANICNVADD(pnv, "eax", (uint32_t)rp->r_eax);
1035*0Sstevel@tonic-gate 	PANICNVADD(pnv, "trapno", (uint32_t)rp->r_trapno);
1036*0Sstevel@tonic-gate 	PANICNVADD(pnv, "err", (uint32_t)rp->r_err);
1037*0Sstevel@tonic-gate 	PANICNVADD(pnv, "eip", (uint32_t)rp->r_eip);
1038*0Sstevel@tonic-gate 	PANICNVADD(pnv, "cs", (uint32_t)rp->r_cs);
1039*0Sstevel@tonic-gate 	PANICNVADD(pnv, "eflags", (uint32_t)rp->r_efl);
1040*0Sstevel@tonic-gate 	PANICNVADD(pnv, "uesp", (uint32_t)rp->r_uesp);
1041*0Sstevel@tonic-gate 	PANICNVADD(pnv, "ss", (uint32_t)rp->r_ss);
1042*0Sstevel@tonic-gate 	PANICNVADD(pnv, "gdt", creg.cr_gdt);
1043*0Sstevel@tonic-gate 	PANICNVADD(pnv, "idt", creg.cr_idt);
1044*0Sstevel@tonic-gate #endif	/* __i386 */
1045*0Sstevel@tonic-gate 
1046*0Sstevel@tonic-gate 	PANICNVADD(pnv, "ldt", creg.cr_ldt);
1047*0Sstevel@tonic-gate 	PANICNVADD(pnv, "task", creg.cr_task);
1048*0Sstevel@tonic-gate 	PANICNVADD(pnv, "cr0", creg.cr_cr0);
1049*0Sstevel@tonic-gate 	PANICNVADD(pnv, "cr2", creg.cr_cr2);
1050*0Sstevel@tonic-gate 	PANICNVADD(pnv, "cr3", creg.cr_cr3);
1051*0Sstevel@tonic-gate 	if (creg.cr_cr4)
1052*0Sstevel@tonic-gate 		PANICNVADD(pnv, "cr4", creg.cr_cr4);
1053*0Sstevel@tonic-gate 
1054*0Sstevel@tonic-gate 	PANICNVSET(pdp, pnv);
1055*0Sstevel@tonic-gate }
1056*0Sstevel@tonic-gate 
1057*0Sstevel@tonic-gate #define	TR_ARG_MAX 6	/* Max args to print, same as SPARC */
1058*0Sstevel@tonic-gate 
1059*0Sstevel@tonic-gate #if !defined(__amd64)
1060*0Sstevel@tonic-gate 
1061*0Sstevel@tonic-gate /*
1062*0Sstevel@tonic-gate  * Given a return address (%eip), determine the likely number of arguments
1063*0Sstevel@tonic-gate  * that were pushed on the stack prior to its execution.  We do this by
1064*0Sstevel@tonic-gate  * expecting that a typical call sequence consists of pushing arguments on
1065*0Sstevel@tonic-gate  * the stack, executing a call instruction, and then performing an add
1066*0Sstevel@tonic-gate  * on %esp to restore it to the value prior to pushing the arguments for
1067*0Sstevel@tonic-gate  * the call.  We attempt to detect such an add, and divide the addend
1068*0Sstevel@tonic-gate  * by the size of a word to determine the number of pushed arguments.
1069*0Sstevel@tonic-gate  *
1070*0Sstevel@tonic-gate  * If we do not find such an add, we punt and return TR_ARG_MAX. It is not
1071*0Sstevel@tonic-gate  * possible to reliably determine if a function took no arguments (i.e. was
1072*0Sstevel@tonic-gate  * void) because assembler routines do not reliably perform an add on %esp
1073*0Sstevel@tonic-gate  * immediately upon returning (eg. _sys_call()), so returning TR_ARG_MAX is
1074*0Sstevel@tonic-gate  * safer than returning 0.
1075*0Sstevel@tonic-gate  */
1076*0Sstevel@tonic-gate static ulong_t
1077*0Sstevel@tonic-gate argcount(uintptr_t eip)
1078*0Sstevel@tonic-gate {
1079*0Sstevel@tonic-gate 	const uint8_t *ins = (const uint8_t *)eip;
1080*0Sstevel@tonic-gate 	ulong_t n;
1081*0Sstevel@tonic-gate 
1082*0Sstevel@tonic-gate 	enum {
1083*0Sstevel@tonic-gate 		M_MODRM_ESP = 0xc4,	/* Mod/RM byte indicates %esp */
1084*0Sstevel@tonic-gate 		M_ADD_IMM32 = 0x81,	/* ADD imm32 to r/m32 */
1085*0Sstevel@tonic-gate 		M_ADD_IMM8  = 0x83	/* ADD imm8 to r/m32 */
1086*0Sstevel@tonic-gate 	};
1087*0Sstevel@tonic-gate 
1088*0Sstevel@tonic-gate 	if (eip < KERNELBASE || ins[1] != M_MODRM_ESP)
1089*0Sstevel@tonic-gate 		return (TR_ARG_MAX);
1090*0Sstevel@tonic-gate 
1091*0Sstevel@tonic-gate 	switch (ins[0]) {
1092*0Sstevel@tonic-gate 	case M_ADD_IMM32:
1093*0Sstevel@tonic-gate 		n = ins[2] + (ins[3] << 8) + (ins[4] << 16) + (ins[5] << 24);
1094*0Sstevel@tonic-gate 		break;
1095*0Sstevel@tonic-gate 
1096*0Sstevel@tonic-gate 	case M_ADD_IMM8:
1097*0Sstevel@tonic-gate 		n = ins[2];
1098*0Sstevel@tonic-gate 		break;
1099*0Sstevel@tonic-gate 
1100*0Sstevel@tonic-gate 	default:
1101*0Sstevel@tonic-gate 		return (TR_ARG_MAX);
1102*0Sstevel@tonic-gate 	}
1103*0Sstevel@tonic-gate 
1104*0Sstevel@tonic-gate 	n /= sizeof (long);
1105*0Sstevel@tonic-gate 	return (MIN(n, TR_ARG_MAX));
1106*0Sstevel@tonic-gate }
1107*0Sstevel@tonic-gate 
1108*0Sstevel@tonic-gate #endif	/* !__amd64 */
1109*0Sstevel@tonic-gate 
1110*0Sstevel@tonic-gate /*
1111*0Sstevel@tonic-gate  * Print a stack backtrace using the specified frame pointer.  We delay two
1112*0Sstevel@tonic-gate  * seconds before continuing, unless this is the panic traceback.  Note
1113*0Sstevel@tonic-gate  * that the frame for the starting stack pointer value is omitted because
1114*0Sstevel@tonic-gate  * the corresponding %eip is not known.
1115*0Sstevel@tonic-gate  */
1116*0Sstevel@tonic-gate #if defined(__amd64)
1117*0Sstevel@tonic-gate 
1118*0Sstevel@tonic-gate void
1119*0Sstevel@tonic-gate traceback(caddr_t fpreg)
1120*0Sstevel@tonic-gate {
1121*0Sstevel@tonic-gate 	struct frame	*fp = (struct frame *)fpreg;
1122*0Sstevel@tonic-gate 	struct frame	*nextfp;
1123*0Sstevel@tonic-gate 	uintptr_t	pc, nextpc;
1124*0Sstevel@tonic-gate 	ulong_t		off;
1125*0Sstevel@tonic-gate 	char		args[TR_ARG_MAX * 2 + 16], *sym;
1126*0Sstevel@tonic-gate 
1127*0Sstevel@tonic-gate 	if (!panicstr)
1128*0Sstevel@tonic-gate 		printf("traceback: %%fp = %p\n", (void *)fp);
1129*0Sstevel@tonic-gate 
1130*0Sstevel@tonic-gate 	if ((uintptr_t)fp < KERNELBASE)
1131*0Sstevel@tonic-gate 		goto out;
1132*0Sstevel@tonic-gate 
1133*0Sstevel@tonic-gate 	pc = fp->fr_savpc;
1134*0Sstevel@tonic-gate 	fp = (struct frame *)fp->fr_savfp;
1135*0Sstevel@tonic-gate 
1136*0Sstevel@tonic-gate 	while ((uintptr_t)fp >= KERNELBASE) {
1137*0Sstevel@tonic-gate 		/*
1138*0Sstevel@tonic-gate 		 * XX64 Until port is complete tolerate 8-byte aligned
1139*0Sstevel@tonic-gate 		 * frame pointers but flag with a warning so they can
1140*0Sstevel@tonic-gate 		 * be fixed.
1141*0Sstevel@tonic-gate 		 */
1142*0Sstevel@tonic-gate 		if (((uintptr_t)fp & (STACK_ALIGN - 1)) != 0) {
1143*0Sstevel@tonic-gate 			if (((uintptr_t)fp & (8 - 1)) == 0) {
1144*0Sstevel@tonic-gate 				printf("  >> warning! 8-byte"
1145*0Sstevel@tonic-gate 				    " aligned %%fp = %p\n", (void *)fp);
1146*0Sstevel@tonic-gate 			} else {
1147*0Sstevel@tonic-gate 				printf(
1148*0Sstevel@tonic-gate 				    "  >> mis-aligned %%fp = %p\n", (void *)fp);
1149*0Sstevel@tonic-gate 				break;
1150*0Sstevel@tonic-gate 			}
1151*0Sstevel@tonic-gate 		}
1152*0Sstevel@tonic-gate 
1153*0Sstevel@tonic-gate 		args[0] = '\0';
1154*0Sstevel@tonic-gate 		nextpc = (uintptr_t)fp->fr_savpc;
1155*0Sstevel@tonic-gate 		nextfp = (struct frame *)fp->fr_savfp;
1156*0Sstevel@tonic-gate 		if ((sym = kobj_getsymname(pc, &off)) != NULL) {
1157*0Sstevel@tonic-gate 			printf("%016lx %s:%s+%lx (%s)\n", (uintptr_t)fp,
1158*0Sstevel@tonic-gate 			    mod_containing_pc((caddr_t)pc), sym, off, args);
1159*0Sstevel@tonic-gate 		} else {
1160*0Sstevel@tonic-gate 			printf("%016lx %lx (%s)\n",
1161*0Sstevel@tonic-gate 			    (uintptr_t)fp, pc, args);
1162*0Sstevel@tonic-gate 		}
1163*0Sstevel@tonic-gate 
1164*0Sstevel@tonic-gate 		pc = nextpc;
1165*0Sstevel@tonic-gate 		fp = nextfp;
1166*0Sstevel@tonic-gate 	}
1167*0Sstevel@tonic-gate out:
1168*0Sstevel@tonic-gate 	if (!panicstr) {
1169*0Sstevel@tonic-gate 		printf("end of traceback\n");
1170*0Sstevel@tonic-gate 		DELAY(2 * MICROSEC);
1171*0Sstevel@tonic-gate 	}
1172*0Sstevel@tonic-gate }
1173*0Sstevel@tonic-gate 
1174*0Sstevel@tonic-gate #elif defined(__i386)
1175*0Sstevel@tonic-gate 
1176*0Sstevel@tonic-gate void
1177*0Sstevel@tonic-gate traceback(caddr_t fpreg)
1178*0Sstevel@tonic-gate {
1179*0Sstevel@tonic-gate 	struct frame *fp = (struct frame *)fpreg;
1180*0Sstevel@tonic-gate 	struct frame *nextfp, *minfp, *stacktop;
1181*0Sstevel@tonic-gate 	uintptr_t pc, nextpc;
1182*0Sstevel@tonic-gate 
1183*0Sstevel@tonic-gate 	cpu_t *cpu;
1184*0Sstevel@tonic-gate 
1185*0Sstevel@tonic-gate 	/*
1186*0Sstevel@tonic-gate 	 * args[] holds TR_ARG_MAX hex long args, plus ", " or '\0'.
1187*0Sstevel@tonic-gate 	 */
1188*0Sstevel@tonic-gate 	char args[TR_ARG_MAX * 2 + 8], *p;
1189*0Sstevel@tonic-gate 
1190*0Sstevel@tonic-gate 	int on_intr;
1191*0Sstevel@tonic-gate 	ulong_t off;
1192*0Sstevel@tonic-gate 	char *sym;
1193*0Sstevel@tonic-gate 
1194*0Sstevel@tonic-gate 	if (!panicstr)
1195*0Sstevel@tonic-gate 		printf("traceback: %%fp = %p\n", (void *)fp);
1196*0Sstevel@tonic-gate 
1197*0Sstevel@tonic-gate 	/*
1198*0Sstevel@tonic-gate 	 * If we are panicking, all high-level interrupt information in
1199*0Sstevel@tonic-gate 	 * CPU was overwritten.  panic_cpu has the correct values.
1200*0Sstevel@tonic-gate 	 */
1201*0Sstevel@tonic-gate 	kpreempt_disable();			/* prevent migration */
1202*0Sstevel@tonic-gate 
1203*0Sstevel@tonic-gate 	cpu = (panicstr && CPU->cpu_id == panic_cpu.cpu_id)? &panic_cpu : CPU;
1204*0Sstevel@tonic-gate 
1205*0Sstevel@tonic-gate 	if ((on_intr = CPU_ON_INTR(cpu)) != 0)
1206*0Sstevel@tonic-gate 		stacktop = (struct frame *)(cpu->cpu_intr_stack + SA(MINFRAME));
1207*0Sstevel@tonic-gate 	else
1208*0Sstevel@tonic-gate 		stacktop = (struct frame *)curthread->t_stk;
1209*0Sstevel@tonic-gate 
1210*0Sstevel@tonic-gate 	kpreempt_enable();
1211*0Sstevel@tonic-gate 
1212*0Sstevel@tonic-gate 	if ((uintptr_t)fp < KERNELBASE)
1213*0Sstevel@tonic-gate 		goto out;
1214*0Sstevel@tonic-gate 
1215*0Sstevel@tonic-gate 	minfp = fp;	/* Baseline minimum frame pointer */
1216*0Sstevel@tonic-gate 	pc = fp->fr_savpc;
1217*0Sstevel@tonic-gate 	fp = (struct frame *)fp->fr_savfp;
1218*0Sstevel@tonic-gate 
1219*0Sstevel@tonic-gate 	while ((uintptr_t)fp >= KERNELBASE) {
1220*0Sstevel@tonic-gate 		ulong_t argc;
1221*0Sstevel@tonic-gate 		long *argv;
1222*0Sstevel@tonic-gate 
1223*0Sstevel@tonic-gate 		if (fp <= minfp || fp >= stacktop) {
1224*0Sstevel@tonic-gate 			if (on_intr) {
1225*0Sstevel@tonic-gate 				/*
1226*0Sstevel@tonic-gate 				 * Hop from interrupt stack to thread stack.
1227*0Sstevel@tonic-gate 				 */
1228*0Sstevel@tonic-gate 				stacktop = (struct frame *)curthread->t_stk;
1229*0Sstevel@tonic-gate 				minfp = (struct frame *)curthread->t_stkbase;
1230*0Sstevel@tonic-gate 				on_intr = 0;
1231*0Sstevel@tonic-gate 				continue;
1232*0Sstevel@tonic-gate 			}
1233*0Sstevel@tonic-gate 			break; /* we're outside of the expected range */
1234*0Sstevel@tonic-gate 		}
1235*0Sstevel@tonic-gate 
1236*0Sstevel@tonic-gate 		if ((uintptr_t)fp & (STACK_ALIGN - 1)) {
1237*0Sstevel@tonic-gate 			printf("  >> mis-aligned %%fp = %p\n", (void *)fp);
1238*0Sstevel@tonic-gate 			break;
1239*0Sstevel@tonic-gate 		}
1240*0Sstevel@tonic-gate 
1241*0Sstevel@tonic-gate 		nextpc = fp->fr_savpc;
1242*0Sstevel@tonic-gate 		nextfp = (struct frame *)fp->fr_savfp;
1243*0Sstevel@tonic-gate 		argc = argcount(nextpc);
1244*0Sstevel@tonic-gate 		argv = (long *)((char *)fp + sizeof (struct frame));
1245*0Sstevel@tonic-gate 
1246*0Sstevel@tonic-gate 		args[0] = '\0';
1247*0Sstevel@tonic-gate 		p = args;
1248*0Sstevel@tonic-gate 		while (argc-- > 0 && argv < (long *)stacktop) {
1249*0Sstevel@tonic-gate 			p += snprintf(p, args + sizeof (args) - p,
1250*0Sstevel@tonic-gate 			    "%s%lx", (p == args) ? "" : ", ", *argv++);
1251*0Sstevel@tonic-gate 		}
1252*0Sstevel@tonic-gate 
1253*0Sstevel@tonic-gate 		if ((sym = kobj_getsymname(pc, &off)) != NULL) {
1254*0Sstevel@tonic-gate 			printf("%08lx %s:%s+%lx (%s)\n", (uintptr_t)fp,
1255*0Sstevel@tonic-gate 			    mod_containing_pc((caddr_t)pc), sym, off, args);
1256*0Sstevel@tonic-gate 		} else {
1257*0Sstevel@tonic-gate 			printf("%08lx %lx (%s)\n",
1258*0Sstevel@tonic-gate 			    (uintptr_t)fp, pc, args);
1259*0Sstevel@tonic-gate 		}
1260*0Sstevel@tonic-gate 
1261*0Sstevel@tonic-gate 		minfp = fp;
1262*0Sstevel@tonic-gate 		pc = nextpc;
1263*0Sstevel@tonic-gate 		fp = nextfp;
1264*0Sstevel@tonic-gate 	}
1265*0Sstevel@tonic-gate out:
1266*0Sstevel@tonic-gate 	if (!panicstr) {
1267*0Sstevel@tonic-gate 		printf("end of traceback\n");
1268*0Sstevel@tonic-gate 		DELAY(2 * MICROSEC);
1269*0Sstevel@tonic-gate 	}
1270*0Sstevel@tonic-gate }
1271*0Sstevel@tonic-gate 
1272*0Sstevel@tonic-gate #endif	/* __i386 */
1273*0Sstevel@tonic-gate 
1274*0Sstevel@tonic-gate /*
1275*0Sstevel@tonic-gate  * Generate a stack backtrace from a saved register set.
1276*0Sstevel@tonic-gate  */
1277*0Sstevel@tonic-gate void
1278*0Sstevel@tonic-gate traceregs(struct regs *rp)
1279*0Sstevel@tonic-gate {
1280*0Sstevel@tonic-gate 	traceback((caddr_t)rp->r_fp);
1281*0Sstevel@tonic-gate }
1282*0Sstevel@tonic-gate 
1283*0Sstevel@tonic-gate void
1284*0Sstevel@tonic-gate exec_set_sp(size_t stksize)
1285*0Sstevel@tonic-gate {
1286*0Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
1287*0Sstevel@tonic-gate 
1288*0Sstevel@tonic-gate 	lwptoregs(lwp)->r_sp = (uintptr_t)curproc->p_usrstack - stksize;
1289*0Sstevel@tonic-gate }
1290*0Sstevel@tonic-gate 
1291*0Sstevel@tonic-gate hrtime_t
1292*0Sstevel@tonic-gate gethrtime_waitfree(void)
1293*0Sstevel@tonic-gate {
1294*0Sstevel@tonic-gate 	return (dtrace_gethrtime());
1295*0Sstevel@tonic-gate }
1296*0Sstevel@tonic-gate 
1297*0Sstevel@tonic-gate hrtime_t
1298*0Sstevel@tonic-gate gethrtime(void)
1299*0Sstevel@tonic-gate {
1300*0Sstevel@tonic-gate 	return (gethrtimef());
1301*0Sstevel@tonic-gate }
1302*0Sstevel@tonic-gate 
1303*0Sstevel@tonic-gate hrtime_t
1304*0Sstevel@tonic-gate gethrtime_unscaled(void)
1305*0Sstevel@tonic-gate {
1306*0Sstevel@tonic-gate 	return (gethrtimeunscaledf());
1307*0Sstevel@tonic-gate }
1308*0Sstevel@tonic-gate 
1309*0Sstevel@tonic-gate void
1310*0Sstevel@tonic-gate scalehrtime(hrtime_t *hrt)
1311*0Sstevel@tonic-gate {
1312*0Sstevel@tonic-gate 	scalehrtimef(hrt);
1313*0Sstevel@tonic-gate }
1314*0Sstevel@tonic-gate 
1315*0Sstevel@tonic-gate void
1316*0Sstevel@tonic-gate gethrestime(timespec_t *tp)
1317*0Sstevel@tonic-gate {
1318*0Sstevel@tonic-gate 	gethrestimef(tp);
1319*0Sstevel@tonic-gate }
1320*0Sstevel@tonic-gate 
1321*0Sstevel@tonic-gate #if defined(__amd64)
1322*0Sstevel@tonic-gate /*
1323*0Sstevel@tonic-gate  * Part of the implementation of hres_tick(); this routine is
1324*0Sstevel@tonic-gate  * easier in C than assembler .. called with the hres_lock held.
1325*0Sstevel@tonic-gate  *
1326*0Sstevel@tonic-gate  * XX64	Many of these timekeeping variables need to be extern'ed in a header
1327*0Sstevel@tonic-gate  */
1328*0Sstevel@tonic-gate 
1329*0Sstevel@tonic-gate #include <sys/time.h>
1330*0Sstevel@tonic-gate #include <sys/machlock.h>
1331*0Sstevel@tonic-gate 
1332*0Sstevel@tonic-gate extern int one_sec;
1333*0Sstevel@tonic-gate extern timestruc_t hrestime;
1334*0Sstevel@tonic-gate extern int max_hres_adj;
1335*0Sstevel@tonic-gate 
1336*0Sstevel@tonic-gate void
1337*0Sstevel@tonic-gate __adj_hrestime(void)
1338*0Sstevel@tonic-gate {
1339*0Sstevel@tonic-gate 	long long adj;
1340*0Sstevel@tonic-gate 
1341*0Sstevel@tonic-gate 	if (hrestime_adj == 0)
1342*0Sstevel@tonic-gate 		adj = 0;
1343*0Sstevel@tonic-gate 	else if (hrestime_adj > 0) {
1344*0Sstevel@tonic-gate 		if (hrestime_adj < max_hres_adj)
1345*0Sstevel@tonic-gate 			adj = hrestime_adj;
1346*0Sstevel@tonic-gate 		else
1347*0Sstevel@tonic-gate 			adj = max_hres_adj;
1348*0Sstevel@tonic-gate 	} else {
1349*0Sstevel@tonic-gate 		if (hrestime_adj < -max_hres_adj)
1350*0Sstevel@tonic-gate 			adj = -max_hres_adj;
1351*0Sstevel@tonic-gate 		else
1352*0Sstevel@tonic-gate 			adj = hrestime_adj;
1353*0Sstevel@tonic-gate 	}
1354*0Sstevel@tonic-gate 
1355*0Sstevel@tonic-gate 	timedelta -= adj;
1356*0Sstevel@tonic-gate 	hrestime_adj = timedelta;
1357*0Sstevel@tonic-gate 	hrestime.tv_nsec += adj;
1358*0Sstevel@tonic-gate 
1359*0Sstevel@tonic-gate 	while (hrestime.tv_nsec >= NANOSEC) {
1360*0Sstevel@tonic-gate 		one_sec++;
1361*0Sstevel@tonic-gate 		hrestime.tv_sec++;
1362*0Sstevel@tonic-gate 		hrestime.tv_nsec -= NANOSEC;
1363*0Sstevel@tonic-gate 	}
1364*0Sstevel@tonic-gate }
1365*0Sstevel@tonic-gate #endif
1366*0Sstevel@tonic-gate 
1367*0Sstevel@tonic-gate /*
1368*0Sstevel@tonic-gate  * Wrapper functions to maintain backwards compability
1369*0Sstevel@tonic-gate  */
1370*0Sstevel@tonic-gate int
1371*0Sstevel@tonic-gate xcopyin(const void *uaddr, void *kaddr, size_t count)
1372*0Sstevel@tonic-gate {
1373*0Sstevel@tonic-gate 	return (xcopyin_nta(uaddr, kaddr, count, UIO_COPY_CACHED));
1374*0Sstevel@tonic-gate }
1375*0Sstevel@tonic-gate 
1376*0Sstevel@tonic-gate int
1377*0Sstevel@tonic-gate xcopyout(const void *kaddr, void *uaddr, size_t count)
1378*0Sstevel@tonic-gate {
1379*0Sstevel@tonic-gate 	return (xcopyout_nta(kaddr, uaddr, count, UIO_COPY_CACHED));
1380*0Sstevel@tonic-gate }
1381