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