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