2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990 Carnegie Mellon University
28 * All Rights Reserved.
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.
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.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
58 #include <mach/vm_types.h>
59 #include <architecture/i386/sel.h>
65 static inline uint16_t
66 sel_to_selector(sel_t sel
)
75 return (tconv
.selector
);
79 selector_to_sel(uint16_t selector
)
86 tconv
.selector
= selector
;
91 #define LDTSZ 8192 /* size of the kernel ldt in entries */
92 #define LDTSZ_MIN 17 /* kernel ldt entries used by the system */
101 * Interrupt table is always 256 entries long.
105 #ifndef __ASSEMBLER__
107 #include <sys/cdefs.h>
110 * Real segment descriptor.
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 */
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 */
133 uint32_t offset_low
:16, /* offset 0..15 */
137 offset_high
:16; /* offset 16..31 */
140 uint32_t offset_low16
:16, /* offset 0..15 */
145 offset_high16
:16, /* offset 16..31 */
146 offset_top32
:32, /* offset 32..63 */
147 reserved32
:32; /* reserved/zero */
151 * We build descriptors and gates in a 'fake' format to let the
152 * fields be contiguous. We shuffle them into the real format
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 */
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 */
169 uint32_t access
:8; /* access */
170 uint32_t reserved
:32; /* reserved/zero */
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)
177 * Boot-time data for master (or only) CPU
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
;
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
;
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);
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);
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 */
211 #endif /*__ASSEMBLER__*/
213 #define SZ_64 0x2 /* 64-bit segment */
214 #define SZ_32 0x4 /* 32-bit segment */
215 #define SZ_G 0x8 /* 4K limit field */
217 #define ACC_A 0x01 /* accessed */
218 #define ACC_TYPE 0x1e /* type field: */
220 #define ACC_TYPE_SYSTEM 0x00 /* system descriptors: */
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 */
230 #define ACC_TSS_BUSY 0x02 /* task busy */
232 #define ACC_TYPE_USER 0x10 /* user descriptors */
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,
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,
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 */
250 * Components of a selector
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 */
258 * Convert selector to descriptor table index.
260 #define sel_idx(sel) (selector_to_sel(sel).index)
261 #define SEL_TO_INDEX(s) ((s)>>3)
266 * User descriptors for MACH - 32-bit flat address space
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 */
285 * Kernel descriptors for MACH - 32-bit flat address space.
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 */
294 #define MC_TSS 0x38 /* machine-check handler TSS */
296 #define CPU_DATA_GS 0x48 /* per-cpu data */
298 #define DF_TSS 0x50 /* double-fault handler TSS */
300 #define USER_LDT 0x58
301 #define USER_TSS 0x60
304 #define USER_WINDOW_SEL 0x70 /* window for copyin/copyout */
305 #define PHYS_WINDOW_SEL 0x78 /* window for copyin/copyout */
307 #define KERNEL64_CS 0x80 /* kernel 64-bit code */
308 #define KERNEL64_SS 0x88 /* kernel 64-bit (syscall) stack */
311 #define DEBUG_TSS 0x90 /* debug TSS (uniprocessor) */
314 struct __gdt_desc_struct
{
316 unsigned long address
__attribute__((packed
));
318 } __attribute__ ((packed
));
320 struct __idt_desc_struct
{
322 unsigned long address
__attribute__((packed
));
324 } __attribute__ ((packed
));
327 #endif /* _I386_SEG_H_ */