2 * Copyright (c) 2000-2004 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>
45 #include <pexpert/pexpert.h>
49 * Data structures referenced (anonymously) from per-cpu data:
52 struct cpu_cons_buffer
;
57 * Data structures embedded in per-cpu data:
59 typedef struct rtclock_timer
{
62 boolean_t has_expired
;
66 uint64_t rnt_tsc
; /* timestamp */
67 uint64_t rnt_nanos
; /* nanoseconds */
68 uint32_t rnt_scale
; /* tsc -> nanosec multiplier */
69 uint32_t rnt_shift
; /* tsc -> nanosec shift/div */
70 uint64_t rnt_step_tsc
; /* tsc when scale applied */
71 uint64_t rnt_step_nanos
; /* ns when scale applied */
75 struct i386_tss
*cdi_ktss
;
77 struct i386_tss
*cdi_dbtss
;
79 struct fake_descriptor
*cdi_gdt
;
80 struct fake_descriptor
*cdi_idt
;
81 struct fake_descriptor
*cdi_ldt
;
88 * Each processor has a per-cpu data area which is dereferenced through the
89 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
90 * using this, inlines provides single-instruction access to frequently used
91 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
94 * Cpu data owned by another processor can be accessed using the
95 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
98 typedef struct cpu_data
100 struct cpu_data
*cpu_this
; /* pointer to myself */
101 thread_t cpu_active_thread
;
102 thread_t cpu_active_kloaded
;
103 vm_offset_t cpu_active_stack
;
104 vm_offset_t cpu_kernel_stack
;
105 vm_offset_t cpu_int_stack_top
;
106 int cpu_preemption_level
;
107 int cpu_simple_lock_count
;
108 int cpu_interrupt_level
;
109 int cpu_number
; /* Logical CPU */
110 int cpu_phys_number
; /* Physical CPU */
111 cpu_id_t cpu_id
; /* Platform Expert */
112 int cpu_signals
; /* IPI events */
113 int cpu_mcount_off
; /* mcount recursion */
114 ast_t cpu_pending_ast
;
119 struct cpu_core
*cpu_core
; /* cpu's parent core */
120 uint64_t cpu_rtc_tick_deadline
;
121 uint64_t cpu_rtc_intr_deadline
;
122 rtclock_timer_t cpu_rtc_timer
;
123 rtc_nanotime_t cpu_rtc_nanotime
;
124 void *cpu_console_buf
;
125 struct processor
*cpu_processor
;
126 struct cpu_pmap
*cpu_pmap
;
127 struct mp_desc_table
*cpu_desc_tablep
;
128 cpu_desc_index_t cpu_desc_index
;
132 int cpu_db_pass_thru
;
133 vm_offset_t cpu_db_stacks
;
134 struct i386_saved_state
*cpu_kdb_saved_state
;
135 spl_t cpu_kdb_saved_ipl
;
136 int cpu_kdb_is_slave
;
138 #endif /* MACH_KDB */
142 extern cpu_data_t
*cpu_data_ptr
[];
143 extern cpu_data_t cpu_data_master
;
145 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
146 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
147 #define CPU_DATA_GET(member,type) \
149 __asm__ volatile ("movl %%gs:%P1,%0" \
151 : "i" (offsetof(cpu_data_t,member))); \
155 * Everyone within the osfmk part of the kernel can use the fast
156 * inline versions of these routines. Everyone outside, must call
159 static inline thread_t
160 get_active_thread(void)
162 CPU_DATA_GET(cpu_active_thread
,thread_t
)
164 #define current_thread_fast() get_active_thread()
165 #define current_thread() current_thread_fast()
168 get_preemption_level(void)
170 CPU_DATA_GET(cpu_preemption_level
,int)
173 get_simple_lock_count(void)
175 CPU_DATA_GET(cpu_simple_lock_count
,int)
178 get_interrupt_level(void)
180 CPU_DATA_GET(cpu_interrupt_level
,int)
185 CPU_DATA_GET(cpu_number
,int)
188 get_cpu_phys_number(void)
190 CPU_DATA_GET(cpu_phys_number
,int)
193 cpu_core
* get_cpu_core(void)
195 CPU_DATA_GET(cpu_core
,struct cpu_core
*)
199 disable_preemption(void)
201 __asm__
volatile ("incl %%gs:%P0"
203 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
)));
207 enable_preemption(void)
209 assert(get_preemption_level() > 0);
211 __asm__
volatile ("decl %%gs:%P0 \n\t"
213 "call _kernel_preempt_check \n\t"
216 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
217 : "eax", "ecx", "edx", "cc", "memory");
221 enable_preemption_no_check(void)
223 assert(get_preemption_level() > 0);
225 __asm__
volatile ("decl %%gs:%P0"
227 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
232 mp_disable_preemption(void)
234 disable_preemption();
238 mp_enable_preemption(void)
244 mp_enable_preemption_no_check(void)
246 enable_preemption_no_check();
249 static inline cpu_data_t
*
250 current_cpu_datap(void)
252 CPU_DATA_GET(cpu_this
, cpu_data_t
*);
255 static inline cpu_data_t
*
258 assert(cpu_data_ptr
[cpu
]);
259 return cpu_data_ptr
[cpu
];
262 extern cpu_data_t
*cpu_data_alloc(boolean_t is_boot_cpu
);
264 #else /* !defined(__GNUC__) */
266 #endif /* defined(__GNUC__) */
268 #endif /* I386_CPU_DATA */