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
;
71 struct i386_tss
*cdi_ktss
;
73 struct i386_tss
*cdi_dbtss
;
75 struct fake_descriptor
*cdi_gdt
;
76 struct fake_descriptor
*cdi_idt
;
77 struct fake_descriptor
*cdi_ldt
;
82 TASK_MAP_32BIT
, /* 32-bit, compatibility mode */
83 TASK_MAP_64BIT
, /* 64-bit, separate address space */
84 TASK_MAP_64BIT_SHARED
/* 64-bit, kernel-shared addr space */
88 * This structure is used on entry into the (uber-)kernel on syscall from
89 * a 64-bit user. It contains the address of the machine state save area
90 * for the current thread and a temporary place to save the user's rsp
91 * before loading this address into rsp.
94 addr64_t cu_isf
; /* thread->pcb->iss.isf */
95 uint64_t cu_tmp
; /* temporary scratch */
96 addr64_t cu_user_gs_base
;
102 * Each processor has a per-cpu data area which is dereferenced through the
103 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
104 * using this, inlines provides single-instruction access to frequently used
105 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
108 * Cpu data owned by another processor can be accessed using the
109 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
112 typedef struct cpu_data
114 struct cpu_data
*cpu_this
; /* pointer to myself */
115 thread_t cpu_active_thread
;
116 void *cpu_int_state
; /* interrupt state */
117 vm_offset_t cpu_active_stack
; /* kernel stack base */
118 vm_offset_t cpu_kernel_stack
; /* kernel stack top */
119 vm_offset_t cpu_int_stack_top
;
120 int cpu_preemption_level
;
121 int cpu_simple_lock_count
;
122 int cpu_interrupt_level
;
123 int cpu_number
; /* Logical CPU */
124 int cpu_phys_number
; /* Physical CPU */
125 cpu_id_t cpu_id
; /* Platform Expert */
126 int cpu_signals
; /* IPI events */
127 int cpu_mcount_off
; /* mcount recursion */
128 ast_t cpu_pending_ast
;
133 uint64_t rtclock_intr_deadline
;
134 rtclock_timer_t rtclock_timer
;
135 boolean_t cpu_is64bit
;
136 task_map_t cpu_task_map
;
137 addr64_t cpu_task_cr3
;
138 addr64_t cpu_active_cr3
;
139 addr64_t cpu_kernel_cr3
;
142 void *cpu_console_buf
;
143 struct x86_lcpu lcpu
;
144 struct processor
*cpu_processor
;
145 struct cpu_pmap
*cpu_pmap
;
146 struct cpu_desc_table
*cpu_desc_tablep
;
147 struct fake_descriptor
*cpu_ldtp
;
148 cpu_desc_index_t cpu_desc_index
;
152 int cpu_db_pass_thru
;
153 vm_offset_t cpu_db_stacks
;
154 void *cpu_kdb_saved_state
;
155 spl_t cpu_kdb_saved_ipl
;
156 int cpu_kdb_is_slave
;
158 #endif /* MACH_KDB */
160 boolean_t cpu_boot_complete
;
163 vm_offset_t cpu_copywindow_base
;
164 uint64_t *cpu_copywindow_pdp
;
166 vm_offset_t cpu_physwindow_base
;
167 uint64_t *cpu_physwindow_ptep
;
169 boolean_t cpu_tlb_invalid
;
170 uint32_t cpu_hwIntCnt
[256]; /* Interrupt counts */
171 uint64_t cpu_dr7
; /* debug control register */
172 uint64_t cpu_int_event_time
; /* intr entry/exit time */
173 vmx_cpu_t cpu_vmx
; /* wonderful world of virtualization */
174 struct mca_state
*cpu_mca_state
; /* State at MC fault */
175 uint64_t cpu_uber_arg_store
; /* Double mapped address
176 * of current thread's
179 uint64_t cpu_uber_arg_store_valid
; /* Double mapped
184 rtc_nanotime_t
*cpu_nanotime
; /* Nanotime info */
188 extern cpu_data_t
*cpu_data_ptr
[];
189 extern cpu_data_t cpu_data_master
;
191 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
193 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
194 #endif /* offsetof */
195 #define CPU_DATA_GET(member,type) \
197 __asm__ volatile ("movl %%gs:%P1,%0" \
199 : "i" (offsetof(cpu_data_t,member))); \
203 * Everyone within the osfmk part of the kernel can use the fast
204 * inline versions of these routines. Everyone outside, must call
207 static inline thread_t
208 get_active_thread(void)
210 CPU_DATA_GET(cpu_active_thread
,thread_t
)
212 #define current_thread_fast() get_active_thread()
213 #define current_thread() current_thread_fast()
215 static inline boolean_t
218 CPU_DATA_GET(cpu_is64bit
, boolean_t
)
220 #define cpu_mode_is64bit() get_is64bit()
223 get_preemption_level(void)
225 CPU_DATA_GET(cpu_preemption_level
,int)
228 get_simple_lock_count(void)
230 CPU_DATA_GET(cpu_simple_lock_count
,int)
233 get_interrupt_level(void)
235 CPU_DATA_GET(cpu_interrupt_level
,int)
240 CPU_DATA_GET(cpu_number
,int)
243 get_cpu_phys_number(void)
245 CPU_DATA_GET(cpu_phys_number
,int)
249 disable_preemption(void)
251 __asm__
volatile ("incl %%gs:%P0"
253 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
)));
257 enable_preemption(void)
259 assert(get_preemption_level() > 0);
261 __asm__
volatile ("decl %%gs:%P0 \n\t"
263 "call _kernel_preempt_check \n\t"
266 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
267 : "eax", "ecx", "edx", "cc", "memory");
271 enable_preemption_no_check(void)
273 assert(get_preemption_level() > 0);
275 __asm__
volatile ("decl %%gs:%P0"
277 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
282 mp_disable_preemption(void)
284 disable_preemption();
288 mp_enable_preemption(void)
294 mp_enable_preemption_no_check(void)
296 enable_preemption_no_check();
299 static inline cpu_data_t
*
300 current_cpu_datap(void)
302 CPU_DATA_GET(cpu_this
, cpu_data_t
*);
305 static inline cpu_data_t
*
308 assert(cpu_data_ptr
[cpu
]);
309 return cpu_data_ptr
[cpu
];
312 extern cpu_data_t
*cpu_data_alloc(boolean_t is_boot_cpu
);
314 #else /* !defined(__GNUC__) */
316 #endif /* defined(__GNUC__) */
318 #endif /* I386_CPU_DATA */