]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/startup.c
xnu-1228.9.59.tar.gz
[apple/xnu.git] / osfmk / kern / startup.c
CommitLineData
1c79356b 1/*
593a1d5f 2 * Copyright (c) 2000-2008 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 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 */
2d21ac55
A
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 */
1c79356b
A
62/*
63 */
64
65/*
66 * Mach kernel startup.
67 */
68
69#include <debug.h>
70#include <xpr_debug.h>
71#include <mach_kdp.h>
1c79356b 72#include <norma_vm.h>
1c79356b
A
73
74#include <mach/boolean.h>
75#include <mach/machine.h>
91447636 76#include <mach/thread_act.h>
1c79356b
A
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>
91447636 84#include <kern/ledger.h>
1c79356b
A
85#include <kern/machine.h>
86#include <kern/processor.h>
87#include <kern/sched_prim.h>
1c79356b
A
88#include <kern/startup.h>
89#include <kern/task.h>
90#include <kern/thread.h>
91#include <kern/timer.h>
1c79356b
A
92#include <kern/xpr.h>
93#include <kern/zalloc.h>
91447636 94#include <kern/locks.h>
0c530ab8 95#include <console/serial_protos.h>
1c79356b
A
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>
2d21ac55 102#include <vm/vm_shared_region.h>
1c79356b 103#include <machine/pmap.h>
43866e37 104#include <machine/commpage.h>
91447636 105#include <libkern/version.h>
1c79356b 106
593a1d5f
A
107#if MACH_KDP
108#include <kdp/kdp.h>
109#endif
110
2d21ac55
A
111#if CONFIG_MACF
112#include <security/mac_mach_internal.h>
113#endif
114
1c79356b
A
115#ifdef __ppc__
116#include <ppc/Firmware.h>
117#include <ppc/mappings.h>
118#endif
119
91447636 120static void kernel_bootstrap_thread(void);
1c79356b 121
91447636
A
122static void load_context(
123 thread_t thread);
0c530ab8 124#ifdef i386
2d21ac55
A
125extern void cpu_userwindow_init(int);
126extern void cpu_physwindow_init(int);
0c530ab8
A
127#endif
128
2d21ac55
A
129#ifdef CONFIG_JETTISON_KERNEL_LINKER
130extern void jettison_kernel_linker(void);
131#endif
0c530ab8 132
1c79356b
A
133/*
134 * Running in virtual memory, on the interrupt stack.
1c79356b 135 */
0c530ab8 136
1c79356b 137void
91447636 138kernel_bootstrap(void)
1c79356b 139{
91447636
A
140 kern_return_t result;
141 thread_t thread;
1c79356b 142
2d21ac55
A
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");
91447636 148 lck_mod_init();
2d21ac55
A
149
150 kernel_bootstrap_kprintf("calling sched_init\n");
1c79356b 151 sched_init();
2d21ac55
A
152
153 kernel_bootstrap_kprintf("calling vm_mem_bootstrap\n");
1c79356b 154 vm_mem_bootstrap();
2d21ac55
A
155
156 kernel_bootstrap_kprintf("calling ipc_bootstrap\n");
1c79356b 157 ipc_bootstrap();
2d21ac55
A
158
159 kernel_bootstrap_kprintf("calling vm_mem_init\n");
1c79356b 160 vm_mem_init();
2d21ac55
A
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");
1c79356b 168 ipc_init();
1c79356b
A
169
170 /*
171 * As soon as the virtual memory system is up, we record
172 * that this CPU is using the kernel pmap.
173 */
2d21ac55 174 kernel_bootstrap_kprintf("calling PMAP_ACTIVATE_KERNEL\n");
1c79356b
A
175 PMAP_ACTIVATE_KERNEL(master_cpu);
176
2d21ac55 177 kernel_bootstrap_kprintf("calling mapping_free_prime\n");
1c79356b 178 mapping_free_prime(); /* Load up with temporary mapping blocks */
1c79356b 179
2d21ac55 180 kernel_bootstrap_kprintf("calling machine_init\n");
1c79356b 181 machine_init();
2d21ac55
A
182
183 kernel_bootstrap_kprintf("calling clock_init\n");
1c79356b
A
184 clock_init();
185
1c79356b 186 machine_info.memory_size = mem_size;
91447636
A
187 machine_info.max_mem = max_mem;
188 machine_info.major_version = version_major;
189 machine_info.minor_version = version_minor;
1c79356b
A
190
191 /*
192 * Initialize the IPC, task, and thread subsystems.
193 */
2d21ac55 194 kernel_bootstrap_kprintf("calling ledger_init\n");
1c79356b 195 ledger_init();
2d21ac55
A
196
197 kernel_bootstrap_kprintf("calling task_init\n");
1c79356b 198 task_init();
2d21ac55
A
199
200 kernel_bootstrap_kprintf("calling thread_init\n");
1c79356b 201 thread_init();
1c79356b
A
202
203 /*
91447636 204 * Create a kernel thread to execute the kernel bootstrap.
1c79356b 205 */
2d21ac55 206 kernel_bootstrap_kprintf("calling kernel_thread_create\n");
91447636 207 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
0c530ab8
A
208
209 if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
9bccf70c 210
91447636
A
211 thread->state = TH_RUN;
212 thread_deallocate(thread);
55e303ae 213
2d21ac55 214 kernel_bootstrap_kprintf("calling load_context - done\n");
91447636 215 load_context(thread);
1c79356b 216 /*NOTREACHED*/
1c79356b
A
217}
218
2d21ac55
A
219int kth_started = 0;
220
1c79356b 221/*
91447636
A
222 * Now running in a thread. Kick off other services,
223 * invoke user bootstrap, enter pageout loop.
1c79356b 224 */
91447636
A
225static void
226kernel_bootstrap_thread(void)
1c79356b 227{
91447636 228 processor_t processor = current_processor();
1c79356b 229
2d21ac55
A
230#define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
231 kernel_bootstrap_thread_kprintf("calling idle_thread_create\n");
1c79356b 232 /*
91447636 233 * Create the idle processor thread.
1c79356b 234 */
91447636 235 idle_thread_create(processor);
0b4e3aa0
A
236
237 /*
91447636
A
238 * N.B. Do not stick anything else
239 * before this point.
240 *
241 * Start up the scheduler services.
0b4e3aa0 242 */
2d21ac55 243 kernel_bootstrap_thread_kprintf("calling sched_startup\n");
91447636 244 sched_startup();
0b4e3aa0 245
1c79356b 246 /*
91447636
A
247 * Remain on current processor as
248 * additional processors come online.
1c79356b 249 */
2d21ac55
A
250 kernel_bootstrap_thread_kprintf("calling thread_bind\n");
251 thread_bind(processor);
1c79356b
A
252
253 /*
91447636 254 * Kick off memory mapping adjustments.
1c79356b 255 */
2d21ac55 256 kernel_bootstrap_thread_kprintf("calling mapping_adjust\n");
1c79356b 257 mapping_adjust();
1c79356b 258
1c79356b
A
259 /*
260 * Create the clock service.
261 */
2d21ac55 262 kernel_bootstrap_thread_kprintf("calling clock_service_create\n");
1c79356b
A
263 clock_service_create();
264
265 /*
266 * Create the device service.
267 */
268 device_service_create();
269
2d21ac55
A
270 kth_started = 1;
271
593a1d5f
A
272#if MACH_KDP
273 kernel_bootstrap_kprintf("calling kdp_init\n");
274 kdp_init();
275#endif
276
2d21ac55
A
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
1c79356b
A
284
285#ifdef IOKIT
2d21ac55 286 PE_init_iokit();
1c79356b 287#endif
43866e37
A
288
289 (void) spllo(); /* Allow interruptions */
290
2d21ac55 291#ifdef i386
0c530ab8 292 /*
2d21ac55
A
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.
0c530ab8 298 */
2d21ac55
A
299 cpu_userwindow_init(0);
300#endif
0c530ab8 301
0c530ab8 302 /*
2d21ac55 303 * Initialize the shared region module.
0c530ab8 304 */
2d21ac55
A
305 vm_shared_region_init();
306 vm_commpage_init();
307
308#if CONFIG_MACF
309 mac_policy_initmach();
0c530ab8 310#endif
1c79356b
A
311
312 /*
313 * Start the user bootstrap.
314 */
1c79356b 315#ifdef MACH_BSD
2d21ac55
A
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();
1c79356b
A
322#endif
323
55e303ae 324 serial_keyboard_init(); /* Start serial keyboard if wanted */
55e303ae 325
2d21ac55 326 thread_bind(PROCESSOR_NULL);
1c79356b
A
327
328 /*
329 * Become the pageout daemon.
330 */
1c79356b
A
331 vm_pageout();
332 /*NOTREACHED*/
333}
334
91447636
A
335/*
336 * slave_main:
337 *
338 * Load the first thread to start a processor.
339 */
1c79356b 340void
593a1d5f 341slave_main(void *machine_param)
1c79356b 342{
91447636 343 processor_t processor = current_processor();
1c79356b
A
344 thread_t thread;
345
91447636
A
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;
593a1d5f 353 thread->parameter = machine_param;
91447636
A
354 }
355 else {
356 thread = processor->next_thread;
357 processor->next_thread = THREAD_NULL;
1c79356b 358 }
9bccf70c 359
91447636 360 load_context(thread);
1c79356b 361 /*NOTREACHED*/
1c79356b
A
362}
363
364/*
91447636
A
365 * processor_start_thread:
366 *
367 * First thread to execute on a started processor.
368 *
369 * Called at splsched.
1c79356b
A
370 */
371void
593a1d5f 372processor_start_thread(void *machine_param)
1c79356b 373{
91447636
A
374 processor_t processor = current_processor();
375 thread_t self = current_thread();
376
593a1d5f 377 slave_machine_init(machine_param);
1c79356b 378
91447636
A
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*/
1c79356b
A
388}
389
390/*
91447636
A
391 * load_context:
392 *
393 * Start the first thread on a processor.
1c79356b 394 */
91447636
A
395static void
396load_context(
1c79356b
A
397 thread_t thread)
398{
91447636 399 processor_t processor = current_processor();
1c79356b 400
2d21ac55
A
401
402#define load_context_kprintf(x...) /* kprintf("load_context: " x) */
403
404 load_context_kprintf("calling machine_set_current_thread\n");
91447636 405 machine_set_current_thread(thread);
2d21ac55
A
406
407 load_context_kprintf("calling processor_up\n");
91447636 408 processor_up(processor);
1c79356b 409
91447636
A
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 */
2d21ac55 417 load_context_kprintf("stack %x, stackptr %x\n",
593a1d5f 418 thread->kernel_stack, thread->machine.kstackptr);
91447636 419 if (!thread->kernel_stack) {
2d21ac55 420 load_context_kprintf("calling stack_alloc_try\n");
91447636
A
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))
2d21ac55 430 sched_run_incr();
1c79356b 431
55e303ae 432 processor->active_thread = thread;
9bccf70c 433 processor->current_pri = thread->sched_pri;
91447636
A
434 processor->deadline = UINT64_MAX;
435 thread->last_processor = processor;
436
437 processor->last_dispatch = mach_absolute_time();
2d21ac55
A
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);
1c79356b 443
91447636 444 PMAP_ACTIVATE_USER(thread, PROCESSOR_DATA(processor, slot_num));
1c79356b 445
2d21ac55 446 load_context_kprintf("calling machine_load_context\n");
55e303ae 447 machine_load_context(thread);
1c79356b
A
448 /*NOTREACHED*/
449}