]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/machine.c
31ab349324800d4d254bc44644a7d5e0d0205c12
[apple/xnu.git] / osfmk / kern / machine.c
1 /*
2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
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
66 #include <string.h>
67
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>
76
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>
89
90 #include <machine/commpage.h>
91
92 #if HIBERNATION
93 #include <IOKit/IOHibernatePrivate.h>
94 #endif
95 #include <IOKit/IOPlatformExpert.h>
96
97 #if CONFIG_DTRACE
98 extern void (*dtrace_cpu_state_changed_hook)(int, boolean_t);
99 #endif
100
101 /*
102 * Exported variables:
103 */
104
105 struct machine_info machine_info;
106
107 /* Forwards */
108 void processor_doshutdown(
109 processor_t processor);
110
111 /*
112 * processor_up:
113 *
114 * Flag processor as up and running, and available
115 * for scheduling.
116 */
117 void
118 processor_up(
119 processor_t processor)
120 {
121 processor_set_t pset;
122 spl_t s;
123
124 s = splsched();
125 init_ast_check(processor);
126 pset = processor->processor_set;
127 pset_lock(pset);
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();
133 pset_unlock(pset);
134 ml_cpu_up();
135 splx(s);
136
137 #if CONFIG_DTRACE
138 if (dtrace_cpu_state_changed_hook)
139 (*dtrace_cpu_state_changed_hook)(processor->cpu_id, TRUE);
140 #endif
141 }
142 #include <atm/atm_internal.h>
143
144 kern_return_t
145 host_reboot(
146 host_priv_t host_priv,
147 int options)
148 {
149 if (host_priv == HOST_PRIV_NULL)
150 return (KERN_INVALID_HOST);
151
152 assert(host_priv == &realhost);
153
154 #if DEVELOPMENT || DEBUG
155 if (options & HOST_REBOOT_DEBUGGER) {
156 Debugger("Debugger");
157 return (KERN_SUCCESS);
158 }
159 #endif
160
161 if (options & HOST_REBOOT_UPSDELAY) {
162 // UPS power cutoff path
163 PEHaltRestart( kPEUPSDelayHaltCPU );
164 } else {
165 halt_all_cpus(!(options & HOST_REBOOT_HALT));
166 }
167
168 return (KERN_SUCCESS);
169 }
170
171 kern_return_t
172 processor_assign(
173 __unused processor_t processor,
174 __unused processor_set_t new_pset,
175 __unused boolean_t wait)
176 {
177 return (KERN_FAILURE);
178 }
179
180 kern_return_t
181 processor_shutdown(
182 processor_t processor)
183 {
184 processor_set_t pset;
185 spl_t s;
186
187 s = splsched();
188 pset = processor->processor_set;
189 pset_lock(pset);
190 if (processor->state == PROCESSOR_OFF_LINE) {
191 /*
192 * Success if already shutdown.
193 */
194 pset_unlock(pset);
195 splx(s);
196
197 return (KERN_SUCCESS);
198 }
199
200 if (processor->state == PROCESSOR_START) {
201 /*
202 * Failure if currently being started.
203 */
204 pset_unlock(pset);
205 splx(s);
206
207 return (KERN_FAILURE);
208 }
209
210 /*
211 * If the processor is dispatching, let it finish.
212 */
213 while (processor->state == PROCESSOR_DISPATCHING) {
214 pset_unlock(pset);
215 splx(s);
216 delay(1);
217 s = splsched();
218 pset_lock(pset);
219 }
220
221 /*
222 * Success if already being shutdown.
223 */
224 if (processor->state == PROCESSOR_SHUTDOWN) {
225 pset_unlock(pset);
226 splx(s);
227
228 return (KERN_SUCCESS);
229 }
230
231 if (processor->state == PROCESSOR_IDLE)
232 remqueue((queue_entry_t)processor);
233 else
234 if (processor->state == PROCESSOR_RUNNING)
235 remqueue((queue_entry_t)processor);
236
237 processor->state = PROCESSOR_SHUTDOWN;
238
239 pset_unlock(pset);
240
241 processor_doshutdown(processor);
242 splx(s);
243
244 cpu_exit_wait(processor->cpu_id);
245
246 return (KERN_SUCCESS);
247 }
248
249 /*
250 * Called with interrupts disabled.
251 */
252 void
253 processor_doshutdown(
254 processor_t processor)
255 {
256 thread_t old_thread, self = current_thread();
257 processor_t prev;
258 processor_set_t pset;
259
260 /*
261 * Get onto the processor to shutdown
262 */
263 prev = thread_bind(processor);
264 thread_block(THREAD_CONTINUE_NULL);
265
266 assert(processor->state == PROCESSOR_SHUTDOWN);
267
268 #if CONFIG_DTRACE
269 if (dtrace_cpu_state_changed_hook)
270 (*dtrace_cpu_state_changed_hook)(processor->cpu_id, FALSE);
271 #endif
272
273 ml_cpu_down();
274
275 #if HIBERNATION
276 if (processor_avail_count < 2) {
277 hibernate_vm_lock();
278 hibernate_vm_unlock();
279 }
280 #endif
281
282 pset = processor->processor_set;
283 pset_lock(pset);
284 processor->state = PROCESSOR_OFF_LINE;
285 --pset->online_processor_count;
286 (void)hw_atomic_sub(&processor_avail_count, 1);
287 commpage_update_active_cpus();
288 SCHED(processor_queue_shutdown)(processor);
289 /* pset lock dropped */
290
291 /*
292 * Continue processor shutdown in shutdown context.
293 *
294 * We save the current context in machine_processor_shutdown in such a way
295 * that when this thread is next invoked it will return from here instead of
296 * from the machine_switch_context() in thread_invoke like a normal context switch.
297 *
298 * As such, 'old_thread' is neither the idle thread nor the current thread - it's whatever
299 * thread invoked back to this one. (Usually, it's another processor's idle thread.)
300 *
301 * TODO: Make this a real thread_run of the idle_thread, so we don't have to keep this in sync
302 * with thread_invoke.
303 */
304 thread_bind(prev);
305 old_thread = machine_processor_shutdown(self, processor_offline, processor);
306
307 thread_dispatch(old_thread, self);
308 }
309
310 /*
311 * Complete the shutdown and place the processor offline.
312 *
313 * Called at splsched in the shutdown context.
314 * This performs a minimal thread_invoke() to the idle thread,
315 * so it needs to be kept in sync with what thread_invoke() does.
316 *
317 * The onlining half of this is done in load_context().
318 */
319 void
320 processor_offline(
321 processor_t processor)
322 {
323 assert(processor == current_processor());
324 assert(processor->active_thread == current_thread());
325
326 thread_t old_thread = processor->active_thread;
327 thread_t new_thread = processor->idle_thread;
328
329 processor->active_thread = new_thread;
330 processor->current_pri = IDLEPRI;
331 processor->current_thmode = TH_MODE_NONE;
332 processor->starting_pri = IDLEPRI;
333 processor->current_sfi_class = SFI_CLASS_KERNEL;
334 processor->deadline = UINT64_MAX;
335 new_thread->last_processor = processor;
336
337 uint64_t ctime = mach_absolute_time();
338
339 processor->last_dispatch = ctime;
340 old_thread->last_run_time = ctime;
341
342 /* Update processor->thread_timer and ->kernel_timer to point to the new thread */
343 thread_timer_event(ctime, &new_thread->system_timer);
344 PROCESSOR_DATA(processor, kernel_timer) = &new_thread->system_timer;
345
346 timer_stop(PROCESSOR_DATA(processor, current_state), ctime);
347
348 KERNEL_DEBUG_CONSTANT_IST(KDEBUG_TRACE,
349 MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED) | DBG_FUNC_NONE,
350 old_thread->reason, (uintptr_t)thread_tid(new_thread),
351 old_thread->sched_pri, new_thread->sched_pri, 0);
352
353 machine_set_current_thread(new_thread);
354
355 thread_dispatch(old_thread, new_thread);
356
357 PMAP_DEACTIVATE_KERNEL(processor->cpu_id);
358
359 cpu_sleep();
360 panic("zombie processor");
361 /*NOTREACHED*/
362 }
363
364 kern_return_t
365 host_get_boot_info(
366 host_priv_t host_priv,
367 kernel_boot_info_t boot_info)
368 {
369 const char *src = "";
370 if (host_priv == HOST_PRIV_NULL)
371 return (KERN_INVALID_HOST);
372
373 assert(host_priv == &realhost);
374
375 /*
376 * Copy first operator string terminated by '\0' followed by
377 * standardized strings generated from boot string.
378 */
379 src = machine_boot_info(boot_info, KERNEL_BOOT_INFO_MAX);
380 if (src != boot_info)
381 (void) strncpy(boot_info, src, KERNEL_BOOT_INFO_MAX);
382
383 return (KERN_SUCCESS);
384 }