]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cpu_data.h
xnu-1456.1.26.tar.gz
[apple/xnu.git] / osfmk / i386 / cpu_data.h
1 /*
2 * Copyright (c) 2000-2008 Apple 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
33 #ifndef I386_CPU_DATA
34 #define I386_CPU_DATA
35
36 #include <mach_assert.h>
37
38 #include <kern/assert.h>
39 #include <kern/kern_types.h>
40 #include <kern/queue.h>
41 #include <kern/processor.h>
42 #include <kern/pms.h>
43 #include <pexpert/pexpert.h>
44 #include <mach/i386/thread_status.h>
45 #include <mach/i386/vm_param.h>
46 #include <i386/rtclock.h>
47 #include <i386/pmCPU.h>
48 #include <i386/cpu_topology.h>
49
50 #if CONFIG_VMX
51 #include <i386/vmx/vmx_cpu.h>
52 #endif
53
54 /*
55 * Data structures referenced (anonymously) from per-cpu data:
56 */
57 struct cpu_cons_buffer;
58 struct cpu_desc_table;
59 struct mca_state;
60
61
62 /*
63 * Data structures embedded in per-cpu data:
64 */
65 typedef struct rtclock_timer {
66 queue_head_t queue;
67 uint64_t deadline;
68 boolean_t is_set;
69 boolean_t has_expired;
70 } rtclock_timer_t;
71
72
73 #if defined(__i386__)
74
75 typedef struct {
76 struct i386_tss *cdi_ktss;
77 #if MACH_KDB
78 struct i386_tss *cdi_dbtss;
79 #endif /* MACH_KDB */
80 struct __attribute__((packed)) {
81 uint16_t size;
82 struct fake_descriptor *ptr;
83 } cdi_gdt, cdi_idt;
84 struct fake_descriptor *cdi_ldt;
85 vm_offset_t cdi_sstk;
86 } cpu_desc_index_t;
87
88 typedef enum {
89 TASK_MAP_32BIT, /* 32-bit, compatibility mode */
90 TASK_MAP_64BIT, /* 64-bit, separate address space */
91 TASK_MAP_64BIT_SHARED /* 64-bit, kernel-shared addr space */
92 } task_map_t;
93
94 #elif defined(__x86_64__)
95
96
97 typedef struct {
98 struct x86_64_tss *cdi_ktss;
99 #if MACH_KDB
100 struct x86_64_tss *cdi_dbtss;
101 #endif /* MACH_KDB */
102 struct __attribute__((packed)) {
103 uint16_t size;
104 void *ptr;
105 } cdi_gdt, cdi_idt;
106 struct fake_descriptor *cdi_ldt;
107 vm_offset_t cdi_sstk;
108 } cpu_desc_index_t;
109
110 typedef enum {
111 TASK_MAP_32BIT, /* 32-bit user, compatibility mode */
112 TASK_MAP_64BIT, /* 64-bit user thread, shared space */
113 } task_map_t;
114
115 #else
116 #error Unsupported architecture
117 #endif
118
119 /*
120 * This structure is used on entry into the (uber-)kernel on syscall from
121 * a 64-bit user. It contains the address of the machine state save area
122 * for the current thread and a temporary place to save the user's rsp
123 * before loading this address into rsp.
124 */
125 typedef struct {
126 addr64_t cu_isf; /* thread->pcb->iss.isf */
127 uint64_t cu_tmp; /* temporary scratch */
128 addr64_t cu_user_gs_base;
129 } cpu_uber_t;
130
131
132 /*
133 * Per-cpu data.
134 *
135 * Each processor has a per-cpu data area which is dereferenced through the
136 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
137 * using this, inlines provides single-instruction access to frequently used
138 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
139 * current_thread().
140 *
141 * Cpu data owned by another processor can be accessed using the
142 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
143 * pointers.
144 */
145 typedef struct cpu_data
146 {
147 struct cpu_data *cpu_this; /* pointer to myself */
148 thread_t cpu_active_thread;
149 void *cpu_int_state; /* interrupt state */
150 vm_offset_t cpu_active_stack; /* kernel stack base */
151 vm_offset_t cpu_kernel_stack; /* kernel stack top */
152 vm_offset_t cpu_int_stack_top;
153 int cpu_preemption_level;
154 int cpu_simple_lock_count;
155 int cpu_interrupt_level;
156 int cpu_number; /* Logical CPU */
157 int cpu_phys_number; /* Physical CPU */
158 cpu_id_t cpu_id; /* Platform Expert */
159 int cpu_signals; /* IPI events */
160 int cpu_mcount_off; /* mcount recursion */
161 ast_t cpu_pending_ast;
162 int cpu_type;
163 int cpu_subtype;
164 int cpu_threadtype;
165 int cpu_running;
166 rtclock_timer_t rtclock_timer;
167 boolean_t cpu_is64bit;
168 task_map_t cpu_task_map;
169 volatile addr64_t cpu_task_cr3;
170 volatile addr64_t cpu_active_cr3;
171 addr64_t cpu_kernel_cr3;
172 cpu_uber_t cpu_uber;
173 void *cpu_chud;
174 void *cpu_console_buf;
175 struct x86_lcpu lcpu;
176 struct processor *cpu_processor;
177 #if NCOPY_WINDOWS > 0
178 struct cpu_pmap *cpu_pmap;
179 #endif
180 struct cpu_desc_table *cpu_desc_tablep;
181 struct fake_descriptor *cpu_ldtp;
182 cpu_desc_index_t cpu_desc_index;
183 int cpu_ldt;
184 #ifdef MACH_KDB
185 /* XXX Untested: */
186 int cpu_db_pass_thru;
187 vm_offset_t cpu_db_stacks;
188 void *cpu_kdb_saved_state;
189 spl_t cpu_kdb_saved_ipl;
190 int cpu_kdb_is_slave;
191 int cpu_kdb_active;
192 #endif /* MACH_KDB */
193 boolean_t cpu_iflag;
194 boolean_t cpu_boot_complete;
195 int cpu_hibernate;
196
197 #if NCOPY_WINDOWS > 0
198 vm_offset_t cpu_copywindow_base;
199 uint64_t *cpu_copywindow_pdp;
200
201 vm_offset_t cpu_physwindow_base;
202 uint64_t *cpu_physwindow_ptep;
203 void *cpu_hi_iss;
204 #endif
205
206
207
208 volatile boolean_t cpu_tlb_invalid;
209 uint32_t cpu_hwIntCnt[256]; /* Interrupt counts */
210 uint64_t cpu_dr7; /* debug control register */
211 uint64_t cpu_int_event_time; /* intr entry/exit time */
212 #if CONFIG_VMX
213 vmx_cpu_t cpu_vmx; /* wonderful world of virtualization */
214 #endif
215 #if CONFIG_MCA
216 struct mca_state *cpu_mca_state; /* State at MC fault */
217 #endif
218 uint64_t cpu_uber_arg_store; /* Double mapped address
219 * of current thread's
220 * uu_arg array.
221 */
222 uint64_t cpu_uber_arg_store_valid; /* Double mapped
223 * address of pcb
224 * arg store
225 * validity flag.
226 */
227 rtc_nanotime_t *cpu_nanotime; /* Nanotime info */
228 thread_t csw_old_thread;
229 thread_t csw_new_thread;
230 } cpu_data_t;
231
232 extern cpu_data_t *cpu_data_ptr[];
233 extern cpu_data_t cpu_data_master;
234
235 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
236 #ifndef offsetof
237 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
238 #endif /* offsetof */
239 #define CPU_DATA_GET(member,type) \
240 type ret; \
241 __asm__ volatile ("mov %%gs:%P1,%0" \
242 : "=r" (ret) \
243 : "i" (offsetof(cpu_data_t,member))); \
244 return ret;
245
246 /*
247 * Everyone within the osfmk part of the kernel can use the fast
248 * inline versions of these routines. Everyone outside, must call
249 * the real thing,
250 */
251 static inline thread_t
252 get_active_thread(void)
253 {
254 CPU_DATA_GET(cpu_active_thread,thread_t)
255 }
256 #define current_thread_fast() get_active_thread()
257 #define current_thread() current_thread_fast()
258
259 #if defined(__i386__)
260 static inline boolean_t
261 get_is64bit(void)
262 {
263 CPU_DATA_GET(cpu_is64bit, boolean_t)
264 }
265 #define cpu_mode_is64bit() get_is64bit()
266 #elif defined(__x86_64__)
267 #define cpu_mode_is64bit() TRUE
268 #endif
269
270 static inline int
271 get_preemption_level(void)
272 {
273 CPU_DATA_GET(cpu_preemption_level,int)
274 }
275 static inline int
276 get_simple_lock_count(void)
277 {
278 CPU_DATA_GET(cpu_simple_lock_count,int)
279 }
280 static inline int
281 get_interrupt_level(void)
282 {
283 CPU_DATA_GET(cpu_interrupt_level,int)
284 }
285 static inline int
286 get_cpu_number(void)
287 {
288 CPU_DATA_GET(cpu_number,int)
289 }
290 static inline int
291 get_cpu_phys_number(void)
292 {
293 CPU_DATA_GET(cpu_phys_number,int)
294 }
295
296
297 static inline void
298 disable_preemption(void)
299 {
300 __asm__ volatile ("incl %%gs:%P0"
301 :
302 : "i" (offsetof(cpu_data_t, cpu_preemption_level)));
303 }
304
305 static inline void
306 enable_preemption(void)
307 {
308 assert(get_preemption_level() > 0);
309
310 __asm__ volatile ("decl %%gs:%P0 \n\t"
311 "jne 1f \n\t"
312 "call _kernel_preempt_check \n\t"
313 "1:"
314 : /* no outputs */
315 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
316 : "eax", "ecx", "edx", "cc", "memory");
317 }
318
319 static inline void
320 enable_preemption_no_check(void)
321 {
322 assert(get_preemption_level() > 0);
323
324 __asm__ volatile ("decl %%gs:%P0"
325 : /* no outputs */
326 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
327 : "cc", "memory");
328 }
329
330 static inline void
331 mp_disable_preemption(void)
332 {
333 disable_preemption();
334 }
335
336 static inline void
337 mp_enable_preemption(void)
338 {
339 enable_preemption();
340 }
341
342 static inline void
343 mp_enable_preemption_no_check(void)
344 {
345 enable_preemption_no_check();
346 }
347
348 static inline cpu_data_t *
349 current_cpu_datap(void)
350 {
351 CPU_DATA_GET(cpu_this, cpu_data_t *);
352 }
353
354 static inline cpu_data_t *
355 cpu_datap(int cpu)
356 {
357 assert(cpu_data_ptr[cpu]);
358 return cpu_data_ptr[cpu];
359 }
360
361 extern cpu_data_t *cpu_data_alloc(boolean_t is_boot_cpu);
362
363 #endif /* I386_CPU_DATA */