]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/seg.h
xnu-1228.9.59.tar.gz
[apple/xnu.git] / osfmk / i386 / seg.h
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, 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_
91447636 58
1c79356b 59#include <mach_kdb.h>
2d21ac55 60#ifndef ASSEMBLER
91447636 61#include <stdint.h>
0c530ab8 62#include <mach/vm_types.h>
91447636 63#include <architecture/i386/sel.h>
1c79356b
A
64
65/*
66 * i386 segmentation.
67 */
68
91447636
A
69static inline uint16_t
70sel_to_selector(sel_t sel)
71{
72 union {
73 sel_t sel;
74 uint16_t selector;
75 } tconv;
76
77 tconv.sel = sel;
78
79 return (tconv.selector);
80}
81
82static inline sel_t
83selector_to_sel(uint16_t selector)
84{
85 union {
86 uint16_t selector;
87 sel_t sel;
88 } tconv;
89
90 tconv.selector = selector;
91
92 return (tconv.sel);
93}
94
0c530ab8 95#define LDTSZ 8192 /* size of the kernel ldt in entries */
2d21ac55
A
96#define LDTSZ_MIN SEL_TO_INDEX(USER_SETTABLE)
97 /* kernel ldt entries */
91447636
A
98
99#if MACH_KDB
0c530ab8 100#define GDTSZ 19
6601e61a 101#else
0c530ab8 102#define GDTSZ 18
6601e61a 103#endif
91447636
A
104
105/*
106 * Interrupt table is always 256 entries long.
107 */
108#define IDTSZ 256
109
91447636
A
110#include <sys/cdefs.h>
111
1c79356b
A
112/*
113 * Real segment descriptor.
114 */
115struct real_descriptor {
0c530ab8 116 uint32_t limit_low:16, /* limit 0..15 */
1c79356b
A
117 base_low:16, /* base 0..15 */
118 base_med:8, /* base 16..23 */
119 access:8, /* access byte */
120 limit_high:4, /* limit 16..19 */
121 granularity:4, /* granularity */
122 base_high:8; /* base 24..31 */
123};
0c530ab8
A
124struct real_descriptor64 {
125 uint32_t limit_low16:16, /* limit 0..15 */
126 base_low16:16, /* base 0..15 */
127 base_med8:8, /* base 16..23 */
128 access8:8, /* access byte */
129 limit_high4:4, /* limit 16..19 */
130 granularity4:4, /* granularity */
131 base_high8:8, /* base 24..31 */
132 base_top32:32, /* base 32..63 */
133 reserved32:32; /* reserved/zero */
134};
1c79356b 135struct real_gate {
0c530ab8 136 uint32_t offset_low:16, /* offset 0..15 */
1c79356b
A
137 selector:16,
138 word_count:8,
139 access:8,
140 offset_high:16; /* offset 16..31 */
141};
0c530ab8
A
142struct real_gate64 {
143 uint32_t offset_low16:16, /* offset 0..15 */
144 selector16:16,
145 IST:3,
146 zeroes5:5,
147 access8:8,
148 offset_high16:16, /* offset 16..31 */
149 offset_top32:32, /* offset 32..63 */
150 reserved32:32; /* reserved/zero */
151};
1c79356b
A
152
153/*
154 * We build descriptors and gates in a 'fake' format to let the
155 * fields be contiguous. We shuffle them into the real format
156 * at runtime.
157 */
158struct fake_descriptor {
0c530ab8
A
159 uint32_t offset:32; /* offset */
160 uint32_t lim_or_seg:20; /* limit */
1c79356b 161 /* or segment, for gate */
0c530ab8 162 uint32_t size_or_wdct:4; /* size/granularity */
1c79356b 163 /* word count, for gate */
0c530ab8 164 uint32_t access:8; /* access */
4452a7af 165};
0c530ab8
A
166struct fake_descriptor64 {
167 uint32_t offset[2]; /* offset [0..31,32..63] */
168 uint32_t lim_or_seg:20; /* limit */
169 /* or segment, for gate */
170 uint32_t size_or_IST:4; /* size/granularity */
171 /* IST for gates */
172 uint32_t access:8; /* access */
173 uint32_t reserved:32; /* reserved/zero */
174};
175#define FAKE_UBER64(addr32) { (uint32_t) (addr32), KERNEL_UBER_BASE_HI32 }
176#define FAKE_COMPAT(addr32) { (uint32_t) (addr32), 0x0 }
cf7d32b8 177#define UBER64(addr32) ((addr64_t) (uintptr_t)addr32 + KERNEL_UBER_BASE)
91447636
A
178
179/*
180 * Boot-time data for master (or only) CPU
181 */
0c530ab8
A
182extern struct fake_descriptor master_idt[IDTSZ];
183extern struct fake_descriptor master_gdt[GDTSZ];
184extern struct fake_descriptor master_ldt[LDTSZ];
185extern struct i386_tss master_ktss;
186extern struct sysenter_stack master_sstk;
187
188extern struct fake_descriptor64 master_idt64[IDTSZ];
189extern struct fake_descriptor64 kernel_ldt_desc64;
190extern struct fake_descriptor64 kernel_tss_desc64;
191extern struct x86_64_tss master_ktss64;
91447636
A
192
193__BEGIN_DECLS
194
0c530ab8
A
195extern char df_task_stack[];
196extern char df_task_stack_end[];
197extern struct i386_tss master_dftss;
198extern void df_task_start(void);
199
200extern char mc_task_stack[];
201extern char mc_task_stack_end[];
202extern struct i386_tss master_mctss;
203extern void mc_task_start(void);
204
91447636
A
205#if MACH_KDB
206extern char db_stack_store[];
207extern char db_task_stack_store[];
0c530ab8 208extern struct i386_tss master_dbtss;
91447636
A
209extern void db_task_start(void);
210#endif /* MACH_KDB */
211
212__END_DECLS
213
1c79356b
A
214#endif /*__ASSEMBLER__*/
215
0c530ab8 216#define SZ_64 0x2 /* 64-bit segment */
1c79356b
A
217#define SZ_32 0x4 /* 32-bit segment */
218#define SZ_G 0x8 /* 4K limit field */
219
220#define ACC_A 0x01 /* accessed */
221#define ACC_TYPE 0x1e /* type field: */
222
223#define ACC_TYPE_SYSTEM 0x00 /* system descriptors: */
224
225#define ACC_LDT 0x02 /* LDT */
226#define ACC_CALL_GATE_16 0x04 /* 16-bit call gate */
227#define ACC_TASK_GATE 0x05 /* task gate */
228#define ACC_TSS 0x09 /* task segment */
229#define ACC_CALL_GATE 0x0c /* call gate */
230#define ACC_INTR_GATE 0x0e /* interrupt gate */
231#define ACC_TRAP_GATE 0x0f /* trap gate */
232
233#define ACC_TSS_BUSY 0x02 /* task busy */
234
235#define ACC_TYPE_USER 0x10 /* user descriptors */
236
237#define ACC_DATA 0x10 /* data */
238#define ACC_DATA_W 0x12 /* data, writable */
239#define ACC_DATA_E 0x14 /* data, expand-down */
240#define ACC_DATA_EW 0x16 /* data, expand-down,
241 writable */
242#define ACC_CODE 0x18 /* code */
243#define ACC_CODE_R 0x1a /* code, readable */
244#define ACC_CODE_C 0x1c /* code, conforming */
245#define ACC_CODE_CR 0x1e /* code, conforming,
246 readable */
247#define ACC_PL 0x60 /* access rights: */
248#define ACC_PL_K 0x00 /* kernel access only */
249#define ACC_PL_U 0x60 /* user access */
250#define ACC_P 0x80 /* segment present */
251
252/*
253 * Components of a selector
254 */
255#define SEL_LDTS 0x04 /* local selector */
256#define SEL_PL 0x03 /* privilege level: */
257#define SEL_PL_K 0x00 /* kernel selector */
258#define SEL_PL_U 0x03 /* user selector */
259
260/*
261 * Convert selector to descriptor table index.
262 */
91447636 263#define sel_idx(sel) (selector_to_sel(sel).index)
0c530ab8 264#define SEL_TO_INDEX(s) ((s)>>3)
91447636
A
265
266#define NULL_SEG 0
1c79356b
A
267
268/*
269 * User descriptors for MACH - 32-bit flat address space
270 */
0c530ab8
A
271#define SYSENTER_CS 0x07 /* sysenter kernel code segment */
272#define SYSENTER_DS 0x0f /* sysenter kernel data segment */
273#define USER_CS 0x17 /* user code segment
274 Must be SYSENTER_CS+16 for sysexit */
275/* Special case: sysenter with EFL_TF (trace bit) set - use iret not sysexit */
276#define SYSENTER_TF_CS (USER_CS|0x10000)
277#define USER_DS 0x1f /* user data segment
278 Must be SYSENTER_CS+24 for sysexit */
279#define USER64_CS 0x27 /* 64-bit user code segment
280 Must be USER_CS+16 for sysret */
281#define USER64_DS USER_DS /* 64-bit user data segment == 32-bit */
282#define SYSCALL_CS 0x2f /* 64-bit syscall pseudo-segment */
283#define USER_CTHREAD 0x37 /* user cthread area */
284#define USER_SETTABLE 0x3f /* start of user settable ldt entries */
91447636 285#define USLDTSZ 10 /* number of user settable entries */
1c79356b
A
286
287/*
288 * Kernel descriptors for MACH - 32-bit flat address space.
289 */
290#define KERNEL_CS 0x08 /* kernel code */
291#define KERNEL_DS 0x10 /* kernel data */
292#define KERNEL_LDT 0x18 /* master LDT */
0c530ab8
A
293#define KERNEL_LDT_2 0x20 /* master LDT expanded for 64-bit */
294#define KERNEL_TSS 0x28 /* master TSS */
295#define KERNEL_TSS_2 0x30 /* master TSS expanded for 64-bit */
296
297#define MC_TSS 0x38 /* machine-check handler TSS */
298
91447636 299#define CPU_DATA_GS 0x48 /* per-cpu data */
1c79356b 300
0c530ab8
A
301#define DF_TSS 0x50 /* double-fault handler TSS */
302
1c79356b
A
303#define USER_LDT 0x58
304#define USER_TSS 0x60
305#define FPE_CS 0x68
0c530ab8
A
306
307#define USER_WINDOW_SEL 0x70 /* window for copyin/copyout */
308#define PHYS_WINDOW_SEL 0x78 /* window for copyin/copyout */
309
310#define KERNEL64_CS 0x80 /* kernel 64-bit code */
311#define KERNEL64_SS 0x88 /* kernel 64-bit (syscall) stack */
1c79356b
A
312
313#if MACH_KDB
0c530ab8 314#define DEBUG_TSS 0x90 /* debug TSS (uniprocessor) */
1c79356b 315#endif
1c79356b 316
2d21ac55 317#ifndef __ASSEMBLER__
0c530ab8
A
318struct __gdt_desc_struct {
319 unsigned short size;
320 unsigned long address __attribute__((packed));
321 unsigned short pad;
322} __attribute__ ((packed));
323
324struct __idt_desc_struct {
325 unsigned short size;
326 unsigned long address __attribute__((packed));
327 unsigned short pad;
328} __attribute__ ((packed));
2d21ac55 329#endif /* __ASSEMBLER__ */
0c530ab8 330
1c79356b 331#endif /* _I386_SEG_H_ */