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