15b81b6b3SRodney W. Grimes /*- 25b81b6b3SRodney W. Grimes * Copyright (c) 1989, 1990 William F. Jolitz 35b81b6b3SRodney W. Grimes * Copyright (c) 1990 The Regents of the University of California. 45b81b6b3SRodney W. Grimes * All rights reserved. 55b81b6b3SRodney W. Grimes * 65b81b6b3SRodney W. Grimes * This code is derived from software contributed to Berkeley by 75b81b6b3SRodney W. Grimes * William Jolitz. 85b81b6b3SRodney W. Grimes * 95b81b6b3SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 105b81b6b3SRodney W. Grimes * modification, are permitted provided that the following conditions 115b81b6b3SRodney W. Grimes * are met: 125b81b6b3SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 135b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 145b81b6b3SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 155b81b6b3SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 165b81b6b3SRodney W. Grimes * documentation and/or other materials provided with the distribution. 175b81b6b3SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 185b81b6b3SRodney W. Grimes * must display the following acknowledgement: 195b81b6b3SRodney W. Grimes * This product includes software developed by the University of 205b81b6b3SRodney W. Grimes * California, Berkeley and its contributors. 215b81b6b3SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 225b81b6b3SRodney W. Grimes * may be used to endorse or promote products derived from this software 235b81b6b3SRodney W. Grimes * without specific prior written permission. 245b81b6b3SRodney W. Grimes * 255b81b6b3SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 265b81b6b3SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 275b81b6b3SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 285b81b6b3SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 295b81b6b3SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 305b81b6b3SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 315b81b6b3SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 325b81b6b3SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 335b81b6b3SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 345b81b6b3SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 355b81b6b3SRodney W. Grimes * SUCH DAMAGE. 365b81b6b3SRodney W. Grimes * 3734a8ed1bSRodney W. Grimes * from: @(#)segments.h 7.1 (Berkeley) 5/9/91 3850045fbcSBruce Evans * $Id: segments.h,v 1.19 1999/04/28 01:04:06 luoqi Exp $ 395b81b6b3SRodney W. Grimes */ 405b81b6b3SRodney W. Grimes 416e393973SGarrett Wollman #ifndef _MACHINE_SEGMENTS_H_ 42b0d1e6deSBruce Evans #define _MACHINE_SEGMENTS_H_ 436e393973SGarrett Wollman 445206bca1SLuoqi Chen #include <machine/globals.h> 455206bca1SLuoqi Chen 465b81b6b3SRodney W. Grimes /* 475b81b6b3SRodney W. Grimes * 386 Segmentation Data Structures and definitions 485b81b6b3SRodney W. Grimes * William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989 495b81b6b3SRodney W. Grimes */ 505b81b6b3SRodney W. Grimes 515b81b6b3SRodney W. Grimes /* 525b81b6b3SRodney W. Grimes * Selectors 535b81b6b3SRodney W. Grimes */ 545b81b6b3SRodney W. Grimes 555b81b6b3SRodney W. Grimes #define ISPL(s) ((s)&3) /* what is the priority level of a selector */ 565b81b6b3SRodney W. Grimes #define SEL_KPL 0 /* kernel priority level */ 575b81b6b3SRodney W. Grimes #define SEL_UPL 3 /* user priority level */ 585b81b6b3SRodney W. Grimes #define ISLDT(s) ((s)&SEL_LDT) /* is it local or global */ 595b81b6b3SRodney W. Grimes #define SEL_LDT 4 /* local descriptor table */ 605b81b6b3SRodney W. Grimes #define IDXSEL(s) (((s)>>3) & 0x1fff) /* index of selector */ 615b81b6b3SRodney W. Grimes #define LSEL(s,r) (((s)<<3) | SEL_LDT | r) /* a local selector */ 625b81b6b3SRodney W. Grimes #define GSEL(s,r) (((s)<<3) | r) /* a global selector */ 635b81b6b3SRodney W. Grimes 645b81b6b3SRodney W. Grimes /* 655b81b6b3SRodney W. Grimes * Memory and System segment descriptors 665b81b6b3SRodney W. Grimes */ 675b81b6b3SRodney W. Grimes struct segment_descriptor { 685b81b6b3SRodney W. Grimes unsigned sd_lolimit:16 ; /* segment extent (lsb) */ 69da59a31cSDavid Greenman unsigned sd_lobase:24 __attribute__ ((packed)); 70da59a31cSDavid Greenman /* segment base address (lsb) */ 715b81b6b3SRodney W. Grimes unsigned sd_type:5 ; /* segment type */ 725b81b6b3SRodney W. Grimes unsigned sd_dpl:2 ; /* segment descriptor priority level */ 735b81b6b3SRodney W. Grimes unsigned sd_p:1 ; /* segment descriptor present */ 745b81b6b3SRodney W. Grimes unsigned sd_hilimit:4 ; /* segment extent (msb) */ 755b81b6b3SRodney W. Grimes unsigned sd_xx:2 ; /* unused */ 765b81b6b3SRodney W. Grimes unsigned sd_def32:1 ; /* default 32 vs 16 bit size */ 775b81b6b3SRodney W. Grimes unsigned sd_gran:1 ; /* limit granularity (byte/page units)*/ 785b81b6b3SRodney W. Grimes unsigned sd_hibase:8 ; /* segment base address (msb) */ 795b81b6b3SRodney W. Grimes } ; 805b81b6b3SRodney W. Grimes 815b81b6b3SRodney W. Grimes /* 825b81b6b3SRodney W. Grimes * Gate descriptors (e.g. indirect descriptors) 835b81b6b3SRodney W. Grimes */ 845b81b6b3SRodney W. Grimes struct gate_descriptor { 855b81b6b3SRodney W. Grimes unsigned gd_looffset:16 ; /* gate offset (lsb) */ 865b81b6b3SRodney W. Grimes unsigned gd_selector:16 ; /* gate segment selector */ 875b81b6b3SRodney W. Grimes unsigned gd_stkcpy:5 ; /* number of stack wds to cpy */ 885b81b6b3SRodney W. Grimes unsigned gd_xx:3 ; /* unused */ 895b81b6b3SRodney W. Grimes unsigned gd_type:5 ; /* segment type */ 905b81b6b3SRodney W. Grimes unsigned gd_dpl:2 ; /* segment descriptor priority level */ 915b81b6b3SRodney W. Grimes unsigned gd_p:1 ; /* segment descriptor present */ 925b81b6b3SRodney W. Grimes unsigned gd_hioffset:16 ; /* gate offset (msb) */ 935b81b6b3SRodney W. Grimes } ; 945b81b6b3SRodney W. Grimes 955b81b6b3SRodney W. Grimes /* 965b81b6b3SRodney W. Grimes * Generic descriptor 975b81b6b3SRodney W. Grimes */ 985b81b6b3SRodney W. Grimes union descriptor { 995b81b6b3SRodney W. Grimes struct segment_descriptor sd; 1005b81b6b3SRodney W. Grimes struct gate_descriptor gd; 1015b81b6b3SRodney W. Grimes }; 1025b81b6b3SRodney W. Grimes 1035b81b6b3SRodney W. Grimes /* system segments and gate types */ 1045b81b6b3SRodney W. Grimes #define SDT_SYSNULL 0 /* system null */ 1055b81b6b3SRodney W. Grimes #define SDT_SYS286TSS 1 /* system 286 TSS available */ 1065b81b6b3SRodney W. Grimes #define SDT_SYSLDT 2 /* system local descriptor table */ 1075b81b6b3SRodney W. Grimes #define SDT_SYS286BSY 3 /* system 286 TSS busy */ 1085b81b6b3SRodney W. Grimes #define SDT_SYS286CGT 4 /* system 286 call gate */ 1095b81b6b3SRodney W. Grimes #define SDT_SYSTASKGT 5 /* system task gate */ 1105b81b6b3SRodney W. Grimes #define SDT_SYS286IGT 6 /* system 286 interrupt gate */ 1115b81b6b3SRodney W. Grimes #define SDT_SYS286TGT 7 /* system 286 trap gate */ 1125b81b6b3SRodney W. Grimes #define SDT_SYSNULL2 8 /* system null again */ 1135b81b6b3SRodney W. Grimes #define SDT_SYS386TSS 9 /* system 386 TSS available */ 1145b81b6b3SRodney W. Grimes #define SDT_SYSNULL3 10 /* system null again */ 1155b81b6b3SRodney W. Grimes #define SDT_SYS386BSY 11 /* system 386 TSS busy */ 1165b81b6b3SRodney W. Grimes #define SDT_SYS386CGT 12 /* system 386 call gate */ 1175b81b6b3SRodney W. Grimes #define SDT_SYSNULL4 13 /* system null again */ 1185b81b6b3SRodney W. Grimes #define SDT_SYS386IGT 14 /* system 386 interrupt gate */ 1195b81b6b3SRodney W. Grimes #define SDT_SYS386TGT 15 /* system 386 trap gate */ 1205b81b6b3SRodney W. Grimes 1215b81b6b3SRodney W. Grimes /* memory segment types */ 1225b81b6b3SRodney W. Grimes #define SDT_MEMRO 16 /* memory read only */ 1235b81b6b3SRodney W. Grimes #define SDT_MEMROA 17 /* memory read only accessed */ 1245b81b6b3SRodney W. Grimes #define SDT_MEMRW 18 /* memory read write */ 1255b81b6b3SRodney W. Grimes #define SDT_MEMRWA 19 /* memory read write accessed */ 1265b81b6b3SRodney W. Grimes #define SDT_MEMROD 20 /* memory read only expand dwn limit */ 1275b81b6b3SRodney W. Grimes #define SDT_MEMRODA 21 /* memory read only expand dwn limit accessed */ 1285b81b6b3SRodney W. Grimes #define SDT_MEMRWD 22 /* memory read write expand dwn limit */ 1296c5e9bbdSMike Pritchard #define SDT_MEMRWDA 23 /* memory read write expand dwn limit accessed */ 1305b81b6b3SRodney W. Grimes #define SDT_MEME 24 /* memory execute only */ 1315b81b6b3SRodney W. Grimes #define SDT_MEMEA 25 /* memory execute only accessed */ 1325b81b6b3SRodney W. Grimes #define SDT_MEMER 26 /* memory execute read */ 1335b81b6b3SRodney W. Grimes #define SDT_MEMERA 27 /* memory execute read accessed */ 1345b81b6b3SRodney W. Grimes #define SDT_MEMEC 28 /* memory execute only conforming */ 1355b81b6b3SRodney W. Grimes #define SDT_MEMEAC 29 /* memory execute only accessed conforming */ 1365b81b6b3SRodney W. Grimes #define SDT_MEMERC 30 /* memory execute read conforming */ 1375b81b6b3SRodney W. Grimes #define SDT_MEMERAC 31 /* memory execute read accessed conforming */ 1385b81b6b3SRodney W. Grimes 1395b81b6b3SRodney W. Grimes /* is memory segment descriptor pointer ? */ 1405b81b6b3SRodney W. Grimes #define ISMEMSDP(s) ((s->d_type) >= SDT_MEMRO && (s->d_type) <= SDT_MEMERAC) 1415b81b6b3SRodney W. Grimes 1425b81b6b3SRodney W. Grimes /* is 286 gate descriptor pointer ? */ 1435b81b6b3SRodney W. Grimes #define IS286GDP(s) (((s->d_type) >= SDT_SYS286CGT \ 1445b81b6b3SRodney W. Grimes && (s->d_type) < SDT_SYS286TGT)) 1455b81b6b3SRodney W. Grimes 1465b81b6b3SRodney W. Grimes /* is 386 gate descriptor pointer ? */ 1475b81b6b3SRodney W. Grimes #define IS386GDP(s) (((s->d_type) >= SDT_SYS386CGT \ 1485b81b6b3SRodney W. Grimes && (s->d_type) < SDT_SYS386TGT)) 1495b81b6b3SRodney W. Grimes 1505b81b6b3SRodney W. Grimes /* is gate descriptor pointer ? */ 1515b81b6b3SRodney W. Grimes #define ISGDP(s) (IS286GDP(s) || IS386GDP(s)) 1525b81b6b3SRodney W. Grimes 1535b81b6b3SRodney W. Grimes /* is segment descriptor pointer ? */ 1545b81b6b3SRodney W. Grimes #define ISSDP(s) (ISMEMSDP(s) || !ISGDP(s)) 1555b81b6b3SRodney W. Grimes 1565b81b6b3SRodney W. Grimes /* is system segment descriptor pointer ? */ 1575b81b6b3SRodney W. Grimes #define ISSYSSDP(s) (!ISMEMSDP(s) && !ISGDP(s)) 1585b81b6b3SRodney W. Grimes 1595b81b6b3SRodney W. Grimes /* 1605b81b6b3SRodney W. Grimes * Software definitions are in this convenient format, 1615b81b6b3SRodney W. Grimes * which are translated into inconvenient segment descriptors 1625b81b6b3SRodney W. Grimes * when needed to be used by the 386 hardware 1635b81b6b3SRodney W. Grimes */ 1645b81b6b3SRodney W. Grimes 1655b81b6b3SRodney W. Grimes struct soft_segment_descriptor { 1665b81b6b3SRodney W. Grimes unsigned ssd_base ; /* segment base address */ 1675b81b6b3SRodney W. Grimes unsigned ssd_limit ; /* segment extent */ 1685b81b6b3SRodney W. Grimes unsigned ssd_type:5 ; /* segment type */ 1695b81b6b3SRodney W. Grimes unsigned ssd_dpl:2 ; /* segment descriptor priority level */ 1705b81b6b3SRodney W. Grimes unsigned ssd_p:1 ; /* segment descriptor present */ 1715b81b6b3SRodney W. Grimes unsigned ssd_xx:4 ; /* unused */ 1725b81b6b3SRodney W. Grimes unsigned ssd_xx1:2 ; /* unused */ 1735b81b6b3SRodney W. Grimes unsigned ssd_def32:1 ; /* default 32 vs 16 bit size */ 1745b81b6b3SRodney W. Grimes unsigned ssd_gran:1 ; /* limit granularity (byte/page units)*/ 1755b81b6b3SRodney W. Grimes }; 1765b81b6b3SRodney W. Grimes 1775b81b6b3SRodney W. Grimes /* 1785b81b6b3SRodney W. Grimes * region descriptors, used to load gdt/idt tables before segments yet exist. 1795b81b6b3SRodney W. Grimes */ 1805b81b6b3SRodney W. Grimes struct region_descriptor { 1815b81b6b3SRodney W. Grimes unsigned rd_limit:16; /* segment extent */ 182da59a31cSDavid Greenman unsigned rd_base:32 __attribute__ ((packed)); /* base address */ 1835b81b6b3SRodney W. Grimes }; 1845b81b6b3SRodney W. Grimes 1855b81b6b3SRodney W. Grimes /* 1865b81b6b3SRodney W. Grimes * Segment Protection Exception code bits 1875b81b6b3SRodney W. Grimes */ 1885b81b6b3SRodney W. Grimes 1895b81b6b3SRodney W. Grimes #define SEGEX_EXT 0x01 /* recursive or externally induced */ 1905b81b6b3SRodney W. Grimes #define SEGEX_IDT 0x02 /* interrupt descriptor table */ 1915b81b6b3SRodney W. Grimes #define SEGEX_TI 0x04 /* local descriptor table */ 1925b81b6b3SRodney W. Grimes /* other bits are affected descriptor index */ 1935b81b6b3SRodney W. Grimes #define SEGEX_IDX(s) ((s)>>3)&0x1fff) 1945b81b6b3SRodney W. Grimes 1955b81b6b3SRodney W. Grimes /* 1965b81b6b3SRodney W. Grimes * Size of IDT table 1975b81b6b3SRodney W. Grimes */ 1985b81b6b3SRodney W. Grimes 199e743fb99SSteve Passe #if defined(SMP) || defined(APIC_IO) 200e743fb99SSteve Passe #define NIDT 256 /* we use them all */ 201e743fb99SSteve Passe #else 2021e1e0b44SSøren Schmidt #define NIDT 129 /* 32 reserved, 16 h/w, 0 s/w, linux's 0x80 */ 203e743fb99SSteve Passe #endif /* SMP || APIC_IO */ 2045b81b6b3SRodney W. Grimes #define NRSVIDT 32 /* reserved entries for cpu exceptions */ 205da59a31cSDavid Greenman 206da59a31cSDavid Greenman /* 207da59a31cSDavid Greenman * Entries in the Global Descriptor Table (GDT) 208da59a31cSDavid Greenman */ 209da59a31cSDavid Greenman #define GNULL_SEL 0 /* Null Descriptor */ 210da59a31cSDavid Greenman #define GCODE_SEL 1 /* Kernel Code Descriptor */ 211da59a31cSDavid Greenman #define GDATA_SEL 2 /* Kernel Data Descriptor */ 2125206bca1SLuoqi Chen #define GPRIV_SEL 3 /* SMP Per-Processor Private Data */ 2135206bca1SLuoqi Chen #define GPROC0_SEL 4 /* Task state process slot zero and up */ 2145206bca1SLuoqi Chen #define GLDT_SEL 5 /* LDT - eventually one per process */ 2155206bca1SLuoqi Chen #define GUSERLDT_SEL 6 /* User LDT */ 2165206bca1SLuoqi Chen #define GTGATE_SEL 7 /* Process task switch gate */ 2175206bca1SLuoqi Chen #define GPANIC_SEL 8 /* Task state to consider panic from */ 2185206bca1SLuoqi Chen #define GAPMCODE32_SEL 9 /* APM BIOS 32-bit interface (32bit Code) */ 2195206bca1SLuoqi Chen #define GAPMCODE16_SEL 10 /* APM BIOS 32-bit interface (16bit Code) */ 2205206bca1SLuoqi Chen #define GAPMDATA_SEL 11 /* APM BIOS 32-bit interface (Data) */ 22122414e53SDavid Greenman 222040f1000SBruce Evans #ifdef BDE_DEBUGGER 223040f1000SBruce Evans #define NGDT 18 /* some of 11-17 are reserved for debugger */ 224040f1000SBruce Evans #else 2255206bca1SLuoqi Chen #define NGDT 12 226040f1000SBruce Evans #endif 227da59a31cSDavid Greenman 228da59a31cSDavid Greenman /* 229da59a31cSDavid Greenman * Entries in the Local Descriptor Table (LDT) 230da59a31cSDavid Greenman */ 231da59a31cSDavid Greenman #define LSYS5CALLS_SEL 0 /* forced by intel BCS */ 232da59a31cSDavid Greenman #define LSYS5SIGR_SEL 1 233da59a31cSDavid Greenman #define L43BSDCALLS_SEL 2 /* notyet */ 234da59a31cSDavid Greenman #define LUCODE_SEL 3 23527286ca9SMark Newton #define LSOL26CALLS_SEL 4 /* Solaris >= 2.6 system call gate */ 23627286ca9SMark Newton #define LUDATA_SEL 5 2376c5e9bbdSMike Pritchard /* separate stack, es,fs,gs sels ? */ 238da59a31cSDavid Greenman /* #define LPOSIXCALLS_SEL 5*/ /* notyet */ 23990af01beSPeter Wemm #define LBSDICALLS_SEL 16 /* BSDI system call gate */ 24090af01beSPeter Wemm #define NLDT (LBSDICALLS_SEL + 1) 241da59a31cSDavid Greenman 242da59a31cSDavid Greenman #ifdef KERNEL 2435206bca1SLuoqi Chen #ifndef currentldt 244b0d1e6deSBruce Evans extern int currentldt; 2455206bca1SLuoqi Chen #endif 246b0d1e6deSBruce Evans extern int _default_ldt; 247477a642cSPeter Wemm extern union descriptor gdt[]; 248b0d1e6deSBruce Evans extern struct soft_segment_descriptor gdt_segs[]; 24950045fbcSBruce Evans extern struct gate_descriptor *idt; 250b0d1e6deSBruce Evans extern union descriptor ldt[NLDT]; 251da59a31cSDavid Greenman 252040f1000SBruce Evans void lgdt __P((struct region_descriptor *rdp)); 253040f1000SBruce Evans void lidt __P((struct region_descriptor *rdp)); 254040f1000SBruce Evans void lldt __P((u_short sel)); 255040f1000SBruce Evans void sdtossd __P((struct segment_descriptor *sdp, 256040f1000SBruce Evans struct soft_segment_descriptor *ssdp)); 257040f1000SBruce Evans void ssdtosd __P((struct soft_segment_descriptor *ssdp, 258040f1000SBruce Evans struct segment_descriptor *sdp)); 259040f1000SBruce Evans #endif /* KERNEL */ 260040f1000SBruce Evans 261040f1000SBruce Evans #endif /* !_MACHINE_SEGMENTS_H_ */ 262