]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
xnu-1228.9.59.tar.gz
[apple/xnu.git] / osfmk / kern / startup.c
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 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 * NOTICE: This file was modified by McAfee Research in 2004 to introduce
58 * support for mandatory and extensible security protections. This notice
59 * is included in support of clause 2.2 (b) of the Apple Public License,
60 * Version 2.0.
61 */
62 /*
63 */
64
65 /*
66 * Mach kernel startup.
67 */
68
69 #include <debug.h>
70 #include <xpr_debug.h>
71 #include <mach_kdp.h>
72 #include <norma_vm.h>
73
74 #include <mach/boolean.h>
75 #include <mach/machine.h>
76 #include <mach/thread_act.h>
77 #include <mach/task_special_ports.h>
78 #include <mach/vm_param.h>
79 #include <ipc/ipc_init.h>
80 #include <kern/assert.h>
81 #include <kern/misc_protos.h>
82 #include <kern/clock.h>
83 #include <kern/cpu_number.h>
84 #include <kern/ledger.h>
85 #include <kern/machine.h>
86 #include <kern/processor.h>
87 #include <kern/sched_prim.h>
88 #include <kern/startup.h>
89 #include <kern/task.h>
90 #include <kern/thread.h>
91 #include <kern/timer.h>
92 #include <kern/xpr.h>
93 #include <kern/zalloc.h>
94 #include <kern/locks.h>
95 #include <console/serial_protos.h>
96 #include <vm/vm_kern.h>
97 #include <vm/vm_init.h>
98 #include <vm/vm_map.h>
99 #include <vm/vm_object.h>
100 #include <vm/vm_page.h>
101 #include <vm/vm_pageout.h>
102 #include <vm/vm_shared_region.h>
103 #include <machine/pmap.h>
104 #include <machine/commpage.h>
105 #include <libkern/version.h>
106
107 #if MACH_KDP
108 #include <kdp/kdp.h>
109 #endif
110
111 #if CONFIG_MACF
112 #include <security/mac_mach_internal.h>
113 #endif
114
115 #ifdef __ppc__
116 #include <ppc/Firmware.h>
117 #include <ppc/mappings.h>
118 #endif
119
120 static void kernel_bootstrap_thread(void);
121
122 static void load_context(
123 thread_t thread);
124 #ifdef i386
125 extern void cpu_userwindow_init(int);
126 extern void cpu_physwindow_init(int);
127 #endif
128
129 #ifdef CONFIG_JETTISON_KERNEL_LINKER
130 extern void jettison_kernel_linker(void);
131 #endif
132
133 /*
134 * Running in virtual memory, on the interrupt stack.
135 */
136
137 void
138 kernel_bootstrap(void)
139 {
140 kern_return_t result;
141 thread_t thread;
142
143 printf("%s\n", version); /* log kernel version */
144
145 #define kernel_bootstrap_kprintf(x...) /* kprintf("kernel_bootstrap: " x) */
146
147 kernel_bootstrap_kprintf("calling lck_mod_init\n");
148 lck_mod_init();
149
150 kernel_bootstrap_kprintf("calling sched_init\n");
151 sched_init();
152
153 kernel_bootstrap_kprintf("calling vm_mem_bootstrap\n");
154 vm_mem_bootstrap();
155
156 kernel_bootstrap_kprintf("calling ipc_bootstrap\n");
157 ipc_bootstrap();
158
159 kernel_bootstrap_kprintf("calling vm_mem_init\n");
160 vm_mem_init();
161
162 kernel_bootstrap_kprintf("calling kmod_init\n");
163 kmod_init();
164 #if CONFIG_MACF
165 mac_policy_init();
166 #endif
167 kernel_bootstrap_kprintf("calling ipc_init\n");
168 ipc_init();
169
170 /*
171 * As soon as the virtual memory system is up, we record
172 * that this CPU is using the kernel pmap.
173 */
174 kernel_bootstrap_kprintf("calling PMAP_ACTIVATE_KERNEL\n");
175 PMAP_ACTIVATE_KERNEL(master_cpu);
176
177 kernel_bootstrap_kprintf("calling mapping_free_prime\n");
178 mapping_free_prime(); /* Load up with temporary mapping blocks */
179
180 kernel_bootstrap_kprintf("calling machine_init\n");
181 machine_init();
182
183 kernel_bootstrap_kprintf("calling clock_init\n");
184 clock_init();
185
186 machine_info.memory_size = mem_size;
187 machine_info.max_mem = max_mem;
188 machine_info.major_version = version_major;
189 machine_info.minor_version = version_minor;
190
191 /*
192 * Initialize the IPC, task, and thread subsystems.
193 */
194 kernel_bootstrap_kprintf("calling ledger_init\n");
195 ledger_init();
196
197 kernel_bootstrap_kprintf("calling task_init\n");
198 task_init();
199
200 kernel_bootstrap_kprintf("calling thread_init\n");
201 thread_init();
202
203 /*
204 * Create a kernel thread to execute the kernel bootstrap.
205 */
206 kernel_bootstrap_kprintf("calling kernel_thread_create\n");
207 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
208
209 if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
210
211 thread->state = TH_RUN;
212 thread_deallocate(thread);
213
214 kernel_bootstrap_kprintf("calling load_context - done\n");
215 load_context(thread);
216 /*NOTREACHED*/
217 }
218
219 int kth_started = 0;
220
221 /*
222 * Now running in a thread. Kick off other services,
223 * invoke user bootstrap, enter pageout loop.
224 */
225 static void
226 kernel_bootstrap_thread(void)
227 {
228 processor_t processor = current_processor();
229
230 #define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
231 kernel_bootstrap_thread_kprintf("calling idle_thread_create\n");
232 /*
233 * Create the idle processor thread.
234 */
235 idle_thread_create(processor);
236
237 /*
238 * N.B. Do not stick anything else
239 * before this point.
240 *
241 * Start up the scheduler services.
242 */
243 kernel_bootstrap_thread_kprintf("calling sched_startup\n");
244 sched_startup();
245
246 /*
247 * Remain on current processor as
248 * additional processors come online.
249 */
250 kernel_bootstrap_thread_kprintf("calling thread_bind\n");
251 thread_bind(processor);
252
253 /*
254 * Kick off memory mapping adjustments.
255 */
256 kernel_bootstrap_thread_kprintf("calling mapping_adjust\n");
257 mapping_adjust();
258
259 /*
260 * Create the clock service.
261 */
262 kernel_bootstrap_thread_kprintf("calling clock_service_create\n");
263 clock_service_create();
264
265 /*
266 * Create the device service.
267 */
268 device_service_create();
269
270 kth_started = 1;
271
272 #if MACH_KDP
273 kernel_bootstrap_kprintf("calling kdp_init\n");
274 kdp_init();
275 #endif
276
277 #ifdef i386
278 /*
279 * Create and initialize the physical copy window for processor 0
280 * This is required before starting kicking off IOKit.
281 */
282 cpu_physwindow_init(0);
283 #endif
284
285 #ifdef IOKIT
286 PE_init_iokit();
287 #endif
288
289 (void) spllo(); /* Allow interruptions */
290
291 #ifdef i386
292 /*
293 * Create and initialize the copy window for processor 0
294 * This also allocates window space for all other processors.
295 * However, this is dependent on the number of processors - so this call
296 * must be after IOKit has been started because IOKit performs processor
297 * discovery.
298 */
299 cpu_userwindow_init(0);
300 #endif
301
302 /*
303 * Initialize the shared region module.
304 */
305 vm_shared_region_init();
306 vm_commpage_init();
307
308 #if CONFIG_MACF
309 mac_policy_initmach();
310 #endif
311
312 /*
313 * Start the user bootstrap.
314 */
315 #ifdef MACH_BSD
316 bsd_init();
317 #endif
318
319 #ifdef CONFIG_JETTISON_KERNEL_LINKER
320 /* We do not run kextd, so get rid of the kernel linker now */
321 jettison_kernel_linker();
322 #endif
323
324 serial_keyboard_init(); /* Start serial keyboard if wanted */
325
326 thread_bind(PROCESSOR_NULL);
327
328 /*
329 * Become the pageout daemon.
330 */
331 vm_pageout();
332 /*NOTREACHED*/
333 }
334
335 /*
336 * slave_main:
337 *
338 * Load the first thread to start a processor.
339 */
340 void
341 slave_main(void *machine_param)
342 {
343 processor_t processor = current_processor();
344 thread_t thread;
345
346 /*
347 * Use the idle processor thread if there
348 * is no dedicated start up thread.
349 */
350 if (processor->next_thread == THREAD_NULL) {
351 thread = processor->idle_thread;
352 thread->continuation = (thread_continue_t)processor_start_thread;
353 thread->parameter = machine_param;
354 }
355 else {
356 thread = processor->next_thread;
357 processor->next_thread = THREAD_NULL;
358 }
359
360 load_context(thread);
361 /*NOTREACHED*/
362 }
363
364 /*
365 * processor_start_thread:
366 *
367 * First thread to execute on a started processor.
368 *
369 * Called at splsched.
370 */
371 void
372 processor_start_thread(void *machine_param)
373 {
374 processor_t processor = current_processor();
375 thread_t self = current_thread();
376
377 slave_machine_init(machine_param);
378
379 /*
380 * If running the idle processor thread,
381 * reenter the idle loop, else terminate.
382 */
383 if (self == processor->idle_thread)
384 thread_block((thread_continue_t)idle_thread);
385
386 thread_terminate(self);
387 /*NOTREACHED*/
388 }
389
390 /*
391 * load_context:
392 *
393 * Start the first thread on a processor.
394 */
395 static void
396 load_context(
397 thread_t thread)
398 {
399 processor_t processor = current_processor();
400
401
402 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
403
404 load_context_kprintf("calling machine_set_current_thread\n");
405 machine_set_current_thread(thread);
406
407 load_context_kprintf("calling processor_up\n");
408 processor_up(processor);
409
410 PMAP_ACTIVATE_KERNEL(PROCESSOR_DATA(processor, slot_num));
411
412 /*
413 * Acquire a stack if none attached. The panic
414 * should never occur since the thread is expected
415 * to have reserved stack.
416 */
417 load_context_kprintf("stack %x, stackptr %x\n",
418 thread->kernel_stack, thread->machine.kstackptr);
419 if (!thread->kernel_stack) {
420 load_context_kprintf("calling stack_alloc_try\n");
421 if (!stack_alloc_try(thread))
422 panic("load_context");
423 }
424
425 /*
426 * The idle processor threads are not counted as
427 * running for load calculations.
428 */
429 if (!(thread->state & TH_IDLE))
430 sched_run_incr();
431
432 processor->active_thread = thread;
433 processor->current_pri = thread->sched_pri;
434 processor->deadline = UINT64_MAX;
435 thread->last_processor = processor;
436
437 processor->last_dispatch = mach_absolute_time();
438 timer_start(&thread->system_timer, processor->last_dispatch);
439 PROCESSOR_DATA(processor, thread_timer) = PROCESSOR_DATA(processor, kernel_timer) = &thread->system_timer;
440
441 timer_start(&PROCESSOR_DATA(processor, system_state), processor->last_dispatch);
442 PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, system_state);
443
444 PMAP_ACTIVATE_USER(thread, PROCESSOR_DATA(processor, slot_num));
445
446 load_context_kprintf("calling machine_load_context\n");
447 machine_load_context(thread);
448 /*NOTREACHED*/
449 }