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