2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
29 * All Rights Reserved.
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
41 * Carnegie Mellon requests users of this software to return to
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
54 * File: kern/machine.c
55 * Author: Avadis Tevanian, Jr.
58 * Support for machine independent machine abstraction.
63 #include <mach/mach_types.h>
64 #include <mach/boolean.h>
65 #include <mach/kern_return.h>
66 #include <mach/machine.h>
67 #include <mach/host_info.h>
68 #include <mach/host_reboot.h>
69 #include <mach/host_priv_server.h>
70 #include <mach/processor_server.h>
72 #include <kern/kern_types.h>
73 #include <kern/counters.h>
74 #include <kern/cpu_data.h>
75 #include <kern/ipc_host.h>
76 #include <kern/host.h>
77 #include <kern/lock.h>
78 #include <kern/machine.h>
79 #include <kern/misc_protos.h>
80 #include <kern/processor.h>
81 #include <kern/queue.h>
82 #include <kern/sched.h>
83 #include <kern/task.h>
84 #include <kern/thread.h>
86 #include <IOKit/IOHibernatePrivate.h>
92 struct machine_info machine_info
;
95 void processor_doshutdown(
96 processor_t processor
);
101 * Flag processor as up and running, and available
106 processor_t processor
)
108 processor_set_t pset
= &default_pset
;
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);
121 processor_unlock(processor
);
127 host_priv_t host_priv
,
130 if (host_priv
== HOST_PRIV_NULL
)
131 return (KERN_INVALID_HOST
);
133 assert(host_priv
== &realhost
);
135 if (options
& HOST_REBOOT_DEBUGGER
) {
136 Debugger("Debugger");
137 return (KERN_SUCCESS
);
140 halt_all_cpus(!(options
& HOST_REBOOT_HALT
));
142 return (KERN_SUCCESS
);
147 __unused processor_t processor
,
148 __unused processor_set_t new_pset
,
149 __unused boolean_t wait
)
151 return (KERN_FAILURE
);
156 processor_t processor
)
158 processor_set_t pset
;
162 processor_lock(processor
);
163 if (processor
->state
== PROCESSOR_OFF_LINE
) {
165 * Success if already shutdown.
167 processor_unlock(processor
);
170 return (KERN_SUCCESS
);
173 if (processor
->state
== PROCESSOR_START
) {
175 * Failure if currently being started.
177 processor_unlock(processor
);
180 return (KERN_FAILURE
);
184 * Must lock the scheduling lock
185 * to get at the processor state.
187 pset
= processor
->processor_set
;
188 if (pset
!= PROCESSOR_SET_NULL
) {
189 simple_lock(&pset
->sched_lock
);
192 * If the processor is dispatching, let it finish.
194 while (processor
->state
== PROCESSOR_DISPATCHING
) {
195 simple_unlock(&pset
->sched_lock
);
197 simple_lock(&pset
->sched_lock
);
201 * Success if already being shutdown.
203 if (processor
->state
== PROCESSOR_SHUTDOWN
) {
204 simple_unlock(&pset
->sched_lock
);
205 processor_unlock(processor
);
208 return (KERN_SUCCESS
);
213 * Success, already being shutdown.
215 processor_unlock(processor
);
218 return (KERN_SUCCESS
);
221 if (processor
->state
== PROCESSOR_IDLE
) {
222 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
226 if (processor
->state
== PROCESSOR_RUNNING
)
227 remqueue(&pset
->active_queue
, (queue_entry_t
)processor
);
229 panic("processor_shutdown");
231 processor
->state
= PROCESSOR_SHUTDOWN
;
233 simple_unlock(&pset
->sched_lock
);
235 processor_unlock(processor
);
237 processor_doshutdown(processor
);
240 cpu_exit_wait(PROCESSOR_DATA(processor
, slot_num
));
242 return (KERN_SUCCESS
);
246 * Called at splsched.
249 processor_doshutdown(
250 processor_t processor
)
252 thread_t old_thread
, self
= current_thread();
253 processor_set_t pset
;
258 * Get onto the processor to shutdown
260 prev
= thread_bind(self
, processor
);
261 thread_block(THREAD_CONTINUE_NULL
);
263 processor_lock(processor
);
264 pset
= processor
->processor_set
;
265 simple_lock(&pset
->sched_lock
);
267 if ((pcount
= pset
->processor_count
) == 1) {
268 simple_unlock(&pset
->sched_lock
);
269 processor_unlock(processor
);
273 processor_lock(processor
);
274 simple_lock(&pset
->sched_lock
);
277 assert(processor
->state
== PROCESSOR_SHUTDOWN
);
279 pset_remove_processor(pset
, processor
);
280 simple_unlock(&pset
->sched_lock
);
281 processor_unlock(processor
);
284 hibernate_vm_unlock();
287 * Continue processor shutdown in shutdown context.
289 thread_bind(self
, prev
);
290 old_thread
= machine_processor_shutdown(self
, processor_offline
, processor
);
292 thread_begin(self
, self
->last_processor
);
294 thread_dispatch(old_thread
);
297 * If we just shutdown another processor, move the
298 * timer call outs to the current processor.
300 if (processor
!= current_processor()) {
301 processor_lock(processor
);
302 if ( processor
->state
== PROCESSOR_OFF_LINE
||
303 processor
->state
== PROCESSOR_SHUTDOWN
)
304 timer_call_shutdown(processor
);
305 processor_unlock(processor
);
310 * Complete the shutdown and place the processor offline.
312 * Called at splsched in the shutdown context.
316 processor_t processor
)
318 thread_t thread
, old_thread
= processor
->active_thread
;
320 thread
= processor
->idle_thread
;
321 processor
->active_thread
= thread
;
322 processor
->current_pri
= IDLEPRI
;
324 processor
->last_dispatch
= mach_absolute_time();
325 timer_switch((uint32_t)processor
->last_dispatch
,
326 &PROCESSOR_DATA(processor
, offline_timer
));
328 thread_done(old_thread
, thread
, processor
);
330 machine_set_current_thread(thread
);
332 thread_begin(thread
, processor
);
334 thread_dispatch(old_thread
);
336 PMAP_DEACTIVATE_KERNEL(PROCESSOR_DATA(processor
, slot_num
));
338 processor_lock(processor
);
339 processor
->state
= PROCESSOR_OFF_LINE
;
340 hw_atomic_sub(&machine_info
.avail_cpus
, 1);
342 processor_unlock(processor
);
345 panic("zombie processor");
351 host_priv_t host_priv
,
352 kernel_boot_info_t boot_info
)
354 const char *src
= "";
355 if (host_priv
== HOST_PRIV_NULL
)
356 return (KERN_INVALID_HOST
);
358 assert(host_priv
== &realhost
);
361 * Copy first operator string terminated by '\0' followed by
362 * standardized strings generated from boot string.
364 src
= machine_boot_info(boot_info
, KERNEL_BOOT_INFO_MAX
);
365 if (src
!= boot_info
)
366 (void) strncpy(boot_info
, src
, KERNEL_BOOT_INFO_MAX
);
368 return (KERN_SUCCESS
);