]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
xnu-792.12.6.tar.gz
[apple/xnu.git] / osfmk / kern / startup.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * @OSF_COPYRIGHT@
32 */
33 /*
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
36 * All Rights Reserved.
37 *
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
43 *
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
47 *
48 * Carnegie Mellon requests users of this software to return to
49 *
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
54 *
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
57 */
58 /*
59 */
60
61 /*
62 * Mach kernel startup.
63 */
64
65 #include <debug.h>
66 #include <xpr_debug.h>
67 #include <mach_kdp.h>
68 #include <mach_host.h>
69 #include <norma_vm.h>
70
71 #include <mach/boolean.h>
72 #include <mach/machine.h>
73 #include <mach/thread_act.h>
74 #include <mach/task_special_ports.h>
75 #include <mach/vm_param.h>
76 #include <ipc/ipc_init.h>
77 #include <kern/assert.h>
78 #include <kern/misc_protos.h>
79 #include <kern/clock.h>
80 #include <kern/cpu_number.h>
81 #include <kern/ledger.h>
82 #include <kern/machine.h>
83 #include <kern/processor.h>
84 #include <kern/sched_prim.h>
85 #include <kern/startup.h>
86 #include <kern/task.h>
87 #include <kern/thread.h>
88 #include <kern/timer.h>
89 #include <kern/xpr.h>
90 #include <kern/zalloc.h>
91 #include <kern/locks.h>
92 #include <vm/vm_shared_memory_server.h>
93 #include <vm/vm_kern.h>
94 #include <vm/vm_init.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_pageout.h>
99 #include <machine/pmap.h>
100 #include <machine/commpage.h>
101 #include <libkern/version.h>
102
103 #ifdef __ppc__
104 #include <ppc/Firmware.h>
105 #include <ppc/mappings.h>
106 #include <ppc/serial_io.h>
107 #endif
108
109 static void kernel_bootstrap_thread(void);
110
111 static void load_context(
112 thread_t thread);
113
114 /*
115 * Running in virtual memory, on the interrupt stack.
116 */
117 void
118 kernel_bootstrap(void)
119 {
120 kern_return_t result;
121 thread_t thread;
122
123 lck_mod_init();
124 sched_init();
125 vm_mem_bootstrap();
126 ipc_bootstrap();
127 vm_mem_init();
128 ipc_init();
129
130 /*
131 * As soon as the virtual memory system is up, we record
132 * that this CPU is using the kernel pmap.
133 */
134 PMAP_ACTIVATE_KERNEL(master_cpu);
135
136 mapping_free_prime(); /* Load up with temporary mapping blocks */
137
138 machine_init();
139 kmod_init();
140 clock_init();
141
142 machine_info.memory_size = mem_size;
143 machine_info.max_mem = max_mem;
144 machine_info.major_version = version_major;
145 machine_info.minor_version = version_minor;
146
147 /*
148 * Initialize the IPC, task, and thread subsystems.
149 */
150 ledger_init();
151 task_init();
152 thread_init();
153
154 /*
155 * Create a kernel thread to execute the kernel bootstrap.
156 */
157 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
158 if (result != KERN_SUCCESS)
159 panic("kernel_bootstrap");
160
161 thread->state = TH_RUN;
162 thread_deallocate(thread);
163
164 load_context(thread);
165 /*NOTREACHED*/
166 }
167
168 /*
169 * Now running in a thread. Kick off other services,
170 * invoke user bootstrap, enter pageout loop.
171 */
172 static void
173 kernel_bootstrap_thread(void)
174 {
175 processor_t processor = current_processor();
176 thread_t self = current_thread();
177
178 /*
179 * Create the idle processor thread.
180 */
181 idle_thread_create(processor);
182
183 /*
184 * N.B. Do not stick anything else
185 * before this point.
186 *
187 * Start up the scheduler services.
188 */
189 sched_startup();
190
191 /*
192 * Remain on current processor as
193 * additional processors come online.
194 */
195 thread_bind(self, processor);
196
197 /*
198 * Kick off memory mapping adjustments.
199 */
200 mapping_adjust();
201
202 /*
203 * Create the clock service.
204 */
205 clock_service_create();
206
207 /*
208 * Create the device service.
209 */
210 device_service_create();
211
212 shared_file_boot_time_init(ENV_DEFAULT_ROOT, cpu_type());
213
214 #ifdef IOKIT
215 {
216 PE_init_iokit();
217 }
218 #endif
219
220 (void) spllo(); /* Allow interruptions */
221
222 /*
223 * Fill in the comm area (mapped into every task address space.)
224 */
225 commpage_populate();
226
227 /*
228 * Start the user bootstrap.
229 */
230 #ifdef MACH_BSD
231 {
232 bsd_init();
233 }
234 #endif
235
236 #if __ppc__
237 serial_keyboard_init(); /* Start serial keyboard if wanted */
238 #endif
239
240 thread_bind(self, PROCESSOR_NULL);
241
242 /*
243 * Become the pageout daemon.
244 */
245 vm_pageout();
246 /*NOTREACHED*/
247 }
248
249 /*
250 * slave_main:
251 *
252 * Load the first thread to start a processor.
253 */
254 void
255 slave_main(void)
256 {
257 processor_t processor = current_processor();
258 thread_t thread;
259
260 /*
261 * Use the idle processor thread if there
262 * is no dedicated start up thread.
263 */
264 if (processor->next_thread == THREAD_NULL) {
265 thread = processor->idle_thread;
266 thread->continuation = (thread_continue_t)processor_start_thread;
267 thread->parameter = NULL;
268 }
269 else {
270 thread = processor->next_thread;
271 processor->next_thread = THREAD_NULL;
272 }
273
274 load_context(thread);
275 /*NOTREACHED*/
276 }
277
278 /*
279 * processor_start_thread:
280 *
281 * First thread to execute on a started processor.
282 *
283 * Called at splsched.
284 */
285 void
286 processor_start_thread(void)
287 {
288 processor_t processor = current_processor();
289 thread_t self = current_thread();
290
291 slave_machine_init();
292
293 /*
294 * If running the idle processor thread,
295 * reenter the idle loop, else terminate.
296 */
297 if (self == processor->idle_thread)
298 thread_block((thread_continue_t)idle_thread);
299
300 thread_terminate(self);
301 /*NOTREACHED*/
302 }
303
304 /*
305 * load_context:
306 *
307 * Start the first thread on a processor.
308 */
309 static void
310 load_context(
311 thread_t thread)
312 {
313 processor_t processor = current_processor();
314
315 machine_set_current_thread(thread);
316 processor_up(processor);
317
318 PMAP_ACTIVATE_KERNEL(PROCESSOR_DATA(processor, slot_num));
319
320 /*
321 * Acquire a stack if none attached. The panic
322 * should never occur since the thread is expected
323 * to have reserved stack.
324 */
325 if (!thread->kernel_stack) {
326 if (!stack_alloc_try(thread))
327 panic("load_context");
328 }
329
330 /*
331 * The idle processor threads are not counted as
332 * running for load calculations.
333 */
334 if (!(thread->state & TH_IDLE))
335 pset_run_incr(thread->processor_set);
336
337 processor->active_thread = thread;
338 processor->current_pri = thread->sched_pri;
339 processor->deadline = UINT64_MAX;
340 thread->last_processor = processor;
341
342 processor->last_dispatch = mach_absolute_time();
343 timer_switch((uint32_t)processor->last_dispatch,
344 &PROCESSOR_DATA(processor, offline_timer));
345
346 PMAP_ACTIVATE_USER(thread, PROCESSOR_DATA(processor, slot_num));
347
348 machine_load_context(thread);
349 /*NOTREACHED*/
350 }