]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/machine.c
8a331ca87380953c2edd5af1031c347fc7e44f85
[apple/xnu.git] / osfmk / kern / machine.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
28 * All Rights Reserved.
29 *
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
35 *
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52 /*
53 * File: kern/machine.c
54 * Author: Avadis Tevanian, Jr.
55 * Date: 1987
56 *
57 * Support for machine independent machine abstraction.
58 */
59
60 #include <string.h>
61
62 #include <mach/mach_types.h>
63 #include <mach/boolean.h>
64 #include <mach/kern_return.h>
65 #include <mach/machine.h>
66 #include <mach/host_info.h>
67 #include <mach/host_reboot.h>
68 #include <mach/host_priv_server.h>
69 #include <mach/processor_server.h>
70
71 #include <kern/kern_types.h>
72 #include <kern/counters.h>
73 #include <kern/cpu_data.h>
74 #include <kern/ipc_host.h>
75 #include <kern/host.h>
76 #include <kern/lock.h>
77 #include <kern/machine.h>
78 #include <kern/misc_protos.h>
79 #include <kern/processor.h>
80 #include <kern/queue.h>
81 #include <kern/sched.h>
82 #include <kern/task.h>
83 #include <kern/thread.h>
84
85 #include <IOKit/IOHibernatePrivate.h>
86 #include <IOKit/IOPlatformExpert.h>
87
88 /*
89 * Exported variables:
90 */
91
92 struct machine_info machine_info;
93
94 /* Forwards */
95 void processor_doshutdown(
96 processor_t processor);
97
98 /*
99 * processor_up:
100 *
101 * Flag processor as up and running, and available
102 * for scheduling.
103 */
104 void
105 processor_up(
106 processor_t processor)
107 {
108 processor_set_t pset = &default_pset;
109 spl_t s;
110
111 s = splsched();
112 processor_lock(processor);
113 init_ast_check(processor);
114 simple_lock(&pset->sched_lock);
115 pset_add_processor(pset, processor);
116 enqueue_tail(&pset->active_queue, (queue_entry_t)processor);
117 processor->state = PROCESSOR_RUNNING;
118 simple_unlock(&pset->sched_lock);
119 hw_atomic_add(&machine_info.avail_cpus, 1);
120 ml_cpu_up();
121 processor_unlock(processor);
122 splx(s);
123 }
124
125 kern_return_t
126 host_reboot(
127 host_priv_t host_priv,
128 int options)
129 {
130 if (host_priv == HOST_PRIV_NULL)
131 return (KERN_INVALID_HOST);
132
133 assert(host_priv == &realhost);
134
135 if (options & HOST_REBOOT_DEBUGGER) {
136 Debugger("Debugger");
137 return (KERN_SUCCESS);
138 }
139
140 if (options & HOST_REBOOT_UPSDELAY) {
141 // UPS power cutoff path
142 PEHaltRestart( kPEUPSDelayHaltCPU );
143 } else {
144 halt_all_cpus(!(options & HOST_REBOOT_HALT));
145 }
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 }