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