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