1 /* $OpenBSD: segments.h,v 1.28 2020/09/24 11:36:50 deraadt Exp $ */ 2 /* $NetBSD: segments.h,v 1.23 1996/02/01 22:31:03 mycroft Exp $ */ 3 4 /*- 5 * Copyright (c) 1995 Charles M. Hannum. All rights reserved. 6 * Copyright (c) 1989, 1990 William F. Jolitz 7 * Copyright (c) 1990 The Regents of the University of California. 8 * All rights reserved. 9 * 10 * This code is derived from software contributed to Berkeley by 11 * William Jolitz. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)segments.h 7.1 (Berkeley) 5/9/91 38 */ 39 40 /* 41 * 386 Segmentation Data Structures and definitions 42 * William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989 43 */ 44 45 #ifndef _MACHINE_SEGMENTS_H_ 46 #define _MACHINE_SEGMENTS_H_ 47 48 /* 49 * Selectors 50 */ 51 52 #define ISPL(s) ((s) & SEL_RPL) /* what is the priority level of a selector */ 53 #define SEL_KPL 0 /* kernel privilege level */ 54 #define SEL_UPL 3 /* user privilege level */ 55 #define SEL_RPL 3 /* requester's privilege level mask */ 56 #define ISLDT(s) ((s) & SEL_LDT) /* is it local or global */ 57 #define SEL_LDT 4 /* local descriptor table */ 58 #define IDXSEL(s) (((s) >> 3) & 0x1fff) /* index of selector */ 59 #define GSEL(s,r) (((s) << 3) | r) /* a global selector */ 60 #define LSEL(s,r) (((s) << 3) | r | SEL_LDT) /* a local selector */ 61 62 #define USERMODE(c, f) (ISPL(c) == SEL_UPL) 63 #define KERNELMODE(c, f) (ISPL(c) == SEL_KPL) 64 65 #ifndef _LOCORE 66 67 /* 68 * Memory and System segment descriptors 69 */ 70 struct segment_descriptor { 71 unsigned sd_lolimit:16; /* segment extent (lsb) */ 72 unsigned sd_lobase:24; /* segment base address (lsb) */ 73 unsigned sd_type:5; /* segment type */ 74 unsigned sd_dpl:2; /* segment descriptor priority level */ 75 unsigned sd_p:1; /* segment descriptor present */ 76 unsigned sd_hilimit:4; /* segment extent (msb) */ 77 unsigned sd_xx:2; /* unused */ 78 unsigned sd_def32:1; /* default 32 vs 16 bit size */ 79 unsigned sd_gran:1; /* limit granularity (byte/page) */ 80 unsigned sd_hibase:8; /* segment base address (msb) */ 81 } __packed; 82 83 /* 84 * Gate descriptors (e.g. indirect descriptors) 85 */ 86 struct gate_descriptor { 87 unsigned gd_looffset:16; /* gate offset (lsb) */ 88 unsigned gd_selector:16; /* gate segment selector */ 89 unsigned gd_stkcpy:5; /* number of stack wds to cpy */ 90 unsigned gd_xx:3; /* unused */ 91 unsigned gd_type:5; /* segment type */ 92 unsigned gd_dpl:2; /* segment descriptor priority level */ 93 unsigned gd_p:1; /* segment descriptor present */ 94 unsigned gd_hioffset:16; /* gate offset (msb) */ 95 } __packed; 96 97 /* 98 * Generic descriptor 99 */ 100 union descriptor { 101 struct segment_descriptor sd; 102 struct gate_descriptor gd; 103 } __packed; 104 105 /* 106 * region descriptors, used to load gdt/idt tables before segments yet exist. 107 */ 108 struct region_descriptor { 109 unsigned rd_limit:16; /* segment extent */ 110 unsigned rd_base:32; /* base address */ 111 } __packed; 112 113 #ifdef _KERNEL 114 extern union descriptor *gdt; 115 extern struct gate_descriptor idt_region[]; 116 extern struct gate_descriptor *idt; 117 118 #define SEGDESC_LIMIT(sd) (ptoa(((sd).sd_hilimit << 16) | (sd).sd_lolimit)) 119 120 void setgate(struct gate_descriptor *, void *, int, int, int, int); 121 void setregion(struct region_descriptor *, void *, size_t); 122 void setsegment(struct segment_descriptor *, void *, size_t, int, int, 123 int, int); 124 void initcodesegment(struct segment_descriptor *); 125 void unsetgate(struct gate_descriptor *); 126 void cpu_init_idt(void); 127 128 int idt_vec_alloc(int, int); 129 void idt_vec_set(int, void (*)(void)); 130 void idt_vec_free(int); 131 132 #endif /* _KERNEL */ 133 134 #endif /* !_LOCORE */ 135 136 /* system segments and gate types */ 137 #define SDT_SYSNULL 0 /* system null */ 138 #define SDT_SYS286TSS 1 /* system 286 TSS available */ 139 #define SDT_SYSLDT 2 /* system local descriptor table */ 140 #define SDT_SYS286BSY 3 /* system 286 TSS busy */ 141 #define SDT_SYS286CGT 4 /* system 286 call gate */ 142 #define SDT_SYSTASKGT 5 /* system task gate */ 143 #define SDT_SYS286IGT 6 /* system 286 interrupt gate */ 144 #define SDT_SYS286TGT 7 /* system 286 trap gate */ 145 #define SDT_SYSNULL2 8 /* system null again */ 146 #define SDT_SYS386TSS 9 /* system 386 TSS available */ 147 #define SDT_SYSNULL3 10 /* system null again */ 148 #define SDT_SYS386BSY 11 /* system 386 TSS busy */ 149 #define SDT_SYS386CGT 12 /* system 386 call gate */ 150 #define SDT_SYSNULL4 13 /* system null again */ 151 #define SDT_SYS386IGT 14 /* system 386 interrupt gate */ 152 #define SDT_SYS386TGT 15 /* system 386 trap gate */ 153 154 /* memory segment types */ 155 #define SDT_MEMRO 16 /* memory read only */ 156 #define SDT_MEMROA 17 /* memory read only accessed */ 157 #define SDT_MEMRW 18 /* memory read write */ 158 #define SDT_MEMRWA 19 /* memory read write accessed */ 159 #define SDT_MEMROD 20 /* memory read only expand dwn limit */ 160 #define SDT_MEMRODA 21 /* memory read only expand dwn limit accessed */ 161 #define SDT_MEMRWD 22 /* memory read write expand dwn limit */ 162 #define SDT_MEMRWDA 23 /* memory read write expand dwn limit acessed */ 163 #define SDT_MEME 24 /* memory execute only */ 164 #define SDT_MEMEA 25 /* memory execute only accessed */ 165 #define SDT_MEMER 26 /* memory execute read */ 166 #define SDT_MEMERA 27 /* memory execute read accessed */ 167 #define SDT_MEMEC 28 /* memory execute only conforming */ 168 #define SDT_MEMEAC 29 /* memory execute only accessed conforming */ 169 #define SDT_MEMERC 30 /* memory execute read conforming */ 170 #define SDT_MEMERAC 31 /* memory execute read accessed conforming */ 171 172 /* is memory segment descriptor pointer ? */ 173 #define ISMEMSDP(s) ((s->d_type) >= SDT_MEMRO && \ 174 (s->d_type) <= SDT_MEMERAC) 175 176 /* is 286 gate descriptor pointer ? */ 177 #define IS286GDP(s) ((s->d_type) >= SDT_SYS286CGT && \ 178 (s->d_type) < SDT_SYS286TGT) 179 180 /* is 386 gate descriptor pointer ? */ 181 #define IS386GDP(s) ((s->d_type) >= SDT_SYS386CGT && \ 182 (s->d_type) < SDT_SYS386TGT) 183 184 /* is gate descriptor pointer ? */ 185 #define ISGDP(s) (IS286GDP(s) || IS386GDP(s)) 186 187 /* is segment descriptor pointer ? */ 188 #define ISSDP(s) (ISMEMSDP(s) || !ISGDP(s)) 189 190 /* is system segment descriptor pointer ? */ 191 #define ISSYSSDP(s) (!ISMEMSDP(s) && !ISGDP(s)) 192 193 /* 194 * Segment Protection Exception code bits 195 */ 196 #define SEGEX_EXT 0x01 /* recursive or externally induced */ 197 #define SEGEX_IDT 0x02 /* interrupt descriptor table */ 198 #define SEGEX_TI 0x04 /* local descriptor table */ 199 200 /* 201 * Entries in the Interrupt Descriptor Table (IDT) 202 */ 203 #define NIDT 256 204 #define NRSVIDT 32 /* reserved entries for cpu exceptions */ 205 206 /* 207 * Entries in the Global Descriptor Table (GDT) 208 */ 209 #define GNULL_SEL 0 /* Null descriptor */ 210 #define GCODE_SEL 1 /* Kernel code descriptor */ 211 #define GDATA_SEL 2 /* Kernel data descriptor */ 212 #define GLDT_SEL 3 /* Default LDT descriptor (UNUSED) */ 213 #define GCPU_SEL 4 /* per-CPU segment */ 214 #define GUCODE_SEL 5 /* User code descriptor (a stack short) */ 215 #define GUDATA_SEL 6 /* User data descriptor */ 216 #define GAPM32CODE_SEL 7 /* 32 bit APM code descriptor */ 217 #define GAPM16CODE_SEL 8 /* 16 bit APM code descriptor */ 218 #define GAPMDATA_SEL 9 /* APM data descriptor */ 219 #define GICODE_SEL 10 /* Interrupt code descriptor (same as Kernel code) */ 220 #define GUFS_SEL 11 /* User per-thread (%fs) descriptor */ 221 #define GUGS_SEL 12 /* User per-thread (%gs) descriptor */ 222 #define GTSS_SEL 13 /* common TSS */ 223 #define GNMITSS_SEL 14 /* NMI TSS */ 224 #define GBIOS32_SEL 15 /* spare slot for 32 bit BIOS calls */ 225 #define NGDT 16 226 227 #define GDT_SIZE (NGDT << 3) 228 229 #endif /* _MACHINE_SEGMENTS_H_ */ 230