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