segments.h revision 93018
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 93018 2002-03-23 15:09:35Z bde $ 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) */ 671051Sdg unsigned sd_lobase:24 __attribute__ ((packed)); 681051Sdg /* segment base address (lsb) */ 694Srgrimes unsigned sd_type:5 ; /* segment type */ 704Srgrimes unsigned sd_dpl:2 ; /* segment descriptor priority level */ 714Srgrimes unsigned sd_p:1 ; /* segment descriptor present */ 724Srgrimes unsigned sd_hilimit:4 ; /* segment extent (msb) */ 734Srgrimes unsigned sd_xx:2 ; /* unused */ 744Srgrimes unsigned sd_def32:1 ; /* default 32 vs 16 bit size */ 754Srgrimes unsigned sd_gran:1 ; /* limit granularity (byte/page units)*/ 764Srgrimes unsigned sd_hibase:8 ; /* segment base address (msb) */ 774Srgrimes} ; 784Srgrimes 794Srgrimes/* 804Srgrimes * Gate descriptors (e.g. indirect descriptors) 814Srgrimes */ 824Srgrimesstruct gate_descriptor { 834Srgrimes unsigned gd_looffset:16 ; /* gate offset (lsb) */ 844Srgrimes unsigned gd_selector:16 ; /* gate segment selector */ 854Srgrimes unsigned gd_stkcpy:5 ; /* number of stack wds to cpy */ 864Srgrimes unsigned gd_xx:3 ; /* unused */ 874Srgrimes unsigned gd_type:5 ; /* segment type */ 884Srgrimes unsigned gd_dpl:2 ; /* segment descriptor priority level */ 894Srgrimes unsigned gd_p:1 ; /* segment descriptor present */ 904Srgrimes unsigned gd_hioffset:16 ; /* gate offset (msb) */ 914Srgrimes} ; 924Srgrimes 934Srgrimes/* 944Srgrimes * Generic descriptor 954Srgrimes */ 964Srgrimesunion descriptor { 974Srgrimes struct segment_descriptor sd; 984Srgrimes struct gate_descriptor gd; 994Srgrimes}; 1004Srgrimes 1014Srgrimes /* system segments and gate types */ 1024Srgrimes#define SDT_SYSNULL 0 /* system null */ 1034Srgrimes#define SDT_SYS286TSS 1 /* system 286 TSS available */ 1044Srgrimes#define SDT_SYSLDT 2 /* system local descriptor table */ 1054Srgrimes#define SDT_SYS286BSY 3 /* system 286 TSS busy */ 1064Srgrimes#define SDT_SYS286CGT 4 /* system 286 call gate */ 1074Srgrimes#define SDT_SYSTASKGT 5 /* system task gate */ 1084Srgrimes#define SDT_SYS286IGT 6 /* system 286 interrupt gate */ 1094Srgrimes#define SDT_SYS286TGT 7 /* system 286 trap gate */ 1104Srgrimes#define SDT_SYSNULL2 8 /* system null again */ 1114Srgrimes#define SDT_SYS386TSS 9 /* system 386 TSS available */ 1124Srgrimes#define SDT_SYSNULL3 10 /* system null again */ 1134Srgrimes#define SDT_SYS386BSY 11 /* system 386 TSS busy */ 1144Srgrimes#define SDT_SYS386CGT 12 /* system 386 call gate */ 1154Srgrimes#define SDT_SYSNULL4 13 /* system null again */ 1164Srgrimes#define SDT_SYS386IGT 14 /* system 386 interrupt gate */ 1174Srgrimes#define SDT_SYS386TGT 15 /* system 386 trap gate */ 1184Srgrimes 1194Srgrimes /* memory segment types */ 1204Srgrimes#define SDT_MEMRO 16 /* memory read only */ 1214Srgrimes#define SDT_MEMROA 17 /* memory read only accessed */ 1224Srgrimes#define SDT_MEMRW 18 /* memory read write */ 1234Srgrimes#define SDT_MEMRWA 19 /* memory read write accessed */ 1244Srgrimes#define SDT_MEMROD 20 /* memory read only expand dwn limit */ 1254Srgrimes#define SDT_MEMRODA 21 /* memory read only expand dwn limit accessed */ 1264Srgrimes#define SDT_MEMRWD 22 /* memory read write expand dwn limit */ 12713765Smpp#define SDT_MEMRWDA 23 /* memory read write expand dwn limit accessed */ 1284Srgrimes#define SDT_MEME 24 /* memory execute only */ 1294Srgrimes#define SDT_MEMEA 25 /* memory execute only accessed */ 1304Srgrimes#define SDT_MEMER 26 /* memory execute read */ 1314Srgrimes#define SDT_MEMERA 27 /* memory execute read accessed */ 1324Srgrimes#define SDT_MEMEC 28 /* memory execute only conforming */ 1334Srgrimes#define SDT_MEMEAC 29 /* memory execute only accessed conforming */ 1344Srgrimes#define SDT_MEMERC 30 /* memory execute read conforming */ 1354Srgrimes#define SDT_MEMERAC 31 /* memory execute read accessed conforming */ 1364Srgrimes 1374Srgrimes/* is memory segment descriptor pointer ? */ 1384Srgrimes#define ISMEMSDP(s) ((s->d_type) >= SDT_MEMRO && (s->d_type) <= SDT_MEMERAC) 1394Srgrimes 1404Srgrimes/* is 286 gate descriptor pointer ? */ 1414Srgrimes#define IS286GDP(s) (((s->d_type) >= SDT_SYS286CGT \ 1424Srgrimes && (s->d_type) < SDT_SYS286TGT)) 1434Srgrimes 1444Srgrimes/* is 386 gate descriptor pointer ? */ 1454Srgrimes#define IS386GDP(s) (((s->d_type) >= SDT_SYS386CGT \ 1464Srgrimes && (s->d_type) < SDT_SYS386TGT)) 1474Srgrimes 1484Srgrimes/* is gate descriptor pointer ? */ 1494Srgrimes#define ISGDP(s) (IS286GDP(s) || IS386GDP(s)) 1504Srgrimes 1514Srgrimes/* is segment descriptor pointer ? */ 1524Srgrimes#define ISSDP(s) (ISMEMSDP(s) || !ISGDP(s)) 1534Srgrimes 1544Srgrimes/* is system segment descriptor pointer ? */ 1554Srgrimes#define ISSYSSDP(s) (!ISMEMSDP(s) && !ISGDP(s)) 1564Srgrimes 1574Srgrimes/* 1584Srgrimes * Software definitions are in this convenient format, 1594Srgrimes * which are translated into inconvenient segment descriptors 1604Srgrimes * when needed to be used by the 386 hardware 1614Srgrimes */ 1624Srgrimes 1634Srgrimesstruct soft_segment_descriptor { 1644Srgrimes unsigned ssd_base ; /* segment base address */ 1654Srgrimes unsigned ssd_limit ; /* segment extent */ 1664Srgrimes unsigned ssd_type:5 ; /* segment type */ 1674Srgrimes unsigned ssd_dpl:2 ; /* segment descriptor priority level */ 1684Srgrimes unsigned ssd_p:1 ; /* segment descriptor present */ 1694Srgrimes unsigned ssd_xx:4 ; /* unused */ 1704Srgrimes unsigned ssd_xx1:2 ; /* unused */ 1714Srgrimes unsigned ssd_def32:1 ; /* default 32 vs 16 bit size */ 1724Srgrimes unsigned ssd_gran:1 ; /* limit granularity (byte/page units)*/ 1734Srgrimes}; 1744Srgrimes 1754Srgrimes/* 1764Srgrimes * region descriptors, used to load gdt/idt tables before segments yet exist. 1774Srgrimes */ 1784Srgrimesstruct region_descriptor { 1791051Sdg unsigned rd_limit:16; /* segment extent */ 1801051Sdg unsigned rd_base:32 __attribute__ ((packed)); /* base address */ 1814Srgrimes}; 1824Srgrimes 1834Srgrimes/* 1844Srgrimes * Segment Protection Exception code bits 1854Srgrimes */ 1864Srgrimes 1874Srgrimes#define SEGEX_EXT 0x01 /* recursive or externally induced */ 1884Srgrimes#define SEGEX_IDT 0x02 /* interrupt descriptor table */ 1894Srgrimes#define SEGEX_TI 0x04 /* local descriptor table */ 1904Srgrimes /* other bits are affected descriptor index */ 19187311Sjhb#define SEGEX_IDX(s) (((s)>>3)&0x1fff) 1924Srgrimes 1934Srgrimes/* 1944Srgrimes * Size of IDT table 1954Srgrimes */ 1964Srgrimes 19727519Sfsmp#if defined(SMP) || defined(APIC_IO) 19827519Sfsmp#define NIDT 256 /* we use them all */ 19927519Sfsmp#else 2006380Ssos#define NIDT 129 /* 32 reserved, 16 h/w, 0 s/w, linux's 0x80 */ 20127519Sfsmp#endif /* SMP || APIC_IO */ 2024Srgrimes#define NRSVIDT 32 /* reserved entries for cpu exceptions */ 2031051Sdg 2041051Sdg/* 2051051Sdg * Entries in the Global Descriptor Table (GDT) 2061051Sdg */ 2071051Sdg#define GNULL_SEL 0 /* Null Descriptor */ 2081051Sdg#define GCODE_SEL 1 /* Kernel Code Descriptor */ 2091051Sdg#define GDATA_SEL 2 /* Kernel Data Descriptor */ 21046129Sluoqi#define GPRIV_SEL 3 /* SMP Per-Processor Private Data */ 21146129Sluoqi#define GPROC0_SEL 4 /* Task state process slot zero and up */ 21246129Sluoqi#define GLDT_SEL 5 /* LDT - eventually one per process */ 21346129Sluoqi#define GUSERLDT_SEL 6 /* User LDT */ 21446129Sluoqi#define GTGATE_SEL 7 /* Process task switch gate */ 21549952Smsmith#define GBIOSLOWMEM_SEL 8 /* BIOS low memory access (must be entry 8) */ 21649952Smsmith#define GPANIC_SEL 9 /* Task state to consider panic from */ 21749952Smsmith#define GBIOSCODE32_SEL 10 /* BIOS interface (32bit Code) */ 21849952Smsmith#define GBIOSCODE16_SEL 11 /* BIOS interface (16bit Code) */ 21949952Smsmith#define GBIOSDATA_SEL 12 /* BIOS interface (Data) */ 22049952Smsmith#define GBIOSUTIL_SEL 13 /* BIOS interface (Utility) */ 22149952Smsmith#define GBIOSARGS_SEL 14 /* BIOS interface (Arguments) */ 2221051Sdg 2234473Sbde#ifdef BDE_DEBUGGER 2244473Sbde#define NGDT 18 /* some of 11-17 are reserved for debugger */ 2254473Sbde#else 22649952Smsmith#define NGDT 15 2274473Sbde#endif 2283258Sdg 2291051Sdg/* 2301051Sdg * Entries in the Local Descriptor Table (LDT) 2311051Sdg */ 2321051Sdg#define LSYS5CALLS_SEL 0 /* forced by intel BCS */ 2331051Sdg#define LSYS5SIGR_SEL 1 2341051Sdg#define L43BSDCALLS_SEL 2 /* notyet */ 2351051Sdg#define LUCODE_SEL 3 23643340Snewton#define LSOL26CALLS_SEL 4 /* Solaris >= 2.6 system call gate */ 23743340Snewton#define LUDATA_SEL 5 23813765Smpp/* separate stack, es,fs,gs sels ? */ 2391051Sdg/* #define LPOSIXCALLS_SEL 5*/ /* notyet */ 24018513Speter#define LBSDICALLS_SEL 16 /* BSDI system call gate */ 24118513Speter#define NLDT (LBSDICALLS_SEL + 1) 2421051Sdg 24355205Speter#ifdef _KERNEL 2444501Sbdeextern int _default_ldt; 24525164Speterextern union descriptor gdt[]; 2464501Sbdeextern struct soft_segment_descriptor gdt_segs[]; 24748005Sbdeextern struct gate_descriptor *idt; 2484501Sbdeextern union descriptor ldt[NLDT]; 2491051Sdg 25093018Sbdevoid lgdt(struct region_descriptor *rdp); 25193018Sbdevoid lidt(struct region_descriptor *rdp); 25293018Sbdevoid lldt(u_short sel); 25393018Sbdevoid sdtossd(struct segment_descriptor *sdp, 25493018Sbde struct soft_segment_descriptor *ssdp); 25593018Sbdevoid ssdtosd(struct soft_segment_descriptor *ssdp, 25693018Sbde struct segment_descriptor *sdp); 25755205Speter#endif /* _KERNEL */ 2584473Sbde 2594473Sbde#endif /* !_MACHINE_SEGMENTS_H_ */ 260