xref: /xnu-11215.41.3/osfmk/i386/machine_routines.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
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 #include <i386/machine_routines.h>
30 #include <i386/cpuid.h>
31 #include <i386/fpu.h>
32 #include <mach/processor.h>
33 #include <kern/processor.h>
34 #include <kern/machine.h>
35 
36 #include <kern/cpu_number.h>
37 #include <kern/thread.h>
38 #include <kern/thread_call.h>
39 #include <kern/policy_internal.h>
40 
41 #include <prng/random.h>
42 #include <prng/entropy.h>
43 #include <i386/machine_cpu.h>
44 #include <i386/lapic.h>
45 #include <i386/bit_routines.h>
46 #include <i386/mp_events.h>
47 #include <i386/pmCPU.h>
48 #include <i386/trap_internal.h>
49 #include <i386/tsc.h>
50 #include <i386/cpu_threads.h>
51 #include <i386/proc_reg.h>
52 #include <mach/vm_param.h>
53 #include <i386/pmap.h>
54 #include <i386/pmap_internal.h>
55 #include <i386/misc_protos.h>
56 #include <kern/timer_queue.h>
57 #include <vm/vm_map_xnu.h>
58 #include <kern/monotonic.h>
59 #include <kern/kpc.h>
60 #include <architecture/i386/pio.h>
61 #include <i386/cpu_data.h>
62 #if DEBUG
63 #define DBG(x...)       kprintf("DBG: " x)
64 #else
65 #define DBG(x...)
66 #endif
67 
68 extern void     wakeup(void *);
69 
70 uint64_t        LockTimeOut;
71 uint64_t        TLBTimeOut;
72 uint64_t        LockTimeOutTSC;
73 uint32_t        LockTimeOutUsec;
74 uint64_t        MutexSpin;
75 uint64_t        low_MutexSpin;
76 int64_t         high_MutexSpin;
77 uint64_t        LastDebuggerEntryAllowance;
78 uint64_t        delay_spin_threshold;
79 
80 extern uint64_t panic_restart_timeout;
81 
82 boolean_t virtualized = FALSE;
83 
84 static SIMPLE_LOCK_DECLARE(ml_timer_evaluation_slock, 0);
85 uint32_t ml_timer_eager_evaluations;
86 uint64_t ml_timer_eager_evaluation_max;
87 static boolean_t ml_timer_evaluation_in_progress = FALSE;
88 
89 LCK_GRP_DECLARE(max_cpus_grp, "max_cpus");
90 LCK_MTX_DECLARE(max_cpus_lock, &max_cpus_grp);
91 static int max_cpus_initialized = 0;
92 #define MAX_CPUS_SET    0x1
93 #define MAX_CPUS_WAIT   0x2
94 
95 /* IO memory map services */
96 
97 /* Map memory map IO space */
98 vm_offset_t
ml_io_map(vm_offset_t phys_addr,vm_size_t size)99 ml_io_map(
100 	vm_offset_t phys_addr,
101 	vm_size_t size)
102 {
103 	return io_map(phys_addr, size, VM_WIMG_IO, VM_PROT_DEFAULT, false);
104 }
105 
106 vm_offset_t
ml_io_map_wcomb(vm_offset_t phys_addr,vm_size_t size)107 ml_io_map_wcomb(
108 	vm_offset_t phys_addr,
109 	vm_size_t size)
110 {
111 	return io_map(phys_addr, size, VM_WIMG_WCOMB, VM_PROT_DEFAULT, false);
112 }
113 
114 vm_offset_t
ml_io_map_unmappable(vm_offset_t phys_addr,vm_size_t size,unsigned int flags)115 ml_io_map_unmappable(
116 	vm_offset_t             phys_addr,
117 	vm_size_t               size,
118 	unsigned int            flags)
119 {
120 	return io_map(phys_addr, size, flags, VM_PROT_DEFAULT, true);
121 }
122 
123 void
ml_get_bouncepool_info(vm_offset_t * phys_addr,vm_size_t * size)124 ml_get_bouncepool_info(vm_offset_t *phys_addr, vm_size_t *size)
125 {
126 	*phys_addr = 0;
127 	*size      = 0;
128 }
129 
130 
131 vm_offset_t
ml_static_ptovirt(vm_offset_t paddr)132 ml_static_ptovirt(
133 	vm_offset_t paddr)
134 {
135 #if defined(__x86_64__)
136 	return (vm_offset_t)(((unsigned long) paddr) | VM_MIN_KERNEL_ADDRESS);
137 #else
138 	return (vm_offset_t)((paddr) | LINEAR_KERNEL_ADDRESS);
139 #endif
140 }
141 
142 vm_offset_t
ml_static_slide(vm_offset_t vaddr)143 ml_static_slide(
144 	vm_offset_t vaddr)
145 {
146 	return vaddr + vm_kernel_slide;
147 }
148 
149 /*
150  * base must be page-aligned, and size must be a multiple of PAGE_SIZE
151  */
152 kern_return_t
ml_static_verify_page_protections(uint64_t base,uint64_t size,vm_prot_t prot)153 ml_static_verify_page_protections(
154 	uint64_t base, uint64_t size, vm_prot_t prot)
155 {
156 	vm_prot_t pageprot;
157 	uint64_t offset;
158 
159 	DBG("ml_static_verify_page_protections: vaddr 0x%llx sz 0x%llx prot 0x%x\n", base, size, prot);
160 
161 	/*
162 	 * base must be within the static bounds, defined to be:
163 	 * (vm_kernel_stext, kc_highest_nonlinkedit_vmaddr)
164 	 */
165 #if DEVELOPMENT || DEBUG || KASAN
166 	assert(kc_highest_nonlinkedit_vmaddr > 0 && base > vm_kernel_stext && base < kc_highest_nonlinkedit_vmaddr);
167 #else   /* On release kernels, assume this is a protection mismatch failure. */
168 	if (kc_highest_nonlinkedit_vmaddr == 0 || base < vm_kernel_stext || base >= kc_highest_nonlinkedit_vmaddr) {
169 		return KERN_FAILURE;
170 	}
171 #endif
172 
173 	for (offset = 0; offset < size; offset += PAGE_SIZE) {
174 		if (pmap_get_prot(kernel_pmap, base + offset, &pageprot) == KERN_FAILURE) {
175 			return KERN_FAILURE;
176 		}
177 		if ((pageprot & prot) != prot) {
178 			return KERN_FAILURE;
179 		}
180 	}
181 
182 	return KERN_SUCCESS;
183 }
184 
185 vm_offset_t
ml_static_unslide(vm_offset_t vaddr)186 ml_static_unslide(
187 	vm_offset_t vaddr)
188 {
189 	return vaddr - vm_kernel_slide;
190 }
191 
192 /*
193  * Reclaim memory, by virtual address, that was used in early boot that is no longer needed
194  * by the kernel.
195  */
196 void
ml_static_mfree(vm_offset_t vaddr,vm_size_t size)197 ml_static_mfree(
198 	vm_offset_t vaddr,
199 	vm_size_t size)
200 {
201 	addr64_t vaddr_cur;
202 	ppnum_t ppn;
203 	uint32_t freed_pages = 0;
204 	vm_size_t map_size;
205 
206 	assert(vaddr >= VM_MIN_KERNEL_ADDRESS);
207 
208 	assert((vaddr & (PAGE_SIZE - 1)) == 0); /* must be page aligned */
209 
210 	for (vaddr_cur = vaddr; vaddr_cur < round_page_64(vaddr + size);) {
211 		map_size = pmap_query_pagesize(kernel_pmap, vaddr_cur);
212 
213 		/* just skip if nothing mapped here */
214 		if (map_size == 0) {
215 			vaddr_cur += PAGE_SIZE;
216 			continue;
217 		}
218 
219 		/*
220 		 * Can't free from the middle of a large page.
221 		 */
222 		assert((vaddr_cur & (map_size - 1)) == 0);
223 
224 		ppn = pmap_find_phys(kernel_pmap, vaddr_cur);
225 		assert(ppn != (ppnum_t)NULL);
226 
227 		pmap_remove(kernel_pmap, vaddr_cur, vaddr_cur + map_size);
228 		while (map_size > 0) {
229 			assert(pmap_valid_page(ppn));
230 			if (IS_MANAGED_PAGE(ppn)) {
231 				vm_page_create(ppn, (ppn + 1));
232 				freed_pages++;
233 			}
234 			map_size -= PAGE_SIZE;
235 			vaddr_cur += PAGE_SIZE;
236 			ppn++;
237 		}
238 	}
239 	vm_page_lockspin_queues();
240 	vm_page_wire_count -= freed_pages;
241 	vm_page_wire_count_initial -= freed_pages;
242 	if (vm_page_wire_count_on_boot != 0) {
243 		assert(vm_page_wire_count_on_boot >= freed_pages);
244 		vm_page_wire_count_on_boot -= freed_pages;
245 	}
246 	vm_page_unlock_queues();
247 
248 #if     DEBUG
249 	kprintf("ml_static_mfree: Released 0x%x pages at VA %p, size:0x%llx, last ppn: 0x%x\n", freed_pages, (void *)vaddr, (uint64_t)size, ppn);
250 #endif
251 }
252 
253 /* Change page protections for addresses previously loaded by efiboot */
254 kern_return_t
ml_static_protect(vm_offset_t vmaddr,vm_size_t size,vm_prot_t prot)255 ml_static_protect(vm_offset_t vmaddr, vm_size_t size, vm_prot_t prot)
256 {
257 	boolean_t NX = !!!(prot & VM_PROT_EXECUTE), ro = !!!(prot & VM_PROT_WRITE);
258 
259 	assert(prot & VM_PROT_READ);
260 
261 	pmap_mark_range(kernel_pmap, vmaddr, size, NX, ro);
262 
263 	return KERN_SUCCESS;
264 }
265 
266 /* virtual to physical on wired pages */
267 vm_offset_t
ml_vtophys(vm_offset_t vaddr)268 ml_vtophys(
269 	vm_offset_t vaddr)
270 {
271 	return (vm_offset_t)kvtophys(vaddr);
272 }
273 
274 /*
275  *	Routine:        ml_nofault_copy
276  *	Function:	Perform a physical mode copy if the source and
277  *			destination have valid translations in the kernel pmap.
278  *			If translations are present, they are assumed to
279  *			be wired; i.e. no attempt is made to guarantee that the
280  *			translations obtained remained valid for
281  *			the duration of the copy process.
282  */
283 
284 vm_size_t
ml_nofault_copy(vm_offset_t virtsrc,vm_offset_t virtdst,vm_size_t size)285 ml_nofault_copy(
286 	vm_offset_t virtsrc, vm_offset_t virtdst, vm_size_t size)
287 {
288 	addr64_t cur_phys_dst, cur_phys_src;
289 	uint32_t count, nbytes = 0;
290 
291 	while (size > 0) {
292 		if (!(cur_phys_src = kvtophys(virtsrc))) {
293 			break;
294 		}
295 		if (!(cur_phys_dst = kvtophys(virtdst))) {
296 			break;
297 		}
298 		if (!pmap_valid_page(i386_btop(cur_phys_dst)) || !pmap_valid_page(i386_btop(cur_phys_src))) {
299 			break;
300 		}
301 		count = (uint32_t)(PAGE_SIZE - (cur_phys_src & PAGE_MASK));
302 		if (count > (PAGE_SIZE - (cur_phys_dst & PAGE_MASK))) {
303 			count = (uint32_t)(PAGE_SIZE - (cur_phys_dst & PAGE_MASK));
304 		}
305 		if (count > size) {
306 			count = (uint32_t)size;
307 		}
308 
309 		bcopy_phys(cur_phys_src, cur_phys_dst, count);
310 
311 		nbytes += count;
312 		virtsrc += count;
313 		virtdst += count;
314 		size -= count;
315 	}
316 
317 	return nbytes;
318 }
319 
320 /*
321  *	Routine:        ml_validate_nofault
322  *	Function: Validate that ths address range has a valid translations
323  *			in the kernel pmap.  If translations are present, they are
324  *			assumed to be wired; i.e. no attempt is made to guarantee
325  *			that the translation persist after the check.
326  *  Returns: TRUE if the range is mapped and will not cause a fault,
327  *			FALSE otherwise.
328  */
329 
330 boolean_t
ml_validate_nofault(vm_offset_t virtsrc,vm_size_t size)331 ml_validate_nofault(
332 	vm_offset_t virtsrc, vm_size_t size)
333 {
334 	addr64_t cur_phys_src;
335 	uint32_t count;
336 
337 	while (size > 0) {
338 		if (!(cur_phys_src = kvtophys(virtsrc))) {
339 			return FALSE;
340 		}
341 		if (!pmap_valid_page(i386_btop(cur_phys_src))) {
342 			return FALSE;
343 		}
344 		count = (uint32_t)(PAGE_SIZE - (cur_phys_src & PAGE_MASK));
345 		if (count > size) {
346 			count = (uint32_t)size;
347 		}
348 
349 		virtsrc += count;
350 		size -= count;
351 	}
352 
353 	return TRUE;
354 }
355 
356 /* Interrupt handling */
357 
358 /* Initialize Interrupts */
359 void
ml_init_interrupt(void)360 ml_init_interrupt(void)
361 {
362 	(void) ml_set_interrupts_enabled(TRUE);
363 }
364 
365 
366 /* Get Interrupts Enabled */
367 boolean_t
ml_get_interrupts_enabled(void)368 ml_get_interrupts_enabled(void)
369 {
370 	unsigned long flags;
371 
372 	__asm__ volatile ("pushf; pop	%0":  "=r" (flags));
373 	return (flags & EFL_IF) != 0;
374 }
375 
376 /* Set Interrupts Enabled */
377 boolean_t
ml_set_interrupts_enabled(boolean_t enable)378 ml_set_interrupts_enabled(boolean_t enable)
379 {
380 	unsigned long flags;
381 	boolean_t istate;
382 
383 	__asm__ volatile ("pushf; pop	%0"  :  "=r" (flags));
384 
385 	assert(get_interrupt_level() ? (enable == FALSE) : TRUE);
386 
387 	istate = ((flags & EFL_IF) != 0);
388 
389 	if (enable) {
390 		__asm__ volatile ("sti;nop");
391 
392 		if ((get_preemption_level() == 0) && (*ast_pending() & AST_URGENT)) {
393 			__asm__ volatile ("int %0" :: "N" (T_PREEMPT));
394 		}
395 	} else {
396 		if (istate) {
397 			__asm__ volatile ("cli");
398 		}
399 	}
400 
401 	return istate;
402 }
403 
404 /* Early Set Interrupts Enabled */
405 boolean_t
ml_early_set_interrupts_enabled(boolean_t enable)406 ml_early_set_interrupts_enabled(boolean_t enable)
407 {
408 	if (enable == TRUE) {
409 		kprintf("Caller attempted to enable interrupts too early in "
410 		    "kernel startup. Halting.\n");
411 		hlt();
412 		/*NOTREACHED*/
413 	}
414 
415 	/* On x86, do not allow interrupts to be enabled very early */
416 	return FALSE;
417 }
418 
419 /* Check if running at interrupt context */
420 boolean_t
ml_at_interrupt_context(void)421 ml_at_interrupt_context(void)
422 {
423 	return get_interrupt_level() != 0;
424 }
425 
426 /*
427  * This answers the question
428  * "after returning from this interrupt handler with the AST_URGENT bit set,
429  * will I end up in ast_taken_user or ast_taken_kernel?"
430  *
431  * If it's called in non-interrupt context (e.g. regular syscall), it should
432  * return false.
433  *
434  * Must be called with interrupts disabled.
435  */
436 bool
ml_did_interrupt_userspace(void)437 ml_did_interrupt_userspace(void)
438 {
439 	assert(ml_get_interrupts_enabled() == false);
440 
441 	x86_saved_state_t *state = current_cpu_datap()->cpu_int_state;
442 	if (!state) {
443 		return false;
444 	}
445 
446 	uint64_t cs;
447 
448 	if (is_saved_state64(state)) {
449 		cs = saved_state64(state)->isf.cs;
450 	} else {
451 		cs = saved_state32(state)->cs;
452 	}
453 
454 	return (cs & SEL_PL) == SEL_PL_U;
455 }
456 
457 void
ml_get_power_state(boolean_t * icp,boolean_t * pidlep)458 ml_get_power_state(boolean_t *icp, boolean_t *pidlep)
459 {
460 	*icp = (get_interrupt_level() != 0);
461 	/* These will be technically inaccurate for interrupts that occur
462 	 * successively within a single "idle exit" event, but shouldn't
463 	 * matter statistically.
464 	 */
465 	*pidlep = (current_cpu_datap()->lcpu.package->num_idle == topoParms.nLThreadsPerPackage);
466 }
467 
468 /* Generate a fake interrupt */
469 __dead2
470 void
ml_cause_interrupt(void)471 ml_cause_interrupt(void)
472 {
473 	panic("ml_cause_interrupt not defined yet on Intel");
474 }
475 
476 /*
477  * TODO: transition users of this to kernel_thread_start_priority
478  * ml_thread_policy is an unsupported KPI
479  */
480 void
ml_thread_policy(thread_t thread,__unused unsigned policy_id,unsigned policy_info)481 ml_thread_policy(
482 	thread_t thread,
483 	__unused        unsigned policy_id,
484 	unsigned policy_info)
485 {
486 	if (policy_info & MACHINE_NETWORK_WORKLOOP) {
487 		thread_precedence_policy_data_t info;
488 		__assert_only kern_return_t kret;
489 
490 		info.importance = 1;
491 
492 		kret = thread_policy_set_internal(thread, THREAD_PRECEDENCE_POLICY,
493 		    (thread_policy_t)&info,
494 		    THREAD_PRECEDENCE_POLICY_COUNT);
495 		assert(kret == KERN_SUCCESS);
496 	}
497 }
498 
499 /* Initialize Interrupts */
500 void
ml_install_interrupt_handler(void * nub,int source,void * target,IOInterruptHandler handler,void * refCon)501 ml_install_interrupt_handler(
502 	void *nub,
503 	int source,
504 	void *target,
505 	IOInterruptHandler handler,
506 	void *refCon)
507 {
508 	boolean_t current_state;
509 
510 	current_state = ml_set_interrupts_enabled(FALSE);
511 
512 	PE_install_interrupt_handler(nub, source, target,
513 	    (IOInterruptHandler) handler, refCon);
514 
515 	(void) ml_set_interrupts_enabled(current_state);
516 }
517 
518 
519 void
machine_signal_idle(processor_t processor)520 machine_signal_idle(
521 	processor_t processor)
522 {
523 	cpu_interrupt(processor->cpu_id);
524 }
525 
526 __dead2
527 void
machine_signal_idle_deferred(__unused processor_t processor)528 machine_signal_idle_deferred(
529 	__unused processor_t processor)
530 {
531 	panic("Unimplemented");
532 }
533 
534 __dead2
535 void
machine_signal_idle_cancel(__unused processor_t processor)536 machine_signal_idle_cancel(
537 	__unused processor_t processor)
538 {
539 	panic("Unimplemented");
540 }
541 
542 static void
register_cpu(uint32_t lapic_id,processor_t * processor_out,boolean_t boot_cpu)543 register_cpu(
544 	uint32_t        lapic_id,
545 	processor_t     *processor_out,
546 	boolean_t       boot_cpu )
547 {
548 	int             target_cpu;
549 	cpu_data_t      *this_cpu_datap;
550 
551 	this_cpu_datap = cpu_data_alloc(boot_cpu);
552 	assert(this_cpu_datap);
553 
554 	target_cpu = this_cpu_datap->cpu_number;
555 	assert((boot_cpu && (target_cpu == 0)) ||
556 	    (!boot_cpu && (target_cpu != 0)));
557 
558 	lapic_cpu_map(lapic_id, target_cpu);
559 
560 	/* The cpu_id is not known at registration phase. Just do
561 	 * lapic_id for now
562 	 */
563 	this_cpu_datap->cpu_phys_number = lapic_id;
564 
565 #if CONFIG_CPU_COUNTERS
566 	kpc_register_cpu(this_cpu_datap);
567 #endif /* CONFIG_CPU_COUNTERS */
568 
569 	if (!boot_cpu) {
570 		cpu_thread_alloc(this_cpu_datap->cpu_number);
571 		assert(this_cpu_datap->lcpu.core != NULL);
572 	}
573 
574 	/*
575 	 * processor_init() deferred to topology start
576 	 * because "slot numbers" a.k.a. logical processor numbers
577 	 * are not yet finalized.
578 	 */
579 	*processor_out = this_cpu_datap->cpu_processor;
580 }
581 
582 /*
583  * AppleACPICPU calls this function twice for each CPU.
584  * Once with start == false to register the CPU, then xnu sorts the topology,
585  * then again with start == true to boot the CPU with the assigned CPU number.
586  *
587  * xnu or EFI can limit the number of booted CPUs.
588  * xnu does it by cpu_topology_start_cpu refusing to call processor_boot.
589  * EFI does it by populating the ACPI table with a flag that convinces
590  * AppleACPICPU to not call ml_processor_register.
591  *
592  * See https://support.apple.com/en-us/101870 for when EFI does this. (nvram SMTDisable=%01)
593  * When this happens the processors show up in ACPI but they do not get ml_processor_register'ed.
594  */
595 kern_return_t
ml_processor_register(cpu_id_t cpu_id,uint32_t lapic_id,processor_t * processor_out,boolean_t boot_cpu,boolean_t start)596 ml_processor_register(
597 	cpu_id_t        cpu_id,
598 	uint32_t        lapic_id,
599 	processor_t     *processor_out,
600 	boolean_t       boot_cpu,
601 	boolean_t       start )
602 {
603 	static boolean_t done_topo_sort = FALSE;
604 	static bool done_registering_and_starting = false;
605 	static uint32_t num_registered = 0;
606 
607 	/* Register all CPUs first, and track max */
608 	if (start == FALSE) {
609 		num_registered++;
610 
611 		DBG( "registering CPU lapic id %d\n", lapic_id );
612 
613 		register_cpu( lapic_id, processor_out, boot_cpu );
614 		return KERN_SUCCESS;
615 	}
616 
617 	/* Sort by topology before we start anything */
618 	if (!done_topo_sort) {
619 		DBG( "about to start CPUs. %d registered\n", num_registered );
620 
621 		cpu_topology_sort( num_registered );
622 		done_topo_sort = TRUE;
623 	}
624 
625 	/* Assign the cpu ID */
626 	uint32_t cpunum = -1;
627 	cpu_data_t  *this_cpu_datap = NULL;
628 
629 	/* find cpu num and pointer */
630 	cpunum = ml_get_cpuid( lapic_id );
631 
632 	if (cpunum == 0xFFFFFFFF) { /* never heard of it? */
633 		panic( "trying to start invalid/unregistered CPU %d", lapic_id );
634 	}
635 
636 	this_cpu_datap = cpu_datap(cpunum);
637 
638 	/* fix the CPU id */
639 	this_cpu_datap->cpu_id = cpu_id;
640 
641 	/* allocate and initialize other per-cpu structures */
642 	if (!boot_cpu) {
643 		mp_cpus_call_cpu_init(cpunum);
644 		random_cpu_init(cpunum);
645 	}
646 
647 	/* output arg */
648 	*processor_out = this_cpu_datap->cpu_processor;
649 
650 	/* OK, try and start this CPU */
651 	kern_return_t ret = cpu_topology_start_cpu( cpunum );
652 
653 	/*
654 	 * AppleACPICPU will start processors in CPU number order,
655 	 * so when we get the last CPU number, it's finished
656 	 * calling ml_processor_register.
657 	 *
658 	 * By this point max cpus has been determined.  There may be more
659 	 * registrations than max_cpus in the case of `cpus=` boot arg.
660 	 */
661 	if (cpunum == num_registered - 1) {
662 		__assert_only bool success;
663 		success = os_atomic_cmpxchg(&done_registering_and_starting, false, true, relaxed);
664 		assert(success);
665 
666 		assert(max_cpus_initialized == MAX_CPUS_SET);
667 
668 		ml_cpu_init_completed();
669 	} else {
670 		assert(os_atomic_load(&done_registering_and_starting, relaxed) == false);
671 	}
672 
673 	return ret;
674 }
675 
676 
677 /*
678  * This is called when all of the ml_processor_info_t structures have been
679  * initialized and all the processors have been started through processor_start().
680  *
681  * Required by the scheduler subsystem.
682  */
683 void
ml_cpu_init_completed(void)684 ml_cpu_init_completed(void)
685 {
686 	sched_cpu_init_completed();
687 }
688 
689 
690 void
ml_cpu_get_info_type(ml_cpu_info_t * cpu_infop,cluster_type_t cluster_type __unused)691 ml_cpu_get_info_type(ml_cpu_info_t *cpu_infop, cluster_type_t cluster_type __unused)
692 {
693 	boolean_t       os_supports_sse;
694 	i386_cpu_info_t *cpuid_infop;
695 
696 	if (cpu_infop == NULL) {
697 		return;
698 	}
699 
700 	/*
701 	 * Are we supporting MMX/SSE/SSE2/SSE3?
702 	 * As distinct from whether the cpu has these capabilities.
703 	 */
704 	os_supports_sse = !!(get_cr4() & CR4_OSXMM);
705 
706 	if (ml_fpu_avx_enabled()) {
707 		cpu_infop->vector_unit = 9;
708 	} else if ((cpuid_features() & CPUID_FEATURE_SSE4_2) && os_supports_sse) {
709 		cpu_infop->vector_unit = 8;
710 	} else if ((cpuid_features() & CPUID_FEATURE_SSE4_1) && os_supports_sse) {
711 		cpu_infop->vector_unit = 7;
712 	} else if ((cpuid_features() & CPUID_FEATURE_SSSE3) && os_supports_sse) {
713 		cpu_infop->vector_unit = 6;
714 	} else if ((cpuid_features() & CPUID_FEATURE_SSE3) && os_supports_sse) {
715 		cpu_infop->vector_unit = 5;
716 	} else if ((cpuid_features() & CPUID_FEATURE_SSE2) && os_supports_sse) {
717 		cpu_infop->vector_unit = 4;
718 	} else if ((cpuid_features() & CPUID_FEATURE_SSE) && os_supports_sse) {
719 		cpu_infop->vector_unit = 3;
720 	} else if (cpuid_features() & CPUID_FEATURE_MMX) {
721 		cpu_infop->vector_unit = 2;
722 	} else {
723 		cpu_infop->vector_unit = 0;
724 	}
725 
726 	cpuid_infop  = cpuid_info();
727 
728 	cpu_infop->cache_line_size = cpuid_infop->cache_linesize;
729 
730 	cpu_infop->l1_icache_size = cpuid_infop->cache_size[L1I];
731 	cpu_infop->l1_dcache_size = cpuid_infop->cache_size[L1D];
732 
733 	if (cpuid_infop->cache_size[L2U] > 0) {
734 		cpu_infop->l2_settings = 1;
735 		cpu_infop->l2_cache_size = cpuid_infop->cache_size[L2U];
736 	} else {
737 		cpu_infop->l2_settings = 0;
738 		cpu_infop->l2_cache_size = 0xFFFFFFFF;
739 	}
740 
741 	if (cpuid_infop->cache_size[L3U] > 0) {
742 		cpu_infop->l3_settings = 1;
743 		cpu_infop->l3_cache_size = cpuid_infop->cache_size[L3U];
744 	} else {
745 		cpu_infop->l3_settings = 0;
746 		cpu_infop->l3_cache_size = 0xFFFFFFFF;
747 	}
748 }
749 
750 /*
751  *	Routine:        ml_cpu_get_info
752  *	Function: Fill out the ml_cpu_info_t structure with parameters associated
753  *	with the boot cluster.
754  */
755 void
ml_cpu_get_info(ml_cpu_info_t * ml_cpu_info)756 ml_cpu_get_info(ml_cpu_info_t * ml_cpu_info)
757 {
758 	ml_cpu_get_info_type(ml_cpu_info, CLUSTER_TYPE_SMP);
759 }
760 
761 unsigned int
ml_get_cpu_number_type(cluster_type_t cluster_type __unused,bool logical,bool available)762 ml_get_cpu_number_type(cluster_type_t cluster_type __unused, bool logical, bool available)
763 {
764 	/*
765 	 * At present no supported x86 system features more than 1 CPU type. Because
766 	 * of this, the cluster_type parameter is ignored.
767 	 */
768 	if (logical && available) {
769 		return machine_info.logical_cpu;
770 	} else if (logical && !available) {
771 		return machine_info.logical_cpu_max;
772 	} else if (!logical && available) {
773 		return machine_info.physical_cpu;
774 	} else {
775 		return machine_info.physical_cpu_max;
776 	}
777 }
778 
779 void
ml_get_cluster_type_name(cluster_type_t cluster_type __unused,char * name,size_t name_size)780 ml_get_cluster_type_name(cluster_type_t cluster_type __unused, char *name, size_t name_size)
781 {
782 	strlcpy(name, "Standard", name_size);
783 }
784 
785 unsigned int
ml_get_cluster_number_type(cluster_type_t cluster_type __unused)786 ml_get_cluster_number_type(cluster_type_t cluster_type __unused)
787 {
788 	/*
789 	 * At present no supported x86 system has more than 1 CPU type and multiple
790 	 * clusters.
791 	 */
792 	return 1;
793 }
794 
795 unsigned int
ml_get_cpu_types(void)796 ml_get_cpu_types(void)
797 {
798 	return 1 << CLUSTER_TYPE_SMP;
799 }
800 
801 unsigned int
ml_get_cluster_count(void)802 ml_get_cluster_count(void)
803 {
804 	/*
805 	 * At present no supported x86 system has more than 1 CPU type and multiple
806 	 * clusters.
807 	 */
808 	return 1;
809 }
810 
811 static_assert(MAX_CPUS <= 256, "MAX_CPUS must fit in _COMM_PAGE_CPU_TO_CLUSTER; Increase table size if needed");
812 
813 void
ml_map_cpus_to_clusters(uint8_t * table)814 ml_map_cpus_to_clusters(uint8_t *table)
815 {
816 	for (uint16_t cpu_id = 0; cpu_id < machine_info.logical_cpu_max; cpu_id++) {
817 		// Supported x86 systems have 1 cluster
818 		*(table + cpu_id) = (uint8_t)0;
819 	}
820 }
821 
822 int
ml_early_cpu_max_number(void)823 ml_early_cpu_max_number(void)
824 {
825 	int n = max_ncpus;
826 
827 	assert(startup_phase >= STARTUP_SUB_TUNABLES);
828 	if (max_cpus_from_firmware) {
829 		n = MIN(n, max_cpus_from_firmware);
830 	}
831 	return n - 1;
832 }
833 
834 void
ml_set_max_cpus(unsigned int max_cpus)835 ml_set_max_cpus(unsigned int max_cpus)
836 {
837 	lck_mtx_lock(&max_cpus_lock);
838 	if (max_cpus_initialized != MAX_CPUS_SET) {
839 		if (max_cpus > 0 && max_cpus <= MAX_CPUS) {
840 			/*
841 			 * Note: max_cpus is the number of enabled processors
842 			 * that ACPI found; max_ncpus is the maximum number
843 			 * that the kernel supports or that the "cpus="
844 			 * boot-arg has set. Here we take int minimum.
845 			 */
846 			machine_info.max_cpus = (integer_t)MIN(max_cpus, max_ncpus);
847 		}
848 		if (max_cpus_initialized == MAX_CPUS_WAIT) {
849 			thread_wakeup((event_t) &max_cpus_initialized);
850 		}
851 		max_cpus_initialized = MAX_CPUS_SET;
852 	}
853 	lck_mtx_unlock(&max_cpus_lock);
854 }
855 
856 unsigned int
ml_wait_max_cpus(void)857 ml_wait_max_cpus(void)
858 {
859 	lck_mtx_lock(&max_cpus_lock);
860 	while (max_cpus_initialized != MAX_CPUS_SET) {
861 		max_cpus_initialized = MAX_CPUS_WAIT;
862 		lck_mtx_sleep(&max_cpus_lock, LCK_SLEEP_DEFAULT, &max_cpus_initialized, THREAD_UNINT);
863 	}
864 	lck_mtx_unlock(&max_cpus_lock);
865 	return machine_info.max_cpus;
866 }
867 
868 void
ml_panic_trap_to_debugger(__unused const char * panic_format_str,__unused va_list * panic_args,__unused unsigned int reason,__unused void * ctx,__unused uint64_t panic_options_mask,__unused unsigned long panic_caller,__unused const char * panic_initiator)869 ml_panic_trap_to_debugger(__unused const char *panic_format_str,
870     __unused va_list *panic_args,
871     __unused unsigned int reason,
872     __unused void *ctx,
873     __unused uint64_t panic_options_mask,
874     __unused unsigned long panic_caller,
875     __unused const char *panic_initiator)
876 {
877 	return;
878 }
879 
880 static uint64_t
virtual_timeout_inflate64(unsigned int vti,uint64_t timeout,uint64_t max_timeout)881 virtual_timeout_inflate64(unsigned int vti, uint64_t timeout, uint64_t max_timeout)
882 {
883 	if (vti >= 64) {
884 		return max_timeout;
885 	}
886 
887 	if ((timeout << vti) >> vti != timeout) {
888 		return max_timeout;
889 	}
890 
891 	if ((timeout << vti) > max_timeout) {
892 		return max_timeout;
893 	}
894 
895 	return timeout << vti;
896 }
897 
898 static uint32_t
virtual_timeout_inflate32(unsigned int vti,uint32_t timeout,uint32_t max_timeout)899 virtual_timeout_inflate32(unsigned int vti, uint32_t timeout, uint32_t max_timeout)
900 {
901 	if (vti >= 32) {
902 		return max_timeout;
903 	}
904 
905 	if ((timeout << vti) >> vti != timeout) {
906 		return max_timeout;
907 	}
908 
909 	return timeout << vti;
910 }
911 
912 /*
913  * Some timeouts are later adjusted or used in calculations setting
914  * other values. In order to avoid overflow, cap the max timeout as
915  * 2^47ns (~39 hours).
916  */
917 static const uint64_t max_timeout_ns = 1ULL << 47;
918 
919 /*
920  * Inflate a timeout in absolutetime.
921  */
922 static uint64_t
virtual_timeout_inflate_abs(unsigned int vti,uint64_t timeout)923 virtual_timeout_inflate_abs(unsigned int vti, uint64_t timeout)
924 {
925 	uint64_t max_timeout;
926 	nanoseconds_to_absolutetime(max_timeout_ns, &max_timeout);
927 	return virtual_timeout_inflate64(vti, timeout, max_timeout);
928 }
929 
930 /*
931  * Inflate a value in TSC ticks.
932  */
933 static uint64_t
virtual_timeout_inflate_tsc(unsigned int vti,uint64_t timeout)934 virtual_timeout_inflate_tsc(unsigned int vti, uint64_t timeout)
935 {
936 	const uint64_t max_timeout = tmrCvt(max_timeout_ns, tscFCvtn2t);
937 	return virtual_timeout_inflate64(vti, timeout, max_timeout);
938 }
939 
940 /*
941  * Inflate a timeout in microseconds.
942  */
943 static uint32_t
virtual_timeout_inflate_us(unsigned int vti,uint64_t timeout)944 virtual_timeout_inflate_us(unsigned int vti, uint64_t timeout)
945 {
946 	const uint32_t max_timeout = ~0;
947 	return virtual_timeout_inflate32(vti, timeout, max_timeout);
948 }
949 
950 uint64_t
ml_get_timebase_entropy(void)951 ml_get_timebase_entropy(void)
952 {
953 	return __builtin_ia32_rdtsc();
954 }
955 
956 /*
957  *	Routine:        ml_init_lock_timeout
958  *	Function:
959  */
960 static void __startup_func
ml_init_lock_timeout(void)961 ml_init_lock_timeout(void)
962 {
963 	uint64_t        abstime;
964 	uint32_t        mtxspin;
965 #if DEVELOPMENT || DEBUG
966 	uint64_t        default_timeout_ns = NSEC_PER_SEC >> 2;
967 #else
968 	uint64_t        default_timeout_ns = NSEC_PER_SEC >> 1;
969 #endif
970 	uint32_t        slto;
971 	uint32_t        prt;
972 
973 	if (PE_parse_boot_argn("slto_us", &slto, sizeof(slto))) {
974 		default_timeout_ns = slto * NSEC_PER_USEC;
975 	}
976 
977 	/*
978 	 * LockTimeOut is absolutetime, LockTimeOutTSC is in TSC ticks,
979 	 * and LockTimeOutUsec is in microseconds and it's 32-bits.
980 	 */
981 	LockTimeOutUsec = (uint32_t) (default_timeout_ns / NSEC_PER_USEC);
982 	nanoseconds_to_absolutetime(default_timeout_ns, &abstime);
983 	LockTimeOut = abstime;
984 	LockTimeOutTSC = tmrCvt(abstime, tscFCvtn2t);
985 
986 	/*
987 	 * TLBTimeOut dictates the TLB flush timeout period. It defaults to
988 	 * LockTimeOut but can be overriden separately. In particular, a
989 	 * zero value inhibits the timeout-panic and cuts a trace evnt instead
990 	 * - see pmap_flush_tlbs().
991 	 */
992 	if (PE_parse_boot_argn("tlbto_us", &slto, sizeof(slto))) {
993 		default_timeout_ns = slto * NSEC_PER_USEC;
994 		nanoseconds_to_absolutetime(default_timeout_ns, &abstime);
995 		TLBTimeOut = (uint32_t) abstime;
996 	} else {
997 		TLBTimeOut = LockTimeOut;
998 	}
999 
1000 #if DEVELOPMENT || DEBUG
1001 	report_phy_read_delay = LockTimeOut >> 1;
1002 #endif
1003 	if (PE_parse_boot_argn("phyreadmaxus", &slto, sizeof(slto))) {
1004 		default_timeout_ns = slto * NSEC_PER_USEC;
1005 		nanoseconds_to_absolutetime(default_timeout_ns, &abstime);
1006 		report_phy_read_delay = abstime;
1007 	}
1008 
1009 	if (PE_parse_boot_argn("phywritemaxus", &slto, sizeof(slto))) {
1010 		nanoseconds_to_absolutetime((uint64_t)slto * NSEC_PER_USEC, &abstime);
1011 		report_phy_write_delay = abstime;
1012 	}
1013 
1014 	if (PE_parse_boot_argn("tracephyreadus", &slto, sizeof(slto))) {
1015 		nanoseconds_to_absolutetime((uint64_t)slto * NSEC_PER_USEC, &abstime);
1016 		trace_phy_read_delay = abstime;
1017 	}
1018 
1019 	if (PE_parse_boot_argn("tracephywriteus", &slto, sizeof(slto))) {
1020 		nanoseconds_to_absolutetime((uint64_t)slto * NSEC_PER_USEC, &abstime);
1021 		trace_phy_write_delay = abstime;
1022 	}
1023 
1024 	if (PE_parse_boot_argn("mtxspin", &mtxspin, sizeof(mtxspin))) {
1025 		if (mtxspin > USEC_PER_SEC >> 4) {
1026 			mtxspin =  USEC_PER_SEC >> 4;
1027 		}
1028 		nanoseconds_to_absolutetime(mtxspin * NSEC_PER_USEC, &abstime);
1029 	} else {
1030 		nanoseconds_to_absolutetime(10 * NSEC_PER_USEC, &abstime);
1031 	}
1032 	MutexSpin = (unsigned int)abstime;
1033 	low_MutexSpin = MutexSpin;
1034 	/*
1035 	 * high_MutexSpin should be initialized as low_MutexSpin * real_ncpus, but
1036 	 * real_ncpus is not set at this time
1037 	 */
1038 	high_MutexSpin = -1;
1039 
1040 	nanoseconds_to_absolutetime(4ULL * NSEC_PER_SEC, &LastDebuggerEntryAllowance);
1041 	if (PE_parse_boot_argn("panic_restart_timeout", &prt, sizeof(prt))) {
1042 		nanoseconds_to_absolutetime(prt * NSEC_PER_SEC, &panic_restart_timeout);
1043 	}
1044 
1045 	virtualized = ((cpuid_features() & CPUID_FEATURE_VMM) != 0);
1046 	if (virtualized) {
1047 		unsigned int vti;
1048 
1049 		if (!PE_parse_boot_argn("vti", &vti, sizeof(vti))) {
1050 			vti = 6;
1051 		}
1052 
1053 #define VIRTUAL_TIMEOUT_INFLATE_ABS(_timeout)              \
1054 MACRO_BEGIN                                                \
1055 	_timeout = virtual_timeout_inflate_abs(vti, _timeout); \
1056 MACRO_END
1057 
1058 #define VIRTUAL_TIMEOUT_INFLATE_TSC(_timeout)              \
1059 MACRO_BEGIN                                                \
1060 	_timeout = virtual_timeout_inflate_tsc(vti, _timeout); \
1061 MACRO_END
1062 #define VIRTUAL_TIMEOUT_INFLATE_US(_timeout)               \
1063 MACRO_BEGIN                                                \
1064 	_timeout = virtual_timeout_inflate_us(vti, _timeout);  \
1065 MACRO_END
1066 		/*
1067 		 * These timeout values are inflated because they cause
1068 		 * the kernel to panic when they expire.
1069 		 * (Needed when running as a guest VM as the host OS
1070 		 * may not always schedule vcpu threads in time to
1071 		 * meet the deadline implied by the narrower time
1072 		 * window used on hardware.)
1073 		 */
1074 		VIRTUAL_TIMEOUT_INFLATE_US(LockTimeOutUsec);
1075 		VIRTUAL_TIMEOUT_INFLATE_ABS(LockTimeOut);
1076 		VIRTUAL_TIMEOUT_INFLATE_TSC(LockTimeOutTSC);
1077 		VIRTUAL_TIMEOUT_INFLATE_ABS(TLBTimeOut);
1078 		VIRTUAL_TIMEOUT_INFLATE_ABS(report_phy_read_delay);
1079 		VIRTUAL_TIMEOUT_INFLATE_TSC(lock_panic_timeout);
1080 	}
1081 
1082 	interrupt_latency_tracker_setup();
1083 }
1084 STARTUP(TIMEOUTS, STARTUP_RANK_MIDDLE, ml_init_lock_timeout);
1085 
1086 /*
1087  * Threshold above which we should attempt to block
1088  * instead of spinning for clock_delay_until().
1089  */
1090 
1091 void
ml_init_delay_spin_threshold(int threshold_us)1092 ml_init_delay_spin_threshold(int threshold_us)
1093 {
1094 	nanoseconds_to_absolutetime(threshold_us * NSEC_PER_USEC, &delay_spin_threshold);
1095 }
1096 
1097 boolean_t
ml_delay_should_spin(uint64_t interval)1098 ml_delay_should_spin(uint64_t interval)
1099 {
1100 	return (interval < delay_spin_threshold) ? TRUE : FALSE;
1101 }
1102 
1103 TUNABLE(uint32_t, yield_delay_us, "yield_delay_us", 0);
1104 
1105 void
ml_delay_on_yield(void)1106 ml_delay_on_yield(void)
1107 {
1108 #if DEVELOPMENT || DEBUG
1109 	if (yield_delay_us) {
1110 		delay(yield_delay_us);
1111 	}
1112 #endif
1113 }
1114 
1115 /*
1116  * This is called from the machine-independent layer
1117  * to perform machine-dependent info updates. Defer to cpu_thread_init().
1118  */
1119 void
ml_cpu_up(void)1120 ml_cpu_up(void)
1121 {
1122 	return;
1123 }
1124 
1125 void
ml_cpu_up_update_counts(__unused int cpu_id)1126 ml_cpu_up_update_counts(__unused int cpu_id)
1127 {
1128 	return;
1129 }
1130 
1131 /*
1132  * This is called from the machine-independent layer
1133  * to perform machine-dependent info updates.
1134  */
1135 void
ml_cpu_down(void)1136 ml_cpu_down(void)
1137 {
1138 	i386_deactivate_cpu();
1139 
1140 	return;
1141 }
1142 
1143 void
ml_cpu_down_update_counts(__unused int cpu_id)1144 ml_cpu_down_update_counts(__unused int cpu_id)
1145 {
1146 	return;
1147 }
1148 
1149 thread_t
current_thread(void)1150 current_thread(void)
1151 {
1152 	return current_thread_fast();
1153 }
1154 
1155 
1156 boolean_t
ml_is64bit(void)1157 ml_is64bit(void)
1158 {
1159 	return cpu_mode_is64bit();
1160 }
1161 
1162 
1163 boolean_t
ml_thread_is64bit(thread_t thread)1164 ml_thread_is64bit(thread_t thread)
1165 {
1166 	return thread_is_64bit_addr(thread);
1167 }
1168 
1169 
1170 boolean_t
ml_state_is64bit(void * saved_state)1171 ml_state_is64bit(void *saved_state)
1172 {
1173 	return is_saved_state64(saved_state);
1174 }
1175 
1176 void
ml_cpu_set_ldt(int selector)1177 ml_cpu_set_ldt(int selector)
1178 {
1179 	/*
1180 	 * Avoid loading the LDT
1181 	 * if we're setting the KERNEL LDT and it's already set.
1182 	 */
1183 	if (selector == KERNEL_LDT &&
1184 	    current_cpu_datap()->cpu_ldt == KERNEL_LDT) {
1185 		return;
1186 	}
1187 
1188 	lldt(selector);
1189 	current_cpu_datap()->cpu_ldt = selector;
1190 }
1191 
1192 void
ml_fp_setvalid(boolean_t value)1193 ml_fp_setvalid(boolean_t value)
1194 {
1195 	fp_setvalid(value);
1196 }
1197 
1198 uint64_t
ml_cpu_int_event_time(void)1199 ml_cpu_int_event_time(void)
1200 {
1201 	return current_cpu_datap()->cpu_int_event_time;
1202 }
1203 
1204 vm_offset_t
ml_stack_remaining(void)1205 ml_stack_remaining(void)
1206 {
1207 	uintptr_t local = (uintptr_t) &local;
1208 
1209 	if (ml_at_interrupt_context() != 0) {
1210 		return local - (current_cpu_datap()->cpu_int_stack_top - INTSTACK_SIZE);
1211 	} else {
1212 		return local - current_thread()->kernel_stack;
1213 	}
1214 }
1215 
1216 #if KASAN
1217 vm_offset_t ml_stack_base(void);
1218 vm_size_t ml_stack_size(void);
1219 
1220 vm_offset_t
ml_stack_base(void)1221 ml_stack_base(void)
1222 {
1223 	if (ml_at_interrupt_context()) {
1224 		return current_cpu_datap()->cpu_int_stack_top - INTSTACK_SIZE;
1225 	} else {
1226 		return current_thread()->kernel_stack;
1227 	}
1228 }
1229 
1230 vm_size_t
ml_stack_size(void)1231 ml_stack_size(void)
1232 {
1233 	if (ml_at_interrupt_context()) {
1234 		return INTSTACK_SIZE;
1235 	} else {
1236 		return kernel_stack_size;
1237 	}
1238 }
1239 #endif
1240 
1241 #if CONFIG_KCOV
1242 kcov_cpu_data_t *
current_kcov_data(void)1243 current_kcov_data(void)
1244 {
1245 	return &current_cpu_datap()->cpu_kcov_data;
1246 }
1247 
1248 kcov_cpu_data_t *
cpu_kcov_data(int cpuid)1249 cpu_kcov_data(int cpuid)
1250 {
1251 	return &cpu_datap(cpuid)->cpu_kcov_data;
1252 }
1253 #endif /* CONFIG_KCOV */
1254 
1255 void
kernel_preempt_check(void)1256 kernel_preempt_check(void)
1257 {
1258 	boolean_t       intr;
1259 	unsigned long flags;
1260 
1261 	assert(get_preemption_level() == 0);
1262 
1263 	if (__improbable(*ast_pending() & AST_URGENT)) {
1264 		/*
1265 		 * can handle interrupts and preemptions
1266 		 * at this point
1267 		 */
1268 		__asm__ volatile ("pushf; pop	%0"  :  "=r" (flags));
1269 
1270 		intr = ((flags & EFL_IF) != 0);
1271 
1272 		/*
1273 		 * now cause the PRE-EMPTION trap
1274 		 */
1275 		if (intr == TRUE) {
1276 			__asm__ volatile ("int %0" :: "N" (T_PREEMPT));
1277 		}
1278 	}
1279 }
1280 
1281 boolean_t
machine_timeout_suspended(void)1282 machine_timeout_suspended(void)
1283 {
1284 	return pmap_tlb_flush_timeout || lck_spinlock_timeout_in_progress ||
1285 	       panic_active() || mp_recent_debugger_activity() ||
1286 	       ml_recent_wake();
1287 }
1288 
1289 /* Eagerly evaluate all pending timer and thread callouts
1290  */
1291 void
ml_timer_evaluate(void)1292 ml_timer_evaluate(void)
1293 {
1294 	KERNEL_DEBUG_CONSTANT(DECR_TIMER_RESCAN | DBG_FUNC_START, 0, 0, 0, 0, 0);
1295 
1296 	uint64_t te_end, te_start = mach_absolute_time();
1297 	simple_lock(&ml_timer_evaluation_slock, LCK_GRP_NULL);
1298 	ml_timer_evaluation_in_progress = TRUE;
1299 	thread_call_delayed_timer_rescan_all();
1300 	mp_cpus_call(CPUMASK_ALL, ASYNC, timer_queue_expire_rescan, NULL);
1301 	ml_timer_evaluation_in_progress = FALSE;
1302 	ml_timer_eager_evaluations++;
1303 	te_end = mach_absolute_time();
1304 	ml_timer_eager_evaluation_max = MAX(ml_timer_eager_evaluation_max, (te_end - te_start));
1305 	simple_unlock(&ml_timer_evaluation_slock);
1306 
1307 	KERNEL_DEBUG_CONSTANT(DECR_TIMER_RESCAN | DBG_FUNC_END, 0, 0, 0, 0, 0);
1308 }
1309 
1310 boolean_t
ml_timer_forced_evaluation(void)1311 ml_timer_forced_evaluation(void)
1312 {
1313 	return ml_timer_evaluation_in_progress;
1314 }
1315 
1316 void
ml_gpu_stat_update(uint64_t gpu_ns_delta)1317 ml_gpu_stat_update(uint64_t gpu_ns_delta)
1318 {
1319 	current_thread()->machine.thread_gpu_ns += gpu_ns_delta;
1320 }
1321 
1322 uint64_t
ml_gpu_stat(thread_t t)1323 ml_gpu_stat(thread_t t)
1324 {
1325 	return t->machine.thread_gpu_ns;
1326 }
1327 
1328 int plctrace_enabled = 0;
1329 
1330 void
_disable_preemption(void)1331 _disable_preemption(void)
1332 {
1333 	disable_preemption_internal();
1334 }
1335 
1336 void
_enable_preemption(void)1337 _enable_preemption(void)
1338 {
1339 	enable_preemption_internal();
1340 }
1341 
1342 void
plctrace_disable(void)1343 plctrace_disable(void)
1344 {
1345 	plctrace_enabled = 0;
1346 }
1347 
1348 static boolean_t ml_quiescing;
1349 
1350 void
ml_set_is_quiescing(boolean_t quiescing)1351 ml_set_is_quiescing(boolean_t quiescing)
1352 {
1353 	ml_quiescing = quiescing;
1354 }
1355 
1356 boolean_t
ml_is_quiescing(void)1357 ml_is_quiescing(void)
1358 {
1359 	return ml_quiescing;
1360 }
1361 
1362 uint64_t
ml_get_booter_memory_size(void)1363 ml_get_booter_memory_size(void)
1364 {
1365 	return 0;
1366 }
1367 
1368 void
machine_lockdown(void)1369 machine_lockdown(void)
1370 {
1371 	x86_64_protect_data_const();
1372 }
1373 
1374 void
ml_cpu_begin_state_transition(__unused int cpu_id)1375 ml_cpu_begin_state_transition(__unused int cpu_id)
1376 {
1377 }
1378 
1379 void
ml_cpu_end_state_transition(__unused int cpu_id)1380 ml_cpu_end_state_transition(__unused int cpu_id)
1381 {
1382 }
1383 
1384 void
ml_cpu_begin_loop(void)1385 ml_cpu_begin_loop(void)
1386 {
1387 }
1388 
1389 void
ml_cpu_end_loop(void)1390 ml_cpu_end_loop(void)
1391 {
1392 }
1393 
1394 size_t
ml_get_vm_reserved_regions(bool vm_is64bit,const struct vm_reserved_region ** regions)1395 ml_get_vm_reserved_regions(bool vm_is64bit, const struct vm_reserved_region **regions)
1396 {
1397 #pragma unused(vm_is64bit)
1398 	assert(regions != NULL);
1399 
1400 	*regions = NULL;
1401 	return 0;
1402 }
1403 
1404 void
ml_cpu_power_enable(__unused int cpu_id)1405 ml_cpu_power_enable(__unused int cpu_id)
1406 {
1407 }
1408 
1409 void
ml_cpu_power_disable(__unused int cpu_id)1410 ml_cpu_power_disable(__unused int cpu_id)
1411 {
1412 }
1413 
1414 int
ml_page_protection_type(void)1415 ml_page_protection_type(void)
1416 {
1417 	return 0; // not supported on x86
1418 }
1419 
1420 bool
ml_addr_in_non_xnu_stack(__unused uintptr_t addr)1421 ml_addr_in_non_xnu_stack(__unused uintptr_t addr)
1422 {
1423 	/* There are no non-XNU stacks on x86 systems. */
1424 	return false;
1425 }
1426 
1427 /**
1428  * Explicitly preallocates a floating point save area.
1429  */
1430 void
ml_fp_save_area_prealloc(void)1431 ml_fp_save_area_prealloc(void)
1432 {
1433 	fpnoextflt();
1434 }
1435 
1436 void
ml_task_post_signature_processing_hook(__unused task_t task)1437 ml_task_post_signature_processing_hook(__unused task_t task)
1438 {
1439 }
1440