2 * Copyright (c) 2000-2009 Apple 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/machine.h>
83 #include <kern/misc_protos.h>
84 #include <kern/processor.h>
85 #include <kern/queue.h>
86 #include <kern/sched.h>
87 #include <kern/task.h>
88 #include <kern/thread.h>
90 #include <machine/commpage.h>
93 #include <IOKit/IOHibernatePrivate.h>
95 #include <IOKit/IOPlatformExpert.h>
98 extern void (*dtrace_cpu_state_changed_hook
)(int, boolean_t
);
102 * Exported variables:
105 struct machine_info machine_info
;
108 void processor_doshutdown(
109 processor_t processor
);
114 * Flag processor as up and running, and available
119 processor_t processor
)
121 processor_set_t pset
;
125 init_ast_check(processor
);
126 pset
= processor
->processor_set
;
128 ++pset
->online_processor_count
;
129 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
130 processor
->state
= PROCESSOR_RUNNING
;
131 (void)hw_atomic_add(&processor_avail_count
, 1);
132 commpage_update_active_cpus();
138 if (dtrace_cpu_state_changed_hook
)
139 (*dtrace_cpu_state_changed_hook
)(processor
->cpu_id
, TRUE
);
142 #include <atm/atm_internal.h>
146 host_priv_t host_priv
,
149 if (host_priv
== HOST_PRIV_NULL
)
150 return (KERN_INVALID_HOST
);
152 assert(host_priv
== &realhost
);
154 if (options
& HOST_REBOOT_DEBUGGER
) {
155 Debugger("Debugger");
156 return (KERN_SUCCESS
);
159 if (options
& HOST_REBOOT_UPSDELAY
) {
160 // UPS power cutoff path
161 PEHaltRestart( kPEUPSDelayHaltCPU
);
163 halt_all_cpus(!(options
& HOST_REBOOT_HALT
));
166 return (KERN_SUCCESS
);
171 __unused processor_t processor
,
172 __unused processor_set_t new_pset
,
173 __unused boolean_t wait
)
175 return (KERN_FAILURE
);
180 processor_t processor
)
182 processor_set_t pset
;
186 pset
= processor
->processor_set
;
188 if (processor
->state
== PROCESSOR_OFF_LINE
) {
190 * Success if already shutdown.
195 return (KERN_SUCCESS
);
198 if (processor
->state
== PROCESSOR_START
) {
200 * Failure if currently being started.
205 return (KERN_FAILURE
);
209 * If the processor is dispatching, let it finish.
211 while (processor
->state
== PROCESSOR_DISPATCHING
) {
220 * Success if already being shutdown.
222 if (processor
->state
== PROCESSOR_SHUTDOWN
) {
226 return (KERN_SUCCESS
);
229 if (processor
->state
== PROCESSOR_IDLE
)
230 remqueue((queue_entry_t
)processor
);
232 if (processor
->state
== PROCESSOR_RUNNING
)
233 remqueue((queue_entry_t
)processor
);
235 processor
->state
= PROCESSOR_SHUTDOWN
;
239 processor_doshutdown(processor
);
242 cpu_exit_wait(processor
->cpu_id
);
244 return (KERN_SUCCESS
);
248 * Called with interrupts disabled.
251 processor_doshutdown(
252 processor_t processor
)
254 thread_t old_thread
, self
= current_thread();
256 processor_set_t pset
;
259 * Get onto the processor to shutdown
261 prev
= thread_bind(processor
);
262 thread_block(THREAD_CONTINUE_NULL
);
264 assert(processor
->state
== PROCESSOR_SHUTDOWN
);
267 if (dtrace_cpu_state_changed_hook
)
268 (*dtrace_cpu_state_changed_hook
)(processor
->cpu_id
, FALSE
);
274 if (processor_avail_count
< 2) {
276 hibernate_vm_unlock();
280 pset
= processor
->processor_set
;
282 processor
->state
= PROCESSOR_OFF_LINE
;
283 --pset
->online_processor_count
;
284 (void)hw_atomic_sub(&processor_avail_count
, 1);
285 commpage_update_active_cpus();
286 SCHED(processor_queue_shutdown
)(processor
);
287 /* pset lock dropped */
290 * Continue processor shutdown in shutdown context.
293 old_thread
= machine_processor_shutdown(self
, processor_offline
, processor
);
295 thread_dispatch(old_thread
, self
);
299 *Complete the shutdown and place the processor offline.
301 * Called at splsched in the shutdown context.
305 processor_t processor
)
307 thread_t new_thread
, old_thread
= processor
->active_thread
;
309 new_thread
= processor
->idle_thread
;
310 processor
->active_thread
= new_thread
;
311 processor
->current_pri
= IDLEPRI
;
312 processor
->current_thmode
= TH_MODE_NONE
;
313 processor
->deadline
= UINT64_MAX
;
314 new_thread
->last_processor
= processor
;
316 processor
->last_dispatch
= mach_absolute_time();
317 timer_stop(PROCESSOR_DATA(processor
, thread_timer
), processor
->last_dispatch
);
319 machine_set_current_thread(new_thread
);
321 thread_dispatch(old_thread
, new_thread
);
323 PMAP_DEACTIVATE_KERNEL(processor
->cpu_id
);
326 panic("zombie processor");
332 host_priv_t host_priv
,
333 kernel_boot_info_t boot_info
)
335 const char *src
= "";
336 if (host_priv
== HOST_PRIV_NULL
)
337 return (KERN_INVALID_HOST
);
339 assert(host_priv
== &realhost
);
342 * Copy first operator string terminated by '\0' followed by
343 * standardized strings generated from boot string.
345 src
= machine_boot_info(boot_info
, KERNEL_BOOT_INFO_MAX
);
346 if (src
!= boot_info
)
347 (void) strncpy(boot_info
, src
, KERNEL_BOOT_INFO_MAX
);
349 return (KERN_SUCCESS
);