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