2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <i386/machine_routines.h>
30 #include <i386/io_map_entries.h>
31 #include <i386/cpuid.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/lock.h>
42 #include <i386/mp_events.h>
43 #include <i386/pmCPU.h>
44 #include <i386/trap.h>
46 #include <i386/cpu_threads.h>
47 #include <i386/proc_reg.h>
48 #include <mach/vm_param.h>
49 #include <i386/pmap.h>
50 #include <i386/pmap_internal.h>
51 #include <i386/misc_protos.h>
54 #define DBG(x...) kprintf("DBG: " x)
59 extern void wakeup(void *);
61 static int max_cpus_initialized
= 0;
63 unsigned int LockTimeOut
;
64 unsigned int LockTimeOutTSC
;
65 unsigned int MutexSpin
;
66 uint64_t LastDebuggerEntryAllowance
;
67 uint64_t delay_spin_threshold
;
69 extern uint64_t panic_restart_timeout
;
71 boolean_t virtualized
= FALSE
;
73 #define MAX_CPUS_SET 0x1
74 #define MAX_CPUS_WAIT 0x2
76 /* IO memory map services */
78 /* Map memory map IO space */
79 vm_offset_t
ml_io_map(
80 vm_offset_t phys_addr
,
83 return(io_map(phys_addr
,size
,VM_WIMG_IO
));
86 /* boot memory allocation */
87 vm_offset_t
ml_static_malloc(
88 __unused vm_size_t size
)
90 return((vm_offset_t
)NULL
);
94 void ml_get_bouncepool_info(vm_offset_t
*phys_addr
, vm_size_t
*size
)
105 #if defined(__x86_64__)
106 return (vm_offset_t
)(((unsigned long) paddr
) | VM_MIN_KERNEL_ADDRESS
);
108 return (vm_offset_t
)((paddr
) | LINEAR_KERNEL_ADDRESS
);
114 * Routine: ml_static_mfree
124 uint32_t freed_pages
= 0;
125 assert(vaddr
>= VM_MIN_KERNEL_ADDRESS
);
127 assert((vaddr
& (PAGE_SIZE
-1)) == 0); /* must be page aligned */
129 for (vaddr_cur
= vaddr
;
130 vaddr_cur
< round_page_64(vaddr
+size
);
131 vaddr_cur
+= PAGE_SIZE
) {
132 ppn
= pmap_find_phys(kernel_pmap
, vaddr_cur
);
133 if (ppn
!= (vm_offset_t
)NULL
) {
134 kernel_pmap
->stats
.resident_count
++;
135 if (kernel_pmap
->stats
.resident_count
>
136 kernel_pmap
->stats
.resident_max
) {
137 kernel_pmap
->stats
.resident_max
=
138 kernel_pmap
->stats
.resident_count
;
140 pmap_remove(kernel_pmap
, vaddr_cur
, vaddr_cur
+PAGE_SIZE
);
141 assert(pmap_valid_page(ppn
));
143 if (IS_MANAGED_PAGE(ppn
)) {
144 vm_page_create(ppn
,(ppn
+1));
145 vm_page_wire_count
--;
151 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
);
156 /* virtual to physical on wired pages */
157 vm_offset_t
ml_vtophys(
160 return (vm_offset_t
)kvtophys(vaddr
);
164 * Routine: ml_nofault_copy
165 * Function: Perform a physical mode copy if the source and
166 * destination have valid translations in the kernel pmap.
167 * If translations are present, they are assumed to
168 * be wired; i.e. no attempt is made to guarantee that the
169 * translations obtained remained valid for
170 * the duration of the copy process.
173 vm_size_t
ml_nofault_copy(
174 vm_offset_t virtsrc
, vm_offset_t virtdst
, vm_size_t size
)
176 addr64_t cur_phys_dst
, cur_phys_src
;
177 uint32_t count
, nbytes
= 0;
180 if (!(cur_phys_src
= kvtophys(virtsrc
)))
182 if (!(cur_phys_dst
= kvtophys(virtdst
)))
184 if (!pmap_valid_page(i386_btop(cur_phys_dst
)) || !pmap_valid_page(i386_btop(cur_phys_src
)))
186 count
= (uint32_t)(PAGE_SIZE
- (cur_phys_src
& PAGE_MASK
));
187 if (count
> (PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
)))
188 count
= (uint32_t)(PAGE_SIZE
- (cur_phys_dst
& PAGE_MASK
));
190 count
= (uint32_t)size
;
192 bcopy_phys(cur_phys_src
, cur_phys_dst
, count
);
203 /* Interrupt handling */
205 /* Initialize Interrupts */
206 void ml_init_interrupt(void)
208 (void) ml_set_interrupts_enabled(TRUE
);
212 /* Get Interrupts Enabled */
213 boolean_t
ml_get_interrupts_enabled(void)
217 __asm__
volatile("pushf; pop %0" : "=r" (flags
));
218 return (flags
& EFL_IF
) != 0;
221 /* Set Interrupts Enabled */
222 boolean_t
ml_set_interrupts_enabled(boolean_t enable
)
227 __asm__
volatile("pushf; pop %0" : "=r" (flags
));
229 istate
= ((flags
& EFL_IF
) != 0);
232 __asm__
volatile("sti;nop");
234 if ((get_preemption_level() == 0) && (*ast_pending() & AST_URGENT
))
235 __asm__
volatile ("int $0xff");
239 __asm__
volatile("cli");
245 /* Check if running at interrupt context */
246 boolean_t
ml_at_interrupt_context(void)
248 return get_interrupt_level() != 0;
251 /* Generate a fake interrupt */
252 void ml_cause_interrupt(void)
254 panic("ml_cause_interrupt not defined yet on Intel");
257 void ml_thread_policy(
259 __unused
unsigned policy_id
,
260 unsigned policy_info
)
262 if (policy_info
& MACHINE_NETWORK_WORKLOOP
) {
263 spl_t s
= splsched();
267 set_priority(thread
, thread
->priority
+ 1);
269 thread_unlock(thread
);
274 /* Initialize Interrupts */
275 void ml_install_interrupt_handler(
279 IOInterruptHandler handler
,
282 boolean_t current_state
;
284 current_state
= ml_get_interrupts_enabled();
286 PE_install_interrupt_handler(nub
, source
, target
,
287 (IOInterruptHandler
) handler
, refCon
);
289 (void) ml_set_interrupts_enabled(current_state
);
291 initialize_screen(NULL
, kPEAcquireScreen
);
297 processor_t processor
)
299 cpu_interrupt(processor
->cpu_id
);
305 processor_t
*processor_out
,
309 cpu_data_t
*this_cpu_datap
;
311 this_cpu_datap
= cpu_data_alloc(boot_cpu
);
312 if (this_cpu_datap
== NULL
) {
315 target_cpu
= this_cpu_datap
->cpu_number
;
316 assert((boot_cpu
&& (target_cpu
== 0)) ||
317 (!boot_cpu
&& (target_cpu
!= 0)));
319 lapic_cpu_map(lapic_id
, target_cpu
);
321 /* The cpu_id is not known at registration phase. Just do
324 this_cpu_datap
->cpu_phys_number
= lapic_id
;
326 this_cpu_datap
->cpu_console_buf
= console_cpu_alloc(boot_cpu
);
327 if (this_cpu_datap
->cpu_console_buf
== NULL
)
330 this_cpu_datap
->cpu_chud
= chudxnu_cpu_alloc(boot_cpu
);
331 if (this_cpu_datap
->cpu_chud
== NULL
)
335 cpu_thread_alloc(this_cpu_datap
->cpu_number
);
336 if (this_cpu_datap
->lcpu
.core
== NULL
)
339 #if NCOPY_WINDOWS > 0
340 this_cpu_datap
->cpu_pmap
= pmap_cpu_alloc(boot_cpu
);
341 if (this_cpu_datap
->cpu_pmap
== NULL
)
345 this_cpu_datap
->cpu_processor
= cpu_processor_alloc(boot_cpu
);
346 if (this_cpu_datap
->cpu_processor
== NULL
)
349 * processor_init() deferred to topology start
350 * because "slot numbers" a.k.a. logical processor numbers
351 * are not yet finalized.
355 *processor_out
= this_cpu_datap
->cpu_processor
;
360 cpu_processor_free(this_cpu_datap
->cpu_processor
);
361 #if NCOPY_WINDOWS > 0
362 pmap_cpu_free(this_cpu_datap
->cpu_pmap
);
364 chudxnu_cpu_free(this_cpu_datap
->cpu_chud
);
365 console_cpu_free(this_cpu_datap
->cpu_console_buf
);
371 ml_processor_register(
374 processor_t
*processor_out
,
378 static boolean_t done_topo_sort
= FALSE
;
379 static uint32_t num_registered
= 0;
381 /* Register all CPUs first, and track max */
386 DBG( "registering CPU lapic id %d\n", lapic_id
);
388 return register_cpu( lapic_id
, processor_out
, boot_cpu
);
391 /* Sort by topology before we start anything */
392 if( !done_topo_sort
)
394 DBG( "about to start CPUs. %d registered\n", num_registered
);
396 cpu_topology_sort( num_registered
);
397 done_topo_sort
= TRUE
;
400 /* Assign the cpu ID */
401 uint32_t cpunum
= -1;
402 cpu_data_t
*this_cpu_datap
= NULL
;
404 /* find cpu num and pointer */
405 cpunum
= ml_get_cpuid( lapic_id
);
407 if( cpunum
== 0xFFFFFFFF ) /* never heard of it? */
408 panic( "trying to start invalid/unregistered CPU %d\n", lapic_id
);
410 this_cpu_datap
= cpu_datap(cpunum
);
413 this_cpu_datap
->cpu_id
= cpu_id
;
416 *processor_out
= this_cpu_datap
->cpu_processor
;
418 /* OK, try and start this CPU */
419 return cpu_topology_start_cpu( cpunum
);
424 ml_cpu_get_info(ml_cpu_info_t
*cpu_infop
)
426 boolean_t os_supports_sse
;
427 i386_cpu_info_t
*cpuid_infop
;
429 if (cpu_infop
== NULL
)
433 * Are we supporting MMX/SSE/SSE2/SSE3?
434 * As distinct from whether the cpu has these capabilities.
436 os_supports_sse
= !!(get_cr4() & CR4_OSXMM
);
438 if (ml_fpu_avx_enabled())
439 cpu_infop
->vector_unit
= 9;
440 else if ((cpuid_features() & CPUID_FEATURE_SSE4_2
) && os_supports_sse
)
441 cpu_infop
->vector_unit
= 8;
442 else if ((cpuid_features() & CPUID_FEATURE_SSE4_1
) && os_supports_sse
)
443 cpu_infop
->vector_unit
= 7;
444 else if ((cpuid_features() & CPUID_FEATURE_SSSE3
) && os_supports_sse
)
445 cpu_infop
->vector_unit
= 6;
446 else if ((cpuid_features() & CPUID_FEATURE_SSE3
) && os_supports_sse
)
447 cpu_infop
->vector_unit
= 5;
448 else if ((cpuid_features() & CPUID_FEATURE_SSE2
) && os_supports_sse
)
449 cpu_infop
->vector_unit
= 4;
450 else if ((cpuid_features() & CPUID_FEATURE_SSE
) && os_supports_sse
)
451 cpu_infop
->vector_unit
= 3;
452 else if (cpuid_features() & CPUID_FEATURE_MMX
)
453 cpu_infop
->vector_unit
= 2;
455 cpu_infop
->vector_unit
= 0;
457 cpuid_infop
= cpuid_info();
459 cpu_infop
->cache_line_size
= cpuid_infop
->cache_linesize
;
461 cpu_infop
->l1_icache_size
= cpuid_infop
->cache_size
[L1I
];
462 cpu_infop
->l1_dcache_size
= cpuid_infop
->cache_size
[L1D
];
464 if (cpuid_infop
->cache_size
[L2U
] > 0) {
465 cpu_infop
->l2_settings
= 1;
466 cpu_infop
->l2_cache_size
= cpuid_infop
->cache_size
[L2U
];
468 cpu_infop
->l2_settings
= 0;
469 cpu_infop
->l2_cache_size
= 0xFFFFFFFF;
472 if (cpuid_infop
->cache_size
[L3U
] > 0) {
473 cpu_infop
->l3_settings
= 1;
474 cpu_infop
->l3_cache_size
= cpuid_infop
->cache_size
[L3U
];
476 cpu_infop
->l3_settings
= 0;
477 cpu_infop
->l3_cache_size
= 0xFFFFFFFF;
482 ml_init_max_cpus(unsigned long max_cpus
)
484 boolean_t current_state
;
486 current_state
= ml_set_interrupts_enabled(FALSE
);
487 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
488 if (max_cpus
> 0 && max_cpus
<= MAX_CPUS
) {
490 * Note: max_cpus is the number of enabled processors
491 * that ACPI found; max_ncpus is the maximum number
492 * that the kernel supports or that the "cpus="
493 * boot-arg has set. Here we take int minimum.
495 machine_info
.max_cpus
= (integer_t
)MIN(max_cpus
, max_ncpus
);
497 if (max_cpus_initialized
== MAX_CPUS_WAIT
)
498 wakeup((event_t
)&max_cpus_initialized
);
499 max_cpus_initialized
= MAX_CPUS_SET
;
501 (void) ml_set_interrupts_enabled(current_state
);
505 ml_get_max_cpus(void)
507 boolean_t current_state
;
509 current_state
= ml_set_interrupts_enabled(FALSE
);
510 if (max_cpus_initialized
!= MAX_CPUS_SET
) {
511 max_cpus_initialized
= MAX_CPUS_WAIT
;
512 assert_wait((event_t
)&max_cpus_initialized
, THREAD_UNINT
);
513 (void)thread_block(THREAD_CONTINUE_NULL
);
515 (void) ml_set_interrupts_enabled(current_state
);
516 return(machine_info
.max_cpus
);
520 * Routine: ml_init_lock_timeout
524 ml_init_lock_timeout(void)
528 uint64_t default_timeout_ns
= NSEC_PER_SEC
>>2;
532 if (PE_parse_boot_argn("slto_us", &slto
, sizeof (slto
)))
533 default_timeout_ns
= slto
* NSEC_PER_USEC
;
535 /* LockTimeOut is absolutetime, LockTimeOutTSC is in TSC ticks */
536 nanoseconds_to_absolutetime(default_timeout_ns
, &abstime
);
537 LockTimeOut
= (uint32_t) abstime
;
538 LockTimeOutTSC
= (uint32_t) tmrCvt(abstime
, tscFCvtn2t
);
540 if (PE_parse_boot_argn("mtxspin", &mtxspin
, sizeof (mtxspin
))) {
541 if (mtxspin
> USEC_PER_SEC
>>4)
542 mtxspin
= USEC_PER_SEC
>>4;
543 nanoseconds_to_absolutetime(mtxspin
*NSEC_PER_USEC
, &abstime
);
545 nanoseconds_to_absolutetime(10*NSEC_PER_USEC
, &abstime
);
547 MutexSpin
= (unsigned int)abstime
;
549 nanoseconds_to_absolutetime(4ULL * NSEC_PER_SEC
, &LastDebuggerEntryAllowance
);
550 if (PE_parse_boot_argn("panic_restart_timeout", &prt
, sizeof (prt
)))
551 nanoseconds_to_absolutetime(prt
* NSEC_PER_SEC
, &panic_restart_timeout
);
552 virtualized
= ((cpuid_features() & CPUID_FEATURE_VMM
) != 0);
553 interrupt_latency_tracker_setup();
557 * Threshold above which we should attempt to block
558 * instead of spinning for clock_delay_until().
561 ml_init_delay_spin_threshold(void)
563 nanoseconds_to_absolutetime(10ULL * NSEC_PER_USEC
, &delay_spin_threshold
);
567 ml_delay_should_spin(uint64_t interval
)
569 return (interval
< delay_spin_threshold
) ? TRUE
: FALSE
;
573 * This is called from the machine-independent routine cpu_up()
574 * to perform machine-dependent info updates. Defer to cpu_thread_init().
583 * This is called from the machine-independent routine cpu_down()
584 * to perform machine-dependent info updates.
593 * The following are required for parts of the kernel
594 * that cannot resolve these functions as inlines:
596 extern thread_t
current_act(void);
600 return(current_thread_fast());
603 #undef current_thread
604 extern thread_t
current_thread(void);
608 return(current_thread_fast());
612 boolean_t
ml_is64bit(void) {
614 return (cpu_mode_is64bit());
618 boolean_t
ml_thread_is64bit(thread_t thread
) {
620 return (thread_is_64bit(thread
));
624 boolean_t
ml_state_is64bit(void *saved_state
) {
626 return is_saved_state64(saved_state
);
629 void ml_cpu_set_ldt(int selector
)
632 * Avoid loading the LDT
633 * if we're setting the KERNEL LDT and it's already set.
635 if (selector
== KERNEL_LDT
&&
636 current_cpu_datap()->cpu_ldt
== KERNEL_LDT
)
639 #if defined(__i386__)
641 * If 64bit this requires a mode switch (and back).
643 if (cpu_mode_is64bit())
644 ml_64bit_lldt(selector
);
650 current_cpu_datap()->cpu_ldt
= selector
;
653 void ml_fp_setvalid(boolean_t value
)
658 uint64_t ml_cpu_int_event_time(void)
660 return current_cpu_datap()->cpu_int_event_time
;
663 vm_offset_t
ml_stack_remaining(void)
665 uintptr_t local
= (uintptr_t) &local
;
667 if (ml_at_interrupt_context() != 0) {
668 return (local
- (current_cpu_datap()->cpu_int_stack_top
- INTSTACK_SIZE
));
670 return (local
- current_thread()->kernel_stack
);
675 kernel_preempt_check(void)
680 assert(get_preemption_level() == 0);
682 __asm__
volatile("pushf; pop %0" : "=r" (flags
));
684 intr
= ((flags
& EFL_IF
) != 0);
686 if ((*ast_pending() & AST_URGENT
) && intr
== TRUE
) {
688 * can handle interrupts and preemptions
693 * now cause the PRE-EMPTION trap
695 __asm__
volatile ("int %0" :: "N" (T_PREEMPT
));
699 boolean_t
machine_timeout_suspended(void) {
700 return (virtualized
|| pmap_tlb_flush_timeout
|| spinlock_timed_out
|| panic_active() || mp_recent_debugger_activity());