]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/machine.c
778c96ba788fd3f3bd45d8e5c23ee54fcc7b426f
[apple/xnu.git] / osfmk / kern / machine.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * @OSF_COPYRIGHT@
25 */
26 /*
27 * Mach Operating System
28 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
29 * All Rights Reserved.
30 *
31 * Permission to use, copy, modify and distribute this software and its
32 * documentation is hereby granted, provided that both the copyright
33 * notice and this permission notice appear in all copies of the
34 * software, derivative works or modified versions, and any portions
35 * thereof, and that both notices appear in supporting documentation.
36 *
37 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
38 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
39 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 *
41 * Carnegie Mellon requests users of this software to return to
42 *
43 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
44 * School of Computer Science
45 * Carnegie Mellon University
46 * Pittsburgh PA 15213-3890
47 *
48 * any improvements or extensions that they make and grant Carnegie Mellon
49 * the rights to redistribute these changes.
50 */
51 /*
52 */
53 /*
54 * File: kern/machine.c
55 * Author: Avadis Tevanian, Jr.
56 * Date: 1987
57 *
58 * Support for machine independent machine abstraction.
59 */
60
61 #include <string.h>
62
63 #include <mach/mach_types.h>
64 #include <mach/boolean.h>
65 #include <mach/kern_return.h>
66 #include <mach/machine.h>
67 #include <mach/host_info.h>
68 #include <mach/host_reboot.h>
69 #include <mach/host_priv_server.h>
70 #include <mach/processor_server.h>
71
72 #include <kern/kern_types.h>
73 #include <kern/counters.h>
74 #include <kern/cpu_data.h>
75 #include <kern/ipc_host.h>
76 #include <kern/host.h>
77 #include <kern/lock.h>
78 #include <kern/machine.h>
79 #include <kern/misc_protos.h>
80 #include <kern/processor.h>
81 #include <kern/queue.h>
82 #include <kern/sched.h>
83 #include <kern/task.h>
84 #include <kern/thread.h>
85
86 #include <IOKit/IOHibernatePrivate.h>
87
88 /*
89 * Exported variables:
90 */
91
92 struct machine_info machine_info;
93
94 /* Forwards */
95 void processor_doshutdown(
96 processor_t processor);
97
98 /*
99 * processor_up:
100 *
101 * Flag processor as up and running, and available
102 * for scheduling.
103 */
104 void
105 processor_up(
106 processor_t processor)
107 {
108 processor_set_t pset = &default_pset;
109 spl_t s;
110
111 s = splsched();
112 processor_lock(processor);
113 init_ast_check(processor);
114 simple_lock(&pset->sched_lock);
115 pset_add_processor(pset, processor);
116 enqueue_tail(&pset->active_queue, (queue_entry_t)processor);
117 processor->state = PROCESSOR_RUNNING;
118 simple_unlock(&pset->sched_lock);
119 hw_atomic_add(&machine_info.avail_cpus, 1);
120 ml_cpu_up();
121 processor_unlock(processor);
122 splx(s);
123 }
124
125 kern_return_t
126 host_reboot(
127 host_priv_t host_priv,
128 int options)
129 {
130 if (host_priv == HOST_PRIV_NULL)
131 return (KERN_INVALID_HOST);
132
133 assert(host_priv == &realhost);
134
135 if (options & HOST_REBOOT_DEBUGGER) {
136 Debugger("Debugger");
137 return (KERN_SUCCESS);
138 }
139
140 halt_all_cpus(!(options & HOST_REBOOT_HALT));
141
142 return (KERN_SUCCESS);
143 }
144
145 kern_return_t
146 processor_assign(
147 __unused processor_t processor,
148 __unused processor_set_t new_pset,
149 __unused boolean_t wait)
150 {
151 return (KERN_FAILURE);
152 }
153
154 kern_return_t
155 processor_shutdown(
156 processor_t processor)
157 {
158 processor_set_t pset;
159 spl_t s;
160
161 s = splsched();
162 processor_lock(processor);
163 if (processor->state == PROCESSOR_OFF_LINE) {
164 /*
165 * Success if already shutdown.
166 */
167 processor_unlock(processor);
168 splx(s);
169
170 return (KERN_SUCCESS);
171 }
172
173 if (processor->state == PROCESSOR_START) {
174 /*
175 * Failure if currently being started.
176 */
177 processor_unlock(processor);
178 splx(s);
179
180 return (KERN_FAILURE);
181 }
182
183 /*
184 * Must lock the scheduling lock
185 * to get at the processor state.
186 */
187 pset = processor->processor_set;
188 if (pset != PROCESSOR_SET_NULL) {
189 simple_lock(&pset->sched_lock);
190
191 /*
192 * If the processor is dispatching, let it finish.
193 */
194 while (processor->state == PROCESSOR_DISPATCHING) {
195 simple_unlock(&pset->sched_lock);
196 delay(1);
197 simple_lock(&pset->sched_lock);
198 }
199
200 /*
201 * Success if already being shutdown.
202 */
203 if (processor->state == PROCESSOR_SHUTDOWN) {
204 simple_unlock(&pset->sched_lock);
205 processor_unlock(processor);
206 splx(s);
207
208 return (KERN_SUCCESS);
209 }
210 }
211 else {
212 /*
213 * Success, already being shutdown.
214 */
215 processor_unlock(processor);
216 splx(s);
217
218 return (KERN_SUCCESS);
219 }
220
221 if (processor->state == PROCESSOR_IDLE) {
222 remqueue(&pset->idle_queue, (queue_entry_t)processor);
223 pset->idle_count--;
224 }
225 else
226 if (processor->state == PROCESSOR_RUNNING)
227 remqueue(&pset->active_queue, (queue_entry_t)processor);
228 else
229 panic("processor_shutdown");
230
231 processor->state = PROCESSOR_SHUTDOWN;
232
233 simple_unlock(&pset->sched_lock);
234
235 processor_unlock(processor);
236
237 processor_doshutdown(processor);
238 splx(s);
239
240 cpu_exit_wait(PROCESSOR_DATA(processor, slot_num));
241
242 return (KERN_SUCCESS);
243 }
244
245 /*
246 * Called at splsched.
247 */
248 void
249 processor_doshutdown(
250 processor_t processor)
251 {
252 thread_t old_thread, self = current_thread();
253 processor_set_t pset;
254 processor_t prev;
255 int pcount;
256
257 /*
258 * Get onto the processor to shutdown
259 */
260 prev = thread_bind(self, processor);
261 thread_block(THREAD_CONTINUE_NULL);
262
263 processor_lock(processor);
264 pset = processor->processor_set;
265 simple_lock(&pset->sched_lock);
266
267 if ((pcount = pset->processor_count) == 1) {
268 simple_unlock(&pset->sched_lock);
269 processor_unlock(processor);
270
271 hibernate_vm_lock();
272
273 processor_lock(processor);
274 simple_lock(&pset->sched_lock);
275 }
276
277 assert(processor->state == PROCESSOR_SHUTDOWN);
278
279 pset_remove_processor(pset, processor);
280 simple_unlock(&pset->sched_lock);
281 processor_unlock(processor);
282
283 if (pcount == 1)
284 hibernate_vm_unlock();
285
286 /*
287 * Continue processor shutdown in shutdown context.
288 */
289 thread_bind(self, prev);
290 old_thread = machine_processor_shutdown(self, processor_offline, processor);
291
292 thread_begin(self, self->last_processor);
293
294 thread_dispatch(old_thread);
295
296 /*
297 * If we just shutdown another processor, move the
298 * timer call outs to the current processor.
299 */
300 if (processor != current_processor()) {
301 processor_lock(processor);
302 if ( processor->state == PROCESSOR_OFF_LINE ||
303 processor->state == PROCESSOR_SHUTDOWN )
304 timer_call_shutdown(processor);
305 processor_unlock(processor);
306 }
307 }
308
309 /*
310 * Complete the shutdown and place the processor offline.
311 *
312 * Called at splsched in the shutdown context.
313 */
314 void
315 processor_offline(
316 processor_t processor)
317 {
318 thread_t thread, old_thread = processor->active_thread;
319
320 thread = processor->idle_thread;
321 processor->active_thread = thread;
322 processor->current_pri = IDLEPRI;
323
324 processor->last_dispatch = mach_absolute_time();
325 timer_switch((uint32_t)processor->last_dispatch,
326 &PROCESSOR_DATA(processor, offline_timer));
327
328 thread_done(old_thread, thread, processor);
329
330 machine_set_current_thread(thread);
331
332 thread_begin(thread, processor);
333
334 thread_dispatch(old_thread);
335
336 PMAP_DEACTIVATE_KERNEL(PROCESSOR_DATA(processor, slot_num));
337
338 processor_lock(processor);
339 processor->state = PROCESSOR_OFF_LINE;
340 hw_atomic_sub(&machine_info.avail_cpus, 1);
341 ml_cpu_down();
342 processor_unlock(processor);
343
344 cpu_sleep();
345 panic("zombie processor");
346 /*NOTREACHED*/
347 }
348
349 kern_return_t
350 host_get_boot_info(
351 host_priv_t host_priv,
352 kernel_boot_info_t boot_info)
353 {
354 const char *src = "";
355 if (host_priv == HOST_PRIV_NULL)
356 return (KERN_INVALID_HOST);
357
358 assert(host_priv == &realhost);
359
360 /*
361 * Copy first operator string terminated by '\0' followed by
362 * standardized strings generated from boot string.
363 */
364 src = machine_boot_info(boot_info, KERNEL_BOOT_INFO_MAX);
365 if (src != boot_info)
366 (void) strncpy(boot_info, src, KERNEL_BOOT_INFO_MAX);
367
368 return (KERN_SUCCESS);
369 }