]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
91447636 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b | 3 | * |
8f6c56a5 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
8f6c56a5 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. | |
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 | |
8ad349bb | 24 | * limitations under the License. |
8f6c56a5 A |
25 | * |
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 | |
3a60a9f5 | 91 | #include <IOKit/IOHibernatePrivate.h> |
89b3af67 | 92 | #include <IOKit/IOPlatformExpert.h> |
1c79356b A |
93 | |
94 | /* | |
95 | * Exported variables: | |
96 | */ | |
97 | ||
98 | struct machine_info machine_info; | |
1c79356b A |
99 | |
100 | /* Forwards */ | |
1c79356b A |
101 | void processor_doshutdown( |
102 | processor_t processor); | |
103 | ||
104 | /* | |
91447636 | 105 | * processor_up: |
1c79356b | 106 | * |
91447636 A |
107 | * Flag processor as up and running, and available |
108 | * for scheduling. | |
1c79356b A |
109 | */ |
110 | void | |
91447636 A |
111 | processor_up( |
112 | processor_t processor) | |
1c79356b | 113 | { |
91447636 A |
114 | processor_set_t pset = &default_pset; |
115 | spl_t s; | |
1c79356b A |
116 | |
117 | s = splsched(); | |
118 | processor_lock(processor); | |
119 | init_ast_check(processor); | |
9bccf70c | 120 | simple_lock(&pset->sched_lock); |
55e303ae | 121 | pset_add_processor(pset, processor); |
9bccf70c | 122 | enqueue_tail(&pset->active_queue, (queue_entry_t)processor); |
1c79356b | 123 | processor->state = PROCESSOR_RUNNING; |
9bccf70c | 124 | simple_unlock(&pset->sched_lock); |
91447636 A |
125 | hw_atomic_add(&machine_info.avail_cpus, 1); |
126 | ml_cpu_up(); | |
1c79356b A |
127 | processor_unlock(processor); |
128 | splx(s); | |
129 | } | |
130 | ||
131 | kern_return_t | |
132 | host_reboot( | |
55e303ae | 133 | host_priv_t host_priv, |
1c79356b A |
134 | int options) |
135 | { | |
136 | if (host_priv == HOST_PRIV_NULL) | |
137 | return (KERN_INVALID_HOST); | |
138 | ||
139 | assert(host_priv == &realhost); | |
140 | ||
141 | if (options & HOST_REBOOT_DEBUGGER) { | |
142 | Debugger("Debugger"); | |
55e303ae | 143 | return (KERN_SUCCESS); |
1c79356b A |
144 | } |
145 | ||
89b3af67 A |
146 | if (options & HOST_REBOOT_UPSDELAY) { |
147 | // UPS power cutoff path | |
148 | PEHaltRestart( kPEUPSDelayHaltCPU ); | |
149 | } else { | |
150 | halt_all_cpus(!(options & HOST_REBOOT_HALT)); | |
151 | } | |
9bccf70c | 152 | |
55e303ae | 153 | return (KERN_SUCCESS); |
1c79356b A |
154 | } |
155 | ||
156 | kern_return_t | |
157 | processor_assign( | |
91447636 A |
158 | __unused processor_t processor, |
159 | __unused processor_set_t new_pset, | |
160 | __unused boolean_t wait) | |
1c79356b | 161 | { |
1c79356b A |
162 | return (KERN_FAILURE); |
163 | } | |
164 | ||
1c79356b A |
165 | kern_return_t |
166 | processor_shutdown( | |
55e303ae | 167 | processor_t processor) |
1c79356b | 168 | { |
55e303ae A |
169 | processor_set_t pset; |
170 | spl_t s; | |
1c79356b A |
171 | |
172 | s = splsched(); | |
173 | processor_lock(processor); | |
91447636 | 174 | if (processor->state == PROCESSOR_OFF_LINE) { |
1c79356b | 175 | /* |
91447636 | 176 | * Success if already shutdown. |
1c79356b A |
177 | */ |
178 | processor_unlock(processor); | |
179 | splx(s); | |
180 | ||
181 | return (KERN_SUCCESS); | |
182 | } | |
183 | ||
55e303ae A |
184 | if (processor->state == PROCESSOR_START) { |
185 | /* | |
186 | * Failure if currently being started. | |
187 | */ | |
188 | processor_unlock(processor); | |
189 | splx(s); | |
1c79356b | 190 | |
55e303ae A |
191 | return (KERN_FAILURE); |
192 | } | |
1c79356b | 193 | |
55e303ae | 194 | /* |
91447636 A |
195 | * Must lock the scheduling lock |
196 | * to get at the processor state. | |
55e303ae A |
197 | */ |
198 | pset = processor->processor_set; | |
91447636 | 199 | if (pset != PROCESSOR_SET_NULL) { |
55e303ae | 200 | simple_lock(&pset->sched_lock); |
91447636 A |
201 | |
202 | /* | |
203 | * If the processor is dispatching, let it finish. | |
204 | */ | |
205 | while (processor->state == PROCESSOR_DISPATCHING) { | |
206 | simple_unlock(&pset->sched_lock); | |
207 | delay(1); | |
208 | simple_lock(&pset->sched_lock); | |
209 | } | |
210 | ||
211 | /* | |
212 | * Success if already being shutdown. | |
213 | */ | |
214 | if (processor->state == PROCESSOR_SHUTDOWN) { | |
215 | simple_unlock(&pset->sched_lock); | |
216 | processor_unlock(processor); | |
217 | splx(s); | |
218 | ||
219 | return (KERN_SUCCESS); | |
220 | } | |
221 | } | |
222 | else { | |
223 | /* | |
224 | * Success, already being shutdown. | |
225 | */ | |
226 | processor_unlock(processor); | |
227 | splx(s); | |
228 | ||
229 | return (KERN_SUCCESS); | |
55e303ae | 230 | } |
1c79356b | 231 | |
55e303ae A |
232 | if (processor->state == PROCESSOR_IDLE) { |
233 | remqueue(&pset->idle_queue, (queue_entry_t)processor); | |
234 | pset->idle_count--; | |
235 | } | |
236 | else | |
237 | if (processor->state == PROCESSOR_RUNNING) | |
238 | remqueue(&pset->active_queue, (queue_entry_t)processor); | |
239 | else | |
91447636 | 240 | panic("processor_shutdown"); |
1c79356b | 241 | |
55e303ae | 242 | processor->state = PROCESSOR_SHUTDOWN; |
1c79356b | 243 | |
55e303ae | 244 | simple_unlock(&pset->sched_lock); |
1c79356b | 245 | |
55e303ae | 246 | processor_unlock(processor); |
1c79356b | 247 | |
55e303ae | 248 | processor_doshutdown(processor); |
1c79356b | 249 | splx(s); |
1c79356b | 250 | |
91447636 | 251 | cpu_exit_wait(PROCESSOR_DATA(processor, slot_num)); |
5353443c | 252 | |
55e303ae | 253 | return (KERN_SUCCESS); |
1c79356b A |
254 | } |
255 | ||
256 | /* | |
55e303ae | 257 | * Called at splsched. |
1c79356b A |
258 | */ |
259 | void | |
55e303ae A |
260 | processor_doshutdown( |
261 | processor_t processor) | |
1c79356b | 262 | { |
55e303ae | 263 | thread_t old_thread, self = current_thread(); |
1c79356b | 264 | processor_set_t pset; |
55e303ae | 265 | processor_t prev; |
91447636 | 266 | int pcount; |
1c79356b A |
267 | |
268 | /* | |
269 | * Get onto the processor to shutdown | |
270 | */ | |
55e303ae | 271 | prev = thread_bind(self, processor); |
9bccf70c | 272 | thread_block(THREAD_CONTINUE_NULL); |
1c79356b | 273 | |
55e303ae | 274 | processor_lock(processor); |
1c79356b | 275 | pset = processor->processor_set; |
55e303ae | 276 | simple_lock(&pset->sched_lock); |
1c79356b | 277 | |
91447636 | 278 | if ((pcount = pset->processor_count) == 1) { |
55e303ae A |
279 | simple_unlock(&pset->sched_lock); |
280 | processor_unlock(processor); | |
1c79356b | 281 | |
3a60a9f5 A |
282 | hibernate_vm_lock(); |
283 | ||
55e303ae A |
284 | processor_lock(processor); |
285 | simple_lock(&pset->sched_lock); | |
1c79356b A |
286 | } |
287 | ||
55e303ae | 288 | assert(processor->state == PROCESSOR_SHUTDOWN); |
1c79356b A |
289 | |
290 | pset_remove_processor(pset, processor); | |
55e303ae | 291 | simple_unlock(&pset->sched_lock); |
1c79356b | 292 | processor_unlock(processor); |
1c79356b | 293 | |
3a60a9f5 A |
294 | if (pcount == 1) |
295 | hibernate_vm_unlock(); | |
91447636 | 296 | |
1c79356b | 297 | /* |
91447636 | 298 | * Continue processor shutdown in shutdown context. |
1c79356b | 299 | */ |
55e303ae | 300 | thread_bind(self, prev); |
91447636 | 301 | old_thread = machine_processor_shutdown(self, processor_offline, processor); |
a3d08fcd | 302 | |
91447636 | 303 | thread_begin(self, self->last_processor); |
a3d08fcd | 304 | |
1c79356b | 305 | thread_dispatch(old_thread); |
91447636 A |
306 | |
307 | /* | |
308 | * If we just shutdown another processor, move the | |
309 | * timer call outs to the current processor. | |
310 | */ | |
311 | if (processor != current_processor()) { | |
312 | processor_lock(processor); | |
313 | if ( processor->state == PROCESSOR_OFF_LINE || | |
314 | processor->state == PROCESSOR_SHUTDOWN ) | |
315 | timer_call_shutdown(processor); | |
316 | processor_unlock(processor); | |
317 | } | |
1c79356b A |
318 | } |
319 | ||
320 | /* | |
91447636 A |
321 | * Complete the shutdown and place the processor offline. |
322 | * | |
323 | * Called at splsched in the shutdown context. | |
1c79356b | 324 | */ |
1c79356b | 325 | void |
55e303ae | 326 | processor_offline( |
1c79356b A |
327 | processor_t processor) |
328 | { | |
91447636 A |
329 | thread_t thread, old_thread = processor->active_thread; |
330 | ||
331 | thread = processor->idle_thread; | |
332 | processor->active_thread = thread; | |
333 | processor->current_pri = IDLEPRI; | |
334 | ||
335 | processor->last_dispatch = mach_absolute_time(); | |
336 | timer_switch((uint32_t)processor->last_dispatch, | |
337 | &PROCESSOR_DATA(processor, offline_timer)); | |
338 | ||
339 | thread_done(old_thread, thread, processor); | |
340 | ||
341 | machine_set_current_thread(thread); | |
342 | ||
343 | thread_begin(thread, processor); | |
1c79356b | 344 | |
55e303ae | 345 | thread_dispatch(old_thread); |
1c79356b | 346 | |
91447636 A |
347 | PMAP_DEACTIVATE_KERNEL(PROCESSOR_DATA(processor, slot_num)); |
348 | ||
349 | processor_lock(processor); | |
350 | processor->state = PROCESSOR_OFF_LINE; | |
351 | hw_atomic_sub(&machine_info.avail_cpus, 1); | |
352 | ml_cpu_down(); | |
353 | processor_unlock(processor); | |
354 | ||
1c79356b A |
355 | cpu_sleep(); |
356 | panic("zombie processor"); | |
357 | /*NOTREACHED*/ | |
358 | } | |
359 | ||
360 | kern_return_t | |
361 | host_get_boot_info( | |
362 | host_priv_t host_priv, | |
363 | kernel_boot_info_t boot_info) | |
364 | { | |
91447636 | 365 | const char *src = ""; |
1c79356b A |
366 | if (host_priv == HOST_PRIV_NULL) |
367 | return (KERN_INVALID_HOST); | |
368 | ||
369 | assert(host_priv == &realhost); | |
370 | ||
371 | /* | |
372 | * Copy first operator string terminated by '\0' followed by | |
373 | * standardized strings generated from boot string. | |
374 | */ | |
375 | src = machine_boot_info(boot_info, KERNEL_BOOT_INFO_MAX); | |
376 | if (src != boot_info) | |
377 | (void) strncpy(boot_info, src, KERNEL_BOOT_INFO_MAX); | |
378 | ||
379 | return (KERN_SUCCESS); | |
380 | } |