xref: /illumos-gate/usr/src/uts/intel/os/archdep.c (revision ed093b41a93e8563e6e1e5dae0768dda2a7bcc27)
1f0089e39SRichard Lowe /*
2f0089e39SRichard Lowe  * CDDL HEADER START
3f0089e39SRichard Lowe  *
4f0089e39SRichard Lowe  * The contents of this file are subject to the terms of the
5f0089e39SRichard Lowe  * Common Development and Distribution License (the "License").
6f0089e39SRichard Lowe  * You may not use this file except in compliance with the License.
7f0089e39SRichard Lowe  *
8f0089e39SRichard Lowe  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9f0089e39SRichard Lowe  * or http://www.opensolaris.org/os/licensing.
10f0089e39SRichard Lowe  * See the License for the specific language governing permissions
11f0089e39SRichard Lowe  * and limitations under the License.
12f0089e39SRichard Lowe  *
13f0089e39SRichard Lowe  * When distributing Covered Code, include this CDDL HEADER in each
14f0089e39SRichard Lowe  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15f0089e39SRichard Lowe  * If applicable, add the following below this CDDL HEADER, with the
16f0089e39SRichard Lowe  * fields enclosed by brackets "[]" replaced with your own identifying
17f0089e39SRichard Lowe  * information: Portions Copyright [yyyy] [name of copyright owner]
18f0089e39SRichard Lowe  *
19f0089e39SRichard Lowe  * CDDL HEADER END
20f0089e39SRichard Lowe  */
21f0089e39SRichard Lowe /*
22f0089e39SRichard Lowe  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
23f0089e39SRichard Lowe  */
24f0089e39SRichard Lowe 
25f0089e39SRichard Lowe /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
26f0089e39SRichard Lowe /*	  All Rights Reserved	*/
27f0089e39SRichard Lowe /*
28f0089e39SRichard Lowe  * Copyright (c) 2018, Joyent, Inc.
29f0089e39SRichard Lowe  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
30*ed093b41SRobert Mustacchi  * Copyright 2023 Oxide Computer Company
31f0089e39SRichard Lowe  */
32f0089e39SRichard Lowe 
33f0089e39SRichard Lowe #include <sys/param.h>
34f0089e39SRichard Lowe #include <sys/types.h>
35f0089e39SRichard Lowe #include <sys/vmparam.h>
36f0089e39SRichard Lowe #include <sys/systm.h>
37f0089e39SRichard Lowe #include <sys/signal.h>
38f0089e39SRichard Lowe #include <sys/stack.h>
39f0089e39SRichard Lowe #include <sys/regset.h>
40f0089e39SRichard Lowe #include <sys/privregs.h>
41f0089e39SRichard Lowe #include <sys/frame.h>
42f0089e39SRichard Lowe #include <sys/proc.h>
43f0089e39SRichard Lowe #include <sys/psw.h>
44f0089e39SRichard Lowe #include <sys/siginfo.h>
45f0089e39SRichard Lowe #include <sys/cpuvar.h>
46f0089e39SRichard Lowe #include <sys/asm_linkage.h>
47f0089e39SRichard Lowe #include <sys/kmem.h>
48f0089e39SRichard Lowe #include <sys/errno.h>
49f0089e39SRichard Lowe #include <sys/bootconf.h>
50f0089e39SRichard Lowe #include <sys/archsystm.h>
51f0089e39SRichard Lowe #include <sys/debug.h>
52f0089e39SRichard Lowe #include <sys/elf.h>
53f0089e39SRichard Lowe #include <sys/spl.h>
54f0089e39SRichard Lowe #include <sys/time.h>
55f0089e39SRichard Lowe #include <sys/atomic.h>
56f0089e39SRichard Lowe #include <sys/sysmacros.h>
57f0089e39SRichard Lowe #include <sys/cmn_err.h>
58f0089e39SRichard Lowe #include <sys/modctl.h>
59f0089e39SRichard Lowe #include <sys/kobj.h>
60f0089e39SRichard Lowe #include <sys/panic.h>
61f0089e39SRichard Lowe #include <sys/reboot.h>
62f0089e39SRichard Lowe #include <sys/time.h>
63f0089e39SRichard Lowe #include <sys/fp.h>
64f0089e39SRichard Lowe #include <sys/x86_archext.h>
65f0089e39SRichard Lowe #include <sys/auxv.h>
66f0089e39SRichard Lowe #include <sys/auxv_386.h>
67f0089e39SRichard Lowe #include <sys/dtrace.h>
68f0089e39SRichard Lowe #include <sys/brand.h>
69f0089e39SRichard Lowe #include <sys/machbrand.h>
70f0089e39SRichard Lowe #include <sys/cmn_err.h>
71f0089e39SRichard Lowe 
72f0089e39SRichard Lowe /*
73f0089e39SRichard Lowe  * Map an fnsave-formatted save area into an fxsave-formatted save area.
74f0089e39SRichard Lowe  *
75f0089e39SRichard Lowe  * Most fields are the same width, content and semantics.  However
76f0089e39SRichard Lowe  * the tag word is compressed.
77f0089e39SRichard Lowe  */
78f0089e39SRichard Lowe static void
fnsave_to_fxsave(const struct fnsave_state * fn,struct fxsave_state * fx)79f0089e39SRichard Lowe fnsave_to_fxsave(const struct fnsave_state *fn, struct fxsave_state *fx)
80f0089e39SRichard Lowe {
81f0089e39SRichard Lowe 	uint_t i, tagbits;
82f0089e39SRichard Lowe 
83f0089e39SRichard Lowe 	fx->fx_fcw = fn->f_fcw;
84f0089e39SRichard Lowe 	fx->fx_fsw = fn->f_fsw;
85f0089e39SRichard Lowe 
86f0089e39SRichard Lowe 	/*
87f0089e39SRichard Lowe 	 * copy element by element (because of holes)
88f0089e39SRichard Lowe 	 */
89f0089e39SRichard Lowe 	for (i = 0; i < 8; i++)
90f0089e39SRichard Lowe 		bcopy(&fn->f_st[i].fpr_16[0], &fx->fx_st[i].fpr_16[0],
91f0089e39SRichard Lowe 		    sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */
92f0089e39SRichard Lowe 
93f0089e39SRichard Lowe 	/*
94f0089e39SRichard Lowe 	 * synthesize compressed tag bits
95f0089e39SRichard Lowe 	 */
96f0089e39SRichard Lowe 	fx->fx_fctw = 0;
97f0089e39SRichard Lowe 	for (tagbits = fn->f_ftw, i = 0; i < 8; i++, tagbits >>= 2)
98f0089e39SRichard Lowe 		if ((tagbits & 3) != 3)
99f0089e39SRichard Lowe 			fx->fx_fctw |= (1 << i);
100f0089e39SRichard Lowe 
101f0089e39SRichard Lowe 	fx->fx_fop = fn->f_fop;
102f0089e39SRichard Lowe 
103f0089e39SRichard Lowe 	fx->fx_rip = (uint64_t)fn->f_eip;
104f0089e39SRichard Lowe 	fx->fx_rdp = (uint64_t)fn->f_dp;
105f0089e39SRichard Lowe }
106f0089e39SRichard Lowe 
107f0089e39SRichard Lowe /*
108f0089e39SRichard Lowe  * Map from an fxsave-format save area to an fnsave-format save area.
109f0089e39SRichard Lowe  */
110f0089e39SRichard Lowe static void
fxsave_to_fnsave(const struct fxsave_state * fx,struct fnsave_state * fn)111f0089e39SRichard Lowe fxsave_to_fnsave(const struct fxsave_state *fx, struct fnsave_state *fn)
112f0089e39SRichard Lowe {
113f0089e39SRichard Lowe 	uint_t i, top, tagbits;
114f0089e39SRichard Lowe 
115f0089e39SRichard Lowe 	fn->f_fcw = fx->fx_fcw;
116f0089e39SRichard Lowe 	fn->__f_ign0 = 0;
117f0089e39SRichard Lowe 	fn->f_fsw = fx->fx_fsw;
118f0089e39SRichard Lowe 	fn->__f_ign1 = 0;
119f0089e39SRichard Lowe 
120f0089e39SRichard Lowe 	top = (fx->fx_fsw & FPS_TOP) >> 11;
121f0089e39SRichard Lowe 
122f0089e39SRichard Lowe 	/*
123f0089e39SRichard Lowe 	 * copy element by element (because of holes)
124f0089e39SRichard Lowe 	 */
125f0089e39SRichard Lowe 	for (i = 0; i < 8; i++)
126f0089e39SRichard Lowe 		bcopy(&fx->fx_st[i].fpr_16[0], &fn->f_st[i].fpr_16[0],
127f0089e39SRichard Lowe 		    sizeof (fn->f_st[0].fpr_16)); /* 80-bit x87-style floats */
128f0089e39SRichard Lowe 
129f0089e39SRichard Lowe 	/*
130f0089e39SRichard Lowe 	 * synthesize uncompressed tag bits
131f0089e39SRichard Lowe 	 */
132f0089e39SRichard Lowe 	fn->f_ftw = 0;
133f0089e39SRichard Lowe 	for (tagbits = fx->fx_fctw, i = 0; i < 8; i++, tagbits >>= 1) {
134f0089e39SRichard Lowe 		uint_t ibit, expo;
135f0089e39SRichard Lowe 		const uint16_t *fpp;
136f0089e39SRichard Lowe 		static const uint16_t zero[5] = { 0, 0, 0, 0, 0 };
137f0089e39SRichard Lowe 
138f0089e39SRichard Lowe 		if ((tagbits & 1) == 0) {
139f0089e39SRichard Lowe 			fn->f_ftw |= 3 << (i << 1);	/* empty */
140f0089e39SRichard Lowe 			continue;
141f0089e39SRichard Lowe 		}
142f0089e39SRichard Lowe 
143f0089e39SRichard Lowe 		/*
144f0089e39SRichard Lowe 		 * (tags refer to *physical* registers)
145f0089e39SRichard Lowe 		 */
146f0089e39SRichard Lowe 		fpp = &fx->fx_st[(i - top + 8) & 7].fpr_16[0];
147f0089e39SRichard Lowe 		ibit = fpp[3] >> 15;
148f0089e39SRichard Lowe 		expo = fpp[4] & 0x7fff;
149f0089e39SRichard Lowe 
150f0089e39SRichard Lowe 		if (ibit && expo != 0 && expo != 0x7fff)
151f0089e39SRichard Lowe 			continue;			/* valid fp number */
152f0089e39SRichard Lowe 
153f0089e39SRichard Lowe 		if (bcmp(fpp, &zero, sizeof (zero)))
154f0089e39SRichard Lowe 			fn->f_ftw |= 2 << (i << 1);	/* NaN */
155f0089e39SRichard Lowe 		else
156f0089e39SRichard Lowe 			fn->f_ftw |= 1 << (i << 1);	/* fp zero */
157f0089e39SRichard Lowe 	}
158f0089e39SRichard Lowe 
159f0089e39SRichard Lowe 	fn->f_fop = fx->fx_fop;
160f0089e39SRichard Lowe 
161f0089e39SRichard Lowe 	fn->__f_ign2 = 0;
162f0089e39SRichard Lowe 	fn->f_eip = (uint32_t)fx->fx_rip;
163f0089e39SRichard Lowe 	fn->f_cs = U32CS_SEL;
164f0089e39SRichard Lowe 	fn->f_dp = (uint32_t)fx->fx_rdp;
165f0089e39SRichard Lowe 	fn->f_ds = UDS_SEL;
166f0089e39SRichard Lowe 	fn->__f_ign3 = 0;
167f0089e39SRichard Lowe }
168f0089e39SRichard Lowe 
169f0089e39SRichard Lowe /*
170f0089e39SRichard Lowe  * Map from an fpregset_t into an fxsave-format save area
171f0089e39SRichard Lowe  */
172f0089e39SRichard Lowe static void
fpregset_to_fxsave(const fpregset_t * fp,struct fxsave_state * fx)173f0089e39SRichard Lowe fpregset_to_fxsave(const fpregset_t *fp, struct fxsave_state *fx)
174f0089e39SRichard Lowe {
175f0089e39SRichard Lowe 	bcopy(fp, fx, sizeof (*fx));
176f0089e39SRichard Lowe 	/*
177f0089e39SRichard Lowe 	 * avoid useless #gp exceptions - mask reserved bits
178f0089e39SRichard Lowe 	 */
179f0089e39SRichard Lowe 	fx->fx_mxcsr &= sse_mxcsr_mask;
180f0089e39SRichard Lowe }
181f0089e39SRichard Lowe 
182f0089e39SRichard Lowe /*
183f0089e39SRichard Lowe  * Map from an fxsave-format save area into a fpregset_t
184f0089e39SRichard Lowe  */
185f0089e39SRichard Lowe static void
fxsave_to_fpregset(const struct fxsave_state * fx,fpregset_t * fp)186f0089e39SRichard Lowe fxsave_to_fpregset(const struct fxsave_state *fx, fpregset_t *fp)
187f0089e39SRichard Lowe {
188f0089e39SRichard Lowe 	bcopy(fx, fp, sizeof (*fx));
189f0089e39SRichard Lowe }
190f0089e39SRichard Lowe 
191f0089e39SRichard Lowe #if defined(_SYSCALL32_IMPL)
192f0089e39SRichard Lowe static void
fpregset32_to_fxsave(const fpregset32_t * fp,struct fxsave_state * fx)193f0089e39SRichard Lowe fpregset32_to_fxsave(const fpregset32_t *fp, struct fxsave_state *fx)
194f0089e39SRichard Lowe {
195f0089e39SRichard Lowe 	const struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state;
196f0089e39SRichard Lowe 
197f0089e39SRichard Lowe 	fnsave_to_fxsave((const struct fnsave_state *)fc, fx);
198f0089e39SRichard Lowe 	/*
199f0089e39SRichard Lowe 	 * avoid useless #gp exceptions - mask reserved bits
200f0089e39SRichard Lowe 	 */
201f0089e39SRichard Lowe 	fx->fx_mxcsr = sse_mxcsr_mask & fc->mxcsr;
202f0089e39SRichard Lowe 	bcopy(&fc->xmm[0], &fx->fx_xmm[0], sizeof (fc->xmm));
203f0089e39SRichard Lowe }
204f0089e39SRichard Lowe 
205f0089e39SRichard Lowe static void
fxsave_to_fpregset32(const struct fxsave_state * fx,fpregset32_t * fp)206f0089e39SRichard Lowe fxsave_to_fpregset32(const struct fxsave_state *fx, fpregset32_t *fp)
207f0089e39SRichard Lowe {
208f0089e39SRichard Lowe 	struct fpchip32_state *fc = &fp->fp_reg_set.fpchip_state;
209f0089e39SRichard Lowe 
210f0089e39SRichard Lowe 	fxsave_to_fnsave(fx, (struct fnsave_state *)fc);
211f0089e39SRichard Lowe 	fc->mxcsr = fx->fx_mxcsr;
212f0089e39SRichard Lowe 	bcopy(&fx->fx_xmm[0], &fc->xmm[0], sizeof (fc->xmm));
213f0089e39SRichard Lowe }
214f0089e39SRichard Lowe 
215f0089e39SRichard Lowe static void
fpregset_nto32(const fpregset_t * src,fpregset32_t * dst)216f0089e39SRichard Lowe fpregset_nto32(const fpregset_t *src, fpregset32_t *dst)
217f0089e39SRichard Lowe {
218f0089e39SRichard Lowe 	fxsave_to_fpregset32((struct fxsave_state *)src, dst);
219f0089e39SRichard Lowe 	dst->fp_reg_set.fpchip_state.status =
220f0089e39SRichard Lowe 	    src->fp_reg_set.fpchip_state.status;
221f0089e39SRichard Lowe 	dst->fp_reg_set.fpchip_state.xstatus =
222f0089e39SRichard Lowe 	    src->fp_reg_set.fpchip_state.xstatus;
223f0089e39SRichard Lowe }
224f0089e39SRichard Lowe 
225f0089e39SRichard Lowe static void
fpregset_32ton(const fpregset32_t * src,fpregset_t * dst)226f0089e39SRichard Lowe fpregset_32ton(const fpregset32_t *src, fpregset_t *dst)
227f0089e39SRichard Lowe {
228f0089e39SRichard Lowe 	fpregset32_to_fxsave(src, (struct fxsave_state *)dst);
229f0089e39SRichard Lowe 	dst->fp_reg_set.fpchip_state.status =
230f0089e39SRichard Lowe 	    src->fp_reg_set.fpchip_state.status;
231f0089e39SRichard Lowe 	dst->fp_reg_set.fpchip_state.xstatus =
232f0089e39SRichard Lowe 	    src->fp_reg_set.fpchip_state.xstatus;
233f0089e39SRichard Lowe }
234f0089e39SRichard Lowe #endif
235f0089e39SRichard Lowe 
236f0089e39SRichard Lowe /*
237f0089e39SRichard Lowe  * Set floating-point registers from a native fpregset_t.
238f0089e39SRichard Lowe  */
239f0089e39SRichard Lowe void
setfpregs(klwp_t * lwp,fpregset_t * fp)240f0089e39SRichard Lowe setfpregs(klwp_t *lwp, fpregset_t *fp)
241f0089e39SRichard Lowe {
242*ed093b41SRobert Mustacchi 	fpu_set_fpregset(lwp, fp);
243f0089e39SRichard Lowe }
244f0089e39SRichard Lowe 
245f0089e39SRichard Lowe /*
246f0089e39SRichard Lowe  * Get floating-point registers into a native fpregset_t.
247f0089e39SRichard Lowe  */
248f0089e39SRichard Lowe void
getfpregs(klwp_t * lwp,fpregset_t * fp)249f0089e39SRichard Lowe getfpregs(klwp_t *lwp, fpregset_t *fp)
250f0089e39SRichard Lowe {
251*ed093b41SRobert Mustacchi 	bzero(fp, sizeof (*fp));
252*ed093b41SRobert Mustacchi 	fpu_get_fpregset(lwp, fp);
253f0089e39SRichard Lowe }
254f0089e39SRichard Lowe 
255f0089e39SRichard Lowe #if defined(_SYSCALL32_IMPL)
256f0089e39SRichard Lowe 
257f0089e39SRichard Lowe /*
258f0089e39SRichard Lowe  * Set floating-point registers from an fpregset32_t.
259f0089e39SRichard Lowe  */
260f0089e39SRichard Lowe void
setfpregs32(klwp_t * lwp,fpregset32_t * fp)261f0089e39SRichard Lowe setfpregs32(klwp_t *lwp, fpregset32_t *fp)
262f0089e39SRichard Lowe {
263f0089e39SRichard Lowe 	fpregset_t fpregs;
264f0089e39SRichard Lowe 
265f0089e39SRichard Lowe 	fpregset_32ton(fp, &fpregs);
266f0089e39SRichard Lowe 	setfpregs(lwp, &fpregs);
267f0089e39SRichard Lowe }
268f0089e39SRichard Lowe 
269f0089e39SRichard Lowe /*
270f0089e39SRichard Lowe  * Get floating-point registers into an fpregset32_t.
271f0089e39SRichard Lowe  */
272f0089e39SRichard Lowe void
getfpregs32(klwp_t * lwp,fpregset32_t * fp)273f0089e39SRichard Lowe getfpregs32(klwp_t *lwp, fpregset32_t *fp)
274f0089e39SRichard Lowe {
275f0089e39SRichard Lowe 	fpregset_t fpregs;
276f0089e39SRichard Lowe 
277f0089e39SRichard Lowe 	getfpregs(lwp, &fpregs);
278f0089e39SRichard Lowe 	fpregset_nto32(&fpregs, fp);
279f0089e39SRichard Lowe }
280f0089e39SRichard Lowe 
281f0089e39SRichard Lowe #endif	/* _SYSCALL32_IMPL */
282f0089e39SRichard Lowe 
283f0089e39SRichard Lowe /*
284f0089e39SRichard Lowe  * Return the general registers
285f0089e39SRichard Lowe  */
286f0089e39SRichard Lowe void
getgregs(klwp_t * lwp,gregset_t grp)287f0089e39SRichard Lowe getgregs(klwp_t *lwp, gregset_t grp)
288f0089e39SRichard Lowe {
289f0089e39SRichard Lowe 	struct regs *rp = lwptoregs(lwp);
290f0089e39SRichard Lowe 	struct pcb *pcb = &lwp->lwp_pcb;
291f0089e39SRichard Lowe 	int thisthread = lwptot(lwp) == curthread;
292f0089e39SRichard Lowe 
293f0089e39SRichard Lowe 	grp[REG_RDI] = rp->r_rdi;
294f0089e39SRichard Lowe 	grp[REG_RSI] = rp->r_rsi;
295f0089e39SRichard Lowe 	grp[REG_RDX] = rp->r_rdx;
296f0089e39SRichard Lowe 	grp[REG_RCX] = rp->r_rcx;
297f0089e39SRichard Lowe 	grp[REG_R8] = rp->r_r8;
298f0089e39SRichard Lowe 	grp[REG_R9] = rp->r_r9;
299f0089e39SRichard Lowe 	grp[REG_RAX] = rp->r_rax;
300f0089e39SRichard Lowe 	grp[REG_RBX] = rp->r_rbx;
301f0089e39SRichard Lowe 	grp[REG_RBP] = rp->r_rbp;
302f0089e39SRichard Lowe 	grp[REG_R10] = rp->r_r10;
303f0089e39SRichard Lowe 	grp[REG_R11] = rp->r_r11;
304f0089e39SRichard Lowe 	grp[REG_R12] = rp->r_r12;
305f0089e39SRichard Lowe 	grp[REG_R13] = rp->r_r13;
306f0089e39SRichard Lowe 	grp[REG_R14] = rp->r_r14;
307f0089e39SRichard Lowe 	grp[REG_R15] = rp->r_r15;
308f0089e39SRichard Lowe 	grp[REG_FSBASE] = pcb->pcb_fsbase;
309f0089e39SRichard Lowe 	grp[REG_GSBASE] = pcb->pcb_gsbase;
310f0089e39SRichard Lowe 	if (thisthread)
311f0089e39SRichard Lowe 		kpreempt_disable();
312f0089e39SRichard Lowe 	if (PCB_NEED_UPDATE_SEGS(pcb)) {
313f0089e39SRichard Lowe 		grp[REG_DS] = pcb->pcb_ds;
314f0089e39SRichard Lowe 		grp[REG_ES] = pcb->pcb_es;
315f0089e39SRichard Lowe 		grp[REG_FS] = pcb->pcb_fs;
316f0089e39SRichard Lowe 		grp[REG_GS] = pcb->pcb_gs;
317f0089e39SRichard Lowe 	} else {
318f0089e39SRichard Lowe 		grp[REG_DS] = rp->r_ds;
319f0089e39SRichard Lowe 		grp[REG_ES] = rp->r_es;
320f0089e39SRichard Lowe 		grp[REG_FS] = rp->r_fs;
321f0089e39SRichard Lowe 		grp[REG_GS] = rp->r_gs;
322f0089e39SRichard Lowe 	}
323f0089e39SRichard Lowe 	if (thisthread)
324f0089e39SRichard Lowe 		kpreempt_enable();
325f0089e39SRichard Lowe 	grp[REG_TRAPNO] = rp->r_trapno;
326f0089e39SRichard Lowe 	grp[REG_ERR] = rp->r_err;
327f0089e39SRichard Lowe 	grp[REG_RIP] = rp->r_rip;
328f0089e39SRichard Lowe 	grp[REG_CS] = rp->r_cs;
329f0089e39SRichard Lowe 	grp[REG_SS] = rp->r_ss;
330f0089e39SRichard Lowe 	grp[REG_RFL] = rp->r_rfl;
331f0089e39SRichard Lowe 	grp[REG_RSP] = rp->r_rsp;
332f0089e39SRichard Lowe }
333f0089e39SRichard Lowe 
334f0089e39SRichard Lowe #if defined(_SYSCALL32_IMPL)
335f0089e39SRichard Lowe 
336f0089e39SRichard Lowe void
getgregs32(klwp_t * lwp,gregset32_t grp)337f0089e39SRichard Lowe getgregs32(klwp_t *lwp, gregset32_t grp)
338f0089e39SRichard Lowe {
339f0089e39SRichard Lowe 	struct regs *rp = lwptoregs(lwp);
340f0089e39SRichard Lowe 	struct pcb *pcb = &lwp->lwp_pcb;
341f0089e39SRichard Lowe 	int thisthread = lwptot(lwp) == curthread;
342f0089e39SRichard Lowe 
343f0089e39SRichard Lowe 	if (thisthread)
344f0089e39SRichard Lowe 		kpreempt_disable();
345f0089e39SRichard Lowe 	if (PCB_NEED_UPDATE_SEGS(pcb)) {
346f0089e39SRichard Lowe 		grp[GS] = (uint16_t)pcb->pcb_gs;
347f0089e39SRichard Lowe 		grp[FS] = (uint16_t)pcb->pcb_fs;
348f0089e39SRichard Lowe 		grp[DS] = (uint16_t)pcb->pcb_ds;
349f0089e39SRichard Lowe 		grp[ES] = (uint16_t)pcb->pcb_es;
350f0089e39SRichard Lowe 	} else {
351f0089e39SRichard Lowe 		grp[GS] = (uint16_t)rp->r_gs;
352f0089e39SRichard Lowe 		grp[FS] = (uint16_t)rp->r_fs;
353f0089e39SRichard Lowe 		grp[DS] = (uint16_t)rp->r_ds;
354f0089e39SRichard Lowe 		grp[ES] = (uint16_t)rp->r_es;
355f0089e39SRichard Lowe 	}
356f0089e39SRichard Lowe 	if (thisthread)
357f0089e39SRichard Lowe 		kpreempt_enable();
358f0089e39SRichard Lowe 	grp[EDI] = (greg32_t)rp->r_rdi;
359f0089e39SRichard Lowe 	grp[ESI] = (greg32_t)rp->r_rsi;
360f0089e39SRichard Lowe 	grp[EBP] = (greg32_t)rp->r_rbp;
361f0089e39SRichard Lowe 	grp[ESP] = 0;
362f0089e39SRichard Lowe 	grp[EBX] = (greg32_t)rp->r_rbx;
363f0089e39SRichard Lowe 	grp[EDX] = (greg32_t)rp->r_rdx;
364f0089e39SRichard Lowe 	grp[ECX] = (greg32_t)rp->r_rcx;
365f0089e39SRichard Lowe 	grp[EAX] = (greg32_t)rp->r_rax;
366f0089e39SRichard Lowe 	grp[TRAPNO] = (greg32_t)rp->r_trapno;
367f0089e39SRichard Lowe 	grp[ERR] = (greg32_t)rp->r_err;
368f0089e39SRichard Lowe 	grp[EIP] = (greg32_t)rp->r_rip;
369f0089e39SRichard Lowe 	grp[CS] = (uint16_t)rp->r_cs;
370f0089e39SRichard Lowe 	grp[EFL] = (greg32_t)rp->r_rfl;
371f0089e39SRichard Lowe 	grp[UESP] = (greg32_t)rp->r_rsp;
372f0089e39SRichard Lowe 	grp[SS] = (uint16_t)rp->r_ss;
373f0089e39SRichard Lowe }
374f0089e39SRichard Lowe 
375f0089e39SRichard Lowe void
ucontext_32ton(const ucontext32_t * src,ucontext_t * dst)376f0089e39SRichard Lowe ucontext_32ton(const ucontext32_t *src, ucontext_t *dst)
377f0089e39SRichard Lowe {
378f0089e39SRichard Lowe 	mcontext_t *dmc = &dst->uc_mcontext;
379f0089e39SRichard Lowe 	const mcontext32_t *smc = &src->uc_mcontext;
380f0089e39SRichard Lowe 
381f0089e39SRichard Lowe 	bzero(dst, sizeof (*dst));
382f0089e39SRichard Lowe 	dst->uc_flags = src->uc_flags;
383f0089e39SRichard Lowe 	dst->uc_link = (ucontext_t *)(uintptr_t)src->uc_link;
384f0089e39SRichard Lowe 
385f0089e39SRichard Lowe 	bcopy(&src->uc_sigmask, &dst->uc_sigmask, sizeof (dst->uc_sigmask));
386f0089e39SRichard Lowe 
387f0089e39SRichard Lowe 	dst->uc_stack.ss_sp = (void *)(uintptr_t)src->uc_stack.ss_sp;
388f0089e39SRichard Lowe 	dst->uc_stack.ss_size = (size_t)src->uc_stack.ss_size;
389f0089e39SRichard Lowe 	dst->uc_stack.ss_flags = src->uc_stack.ss_flags;
390f0089e39SRichard Lowe 
391f0089e39SRichard Lowe 	dmc->gregs[REG_GS] = (greg_t)(uint32_t)smc->gregs[GS];
392f0089e39SRichard Lowe 	dmc->gregs[REG_FS] = (greg_t)(uint32_t)smc->gregs[FS];
393f0089e39SRichard Lowe 	dmc->gregs[REG_ES] = (greg_t)(uint32_t)smc->gregs[ES];
394f0089e39SRichard Lowe 	dmc->gregs[REG_DS] = (greg_t)(uint32_t)smc->gregs[DS];
395f0089e39SRichard Lowe 	dmc->gregs[REG_RDI] = (greg_t)(uint32_t)smc->gregs[EDI];
396f0089e39SRichard Lowe 	dmc->gregs[REG_RSI] = (greg_t)(uint32_t)smc->gregs[ESI];
397f0089e39SRichard Lowe 	dmc->gregs[REG_RBP] = (greg_t)(uint32_t)smc->gregs[EBP];
398f0089e39SRichard Lowe 	dmc->gregs[REG_RBX] = (greg_t)(uint32_t)smc->gregs[EBX];
399f0089e39SRichard Lowe 	dmc->gregs[REG_RDX] = (greg_t)(uint32_t)smc->gregs[EDX];
400f0089e39SRichard Lowe 	dmc->gregs[REG_RCX] = (greg_t)(uint32_t)smc->gregs[ECX];
401f0089e39SRichard Lowe 	dmc->gregs[REG_RAX] = (greg_t)(uint32_t)smc->gregs[EAX];
402f0089e39SRichard Lowe 	dmc->gregs[REG_TRAPNO] = (greg_t)(uint32_t)smc->gregs[TRAPNO];
403f0089e39SRichard Lowe 	dmc->gregs[REG_ERR] = (greg_t)(uint32_t)smc->gregs[ERR];
404f0089e39SRichard Lowe 	dmc->gregs[REG_RIP] = (greg_t)(uint32_t)smc->gregs[EIP];
405f0089e39SRichard Lowe 	dmc->gregs[REG_CS] = (greg_t)(uint32_t)smc->gregs[CS];
406f0089e39SRichard Lowe 	dmc->gregs[REG_RFL] = (greg_t)(uint32_t)smc->gregs[EFL];
407f0089e39SRichard Lowe 	dmc->gregs[REG_RSP] = (greg_t)(uint32_t)smc->gregs[UESP];
408f0089e39SRichard Lowe 	dmc->gregs[REG_SS] = (greg_t)(uint32_t)smc->gregs[SS];
409f0089e39SRichard Lowe 
410f0089e39SRichard Lowe 	/*
411f0089e39SRichard Lowe 	 * A valid fpregs is only copied in if uc.uc_flags has UC_FPU set
412f0089e39SRichard Lowe 	 * otherwise there is no guarantee that anything in fpregs is valid.
413f0089e39SRichard Lowe 	 */
414f0089e39SRichard Lowe 	if (src->uc_flags & UC_FPU)
415f0089e39SRichard Lowe 		fpregset_32ton(&src->uc_mcontext.fpregs,
416f0089e39SRichard Lowe 		    &dst->uc_mcontext.fpregs);
417*ed093b41SRobert Mustacchi 
418*ed093b41SRobert Mustacchi 	if (src->uc_flags & UC_XSAVE) {
419*ed093b41SRobert Mustacchi 		dst->uc_xsave = (long)(uint32_t)src->uc_xsave;
420*ed093b41SRobert Mustacchi 	} else {
421*ed093b41SRobert Mustacchi 		dst->uc_xsave = 0;
422*ed093b41SRobert Mustacchi 	}
423f0089e39SRichard Lowe }
424f0089e39SRichard Lowe 
425f0089e39SRichard Lowe #endif	/* _SYSCALL32_IMPL */
426f0089e39SRichard Lowe 
427f0089e39SRichard Lowe /*
428f0089e39SRichard Lowe  * Return the user-level PC.
429f0089e39SRichard Lowe  * If in a system call, return the address of the syscall trap.
430f0089e39SRichard Lowe  */
431f0089e39SRichard Lowe greg_t
getuserpc(void)432*ed093b41SRobert Mustacchi getuserpc(void)
433f0089e39SRichard Lowe {
434f0089e39SRichard Lowe 	greg_t upc = lwptoregs(ttolwp(curthread))->r_pc;
435f0089e39SRichard Lowe 	uint32_t insn;
436f0089e39SRichard Lowe 
437f0089e39SRichard Lowe 	if (curthread->t_sysnum == 0)
438f0089e39SRichard Lowe 		return (upc);
439f0089e39SRichard Lowe 
440f0089e39SRichard Lowe 	/*
441f0089e39SRichard Lowe 	 * We might've gotten here from sysenter (0xf 0x34),
442f0089e39SRichard Lowe 	 * syscall (0xf 0x5) or lcall (0x9a 0 0 0 0 0x27 0).
443f0089e39SRichard Lowe 	 *
444f0089e39SRichard Lowe 	 * Go peek at the binary to figure it out..
445f0089e39SRichard Lowe 	 */
446f0089e39SRichard Lowe 	if (fuword32((void *)(upc - 2), &insn) != -1 &&
447f0089e39SRichard Lowe 	    (insn & 0xffff) == 0x340f || (insn & 0xffff) == 0x050f)
448f0089e39SRichard Lowe 		return (upc - 2);
449f0089e39SRichard Lowe 	return (upc - 7);
450f0089e39SRichard Lowe }
451f0089e39SRichard Lowe 
452f0089e39SRichard Lowe /*
453f0089e39SRichard Lowe  * Protect segment registers from non-user privilege levels and GDT selectors
454f0089e39SRichard Lowe  * other than USER_CS, USER_DS and lwp FS and GS values.  If the segment
455f0089e39SRichard Lowe  * selector is non-null and not USER_CS/USER_DS, we make sure that the
456f0089e39SRichard Lowe  * TI bit is set to point into the LDT and that the RPL is set to 3.
457f0089e39SRichard Lowe  *
458f0089e39SRichard Lowe  * Since struct regs stores each 16-bit segment register as a 32-bit greg_t, we
459f0089e39SRichard Lowe  * also explicitly zero the top 16 bits since they may be coming from the
460f0089e39SRichard Lowe  * user's address space via setcontext(2) or /proc.
461f0089e39SRichard Lowe  *
462f0089e39SRichard Lowe  * Note about null selector. When running on the hypervisor if we allow a
463f0089e39SRichard Lowe  * process to set its %cs to null selector with RPL of 0 the hypervisor will
464f0089e39SRichard Lowe  * crash the domain. If running on bare metal we would get a #gp fault and
465f0089e39SRichard Lowe  * be able to kill the process and continue on. Therefore we make sure to
466f0089e39SRichard Lowe  * force RPL to SEL_UPL even for null selector when setting %cs.
467f0089e39SRichard Lowe  */
468f0089e39SRichard Lowe 
469f0089e39SRichard Lowe #if defined(IS_CS) || defined(IS_NOT_CS)
470f0089e39SRichard Lowe #error	"IS_CS and IS_NOT_CS already defined"
471f0089e39SRichard Lowe #endif
472f0089e39SRichard Lowe 
473f0089e39SRichard Lowe #define	IS_CS		1
474f0089e39SRichard Lowe #define	IS_NOT_CS	0
475f0089e39SRichard Lowe 
476f0089e39SRichard Lowe /*ARGSUSED*/
477f0089e39SRichard Lowe static greg_t
fix_segreg(greg_t sr,int iscs,model_t datamodel)478f0089e39SRichard Lowe fix_segreg(greg_t sr, int iscs, model_t datamodel)
479f0089e39SRichard Lowe {
480f0089e39SRichard Lowe 	switch (sr &= 0xffff) {
481f0089e39SRichard Lowe 
482f0089e39SRichard Lowe 	case 0:
483f0089e39SRichard Lowe 		if (iscs == IS_CS)
484f0089e39SRichard Lowe 			return (0 | SEL_UPL);
485f0089e39SRichard Lowe 		else
486f0089e39SRichard Lowe 			return (0);
487f0089e39SRichard Lowe 
488f0089e39SRichard Lowe 	/*
489f0089e39SRichard Lowe 	 * If lwp attempts to switch data model then force their
490f0089e39SRichard Lowe 	 * code selector to be null selector.
491f0089e39SRichard Lowe 	 */
492f0089e39SRichard Lowe 	case U32CS_SEL:
493f0089e39SRichard Lowe 		if (datamodel == DATAMODEL_NATIVE)
494f0089e39SRichard Lowe 			return (0 | SEL_UPL);
495f0089e39SRichard Lowe 		else
496f0089e39SRichard Lowe 			return (sr);
497f0089e39SRichard Lowe 
498f0089e39SRichard Lowe 	case UCS_SEL:
499f0089e39SRichard Lowe 		if (datamodel == DATAMODEL_ILP32)
500f0089e39SRichard Lowe 			return (0 | SEL_UPL);
501f0089e39SRichard Lowe 	/*FALLTHROUGH*/
502f0089e39SRichard Lowe 	case UDS_SEL:
503f0089e39SRichard Lowe 	case LWPFS_SEL:
504f0089e39SRichard Lowe 	case LWPGS_SEL:
505f0089e39SRichard Lowe 	case SEL_UPL:
506f0089e39SRichard Lowe 		return (sr);
507f0089e39SRichard Lowe 	default:
508f0089e39SRichard Lowe 		break;
509f0089e39SRichard Lowe 	}
510f0089e39SRichard Lowe 
511f0089e39SRichard Lowe 	/*
512f0089e39SRichard Lowe 	 * Force it into the LDT in ring 3 for 32-bit processes, which by
513f0089e39SRichard Lowe 	 * default do not have an LDT, so that any attempt to use an invalid
514f0089e39SRichard Lowe 	 * selector will reference the (non-existant) LDT, and cause a #gp
515f0089e39SRichard Lowe 	 * fault for the process.
516f0089e39SRichard Lowe 	 *
517f0089e39SRichard Lowe 	 * 64-bit processes get the null gdt selector since they
518f0089e39SRichard Lowe 	 * are not allowed to have a private LDT.
519f0089e39SRichard Lowe 	 */
520f0089e39SRichard Lowe 	if (datamodel == DATAMODEL_ILP32) {
521f0089e39SRichard Lowe 		return (sr | SEL_TI_LDT | SEL_UPL);
522f0089e39SRichard Lowe 	} else {
523f0089e39SRichard Lowe 		if (iscs == IS_CS)
524f0089e39SRichard Lowe 			return (0 | SEL_UPL);
525f0089e39SRichard Lowe 		else
526f0089e39SRichard Lowe 			return (0);
527f0089e39SRichard Lowe 	}
528f0089e39SRichard Lowe 
529f0089e39SRichard Lowe }
530f0089e39SRichard Lowe 
531f0089e39SRichard Lowe /*
532f0089e39SRichard Lowe  * Set general registers.
533f0089e39SRichard Lowe  */
534f0089e39SRichard Lowe void
setgregs(klwp_t * lwp,gregset_t grp)535f0089e39SRichard Lowe setgregs(klwp_t *lwp, gregset_t grp)
536f0089e39SRichard Lowe {
537f0089e39SRichard Lowe 	struct regs *rp = lwptoregs(lwp);
538f0089e39SRichard Lowe 	model_t	datamodel = lwp_getdatamodel(lwp);
539f0089e39SRichard Lowe 
540f0089e39SRichard Lowe 	struct pcb *pcb = &lwp->lwp_pcb;
541f0089e39SRichard Lowe 	int thisthread = lwptot(lwp) == curthread;
542f0089e39SRichard Lowe 
543f0089e39SRichard Lowe 	if (datamodel == DATAMODEL_NATIVE) {
544f0089e39SRichard Lowe 		if (thisthread)
545f0089e39SRichard Lowe 			(void) save_syscall_args();	/* copy the args */
546f0089e39SRichard Lowe 
547f0089e39SRichard Lowe 		rp->r_rdi = grp[REG_RDI];
548f0089e39SRichard Lowe 		rp->r_rsi = grp[REG_RSI];
549f0089e39SRichard Lowe 		rp->r_rdx = grp[REG_RDX];
550f0089e39SRichard Lowe 		rp->r_rcx = grp[REG_RCX];
551f0089e39SRichard Lowe 		rp->r_r8 = grp[REG_R8];
552f0089e39SRichard Lowe 		rp->r_r9 = grp[REG_R9];
553f0089e39SRichard Lowe 		rp->r_rax = grp[REG_RAX];
554f0089e39SRichard Lowe 		rp->r_rbx = grp[REG_RBX];
555f0089e39SRichard Lowe 		rp->r_rbp = grp[REG_RBP];
556f0089e39SRichard Lowe 		rp->r_r10 = grp[REG_R10];
557f0089e39SRichard Lowe 		rp->r_r11 = grp[REG_R11];
558f0089e39SRichard Lowe 		rp->r_r12 = grp[REG_R12];
559f0089e39SRichard Lowe 		rp->r_r13 = grp[REG_R13];
560f0089e39SRichard Lowe 		rp->r_r14 = grp[REG_R14];
561f0089e39SRichard Lowe 		rp->r_r15 = grp[REG_R15];
562f0089e39SRichard Lowe 		rp->r_trapno = grp[REG_TRAPNO];
563f0089e39SRichard Lowe 		rp->r_err = grp[REG_ERR];
564f0089e39SRichard Lowe 		rp->r_rip = grp[REG_RIP];
565f0089e39SRichard Lowe 		/*
566f0089e39SRichard Lowe 		 * Setting %cs or %ss to anything else is quietly but
567f0089e39SRichard Lowe 		 * quite definitely forbidden!
568f0089e39SRichard Lowe 		 */
569f0089e39SRichard Lowe 		rp->r_cs = UCS_SEL;
570f0089e39SRichard Lowe 		rp->r_ss = UDS_SEL;
571f0089e39SRichard Lowe 		rp->r_rsp = grp[REG_RSP];
572f0089e39SRichard Lowe 
573f0089e39SRichard Lowe 		if (thisthread)
574f0089e39SRichard Lowe 			kpreempt_disable();
575f0089e39SRichard Lowe 
576f0089e39SRichard Lowe 		pcb->pcb_ds = UDS_SEL;
577f0089e39SRichard Lowe 		pcb->pcb_es = UDS_SEL;
578f0089e39SRichard Lowe 
579f0089e39SRichard Lowe 		/*
580f0089e39SRichard Lowe 		 * 64-bit processes -are- allowed to set their fsbase/gsbase
581f0089e39SRichard Lowe 		 * values directly, but only if they're using the segment
582f0089e39SRichard Lowe 		 * selectors that allow that semantic.
583f0089e39SRichard Lowe 		 *
584f0089e39SRichard Lowe 		 * (32-bit processes must use lwp_set_private().)
585f0089e39SRichard Lowe 		 */
586f0089e39SRichard Lowe 		pcb->pcb_fsbase = grp[REG_FSBASE];
587f0089e39SRichard Lowe 		pcb->pcb_gsbase = grp[REG_GSBASE];
588f0089e39SRichard Lowe 		pcb->pcb_fs = fix_segreg(grp[REG_FS], IS_NOT_CS, datamodel);
589f0089e39SRichard Lowe 		pcb->pcb_gs = fix_segreg(grp[REG_GS], IS_NOT_CS, datamodel);
590f0089e39SRichard Lowe 
591f0089e39SRichard Lowe 		/*
592f0089e39SRichard Lowe 		 * Ensure that we go out via update_sregs
593f0089e39SRichard Lowe 		 */
594f0089e39SRichard Lowe 		PCB_SET_UPDATE_SEGS(pcb);
595f0089e39SRichard Lowe 		lwptot(lwp)->t_post_sys = 1;
596f0089e39SRichard Lowe 		if (thisthread)
597f0089e39SRichard Lowe 			kpreempt_enable();
598f0089e39SRichard Lowe #if defined(_SYSCALL32_IMPL)
599f0089e39SRichard Lowe 	} else {
600f0089e39SRichard Lowe 		rp->r_rdi = (uint32_t)grp[REG_RDI];
601f0089e39SRichard Lowe 		rp->r_rsi = (uint32_t)grp[REG_RSI];
602f0089e39SRichard Lowe 		rp->r_rdx = (uint32_t)grp[REG_RDX];
603f0089e39SRichard Lowe 		rp->r_rcx = (uint32_t)grp[REG_RCX];
604f0089e39SRichard Lowe 		rp->r_rax = (uint32_t)grp[REG_RAX];
605f0089e39SRichard Lowe 		rp->r_rbx = (uint32_t)grp[REG_RBX];
606f0089e39SRichard Lowe 		rp->r_rbp = (uint32_t)grp[REG_RBP];
607f0089e39SRichard Lowe 		rp->r_trapno = (uint32_t)grp[REG_TRAPNO];
608f0089e39SRichard Lowe 		rp->r_err = (uint32_t)grp[REG_ERR];
609f0089e39SRichard Lowe 		rp->r_rip = (uint32_t)grp[REG_RIP];
610f0089e39SRichard Lowe 
611f0089e39SRichard Lowe 		rp->r_cs = fix_segreg(grp[REG_CS], IS_CS, datamodel);
612f0089e39SRichard Lowe 		rp->r_ss = fix_segreg(grp[REG_DS], IS_NOT_CS, datamodel);
613f0089e39SRichard Lowe 
614f0089e39SRichard Lowe 		rp->r_rsp = (uint32_t)grp[REG_RSP];
615f0089e39SRichard Lowe 
616f0089e39SRichard Lowe 		if (thisthread)
617f0089e39SRichard Lowe 			kpreempt_disable();
618f0089e39SRichard Lowe 
619f0089e39SRichard Lowe 		pcb->pcb_ds = fix_segreg(grp[REG_DS], IS_NOT_CS, datamodel);
620f0089e39SRichard Lowe 		pcb->pcb_es = fix_segreg(grp[REG_ES], IS_NOT_CS, datamodel);
621f0089e39SRichard Lowe 
622f0089e39SRichard Lowe 		/*
623f0089e39SRichard Lowe 		 * (See fsbase/gsbase commentary above)
624f0089e39SRichard Lowe 		 */
625f0089e39SRichard Lowe 		pcb->pcb_fs = fix_segreg(grp[REG_FS], IS_NOT_CS, datamodel);
626f0089e39SRichard Lowe 		pcb->pcb_gs = fix_segreg(grp[REG_GS], IS_NOT_CS, datamodel);
627f0089e39SRichard Lowe 
628f0089e39SRichard Lowe 		/*
629f0089e39SRichard Lowe 		 * Ensure that we go out via update_sregs
630f0089e39SRichard Lowe 		 */
631f0089e39SRichard Lowe 		PCB_SET_UPDATE_SEGS(pcb);
632f0089e39SRichard Lowe 		lwptot(lwp)->t_post_sys = 1;
633f0089e39SRichard Lowe 		if (thisthread)
634f0089e39SRichard Lowe 			kpreempt_enable();
635f0089e39SRichard Lowe #endif
636f0089e39SRichard Lowe 	}
637f0089e39SRichard Lowe 
638f0089e39SRichard Lowe 	/*
639f0089e39SRichard Lowe 	 * Only certain bits of the flags register can be modified.
640f0089e39SRichard Lowe 	 */
641f0089e39SRichard Lowe 	rp->r_rfl = (rp->r_rfl & ~PSL_USERMASK) |
642f0089e39SRichard Lowe 	    (grp[REG_RFL] & PSL_USERMASK);
643f0089e39SRichard Lowe }
644f0089e39SRichard Lowe 
645f0089e39SRichard Lowe /*
646f0089e39SRichard Lowe  * Determine whether eip is likely to have an interrupt frame
647f0089e39SRichard Lowe  * on the stack.  We do this by comparing the address to the
648f0089e39SRichard Lowe  * range of addresses spanned by several well-known routines.
649f0089e39SRichard Lowe  */
650f0089e39SRichard Lowe extern void _interrupt();
651f0089e39SRichard Lowe extern void _allsyscalls();
652f0089e39SRichard Lowe extern void _cmntrap();
653f0089e39SRichard Lowe extern void fakesoftint();
654f0089e39SRichard Lowe 
655f0089e39SRichard Lowe extern size_t _interrupt_size;
656f0089e39SRichard Lowe extern size_t _allsyscalls_size;
657f0089e39SRichard Lowe extern size_t _cmntrap_size;
658f0089e39SRichard Lowe extern size_t _fakesoftint_size;
659f0089e39SRichard Lowe 
660f0089e39SRichard Lowe /*
661f0089e39SRichard Lowe  * Get a pc-only stacktrace.  Used for kmem_alloc() buffer ownership tracking.
662f0089e39SRichard Lowe  * Returns MIN(current stack depth, pcstack_limit).
663f0089e39SRichard Lowe  */
664f0089e39SRichard Lowe int
getpcstack(pc_t * pcstack,int pcstack_limit)665f0089e39SRichard Lowe getpcstack(pc_t *pcstack, int pcstack_limit)
666f0089e39SRichard Lowe {
667f0089e39SRichard Lowe 	struct frame *fp = (struct frame *)getfp();
668f0089e39SRichard Lowe 	struct frame *nextfp, *minfp, *stacktop;
669f0089e39SRichard Lowe 	int depth = 0;
670f0089e39SRichard Lowe 	int on_intr;
671f0089e39SRichard Lowe 	uintptr_t pc;
672f0089e39SRichard Lowe 
673f0089e39SRichard Lowe 	if ((on_intr = CPU_ON_INTR(CPU)) != 0)
674f0089e39SRichard Lowe 		stacktop = (struct frame *)(CPU->cpu_intr_stack + SA(MINFRAME));
675f0089e39SRichard Lowe 	else
676f0089e39SRichard Lowe 		stacktop = (struct frame *)curthread->t_stk;
677f0089e39SRichard Lowe 	minfp = fp;
678f0089e39SRichard Lowe 
679f0089e39SRichard Lowe 	pc = ((struct regs *)fp)->r_pc;
680f0089e39SRichard Lowe 
681f0089e39SRichard Lowe 	while (depth < pcstack_limit) {
682f0089e39SRichard Lowe 		nextfp = (struct frame *)fp->fr_savfp;
683f0089e39SRichard Lowe 		pc = fp->fr_savpc;
684f0089e39SRichard Lowe 		if (nextfp <= minfp || nextfp >= stacktop) {
685f0089e39SRichard Lowe 			if (on_intr) {
686f0089e39SRichard Lowe 				/*
687f0089e39SRichard Lowe 				 * Hop from interrupt stack to thread stack.
688f0089e39SRichard Lowe 				 */
689f0089e39SRichard Lowe 				stacktop = (struct frame *)curthread->t_stk;
690f0089e39SRichard Lowe 				minfp = (struct frame *)curthread->t_stkbase;
691f0089e39SRichard Lowe 				on_intr = 0;
692f0089e39SRichard Lowe 				continue;
693f0089e39SRichard Lowe 			}
694f0089e39SRichard Lowe 			break;
695f0089e39SRichard Lowe 		}
696f0089e39SRichard Lowe 		pcstack[depth++] = (pc_t)pc;
697f0089e39SRichard Lowe 		fp = nextfp;
698f0089e39SRichard Lowe 		minfp = fp;
699f0089e39SRichard Lowe 	}
700f0089e39SRichard Lowe 	return (depth);
701f0089e39SRichard Lowe }
702f0089e39SRichard Lowe 
703f0089e39SRichard Lowe /*
704f0089e39SRichard Lowe  * The following ELF header fields are defined as processor-specific
705f0089e39SRichard Lowe  * in the V8 ABI:
706f0089e39SRichard Lowe  *
707f0089e39SRichard Lowe  *	e_ident[EI_DATA]	encoding of the processor-specific
708f0089e39SRichard Lowe  *				data in the object file
709f0089e39SRichard Lowe  *	e_machine		processor identification
710f0089e39SRichard Lowe  *	e_flags			processor-specific flags associated
711f0089e39SRichard Lowe  *				with the file
712f0089e39SRichard Lowe  */
713f0089e39SRichard Lowe 
714f0089e39SRichard Lowe /*
715f0089e39SRichard Lowe  * The value of at_flags reflects a platform's cpu module support.
716f0089e39SRichard Lowe  * at_flags is used to check for allowing a binary to execute and
717f0089e39SRichard Lowe  * is passed as the value of the AT_FLAGS auxiliary vector.
718f0089e39SRichard Lowe  */
719f0089e39SRichard Lowe int at_flags = 0;
720f0089e39SRichard Lowe 
721f0089e39SRichard Lowe /*
722f0089e39SRichard Lowe  * Check the processor-specific fields of an ELF header.
723f0089e39SRichard Lowe  *
724f0089e39SRichard Lowe  * returns 1 if the fields are valid, 0 otherwise
725f0089e39SRichard Lowe  */
726f0089e39SRichard Lowe /*ARGSUSED2*/
727f0089e39SRichard Lowe int
elfheadcheck(unsigned char e_data,Elf32_Half e_machine,Elf32_Word e_flags)728f0089e39SRichard Lowe elfheadcheck(
729f0089e39SRichard Lowe 	unsigned char e_data,
730f0089e39SRichard Lowe 	Elf32_Half e_machine,
731f0089e39SRichard Lowe 	Elf32_Word e_flags)
732f0089e39SRichard Lowe {
733f0089e39SRichard Lowe 	if (e_data != ELFDATA2LSB)
734f0089e39SRichard Lowe 		return (0);
735f0089e39SRichard Lowe 	if (e_machine == EM_AMD64)
736f0089e39SRichard Lowe 		return (1);
737f0089e39SRichard Lowe 	return (e_machine == EM_386);
738f0089e39SRichard Lowe }
739f0089e39SRichard Lowe 
740f0089e39SRichard Lowe uint_t auxv_hwcap_include = 0;	/* patch to enable unrecognized features */
741f0089e39SRichard Lowe uint_t auxv_hwcap_include_2 = 0;	/* second word */
742f0089e39SRichard Lowe uint_t auxv_hwcap_exclude = 0;	/* patch for broken cpus, debugging */
743f0089e39SRichard Lowe uint_t auxv_hwcap_exclude_2 = 0;	/* second word */
744f0089e39SRichard Lowe #if defined(_SYSCALL32_IMPL)
745f0089e39SRichard Lowe uint_t auxv_hwcap32_include = 0;	/* ditto for 32-bit apps */
746f0089e39SRichard Lowe uint_t auxv_hwcap32_include_2 = 0;	/* ditto for 32-bit apps */
747f0089e39SRichard Lowe uint_t auxv_hwcap32_exclude = 0;	/* ditto for 32-bit apps */
748f0089e39SRichard Lowe uint_t auxv_hwcap32_exclude_2 = 0;	/* ditto for 32-bit apps */
749f0089e39SRichard Lowe #endif
750f0089e39SRichard Lowe 
751f0089e39SRichard Lowe /*
752f0089e39SRichard Lowe  * Gather information about the processor and place it into auxv_hwcap
753f0089e39SRichard Lowe  * so that it can be exported to the linker via the aux vector.
754f0089e39SRichard Lowe  *
755f0089e39SRichard Lowe  * We use this seemingly complicated mechanism so that we can ensure
756f0089e39SRichard Lowe  * that /etc/system can be used to override what the system can or
75756726c7eSRobert Mustacchi  * cannot discover for itself. Due to a lack of use, this has not
75856726c7eSRobert Mustacchi  * been extended to the 3rd word.
759f0089e39SRichard Lowe  */
760f0089e39SRichard Lowe void
bind_hwcap(void)761f0089e39SRichard Lowe bind_hwcap(void)
762f0089e39SRichard Lowe {
76356726c7eSRobert Mustacchi 	uint_t cpu_hwcap_flags[3];
764ab5bb018SKeith M Wesolowski 	cpuid_execpass(NULL, CPUID_PASS_RESOLVE, cpu_hwcap_flags);
765f0089e39SRichard Lowe 
766f0089e39SRichard Lowe 	auxv_hwcap = (auxv_hwcap_include | cpu_hwcap_flags[0]) &
767f0089e39SRichard Lowe 	    ~auxv_hwcap_exclude;
768f0089e39SRichard Lowe 	auxv_hwcap_2 = (auxv_hwcap_include_2 | cpu_hwcap_flags[1]) &
769f0089e39SRichard Lowe 	    ~auxv_hwcap_exclude_2;
77056726c7eSRobert Mustacchi 	auxv_hwcap_3 = cpu_hwcap_flags[2];
771f0089e39SRichard Lowe 
772f0089e39SRichard Lowe 	/*
773f0089e39SRichard Lowe 	 * On AMD processors, sysenter just doesn't work at all
774f0089e39SRichard Lowe 	 * when the kernel is in long mode.  On IA-32e processors
775f0089e39SRichard Lowe 	 * it does, but there's no real point in all the alternate
776f0089e39SRichard Lowe 	 * mechanism when syscall works on both.
777f0089e39SRichard Lowe 	 *
778f0089e39SRichard Lowe 	 * Besides, the kernel's sysenter handler is expecting a
779f0089e39SRichard Lowe 	 * 32-bit lwp ...
780f0089e39SRichard Lowe 	 */
781f0089e39SRichard Lowe 	auxv_hwcap &= ~AV_386_SEP;
782f0089e39SRichard Lowe 
783f0089e39SRichard Lowe 	if (auxv_hwcap_include || auxv_hwcap_exclude || auxv_hwcap_include_2 ||
784f0089e39SRichard Lowe 	    auxv_hwcap_exclude_2) {
785f0089e39SRichard Lowe 		/*
786f0089e39SRichard Lowe 		 * The below assignment is regrettably required to get lint
787f0089e39SRichard Lowe 		 * to accept the validity of our format string.  The format
788f0089e39SRichard Lowe 		 * string is in fact valid, but whatever intelligence in lint
789f0089e39SRichard Lowe 		 * understands the cmn_err()-specific %b appears to have an
790f0089e39SRichard Lowe 		 * off-by-one error:  it (mistakenly) complains about bit
791f0089e39SRichard Lowe 		 * number 32 (even though this is explicitly permitted).
792f0089e39SRichard Lowe 		 * Normally, one would will away such warnings with a "LINTED"
793f0089e39SRichard Lowe 		 * directive, but for reasons unclear and unknown, lint
794f0089e39SRichard Lowe 		 * refuses to be assuaged in this case.  Fortunately, lint
795f0089e39SRichard Lowe 		 * doesn't pretend to have solved the Halting Problem --
796f0089e39SRichard Lowe 		 * and as soon as the format string is programmatic, it
797f0089e39SRichard Lowe 		 * knows enough to shut up.
798f0089e39SRichard Lowe 		 */
799f0089e39SRichard Lowe 		char *fmt = "?user ABI extensions: %b\n";
800f0089e39SRichard Lowe 		cmn_err(CE_CONT, fmt, auxv_hwcap, FMT_AV_386);
801f0089e39SRichard Lowe 		fmt = "?user ABI extensions (word 2): %b\n";
802f0089e39SRichard Lowe 		cmn_err(CE_CONT, fmt, auxv_hwcap_2, FMT_AV_386_2);
80356726c7eSRobert Mustacchi 		fmt = "?user ABI extensions (word 2): %b\n";
80456726c7eSRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap_3, FMT_AV_386_3);
805f0089e39SRichard Lowe 	}
806f0089e39SRichard Lowe 
807f0089e39SRichard Lowe #if defined(_SYSCALL32_IMPL)
808f0089e39SRichard Lowe 	auxv_hwcap32 = (auxv_hwcap32_include | cpu_hwcap_flags[0]) &
809f0089e39SRichard Lowe 	    ~auxv_hwcap32_exclude;
810f0089e39SRichard Lowe 	auxv_hwcap32_2 = (auxv_hwcap32_include_2 | cpu_hwcap_flags[1]) &
811f0089e39SRichard Lowe 	    ~auxv_hwcap32_exclude_2;
81256726c7eSRobert Mustacchi 	auxv_hwcap32_3 = auxv_hwcap_3;
813f0089e39SRichard Lowe 
814f0089e39SRichard Lowe 	/*
815f0089e39SRichard Lowe 	 * If this is an amd64 architecture machine from Intel, then
816f0089e39SRichard Lowe 	 * syscall -doesn't- work in compatibility mode, only sysenter does.
817f0089e39SRichard Lowe 	 *
818f0089e39SRichard Lowe 	 * Sigh.
819f0089e39SRichard Lowe 	 */
820f0089e39SRichard Lowe 	if (!cpuid_syscall32_insn(NULL))
821f0089e39SRichard Lowe 		auxv_hwcap32 &= ~AV_386_AMD_SYSC;
822f0089e39SRichard Lowe 
823f0089e39SRichard Lowe 	/*
824f0089e39SRichard Lowe 	 * 32-bit processes can -always- use the lahf/sahf instructions
825f0089e39SRichard Lowe 	 */
826f0089e39SRichard Lowe 	auxv_hwcap32 |= AV_386_AHF;
827f0089e39SRichard Lowe 
828f0089e39SRichard Lowe 	/*
829f0089e39SRichard Lowe 	 * 32-bit processes can -never- use fsgsbase instructions.
830f0089e39SRichard Lowe 	 */
831f0089e39SRichard Lowe 	auxv_hwcap32_2 &= ~AV_386_2_FSGSBASE;
832f0089e39SRichard Lowe 
833f0089e39SRichard Lowe 	if (auxv_hwcap32_include || auxv_hwcap32_exclude ||
834f0089e39SRichard Lowe 	    auxv_hwcap32_include_2 || auxv_hwcap32_exclude_2) {
835f0089e39SRichard Lowe 		/*
836f0089e39SRichard Lowe 		 * See the block comment in the cmn_err() of auxv_hwcap, above.
837f0089e39SRichard Lowe 		 */
838f0089e39SRichard Lowe 		char *fmt = "?32-bit user ABI extensions: %b\n";
839f0089e39SRichard Lowe 		cmn_err(CE_CONT, fmt, auxv_hwcap32, FMT_AV_386);
840f0089e39SRichard Lowe 		fmt = "?32-bit user ABI extensions (word 2): %b\n";
841f0089e39SRichard Lowe 		cmn_err(CE_CONT, fmt, auxv_hwcap32_2, FMT_AV_386_2);
84256726c7eSRobert Mustacchi 		fmt = "?32-bit user ABI extensions (word 3): %b\n";
84356726c7eSRobert Mustacchi 		cmn_err(CE_CONT, fmt, auxv_hwcap32_3, FMT_AV_386_3);
844f0089e39SRichard Lowe 	}
845f0089e39SRichard Lowe #endif
846f0089e39SRichard Lowe }
847f0089e39SRichard Lowe 
848f0089e39SRichard Lowe /*
849f0089e39SRichard Lowe  *	sync_icache() - this is called
850f0089e39SRichard Lowe  *	in proc/fs/prusrio.c. x86 has an unified cache and therefore
851f0089e39SRichard Lowe  *	this is a nop.
852f0089e39SRichard Lowe  */
853f0089e39SRichard Lowe /* ARGSUSED */
854f0089e39SRichard Lowe void
sync_icache(caddr_t addr,uint_t len)855f0089e39SRichard Lowe sync_icache(caddr_t addr, uint_t len)
856f0089e39SRichard Lowe {
857f0089e39SRichard Lowe 	/* Do nothing for now */
858f0089e39SRichard Lowe }
859f0089e39SRichard Lowe 
860f0089e39SRichard Lowe /*ARGSUSED*/
861f0089e39SRichard Lowe void
sync_data_memory(caddr_t va,size_t len)862f0089e39SRichard Lowe sync_data_memory(caddr_t va, size_t len)
863f0089e39SRichard Lowe {
864f0089e39SRichard Lowe 	/* Not implemented for this platform */
865f0089e39SRichard Lowe }
866f0089e39SRichard Lowe 
867f0089e39SRichard Lowe int
__ipltospl(int ipl)868f0089e39SRichard Lowe __ipltospl(int ipl)
869f0089e39SRichard Lowe {
870f0089e39SRichard Lowe 	return (ipltospl(ipl));
871f0089e39SRichard Lowe }
872f0089e39SRichard Lowe 
873f0089e39SRichard Lowe /*
874f0089e39SRichard Lowe  * The panic code invokes panic_saveregs() to record the contents of a
875f0089e39SRichard Lowe  * regs structure into the specified panic_data structure for debuggers.
876f0089e39SRichard Lowe  */
877f0089e39SRichard Lowe void
panic_saveregs(panic_data_t * pdp,struct regs * rp)878f0089e39SRichard Lowe panic_saveregs(panic_data_t *pdp, struct regs *rp)
879f0089e39SRichard Lowe {
880f0089e39SRichard Lowe 	panic_nv_t *pnv = PANICNVGET(pdp);
881f0089e39SRichard Lowe 
882f0089e39SRichard Lowe 	struct cregs	creg;
883f0089e39SRichard Lowe 
884f0089e39SRichard Lowe 	getcregs(&creg);
885f0089e39SRichard Lowe 
886f0089e39SRichard Lowe 	PANICNVADD(pnv, "rdi", rp->r_rdi);
887f0089e39SRichard Lowe 	PANICNVADD(pnv, "rsi", rp->r_rsi);
888f0089e39SRichard Lowe 	PANICNVADD(pnv, "rdx", rp->r_rdx);
889f0089e39SRichard Lowe 	PANICNVADD(pnv, "rcx", rp->r_rcx);
890f0089e39SRichard Lowe 	PANICNVADD(pnv, "r8", rp->r_r8);
891f0089e39SRichard Lowe 	PANICNVADD(pnv, "r9", rp->r_r9);
892f0089e39SRichard Lowe 	PANICNVADD(pnv, "rax", rp->r_rax);
893f0089e39SRichard Lowe 	PANICNVADD(pnv, "rbx", rp->r_rbx);
894f0089e39SRichard Lowe 	PANICNVADD(pnv, "rbp", rp->r_rbp);
895f0089e39SRichard Lowe 	PANICNVADD(pnv, "r10", rp->r_r10);
896f0089e39SRichard Lowe 	PANICNVADD(pnv, "r11", rp->r_r11);
897f0089e39SRichard Lowe 	PANICNVADD(pnv, "r12", rp->r_r12);
898f0089e39SRichard Lowe 	PANICNVADD(pnv, "r13", rp->r_r13);
899f0089e39SRichard Lowe 	PANICNVADD(pnv, "r14", rp->r_r14);
900f0089e39SRichard Lowe 	PANICNVADD(pnv, "r15", rp->r_r15);
901f0089e39SRichard Lowe 	PANICNVADD(pnv, "fsbase", rdmsr(MSR_AMD_FSBASE));
902f0089e39SRichard Lowe 	PANICNVADD(pnv, "gsbase", rdmsr(MSR_AMD_GSBASE));
903f0089e39SRichard Lowe 	PANICNVADD(pnv, "ds", rp->r_ds);
904f0089e39SRichard Lowe 	PANICNVADD(pnv, "es", rp->r_es);
905f0089e39SRichard Lowe 	PANICNVADD(pnv, "fs", rp->r_fs);
906f0089e39SRichard Lowe 	PANICNVADD(pnv, "gs", rp->r_gs);
907f0089e39SRichard Lowe 	PANICNVADD(pnv, "trapno", rp->r_trapno);
908f0089e39SRichard Lowe 	PANICNVADD(pnv, "err", rp->r_err);
909f0089e39SRichard Lowe 	PANICNVADD(pnv, "rip", rp->r_rip);
910f0089e39SRichard Lowe 	PANICNVADD(pnv, "cs", rp->r_cs);
911f0089e39SRichard Lowe 	PANICNVADD(pnv, "rflags", rp->r_rfl);
912f0089e39SRichard Lowe 	PANICNVADD(pnv, "rsp", rp->r_rsp);
913f0089e39SRichard Lowe 	PANICNVADD(pnv, "ss", rp->r_ss);
914f0089e39SRichard Lowe 	PANICNVADD(pnv, "gdt_hi", (uint64_t)(creg.cr_gdt._l[3]));
915f0089e39SRichard Lowe 	PANICNVADD(pnv, "gdt_lo", (uint64_t)(creg.cr_gdt._l[0]));
916f0089e39SRichard Lowe 	PANICNVADD(pnv, "idt_hi", (uint64_t)(creg.cr_idt._l[3]));
917f0089e39SRichard Lowe 	PANICNVADD(pnv, "idt_lo", (uint64_t)(creg.cr_idt._l[0]));
918f0089e39SRichard Lowe 
919f0089e39SRichard Lowe 	PANICNVADD(pnv, "ldt", creg.cr_ldt);
920f0089e39SRichard Lowe 	PANICNVADD(pnv, "task", creg.cr_task);
921f0089e39SRichard Lowe 	PANICNVADD(pnv, "cr0", creg.cr_cr0);
922f0089e39SRichard Lowe 	PANICNVADD(pnv, "cr2", creg.cr_cr2);
923f0089e39SRichard Lowe 	PANICNVADD(pnv, "cr3", creg.cr_cr3);
924f0089e39SRichard Lowe 	if (creg.cr_cr4)
925f0089e39SRichard Lowe 		PANICNVADD(pnv, "cr4", creg.cr_cr4);
926f0089e39SRichard Lowe 
927f0089e39SRichard Lowe 	PANICNVSET(pdp, pnv);
928f0089e39SRichard Lowe }
929f0089e39SRichard Lowe 
930f0089e39SRichard Lowe #define	TR_ARG_MAX 6	/* Max args to print, same as SPARC */
931f0089e39SRichard Lowe 
932f0089e39SRichard Lowe 
933f0089e39SRichard Lowe /*
934f0089e39SRichard Lowe  * Print a stack backtrace using the specified frame pointer.  We delay two
935f0089e39SRichard Lowe  * seconds before continuing, unless this is the panic traceback.
936f0089e39SRichard Lowe  * If we are in the process of panicking, we also attempt to write the
937f0089e39SRichard Lowe  * stack backtrace to a staticly assigned buffer, to allow the panic
938f0089e39SRichard Lowe  * code to find it and write it in to uncompressed pages within the
939f0089e39SRichard Lowe  * system crash dump.
940f0089e39SRichard Lowe  * Note that the frame for the starting stack pointer value is omitted because
941f0089e39SRichard Lowe  * the corresponding %eip is not known.
942f0089e39SRichard Lowe  */
943f0089e39SRichard Lowe 
944f0089e39SRichard Lowe extern char *dump_stack_scratch;
945f0089e39SRichard Lowe 
946f0089e39SRichard Lowe 
947f0089e39SRichard Lowe void
traceback(caddr_t fpreg)948f0089e39SRichard Lowe traceback(caddr_t fpreg)
949f0089e39SRichard Lowe {
950f0089e39SRichard Lowe 	struct frame	*fp = (struct frame *)fpreg;
951f0089e39SRichard Lowe 	struct frame	*nextfp;
952f0089e39SRichard Lowe 	uintptr_t	pc, nextpc;
953f0089e39SRichard Lowe 	ulong_t		off;
954f0089e39SRichard Lowe 	char		args[TR_ARG_MAX * 2 + 16], *sym;
955f0089e39SRichard Lowe 	uint_t	  offset = 0;
956f0089e39SRichard Lowe 	uint_t	  next_offset = 0;
957f0089e39SRichard Lowe 	char	    stack_buffer[1024];
958f0089e39SRichard Lowe 
959f0089e39SRichard Lowe 	if (!panicstr)
960f0089e39SRichard Lowe 		printf("traceback: %%fp = %p\n", (void *)fp);
961f0089e39SRichard Lowe 
962f0089e39SRichard Lowe 	if (panicstr && !dump_stack_scratch) {
963f0089e39SRichard Lowe 		printf("Warning - stack not written to the dump buffer\n");
964f0089e39SRichard Lowe 	}
965f0089e39SRichard Lowe 
966f0089e39SRichard Lowe 	fp = (struct frame *)plat_traceback(fpreg);
967f0089e39SRichard Lowe 	if ((uintptr_t)fp < KERNELBASE)
968f0089e39SRichard Lowe 		goto out;
969f0089e39SRichard Lowe 
970f0089e39SRichard Lowe 	pc = fp->fr_savpc;
971f0089e39SRichard Lowe 	fp = (struct frame *)fp->fr_savfp;
972f0089e39SRichard Lowe 
973f0089e39SRichard Lowe 	while ((uintptr_t)fp >= KERNELBASE) {
974f0089e39SRichard Lowe 		/*
975f0089e39SRichard Lowe 		 * XX64 Until port is complete tolerate 8-byte aligned
976f0089e39SRichard Lowe 		 * frame pointers but flag with a warning so they can
977f0089e39SRichard Lowe 		 * be fixed.
978f0089e39SRichard Lowe 		 */
979f0089e39SRichard Lowe 		if (((uintptr_t)fp & (STACK_ALIGN - 1)) != 0) {
980f0089e39SRichard Lowe 			if (((uintptr_t)fp & (8 - 1)) == 0) {
981f0089e39SRichard Lowe 				printf("  >> warning! 8-byte"
982f0089e39SRichard Lowe 				    " aligned %%fp = %p\n", (void *)fp);
983f0089e39SRichard Lowe 			} else {
984f0089e39SRichard Lowe 				printf(
985f0089e39SRichard Lowe 				    "  >> mis-aligned %%fp = %p\n", (void *)fp);
986f0089e39SRichard Lowe 				break;
987f0089e39SRichard Lowe 			}
988f0089e39SRichard Lowe 		}
989f0089e39SRichard Lowe 
990f0089e39SRichard Lowe 		args[0] = '\0';
991f0089e39SRichard Lowe 		nextpc = (uintptr_t)fp->fr_savpc;
992f0089e39SRichard Lowe 		nextfp = (struct frame *)fp->fr_savfp;
993f0089e39SRichard Lowe 		if ((sym = kobj_getsymname(pc, &off)) != NULL) {
994f0089e39SRichard Lowe 			printf("%016lx %s:%s+%lx (%s)\n", (uintptr_t)fp,
995f0089e39SRichard Lowe 			    mod_containing_pc((caddr_t)pc), sym, off, args);
996f0089e39SRichard Lowe 			(void) snprintf(stack_buffer, sizeof (stack_buffer),
997f0089e39SRichard Lowe 			    "%s:%s+%lx (%s) | ",
998f0089e39SRichard Lowe 			    mod_containing_pc((caddr_t)pc), sym, off, args);
999f0089e39SRichard Lowe 		} else {
1000f0089e39SRichard Lowe 			printf("%016lx %lx (%s)\n",
1001f0089e39SRichard Lowe 			    (uintptr_t)fp, pc, args);
1002f0089e39SRichard Lowe 			(void) snprintf(stack_buffer, sizeof (stack_buffer),
1003f0089e39SRichard Lowe 			    "%lx (%s) | ", pc, args);
1004f0089e39SRichard Lowe 		}
1005f0089e39SRichard Lowe 
1006f0089e39SRichard Lowe 		if (panicstr && dump_stack_scratch) {
1007f0089e39SRichard Lowe 			next_offset = offset + strlen(stack_buffer);
1008f0089e39SRichard Lowe 			if (next_offset < STACK_BUF_SIZE) {
1009f0089e39SRichard Lowe 				bcopy(stack_buffer, dump_stack_scratch + offset,
1010f0089e39SRichard Lowe 				    strlen(stack_buffer));
1011f0089e39SRichard Lowe 				offset = next_offset;
1012f0089e39SRichard Lowe 			} else {
1013f0089e39SRichard Lowe 				/*
1014f0089e39SRichard Lowe 				 * In attempting to save the panic stack
1015f0089e39SRichard Lowe 				 * to the dumpbuf we have overflowed that area.
1016f0089e39SRichard Lowe 				 * Print a warning and continue to printf the
1017f0089e39SRichard Lowe 				 * stack to the msgbuf
1018f0089e39SRichard Lowe 				 */
1019f0089e39SRichard Lowe 				printf("Warning: stack in the dump buffer"
1020f0089e39SRichard Lowe 				    " may be incomplete\n");
1021f0089e39SRichard Lowe 				offset = next_offset;
1022f0089e39SRichard Lowe 			}
1023f0089e39SRichard Lowe 		}
1024f0089e39SRichard Lowe 
1025f0089e39SRichard Lowe 		pc = nextpc;
1026f0089e39SRichard Lowe 		fp = nextfp;
1027f0089e39SRichard Lowe 	}
1028f0089e39SRichard Lowe out:
1029f0089e39SRichard Lowe 	if (!panicstr) {
1030f0089e39SRichard Lowe 		printf("end of traceback\n");
1031f0089e39SRichard Lowe 		DELAY(2 * MICROSEC);
1032f0089e39SRichard Lowe 	} else if (dump_stack_scratch) {
1033f0089e39SRichard Lowe 		dump_stack_scratch[offset] = '\0';
1034f0089e39SRichard Lowe 	}
1035f0089e39SRichard Lowe }
1036f0089e39SRichard Lowe 
1037f0089e39SRichard Lowe 
1038f0089e39SRichard Lowe /*
1039f0089e39SRichard Lowe  * Generate a stack backtrace from a saved register set.
1040f0089e39SRichard Lowe  */
1041f0089e39SRichard Lowe void
traceregs(struct regs * rp)1042f0089e39SRichard Lowe traceregs(struct regs *rp)
1043f0089e39SRichard Lowe {
1044f0089e39SRichard Lowe 	traceback((caddr_t)rp->r_fp);
1045f0089e39SRichard Lowe }
1046f0089e39SRichard Lowe 
1047f0089e39SRichard Lowe void
exec_set_sp(size_t stksize)1048f0089e39SRichard Lowe exec_set_sp(size_t stksize)
1049f0089e39SRichard Lowe {
1050f0089e39SRichard Lowe 	klwp_t *lwp = ttolwp(curthread);
1051f0089e39SRichard Lowe 
1052f0089e39SRichard Lowe 	lwptoregs(lwp)->r_sp = (uintptr_t)curproc->p_usrstack - stksize;
1053f0089e39SRichard Lowe }
1054f0089e39SRichard Lowe 
1055f0089e39SRichard Lowe hrtime_t
gethrtime_waitfree(void)1056f0089e39SRichard Lowe gethrtime_waitfree(void)
1057f0089e39SRichard Lowe {
1058f0089e39SRichard Lowe 	return (dtrace_gethrtime());
1059f0089e39SRichard Lowe }
1060f0089e39SRichard Lowe 
1061f0089e39SRichard Lowe hrtime_t
gethrtime(void)1062f0089e39SRichard Lowe gethrtime(void)
1063f0089e39SRichard Lowe {
1064f0089e39SRichard Lowe 	return (gethrtimef());
1065f0089e39SRichard Lowe }
1066f0089e39SRichard Lowe 
1067f0089e39SRichard Lowe hrtime_t
gethrtime_unscaled(void)1068f0089e39SRichard Lowe gethrtime_unscaled(void)
1069f0089e39SRichard Lowe {
1070f0089e39SRichard Lowe 	return (gethrtimeunscaledf());
1071f0089e39SRichard Lowe }
1072f0089e39SRichard Lowe 
1073f0089e39SRichard Lowe void
scalehrtime(hrtime_t * hrt)1074f0089e39SRichard Lowe scalehrtime(hrtime_t *hrt)
1075f0089e39SRichard Lowe {
1076f0089e39SRichard Lowe 	scalehrtimef(hrt);
1077f0089e39SRichard Lowe }
1078f0089e39SRichard Lowe 
1079f0089e39SRichard Lowe uint64_t
unscalehrtime(hrtime_t nsecs)1080f0089e39SRichard Lowe unscalehrtime(hrtime_t nsecs)
1081f0089e39SRichard Lowe {
1082f0089e39SRichard Lowe 	return (unscalehrtimef(nsecs));
1083f0089e39SRichard Lowe }
1084f0089e39SRichard Lowe 
1085f0089e39SRichard Lowe void
gethrestime(timespec_t * tp)1086f0089e39SRichard Lowe gethrestime(timespec_t *tp)
1087f0089e39SRichard Lowe {
1088f0089e39SRichard Lowe 	gethrestimef(tp);
1089f0089e39SRichard Lowe }
1090f0089e39SRichard Lowe 
1091f0089e39SRichard Lowe /*
1092f0089e39SRichard Lowe  * Part of the implementation of hres_tick(); this routine is
1093f0089e39SRichard Lowe  * easier in C than assembler .. called with the hres_lock held.
1094f0089e39SRichard Lowe  *
1095f0089e39SRichard Lowe  * XX64	Many of these timekeeping variables need to be extern'ed in a header
1096f0089e39SRichard Lowe  */
1097f0089e39SRichard Lowe 
1098f0089e39SRichard Lowe #include <sys/time.h>
1099f0089e39SRichard Lowe #include <sys/machlock.h>
1100f0089e39SRichard Lowe 
1101f0089e39SRichard Lowe extern int one_sec;
1102f0089e39SRichard Lowe extern int max_hres_adj;
1103f0089e39SRichard Lowe 
1104f0089e39SRichard Lowe void
__adj_hrestime(void)1105f0089e39SRichard Lowe __adj_hrestime(void)
1106f0089e39SRichard Lowe {
1107f0089e39SRichard Lowe 	long long adj;
1108f0089e39SRichard Lowe 
1109f0089e39SRichard Lowe 	if (hrestime_adj == 0)
1110f0089e39SRichard Lowe 		adj = 0;
1111f0089e39SRichard Lowe 	else if (hrestime_adj > 0) {
1112f0089e39SRichard Lowe 		if (hrestime_adj < max_hres_adj)
1113f0089e39SRichard Lowe 			adj = hrestime_adj;
1114f0089e39SRichard Lowe 		else
1115f0089e39SRichard Lowe 			adj = max_hres_adj;
1116f0089e39SRichard Lowe 	} else {
1117f0089e39SRichard Lowe 		if (hrestime_adj < -max_hres_adj)
1118f0089e39SRichard Lowe 			adj = -max_hres_adj;
1119f0089e39SRichard Lowe 		else
1120f0089e39SRichard Lowe 			adj = hrestime_adj;
1121f0089e39SRichard Lowe 	}
1122f0089e39SRichard Lowe 
1123f0089e39SRichard Lowe 	timedelta -= adj;
1124f0089e39SRichard Lowe 	hrestime_adj = timedelta;
1125f0089e39SRichard Lowe 	hrestime.tv_nsec += adj;
1126f0089e39SRichard Lowe 
1127f0089e39SRichard Lowe 	while (hrestime.tv_nsec >= NANOSEC) {
1128f0089e39SRichard Lowe 		one_sec++;
1129f0089e39SRichard Lowe 		hrestime.tv_sec++;
1130f0089e39SRichard Lowe 		hrestime.tv_nsec -= NANOSEC;
1131f0089e39SRichard Lowe 	}
1132f0089e39SRichard Lowe }
1133f0089e39SRichard Lowe 
1134f0089e39SRichard Lowe /*
1135f0089e39SRichard Lowe  * Wrapper functions to maintain backwards compability
1136f0089e39SRichard Lowe  */
1137f0089e39SRichard Lowe int
xcopyin(const void * uaddr,void * kaddr,size_t count)1138f0089e39SRichard Lowe xcopyin(const void *uaddr, void *kaddr, size_t count)
1139f0089e39SRichard Lowe {
1140f0089e39SRichard Lowe 	return (xcopyin_nta(uaddr, kaddr, count, UIO_COPY_CACHED));
1141f0089e39SRichard Lowe }
1142f0089e39SRichard Lowe 
1143f0089e39SRichard Lowe int
xcopyout(const void * kaddr,void * uaddr,size_t count)1144f0089e39SRichard Lowe xcopyout(const void *kaddr, void *uaddr, size_t count)
1145f0089e39SRichard Lowe {
1146f0089e39SRichard Lowe 	return (xcopyout_nta(kaddr, uaddr, count, UIO_COPY_CACHED));
1147f0089e39SRichard Lowe }
1148