2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. 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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
59 * File: kern/machine.c
60 * Author: Avadis Tevanian, Jr.
63 * Support for machine independent machine abstraction.
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>
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>
91 #include <IOKit/IOHibernatePrivate.h>
92 #include <IOKit/IOPlatformExpert.h>
98 struct machine_info machine_info
;
101 void processor_doshutdown(
102 processor_t processor
);
107 * Flag processor as up and running, and available
112 processor_t processor
)
114 processor_set_t pset
= &default_pset
;
118 processor_lock(processor
);
119 init_ast_check(processor
);
120 simple_lock(&pset
->sched_lock
);
121 pset_add_processor(pset
, processor
);
122 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
123 processor
->state
= PROCESSOR_RUNNING
;
124 simple_unlock(&pset
->sched_lock
);
125 hw_atomic_add(&machine_info
.avail_cpus
, 1);
127 processor_unlock(processor
);
133 host_priv_t host_priv
,
136 if (host_priv
== HOST_PRIV_NULL
)
137 return (KERN_INVALID_HOST
);
139 assert(host_priv
== &realhost
);
141 if (options
& HOST_REBOOT_DEBUGGER
) {
142 Debugger("Debugger");
143 return (KERN_SUCCESS
);
146 if (options
& HOST_REBOOT_UPSDELAY
) {
147 // UPS power cutoff path
148 PEHaltRestart( kPEUPSDelayHaltCPU
);
150 halt_all_cpus(!(options
& HOST_REBOOT_HALT
));
153 return (KERN_SUCCESS
);
158 __unused processor_t processor
,
159 __unused processor_set_t new_pset
,
160 __unused boolean_t wait
)
162 return (KERN_FAILURE
);
167 processor_t processor
)
169 processor_set_t pset
;
173 processor_lock(processor
);
174 if (processor
->state
== PROCESSOR_OFF_LINE
) {
176 * Success if already shutdown.
178 processor_unlock(processor
);
181 return (KERN_SUCCESS
);
184 if (processor
->state
== PROCESSOR_START
) {
186 * Failure if currently being started.
188 processor_unlock(processor
);
191 return (KERN_FAILURE
);
195 * Must lock the scheduling lock
196 * to get at the processor state.
198 pset
= processor
->processor_set
;
199 if (pset
!= PROCESSOR_SET_NULL
) {
200 simple_lock(&pset
->sched_lock
);
203 * If the processor is dispatching, let it finish.
205 while (processor
->state
== PROCESSOR_DISPATCHING
) {
206 simple_unlock(&pset
->sched_lock
);
208 simple_lock(&pset
->sched_lock
);
212 * Success if already being shutdown.
214 if (processor
->state
== PROCESSOR_SHUTDOWN
) {
215 simple_unlock(&pset
->sched_lock
);
216 processor_unlock(processor
);
219 return (KERN_SUCCESS
);
224 * Success, already being shutdown.
226 processor_unlock(processor
);
229 return (KERN_SUCCESS
);
232 if (processor
->state
== PROCESSOR_IDLE
) {
233 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
237 if (processor
->state
== PROCESSOR_RUNNING
)
238 remqueue(&pset
->active_queue
, (queue_entry_t
)processor
);
240 panic("processor_shutdown");
242 processor
->state
= PROCESSOR_SHUTDOWN
;
244 simple_unlock(&pset
->sched_lock
);
246 processor_unlock(processor
);
248 processor_doshutdown(processor
);
251 cpu_exit_wait(PROCESSOR_DATA(processor
, slot_num
));
253 return (KERN_SUCCESS
);
257 * Called at splsched.
260 processor_doshutdown(
261 processor_t processor
)
263 thread_t old_thread
, self
= current_thread();
264 processor_set_t pset
;
269 * Get onto the processor to shutdown
271 prev
= thread_bind(self
, processor
);
272 thread_block(THREAD_CONTINUE_NULL
);
274 processor_lock(processor
);
275 pset
= processor
->processor_set
;
276 simple_lock(&pset
->sched_lock
);
278 if ((pcount
= pset
->processor_count
) == 1) {
279 simple_unlock(&pset
->sched_lock
);
280 processor_unlock(processor
);
284 processor_lock(processor
);
285 simple_lock(&pset
->sched_lock
);
288 assert(processor
->state
== PROCESSOR_SHUTDOWN
);
290 pset_remove_processor(pset
, processor
);
291 simple_unlock(&pset
->sched_lock
);
292 processor_unlock(processor
);
295 hibernate_vm_unlock();
298 * Continue processor shutdown in shutdown context.
300 thread_bind(self
, prev
);
301 old_thread
= machine_processor_shutdown(self
, processor_offline
, processor
);
303 thread_begin(self
, self
->last_processor
);
305 thread_dispatch(old_thread
);
308 * If we just shutdown another processor, move the
309 * timer call outs to the current processor.
311 if (processor
!= current_processor()) {
312 processor_lock(processor
);
313 if ( processor
->state
== PROCESSOR_OFF_LINE
||
314 processor
->state
== PROCESSOR_SHUTDOWN
)
315 timer_call_shutdown(processor
);
316 processor_unlock(processor
);
321 * Complete the shutdown and place the processor offline.
323 * Called at splsched in the shutdown context.
327 processor_t processor
)
329 thread_t thread
, old_thread
= processor
->active_thread
;
331 thread
= processor
->idle_thread
;
332 processor
->active_thread
= thread
;
333 processor
->current_pri
= IDLEPRI
;
335 processor
->last_dispatch
= mach_absolute_time();
336 timer_switch((uint32_t)processor
->last_dispatch
,
337 &PROCESSOR_DATA(processor
, offline_timer
));
339 thread_done(old_thread
, thread
, processor
);
341 machine_set_current_thread(thread
);
343 thread_begin(thread
, processor
);
345 thread_dispatch(old_thread
);
347 PMAP_DEACTIVATE_KERNEL(PROCESSOR_DATA(processor
, slot_num
));
349 processor_lock(processor
);
350 processor
->state
= PROCESSOR_OFF_LINE
;
351 hw_atomic_sub(&machine_info
.avail_cpus
, 1);
353 processor_unlock(processor
);
356 panic("zombie processor");
362 host_priv_t host_priv
,
363 kernel_boot_info_t boot_info
)
365 const char *src
= "";
366 if (host_priv
== HOST_PRIV_NULL
)
367 return (KERN_INVALID_HOST
);
369 assert(host_priv
== &realhost
);
372 * Copy first operator string terminated by '\0' followed by
373 * standardized strings generated from boot string.
375 src
= machine_boot_info(boot_info
, KERNEL_BOOT_INFO_MAX
);
376 if (src
!= boot_info
)
377 (void) strncpy(boot_info
, src
, KERNEL_BOOT_INFO_MAX
);
379 return (KERN_SUCCESS
);