1 /* 2 * 386 Segmentation Data Structures and definitions 3 * William F. Jolitz (william@ernie.berkeley.edu) 6/20/1989 4 */ 5 6 /* 7 * Selectors 8 */ 9 10 #define ISPL(s) ((s)&3) /* what is the priority level of a selector */ 11 #define SEL_KPL 0 /* kernel priority level */ 12 #define SEL_UPL 3 /* user priority level */ 13 #define ISLDT(s) ((s)&SEL_LDT) /* is it local or global */ 14 #define SEL_LDT 4 /* local descriptor table */ 15 #define IDXSEL(s) (((s)>>3) & 0x1fff) /* index of selector */ 16 #define LSEL(s,r) (((s)<<3) | SEL_LDT | r) /* a local selector */ 17 #define GSEL(s,r) (((s)<<3) | r) /* a global selector */ 18 19 /* 20 * Memory and System segment descriptors 21 */ 22 struct segment_descriptor { 23 unsigned sd_lolimit:16 ; /* segment extent (lsb) */ 24 unsigned sd_lobase:24 ; /* segment base address (lsb) */ 25 unsigned sd_type:5 ; /* segment type */ 26 unsigned sd_dpl:2 ; /* segment descriptor priority level */ 27 unsigned sd_p:1 ; /* segment descriptor present */ 28 unsigned sd_hilimit:4 ; /* segment extent (msb) */ 29 unsigned sd_xx:2 ; /* unused */ 30 unsigned sd_def32:1 ; /* default 32 vs 16 bit size */ 31 unsigned sd_gran:1 ; /* limit granularity (byte/page units)*/ 32 unsigned sd_hibase:8 ; /* segment base address (msb) */ 33 } ; 34 35 /* 36 * Gate descriptors (e.g. indirect descriptors) 37 */ 38 struct gate_descriptor { 39 unsigned gd_looffset:16 ; /* gate offset (lsb) */ 40 unsigned gd_selector:16 ; /* gate segment selector */ 41 unsigned gd_stkcpy:5 ; /* number of stack wds to cpy */ 42 unsigned gd_xx:3 ; /* unused */ 43 unsigned gd_type:5 ; /* segment type */ 44 unsigned gd_dpl:2 ; /* segment descriptor priority level */ 45 unsigned gd_p:1 ; /* segment descriptor present */ 46 unsigned gd_hioffset:16 ; /* gate offset (msb) */ 47 } ; 48 49 /* 50 * Generic descriptor 51 */ 52 union descriptor { 53 struct segment_descriptor sd; 54 struct gate_descriptor gd; 55 }; 56 #define d_type gd.gd_type 57 58 /* system segments and gate types */ 59 #define SDT_SYSNULL 0 /* system null */ 60 #define SDT_SYS286TSS 1 /* system 286 TSS available */ 61 #define SDT_SYSLDT 2 /* system local descriptor table */ 62 #define SDT_SYS286BSY 3 /* system 286 TSS busy */ 63 #define SDT_SYS286CGT 4 /* system 286 call gate */ 64 #define SDT_SYSTASKGT 5 /* system task gate */ 65 #define SDT_SYS286IGT 6 /* system 286 interrupt gate */ 66 #define SDT_SYS286TGT 7 /* system 286 trap gate */ 67 #define SDT_SYSNULL2 8 /* system null again */ 68 #define SDT_SYS386TSS 9 /* system 386 TSS available */ 69 #define SDT_SYSNULL3 10 /* system null again */ 70 #define SDT_SYS386BSY 11 /* system 386 TSS busy */ 71 #define SDT_SYS386CGT 12 /* system 386 call gate */ 72 #define SDT_SYSNULL4 13 /* system null again */ 73 #define SDT_SYS386IGT 14 /* system 386 interrupt gate */ 74 #define SDT_SYS386TGT 15 /* system 386 trap gate */ 75 76 /* memory segment types */ 77 #define SDT_MEMRO 16 /* memory read only */ 78 #define SDT_MEMROA 17 /* memory read only accessed */ 79 #define SDT_MEMRW 18 /* memory read write */ 80 #define SDT_MEMRWA 19 /* memory read write accessed */ 81 #define SDT_MEMROD 20 /* memory read only expand dwn limit */ 82 #define SDT_MEMRODA 21 /* memory read only expand dwn limit accessed */ 83 #define SDT_MEMRWD 22 /* memory read write expand dwn limit */ 84 #define SDT_MEMRWDA 23 /* memory read write expand dwn limit acessed */ 85 #define SDT_MEME 24 /* memory execute only */ 86 #define SDT_MEMEA 25 /* memory execute only accessed */ 87 #define SDT_MEMER 26 /* memory execute read */ 88 #define SDT_MEMERA 27 /* memory execute read accessed */ 89 #define SDT_MEMEC 28 /* memory execute only conforming */ 90 #define SDT_MEMEAC 29 /* memory execute only accessed conforming */ 91 #define SDT_MEMERC 30 /* memory execute read conforming */ 92 #define SDT_MEMERAC 31 /* memory execute read accessed conforming */ 93 94 /* is memory segment descriptor pointer ? */ 95 #define ISMEMSDP(s) ((s->d_type) >= SDT_MEMRO && (s->d_type) <= SDT_MEMERAC) 96 97 /* is 286 gate descriptor pointer ? */ 98 #define IS286GDP(s) (((s->d_type) >= SDT_SYS286CGT \ 99 && (s->d_type) < SDT_SYS286TGT)) 100 101 /* is 386 gate descriptor pointer ? */ 102 #define IS386GDP(s) (((s->d_type) >= SDT_SYS386CGT \ 103 && (s->d_type) < SDT_SYS386TGT)) 104 105 /* is gate descriptor pointer ? */ 106 #define ISGDP(s) (IS286GDP(s) || IS386GDP(s)) 107 108 /* is segment descriptor pointer ? */ 109 #define ISSDP(s) (ISMEMSDP(s) || !ISGDP(s)) 110 111 /* is system segment descriptor pointer ? */ 112 #define ISSYSSDP(s) (!ISMEMSDP(s) && !ISGDP(s)) 113 114 /* 115 * Software definitions are in this convenient format, 116 * which are translated into inconvenient segment descriptors 117 * when needed to be used by the 386 hardware 118 */ 119 120 struct soft_segment_descriptor { 121 unsigned ssd_base ; /* segment base address */ 122 unsigned ssd_limit ; /* segment extent */ 123 unsigned ssd_type:5 ; /* segment type */ 124 unsigned ssd_dpl:2 ; /* segment descriptor priority level */ 125 unsigned ssd_p:1 ; /* segment descriptor present */ 126 unsigned ssd_xx:4 ; /* unused */ 127 unsigned ssd_xx1:2 ; /* unused */ 128 unsigned ssd_def32:1 ; /* default 32 vs 16 bit size */ 129 unsigned ssd_gran:1 ; /* limit granularity (byte/page units)*/ 130 }; 131 132 extern ssdtosd() ; /* to decode a ssd */ 133 extern sdtossd() ; /* to encode a sd */ 134 135 /* 136 * region descriptors, used to load gdt/idt tables before segments yet exist 137 */ 138 struct region_descriptor { 139 unsigned rd_limit:16 ; /* segment extent */ 140 char *rd_base; /* base address */ 141 }; 142 143