]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cpu_data.h
xnu-2782.20.48.tar.gz
[apple/xnu.git] / osfmk / i386 / cpu_data.h
1 /*
2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * @OSF_COPYRIGHT@
30 *
31 */
32
33 #ifndef I386_CPU_DATA
34 #define I386_CPU_DATA
35
36 #include <mach_assert.h>
37
38 #include <kern/assert.h>
39 #include <kern/kern_types.h>
40 #include <kern/queue.h>
41 #include <kern/processor.h>
42 #include <kern/pms.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>
50
51 #if CONFIG_VMX
52 #include <i386/vmx/vmx_cpu.h>
53 #endif
54
55 #include <machine/pal_routines.h>
56
57 /*
58 * Data structures referenced (anonymously) from per-cpu data:
59 */
60 struct cpu_cons_buffer;
61 struct cpu_desc_table;
62 struct mca_state;
63 struct prngContext;
64
65 /*
66 * Data structures embedded in per-cpu data:
67 */
68 typedef struct rtclock_timer {
69 mpqueue_head_t queue;
70 uint64_t deadline;
71 uint64_t when_set;
72 boolean_t has_expired;
73 } rtclock_timer_t;
74
75
76 typedef struct {
77 struct x86_64_tss *cdi_ktss;
78 struct __attribute__((packed)) {
79 uint16_t size;
80 void *ptr;
81 } cdi_gdt, cdi_idt;
82 struct fake_descriptor *cdi_ldt;
83 vm_offset_t cdi_sstk;
84 } cpu_desc_index_t;
85
86 typedef enum {
87 TASK_MAP_32BIT, /* 32-bit user, compatibility mode */
88 TASK_MAP_64BIT, /* 64-bit user thread, shared space */
89 } task_map_t;
90
91
92 /*
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.
97 */
98 typedef struct {
99 addr64_t cu_isf; /* thread->pcb->iss.isf */
100 uint64_t cu_tmp; /* temporary scratch */
101 addr64_t cu_user_gs_base;
102 } cpu_uber_t;
103
104 typedef uint16_t pcid_t;
105 typedef uint8_t pcid_ref_t;
106
107 #define CPU_RTIME_BINS (12)
108 #define CPU_ITIME_BINS (CPU_RTIME_BINS)
109
110 /*
111 * Per-cpu data.
112 *
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()/
117 * current_thread().
118 *
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
121 * pointers.
122 */
123 typedef struct cpu_data
124 {
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,
141 * debugging
142 */
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)));
148 union {
149 volatile uint32_t cpu_tlb_invalid;
150 struct {
151 volatile uint16_t cpu_tlb_invalid_local;
152 volatile uint16_t cpu_tlb_invalid_global;
153 };
154 };
155 volatile task_map_t cpu_task_map;
156 volatile addr64_t cpu_task_cr3;
157 addr64_t cpu_kernel_cr3;
158 cpu_uber_t cpu_uber;
159 void *cpu_chud;
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;
165 #endif
166 struct cpu_desc_table *cpu_desc_tablep;
167 struct fake_descriptor *cpu_ldtp;
168 cpu_desc_index_t cpu_desc_index;
169 int cpu_ldt;
170 #if NCOPY_WINDOWS > 0
171 vm_offset_t cpu_copywindow_base;
172 uint64_t *cpu_copywindow_pdp;
173
174 vm_offset_t cpu_physwindow_base;
175 uint64_t *cpu_physwindow_ptep;
176 #endif
177
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 */
185 #if CONFIG_COUNTERS
186 thread_t csw_old_thread;
187 thread_t csw_new_thread;
188 #endif /* CONFIG COUNTERS */
189 #if KPC
190 /* double-buffered performance counter data */
191 uint64_t *cpu_kpc_buf[2];
192 /* PMC shadow and reload value buffers */
193 uint64_t *cpu_kpc_shadow;
194 uint64_t *cpu_kpc_reload;
195 #endif
196 uint32_t cpu_pmap_pcid_enabled;
197 pcid_t cpu_active_pcid;
198 pcid_t cpu_last_pcid;
199 volatile pcid_ref_t *cpu_pmap_pcid_coherentp;
200 volatile pcid_ref_t *cpu_pmap_pcid_coherentp_kernel;
201 #define PMAP_PCID_MAX_PCID (0x1000)
202 pcid_t cpu_pcid_free_hint;
203 pcid_ref_t cpu_pcid_refcounts[PMAP_PCID_MAX_PCID];
204 pmap_t cpu_pcid_last_pmap_dispatched[PMAP_PCID_MAX_PCID];
205 #ifdef PCID_STATS
206 uint64_t cpu_pmap_pcid_flushes;
207 uint64_t cpu_pmap_pcid_preserves;
208 #endif
209 uint64_t cpu_aperf;
210 uint64_t cpu_mperf;
211 uint64_t cpu_c3res;
212 uint64_t cpu_c6res;
213 uint64_t cpu_c7res;
214 uint64_t cpu_itime_total;
215 uint64_t cpu_rtime_total;
216 uint64_t cpu_ixtime;
217 uint64_t cpu_idle_exits;
218 uint64_t cpu_rtimes[CPU_RTIME_BINS];
219 uint64_t cpu_itimes[CPU_ITIME_BINS];
220 uint64_t cpu_cur_insns;
221 uint64_t cpu_cur_ucc;
222 uint64_t cpu_cur_urc;
223 uint64_t cpu_gpmcs[4];
224 uint64_t cpu_max_observed_int_latency;
225 int cpu_max_observed_int_latency_vector;
226 volatile boolean_t cpu_NMI_acknowledged;
227 uint64_t debugger_entry_time;
228 uint64_t debugger_ipi_time;
229 /* A separate nested interrupt stack flag, to account
230 * for non-nested interrupts arriving while on the interrupt stack
231 * Currently only occurs when AICPM enables interrupts on the
232 * interrupt stack during processor offlining.
233 */
234 uint32_t cpu_nested_istack;
235 uint32_t cpu_nested_istack_events;
236 x86_saved_state64_t *cpu_fatal_trap_state;
237 x86_saved_state64_t *cpu_post_fatal_trap_state;
238 #if CONFIG_VMX
239 vmx_cpu_t cpu_vmx; /* wonderful world of virtualization */
240 #endif
241 #if CONFIG_MCA
242 struct mca_state *cpu_mca_state; /* State at MC fault */
243 #endif
244 struct prngContext *cpu_prng; /* PRNG's context */
245 int cpu_type;
246 int cpu_subtype;
247 int cpu_threadtype;
248 boolean_t cpu_iflag;
249 boolean_t cpu_boot_complete;
250 int cpu_hibernate;
251 } cpu_data_t;
252
253 extern cpu_data_t *cpu_data_ptr[];
254
255 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
256 #if defined(__clang__)
257 #define GS_RELATIVE volatile __attribute__((address_space(256)))
258 #ifndef offsetof
259 #define offsetof(TYPE,MEMBER) __builtin_offsetof(TYPE,MEMBER)
260 #endif
261
262 #define CPU_DATA_GET(member,type) \
263 cpu_data_t GS_RELATIVE *cpu_data = \
264 (cpu_data_t GS_RELATIVE *)0UL; \
265 type ret; \
266 ret = cpu_data->member; \
267 return ret;
268
269 #define CPU_DATA_GET_INDEX(member,index,type) \
270 cpu_data_t GS_RELATIVE *cpu_data = \
271 (cpu_data_t GS_RELATIVE *)0UL; \
272 type ret; \
273 ret = cpu_data->member[index]; \
274 return ret;
275
276 #define CPU_DATA_SET(member,value) \
277 cpu_data_t GS_RELATIVE *cpu_data = \
278 (cpu_data_t GS_RELATIVE *)0UL; \
279 cpu_data->member = value;
280
281 #define CPU_DATA_XCHG(member,value,type) \
282 cpu_data_t GS_RELATIVE *cpu_data = \
283 (cpu_data_t GS_RELATIVE *)0UL; \
284 type ret; \
285 ret = cpu_data->member; \
286 cpu_data->member = value; \
287 return ret;
288
289 #else /* !defined(__clang__) */
290
291 #ifndef offsetof
292 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
293 #endif /* offsetof */
294 #define CPU_DATA_GET(member,type) \
295 type ret; \
296 __asm__ volatile ("mov %%gs:%P1,%0" \
297 : "=r" (ret) \
298 : "i" (offsetof(cpu_data_t,member))); \
299 return ret;
300
301 #define CPU_DATA_GET_INDEX(member,index,type) \
302 type ret; \
303 __asm__ volatile ("mov %%gs:(%1),%0" \
304 : "=r" (ret) \
305 : "r" (offsetof(cpu_data_t,member[index]))); \
306 return ret;
307
308 #define CPU_DATA_SET(member,value) \
309 __asm__ volatile ("mov %0,%%gs:%P1" \
310 : \
311 : "r" (value), "i" (offsetof(cpu_data_t,member)));
312
313 #define CPU_DATA_XCHG(member,value,type) \
314 type ret; \
315 __asm__ volatile ("xchg %0,%%gs:%P1" \
316 : "=r" (ret) \
317 : "i" (offsetof(cpu_data_t,member)), "0" (value)); \
318 return ret;
319
320 #endif /* !defined(__clang__) */
321
322 /*
323 * Everyone within the osfmk part of the kernel can use the fast
324 * inline versions of these routines. Everyone outside, must call
325 * the real thing,
326 */
327 static inline thread_t
328 get_active_thread(void)
329 {
330 CPU_DATA_GET(cpu_active_thread,thread_t)
331 }
332 #define current_thread_fast() get_active_thread()
333 #define current_thread() current_thread_fast()
334
335 #define cpu_mode_is64bit() TRUE
336
337 static inline int
338 get_preemption_level(void)
339 {
340 CPU_DATA_GET(cpu_preemption_level,int)
341 }
342 static inline int
343 get_interrupt_level(void)
344 {
345 CPU_DATA_GET(cpu_interrupt_level,int)
346 }
347 static inline int
348 get_cpu_number(void)
349 {
350 CPU_DATA_GET(cpu_number,int)
351 }
352 static inline int
353 get_cpu_phys_number(void)
354 {
355 CPU_DATA_GET(cpu_phys_number,int)
356 }
357
358
359 static inline void
360 disable_preemption(void)
361 {
362 #if defined(__clang__)
363 cpu_data_t GS_RELATIVE *cpu_data = (cpu_data_t GS_RELATIVE *)0UL;
364 cpu_data->cpu_preemption_level++;
365 #else
366 __asm__ volatile ("incl %%gs:%P0"
367 :
368 : "i" (offsetof(cpu_data_t, cpu_preemption_level)));
369 #endif
370 }
371
372 static inline void
373 enable_preemption(void)
374 {
375 assert(get_preemption_level() > 0);
376
377 #if defined(__clang__)
378 cpu_data_t GS_RELATIVE *cpu_data = (cpu_data_t GS_RELATIVE *)0UL;
379 if (0 == --cpu_data->cpu_preemption_level)
380 kernel_preempt_check();
381 #else
382 __asm__ volatile ("decl %%gs:%P0 \n\t"
383 "jne 1f \n\t"
384 "call _kernel_preempt_check \n\t"
385 "1:"
386 : /* no outputs */
387 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
388 : "eax", "ecx", "edx", "cc", "memory");
389 #endif
390 }
391
392 static inline void
393 enable_preemption_no_check(void)
394 {
395 assert(get_preemption_level() > 0);
396
397 #if defined(__clang__)
398 cpu_data_t GS_RELATIVE *cpu_data = (cpu_data_t GS_RELATIVE *)0UL;
399 cpu_data->cpu_preemption_level--;
400 #else
401 __asm__ volatile ("decl %%gs:%P0"
402 : /* no outputs */
403 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
404 : "cc", "memory");
405 #endif
406 }
407
408 static inline void
409 mp_disable_preemption(void)
410 {
411 disable_preemption();
412 }
413
414 static inline void
415 mp_enable_preemption(void)
416 {
417 enable_preemption();
418 }
419
420 static inline void
421 mp_enable_preemption_no_check(void)
422 {
423 enable_preemption_no_check();
424 }
425
426 static inline cpu_data_t *
427 current_cpu_datap(void)
428 {
429 CPU_DATA_GET(cpu_this, cpu_data_t *);
430 }
431
432 static inline cpu_data_t *
433 cpu_datap(int cpu)
434 {
435 return cpu_data_ptr[cpu];
436 }
437
438 extern cpu_data_t *cpu_data_alloc(boolean_t is_boot_cpu);
439 extern void cpu_data_realloc(void);
440
441 #endif /* I386_CPU_DATA */