2 * Copyright (c) 2000-2007 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>
40 #include <kern/assert.h>
41 #include <kern/kern_types.h>
42 #include <kern/processor.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>
50 #include <i386/vmx/vmx_cpu.h>
53 * Data structures referenced (anonymously) from per-cpu data:
55 struct cpu_cons_buffer
;
56 struct cpu_desc_table
;
61 * Data structures embedded in per-cpu data:
63 typedef struct rtclock_timer
{
66 boolean_t has_expired
;
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 */
81 #define SLOW_TSC_THRESHOLD 1000067800 /* TSC is too slow for regular nanotime() algorithm */
85 struct i386_tss
*cdi_ktss
;
87 struct i386_tss
*cdi_dbtss
;
89 struct fake_descriptor
*cdi_gdt
;
90 struct fake_descriptor
*cdi_idt
;
91 struct fake_descriptor
*cdi_ldt
;
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 */
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.
108 addr64_t cu_isf
; /* thread->pcb->iss.isf */
109 uint64_t cu_tmp
; /* temporary scratch */
110 addr64_t cu_user_gs_base
;
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()/
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
126 typedef struct cpu_data
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
;
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
;
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
;
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
;
172 #endif /* MACH_KDB */
174 boolean_t cpu_boot_complete
;
177 vm_offset_t cpu_copywindow_base
;
178 uint64_t *cpu_copywindow_pdp
;
180 vm_offset_t cpu_physwindow_base
;
181 uint64_t *cpu_physwindow_ptep
;
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
193 uint64_t cpu_uber_arg_store_valid
; /* Double mapped
202 extern cpu_data_t
*cpu_data_ptr
[];
203 extern cpu_data_t cpu_data_master
;
205 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
207 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
208 #endif /* offsetof */
209 #define CPU_DATA_GET(member,type) \
211 __asm__ volatile ("movl %%gs:%P1,%0" \
213 : "i" (offsetof(cpu_data_t,member))); \
217 * Everyone within the osfmk part of the kernel can use the fast
218 * inline versions of these routines. Everyone outside, must call
221 static inline thread_t
222 get_active_thread(void)
224 CPU_DATA_GET(cpu_active_thread
,thread_t
)
226 #define current_thread_fast() get_active_thread()
227 #define current_thread() current_thread_fast()
229 static inline boolean_t
232 CPU_DATA_GET(cpu_is64bit
, boolean_t
)
234 #define cpu_mode_is64bit() get_is64bit()
237 get_preemption_level(void)
239 CPU_DATA_GET(cpu_preemption_level
,int)
242 get_simple_lock_count(void)
244 CPU_DATA_GET(cpu_simple_lock_count
,int)
247 get_interrupt_level(void)
249 CPU_DATA_GET(cpu_interrupt_level
,int)
254 CPU_DATA_GET(cpu_number
,int)
257 get_cpu_phys_number(void)
259 CPU_DATA_GET(cpu_phys_number
,int)
263 disable_preemption(void)
265 __asm__
volatile ("incl %%gs:%P0"
267 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
)));
271 enable_preemption(void)
273 assert(get_preemption_level() > 0);
275 __asm__
volatile ("decl %%gs:%P0 \n\t"
277 "call _kernel_preempt_check \n\t"
280 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
281 : "eax", "ecx", "edx", "cc", "memory");
285 enable_preemption_no_check(void)
287 assert(get_preemption_level() > 0);
289 __asm__
volatile ("decl %%gs:%P0"
291 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
296 mp_disable_preemption(void)
298 disable_preemption();
302 mp_enable_preemption(void)
308 mp_enable_preemption_no_check(void)
310 enable_preemption_no_check();
313 static inline cpu_data_t
*
314 current_cpu_datap(void)
316 CPU_DATA_GET(cpu_this
, cpu_data_t
*);
319 static inline cpu_data_t
*
322 assert(cpu_data_ptr
[cpu
]);
323 return cpu_data_ptr
[cpu
];
326 extern cpu_data_t
*cpu_data_alloc(boolean_t is_boot_cpu
);
328 #else /* !defined(__GNUC__) */
330 #endif /* defined(__GNUC__) */
332 #endif /* I386_CPU_DATA */