]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/kern/machine.c
xnu-517.12.7.tar.gz
[apple/xnu.git] / osfmk / kern / machine.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2005 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 <cpus.h>
61
62#include <string.h>
63#include <mach/boolean.h>
64#include <mach/kern_return.h>
65#include <mach/mach_types.h>
66#include <mach/machine.h>
67#include <mach/host_info.h>
68#include <mach/host_reboot.h>
69#include <kern/counters.h>
70#include <kern/cpu_data.h>
71#include <kern/ipc_host.h>
72#include <kern/host.h>
73#include <kern/lock.h>
74#include <kern/machine.h>
75#include <kern/processor.h>
76#include <kern/queue.h>
77#include <kern/sched.h>
78#include <kern/task.h>
79#include <kern/thread.h>
80#include <kern/thread_swap.h>
81#include <kern/misc_protos.h>
82
83#include <kern/mk_sp.h>
84
85/*
86 * Exported variables:
87 */
88
89struct machine_info machine_info;
90struct machine_slot machine_slot[NCPUS];
91
92thread_t machine_wake_thread;
93
94/* Forwards */
95void processor_doshutdown(
96 processor_t processor);
97
98/*
99 * cpu_up:
100 *
101 * Flag specified cpu as up and running. Called when a processor comes
102 * online.
103 */
104void
105cpu_up(
106 int cpu)
107{
108 processor_t processor = cpu_to_processor(cpu);
109 processor_set_t pset = &default_pset;
110 struct machine_slot *ms;
111 spl_t s;
112
113 s = splsched();
114 processor_lock(processor);
115 init_ast_check(processor);
116 ms = &machine_slot[cpu];
117 ms->running = TRUE;
118 machine_info.avail_cpus++;
119 simple_lock(&pset->sched_lock);
120 pset_add_processor(pset, processor);
121 enqueue_tail(&pset->active_queue, (queue_entry_t)processor);
122 processor->deadline = UINT64_MAX;
123 processor->state = PROCESSOR_RUNNING;
124 simple_unlock(&pset->sched_lock);
125 processor_unlock(processor);
126 splx(s);
127}
128
129/*
130 * cpu_down:
131 *
132 * Flag specified cpu as down. Called when a processor is about to
133 * go offline.
134 */
135void
136cpu_down(
137 int cpu)
138{
139 processor_t processor;
140 struct machine_slot *ms;
141 spl_t s;
142
143 processor = cpu_to_processor(cpu);
144
145 s = splsched();
146 processor_lock(processor);
147 ms = &machine_slot[cpu];
148 ms->running = FALSE;
149 machine_info.avail_cpus--;
150 /*
151 * processor has already been removed from pset.
152 */
153 processor->state = PROCESSOR_OFF_LINE;
154 processor_unlock(processor);
155 splx(s);
156}
157
158kern_return_t
159host_reboot(
160 host_priv_t host_priv,
161 int options)
162{
163 if (host_priv == HOST_PRIV_NULL)
164 return (KERN_INVALID_HOST);
165
166 assert(host_priv == &realhost);
167
168 if (options & HOST_REBOOT_DEBUGGER) {
169 Debugger("Debugger");
170 return (KERN_SUCCESS);
171 }
172
173 halt_all_cpus(!(options & HOST_REBOOT_HALT));
174
175 return (KERN_SUCCESS);
176}
177
178kern_return_t
179processor_assign(
180 processor_t processor,
181 processor_set_t new_pset,
182 boolean_t wait)
183{
184#ifdef lint
185 processor++; new_pset++; wait++;
186#endif /* lint */
187 return (KERN_FAILURE);
188}
189
190kern_return_t
191processor_shutdown(
192 processor_t processor)
193{
194 processor_set_t pset;
195 spl_t s;
196
197 s = splsched();
198 processor_lock(processor);
199 if ( processor->state == PROCESSOR_OFF_LINE ||
200 processor->state == PROCESSOR_SHUTDOWN ) {
201 /*
202 * Success if already shutdown or being shutdown.
203 */
204 processor_unlock(processor);
205 splx(s);
206
207 return (KERN_SUCCESS);
208 }
209
210 if (processor->state == PROCESSOR_START) {
211 /*
212 * Failure if currently being started.
213 */
214 processor_unlock(processor);
215 splx(s);
216
217 return (KERN_FAILURE);
218 }
219
220 /*
221 * Processor must be in a processor set. Must lock the scheduling
222 * lock to get at the processor state.
223 */
224 pset = processor->processor_set;
225 simple_lock(&pset->sched_lock);
226
227 /*
228 * If the processor is dispatching, let it finish - it will set its
229 * state to running very soon.
230 */
231 while (*(volatile int *)&processor->state == PROCESSOR_DISPATCHING) {
232 simple_unlock(&pset->sched_lock);
233 delay(1);
234 simple_lock(&pset->sched_lock);
235 }
236
237 if (processor->state == PROCESSOR_IDLE) {
238 remqueue(&pset->idle_queue, (queue_entry_t)processor);
239 pset->idle_count--;
240 }
241 else
242 if (processor->state == PROCESSOR_RUNNING)
243 remqueue(&pset->active_queue, (queue_entry_t)processor);
244 else
245 panic("processor_request_action");
246
247 processor->state = PROCESSOR_SHUTDOWN;
248
249 simple_unlock(&pset->sched_lock);
250
251 processor_unlock(processor);
252
253 processor_doshutdown(processor);
254 splx(s);
255
256#ifdef __ppc__
257 cpu_exit_wait(processor->slot_num);
258#endif
259
260 return (KERN_SUCCESS);
261}
262
263/*
264 * Called at splsched.
265 */
266void
267processor_doshutdown(
268 processor_t processor)
269{
270 thread_t old_thread, self = current_thread();
271 processor_set_t pset;
272 processor_t prev;
273
274 /*
275 * Get onto the processor to shutdown
276 */
277 prev = thread_bind(self, processor);
278 thread_block(THREAD_CONTINUE_NULL);
279
280 processor_lock(processor);
281 pset = processor->processor_set;
282 simple_lock(&pset->sched_lock);
283
284 if (pset->processor_count == 1) {
285 thread_t thread;
286 extern void start_cpu_thread(void);
287
288 simple_unlock(&pset->sched_lock);
289 processor_unlock(processor);
290
291 /*
292 * Create the thread, and point it at the routine.
293 */
294 thread = kernel_thread_create(start_cpu_thread, MAXPRI_KERNEL);
295
296 thread_lock(thread);
297 machine_wake_thread = thread;
298 thread->state = TH_RUN;
299 pset_run_incr(thread->processor_set);
300 thread_unlock(thread);
301
302 processor_lock(processor);
303 simple_lock(&pset->sched_lock);
304 }
305
306 assert(processor->state == PROCESSOR_SHUTDOWN);
307
308 pset_remove_processor(pset, processor);
309 simple_unlock(&pset->sched_lock);
310 processor_unlock(processor);
311
312 /*
313 * Clean up.
314 */
315 thread_bind(self, prev);
316 old_thread = switch_to_shutdown_context(self,
317 processor_offline, processor);
318 if (processor != current_processor())
319 timer_call_shutdown(processor);
320
321 _mk_sp_thread_begin(self, self->last_processor);
322
323 thread_dispatch(old_thread);
324}
325
326/*
327 * Actually do the processor shutdown. This is called at splsched,
328 * running on the processor's shutdown stack.
329 */
330
331void
332processor_offline(
333 processor_t processor)
334{
335 register thread_t old_thread = processor->active_thread;
336 register int cpu = processor->slot_num;
337
338 timer_call_cancel(&processor->quantum_timer);
339 timer_switch(&kernel_timer[cpu]);
340 processor->active_thread = processor->idle_thread;
341 machine_thread_set_current(processor->active_thread);
342 thread_dispatch(old_thread);
343
344 /*
345 * OK, now exit this cpu.
346 */
347 PMAP_DEACTIVATE_KERNEL(cpu);
348 cpu_down(cpu);
349 cpu_sleep();
350 panic("zombie processor");
351 /*NOTREACHED*/
352}
353
354kern_return_t
355host_get_boot_info(
356 host_priv_t host_priv,
357 kernel_boot_info_t boot_info)
358{
359 char *src = "";
360 extern char *machine_boot_info(
361 kernel_boot_info_t boot_info,
362 vm_size_t buf_len);
363
364 if (host_priv == HOST_PRIV_NULL)
365 return (KERN_INVALID_HOST);
366
367 assert(host_priv == &realhost);
368
369 /*
370 * Copy first operator string terminated by '\0' followed by
371 * standardized strings generated from boot string.
372 */
373 src = machine_boot_info(boot_info, KERNEL_BOOT_INFO_MAX);
374 if (src != boot_info)
375 (void) strncpy(boot_info, src, KERNEL_BOOT_INFO_MAX);
376
377 return (KERN_SUCCESS);
378}