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