2 * Copyright (c) 2000-2008 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/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>
92 #include <IOKit/IOHibernatePrivate.h>
94 #include <IOKit/IOPlatformExpert.h>
100 struct machine_info machine_info
;
103 void processor_doshutdown(
104 processor_t processor
);
109 * Flag processor as up and running, and available
114 processor_t processor
)
116 processor_set_t pset
;
120 init_ast_check(processor
);
121 pset
= processor
->processor_set
;
123 if (++pset
->processor_count
== 1)
124 pset
->low_pri
= pset
->low_count
= processor
;
125 enqueue_tail(&pset
->active_queue
, (queue_entry_t
)processor
);
126 processor
->state
= PROCESSOR_RUNNING
;
127 (void)hw_atomic_add(&processor_avail_count
, 1);
135 host_priv_t host_priv
,
138 if (host_priv
== HOST_PRIV_NULL
)
139 return (KERN_INVALID_HOST
);
141 assert(host_priv
== &realhost
);
143 if (options
& HOST_REBOOT_DEBUGGER
) {
144 Debugger("Debugger");
145 return (KERN_SUCCESS
);
148 if (options
& HOST_REBOOT_UPSDELAY
) {
149 // UPS power cutoff path
150 PEHaltRestart( kPEUPSDelayHaltCPU
);
152 halt_all_cpus(!(options
& HOST_REBOOT_HALT
));
155 return (KERN_SUCCESS
);
160 __unused processor_t processor
,
161 __unused processor_set_t new_pset
,
162 __unused boolean_t wait
)
164 return (KERN_FAILURE
);
169 processor_t processor
)
171 processor_set_t pset
;
175 pset
= processor
->processor_set
;
177 if (processor
->state
== PROCESSOR_OFF_LINE
) {
179 * Success if already shutdown.
184 return (KERN_SUCCESS
);
187 if (processor
->state
== PROCESSOR_START
) {
189 * Failure if currently being started.
194 return (KERN_FAILURE
);
198 * If the processor is dispatching, let it finish.
200 while (processor
->state
== PROCESSOR_DISPATCHING
) {
207 * Success if already being shutdown.
209 if (processor
->state
== PROCESSOR_SHUTDOWN
) {
213 return (KERN_SUCCESS
);
216 if (processor
->state
== PROCESSOR_IDLE
)
217 remqueue(&pset
->idle_queue
, (queue_entry_t
)processor
);
219 if (processor
->state
== PROCESSOR_RUNNING
)
220 remqueue(&pset
->active_queue
, (queue_entry_t
)processor
);
222 processor
->state
= PROCESSOR_SHUTDOWN
;
226 processor_doshutdown(processor
);
229 cpu_exit_wait(processor
->cpu_num
);
231 return (KERN_SUCCESS
);
235 * Called at splsched.
238 processor_doshutdown(
239 processor_t processor
)
241 thread_t old_thread
, self
= current_thread();
245 * Get onto the processor to shutdown
247 prev
= thread_bind(processor
);
248 thread_block(THREAD_CONTINUE_NULL
);
251 if (processor_avail_count
< 2)
255 assert(processor
->state
== PROCESSOR_SHUTDOWN
);
258 if (processor_avail_count
< 2)
259 hibernate_vm_unlock();
263 * Continue processor shutdown in shutdown context.
266 old_thread
= machine_processor_shutdown(self
, processor_offline
, processor
);
268 thread_dispatch(old_thread
, self
);
272 * Complete the shutdown and place the processor offline.
274 * Called at splsched in the shutdown context.
278 processor_t processor
)
280 thread_t new_thread
, old_thread
= processor
->active_thread
;
281 processor_set_t pset
;
283 new_thread
= processor
->idle_thread
;
284 processor
->active_thread
= new_thread
;
285 processor
->current_pri
= IDLEPRI
;
286 processor
->deadline
= UINT64_MAX
;
287 new_thread
->last_processor
= processor
;
289 processor
->last_dispatch
= mach_absolute_time();
290 timer_stop(PROCESSOR_DATA(processor
, thread_timer
), processor
->last_dispatch
);
292 machine_set_current_thread(new_thread
);
294 thread_dispatch(old_thread
, new_thread
);
296 PMAP_DEACTIVATE_KERNEL(processor
->cpu_num
);
298 pset
= processor
->processor_set
;
300 processor
->state
= PROCESSOR_OFF_LINE
;
301 if (--pset
->processor_count
== 0)
302 pset
->low_pri
= pset
->low_count
= PROCESSOR_NULL
;
303 (void)hw_atomic_sub(&processor_avail_count
, 1);
304 processor_queue_shutdown(processor
);
305 /* pset lock dropped */
310 panic("zombie processor");
316 host_priv_t host_priv
,
317 kernel_boot_info_t boot_info
)
319 const char *src
= "";
320 if (host_priv
== HOST_PRIV_NULL
)
321 return (KERN_INVALID_HOST
);
323 assert(host_priv
== &realhost
);
326 * Copy first operator string terminated by '\0' followed by
327 * standardized strings generated from boot string.
329 src
= machine_boot_info(boot_info
, KERNEL_BOOT_INFO_MAX
);
330 if (src
!= boot_info
)
331 (void) strncpy(boot_info
, src
, KERNEL_BOOT_INFO_MAX
);
333 return (KERN_SUCCESS
);