2 * Copyright (c) 2000-2004 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>
37 #include <pexpert/pexpert.h>
41 * Data structures referenced (anonymously) from per-cpu data:
44 struct cpu_cons_buffer
;
49 * Data structures embedded in per-cpu data:
51 typedef struct rtclock_timer
{
54 boolean_t has_expired
;
58 uint64_t rnt_tsc
; /* timestamp */
59 uint64_t rnt_nanos
; /* nanoseconds */
60 uint32_t rnt_scale
; /* tsc -> nanosec multiplier */
61 uint32_t rnt_shift
; /* tsc -> nanosec shift/div */
62 uint64_t rnt_step_tsc
; /* tsc when scale applied */
63 uint64_t rnt_step_nanos
; /* ns when scale applied */
67 struct i386_tss
*cdi_ktss
;
69 struct i386_tss
*cdi_dbtss
;
71 struct fake_descriptor
*cdi_gdt
;
72 struct fake_descriptor
*cdi_idt
;
73 struct fake_descriptor
*cdi_ldt
;
80 * Each processor has a per-cpu data area which is dereferenced through the
81 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
82 * using this, inlines provides single-instruction access to frequently used
83 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
86 * Cpu data owned by another processor can be accessed using the
87 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
90 typedef struct cpu_data
92 struct cpu_data
*cpu_this
; /* pointer to myself */
93 thread_t cpu_active_thread
;
94 thread_t cpu_active_kloaded
;
95 vm_offset_t cpu_active_stack
;
96 vm_offset_t cpu_kernel_stack
;
97 vm_offset_t cpu_int_stack_top
;
98 int cpu_preemption_level
;
99 int cpu_simple_lock_count
;
100 int cpu_interrupt_level
;
101 int cpu_number
; /* Logical CPU */
102 int cpu_phys_number
; /* Physical CPU */
103 cpu_id_t cpu_id
; /* Platform Expert */
104 int cpu_signals
; /* IPI events */
105 int cpu_mcount_off
; /* mcount recursion */
106 ast_t cpu_pending_ast
;
111 struct cpu_core
*cpu_core
; /* cpu's parent core */
112 uint64_t cpu_rtc_tick_deadline
;
113 uint64_t cpu_rtc_intr_deadline
;
114 rtclock_timer_t cpu_rtc_timer
;
115 rtc_nanotime_t cpu_rtc_nanotime
;
116 void *cpu_console_buf
;
117 struct processor
*cpu_processor
;
118 struct cpu_pmap
*cpu_pmap
;
119 struct mp_desc_table
*cpu_desc_tablep
;
120 cpu_desc_index_t cpu_desc_index
;
124 int cpu_db_pass_thru
;
125 vm_offset_t cpu_db_stacks
;
126 struct i386_saved_state
*cpu_kdb_saved_state
;
127 spl_t cpu_kdb_saved_ipl
;
128 int cpu_kdb_is_slave
;
130 #endif /* MACH_KDB */
134 extern cpu_data_t
*cpu_data_ptr
[];
135 extern cpu_data_t cpu_data_master
;
137 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
138 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
139 #define CPU_DATA_GET(member,type) \
141 __asm__ volatile ("movl %%gs:%P1,%0" \
143 : "i" (offsetof(cpu_data_t,member))); \
147 * Everyone within the osfmk part of the kernel can use the fast
148 * inline versions of these routines. Everyone outside, must call
151 static inline thread_t
152 get_active_thread(void)
154 CPU_DATA_GET(cpu_active_thread
,thread_t
)
156 #define current_thread_fast() get_active_thread()
157 #define current_thread() current_thread_fast()
160 get_preemption_level(void)
162 CPU_DATA_GET(cpu_preemption_level
,int)
165 get_simple_lock_count(void)
167 CPU_DATA_GET(cpu_simple_lock_count
,int)
170 get_interrupt_level(void)
172 CPU_DATA_GET(cpu_interrupt_level
,int)
177 CPU_DATA_GET(cpu_number
,int)
180 get_cpu_phys_number(void)
182 CPU_DATA_GET(cpu_phys_number
,int)
185 cpu_core
* get_cpu_core(void)
187 CPU_DATA_GET(cpu_core
,struct cpu_core
*)
191 disable_preemption(void)
193 __asm__
volatile ("incl %%gs:%P0"
195 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
)));
199 enable_preemption(void)
201 assert(get_preemption_level() > 0);
203 __asm__
volatile ("decl %%gs:%P0 \n\t"
205 "call _kernel_preempt_check \n\t"
208 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
209 : "eax", "ecx", "edx", "cc", "memory");
213 enable_preemption_no_check(void)
215 assert(get_preemption_level() > 0);
217 __asm__
volatile ("decl %%gs:%P0"
219 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
224 mp_disable_preemption(void)
226 disable_preemption();
230 mp_enable_preemption(void)
236 mp_enable_preemption_no_check(void)
238 enable_preemption_no_check();
241 static inline cpu_data_t
*
242 current_cpu_datap(void)
244 CPU_DATA_GET(cpu_this
, cpu_data_t
*);
247 static inline cpu_data_t
*
250 assert(cpu_data_ptr
[cpu
]);
251 return cpu_data_ptr
[cpu
];
254 extern cpu_data_t
*cpu_data_alloc(boolean_t is_boot_cpu
);
256 #else /* !defined(__GNUC__) */
258 #endif /* defined(__GNUC__) */
260 #endif /* I386_CPU_DATA */