10Sstevel@tonic-gate /* 2*1217Srab * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 30Sstevel@tonic-gate * Use is subject to license terms. 40Sstevel@tonic-gate */ 50Sstevel@tonic-gate 60Sstevel@tonic-gate #ifndef _SYS_SEGMENTS_H 70Sstevel@tonic-gate #define _SYS_SEGMENTS_H 80Sstevel@tonic-gate 90Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 100Sstevel@tonic-gate 110Sstevel@tonic-gate #ifdef __cplusplus 120Sstevel@tonic-gate extern "C" { 130Sstevel@tonic-gate #endif 140Sstevel@tonic-gate 150Sstevel@tonic-gate /* 160Sstevel@tonic-gate * Copyright (c) 1989, 1990 William F. Jolitz 170Sstevel@tonic-gate * Copyright (c) 1990 The Regents of the University of California. 180Sstevel@tonic-gate * All rights reserved. 190Sstevel@tonic-gate * 200Sstevel@tonic-gate * This code is derived from software contributed to Berkeley by 210Sstevel@tonic-gate * William Jolitz. 220Sstevel@tonic-gate * 230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 240Sstevel@tonic-gate * modification, are permitted provided that the following conditions 250Sstevel@tonic-gate * are met: 260Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 320Sstevel@tonic-gate * must display the following acknowledgement: 330Sstevel@tonic-gate * This product includes software developed by the University of 340Sstevel@tonic-gate * California, Berkeley and its contributors. 350Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors 360Sstevel@tonic-gate * may be used to endorse or promote products derived from this software 370Sstevel@tonic-gate * without specific prior written permission. 380Sstevel@tonic-gate * 390Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 400Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 410Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 420Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 430Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 440Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 450Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 460Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 470Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 480Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 490Sstevel@tonic-gate * SUCH DAMAGE. 500Sstevel@tonic-gate * 510Sstevel@tonic-gate * from: @(#)segments.h 7.1 (Berkeley) 5/9/91 520Sstevel@tonic-gate * $FreeBSD: src/sys/i386/include/segments.h,v 1.34 2003/09/10 01:07:04 530Sstevel@tonic-gate * jhb Exp $ 540Sstevel@tonic-gate * 550Sstevel@tonic-gate * 386 Segmentation Data Structures and definitions 560Sstevel@tonic-gate * William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate 590Sstevel@tonic-gate #include <sys/tss.h> 600Sstevel@tonic-gate 610Sstevel@tonic-gate /* 620Sstevel@tonic-gate * Selector register format 630Sstevel@tonic-gate * CS, DS, ES, FS, GS, SS 640Sstevel@tonic-gate * 650Sstevel@tonic-gate * 15 3 2 1 0 660Sstevel@tonic-gate * +---------------------+---+----+ 670Sstevel@tonic-gate * | SI |TI |RPL | 680Sstevel@tonic-gate * +---------------------+---+----+ 690Sstevel@tonic-gate * 700Sstevel@tonic-gate * SI = selector index 710Sstevel@tonic-gate * TI = table indicator (0 = GDT, 1 = LDT) 720Sstevel@tonic-gate * RPL = requestor privilege level 730Sstevel@tonic-gate */ 740Sstevel@tonic-gate #if !defined(_ASM) || defined(__GNUC_AS__) 750Sstevel@tonic-gate #define IDXTOSEL(s) ((s) << 3) /* index to selector */ 760Sstevel@tonic-gate #define SEL_GDT(s, r) (IDXTOSEL(s) | r) /* global sel */ 770Sstevel@tonic-gate #else 780Sstevel@tonic-gate #define IDXTOSEL(s) [s << 3] 790Sstevel@tonic-gate #define SEL_GDT(s, r) [IDXTOSEL(s) | r] 800Sstevel@tonic-gate #endif 810Sstevel@tonic-gate 820Sstevel@tonic-gate #define SELTOIDX(s) ((s) >> 3) /* selector to index */ 830Sstevel@tonic-gate #define SEL_KPL 0 /* kernel priority level */ 840Sstevel@tonic-gate #define SEL_UPL 3 /* user priority level */ 850Sstevel@tonic-gate #define SEL_TI_LDT 4 /* local descriptor table */ 860Sstevel@tonic-gate #define SEL_LDT(s) (IDXTOSEL(s) | SEL_TI_LDT | SEL_UPL) /* local sel */ 870Sstevel@tonic-gate #define CPL_MASK 3 /* RPL mask for selector */ 880Sstevel@tonic-gate #define SELISLDT(s) (((s) & SEL_TI_LDT) == SEL_TI_LDT) 890Sstevel@tonic-gate #define SELISUPL(s) (((s) & CPL_MASK) == SEL_UPL) 900Sstevel@tonic-gate 910Sstevel@tonic-gate #ifndef _ASM 920Sstevel@tonic-gate 930Sstevel@tonic-gate typedef uint16_t selector_t; /* selector reigster */ 940Sstevel@tonic-gate 950Sstevel@tonic-gate /* 960Sstevel@tonic-gate * Hardware descriptor table register format for GDT and IDT. 970Sstevel@tonic-gate */ 980Sstevel@tonic-gate #if defined(__amd64) 990Sstevel@tonic-gate 1000Sstevel@tonic-gate #pragma pack(2) 1010Sstevel@tonic-gate typedef struct { 1020Sstevel@tonic-gate uint16_t dtr_limit; /* table limit */ 1030Sstevel@tonic-gate uint64_t dtr_base; /* table base address */ 1040Sstevel@tonic-gate } desctbr_t; 1050Sstevel@tonic-gate #pragma pack() 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate #elif defined(__i386) 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate #pragma pack(2) 1100Sstevel@tonic-gate typedef struct { 1110Sstevel@tonic-gate uint16_t dtr_limit; /* table limit */ 1120Sstevel@tonic-gate uint32_t dtr_base; /* table base address */ 1130Sstevel@tonic-gate } desctbr_t; 1140Sstevel@tonic-gate #pragma pack() 1150Sstevel@tonic-gate 1160Sstevel@tonic-gate #endif /* __i386 */ 1170Sstevel@tonic-gate 1180Sstevel@tonic-gate /* 1190Sstevel@tonic-gate * Functions for loading and storing descriptor table 1200Sstevel@tonic-gate * registers. 1210Sstevel@tonic-gate */ 1220Sstevel@tonic-gate extern void rd_idtr(desctbr_t *); 1230Sstevel@tonic-gate extern void wr_idtr(desctbr_t *); 1240Sstevel@tonic-gate extern void rd_gdtr(desctbr_t *); 1250Sstevel@tonic-gate extern void wr_gdtr(desctbr_t *); 1260Sstevel@tonic-gate extern void wr_ldtr(selector_t); 1270Sstevel@tonic-gate extern selector_t rd_ldtr(void); 1280Sstevel@tonic-gate extern void wr_tsr(selector_t); 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate #if defined(__amd64) 1310Sstevel@tonic-gate extern void clr_ldt_sregs(void); 1320Sstevel@tonic-gate #endif 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate #if !defined(__amd64) 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate /* 1370Sstevel@tonic-gate * User segment descriptors (code and data). 1380Sstevel@tonic-gate * Legacy mode 64-bits wide. 1390Sstevel@tonic-gate */ 1400Sstevel@tonic-gate typedef struct user_desc { 1410Sstevel@tonic-gate uint32_t usd_lolimit:16; /* segment limit 15:0 */ 1420Sstevel@tonic-gate uint32_t usd_lobase:16; /* segment base 15:0 */ 1430Sstevel@tonic-gate uint32_t usd_midbase:8; /* segment base 23:16 */ 1440Sstevel@tonic-gate uint32_t usd_type:5; /* segment type, includes S bit */ 1450Sstevel@tonic-gate uint32_t usd_dpl:2; /* segment descriptor priority level */ 1460Sstevel@tonic-gate uint32_t usd_p:1; /* segment descriptor present */ 1470Sstevel@tonic-gate uint32_t usd_hilimit:4; /* segment limit 19:16 */ 1480Sstevel@tonic-gate uint32_t usd_avl:1; /* available to sw, but not used */ 1490Sstevel@tonic-gate uint32_t usd_reserved:1; /* unused, ignored */ 1500Sstevel@tonic-gate uint32_t usd_def32:1; /* default 32 vs 16 bit operand */ 1510Sstevel@tonic-gate uint32_t usd_gran:1; /* limit units (bytes vs pages) */ 1520Sstevel@tonic-gate uint32_t usd_hibase:8; /* segment base 31:24 */ 1530Sstevel@tonic-gate } user_desc_t; 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate #define USEGD_GETBASE(usd) ((usd)->usd_lobase | \ 1560Sstevel@tonic-gate (usd)->usd_midbase << 16 | \ 1570Sstevel@tonic-gate (usd)->usd_hibase << (16 + 8)) 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate #define USEGD_SETBASE(usd, b) ((usd)->usd_lobase = (b), \ 1600Sstevel@tonic-gate (usd)->usd_midbase = (b) >> 16, \ 1610Sstevel@tonic-gate (usd)->usd_hibase = (b) >> (16 + 8)) 1620Sstevel@tonic-gate 1630Sstevel@tonic-gate #define USEGD_GETLIMIT(usd) ((usd)->usd_lolimit | \ 1640Sstevel@tonic-gate (usd)->usd_hilimit << 16) 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate #define USEGD_SETLIMIT(usd, lim) ((usd)->usd_lolimit = lim, \ 1670Sstevel@tonic-gate (usd)->usd_hilimit = lim >> 16) 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate #define USD_TYPESHIFT 5 /* size of usd_type field */ 1700Sstevel@tonic-gate 1710Sstevel@tonic-gate #else /* __amd64 */ 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate /* 1740Sstevel@tonic-gate * User segment descriptors. 1750Sstevel@tonic-gate * Long mode 64-bits wide. 1760Sstevel@tonic-gate * 1770Sstevel@tonic-gate * In 32-bit compatibility mode (%cs:usd_long=0) all fields are interpreted 1780Sstevel@tonic-gate * as in legacy mode for both code and data. 1790Sstevel@tonic-gate * 1800Sstevel@tonic-gate * In 64-bit mode (%cs:usd_long=1) code segments only have the conforming 1810Sstevel@tonic-gate * bit in usd_type, usd_dpl, usd_p, usd_long and usd_def32=0. usd_def32 1820Sstevel@tonic-gate * must be zero in 64-bit mode. Setting it to 1 is reserved for future use. 1830Sstevel@tonic-gate * All other fields are loaded but ignored by hardware. 1840Sstevel@tonic-gate * 1850Sstevel@tonic-gate * 64-bit data segments only have usd_p. All other fields are loaded but 1860Sstevel@tonic-gate * ignored by hardware when in 64-bit mode. 1870Sstevel@tonic-gate */ 1880Sstevel@tonic-gate typedef struct user_desc { 1890Sstevel@tonic-gate uint64_t usd_lolimit:16; /* segment limit 15:0 */ 1900Sstevel@tonic-gate uint64_t usd_lobase:16; /* segment base 15:0 */ 1910Sstevel@tonic-gate uint64_t usd_midbase:8; /* segment base 23:16 */ 1920Sstevel@tonic-gate uint64_t usd_type:5; /* segment type, includes S bit */ 1930Sstevel@tonic-gate uint64_t usd_dpl:2; /* segment descriptor priority level */ 1940Sstevel@tonic-gate uint64_t usd_p:1; /* segment descriptor present */ 1950Sstevel@tonic-gate uint64_t usd_hilimit:4; /* segment limit 19:16 */ 1960Sstevel@tonic-gate uint64_t usd_avl:1; /* available to sw, but not used */ 1970Sstevel@tonic-gate uint64_t usd_long:1; /* long mode (%cs only) */ 1980Sstevel@tonic-gate uint64_t usd_def32:1; /* default 32 vs 16 bit operand */ 1990Sstevel@tonic-gate uint64_t usd_gran:1; /* limit units (bytes vs page) */ 2000Sstevel@tonic-gate uint64_t usd_hibase:8; /* segment base 31:24 */ 2010Sstevel@tonic-gate } user_desc_t; 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate #define USEGD_GETBASE(usd) ((usd)->usd_lobase | \ 2040Sstevel@tonic-gate (usd)->usd_midbase << 16 | \ 2050Sstevel@tonic-gate (usd)->usd_hibase << (16 + 8)) 2060Sstevel@tonic-gate 2070Sstevel@tonic-gate #define USEGD_SETBASE(usd, b) ((usd)->usd_lobase = (b), \ 2080Sstevel@tonic-gate (usd)->usd_midbase = (b) >> 16, \ 2090Sstevel@tonic-gate (usd)->usd_hibase = (b) >> (16 + 8)) 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate #define USEGD_GETLIMIT(usd) ((usd)->usd_lolimit | \ 2120Sstevel@tonic-gate (usd)->usd_hilimit << 16) 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate #define USEGD_SETLIMIT(usd, lim) ((usd)->usd_lolimit = lim, \ 2150Sstevel@tonic-gate (usd)->usd_hilimit = lim >> 16) 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate #define USD_TYPESHIFT 5 /* size of usd_type field */ 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate #endif /* __amd64 */ 2200Sstevel@tonic-gate 2210Sstevel@tonic-gate #if !defined(__amd64) 2220Sstevel@tonic-gate 2230Sstevel@tonic-gate /* 2240Sstevel@tonic-gate * System segment descriptors for LDT and TSS segments. 2250Sstevel@tonic-gate * Legacy mode 64-bits wide. 2260Sstevel@tonic-gate */ 2270Sstevel@tonic-gate typedef struct system_desc { 2280Sstevel@tonic-gate uint32_t ssd_lolimit:16; /* segment limit 15:0 */ 2290Sstevel@tonic-gate uint32_t ssd_lobase:16; /* segment base 15:0 */ 2300Sstevel@tonic-gate uint32_t ssd_midbase:8; /* segment base 23:16 */ 2310Sstevel@tonic-gate uint32_t ssd_type:4; /* segment type */ 2320Sstevel@tonic-gate uint32_t ssd_zero:1; /* must be zero */ 2330Sstevel@tonic-gate uint32_t ssd_dpl:2; /* segment descriptor priority level */ 2340Sstevel@tonic-gate uint32_t ssd_p:1; /* segment descriptor present */ 2350Sstevel@tonic-gate uint32_t ssd_hilimit:4; /* segment limit 19:16 */ 2360Sstevel@tonic-gate uint32_t ssd_avl:1; /* available to sw, but not used */ 2370Sstevel@tonic-gate uint32_t ssd_reserved:2; /* unused, ignored */ 2380Sstevel@tonic-gate uint32_t ssd_gran:1; /* limit unit (bytes vs pages) */ 2390Sstevel@tonic-gate uint32_t ssd_hibase:8; /* segment base 31:24 */ 2400Sstevel@tonic-gate } system_desc_t; 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate #else /* __amd64 */ 2430Sstevel@tonic-gate 2440Sstevel@tonic-gate /* 2450Sstevel@tonic-gate * System segment descriptors for LDT and TSS segments. 2460Sstevel@tonic-gate * Long mode 128-bits wide. 2470Sstevel@tonic-gate * 2480Sstevel@tonic-gate * 32-bit LDT and TSS descriptor types are redefined to 64-bit equivalents. 2490Sstevel@tonic-gate * All other legacy types are reserved and illegal. 2500Sstevel@tonic-gate */ 2510Sstevel@tonic-gate typedef struct system_desc { 2520Sstevel@tonic-gate uint64_t ssd_lolimit:16; /* segment limit 15:0 */ 2530Sstevel@tonic-gate uint64_t ssd_lobase:16; /* segment base 15:0 */ 2540Sstevel@tonic-gate uint64_t ssd_midbase:8; /* segment base 23:16 */ 2550Sstevel@tonic-gate uint64_t ssd_type:4; /* segment type */ 2560Sstevel@tonic-gate uint64_t ssd_zero1:1; /* must be zero */ 2570Sstevel@tonic-gate uint64_t ssd_dpl:2; /* segment descriptor priority level */ 2580Sstevel@tonic-gate uint64_t ssd_p:1; /* segment descriptor present */ 2590Sstevel@tonic-gate uint64_t ssd_hilimit:4; /* segment limit 19:16 */ 2600Sstevel@tonic-gate uint64_t ssd_avl:1; /* available to sw, but not used */ 2610Sstevel@tonic-gate uint64_t ssd_resv1:2; /* unused, ignored */ 2620Sstevel@tonic-gate uint64_t ssd_gran:1; /* limit unit (bytes vs pages) */ 2630Sstevel@tonic-gate uint64_t ssd_hibase:8; /* segment base 31:24 */ 2640Sstevel@tonic-gate uint64_t ssd_hi64base:32; /* segment base 63:32 */ 2650Sstevel@tonic-gate uint64_t ssd_resv2:8; /* unused, ignored */ 2660Sstevel@tonic-gate uint64_t ssd_zero2:5; /* must be zero */ 2670Sstevel@tonic-gate uint64_t ssd_resv3:19; /* unused, ignored */ 2680Sstevel@tonic-gate } system_desc_t; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate #endif /* __amd64 */ 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate #define SYSSEGD_SETLIMIT(ssd, lim) ((ssd)->ssd_lolimit = lim, \ 2730Sstevel@tonic-gate (ssd)->ssd_hilimit = lim >> 16) 2740Sstevel@tonic-gate #if !defined(__amd64) 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate /* 2770Sstevel@tonic-gate * System gate segment descriptors for interrupt, trap, call and task gates. 2780Sstevel@tonic-gate * Legacy mode 64-bits wide. 2790Sstevel@tonic-gate */ 2800Sstevel@tonic-gate typedef struct gate_desc { 2810Sstevel@tonic-gate uint32_t sgd_looffset:16; /* segment code offset 15:0 */ 2820Sstevel@tonic-gate uint32_t sgd_selector:16; /* target code or task selector */ 2830Sstevel@tonic-gate uint32_t sgd_stkcpy:5; /* number of stack wds to cpy */ 2840Sstevel@tonic-gate uint32_t sgd_resv:3; /* unused, ignored */ 2850Sstevel@tonic-gate uint32_t sgd_type:5; /* segment type, includes S bit */ 2860Sstevel@tonic-gate uint32_t sgd_dpl:2; /* segment descriptor priority level */ 2870Sstevel@tonic-gate uint32_t sgd_p:1; /* segment descriptor present */ 2880Sstevel@tonic-gate uint32_t sgd_hioffset:16; /* code seg off 31:16 */ 2890Sstevel@tonic-gate } gate_desc_t; 2900Sstevel@tonic-gate 2910Sstevel@tonic-gate #define GATESEG_GETOFFSET(sgd) ((sgd)->sgd_looffset | \ 2920Sstevel@tonic-gate (sgd)->sgd_hioffset << 16) 2930Sstevel@tonic-gate 2940Sstevel@tonic-gate #else /* __amd64 */ 2950Sstevel@tonic-gate 2960Sstevel@tonic-gate /* 2970Sstevel@tonic-gate * System segment descriptors for interrupt, trap and call gates. 2980Sstevel@tonic-gate * Long mode 128-bits wide. 2990Sstevel@tonic-gate * 3000Sstevel@tonic-gate * 32-bit interrupt, trap and call gate types are redefined to 64-bit 3010Sstevel@tonic-gate * equivalents. Task gates along with all other legacy types are reserved 3020Sstevel@tonic-gate * and illegal. 3030Sstevel@tonic-gate */ 3040Sstevel@tonic-gate typedef struct gate_desc { 3050Sstevel@tonic-gate uint64_t sgd_looffset:16; /* segment code offset 15:0 */ 3060Sstevel@tonic-gate uint64_t sgd_selector:16; /* target code or task selector */ 3070Sstevel@tonic-gate uint64_t sgd_ist:3; /* IST table index */ 3080Sstevel@tonic-gate uint64_t sgd_resv1:5; /* unused, ignored */ 3090Sstevel@tonic-gate uint64_t sgd_type:5; /* segment type, includes S bit */ 3100Sstevel@tonic-gate uint64_t sgd_dpl:2; /* segment descriptor priority level */ 3110Sstevel@tonic-gate uint64_t sgd_p:1; /* segment descriptor present */ 3120Sstevel@tonic-gate uint64_t sgd_hioffset:16; /* segment code offset 31:16 */ 3130Sstevel@tonic-gate uint64_t sgd_hi64offset:32; /* segment code offset 63:32 */ 3140Sstevel@tonic-gate uint64_t sgd_resv2:8; /* unused, ignored */ 3150Sstevel@tonic-gate uint64_t sgd_zero:5; /* call gate only: must be zero */ 3160Sstevel@tonic-gate uint64_t sgd_resv3:19; /* unused, ignored */ 3170Sstevel@tonic-gate } gate_desc_t; 3180Sstevel@tonic-gate 3190Sstevel@tonic-gate #define GATESEG_GETOFFSET(sgd) ((sgd)->sgd_looffset | \ 3200Sstevel@tonic-gate (sgd)->sgd_hioffset << 16 | \ 3210Sstevel@tonic-gate (sgd)->sgd_hi64offset << 32) 3220Sstevel@tonic-gate 3230Sstevel@tonic-gate #endif /* __amd64 */ 3240Sstevel@tonic-gate 3250Sstevel@tonic-gate /* 3260Sstevel@tonic-gate * functions for initializing and updating segment descriptors. 3270Sstevel@tonic-gate */ 3280Sstevel@tonic-gate #if defined(__amd64) 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate extern void set_usegd(user_desc_t *, uint_t, void *, size_t, uint_t, uint_t, 3310Sstevel@tonic-gate uint_t, uint_t); 3320Sstevel@tonic-gate extern void set_gatesegd(gate_desc_t *, void (*)(void), selector_t, uint_t, 3330Sstevel@tonic-gate uint_t, uint_t); 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate #elif defined(__i386) 3360Sstevel@tonic-gate 3370Sstevel@tonic-gate extern void set_usegd(user_desc_t *, void *, size_t, uint_t, uint_t, 3380Sstevel@tonic-gate uint_t, uint_t); 3390Sstevel@tonic-gate extern void set_gatesegd(gate_desc_t *, void (*)(void), selector_t, 3400Sstevel@tonic-gate uint_t, uint_t, uint_t); 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate #endif /* __i386 */ 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate void set_syssegd(system_desc_t *, void *, size_t, uint_t, uint_t); 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate #endif /* _ASM */ 3470Sstevel@tonic-gate 3480Sstevel@tonic-gate /* 3490Sstevel@tonic-gate * Common segment parameter defintions for granularity, default 3500Sstevel@tonic-gate * operand size and operaton mode. 3510Sstevel@tonic-gate */ 3520Sstevel@tonic-gate #define SDP_BYTES 0 /* segment limit scaled to bytes */ 3530Sstevel@tonic-gate #define SDP_PAGES 1 /* segment limit scaled to pages */ 3540Sstevel@tonic-gate #define SDP_OP32 1 /* code and data default operand = 32 bits */ 3550Sstevel@tonic-gate #define SDP_LONG 1 /* long mode code segment (64 bits) */ 3560Sstevel@tonic-gate #define SDP_SHORT 0 /* compat/legacy code segment (32 bits) */ 3570Sstevel@tonic-gate /* 3580Sstevel@tonic-gate * System segments and gate types. 3590Sstevel@tonic-gate * 3600Sstevel@tonic-gate * In long mode i386 32-bit ldt, tss, call, interrupt and trap gate 3610Sstevel@tonic-gate * types are redefined into 64-bit equivalents. 3620Sstevel@tonic-gate */ 3630Sstevel@tonic-gate #define SDT_SYSNULL 0 /* system null */ 3640Sstevel@tonic-gate #define SDT_SYS286TSS 1 /* system 286 TSS available */ 3650Sstevel@tonic-gate #define SDT_SYSLDT 2 /* system local descriptor table */ 3660Sstevel@tonic-gate #define SDT_SYS286BSY 3 /* system 286 TSS busy */ 3670Sstevel@tonic-gate #define SDT_SYS286CGT 4 /* system 286 call gate */ 3680Sstevel@tonic-gate #define SDT_SYSTASKGT 5 /* system task gate */ 3690Sstevel@tonic-gate #define SDT_SYS286IGT 6 /* system 286 interrupt gate */ 3700Sstevel@tonic-gate #define SDT_SYS286TGT 7 /* system 286 trap gate */ 3710Sstevel@tonic-gate #define SDT_SYSNULL2 8 /* system null again */ 3720Sstevel@tonic-gate #define SDT_SYSTSS 9 /* system TSS available */ 3730Sstevel@tonic-gate #define SDT_SYSNULL3 10 /* system null again */ 3740Sstevel@tonic-gate #define SDT_SYSTSSBSY 11 /* system TSS busy */ 3750Sstevel@tonic-gate #define SDT_SYSCGT 12 /* system call gate */ 3760Sstevel@tonic-gate #define SDT_SYSNULL4 13 /* system null again */ 3770Sstevel@tonic-gate #define SDT_SYSIGT 14 /* system interrupt gate */ 3780Sstevel@tonic-gate #define SDT_SYSTGT 15 /* system trap gate */ 3790Sstevel@tonic-gate 3800Sstevel@tonic-gate /* 3810Sstevel@tonic-gate * Memory segment types. 3820Sstevel@tonic-gate * 3830Sstevel@tonic-gate * While in long mode expand-down, writable and accessed type field 3840Sstevel@tonic-gate * attributes are ignored. Only the conforming bit is loaded by hardware 3850Sstevel@tonic-gate * for long mode code segment descriptors. 3860Sstevel@tonic-gate */ 3870Sstevel@tonic-gate #define SDT_MEMRO 16 /* read only */ 3880Sstevel@tonic-gate #define SDT_MEMROA 17 /* read only accessed */ 3890Sstevel@tonic-gate #define SDT_MEMRW 18 /* read write */ 3900Sstevel@tonic-gate #define SDT_MEMRWA 19 /* read write accessed */ 3910Sstevel@tonic-gate #define SDT_MEMROD 20 /* read only expand dwn limit */ 3920Sstevel@tonic-gate #define SDT_MEMRODA 21 /* read only expand dwn limit accessed */ 3930Sstevel@tonic-gate #define SDT_MEMRWD 22 /* read write expand dwn limit */ 3940Sstevel@tonic-gate #define SDT_MEMRWDA 23 /* read write expand dwn limit accessed */ 3950Sstevel@tonic-gate #define SDT_MEME 24 /* execute only */ 3960Sstevel@tonic-gate #define SDT_MEMEA 25 /* execute only accessed */ 3970Sstevel@tonic-gate #define SDT_MEMER 26 /* execute read */ 3980Sstevel@tonic-gate #define SDT_MEMERA 27 /* execute read accessed */ 3990Sstevel@tonic-gate #define SDT_MEMEC 28 /* execute only conforming */ 4000Sstevel@tonic-gate #define SDT_MEMEAC 29 /* execute only accessed conforming */ 4010Sstevel@tonic-gate #define SDT_MEMERC 30 /* execute read conforming */ 4020Sstevel@tonic-gate #define SDT_MEMERAC 31 /* execute read accessed conforming */ 4030Sstevel@tonic-gate 4040Sstevel@tonic-gate /* 4050Sstevel@tonic-gate * Entries in the Interrupt Descriptor Table (IDT) 4060Sstevel@tonic-gate */ 4070Sstevel@tonic-gate #define IDT_DE 0 /* #DE: Divide Error */ 4080Sstevel@tonic-gate #define IDT_DB 1 /* #DB: Debug */ 4090Sstevel@tonic-gate #define IDT_NMI 2 /* Nonmaskable External Interrupt */ 4100Sstevel@tonic-gate #define IDT_BP 3 /* #BP: Breakpoint */ 4110Sstevel@tonic-gate #define IDT_OF 4 /* #OF: Overflow */ 4120Sstevel@tonic-gate #define IDT_BR 5 /* #BR: Bound Range Exceeded */ 4130Sstevel@tonic-gate #define IDT_UD 6 /* #UD: Undefined/Invalid Opcode */ 4140Sstevel@tonic-gate #define IDT_NM 7 /* #NM: No Math Coprocessor */ 4150Sstevel@tonic-gate #define IDT_DF 8 /* #DF: Double Fault */ 4160Sstevel@tonic-gate #define IDT_FPUGP 9 /* Coprocessor Segment Overrun */ 4170Sstevel@tonic-gate #define IDT_TS 10 /* #TS: Invalid TSS */ 4180Sstevel@tonic-gate #define IDT_NP 11 /* #NP: Segment Not Present */ 4190Sstevel@tonic-gate #define IDT_SS 12 /* #SS: Stack Segment Fault */ 4200Sstevel@tonic-gate #define IDT_GP 13 /* #GP: General Protection Fault */ 4210Sstevel@tonic-gate #define IDT_PF 14 /* #PF: Page Fault */ 4220Sstevel@tonic-gate #define IDT_MF 16 /* #MF: FPU Floating-Point Error */ 4230Sstevel@tonic-gate #define IDT_AC 17 /* #AC: Alignment Check */ 4240Sstevel@tonic-gate #define IDT_MC 18 /* #MC: Machine Check */ 4250Sstevel@tonic-gate #define IDT_XF 19 /* #XF: SIMD Floating-Point Exception */ 4260Sstevel@tonic-gate #define NIDT 256 /* size in entries of IDT */ 4270Sstevel@tonic-gate 4280Sstevel@tonic-gate /* 4290Sstevel@tonic-gate * Entries in the Global Descriptor Table (GDT) 4300Sstevel@tonic-gate * 4310Sstevel@tonic-gate * We make sure to space the system descriptors (LDT's, TSS') 4320Sstevel@tonic-gate * such that they are double gdt slot aligned. This is because 4330Sstevel@tonic-gate * in long mode system segment decriptors expand to 128 bits. 4340Sstevel@tonic-gate * 4350Sstevel@tonic-gate * GDT_LWPFS and GDT_LWPGS must be the same for both 32 and 64-bit 4360Sstevel@tonic-gate * kernels. See setup_context in libc. 4370Sstevel@tonic-gate */ 4380Sstevel@tonic-gate #if defined(__amd64) 4390Sstevel@tonic-gate 4400Sstevel@tonic-gate #define GDT_NULL 0 /* null */ 4410Sstevel@tonic-gate #define GDT_B32DATA 1 /* copied from boot */ 4420Sstevel@tonic-gate #define GDT_B32CODE 2 /* copied from boot */ 4430Sstevel@tonic-gate #define GDT_B64DATA 3 /* copied from boot */ 4440Sstevel@tonic-gate #define GDT_B64CODE 4 /* copied from boot */ 4450Sstevel@tonic-gate #define GDT_KCODE 5 /* kernel code seg %cs */ 4460Sstevel@tonic-gate #define GDT_KDATA 6 /* kernel data seg %ds */ 4470Sstevel@tonic-gate #define GDT_U32CODE 7 /* 32-bit process on 64-bit kernel %cs */ 4480Sstevel@tonic-gate #define GDT_UDATA 8 /* user data seg %ds (32 and 64 bit) */ 4490Sstevel@tonic-gate #define GDT_UCODE 9 /* native user code seg %cs */ 4500Sstevel@tonic-gate #define GDT_LDT 10 /* LDT for current process */ 4510Sstevel@tonic-gate #define GDT_KTSS 12 /* kernel tss */ 4520Sstevel@tonic-gate #define GDT_FS GDT_NULL /* kernel %fs segment selector */ 4530Sstevel@tonic-gate #define GDT_GS GDT_NULL /* kernel %gs segment selector */ 4540Sstevel@tonic-gate #define GDT_LWPFS 55 /* lwp private %fs segment selector */ 4550Sstevel@tonic-gate #define GDT_LWPGS 56 /* lwp private %gs segment selector */ 4560Sstevel@tonic-gate #define NGDT 58 /* number of entries in GDT */ 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate /* 4590Sstevel@tonic-gate * This selector is only used in the temporary GDT used to bring additional 4600Sstevel@tonic-gate * CPUs from 16-bit real mode into long mode in real_mode_start(). 4610Sstevel@tonic-gate */ 4620Sstevel@tonic-gate #define TEMPGDT_KCODE64 1 /* 64-bit code selector */ 4630Sstevel@tonic-gate 4640Sstevel@tonic-gate #elif defined(__i386) 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate #define GDT_NULL 0 /* null */ 4670Sstevel@tonic-gate #define GDT_BOOTFLAT 1 /* copied from boot */ 4680Sstevel@tonic-gate #define GDT_BOOTCODE 2 /* copied from boot */ 4690Sstevel@tonic-gate #define GDT_BOOTCODE16 3 /* copied from boot */ 4700Sstevel@tonic-gate #define GDT_BOOTDATA 4 /* copied from boot */ 4710Sstevel@tonic-gate #define GDT_LDT 40 /* LDT for current process */ 4720Sstevel@tonic-gate #define GDT_KTSS 42 /* kernel tss */ 4730Sstevel@tonic-gate #define GDT_KCODE 43 /* kernel code seg %cs */ 4740Sstevel@tonic-gate #define GDT_KDATA 44 /* kernel data seg %ds */ 4750Sstevel@tonic-gate #define GDT_UCODE 45 /* native user code seg %cs */ 4760Sstevel@tonic-gate #define GDT_UDATA 46 /* user data seg %ds (32 and 64 bit) */ 4770Sstevel@tonic-gate #define GDT_DBFLT 47 /* double fault #DF selector */ 4780Sstevel@tonic-gate #define GDT_FS 53 /* kernel %fs segment selector */ 4790Sstevel@tonic-gate #define GDT_GS 54 /* kernel %gs segment selector */ 4800Sstevel@tonic-gate #define GDT_LWPFS 55 /* lwp private %fs segment selector */ 4810Sstevel@tonic-gate #define GDT_LWPGS 56 /* lwp private %gs segment selector */ 4820Sstevel@tonic-gate #define NGDT 90 /* number of entries in GDT */ 4830Sstevel@tonic-gate 4840Sstevel@tonic-gate #endif /* __i386 */ 4850Sstevel@tonic-gate 4860Sstevel@tonic-gate /* 4870Sstevel@tonic-gate * Convenient selector definitions. 4880Sstevel@tonic-gate */ 4890Sstevel@tonic-gate #define KCS_SEL SEL_GDT(GDT_KCODE, SEL_KPL) 4900Sstevel@tonic-gate #define KDS_SEL SEL_GDT(GDT_KDATA, SEL_KPL) 4910Sstevel@tonic-gate #define UCS_SEL SEL_GDT(GDT_UCODE, SEL_UPL) 4920Sstevel@tonic-gate #if defined(__amd64) 4930Sstevel@tonic-gate #define TEMP_CS64_SEL SEL_GDT(TEMPGDT_KCODE64, SEL_KPL) 4940Sstevel@tonic-gate #define U32CS_SEL SEL_GDT(GDT_U32CODE, SEL_UPL) 4950Sstevel@tonic-gate #endif /* __amd64 */ 4960Sstevel@tonic-gate #define UDS_SEL SEL_GDT(GDT_UDATA, SEL_UPL) 4970Sstevel@tonic-gate #define ULDT_SEL SEL_GDT(GDT_LDT, SEL_KPL) 4980Sstevel@tonic-gate #define KTSS_SEL SEL_GDT(GDT_KTSS, SEL_KPL) 4990Sstevel@tonic-gate #define DFTSS_SEL SEL_GDT(GDT_DBFLT, SEL_KPL) 5000Sstevel@tonic-gate #define KFS_SEL SEL_GDT(GDT_NULL, SEL_KPL) 5010Sstevel@tonic-gate #define KGS_SEL SEL_GDT(GDT_GS, SEL_KPL) 5020Sstevel@tonic-gate #define LWPFS_SEL SEL_GDT(GDT_LWPFS, SEL_UPL) 5030Sstevel@tonic-gate #define LWPGS_SEL SEL_GDT(GDT_LWPGS, SEL_UPL) 5040Sstevel@tonic-gate #if defined(__amd64) 5050Sstevel@tonic-gate #define B64CODE_SEL SEL_GDT(GDT_B64CODE, SEL_KPL) 5060Sstevel@tonic-gate #else 5070Sstevel@tonic-gate #define BOOTCODE_SEL SEL_GDT(GDT_BOOTCODE, SEL_KPL) 5080Sstevel@tonic-gate #define BOOTFLAT_SEL SEL_GDT(GDT_BOOTFLAT, SEL_KPL) 5090Sstevel@tonic-gate #endif 5100Sstevel@tonic-gate 5110Sstevel@tonic-gate /* 5120Sstevel@tonic-gate * Entries in default Local Descriptor Table (LDT) for every process. 5130Sstevel@tonic-gate */ 5140Sstevel@tonic-gate #define LDT_SYSCALL 0 /* call gate for libc.a (obsolete) */ 5150Sstevel@tonic-gate #define LDT_SIGCALL 1 /* EOL me, call gate for static sigreturn */ 5160Sstevel@tonic-gate #define LDT_RESVD1 2 /* old user %cs */ 5170Sstevel@tonic-gate #define LDT_RESVD2 3 /* old user %ds */ 5180Sstevel@tonic-gate #define LDT_ALTSYSCALL 4 /* alternate call gate for system calls */ 5190Sstevel@tonic-gate #define LDT_ALTSIGCALL 5 /* EOL me, alternate call gate for sigreturn */ 5200Sstevel@tonic-gate #define LDT_UDBASE 6 /* user descriptor base index */ 5210Sstevel@tonic-gate #define MINNLDT 64 /* Current min solaris ldt size */ 5220Sstevel@tonic-gate #define MAXNLDT 8192 /* max solaris ldt size */ 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate #ifndef _ASM 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate #pragma align 16(idt0) 5270Sstevel@tonic-gate extern gate_desc_t idt0[NIDT]; 5280Sstevel@tonic-gate extern desctbr_t idt0_default_reg; 5290Sstevel@tonic-gate #pragma align 16(gdt0) 5300Sstevel@tonic-gate extern user_desc_t gdt0[NGDT]; 531*1217Srab 5320Sstevel@tonic-gate extern user_desc_t zero_udesc; 533*1217Srab extern system_desc_t zero_sdesc; 534*1217Srab 5350Sstevel@tonic-gate #if defined(__amd64) 5360Sstevel@tonic-gate extern user_desc_t zero_u32desc; 5370Sstevel@tonic-gate #endif 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate #pragma align 16(ktss0) 5400Sstevel@tonic-gate extern struct tss ktss0; 5410Sstevel@tonic-gate 5420Sstevel@tonic-gate #if defined(__i386) 5430Sstevel@tonic-gate extern struct tss dftss0; 5440Sstevel@tonic-gate #endif /* __i386 */ 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate extern void div0trap(), dbgtrap(), nmiint(), brktrap(), ovflotrap(); 5470Sstevel@tonic-gate extern void boundstrap(), invoptrap(), ndptrap(), syserrtrap(); 5480Sstevel@tonic-gate extern void invaltrap(), invtsstrap(), segnptrap(), stktrap(); 5490Sstevel@tonic-gate extern void gptrap(), pftrap(), ndperr(); 5500Sstevel@tonic-gate extern void overrun(), resvtrap(); 5510Sstevel@tonic-gate extern void _start(), cmnint(); 5520Sstevel@tonic-gate extern void achktrap(), mcetrap(); 5530Sstevel@tonic-gate extern void xmtrap(); 5540Sstevel@tonic-gate extern void fasttrap(); 5550Sstevel@tonic-gate extern void dtrace_fasttrap(), dtrace_ret(); 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate #if !defined(__amd64) 5580Sstevel@tonic-gate extern void pentium_pftrap(); 5590Sstevel@tonic-gate #endif 5600Sstevel@tonic-gate 5610Sstevel@tonic-gate #endif /* _ASM */ 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate #ifdef __cplusplus 5640Sstevel@tonic-gate } 5650Sstevel@tonic-gate #endif 5660Sstevel@tonic-gate 5670Sstevel@tonic-gate #endif /* _SYS_SEGMENTS_H */ 568