]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/machine_routines.c
xnu-2422.90.20.tar.gz
[apple/xnu.git] / osfmk / i386 / machine_routines.c
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/io_map_entries.h>
31 #include <i386/cpuid.h>
32 #include <i386/fpu.h>
33 #include <mach/processor.h>
34 #include <kern/processor.h>
35 #include <kern/machine.h>
36 #include <kern/cpu_data.h>
37 #include <kern/cpu_number.h>
38 #include <kern/thread.h>
39 #include <kern/thread_call.h>
40 #include <i386/machine_cpu.h>
41 #include <i386/lapic.h>
42 #include <i386/lock.h>
43 #include <i386/mp_events.h>
44 #include <i386/pmCPU.h>
45 #include <i386/trap.h>
46 #include <i386/tsc.h>
47 #include <i386/cpu_threads.h>
48 #include <i386/proc_reg.h>
49 #include <mach/vm_param.h>
50 #include <i386/pmap.h>
51 #include <i386/pmap_internal.h>
52 #include <i386/misc_protos.h>
53 #include <kern/timer_queue.h>
54 #if KPC
55 #include <kern/kpc.h>
56 #endif
57
58 #if DEBUG
59 #define DBG(x...) kprintf("DBG: " x)
60 #else
61 #define DBG(x...)
62 #endif
63
64 extern void wakeup(void *);
65
66 static int max_cpus_initialized = 0;
67
68 unsigned int LockTimeOut;
69 unsigned int LockTimeOutTSC;
70 unsigned int MutexSpin;
71 uint64_t LastDebuggerEntryAllowance;
72 uint64_t delay_spin_threshold;
73
74 extern uint64_t panic_restart_timeout;
75
76 boolean_t virtualized = FALSE;
77
78 decl_simple_lock_data(static, ml_timer_evaluation_slock);
79 uint32_t ml_timer_eager_evaluations;
80 uint64_t ml_timer_eager_evaluation_max;
81 static boolean_t ml_timer_evaluation_in_progress = FALSE;
82
83
84 #define MAX_CPUS_SET 0x1
85 #define MAX_CPUS_WAIT 0x2
86
87 /* IO memory map services */
88
89 /* Map memory map IO space */
90 vm_offset_t ml_io_map(
91 vm_offset_t phys_addr,
92 vm_size_t size)
93 {
94 return(io_map(phys_addr,size,VM_WIMG_IO));
95 }
96
97 /* boot memory allocation */
98 vm_offset_t ml_static_malloc(
99 __unused vm_size_t size)
100 {
101 return((vm_offset_t)NULL);
102 }
103
104
105 void ml_get_bouncepool_info(vm_offset_t *phys_addr, vm_size_t *size)
106 {
107 *phys_addr = 0;
108 *size = 0;
109 }
110
111
112 vm_offset_t
113 ml_static_ptovirt(
114 vm_offset_t paddr)
115 {
116 #if defined(__x86_64__)
117 return (vm_offset_t)(((unsigned long) paddr) | VM_MIN_KERNEL_ADDRESS);
118 #else
119 return (vm_offset_t)((paddr) | LINEAR_KERNEL_ADDRESS);
120 #endif
121 }
122
123
124 /*
125 * Routine: ml_static_mfree
126 * Function:
127 */
128 void
129 ml_static_mfree(
130 vm_offset_t vaddr,
131 vm_size_t size)
132 {
133 addr64_t vaddr_cur;
134 ppnum_t ppn;
135 uint32_t freed_pages = 0;
136 assert(vaddr >= VM_MIN_KERNEL_ADDRESS);
137
138 assert((vaddr & (PAGE_SIZE-1)) == 0); /* must be page aligned */
139
140 for (vaddr_cur = vaddr;
141 vaddr_cur < round_page_64(vaddr+size);
142 vaddr_cur += PAGE_SIZE) {
143 ppn = pmap_find_phys(kernel_pmap, vaddr_cur);
144 if (ppn != (vm_offset_t)NULL) {
145 kernel_pmap->stats.resident_count++;
146 if (kernel_pmap->stats.resident_count >
147 kernel_pmap->stats.resident_max) {
148 kernel_pmap->stats.resident_max =
149 kernel_pmap->stats.resident_count;
150 }
151 pmap_remove(kernel_pmap, vaddr_cur, vaddr_cur+PAGE_SIZE);
152 assert(pmap_valid_page(ppn));
153
154 if (IS_MANAGED_PAGE(ppn)) {
155 vm_page_create(ppn,(ppn+1));
156 vm_page_wire_count--;
157 freed_pages++;
158 }
159 }
160 }
161 #if DEBUG
162 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);
163 #endif
164 }
165
166
167 /* virtual to physical on wired pages */
168 vm_offset_t ml_vtophys(
169 vm_offset_t vaddr)
170 {
171 return (vm_offset_t)kvtophys(vaddr);
172 }
173
174 /*
175 * Routine: ml_nofault_copy
176 * Function: Perform a physical mode copy if the source and
177 * destination have valid translations in the kernel pmap.
178 * If translations are present, they are assumed to
179 * be wired; i.e. no attempt is made to guarantee that the
180 * translations obtained remained valid for
181 * the duration of the copy process.
182 */
183
184 vm_size_t ml_nofault_copy(
185 vm_offset_t virtsrc, vm_offset_t virtdst, vm_size_t size)
186 {
187 addr64_t cur_phys_dst, cur_phys_src;
188 uint32_t count, nbytes = 0;
189
190 while (size > 0) {
191 if (!(cur_phys_src = kvtophys(virtsrc)))
192 break;
193 if (!(cur_phys_dst = kvtophys(virtdst)))
194 break;
195 if (!pmap_valid_page(i386_btop(cur_phys_dst)) || !pmap_valid_page(i386_btop(cur_phys_src)))
196 break;
197 count = (uint32_t)(PAGE_SIZE - (cur_phys_src & PAGE_MASK));
198 if (count > (PAGE_SIZE - (cur_phys_dst & PAGE_MASK)))
199 count = (uint32_t)(PAGE_SIZE - (cur_phys_dst & PAGE_MASK));
200 if (count > size)
201 count = (uint32_t)size;
202
203 bcopy_phys(cur_phys_src, cur_phys_dst, count);
204
205 nbytes += count;
206 virtsrc += count;
207 virtdst += count;
208 size -= count;
209 }
210
211 return nbytes;
212 }
213
214 /*
215 * Routine: ml_validate_nofault
216 * Function: Validate that ths address range has a valid translations
217 * in the kernel pmap. If translations are present, they are
218 * assumed to be wired; i.e. no attempt is made to guarantee
219 * that the translation persist after the check.
220 * Returns: TRUE if the range is mapped and will not cause a fault,
221 * FALSE otherwise.
222 */
223
224 boolean_t ml_validate_nofault(
225 vm_offset_t virtsrc, vm_size_t size)
226 {
227 addr64_t cur_phys_src;
228 uint32_t count;
229
230 while (size > 0) {
231 if (!(cur_phys_src = kvtophys(virtsrc)))
232 return FALSE;
233 if (!pmap_valid_page(i386_btop(cur_phys_src)))
234 return FALSE;
235 count = (uint32_t)(PAGE_SIZE - (cur_phys_src & PAGE_MASK));
236 if (count > size)
237 count = (uint32_t)size;
238
239 virtsrc += count;
240 size -= count;
241 }
242
243 return TRUE;
244 }
245
246 /* Interrupt handling */
247
248 /* Initialize Interrupts */
249 void ml_init_interrupt(void)
250 {
251 (void) ml_set_interrupts_enabled(TRUE);
252 }
253
254
255 /* Get Interrupts Enabled */
256 boolean_t ml_get_interrupts_enabled(void)
257 {
258 unsigned long flags;
259
260 __asm__ volatile("pushf; pop %0" : "=r" (flags));
261 return (flags & EFL_IF) != 0;
262 }
263
264 /* Set Interrupts Enabled */
265 boolean_t ml_set_interrupts_enabled(boolean_t enable)
266 {
267 unsigned long flags;
268 boolean_t istate;
269
270 __asm__ volatile("pushf; pop %0" : "=r" (flags));
271
272 assert(get_interrupt_level() ? (enable == FALSE) : TRUE);
273
274 istate = ((flags & EFL_IF) != 0);
275
276 if (enable) {
277 __asm__ volatile("sti;nop");
278
279 if ((get_preemption_level() == 0) && (*ast_pending() & AST_URGENT))
280 __asm__ volatile ("int $0xff");
281 }
282 else {
283 if (istate)
284 __asm__ volatile("cli");
285 }
286
287 return istate;
288 }
289
290 /* Check if running at interrupt context */
291 boolean_t ml_at_interrupt_context(void)
292 {
293 return get_interrupt_level() != 0;
294 }
295
296 void ml_get_power_state(boolean_t *icp, boolean_t *pidlep) {
297 *icp = (get_interrupt_level() != 0);
298 /* These will be technically inaccurate for interrupts that occur
299 * successively within a single "idle exit" event, but shouldn't
300 * matter statistically.
301 */
302 *pidlep = (current_cpu_datap()->lcpu.package->num_idle == topoParms.nLThreadsPerPackage);
303 }
304
305 /* Generate a fake interrupt */
306 void ml_cause_interrupt(void)
307 {
308 panic("ml_cause_interrupt not defined yet on Intel");
309 }
310
311 void ml_thread_policy(
312 thread_t thread,
313 __unused unsigned policy_id,
314 unsigned policy_info)
315 {
316 if (policy_info & MACHINE_NETWORK_WORKLOOP) {
317 spl_t s = splsched();
318
319 thread_lock(thread);
320
321 set_priority(thread, thread->priority + 1);
322
323 thread_unlock(thread);
324 splx(s);
325 }
326 }
327
328 /* Initialize Interrupts */
329 void ml_install_interrupt_handler(
330 void *nub,
331 int source,
332 void *target,
333 IOInterruptHandler handler,
334 void *refCon)
335 {
336 boolean_t current_state;
337
338 current_state = ml_get_interrupts_enabled();
339
340 PE_install_interrupt_handler(nub, source, target,
341 (IOInterruptHandler) handler, refCon);
342
343 (void) ml_set_interrupts_enabled(current_state);
344
345 initialize_screen(NULL, kPEAcquireScreen);
346 }
347
348
349 void
350 machine_signal_idle(
351 processor_t processor)
352 {
353 cpu_interrupt(processor->cpu_id);
354 }
355
356 static kern_return_t
357 register_cpu(
358 uint32_t lapic_id,
359 processor_t *processor_out,
360 boolean_t boot_cpu )
361 {
362 int target_cpu;
363 cpu_data_t *this_cpu_datap;
364
365 this_cpu_datap = cpu_data_alloc(boot_cpu);
366 if (this_cpu_datap == NULL) {
367 return KERN_FAILURE;
368 }
369 target_cpu = this_cpu_datap->cpu_number;
370 assert((boot_cpu && (target_cpu == 0)) ||
371 (!boot_cpu && (target_cpu != 0)));
372
373 lapic_cpu_map(lapic_id, target_cpu);
374
375 /* The cpu_id is not known at registration phase. Just do
376 * lapic_id for now
377 */
378 this_cpu_datap->cpu_phys_number = lapic_id;
379
380 this_cpu_datap->cpu_console_buf = console_cpu_alloc(boot_cpu);
381 if (this_cpu_datap->cpu_console_buf == NULL)
382 goto failed;
383
384 this_cpu_datap->cpu_chud = chudxnu_cpu_alloc(boot_cpu);
385 if (this_cpu_datap->cpu_chud == NULL)
386 goto failed;
387
388 #if KPC
389 this_cpu_datap->cpu_kpc_buf[0] = kpc_counterbuf_alloc();
390 if(this_cpu_datap->cpu_kpc_buf[0] == NULL )
391 goto failed;
392 this_cpu_datap->cpu_kpc_buf[1] = kpc_counterbuf_alloc();
393 if(this_cpu_datap->cpu_kpc_buf[1] == NULL )
394 goto failed;
395
396 this_cpu_datap->cpu_kpc_shadow = kpc_counterbuf_alloc();
397 if(this_cpu_datap->cpu_kpc_shadow == NULL )
398 goto failed;
399
400 this_cpu_datap->cpu_kpc_reload = kpc_counterbuf_alloc();
401 if(this_cpu_datap->cpu_kpc_reload == NULL )
402 goto failed;
403 #endif
404
405 if (!boot_cpu) {
406 cpu_thread_alloc(this_cpu_datap->cpu_number);
407 if (this_cpu_datap->lcpu.core == NULL)
408 goto failed;
409
410 #if NCOPY_WINDOWS > 0
411 this_cpu_datap->cpu_pmap = pmap_cpu_alloc(boot_cpu);
412 if (this_cpu_datap->cpu_pmap == NULL)
413 goto failed;
414 #endif
415
416 this_cpu_datap->cpu_processor = cpu_processor_alloc(boot_cpu);
417 if (this_cpu_datap->cpu_processor == NULL)
418 goto failed;
419 /*
420 * processor_init() deferred to topology start
421 * because "slot numbers" a.k.a. logical processor numbers
422 * are not yet finalized.
423 */
424 }
425
426 *processor_out = this_cpu_datap->cpu_processor;
427
428 return KERN_SUCCESS;
429
430 failed:
431 cpu_processor_free(this_cpu_datap->cpu_processor);
432 #if NCOPY_WINDOWS > 0
433 pmap_cpu_free(this_cpu_datap->cpu_pmap);
434 #endif
435 chudxnu_cpu_free(this_cpu_datap->cpu_chud);
436 console_cpu_free(this_cpu_datap->cpu_console_buf);
437 #if KPC
438 kpc_counterbuf_free(this_cpu_datap->cpu_kpc_buf[0]);
439 kpc_counterbuf_free(this_cpu_datap->cpu_kpc_buf[1]);
440 kpc_counterbuf_free(this_cpu_datap->cpu_kpc_shadow);
441 kpc_counterbuf_free(this_cpu_datap->cpu_kpc_reload);
442 #endif
443
444 return KERN_FAILURE;
445 }
446
447
448 kern_return_t
449 ml_processor_register(
450 cpu_id_t cpu_id,
451 uint32_t lapic_id,
452 processor_t *processor_out,
453 boolean_t boot_cpu,
454 boolean_t start )
455 {
456 static boolean_t done_topo_sort = FALSE;
457 static uint32_t num_registered = 0;
458
459 /* Register all CPUs first, and track max */
460 if( start == FALSE )
461 {
462 num_registered++;
463
464 DBG( "registering CPU lapic id %d\n", lapic_id );
465
466 return register_cpu( lapic_id, processor_out, boot_cpu );
467 }
468
469 /* Sort by topology before we start anything */
470 if( !done_topo_sort )
471 {
472 DBG( "about to start CPUs. %d registered\n", num_registered );
473
474 cpu_topology_sort( num_registered );
475 done_topo_sort = TRUE;
476 }
477
478 /* Assign the cpu ID */
479 uint32_t cpunum = -1;
480 cpu_data_t *this_cpu_datap = NULL;
481
482 /* find cpu num and pointer */
483 cpunum = ml_get_cpuid( lapic_id );
484
485 if( cpunum == 0xFFFFFFFF ) /* never heard of it? */
486 panic( "trying to start invalid/unregistered CPU %d\n", lapic_id );
487
488 this_cpu_datap = cpu_datap(cpunum);
489
490 /* fix the CPU id */
491 this_cpu_datap->cpu_id = cpu_id;
492
493 /* output arg */
494 *processor_out = this_cpu_datap->cpu_processor;
495
496 /* OK, try and start this CPU */
497 return cpu_topology_start_cpu( cpunum );
498 }
499
500
501 void
502 ml_cpu_get_info(ml_cpu_info_t *cpu_infop)
503 {
504 boolean_t os_supports_sse;
505 i386_cpu_info_t *cpuid_infop;
506
507 if (cpu_infop == NULL)
508 return;
509
510 /*
511 * Are we supporting MMX/SSE/SSE2/SSE3?
512 * As distinct from whether the cpu has these capabilities.
513 */
514 os_supports_sse = !!(get_cr4() & CR4_OSXMM);
515
516 if (ml_fpu_avx_enabled())
517 cpu_infop->vector_unit = 9;
518 else if ((cpuid_features() & CPUID_FEATURE_SSE4_2) && os_supports_sse)
519 cpu_infop->vector_unit = 8;
520 else if ((cpuid_features() & CPUID_FEATURE_SSE4_1) && os_supports_sse)
521 cpu_infop->vector_unit = 7;
522 else if ((cpuid_features() & CPUID_FEATURE_SSSE3) && os_supports_sse)
523 cpu_infop->vector_unit = 6;
524 else if ((cpuid_features() & CPUID_FEATURE_SSE3) && os_supports_sse)
525 cpu_infop->vector_unit = 5;
526 else if ((cpuid_features() & CPUID_FEATURE_SSE2) && os_supports_sse)
527 cpu_infop->vector_unit = 4;
528 else if ((cpuid_features() & CPUID_FEATURE_SSE) && os_supports_sse)
529 cpu_infop->vector_unit = 3;
530 else if (cpuid_features() & CPUID_FEATURE_MMX)
531 cpu_infop->vector_unit = 2;
532 else
533 cpu_infop->vector_unit = 0;
534
535 cpuid_infop = cpuid_info();
536
537 cpu_infop->cache_line_size = cpuid_infop->cache_linesize;
538
539 cpu_infop->l1_icache_size = cpuid_infop->cache_size[L1I];
540 cpu_infop->l1_dcache_size = cpuid_infop->cache_size[L1D];
541
542 if (cpuid_infop->cache_size[L2U] > 0) {
543 cpu_infop->l2_settings = 1;
544 cpu_infop->l2_cache_size = cpuid_infop->cache_size[L2U];
545 } else {
546 cpu_infop->l2_settings = 0;
547 cpu_infop->l2_cache_size = 0xFFFFFFFF;
548 }
549
550 if (cpuid_infop->cache_size[L3U] > 0) {
551 cpu_infop->l3_settings = 1;
552 cpu_infop->l3_cache_size = cpuid_infop->cache_size[L3U];
553 } else {
554 cpu_infop->l3_settings = 0;
555 cpu_infop->l3_cache_size = 0xFFFFFFFF;
556 }
557 }
558
559 void
560 ml_init_max_cpus(unsigned long max_cpus)
561 {
562 boolean_t current_state;
563
564 current_state = ml_set_interrupts_enabled(FALSE);
565 if (max_cpus_initialized != MAX_CPUS_SET) {
566 if (max_cpus > 0 && max_cpus <= MAX_CPUS) {
567 /*
568 * Note: max_cpus is the number of enabled processors
569 * that ACPI found; max_ncpus is the maximum number
570 * that the kernel supports or that the "cpus="
571 * boot-arg has set. Here we take int minimum.
572 */
573 machine_info.max_cpus = (integer_t)MIN(max_cpus, max_ncpus);
574 }
575 if (max_cpus_initialized == MAX_CPUS_WAIT)
576 wakeup((event_t)&max_cpus_initialized);
577 max_cpus_initialized = MAX_CPUS_SET;
578 }
579 (void) ml_set_interrupts_enabled(current_state);
580 }
581
582 int
583 ml_get_max_cpus(void)
584 {
585 boolean_t current_state;
586
587 current_state = ml_set_interrupts_enabled(FALSE);
588 if (max_cpus_initialized != MAX_CPUS_SET) {
589 max_cpus_initialized = MAX_CPUS_WAIT;
590 assert_wait((event_t)&max_cpus_initialized, THREAD_UNINT);
591 (void)thread_block(THREAD_CONTINUE_NULL);
592 }
593 (void) ml_set_interrupts_enabled(current_state);
594 return(machine_info.max_cpus);
595 }
596
597 /*
598 * Routine: ml_init_lock_timeout
599 * Function:
600 */
601 void
602 ml_init_lock_timeout(void)
603 {
604 uint64_t abstime;
605 uint32_t mtxspin;
606 uint64_t default_timeout_ns = NSEC_PER_SEC>>2;
607 uint32_t slto;
608 uint32_t prt;
609
610 if (PE_parse_boot_argn("slto_us", &slto, sizeof (slto)))
611 default_timeout_ns = slto * NSEC_PER_USEC;
612
613 /* LockTimeOut is absolutetime, LockTimeOutTSC is in TSC ticks */
614 nanoseconds_to_absolutetime(default_timeout_ns, &abstime);
615 LockTimeOut = (uint32_t) abstime;
616 LockTimeOutTSC = (uint32_t) tmrCvt(abstime, tscFCvtn2t);
617
618 if (PE_parse_boot_argn("mtxspin", &mtxspin, sizeof (mtxspin))) {
619 if (mtxspin > USEC_PER_SEC>>4)
620 mtxspin = USEC_PER_SEC>>4;
621 nanoseconds_to_absolutetime(mtxspin*NSEC_PER_USEC, &abstime);
622 } else {
623 nanoseconds_to_absolutetime(10*NSEC_PER_USEC, &abstime);
624 }
625 MutexSpin = (unsigned int)abstime;
626
627 nanoseconds_to_absolutetime(4ULL * NSEC_PER_SEC, &LastDebuggerEntryAllowance);
628 if (PE_parse_boot_argn("panic_restart_timeout", &prt, sizeof (prt)))
629 nanoseconds_to_absolutetime(prt * NSEC_PER_SEC, &panic_restart_timeout);
630 virtualized = ((cpuid_features() & CPUID_FEATURE_VMM) != 0);
631 interrupt_latency_tracker_setup();
632 simple_lock_init(&ml_timer_evaluation_slock, 0);
633 }
634
635 /*
636 * Threshold above which we should attempt to block
637 * instead of spinning for clock_delay_until().
638 */
639
640 void
641 ml_init_delay_spin_threshold(int threshold_us)
642 {
643 nanoseconds_to_absolutetime(threshold_us * NSEC_PER_USEC, &delay_spin_threshold);
644 }
645
646 boolean_t
647 ml_delay_should_spin(uint64_t interval)
648 {
649 return (interval < delay_spin_threshold) ? TRUE : FALSE;
650 }
651
652 /*
653 * This is called from the machine-independent layer
654 * to perform machine-dependent info updates. Defer to cpu_thread_init().
655 */
656 void
657 ml_cpu_up(void)
658 {
659 return;
660 }
661
662 /*
663 * This is called from the machine-independent layer
664 * to perform machine-dependent info updates.
665 */
666 void
667 ml_cpu_down(void)
668 {
669 i386_deactivate_cpu();
670
671 return;
672 }
673
674 /*
675 * The following are required for parts of the kernel
676 * that cannot resolve these functions as inlines:
677 */
678 extern thread_t current_act(void);
679 thread_t
680 current_act(void)
681 {
682 return(current_thread_fast());
683 }
684
685 #undef current_thread
686 extern thread_t current_thread(void);
687 thread_t
688 current_thread(void)
689 {
690 return(current_thread_fast());
691 }
692
693
694 boolean_t ml_is64bit(void) {
695
696 return (cpu_mode_is64bit());
697 }
698
699
700 boolean_t ml_thread_is64bit(thread_t thread) {
701
702 return (thread_is_64bit(thread));
703 }
704
705
706 boolean_t ml_state_is64bit(void *saved_state) {
707
708 return is_saved_state64(saved_state);
709 }
710
711 void ml_cpu_set_ldt(int selector)
712 {
713 /*
714 * Avoid loading the LDT
715 * if we're setting the KERNEL LDT and it's already set.
716 */
717 if (selector == KERNEL_LDT &&
718 current_cpu_datap()->cpu_ldt == KERNEL_LDT)
719 return;
720
721 lldt(selector);
722 current_cpu_datap()->cpu_ldt = selector;
723 }
724
725 void ml_fp_setvalid(boolean_t value)
726 {
727 fp_setvalid(value);
728 }
729
730 uint64_t ml_cpu_int_event_time(void)
731 {
732 return current_cpu_datap()->cpu_int_event_time;
733 }
734
735 vm_offset_t ml_stack_remaining(void)
736 {
737 uintptr_t local = (uintptr_t) &local;
738
739 if (ml_at_interrupt_context() != 0) {
740 return (local - (current_cpu_datap()->cpu_int_stack_top - INTSTACK_SIZE));
741 } else {
742 return (local - current_thread()->kernel_stack);
743 }
744 }
745
746 void
747 kernel_preempt_check(void)
748 {
749 boolean_t intr;
750 unsigned long flags;
751
752 assert(get_preemption_level() == 0);
753
754 __asm__ volatile("pushf; pop %0" : "=r" (flags));
755
756 intr = ((flags & EFL_IF) != 0);
757
758 if ((*ast_pending() & AST_URGENT) && intr == TRUE) {
759 /*
760 * can handle interrupts and preemptions
761 * at this point
762 */
763
764 /*
765 * now cause the PRE-EMPTION trap
766 */
767 __asm__ volatile ("int %0" :: "N" (T_PREEMPT));
768 }
769 }
770
771 boolean_t machine_timeout_suspended(void) {
772 return (virtualized || pmap_tlb_flush_timeout || spinlock_timed_out || panic_active() || mp_recent_debugger_activity());
773 }
774
775 /* Eagerly evaluate all pending timer and thread callouts
776 */
777 void ml_timer_evaluate(void) {
778 KERNEL_DEBUG_CONSTANT(DECR_TIMER_RESCAN|DBG_FUNC_START, 0, 0, 0, 0, 0);
779
780 uint64_t te_end, te_start = mach_absolute_time();
781 simple_lock(&ml_timer_evaluation_slock);
782 ml_timer_evaluation_in_progress = TRUE;
783 thread_call_delayed_timer_rescan_all();
784 mp_cpus_call(CPUMASK_ALL, ASYNC, timer_queue_expire_rescan, NULL);
785 ml_timer_evaluation_in_progress = FALSE;
786 ml_timer_eager_evaluations++;
787 te_end = mach_absolute_time();
788 ml_timer_eager_evaluation_max = MAX(ml_timer_eager_evaluation_max, (te_end - te_start));
789 simple_unlock(&ml_timer_evaluation_slock);
790
791 KERNEL_DEBUG_CONSTANT(DECR_TIMER_RESCAN|DBG_FUNC_END, 0, 0, 0, 0, 0);
792 }
793
794 boolean_t
795 ml_timer_forced_evaluation(void) {
796 return ml_timer_evaluation_in_progress;
797 }