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