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