]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cpu_data.h
ff08d74937aa32cab85d7097f1aeba0df4500661
[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 #include <i386/seg.h>
51
52 #if CONFIG_VMX
53 #include <i386/vmx/vmx_cpu.h>
54 #endif
55
56 #if MONOTONIC
57 #include <machine/monotonic.h>
58 #endif /* MONOTONIC */
59
60 #include <machine/pal_routines.h>
61
62 /*
63 * Data structures referenced (anonymously) from per-cpu data:
64 */
65 struct cpu_cons_buffer;
66 struct cpu_desc_table;
67 struct mca_state;
68 struct prngContext;
69
70 /*
71 * Data structures embedded in per-cpu data:
72 */
73 typedef struct rtclock_timer {
74 mpqueue_head_t queue;
75 uint64_t deadline;
76 uint64_t when_set;
77 boolean_t has_expired;
78 } rtclock_timer_t;
79
80 typedef struct {
81 /* The 'u' suffixed fields store the double-mapped descriptor addresses */
82 struct x86_64_tss *cdi_ktssu;
83 struct x86_64_tss *cdi_ktssb;
84 x86_64_desc_register_t cdi_gdtu;
85 x86_64_desc_register_t cdi_gdtb;
86 x86_64_desc_register_t cdi_idtu;
87 x86_64_desc_register_t cdi_idtb;
88 struct fake_descriptor *cdi_ldtu;
89 struct fake_descriptor *cdi_ldtb;
90 vm_offset_t cdi_sstku;
91 vm_offset_t cdi_sstkb;
92 } cpu_desc_index_t;
93
94 typedef enum {
95 TASK_MAP_32BIT, /* 32-bit user, compatibility mode */
96 TASK_MAP_64BIT, /* 64-bit user thread, shared space */
97 } task_map_t;
98
99
100 /*
101 * This structure is used on entry into the (uber-)kernel on syscall from
102 * a 64-bit user. It contains the address of the machine state save area
103 * for the current thread and a temporary place to save the user's rsp
104 * before loading this address into rsp.
105 */
106 typedef struct {
107 addr64_t cu_isf; /* thread->pcb->iss.isf */
108 uint64_t cu_tmp; /* temporary scratch */
109 addr64_t cu_user_gs_base;
110 } cpu_uber_t;
111
112 typedef uint16_t pcid_t;
113 typedef uint8_t pcid_ref_t;
114
115 #define CPU_RTIME_BINS (12)
116 #define CPU_ITIME_BINS (CPU_RTIME_BINS)
117
118 #define MAXPLFRAMES (16)
119 typedef struct {
120 boolean_t pltype;
121 int plevel;
122 uint64_t plbt[MAXPLFRAMES];
123 } plrecord_t;
124
125 /*
126 * Per-cpu data.
127 *
128 * Each processor has a per-cpu data area which is dereferenced through the
129 * current_cpu_datap() macro. For speed, the %gs segment is based here, and
130 * using this, inlines provides single-instruction access to frequently used
131 * members - such as get_cpu_number()/cpu_number(), and get_active_thread()/
132 * current_thread().
133 *
134 * Cpu data owned by another processor can be accessed using the
135 * cpu_datap(cpu_number) macro which uses the cpu_data_ptr[] array of per-cpu
136 * pointers.
137 */
138 typedef struct {
139 pcid_t cpu_pcid_free_hint;
140 #define PMAP_PCID_MAX_PCID (0x800)
141 pcid_ref_t cpu_pcid_refcounts[PMAP_PCID_MAX_PCID];
142 pmap_t cpu_pcid_last_pmap_dispatched[PMAP_PCID_MAX_PCID];
143 } pcid_cdata_t;
144
145 typedef struct cpu_data
146 {
147 struct pal_cpu_data cpu_pal_data; /* PAL-specific data */
148 #define cpu_pd cpu_pal_data /* convenience alias */
149 struct cpu_data *cpu_this; /* pointer to myself */
150 thread_t cpu_active_thread;
151 thread_t cpu_nthread;
152 volatile int cpu_preemption_level;
153 int cpu_number; /* Logical CPU */
154 void *cpu_int_state; /* interrupt state */
155 vm_offset_t cpu_active_stack; /* kernel stack base */
156 vm_offset_t cpu_kernel_stack; /* kernel stack top */
157 vm_offset_t cpu_int_stack_top;
158 int cpu_interrupt_level;
159 volatile int cpu_signals; /* IPI events */
160 volatile int cpu_prior_signals; /* Last set of events,
161 * debugging
162 */
163 ast_t cpu_pending_ast;
164 volatile int cpu_running;
165 #if !MONOTONIC
166 boolean_t cpu_fixed_pmcs_enabled;
167 #endif /* !MONOTONIC */
168 rtclock_timer_t rtclock_timer;
169 uint64_t quantum_timer_deadline;
170 volatile addr64_t cpu_active_cr3 __attribute((aligned(64)));
171 union {
172 volatile uint32_t cpu_tlb_invalid;
173 struct {
174 volatile uint16_t cpu_tlb_invalid_local;
175 volatile uint16_t cpu_tlb_invalid_global;
176 };
177 };
178 volatile task_map_t cpu_task_map;
179 volatile addr64_t cpu_task_cr3;
180 addr64_t cpu_kernel_cr3;
181 volatile addr64_t cpu_ucr3;
182 boolean_t cpu_pagezero_mapped;
183 cpu_uber_t cpu_uber;
184 /* Double-mapped per-CPU exception stack address */
185 uintptr_t cd_estack;
186 /* Address of shadowed, partially mirrored CPU data structures located
187 * in the double mapped PML4
188 */
189 void *cd_shadow;
190 struct processor *cpu_processor;
191 #if NCOPY_WINDOWS > 0
192 struct cpu_pmap *cpu_pmap;
193 #endif
194 struct real_descriptor *cpu_ldtp;
195 struct cpu_desc_table *cpu_desc_tablep;
196 cpu_desc_index_t cpu_desc_index;
197 int cpu_ldt;
198 #if NCOPY_WINDOWS > 0
199 vm_offset_t cpu_copywindow_base;
200 uint64_t *cpu_copywindow_pdp;
201
202 vm_offset_t cpu_physwindow_base;
203 uint64_t *cpu_physwindow_ptep;
204 #endif
205
206 #define HWINTCNT_SIZE 256
207 uint32_t cpu_hwIntCnt[HWINTCNT_SIZE]; /* Interrupt counts */
208 uint64_t cpu_hwIntpexits[HWINTCNT_SIZE];
209 uint64_t cpu_dr7; /* debug control register */
210 uint64_t cpu_int_event_time; /* intr entry/exit time */
211 pal_rtc_nanotime_t *cpu_nanotime; /* Nanotime info */
212 #if KPC
213 /* double-buffered performance counter data */
214 uint64_t *cpu_kpc_buf[2];
215 /* PMC shadow and reload value buffers */
216 uint64_t *cpu_kpc_shadow;
217 uint64_t *cpu_kpc_reload;
218 #endif
219 #if MONOTONIC
220 struct mt_cpu cpu_monotonic;
221 #endif /* MONOTONIC */
222 uint32_t cpu_pmap_pcid_enabled;
223 pcid_t cpu_active_pcid;
224 pcid_t cpu_last_pcid;
225 pcid_t cpu_kernel_pcid;
226 volatile pcid_ref_t *cpu_pmap_pcid_coherentp;
227 volatile pcid_ref_t *cpu_pmap_pcid_coherentp_kernel;
228 pcid_cdata_t *cpu_pcid_data;
229 #ifdef PCID_STATS
230 uint64_t cpu_pmap_pcid_flushes;
231 uint64_t cpu_pmap_pcid_preserves;
232 #endif
233 uint64_t cpu_aperf;
234 uint64_t cpu_mperf;
235 uint64_t cpu_c3res;
236 uint64_t cpu_c6res;
237 uint64_t cpu_c7res;
238 uint64_t cpu_itime_total;
239 uint64_t cpu_rtime_total;
240 uint64_t cpu_ixtime;
241 uint64_t cpu_idle_exits;
242 uint64_t cpu_rtimes[CPU_RTIME_BINS];
243 uint64_t cpu_itimes[CPU_ITIME_BINS];
244 #if !MONOTONIC
245 uint64_t cpu_cur_insns;
246 uint64_t cpu_cur_ucc;
247 uint64_t cpu_cur_urc;
248 #endif /* !MONOTONIC */
249 uint64_t cpu_gpmcs[4];
250 uint64_t cpu_max_observed_int_latency;
251 int cpu_max_observed_int_latency_vector;
252 volatile boolean_t cpu_NMI_acknowledged;
253 uint64_t debugger_entry_time;
254 uint64_t debugger_ipi_time;
255 /* A separate nested interrupt stack flag, to account
256 * for non-nested interrupts arriving while on the interrupt stack
257 * Currently only occurs when AICPM enables interrupts on the
258 * interrupt stack during processor offlining.
259 */
260 uint32_t cpu_nested_istack;
261 uint32_t cpu_nested_istack_events;
262 x86_saved_state64_t *cpu_fatal_trap_state;
263 x86_saved_state64_t *cpu_post_fatal_trap_state;
264 #if CONFIG_VMX
265 vmx_cpu_t cpu_vmx; /* wonderful world of virtualization */
266 #endif
267 #if CONFIG_MCA
268 struct mca_state *cpu_mca_state; /* State at MC fault */
269 #endif
270 struct prngContext *cpu_prng; /* PRNG's context */
271 int cpu_type;
272 int cpu_subtype;
273 int cpu_threadtype;
274 boolean_t cpu_iflag;
275 boolean_t cpu_boot_complete;
276 int cpu_hibernate;
277 #define MAX_PREEMPTION_RECORDS (8)
278 #if DEVELOPMENT || DEBUG
279 int cpu_plri;
280 plrecord_t plrecords[MAX_PREEMPTION_RECORDS];
281 #endif
282 void *cpu_console_buf;
283 struct x86_lcpu lcpu;
284 int cpu_phys_number; /* Physical CPU */
285 cpu_id_t cpu_id; /* Platform Expert */
286 #if DEBUG
287 uint64_t cpu_entry_cr3;
288 uint64_t cpu_exit_cr3;
289 uint64_t cpu_pcid_last_cr3;
290 #endif
291 } cpu_data_t;
292
293 extern cpu_data_t *cpu_data_ptr[];
294
295 /* Macro to generate inline bodies to retrieve per-cpu data fields. */
296 #if defined(__clang__)
297 #define GS_RELATIVE volatile __attribute__((address_space(256)))
298 #ifndef offsetof
299 #define offsetof(TYPE,MEMBER) __builtin_offsetof(TYPE,MEMBER)
300 #endif
301
302 #define CPU_DATA_GET(member,type) \
303 cpu_data_t GS_RELATIVE *cpu_data = \
304 (cpu_data_t GS_RELATIVE *)0UL; \
305 type ret; \
306 ret = cpu_data->member; \
307 return ret;
308
309 #define CPU_DATA_GET_INDEX(member,index,type) \
310 cpu_data_t GS_RELATIVE *cpu_data = \
311 (cpu_data_t GS_RELATIVE *)0UL; \
312 type ret; \
313 ret = cpu_data->member[index]; \
314 return ret;
315
316 #define CPU_DATA_SET(member,value) \
317 cpu_data_t GS_RELATIVE *cpu_data = \
318 (cpu_data_t GS_RELATIVE *)0UL; \
319 cpu_data->member = value;
320
321 #define CPU_DATA_XCHG(member,value,type) \
322 cpu_data_t GS_RELATIVE *cpu_data = \
323 (cpu_data_t GS_RELATIVE *)0UL; \
324 type ret; \
325 ret = cpu_data->member; \
326 cpu_data->member = value; \
327 return ret;
328
329 #else /* !defined(__clang__) */
330
331 #ifndef offsetof
332 #define offsetof(TYPE,MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
333 #endif /* offsetof */
334 #define CPU_DATA_GET(member,type) \
335 type ret; \
336 __asm__ volatile ("mov %%gs:%P1,%0" \
337 : "=r" (ret) \
338 : "i" (offsetof(cpu_data_t,member))); \
339 return ret;
340
341 #define CPU_DATA_GET_INDEX(member,index,type) \
342 type ret; \
343 __asm__ volatile ("mov %%gs:(%1),%0" \
344 : "=r" (ret) \
345 : "r" (offsetof(cpu_data_t,member[index]))); \
346 return ret;
347
348 #define CPU_DATA_SET(member,value) \
349 __asm__ volatile ("mov %0,%%gs:%P1" \
350 : \
351 : "r" (value), "i" (offsetof(cpu_data_t,member)));
352
353 #define CPU_DATA_XCHG(member,value,type) \
354 type ret; \
355 __asm__ volatile ("xchg %0,%%gs:%P1" \
356 : "=r" (ret) \
357 : "i" (offsetof(cpu_data_t,member)), "0" (value)); \
358 return ret;
359
360 #endif /* !defined(__clang__) */
361
362 /*
363 * Everyone within the osfmk part of the kernel can use the fast
364 * inline versions of these routines. Everyone outside, must call
365 * the real thing,
366 */
367 static inline thread_t
368 get_active_thread(void)
369 {
370 CPU_DATA_GET(cpu_active_thread,thread_t)
371 }
372 #define current_thread_fast() get_active_thread()
373 #define current_thread() current_thread_fast()
374
375 #define cpu_mode_is64bit() TRUE
376
377 static inline int
378 get_preemption_level(void)
379 {
380 CPU_DATA_GET(cpu_preemption_level,int)
381 }
382 static inline int
383 get_interrupt_level(void)
384 {
385 CPU_DATA_GET(cpu_interrupt_level,int)
386 }
387 static inline int
388 get_cpu_number(void)
389 {
390 CPU_DATA_GET(cpu_number,int)
391 }
392 static inline int
393 get_cpu_phys_number(void)
394 {
395 CPU_DATA_GET(cpu_phys_number,int)
396 }
397
398 static inline cpu_data_t *
399 current_cpu_datap(void) {
400 CPU_DATA_GET(cpu_this, cpu_data_t *);
401 }
402
403 /*
404 * Facility to diagnose preemption-level imbalances, which are otherwise
405 * challenging to debug. On each operation that enables or disables preemption,
406 * we record a backtrace into a per-CPU ring buffer, along with the current
407 * preemption level and operation type. Thus, if an imbalance is observed,
408 * one can examine these per-CPU records to determine which codepath failed
409 * to re-enable preemption, enabled premption without a corresponding
410 * disablement etc. The backtracer determines which stack is currently active,
411 * and uses that to perform bounds checks on unterminated stacks.
412 * To enable, sysctl -w machdep.pltrace=1 on DEVELOPMENT or DEBUG kernels (DRK '15)
413 * The bounds check currently doesn't account for non-default thread stack sizes.
414 */
415 #if DEVELOPMENT || DEBUG
416 static inline void pltrace_bt(uint64_t *rets, int maxframes, uint64_t stacklo, uint64_t stackhi) {
417 uint64_t *cfp = (uint64_t *) __builtin_frame_address(0);
418 int plbtf;
419
420 assert(stacklo !=0 && stackhi !=0);
421
422 for (plbtf = 0; plbtf < maxframes; plbtf++) {
423 if (((uint64_t)cfp == 0) || (((uint64_t)cfp < stacklo) || ((uint64_t)cfp > stackhi))) {
424 rets[plbtf] = 0;
425 continue;
426 }
427 rets[plbtf] = *(cfp + 1);
428 cfp = (uint64_t *) (*cfp);
429 }
430 }
431
432
433 extern uint32_t low_intstack[]; /* bottom */
434 extern uint32_t low_eintstack[]; /* top */
435 extern char mp_slave_stack[PAGE_SIZE];
436
437 static inline void pltrace_internal(boolean_t enable) {
438 cpu_data_t *cdata = current_cpu_datap();
439 int cpli = cdata->cpu_preemption_level;
440 int cplrecord = cdata->cpu_plri;
441 uint64_t kstackb, kstackt, *plbts;
442
443 assert(cpli >= 0);
444
445 cdata->plrecords[cplrecord].pltype = enable;
446 cdata->plrecords[cplrecord].plevel = cpli;
447
448 plbts = &cdata->plrecords[cplrecord].plbt[0];
449
450 cplrecord++;
451
452 if (cplrecord >= MAX_PREEMPTION_RECORDS) {
453 cplrecord = 0;
454 }
455
456 cdata->cpu_plri = cplrecord;
457 /* Obtain the 'current' program counter, initial backtrace
458 * element. This will also indicate if we were unable to
459 * trace further up the stack for some reason
460 */
461 __asm__ volatile("leaq 1f(%%rip), %%rax; mov %%rax, %0\n1:"
462 : "=m" (plbts[0])
463 :
464 : "rax");
465
466
467 thread_t cplthread = cdata->cpu_active_thread;
468 if (cplthread) {
469 uintptr_t csp;
470 __asm__ __volatile__ ("movq %%rsp, %0": "=r" (csp):);
471 /* Determine which stack we're on to populate stack bounds.
472 * We don't need to trace across stack boundaries for this
473 * routine.
474 */
475 kstackb = cdata->cpu_active_stack;
476 kstackt = kstackb + KERNEL_STACK_SIZE;
477 if (csp < kstackb || csp > kstackt) {
478 kstackt = cdata->cpu_kernel_stack;
479 kstackb = kstackb - KERNEL_STACK_SIZE;
480 if (csp < kstackb || csp > kstackt) {
481 kstackt = cdata->cpu_int_stack_top;
482 kstackb = kstackt - INTSTACK_SIZE;
483 if (csp < kstackb || csp > kstackt) {
484 kstackt = (uintptr_t)low_eintstack;
485 kstackb = (uintptr_t)low_eintstack - INTSTACK_SIZE;
486 if (csp < kstackb || csp > kstackt) {
487 kstackb = (uintptr_t) mp_slave_stack;
488 kstackt = (uintptr_t) mp_slave_stack + PAGE_SIZE;
489 }
490 }
491 }
492 }
493
494 if (kstackb) {
495 pltrace_bt(&plbts[1], MAXPLFRAMES - 1, kstackb, kstackt);
496 }
497 }
498 }
499
500 extern int plctrace_enabled;
501 #endif /* DEVELOPMENT || DEBUG */
502
503 static inline void pltrace(boolean_t plenable) {
504 #if DEVELOPMENT || DEBUG
505 if (__improbable(plctrace_enabled != 0)) {
506 pltrace_internal(plenable);
507 }
508 #else
509 (void)plenable;
510 #endif
511 }
512
513 static inline void
514 disable_preemption_internal(void) {
515 assert(get_preemption_level() >= 0);
516
517 #if defined(__clang__)
518 cpu_data_t GS_RELATIVE *cpu_data = (cpu_data_t GS_RELATIVE *)0UL;
519 cpu_data->cpu_preemption_level++;
520 #else
521 __asm__ volatile ("incl %%gs:%P0"
522 :
523 : "i" (offsetof(cpu_data_t, cpu_preemption_level)));
524 #endif
525 pltrace(FALSE);
526 }
527
528 static inline void
529 enable_preemption_internal(void) {
530 assert(get_preemption_level() > 0);
531 pltrace(TRUE);
532 #if defined(__clang__)
533 cpu_data_t GS_RELATIVE *cpu_data = (cpu_data_t GS_RELATIVE *)0UL;
534 if (0 == --cpu_data->cpu_preemption_level)
535 kernel_preempt_check();
536 #else
537 __asm__ volatile ("decl %%gs:%P0 \n\t"
538 "jne 1f \n\t"
539 "call _kernel_preempt_check \n\t"
540 "1:"
541 : /* no outputs */
542 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
543 : "eax", "ecx", "edx", "cc", "memory");
544 #endif
545 }
546
547 static inline void
548 enable_preemption_no_check(void)
549 {
550 assert(get_preemption_level() > 0);
551
552 pltrace(TRUE);
553 #if defined(__clang__)
554 cpu_data_t GS_RELATIVE *cpu_data = (cpu_data_t GS_RELATIVE *)0UL;
555 cpu_data->cpu_preemption_level--;
556 #else
557 __asm__ volatile ("decl %%gs:%P0"
558 : /* no outputs */
559 : "i" (offsetof(cpu_data_t, cpu_preemption_level))
560 : "cc", "memory");
561 #endif
562 }
563
564 static inline void
565 _enable_preemption_no_check(void) {
566 enable_preemption_no_check();
567 }
568
569 static inline void
570 mp_disable_preemption(void)
571 {
572 disable_preemption_internal();
573 }
574
575 static inline void
576 _mp_disable_preemption(void)
577 {
578 disable_preemption_internal();
579 }
580
581 static inline void
582 mp_enable_preemption(void)
583 {
584 enable_preemption_internal();
585 }
586
587 static inline void
588 _mp_enable_preemption(void) {
589 enable_preemption_internal();
590 }
591
592 static inline void
593 mp_enable_preemption_no_check(void) {
594 enable_preemption_no_check();
595 }
596
597 static inline void
598 _mp_enable_preemption_no_check(void) {
599 enable_preemption_no_check();
600 }
601
602 #ifdef XNU_KERNEL_PRIVATE
603 #define disable_preemption() disable_preemption_internal()
604 #define enable_preemption() enable_preemption_internal()
605 #define MACHINE_PREEMPTION_MACROS (1)
606 #endif
607
608 static inline cpu_data_t *
609 cpu_datap(int cpu) {
610 return cpu_data_ptr[cpu];
611 }
612
613 static inline int
614 cpu_is_running(int cpu) {
615 return ((cpu_datap(cpu) != NULL) && (cpu_datap(cpu)->cpu_running));
616 }
617
618 #ifdef MACH_KERNEL_PRIVATE
619 static inline cpu_data_t *
620 cpu_shadowp(int cpu) {
621 return cpu_data_ptr[cpu]->cd_shadow;
622 }
623
624 #endif
625 extern cpu_data_t *cpu_data_alloc(boolean_t is_boot_cpu);
626 extern void cpu_data_realloc(void);
627
628 #endif /* I386_CPU_DATA */