2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
36 #include <mach_assert.h>
38 #include <kern/assert.h>
39 #include <kern/kern_types.h>
40 #include <kern/queue.h>
41 #include <kern/processor.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>
51 #include <i386/vmx/vmx_cpu.h>
55 * Data structures referenced (anonymously) from per-cpu data:
57 struct cpu_cons_buffer
;
58 struct cpu_desc_table
;
63 * Data structures embedded in per-cpu data:
65 typedef struct rtclock_timer
{
69 boolean_t has_expired
;
76 struct i386_tss
*cdi_ktss
;
78 struct i386_tss
*cdi_dbtss
;
80 struct __attribute__((packed
)) {
82 struct fake_descriptor
*ptr
;
84 struct fake_descriptor
*cdi_ldt
;
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 */
94 #elif defined(__x86_64__)
98 struct x86_64_tss
*cdi_ktss
;
100 struct x86_64_tss
*cdi_dbtss
;
101 #endif /* MACH_KDB */
102 struct __attribute__((packed
)) {
106 struct fake_descriptor
*cdi_ldt
;
107 vm_offset_t cdi_sstk
;
111 TASK_MAP_32BIT
, /* 32-bit user, compatibility mode */
112 TASK_MAP_64BIT
, /* 64-bit user thread, shared space */
116 #error Unsupported architecture
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.
126 addr64_t cu_isf
; /* thread->pcb->iss.isf */
127 uint64_t cu_tmp
; /* temporary scratch */
128 addr64_t cu_user_gs_base
;
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()/
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
145 typedef struct cpu_data
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
;
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
;
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
;
180 struct cpu_desc_table
*cpu_desc_tablep
;
181 struct fake_descriptor
*cpu_ldtp
;
182 cpu_desc_index_t cpu_desc_index
;
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
;
192 #endif /* MACH_KDB */
194 boolean_t cpu_boot_complete
;
197 #if NCOPY_WINDOWS > 0
198 vm_offset_t cpu_copywindow_base
;
199 uint64_t *cpu_copywindow_pdp
;
201 vm_offset_t cpu_physwindow_base
;
202 uint64_t *cpu_physwindow_ptep
;
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 */
213 vmx_cpu_t cpu_vmx
; /* wonderful world of virtualization */
216 struct mca_state
*cpu_mca_state
; /* State at MC fault */
218 uint64_t cpu_uber_arg_store
; /* Double mapped address
219 * of current thread's
222 uint64_t cpu_uber_arg_store_valid
; /* Double mapped
227 rtc_nanotime_t
*cpu_nanotime
; /* Nanotime info */
228 thread_t csw_old_thread
;
229 thread_t csw_new_thread
;
232 extern cpu_data_t
*cpu_data_ptr
[];
233 extern cpu_data_t cpu_data_master
;
235 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
237 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
238 #endif /* offsetof */
239 #define CPU_DATA_GET(member,type) \
241 __asm__ volatile ("mov %%gs:%P1,%0" \
243 : "i" (offsetof(cpu_data_t,member))); \
247 * Everyone within the osfmk part of the kernel can use the fast
248 * inline versions of these routines. Everyone outside, must call
251 static inline thread_t
252 get_active_thread(void)
254 CPU_DATA_GET(cpu_active_thread
,thread_t
)
256 #define current_thread_fast() get_active_thread()
257 #define current_thread() current_thread_fast()
259 #if defined(__i386__)
260 static inline boolean_t
263 CPU_DATA_GET(cpu_is64bit
, boolean_t
)
265 #define cpu_mode_is64bit() get_is64bit()
266 #elif defined(__x86_64__)
267 #define cpu_mode_is64bit() TRUE
271 get_preemption_level(void)
273 CPU_DATA_GET(cpu_preemption_level
,int)
276 get_simple_lock_count(void)
278 CPU_DATA_GET(cpu_simple_lock_count
,int)
281 get_interrupt_level(void)
283 CPU_DATA_GET(cpu_interrupt_level
,int)
288 CPU_DATA_GET(cpu_number
,int)
291 get_cpu_phys_number(void)
293 CPU_DATA_GET(cpu_phys_number
,int)
298 disable_preemption(void)
300 __asm__
volatile ("incl %%gs:%P0"
302 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
)));
306 enable_preemption(void)
308 assert(get_preemption_level() > 0);
310 __asm__
volatile ("decl %%gs:%P0 \n\t"
312 "call _kernel_preempt_check \n\t"
315 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
316 : "eax", "ecx", "edx", "cc", "memory");
320 enable_preemption_no_check(void)
322 assert(get_preemption_level() > 0);
324 __asm__
volatile ("decl %%gs:%P0"
326 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
331 mp_disable_preemption(void)
333 disable_preemption();
337 mp_enable_preemption(void)
343 mp_enable_preemption_no_check(void)
345 enable_preemption_no_check();
348 static inline cpu_data_t
*
349 current_cpu_datap(void)
351 CPU_DATA_GET(cpu_this
, cpu_data_t
*);
354 static inline cpu_data_t
*
357 assert(cpu_data_ptr
[cpu
]);
358 return cpu_data_ptr
[cpu
];
361 extern cpu_data_t
*cpu_data_alloc(boolean_t is_boot_cpu
);
363 #endif /* I386_CPU_DATA */