xref: /illumos-gate/usr/src/uts/intel/sys/segments.h (revision 5d9d9091)
17c478bd9Sstevel@tonic-gate /*
2eb5a5c78SSurya Prakki  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
37c478bd9Sstevel@tonic-gate  */
4f16a0f4cSRobert Mustacchi /*
542cd1931SJohn Levon  * Copyright 2019 Joyent, Inc.
6075343cbSDan Cross  * Copyright 2022 Oxide Computer Company
7f16a0f4cSRobert Mustacchi  */
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate #ifndef	_SYS_SEGMENTS_H
107c478bd9Sstevel@tonic-gate #define	_SYS_SEGMENTS_H
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
137c478bd9Sstevel@tonic-gate extern "C" {
147c478bd9Sstevel@tonic-gate #endif
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate /*
177c478bd9Sstevel@tonic-gate  * Copyright (c) 1989, 1990 William F. Jolitz
187c478bd9Sstevel@tonic-gate  * Copyright (c) 1990 The Regents of the University of California.
197c478bd9Sstevel@tonic-gate  * All rights reserved.
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
227c478bd9Sstevel@tonic-gate  * William Jolitz.
237c478bd9Sstevel@tonic-gate  *
247c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
257c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
267c478bd9Sstevel@tonic-gate  * are met:
277c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
287c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
297c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
307c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
317c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
327c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
337c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
347c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
357c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
367c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
377c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
387c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
417c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
427c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
437c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
447c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
457c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
467c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
477c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
487c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
497c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
507c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  *	from: @(#)segments.h	7.1 (Berkeley) 5/9/91
537c478bd9Sstevel@tonic-gate  * $FreeBSD: src/sys/i386/include/segments.h,v 1.34 2003/09/10 01:07:04
547c478bd9Sstevel@tonic-gate  * jhb Exp $
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * 386 Segmentation Data Structures and definitions
577c478bd9Sstevel@tonic-gate  *	William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <sys/tss.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * Selector register format
647c478bd9Sstevel@tonic-gate  * CS, DS, ES, FS, GS, SS
657c478bd9Sstevel@tonic-gate  *
667c478bd9Sstevel@tonic-gate  *  15                  3  2  1 0
677c478bd9Sstevel@tonic-gate  * +---------------------+---+----+
687c478bd9Sstevel@tonic-gate  * |          SI         |TI |RPL |
697c478bd9Sstevel@tonic-gate  * +---------------------+---+----+
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * SI  = selector index
727c478bd9Sstevel@tonic-gate  * TI  = table indicator (0 = GDT, 1 = LDT)
737c478bd9Sstevel@tonic-gate  * RPL = requestor privilege level
747c478bd9Sstevel@tonic-gate  */
757c478bd9Sstevel@tonic-gate #define	IDXTOSEL(s)	((s) << 3)		/* index to selector */
76*5d9d9091SRichard Lowe #define	SELTOIDX(s)	((s) >> 3)		/* selector to index */
777c478bd9Sstevel@tonic-gate #define	SEL_GDT(s, r)	(IDXTOSEL(s) | r)	/* global sel */
78843e1988Sjohnlev 
79843e1988Sjohnlev /*
80843e1988Sjohnlev  * SEL_(KPL,UPL,XPL) is the RPL or DPL value for code and data selectors
81843e1988Sjohnlev  * and their descriptors respectively.
82843e1988Sjohnlev  *
83843e1988Sjohnlev  * TRP_(KPL,UPL,XPL) is used to indicate the DPL for system gates only.
84843e1988Sjohnlev  *
85843e1988Sjohnlev  * This distinction is important to support para-virt guests on the
86843e1988Sjohnlev  * 64-bit hypervisor. Both guest kernel and user run in ring 3 and the
87843e1988Sjohnlev  * hypervisor runs in ring 0. When the kernel creates its trap and
88843e1988Sjohnlev  * interrupt gates it needs a way to prevent any arbitrary int $n
89843e1988Sjohnlev  * instruction from entering a gate that is not expected. The hypervisor
90843e1988Sjohnlev  * allows these gates to have a DPL from 1 to 3. By creating gates
91843e1988Sjohnlev  * with a DPL below user (ring 3) the int $n will generate a #gp fault
92843e1988Sjohnlev  * which the hypervisor catches and forwards to the guest.
93843e1988Sjohnlev  */
94843e1988Sjohnlev #if defined(__xpv)
95843e1988Sjohnlev 
96843e1988Sjohnlev #define	SEL_XPL		0		/* hypervisor privilege level */
97843e1988Sjohnlev #define	SEL_KPL		3		/* both kernel and user in ring 3 */
98843e1988Sjohnlev #define	TRP_KPL		1		/* system gate priv (user blocked) */
99843e1988Sjohnlev #define	TRP_XPL		0		/* system gate priv (hypervisor) */
100843e1988Sjohnlev 
10174ecdb51SJohn Levon #define	IST_DBG		0
10274ecdb51SJohn Levon 
103843e1988Sjohnlev #else	/* __xpv */
104843e1988Sjohnlev 
105843e1988Sjohnlev #define	SEL_KPL		0		/* kernel privilege level on metal */
106843e1988Sjohnlev #define	TRP_KPL		SEL_KPL		/* system gate priv (user blocked) */
107843e1988Sjohnlev 
10874ecdb51SJohn Levon 
10974ecdb51SJohn Levon #define	IST_DF		1
11074ecdb51SJohn Levon #define	IST_NMI		2
11174ecdb51SJohn Levon #define	IST_MCE		3
11274ecdb51SJohn Levon #define	IST_DBG		4
11374ecdb51SJohn Levon #define	IST_NESTABLE	5
11474ecdb51SJohn Levon #define	IST_DEFAULT	6
11574ecdb51SJohn Levon 
116843e1988Sjohnlev #endif	/* __xpv */
117843e1988Sjohnlev 
11874ecdb51SJohn Levon #define	IST_NONE	0
11974ecdb51SJohn Levon 
1207c478bd9Sstevel@tonic-gate #define	SEL_UPL		3		/* user priority level */
121843e1988Sjohnlev #define	TRP_UPL		3		/* system gate priv (user allowed) */
1227c478bd9Sstevel@tonic-gate #define	SEL_TI_LDT	4		/* local descriptor table */
1237c478bd9Sstevel@tonic-gate #define	SEL_LDT(s)	(IDXTOSEL(s) | SEL_TI_LDT | SEL_UPL)	/* local sel */
1247c478bd9Sstevel@tonic-gate #define	CPL_MASK	3		/* RPL mask for selector */
1257c478bd9Sstevel@tonic-gate #define	SELISLDT(s)	(((s) & SEL_TI_LDT) == SEL_TI_LDT)
1267c478bd9Sstevel@tonic-gate #define	SELISUPL(s)	(((s) & CPL_MASK) == SEL_UPL)
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #ifndef	_ASM
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate typedef	uint16_t	selector_t;	/* selector reigster */
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  * Hardware descriptor table register format for GDT and IDT.
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate #if defined(__amd64)
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate #pragma pack(2)
1387c478bd9Sstevel@tonic-gate typedef struct {
1397c478bd9Sstevel@tonic-gate 	uint16_t dtr_limit;	/* table limit */
1407c478bd9Sstevel@tonic-gate 	uint64_t dtr_base;	/* table base address  */
1417c478bd9Sstevel@tonic-gate } desctbr_t;
1427c478bd9Sstevel@tonic-gate #pragma	pack()
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate #elif defined(__i386)
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #pragma pack(2)
1477c478bd9Sstevel@tonic-gate typedef struct {
1487c478bd9Sstevel@tonic-gate 	uint16_t dtr_limit;	/* table limit */
1497c478bd9Sstevel@tonic-gate 	uint32_t dtr_base;	/* table base address  */
1507c478bd9Sstevel@tonic-gate } desctbr_t;
1517c478bd9Sstevel@tonic-gate #pragma	pack()
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate #endif	/* __i386 */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate /*
1567c478bd9Sstevel@tonic-gate  * Functions for loading and storing descriptor table
1577c478bd9Sstevel@tonic-gate  * registers.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate extern void rd_idtr(desctbr_t *);
1607c478bd9Sstevel@tonic-gate extern void wr_idtr(desctbr_t *);
1617c478bd9Sstevel@tonic-gate extern void rd_gdtr(desctbr_t *);
1627c478bd9Sstevel@tonic-gate extern void wr_gdtr(desctbr_t *);
1637c478bd9Sstevel@tonic-gate extern void wr_ldtr(selector_t);
1647c478bd9Sstevel@tonic-gate extern selector_t rd_ldtr(void);
1657c478bd9Sstevel@tonic-gate extern void wr_tsr(selector_t);
166ae115bc7Smrj extern void kmdb_enter(void);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #if defined(__amd64)
169ae115bc7Smrj 
170ae115bc7Smrj /*
171ae115bc7Smrj  * inlines for update_segregs
172ae115bc7Smrj  */
173ae115bc7Smrj extern void __set_ds(selector_t);
174ae115bc7Smrj extern void __set_es(selector_t);
175ae115bc7Smrj extern void __set_fs(selector_t);
176ae115bc7Smrj extern void __set_gs(selector_t);
177ae115bc7Smrj #endif	/* __amd64 */
178ae115bc7Smrj 
179ae115bc7Smrj #if defined(__amd64)
180ae115bc7Smrj extern void load_segment_registers(selector_t, selector_t, selector_t,
181ae115bc7Smrj     selector_t);	/* (alphabetical) */
182ae115bc7Smrj #elif defined(__i386)
183ae115bc7Smrj extern void load_segment_registers(selector_t, selector_t, selector_t,
184ae115bc7Smrj     selector_t, selector_t, selector_t);	/* (alphabetical) */
185ae115bc7Smrj #endif	/* __i386 */
1867c478bd9Sstevel@tonic-gate 
187843e1988Sjohnlev selector_t get_cs_register();
188843e1988Sjohnlev 
1897c478bd9Sstevel@tonic-gate #if !defined(__amd64)
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * User segment descriptors (code and data).
1937c478bd9Sstevel@tonic-gate  * Legacy mode 64-bits wide.
1947c478bd9Sstevel@tonic-gate  */
1957c478bd9Sstevel@tonic-gate typedef struct user_desc {
1967c478bd9Sstevel@tonic-gate 	uint32_t usd_lolimit:16;	/* segment limit 15:0 */
1977c478bd9Sstevel@tonic-gate 	uint32_t usd_lobase:16;		/* segment base 15:0 */
1987c478bd9Sstevel@tonic-gate 	uint32_t usd_midbase:8;		/* segment base 23:16 */
1997c478bd9Sstevel@tonic-gate 	uint32_t usd_type:5;		/* segment type, includes S bit */
2007c478bd9Sstevel@tonic-gate 	uint32_t usd_dpl:2;		/* segment descriptor priority level */
2017c478bd9Sstevel@tonic-gate 	uint32_t usd_p:1;		/* segment descriptor present */
2027c478bd9Sstevel@tonic-gate 	uint32_t usd_hilimit:4;		/* segment limit 19:16 */
2037c478bd9Sstevel@tonic-gate 	uint32_t usd_avl:1;		/* available to sw, but not used */
2047c478bd9Sstevel@tonic-gate 	uint32_t usd_reserved:1;	/* unused, ignored */
2057c478bd9Sstevel@tonic-gate 	uint32_t usd_def32:1;		/* default 32 vs 16 bit operand */
2067c478bd9Sstevel@tonic-gate 	uint32_t usd_gran:1;		/* limit units (bytes vs pages) */
2077c478bd9Sstevel@tonic-gate 	uint32_t usd_hibase:8;		/* segment base 31:24 */
2087c478bd9Sstevel@tonic-gate } user_desc_t;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate #define	USEGD_GETBASE(usd)		((usd)->usd_lobase |		\
2117c478bd9Sstevel@tonic-gate 					(usd)->usd_midbase << 16 |	\
2127c478bd9Sstevel@tonic-gate 					(usd)->usd_hibase << (16 + 8))
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate #define	USEGD_SETBASE(usd, b)		((usd)->usd_lobase = (b),	\
2157c478bd9Sstevel@tonic-gate 					(usd)->usd_midbase = (b) >> 16, \
2167c478bd9Sstevel@tonic-gate 					(usd)->usd_hibase = (b) >> (16 + 8))
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate #define	USEGD_GETLIMIT(usd)		((usd)->usd_lolimit |		\
2197c478bd9Sstevel@tonic-gate 					(usd)->usd_hilimit << 16)
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate #define	USEGD_SETLIMIT(usd, lim)	((usd)->usd_lolimit = lim,	\
2227c478bd9Sstevel@tonic-gate 					(usd)->usd_hilimit = lim >> 16)
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate #define	USD_TYPESHIFT			5	/* size of usd_type field */
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate #else	/* __amd64 */
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate  * User segment descriptors.
2307c478bd9Sstevel@tonic-gate  * Long mode 64-bits wide.
2317c478bd9Sstevel@tonic-gate  *
2327c478bd9Sstevel@tonic-gate  * In 32-bit compatibility mode (%cs:usd_long=0) all fields are interpreted
2337c478bd9Sstevel@tonic-gate  * as in legacy mode for both code and data.
2347c478bd9Sstevel@tonic-gate  *
2357c478bd9Sstevel@tonic-gate  * In 64-bit mode (%cs:usd_long=1) code segments only have the conforming
2367c478bd9Sstevel@tonic-gate  * bit in usd_type, usd_dpl, usd_p, usd_long and usd_def32=0. usd_def32
2377c478bd9Sstevel@tonic-gate  * must be zero in 64-bit mode. Setting it to 1 is reserved for future use.
2387c478bd9Sstevel@tonic-gate  * All other fields are loaded but ignored by hardware.
2397c478bd9Sstevel@tonic-gate  *
2407c478bd9Sstevel@tonic-gate  * 64-bit data segments only have usd_p. All other fields are loaded but
2417c478bd9Sstevel@tonic-gate  * ignored by hardware when in 64-bit mode.
2427c478bd9Sstevel@tonic-gate  */
2437c478bd9Sstevel@tonic-gate typedef struct user_desc {
2447c478bd9Sstevel@tonic-gate 	uint64_t usd_lolimit:16;	/* segment limit 15:0 */
2457c478bd9Sstevel@tonic-gate 	uint64_t usd_lobase:16;		/* segment base 15:0 */
2467c478bd9Sstevel@tonic-gate 	uint64_t usd_midbase:8;		/* segment base 23:16 */
2477c478bd9Sstevel@tonic-gate 	uint64_t usd_type:5;		/* segment type, includes S bit */
2487c478bd9Sstevel@tonic-gate 	uint64_t usd_dpl:2;		/* segment descriptor priority level */
2497c478bd9Sstevel@tonic-gate 	uint64_t usd_p:1;		/* segment descriptor present */
2507c478bd9Sstevel@tonic-gate 	uint64_t usd_hilimit:4;		/* segment limit 19:16 */
2517c478bd9Sstevel@tonic-gate 	uint64_t usd_avl:1;		/* available to sw, but not used */
2527c478bd9Sstevel@tonic-gate 	uint64_t usd_long:1;		/* long mode (%cs only) */
2537c478bd9Sstevel@tonic-gate 	uint64_t usd_def32:1;		/* default 32 vs 16 bit operand */
2547c478bd9Sstevel@tonic-gate 	uint64_t usd_gran:1;		/* limit units (bytes vs page) */
2557c478bd9Sstevel@tonic-gate 	uint64_t usd_hibase:8;		/* segment base 31:24 */
2567c478bd9Sstevel@tonic-gate } user_desc_t;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate #define	USEGD_GETBASE(usd)		((usd)->usd_lobase |		\
2597c478bd9Sstevel@tonic-gate 					(usd)->usd_midbase << 16 |	\
2607c478bd9Sstevel@tonic-gate 					(usd)->usd_hibase << (16 + 8))
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate #define	USEGD_SETBASE(usd, b)		((usd)->usd_lobase = (b),	\
2637c478bd9Sstevel@tonic-gate 					(usd)->usd_midbase = (b) >> 16, \
2647c478bd9Sstevel@tonic-gate 					(usd)->usd_hibase = (b) >> (16 + 8))
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate #define	USEGD_GETLIMIT(usd)		((usd)->usd_lolimit |		\
2677c478bd9Sstevel@tonic-gate 					(usd)->usd_hilimit << 16)
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate #define	USEGD_SETLIMIT(usd, lim)	((usd)->usd_lolimit = lim,	\
2707c478bd9Sstevel@tonic-gate 					(usd)->usd_hilimit = lim >> 16)
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate #define	USD_TYPESHIFT			5	/* size of usd_type field */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate #endif /* __amd64 */
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate #if !defined(__amd64)
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * System segment descriptors for LDT and TSS segments.
2807c478bd9Sstevel@tonic-gate  * Legacy mode 64-bits wide.
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate typedef struct system_desc {
2837c478bd9Sstevel@tonic-gate 	uint32_t ssd_lolimit:16;	/* segment limit 15:0 */
2847c478bd9Sstevel@tonic-gate 	uint32_t ssd_lobase:16;		/* segment base 15:0 */
2857c478bd9Sstevel@tonic-gate 	uint32_t ssd_midbase:8;		/* segment base 23:16 */
2867c478bd9Sstevel@tonic-gate 	uint32_t ssd_type:4;		/* segment type */
2877c478bd9Sstevel@tonic-gate 	uint32_t ssd_zero:1;		/* must be zero */
2887c478bd9Sstevel@tonic-gate 	uint32_t ssd_dpl:2;		/* segment descriptor priority level */
2897c478bd9Sstevel@tonic-gate 	uint32_t ssd_p:1;		/* segment descriptor present */
2907c478bd9Sstevel@tonic-gate 	uint32_t ssd_hilimit:4;		/* segment limit 19:16 */
2917c478bd9Sstevel@tonic-gate 	uint32_t ssd_avl:1;		/* available to sw, but not used */
2927c478bd9Sstevel@tonic-gate 	uint32_t ssd_reserved:2;	/* unused, ignored */
2937c478bd9Sstevel@tonic-gate 	uint32_t ssd_gran:1;		/* limit unit (bytes vs pages) */
2947c478bd9Sstevel@tonic-gate 	uint32_t ssd_hibase:8;		/* segment base 31:24 */
2957c478bd9Sstevel@tonic-gate } system_desc_t;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate #else	/* __amd64 */
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate  * System segment descriptors for LDT and TSS segments.
3017c478bd9Sstevel@tonic-gate  * Long mode 128-bits wide.
3027c478bd9Sstevel@tonic-gate  *
3037c478bd9Sstevel@tonic-gate  * 32-bit LDT and TSS descriptor types are redefined to 64-bit equivalents.
3047c478bd9Sstevel@tonic-gate  * All other legacy types are reserved and illegal.
3057c478bd9Sstevel@tonic-gate  */
3067c478bd9Sstevel@tonic-gate typedef struct system_desc {
3077c478bd9Sstevel@tonic-gate 	uint64_t ssd_lolimit:16;	/* segment limit 15:0 */
3087c478bd9Sstevel@tonic-gate 	uint64_t ssd_lobase:16;		/* segment base 15:0 */
3097c478bd9Sstevel@tonic-gate 	uint64_t ssd_midbase:8;		/* segment base 23:16 */
3107c478bd9Sstevel@tonic-gate 	uint64_t ssd_type:4;		/* segment type */
3117c478bd9Sstevel@tonic-gate 	uint64_t ssd_zero1:1;		/* must be zero */
3127c478bd9Sstevel@tonic-gate 	uint64_t ssd_dpl:2;		/* segment descriptor priority level */
3137c478bd9Sstevel@tonic-gate 	uint64_t ssd_p:1;		/* segment descriptor present */
3147c478bd9Sstevel@tonic-gate 	uint64_t ssd_hilimit:4;		/* segment limit 19:16 */
3157c478bd9Sstevel@tonic-gate 	uint64_t ssd_avl:1;		/* available to sw, but not used */
3167c478bd9Sstevel@tonic-gate 	uint64_t ssd_resv1:2;		/* unused, ignored */
3177c478bd9Sstevel@tonic-gate 	uint64_t ssd_gran:1;		/* limit unit (bytes vs pages) */
3187c478bd9Sstevel@tonic-gate 	uint64_t ssd_hibase:8;		/* segment base 31:24 */
3197c478bd9Sstevel@tonic-gate 	uint64_t ssd_hi64base:32;	/* segment base 63:32 */
3207c478bd9Sstevel@tonic-gate 	uint64_t ssd_resv2:8;		/* unused, ignored */
3217c478bd9Sstevel@tonic-gate 	uint64_t ssd_zero2:5;		/* must be zero */
3227c478bd9Sstevel@tonic-gate 	uint64_t ssd_resv3:19;		/* unused, ignored */
3237c478bd9Sstevel@tonic-gate } system_desc_t;
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate #endif	/* __amd64 */
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate #define	SYSSEGD_SETLIMIT(ssd, lim)	((ssd)->ssd_lolimit = lim,	\
3287c478bd9Sstevel@tonic-gate 					(ssd)->ssd_hilimit = lim >> 16)
329843e1988Sjohnlev 
330843e1988Sjohnlev #define	SYSSEGD_GETLIMIT(ssd)		(((ssd)->ssd_hilimit << 16) |   \
331843e1988Sjohnlev 					(ssd)->ssd_lolimit)
332843e1988Sjohnlev 
3337c478bd9Sstevel@tonic-gate #if !defined(__amd64)
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate /*
3367c478bd9Sstevel@tonic-gate  * System gate segment descriptors for interrupt, trap, call and task gates.
3377c478bd9Sstevel@tonic-gate  * Legacy mode 64-bits wide.
3387c478bd9Sstevel@tonic-gate  */
3397c478bd9Sstevel@tonic-gate typedef struct gate_desc {
3407c478bd9Sstevel@tonic-gate 	uint32_t sgd_looffset:16;	/* segment code offset 15:0 */
3417c478bd9Sstevel@tonic-gate 	uint32_t sgd_selector:16;	/* target code or task selector */
3427c478bd9Sstevel@tonic-gate 	uint32_t sgd_stkcpy:5;		/* number of stack wds to cpy */
3437c478bd9Sstevel@tonic-gate 	uint32_t sgd_resv:3;		/* unused, ignored */
3447c478bd9Sstevel@tonic-gate 	uint32_t sgd_type:5;		/* segment type, includes S bit */
3457c478bd9Sstevel@tonic-gate 	uint32_t sgd_dpl:2;		/* segment descriptor priority level */
3467c478bd9Sstevel@tonic-gate 	uint32_t sgd_p:1;		/* segment descriptor present */
3477c478bd9Sstevel@tonic-gate 	uint32_t sgd_hioffset:16;	/* code seg off 31:16 */
3487c478bd9Sstevel@tonic-gate } gate_desc_t;
3497c478bd9Sstevel@tonic-gate 
350c909a41bSRichard Lowe #define	GATESEG_GETOFFSET(sgd)	((uintptr_t)((sgd)->sgd_looffset |	\
351c909a41bSRichard Lowe 				(sgd)->sgd_hioffset << 16))
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate #else	/* __amd64 */
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /*
3567c478bd9Sstevel@tonic-gate  * System segment descriptors for interrupt, trap and call gates.
3577c478bd9Sstevel@tonic-gate  * Long mode 128-bits wide.
3587c478bd9Sstevel@tonic-gate  *
3597c478bd9Sstevel@tonic-gate  * 32-bit interrupt, trap and call gate types are redefined to 64-bit
3607c478bd9Sstevel@tonic-gate  * equivalents. Task gates along with all other legacy types are reserved
3617c478bd9Sstevel@tonic-gate  * and illegal.
3627c478bd9Sstevel@tonic-gate  */
3637c478bd9Sstevel@tonic-gate typedef struct gate_desc {
3647c478bd9Sstevel@tonic-gate 	uint64_t sgd_looffset:16;	/* segment code offset 15:0 */
3657c478bd9Sstevel@tonic-gate 	uint64_t sgd_selector:16;	/* target code or task selector */
3667c478bd9Sstevel@tonic-gate 	uint64_t sgd_ist:3;		/* IST table index */
3677c478bd9Sstevel@tonic-gate 	uint64_t sgd_resv1:5;		/* unused, ignored */
3687c478bd9Sstevel@tonic-gate 	uint64_t sgd_type:5;		/* segment type, includes S bit */
3697c478bd9Sstevel@tonic-gate 	uint64_t sgd_dpl:2;		/* segment descriptor priority level */
3707c478bd9Sstevel@tonic-gate 	uint64_t sgd_p:1;		/* segment descriptor present */
3717c478bd9Sstevel@tonic-gate 	uint64_t sgd_hioffset:16;	/* segment code offset 31:16 */
3727c478bd9Sstevel@tonic-gate 	uint64_t sgd_hi64offset:32;	/* segment code offset 63:32 */
3737c478bd9Sstevel@tonic-gate 	uint64_t sgd_resv2:8;		/* unused, ignored */
3747c478bd9Sstevel@tonic-gate 	uint64_t sgd_zero:5;		/* call gate only: must be zero */
3757c478bd9Sstevel@tonic-gate 	uint64_t sgd_resv3:19;		/* unused, ignored */
3767c478bd9Sstevel@tonic-gate } gate_desc_t;
3777c478bd9Sstevel@tonic-gate 
378c909a41bSRichard Lowe #define	GATESEG_GETOFFSET(sgd)	((uintptr_t)((sgd)->sgd_looffset |	\
379c909a41bSRichard Lowe 				(sgd)->sgd_hioffset << 16 |		\
380c909a41bSRichard Lowe 				(uint64_t)((sgd)->sgd_hi64offset) << 32))
3817c478bd9Sstevel@tonic-gate 
3827c478bd9Sstevel@tonic-gate #endif	/* __amd64 */
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate /*
3857c478bd9Sstevel@tonic-gate  * functions for initializing and updating segment descriptors.
3867c478bd9Sstevel@tonic-gate  */
3877c478bd9Sstevel@tonic-gate #if defined(__amd64)
3887c478bd9Sstevel@tonic-gate 
389075343cbSDan Cross extern void set_usegd(user_desc_t *, uint_t, void *, uint32_t, uint_t, uint_t,
3907c478bd9Sstevel@tonic-gate     uint_t, uint_t);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate #elif defined(__i386)
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate extern void set_usegd(user_desc_t *, void *, size_t, uint_t, uint_t,
3957c478bd9Sstevel@tonic-gate     uint_t, uint_t);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate #endif	/* __i386 */
3987c478bd9Sstevel@tonic-gate 
39974ecdb51SJohn Levon extern uint_t idt_vector_to_ist(uint_t);
40074ecdb51SJohn Levon 
401ae115bc7Smrj extern void set_gatesegd(gate_desc_t *, void (*)(void), selector_t,
4029844da31SSeth Goldberg     uint_t, uint_t, uint_t);
403ae115bc7Smrj 
404843e1988Sjohnlev extern void set_syssegd(system_desc_t *, void *, size_t, uint_t, uint_t);
405843e1988Sjohnlev 
406843e1988Sjohnlev extern void *get_ssd_base(system_desc_t *);
407843e1988Sjohnlev 
408843e1988Sjohnlev extern void gdt_update_usegd(uint_t, user_desc_t *);
409843e1988Sjohnlev 
410843e1988Sjohnlev extern int ldt_update_segd(user_desc_t *, user_desc_t *);
411843e1988Sjohnlev 
412843e1988Sjohnlev #if defined(__xpv)
413843e1988Sjohnlev 
414843e1988Sjohnlev extern int xen_idt_to_trap_info(uint_t, gate_desc_t *, void *);
415843e1988Sjohnlev extern void xen_idt_write(gate_desc_t *, uint_t);
416843e1988Sjohnlev 
417843e1988Sjohnlev #endif	/* __xen */
4187c478bd9Sstevel@tonic-gate 
419ae115bc7Smrj void init_boot_gdt(user_desc_t *);
420ae115bc7Smrj 
4217c478bd9Sstevel@tonic-gate #endif	/* _ASM */
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate /*
4247c478bd9Sstevel@tonic-gate  * Common segment parameter defintions for granularity, default
4257c478bd9Sstevel@tonic-gate  * operand size and operaton mode.
4267c478bd9Sstevel@tonic-gate  */
4277c478bd9Sstevel@tonic-gate #define	SDP_BYTES	0	/* segment limit scaled to bytes */
4287c478bd9Sstevel@tonic-gate #define	SDP_PAGES	1	/* segment limit scaled to pages */
4297c478bd9Sstevel@tonic-gate #define	SDP_OP32	1	/* code and data default operand = 32 bits */
4307c478bd9Sstevel@tonic-gate #define	SDP_LONG	1	/* long mode code segment (64 bits) */
4317c478bd9Sstevel@tonic-gate #define	SDP_SHORT	0	/* compat/legacy code segment (32 bits) */
432075343cbSDan Cross 
433075343cbSDan Cross /*
434075343cbSDan Cross  * The maximum segment limit that can be put into a segment descriptor for
435075343cbSDan Cross  * 16-bit and 32-bit segments.  In 64-bit mode, segment base addresses are
436075343cbSDan Cross  * fixed to 0 and the segment limit is ignored.
437075343cbSDan Cross  */
438075343cbSDan Cross #define	SDP_LIMIT_MAX	0xFFFFFU
439075343cbSDan Cross 
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate  * System segments and gate types.
4427c478bd9Sstevel@tonic-gate  *
4437c478bd9Sstevel@tonic-gate  * In long mode i386 32-bit ldt, tss, call, interrupt and trap gate
4447c478bd9Sstevel@tonic-gate  * types are redefined into 64-bit equivalents.
4457c478bd9Sstevel@tonic-gate  */
4467c478bd9Sstevel@tonic-gate #define	SDT_SYSNULL	 0	/* system null */
4477c478bd9Sstevel@tonic-gate #define	SDT_SYS286TSS	 1	/* system 286 TSS available */
4487c478bd9Sstevel@tonic-gate #define	SDT_SYSLDT	 2	/* system local descriptor table */
4497c478bd9Sstevel@tonic-gate #define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
4507c478bd9Sstevel@tonic-gate #define	SDT_SYS286CGT	 4	/* system 286 call gate */
4517c478bd9Sstevel@tonic-gate #define	SDT_SYSTASKGT	 5	/* system task gate */
4527c478bd9Sstevel@tonic-gate #define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
4537c478bd9Sstevel@tonic-gate #define	SDT_SYS286TGT	 7	/* system 286 trap gate */
4547c478bd9Sstevel@tonic-gate #define	SDT_SYSNULL2	 8	/* system null again */
4557c478bd9Sstevel@tonic-gate #define	SDT_SYSTSS	 9	/* system TSS available */
4567c478bd9Sstevel@tonic-gate #define	SDT_SYSNULL3	10	/* system null again */
4577c478bd9Sstevel@tonic-gate #define	SDT_SYSTSSBSY	11	/* system TSS busy */
4587c478bd9Sstevel@tonic-gate #define	SDT_SYSCGT	12	/* system call gate */
4597c478bd9Sstevel@tonic-gate #define	SDT_SYSNULL4	13	/* system null again */
4607c478bd9Sstevel@tonic-gate #define	SDT_SYSIGT	14	/* system interrupt gate */
4617c478bd9Sstevel@tonic-gate #define	SDT_SYSTGT	15	/* system trap gate */
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate  * Memory segment types.
4657c478bd9Sstevel@tonic-gate  *
4667c478bd9Sstevel@tonic-gate  * While in long mode expand-down, writable and accessed type field
4677c478bd9Sstevel@tonic-gate  * attributes are ignored. Only the conforming bit is loaded by hardware
4687c478bd9Sstevel@tonic-gate  * for long mode code segment descriptors.
4697c478bd9Sstevel@tonic-gate  */
4707c478bd9Sstevel@tonic-gate #define	SDT_MEMRO	16	/* read only */
4717c478bd9Sstevel@tonic-gate #define	SDT_MEMROA	17	/* read only accessed */
4727c478bd9Sstevel@tonic-gate #define	SDT_MEMRW	18	/* read write */
4737c478bd9Sstevel@tonic-gate #define	SDT_MEMRWA	19	/* read write accessed */
4747c478bd9Sstevel@tonic-gate #define	SDT_MEMROD	20	/* read only expand dwn limit */
4757c478bd9Sstevel@tonic-gate #define	SDT_MEMRODA	21	/* read only expand dwn limit accessed */
4767c478bd9Sstevel@tonic-gate #define	SDT_MEMRWD	22	/* read write expand dwn limit */
4777c478bd9Sstevel@tonic-gate #define	SDT_MEMRWDA	23	/* read write expand dwn limit accessed */
4787c478bd9Sstevel@tonic-gate #define	SDT_MEME	24	/* execute only */
4797c478bd9Sstevel@tonic-gate #define	SDT_MEMEA	25	/* execute only accessed */
4807c478bd9Sstevel@tonic-gate #define	SDT_MEMER	26	/* execute read */
4817c478bd9Sstevel@tonic-gate #define	SDT_MEMERA	27	/* execute read accessed */
4827c478bd9Sstevel@tonic-gate #define	SDT_MEMEC	28	/* execute only conforming */
4837c478bd9Sstevel@tonic-gate #define	SDT_MEMEAC	29	/* execute only accessed conforming */
4847c478bd9Sstevel@tonic-gate #define	SDT_MEMERC	30	/* execute read conforming */
4857c478bd9Sstevel@tonic-gate #define	SDT_MEMERAC	31	/* execute read accessed conforming */
4867c478bd9Sstevel@tonic-gate 
487a0955b86SJohn Levon /* These bits are within the "type" field, like the values above. */
488a0955b86SJohn Levon #define	SDT_A		0x01	/* accessed bit */
489a0955b86SJohn Levon #define	SDT_S		0x10	/* S-bit at the top of "type" for usegs */
490a0955b86SJohn Levon 
4917c478bd9Sstevel@tonic-gate /*
4927c478bd9Sstevel@tonic-gate  * Entries in the Interrupt Descriptor Table (IDT)
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate #define	IDT_DE		0	/* #DE: Divide Error */
4957c478bd9Sstevel@tonic-gate #define	IDT_DB		1	/* #DB: Debug */
4967c478bd9Sstevel@tonic-gate #define	IDT_NMI		2	/* Nonmaskable External Interrupt */
4977c478bd9Sstevel@tonic-gate #define	IDT_BP		3	/* #BP: Breakpoint */
4987c478bd9Sstevel@tonic-gate #define	IDT_OF		4	/* #OF: Overflow */
4997c478bd9Sstevel@tonic-gate #define	IDT_BR		5	/* #BR: Bound Range Exceeded */
5007c478bd9Sstevel@tonic-gate #define	IDT_UD		6	/* #UD: Undefined/Invalid Opcode */
5017c478bd9Sstevel@tonic-gate #define	IDT_NM		7	/* #NM: No Math Coprocessor */
5027c478bd9Sstevel@tonic-gate #define	IDT_DF		8	/* #DF: Double Fault */
5037c478bd9Sstevel@tonic-gate #define	IDT_FPUGP	9	/* Coprocessor Segment Overrun */
5047c478bd9Sstevel@tonic-gate #define	IDT_TS		10	/* #TS: Invalid TSS */
5057c478bd9Sstevel@tonic-gate #define	IDT_NP		11	/* #NP: Segment Not Present */
5067c478bd9Sstevel@tonic-gate #define	IDT_SS		12	/* #SS: Stack Segment Fault */
5077c478bd9Sstevel@tonic-gate #define	IDT_GP		13	/* #GP: General Protection Fault */
5087c478bd9Sstevel@tonic-gate #define	IDT_PF		14	/* #PF: Page Fault */
5097c478bd9Sstevel@tonic-gate #define	IDT_MF		16	/* #MF: FPU Floating-Point Error */
5107c478bd9Sstevel@tonic-gate #define	IDT_AC		17	/* #AC: Alignment Check */
5117c478bd9Sstevel@tonic-gate #define	IDT_MC		18	/* #MC: Machine Check */
5127c478bd9Sstevel@tonic-gate #define	IDT_XF		19	/* #XF: SIMD Floating-Point Exception */
5137c478bd9Sstevel@tonic-gate #define	NIDT		256	/* size in entries of IDT */
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate /*
5167c478bd9Sstevel@tonic-gate  * Entries in the Global Descriptor Table (GDT)
5177c478bd9Sstevel@tonic-gate  *
5187c478bd9Sstevel@tonic-gate  * We make sure to space the system descriptors (LDT's, TSS')
5197c478bd9Sstevel@tonic-gate  * such that they are double gdt slot aligned. This is because
5207c478bd9Sstevel@tonic-gate  * in long mode system segment decriptors expand to 128 bits.
5217c478bd9Sstevel@tonic-gate  *
5227c478bd9Sstevel@tonic-gate  * GDT_LWPFS and GDT_LWPGS must be the same for both 32 and 64-bit
523ae115bc7Smrj  * kernels. See setup_context in libc. 64-bit processes must set
524ae115bc7Smrj  * %fs or %gs to null selector to use 64-bit fsbase or gsbase
525ae115bc7Smrj  * respectively.
5267c478bd9Sstevel@tonic-gate  */
527ae115bc7Smrj #define	GDT_NULL	0	/* null */
528ae115bc7Smrj #define	GDT_B32DATA	1	/* dboot 32 bit data descriptor */
529ae115bc7Smrj #define	GDT_B32CODE	2	/* dboot 32 bit code descriptor */
530ae115bc7Smrj #define	GDT_B16CODE	3	/* bios call 16 bit code descriptor */
531ae115bc7Smrj #define	GDT_B16DATA	4	/* bios call 16 bit data descriptor */
532ae115bc7Smrj #define	GDT_B64CODE	5	/* dboot 64 bit code descriptor */
533ae115bc7Smrj #define	GDT_BGSTMP	7	/* kmdb descriptor only used early in boot */
5342428aad8SPatrick Mooney #define	GDT_CPUID	16	/* store numeric id of current CPU */
535ae115bc7Smrj 
536075343cbSDan Cross /*
537075343cbSDan Cross  * Early boot code may need to create a temporary GDT;
538075343cbSDan Cross  * this is the minimum length required.
539075343cbSDan Cross  */
540075343cbSDan Cross #define	LEN_MIN_GDT_BOOT	(GDT_B64CODE + 1)
541075343cbSDan Cross 
5427c478bd9Sstevel@tonic-gate #if defined(__amd64)
5437c478bd9Sstevel@tonic-gate 
544ae115bc7Smrj #define	GDT_KCODE	6	/* kernel code seg %cs */
545ae115bc7Smrj #define	GDT_KDATA	7	/* kernel data seg %ds */
546ae115bc7Smrj #define	GDT_U32CODE	8	/* 32-bit process on 64-bit kernel %cs */
547ae115bc7Smrj #define	GDT_UDATA	9	/* user data seg %ds (32 and 64 bit) */
548ae115bc7Smrj #define	GDT_UCODE	10	/* native user code  seg %cs */
5492428aad8SPatrick Mooney #define	GDT_LDT		12	/* (12-13) LDT for current process */
5502428aad8SPatrick Mooney #define	GDT_KTSS	14	/* (14-15) kernel tss */
5517c478bd9Sstevel@tonic-gate #define	GDT_FS		GDT_NULL /* kernel %fs segment selector */
5527c478bd9Sstevel@tonic-gate #define	GDT_GS		GDT_NULL /* kernel %gs segment selector */
553ae115bc7Smrj #define	GDT_LWPFS	55	/* lwp private %fs segment selector (32-bit) */
554ae115bc7Smrj #define	GDT_LWPGS	56	/* lwp private %gs segment selector (32-bit) */
5559acbbeafSnn #define	GDT_BRANDMIN	57	/* first entry in GDT for brand usage */
5569acbbeafSnn #define	GDT_BRANDMAX	61	/* last entry in GDT for brand usage */
5579acbbeafSnn #define	NGDT		62	/* number of entries in GDT */
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate /*
5607c478bd9Sstevel@tonic-gate  * This selector is only used in the temporary GDT used to bring additional
5617c478bd9Sstevel@tonic-gate  * CPUs from 16-bit real mode into long mode in real_mode_start().
5627c478bd9Sstevel@tonic-gate  */
5637c478bd9Sstevel@tonic-gate #define	TEMPGDT_KCODE64	1	/* 64-bit code selector */
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate #elif defined(__i386)
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate #define	GDT_LDT		40	/* LDT for current process */
5687c478bd9Sstevel@tonic-gate #define	GDT_KTSS	42	/* kernel tss */
5697c478bd9Sstevel@tonic-gate #define	GDT_KCODE	43	/* kernel code seg %cs */
5707c478bd9Sstevel@tonic-gate #define	GDT_KDATA	44	/* kernel data seg %ds */
5717c478bd9Sstevel@tonic-gate #define	GDT_UCODE	45	/* native user code  seg %cs */
5727c478bd9Sstevel@tonic-gate #define	GDT_UDATA	46	/* user data seg %ds (32 and 64 bit) */
5737c478bd9Sstevel@tonic-gate #define	GDT_DBFLT	47	/* double fault #DF selector */
5747c478bd9Sstevel@tonic-gate #define	GDT_FS		53	/* kernel %fs segment selector */
5757c478bd9Sstevel@tonic-gate #define	GDT_GS		54	/* kernel %gs segment selector */
5767c478bd9Sstevel@tonic-gate #define	GDT_LWPFS	55	/* lwp private %fs segment selector */
5777c478bd9Sstevel@tonic-gate #define	GDT_LWPGS	56	/* lwp private %gs segment selector */
5789acbbeafSnn #define	GDT_BRANDMIN	57	/* first entry in GDT for brand usage */
5799acbbeafSnn #define	GDT_BRANDMAX	61	/* last entry in GDT for brand usage */
580843e1988Sjohnlev #if !defined(__xpv)
5817c478bd9Sstevel@tonic-gate #define	NGDT		90	/* number of entries in GDT */
582843e1988Sjohnlev #else
583843e1988Sjohnlev #define	NGDT		512	/* single 4K page for the hypervisor */
584843e1988Sjohnlev #endif
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate #endif	/* __i386 */
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate /*
5897c478bd9Sstevel@tonic-gate  * Convenient selector definitions.
5907c478bd9Sstevel@tonic-gate  */
591843e1988Sjohnlev 
592843e1988Sjohnlev /*
593843e1988Sjohnlev  * XXPV	64 bit Xen only allows the guest %cs/%ss be the private ones it
594843e1988Sjohnlev  * provides, not the ones we create for ourselves.  See FLAT_RING3_CS64 in
595843e1988Sjohnlev  * public/arch-x86_64.h
596843e1988Sjohnlev  *
597843e1988Sjohnlev  * 64-bit Xen runs paravirtual guests in ring 3 but emulates them running in
598843e1988Sjohnlev  * ring 0 by clearing CPL in %cs value pushed on guest exception stacks.
599843e1988Sjohnlev  * Therefore we will have KCS_SEL value indicate ring 0 and use that everywhere
600843e1988Sjohnlev  * in the kernel. But in the few files where we initialize segment registers or
601843e1988Sjohnlev  * create and update descriptors we will explicity OR in SEL_KPL (ring 3) for
602843e1988Sjohnlev  * kernel %cs. See desctbls.c for an example.
603843e1988Sjohnlev  */
604843e1988Sjohnlev 
605843e1988Sjohnlev #if defined(__xpv) && defined(__amd64)
606843e1988Sjohnlev #define	KCS_SEL		0xe030		/* FLAT_RING3_CS64 & 0xFFF0 */
607843e1988Sjohnlev #define	KDS_SEL		0xe02b		/* FLAT_RING3_SS64 */
608843e1988Sjohnlev #else
6097c478bd9Sstevel@tonic-gate #define	KCS_SEL		SEL_GDT(GDT_KCODE, SEL_KPL)
6107c478bd9Sstevel@tonic-gate #define	KDS_SEL		SEL_GDT(GDT_KDATA, SEL_KPL)
611843e1988Sjohnlev #endif
612843e1988Sjohnlev 
6137c478bd9Sstevel@tonic-gate #define	UCS_SEL		SEL_GDT(GDT_UCODE, SEL_UPL)
6147c478bd9Sstevel@tonic-gate #if defined(__amd64)
6157c478bd9Sstevel@tonic-gate #define	TEMP_CS64_SEL	SEL_GDT(TEMPGDT_KCODE64, SEL_KPL)
6167c478bd9Sstevel@tonic-gate #define	U32CS_SEL	SEL_GDT(GDT_U32CODE, SEL_UPL)
617843e1988Sjohnlev #endif
618843e1988Sjohnlev 
6197c478bd9Sstevel@tonic-gate #define	UDS_SEL		SEL_GDT(GDT_UDATA, SEL_UPL)
6207c478bd9Sstevel@tonic-gate #define	ULDT_SEL	SEL_GDT(GDT_LDT, SEL_KPL)
6217c478bd9Sstevel@tonic-gate #define	KTSS_SEL	SEL_GDT(GDT_KTSS, SEL_KPL)
6227c478bd9Sstevel@tonic-gate #define	DFTSS_SEL	SEL_GDT(GDT_DBFLT, SEL_KPL)
623ae115bc7Smrj #define	KFS_SEL		0
6247c478bd9Sstevel@tonic-gate #define	KGS_SEL		SEL_GDT(GDT_GS, SEL_KPL)
6257c478bd9Sstevel@tonic-gate #define	LWPFS_SEL	SEL_GDT(GDT_LWPFS, SEL_UPL)
6267c478bd9Sstevel@tonic-gate #define	LWPGS_SEL	SEL_GDT(GDT_LWPGS, SEL_UPL)
6279acbbeafSnn #define	BRANDMIN_SEL	SEL_GDT(GDT_BRANDMIN, SEL_UPL)
6289acbbeafSnn #define	BRANDMAX_SEL	SEL_GDT(GDT_BRANDMAX, SEL_UPL)
629ae115bc7Smrj 
6307c478bd9Sstevel@tonic-gate #define	B64CODE_SEL	SEL_GDT(GDT_B64CODE, SEL_KPL)
631ae115bc7Smrj #define	B32CODE_SEL	SEL_GDT(GDT_B32CODE, SEL_KPL)
632ae115bc7Smrj #define	B32DATA_SEL	SEL_GDT(GDT_B32DATA, SEL_KPL)
633ae115bc7Smrj #define	B16CODE_SEL	SEL_GDT(GDT_B16CODE, SEL_KPL)
634ae115bc7Smrj #define	B16DATA_SEL	SEL_GDT(GDT_B16DATA, SEL_KPL)
635ae115bc7Smrj 
636ae115bc7Smrj /*
637ae115bc7Smrj  * Temporary %gs descriptor used by kmdb with -d option. Only lives
638ae115bc7Smrj  * in boot's GDT and is not copied into kernel's GDT from boot.
639ae115bc7Smrj  */
640ae115bc7Smrj #define	KMDBGS_SEL	SEL_GDT(GDT_BGSTMP, SEL_KPL)
641ae115bc7Smrj 
642ae115bc7Smrj /*
643ae115bc7Smrj  * Selector used for kdi_idt when kmdb has taken over the IDT.
644ae115bc7Smrj  */
645ae115bc7Smrj #if defined(__amd64)
646ae115bc7Smrj #define	KMDBCODE_SEL	B64CODE_SEL
6477c478bd9Sstevel@tonic-gate #else
648ae115bc7Smrj #define	KMDBCODE_SEL	B32CODE_SEL
6497c478bd9Sstevel@tonic-gate #endif
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate  * Entries in default Local Descriptor Table (LDT) for every process.
6537c478bd9Sstevel@tonic-gate  */
6547c478bd9Sstevel@tonic-gate #define	LDT_SYSCALL	0	/* call gate for libc.a (obsolete) */
6557c478bd9Sstevel@tonic-gate #define	LDT_SIGCALL	1	/* EOL me, call gate for static sigreturn */
6567c478bd9Sstevel@tonic-gate #define	LDT_RESVD1	2	/* old user %cs */
6577c478bd9Sstevel@tonic-gate #define	LDT_RESVD2	3	/* old user %ds */
6587c478bd9Sstevel@tonic-gate #define	LDT_ALTSYSCALL	4	/* alternate call gate for system calls */
6597c478bd9Sstevel@tonic-gate #define	LDT_ALTSIGCALL	5	/* EOL me, alternate call gate for sigreturn */
6607c478bd9Sstevel@tonic-gate #define	LDT_UDBASE	6	/* user descriptor base index */
661843e1988Sjohnlev #define	MINNLDT		512	/* Current min solaris ldt size (1 4K page) */
662843e1988Sjohnlev #define	MAXNLDT		8192	/* max solaris ldt size (16 4K pages) */
6637c478bd9Sstevel@tonic-gate 
66474ecdb51SJohn Levon #ifdef _KERNEL
66574ecdb51SJohn Levon #define	LDT_CPU_SIZE	(16 * 4096)	/* Size of kernel per-CPU allocation */
66674ecdb51SJohn Levon #endif
66774ecdb51SJohn Levon 
6687c478bd9Sstevel@tonic-gate #ifndef	_ASM
6697c478bd9Sstevel@tonic-gate 
6700cfdb603Sjosephb extern	gate_desc_t	*idt0;
6717c478bd9Sstevel@tonic-gate extern	desctbr_t	idt0_default_reg;
672ae115bc7Smrj extern	user_desc_t	*gdt0;
6730baeff3dSrab 
6747c478bd9Sstevel@tonic-gate extern user_desc_t	zero_udesc;
675843e1988Sjohnlev extern user_desc_t	null_udesc;
676843e1988Sjohnlev extern system_desc_t	null_sdesc;
6770baeff3dSrab 
6787c478bd9Sstevel@tonic-gate #if defined(__amd64)
6797c478bd9Sstevel@tonic-gate extern user_desc_t	zero_u32desc;
6807c478bd9Sstevel@tonic-gate #endif
681843e1988Sjohnlev #if defined(__amd64)
682843e1988Sjohnlev extern user_desc_t	ucs_on;
683843e1988Sjohnlev extern user_desc_t	ucs_off;
684843e1988Sjohnlev extern user_desc_t	ucs32_on;
685843e1988Sjohnlev extern user_desc_t	ucs32_off;
686843e1988Sjohnlev #endif  /* __amd64 */
6877c478bd9Sstevel@tonic-gate 
688f16a0f4cSRobert Mustacchi extern tss_t *ktss0;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate #if defined(__i386)
691f16a0f4cSRobert Mustacchi extern tss_t *dftss0;
6927c478bd9Sstevel@tonic-gate #endif	/* __i386 */
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate extern void div0trap(), dbgtrap(), nmiint(), brktrap(), ovflotrap();
695843e1988Sjohnlev extern void boundstrap(), invoptrap(), ndptrap();
696843e1988Sjohnlev #if !defined(__xpv)
697843e1988Sjohnlev extern void syserrtrap();
698843e1988Sjohnlev #endif
6997c478bd9Sstevel@tonic-gate extern void invaltrap(), invtsstrap(), segnptrap(), stktrap();
7007c478bd9Sstevel@tonic-gate extern void gptrap(), pftrap(), ndperr();
7017c478bd9Sstevel@tonic-gate extern void overrun(), resvtrap();
7027c478bd9Sstevel@tonic-gate extern void _start(), cmnint();
7037c478bd9Sstevel@tonic-gate extern void achktrap(), mcetrap();
7047c478bd9Sstevel@tonic-gate extern void xmtrap();
7057c478bd9Sstevel@tonic-gate extern void fasttrap();
706f498645aSahl extern void dtrace_ret();
7077c478bd9Sstevel@tonic-gate 
70874ecdb51SJohn Levon /* KPTI trampolines */
70974ecdb51SJohn Levon extern void tr_invaltrap();
71074ecdb51SJohn Levon extern void tr_div0trap(), tr_dbgtrap(), tr_nmiint(), tr_brktrap();
71174ecdb51SJohn Levon extern void tr_ovflotrap(), tr_boundstrap(), tr_invoptrap(), tr_ndptrap();
71274ecdb51SJohn Levon #if !defined(__xpv)
71374ecdb51SJohn Levon extern void tr_syserrtrap();
71474ecdb51SJohn Levon #endif
71574ecdb51SJohn Levon extern void tr_invaltrap(), tr_invtsstrap(), tr_segnptrap(), tr_stktrap();
71674ecdb51SJohn Levon extern void tr_gptrap(), tr_pftrap(), tr_ndperr();
71774ecdb51SJohn Levon extern void tr_overrun(), tr_resvtrap();
71874ecdb51SJohn Levon extern void tr_achktrap(), tr_mcetrap();
71974ecdb51SJohn Levon extern void tr_xmtrap();
72074ecdb51SJohn Levon extern void tr_fasttrap();
72174ecdb51SJohn Levon extern void tr_dtrace_ret();
72274ecdb51SJohn Levon 
7237c478bd9Sstevel@tonic-gate #if !defined(__amd64)
7247c478bd9Sstevel@tonic-gate extern void pentium_pftrap();
7257c478bd9Sstevel@tonic-gate #endif
7267c478bd9Sstevel@tonic-gate 
72774ecdb51SJohn Levon extern uint64_t kpti_enable;
72874ecdb51SJohn Levon 
7297c478bd9Sstevel@tonic-gate #endif /* _ASM */
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate #endif
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate #endif /* _SYS_SEGMENTS_H */
736