]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
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 | ||
1c79356b | 66 | #include <string.h> |
91447636 A |
67 | |
68 | #include <mach/mach_types.h> | |
1c79356b A |
69 | #include <mach/boolean.h> |
70 | #include <mach/kern_return.h> | |
1c79356b A |
71 | #include <mach/machine.h> |
72 | #include <mach/host_info.h> | |
73 | #include <mach/host_reboot.h> | |
91447636 A |
74 | #include <mach/host_priv_server.h> |
75 | #include <mach/processor_server.h> | |
76 | ||
77 | #include <kern/kern_types.h> | |
1c79356b A |
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> | |
91447636 | 84 | #include <kern/misc_protos.h> |
1c79356b A |
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> | |
1c79356b | 90 | |
2d21ac55 | 91 | #if HIBERNATION |
3a60a9f5 | 92 | #include <IOKit/IOHibernatePrivate.h> |
2d21ac55 | 93 | #endif |
0c530ab8 | 94 | #include <IOKit/IOPlatformExpert.h> |
1c79356b A |
95 | |
96 | /* | |
97 | * Exported variables: | |
98 | */ | |
99 | ||
100 | struct machine_info machine_info; | |
1c79356b A |
101 | |
102 | /* Forwards */ | |
1c79356b A |
103 | void processor_doshutdown( |
104 | processor_t processor); | |
105 | ||
106 | /* | |
91447636 | 107 | * processor_up: |
1c79356b | 108 | * |
91447636 A |
109 | * Flag processor as up and running, and available |
110 | * for scheduling. | |
1c79356b A |
111 | */ |
112 | void | |
91447636 | 113 | processor_up( |
2d21ac55 | 114 | processor_t processor) |
1c79356b | 115 | { |
2d21ac55 | 116 | processor_set_t pset; |
91447636 | 117 | spl_t s; |
1c79356b A |
118 | |
119 | s = splsched(); | |
1c79356b | 120 | init_ast_check(processor); |
2d21ac55 A |
121 | pset = processor->processor_set; |
122 | pset_lock(pset); | |
123 | pset->processor_count++; | |
124 | enqueue_head(&pset->active_queue, (queue_entry_t)processor); | |
1c79356b | 125 | processor->state = PROCESSOR_RUNNING; |
2d21ac55 A |
126 | (void)hw_atomic_add(&processor_avail_count, 1); |
127 | pset_unlock(pset); | |
91447636 | 128 | ml_cpu_up(); |
1c79356b A |
129 | splx(s); |
130 | } | |
131 | ||
132 | kern_return_t | |
133 | host_reboot( | |
55e303ae | 134 | host_priv_t host_priv, |
1c79356b A |
135 | int options) |
136 | { | |
137 | if (host_priv == HOST_PRIV_NULL) | |
138 | return (KERN_INVALID_HOST); | |
139 | ||
140 | assert(host_priv == &realhost); | |
141 | ||
142 | if (options & HOST_REBOOT_DEBUGGER) { | |
143 | Debugger("Debugger"); | |
55e303ae | 144 | return (KERN_SUCCESS); |
1c79356b A |
145 | } |
146 | ||
0c530ab8 A |
147 | if (options & HOST_REBOOT_UPSDELAY) { |
148 | // UPS power cutoff path | |
149 | PEHaltRestart( kPEUPSDelayHaltCPU ); | |
150 | } else { | |
151 | halt_all_cpus(!(options & HOST_REBOOT_HALT)); | |
152 | } | |
9bccf70c | 153 | |
55e303ae | 154 | return (KERN_SUCCESS); |
1c79356b A |
155 | } |
156 | ||
157 | kern_return_t | |
158 | processor_assign( | |
91447636 A |
159 | __unused processor_t processor, |
160 | __unused processor_set_t new_pset, | |
161 | __unused boolean_t wait) | |
1c79356b | 162 | { |
1c79356b A |
163 | return (KERN_FAILURE); |
164 | } | |
165 | ||
1c79356b A |
166 | kern_return_t |
167 | processor_shutdown( | |
55e303ae | 168 | processor_t processor) |
1c79356b | 169 | { |
55e303ae A |
170 | processor_set_t pset; |
171 | spl_t s; | |
1c79356b A |
172 | |
173 | s = splsched(); | |
2d21ac55 A |
174 | pset = processor->processor_set; |
175 | pset_lock(pset); | |
91447636 | 176 | if (processor->state == PROCESSOR_OFF_LINE) { |
1c79356b | 177 | /* |
91447636 | 178 | * Success if already shutdown. |
1c79356b | 179 | */ |
2d21ac55 | 180 | pset_unlock(pset); |
1c79356b A |
181 | splx(s); |
182 | ||
183 | return (KERN_SUCCESS); | |
184 | } | |
185 | ||
55e303ae A |
186 | if (processor->state == PROCESSOR_START) { |
187 | /* | |
188 | * Failure if currently being started. | |
189 | */ | |
2d21ac55 | 190 | pset_unlock(pset); |
55e303ae | 191 | splx(s); |
1c79356b | 192 | |
55e303ae A |
193 | return (KERN_FAILURE); |
194 | } | |
1c79356b | 195 | |
55e303ae | 196 | /* |
2d21ac55 | 197 | * If the processor is dispatching, let it finish. |
55e303ae | 198 | */ |
2d21ac55 A |
199 | while (processor->state == PROCESSOR_DISPATCHING) { |
200 | pset_unlock(pset); | |
201 | delay(1); | |
202 | pset_lock(pset); | |
91447636 | 203 | } |
2d21ac55 A |
204 | |
205 | /* | |
206 | * Success if already being shutdown. | |
207 | */ | |
208 | if (processor->state == PROCESSOR_SHUTDOWN) { | |
209 | pset_unlock(pset); | |
91447636 A |
210 | splx(s); |
211 | ||
212 | return (KERN_SUCCESS); | |
55e303ae | 213 | } |
1c79356b | 214 | |
55e303ae A |
215 | if (processor->state == PROCESSOR_IDLE) { |
216 | remqueue(&pset->idle_queue, (queue_entry_t)processor); | |
217 | pset->idle_count--; | |
218 | } | |
219 | else | |
220 | if (processor->state == PROCESSOR_RUNNING) | |
221 | remqueue(&pset->active_queue, (queue_entry_t)processor); | |
222 | else | |
91447636 | 223 | panic("processor_shutdown"); |
1c79356b | 224 | |
55e303ae | 225 | processor->state = PROCESSOR_SHUTDOWN; |
1c79356b | 226 | |
2d21ac55 | 227 | pset_unlock(pset); |
1c79356b | 228 | |
55e303ae | 229 | processor_doshutdown(processor); |
1c79356b | 230 | splx(s); |
1c79356b | 231 | |
91447636 | 232 | cpu_exit_wait(PROCESSOR_DATA(processor, slot_num)); |
5353443c | 233 | |
55e303ae | 234 | return (KERN_SUCCESS); |
1c79356b A |
235 | } |
236 | ||
237 | /* | |
55e303ae | 238 | * Called at splsched. |
1c79356b A |
239 | */ |
240 | void | |
55e303ae A |
241 | processor_doshutdown( |
242 | processor_t processor) | |
1c79356b | 243 | { |
55e303ae | 244 | thread_t old_thread, self = current_thread(); |
55e303ae | 245 | processor_t prev; |
1c79356b A |
246 | |
247 | /* | |
248 | * Get onto the processor to shutdown | |
249 | */ | |
2d21ac55 | 250 | prev = thread_bind(processor); |
9bccf70c | 251 | thread_block(THREAD_CONTINUE_NULL); |
1c79356b | 252 | |
2d21ac55 A |
253 | #if HIBERNATION |
254 | if (processor_avail_count < 2) | |
3a60a9f5 | 255 | hibernate_vm_lock(); |
2d21ac55 | 256 | #endif |
1c79356b | 257 | |
55e303ae | 258 | assert(processor->state == PROCESSOR_SHUTDOWN); |
1c79356b | 259 | |
2d21ac55 A |
260 | #if HIBERNATION |
261 | if (processor_avail_count < 2) | |
3a60a9f5 | 262 | hibernate_vm_unlock(); |
2d21ac55 | 263 | #endif |
91447636 | 264 | |
1c79356b | 265 | /* |
91447636 | 266 | * Continue processor shutdown in shutdown context. |
1c79356b | 267 | */ |
2d21ac55 | 268 | thread_bind(prev); |
91447636 | 269 | old_thread = machine_processor_shutdown(self, processor_offline, processor); |
a3d08fcd | 270 | |
2d21ac55 | 271 | thread_dispatch(old_thread, self); |
91447636 A |
272 | |
273 | /* | |
2d21ac55 A |
274 | * If we just shutdown another processor, move any |
275 | * threads and timer call outs to the current processor. | |
91447636 A |
276 | */ |
277 | if (processor != current_processor()) { | |
2d21ac55 A |
278 | processor_set_t pset = processor->processor_set; |
279 | ||
280 | pset_lock(pset); | |
281 | ||
282 | if (processor->state == PROCESSOR_OFF_LINE || processor->state == PROCESSOR_SHUTDOWN) { | |
91447636 | 283 | timer_call_shutdown(processor); |
2d21ac55 A |
284 | processor_queue_shutdown(processor); |
285 | return; | |
286 | } | |
287 | ||
288 | pset_unlock(pset); | |
91447636 | 289 | } |
1c79356b A |
290 | } |
291 | ||
292 | /* | |
91447636 A |
293 | * Complete the shutdown and place the processor offline. |
294 | * | |
295 | * Called at splsched in the shutdown context. | |
1c79356b | 296 | */ |
1c79356b | 297 | void |
55e303ae | 298 | processor_offline( |
2d21ac55 | 299 | processor_t processor) |
1c79356b | 300 | { |
2d21ac55 A |
301 | thread_t new_thread, old_thread = processor->active_thread; |
302 | processor_set_t pset; | |
91447636 | 303 | |
2d21ac55 A |
304 | new_thread = processor->idle_thread; |
305 | processor->active_thread = new_thread; | |
91447636 | 306 | processor->current_pri = IDLEPRI; |
2d21ac55 A |
307 | processor->deadline = UINT64_MAX; |
308 | new_thread->last_processor = processor; | |
91447636 A |
309 | |
310 | processor->last_dispatch = mach_absolute_time(); | |
2d21ac55 | 311 | timer_stop(PROCESSOR_DATA(processor, thread_timer), processor->last_dispatch); |
91447636 | 312 | |
2d21ac55 | 313 | machine_set_current_thread(new_thread); |
91447636 | 314 | |
2d21ac55 | 315 | thread_dispatch(old_thread, new_thread); |
1c79356b | 316 | |
91447636 A |
317 | PMAP_DEACTIVATE_KERNEL(PROCESSOR_DATA(processor, slot_num)); |
318 | ||
2d21ac55 A |
319 | pset = processor->processor_set; |
320 | pset_lock(pset); | |
321 | pset->processor_count--; | |
91447636 | 322 | processor->state = PROCESSOR_OFF_LINE; |
2d21ac55 A |
323 | if (processor == pset->low_hint) |
324 | pset->low_hint = PROCESSOR_NULL; | |
325 | (void)hw_atomic_sub(&processor_avail_count, 1); | |
326 | pset_unlock(pset); | |
91447636 | 327 | ml_cpu_down(); |
91447636 | 328 | |
1c79356b A |
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 | { | |
91447636 | 339 | const char *src = ""; |
1c79356b A |
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 | } |