]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
dc9a6a92a28afc24d8e9cc2766264020d8915631
[apple/xnu.git] / osfmk / kern / startup.c
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 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 /*
54 * Mach kernel startup.
55 */
56
57 #include <debug.h>
58 #include <xpr_debug.h>
59 #include <mach_kdp.h>
60 #include <mach_host.h>
61 #include <norma_vm.h>
62
63 #include <mach/boolean.h>
64 #include <mach/machine.h>
65 #include <mach/thread_act.h>
66 #include <mach/task_special_ports.h>
67 #include <mach/vm_param.h>
68 #include <ipc/ipc_init.h>
69 #include <kern/assert.h>
70 #include <kern/misc_protos.h>
71 #include <kern/clock.h>
72 #include <kern/cpu_number.h>
73 #include <kern/ledger.h>
74 #include <kern/machine.h>
75 #include <kern/processor.h>
76 #include <kern/sched_prim.h>
77 #include <kern/startup.h>
78 #include <kern/task.h>
79 #include <kern/thread.h>
80 #include <kern/timer.h>
81 #include <kern/xpr.h>
82 #include <kern/zalloc.h>
83 #include <kern/locks.h>
84 #include <console/serial_protos.h>
85 #include <vm/vm_shared_memory_server.h>
86 #include <vm/vm_kern.h>
87 #include <vm/vm_init.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_pageout.h>
92 #include <machine/pmap.h>
93 #include <machine/commpage.h>
94 #include <libkern/version.h>
95
96 #ifdef __ppc__
97 #include <ppc/Firmware.h>
98 #include <ppc/mappings.h>
99 #endif
100
101 static void kernel_bootstrap_thread(void);
102
103 static void load_context(
104 thread_t thread);
105
106 #ifdef i386
107 extern void cpu_window_init(int);
108 #endif
109
110
111 /*
112 * Running in virtual memory, on the interrupt stack.
113 */
114
115 void
116 kernel_bootstrap(void)
117 {
118 kern_return_t result;
119 thread_t thread;
120
121 lck_mod_init();
122 sched_init();
123 vm_mem_bootstrap();
124 ipc_bootstrap();
125 vm_mem_init();
126 ipc_init();
127
128 /*
129 * As soon as the virtual memory system is up, we record
130 * that this CPU is using the kernel pmap.
131 */
132 PMAP_ACTIVATE_KERNEL(master_cpu);
133
134 mapping_free_prime(); /* Load up with temporary mapping blocks */
135
136 machine_init();
137 kmod_init();
138 clock_init();
139
140 machine_info.memory_size = mem_size;
141 machine_info.max_mem = max_mem;
142 machine_info.major_version = version_major;
143 machine_info.minor_version = version_minor;
144
145 /*
146 * Initialize the IPC, task, and thread subsystems.
147 */
148 ledger_init();
149 task_init();
150 thread_init();
151
152 /*
153 * Create a kernel thread to execute the kernel bootstrap.
154 */
155 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
156
157 if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
158
159 thread->state = TH_RUN;
160 thread_deallocate(thread);
161
162 load_context(thread);
163 /*NOTREACHED*/
164 }
165
166 /*
167 * Now running in a thread. Kick off other services,
168 * invoke user bootstrap, enter pageout loop.
169 */
170 static void
171 kernel_bootstrap_thread(void)
172 {
173 processor_t processor = current_processor();
174 thread_t self = current_thread();
175
176 /*
177 * Create the idle processor thread.
178 */
179 idle_thread_create(processor);
180
181 /*
182 * N.B. Do not stick anything else
183 * before this point.
184 *
185 * Start up the scheduler services.
186 */
187 sched_startup();
188
189 /*
190 * Remain on current processor as
191 * additional processors come online.
192 */
193 thread_bind(self, processor);
194
195 /*
196 * Kick off memory mapping adjustments.
197 */
198 mapping_adjust();
199
200 /*
201 * Create the clock service.
202 */
203 clock_service_create();
204
205 /*
206 * Create the device service.
207 */
208 device_service_create();
209
210 shared_file_boot_time_init(ENV_DEFAULT_ROOT, cpu_type());
211
212 #ifdef IOKIT
213 {
214 PE_init_iokit();
215 }
216 #endif
217
218 (void) spllo(); /* Allow interruptions */
219
220 /*
221 * Fill in the comm area (mapped into every task address space.)
222 */
223 commpage_populate();
224
225 #ifdef i386
226 /*
227 * create and initialize a copy window
228 * for processor 0
229 */
230 cpu_window_init(0);
231 #endif
232
233 /*
234 * Start the user bootstrap.
235 */
236 #ifdef MACH_BSD
237 {
238 bsd_init();
239 }
240 #endif
241
242 serial_keyboard_init(); /* Start serial keyboard if wanted */
243
244 thread_bind(self, PROCESSOR_NULL);
245
246 /*
247 * Become the pageout daemon.
248 */
249 vm_pageout();
250 /*NOTREACHED*/
251 }
252
253 /*
254 * slave_main:
255 *
256 * Load the first thread to start a processor.
257 */
258 void
259 slave_main(void)
260 {
261 processor_t processor = current_processor();
262 thread_t thread;
263
264 /*
265 * Use the idle processor thread if there
266 * is no dedicated start up thread.
267 */
268 if (processor->next_thread == THREAD_NULL) {
269 thread = processor->idle_thread;
270 thread->continuation = (thread_continue_t)processor_start_thread;
271 thread->parameter = NULL;
272 }
273 else {
274 thread = processor->next_thread;
275 processor->next_thread = THREAD_NULL;
276 }
277
278 load_context(thread);
279 /*NOTREACHED*/
280 }
281
282 /*
283 * processor_start_thread:
284 *
285 * First thread to execute on a started processor.
286 *
287 * Called at splsched.
288 */
289 void
290 processor_start_thread(void)
291 {
292 processor_t processor = current_processor();
293 thread_t self = current_thread();
294
295 slave_machine_init();
296
297 /*
298 * If running the idle processor thread,
299 * reenter the idle loop, else terminate.
300 */
301 if (self == processor->idle_thread)
302 thread_block((thread_continue_t)idle_thread);
303
304 thread_terminate(self);
305 /*NOTREACHED*/
306 }
307
308 /*
309 * load_context:
310 *
311 * Start the first thread on a processor.
312 */
313 static void
314 load_context(
315 thread_t thread)
316 {
317 processor_t processor = current_processor();
318
319 machine_set_current_thread(thread);
320 processor_up(processor);
321
322 PMAP_ACTIVATE_KERNEL(PROCESSOR_DATA(processor, slot_num));
323
324 /*
325 * Acquire a stack if none attached. The panic
326 * should never occur since the thread is expected
327 * to have reserved stack.
328 */
329 if (!thread->kernel_stack) {
330 if (!stack_alloc_try(thread))
331 panic("load_context");
332 }
333
334 /*
335 * The idle processor threads are not counted as
336 * running for load calculations.
337 */
338 if (!(thread->state & TH_IDLE))
339 pset_run_incr(thread->processor_set);
340
341 processor->active_thread = thread;
342 processor->current_pri = thread->sched_pri;
343 processor->deadline = UINT64_MAX;
344 thread->last_processor = processor;
345
346 processor->last_dispatch = mach_absolute_time();
347 timer_switch((uint32_t)processor->last_dispatch,
348 &PROCESSOR_DATA(processor, offline_timer));
349
350 PMAP_ACTIVATE_USER(thread, PROCESSOR_DATA(processor, slot_num));
351
352 machine_load_context(thread);
353 /*NOTREACHED*/
354 }