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