segments.h revision 121754
14Srgrimes/*- 24Srgrimes * Copyright (c) 1989, 1990 William F. Jolitz 34Srgrimes * Copyright (c) 1990 The Regents of the University of California. 44Srgrimes * All rights reserved. 54Srgrimes * 64Srgrimes * This code is derived from software contributed to Berkeley by 74Srgrimes * William Jolitz. 84Srgrimes * 94Srgrimes * Redistribution and use in source and binary forms, with or without 104Srgrimes * modification, are permitted provided that the following conditions 114Srgrimes * are met: 124Srgrimes * 1. Redistributions of source code must retain the above copyright 134Srgrimes * notice, this list of conditions and the following disclaimer. 144Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 154Srgrimes * notice, this list of conditions and the following disclaimer in the 164Srgrimes * documentation and/or other materials provided with the distribution. 174Srgrimes * 3. All advertising materials mentioning features or use of this software 184Srgrimes * must display the following acknowledgement: 194Srgrimes * This product includes software developed by the University of 204Srgrimes * California, Berkeley and its contributors. 214Srgrimes * 4. Neither the name of the University nor the names of its contributors 224Srgrimes * may be used to endorse or promote products derived from this software 234Srgrimes * without specific prior written permission. 244Srgrimes * 254Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 264Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 274Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 284Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 294Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 304Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 314Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 324Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 334Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 344Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 354Srgrimes * SUCH DAMAGE. 364Srgrimes * 37621Srgrimes * from: @(#)segments.h 7.1 (Berkeley) 5/9/91 3850477Speter * $FreeBSD: head/sys/i386/include/segments.h 121754 2003-10-30 21:42:17Z jhb $ 394Srgrimes */ 404Srgrimes 41719Swollman#ifndef _MACHINE_SEGMENTS_H_ 424501Sbde#define _MACHINE_SEGMENTS_H_ 43719Swollman 444Srgrimes/* 454Srgrimes * 386 Segmentation Data Structures and definitions 464Srgrimes * William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989 474Srgrimes */ 484Srgrimes 494Srgrimes/* 504Srgrimes * Selectors 514Srgrimes */ 524Srgrimes 534Srgrimes#define ISPL(s) ((s)&3) /* what is the priority level of a selector */ 548876Srgrimes#define SEL_KPL 0 /* kernel priority level */ 558876Srgrimes#define SEL_UPL 3 /* user priority level */ 564Srgrimes#define ISLDT(s) ((s)&SEL_LDT) /* is it local or global */ 578876Srgrimes#define SEL_LDT 4 /* local descriptor table */ 584Srgrimes#define IDXSEL(s) (((s)>>3) & 0x1fff) /* index of selector */ 594Srgrimes#define LSEL(s,r) (((s)<<3) | SEL_LDT | r) /* a local selector */ 604Srgrimes#define GSEL(s,r) (((s)<<3) | r) /* a global selector */ 614Srgrimes 624Srgrimes/* 634Srgrimes * Memory and System segment descriptors 644Srgrimes */ 654Srgrimesstruct segment_descriptor { 664Srgrimes unsigned sd_lolimit:16 ; /* segment extent (lsb) */ 67103847Speter unsigned sd_lobase:24 __packed; /* segment base address (lsb) */ 684Srgrimes unsigned sd_type:5 ; /* segment type */ 694Srgrimes unsigned sd_dpl:2 ; /* segment descriptor priority level */ 704Srgrimes unsigned sd_p:1 ; /* segment descriptor present */ 714Srgrimes unsigned sd_hilimit:4 ; /* segment extent (msb) */ 724Srgrimes unsigned sd_xx:2 ; /* unused */ 734Srgrimes unsigned sd_def32:1 ; /* default 32 vs 16 bit size */ 744Srgrimes unsigned sd_gran:1 ; /* limit granularity (byte/page units)*/ 754Srgrimes unsigned sd_hibase:8 ; /* segment base address (msb) */ 764Srgrimes} ; 774Srgrimes 784Srgrimes/* 794Srgrimes * Gate descriptors (e.g. indirect descriptors) 804Srgrimes */ 814Srgrimesstruct gate_descriptor { 824Srgrimes unsigned gd_looffset:16 ; /* gate offset (lsb) */ 834Srgrimes unsigned gd_selector:16 ; /* gate segment selector */ 844Srgrimes unsigned gd_stkcpy:5 ; /* number of stack wds to cpy */ 854Srgrimes unsigned gd_xx:3 ; /* unused */ 864Srgrimes unsigned gd_type:5 ; /* segment type */ 874Srgrimes unsigned gd_dpl:2 ; /* segment descriptor priority level */ 884Srgrimes unsigned gd_p:1 ; /* segment descriptor present */ 894Srgrimes unsigned gd_hioffset:16 ; /* gate offset (msb) */ 904Srgrimes} ; 914Srgrimes 924Srgrimes/* 934Srgrimes * Generic descriptor 944Srgrimes */ 954Srgrimesunion descriptor { 964Srgrimes struct segment_descriptor sd; 974Srgrimes struct gate_descriptor gd; 984Srgrimes}; 994Srgrimes 1004Srgrimes /* system segments and gate types */ 1014Srgrimes#define SDT_SYSNULL 0 /* system null */ 1024Srgrimes#define SDT_SYS286TSS 1 /* system 286 TSS available */ 1034Srgrimes#define SDT_SYSLDT 2 /* system local descriptor table */ 1044Srgrimes#define SDT_SYS286BSY 3 /* system 286 TSS busy */ 1054Srgrimes#define SDT_SYS286CGT 4 /* system 286 call gate */ 1064Srgrimes#define SDT_SYSTASKGT 5 /* system task gate */ 1074Srgrimes#define SDT_SYS286IGT 6 /* system 286 interrupt gate */ 1084Srgrimes#define SDT_SYS286TGT 7 /* system 286 trap gate */ 1094Srgrimes#define SDT_SYSNULL2 8 /* system null again */ 1104Srgrimes#define SDT_SYS386TSS 9 /* system 386 TSS available */ 1114Srgrimes#define SDT_SYSNULL3 10 /* system null again */ 1124Srgrimes#define SDT_SYS386BSY 11 /* system 386 TSS busy */ 1134Srgrimes#define SDT_SYS386CGT 12 /* system 386 call gate */ 1144Srgrimes#define SDT_SYSNULL4 13 /* system null again */ 1154Srgrimes#define SDT_SYS386IGT 14 /* system 386 interrupt gate */ 1164Srgrimes#define SDT_SYS386TGT 15 /* system 386 trap gate */ 1174Srgrimes 1184Srgrimes /* memory segment types */ 1194Srgrimes#define SDT_MEMRO 16 /* memory read only */ 1204Srgrimes#define SDT_MEMROA 17 /* memory read only accessed */ 1214Srgrimes#define SDT_MEMRW 18 /* memory read write */ 1224Srgrimes#define SDT_MEMRWA 19 /* memory read write accessed */ 1234Srgrimes#define SDT_MEMROD 20 /* memory read only expand dwn limit */ 1244Srgrimes#define SDT_MEMRODA 21 /* memory read only expand dwn limit accessed */ 1254Srgrimes#define SDT_MEMRWD 22 /* memory read write expand dwn limit */ 12613765Smpp#define SDT_MEMRWDA 23 /* memory read write expand dwn limit accessed */ 1274Srgrimes#define SDT_MEME 24 /* memory execute only */ 1284Srgrimes#define SDT_MEMEA 25 /* memory execute only accessed */ 1294Srgrimes#define SDT_MEMER 26 /* memory execute read */ 1304Srgrimes#define SDT_MEMERA 27 /* memory execute read accessed */ 1314Srgrimes#define SDT_MEMEC 28 /* memory execute only conforming */ 1324Srgrimes#define SDT_MEMEAC 29 /* memory execute only accessed conforming */ 1334Srgrimes#define SDT_MEMERC 30 /* memory execute read conforming */ 1344Srgrimes#define SDT_MEMERAC 31 /* memory execute read accessed conforming */ 1354Srgrimes 1364Srgrimes/* 1374Srgrimes * Software definitions are in this convenient format, 1384Srgrimes * which are translated into inconvenient segment descriptors 1394Srgrimes * when needed to be used by the 386 hardware 1404Srgrimes */ 1414Srgrimes 1424Srgrimesstruct soft_segment_descriptor { 1434Srgrimes unsigned ssd_base ; /* segment base address */ 1444Srgrimes unsigned ssd_limit ; /* segment extent */ 1454Srgrimes unsigned ssd_type:5 ; /* segment type */ 1464Srgrimes unsigned ssd_dpl:2 ; /* segment descriptor priority level */ 1474Srgrimes unsigned ssd_p:1 ; /* segment descriptor present */ 1484Srgrimes unsigned ssd_xx:4 ; /* unused */ 1494Srgrimes unsigned ssd_xx1:2 ; /* unused */ 1504Srgrimes unsigned ssd_def32:1 ; /* default 32 vs 16 bit size */ 1514Srgrimes unsigned ssd_gran:1 ; /* limit granularity (byte/page units)*/ 1524Srgrimes}; 1534Srgrimes 1544Srgrimes/* 1554Srgrimes * region descriptors, used to load gdt/idt tables before segments yet exist. 1564Srgrimes */ 1574Srgrimesstruct region_descriptor { 158103847Speter unsigned rd_limit:16; /* segment extent */ 159103847Speter unsigned rd_base:32 __packed; /* base address */ 1604Srgrimes}; 1614Srgrimes 1624Srgrimes/* 1634Srgrimes * Segment Protection Exception code bits 1644Srgrimes */ 1654Srgrimes 1664Srgrimes#define SEGEX_EXT 0x01 /* recursive or externally induced */ 1674Srgrimes#define SEGEX_IDT 0x02 /* interrupt descriptor table */ 1684Srgrimes#define SEGEX_TI 0x04 /* local descriptor table */ 1694Srgrimes /* other bits are affected descriptor index */ 17087311Sjhb#define SEGEX_IDX(s) (((s)>>3)&0x1fff) 1714Srgrimes 1724Srgrimes/* 1734Srgrimes * Size of IDT table 1744Srgrimes */ 1754Srgrimes 17627519Sfsmp#if defined(SMP) || defined(APIC_IO) 17727519Sfsmp#define NIDT 256 /* we use them all */ 17827519Sfsmp#else 1796380Ssos#define NIDT 129 /* 32 reserved, 16 h/w, 0 s/w, linux's 0x80 */ 18027519Sfsmp#endif /* SMP || APIC_IO */ 1814Srgrimes#define NRSVIDT 32 /* reserved entries for cpu exceptions */ 1821051Sdg 1831051Sdg/* 184119935Sjhb * Entries in the Interrupt Descriptor Table (IDT) 185119935Sjhb */ 186119935Sjhb#define IDT_DE 0 /* #DE: Divide Error */ 187119935Sjhb#define IDT_DB 1 /* #DB: Debug */ 188119935Sjhb#define IDT_NMI 2 /* Nonmaskable External Interrupt */ 189119935Sjhb#define IDT_BP 3 /* #BP: Breakpoint */ 190119935Sjhb#define IDT_OF 4 /* #OF: Overflow */ 191119935Sjhb#define IDT_BR 5 /* #BR: Bound Range Exceeded */ 192119935Sjhb#define IDT_UD 6 /* #UD: Undefined/Invalid Opcode */ 193119935Sjhb#define IDT_NM 7 /* #NM: No Math Coprocessor */ 194119935Sjhb#define IDT_DF 8 /* #DF: Double Fault */ 195119935Sjhb#define IDT_FPUGP 9 /* Coprocessor Segment Overrun */ 196119935Sjhb#define IDT_TS 10 /* #TS: Invalid TSS */ 197119935Sjhb#define IDT_NP 11 /* #NP: Segment Not Present */ 198119935Sjhb#define IDT_SS 12 /* #SS: Stack Segment Fault */ 199119935Sjhb#define IDT_GP 13 /* #GP: General Protection Fault */ 200119935Sjhb#define IDT_PF 14 /* #PF: Page Fault */ 201119935Sjhb#define IDT_MF 16 /* #MF: FPU Floating-Point Error */ 202119935Sjhb#define IDT_AC 17 /* #AC: Alignment Check */ 203119935Sjhb#define IDT_MC 18 /* #MC: Machine Check */ 204119935Sjhb#define IDT_XF 19 /* #XF: SIMD Floating-Point Exception */ 205119935Sjhb#define IDT_SYSCALL 0x80 /* System Call Interrupt Vector */ 206119935Sjhb 207119935Sjhb/* 2081051Sdg * Entries in the Global Descriptor Table (GDT) 2091051Sdg */ 2101051Sdg#define GNULL_SEL 0 /* Null Descriptor */ 2111051Sdg#define GCODE_SEL 1 /* Kernel Code Descriptor */ 2121051Sdg#define GDATA_SEL 2 /* Kernel Data Descriptor */ 21346129Sluoqi#define GPRIV_SEL 3 /* SMP Per-Processor Private Data */ 21446129Sluoqi#define GPROC0_SEL 4 /* Task state process slot zero and up */ 21546129Sluoqi#define GLDT_SEL 5 /* LDT - eventually one per process */ 21646129Sluoqi#define GUSERLDT_SEL 6 /* User LDT */ 21746129Sluoqi#define GTGATE_SEL 7 /* Process task switch gate */ 21849952Smsmith#define GBIOSLOWMEM_SEL 8 /* BIOS low memory access (must be entry 8) */ 21949952Smsmith#define GPANIC_SEL 9 /* Task state to consider panic from */ 22049952Smsmith#define GBIOSCODE32_SEL 10 /* BIOS interface (32bit Code) */ 22149952Smsmith#define GBIOSCODE16_SEL 11 /* BIOS interface (16bit Code) */ 22249952Smsmith#define GBIOSDATA_SEL 12 /* BIOS interface (Data) */ 22349952Smsmith#define GBIOSUTIL_SEL 13 /* BIOS interface (Utility) */ 22449952Smsmith#define GBIOSARGS_SEL 14 /* BIOS interface (Arguments) */ 2251051Sdg 22649952Smsmith#define NGDT 15 2273258Sdg 2281051Sdg/* 2291051Sdg * Entries in the Local Descriptor Table (LDT) 2301051Sdg */ 2311051Sdg#define LSYS5CALLS_SEL 0 /* forced by intel BCS */ 2321051Sdg#define LSYS5SIGR_SEL 1 2331051Sdg#define L43BSDCALLS_SEL 2 /* notyet */ 2341051Sdg#define LUCODE_SEL 3 23543340Snewton#define LSOL26CALLS_SEL 4 /* Solaris >= 2.6 system call gate */ 23643340Snewton#define LUDATA_SEL 5 23713765Smpp/* separate stack, es,fs,gs sels ? */ 2381051Sdg/* #define LPOSIXCALLS_SEL 5*/ /* notyet */ 23918513Speter#define LBSDICALLS_SEL 16 /* BSDI system call gate */ 24018513Speter#define NLDT (LBSDICALLS_SEL + 1) 2411051Sdg 24255205Speter#ifdef _KERNEL 2434501Sbdeextern int _default_ldt; 24425164Speterextern union descriptor gdt[]; 2454501Sbdeextern struct soft_segment_descriptor gdt_segs[]; 24648005Sbdeextern struct gate_descriptor *idt; 2474501Sbdeextern union descriptor ldt[NLDT]; 248121754Sjhbextern struct region_descriptor r_gdt, r_idt; 2491051Sdg 25093018Sbdevoid lgdt(struct region_descriptor *rdp); 25193018Sbdevoid sdtossd(struct segment_descriptor *sdp, 25293018Sbde struct soft_segment_descriptor *ssdp); 25393018Sbdevoid ssdtosd(struct soft_segment_descriptor *ssdp, 25493018Sbde struct segment_descriptor *sdp); 25555205Speter#endif /* _KERNEL */ 2564473Sbde 2574473Sbde#endif /* !_MACHINE_SEGMENTS_H_ */ 258