2 * Copyright (c) 2000-2012 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>
38 #include <kern/assert.h>
39 #include <kern/kern_types.h>
40 #include <kern/queue.h>
41 #include <kern/processor.h>
43 #include <pexpert/pexpert.h>
44 #include <mach/i386/thread_status.h>
45 #include <mach/i386/vm_param.h>
46 #include <i386/locks.h>
47 #include <i386/rtclock_protos.h>
48 #include <i386/pmCPU.h>
49 #include <i386/cpu_topology.h>
52 #include <i386/vmx/vmx_cpu.h>
55 #include <machine/pal_routines.h>
58 * Data structures referenced (anonymously) from per-cpu data:
60 struct cpu_cons_buffer
;
61 struct cpu_desc_table
;
66 * Data structures embedded in per-cpu data:
68 typedef struct rtclock_timer
{
72 boolean_t has_expired
;
77 struct x86_64_tss
*cdi_ktss
;
78 struct __attribute__((packed
)) {
82 struct fake_descriptor
*cdi_ldt
;
87 TASK_MAP_32BIT
, /* 32-bit user, compatibility mode */
88 TASK_MAP_64BIT
, /* 64-bit user thread, shared 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
;
104 typedef uint16_t pcid_t
;
105 typedef uint8_t pcid_ref_t
;
107 #define CPU_RTIME_BINS (12)
108 #define CPU_ITIME_BINS (CPU_RTIME_BINS)
113 * Each processor has a per-cpu data area which is dereferenced through the
114 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
115 * using this, inlines provides single-instruction access to frequently used
116 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
119 * Cpu data owned by another processor can be accessed using the
120 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
123 typedef struct cpu_data
125 struct pal_cpu_data cpu_pal_data
; /* PAL-specific data */
126 #define cpu_pd cpu_pal_data /* convenience alias */
127 struct cpu_data
*cpu_this
; /* pointer to myself */
128 thread_t cpu_active_thread
;
129 thread_t cpu_nthread
;
130 volatile int cpu_preemption_level
;
131 int cpu_number
; /* Logical CPU */
132 void *cpu_int_state
; /* interrupt state */
133 vm_offset_t cpu_active_stack
; /* kernel stack base */
134 vm_offset_t cpu_kernel_stack
; /* kernel stack top */
135 vm_offset_t cpu_int_stack_top
;
136 int cpu_interrupt_level
;
137 int cpu_phys_number
; /* Physical CPU */
138 cpu_id_t cpu_id
; /* Platform Expert */
139 volatile int cpu_signals
; /* IPI events */
140 volatile int cpu_prior_signals
; /* Last set of events,
143 ast_t cpu_pending_ast
;
144 volatile int cpu_running
;
145 boolean_t cpu_fixed_pmcs_enabled
;
146 rtclock_timer_t rtclock_timer
;
147 volatile addr64_t cpu_active_cr3
__attribute((aligned(64)));
149 volatile uint32_t cpu_tlb_invalid
;
151 volatile uint16_t cpu_tlb_invalid_local
;
152 volatile uint16_t cpu_tlb_invalid_global
;
155 volatile task_map_t cpu_task_map
;
156 volatile addr64_t cpu_task_cr3
;
157 addr64_t cpu_kernel_cr3
;
160 void *cpu_console_buf
;
161 struct x86_lcpu lcpu
;
162 struct processor
*cpu_processor
;
163 #if NCOPY_WINDOWS > 0
164 struct cpu_pmap
*cpu_pmap
;
166 struct cpu_desc_table
*cpu_desc_tablep
;
167 struct fake_descriptor
*cpu_ldtp
;
168 cpu_desc_index_t cpu_desc_index
;
170 #if NCOPY_WINDOWS > 0
171 vm_offset_t cpu_copywindow_base
;
172 uint64_t *cpu_copywindow_pdp
;
174 vm_offset_t cpu_physwindow_base
;
175 uint64_t *cpu_physwindow_ptep
;
178 #define HWINTCNT_SIZE 256
179 uint32_t cpu_hwIntCnt
[HWINTCNT_SIZE
]; /* Interrupt counts */
180 uint64_t cpu_hwIntpexits
[HWINTCNT_SIZE
];
181 uint64_t cpu_hwIntcexits
[HWINTCNT_SIZE
];
182 uint64_t cpu_dr7
; /* debug control register */
183 uint64_t cpu_int_event_time
; /* intr entry/exit time */
184 pal_rtc_nanotime_t
*cpu_nanotime
; /* Nanotime info */
186 /* double-buffered performance counter data */
187 uint64_t *cpu_kpc_buf
[2];
188 /* PMC shadow and reload value buffers */
189 uint64_t *cpu_kpc_shadow
;
190 uint64_t *cpu_kpc_reload
;
192 uint32_t cpu_pmap_pcid_enabled
;
193 pcid_t cpu_active_pcid
;
194 pcid_t cpu_last_pcid
;
195 volatile pcid_ref_t
*cpu_pmap_pcid_coherentp
;
196 volatile pcid_ref_t
*cpu_pmap_pcid_coherentp_kernel
;
197 #define PMAP_PCID_MAX_PCID (0x1000)
198 pcid_t cpu_pcid_free_hint
;
199 pcid_ref_t cpu_pcid_refcounts
[PMAP_PCID_MAX_PCID
];
200 pmap_t cpu_pcid_last_pmap_dispatched
[PMAP_PCID_MAX_PCID
];
202 uint64_t cpu_pmap_pcid_flushes
;
203 uint64_t cpu_pmap_pcid_preserves
;
210 uint64_t cpu_itime_total
;
211 uint64_t cpu_rtime_total
;
213 uint64_t cpu_idle_exits
;
214 uint64_t cpu_rtimes
[CPU_RTIME_BINS
];
215 uint64_t cpu_itimes
[CPU_ITIME_BINS
];
216 uint64_t cpu_cur_insns
;
217 uint64_t cpu_cur_ucc
;
218 uint64_t cpu_cur_urc
;
219 uint64_t cpu_gpmcs
[4];
220 uint64_t cpu_max_observed_int_latency
;
221 int cpu_max_observed_int_latency_vector
;
222 volatile boolean_t cpu_NMI_acknowledged
;
223 uint64_t debugger_entry_time
;
224 uint64_t debugger_ipi_time
;
225 /* A separate nested interrupt stack flag, to account
226 * for non-nested interrupts arriving while on the interrupt stack
227 * Currently only occurs when AICPM enables interrupts on the
228 * interrupt stack during processor offlining.
230 uint32_t cpu_nested_istack
;
231 uint32_t cpu_nested_istack_events
;
232 x86_saved_state64_t
*cpu_fatal_trap_state
;
233 x86_saved_state64_t
*cpu_post_fatal_trap_state
;
235 vmx_cpu_t cpu_vmx
; /* wonderful world of virtualization */
238 struct mca_state
*cpu_mca_state
; /* State at MC fault */
240 struct prngContext
*cpu_prng
; /* PRNG's context */
245 boolean_t cpu_boot_complete
;
249 extern cpu_data_t
*cpu_data_ptr
[];
251 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
252 #if defined(__clang__)
253 #define GS_RELATIVE volatile __attribute__((address_space(256)))
255 #define offsetof(TYPE,MEMBER) __builtin_offsetof(TYPE,MEMBER)
258 #define CPU_DATA_GET(member,type) \
259 cpu_data_t GS_RELATIVE *cpu_data = \
260 (cpu_data_t GS_RELATIVE *)0UL; \
262 ret = cpu_data->member; \
265 #define CPU_DATA_GET_INDEX(member,index,type) \
266 cpu_data_t GS_RELATIVE *cpu_data = \
267 (cpu_data_t GS_RELATIVE *)0UL; \
269 ret = cpu_data->member[index]; \
272 #define CPU_DATA_SET(member,value) \
273 cpu_data_t GS_RELATIVE *cpu_data = \
274 (cpu_data_t GS_RELATIVE *)0UL; \
275 cpu_data->member = value;
277 #define CPU_DATA_XCHG(member,value,type) \
278 cpu_data_t GS_RELATIVE *cpu_data = \
279 (cpu_data_t GS_RELATIVE *)0UL; \
281 ret = cpu_data->member; \
282 cpu_data->member = value; \
285 #else /* !defined(__clang__) */
288 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
289 #endif /* offsetof */
290 #define CPU_DATA_GET(member,type) \
292 __asm__ volatile ("mov %%gs:%P1,%0" \
294 : "i" (offsetof(cpu_data_t,member))); \
297 #define CPU_DATA_GET_INDEX(member,index,type) \
299 __asm__ volatile ("mov %%gs:(%1),%0" \
301 : "r" (offsetof(cpu_data_t,member[index]))); \
304 #define CPU_DATA_SET(member,value) \
305 __asm__ volatile ("mov %0,%%gs:%P1" \
307 : "r" (value), "i" (offsetof(cpu_data_t,member)));
309 #define CPU_DATA_XCHG(member,value,type) \
311 __asm__ volatile ("xchg %0,%%gs:%P1" \
313 : "i" (offsetof(cpu_data_t,member)), "0" (value)); \
316 #endif /* !defined(__clang__) */
319 * Everyone within the osfmk part of the kernel can use the fast
320 * inline versions of these routines. Everyone outside, must call
323 static inline thread_t
324 get_active_thread(void)
326 CPU_DATA_GET(cpu_active_thread
,thread_t
)
328 #define current_thread_fast() get_active_thread()
329 #define current_thread() current_thread_fast()
331 #define cpu_mode_is64bit() TRUE
334 get_preemption_level(void)
336 CPU_DATA_GET(cpu_preemption_level
,int)
339 get_interrupt_level(void)
341 CPU_DATA_GET(cpu_interrupt_level
,int)
346 CPU_DATA_GET(cpu_number
,int)
349 get_cpu_phys_number(void)
351 CPU_DATA_GET(cpu_phys_number
,int)
356 disable_preemption(void)
358 #if defined(__clang__)
359 cpu_data_t GS_RELATIVE
*cpu_data
= (cpu_data_t GS_RELATIVE
*)0UL;
360 cpu_data
->cpu_preemption_level
++;
362 __asm__
volatile ("incl %%gs:%P0"
364 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
)));
369 enable_preemption(void)
371 assert(get_preemption_level() > 0);
373 #if defined(__clang__)
374 cpu_data_t GS_RELATIVE
*cpu_data
= (cpu_data_t GS_RELATIVE
*)0UL;
375 if (0 == --cpu_data
->cpu_preemption_level
)
376 kernel_preempt_check();
378 __asm__
volatile ("decl %%gs:%P0 \n\t"
380 "call _kernel_preempt_check \n\t"
383 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
384 : "eax", "ecx", "edx", "cc", "memory");
389 enable_preemption_no_check(void)
391 assert(get_preemption_level() > 0);
393 #if defined(__clang__)
394 cpu_data_t GS_RELATIVE
*cpu_data
= (cpu_data_t GS_RELATIVE
*)0UL;
395 cpu_data
->cpu_preemption_level
--;
397 __asm__
volatile ("decl %%gs:%P0"
399 : "i" (offsetof(cpu_data_t
, cpu_preemption_level
))
405 mp_disable_preemption(void)
407 disable_preemption();
411 mp_enable_preemption(void)
417 mp_enable_preemption_no_check(void)
419 enable_preemption_no_check();
422 static inline cpu_data_t
*
423 current_cpu_datap(void)
425 CPU_DATA_GET(cpu_this
, cpu_data_t
*);
428 static inline cpu_data_t
*
431 return cpu_data_ptr
[cpu
];
434 extern cpu_data_t
*cpu_data_alloc(boolean_t is_boot_cpu
);
435 extern void cpu_data_realloc(void);
437 #endif /* I386_CPU_DATA */