]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cpu_data.h
63eb4446b6ee99443ece518ef700b51b67cb2108
[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 * Per-cpu data.
133 *
134 * Each processor has a per-cpu data area which is dereferenced through the
135 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
136 * using this, inlines provides single-instruction access to frequently used
137 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
138 * current_thread().
139 *
140 * Cpu data owned by another processor can be accessed using the
141 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
142 * pointers.
143 */
144 typedef struct cpu_data
145 {
146 struct cpu_data *cpu_this; /* pointer to myself */
147 thread_t cpu_active_thread;
148 void *cpu_int_state; /* interrupt state */
149 vm_offset_t cpu_active_stack; /* kernel stack base */
150 vm_offset_t cpu_kernel_stack; /* kernel stack top */
151 vm_offset_t cpu_int_stack_top;
152 int cpu_preemption_level;
153 int cpu_simple_lock_count;
154 int cpu_interrupt_level;
155 int cpu_number; /* Logical CPU */
156 int cpu_phys_number; /* Physical CPU */
157 cpu_id_t cpu_id; /* Platform Expert */
158 int cpu_signals; /* IPI events */
159 int cpu_prior_signals; /* Last set of events,
160 * debugging
161 */
162 int cpu_mcount_off; /* mcount recursion */
163 ast_t cpu_pending_ast;
164 int cpu_type;
165 int cpu_subtype;
166 int cpu_threadtype;
167 int cpu_running;
168 rtclock_timer_t rtclock_timer;
169 boolean_t cpu_is64bit;
170 task_map_t cpu_task_map;
171 volatile addr64_t cpu_task_cr3;
172 volatile addr64_t cpu_active_cr3;
173 addr64_t cpu_kernel_cr3;
174 cpu_uber_t cpu_uber;
175 void *cpu_chud;
176 void *cpu_console_buf;
177 struct x86_lcpu lcpu;
178 struct processor *cpu_processor;
179 #if NCOPY_WINDOWS > 0
180 struct cpu_pmap *cpu_pmap;
181 #endif
182 struct cpu_desc_table *cpu_desc_tablep;
183 struct fake_descriptor *cpu_ldtp;
184 cpu_desc_index_t cpu_desc_index;
185 int cpu_ldt;
186 #ifdef MACH_KDB
187 /* XXX Untested: */
188 int cpu_db_pass_thru;
189 vm_offset_t cpu_db_stacks;
190 void *cpu_kdb_saved_state;
191 spl_t cpu_kdb_saved_ipl;
192 int cpu_kdb_is_slave;
193 int cpu_kdb_active;
194 #endif /* MACH_KDB */
195 boolean_t cpu_iflag;
196 boolean_t cpu_boot_complete;
197 int cpu_hibernate;
198
199 #if NCOPY_WINDOWS > 0
200 vm_offset_t cpu_copywindow_base;
201 uint64_t *cpu_copywindow_pdp;
202
203 vm_offset_t cpu_physwindow_base;
204 uint64_t *cpu_physwindow_ptep;
205 void *cpu_hi_iss;
206 #endif
207
208
209
210 volatile boolean_t cpu_tlb_invalid;
211 uint32_t cpu_hwIntCnt[256]; /* Interrupt counts */
212 uint64_t cpu_dr7; /* debug control register */
213 uint64_t cpu_int_event_time; /* intr entry/exit time */
214 #if CONFIG_VMX
215 vmx_cpu_t cpu_vmx; /* wonderful world of virtualization */
216 #endif
217 #if CONFIG_MCA
218 struct mca_state *cpu_mca_state; /* State at MC fault */
219 #endif
220 uint64_t cpu_uber_arg_store; /* Double mapped address
221 * of current thread's
222 * uu_arg array.
223 */
224 uint64_t cpu_uber_arg_store_valid; /* Double mapped
225 * address of pcb
226 * arg store
227 * validity flag.
228 */
229 rtc_nanotime_t *cpu_nanotime; /* Nanotime info */
230 thread_t csw_old_thread;
231 thread_t csw_new_thread;
232 uint64_t cpu_max_observed_int_latency;
233 int cpu_max_observed_int_latency_vector;
234 uint64_t debugger_entry_time;
235 volatile boolean_t cpu_NMI_acknowledged;
236 /* A separate nested interrupt stack flag, to account
237 * for non-nested interrupts arriving while on the interrupt stack
238 * Currently only occurs when AICPM enables interrupts on the
239 * interrupt stack during processor offlining.
240 */
241 uint32_t cpu_nested_istack;
242 uint32_t cpu_nested_istack_events;
243 } cpu_data_t;
244
245 extern cpu_data_t *cpu_data_ptr[];
246 extern cpu_data_t cpu_data_master;
247
248 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
249 #ifndef offsetof
250 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
251 #endif /* offsetof */
252 #define CPU_DATA_GET(member,type) \
253 type ret; \
254 __asm__ volatile ("mov %%gs:%P1,%0" \
255 : "=r" (ret) \
256 : "i" (offsetof(cpu_data_t,member))); \
257 return ret;
258
259 /*
260 * Everyone within the osfmk part of the kernel can use the fast
261 * inline versions of these routines. Everyone outside, must call
262 * the real thing,
263 */
264 static inline thread_t
265 get_active_thread(void)
266 {
267 CPU_DATA_GET(cpu_active_thread,thread_t)
268 }
269 #define current_thread_fast() get_active_thread()
270 #define current_thread() current_thread_fast()
271
272 #if defined(__i386__)
273 static inline boolean_t
274 get_is64bit(void)
275 {
276 CPU_DATA_GET(cpu_is64bit, boolean_t)
277 }
278 #define cpu_mode_is64bit() get_is64bit()
279 #elif defined(__x86_64__)
280 #define cpu_mode_is64bit() TRUE
281 #endif
282
283 static inline int
284 get_preemption_level(void)
285 {
286 CPU_DATA_GET(cpu_preemption_level,int)
287 }
288 static inline int
289 get_simple_lock_count(void)
290 {
291 CPU_DATA_GET(cpu_simple_lock_count,int)
292 }
293 static inline int
294 get_interrupt_level(void)
295 {
296 CPU_DATA_GET(cpu_interrupt_level,int)
297 }
298 static inline int
299 get_cpu_number(void)
300 {
301 CPU_DATA_GET(cpu_number,int)
302 }
303 static inline int
304 get_cpu_phys_number(void)
305 {
306 CPU_DATA_GET(cpu_phys_number,int)
307 }
308
309
310 static inline void
311 disable_preemption(void)
312 {
313 __asm__ volatile ("incl %%gs:%P0"
314 :
315 : "i" (offsetof(cpu_data_t, cpu_preemption_level)));
316 }
317
318 static inline void
319 enable_preemption(void)
320 {
321 assert(get_preemption_level() > 0);
322
323 __asm__ volatile ("decl %%gs:%P0 \n\t"
324 "jne 1f \n\t"
325 "call _kernel_preempt_check \n\t"
326 "1:"
327 : /* no outputs */
328 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
329 : "eax", "ecx", "edx", "cc", "memory");
330 }
331
332 static inline void
333 enable_preemption_no_check(void)
334 {
335 assert(get_preemption_level() > 0);
336
337 __asm__ volatile ("decl %%gs:%P0"
338 : /* no outputs */
339 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
340 : "cc", "memory");
341 }
342
343 static inline void
344 mp_disable_preemption(void)
345 {
346 disable_preemption();
347 }
348
349 static inline void
350 mp_enable_preemption(void)
351 {
352 enable_preemption();
353 }
354
355 static inline void
356 mp_enable_preemption_no_check(void)
357 {
358 enable_preemption_no_check();
359 }
360
361 static inline cpu_data_t *
362 current_cpu_datap(void)
363 {
364 CPU_DATA_GET(cpu_this, cpu_data_t *);
365 }
366
367 static inline cpu_data_t *
368 cpu_datap(int cpu)
369 {
370 return cpu_data_ptr[cpu];
371 }
372
373 extern cpu_data_t *cpu_data_alloc(boolean_t is_boot_cpu);
374
375 #endif /* I386_CPU_DATA */