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