]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/startup.c
xnu-1504.7.4.tar.gz
[apple/xnu.git] / osfmk / kern / startup.c
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2009 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>
b0d623f7 81#include <kern/mach_param.h>
1c79356b
A
82#include <kern/misc_protos.h>
83#include <kern/clock.h>
84#include <kern/cpu_number.h>
91447636 85#include <kern/ledger.h>
1c79356b
A
86#include <kern/machine.h>
87#include <kern/processor.h>
88#include <kern/sched_prim.h>
1c79356b
A
89#include <kern/startup.h>
90#include <kern/task.h>
91#include <kern/thread.h>
92#include <kern/timer.h>
b0d623f7 93#include <kern/wait_queue.h>
1c79356b
A
94#include <kern/xpr.h>
95#include <kern/zalloc.h>
91447636 96#include <kern/locks.h>
0c530ab8 97#include <console/serial_protos.h>
1c79356b
A
98#include <vm/vm_kern.h>
99#include <vm/vm_init.h>
100#include <vm/vm_map.h>
101#include <vm/vm_object.h>
102#include <vm/vm_page.h>
103#include <vm/vm_pageout.h>
2d21ac55 104#include <vm/vm_shared_region.h>
1c79356b 105#include <machine/pmap.h>
43866e37 106#include <machine/commpage.h>
91447636 107#include <libkern/version.h>
1c79356b 108
593a1d5f
A
109#if MACH_KDP
110#include <kdp/kdp.h>
111#endif
112
2d21ac55
A
113#if CONFIG_MACF
114#include <security/mac_mach_internal.h>
115#endif
116
b0d623f7
A
117#if CONFIG_COUNTERS
118#include <pmc/pmc.h>
119#endif
120
1c79356b
A
121#ifdef __ppc__
122#include <ppc/Firmware.h>
123#include <ppc/mappings.h>
124#endif
125
91447636 126static void kernel_bootstrap_thread(void);
1c79356b 127
91447636
A
128static void load_context(
129 thread_t thread);
b0d623f7 130#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
2d21ac55
A
131extern void cpu_userwindow_init(int);
132extern void cpu_physwindow_init(int);
0c530ab8
A
133#endif
134
b0d623f7
A
135// libkern/OSKextLib.cpp
136extern void OSKextRemoveKextBootstrap(void);
137
138void srv_setup(void);
139extern void bsd_srv_setup(int);
140extern unsigned int semaphore_max;
141
0c530ab8 142
1c79356b
A
143/*
144 * Running in virtual memory, on the interrupt stack.
1c79356b 145 */
0c530ab8 146
b0d623f7
A
147extern int srv;
148
1c79356b 149void
91447636 150kernel_bootstrap(void)
1c79356b 151{
91447636
A
152 kern_return_t result;
153 thread_t thread;
1c79356b 154
2d21ac55
A
155 printf("%s\n", version); /* log kernel version */
156
157#define kernel_bootstrap_kprintf(x...) /* kprintf("kernel_bootstrap: " x) */
158
b0d623f7
A
159 /* i386_vm_init already checks for this ; do it aagin anyway */
160 if (PE_parse_boot_argn("srv", &srv, sizeof (srv))) {
161 srv = 1;
162 }
163
164 srv_setup();
165
2d21ac55 166 kernel_bootstrap_kprintf("calling lck_mod_init\n");
91447636 167 lck_mod_init();
2d21ac55 168
b0d623f7
A
169 kernel_bootstrap_kprintf("calling vm_mem_bootstrap\n");
170 vm_mem_bootstrap();
171
172 kernel_bootstrap_kprintf("calling vm_mem_init\n");
173 vm_mem_init();
174
175 machine_info.memory_size = (uint32_t)mem_size;
176 machine_info.max_mem = max_mem;
177 machine_info.major_version = version_major;
178 machine_info.minor_version = version_minor;
179
2d21ac55 180 kernel_bootstrap_kprintf("calling sched_init\n");
1c79356b 181 sched_init();
2d21ac55 182
b0d623f7
A
183 kernel_bootstrap_kprintf("calling wait_queue_bootstrap\n");
184 wait_queue_bootstrap();
2d21ac55
A
185
186 kernel_bootstrap_kprintf("calling ipc_bootstrap\n");
1c79356b 187 ipc_bootstrap();
2d21ac55 188
2d21ac55
A
189#if CONFIG_MACF
190 mac_policy_init();
191#endif
192 kernel_bootstrap_kprintf("calling ipc_init\n");
1c79356b 193 ipc_init();
1c79356b
A
194
195 /*
196 * As soon as the virtual memory system is up, we record
197 * that this CPU is using the kernel pmap.
198 */
2d21ac55 199 kernel_bootstrap_kprintf("calling PMAP_ACTIVATE_KERNEL\n");
1c79356b
A
200 PMAP_ACTIVATE_KERNEL(master_cpu);
201
2d21ac55 202 kernel_bootstrap_kprintf("calling mapping_free_prime\n");
1c79356b 203 mapping_free_prime(); /* Load up with temporary mapping blocks */
1c79356b 204
2d21ac55 205 kernel_bootstrap_kprintf("calling machine_init\n");
1c79356b 206 machine_init();
2d21ac55
A
207
208 kernel_bootstrap_kprintf("calling clock_init\n");
1c79356b
A
209 clock_init();
210
1c79356b
A
211
212 /*
213 * Initialize the IPC, task, and thread subsystems.
214 */
2d21ac55 215 kernel_bootstrap_kprintf("calling ledger_init\n");
1c79356b 216 ledger_init();
2d21ac55
A
217
218 kernel_bootstrap_kprintf("calling task_init\n");
1c79356b 219 task_init();
2d21ac55
A
220
221 kernel_bootstrap_kprintf("calling thread_init\n");
1c79356b 222 thread_init();
1c79356b
A
223
224 /*
91447636 225 * Create a kernel thread to execute the kernel bootstrap.
1c79356b 226 */
2d21ac55 227 kernel_bootstrap_kprintf("calling kernel_thread_create\n");
91447636 228 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
0c530ab8
A
229
230 if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
9bccf70c 231
91447636
A
232 thread->state = TH_RUN;
233 thread_deallocate(thread);
55e303ae 234
2d21ac55 235 kernel_bootstrap_kprintf("calling load_context - done\n");
91447636 236 load_context(thread);
1c79356b 237 /*NOTREACHED*/
1c79356b
A
238}
239
2d21ac55
A
240int kth_started = 0;
241
1c79356b 242/*
91447636
A
243 * Now running in a thread. Kick off other services,
244 * invoke user bootstrap, enter pageout loop.
1c79356b 245 */
91447636
A
246static void
247kernel_bootstrap_thread(void)
1c79356b 248{
91447636 249 processor_t processor = current_processor();
1c79356b 250
2d21ac55
A
251#define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
252 kernel_bootstrap_thread_kprintf("calling idle_thread_create\n");
1c79356b 253 /*
91447636 254 * Create the idle processor thread.
1c79356b 255 */
91447636 256 idle_thread_create(processor);
0b4e3aa0
A
257
258 /*
91447636
A
259 * N.B. Do not stick anything else
260 * before this point.
261 *
262 * Start up the scheduler services.
0b4e3aa0 263 */
2d21ac55 264 kernel_bootstrap_thread_kprintf("calling sched_startup\n");
91447636 265 sched_startup();
0b4e3aa0 266
1c79356b 267 /*
91447636
A
268 * Remain on current processor as
269 * additional processors come online.
1c79356b 270 */
2d21ac55
A
271 kernel_bootstrap_thread_kprintf("calling thread_bind\n");
272 thread_bind(processor);
1c79356b
A
273
274 /*
91447636 275 * Kick off memory mapping adjustments.
1c79356b 276 */
2d21ac55 277 kernel_bootstrap_thread_kprintf("calling mapping_adjust\n");
1c79356b 278 mapping_adjust();
1c79356b 279
1c79356b
A
280 /*
281 * Create the clock service.
282 */
2d21ac55 283 kernel_bootstrap_thread_kprintf("calling clock_service_create\n");
1c79356b
A
284 clock_service_create();
285
286 /*
287 * Create the device service.
288 */
289 device_service_create();
290
2d21ac55
A
291 kth_started = 1;
292
593a1d5f
A
293#if MACH_KDP
294 kernel_bootstrap_kprintf("calling kdp_init\n");
295 kdp_init();
296#endif
297
b0d623f7 298#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
2d21ac55
A
299 /*
300 * Create and initialize the physical copy window for processor 0
301 * This is required before starting kicking off IOKit.
302 */
303 cpu_physwindow_init(0);
304#endif
1c79356b 305
b0d623f7
A
306#if CONFIG_COUNTERS
307 pmc_bootstrap();
308#endif
309
1c79356b 310#ifdef IOKIT
2d21ac55 311 PE_init_iokit();
1c79356b 312#endif
43866e37
A
313
314 (void) spllo(); /* Allow interruptions */
315
b0d623f7 316#if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
0c530ab8 317 /*
2d21ac55
A
318 * Create and initialize the copy window for processor 0
319 * This also allocates window space for all other processors.
320 * However, this is dependent on the number of processors - so this call
321 * must be after IOKit has been started because IOKit performs processor
322 * discovery.
0c530ab8 323 */
2d21ac55
A
324 cpu_userwindow_init(0);
325#endif
0c530ab8 326
0c530ab8 327 /*
2d21ac55 328 * Initialize the shared region module.
0c530ab8 329 */
2d21ac55
A
330 vm_shared_region_init();
331 vm_commpage_init();
332
333#if CONFIG_MACF
334 mac_policy_initmach();
0c530ab8 335#endif
1c79356b
A
336
337 /*
338 * Start the user bootstrap.
339 */
1c79356b 340#ifdef MACH_BSD
2d21ac55
A
341 bsd_init();
342#endif
343
b0d623f7
A
344 /*
345 * Get rid of segments used to bootstrap kext loading. This removes
346 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
347 */
348 OSKextRemoveKextBootstrap();
1c79356b 349
55e303ae 350 serial_keyboard_init(); /* Start serial keyboard if wanted */
55e303ae 351
b0d623f7
A
352 vm_page_init_local_q();
353
2d21ac55 354 thread_bind(PROCESSOR_NULL);
1c79356b
A
355
356 /*
357 * Become the pageout daemon.
358 */
1c79356b
A
359 vm_pageout();
360 /*NOTREACHED*/
361}
362
91447636
A
363/*
364 * slave_main:
365 *
366 * Load the first thread to start a processor.
367 */
1c79356b 368void
593a1d5f 369slave_main(void *machine_param)
1c79356b 370{
91447636 371 processor_t processor = current_processor();
1c79356b
A
372 thread_t thread;
373
91447636
A
374 /*
375 * Use the idle processor thread if there
376 * is no dedicated start up thread.
377 */
378 if (processor->next_thread == THREAD_NULL) {
379 thread = processor->idle_thread;
380 thread->continuation = (thread_continue_t)processor_start_thread;
593a1d5f 381 thread->parameter = machine_param;
91447636
A
382 }
383 else {
384 thread = processor->next_thread;
385 processor->next_thread = THREAD_NULL;
1c79356b 386 }
9bccf70c 387
91447636 388 load_context(thread);
1c79356b 389 /*NOTREACHED*/
1c79356b
A
390}
391
392/*
91447636
A
393 * processor_start_thread:
394 *
395 * First thread to execute on a started processor.
396 *
397 * Called at splsched.
1c79356b
A
398 */
399void
593a1d5f 400processor_start_thread(void *machine_param)
1c79356b 401{
91447636
A
402 processor_t processor = current_processor();
403 thread_t self = current_thread();
404
593a1d5f 405 slave_machine_init(machine_param);
1c79356b 406
91447636
A
407 /*
408 * If running the idle processor thread,
409 * reenter the idle loop, else terminate.
410 */
411 if (self == processor->idle_thread)
412 thread_block((thread_continue_t)idle_thread);
413
414 thread_terminate(self);
415 /*NOTREACHED*/
1c79356b
A
416}
417
418/*
91447636
A
419 * load_context:
420 *
421 * Start the first thread on a processor.
1c79356b 422 */
91447636
A
423static void
424load_context(
1c79356b
A
425 thread_t thread)
426{
91447636 427 processor_t processor = current_processor();
1c79356b 428
2d21ac55
A
429
430#define load_context_kprintf(x...) /* kprintf("load_context: " x) */
431
432 load_context_kprintf("calling machine_set_current_thread\n");
91447636 433 machine_set_current_thread(thread);
2d21ac55
A
434
435 load_context_kprintf("calling processor_up\n");
91447636 436 processor_up(processor);
1c79356b 437
b0d623f7 438 PMAP_ACTIVATE_KERNEL(processor->cpu_id);
91447636
A
439
440 /*
441 * Acquire a stack if none attached. The panic
442 * should never occur since the thread is expected
443 * to have reserved stack.
444 */
2d21ac55 445 load_context_kprintf("stack %x, stackptr %x\n",
593a1d5f 446 thread->kernel_stack, thread->machine.kstackptr);
91447636 447 if (!thread->kernel_stack) {
2d21ac55 448 load_context_kprintf("calling stack_alloc_try\n");
91447636
A
449 if (!stack_alloc_try(thread))
450 panic("load_context");
451 }
452
453 /*
454 * The idle processor threads are not counted as
455 * running for load calculations.
456 */
457 if (!(thread->state & TH_IDLE))
2d21ac55 458 sched_run_incr();
1c79356b 459
55e303ae 460 processor->active_thread = thread;
9bccf70c 461 processor->current_pri = thread->sched_pri;
91447636
A
462 processor->deadline = UINT64_MAX;
463 thread->last_processor = processor;
464
465 processor->last_dispatch = mach_absolute_time();
2d21ac55
A
466 timer_start(&thread->system_timer, processor->last_dispatch);
467 PROCESSOR_DATA(processor, thread_timer) = PROCESSOR_DATA(processor, kernel_timer) = &thread->system_timer;
468
469 timer_start(&PROCESSOR_DATA(processor, system_state), processor->last_dispatch);
470 PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, system_state);
1c79356b 471
b0d623f7 472 PMAP_ACTIVATE_USER(thread, processor->cpu_id);
1c79356b 473
2d21ac55 474 load_context_kprintf("calling machine_load_context\n");
55e303ae 475 machine_load_context(thread);
1c79356b
A
476 /*NOTREACHED*/
477}
b0d623f7
A
478
479void
480srv_setup()
481{
482 int scale = 0;
483#if defined(__LP64__)
484 /* if memory is more than 16G, then apply rules for processes */
485 if ((srv != 0) && ((uint64_t)sane_size >= (uint64_t)(16 * 1024 * 1024 *1024ULL))) {
486 scale = (int)((uint64_t)sane_size / (uint64_t)(8 * 1024 * 1024 *1024ULL));
487 /* limit to 128 G */
488 if (scale > 16)
489 scale = 16;
490 task_max = 2500 * scale;
491 task_threadmax = task_max;
492 thread_max = task_max * 5;
493 } else
494 scale = 0;
495#endif
496 bsd_srv_setup(scale);
497
498 ipc_space_max = SPACE_MAX;
499 ipc_tree_entry_max = ITE_MAX;
500 ipc_port_max = PORT_MAX;
501 ipc_pset_max = SET_MAX;
502 semaphore_max = SEMAPHORE_MAX;
503}
504