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