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