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