1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #ifndef	_SYS_SEGMENTS_H
7*0Sstevel@tonic-gate #define	_SYS_SEGMENTS_H
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
10*0Sstevel@tonic-gate 
11*0Sstevel@tonic-gate #ifdef	__cplusplus
12*0Sstevel@tonic-gate extern "C" {
13*0Sstevel@tonic-gate #endif
14*0Sstevel@tonic-gate 
15*0Sstevel@tonic-gate /*
16*0Sstevel@tonic-gate  * Copyright (c) 1989, 1990 William F. Jolitz
17*0Sstevel@tonic-gate  * Copyright (c) 1990 The Regents of the University of California.
18*0Sstevel@tonic-gate  * All rights reserved.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
21*0Sstevel@tonic-gate  * William Jolitz.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *	This product includes software developed by the University of
34*0Sstevel@tonic-gate  *	California, Berkeley and its contributors.
35*0Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
36*0Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
37*0Sstevel@tonic-gate  *    without specific prior written permission.
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
40*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
43*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
44*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
45*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
47*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
48*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
49*0Sstevel@tonic-gate  * SUCH DAMAGE.
50*0Sstevel@tonic-gate  *
51*0Sstevel@tonic-gate  *	from: @(#)segments.h	7.1 (Berkeley) 5/9/91
52*0Sstevel@tonic-gate  * $FreeBSD: src/sys/i386/include/segments.h,v 1.34 2003/09/10 01:07:04
53*0Sstevel@tonic-gate  * jhb Exp $
54*0Sstevel@tonic-gate  *
55*0Sstevel@tonic-gate  * 386 Segmentation Data Structures and definitions
56*0Sstevel@tonic-gate  *	William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <sys/tss.h>
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate /*
62*0Sstevel@tonic-gate  * Selector register format
63*0Sstevel@tonic-gate  * CS, DS, ES, FS, GS, SS
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  *  15                  3  2  1 0
66*0Sstevel@tonic-gate  * +---------------------+---+----+
67*0Sstevel@tonic-gate  * |          SI         |TI |RPL |
68*0Sstevel@tonic-gate  * +---------------------+---+----+
69*0Sstevel@tonic-gate  *
70*0Sstevel@tonic-gate  * SI  = selector index
71*0Sstevel@tonic-gate  * TI  = table indicator (0 = GDT, 1 = LDT)
72*0Sstevel@tonic-gate  * RPL = requestor privilege level
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate #if !defined(_ASM) || defined(__GNUC_AS__)
75*0Sstevel@tonic-gate #define	IDXTOSEL(s)	((s) << 3)		/* index to selector */
76*0Sstevel@tonic-gate #define	SEL_GDT(s, r)	(IDXTOSEL(s) | r)	/* global sel */
77*0Sstevel@tonic-gate #else
78*0Sstevel@tonic-gate #define	IDXTOSEL(s)	[s << 3]
79*0Sstevel@tonic-gate #define	SEL_GDT(s, r)	[IDXTOSEL(s) | r]
80*0Sstevel@tonic-gate #endif
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate #define	SELTOIDX(s)	((s) >> 3)	/* selector to index */
83*0Sstevel@tonic-gate #define	SEL_KPL		0		/* kernel priority level */
84*0Sstevel@tonic-gate #define	SEL_UPL		3		/* user priority level */
85*0Sstevel@tonic-gate #define	SEL_TI_LDT	4		/* local descriptor table */
86*0Sstevel@tonic-gate #define	SEL_LDT(s)	(IDXTOSEL(s) | SEL_TI_LDT | SEL_UPL)	/* local sel */
87*0Sstevel@tonic-gate #define	CPL_MASK	3		/* RPL mask for selector */
88*0Sstevel@tonic-gate #define	SELISLDT(s)	(((s) & SEL_TI_LDT) == SEL_TI_LDT)
89*0Sstevel@tonic-gate #define	SELISUPL(s)	(((s) & CPL_MASK) == SEL_UPL)
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate #ifndef	_ASM
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate typedef	uint16_t	selector_t;	/* selector reigster */
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate  * Hardware descriptor table register format for GDT and IDT.
97*0Sstevel@tonic-gate  */
98*0Sstevel@tonic-gate #if defined(__amd64)
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate #pragma pack(2)
101*0Sstevel@tonic-gate typedef struct {
102*0Sstevel@tonic-gate 	uint16_t dtr_limit;	/* table limit */
103*0Sstevel@tonic-gate 	uint64_t dtr_base;	/* table base address  */
104*0Sstevel@tonic-gate } desctbr_t;
105*0Sstevel@tonic-gate #pragma	pack()
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate #elif defined(__i386)
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate #pragma pack(2)
110*0Sstevel@tonic-gate typedef struct {
111*0Sstevel@tonic-gate 	uint16_t dtr_limit;	/* table limit */
112*0Sstevel@tonic-gate 	uint32_t dtr_base;	/* table base address  */
113*0Sstevel@tonic-gate } desctbr_t;
114*0Sstevel@tonic-gate #pragma	pack()
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate #endif	/* __i386 */
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate /*
119*0Sstevel@tonic-gate  * Functions for loading and storing descriptor table
120*0Sstevel@tonic-gate  * registers.
121*0Sstevel@tonic-gate  */
122*0Sstevel@tonic-gate extern void rd_idtr(desctbr_t *);
123*0Sstevel@tonic-gate extern void wr_idtr(desctbr_t *);
124*0Sstevel@tonic-gate extern void rd_gdtr(desctbr_t *);
125*0Sstevel@tonic-gate extern void wr_gdtr(desctbr_t *);
126*0Sstevel@tonic-gate extern void wr_ldtr(selector_t);
127*0Sstevel@tonic-gate extern selector_t rd_ldtr(void);
128*0Sstevel@tonic-gate extern void wr_tsr(selector_t);
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate #if defined(__amd64)
131*0Sstevel@tonic-gate extern void clr_ldt_sregs(void);
132*0Sstevel@tonic-gate #endif
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate #if !defined(__amd64)
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate /*
137*0Sstevel@tonic-gate  * User segment descriptors (code and data).
138*0Sstevel@tonic-gate  * Legacy mode 64-bits wide.
139*0Sstevel@tonic-gate  */
140*0Sstevel@tonic-gate typedef struct user_desc {
141*0Sstevel@tonic-gate 	uint32_t usd_lolimit:16;	/* segment limit 15:0 */
142*0Sstevel@tonic-gate 	uint32_t usd_lobase:16;		/* segment base 15:0 */
143*0Sstevel@tonic-gate 	uint32_t usd_midbase:8;		/* segment base 23:16 */
144*0Sstevel@tonic-gate 	uint32_t usd_type:5;		/* segment type, includes S bit */
145*0Sstevel@tonic-gate 	uint32_t usd_dpl:2;		/* segment descriptor priority level */
146*0Sstevel@tonic-gate 	uint32_t usd_p:1;		/* segment descriptor present */
147*0Sstevel@tonic-gate 	uint32_t usd_hilimit:4;		/* segment limit 19:16 */
148*0Sstevel@tonic-gate 	uint32_t usd_avl:1;		/* available to sw, but not used */
149*0Sstevel@tonic-gate 	uint32_t usd_reserved:1;	/* unused, ignored */
150*0Sstevel@tonic-gate 	uint32_t usd_def32:1;		/* default 32 vs 16 bit operand */
151*0Sstevel@tonic-gate 	uint32_t usd_gran:1;		/* limit units (bytes vs pages) */
152*0Sstevel@tonic-gate 	uint32_t usd_hibase:8;		/* segment base 31:24 */
153*0Sstevel@tonic-gate } user_desc_t;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate #define	USEGD_GETBASE(usd)		((usd)->usd_lobase |		\
156*0Sstevel@tonic-gate 					(usd)->usd_midbase << 16 |	\
157*0Sstevel@tonic-gate 					(usd)->usd_hibase << (16 + 8))
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate #define	USEGD_SETBASE(usd, b)		((usd)->usd_lobase = (b),	\
160*0Sstevel@tonic-gate 					(usd)->usd_midbase = (b) >> 16, \
161*0Sstevel@tonic-gate 					(usd)->usd_hibase = (b) >> (16 + 8))
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate #define	USEGD_GETLIMIT(usd)		((usd)->usd_lolimit |		\
164*0Sstevel@tonic-gate 					(usd)->usd_hilimit << 16)
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate #define	USEGD_SETLIMIT(usd, lim)	((usd)->usd_lolimit = lim,	\
167*0Sstevel@tonic-gate 					(usd)->usd_hilimit = lim >> 16)
168*0Sstevel@tonic-gate 
169*0Sstevel@tonic-gate #define	USD_TYPESHIFT			5	/* size of usd_type field */
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate #else	/* __amd64 */
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate /*
174*0Sstevel@tonic-gate  * User segment descriptors.
175*0Sstevel@tonic-gate  * Long mode 64-bits wide.
176*0Sstevel@tonic-gate  *
177*0Sstevel@tonic-gate  * In 32-bit compatibility mode (%cs:usd_long=0) all fields are interpreted
178*0Sstevel@tonic-gate  * as in legacy mode for both code and data.
179*0Sstevel@tonic-gate  *
180*0Sstevel@tonic-gate  * In 64-bit mode (%cs:usd_long=1) code segments only have the conforming
181*0Sstevel@tonic-gate  * bit in usd_type, usd_dpl, usd_p, usd_long and usd_def32=0. usd_def32
182*0Sstevel@tonic-gate  * must be zero in 64-bit mode. Setting it to 1 is reserved for future use.
183*0Sstevel@tonic-gate  * All other fields are loaded but ignored by hardware.
184*0Sstevel@tonic-gate  *
185*0Sstevel@tonic-gate  * 64-bit data segments only have usd_p. All other fields are loaded but
186*0Sstevel@tonic-gate  * ignored by hardware when in 64-bit mode.
187*0Sstevel@tonic-gate  */
188*0Sstevel@tonic-gate typedef struct user_desc {
189*0Sstevel@tonic-gate 	uint64_t usd_lolimit:16;	/* segment limit 15:0 */
190*0Sstevel@tonic-gate 	uint64_t usd_lobase:16;		/* segment base 15:0 */
191*0Sstevel@tonic-gate 	uint64_t usd_midbase:8;		/* segment base 23:16 */
192*0Sstevel@tonic-gate 	uint64_t usd_type:5;		/* segment type, includes S bit */
193*0Sstevel@tonic-gate 	uint64_t usd_dpl:2;		/* segment descriptor priority level */
194*0Sstevel@tonic-gate 	uint64_t usd_p:1;		/* segment descriptor present */
195*0Sstevel@tonic-gate 	uint64_t usd_hilimit:4;		/* segment limit 19:16 */
196*0Sstevel@tonic-gate 	uint64_t usd_avl:1;		/* available to sw, but not used */
197*0Sstevel@tonic-gate 	uint64_t usd_long:1;		/* long mode (%cs only) */
198*0Sstevel@tonic-gate 	uint64_t usd_def32:1;		/* default 32 vs 16 bit operand */
199*0Sstevel@tonic-gate 	uint64_t usd_gran:1;		/* limit units (bytes vs page) */
200*0Sstevel@tonic-gate 	uint64_t usd_hibase:8;		/* segment base 31:24 */
201*0Sstevel@tonic-gate } user_desc_t;
202*0Sstevel@tonic-gate 
203*0Sstevel@tonic-gate #define	USEGD_GETBASE(usd)		((usd)->usd_lobase |		\
204*0Sstevel@tonic-gate 					(usd)->usd_midbase << 16 |	\
205*0Sstevel@tonic-gate 					(usd)->usd_hibase << (16 + 8))
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate #define	USEGD_SETBASE(usd, b)		((usd)->usd_lobase = (b),	\
208*0Sstevel@tonic-gate 					(usd)->usd_midbase = (b) >> 16, \
209*0Sstevel@tonic-gate 					(usd)->usd_hibase = (b) >> (16 + 8))
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate #define	USEGD_GETLIMIT(usd)		((usd)->usd_lolimit |		\
212*0Sstevel@tonic-gate 					(usd)->usd_hilimit << 16)
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate #define	USEGD_SETLIMIT(usd, lim)	((usd)->usd_lolimit = lim,	\
215*0Sstevel@tonic-gate 					(usd)->usd_hilimit = lim >> 16)
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate #define	USD_TYPESHIFT			5	/* size of usd_type field */
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate #endif /* __amd64 */
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate #if !defined(__amd64)
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate /*
224*0Sstevel@tonic-gate  * System segment descriptors for LDT and TSS segments.
225*0Sstevel@tonic-gate  * Legacy mode 64-bits wide.
226*0Sstevel@tonic-gate  */
227*0Sstevel@tonic-gate typedef struct system_desc {
228*0Sstevel@tonic-gate 	uint32_t ssd_lolimit:16;	/* segment limit 15:0 */
229*0Sstevel@tonic-gate 	uint32_t ssd_lobase:16;		/* segment base 15:0 */
230*0Sstevel@tonic-gate 	uint32_t ssd_midbase:8;		/* segment base 23:16 */
231*0Sstevel@tonic-gate 	uint32_t ssd_type:4;		/* segment type */
232*0Sstevel@tonic-gate 	uint32_t ssd_zero:1;		/* must be zero */
233*0Sstevel@tonic-gate 	uint32_t ssd_dpl:2;		/* segment descriptor priority level */
234*0Sstevel@tonic-gate 	uint32_t ssd_p:1;		/* segment descriptor present */
235*0Sstevel@tonic-gate 	uint32_t ssd_hilimit:4;		/* segment limit 19:16 */
236*0Sstevel@tonic-gate 	uint32_t ssd_avl:1;		/* available to sw, but not used */
237*0Sstevel@tonic-gate 	uint32_t ssd_reserved:2;	/* unused, ignored */
238*0Sstevel@tonic-gate 	uint32_t ssd_gran:1;		/* limit unit (bytes vs pages) */
239*0Sstevel@tonic-gate 	uint32_t ssd_hibase:8;		/* segment base 31:24 */
240*0Sstevel@tonic-gate } system_desc_t;
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate #else	/* __amd64 */
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate /*
245*0Sstevel@tonic-gate  * System segment descriptors for LDT and TSS segments.
246*0Sstevel@tonic-gate  * Long mode 128-bits wide.
247*0Sstevel@tonic-gate  *
248*0Sstevel@tonic-gate  * 32-bit LDT and TSS descriptor types are redefined to 64-bit equivalents.
249*0Sstevel@tonic-gate  * All other legacy types are reserved and illegal.
250*0Sstevel@tonic-gate  */
251*0Sstevel@tonic-gate typedef struct system_desc {
252*0Sstevel@tonic-gate 	uint64_t ssd_lolimit:16;	/* segment limit 15:0 */
253*0Sstevel@tonic-gate 	uint64_t ssd_lobase:16;		/* segment base 15:0 */
254*0Sstevel@tonic-gate 	uint64_t ssd_midbase:8;		/* segment base 23:16 */
255*0Sstevel@tonic-gate 	uint64_t ssd_type:4;		/* segment type */
256*0Sstevel@tonic-gate 	uint64_t ssd_zero1:1;		/* must be zero */
257*0Sstevel@tonic-gate 	uint64_t ssd_dpl:2;		/* segment descriptor priority level */
258*0Sstevel@tonic-gate 	uint64_t ssd_p:1;		/* segment descriptor present */
259*0Sstevel@tonic-gate 	uint64_t ssd_hilimit:4;		/* segment limit 19:16 */
260*0Sstevel@tonic-gate 	uint64_t ssd_avl:1;		/* available to sw, but not used */
261*0Sstevel@tonic-gate 	uint64_t ssd_resv1:2;		/* unused, ignored */
262*0Sstevel@tonic-gate 	uint64_t ssd_gran:1;		/* limit unit (bytes vs pages) */
263*0Sstevel@tonic-gate 	uint64_t ssd_hibase:8;		/* segment base 31:24 */
264*0Sstevel@tonic-gate 	uint64_t ssd_hi64base:32;	/* segment base 63:32 */
265*0Sstevel@tonic-gate 	uint64_t ssd_resv2:8;		/* unused, ignored */
266*0Sstevel@tonic-gate 	uint64_t ssd_zero2:5;		/* must be zero */
267*0Sstevel@tonic-gate 	uint64_t ssd_resv3:19;		/* unused, ignored */
268*0Sstevel@tonic-gate } system_desc_t;
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate #endif	/* __amd64 */
271*0Sstevel@tonic-gate 
272*0Sstevel@tonic-gate #define	SYSSEGD_SETLIMIT(ssd, lim)	((ssd)->ssd_lolimit = lim,	\
273*0Sstevel@tonic-gate 					(ssd)->ssd_hilimit = lim >> 16)
274*0Sstevel@tonic-gate #if !defined(__amd64)
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate /*
277*0Sstevel@tonic-gate  * System gate segment descriptors for interrupt, trap, call and task gates.
278*0Sstevel@tonic-gate  * Legacy mode 64-bits wide.
279*0Sstevel@tonic-gate  */
280*0Sstevel@tonic-gate typedef struct gate_desc {
281*0Sstevel@tonic-gate 	uint32_t sgd_looffset:16;	/* segment code offset 15:0 */
282*0Sstevel@tonic-gate 	uint32_t sgd_selector:16;	/* target code or task selector */
283*0Sstevel@tonic-gate 	uint32_t sgd_stkcpy:5;		/* number of stack wds to cpy */
284*0Sstevel@tonic-gate 	uint32_t sgd_resv:3;		/* unused, ignored */
285*0Sstevel@tonic-gate 	uint32_t sgd_type:5;		/* segment type, includes S bit */
286*0Sstevel@tonic-gate 	uint32_t sgd_dpl:2;		/* segment descriptor priority level */
287*0Sstevel@tonic-gate 	uint32_t sgd_p:1;		/* segment descriptor present */
288*0Sstevel@tonic-gate 	uint32_t sgd_hioffset:16;	/* code seg off 31:16 */
289*0Sstevel@tonic-gate } gate_desc_t;
290*0Sstevel@tonic-gate 
291*0Sstevel@tonic-gate #define	GATESEG_GETOFFSET(sgd)		((sgd)->sgd_looffset |		\
292*0Sstevel@tonic-gate 					(sgd)->sgd_hioffset << 16)
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate #else	/* __amd64 */
295*0Sstevel@tonic-gate 
296*0Sstevel@tonic-gate /*
297*0Sstevel@tonic-gate  * System segment descriptors for interrupt, trap and call gates.
298*0Sstevel@tonic-gate  * Long mode 128-bits wide.
299*0Sstevel@tonic-gate  *
300*0Sstevel@tonic-gate  * 32-bit interrupt, trap and call gate types are redefined to 64-bit
301*0Sstevel@tonic-gate  * equivalents. Task gates along with all other legacy types are reserved
302*0Sstevel@tonic-gate  * and illegal.
303*0Sstevel@tonic-gate  */
304*0Sstevel@tonic-gate typedef struct gate_desc {
305*0Sstevel@tonic-gate 	uint64_t sgd_looffset:16;	/* segment code offset 15:0 */
306*0Sstevel@tonic-gate 	uint64_t sgd_selector:16;	/* target code or task selector */
307*0Sstevel@tonic-gate 	uint64_t sgd_ist:3;		/* IST table index */
308*0Sstevel@tonic-gate 	uint64_t sgd_resv1:5;		/* unused, ignored */
309*0Sstevel@tonic-gate 	uint64_t sgd_type:5;		/* segment type, includes S bit */
310*0Sstevel@tonic-gate 	uint64_t sgd_dpl:2;		/* segment descriptor priority level */
311*0Sstevel@tonic-gate 	uint64_t sgd_p:1;		/* segment descriptor present */
312*0Sstevel@tonic-gate 	uint64_t sgd_hioffset:16;	/* segment code offset 31:16 */
313*0Sstevel@tonic-gate 	uint64_t sgd_hi64offset:32;	/* segment code offset 63:32 */
314*0Sstevel@tonic-gate 	uint64_t sgd_resv2:8;		/* unused, ignored */
315*0Sstevel@tonic-gate 	uint64_t sgd_zero:5;		/* call gate only: must be zero */
316*0Sstevel@tonic-gate 	uint64_t sgd_resv3:19;		/* unused, ignored */
317*0Sstevel@tonic-gate } gate_desc_t;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate #define	GATESEG_GETOFFSET(sgd)		((sgd)->sgd_looffset |		\
320*0Sstevel@tonic-gate 					(sgd)->sgd_hioffset << 16 |	\
321*0Sstevel@tonic-gate 					(sgd)->sgd_hi64offset << 32)
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate #endif	/* __amd64 */
324*0Sstevel@tonic-gate 
325*0Sstevel@tonic-gate /*
326*0Sstevel@tonic-gate  * functions for initializing and updating segment descriptors.
327*0Sstevel@tonic-gate  */
328*0Sstevel@tonic-gate #if defined(__amd64)
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate extern void set_usegd(user_desc_t *, uint_t, void *, size_t, uint_t, uint_t,
331*0Sstevel@tonic-gate     uint_t, uint_t);
332*0Sstevel@tonic-gate extern void set_gatesegd(gate_desc_t *, void (*)(void), selector_t, uint_t,
333*0Sstevel@tonic-gate     uint_t, uint_t);
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate #elif defined(__i386)
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate extern void set_usegd(user_desc_t *, void *, size_t, uint_t, uint_t,
338*0Sstevel@tonic-gate     uint_t, uint_t);
339*0Sstevel@tonic-gate extern void set_gatesegd(gate_desc_t *, void (*)(void), selector_t,
340*0Sstevel@tonic-gate     uint_t, uint_t, uint_t);
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate #endif	/* __i386 */
343*0Sstevel@tonic-gate 
344*0Sstevel@tonic-gate void set_syssegd(system_desc_t *, void *, size_t, uint_t, uint_t);
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate #endif	/* _ASM */
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate /*
349*0Sstevel@tonic-gate  * Common segment parameter defintions for granularity, default
350*0Sstevel@tonic-gate  * operand size and operaton mode.
351*0Sstevel@tonic-gate  */
352*0Sstevel@tonic-gate #define	SDP_BYTES	0	/* segment limit scaled to bytes */
353*0Sstevel@tonic-gate #define	SDP_PAGES	1	/* segment limit scaled to pages */
354*0Sstevel@tonic-gate #define	SDP_OP32	1	/* code and data default operand = 32 bits */
355*0Sstevel@tonic-gate #define	SDP_LONG	1	/* long mode code segment (64 bits) */
356*0Sstevel@tonic-gate #define	SDP_SHORT	0	/* compat/legacy code segment (32 bits) */
357*0Sstevel@tonic-gate /*
358*0Sstevel@tonic-gate  * System segments and gate types.
359*0Sstevel@tonic-gate  *
360*0Sstevel@tonic-gate  * In long mode i386 32-bit ldt, tss, call, interrupt and trap gate
361*0Sstevel@tonic-gate  * types are redefined into 64-bit equivalents.
362*0Sstevel@tonic-gate  */
363*0Sstevel@tonic-gate #define	SDT_SYSNULL	 0	/* system null */
364*0Sstevel@tonic-gate #define	SDT_SYS286TSS	 1	/* system 286 TSS available */
365*0Sstevel@tonic-gate #define	SDT_SYSLDT	 2	/* system local descriptor table */
366*0Sstevel@tonic-gate #define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
367*0Sstevel@tonic-gate #define	SDT_SYS286CGT	 4	/* system 286 call gate */
368*0Sstevel@tonic-gate #define	SDT_SYSTASKGT	 5	/* system task gate */
369*0Sstevel@tonic-gate #define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
370*0Sstevel@tonic-gate #define	SDT_SYS286TGT	 7	/* system 286 trap gate */
371*0Sstevel@tonic-gate #define	SDT_SYSNULL2	 8	/* system null again */
372*0Sstevel@tonic-gate #define	SDT_SYSTSS	 9	/* system TSS available */
373*0Sstevel@tonic-gate #define	SDT_SYSNULL3	10	/* system null again */
374*0Sstevel@tonic-gate #define	SDT_SYSTSSBSY	11	/* system TSS busy */
375*0Sstevel@tonic-gate #define	SDT_SYSCGT	12	/* system call gate */
376*0Sstevel@tonic-gate #define	SDT_SYSNULL4	13	/* system null again */
377*0Sstevel@tonic-gate #define	SDT_SYSIGT	14	/* system interrupt gate */
378*0Sstevel@tonic-gate #define	SDT_SYSTGT	15	/* system trap gate */
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate /*
381*0Sstevel@tonic-gate  * Memory segment types.
382*0Sstevel@tonic-gate  *
383*0Sstevel@tonic-gate  * While in long mode expand-down, writable and accessed type field
384*0Sstevel@tonic-gate  * attributes are ignored. Only the conforming bit is loaded by hardware
385*0Sstevel@tonic-gate  * for long mode code segment descriptors.
386*0Sstevel@tonic-gate  */
387*0Sstevel@tonic-gate #define	SDT_MEMRO	16	/* read only */
388*0Sstevel@tonic-gate #define	SDT_MEMROA	17	/* read only accessed */
389*0Sstevel@tonic-gate #define	SDT_MEMRW	18	/* read write */
390*0Sstevel@tonic-gate #define	SDT_MEMRWA	19	/* read write accessed */
391*0Sstevel@tonic-gate #define	SDT_MEMROD	20	/* read only expand dwn limit */
392*0Sstevel@tonic-gate #define	SDT_MEMRODA	21	/* read only expand dwn limit accessed */
393*0Sstevel@tonic-gate #define	SDT_MEMRWD	22	/* read write expand dwn limit */
394*0Sstevel@tonic-gate #define	SDT_MEMRWDA	23	/* read write expand dwn limit accessed */
395*0Sstevel@tonic-gate #define	SDT_MEME	24	/* execute only */
396*0Sstevel@tonic-gate #define	SDT_MEMEA	25	/* execute only accessed */
397*0Sstevel@tonic-gate #define	SDT_MEMER	26	/* execute read */
398*0Sstevel@tonic-gate #define	SDT_MEMERA	27	/* execute read accessed */
399*0Sstevel@tonic-gate #define	SDT_MEMEC	28	/* execute only conforming */
400*0Sstevel@tonic-gate #define	SDT_MEMEAC	29	/* execute only accessed conforming */
401*0Sstevel@tonic-gate #define	SDT_MEMERC	30	/* execute read conforming */
402*0Sstevel@tonic-gate #define	SDT_MEMERAC	31	/* execute read accessed conforming */
403*0Sstevel@tonic-gate 
404*0Sstevel@tonic-gate /*
405*0Sstevel@tonic-gate  * Entries in the Interrupt Descriptor Table (IDT)
406*0Sstevel@tonic-gate  */
407*0Sstevel@tonic-gate #define	IDT_DE		0	/* #DE: Divide Error */
408*0Sstevel@tonic-gate #define	IDT_DB		1	/* #DB: Debug */
409*0Sstevel@tonic-gate #define	IDT_NMI		2	/* Nonmaskable External Interrupt */
410*0Sstevel@tonic-gate #define	IDT_BP		3	/* #BP: Breakpoint */
411*0Sstevel@tonic-gate #define	IDT_OF		4	/* #OF: Overflow */
412*0Sstevel@tonic-gate #define	IDT_BR		5	/* #BR: Bound Range Exceeded */
413*0Sstevel@tonic-gate #define	IDT_UD		6	/* #UD: Undefined/Invalid Opcode */
414*0Sstevel@tonic-gate #define	IDT_NM		7	/* #NM: No Math Coprocessor */
415*0Sstevel@tonic-gate #define	IDT_DF		8	/* #DF: Double Fault */
416*0Sstevel@tonic-gate #define	IDT_FPUGP	9	/* Coprocessor Segment Overrun */
417*0Sstevel@tonic-gate #define	IDT_TS		10	/* #TS: Invalid TSS */
418*0Sstevel@tonic-gate #define	IDT_NP		11	/* #NP: Segment Not Present */
419*0Sstevel@tonic-gate #define	IDT_SS		12	/* #SS: Stack Segment Fault */
420*0Sstevel@tonic-gate #define	IDT_GP		13	/* #GP: General Protection Fault */
421*0Sstevel@tonic-gate #define	IDT_PF		14	/* #PF: Page Fault */
422*0Sstevel@tonic-gate #define	IDT_MF		16	/* #MF: FPU Floating-Point Error */
423*0Sstevel@tonic-gate #define	IDT_AC		17	/* #AC: Alignment Check */
424*0Sstevel@tonic-gate #define	IDT_MC		18	/* #MC: Machine Check */
425*0Sstevel@tonic-gate #define	IDT_XF		19	/* #XF: SIMD Floating-Point Exception */
426*0Sstevel@tonic-gate #define	NIDT		256	/* size in entries of IDT */
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate /*
429*0Sstevel@tonic-gate  * Entries in the Global Descriptor Table (GDT)
430*0Sstevel@tonic-gate  *
431*0Sstevel@tonic-gate  * We make sure to space the system descriptors (LDT's, TSS')
432*0Sstevel@tonic-gate  * such that they are double gdt slot aligned. This is because
433*0Sstevel@tonic-gate  * in long mode system segment decriptors expand to 128 bits.
434*0Sstevel@tonic-gate  *
435*0Sstevel@tonic-gate  * GDT_LWPFS and GDT_LWPGS must be the same for both 32 and 64-bit
436*0Sstevel@tonic-gate  * kernels. See setup_context in libc.
437*0Sstevel@tonic-gate  */
438*0Sstevel@tonic-gate #if defined(__amd64)
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate #define	GDT_NULL	0	/* null */
441*0Sstevel@tonic-gate #define	GDT_B32DATA	1	/* copied from boot */
442*0Sstevel@tonic-gate #define	GDT_B32CODE	2	/* copied from boot */
443*0Sstevel@tonic-gate #define	GDT_B64DATA	3	/* copied from boot */
444*0Sstevel@tonic-gate #define	GDT_B64CODE	4	/* copied from boot */
445*0Sstevel@tonic-gate #define	GDT_KCODE	5	/* kernel code seg %cs */
446*0Sstevel@tonic-gate #define	GDT_KDATA	6	/* kernel data seg %ds */
447*0Sstevel@tonic-gate #define	GDT_U32CODE	7	/* 32-bit process on 64-bit kernel %cs */
448*0Sstevel@tonic-gate #define	GDT_UDATA	8	/* user data seg %ds (32 and 64 bit) */
449*0Sstevel@tonic-gate #define	GDT_UCODE	9	/* native user code  seg %cs */
450*0Sstevel@tonic-gate #define	GDT_LDT		10	/* LDT for current process */
451*0Sstevel@tonic-gate #define	GDT_KTSS	12	/* kernel tss */
452*0Sstevel@tonic-gate #define	GDT_FS		GDT_NULL /* kernel %fs segment selector */
453*0Sstevel@tonic-gate #define	GDT_GS		GDT_NULL /* kernel %gs segment selector */
454*0Sstevel@tonic-gate #define	GDT_LWPFS	55	/* lwp private %fs segment selector */
455*0Sstevel@tonic-gate #define	GDT_LWPGS	56	/* lwp private %gs segment selector */
456*0Sstevel@tonic-gate #define	NGDT		58	/* number of entries in GDT */
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate /*
459*0Sstevel@tonic-gate  * This selector is only used in the temporary GDT used to bring additional
460*0Sstevel@tonic-gate  * CPUs from 16-bit real mode into long mode in real_mode_start().
461*0Sstevel@tonic-gate  */
462*0Sstevel@tonic-gate #define	TEMPGDT_KCODE64	1	/* 64-bit code selector */
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate #elif defined(__i386)
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate #define	GDT_NULL	0	/* null */
467*0Sstevel@tonic-gate #define	GDT_BOOTFLAT	1	/* copied from boot */
468*0Sstevel@tonic-gate #define	GDT_BOOTCODE	2	/* copied from boot */
469*0Sstevel@tonic-gate #define	GDT_BOOTCODE16	3	/* copied from boot */
470*0Sstevel@tonic-gate #define	GDT_BOOTDATA	4	/* copied from boot */
471*0Sstevel@tonic-gate #define	GDT_LDT		40	/* LDT for current process */
472*0Sstevel@tonic-gate #define	GDT_KTSS	42	/* kernel tss */
473*0Sstevel@tonic-gate #define	GDT_KCODE	43	/* kernel code seg %cs */
474*0Sstevel@tonic-gate #define	GDT_KDATA	44	/* kernel data seg %ds */
475*0Sstevel@tonic-gate #define	GDT_UCODE	45	/* native user code  seg %cs */
476*0Sstevel@tonic-gate #define	GDT_UDATA	46	/* user data seg %ds (32 and 64 bit) */
477*0Sstevel@tonic-gate #define	GDT_DBFLT	47	/* double fault #DF selector */
478*0Sstevel@tonic-gate #define	GDT_FS		53	/* kernel %fs segment selector */
479*0Sstevel@tonic-gate #define	GDT_GS		54	/* kernel %gs segment selector */
480*0Sstevel@tonic-gate #define	GDT_LWPFS	55	/* lwp private %fs segment selector */
481*0Sstevel@tonic-gate #define	GDT_LWPGS	56	/* lwp private %gs segment selector */
482*0Sstevel@tonic-gate #define	NGDT		90	/* number of entries in GDT */
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate #endif	/* __i386 */
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate /*
487*0Sstevel@tonic-gate  * Convenient selector definitions.
488*0Sstevel@tonic-gate  */
489*0Sstevel@tonic-gate #define	KCS_SEL		SEL_GDT(GDT_KCODE, SEL_KPL)
490*0Sstevel@tonic-gate #define	KDS_SEL		SEL_GDT(GDT_KDATA, SEL_KPL)
491*0Sstevel@tonic-gate #define	UCS_SEL		SEL_GDT(GDT_UCODE, SEL_UPL)
492*0Sstevel@tonic-gate #if defined(__amd64)
493*0Sstevel@tonic-gate #define	TEMP_CS64_SEL	SEL_GDT(TEMPGDT_KCODE64, SEL_KPL)
494*0Sstevel@tonic-gate #define	U32CS_SEL	SEL_GDT(GDT_U32CODE, SEL_UPL)
495*0Sstevel@tonic-gate #endif	/* __amd64 */
496*0Sstevel@tonic-gate #define	UDS_SEL		SEL_GDT(GDT_UDATA, SEL_UPL)
497*0Sstevel@tonic-gate #define	ULDT_SEL	SEL_GDT(GDT_LDT, SEL_KPL)
498*0Sstevel@tonic-gate #define	KTSS_SEL	SEL_GDT(GDT_KTSS, SEL_KPL)
499*0Sstevel@tonic-gate #define	DFTSS_SEL	SEL_GDT(GDT_DBFLT, SEL_KPL)
500*0Sstevel@tonic-gate #define	KFS_SEL		SEL_GDT(GDT_NULL, SEL_KPL)
501*0Sstevel@tonic-gate #define	KGS_SEL		SEL_GDT(GDT_GS, SEL_KPL)
502*0Sstevel@tonic-gate #define	LWPFS_SEL	SEL_GDT(GDT_LWPFS, SEL_UPL)
503*0Sstevel@tonic-gate #define	LWPGS_SEL	SEL_GDT(GDT_LWPGS, SEL_UPL)
504*0Sstevel@tonic-gate #if defined(__amd64)
505*0Sstevel@tonic-gate #define	B64CODE_SEL	SEL_GDT(GDT_B64CODE, SEL_KPL)
506*0Sstevel@tonic-gate #else
507*0Sstevel@tonic-gate #define	BOOTCODE_SEL	SEL_GDT(GDT_BOOTCODE, SEL_KPL)
508*0Sstevel@tonic-gate #define	BOOTFLAT_SEL	SEL_GDT(GDT_BOOTFLAT, SEL_KPL)
509*0Sstevel@tonic-gate #endif
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate /*
512*0Sstevel@tonic-gate  * Entries in default Local Descriptor Table (LDT) for every process.
513*0Sstevel@tonic-gate  */
514*0Sstevel@tonic-gate #define	LDT_SYSCALL	0	/* call gate for libc.a (obsolete) */
515*0Sstevel@tonic-gate #define	LDT_SIGCALL	1	/* EOL me, call gate for static sigreturn */
516*0Sstevel@tonic-gate #define	LDT_RESVD1	2	/* old user %cs */
517*0Sstevel@tonic-gate #define	LDT_RESVD2	3	/* old user %ds */
518*0Sstevel@tonic-gate #define	LDT_ALTSYSCALL	4	/* alternate call gate for system calls */
519*0Sstevel@tonic-gate #define	LDT_ALTSIGCALL	5	/* EOL me, alternate call gate for sigreturn */
520*0Sstevel@tonic-gate #define	LDT_UDBASE	6	/* user descriptor base index */
521*0Sstevel@tonic-gate #define	MINNLDT		64	/* Current min solaris ldt size */
522*0Sstevel@tonic-gate #define	MAXNLDT		8192	/* max solaris ldt size */
523*0Sstevel@tonic-gate 
524*0Sstevel@tonic-gate #ifndef	_ASM
525*0Sstevel@tonic-gate 
526*0Sstevel@tonic-gate #pragma	align	16(idt0)
527*0Sstevel@tonic-gate extern	gate_desc_t	idt0[NIDT];
528*0Sstevel@tonic-gate extern	desctbr_t	idt0_default_reg;
529*0Sstevel@tonic-gate #pragma	align	16(gdt0)
530*0Sstevel@tonic-gate extern	user_desc_t	gdt0[NGDT];
531*0Sstevel@tonic-gate #pragma	align	16(ldt0_default)
532*0Sstevel@tonic-gate extern	user_desc_t	ldt0_default[MINNLDT];
533*0Sstevel@tonic-gate extern	system_desc_t	ldt0_default_desc;
534*0Sstevel@tonic-gate #pragma align	16(ldt0_default64)
535*0Sstevel@tonic-gate extern user_desc_t	ldt0_default64[MINNLDT];
536*0Sstevel@tonic-gate extern system_desc_t	ldt0_default64_desc;
537*0Sstevel@tonic-gate extern user_desc_t	zero_udesc;
538*0Sstevel@tonic-gate #if defined(__amd64)
539*0Sstevel@tonic-gate extern user_desc_t	zero_u32desc;
540*0Sstevel@tonic-gate #endif
541*0Sstevel@tonic-gate 
542*0Sstevel@tonic-gate #pragma	align	16(ktss0)
543*0Sstevel@tonic-gate extern struct tss ktss0;
544*0Sstevel@tonic-gate 
545*0Sstevel@tonic-gate #if defined(__i386)
546*0Sstevel@tonic-gate extern struct tss dftss0;
547*0Sstevel@tonic-gate #endif	/* __i386 */
548*0Sstevel@tonic-gate 
549*0Sstevel@tonic-gate extern void div0trap(), dbgtrap(), nmiint(), brktrap(), ovflotrap();
550*0Sstevel@tonic-gate extern void boundstrap(), invoptrap(), ndptrap(), syserrtrap();
551*0Sstevel@tonic-gate extern void invaltrap(), invtsstrap(), segnptrap(), stktrap();
552*0Sstevel@tonic-gate extern void gptrap(), pftrap(), ndperr();
553*0Sstevel@tonic-gate extern void overrun(), resvtrap();
554*0Sstevel@tonic-gate extern void _start(), cmnint();
555*0Sstevel@tonic-gate extern void achktrap(), mcetrap();
556*0Sstevel@tonic-gate extern void xmtrap();
557*0Sstevel@tonic-gate extern void fasttrap();
558*0Sstevel@tonic-gate extern void dtrace_fasttrap(), dtrace_ret();
559*0Sstevel@tonic-gate 
560*0Sstevel@tonic-gate #if !defined(__amd64)
561*0Sstevel@tonic-gate extern void pentium_pftrap();
562*0Sstevel@tonic-gate #endif
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate #endif /* _ASM */
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate #ifdef	__cplusplus
567*0Sstevel@tonic-gate }
568*0Sstevel@tonic-gate #endif
569*0Sstevel@tonic-gate 
570*0Sstevel@tonic-gate #endif /* _SYS_SEGMENTS_H */
571