2 * Copyright (c) 2000-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
38 #include <mach_assert.h>
42 #include <kern/assert.h>
43 #include <kern/kern_types.h>
44 #include <kern/processor.h>
46 #include <pexpert/pexpert.h>
47 #include <mach/i386/thread_status.h>
48 #include <i386/rtclock.h>
49 #include <i386/pmCPU.h>
52 * Data structures referenced (anonymously) from per-cpu data:
55 struct cpu_cons_buffer
;
56 struct cpu_desc_table
;
60 * Data structures embedded in per-cpu data:
62 typedef struct rtclock_timer
{
65 boolean_t has_expired
;
69 uint64_t tsc_base
; /* timestamp */
70 uint64_t ns_base
; /* nanoseconds */
71 uint32_t scale
; /* tsc -> nanosec multiplier */
72 uint32_t shift
; /* tsc -> nanosec shift/div */
76 struct i386_tss
*cdi_ktss
;
78 struct i386_tss
*cdi_dbtss
;
80 struct fake_descriptor
*cdi_gdt
;
81 struct fake_descriptor
*cdi_idt
;
82 struct fake_descriptor
*cdi_ldt
;
87 TASK_MAP_32BIT
, /* 32-bit, compatibility mode */
88 TASK_MAP_64BIT
, /* 64-bit, separate address space */
89 TASK_MAP_64BIT_SHARED
/* 64-bit, kernel-shared addr space */
93 * This structure is used on entry into the (uber-)kernel on syscall from
94 * a 64-bit user. It contains the address of the machine state save area
95 * for the current thread and a temporary place to save the user's rsp
96 * before loading this address into rsp.
99 addr64_t cu_isf
; /* thread->pcb->iss.isf */
100 uint64_t cu_tmp
; /* temporary scratch */
101 addr64_t cu_user_gs_base
;
107 * Each processor has a per-cpu data area which is dereferenced through the
108 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
109 * using this, inlines provides single-instruction access to frequently used
110 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
113 * Cpu data owned by another processor can be accessed using the
114 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
117 typedef struct cpu_data
119 struct cpu_data
*cpu_this
; /* pointer to myself */
120 thread_t cpu_active_thread
;
121 void *cpu_int_state
; /* interrupt state */
122 vm_offset_t cpu_active_stack
; /* kernel stack base */
123 vm_offset_t cpu_kernel_stack
; /* kernel stack top */
124 vm_offset_t cpu_int_stack_top
;
125 int cpu_preemption_level
;
126 int cpu_simple_lock_count
;
127 int cpu_interrupt_level
;
128 int cpu_number
; /* Logical CPU */
129 int cpu_phys_number
; /* Physical CPU */
130 cpu_id_t cpu_id
; /* Platform Expert */
131 int cpu_signals
; /* IPI events */
132 int cpu_mcount_off
; /* mcount recursion */
133 ast_t cpu_pending_ast
;
138 uint64_t rtclock_intr_deadline
;
139 rtclock_timer_t rtclock_timer
;
140 boolean_t cpu_is64bit
;
141 task_map_t cpu_task_map
;
142 addr64_t cpu_task_cr3
;
143 addr64_t cpu_active_cr3
;
144 addr64_t cpu_kernel_cr3
;
147 void *cpu_console_buf
;
148 struct cpu_core
*cpu_core
; /* cpu's parent core */
149 struct processor
*cpu_processor
;
150 struct cpu_pmap
*cpu_pmap
;
151 struct cpu_desc_table
*cpu_desc_tablep
;
152 struct fake_descriptor
*cpu_ldtp
;
153 cpu_desc_index_t cpu_desc_index
;
157 int cpu_db_pass_thru
;
158 vm_offset_t cpu_db_stacks
;
159 void *cpu_kdb_saved_state
;
160 spl_t cpu_kdb_saved_ipl
;
161 int cpu_kdb_is_slave
;
163 #endif /* MACH_KDB */
165 boolean_t cpu_boot_complete
;
167 pmsd pms
; /* Power Management Stepper control */
168 uint64_t rtcPop
; /* when the etimer wants a timer pop */
170 vm_offset_t cpu_copywindow_base
;
171 uint64_t *cpu_copywindow_pdp
;
173 vm_offset_t cpu_physwindow_base
;
174 uint64_t *cpu_physwindow_ptep
;
176 boolean_t cpu_tlb_invalid
;
178 uint64_t *cpu_pmHpet
; /* Address of the HPET for this processor */
179 uint32_t cpu_pmHpetVec
; /* Interrupt vector for HPET for this processor */
181 pmStats_t cpu_pmStats
; /* Power management data */
182 uint32_t cpu_hwIntCnt
[256]; /* Interrupt counts */
184 uint64_t cpu_dr7
; /* debug control register */
187 extern cpu_data_t
*cpu_data_ptr
[];
188 extern cpu_data_t cpu_data_master
;
190 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
191 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
192 #define CPU_DATA_GET(member,type) \
194 __asm__ volatile ("movl %%gs:%P1,%0" \
196 : "i" (offsetof(cpu_data_t,member))); \
200 * Everyone within the osfmk part of the kernel can use the fast
201 * inline versions of these routines. Everyone outside, must call
204 static inline thread_t
205 get_active_thread(void)
207 CPU_DATA_GET(cpu_active_thread
,thread_t
)
209 #define current_thread_fast() get_active_thread()
210 #define current_thread() current_thread_fast()
212 static inline boolean_t
215 CPU_DATA_GET(cpu_is64bit
, boolean_t
)
217 #define cpu_mode_is64bit() get_is64bit()
220 get_preemption_level(void)
222 CPU_DATA_GET(cpu_preemption_level
,int)
225 get_simple_lock_count(void)
227 CPU_DATA_GET(cpu_simple_lock_count
,int)
230 get_interrupt_level(void)
232 CPU_DATA_GET(cpu_interrupt_level
,int)
237 CPU_DATA_GET(cpu_number
,int)
240 get_cpu_phys_number(void)
242 CPU_DATA_GET(cpu_phys_number
,int)
245 cpu_core
* get_cpu_core(void)
247 CPU_DATA_GET(cpu_core
,struct cpu_core
*)
251 disable_preemption(void)
253 __asm__
volatile ("incl %%gs:%P0"
255 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
)));
259 enable_preemption(void)
261 assert(get_preemption_level() > 0);
263 __asm__
volatile ("decl %%gs:%P0 \n\t"
265 "call _kernel_preempt_check \n\t"
268 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
269 : "eax", "ecx", "edx", "cc", "memory");
273 enable_preemption_no_check(void)
275 assert(get_preemption_level() > 0);
277 __asm__
volatile ("decl %%gs:%P0"
279 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
284 mp_disable_preemption(void)
286 disable_preemption();
290 mp_enable_preemption(void)
296 mp_enable_preemption_no_check(void)
298 enable_preemption_no_check();
301 static inline cpu_data_t
*
302 current_cpu_datap(void)
304 CPU_DATA_GET(cpu_this
, cpu_data_t
*);
307 static inline cpu_data_t
*
310 assert(cpu_data_ptr
[cpu
]);
311 return cpu_data_ptr
[cpu
];
314 extern cpu_data_t
*cpu_data_alloc(boolean_t is_boot_cpu
);
316 #else /* !defined(__GNUC__) */
318 #endif /* defined(__GNUC__) */
320 #endif /* I386_CPU_DATA */