1*9c8f3233SToomas Soome /*-
2*9c8f3233SToomas Soome  * Copyright (c) 1989, 1990 William F. Jolitz
3*9c8f3233SToomas Soome  * Copyright (c) 1990 The Regents of the University of California.
4*9c8f3233SToomas Soome  * All rights reserved.
5*9c8f3233SToomas Soome  *
6*9c8f3233SToomas Soome  * This code is derived from software contributed to Berkeley by
7*9c8f3233SToomas Soome  * William Jolitz.
8*9c8f3233SToomas Soome  *
9*9c8f3233SToomas Soome  * Redistribution and use in source and binary forms, with or without
10*9c8f3233SToomas Soome  * modification, are permitted provided that the following conditions
11*9c8f3233SToomas Soome  * are met:
12*9c8f3233SToomas Soome  * 1. Redistributions of source code must retain the above copyright
13*9c8f3233SToomas Soome  *    notice, this list of conditions and the following disclaimer.
14*9c8f3233SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
15*9c8f3233SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
16*9c8f3233SToomas Soome  *    documentation and/or other materials provided with the distribution.
17*9c8f3233SToomas Soome  * 3. Neither the name of the University nor the names of its contributors
18*9c8f3233SToomas Soome  *    may be used to endorse or promote products derived from this software
19*9c8f3233SToomas Soome  *    without specific prior written permission.
20*9c8f3233SToomas Soome  *
21*9c8f3233SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22*9c8f3233SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23*9c8f3233SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24*9c8f3233SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25*9c8f3233SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*9c8f3233SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27*9c8f3233SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28*9c8f3233SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29*9c8f3233SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30*9c8f3233SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*9c8f3233SToomas Soome  * SUCH DAMAGE.
32*9c8f3233SToomas Soome  *
33*9c8f3233SToomas Soome  *	from: @(#)segments.h	7.1 (Berkeley) 5/9/91
34*9c8f3233SToomas Soome  */
35*9c8f3233SToomas Soome 
36*9c8f3233SToomas Soome #ifndef _X86_SEGMENTS_H_
37*9c8f3233SToomas Soome #define	_X86_SEGMENTS_H_
38*9c8f3233SToomas Soome 
39*9c8f3233SToomas Soome /*
40*9c8f3233SToomas Soome  * X86 Segmentation Data Structures and definitions
41*9c8f3233SToomas Soome  */
42*9c8f3233SToomas Soome 
43*9c8f3233SToomas Soome /*
44*9c8f3233SToomas Soome  * Selectors
45*9c8f3233SToomas Soome  */
46*9c8f3233SToomas Soome #define	SEL_RPL_MASK	3		/* requester priv level */
47*9c8f3233SToomas Soome #define	ISPL(s)		((s)&3)		/* priority level of a selector */
48*9c8f3233SToomas Soome #define	SEL_KPL		0		/* kernel priority level */
49*9c8f3233SToomas Soome #define	SEL_UPL		3		/* user priority level */
50*9c8f3233SToomas Soome #define	ISLDT(s)	((s)&SEL_LDT)	/* is it local or global */
51*9c8f3233SToomas Soome #define	SEL_LDT		4		/* local descriptor table */
52*9c8f3233SToomas Soome #define	IDXSEL(s)	(((s)>>3) & 0x1fff) /* index of selector */
53*9c8f3233SToomas Soome #define	LSEL(s,r)	(((s)<<3) | SEL_LDT | r) /* a local selector */
54*9c8f3233SToomas Soome #define	GSEL(s,r)	(((s)<<3) | r)	/* a global selector */
55*9c8f3233SToomas Soome 
56*9c8f3233SToomas Soome /*
57*9c8f3233SToomas Soome  * User segment descriptors (%cs, %ds etc for i386 apps. 64 bit wide)
58*9c8f3233SToomas Soome  * For long-mode apps, %cs only has the conforming bit in sd_type, the sd_dpl,
59*9c8f3233SToomas Soome  * sd_p, sd_l and sd_def32 which must be zero).  %ds only has sd_p.
60*9c8f3233SToomas Soome  */
61*9c8f3233SToomas Soome struct segment_descriptor {
62*9c8f3233SToomas Soome 	unsigned sd_lolimit:16;		/* segment extent (lsb) */
63*9c8f3233SToomas Soome 	unsigned sd_lobase:24;		/* segment base address (lsb) */
64*9c8f3233SToomas Soome 	unsigned sd_type:5;		/* segment type */
65*9c8f3233SToomas Soome 	unsigned sd_dpl:2;		/* segment descriptor priority level */
66*9c8f3233SToomas Soome 	unsigned sd_p:1;		/* segment descriptor present */
67*9c8f3233SToomas Soome 	unsigned sd_hilimit:4;		/* segment extent (msb) */
68*9c8f3233SToomas Soome 	unsigned sd_xx:2;		/* unused */
69*9c8f3233SToomas Soome 	unsigned sd_def32:1;		/* default 32 vs 16 bit size */
70*9c8f3233SToomas Soome 	unsigned sd_gran:1;		/* limit granularity (byte/page units)*/
71*9c8f3233SToomas Soome 	unsigned sd_hibase:8;		/* segment base address  (msb) */
72*9c8f3233SToomas Soome } __packed;
73*9c8f3233SToomas Soome 
74*9c8f3233SToomas Soome struct user_segment_descriptor {
75*9c8f3233SToomas Soome 	unsigned sd_lolimit:16;		/* segment extent (lsb) */
76*9c8f3233SToomas Soome 	unsigned sd_lobase:24;		/* segment base address (lsb) */
77*9c8f3233SToomas Soome 	unsigned sd_type:5;		/* segment type */
78*9c8f3233SToomas Soome 	unsigned sd_dpl:2;		/* segment descriptor priority level */
79*9c8f3233SToomas Soome 	unsigned sd_p:1;		/* segment descriptor present */
80*9c8f3233SToomas Soome 	unsigned sd_hilimit:4;		/* segment extent (msb) */
81*9c8f3233SToomas Soome 	unsigned sd_xx:1;		/* unused */
82*9c8f3233SToomas Soome 	unsigned sd_long:1;		/* long mode (cs only) */
83*9c8f3233SToomas Soome 	unsigned sd_def32:1;		/* default 32 vs 16 bit size */
84*9c8f3233SToomas Soome 	unsigned sd_gran:1;		/* limit granularity (byte/page units)*/
85*9c8f3233SToomas Soome 	unsigned sd_hibase:8;		/* segment base address  (msb) */
86*9c8f3233SToomas Soome } __packed;
87*9c8f3233SToomas Soome 
88*9c8f3233SToomas Soome #define	USD_GETBASE(sd)		(((sd)->sd_lobase) | (sd)->sd_hibase << 24)
89*9c8f3233SToomas Soome #define	USD_SETBASE(sd, b)	(sd)->sd_lobase = (b);	\
90*9c8f3233SToomas Soome 				(sd)->sd_hibase = ((b) >> 24);
91*9c8f3233SToomas Soome #define	USD_GETLIMIT(sd)	(((sd)->sd_lolimit) | (sd)->sd_hilimit << 16)
92*9c8f3233SToomas Soome #define	USD_SETLIMIT(sd, l)	(sd)->sd_lolimit = (l);	\
93*9c8f3233SToomas Soome 				(sd)->sd_hilimit = ((l) >> 16);
94*9c8f3233SToomas Soome 
95*9c8f3233SToomas Soome #ifdef __i386__
96*9c8f3233SToomas Soome /*
97*9c8f3233SToomas Soome  * Gate descriptors (e.g. indirect descriptors)
98*9c8f3233SToomas Soome  */
99*9c8f3233SToomas Soome struct gate_descriptor {
100*9c8f3233SToomas Soome 	unsigned gd_looffset:16;	/* gate offset (lsb) */
101*9c8f3233SToomas Soome 	unsigned gd_selector:16;	/* gate segment selector */
102*9c8f3233SToomas Soome 	unsigned gd_stkcpy:5;		/* number of stack wds to cpy */
103*9c8f3233SToomas Soome 	unsigned gd_xx:3;		/* unused */
104*9c8f3233SToomas Soome 	unsigned gd_type:5;		/* segment type */
105*9c8f3233SToomas Soome 	unsigned gd_dpl:2;		/* segment descriptor priority level */
106*9c8f3233SToomas Soome 	unsigned gd_p:1;		/* segment descriptor present */
107*9c8f3233SToomas Soome 	unsigned gd_hioffset:16;	/* gate offset (msb) */
108*9c8f3233SToomas Soome } __packed;
109*9c8f3233SToomas Soome 
110*9c8f3233SToomas Soome /*
111*9c8f3233SToomas Soome  * Generic descriptor
112*9c8f3233SToomas Soome  */
113*9c8f3233SToomas Soome union descriptor {
114*9c8f3233SToomas Soome 	struct segment_descriptor sd;
115*9c8f3233SToomas Soome 	struct gate_descriptor gd;
116*9c8f3233SToomas Soome };
117*9c8f3233SToomas Soome #else
118*9c8f3233SToomas Soome /*
119*9c8f3233SToomas Soome  * Gate descriptors (e.g. indirect descriptors, trap, interrupt etc. 128 bit)
120*9c8f3233SToomas Soome  * Only interrupt and trap gates have gd_ist.
121*9c8f3233SToomas Soome  */
122*9c8f3233SToomas Soome struct gate_descriptor {
123*9c8f3233SToomas Soome 	uint64_t gd_looffset:16;	/* gate offset (lsb) */
124*9c8f3233SToomas Soome 	uint64_t gd_selector:16;	/* gate segment selector */
125*9c8f3233SToomas Soome 	uint64_t gd_ist:3;		/* IST table index */
126*9c8f3233SToomas Soome 	uint64_t gd_xx:5;		/* unused */
127*9c8f3233SToomas Soome 	uint64_t gd_type:5;		/* segment type */
128*9c8f3233SToomas Soome 	uint64_t gd_dpl:2;		/* segment descriptor priority level */
129*9c8f3233SToomas Soome 	uint64_t gd_p:1;		/* segment descriptor present */
130*9c8f3233SToomas Soome 	uint64_t gd_hioffset:48;	/* gate offset (msb) */
131*9c8f3233SToomas Soome 	uint64_t sd_xx1:32;
132*9c8f3233SToomas Soome } __packed;
133*9c8f3233SToomas Soome 
134*9c8f3233SToomas Soome /*
135*9c8f3233SToomas Soome  * Generic descriptor
136*9c8f3233SToomas Soome  */
137*9c8f3233SToomas Soome union descriptor {
138*9c8f3233SToomas Soome 	struct user_segment_descriptor sd;
139*9c8f3233SToomas Soome 	struct gate_descriptor gd;
140*9c8f3233SToomas Soome };
141*9c8f3233SToomas Soome #endif
142*9c8f3233SToomas Soome 
143*9c8f3233SToomas Soome 	/* system segments and gate types */
144*9c8f3233SToomas Soome #define	SDT_SYSNULL	 0	/* system null */
145*9c8f3233SToomas Soome #define	SDT_SYS286TSS	 1	/* system 286 TSS available */
146*9c8f3233SToomas Soome #define	SDT_SYSLDT	 2	/* system local descriptor table */
147*9c8f3233SToomas Soome #define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
148*9c8f3233SToomas Soome #define	SDT_SYS286CGT	 4	/* system 286 call gate */
149*9c8f3233SToomas Soome #define	SDT_SYSTASKGT	 5	/* system task gate */
150*9c8f3233SToomas Soome #define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
151*9c8f3233SToomas Soome #define	SDT_SYS286TGT	 7	/* system 286 trap gate */
152*9c8f3233SToomas Soome #define	SDT_SYSNULL2	 8	/* system null again */
153*9c8f3233SToomas Soome #define	SDT_SYS386TSS	 9	/* system 386 TSS available */
154*9c8f3233SToomas Soome #define	SDT_SYSTSS	 9	/* system available 64 bit TSS */
155*9c8f3233SToomas Soome #define	SDT_SYSNULL3	10	/* system null again */
156*9c8f3233SToomas Soome #define	SDT_SYS386BSY	11	/* system 386 TSS busy */
157*9c8f3233SToomas Soome #define	SDT_SYSBSY	11	/* system busy 64 bit TSS */
158*9c8f3233SToomas Soome #define	SDT_SYS386CGT	12	/* system 386 call gate */
159*9c8f3233SToomas Soome #define	SDT_SYSCGT	12	/* system 64 bit call gate */
160*9c8f3233SToomas Soome #define	SDT_SYSNULL4	13	/* system null again */
161*9c8f3233SToomas Soome #define	SDT_SYS386IGT	14	/* system 386 interrupt gate */
162*9c8f3233SToomas Soome #define	SDT_SYSIGT	14	/* system 64 bit interrupt gate */
163*9c8f3233SToomas Soome #define	SDT_SYS386TGT	15	/* system 386 trap gate */
164*9c8f3233SToomas Soome #define	SDT_SYSTGT	15	/* system 64 bit trap gate */
165*9c8f3233SToomas Soome 
166*9c8f3233SToomas Soome 	/* memory segment types */
167*9c8f3233SToomas Soome #define	SDT_MEMRO	16	/* memory read only */
168*9c8f3233SToomas Soome #define	SDT_MEMROA	17	/* memory read only accessed */
169*9c8f3233SToomas Soome #define	SDT_MEMRW	18	/* memory read write */
170*9c8f3233SToomas Soome #define	SDT_MEMRWA	19	/* memory read write accessed */
171*9c8f3233SToomas Soome #define	SDT_MEMROD	20	/* memory read only expand dwn limit */
172*9c8f3233SToomas Soome #define	SDT_MEMRODA	21	/* memory read only expand dwn limit accessed */
173*9c8f3233SToomas Soome #define	SDT_MEMRWD	22	/* memory read write expand dwn limit */
174*9c8f3233SToomas Soome #define	SDT_MEMRWDA	23	/* memory read write expand dwn limit accessed*/
175*9c8f3233SToomas Soome #define	SDT_MEME	24	/* memory execute only */
176*9c8f3233SToomas Soome #define	SDT_MEMEA	25	/* memory execute only accessed */
177*9c8f3233SToomas Soome #define	SDT_MEMER	26	/* memory execute read */
178*9c8f3233SToomas Soome #define	SDT_MEMERA	27	/* memory execute read accessed */
179*9c8f3233SToomas Soome #define	SDT_MEMEC	28	/* memory execute only conforming */
180*9c8f3233SToomas Soome #define	SDT_MEMEAC	29	/* memory execute only accessed conforming */
181*9c8f3233SToomas Soome #define	SDT_MEMERC	30	/* memory execute read conforming */
182*9c8f3233SToomas Soome #define	SDT_MEMERAC	31	/* memory execute read accessed conforming */
183*9c8f3233SToomas Soome 
184*9c8f3233SToomas Soome /*
185*9c8f3233SToomas Soome  * Size of IDT table
186*9c8f3233SToomas Soome  */
187*9c8f3233SToomas Soome #define	NIDT		256	/* 32 reserved, 0x80 syscall, most are h/w */
188*9c8f3233SToomas Soome #define	NRSVIDT		32	/* reserved entries for cpu exceptions */
189*9c8f3233SToomas Soome 
190*9c8f3233SToomas Soome /*
191*9c8f3233SToomas Soome  * Entries in the Interrupt Descriptor Table (IDT)
192*9c8f3233SToomas Soome  */
193*9c8f3233SToomas Soome #define	IDT_DE		0	/* #DE: Divide Error */
194*9c8f3233SToomas Soome #define	IDT_DB		1	/* #DB: Debug */
195*9c8f3233SToomas Soome #define	IDT_NMI		2	/* Nonmaskable External Interrupt */
196*9c8f3233SToomas Soome #define	IDT_BP		3	/* #BP: Breakpoint */
197*9c8f3233SToomas Soome #define	IDT_OF		4	/* #OF: Overflow */
198*9c8f3233SToomas Soome #define	IDT_BR		5	/* #BR: Bound Range Exceeded */
199*9c8f3233SToomas Soome #define	IDT_UD		6	/* #UD: Undefined/Invalid Opcode */
200*9c8f3233SToomas Soome #define	IDT_NM		7	/* #NM: No Math Coprocessor */
201*9c8f3233SToomas Soome #define	IDT_DF		8	/* #DF: Double Fault */
202*9c8f3233SToomas Soome #define	IDT_FPUGP	9	/* Coprocessor Segment Overrun */
203*9c8f3233SToomas Soome #define	IDT_TS		10	/* #TS: Invalid TSS */
204*9c8f3233SToomas Soome #define	IDT_NP		11	/* #NP: Segment Not Present */
205*9c8f3233SToomas Soome #define	IDT_SS		12	/* #SS: Stack Segment Fault */
206*9c8f3233SToomas Soome #define	IDT_GP		13	/* #GP: General Protection Fault */
207*9c8f3233SToomas Soome #define	IDT_PF		14	/* #PF: Page Fault */
208*9c8f3233SToomas Soome #define	IDT_MF		16	/* #MF: FPU Floating-Point Error */
209*9c8f3233SToomas Soome #define	IDT_AC		17	/* #AC: Alignment Check */
210*9c8f3233SToomas Soome #define	IDT_MC		18	/* #MC: Machine Check */
211*9c8f3233SToomas Soome #define	IDT_XF		19	/* #XF: SIMD Floating-Point Exception */
212*9c8f3233SToomas Soome #define	IDT_IO_INTS	NRSVIDT	/* Base of IDT entries for I/O interrupts. */
213*9c8f3233SToomas Soome #define	IDT_SYSCALL	0x80	/* System Call Interrupt Vector */
214*9c8f3233SToomas Soome #define	IDT_DTRACE_RET	0x92	/* DTrace pid provider Interrupt Vector */
215*9c8f3233SToomas Soome #define	IDT_EVTCHN	0x93	/* Xen HVM Event Channel Interrupt Vector */
216*9c8f3233SToomas Soome 
217*9c8f3233SToomas Soome #if defined(__i386__)
218*9c8f3233SToomas Soome /*
219*9c8f3233SToomas Soome  * Entries in the Global Descriptor Table (GDT)
220*9c8f3233SToomas Soome  * Note that each 4 entries share a single 32 byte L1 cache line.
221*9c8f3233SToomas Soome  * Some of the fast syscall instructions require a specific order here.
222*9c8f3233SToomas Soome  */
223*9c8f3233SToomas Soome #define	GNULL_SEL	0	/* Null Descriptor */
224*9c8f3233SToomas Soome #define	GPRIV_SEL	1	/* SMP Per-Processor Private Data */
225*9c8f3233SToomas Soome #define	GUFS_SEL	2	/* User %fs Descriptor (order critical: 1) */
226*9c8f3233SToomas Soome #define	GUGS_SEL	3	/* User %gs Descriptor (order critical: 2) */
227*9c8f3233SToomas Soome #define	GCODE_SEL	4	/* Kernel Code Descriptor (order critical: 1) */
228*9c8f3233SToomas Soome #define	GDATA_SEL	5	/* Kernel Data Descriptor (order critical: 2) */
229*9c8f3233SToomas Soome #define	GUCODE_SEL	6	/* User Code Descriptor (order critical: 3) */
230*9c8f3233SToomas Soome #define	GUDATA_SEL	7	/* User Data Descriptor (order critical: 4) */
231*9c8f3233SToomas Soome #define	GBIOSLOWMEM_SEL	8	/* BIOS low memory access (must be entry 8) */
232*9c8f3233SToomas Soome #define	GPROC0_SEL	9	/* Task state process slot zero and up */
233*9c8f3233SToomas Soome #define	GLDT_SEL	10	/* Default User LDT */
234*9c8f3233SToomas Soome #define	GUSERLDT_SEL	11	/* User LDT */
235*9c8f3233SToomas Soome #define	GPANIC_SEL	12	/* Task state to consider panic from */
236*9c8f3233SToomas Soome #define	GBIOSCODE32_SEL	13	/* BIOS interface (32bit Code) */
237*9c8f3233SToomas Soome #define	GBIOSCODE16_SEL	14	/* BIOS interface (16bit Code) */
238*9c8f3233SToomas Soome #define	GBIOSDATA_SEL	15	/* BIOS interface (Data) */
239*9c8f3233SToomas Soome #define	GBIOSUTIL_SEL	16	/* BIOS interface (Utility) */
240*9c8f3233SToomas Soome #define	GBIOSARGS_SEL	17	/* BIOS interface (Arguments) */
241*9c8f3233SToomas Soome #define	GNDIS_SEL	18	/* For the NDIS layer */
242*9c8f3233SToomas Soome #define	NGDT		19
243*9c8f3233SToomas Soome 
244*9c8f3233SToomas Soome /*
245*9c8f3233SToomas Soome  * Entries in the Local Descriptor Table (LDT)
246*9c8f3233SToomas Soome  */
247*9c8f3233SToomas Soome #define	LSYS5CALLS_SEL	0	/* forced by intel BCS */
248*9c8f3233SToomas Soome #define	LSYS5SIGR_SEL	1
249*9c8f3233SToomas Soome #define	LUCODE_SEL	3
250*9c8f3233SToomas Soome #define	LUDATA_SEL	5
251*9c8f3233SToomas Soome #define	NLDT		(LUDATA_SEL + 1)
252*9c8f3233SToomas Soome 
253*9c8f3233SToomas Soome #else /* !__i386__ */
254*9c8f3233SToomas Soome /*
255*9c8f3233SToomas Soome  * Entries in the Global Descriptor Table (GDT)
256*9c8f3233SToomas Soome  */
257*9c8f3233SToomas Soome #define	GNULL_SEL	0	/* Null Descriptor */
258*9c8f3233SToomas Soome #define	GNULL2_SEL	1	/* Null Descriptor */
259*9c8f3233SToomas Soome #define	GUFS32_SEL	2	/* User 32 bit %fs Descriptor */
260*9c8f3233SToomas Soome #define	GUGS32_SEL	3	/* User 32 bit %gs Descriptor */
261*9c8f3233SToomas Soome #define	GCODE_SEL	4	/* Kernel Code Descriptor */
262*9c8f3233SToomas Soome #define	GDATA_SEL	5	/* Kernel Data Descriptor */
263*9c8f3233SToomas Soome #define	GUCODE32_SEL	6	/* User 32 bit code Descriptor */
264*9c8f3233SToomas Soome #define	GUDATA_SEL	7	/* User 32/64 bit Data Descriptor */
265*9c8f3233SToomas Soome #define	GUCODE_SEL	8	/* User 64 bit Code Descriptor */
266*9c8f3233SToomas Soome #define	GPROC0_SEL	9	/* TSS for entering kernel etc */
267*9c8f3233SToomas Soome /* slot 10 is second half of GPROC0_SEL */
268*9c8f3233SToomas Soome #define	GUSERLDT_SEL	11	/* LDT */
269*9c8f3233SToomas Soome /* slot 12 is second half of GUSERLDT_SEL */
270*9c8f3233SToomas Soome #define	NGDT		13
271*9c8f3233SToomas Soome #endif /* __i386__ */
272*9c8f3233SToomas Soome 
273*9c8f3233SToomas Soome #endif /* !_X86_SEGMENTS_H_ */
274