]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cpu_data.h
e238452511e5eb6a2bc29bc3b2b32c9493f16331
[apple/xnu.git] / osfmk / i386 / cpu_data.h
1 /*
2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 *
33 */
34
35 #ifndef I386_CPU_DATA
36 #define I386_CPU_DATA
37
38 #include <mach_assert.h>
39
40 #if defined(__GNUC__)
41
42 #include <kern/assert.h>
43 #include <kern/kern_types.h>
44 #include <kern/processor.h>
45 #include <kern/pms.h>
46 #include <pexpert/pexpert.h>
47 #include <mach/i386/thread_status.h>
48 #include <i386/rtclock.h>
49 #include <i386/pmCPU.h>
50
51 /*
52 * Data structures referenced (anonymously) from per-cpu data:
53 */
54 struct cpu_core;
55 struct cpu_cons_buffer;
56 struct cpu_desc_table;
57
58
59 /*
60 * Data structures embedded in per-cpu data:
61 */
62 typedef struct rtclock_timer {
63 uint64_t deadline;
64 boolean_t is_set;
65 boolean_t has_expired;
66 } rtclock_timer_t;
67
68 typedef struct {
69 uint64_t tsc_base; /* timestamp */
70 uint64_t ns_base; /* nanoseconds */
71 uint32_t scale; /* tsc -> nanosec multiplier */
72 uint32_t shift; /* tsc -> nanosec shift/div */
73 } rtc_nanotime_t;
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 fake_descriptor *cdi_gdt;
81 struct fake_descriptor *cdi_idt;
82 struct fake_descriptor *cdi_ldt;
83 vm_offset_t cdi_sstk;
84 } cpu_desc_index_t;
85
86 typedef enum {
87 TASK_MAP_32BIT, /* 32-bit, compatibility mode */
88 TASK_MAP_64BIT, /* 64-bit, separate address space */
89 TASK_MAP_64BIT_SHARED /* 64-bit, kernel-shared addr space */
90 } task_map_t;
91
92 /*
93 * This structure is used on entry into the (uber-)kernel on syscall from
94 * a 64-bit user. It contains the address of the machine state save area
95 * for the current thread and a temporary place to save the user's rsp
96 * before loading this address into rsp.
97 */
98 typedef struct {
99 addr64_t cu_isf; /* thread->pcb->iss.isf */
100 uint64_t cu_tmp; /* temporary scratch */
101 addr64_t cu_user_gs_base;
102 } cpu_uber_t;
103
104 /*
105 * Per-cpu data.
106 *
107 * Each processor has a per-cpu data area which is dereferenced through the
108 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
109 * using this, inlines provides single-instruction access to frequently used
110 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
111 * current_thread().
112 *
113 * Cpu data owned by another processor can be accessed using the
114 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
115 * pointers.
116 */
117 typedef struct cpu_data
118 {
119 struct cpu_data *cpu_this; /* pointer to myself */
120 thread_t cpu_active_thread;
121 void *cpu_int_state; /* interrupt state */
122 vm_offset_t cpu_active_stack; /* kernel stack base */
123 vm_offset_t cpu_kernel_stack; /* kernel stack top */
124 vm_offset_t cpu_int_stack_top;
125 int cpu_preemption_level;
126 int cpu_simple_lock_count;
127 int cpu_interrupt_level;
128 int cpu_number; /* Logical CPU */
129 int cpu_phys_number; /* Physical CPU */
130 cpu_id_t cpu_id; /* Platform Expert */
131 int cpu_signals; /* IPI events */
132 int cpu_mcount_off; /* mcount recursion */
133 ast_t cpu_pending_ast;
134 int cpu_type;
135 int cpu_subtype;
136 int cpu_threadtype;
137 int cpu_running;
138 uint64_t rtclock_intr_deadline;
139 rtclock_timer_t rtclock_timer;
140 boolean_t cpu_is64bit;
141 task_map_t cpu_task_map;
142 addr64_t cpu_task_cr3;
143 addr64_t cpu_active_cr3;
144 addr64_t cpu_kernel_cr3;
145 cpu_uber_t cpu_uber;
146 void *cpu_chud;
147 void *cpu_console_buf;
148 struct cpu_core *cpu_core; /* cpu's parent core */
149 struct processor *cpu_processor;
150 struct cpu_pmap *cpu_pmap;
151 struct cpu_desc_table *cpu_desc_tablep;
152 struct fake_descriptor *cpu_ldtp;
153 cpu_desc_index_t cpu_desc_index;
154 int cpu_ldt;
155 #ifdef MACH_KDB
156 /* XXX Untested: */
157 int cpu_db_pass_thru;
158 vm_offset_t cpu_db_stacks;
159 void *cpu_kdb_saved_state;
160 spl_t cpu_kdb_saved_ipl;
161 int cpu_kdb_is_slave;
162 int cpu_kdb_active;
163 #endif /* MACH_KDB */
164 boolean_t cpu_iflag;
165 boolean_t cpu_boot_complete;
166 int cpu_hibernate;
167 pmsd pms; /* Power Management Stepper control */
168 uint64_t rtcPop; /* when the etimer wants a timer pop */
169
170 vm_offset_t cpu_copywindow_base;
171 uint64_t *cpu_copywindow_pdp;
172
173 vm_offset_t cpu_physwindow_base;
174 uint64_t *cpu_physwindow_ptep;
175 void *cpu_hi_iss;
176 boolean_t cpu_tlb_invalid;
177
178 uint64_t *cpu_pmHpet; /* Address of the HPET for this processor */
179 uint32_t cpu_pmHpetVec; /* Interrupt vector for HPET for this processor */
180 /* Statistics */
181 pmStats_t cpu_pmStats; /* Power management data */
182 uint32_t cpu_hwIntCnt[256]; /* Interrupt counts */
183
184 uint64_t cpu_dr7; /* debug control register */
185 } cpu_data_t;
186
187 extern cpu_data_t *cpu_data_ptr[];
188 extern cpu_data_t cpu_data_master;
189
190 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
191 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
192 #define CPU_DATA_GET(member,type) \
193 type ret; \
194 __asm__ volatile ("movl %%gs:%P1,%0" \
195 : "=r" (ret) \
196 : "i" (offsetof(cpu_data_t,member))); \
197 return ret;
198
199 /*
200 * Everyone within the osfmk part of the kernel can use the fast
201 * inline versions of these routines. Everyone outside, must call
202 * the real thing,
203 */
204 static inline thread_t
205 get_active_thread(void)
206 {
207 CPU_DATA_GET(cpu_active_thread,thread_t)
208 }
209 #define current_thread_fast() get_active_thread()
210 #define current_thread() current_thread_fast()
211
212 static inline boolean_t
213 get_is64bit(void)
214 {
215 CPU_DATA_GET(cpu_is64bit, boolean_t)
216 }
217 #define cpu_mode_is64bit() get_is64bit()
218
219 static inline int
220 get_preemption_level(void)
221 {
222 CPU_DATA_GET(cpu_preemption_level,int)
223 }
224 static inline int
225 get_simple_lock_count(void)
226 {
227 CPU_DATA_GET(cpu_simple_lock_count,int)
228 }
229 static inline int
230 get_interrupt_level(void)
231 {
232 CPU_DATA_GET(cpu_interrupt_level,int)
233 }
234 static inline int
235 get_cpu_number(void)
236 {
237 CPU_DATA_GET(cpu_number,int)
238 }
239 static inline int
240 get_cpu_phys_number(void)
241 {
242 CPU_DATA_GET(cpu_phys_number,int)
243 }
244 static inline struct
245 cpu_core * get_cpu_core(void)
246 {
247 CPU_DATA_GET(cpu_core,struct cpu_core *)
248 }
249
250 static inline void
251 disable_preemption(void)
252 {
253 __asm__ volatile ("incl %%gs:%P0"
254 :
255 : "i" (offsetof(cpu_data_t, cpu_preemption_level)));
256 }
257
258 static inline void
259 enable_preemption(void)
260 {
261 assert(get_preemption_level() > 0);
262
263 __asm__ volatile ("decl %%gs:%P0 \n\t"
264 "jne 1f \n\t"
265 "call _kernel_preempt_check \n\t"
266 "1:"
267 : /* no outputs */
268 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
269 : "eax", "ecx", "edx", "cc", "memory");
270 }
271
272 static inline void
273 enable_preemption_no_check(void)
274 {
275 assert(get_preemption_level() > 0);
276
277 __asm__ volatile ("decl %%gs:%P0"
278 : /* no outputs */
279 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
280 : "cc", "memory");
281 }
282
283 static inline void
284 mp_disable_preemption(void)
285 {
286 disable_preemption();
287 }
288
289 static inline void
290 mp_enable_preemption(void)
291 {
292 enable_preemption();
293 }
294
295 static inline void
296 mp_enable_preemption_no_check(void)
297 {
298 enable_preemption_no_check();
299 }
300
301 static inline cpu_data_t *
302 current_cpu_datap(void)
303 {
304 CPU_DATA_GET(cpu_this, cpu_data_t *);
305 }
306
307 static inline cpu_data_t *
308 cpu_datap(int cpu)
309 {
310 assert(cpu_data_ptr[cpu]);
311 return cpu_data_ptr[cpu];
312 }
313
314 extern cpu_data_t *cpu_data_alloc(boolean_t is_boot_cpu);
315
316 #else /* !defined(__GNUC__) */
317
318 #endif /* defined(__GNUC__) */
319
320 #endif /* I386_CPU_DATA */