]> git.saurik.com Git - apple/xnu.git/blame - osfmk/i386/cpu.c
xnu-2422.115.4.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@
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
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>
91447636 36#include <kern/machine.h>
1c79356b 37#include <mach/processor_info.h>
b0d623f7 38#include <i386/pmap.h>
55e303ae
A
39#include <i386/machine_cpu.h>
40#include <i386/machine_routines.h>
91447636
A
41#include <i386/misc_protos.h>
42#include <i386/cpu_threads.h>
6d2010ae 43#include <i386/rtclock_protos.h>
b0d623f7
A
44#include <i386/cpuid.h>
45#if CONFIG_VMX
46#include <i386/vmx/vmx_cpu.h>
47#endif
91447636 48#include <vm/vm_kern.h>
6d2010ae 49#include <kern/timer_call.h>
1c79356b 50
91447636 51struct processor processor_master;
9bccf70c 52
1c79356b
A
53/*ARGSUSED*/
54kern_return_t
55cpu_control(
56 int slot_num,
57 processor_info_t info,
58 unsigned int count)
59{
2d21ac55 60 printf("cpu_control(%d,%p,%d) not implemented\n",
91447636 61 slot_num, info, count);
1c79356b
A
62 return (KERN_FAILURE);
63}
64
65/*ARGSUSED*/
66kern_return_t
67cpu_info_count(
91447636
A
68 __unused processor_flavor_t flavor,
69 unsigned int *count)
1c79356b
A
70{
71 *count = 0;
72 return (KERN_FAILURE);
73}
74
75/*ARGSUSED*/
76kern_return_t
77cpu_info(
78 processor_flavor_t flavor,
79 int slot_num,
80 processor_info_t info,
81 unsigned int *count)
82{
2d21ac55 83 printf("cpu_info(%d,%d,%p,%p) not implemented\n",
91447636 84 flavor, slot_num, info, count);
1c79356b
A
85 return (KERN_FAILURE);
86}
87
88void
91447636 89cpu_sleep(void)
1c79356b 90{
2d21ac55 91 cpu_data_t *cdp = current_cpu_datap();
55e303ae 92
2d21ac55 93 PE_cpu_machine_quiesce(cdp->cpu_id);
55e303ae 94
91447636 95 cpu_thread_halt();
55e303ae
A
96}
97
91447636
A
98void
99cpu_init(void)
55e303ae 100{
91447636 101 cpu_data_t *cdp = current_cpu_datap();
55e303ae 102
39236c6e 103 timer_call_queue_init(&cdp->rtclock_timer.queue);
6d2010ae
A
104 cdp->rtclock_timer.deadline = EndOfAllTime;
105
0c530ab8
A
106 cdp->cpu_type = cpuid_cputype();
107 cdp->cpu_subtype = cpuid_cpusubtype();
108
2d21ac55 109 i386_activate_cpu();
55e303ae
A
110}
111
112kern_return_t
113cpu_start(
114 int cpu)
115{
116 kern_return_t ret;
117
118 if (cpu == cpu_number()) {
91447636 119 cpu_machine_init();
55e303ae 120 return KERN_SUCCESS;
593a1d5f
A
121 }
122
123 /*
124 * Try to bring the CPU back online without a reset.
125 * If the fast restart doesn't succeed, fall back to
126 * the slow way.
127 */
128 ret = intel_startCPU_fast(cpu);
129 if (ret != KERN_SUCCESS) {
55e303ae
A
130 /*
131 * Should call out through PE.
132 * But take the shortcut here.
133 */
134 ret = intel_startCPU(cpu);
55e303ae 135 }
593a1d5f
A
136
137 if (ret != KERN_SUCCESS)
138 kprintf("cpu: cpu_start(%d) returning failure!\n", cpu);
139
140 return(ret);
55e303ae
A
141}
142
91447636
A
143void
144cpu_exit_wait(
2d21ac55 145 int cpu)
91447636 146{
2d21ac55 147 cpu_data_t *cdp = cpu_datap(cpu);
39236c6e
A
148 boolean_t intrs_enabled;
149 uint64_t tsc_timeout;
2d21ac55 150
e2fac8b1
A
151 /*
152 * Wait until the CPU indicates that it has stopped.
39236c6e
A
153 * Disable interrupts while the topo lock is held -- arguably
154 * this should always be done but in this instance it can lead to
155 * a timeout if long-running interrupt were to occur here.
e2fac8b1 156 */
39236c6e 157 intrs_enabled = ml_set_interrupts_enabled(FALSE);
2d21ac55 158 simple_lock(&x86_topo_lock);
39236c6e
A
159 /* Set a generous timeout of several seconds (in TSC ticks) */
160 tsc_timeout = rdtsc64() + (10ULL * 1000 * 1000 * 1000);
593a1d5f 161 while ((cdp->lcpu.state != LCPU_HALT)
e2fac8b1
A
162 && (cdp->lcpu.state != LCPU_OFF)
163 && !cdp->lcpu.stopped) {
2d21ac55 164 simple_unlock(&x86_topo_lock);
39236c6e 165 ml_set_interrupts_enabled(intrs_enabled);
2d21ac55 166 cpu_pause();
39236c6e
A
167 if (rdtsc64() > tsc_timeout)
168 panic("cpu_exit_wait(%d) timeout", cpu);
169 ml_set_interrupts_enabled(FALSE);
2d21ac55
A
170 simple_lock(&x86_topo_lock);
171 }
172 simple_unlock(&x86_topo_lock);
39236c6e 173 ml_set_interrupts_enabled(intrs_enabled);
91447636
A
174}
175
55e303ae
A
176void
177cpu_machine_init(
178 void)
179{
0c530ab8 180 cpu_data_t *cdp = current_cpu_datap();
55e303ae 181
0c530ab8
A
182 PE_cpu_machine_init(cdp->cpu_id, !cdp->cpu_boot_complete);
183 cdp->cpu_boot_complete = TRUE;
184 cdp->cpu_running = TRUE;
55e303ae 185 ml_init_interrupt();
2d21ac55 186
b0d623f7 187#if CONFIG_VMX
2d21ac55
A
188 /* for every CPU, get the VT specs */
189 vmx_get_specs();
b0d623f7 190#endif
55e303ae
A
191}
192
91447636
A
193processor_t
194cpu_processor_alloc(boolean_t is_boot_cpu)
195{
196 int ret;
197 processor_t proc;
198
199 if (is_boot_cpu)
200 return &processor_master;
201
202 ret = kmem_alloc(kernel_map, (vm_offset_t *) &proc, sizeof(*proc));
203 if (ret != KERN_SUCCESS)
204 return NULL;
205
206 bzero((void *) proc, sizeof(*proc));
207 return proc;
208}
209
210void
211cpu_processor_free(processor_t proc)
212{
213 if (proc != NULL && proc != &processor_master)
214 kfree((void *) proc, sizeof(*proc));
215}
216
217processor_t
218current_processor(void)
219{
220 return current_cpu_datap()->cpu_processor;
221}
222
223processor_t
224cpu_to_processor(
225 int cpu)
226{
227 return cpu_datap(cpu)->cpu_processor;
228}
229
230ast_t *
231ast_pending(void)
232{
233 return (&current_cpu_datap()->cpu_pending_ast);
234}
235
236cpu_type_t
237slot_type(
238 int slot_num)
239{
240 return (cpu_datap(slot_num)->cpu_type);
241}
242
243cpu_subtype_t
244slot_subtype(
245 int slot_num)
246{
247 return (cpu_datap(slot_num)->cpu_subtype);
248}
249
250cpu_threadtype_t
251slot_threadtype(
252 int slot_num)
253{
254 return (cpu_datap(slot_num)->cpu_threadtype);
255}
256
257cpu_type_t
258cpu_type(void)
259{
260 return (current_cpu_datap()->cpu_type);
261}
262
263cpu_subtype_t
264cpu_subtype(void)
265{
266 return (current_cpu_datap()->cpu_subtype);
267}
268
269cpu_threadtype_t
270cpu_threadtype(void)
271{
272 return (current_cpu_datap()->cpu_threadtype);
273}