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