]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/machine.c
xnu-1228.12.14.tar.gz
[apple/xnu.git] / osfmk / kern / machine.c
CommitLineData
1c79356b 1/*
cf7d32b8 2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58/*
59 * File: kern/machine.c
60 * Author: Avadis Tevanian, Jr.
61 * Date: 1987
62 *
63 * Support for machine independent machine abstraction.
64 */
65
1c79356b 66#include <string.h>
91447636
A
67
68#include <mach/mach_types.h>
1c79356b
A
69#include <mach/boolean.h>
70#include <mach/kern_return.h>
1c79356b
A
71#include <mach/machine.h>
72#include <mach/host_info.h>
73#include <mach/host_reboot.h>
91447636
A
74#include <mach/host_priv_server.h>
75#include <mach/processor_server.h>
76
77#include <kern/kern_types.h>
1c79356b
A
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>
91447636 84#include <kern/misc_protos.h>
1c79356b
A
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>
1c79356b 90
2d21ac55 91#if HIBERNATION
3a60a9f5 92#include <IOKit/IOHibernatePrivate.h>
2d21ac55 93#endif
0c530ab8 94#include <IOKit/IOPlatformExpert.h>
1c79356b
A
95
96/*
97 * Exported variables:
98 */
99
100struct machine_info machine_info;
1c79356b
A
101
102/* Forwards */
1c79356b
A
103void processor_doshutdown(
104 processor_t processor);
105
106/*
91447636 107 * processor_up:
1c79356b 108 *
91447636
A
109 * Flag processor as up and running, and available
110 * for scheduling.
1c79356b
A
111 */
112void
91447636 113processor_up(
2d21ac55 114 processor_t processor)
1c79356b 115{
2d21ac55 116 processor_set_t pset;
91447636 117 spl_t s;
1c79356b
A
118
119 s = splsched();
1c79356b 120 init_ast_check(processor);
2d21ac55
A
121 pset = processor->processor_set;
122 pset_lock(pset);
cf7d32b8 123 if (++pset->processor_count == 1)
c910b4d9 124 pset->low_pri = pset->low_count = processor;
cf7d32b8 125 enqueue_tail(&pset->active_queue, (queue_entry_t)processor);
1c79356b 126 processor->state = PROCESSOR_RUNNING;
2d21ac55
A
127 (void)hw_atomic_add(&processor_avail_count, 1);
128 pset_unlock(pset);
91447636 129 ml_cpu_up();
1c79356b
A
130 splx(s);
131}
132
133kern_return_t
134host_reboot(
55e303ae 135 host_priv_t host_priv,
1c79356b
A
136 int options)
137{
138 if (host_priv == HOST_PRIV_NULL)
139 return (KERN_INVALID_HOST);
140
141 assert(host_priv == &realhost);
142
143 if (options & HOST_REBOOT_DEBUGGER) {
144 Debugger("Debugger");
55e303ae 145 return (KERN_SUCCESS);
1c79356b
A
146 }
147
0c530ab8
A
148 if (options & HOST_REBOOT_UPSDELAY) {
149 // UPS power cutoff path
150 PEHaltRestart( kPEUPSDelayHaltCPU );
151 } else {
152 halt_all_cpus(!(options & HOST_REBOOT_HALT));
153 }
9bccf70c 154
55e303ae 155 return (KERN_SUCCESS);
1c79356b
A
156}
157
158kern_return_t
159processor_assign(
91447636
A
160 __unused processor_t processor,
161 __unused processor_set_t new_pset,
162 __unused boolean_t wait)
1c79356b 163{
1c79356b
A
164 return (KERN_FAILURE);
165}
166
1c79356b
A
167kern_return_t
168processor_shutdown(
55e303ae 169 processor_t processor)
1c79356b 170{
55e303ae
A
171 processor_set_t pset;
172 spl_t s;
1c79356b
A
173
174 s = splsched();
2d21ac55
A
175 pset = processor->processor_set;
176 pset_lock(pset);
91447636 177 if (processor->state == PROCESSOR_OFF_LINE) {
1c79356b 178 /*
91447636 179 * Success if already shutdown.
1c79356b 180 */
2d21ac55 181 pset_unlock(pset);
1c79356b
A
182 splx(s);
183
184 return (KERN_SUCCESS);
185 }
186
55e303ae
A
187 if (processor->state == PROCESSOR_START) {
188 /*
189 * Failure if currently being started.
190 */
2d21ac55 191 pset_unlock(pset);
55e303ae 192 splx(s);
1c79356b 193
55e303ae
A
194 return (KERN_FAILURE);
195 }
1c79356b 196
55e303ae 197 /*
2d21ac55 198 * If the processor is dispatching, let it finish.
55e303ae 199 */
2d21ac55
A
200 while (processor->state == PROCESSOR_DISPATCHING) {
201 pset_unlock(pset);
202 delay(1);
203 pset_lock(pset);
91447636 204 }
2d21ac55
A
205
206 /*
207 * Success if already being shutdown.
208 */
209 if (processor->state == PROCESSOR_SHUTDOWN) {
210 pset_unlock(pset);
91447636
A
211 splx(s);
212
213 return (KERN_SUCCESS);
55e303ae 214 }
1c79356b 215
c910b4d9 216 if (processor->state == PROCESSOR_IDLE)
55e303ae 217 remqueue(&pset->idle_queue, (queue_entry_t)processor);
55e303ae
A
218 else
219 if (processor->state == PROCESSOR_RUNNING)
220 remqueue(&pset->active_queue, (queue_entry_t)processor);
1c79356b 221
55e303ae 222 processor->state = PROCESSOR_SHUTDOWN;
1c79356b 223
2d21ac55 224 pset_unlock(pset);
1c79356b 225
55e303ae 226 processor_doshutdown(processor);
1c79356b 227 splx(s);
1c79356b 228
c910b4d9 229 cpu_exit_wait(processor->cpu_num);
5353443c 230
55e303ae 231 return (KERN_SUCCESS);
1c79356b
A
232}
233
234/*
55e303ae 235 * Called at splsched.
1c79356b
A
236 */
237void
55e303ae
A
238processor_doshutdown(
239 processor_t processor)
1c79356b 240{
55e303ae 241 thread_t old_thread, self = current_thread();
55e303ae 242 processor_t prev;
1c79356b
A
243
244 /*
245 * Get onto the processor to shutdown
246 */
2d21ac55 247 prev = thread_bind(processor);
9bccf70c 248 thread_block(THREAD_CONTINUE_NULL);
1c79356b 249
2d21ac55
A
250#if HIBERNATION
251 if (processor_avail_count < 2)
3a60a9f5 252 hibernate_vm_lock();
2d21ac55 253#endif
1c79356b 254
55e303ae 255 assert(processor->state == PROCESSOR_SHUTDOWN);
1c79356b 256
2d21ac55
A
257#if HIBERNATION
258 if (processor_avail_count < 2)
3a60a9f5 259 hibernate_vm_unlock();
2d21ac55 260#endif
91447636 261
1c79356b 262 /*
91447636 263 * Continue processor shutdown in shutdown context.
1c79356b 264 */
2d21ac55 265 thread_bind(prev);
91447636 266 old_thread = machine_processor_shutdown(self, processor_offline, processor);
a3d08fcd 267
2d21ac55 268 thread_dispatch(old_thread, self);
1c79356b
A
269}
270
271/*
91447636
A
272 * Complete the shutdown and place the processor offline.
273 *
274 * Called at splsched in the shutdown context.
1c79356b 275 */
1c79356b 276void
55e303ae 277processor_offline(
2d21ac55 278 processor_t processor)
1c79356b 279{
2d21ac55
A
280 thread_t new_thread, old_thread = processor->active_thread;
281 processor_set_t pset;
91447636 282
2d21ac55
A
283 new_thread = processor->idle_thread;
284 processor->active_thread = new_thread;
91447636 285 processor->current_pri = IDLEPRI;
2d21ac55
A
286 processor->deadline = UINT64_MAX;
287 new_thread->last_processor = processor;
91447636
A
288
289 processor->last_dispatch = mach_absolute_time();
2d21ac55 290 timer_stop(PROCESSOR_DATA(processor, thread_timer), processor->last_dispatch);
91447636 291
2d21ac55 292 machine_set_current_thread(new_thread);
91447636 293
2d21ac55 294 thread_dispatch(old_thread, new_thread);
1c79356b 295
c910b4d9 296 PMAP_DEACTIVATE_KERNEL(processor->cpu_num);
91447636 297
2d21ac55
A
298 pset = processor->processor_set;
299 pset_lock(pset);
91447636 300 processor->state = PROCESSOR_OFF_LINE;
cf7d32b8 301 if (--pset->processor_count == 0)
c910b4d9 302 pset->low_pri = pset->low_count = PROCESSOR_NULL;
2d21ac55 303 (void)hw_atomic_sub(&processor_avail_count, 1);
c910b4d9
A
304 processor_queue_shutdown(processor);
305 /* pset lock dropped */
306
91447636 307 ml_cpu_down();
91447636 308
1c79356b
A
309 cpu_sleep();
310 panic("zombie processor");
311 /*NOTREACHED*/
312}
313
314kern_return_t
315host_get_boot_info(
316 host_priv_t host_priv,
317 kernel_boot_info_t boot_info)
318{
91447636 319 const char *src = "";
1c79356b
A
320 if (host_priv == HOST_PRIV_NULL)
321 return (KERN_INVALID_HOST);
322
323 assert(host_priv == &realhost);
324
325 /*
326 * Copy first operator string terminated by '\0' followed by
327 * standardized strings generated from boot string.
328 */
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);
332
333 return (KERN_SUCCESS);
334}