]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/startup.c
xnu-2782.20.48.tar.gz
[apple/xnu.git] / osfmk / kern / startup.c
1 /*
2 * Copyright (c) 2000-2010 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
73 #include <mach/boolean.h>
74 #include <mach/machine.h>
75 #include <mach/thread_act.h>
76 #include <mach/task_special_ports.h>
77 #include <mach/vm_param.h>
78 #include <ipc/ipc_init.h>
79 #include <kern/assert.h>
80 #include <kern/mach_param.h>
81 #include <kern/misc_protos.h>
82 #include <kern/clock.h>
83 #include <kern/coalition.h>
84 #include <kern/cpu_number.h>
85 #include <kern/ledger.h>
86 #include <kern/machine.h>
87 #include <kern/processor.h>
88 #include <kern/sched_prim.h>
89 #include <kern/sfi.h>
90 #include <kern/startup.h>
91 #include <kern/task.h>
92 #include <kern/thread.h>
93 #include <kern/timer.h>
94 #if CONFIG_TELEMETRY
95 #include <kern/telemetry.h>
96 #endif
97 #include <kern/wait_queue.h>
98 #include <kern/xpr.h>
99 #include <kern/zalloc.h>
100 #include <kern/locks.h>
101 #include <prng/random.h>
102 #include <console/serial_protos.h>
103 #include <vm/vm_kern.h>
104 #include <vm/vm_init.h>
105 #include <vm/vm_map.h>
106 #include <vm/vm_object.h>
107 #include <vm/vm_page.h>
108 #include <vm/vm_pageout.h>
109 #include <vm/vm_shared_region.h>
110 #include <machine/pmap.h>
111 #include <machine/commpage.h>
112 #include <libkern/version.h>
113 #include <sys/codesign.h>
114 #include <sys/kdebug.h>
115 #include <sys/random.h>
116
117 #if CONFIG_ATM
118 #include <atm/atm_internal.h>
119 #endif
120
121 #if CONFIG_CSR
122 #include <sys/csr.h>
123 #endif
124
125 #if CONFIG_BANK
126 #include <bank/bank_internal.h>
127 #endif
128
129 #if ALTERNATE_DEBUGGER
130 #include <arm64/alternate_debugger.h>
131 #endif
132
133 #if MACH_KDP
134 #include <kdp/kdp.h>
135 #endif
136
137 #if CONFIG_MACF
138 #include <security/mac_mach_internal.h>
139 #endif
140
141 #if CONFIG_COUNTERS
142 #include <pmc/pmc.h>
143 #endif
144
145 #if KPC
146 #include <kern/kpc.h>
147 #endif
148
149 #if KPERF
150 #include <kperf/kperf.h>
151 #endif
152
153 #if HYPERVISOR
154 #include <kern/hv_support.h>
155 #endif
156
157
158 #include <i386/pmCPU.h>
159 static void kernel_bootstrap_thread(void);
160
161 static void load_context(
162 thread_t thread);
163 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
164 extern void cpu_userwindow_init(int);
165 extern void cpu_physwindow_init(int);
166 #endif
167
168 #if CONFIG_ECC_LOGGING
169 #include <kern/ecc.h>
170 #endif
171
172 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
173 #include <i386/vmx/vmx_cpu.h>
174 #endif
175
176 // libkern/OSKextLib.cpp
177 extern void OSKextRemoveKextBootstrap(void);
178
179 void scale_setup(void);
180 extern void bsd_scale_setup(int);
181 extern unsigned int semaphore_max;
182 extern void stackshot_lock_init(void);
183
184 /*
185 * Running in virtual memory, on the interrupt stack.
186 */
187
188 extern int serverperfmode;
189
190 /* size of kernel trace buffer, disabled by default */
191 unsigned int new_nkdbufs = 0;
192 unsigned int wake_nkdbufs = 0;
193 unsigned int write_trace_on_panic = 0;
194 unsigned int trace_typefilter = 0;
195 boolean_t trace_serial = FALSE;
196
197 /* mach leak logging */
198 int log_leaks = 0;
199 int turn_on_log_leaks = 0;
200
201 static inline void
202 kernel_bootstrap_log(const char *message)
203 {
204 // kprintf("kernel_bootstrap: %s\n", message);
205 kernel_debug_string(message);
206 }
207
208 static inline void
209 kernel_bootstrap_thread_log(const char *message)
210 {
211 // kprintf("kernel_bootstrap_thread: %s\n", message);
212 kernel_debug_string(message);
213 }
214
215 void
216 kernel_early_bootstrap(void)
217 {
218 /* serverperfmode is needed by timer setup */
219 if (PE_parse_boot_argn("serverperfmode", &serverperfmode, sizeof (serverperfmode))) {
220 serverperfmode = 1;
221 }
222
223 lck_mod_init();
224
225 /*
226 * Initialize the timer callout world
227 */
228 timer_call_init();
229
230 /*
231 * Configure SFI classes
232 */
233 sfi_early_init();
234 }
235
236
237 void
238 kernel_bootstrap(void)
239 {
240 kern_return_t result;
241 thread_t thread;
242 char namep[16];
243
244 printf("%s\n", version); /* log kernel version */
245
246 if (PE_parse_boot_argn("-l", namep, sizeof (namep))) /* leaks logging */
247 turn_on_log_leaks = 1;
248
249 PE_parse_boot_argn("trace", &new_nkdbufs, sizeof (new_nkdbufs));
250 PE_parse_boot_argn("trace_wake", &wake_nkdbufs, sizeof (wake_nkdbufs));
251 PE_parse_boot_argn("trace_panic", &write_trace_on_panic, sizeof(write_trace_on_panic));
252 PE_parse_boot_argn("trace_typefilter", &trace_typefilter, sizeof(trace_typefilter));
253
254 scale_setup();
255
256 kernel_bootstrap_log("vm_mem_bootstrap");
257 vm_mem_bootstrap();
258
259 kernel_bootstrap_log("cs_init");
260 cs_init();
261
262 kernel_bootstrap_log("vm_mem_init");
263 vm_mem_init();
264
265 machine_info.memory_size = (uint32_t)mem_size;
266 machine_info.max_mem = max_mem;
267 machine_info.major_version = version_major;
268 machine_info.minor_version = version_minor;
269
270 #if CONFIG_TELEMETRY
271 kernel_bootstrap_log("telemetry_init");
272 telemetry_init();
273 #endif
274
275 kernel_bootstrap_log("stackshot_lock_init");
276 stackshot_lock_init();
277
278 kernel_bootstrap_log("sched_init");
279 sched_init();
280
281 kernel_bootstrap_log("wait_queue_bootstrap");
282 wait_queue_bootstrap();
283
284 kernel_bootstrap_log("ipc_bootstrap");
285 ipc_bootstrap();
286
287 #if CONFIG_MACF
288 mac_policy_init();
289 #endif
290 kernel_bootstrap_log("ipc_init");
291 ipc_init();
292
293 /*
294 * As soon as the virtual memory system is up, we record
295 * that this CPU is using the kernel pmap.
296 */
297 kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL");
298 PMAP_ACTIVATE_KERNEL(master_cpu);
299
300 kernel_bootstrap_log("mapping_free_prime");
301 mapping_free_prime(); /* Load up with temporary mapping blocks */
302
303 kernel_bootstrap_log("machine_init");
304 machine_init();
305
306 kernel_bootstrap_log("clock_init");
307 clock_init();
308
309 ledger_init();
310
311 /*
312 * Initialize the IPC, task, and thread subsystems.
313 */
314 #if CONFIG_COALITIONS
315 kernel_bootstrap_log("coalition_init");
316 coalition_init();
317 #endif
318
319 kernel_bootstrap_log("task_init");
320 task_init();
321
322 kernel_bootstrap_log("thread_init");
323 thread_init();
324
325 #if CONFIG_ATM
326 /* Initialize the Activity Trace Resource Manager. */
327 kernel_bootstrap_log("atm_init");
328 atm_init();
329 #endif
330
331 #if CONFIG_CSR
332 kernel_bootstrap_log("csr_init");
333 csr_init();
334 #endif
335
336 #if CONFIG_BANK
337 /* Initialize the BANK Manager. */
338 kernel_bootstrap_log("bank_init");
339 bank_init();
340 #endif
341
342 /*
343 * Create a kernel thread to execute the kernel bootstrap.
344 */
345 kernel_bootstrap_log("kernel_thread_create");
346 result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
347
348 if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
349
350 thread->state = TH_RUN;
351 thread_deallocate(thread);
352
353 kernel_bootstrap_log("load_context - done");
354 load_context(thread);
355 /*NOTREACHED*/
356 }
357
358 int kth_started = 0;
359
360 vm_offset_t vm_kernel_addrperm;
361 vm_offset_t buf_kernel_addrperm;
362
363 /*
364 * Now running in a thread. Kick off other services,
365 * invoke user bootstrap, enter pageout loop.
366 */
367 static void
368 kernel_bootstrap_thread(void)
369 {
370 processor_t processor = current_processor();
371
372 #define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
373 kernel_bootstrap_thread_log("idle_thread_create");
374 /*
375 * Create the idle processor thread.
376 */
377 idle_thread_create(processor);
378
379 /*
380 * N.B. Do not stick anything else
381 * before this point.
382 *
383 * Start up the scheduler services.
384 */
385 kernel_bootstrap_thread_log("sched_startup");
386 sched_startup();
387
388 /*
389 * Thread lifecycle maintenance (teardown, stack allocation)
390 */
391 kernel_bootstrap_thread_log("thread_daemon_init");
392 thread_daemon_init();
393
394 /* Create kernel map entry reserve */
395 vm_kernel_reserved_entry_init();
396
397 /*
398 * Thread callout service.
399 */
400 kernel_bootstrap_thread_log("thread_call_initialize");
401 thread_call_initialize();
402
403 /*
404 * Remain on current processor as
405 * additional processors come online.
406 */
407 kernel_bootstrap_thread_log("thread_bind");
408 thread_bind(processor);
409
410 /*
411 * Initialize ipc thread call support.
412 */
413 kernel_bootstrap_thread_log("ipc_thread_call_init");
414 ipc_thread_call_init();
415
416 /*
417 * Kick off memory mapping adjustments.
418 */
419 kernel_bootstrap_thread_log("mapping_adjust");
420 mapping_adjust();
421
422 /*
423 * Create the clock service.
424 */
425 kernel_bootstrap_thread_log("clock_service_create");
426 clock_service_create();
427
428 /*
429 * Create the device service.
430 */
431 device_service_create();
432
433 kth_started = 1;
434
435 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
436 /*
437 * Create and initialize the physical copy window for processor 0
438 * This is required before starting kicking off IOKit.
439 */
440 cpu_physwindow_init(0);
441 #endif
442
443
444
445 #if MACH_KDP
446 kernel_bootstrap_log("kdp_init");
447 kdp_init();
448 #endif
449
450 #if ALTERNATE_DEBUGGER
451 alternate_debugger_init();
452 #endif
453
454 #if CONFIG_COUNTERS
455 pmc_bootstrap();
456 #endif
457
458 #if KPC
459 kpc_init();
460 #endif
461
462 #if CONFIG_ECC_LOGGING
463 ecc_log_init();
464 #endif
465
466 #if KPERF
467 kperf_bootstrap();
468 #endif
469
470 #if HYPERVISOR
471 hv_support_init();
472 #endif
473
474 #if CONFIG_TELEMETRY
475 kernel_bootstrap_log("bootprofile_init");
476 bootprofile_init();
477 #endif
478
479 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
480 vmx_init();
481 #endif
482
483 #if (defined(__i386__) || defined(__x86_64__))
484 if (kdebug_serial) {
485 new_nkdbufs = 1;
486 if (trace_typefilter == 0)
487 trace_typefilter = 1;
488 }
489 if (turn_on_log_leaks && !new_nkdbufs)
490 new_nkdbufs = 200000;
491 if (trace_typefilter)
492 start_kern_tracing_with_typefilter(new_nkdbufs,
493 FALSE,
494 trace_typefilter);
495 else
496 start_kern_tracing(new_nkdbufs, FALSE);
497 if (turn_on_log_leaks)
498 log_leaks = 1;
499
500 #endif
501
502 kernel_bootstrap_log("prng_init");
503 prng_cpu_init(master_cpu);
504
505 #ifdef IOKIT
506 PE_init_iokit();
507 #endif
508
509 assert(ml_get_interrupts_enabled() == FALSE);
510 (void) spllo(); /* Allow interruptions */
511
512 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
513 /*
514 * Create and initialize the copy window for processor 0
515 * This also allocates window space for all other processors.
516 * However, this is dependent on the number of processors - so this call
517 * must be after IOKit has been started because IOKit performs processor
518 * discovery.
519 */
520 cpu_userwindow_init(0);
521 #endif
522
523 #if (!defined(__i386__) && !defined(__x86_64__))
524 if (turn_on_log_leaks && !new_nkdbufs)
525 new_nkdbufs = 200000;
526 if (trace_typefilter)
527 start_kern_tracing_with_typefilter(new_nkdbufs, FALSE, trace_typefilter);
528 else
529 start_kern_tracing(new_nkdbufs, FALSE);
530 if (turn_on_log_leaks)
531 log_leaks = 1;
532 #endif
533
534 /*
535 * Initialize the shared region module.
536 */
537 vm_shared_region_init();
538 vm_commpage_init();
539 vm_commpage_text_init();
540
541
542 #if CONFIG_MACF
543 kernel_bootstrap_log("mac_policy_initmach");
544 mac_policy_initmach();
545 #endif
546
547 kernel_bootstrap_log("sfi_init");
548 sfi_init();
549
550 /*
551 * Initialize the global used for permuting kernel
552 * addresses that may be exported to userland as tokens
553 * using VM_KERNEL_ADDRPERM(). Force the random number
554 * to be odd to avoid mapping a non-zero
555 * word-aligned address to zero via addition.
556 * Note: at this stage we can use the cryptographically secure PRNG
557 * rather than early_random().
558 */
559 read_random(&vm_kernel_addrperm, sizeof(vm_kernel_addrperm));
560 vm_kernel_addrperm |= 1;
561 read_random(&buf_kernel_addrperm, sizeof(buf_kernel_addrperm));
562 buf_kernel_addrperm |= 1;
563
564 /*
565 * Start the user bootstrap.
566 */
567 #ifdef MACH_BSD
568 bsd_init();
569 #endif
570
571 /*
572 * Get rid of segments used to bootstrap kext loading. This removes
573 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
574 */
575 OSKextRemoveKextBootstrap();
576
577 serial_keyboard_init(); /* Start serial keyboard if wanted */
578
579 vm_page_init_local_q();
580
581 thread_bind(PROCESSOR_NULL);
582
583 /*
584 * Become the pageout daemon.
585 */
586 vm_pageout();
587 /*NOTREACHED*/
588 }
589
590 /*
591 * slave_main:
592 *
593 * Load the first thread to start a processor.
594 */
595 void
596 slave_main(void *machine_param)
597 {
598 processor_t processor = current_processor();
599 thread_t thread;
600
601 /*
602 * Use the idle processor thread if there
603 * is no dedicated start up thread.
604 */
605 if (processor->next_thread == THREAD_NULL) {
606 thread = processor->idle_thread;
607 thread->continuation = (thread_continue_t)processor_start_thread;
608 thread->parameter = machine_param;
609 }
610 else {
611 thread = processor->next_thread;
612 processor->next_thread = THREAD_NULL;
613 }
614
615 load_context(thread);
616 /*NOTREACHED*/
617 }
618
619 /*
620 * processor_start_thread:
621 *
622 * First thread to execute on a started processor.
623 *
624 * Called at splsched.
625 */
626 void
627 processor_start_thread(void *machine_param)
628 {
629 processor_t processor = current_processor();
630 thread_t self = current_thread();
631
632 slave_machine_init(machine_param);
633
634 /*
635 * If running the idle processor thread,
636 * reenter the idle loop, else terminate.
637 */
638 if (self == processor->idle_thread)
639 thread_block((thread_continue_t)idle_thread);
640
641 thread_terminate(self);
642 /*NOTREACHED*/
643 }
644
645 /*
646 * load_context:
647 *
648 * Start the first thread on a processor.
649 */
650 static void
651 load_context(
652 thread_t thread)
653 {
654 processor_t processor = current_processor();
655
656
657 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
658
659 load_context_kprintf("machine_set_current_thread\n");
660 machine_set_current_thread(thread);
661
662 load_context_kprintf("processor_up\n");
663 processor_up(processor);
664
665 PMAP_ACTIVATE_KERNEL(processor->cpu_id);
666
667 /*
668 * Acquire a stack if none attached. The panic
669 * should never occur since the thread is expected
670 * to have reserved stack.
671 */
672 load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread,
673 thread->kernel_stack, thread->machine.kstackptr);
674 if (!thread->kernel_stack) {
675 load_context_kprintf("stack_alloc_try\n");
676 if (!stack_alloc_try(thread))
677 panic("load_context");
678 }
679
680 /*
681 * The idle processor threads are not counted as
682 * running for load calculations.
683 */
684 if (!(thread->state & TH_IDLE))
685 sched_run_incr(thread);
686
687 processor->active_thread = thread;
688 processor->current_pri = thread->sched_pri;
689 processor->current_thmode = thread->sched_mode;
690 processor->deadline = UINT64_MAX;
691 thread->last_processor = processor;
692
693 processor->last_dispatch = mach_absolute_time();
694 timer_start(&thread->system_timer, processor->last_dispatch);
695 PROCESSOR_DATA(processor, thread_timer) = PROCESSOR_DATA(processor, kernel_timer) = &thread->system_timer;
696
697 timer_start(&PROCESSOR_DATA(processor, system_state), processor->last_dispatch);
698 PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, system_state);
699
700 PMAP_ACTIVATE_USER(thread, processor->cpu_id);
701
702 load_context_kprintf("machine_load_context\n");
703 machine_load_context(thread);
704 /*NOTREACHED*/
705 }
706
707 void
708 scale_setup()
709 {
710 int scale = 0;
711 #if defined(__LP64__)
712 typeof(task_max) task_max_base = task_max;
713
714 /* Raise limits for servers with >= 16G */
715 if ((serverperfmode != 0) && ((uint64_t)sane_size >= (uint64_t)(16 * 1024 * 1024 *1024ULL))) {
716 scale = (int)((uint64_t)sane_size / (uint64_t)(8 * 1024 * 1024 *1024ULL));
717 /* limit to 128 G */
718 if (scale > 16)
719 scale = 16;
720 task_max_base = 2500;
721 } else if ((uint64_t)sane_size >= (uint64_t)(3 * 1024 * 1024 *1024ULL))
722 scale = 2;
723
724 task_max = MAX(task_max, task_max_base * scale);
725
726 if (scale != 0) {
727 task_threadmax = task_max;
728 thread_max = task_max * 5;
729 }
730
731 #endif
732
733 bsd_scale_setup(scale);
734
735 ipc_space_max = SPACE_MAX;
736 ipc_port_max = PORT_MAX;
737 ipc_pset_max = SET_MAX;
738 semaphore_max = SEMAPHORE_MAX;
739 }
740