]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/seg.h
eca69d6f39ed263b59f96048202adfd578e80c55
[apple/xnu.git] / osfmk / i386 / seg.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52
53 #ifndef _I386_SEG_H_
54 #define _I386_SEG_H_
55
56 #include <mach_kdb.h>
57 #include <stdint.h>
58 #include <mach/vm_types.h>
59 #include <architecture/i386/sel.h>
60
61 /*
62 * i386 segmentation.
63 */
64
65 static inline uint16_t
66 sel_to_selector(sel_t sel)
67 {
68 union {
69 sel_t sel;
70 uint16_t selector;
71 } tconv;
72
73 tconv.sel = sel;
74
75 return (tconv.selector);
76 }
77
78 static inline sel_t
79 selector_to_sel(uint16_t selector)
80 {
81 union {
82 uint16_t selector;
83 sel_t sel;
84 } tconv;
85
86 tconv.selector = selector;
87
88 return (tconv.sel);
89 }
90
91 #define LDTSZ 8192 /* size of the kernel ldt in entries */
92 #define LDTSZ_MIN 17 /* kernel ldt entries used by the system */
93
94 #if MACH_KDB
95 #define GDTSZ 19
96 #else
97 #define GDTSZ 18
98 #endif
99
100 /*
101 * Interrupt table is always 256 entries long.
102 */
103 #define IDTSZ 256
104
105 #ifndef __ASSEMBLER__
106
107 #include <sys/cdefs.h>
108
109 /*
110 * Real segment descriptor.
111 */
112 struct real_descriptor {
113 uint32_t limit_low:16, /* limit 0..15 */
114 base_low:16, /* base 0..15 */
115 base_med:8, /* base 16..23 */
116 access:8, /* access byte */
117 limit_high:4, /* limit 16..19 */
118 granularity:4, /* granularity */
119 base_high:8; /* base 24..31 */
120 };
121 struct real_descriptor64 {
122 uint32_t limit_low16:16, /* limit 0..15 */
123 base_low16:16, /* base 0..15 */
124 base_med8:8, /* base 16..23 */
125 access8:8, /* access byte */
126 limit_high4:4, /* limit 16..19 */
127 granularity4:4, /* granularity */
128 base_high8:8, /* base 24..31 */
129 base_top32:32, /* base 32..63 */
130 reserved32:32; /* reserved/zero */
131 };
132 struct real_gate {
133 uint32_t offset_low:16, /* offset 0..15 */
134 selector:16,
135 word_count:8,
136 access:8,
137 offset_high:16; /* offset 16..31 */
138 };
139 struct real_gate64 {
140 uint32_t offset_low16:16, /* offset 0..15 */
141 selector16:16,
142 IST:3,
143 zeroes5:5,
144 access8:8,
145 offset_high16:16, /* offset 16..31 */
146 offset_top32:32, /* offset 32..63 */
147 reserved32:32; /* reserved/zero */
148 };
149
150 /*
151 * We build descriptors and gates in a 'fake' format to let the
152 * fields be contiguous. We shuffle them into the real format
153 * at runtime.
154 */
155 struct fake_descriptor {
156 uint32_t offset:32; /* offset */
157 uint32_t lim_or_seg:20; /* limit */
158 /* or segment, for gate */
159 uint32_t size_or_wdct:4; /* size/granularity */
160 /* word count, for gate */
161 uint32_t access:8; /* access */
162 };
163 struct fake_descriptor64 {
164 uint32_t offset[2]; /* offset [0..31,32..63] */
165 uint32_t lim_or_seg:20; /* limit */
166 /* or segment, for gate */
167 uint32_t size_or_IST:4; /* size/granularity */
168 /* IST for gates */
169 uint32_t access:8; /* access */
170 uint32_t reserved:32; /* reserved/zero */
171 };
172 #define FAKE_UBER64(addr32) { (uint32_t) (addr32), KERNEL_UBER_BASE_HI32 }
173 #define FAKE_COMPAT(addr32) { (uint32_t) (addr32), 0x0 }
174 #define UBER64(addr32) ((addr64_t) addr32 + KERNEL_UBER_BASE)
175
176 /*
177 * Boot-time data for master (or only) CPU
178 */
179 extern struct fake_descriptor master_idt[IDTSZ];
180 extern struct fake_descriptor master_gdt[GDTSZ];
181 extern struct fake_descriptor master_ldt[LDTSZ];
182 extern struct i386_tss master_ktss;
183 extern struct sysenter_stack master_sstk;
184
185 extern struct fake_descriptor64 master_idt64[IDTSZ];
186 extern struct fake_descriptor64 kernel_ldt_desc64;
187 extern struct fake_descriptor64 kernel_tss_desc64;
188 extern struct x86_64_tss master_ktss64;
189
190 __BEGIN_DECLS
191
192 extern char df_task_stack[];
193 extern char df_task_stack_end[];
194 extern struct i386_tss master_dftss;
195 extern void df_task_start(void);
196
197 extern char mc_task_stack[];
198 extern char mc_task_stack_end[];
199 extern struct i386_tss master_mctss;
200 extern void mc_task_start(void);
201
202 #if MACH_KDB
203 extern char db_stack_store[];
204 extern char db_task_stack_store[];
205 extern struct i386_tss master_dbtss;
206 extern void db_task_start(void);
207 #endif /* MACH_KDB */
208
209 __END_DECLS
210
211 #endif /*__ASSEMBLER__*/
212
213 #define SZ_64 0x2 /* 64-bit segment */
214 #define SZ_32 0x4 /* 32-bit segment */
215 #define SZ_G 0x8 /* 4K limit field */
216
217 #define ACC_A 0x01 /* accessed */
218 #define ACC_TYPE 0x1e /* type field: */
219
220 #define ACC_TYPE_SYSTEM 0x00 /* system descriptors: */
221
222 #define ACC_LDT 0x02 /* LDT */
223 #define ACC_CALL_GATE_16 0x04 /* 16-bit call gate */
224 #define ACC_TASK_GATE 0x05 /* task gate */
225 #define ACC_TSS 0x09 /* task segment */
226 #define ACC_CALL_GATE 0x0c /* call gate */
227 #define ACC_INTR_GATE 0x0e /* interrupt gate */
228 #define ACC_TRAP_GATE 0x0f /* trap gate */
229
230 #define ACC_TSS_BUSY 0x02 /* task busy */
231
232 #define ACC_TYPE_USER 0x10 /* user descriptors */
233
234 #define ACC_DATA 0x10 /* data */
235 #define ACC_DATA_W 0x12 /* data, writable */
236 #define ACC_DATA_E 0x14 /* data, expand-down */
237 #define ACC_DATA_EW 0x16 /* data, expand-down,
238 writable */
239 #define ACC_CODE 0x18 /* code */
240 #define ACC_CODE_R 0x1a /* code, readable */
241 #define ACC_CODE_C 0x1c /* code, conforming */
242 #define ACC_CODE_CR 0x1e /* code, conforming,
243 readable */
244 #define ACC_PL 0x60 /* access rights: */
245 #define ACC_PL_K 0x00 /* kernel access only */
246 #define ACC_PL_U 0x60 /* user access */
247 #define ACC_P 0x80 /* segment present */
248
249 /*
250 * Components of a selector
251 */
252 #define SEL_LDTS 0x04 /* local selector */
253 #define SEL_PL 0x03 /* privilege level: */
254 #define SEL_PL_K 0x00 /* kernel selector */
255 #define SEL_PL_U 0x03 /* user selector */
256
257 /*
258 * Convert selector to descriptor table index.
259 */
260 #define sel_idx(sel) (selector_to_sel(sel).index)
261 #define SEL_TO_INDEX(s) ((s)>>3)
262
263 #define NULL_SEG 0
264
265 /*
266 * User descriptors for MACH - 32-bit flat address space
267 */
268 #define SYSENTER_CS 0x07 /* sysenter kernel code segment */
269 #define SYSENTER_DS 0x0f /* sysenter kernel data segment */
270 #define USER_CS 0x17 /* user code segment
271 Must be SYSENTER_CS+16 for sysexit */
272 /* Special case: sysenter with EFL_TF (trace bit) set - use iret not sysexit */
273 #define SYSENTER_TF_CS (USER_CS|0x10000)
274 #define USER_DS 0x1f /* user data segment
275 Must be SYSENTER_CS+24 for sysexit */
276 #define USER64_CS 0x27 /* 64-bit user code segment
277 Must be USER_CS+16 for sysret */
278 #define USER64_DS USER_DS /* 64-bit user data segment == 32-bit */
279 #define SYSCALL_CS 0x2f /* 64-bit syscall pseudo-segment */
280 #define USER_CTHREAD 0x37 /* user cthread area */
281 #define USER_SETTABLE 0x3f /* start of user settable ldt entries */
282 #define USLDTSZ 10 /* number of user settable entries */
283
284 /*
285 * Kernel descriptors for MACH - 32-bit flat address space.
286 */
287 #define KERNEL_CS 0x08 /* kernel code */
288 #define KERNEL_DS 0x10 /* kernel data */
289 #define KERNEL_LDT 0x18 /* master LDT */
290 #define KERNEL_LDT_2 0x20 /* master LDT expanded for 64-bit */
291 #define KERNEL_TSS 0x28 /* master TSS */
292 #define KERNEL_TSS_2 0x30 /* master TSS expanded for 64-bit */
293
294 #define MC_TSS 0x38 /* machine-check handler TSS */
295
296 #define CPU_DATA_GS 0x48 /* per-cpu data */
297
298 #define DF_TSS 0x50 /* double-fault handler TSS */
299
300 #define USER_LDT 0x58
301 #define USER_TSS 0x60
302 #define FPE_CS 0x68
303
304 #define USER_WINDOW_SEL 0x70 /* window for copyin/copyout */
305 #define PHYS_WINDOW_SEL 0x78 /* window for copyin/copyout */
306
307 #define KERNEL64_CS 0x80 /* kernel 64-bit code */
308 #define KERNEL64_SS 0x88 /* kernel 64-bit (syscall) stack */
309
310 #if MACH_KDB
311 #define DEBUG_TSS 0x90 /* debug TSS (uniprocessor) */
312 #endif
313
314 struct __gdt_desc_struct {
315 unsigned short size;
316 unsigned long address __attribute__((packed));
317 unsigned short pad;
318 } __attribute__ ((packed));
319
320 struct __idt_desc_struct {
321 unsigned short size;
322 unsigned long address __attribute__((packed));
323 unsigned short pad;
324 } __attribute__ ((packed));
325
326
327 #endif /* _I386_SEG_H_ */