]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/cpu.c
xnu-1699.24.23.tar.gz
[apple/xnu.git] / osfmk / i386 / cpu.c
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/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 #if CONFIG_VMX
46 #include <i386/vmx/vmx_cpu.h>
47 #endif
48 #include <vm/vm_kern.h>
49 #include <kern/etimer.h>
50 #include <kern/timer_call.h>
51
52 struct processor processor_master;
53
54 /*ARGSUSED*/
55 kern_return_t
56 cpu_control(
57 int slot_num,
58 processor_info_t info,
59 unsigned int count)
60 {
61 printf("cpu_control(%d,%p,%d) not implemented\n",
62 slot_num, info, count);
63 return (KERN_FAILURE);
64 }
65
66 /*ARGSUSED*/
67 kern_return_t
68 cpu_info_count(
69 __unused processor_flavor_t flavor,
70 unsigned int *count)
71 {
72 *count = 0;
73 return (KERN_FAILURE);
74 }
75
76 /*ARGSUSED*/
77 kern_return_t
78 cpu_info(
79 processor_flavor_t flavor,
80 int slot_num,
81 processor_info_t info,
82 unsigned int *count)
83 {
84 printf("cpu_info(%d,%d,%p,%p) not implemented\n",
85 flavor, slot_num, info, count);
86 return (KERN_FAILURE);
87 }
88
89 void
90 cpu_sleep(void)
91 {
92 cpu_data_t *cdp = current_cpu_datap();
93
94 i386_deactivate_cpu();
95
96 PE_cpu_machine_quiesce(cdp->cpu_id);
97
98 cpu_thread_halt();
99 }
100
101 void
102 cpu_init(void)
103 {
104 cpu_data_t *cdp = current_cpu_datap();
105
106 timer_call_initialize_queue(&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
115 kern_return_t
116 cpu_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 return(ret);
144 }
145
146 void
147 cpu_exit_wait(
148 int cpu)
149 {
150 cpu_data_t *cdp = cpu_datap(cpu);
151
152 /*
153 * Wait until the CPU indicates that it has stopped.
154 */
155 simple_lock(&x86_topo_lock);
156 while ((cdp->lcpu.state != LCPU_HALT)
157 && (cdp->lcpu.state != LCPU_OFF)
158 && !cdp->lcpu.stopped) {
159 simple_unlock(&x86_topo_lock);
160 cpu_pause();
161 simple_lock(&x86_topo_lock);
162 }
163 simple_unlock(&x86_topo_lock);
164 }
165
166 void
167 cpu_machine_init(
168 void)
169 {
170 cpu_data_t *cdp = current_cpu_datap();
171
172 PE_cpu_machine_init(cdp->cpu_id, !cdp->cpu_boot_complete);
173 cdp->cpu_boot_complete = TRUE;
174 cdp->cpu_running = TRUE;
175 ml_init_interrupt();
176
177 #if CONFIG_VMX
178 /* for every CPU, get the VT specs */
179 vmx_get_specs();
180 #endif
181 }
182
183 processor_t
184 cpu_processor_alloc(boolean_t is_boot_cpu)
185 {
186 int ret;
187 processor_t proc;
188
189 if (is_boot_cpu)
190 return &processor_master;
191
192 ret = kmem_alloc(kernel_map, (vm_offset_t *) &proc, sizeof(*proc));
193 if (ret != KERN_SUCCESS)
194 return NULL;
195
196 bzero((void *) proc, sizeof(*proc));
197 return proc;
198 }
199
200 void
201 cpu_processor_free(processor_t proc)
202 {
203 if (proc != NULL && proc != &processor_master)
204 kfree((void *) proc, sizeof(*proc));
205 }
206
207 processor_t
208 current_processor(void)
209 {
210 return current_cpu_datap()->cpu_processor;
211 }
212
213 processor_t
214 cpu_to_processor(
215 int cpu)
216 {
217 return cpu_datap(cpu)->cpu_processor;
218 }
219
220 ast_t *
221 ast_pending(void)
222 {
223 return (&current_cpu_datap()->cpu_pending_ast);
224 }
225
226 cpu_type_t
227 slot_type(
228 int slot_num)
229 {
230 return (cpu_datap(slot_num)->cpu_type);
231 }
232
233 cpu_subtype_t
234 slot_subtype(
235 int slot_num)
236 {
237 return (cpu_datap(slot_num)->cpu_subtype);
238 }
239
240 cpu_threadtype_t
241 slot_threadtype(
242 int slot_num)
243 {
244 return (cpu_datap(slot_num)->cpu_threadtype);
245 }
246
247 cpu_type_t
248 cpu_type(void)
249 {
250 return (current_cpu_datap()->cpu_type);
251 }
252
253 cpu_subtype_t
254 cpu_subtype(void)
255 {
256 return (current_cpu_datap()->cpu_subtype);
257 }
258
259 cpu_threadtype_t
260 cpu_threadtype(void)
261 {
262 return (current_cpu_datap()->cpu_threadtype);
263 }