1 /* $NetBSD: segments.h,v 1.1 2003/04/26 18:39:47 fvdl Exp $ */ 2 3 /*- 4 * Copyright (c) 1995, 1997 5 * 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. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by the University of 24 * California, Berkeley and its contributors. 25 * 4. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)segments.h 7.1 (Berkeley) 5/9/91 42 */ 43 44 /* 45 * Adapted for NetBSD/amd64 by fvdl@wasabisystems.com. 46 */ 47 48 /* 49 * 386 Segmentation Data Structures and definitions 50 * William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989 51 */ 52 53 #ifndef _AMD64_SEGMENTS_H_ 54 #define _AMD64_SEGMENTS_H_ 55 56 /* 57 * Selectors 58 */ 59 60 #define ISPL(s) ((s) & SEL_RPL) /* what is the priority level of a selector */ 61 #define SEL_KPL 0 /* kernel privilege level */ 62 #define SEL_UPL 3 /* user privilege level */ 63 #define SEL_RPL 3 /* requester's privilege level mask */ 64 #define ISLDT(s) ((s) & SEL_LDT) /* is it local or global */ 65 #define SEL_LDT 4 /* local descriptor table */ 66 67 /* Dynamically allocated TSSs and LDTs start (byte offset) */ 68 #define SYSSEL_START (NGDT_MEM << 3) 69 #define DYNSEL_START (SYSSEL_START + (NGDT_SYS << 4)) 70 71 /* 72 * These define the index not from the start of the GDT, but from 73 * the part of the GDT that they're allocated from. 74 * First NGDT_MEM entries are 8-byte descriptors for CS and DS. 75 * Next NGDT_SYS entries are 16-byte descriptors defining LDTs. 76 * 77 * The rest is 16-byte descriptors for TSS and LDT. 78 */ 79 80 #define IDXSEL(s) (((s) >> 3) & 0x1fff) 81 #define IDXDYNSEL(s) ((((s) & ~SEL_RPL) - DYNSEL_START) >> 4) 82 83 #define GSEL(s,r) (((s) << 3) | r) 84 #define GSYSSEL(s,r) ((((s) << 4) + SYSSEL_START) | r) 85 #define GDYNSEL(s,r) ((((s) << 4) + DYNSEL_START) | r | SEL_KPL) 86 87 #define LSEL(s,r) ((s) | r | SEL_LDT) 88 89 #define USERMODE(c, f) (ISPL(c) == SEL_UPL) 90 #define KERNELMODE(c, f) (ISPL(c) == SEL_KPL) 91 92 #ifndef _LOCORE 93 94 /* 95 * Memory and System segment descriptors 96 */ 97 98 /* 99 * Below is used for TSS and LDT. 100 */ 101 struct sys_segment_descriptor { 102 /*BITFIELDTYPE*/ u_int64_t sd_lolimit:16;/* segment extent (lsb) */ 103 /*BITFIELDTYPE*/ u_int64_t sd_lobase:24;/* segment base address (lsb) */ 104 /*BITFIELDTYPE*/ u_int64_t sd_type:5; /* segment type */ 105 /*BITFIELDTYPE*/ u_int64_t sd_dpl:2; /* segment descriptor priority level */ 106 /*BITFIELDTYPE*/ u_int64_t sd_p:1; /* segment descriptor present */ 107 /*BITFIELDTYPE*/ u_int64_t sd_hilimit:4;/* segment extent (msb) */ 108 /*BITFIELDTYPE*/ u_int64_t sd_xx1:3; /* avl, long and def32 (not used) */ 109 /*BITFIELDTYPE*/ u_int64_t sd_gran:1; /* limit granularity (byte/page) */ 110 /*BITFIELDTYPE*/ u_int64_t sd_hibase:40;/* segment base address (msb) */ 111 /*BITFIELDTYPE*/ u_int64_t sd_xx2:8; /* reserved */ 112 /*BITFIELDTYPE*/ u_int64_t sd_zero:5; /* must be zero */ 113 /*BITFIELDTYPE*/ u_int64_t sd_xx3:19; /* reserved */ 114 } __attribute__((packed)); 115 116 /* 117 * Below is used for cs, ds, etc. 118 */ 119 struct mem_segment_descriptor { 120 unsigned sd_lolimit:16; /* segment extent (lsb) */ 121 unsigned sd_lobase:24; /* segment base address (lsb) */ 122 unsigned sd_type:5; /* segment type */ 123 unsigned sd_dpl:2; /* segment descriptor priority level */ 124 unsigned sd_p:1; /* segment descriptor present */ 125 unsigned sd_hilimit:4; /* segment extent (msb) */ 126 unsigned sd_avl:1; /* available */ 127 unsigned sd_long:1; /* long mode */ 128 unsigned sd_def32:1; /* default 32 vs 16 bit size */ 129 unsigned sd_gran:1; /* limit granularity (byte/page) */ 130 unsigned sd_hibase:8; /* segment base address (msb) */ 131 } __attribute__((packed)); 132 133 /* 134 * Gate descriptors (e.g. indirect descriptors) 135 */ 136 struct gate_descriptor { 137 /*BITFIELDTYPE*/ u_int64_t gd_looffset:16;/* gate offset (lsb) */ 138 /*BITFIELDTYPE*/ u_int64_t gd_selector:16;/* gate segment selector */ 139 /*BITFIELDTYPE*/ u_int64_t gd_ist:3; /* IST select */ 140 /*BITFIELDTYPE*/ u_int64_t gd_xx1:5; /* reserved */ 141 /*BITFIELDTYPE*/ u_int64_t gd_type:5; /* segment type */ 142 /*BITFIELDTYPE*/ u_int64_t gd_dpl:2; /* segment descriptor priority level */ 143 /*BITFIELDTYPE*/ u_int64_t gd_p:1; /* segment descriptor present */ 144 /*BITFIELDTYPE*/ u_int64_t gd_hioffset:48;/* gate offset (msb) */ 145 /*BITFIELDTYPE*/ u_int64_t gd_xx2:8; /* reserved */ 146 /*BITFIELDTYPE*/ u_int64_t gd_zero:5; /* must be zero */ 147 /*BITFIELDTYPE*/ u_int64_t gd_xx3:19; /* reserved */ 148 } __attribute__((packed)); 149 150 /* 151 * region descriptors, used to load gdt/idt tables before segments yet exist. 152 */ 153 struct region_descriptor { 154 u_int16_t rd_limit; /* segment extent */ 155 u_int64_t rd_base; /* base address */ 156 } __attribute__((packed)); 157 158 #ifdef _KERNEL 159 #if 0 160 extern struct sys_segment_descriptor *ldt; 161 #endif 162 extern struct gate_descriptor *idt; 163 extern char *gdtstore; 164 extern char *ldtstore; 165 166 void setgate __P((struct gate_descriptor *, void *, int, int, int, int)); 167 void unsetgate __P((struct gate_descriptor *)); 168 void setregion __P((struct region_descriptor *, void *, u_int16_t)); 169 void set_sys_segment __P((struct sys_segment_descriptor *, void *, size_t, 170 int, int, int)); 171 void set_mem_segment __P((struct mem_segment_descriptor *, void *, size_t, 172 int, int, int, int, int)); 173 int idt_vec_alloc __P((int, int)); 174 void idt_vec_set __P((int, void (*)(void))); 175 void idt_vec_free __P((int)); 176 void cpu_init_idt __P((void)); 177 178 #endif /* _KERNEL */ 179 180 #endif /* !_LOCORE */ 181 182 /* system segments and gate types */ 183 #define SDT_SYSNULL 0 /* system null */ 184 #define SDT_SYS286TSS 1 /* system 286 TSS available */ 185 #define SDT_SYSLDT 2 /* system local descriptor table */ 186 #define SDT_SYS286BSY 3 /* system 286 TSS busy */ 187 #define SDT_SYS286CGT 4 /* system 286 call gate */ 188 #define SDT_SYSTASKGT 5 /* system task gate */ 189 #define SDT_SYS286IGT 6 /* system 286 interrupt gate */ 190 #define SDT_SYS286TGT 7 /* system 286 trap gate */ 191 #define SDT_SYSNULL2 8 /* system null again */ 192 #define SDT_SYS386TSS 9 /* system 386 TSS available */ 193 #define SDT_SYSNULL3 10 /* system null again */ 194 #define SDT_SYS386BSY 11 /* system 386 TSS busy */ 195 #define SDT_SYS386CGT 12 /* system 386 call gate */ 196 #define SDT_SYSNULL4 13 /* system null again */ 197 #define SDT_SYS386IGT 14 /* system 386 interrupt gate */ 198 #define SDT_SYS386TGT 15 /* system 386 trap gate */ 199 200 /* memory segment types */ 201 #define SDT_MEMRO 16 /* memory read only */ 202 #define SDT_MEMROA 17 /* memory read only accessed */ 203 #define SDT_MEMRW 18 /* memory read write */ 204 #define SDT_MEMRWA 19 /* memory read write accessed */ 205 #define SDT_MEMROD 20 /* memory read only expand dwn limit */ 206 #define SDT_MEMRODA 21 /* memory read only expand dwn limit accessed */ 207 #define SDT_MEMRWD 22 /* memory read write expand dwn limit */ 208 #define SDT_MEMRWDA 23 /* memory read write expand dwn limit acessed */ 209 #define SDT_MEME 24 /* memory execute only */ 210 #define SDT_MEMEA 25 /* memory execute only accessed */ 211 #define SDT_MEMER 26 /* memory execute read */ 212 #define SDT_MEMERA 27 /* memory execute read accessed */ 213 #define SDT_MEMEC 28 /* memory execute only conforming */ 214 #define SDT_MEMEAC 29 /* memory execute only accessed conforming */ 215 #define SDT_MEMERC 30 /* memory execute read conforming */ 216 #define SDT_MEMERAC 31 /* memory execute read accessed conforming */ 217 218 /* is memory segment descriptor pointer ? */ 219 #define ISMEMSDP(s) ((s->d_type) >= SDT_MEMRO && \ 220 (s->d_type) <= SDT_MEMERAC) 221 222 /* is 286 gate descriptor pointer ? */ 223 #define IS286GDP(s) ((s->d_type) >= SDT_SYS286CGT && \ 224 (s->d_type) < SDT_SYS286TGT) 225 226 /* is 386 gate descriptor pointer ? */ 227 #define IS386GDP(s) ((s->d_type) >= SDT_SYS386CGT && \ 228 (s->d_type) < SDT_SYS386TGT) 229 230 /* is gate descriptor pointer ? */ 231 #define ISGDP(s) (IS286GDP(s) || IS386GDP(s)) 232 233 /* is segment descriptor pointer ? */ 234 #define ISSDP(s) (ISMEMSDP(s) || !ISGDP(s)) 235 236 /* is system segment descriptor pointer ? */ 237 #define ISSYSSDP(s) (!ISMEMSDP(s) && !ISGDP(s)) 238 239 /* 240 * Segment Protection Exception code bits 241 */ 242 #define SEGEX_EXT 0x01 /* recursive or externally induced */ 243 #define SEGEX_IDT 0x02 /* interrupt descriptor table */ 244 #define SEGEX_TI 0x04 /* local descriptor table */ 245 246 /* 247 * Entries in the Interrupt Descriptor Table (IDT) 248 */ 249 #define NIDT 256 250 #define NRSVIDT 32 /* reserved entries for cpu exceptions */ 251 252 /* 253 * Entries in the Global Descriptor Table (GDT) 254 * The code and data descriptors must come first. There 255 * are NGDT_MEM of them. 256 * 257 * Then come the predefined LDT (and possibly TSS) descriptors. 258 * There are NGDT_SYS of them. 259 */ 260 #define GNULL_SEL 0 /* Null descriptor */ 261 #define GCODE_SEL 1 /* Kernel code descriptor */ 262 #define GDATA_SEL 2 /* Kernel data descriptor */ 263 #define GUCODE_SEL 3 /* User code descriptor */ 264 #define GUDATA_SEL 4 /* User data descriptor */ 265 #define GAPM32CODE_SEL 5 266 #define GAPM16CODE_SEL 6 267 #define GAPMDATA_SEL 7 268 #define GBIOSCODE_SEL 8 269 #define GBIOSDATA_SEL 9 270 #define GPNPBIOSCODE_SEL 10 271 #define GPNPBIOSDATA_SEL 11 272 #define GPNPBIOSSCRATCH_SEL 12 273 #define GPNPBIOSTRAMP_SEL 13 274 #define GUCODE32_SEL 14 275 #define GUDATA32_SEL 15 276 #define NGDT_MEM 16 277 278 #define GLDT_SEL 0 /* Default LDT descriptor */ 279 #define NGDT_SYS 1 280 281 #define GDT_SYS_OFFSET (NGDT_MEM << 3) 282 283 #define GDT_ADDR_MEM(s,i) \ 284 ((struct mem_segment_descriptor *)((s) + ((i) << 3))) 285 #define GDT_ADDR_SYS(s,i) \ 286 ((struct sys_segment_descriptor *)((s) + (((i) << 4) + SYSSEL_START))) 287 288 /* 289 * Byte offsets in the Local Descriptor Table (LDT) 290 * Strange order because of syscall/sysret insns 291 */ 292 #define LSYS5CALLS_SEL 0 /* iBCS system call gate */ 293 #define LUCODE32_SEL 8 /* 32 bit user code descriptor */ 294 #define LUDATA_SEL 16 /* User data descriptor */ 295 #define LUCODE_SEL 24 /* User code descriptor */ 296 #define LSOL26CALLS_SEL 32 /* Solaris 2.6 system call gate */ 297 #define LUDATA32_SEL 56 /* 32 bit user data descriptor (needed?)*/ 298 #define LBSDICALLS_SEL 128 /* BSDI system call gate */ 299 300 #define LDT_SIZE 144 301 302 #define LSYSRETBASE_SEL LUCODE32_SEL 303 304 #endif /* _AMD64_SEGMENTS_H_ */ 305