]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/machine.c
043a29508318b33a2f92ec68e4490e7fe1ce71c9
[apple/xnu.git] / osfmk / kern / machine.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, 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 * @OSF_COPYRIGHT@
30 */
31 /*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56 /*
57 */
58 /*
59 * File: kern/machine.c
60 * Author: Avadis Tevanian, Jr.
61 * Date: 1987
62 *
63 * Support for machine independent machine abstraction.
64 */
65
66 #include <string.h>
67
68 #include <mach/mach_types.h>
69 #include <mach/boolean.h>
70 #include <mach/kern_return.h>
71 #include <mach/machine.h>
72 #include <mach/host_info.h>
73 #include <mach/host_reboot.h>
74 #include <mach/host_priv_server.h>
75 #include <mach/processor_server.h>
76
77 #include <kern/kern_types.h>
78 #include <kern/counters.h>
79 #include <kern/cpu_data.h>
80 #include <kern/ipc_host.h>
81 #include <kern/host.h>
82 #include <kern/lock.h>
83 #include <kern/machine.h>
84 #include <kern/misc_protos.h>
85 #include <kern/processor.h>
86 #include <kern/queue.h>
87 #include <kern/sched.h>
88 #include <kern/task.h>
89 #include <kern/thread.h>
90
91 #include <IOKit/IOHibernatePrivate.h>
92
93 /*
94 * Exported variables:
95 */
96
97 struct machine_info machine_info;
98
99 /* Forwards */
100 void processor_doshutdown(
101 processor_t processor);
102
103 /*
104 * processor_up:
105 *
106 * Flag processor as up and running, and available
107 * for scheduling.
108 */
109 void
110 processor_up(
111 processor_t processor)
112 {
113 processor_set_t pset = &default_pset;
114 spl_t s;
115
116 s = splsched();
117 processor_lock(processor);
118 init_ast_check(processor);
119 simple_lock(&pset->sched_lock);
120 pset_add_processor(pset, processor);
121 enqueue_tail(&pset->active_queue, (queue_entry_t)processor);
122 processor->state = PROCESSOR_RUNNING;
123 simple_unlock(&pset->sched_lock);
124 hw_atomic_add(&machine_info.avail_cpus, 1);
125 ml_cpu_up();
126 processor_unlock(processor);
127 splx(s);
128 }
129
130 kern_return_t
131 host_reboot(
132 host_priv_t host_priv,
133 int options)
134 {
135 if (host_priv == HOST_PRIV_NULL)
136 return (KERN_INVALID_HOST);
137
138 assert(host_priv == &realhost);
139
140 if (options & HOST_REBOOT_DEBUGGER) {
141 Debugger("Debugger");
142 return (KERN_SUCCESS);
143 }
144
145 halt_all_cpus(!(options & HOST_REBOOT_HALT));
146
147 return (KERN_SUCCESS);
148 }
149
150 kern_return_t
151 processor_assign(
152 __unused processor_t processor,
153 __unused processor_set_t new_pset,
154 __unused boolean_t wait)
155 {
156 return (KERN_FAILURE);
157 }
158
159 kern_return_t
160 processor_shutdown(
161 processor_t processor)
162 {
163 processor_set_t pset;
164 spl_t s;
165
166 s = splsched();
167 processor_lock(processor);
168 if (processor->state == PROCESSOR_OFF_LINE) {
169 /*
170 * Success if already shutdown.
171 */
172 processor_unlock(processor);
173 splx(s);
174
175 return (KERN_SUCCESS);
176 }
177
178 if (processor->state == PROCESSOR_START) {
179 /*
180 * Failure if currently being started.
181 */
182 processor_unlock(processor);
183 splx(s);
184
185 return (KERN_FAILURE);
186 }
187
188 /*
189 * Must lock the scheduling lock
190 * to get at the processor state.
191 */
192 pset = processor->processor_set;
193 if (pset != PROCESSOR_SET_NULL) {
194 simple_lock(&pset->sched_lock);
195
196 /*
197 * If the processor is dispatching, let it finish.
198 */
199 while (processor->state == PROCESSOR_DISPATCHING) {
200 simple_unlock(&pset->sched_lock);
201 delay(1);
202 simple_lock(&pset->sched_lock);
203 }
204
205 /*
206 * Success if already being shutdown.
207 */
208 if (processor->state == PROCESSOR_SHUTDOWN) {
209 simple_unlock(&pset->sched_lock);
210 processor_unlock(processor);
211 splx(s);
212
213 return (KERN_SUCCESS);
214 }
215 }
216 else {
217 /*
218 * Success, already being shutdown.
219 */
220 processor_unlock(processor);
221 splx(s);
222
223 return (KERN_SUCCESS);
224 }
225
226 if (processor->state == PROCESSOR_IDLE) {
227 remqueue(&pset->idle_queue, (queue_entry_t)processor);
228 pset->idle_count--;
229 }
230 else
231 if (processor->state == PROCESSOR_RUNNING)
232 remqueue(&pset->active_queue, (queue_entry_t)processor);
233 else
234 panic("processor_shutdown");
235
236 processor->state = PROCESSOR_SHUTDOWN;
237
238 simple_unlock(&pset->sched_lock);
239
240 processor_unlock(processor);
241
242 processor_doshutdown(processor);
243 splx(s);
244
245 cpu_exit_wait(PROCESSOR_DATA(processor, slot_num));
246
247 return (KERN_SUCCESS);
248 }
249
250 /*
251 * Called at splsched.
252 */
253 void
254 processor_doshutdown(
255 processor_t processor)
256 {
257 thread_t old_thread, self = current_thread();
258 processor_set_t pset;
259 processor_t prev;
260 int pcount;
261
262 /*
263 * Get onto the processor to shutdown
264 */
265 prev = thread_bind(self, processor);
266 thread_block(THREAD_CONTINUE_NULL);
267
268 processor_lock(processor);
269 pset = processor->processor_set;
270 simple_lock(&pset->sched_lock);
271
272 if ((pcount = pset->processor_count) == 1) {
273 simple_unlock(&pset->sched_lock);
274 processor_unlock(processor);
275
276 hibernate_vm_lock();
277
278 processor_lock(processor);
279 simple_lock(&pset->sched_lock);
280 }
281
282 assert(processor->state == PROCESSOR_SHUTDOWN);
283
284 pset_remove_processor(pset, processor);
285 simple_unlock(&pset->sched_lock);
286 processor_unlock(processor);
287
288 if (pcount == 1)
289 hibernate_vm_unlock();
290
291 /*
292 * Continue processor shutdown in shutdown context.
293 */
294 thread_bind(self, prev);
295 old_thread = machine_processor_shutdown(self, processor_offline, processor);
296
297 thread_begin(self, self->last_processor);
298
299 thread_dispatch(old_thread);
300
301 /*
302 * If we just shutdown another processor, move the
303 * timer call outs to the current processor.
304 */
305 if (processor != current_processor()) {
306 processor_lock(processor);
307 if ( processor->state == PROCESSOR_OFF_LINE ||
308 processor->state == PROCESSOR_SHUTDOWN )
309 timer_call_shutdown(processor);
310 processor_unlock(processor);
311 }
312 }
313
314 /*
315 * Complete the shutdown and place the processor offline.
316 *
317 * Called at splsched in the shutdown context.
318 */
319 void
320 processor_offline(
321 processor_t processor)
322 {
323 thread_t thread, old_thread = processor->active_thread;
324
325 thread = processor->idle_thread;
326 processor->active_thread = thread;
327 processor->current_pri = IDLEPRI;
328
329 processor->last_dispatch = mach_absolute_time();
330 timer_switch((uint32_t)processor->last_dispatch,
331 &PROCESSOR_DATA(processor, offline_timer));
332
333 thread_done(old_thread, thread, processor);
334
335 machine_set_current_thread(thread);
336
337 thread_begin(thread, processor);
338
339 thread_dispatch(old_thread);
340
341 PMAP_DEACTIVATE_KERNEL(PROCESSOR_DATA(processor, slot_num));
342
343 processor_lock(processor);
344 processor->state = PROCESSOR_OFF_LINE;
345 hw_atomic_sub(&machine_info.avail_cpus, 1);
346 ml_cpu_down();
347 processor_unlock(processor);
348
349 cpu_sleep();
350 panic("zombie processor");
351 /*NOTREACHED*/
352 }
353
354 kern_return_t
355 host_get_boot_info(
356 host_priv_t host_priv,
357 kernel_boot_info_t boot_info)
358 {
359 const char *src = "";
360 if (host_priv == HOST_PRIV_NULL)
361 return (KERN_INVALID_HOST);
362
363 assert(host_priv == &realhost);
364
365 /*
366 * Copy first operator string terminated by '\0' followed by
367 * standardized strings generated from boot string.
368 */
369 src = machine_boot_info(boot_info, KERNEL_BOOT_INFO_MAX);
370 if (src != boot_info)
371 (void) strncpy(boot_info, src, KERNEL_BOOT_INFO_MAX);
372
373 return (KERN_SUCCESS);
374 }