]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000-2008 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/lock.h> | |
83 | #include <kern/machine.h> | |
84 | #include <kern/misc_protos.h> | |
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> | |
90 | ||
91 | #if HIBERNATION | |
92 | #include <IOKit/IOHibernatePrivate.h> | |
93 | #endif | |
94 | #include <IOKit/IOPlatformExpert.h> | |
95 | ||
96 | /* | |
97 | * Exported variables: | |
98 | */ | |
99 | ||
100 | struct machine_info machine_info; | |
101 | ||
102 | /* Forwards */ | |
103 | void processor_doshutdown( | |
104 | processor_t processor); | |
105 | ||
106 | /* | |
107 | * processor_up: | |
108 | * | |
109 | * Flag processor as up and running, and available | |
110 | * for scheduling. | |
111 | */ | |
112 | void | |
113 | processor_up( | |
114 | processor_t processor) | |
115 | { | |
116 | processor_set_t pset; | |
117 | spl_t s; | |
118 | ||
119 | s = splsched(); | |
120 | init_ast_check(processor); | |
121 | pset = processor->processor_set; | |
122 | pset_lock(pset); | |
123 | if (++pset->processor_count == 1) | |
124 | pset->low_pri = processor; | |
125 | enqueue_tail(&pset->active_queue, (queue_entry_t)processor); | |
126 | processor->state = PROCESSOR_RUNNING; | |
127 | (void)hw_atomic_add(&processor_avail_count, 1); | |
128 | pset_unlock(pset); | |
129 | ml_cpu_up(); | |
130 | splx(s); | |
131 | } | |
132 | ||
133 | kern_return_t | |
134 | host_reboot( | |
135 | host_priv_t host_priv, | |
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"); | |
145 | return (KERN_SUCCESS); | |
146 | } | |
147 | ||
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 | } | |
154 | ||
155 | return (KERN_SUCCESS); | |
156 | } | |
157 | ||
158 | kern_return_t | |
159 | processor_assign( | |
160 | __unused processor_t processor, | |
161 | __unused processor_set_t new_pset, | |
162 | __unused boolean_t wait) | |
163 | { | |
164 | return (KERN_FAILURE); | |
165 | } | |
166 | ||
167 | kern_return_t | |
168 | processor_shutdown( | |
169 | processor_t processor) | |
170 | { | |
171 | processor_set_t pset; | |
172 | spl_t s; | |
173 | ||
174 | s = splsched(); | |
175 | pset = processor->processor_set; | |
176 | pset_lock(pset); | |
177 | if (processor->state == PROCESSOR_OFF_LINE) { | |
178 | /* | |
179 | * Success if already shutdown. | |
180 | */ | |
181 | pset_unlock(pset); | |
182 | splx(s); | |
183 | ||
184 | return (KERN_SUCCESS); | |
185 | } | |
186 | ||
187 | if (processor->state == PROCESSOR_START) { | |
188 | /* | |
189 | * Failure if currently being started. | |
190 | */ | |
191 | pset_unlock(pset); | |
192 | splx(s); | |
193 | ||
194 | return (KERN_FAILURE); | |
195 | } | |
196 | ||
197 | /* | |
198 | * If the processor is dispatching, let it finish. | |
199 | */ | |
200 | while (processor->state == PROCESSOR_DISPATCHING) { | |
201 | pset_unlock(pset); | |
202 | delay(1); | |
203 | pset_lock(pset); | |
204 | } | |
205 | ||
206 | /* | |
207 | * Success if already being shutdown. | |
208 | */ | |
209 | if (processor->state == PROCESSOR_SHUTDOWN) { | |
210 | pset_unlock(pset); | |
211 | splx(s); | |
212 | ||
213 | return (KERN_SUCCESS); | |
214 | } | |
215 | ||
216 | if (processor->state == PROCESSOR_IDLE) { | |
217 | remqueue(&pset->idle_queue, (queue_entry_t)processor); | |
218 | pset->idle_count--; | |
219 | } | |
220 | else | |
221 | if (processor->state == PROCESSOR_RUNNING) | |
222 | remqueue(&pset->active_queue, (queue_entry_t)processor); | |
223 | else | |
224 | panic("processor_shutdown"); | |
225 | ||
226 | processor->state = PROCESSOR_SHUTDOWN; | |
227 | ||
228 | pset_unlock(pset); | |
229 | ||
230 | processor_doshutdown(processor); | |
231 | splx(s); | |
232 | ||
233 | cpu_exit_wait(PROCESSOR_DATA(processor, slot_num)); | |
234 | ||
235 | return (KERN_SUCCESS); | |
236 | } | |
237 | ||
238 | /* | |
239 | * Called at splsched. | |
240 | */ | |
241 | void | |
242 | processor_doshutdown( | |
243 | processor_t processor) | |
244 | { | |
245 | thread_t old_thread, self = current_thread(); | |
246 | processor_t prev; | |
247 | ||
248 | /* | |
249 | * Get onto the processor to shutdown | |
250 | */ | |
251 | prev = thread_bind(processor); | |
252 | thread_block(THREAD_CONTINUE_NULL); | |
253 | ||
254 | #if HIBERNATION | |
255 | if (processor_avail_count < 2) | |
256 | hibernate_vm_lock(); | |
257 | #endif | |
258 | ||
259 | assert(processor->state == PROCESSOR_SHUTDOWN); | |
260 | ||
261 | #if HIBERNATION | |
262 | if (processor_avail_count < 2) | |
263 | hibernate_vm_unlock(); | |
264 | #endif | |
265 | ||
266 | /* | |
267 | * Continue processor shutdown in shutdown context. | |
268 | */ | |
269 | thread_bind(prev); | |
270 | old_thread = machine_processor_shutdown(self, processor_offline, processor); | |
271 | ||
272 | thread_dispatch(old_thread, self); | |
273 | ||
274 | /* | |
275 | * If we just shutdown another processor, move any | |
276 | * threads and timer call outs to the current processor. | |
277 | */ | |
278 | if (processor != current_processor()) { | |
279 | processor_set_t pset = processor->processor_set; | |
280 | ||
281 | pset_lock(pset); | |
282 | ||
283 | if (processor->state == PROCESSOR_OFF_LINE || processor->state == PROCESSOR_SHUTDOWN) { | |
284 | timer_call_shutdown(processor); | |
285 | processor_queue_shutdown(processor); | |
286 | return; | |
287 | } | |
288 | ||
289 | pset_unlock(pset); | |
290 | } | |
291 | } | |
292 | ||
293 | /* | |
294 | * Complete the shutdown and place the processor offline. | |
295 | * | |
296 | * Called at splsched in the shutdown context. | |
297 | */ | |
298 | void | |
299 | processor_offline( | |
300 | processor_t processor) | |
301 | { | |
302 | thread_t new_thread, old_thread = processor->active_thread; | |
303 | processor_set_t pset; | |
304 | ||
305 | new_thread = processor->idle_thread; | |
306 | processor->active_thread = new_thread; | |
307 | processor->current_pri = IDLEPRI; | |
308 | processor->deadline = UINT64_MAX; | |
309 | new_thread->last_processor = processor; | |
310 | ||
311 | processor->last_dispatch = mach_absolute_time(); | |
312 | timer_stop(PROCESSOR_DATA(processor, thread_timer), processor->last_dispatch); | |
313 | ||
314 | machine_set_current_thread(new_thread); | |
315 | ||
316 | thread_dispatch(old_thread, new_thread); | |
317 | ||
318 | PMAP_DEACTIVATE_KERNEL(PROCESSOR_DATA(processor, slot_num)); | |
319 | ||
320 | pset = processor->processor_set; | |
321 | pset_lock(pset); | |
322 | processor->state = PROCESSOR_OFF_LINE; | |
323 | if (--pset->processor_count == 0) | |
324 | pset->low_pri = PROCESSOR_NULL; | |
325 | (void)hw_atomic_sub(&processor_avail_count, 1); | |
326 | pset_unlock(pset); | |
327 | ml_cpu_down(); | |
328 | ||
329 | cpu_sleep(); | |
330 | panic("zombie processor"); | |
331 | /*NOTREACHED*/ | |
332 | } | |
333 | ||
334 | kern_return_t | |
335 | host_get_boot_info( | |
336 | host_priv_t host_priv, | |
337 | kernel_boot_info_t boot_info) | |
338 | { | |
339 | const char *src = ""; | |
340 | if (host_priv == HOST_PRIV_NULL) | |
341 | return (KERN_INVALID_HOST); | |
342 | ||
343 | assert(host_priv == &realhost); | |
344 | ||
345 | /* | |
346 | * Copy first operator string terminated by '\0' followed by | |
347 | * standardized strings generated from boot string. | |
348 | */ | |
349 | src = machine_boot_info(boot_info, KERNEL_BOOT_INFO_MAX); | |
350 | if (src != boot_info) | |
351 | (void) strncpy(boot_info, src, KERNEL_BOOT_INFO_MAX); | |
352 | ||
353 | return (KERN_SUCCESS); | |
354 | } |