]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/seg.h
e146f1d4aed2fa5080653ca8ec474c570a75c449
[apple/xnu.git] / osfmk / i386 / seg.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * @OSF_COPYRIGHT@
25 */
26 /*
27 * Mach Operating System
28 * Copyright (c) 1991,1990 Carnegie Mellon University
29 * All Rights Reserved.
30 *
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
36 *
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 *
41 * Carnegie Mellon requests users of this software to return to
42 *
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
47 *
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
50 */
51 /*
52 */
53
54 #ifndef _I386_SEG_H_
55 #define _I386_SEG_H_
56
57 #include <mach_kdb.h>
58 #include <stdint.h>
59 #include <architecture/i386/sel.h>
60
61 /*
62 * i386 segmentation.
63 */
64
65 static inline uint16_t
66 sel_to_selector(sel_t sel)
67 {
68 union {
69 sel_t sel;
70 uint16_t selector;
71 } tconv;
72
73 tconv.sel = sel;
74
75 return (tconv.selector);
76 }
77
78 static inline sel_t
79 selector_to_sel(uint16_t selector)
80 {
81 union {
82 uint16_t selector;
83 sel_t sel;
84 } tconv;
85
86 tconv.selector = selector;
87
88 return (tconv.sel);
89 }
90
91 #define LDTSZ 15 /* size of the kernel ldt in entries*/
92
93 #if MACH_KDB
94 #ifdef MACH_BSD
95 #define GDTSZ 14
96 #else
97 #define GDTSZ 11
98 #endif
99 #else /* MACH_KDB */
100 #ifdef MACH_BSD
101 #define GDTSZ 13
102 #else
103 #define GDTSZ 10
104 #endif
105 #endif /* MACH_KDB */
106
107 /*
108 * Interrupt table is always 256 entries long.
109 */
110 #define IDTSZ 256
111
112 #ifndef __ASSEMBLER__
113
114 #include <sys/cdefs.h>
115
116 /*
117 * Real segment descriptor.
118 */
119 struct real_descriptor {
120 unsigned int limit_low:16, /* limit 0..15 */
121 base_low:16, /* base 0..15 */
122 base_med:8, /* base 16..23 */
123 access:8, /* access byte */
124 limit_high:4, /* limit 16..19 */
125 granularity:4, /* granularity */
126 base_high:8; /* base 24..31 */
127 };
128
129 struct real_gate {
130 unsigned int offset_low:16, /* offset 0..15 */
131 selector:16,
132 word_count:8,
133 access:8,
134 offset_high:16; /* offset 16..31 */
135 };
136
137 /*
138 * We build descriptors and gates in a 'fake' format to let the
139 * fields be contiguous. We shuffle them into the real format
140 * at runtime.
141 */
142 struct fake_descriptor {
143 unsigned int offset:32; /* offset */
144 unsigned int lim_or_seg:20; /* limit */
145 /* or segment, for gate */
146 unsigned int size_or_wdct:4; /* size/granularity */
147 /* word count, for gate */
148 unsigned int access:8; /* access */
149 };
150
151 /*
152 * Boot-time data for master (or only) CPU
153 */
154 extern struct fake_descriptor idt[IDTSZ];
155 extern struct fake_descriptor gdt[GDTSZ];
156 extern struct fake_descriptor ldt[LDTSZ];
157 extern struct i386_tss ktss;
158
159 __BEGIN_DECLS
160
161 #if MACH_KDB
162 extern char db_stack_store[];
163 extern char db_task_stack_store[];
164 extern struct i386_tss dbtss;
165 extern void db_task_start(void);
166 #endif /* MACH_KDB */
167
168 __END_DECLS
169
170 #endif /*__ASSEMBLER__*/
171
172 #define SZ_32 0x4 /* 32-bit segment */
173 #define SZ_G 0x8 /* 4K limit field */
174
175 #define ACC_A 0x01 /* accessed */
176 #define ACC_TYPE 0x1e /* type field: */
177
178 #define ACC_TYPE_SYSTEM 0x00 /* system descriptors: */
179
180 #define ACC_LDT 0x02 /* LDT */
181 #define ACC_CALL_GATE_16 0x04 /* 16-bit call gate */
182 #define ACC_TASK_GATE 0x05 /* task gate */
183 #define ACC_TSS 0x09 /* task segment */
184 #define ACC_CALL_GATE 0x0c /* call gate */
185 #define ACC_INTR_GATE 0x0e /* interrupt gate */
186 #define ACC_TRAP_GATE 0x0f /* trap gate */
187
188 #define ACC_TSS_BUSY 0x02 /* task busy */
189
190 #define ACC_TYPE_USER 0x10 /* user descriptors */
191
192 #define ACC_DATA 0x10 /* data */
193 #define ACC_DATA_W 0x12 /* data, writable */
194 #define ACC_DATA_E 0x14 /* data, expand-down */
195 #define ACC_DATA_EW 0x16 /* data, expand-down,
196 writable */
197 #define ACC_CODE 0x18 /* code */
198 #define ACC_CODE_R 0x1a /* code, readable */
199 #define ACC_CODE_C 0x1c /* code, conforming */
200 #define ACC_CODE_CR 0x1e /* code, conforming,
201 readable */
202 #define ACC_PL 0x60 /* access rights: */
203 #define ACC_PL_K 0x00 /* kernel access only */
204 #define ACC_PL_U 0x60 /* user access */
205 #define ACC_P 0x80 /* segment present */
206
207 /*
208 * Components of a selector
209 */
210 #define SEL_LDTS 0x04 /* local selector */
211 #define SEL_PL 0x03 /* privilege level: */
212 #define SEL_PL_K 0x00 /* kernel selector */
213 #define SEL_PL_U 0x03 /* user selector */
214
215 /*
216 * Convert selector to descriptor table index.
217 */
218 #define sel_idx(sel) (selector_to_sel(sel).index)
219
220 #define NULL_SEG 0
221
222 /*
223 * User descriptors for MACH - 32-bit flat address space
224 */
225 #define USER_SCALL 0x07 /* system call gate */
226 #define USER_RPC 0x0f /* mach rpc call gate */
227 #define USER_CS 0x17 /* user code segment */
228 #define USER_DS 0x1f /* user data segment */
229 #define USER_CTHREAD 0x27 /* user cthread area */
230 #define USER_SETTABLE 0x2f /* start of user settable ldt entries */
231 #define USLDTSZ 10 /* number of user settable entries */
232
233 /*
234 * Kernel descriptors for MACH - 32-bit flat address space.
235 */
236 #define KERNEL_CS 0x08 /* kernel code */
237 #define KERNEL_DS 0x10 /* kernel data */
238 #define KERNEL_LDT 0x18 /* master LDT */
239 #define KERNEL_TSS 0x20 /* master TSS (uniprocessor) */
240 #ifdef MACH_BSD
241 #define BSD_SCALL_SEL 0x28 /* BSD System calls */
242 #define MK25_SCALL_SEL 0x30 /* MK25 System Calls */
243 #define MACHDEP_SCALL_SEL 0x38 /* Machdep SYstem calls */
244 #else
245 #define USER_LDT 0x28 /* place for per-thread LDT */
246 #define USER_TSS 0x30 /* place for per-thread TSS
247 that holds IO bitmap */
248 #define FPE_CS 0x38 /* floating-point emulator code */
249 #endif
250 #define USER_FPREGS 0x40 /* user-mode access to saved
251 floating-point registers */
252 #define CPU_DATA_GS 0x48 /* per-cpu data */
253
254 #ifdef MACH_BSD
255 #define USER_LDT 0x58
256 #define USER_TSS 0x60
257 #define FPE_CS 0x68
258 #endif
259
260 #if MACH_KDB
261 #define DEBUG_TSS 0x50 /* debug TSS (uniprocessor) */
262 #endif
263
264 #endif /* _I386_SEG_H_ */