]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
c910b4d9 | 2 | * Copyright (c) 2000-2008 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | * | |
31 | */ | |
32 | ||
33 | #ifndef I386_CPU_DATA | |
34 | #define I386_CPU_DATA | |
35 | ||
1c79356b A |
36 | #include <mach_assert.h> |
37 | ||
1c79356b A |
38 | #include <kern/assert.h> |
39 | #include <kern/kern_types.h> | |
b0d623f7 | 40 | #include <kern/queue.h> |
91447636 | 41 | #include <kern/processor.h> |
0c530ab8 | 42 | #include <kern/pms.h> |
55e303ae | 43 | #include <pexpert/pexpert.h> |
0c530ab8 | 44 | #include <mach/i386/thread_status.h> |
b0d623f7 | 45 | #include <mach/i386/vm_param.h> |
0c530ab8 A |
46 | #include <i386/rtclock.h> |
47 | #include <i386/pmCPU.h> | |
2d21ac55 A |
48 | #include <i386/cpu_topology.h> |
49 | ||
b0d623f7 | 50 | #if CONFIG_VMX |
2d21ac55 | 51 | #include <i386/vmx/vmx_cpu.h> |
b0d623f7 | 52 | #endif |
91447636 A |
53 | |
54 | /* | |
55 | * Data structures referenced (anonymously) from per-cpu data: | |
56 | */ | |
91447636 | 57 | struct cpu_cons_buffer; |
0c530ab8 | 58 | struct cpu_desc_table; |
2d21ac55 | 59 | struct mca_state; |
91447636 A |
60 | |
61 | ||
62 | /* | |
63 | * Data structures embedded in per-cpu data: | |
64 | */ | |
65 | typedef struct rtclock_timer { | |
c910b4d9 A |
66 | queue_head_t queue; |
67 | uint64_t deadline; | |
68 | boolean_t is_set; | |
69 | boolean_t has_expired; | |
91447636 A |
70 | } rtclock_timer_t; |
71 | ||
2d21ac55 | 72 | |
b0d623f7 A |
73 | #if defined(__i386__) |
74 | ||
91447636 A |
75 | typedef struct { |
76 | struct i386_tss *cdi_ktss; | |
77 | #if MACH_KDB | |
78 | struct i386_tss *cdi_dbtss; | |
79 | #endif /* MACH_KDB */ | |
b0d623f7 A |
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; | |
91447636 A |
86 | } cpu_desc_index_t; |
87 | ||
0c530ab8 A |
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 | ||
b0d623f7 A |
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 | ||
0c530ab8 A |
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; | |
91447636 | 130 | |
b0d623f7 | 131 | |
91447636 A |
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; | |
0c530ab8 A |
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 */ | |
91447636 A |
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; | |
0c530ab8 | 166 | rtclock_timer_t rtclock_timer; |
2d21ac55 A |
167 | boolean_t cpu_is64bit; |
168 | task_map_t cpu_task_map; | |
b0d623f7 A |
169 | volatile addr64_t cpu_task_cr3; |
170 | volatile addr64_t cpu_active_cr3; | |
2d21ac55 | 171 | addr64_t cpu_kernel_cr3; |
0c530ab8 A |
172 | cpu_uber_t cpu_uber; |
173 | void *cpu_chud; | |
6601e61a | 174 | void *cpu_console_buf; |
2d21ac55 | 175 | struct x86_lcpu lcpu; |
91447636 | 176 | struct processor *cpu_processor; |
b0d623f7 | 177 | #if NCOPY_WINDOWS > 0 |
91447636 | 178 | struct cpu_pmap *cpu_pmap; |
b0d623f7 | 179 | #endif |
0c530ab8 A |
180 | struct cpu_desc_table *cpu_desc_tablep; |
181 | struct fake_descriptor *cpu_ldtp; | |
91447636 | 182 | cpu_desc_index_t cpu_desc_index; |
0c530ab8 | 183 | int cpu_ldt; |
91447636 A |
184 | #ifdef MACH_KDB |
185 | /* XXX Untested: */ | |
186 | int cpu_db_pass_thru; | |
2d21ac55 A |
187 | vm_offset_t cpu_db_stacks; |
188 | void *cpu_kdb_saved_state; | |
189 | spl_t cpu_kdb_saved_ipl; | |
91447636 A |
190 | int cpu_kdb_is_slave; |
191 | int cpu_kdb_active; | |
192 | #endif /* MACH_KDB */ | |
0c530ab8 A |
193 | boolean_t cpu_iflag; |
194 | boolean_t cpu_boot_complete; | |
195 | int cpu_hibernate; | |
2d21ac55 | 196 | |
b0d623f7 | 197 | #if NCOPY_WINDOWS > 0 |
2d21ac55 A |
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; | |
b0d623f7 A |
204 | #endif |
205 | ||
206 | ||
207 | ||
208 | volatile boolean_t cpu_tlb_invalid; | |
593a1d5f | 209 | uint32_t cpu_hwIntCnt[256]; /* Interrupt counts */ |
0c530ab8 | 210 | uint64_t cpu_dr7; /* debug control register */ |
2d21ac55 | 211 | uint64_t cpu_int_event_time; /* intr entry/exit time */ |
b0d623f7 | 212 | #if CONFIG_VMX |
2d21ac55 | 213 | vmx_cpu_t cpu_vmx; /* wonderful world of virtualization */ |
b0d623f7 A |
214 | #endif |
215 | #if CONFIG_MCA | |
2d21ac55 | 216 | struct mca_state *cpu_mca_state; /* State at MC fault */ |
b0d623f7 | 217 | #endif |
2d21ac55 A |
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 | */ | |
593a1d5f | 227 | rtc_nanotime_t *cpu_nanotime; /* Nanotime info */ |
b0d623f7 A |
228 | thread_t csw_old_thread; |
229 | thread_t csw_new_thread; | |
55e303ae | 230 | } cpu_data_t; |
1c79356b | 231 | |
91447636 A |
232 | extern cpu_data_t *cpu_data_ptr[]; |
233 | extern cpu_data_t cpu_data_master; | |
9bccf70c | 234 | |
55e303ae | 235 | /* Macro to generate inline bodies to retrieve per-cpu data fields. */ |
2d21ac55 | 236 | #ifndef offsetof |
55e303ae | 237 | #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
2d21ac55 | 238 | #endif /* offsetof */ |
91447636 | 239 | #define CPU_DATA_GET(member,type) \ |
55e303ae | 240 | type ret; \ |
b0d623f7 | 241 | __asm__ volatile ("mov %%gs:%P1,%0" \ |
55e303ae | 242 | : "=r" (ret) \ |
91447636 | 243 | : "i" (offsetof(cpu_data_t,member))); \ |
55e303ae | 244 | return ret; |
9bccf70c | 245 | |
1c79356b A |
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 | */ | |
91447636 A |
251 | static inline thread_t |
252 | get_active_thread(void) | |
1c79356b | 253 | { |
91447636 | 254 | CPU_DATA_GET(cpu_active_thread,thread_t) |
1c79356b | 255 | } |
91447636 A |
256 | #define current_thread_fast() get_active_thread() |
257 | #define current_thread() current_thread_fast() | |
1c79356b | 258 | |
b0d623f7 | 259 | #if defined(__i386__) |
0c530ab8 A |
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() | |
b0d623f7 A |
266 | #elif defined(__x86_64__) |
267 | #define cpu_mode_is64bit() TRUE | |
268 | #endif | |
0c530ab8 | 269 | |
91447636 A |
270 | static inline int |
271 | get_preemption_level(void) | |
1c79356b | 272 | { |
91447636 | 273 | CPU_DATA_GET(cpu_preemption_level,int) |
55e303ae | 274 | } |
91447636 A |
275 | static inline int |
276 | get_simple_lock_count(void) | |
55e303ae | 277 | { |
91447636 | 278 | CPU_DATA_GET(cpu_simple_lock_count,int) |
55e303ae | 279 | } |
91447636 A |
280 | static inline int |
281 | get_interrupt_level(void) | |
55e303ae | 282 | { |
91447636 | 283 | CPU_DATA_GET(cpu_interrupt_level,int) |
55e303ae | 284 | } |
91447636 A |
285 | static inline int |
286 | get_cpu_number(void) | |
55e303ae A |
287 | { |
288 | CPU_DATA_GET(cpu_number,int) | |
289 | } | |
91447636 A |
290 | static inline int |
291 | get_cpu_phys_number(void) | |
55e303ae A |
292 | { |
293 | CPU_DATA_GET(cpu_phys_number,int) | |
1c79356b | 294 | } |
1c79356b | 295 | |
b0d623f7 | 296 | |
91447636 A |
297 | static inline void |
298 | disable_preemption(void) | |
1c79356b | 299 | { |
91447636 A |
300 | __asm__ volatile ("incl %%gs:%P0" |
301 | : | |
302 | : "i" (offsetof(cpu_data_t, cpu_preemption_level))); | |
303 | } | |
1c79356b | 304 | |
91447636 A |
305 | static inline void |
306 | enable_preemption(void) | |
307 | { | |
55e303ae A |
308 | assert(get_preemption_level() > 0); |
309 | ||
91447636 A |
310 | __asm__ volatile ("decl %%gs:%P0 \n\t" |
311 | "jne 1f \n\t" | |
312 | "call _kernel_preempt_check \n\t" | |
313 | "1:" | |
1c79356b | 314 | : /* no outputs */ |
91447636 A |
315 | : "i" (offsetof(cpu_data_t, cpu_preemption_level)) |
316 | : "eax", "ecx", "edx", "cc", "memory"); | |
1c79356b A |
317 | } |
318 | ||
91447636 A |
319 | static inline void |
320 | enable_preemption_no_check(void) | |
1c79356b | 321 | { |
1c79356b | 322 | assert(get_preemption_level() > 0); |
1c79356b | 323 | |
91447636 | 324 | __asm__ volatile ("decl %%gs:%P0" |
1c79356b | 325 | : /* no outputs */ |
91447636 | 326 | : "i" (offsetof(cpu_data_t, cpu_preemption_level)) |
1c79356b | 327 | : "cc", "memory"); |
1c79356b A |
328 | } |
329 | ||
91447636 A |
330 | static inline void |
331 | mp_disable_preemption(void) | |
1c79356b | 332 | { |
1c79356b | 333 | disable_preemption(); |
1c79356b A |
334 | } |
335 | ||
91447636 A |
336 | static inline void |
337 | mp_enable_preemption(void) | |
1c79356b | 338 | { |
1c79356b | 339 | enable_preemption(); |
1c79356b A |
340 | } |
341 | ||
91447636 A |
342 | static inline void |
343 | mp_enable_preemption_no_check(void) | |
1c79356b | 344 | { |
1c79356b | 345 | enable_preemption_no_check(); |
1c79356b A |
346 | } |
347 | ||
91447636 A |
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 | { | |
91447636 A |
357 | return cpu_data_ptr[cpu]; |
358 | } | |
359 | ||
360 | extern cpu_data_t *cpu_data_alloc(boolean_t is_boot_cpu); | |
1c79356b | 361 | |
1c79356b | 362 | #endif /* I386_CPU_DATA */ |