1 /*
2 * Copyright (c) 2000-2009 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 * File: i386/cpu.c
30 *
31 * cpu specific routines
32 */
33
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>
45 #include <i386/lbr.h>
46 #include <kern/debug.h>
47 #if CONFIG_VMX
48 #include <i386/vmx/vmx_cpu.h>
49 #endif
50 #include <vm/vm_kern.h>
51 #include <kern/timer_call.h>
52
53 const char *processor_to_datastring(const char *prefix, processor_t target_processor);
54
55 struct processor processor_master;
56
57 /*ARGSUSED*/
58 kern_return_t
cpu_control(int slot_num,processor_info_t info,unsigned int count)59 cpu_control(
60 int slot_num,
61 processor_info_t info,
62 unsigned int count)
63 {
64 printf("cpu_control(%d,%p,%d) not implemented\n",
65 slot_num, info, count);
66 return KERN_FAILURE;
67 }
68
69 /*ARGSUSED*/
70 kern_return_t
cpu_info_count(__unused processor_flavor_t flavor,unsigned int * count)71 cpu_info_count(
72 __unused processor_flavor_t flavor,
73 unsigned int *count)
74 {
75 *count = 0;
76 return KERN_FAILURE;
77 }
78
79 /*ARGSUSED*/
80 kern_return_t
cpu_info(processor_flavor_t flavor,int slot_num,processor_info_t info,unsigned int * count)81 cpu_info(
82 processor_flavor_t flavor,
83 int slot_num,
84 processor_info_t info,
85 unsigned int *count)
86 {
87 printf("cpu_info(%d,%d,%p,%p) not implemented\n",
88 flavor, slot_num, info, count);
89 return KERN_FAILURE;
90 }
91
92 void
cpu_sleep(void)93 cpu_sleep(void)
94 {
95 cpu_data_t *cdp = current_cpu_datap();
96
97 /* This calls IOCPURunPlatformQuiesceActions when sleeping the boot cpu */
98 PE_cpu_machine_quiesce(cdp->cpu_id);
99
100 cpu_thread_halt();
101 }
102
103 void
cpu_init(void)104 cpu_init(void)
105 {
106 cpu_data_t *cdp = current_cpu_datap();
107
108 timer_call_queue_init(&cdp->rtclock_timer.queue);
109 cdp->rtclock_timer.deadline = EndOfAllTime;
110
111 cdp->cpu_type = cpuid_cputype();
112 cdp->cpu_subtype = cpuid_cpusubtype();
113
114 i386_activate_cpu();
115 }
116
117 kern_return_t
cpu_start(int cpu)118 cpu_start(
119 int cpu)
120 {
121 kern_return_t ret;
122
123 if (cpu == cpu_number()) {
124 cpu_machine_init();
125 return KERN_SUCCESS;
126 }
127
128 /*
129 * Try to bring the CPU back online without a reset.
130 * If the fast restart doesn't succeed, fall back to
131 * the slow way.
132 */
133 ret = intel_startCPU_fast(cpu);
134 if (ret != KERN_SUCCESS) {
135 /*
136 * Should call out through PE.
137 * But take the shortcut here.
138 */
139 ret = intel_startCPU(cpu);
140 }
141
142 if (ret != KERN_SUCCESS) {
143 kprintf("cpu: cpu_start(%d) returning failure!\n", cpu);
144 }
145
146 return ret;
147 }
148
149 void
cpu_exit_wait(int cpu)150 cpu_exit_wait(
151 int cpu)
152 {
153 cpu_data_t *cdp = cpu_datap(cpu);
154 boolean_t intrs_enabled;
155 uint64_t tsc_timeout;
156
157 /*
158 * Wait until the CPU indicates that it has stopped.
159 * Disable interrupts while the topo lock is held -- arguably
160 * this should always be done but in this instance it can lead to
161 * a timeout if long-running interrupt were to occur here.
162 */
163 intrs_enabled = ml_set_interrupts_enabled(FALSE);
164 mp_safe_spin_lock(&x86_topo_lock);
165 /* Set a generous timeout of several seconds (in TSC ticks) */
166 tsc_timeout = rdtsc64() + (10ULL * 1000 * 1000 * 1000);
167 while ((cdp->lcpu.state != LCPU_HALT)
168 && (cdp->lcpu.state != LCPU_OFF)
169 && !cdp->lcpu.stopped) {
170 simple_unlock(&x86_topo_lock);
171 ml_set_interrupts_enabled(intrs_enabled);
172 cpu_pause();
173 if (rdtsc64() > tsc_timeout) {
174 panic("cpu_exit_wait(%d) timeout", cpu);
175 }
176 ml_set_interrupts_enabled(FALSE);
177 mp_safe_spin_lock(&x86_topo_lock);
178 }
179 simple_unlock(&x86_topo_lock);
180 ml_set_interrupts_enabled(intrs_enabled);
181 }
182
183 void
cpu_machine_init(void)184 cpu_machine_init(
185 void)
186 {
187 cpu_data_t *cdp = current_cpu_datap();
188
189 PE_cpu_machine_init(cdp->cpu_id, !cdp->cpu_boot_complete);
190 cdp->cpu_boot_complete = TRUE;
191 cdp->cpu_running = TRUE;
192 ml_init_interrupt();
193
194 #if CONFIG_VMX
195 /* initialize VMX for every CPU */
196 vmx_cpu_init();
197 #endif
198 }
199
200 processor_t
current_processor(void)201 current_processor(void)
202 {
203 return current_cpu_datap()->cpu_processor;
204 }
205
206 processor_t
cpu_to_processor(int cpu)207 cpu_to_processor(
208 int cpu)
209 {
210 return cpu_datap(cpu)->cpu_processor;
211 }
212
213 ast_t *
ast_pending(void)214 ast_pending(void)
215 {
216 return ¤t_cpu_datap()->cpu_pending_ast;
217 }
218
219 cpu_type_t
slot_type(int slot_num)220 slot_type(
221 int slot_num)
222 {
223 return cpu_datap(slot_num)->cpu_type;
224 }
225
226 cpu_subtype_t
slot_subtype(int slot_num)227 slot_subtype(
228 int slot_num)
229 {
230 return cpu_datap(slot_num)->cpu_subtype;
231 }
232
233 cpu_threadtype_t
slot_threadtype(int slot_num)234 slot_threadtype(
235 int slot_num)
236 {
237 return cpu_datap(slot_num)->cpu_threadtype;
238 }
239
240 cpu_type_t
cpu_type(void)241 cpu_type(void)
242 {
243 return current_cpu_datap()->cpu_type;
244 }
245
246 cpu_subtype_t
cpu_subtype(void)247 cpu_subtype(void)
248 {
249 return current_cpu_datap()->cpu_subtype;
250 }
251
252 cpu_threadtype_t
cpu_threadtype(void)253 cpu_threadtype(void)
254 {
255 return current_cpu_datap()->cpu_threadtype;
256 }
257
258 const char *
processor_to_datastring(const char * prefix,processor_t target_processor)259 processor_to_datastring(const char *prefix, processor_t target_processor)
260 {
261 static char printBuf[256];
262 uint32_t cpu_num = target_processor->cpu_id;
263
264 cpu_data_t *cpup = cpu_datap(cpu_num);
265 thread_t act;
266
267 act = ml_validate_nofault((vm_offset_t)cpup->cpu_active_thread,
268 sizeof(struct thread)) ? cpup->cpu_active_thread : NULL;
269
270 snprintf(printBuf, sizeof(printBuf),
271 "%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 prefix,
273 cpu_num,
274 target_processor->state,
275 act ? act->thread_id : ~0ULL,
276 act ? act->base_pri : -1,
277 act ? act->sched_pri : -1,
278 cpup->cpu_signals,
279 cpup->cpu_prior_signals,
280 cpup->cpu_pending_ast,
281 target_processor->processor_set->pending_AST_URGENT_cpu_mask,
282 cpup->cpu_preemption_level,
283 cpup->cpu_interrupt_level,
284 cpup->cpu_running);
285
286 return (const char *)&printBuf[0];
287 }
288