xref: /titanic_50/usr/src/uts/intel/sys/segments.h (revision 0cfdb6036e046270988a17ac442e4d717d426a44)
17c478bd9Sstevel@tonic-gate /*
2ae115bc7Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate #ifndef	_SYS_SEGMENTS_H
77c478bd9Sstevel@tonic-gate #define	_SYS_SEGMENTS_H
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
107c478bd9Sstevel@tonic-gate 
117c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
127c478bd9Sstevel@tonic-gate extern "C" {
137c478bd9Sstevel@tonic-gate #endif
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate /*
167c478bd9Sstevel@tonic-gate  * Copyright (c) 1989, 1990 William F. Jolitz
177c478bd9Sstevel@tonic-gate  * Copyright (c) 1990 The Regents of the University of California.
187c478bd9Sstevel@tonic-gate  * All rights reserved.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * This code is derived from software contributed to Berkeley by
217c478bd9Sstevel@tonic-gate  * William Jolitz.
227c478bd9Sstevel@tonic-gate  *
237c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
247c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
257c478bd9Sstevel@tonic-gate  * are met:
267c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
277c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
287c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
297c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
307c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
317c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
327c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
337c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
347c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
357c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
367c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
377c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
407c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
417c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
427c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
437c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
447c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
457c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
467c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
477c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
487c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
497c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  *	from: @(#)segments.h	7.1 (Berkeley) 5/9/91
527c478bd9Sstevel@tonic-gate  * $FreeBSD: src/sys/i386/include/segments.h,v 1.34 2003/09/10 01:07:04
537c478bd9Sstevel@tonic-gate  * jhb Exp $
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * 386 Segmentation Data Structures and definitions
567c478bd9Sstevel@tonic-gate  *	William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/tss.h>
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * Selector register format
637c478bd9Sstevel@tonic-gate  * CS, DS, ES, FS, GS, SS
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  *  15                  3  2  1 0
667c478bd9Sstevel@tonic-gate  * +---------------------+---+----+
677c478bd9Sstevel@tonic-gate  * |          SI         |TI |RPL |
687c478bd9Sstevel@tonic-gate  * +---------------------+---+----+
697c478bd9Sstevel@tonic-gate  *
707c478bd9Sstevel@tonic-gate  * SI  = selector index
717c478bd9Sstevel@tonic-gate  * TI  = table indicator (0 = GDT, 1 = LDT)
727c478bd9Sstevel@tonic-gate  * RPL = requestor privilege level
737c478bd9Sstevel@tonic-gate  */
747c478bd9Sstevel@tonic-gate #if !defined(_ASM) || defined(__GNUC_AS__)
757c478bd9Sstevel@tonic-gate #define	IDXTOSEL(s)	((s) << 3)		/* index to selector */
767c478bd9Sstevel@tonic-gate #define	SEL_GDT(s, r)	(IDXTOSEL(s) | r)	/* global sel */
777c478bd9Sstevel@tonic-gate #else
787c478bd9Sstevel@tonic-gate #define	IDXTOSEL(s)	[s << 3]
797c478bd9Sstevel@tonic-gate #define	SEL_GDT(s, r)	[IDXTOSEL(s) | r]
807c478bd9Sstevel@tonic-gate #endif
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	SELTOIDX(s)	((s) >> 3)	/* selector to index */
83843e1988Sjohnlev 
84843e1988Sjohnlev /*
85843e1988Sjohnlev  * SEL_(KPL,UPL,XPL) is the RPL or DPL value for code and data selectors
86843e1988Sjohnlev  * and their descriptors respectively.
87843e1988Sjohnlev  *
88843e1988Sjohnlev  * TRP_(KPL,UPL,XPL) is used to indicate the DPL for system gates only.
89843e1988Sjohnlev  *
90843e1988Sjohnlev  * This distinction is important to support para-virt guests on the
91843e1988Sjohnlev  * 64-bit hypervisor. Both guest kernel and user run in ring 3 and the
92843e1988Sjohnlev  * hypervisor runs in ring 0. When the kernel creates its trap and
93843e1988Sjohnlev  * interrupt gates it needs a way to prevent any arbitrary int $n
94843e1988Sjohnlev  * instruction from entering a gate that is not expected. The hypervisor
95843e1988Sjohnlev  * allows these gates to have a DPL from 1 to 3. By creating gates
96843e1988Sjohnlev  * with a DPL below user (ring 3) the int $n will generate a #gp fault
97843e1988Sjohnlev  * which the hypervisor catches and forwards to the guest.
98843e1988Sjohnlev  */
99843e1988Sjohnlev #if defined(__xpv)
100843e1988Sjohnlev 
101843e1988Sjohnlev #if defined(__amd64)
102843e1988Sjohnlev 
103843e1988Sjohnlev #define	SEL_XPL		0		/* hypervisor privilege level */
104843e1988Sjohnlev #define	SEL_KPL		3		/* both kernel and user in ring 3 */
105843e1988Sjohnlev #define	TRP_KPL		1		/* system gate priv (user blocked) */
106843e1988Sjohnlev 
107843e1988Sjohnlev #elif defined(__i386)
108843e1988Sjohnlev 
109843e1988Sjohnlev #define	SEL_XPL		0		/* hypervisor privilege level */
110843e1988Sjohnlev #define	SEL_KPL		1		/* kernel privilege level */
111843e1988Sjohnlev #define	TRP_KPL		SEL_KPL		/* system gate priv (user blocked) */
112843e1988Sjohnlev 
113843e1988Sjohnlev #endif	/* __i386 */
114843e1988Sjohnlev 
115843e1988Sjohnlev #define	TRP_XPL		0		/* system gate priv (hypervisor) */
116843e1988Sjohnlev 
117843e1988Sjohnlev #else	/* __xpv */
118843e1988Sjohnlev 
119843e1988Sjohnlev #define	SEL_KPL		0		/* kernel privilege level on metal */
120843e1988Sjohnlev #define	TRP_KPL		SEL_KPL		/* system gate priv (user blocked) */
121843e1988Sjohnlev 
122843e1988Sjohnlev #endif	/* __xpv */
123843e1988Sjohnlev 
1247c478bd9Sstevel@tonic-gate #define	SEL_UPL		3		/* user priority level */
125843e1988Sjohnlev #define	TRP_UPL		3		/* system gate priv (user allowed) */
1267c478bd9Sstevel@tonic-gate #define	SEL_TI_LDT	4		/* local descriptor table */
1277c478bd9Sstevel@tonic-gate #define	SEL_LDT(s)	(IDXTOSEL(s) | SEL_TI_LDT | SEL_UPL)	/* local sel */
1287c478bd9Sstevel@tonic-gate #define	CPL_MASK	3		/* RPL mask for selector */
1297c478bd9Sstevel@tonic-gate #define	SELISLDT(s)	(((s) & SEL_TI_LDT) == SEL_TI_LDT)
1307c478bd9Sstevel@tonic-gate #define	SELISUPL(s)	(((s) & CPL_MASK) == SEL_UPL)
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate #ifndef	_ASM
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate typedef	uint16_t	selector_t;	/* selector reigster */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Hardware descriptor table register format for GDT and IDT.
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate #if defined(__amd64)
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate #pragma pack(2)
1427c478bd9Sstevel@tonic-gate typedef struct {
1437c478bd9Sstevel@tonic-gate 	uint16_t dtr_limit;	/* table limit */
1447c478bd9Sstevel@tonic-gate 	uint64_t dtr_base;	/* table base address  */
1457c478bd9Sstevel@tonic-gate } desctbr_t;
1467c478bd9Sstevel@tonic-gate #pragma	pack()
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate #elif defined(__i386)
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate #pragma pack(2)
1517c478bd9Sstevel@tonic-gate typedef struct {
1527c478bd9Sstevel@tonic-gate 	uint16_t dtr_limit;	/* table limit */
1537c478bd9Sstevel@tonic-gate 	uint32_t dtr_base;	/* table base address  */
1547c478bd9Sstevel@tonic-gate } desctbr_t;
1557c478bd9Sstevel@tonic-gate #pragma	pack()
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #endif	/* __i386 */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate /*
1607c478bd9Sstevel@tonic-gate  * Functions for loading and storing descriptor table
1617c478bd9Sstevel@tonic-gate  * registers.
1627c478bd9Sstevel@tonic-gate  */
1637c478bd9Sstevel@tonic-gate extern void rd_idtr(desctbr_t *);
1647c478bd9Sstevel@tonic-gate extern void wr_idtr(desctbr_t *);
1657c478bd9Sstevel@tonic-gate extern void rd_gdtr(desctbr_t *);
1667c478bd9Sstevel@tonic-gate extern void wr_gdtr(desctbr_t *);
1677c478bd9Sstevel@tonic-gate extern void wr_ldtr(selector_t);
1687c478bd9Sstevel@tonic-gate extern selector_t rd_ldtr(void);
1697c478bd9Sstevel@tonic-gate extern void wr_tsr(selector_t);
170ae115bc7Smrj extern void kmdb_enter(void);
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate #if defined(__amd64)
173ae115bc7Smrj 
174ae115bc7Smrj /*
175ae115bc7Smrj  * inlines for update_segregs
176ae115bc7Smrj  */
177ae115bc7Smrj extern void __set_ds(selector_t);
178ae115bc7Smrj extern void __set_es(selector_t);
179ae115bc7Smrj extern void __set_fs(selector_t);
180ae115bc7Smrj extern void __set_gs(selector_t);
181ae115bc7Smrj extern void __swapgs(void);
182ae115bc7Smrj #endif	/* __amd64 */
183ae115bc7Smrj 
184ae115bc7Smrj #if defined(__amd64)
185ae115bc7Smrj extern void load_segment_registers(selector_t, selector_t, selector_t,
186ae115bc7Smrj     selector_t);	/* (alphabetical) */
187ae115bc7Smrj #elif defined(__i386)
188ae115bc7Smrj extern void load_segment_registers(selector_t, selector_t, selector_t,
189ae115bc7Smrj     selector_t, selector_t, selector_t);	/* (alphabetical) */
190ae115bc7Smrj #endif	/* __i386 */
1917c478bd9Sstevel@tonic-gate 
192843e1988Sjohnlev selector_t get_cs_register();
193843e1988Sjohnlev 
1947c478bd9Sstevel@tonic-gate #if !defined(__amd64)
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * User segment descriptors (code and data).
1987c478bd9Sstevel@tonic-gate  * Legacy mode 64-bits wide.
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate typedef struct user_desc {
2017c478bd9Sstevel@tonic-gate 	uint32_t usd_lolimit:16;	/* segment limit 15:0 */
2027c478bd9Sstevel@tonic-gate 	uint32_t usd_lobase:16;		/* segment base 15:0 */
2037c478bd9Sstevel@tonic-gate 	uint32_t usd_midbase:8;		/* segment base 23:16 */
2047c478bd9Sstevel@tonic-gate 	uint32_t usd_type:5;		/* segment type, includes S bit */
2057c478bd9Sstevel@tonic-gate 	uint32_t usd_dpl:2;		/* segment descriptor priority level */
2067c478bd9Sstevel@tonic-gate 	uint32_t usd_p:1;		/* segment descriptor present */
2077c478bd9Sstevel@tonic-gate 	uint32_t usd_hilimit:4;		/* segment limit 19:16 */
2087c478bd9Sstevel@tonic-gate 	uint32_t usd_avl:1;		/* available to sw, but not used */
2097c478bd9Sstevel@tonic-gate 	uint32_t usd_reserved:1;	/* unused, ignored */
2107c478bd9Sstevel@tonic-gate 	uint32_t usd_def32:1;		/* default 32 vs 16 bit operand */
2117c478bd9Sstevel@tonic-gate 	uint32_t usd_gran:1;		/* limit units (bytes vs pages) */
2127c478bd9Sstevel@tonic-gate 	uint32_t usd_hibase:8;		/* segment base 31:24 */
2137c478bd9Sstevel@tonic-gate } user_desc_t;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate #define	USEGD_GETBASE(usd)		((usd)->usd_lobase |		\
2167c478bd9Sstevel@tonic-gate 					(usd)->usd_midbase << 16 |	\
2177c478bd9Sstevel@tonic-gate 					(usd)->usd_hibase << (16 + 8))
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate #define	USEGD_SETBASE(usd, b)		((usd)->usd_lobase = (b),	\
2207c478bd9Sstevel@tonic-gate 					(usd)->usd_midbase = (b) >> 16, \
2217c478bd9Sstevel@tonic-gate 					(usd)->usd_hibase = (b) >> (16 + 8))
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate #define	USEGD_GETLIMIT(usd)		((usd)->usd_lolimit |		\
2247c478bd9Sstevel@tonic-gate 					(usd)->usd_hilimit << 16)
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate #define	USEGD_SETLIMIT(usd, lim)	((usd)->usd_lolimit = lim,	\
2277c478bd9Sstevel@tonic-gate 					(usd)->usd_hilimit = lim >> 16)
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate #define	USD_TYPESHIFT			5	/* size of usd_type field */
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate #else	/* __amd64 */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate /*
2347c478bd9Sstevel@tonic-gate  * User segment descriptors.
2357c478bd9Sstevel@tonic-gate  * Long mode 64-bits wide.
2367c478bd9Sstevel@tonic-gate  *
2377c478bd9Sstevel@tonic-gate  * In 32-bit compatibility mode (%cs:usd_long=0) all fields are interpreted
2387c478bd9Sstevel@tonic-gate  * as in legacy mode for both code and data.
2397c478bd9Sstevel@tonic-gate  *
2407c478bd9Sstevel@tonic-gate  * In 64-bit mode (%cs:usd_long=1) code segments only have the conforming
2417c478bd9Sstevel@tonic-gate  * bit in usd_type, usd_dpl, usd_p, usd_long and usd_def32=0. usd_def32
2427c478bd9Sstevel@tonic-gate  * must be zero in 64-bit mode. Setting it to 1 is reserved for future use.
2437c478bd9Sstevel@tonic-gate  * All other fields are loaded but ignored by hardware.
2447c478bd9Sstevel@tonic-gate  *
2457c478bd9Sstevel@tonic-gate  * 64-bit data segments only have usd_p. All other fields are loaded but
2467c478bd9Sstevel@tonic-gate  * ignored by hardware when in 64-bit mode.
2477c478bd9Sstevel@tonic-gate  */
2487c478bd9Sstevel@tonic-gate typedef struct user_desc {
2497c478bd9Sstevel@tonic-gate 	uint64_t usd_lolimit:16;	/* segment limit 15:0 */
2507c478bd9Sstevel@tonic-gate 	uint64_t usd_lobase:16;		/* segment base 15:0 */
2517c478bd9Sstevel@tonic-gate 	uint64_t usd_midbase:8;		/* segment base 23:16 */
2527c478bd9Sstevel@tonic-gate 	uint64_t usd_type:5;		/* segment type, includes S bit */
2537c478bd9Sstevel@tonic-gate 	uint64_t usd_dpl:2;		/* segment descriptor priority level */
2547c478bd9Sstevel@tonic-gate 	uint64_t usd_p:1;		/* segment descriptor present */
2557c478bd9Sstevel@tonic-gate 	uint64_t usd_hilimit:4;		/* segment limit 19:16 */
2567c478bd9Sstevel@tonic-gate 	uint64_t usd_avl:1;		/* available to sw, but not used */
2577c478bd9Sstevel@tonic-gate 	uint64_t usd_long:1;		/* long mode (%cs only) */
2587c478bd9Sstevel@tonic-gate 	uint64_t usd_def32:1;		/* default 32 vs 16 bit operand */
2597c478bd9Sstevel@tonic-gate 	uint64_t usd_gran:1;		/* limit units (bytes vs page) */
2607c478bd9Sstevel@tonic-gate 	uint64_t usd_hibase:8;		/* segment base 31:24 */
2617c478bd9Sstevel@tonic-gate } user_desc_t;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate #define	USEGD_GETBASE(usd)		((usd)->usd_lobase |		\
2647c478bd9Sstevel@tonic-gate 					(usd)->usd_midbase << 16 |	\
2657c478bd9Sstevel@tonic-gate 					(usd)->usd_hibase << (16 + 8))
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate #define	USEGD_SETBASE(usd, b)		((usd)->usd_lobase = (b),	\
2687c478bd9Sstevel@tonic-gate 					(usd)->usd_midbase = (b) >> 16, \
2697c478bd9Sstevel@tonic-gate 					(usd)->usd_hibase = (b) >> (16 + 8))
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate #define	USEGD_GETLIMIT(usd)		((usd)->usd_lolimit |		\
2727c478bd9Sstevel@tonic-gate 					(usd)->usd_hilimit << 16)
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate #define	USEGD_SETLIMIT(usd, lim)	((usd)->usd_lolimit = lim,	\
2757c478bd9Sstevel@tonic-gate 					(usd)->usd_hilimit = lim >> 16)
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate #define	USD_TYPESHIFT			5	/* size of usd_type field */
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate #endif /* __amd64 */
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate #if !defined(__amd64)
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate /*
2847c478bd9Sstevel@tonic-gate  * System segment descriptors for LDT and TSS segments.
2857c478bd9Sstevel@tonic-gate  * Legacy mode 64-bits wide.
2867c478bd9Sstevel@tonic-gate  */
2877c478bd9Sstevel@tonic-gate typedef struct system_desc {
2887c478bd9Sstevel@tonic-gate 	uint32_t ssd_lolimit:16;	/* segment limit 15:0 */
2897c478bd9Sstevel@tonic-gate 	uint32_t ssd_lobase:16;		/* segment base 15:0 */
2907c478bd9Sstevel@tonic-gate 	uint32_t ssd_midbase:8;		/* segment base 23:16 */
2917c478bd9Sstevel@tonic-gate 	uint32_t ssd_type:4;		/* segment type */
2927c478bd9Sstevel@tonic-gate 	uint32_t ssd_zero:1;		/* must be zero */
2937c478bd9Sstevel@tonic-gate 	uint32_t ssd_dpl:2;		/* segment descriptor priority level */
2947c478bd9Sstevel@tonic-gate 	uint32_t ssd_p:1;		/* segment descriptor present */
2957c478bd9Sstevel@tonic-gate 	uint32_t ssd_hilimit:4;		/* segment limit 19:16 */
2967c478bd9Sstevel@tonic-gate 	uint32_t ssd_avl:1;		/* available to sw, but not used */
2977c478bd9Sstevel@tonic-gate 	uint32_t ssd_reserved:2;	/* unused, ignored */
2987c478bd9Sstevel@tonic-gate 	uint32_t ssd_gran:1;		/* limit unit (bytes vs pages) */
2997c478bd9Sstevel@tonic-gate 	uint32_t ssd_hibase:8;		/* segment base 31:24 */
3007c478bd9Sstevel@tonic-gate } system_desc_t;
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate #else	/* __amd64 */
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate  * System segment descriptors for LDT and TSS segments.
3067c478bd9Sstevel@tonic-gate  * Long mode 128-bits wide.
3077c478bd9Sstevel@tonic-gate  *
3087c478bd9Sstevel@tonic-gate  * 32-bit LDT and TSS descriptor types are redefined to 64-bit equivalents.
3097c478bd9Sstevel@tonic-gate  * All other legacy types are reserved and illegal.
3107c478bd9Sstevel@tonic-gate  */
3117c478bd9Sstevel@tonic-gate typedef struct system_desc {
3127c478bd9Sstevel@tonic-gate 	uint64_t ssd_lolimit:16;	/* segment limit 15:0 */
3137c478bd9Sstevel@tonic-gate 	uint64_t ssd_lobase:16;		/* segment base 15:0 */
3147c478bd9Sstevel@tonic-gate 	uint64_t ssd_midbase:8;		/* segment base 23:16 */
3157c478bd9Sstevel@tonic-gate 	uint64_t ssd_type:4;		/* segment type */
3167c478bd9Sstevel@tonic-gate 	uint64_t ssd_zero1:1;		/* must be zero */
3177c478bd9Sstevel@tonic-gate 	uint64_t ssd_dpl:2;		/* segment descriptor priority level */
3187c478bd9Sstevel@tonic-gate 	uint64_t ssd_p:1;		/* segment descriptor present */
3197c478bd9Sstevel@tonic-gate 	uint64_t ssd_hilimit:4;		/* segment limit 19:16 */
3207c478bd9Sstevel@tonic-gate 	uint64_t ssd_avl:1;		/* available to sw, but not used */
3217c478bd9Sstevel@tonic-gate 	uint64_t ssd_resv1:2;		/* unused, ignored */
3227c478bd9Sstevel@tonic-gate 	uint64_t ssd_gran:1;		/* limit unit (bytes vs pages) */
3237c478bd9Sstevel@tonic-gate 	uint64_t ssd_hibase:8;		/* segment base 31:24 */
3247c478bd9Sstevel@tonic-gate 	uint64_t ssd_hi64base:32;	/* segment base 63:32 */
3257c478bd9Sstevel@tonic-gate 	uint64_t ssd_resv2:8;		/* unused, ignored */
3267c478bd9Sstevel@tonic-gate 	uint64_t ssd_zero2:5;		/* must be zero */
3277c478bd9Sstevel@tonic-gate 	uint64_t ssd_resv3:19;		/* unused, ignored */
3287c478bd9Sstevel@tonic-gate } system_desc_t;
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate #endif	/* __amd64 */
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate #define	SYSSEGD_SETLIMIT(ssd, lim)	((ssd)->ssd_lolimit = lim,	\
3337c478bd9Sstevel@tonic-gate 					(ssd)->ssd_hilimit = lim >> 16)
334843e1988Sjohnlev 
335843e1988Sjohnlev #define	SYSSEGD_GETLIMIT(ssd)		(((ssd)->ssd_hilimit << 16) |   \
336843e1988Sjohnlev 					(ssd)->ssd_lolimit)
337843e1988Sjohnlev 
3387c478bd9Sstevel@tonic-gate #if !defined(__amd64)
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate  * System gate segment descriptors for interrupt, trap, call and task gates.
3427c478bd9Sstevel@tonic-gate  * Legacy mode 64-bits wide.
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate typedef struct gate_desc {
3457c478bd9Sstevel@tonic-gate 	uint32_t sgd_looffset:16;	/* segment code offset 15:0 */
3467c478bd9Sstevel@tonic-gate 	uint32_t sgd_selector:16;	/* target code or task selector */
3477c478bd9Sstevel@tonic-gate 	uint32_t sgd_stkcpy:5;		/* number of stack wds to cpy */
3487c478bd9Sstevel@tonic-gate 	uint32_t sgd_resv:3;		/* unused, ignored */
3497c478bd9Sstevel@tonic-gate 	uint32_t sgd_type:5;		/* segment type, includes S bit */
3507c478bd9Sstevel@tonic-gate 	uint32_t sgd_dpl:2;		/* segment descriptor priority level */
3517c478bd9Sstevel@tonic-gate 	uint32_t sgd_p:1;		/* segment descriptor present */
3527c478bd9Sstevel@tonic-gate 	uint32_t sgd_hioffset:16;	/* code seg off 31:16 */
3537c478bd9Sstevel@tonic-gate } gate_desc_t;
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate #define	GATESEG_GETOFFSET(sgd)		((sgd)->sgd_looffset |		\
3567c478bd9Sstevel@tonic-gate 					(sgd)->sgd_hioffset << 16)
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate #else	/* __amd64 */
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate  * System segment descriptors for interrupt, trap and call gates.
3627c478bd9Sstevel@tonic-gate  * Long mode 128-bits wide.
3637c478bd9Sstevel@tonic-gate  *
3647c478bd9Sstevel@tonic-gate  * 32-bit interrupt, trap and call gate types are redefined to 64-bit
3657c478bd9Sstevel@tonic-gate  * equivalents. Task gates along with all other legacy types are reserved
3667c478bd9Sstevel@tonic-gate  * and illegal.
3677c478bd9Sstevel@tonic-gate  */
3687c478bd9Sstevel@tonic-gate typedef struct gate_desc {
3697c478bd9Sstevel@tonic-gate 	uint64_t sgd_looffset:16;	/* segment code offset 15:0 */
3707c478bd9Sstevel@tonic-gate 	uint64_t sgd_selector:16;	/* target code or task selector */
3717c478bd9Sstevel@tonic-gate 	uint64_t sgd_ist:3;		/* IST table index */
3727c478bd9Sstevel@tonic-gate 	uint64_t sgd_resv1:5;		/* unused, ignored */
3737c478bd9Sstevel@tonic-gate 	uint64_t sgd_type:5;		/* segment type, includes S bit */
3747c478bd9Sstevel@tonic-gate 	uint64_t sgd_dpl:2;		/* segment descriptor priority level */
3757c478bd9Sstevel@tonic-gate 	uint64_t sgd_p:1;		/* segment descriptor present */
3767c478bd9Sstevel@tonic-gate 	uint64_t sgd_hioffset:16;	/* segment code offset 31:16 */
3777c478bd9Sstevel@tonic-gate 	uint64_t sgd_hi64offset:32;	/* segment code offset 63:32 */
3787c478bd9Sstevel@tonic-gate 	uint64_t sgd_resv2:8;		/* unused, ignored */
3797c478bd9Sstevel@tonic-gate 	uint64_t sgd_zero:5;		/* call gate only: must be zero */
3807c478bd9Sstevel@tonic-gate 	uint64_t sgd_resv3:19;		/* unused, ignored */
3817c478bd9Sstevel@tonic-gate } gate_desc_t;
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate #define	GATESEG_GETOFFSET(sgd)		((sgd)->sgd_looffset |		\
3847c478bd9Sstevel@tonic-gate 					(sgd)->sgd_hioffset << 16 |	\
3857c478bd9Sstevel@tonic-gate 					(sgd)->sgd_hi64offset << 32)
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate #endif	/* __amd64 */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*
3907c478bd9Sstevel@tonic-gate  * functions for initializing and updating segment descriptors.
3917c478bd9Sstevel@tonic-gate  */
3927c478bd9Sstevel@tonic-gate #if defined(__amd64)
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate extern void set_usegd(user_desc_t *, uint_t, void *, size_t, uint_t, uint_t,
3957c478bd9Sstevel@tonic-gate     uint_t, uint_t);
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate #elif defined(__i386)
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate extern void set_usegd(user_desc_t *, void *, size_t, uint_t, uint_t,
4007c478bd9Sstevel@tonic-gate     uint_t, uint_t);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate #endif	/* __i386 */
4037c478bd9Sstevel@tonic-gate 
404ae115bc7Smrj extern void set_gatesegd(gate_desc_t *, void (*)(void), selector_t,
405ae115bc7Smrj     uint_t, uint_t);
406ae115bc7Smrj 
407843e1988Sjohnlev extern void set_syssegd(system_desc_t *, void *, size_t, uint_t, uint_t);
408843e1988Sjohnlev 
409843e1988Sjohnlev extern void *get_ssd_base(system_desc_t *);
410843e1988Sjohnlev 
411843e1988Sjohnlev extern void gdt_update_usegd(uint_t, user_desc_t *);
412843e1988Sjohnlev 
413843e1988Sjohnlev extern int ldt_update_segd(user_desc_t *, user_desc_t *);
414843e1988Sjohnlev 
415843e1988Sjohnlev #if defined(__xpv)
416843e1988Sjohnlev 
417843e1988Sjohnlev extern int xen_idt_to_trap_info(uint_t, gate_desc_t *, void *);
418843e1988Sjohnlev extern void xen_idt_write(gate_desc_t *, uint_t);
419843e1988Sjohnlev 
420843e1988Sjohnlev #endif	/* __xen */
4217c478bd9Sstevel@tonic-gate 
422ae115bc7Smrj void init_boot_gdt(user_desc_t *);
423ae115bc7Smrj 
4247c478bd9Sstevel@tonic-gate #endif	/* _ASM */
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate /*
4277c478bd9Sstevel@tonic-gate  * Common segment parameter defintions for granularity, default
4287c478bd9Sstevel@tonic-gate  * operand size and operaton mode.
4297c478bd9Sstevel@tonic-gate  */
4307c478bd9Sstevel@tonic-gate #define	SDP_BYTES	0	/* segment limit scaled to bytes */
4317c478bd9Sstevel@tonic-gate #define	SDP_PAGES	1	/* segment limit scaled to pages */
4327c478bd9Sstevel@tonic-gate #define	SDP_OP32	1	/* code and data default operand = 32 bits */
4337c478bd9Sstevel@tonic-gate #define	SDP_LONG	1	/* long mode code segment (64 bits) */
4347c478bd9Sstevel@tonic-gate #define	SDP_SHORT	0	/* compat/legacy code segment (32 bits) */
4357c478bd9Sstevel@tonic-gate /*
4367c478bd9Sstevel@tonic-gate  * System segments and gate types.
4377c478bd9Sstevel@tonic-gate  *
4387c478bd9Sstevel@tonic-gate  * In long mode i386 32-bit ldt, tss, call, interrupt and trap gate
4397c478bd9Sstevel@tonic-gate  * types are redefined into 64-bit equivalents.
4407c478bd9Sstevel@tonic-gate  */
4417c478bd9Sstevel@tonic-gate #define	SDT_SYSNULL	 0	/* system null */
4427c478bd9Sstevel@tonic-gate #define	SDT_SYS286TSS	 1	/* system 286 TSS available */
4437c478bd9Sstevel@tonic-gate #define	SDT_SYSLDT	 2	/* system local descriptor table */
4447c478bd9Sstevel@tonic-gate #define	SDT_SYS286BSY	 3	/* system 286 TSS busy */
4457c478bd9Sstevel@tonic-gate #define	SDT_SYS286CGT	 4	/* system 286 call gate */
4467c478bd9Sstevel@tonic-gate #define	SDT_SYSTASKGT	 5	/* system task gate */
4477c478bd9Sstevel@tonic-gate #define	SDT_SYS286IGT	 6	/* system 286 interrupt gate */
4487c478bd9Sstevel@tonic-gate #define	SDT_SYS286TGT	 7	/* system 286 trap gate */
4497c478bd9Sstevel@tonic-gate #define	SDT_SYSNULL2	 8	/* system null again */
4507c478bd9Sstevel@tonic-gate #define	SDT_SYSTSS	 9	/* system TSS available */
4517c478bd9Sstevel@tonic-gate #define	SDT_SYSNULL3	10	/* system null again */
4527c478bd9Sstevel@tonic-gate #define	SDT_SYSTSSBSY	11	/* system TSS busy */
4537c478bd9Sstevel@tonic-gate #define	SDT_SYSCGT	12	/* system call gate */
4547c478bd9Sstevel@tonic-gate #define	SDT_SYSNULL4	13	/* system null again */
4557c478bd9Sstevel@tonic-gate #define	SDT_SYSIGT	14	/* system interrupt gate */
4567c478bd9Sstevel@tonic-gate #define	SDT_SYSTGT	15	/* system trap gate */
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate  * Memory segment types.
4607c478bd9Sstevel@tonic-gate  *
4617c478bd9Sstevel@tonic-gate  * While in long mode expand-down, writable and accessed type field
4627c478bd9Sstevel@tonic-gate  * attributes are ignored. Only the conforming bit is loaded by hardware
4637c478bd9Sstevel@tonic-gate  * for long mode code segment descriptors.
4647c478bd9Sstevel@tonic-gate  */
4657c478bd9Sstevel@tonic-gate #define	SDT_MEMRO	16	/* read only */
4667c478bd9Sstevel@tonic-gate #define	SDT_MEMROA	17	/* read only accessed */
4677c478bd9Sstevel@tonic-gate #define	SDT_MEMRW	18	/* read write */
4687c478bd9Sstevel@tonic-gate #define	SDT_MEMRWA	19	/* read write accessed */
4697c478bd9Sstevel@tonic-gate #define	SDT_MEMROD	20	/* read only expand dwn limit */
4707c478bd9Sstevel@tonic-gate #define	SDT_MEMRODA	21	/* read only expand dwn limit accessed */
4717c478bd9Sstevel@tonic-gate #define	SDT_MEMRWD	22	/* read write expand dwn limit */
4727c478bd9Sstevel@tonic-gate #define	SDT_MEMRWDA	23	/* read write expand dwn limit accessed */
4737c478bd9Sstevel@tonic-gate #define	SDT_MEME	24	/* execute only */
4747c478bd9Sstevel@tonic-gate #define	SDT_MEMEA	25	/* execute only accessed */
4757c478bd9Sstevel@tonic-gate #define	SDT_MEMER	26	/* execute read */
4767c478bd9Sstevel@tonic-gate #define	SDT_MEMERA	27	/* execute read accessed */
4777c478bd9Sstevel@tonic-gate #define	SDT_MEMEC	28	/* execute only conforming */
4787c478bd9Sstevel@tonic-gate #define	SDT_MEMEAC	29	/* execute only accessed conforming */
4797c478bd9Sstevel@tonic-gate #define	SDT_MEMERC	30	/* execute read conforming */
4807c478bd9Sstevel@tonic-gate #define	SDT_MEMERAC	31	/* execute read accessed conforming */
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate /*
4837c478bd9Sstevel@tonic-gate  * Entries in the Interrupt Descriptor Table (IDT)
4847c478bd9Sstevel@tonic-gate  */
4857c478bd9Sstevel@tonic-gate #define	IDT_DE		0	/* #DE: Divide Error */
4867c478bd9Sstevel@tonic-gate #define	IDT_DB		1	/* #DB: Debug */
4877c478bd9Sstevel@tonic-gate #define	IDT_NMI		2	/* Nonmaskable External Interrupt */
4887c478bd9Sstevel@tonic-gate #define	IDT_BP		3	/* #BP: Breakpoint */
4897c478bd9Sstevel@tonic-gate #define	IDT_OF		4	/* #OF: Overflow */
4907c478bd9Sstevel@tonic-gate #define	IDT_BR		5	/* #BR: Bound Range Exceeded */
4917c478bd9Sstevel@tonic-gate #define	IDT_UD		6	/* #UD: Undefined/Invalid Opcode */
4927c478bd9Sstevel@tonic-gate #define	IDT_NM		7	/* #NM: No Math Coprocessor */
4937c478bd9Sstevel@tonic-gate #define	IDT_DF		8	/* #DF: Double Fault */
4947c478bd9Sstevel@tonic-gate #define	IDT_FPUGP	9	/* Coprocessor Segment Overrun */
4957c478bd9Sstevel@tonic-gate #define	IDT_TS		10	/* #TS: Invalid TSS */
4967c478bd9Sstevel@tonic-gate #define	IDT_NP		11	/* #NP: Segment Not Present */
4977c478bd9Sstevel@tonic-gate #define	IDT_SS		12	/* #SS: Stack Segment Fault */
4987c478bd9Sstevel@tonic-gate #define	IDT_GP		13	/* #GP: General Protection Fault */
4997c478bd9Sstevel@tonic-gate #define	IDT_PF		14	/* #PF: Page Fault */
5007c478bd9Sstevel@tonic-gate #define	IDT_MF		16	/* #MF: FPU Floating-Point Error */
5017c478bd9Sstevel@tonic-gate #define	IDT_AC		17	/* #AC: Alignment Check */
5027c478bd9Sstevel@tonic-gate #define	IDT_MC		18	/* #MC: Machine Check */
5037c478bd9Sstevel@tonic-gate #define	IDT_XF		19	/* #XF: SIMD Floating-Point Exception */
5047c478bd9Sstevel@tonic-gate #define	NIDT		256	/* size in entries of IDT */
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate /*
5077c478bd9Sstevel@tonic-gate  * Entries in the Global Descriptor Table (GDT)
5087c478bd9Sstevel@tonic-gate  *
5097c478bd9Sstevel@tonic-gate  * We make sure to space the system descriptors (LDT's, TSS')
5107c478bd9Sstevel@tonic-gate  * such that they are double gdt slot aligned. This is because
5117c478bd9Sstevel@tonic-gate  * in long mode system segment decriptors expand to 128 bits.
5127c478bd9Sstevel@tonic-gate  *
5137c478bd9Sstevel@tonic-gate  * GDT_LWPFS and GDT_LWPGS must be the same for both 32 and 64-bit
514ae115bc7Smrj  * kernels. See setup_context in libc. 64-bit processes must set
515ae115bc7Smrj  * %fs or %gs to null selector to use 64-bit fsbase or gsbase
516ae115bc7Smrj  * respectively.
5177c478bd9Sstevel@tonic-gate  */
518ae115bc7Smrj #define	GDT_NULL	0	/* null */
519ae115bc7Smrj #define	GDT_B32DATA	1	/* dboot 32 bit data descriptor */
520ae115bc7Smrj #define	GDT_B32CODE	2	/* dboot 32 bit code descriptor */
521ae115bc7Smrj #define	GDT_B16CODE	3	/* bios call 16 bit code descriptor */
522ae115bc7Smrj #define	GDT_B16DATA	4	/* bios call 16 bit data descriptor */
523ae115bc7Smrj #define	GDT_B64CODE	5	/* dboot 64 bit code descriptor */
524ae115bc7Smrj #define	GDT_BGSTMP	7	/* kmdb descriptor only used early in boot */
525ae115bc7Smrj 
5267c478bd9Sstevel@tonic-gate #if defined(__amd64)
5277c478bd9Sstevel@tonic-gate 
528ae115bc7Smrj #define	GDT_KCODE	6	/* kernel code seg %cs */
529ae115bc7Smrj #define	GDT_KDATA	7	/* kernel data seg %ds */
530ae115bc7Smrj #define	GDT_U32CODE	8	/* 32-bit process on 64-bit kernel %cs */
531ae115bc7Smrj #define	GDT_UDATA	9	/* user data seg %ds (32 and 64 bit) */
532ae115bc7Smrj #define	GDT_UCODE	10	/* native user code  seg %cs */
533ae115bc7Smrj #define	GDT_LDT		12	/* LDT for current process */
534ae115bc7Smrj #define	GDT_KTSS	14	/* kernel tss */
5357c478bd9Sstevel@tonic-gate #define	GDT_FS		GDT_NULL /* kernel %fs segment selector */
5367c478bd9Sstevel@tonic-gate #define	GDT_GS		GDT_NULL /* kernel %gs segment selector */
537ae115bc7Smrj #define	GDT_LWPFS	55	/* lwp private %fs segment selector (32-bit) */
538ae115bc7Smrj #define	GDT_LWPGS	56	/* lwp private %gs segment selector (32-bit) */
5399acbbeafSnn35248 #define	GDT_BRANDMIN	57	/* first entry in GDT for brand usage */
5409acbbeafSnn35248 #define	GDT_BRANDMAX	61	/* last entry in GDT for brand usage */
5419acbbeafSnn35248 #define	NGDT		62	/* number of entries in GDT */
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /*
5447c478bd9Sstevel@tonic-gate  * This selector is only used in the temporary GDT used to bring additional
5457c478bd9Sstevel@tonic-gate  * CPUs from 16-bit real mode into long mode in real_mode_start().
5467c478bd9Sstevel@tonic-gate  */
5477c478bd9Sstevel@tonic-gate #define	TEMPGDT_KCODE64	1	/* 64-bit code selector */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate #elif defined(__i386)
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate #define	GDT_LDT		40	/* LDT for current process */
5527c478bd9Sstevel@tonic-gate #define	GDT_KTSS	42	/* kernel tss */
5537c478bd9Sstevel@tonic-gate #define	GDT_KCODE	43	/* kernel code seg %cs */
5547c478bd9Sstevel@tonic-gate #define	GDT_KDATA	44	/* kernel data seg %ds */
5557c478bd9Sstevel@tonic-gate #define	GDT_UCODE	45	/* native user code  seg %cs */
5567c478bd9Sstevel@tonic-gate #define	GDT_UDATA	46	/* user data seg %ds (32 and 64 bit) */
5577c478bd9Sstevel@tonic-gate #define	GDT_DBFLT	47	/* double fault #DF selector */
5587c478bd9Sstevel@tonic-gate #define	GDT_FS		53	/* kernel %fs segment selector */
5597c478bd9Sstevel@tonic-gate #define	GDT_GS		54	/* kernel %gs segment selector */
5607c478bd9Sstevel@tonic-gate #define	GDT_LWPFS	55	/* lwp private %fs segment selector */
5617c478bd9Sstevel@tonic-gate #define	GDT_LWPGS	56	/* lwp private %gs segment selector */
5629acbbeafSnn35248 #define	GDT_BRANDMIN	57	/* first entry in GDT for brand usage */
5639acbbeafSnn35248 #define	GDT_BRANDMAX	61	/* last entry in GDT for brand usage */
564843e1988Sjohnlev #if !defined(__xpv)
5657c478bd9Sstevel@tonic-gate #define	NGDT		90	/* number of entries in GDT */
566843e1988Sjohnlev #else
567843e1988Sjohnlev #define	NGDT		512	/* single 4K page for the hypervisor */
568843e1988Sjohnlev #endif
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate #endif	/* __i386 */
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /*
5737c478bd9Sstevel@tonic-gate  * Convenient selector definitions.
5747c478bd9Sstevel@tonic-gate  */
575843e1988Sjohnlev 
576843e1988Sjohnlev /*
577843e1988Sjohnlev  * XXPV	64 bit Xen only allows the guest %cs/%ss be the private ones it
578843e1988Sjohnlev  * provides, not the ones we create for ourselves.  See FLAT_RING3_CS64 in
579843e1988Sjohnlev  * public/arch-x86_64.h
580843e1988Sjohnlev  *
581843e1988Sjohnlev  * 64-bit Xen runs paravirtual guests in ring 3 but emulates them running in
582843e1988Sjohnlev  * ring 0 by clearing CPL in %cs value pushed on guest exception stacks.
583843e1988Sjohnlev  * Therefore we will have KCS_SEL value indicate ring 0 and use that everywhere
584843e1988Sjohnlev  * in the kernel. But in the few files where we initialize segment registers or
585843e1988Sjohnlev  * create and update descriptors we will explicity OR in SEL_KPL (ring 3) for
586843e1988Sjohnlev  * kernel %cs. See desctbls.c for an example.
587843e1988Sjohnlev  */
588843e1988Sjohnlev 
589843e1988Sjohnlev #if defined(__xpv) && defined(__amd64)
590843e1988Sjohnlev #define	KCS_SEL		0xe030		/* FLAT_RING3_CS64 & 0xFFF0 */
591843e1988Sjohnlev #define	KDS_SEL		0xe02b		/* FLAT_RING3_SS64 */
592843e1988Sjohnlev #else
5937c478bd9Sstevel@tonic-gate #define	KCS_SEL		SEL_GDT(GDT_KCODE, SEL_KPL)
5947c478bd9Sstevel@tonic-gate #define	KDS_SEL		SEL_GDT(GDT_KDATA, SEL_KPL)
595843e1988Sjohnlev #endif
596843e1988Sjohnlev 
5977c478bd9Sstevel@tonic-gate #define	UCS_SEL		SEL_GDT(GDT_UCODE, SEL_UPL)
5987c478bd9Sstevel@tonic-gate #if defined(__amd64)
5997c478bd9Sstevel@tonic-gate #define	TEMP_CS64_SEL	SEL_GDT(TEMPGDT_KCODE64, SEL_KPL)
6007c478bd9Sstevel@tonic-gate #define	U32CS_SEL	SEL_GDT(GDT_U32CODE, SEL_UPL)
601843e1988Sjohnlev #endif
602843e1988Sjohnlev 
6037c478bd9Sstevel@tonic-gate #define	UDS_SEL		SEL_GDT(GDT_UDATA, SEL_UPL)
6047c478bd9Sstevel@tonic-gate #define	ULDT_SEL	SEL_GDT(GDT_LDT, SEL_KPL)
6057c478bd9Sstevel@tonic-gate #define	KTSS_SEL	SEL_GDT(GDT_KTSS, SEL_KPL)
6067c478bd9Sstevel@tonic-gate #define	DFTSS_SEL	SEL_GDT(GDT_DBFLT, SEL_KPL)
607ae115bc7Smrj #define	KFS_SEL		0
6087c478bd9Sstevel@tonic-gate #define	KGS_SEL		SEL_GDT(GDT_GS, SEL_KPL)
6097c478bd9Sstevel@tonic-gate #define	LWPFS_SEL	SEL_GDT(GDT_LWPFS, SEL_UPL)
6107c478bd9Sstevel@tonic-gate #define	LWPGS_SEL	SEL_GDT(GDT_LWPGS, SEL_UPL)
6119acbbeafSnn35248 #define	BRANDMIN_SEL	SEL_GDT(GDT_BRANDMIN, SEL_UPL)
6129acbbeafSnn35248 #define	BRANDMAX_SEL	SEL_GDT(GDT_BRANDMAX, SEL_UPL)
613ae115bc7Smrj 
6147c478bd9Sstevel@tonic-gate #define	B64CODE_SEL	SEL_GDT(GDT_B64CODE, SEL_KPL)
615ae115bc7Smrj #define	B32CODE_SEL	SEL_GDT(GDT_B32CODE, SEL_KPL)
616ae115bc7Smrj #define	B32DATA_SEL	SEL_GDT(GDT_B32DATA, SEL_KPL)
617ae115bc7Smrj #define	B16CODE_SEL	SEL_GDT(GDT_B16CODE, SEL_KPL)
618ae115bc7Smrj #define	B16DATA_SEL	SEL_GDT(GDT_B16DATA, SEL_KPL)
619ae115bc7Smrj 
620ae115bc7Smrj /*
621ae115bc7Smrj  * Temporary %gs descriptor used by kmdb with -d option. Only lives
622ae115bc7Smrj  * in boot's GDT and is not copied into kernel's GDT from boot.
623ae115bc7Smrj  */
624ae115bc7Smrj #define	KMDBGS_SEL	SEL_GDT(GDT_BGSTMP, SEL_KPL)
625ae115bc7Smrj 
626ae115bc7Smrj /*
627ae115bc7Smrj  * Selector used for kdi_idt when kmdb has taken over the IDT.
628ae115bc7Smrj  */
629ae115bc7Smrj #if defined(__amd64)
630ae115bc7Smrj #define	KMDBCODE_SEL	B64CODE_SEL
6317c478bd9Sstevel@tonic-gate #else
632ae115bc7Smrj #define	KMDBCODE_SEL	B32CODE_SEL
6337c478bd9Sstevel@tonic-gate #endif
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate /*
6367c478bd9Sstevel@tonic-gate  * Entries in default Local Descriptor Table (LDT) for every process.
6377c478bd9Sstevel@tonic-gate  */
6387c478bd9Sstevel@tonic-gate #define	LDT_SYSCALL	0	/* call gate for libc.a (obsolete) */
6397c478bd9Sstevel@tonic-gate #define	LDT_SIGCALL	1	/* EOL me, call gate for static sigreturn */
6407c478bd9Sstevel@tonic-gate #define	LDT_RESVD1	2	/* old user %cs */
6417c478bd9Sstevel@tonic-gate #define	LDT_RESVD2	3	/* old user %ds */
6427c478bd9Sstevel@tonic-gate #define	LDT_ALTSYSCALL	4	/* alternate call gate for system calls */
6437c478bd9Sstevel@tonic-gate #define	LDT_ALTSIGCALL	5	/* EOL me, alternate call gate for sigreturn */
6447c478bd9Sstevel@tonic-gate #define	LDT_UDBASE	6	/* user descriptor base index */
645843e1988Sjohnlev #define	MINNLDT		512	/* Current min solaris ldt size (1 4K page) */
646843e1988Sjohnlev #define	MAXNLDT		8192	/* max solaris ldt size (16 4K pages) */
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate #ifndef	_ASM
6497c478bd9Sstevel@tonic-gate 
650*0cfdb603Sjosephb extern	gate_desc_t	*idt0;
6517c478bd9Sstevel@tonic-gate extern	desctbr_t	idt0_default_reg;
652ae115bc7Smrj extern	user_desc_t	*gdt0;
6530baeff3dSrab 
6547c478bd9Sstevel@tonic-gate extern user_desc_t	zero_udesc;
655843e1988Sjohnlev extern user_desc_t	null_udesc;
656843e1988Sjohnlev extern system_desc_t	null_sdesc;
6570baeff3dSrab 
6587c478bd9Sstevel@tonic-gate #if defined(__amd64)
6597c478bd9Sstevel@tonic-gate extern user_desc_t	zero_u32desc;
6607c478bd9Sstevel@tonic-gate #endif
661843e1988Sjohnlev #if defined(__amd64)
662843e1988Sjohnlev extern user_desc_t	ucs_on;
663843e1988Sjohnlev extern user_desc_t	ucs_off;
664843e1988Sjohnlev extern user_desc_t	ucs32_on;
665843e1988Sjohnlev extern user_desc_t	ucs32_off;
666843e1988Sjohnlev #endif  /* __amd64 */
6677c478bd9Sstevel@tonic-gate 
668*0cfdb603Sjosephb extern struct tss *ktss0;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate #if defined(__i386)
671*0cfdb603Sjosephb extern struct tss *dftss0;
6727c478bd9Sstevel@tonic-gate #endif	/* __i386 */
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate extern void div0trap(), dbgtrap(), nmiint(), brktrap(), ovflotrap();
675843e1988Sjohnlev extern void boundstrap(), invoptrap(), ndptrap();
676843e1988Sjohnlev #if !defined(__xpv)
677843e1988Sjohnlev extern void syserrtrap();
678843e1988Sjohnlev #endif
6797c478bd9Sstevel@tonic-gate extern void invaltrap(), invtsstrap(), segnptrap(), stktrap();
6807c478bd9Sstevel@tonic-gate extern void gptrap(), pftrap(), ndperr();
6817c478bd9Sstevel@tonic-gate extern void overrun(), resvtrap();
6827c478bd9Sstevel@tonic-gate extern void _start(), cmnint();
6837c478bd9Sstevel@tonic-gate extern void achktrap(), mcetrap();
6847c478bd9Sstevel@tonic-gate extern void xmtrap();
6857c478bd9Sstevel@tonic-gate extern void fasttrap();
6869acbbeafSnn35248 extern void sys_int80();
6879acbbeafSnn35248 extern void brand_sys_int80();
688f498645aSahl extern void dtrace_ret();
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate #if !defined(__amd64)
6917c478bd9Sstevel@tonic-gate extern void pentium_pftrap();
6927c478bd9Sstevel@tonic-gate #endif
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate #endif /* _ASM */
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate #endif
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate #endif /* _SYS_SEGMENTS_H */
701