2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
30 #include <mach_assert.h>
34 #include <kern/assert.h>
35 #include <kern/kern_types.h>
36 #include <kern/processor.h>
38 #include <pexpert/pexpert.h>
39 #include <mach/i386/thread_status.h>
40 #include <i386/rtclock.h>
41 #include <i386/pmCPU.h>
44 * Data structures referenced (anonymously) from per-cpu data:
47 struct cpu_cons_buffer
;
48 struct cpu_desc_table
;
52 * Data structures embedded in per-cpu data:
54 typedef struct rtclock_timer
{
57 boolean_t has_expired
;
61 uint64_t tsc_base
; /* timestamp */
62 uint64_t ns_base
; /* nanoseconds */
63 uint32_t scale
; /* tsc -> nanosec multiplier */
64 uint32_t shift
; /* tsc -> nanosec shift/div */
68 struct i386_tss
*cdi_ktss
;
70 struct i386_tss
*cdi_dbtss
;
72 struct fake_descriptor
*cdi_gdt
;
73 struct fake_descriptor
*cdi_idt
;
74 struct fake_descriptor
*cdi_ldt
;
79 TASK_MAP_32BIT
, /* 32-bit, compatibility mode */
80 TASK_MAP_64BIT
, /* 64-bit, separate address space */
81 TASK_MAP_64BIT_SHARED
/* 64-bit, kernel-shared addr space */
85 * This structure is used on entry into the (uber-)kernel on syscall from
86 * a 64-bit user. It contains the address of the machine state save area
87 * for the current thread and a temporary place to save the user's rsp
88 * before loading this address into rsp.
91 addr64_t cu_isf
; /* thread->pcb->iss.isf */
92 uint64_t cu_tmp
; /* temporary scratch */
93 addr64_t cu_user_gs_base
;
99 * Each processor has a per-cpu data area which is dereferenced through the
100 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
101 * using this, inlines provides single-instruction access to frequently used
102 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
105 * Cpu data owned by another processor can be accessed using the
106 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
109 typedef struct cpu_data
111 struct cpu_data
*cpu_this
; /* pointer to myself */
112 thread_t cpu_active_thread
;
113 void *cpu_int_state
; /* interrupt state */
114 vm_offset_t cpu_active_stack
; /* kernel stack base */
115 vm_offset_t cpu_kernel_stack
; /* kernel stack top */
116 vm_offset_t cpu_int_stack_top
;
117 int cpu_preemption_level
;
118 int cpu_simple_lock_count
;
119 int cpu_interrupt_level
;
120 int cpu_number
; /* Logical CPU */
121 int cpu_phys_number
; /* Physical CPU */
122 cpu_id_t cpu_id
; /* Platform Expert */
123 int cpu_signals
; /* IPI events */
124 int cpu_mcount_off
; /* mcount recursion */
125 ast_t cpu_pending_ast
;
130 uint64_t rtclock_intr_deadline
;
131 rtclock_timer_t rtclock_timer
;
132 boolean_t cpu_is64bit
;
133 task_map_t cpu_task_map
;
134 addr64_t cpu_task_cr3
;
135 addr64_t cpu_active_cr3
;
136 addr64_t cpu_kernel_cr3
;
139 void *cpu_console_buf
;
140 struct cpu_core
*cpu_core
; /* cpu's parent core */
141 struct processor
*cpu_processor
;
142 struct cpu_pmap
*cpu_pmap
;
143 struct cpu_desc_table
*cpu_desc_tablep
;
144 struct fake_descriptor
*cpu_ldtp
;
145 cpu_desc_index_t cpu_desc_index
;
149 int cpu_db_pass_thru
;
150 vm_offset_t cpu_db_stacks
;
151 void *cpu_kdb_saved_state
;
152 spl_t cpu_kdb_saved_ipl
;
153 int cpu_kdb_is_slave
;
155 #endif /* MACH_KDB */
157 boolean_t cpu_boot_complete
;
159 pmsd pms
; /* Power Management Stepper control */
160 uint64_t rtcPop
; /* when the etimer wants a timer pop */
162 vm_offset_t cpu_copywindow_base
;
163 uint64_t *cpu_copywindow_pdp
;
165 vm_offset_t cpu_physwindow_base
;
166 uint64_t *cpu_physwindow_ptep
;
168 boolean_t cpu_tlb_invalid
;
170 uint64_t *cpu_pmHpet
; /* Address of the HPET for this processor */
171 uint32_t cpu_pmHpetVec
; /* Interrupt vector for HPET for this processor */
173 pmStats_t cpu_pmStats
; /* Power management data */
174 uint32_t cpu_hwIntCnt
[256]; /* Interrupt counts */
176 uint64_t cpu_dr7
; /* debug control register */
179 extern cpu_data_t
*cpu_data_ptr
[];
180 extern cpu_data_t cpu_data_master
;
182 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
183 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
184 #define CPU_DATA_GET(member,type) \
186 __asm__ volatile ("movl %%gs:%P1,%0" \
188 : "i" (offsetof(cpu_data_t,member))); \
192 * Everyone within the osfmk part of the kernel can use the fast
193 * inline versions of these routines. Everyone outside, must call
196 static inline thread_t
197 get_active_thread(void)
199 CPU_DATA_GET(cpu_active_thread
,thread_t
)
201 #define current_thread_fast() get_active_thread()
202 #define current_thread() current_thread_fast()
204 static inline boolean_t
207 CPU_DATA_GET(cpu_is64bit
, boolean_t
)
209 #define cpu_mode_is64bit() get_is64bit()
212 get_preemption_level(void)
214 CPU_DATA_GET(cpu_preemption_level
,int)
217 get_simple_lock_count(void)
219 CPU_DATA_GET(cpu_simple_lock_count
,int)
222 get_interrupt_level(void)
224 CPU_DATA_GET(cpu_interrupt_level
,int)
229 CPU_DATA_GET(cpu_number
,int)
232 get_cpu_phys_number(void)
234 CPU_DATA_GET(cpu_phys_number
,int)
237 cpu_core
* get_cpu_core(void)
239 CPU_DATA_GET(cpu_core
,struct cpu_core
*)
243 disable_preemption(void)
245 __asm__
volatile ("incl %%gs:%P0"
247 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
)));
251 enable_preemption(void)
253 assert(get_preemption_level() > 0);
255 __asm__
volatile ("decl %%gs:%P0 \n\t"
257 "call _kernel_preempt_check \n\t"
260 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
261 : "eax", "ecx", "edx", "cc", "memory");
265 enable_preemption_no_check(void)
267 assert(get_preemption_level() > 0);
269 __asm__
volatile ("decl %%gs:%P0"
271 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
276 mp_disable_preemption(void)
278 disable_preemption();
282 mp_enable_preemption(void)
288 mp_enable_preemption_no_check(void)
290 enable_preemption_no_check();
293 static inline cpu_data_t
*
294 current_cpu_datap(void)
296 CPU_DATA_GET(cpu_this
, cpu_data_t
*);
299 static inline cpu_data_t
*
302 assert(cpu_data_ptr
[cpu
]);
303 return cpu_data_ptr
[cpu
];
306 extern cpu_data_t
*cpu_data_alloc(boolean_t is_boot_cpu
);
308 #else /* !defined(__GNUC__) */
310 #endif /* defined(__GNUC__) */
312 #endif /* I386_CPU_DATA */