]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/seg.h
xnu-792.17.14.tar.gz
[apple/xnu.git] / osfmk / i386 / seg.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_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 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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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 */
56 /*
57 */
58
59 #ifndef _I386_SEG_H_
60 #define _I386_SEG_H_
61
62 #include <mach_kdb.h>
63 #include <stdint.h>
64 #include <architecture/i386/sel.h>
65
66 /*
67 * i386 segmentation.
68 */
69
70 static inline uint16_t
71 sel_to_selector(sel_t sel)
72 {
73 union {
74 sel_t sel;
75 uint16_t selector;
76 } tconv;
77
78 tconv.sel = sel;
79
80 return (tconv.selector);
81 }
82
83 static inline sel_t
84 selector_to_sel(uint16_t selector)
85 {
86 union {
87 uint16_t selector;
88 sel_t sel;
89 } tconv;
90
91 tconv.selector = selector;
92
93 return (tconv.sel);
94 }
95
96 #define LDTSZ 15 /* size of the kernel ldt in entries*/
97
98 #if MACH_KDB
99 #ifdef MACH_BSD
100 #define GDTSZ 14
101 #else
102 #define GDTSZ 11
103 #endif
104 #else /* MACH_KDB */
105 #ifdef MACH_BSD
106 #define GDTSZ 13
107 #else
108 #define GDTSZ 10
109 #endif
110 #endif /* MACH_KDB */
111
112 /*
113 * Interrupt table is always 256 entries long.
114 */
115 #define IDTSZ 256
116
117 #ifndef __ASSEMBLER__
118
119 #include <sys/cdefs.h>
120
121 /*
122 * Real segment descriptor.
123 */
124 struct real_descriptor {
125 unsigned int limit_low:16, /* limit 0..15 */
126 base_low:16, /* base 0..15 */
127 base_med:8, /* base 16..23 */
128 access:8, /* access byte */
129 limit_high:4, /* limit 16..19 */
130 granularity:4, /* granularity */
131 base_high:8; /* base 24..31 */
132 };
133
134 struct real_gate {
135 unsigned int offset_low:16, /* offset 0..15 */
136 selector:16,
137 word_count:8,
138 access:8,
139 offset_high:16; /* offset 16..31 */
140 };
141
142 /*
143 * We build descriptors and gates in a 'fake' format to let the
144 * fields be contiguous. We shuffle them into the real format
145 * at runtime.
146 */
147 struct fake_descriptor {
148 unsigned int offset:32; /* offset */
149 unsigned int lim_or_seg:20; /* limit */
150 /* or segment, for gate */
151 unsigned int size_or_wdct:4; /* size/granularity */
152 /* word count, for gate */
153 unsigned int access:8; /* access */
154 };
155
156 /*
157 * Boot-time data for master (or only) CPU
158 */
159 extern struct fake_descriptor idt[IDTSZ];
160 extern struct fake_descriptor gdt[GDTSZ];
161 extern struct fake_descriptor ldt[LDTSZ];
162 extern struct i386_tss ktss;
163
164 __BEGIN_DECLS
165
166 #if MACH_KDB
167 extern char db_stack_store[];
168 extern char db_task_stack_store[];
169 extern struct i386_tss dbtss;
170 extern void db_task_start(void);
171 #endif /* MACH_KDB */
172
173 __END_DECLS
174
175 #endif /*__ASSEMBLER__*/
176
177 #define SZ_32 0x4 /* 32-bit segment */
178 #define SZ_G 0x8 /* 4K limit field */
179
180 #define ACC_A 0x01 /* accessed */
181 #define ACC_TYPE 0x1e /* type field: */
182
183 #define ACC_TYPE_SYSTEM 0x00 /* system descriptors: */
184
185 #define ACC_LDT 0x02 /* LDT */
186 #define ACC_CALL_GATE_16 0x04 /* 16-bit call gate */
187 #define ACC_TASK_GATE 0x05 /* task gate */
188 #define ACC_TSS 0x09 /* task segment */
189 #define ACC_CALL_GATE 0x0c /* call gate */
190 #define ACC_INTR_GATE 0x0e /* interrupt gate */
191 #define ACC_TRAP_GATE 0x0f /* trap gate */
192
193 #define ACC_TSS_BUSY 0x02 /* task busy */
194
195 #define ACC_TYPE_USER 0x10 /* user descriptors */
196
197 #define ACC_DATA 0x10 /* data */
198 #define ACC_DATA_W 0x12 /* data, writable */
199 #define ACC_DATA_E 0x14 /* data, expand-down */
200 #define ACC_DATA_EW 0x16 /* data, expand-down,
201 writable */
202 #define ACC_CODE 0x18 /* code */
203 #define ACC_CODE_R 0x1a /* code, readable */
204 #define ACC_CODE_C 0x1c /* code, conforming */
205 #define ACC_CODE_CR 0x1e /* code, conforming,
206 readable */
207 #define ACC_PL 0x60 /* access rights: */
208 #define ACC_PL_K 0x00 /* kernel access only */
209 #define ACC_PL_U 0x60 /* user access */
210 #define ACC_P 0x80 /* segment present */
211
212 /*
213 * Components of a selector
214 */
215 #define SEL_LDTS 0x04 /* local selector */
216 #define SEL_PL 0x03 /* privilege level: */
217 #define SEL_PL_K 0x00 /* kernel selector */
218 #define SEL_PL_U 0x03 /* user selector */
219
220 /*
221 * Convert selector to descriptor table index.
222 */
223 #define sel_idx(sel) (selector_to_sel(sel).index)
224
225 #define NULL_SEG 0
226
227 /*
228 * User descriptors for MACH - 32-bit flat address space
229 */
230 #define USER_SCALL 0x07 /* system call gate */
231 #define USER_RPC 0x0f /* mach rpc call gate */
232 #define USER_CS 0x17 /* user code segment */
233 #define USER_DS 0x1f /* user data segment */
234 #define USER_CTHREAD 0x27 /* user cthread area */
235 #define USER_SETTABLE 0x2f /* start of user settable ldt entries */
236 #define USLDTSZ 10 /* number of user settable entries */
237
238 /*
239 * Kernel descriptors for MACH - 32-bit flat address space.
240 */
241 #define KERNEL_CS 0x08 /* kernel code */
242 #define KERNEL_DS 0x10 /* kernel data */
243 #define KERNEL_LDT 0x18 /* master LDT */
244 #define KERNEL_TSS 0x20 /* master TSS (uniprocessor) */
245 #ifdef MACH_BSD
246 #define BSD_SCALL_SEL 0x28 /* BSD System calls */
247 #define MK25_SCALL_SEL 0x30 /* MK25 System Calls */
248 #define MACHDEP_SCALL_SEL 0x38 /* Machdep SYstem calls */
249 #else
250 #define USER_LDT 0x28 /* place for per-thread LDT */
251 #define USER_TSS 0x30 /* place for per-thread TSS
252 that holds IO bitmap */
253 #define FPE_CS 0x38 /* floating-point emulator code */
254 #endif
255 #define USER_FPREGS 0x40 /* user-mode access to saved
256 floating-point registers */
257 #define CPU_DATA_GS 0x48 /* per-cpu data */
258
259 #ifdef MACH_BSD
260 #define USER_LDT 0x58
261 #define USER_TSS 0x60
262 #define FPE_CS 0x68
263 #endif
264
265 #if MACH_KDB
266 #define DEBUG_TSS 0x50 /* debug TSS (uniprocessor) */
267 #endif
268
269 #endif /* _I386_SEG_H_ */