2 * Copyright (c) 2000-2009 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@
31 * cpu specific routines
34 #include <kern/misc_protos.h>
35 #include <kern/lock_group.h>
36 #include <kern/machine.h>
37 #include <mach/processor_info.h>
38 #include <i386/pmap.h>
39 #include <i386/machine_cpu.h>
40 #include <i386/machine_routines.h>
41 #include <i386/misc_protos.h>
42 #include <i386/cpu_threads.h>
43 #include <i386/rtclock_protos.h>
44 #include <i386/cpuid.h>
46 #include <i386/vmx/vmx_cpu.h>
48 #include <vm/vm_kern.h>
49 #include <kern/timer_call.h>
51 const char *processor_to_datastring(const char *prefix
, processor_t target_processor
);
53 struct processor processor_master
;
59 processor_info_t info
,
62 printf("cpu_control(%d,%p,%d) not implemented\n",
63 slot_num
, info
, count
);
70 __unused processor_flavor_t flavor
,
80 processor_flavor_t flavor
,
82 processor_info_t info
,
85 printf("cpu_info(%d,%d,%p,%p) not implemented\n",
86 flavor
, slot_num
, info
, count
);
93 cpu_data_t
*cdp
= current_cpu_datap();
95 /* This calls IOCPURunPlatformQuiesceActions when sleeping the boot cpu */
96 PE_cpu_machine_quiesce(cdp
->cpu_id
);
104 cpu_data_t
*cdp
= current_cpu_datap();
106 timer_call_queue_init(&cdp
->rtclock_timer
.queue
);
107 cdp
->rtclock_timer
.deadline
= EndOfAllTime
;
109 cdp
->cpu_type
= cpuid_cputype();
110 cdp
->cpu_subtype
= cpuid_cpusubtype();
121 if (cpu
== cpu_number()) {
127 * Try to bring the CPU back online without a reset.
128 * If the fast restart doesn't succeed, fall back to
131 ret
= intel_startCPU_fast(cpu
);
132 if (ret
!= KERN_SUCCESS
) {
134 * Should call out through PE.
135 * But take the shortcut here.
137 ret
= intel_startCPU(cpu
);
140 if (ret
!= KERN_SUCCESS
) {
141 kprintf("cpu: cpu_start(%d) returning failure!\n", cpu
);
151 cpu_data_t
*cdp
= cpu_datap(cpu
);
152 boolean_t intrs_enabled
;
153 uint64_t tsc_timeout
;
156 * Wait until the CPU indicates that it has stopped.
157 * Disable interrupts while the topo lock is held -- arguably
158 * this should always be done but in this instance it can lead to
159 * a timeout if long-running interrupt were to occur here.
161 intrs_enabled
= ml_set_interrupts_enabled(FALSE
);
162 mp_safe_spin_lock(&x86_topo_lock
);
163 /* Set a generous timeout of several seconds (in TSC ticks) */
164 tsc_timeout
= rdtsc64() + (10ULL * 1000 * 1000 * 1000);
165 while ((cdp
->lcpu
.state
!= LCPU_HALT
)
166 && (cdp
->lcpu
.state
!= LCPU_OFF
)
167 && !cdp
->lcpu
.stopped
) {
168 simple_unlock(&x86_topo_lock
);
169 ml_set_interrupts_enabled(intrs_enabled
);
171 if (rdtsc64() > tsc_timeout
) {
172 panic("cpu_exit_wait(%d) timeout", cpu
);
174 ml_set_interrupts_enabled(FALSE
);
175 mp_safe_spin_lock(&x86_topo_lock
);
177 simple_unlock(&x86_topo_lock
);
178 ml_set_interrupts_enabled(intrs_enabled
);
185 cpu_data_t
*cdp
= current_cpu_datap();
187 PE_cpu_machine_init(cdp
->cpu_id
, !cdp
->cpu_boot_complete
);
188 cdp
->cpu_boot_complete
= TRUE
;
189 cdp
->cpu_running
= TRUE
;
193 /* initialize VMX for every CPU */
199 current_processor(void)
201 return current_cpu_datap()->cpu_processor
;
208 return cpu_datap(cpu
)->cpu_processor
;
214 return ¤t_cpu_datap()->cpu_pending_ast
;
221 return cpu_datap(slot_num
)->cpu_type
;
228 return cpu_datap(slot_num
)->cpu_subtype
;
235 return cpu_datap(slot_num
)->cpu_threadtype
;
241 return current_cpu_datap()->cpu_type
;
247 return current_cpu_datap()->cpu_subtype
;
253 return current_cpu_datap()->cpu_threadtype
;
257 processor_to_datastring(const char *prefix
, processor_t target_processor
)
259 static char printBuf
[256];
260 uint32_t cpu_num
= target_processor
->cpu_id
;
262 cpu_data_t
*cpup
= cpu_datap(cpu_num
);
265 act
= ml_validate_nofault((vm_offset_t
)cpup
->cpu_active_thread
,
266 sizeof(struct thread
)) ? cpup
->cpu_active_thread
: NULL
;
268 snprintf(printBuf
, sizeof(printBuf
),
269 "%s: tCPU %u (%d) [tid=0x%llx(bp=%d sp=%d) s=0x%x ps=0x%x cpa=0x%x spa=0x%llx pl=%d il=%d r=%d]",
272 target_processor
->state
,
273 act
? act
->thread_id
: ~0ULL,
274 act
? act
->base_pri
: -1,
275 act
? act
->sched_pri
: -1,
277 cpup
->cpu_prior_signals
,
278 cpup
->cpu_pending_ast
,
279 target_processor
->processor_set
->pending_AST_URGENT_cpu_mask
,
280 cpup
->cpu_preemption_level
,
281 cpup
->cpu_interrupt_level
,
284 return (const char *)&printBuf
[0];