2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
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,
66 * Mach kernel startup.
70 #include <xpr_debug.h>
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>
92 #include <kern/startup.h>
93 #include <kern/task.h>
94 #include <kern/thread.h>
95 #include <kern/timer.h>
97 #include <kern/telemetry.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>
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 #include <mach/host_info.h>
129 #include <atm/atm_internal.h>
136 #include <bank/bank_internal.h>
138 #if ALTERNATE_DEBUGGER
139 #include <arm64/alternate_debugger.h>
147 #include <security/mac_mach_internal.h>
149 extern void vnguard_policy_init(void);
154 #include <kern/kpc.h>
158 #include <kern/hv_support.h>
161 #include <san/kasan.h>
163 #if defined(__arm__) || defined(__arm64__)
164 #include <arm/misc_protos.h> // for arm_vm_prot_finalize
167 #include <i386/pmCPU.h>
168 static void kernel_bootstrap_thread(void);
170 static void load_context(
172 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
173 extern void cpu_userwindow_init(int);
174 extern void cpu_physwindow_init(int);
177 #if CONFIG_ECC_LOGGING
178 #include <kern/ecc.h>
181 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
182 #include <i386/vmx/vmx_cpu.h>
185 // libkern/OSKextLib.cpp
186 extern void OSKextRemoveKextBootstrap(void);
188 void scale_setup(void);
189 extern void bsd_scale_setup(int);
190 extern unsigned int semaphore_max
;
191 extern void stackshot_init(void);
192 extern void ktrace_init(void);
193 extern void oslog_init(void);
196 * Running in virtual memory, on the interrupt stack.
199 extern int serverperfmode
;
201 /* size of kernel trace buffer, disabled by default */
202 unsigned int new_nkdbufs
= 0;
203 unsigned int wake_nkdbufs
= 0;
204 unsigned int write_trace_on_panic
= 0;
205 static char trace_typefilter
[64] = { 0 };
206 unsigned int trace_wrap
= 0;
207 boolean_t trace_serial
= FALSE
;
208 boolean_t early_boot_complete
= FALSE
;
210 /* mach leak logging */
214 kernel_bootstrap_log(const char *message
)
216 // kprintf("kernel_bootstrap: %s\n", message);
217 kernel_debug_string_early(message
);
221 kernel_bootstrap_thread_log(const char *message
)
223 // kprintf("kernel_bootstrap_thread: %s\n", message);
224 kernel_debug_string_early(message
);
228 kernel_early_bootstrap(void)
230 /* serverperfmode is needed by timer setup */
231 if (PE_parse_boot_argn("serverperfmode", &serverperfmode
, sizeof (serverperfmode
))) {
238 * Initialize the timer callout world
244 * Configure SFI classes
250 extern boolean_t
IORamDiskBSDRoot(void);
251 extern kern_return_t
cpm_preallocate_early(void);
254 kernel_bootstrap(void)
256 kern_return_t result
;
260 printf("%s\n", version
); /* log kernel version */
262 if (PE_parse_boot_argn("-l", namep
, sizeof (namep
))) /* leaks logging */
265 PE_parse_boot_argn("trace", &new_nkdbufs
, sizeof (new_nkdbufs
));
266 PE_parse_boot_argn("trace_wake", &wake_nkdbufs
, sizeof (wake_nkdbufs
));
267 PE_parse_boot_argn("trace_panic", &write_trace_on_panic
, sizeof(write_trace_on_panic
));
268 PE_parse_boot_arg_str("trace_typefilter", trace_typefilter
, sizeof(trace_typefilter
));
269 PE_parse_boot_argn("trace_wrap", &trace_wrap
, sizeof(trace_wrap
));
273 kernel_bootstrap_log("vm_mem_bootstrap");
276 kernel_bootstrap_log("cs_init");
279 kernel_bootstrap_log("vm_mem_init");
282 machine_info
.memory_size
= (uint32_t)mem_size
;
283 machine_info
.max_mem
= max_mem
;
284 machine_info
.major_version
= version_major
;
285 machine_info
.minor_version
= version_minor
;
290 kernel_bootstrap_log("kasan_late_init");
295 kernel_bootstrap_log("telemetry_init");
300 kernel_bootstrap_log("csr_init");
304 if (PE_i_can_has_debugger(NULL
) &&
305 PE_parse_boot_argn("-show_pointers", &namep
, sizeof (namep
))) {
306 doprnt_hide_pointers
= FALSE
;
309 kernel_bootstrap_log("console_init");
312 kernel_bootstrap_log("stackshot_init");
315 kernel_bootstrap_log("sched_init");
318 kernel_bootstrap_log("ltable_bootstrap");
321 kernel_bootstrap_log("waitq_bootstrap");
324 kernel_bootstrap_log("ipc_bootstrap");
328 kernel_bootstrap_log("mac_policy_init");
332 kernel_bootstrap_log("ipc_init");
336 * As soon as the virtual memory system is up, we record
337 * that this CPU is using the kernel pmap.
339 kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL");
340 PMAP_ACTIVATE_KERNEL(master_cpu
);
342 kernel_bootstrap_log("mapping_free_prime");
343 mapping_free_prime(); /* Load up with temporary mapping blocks */
345 kernel_bootstrap_log("machine_init");
348 kernel_bootstrap_log("clock_init");
354 * Initialize the IPC, task, and thread subsystems.
357 #if CONFIG_COALITIONS
358 kernel_bootstrap_log("coalitions_init");
362 kernel_bootstrap_log("task_init");
365 kernel_bootstrap_log("thread_init");
369 /* Initialize the Activity Trace Resource Manager. */
370 kernel_bootstrap_log("atm_init");
373 kernel_bootstrap_log("mach_init_activity_id");
374 mach_init_activity_id();
376 /* Initialize the BANK Manager. */
377 kernel_bootstrap_log("bank_init");
380 kernel_bootstrap_log("ipc_pthread_priority_init");
381 ipc_pthread_priority_init();
383 /* initialize the corpse config based on boot-args */
386 /* initialize host_statistics */
387 host_statistics_init();
390 * Create a kernel thread to execute the kernel bootstrap.
392 kernel_bootstrap_log("kernel_thread_create");
393 result
= kernel_thread_create((thread_continue_t
)kernel_bootstrap_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
395 if (result
!= KERN_SUCCESS
) panic("kernel_bootstrap: result = %08X\n", result
);
397 thread
->state
= TH_RUN
;
398 thread
->last_made_runnable_time
= mach_absolute_time();
399 thread_deallocate(thread
);
401 kernel_bootstrap_log("load_context - done");
402 load_context(thread
);
408 vm_offset_t vm_kernel_addrperm
;
409 vm_offset_t buf_kernel_addrperm
;
410 vm_offset_t vm_kernel_addrperm_ext
;
411 uint64_t vm_kernel_addrhash_salt
;
412 uint64_t vm_kernel_addrhash_salt_ext
;
415 * Now running in a thread. Kick off other services,
416 * invoke user bootstrap, enter pageout loop.
419 kernel_bootstrap_thread(void)
421 processor_t processor
= current_processor();
423 #define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
424 kernel_bootstrap_thread_log("idle_thread_create");
426 * Create the idle processor thread.
428 idle_thread_create(processor
);
431 * N.B. Do not stick anything else
434 * Start up the scheduler services.
436 kernel_bootstrap_thread_log("sched_startup");
440 * Thread lifecycle maintenance (teardown, stack allocation)
442 kernel_bootstrap_thread_log("thread_daemon_init");
443 thread_daemon_init();
445 /* Create kernel map entry reserve */
446 vm_kernel_reserved_entry_init();
449 * Thread callout service.
451 kernel_bootstrap_thread_log("thread_call_initialize");
452 thread_call_initialize();
455 * Remain on current processor as
456 * additional processors come online.
458 kernel_bootstrap_thread_log("thread_bind");
459 thread_bind(processor
);
462 if (IORamDiskBSDRoot()) {
463 cpm_preallocate_early();
465 #endif /* __arm64__ */
468 * Initialize ipc thread call support.
470 kernel_bootstrap_thread_log("ipc_thread_call_init");
471 ipc_thread_call_init();
474 * Kick off memory mapping adjustments.
476 kernel_bootstrap_thread_log("mapping_adjust");
480 * Create the clock service.
482 kernel_bootstrap_thread_log("clock_service_create");
483 clock_service_create();
486 * Create the device service.
488 device_service_create();
492 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
494 * Create and initialize the physical copy window for processor 0
495 * This is required before starting kicking off IOKit.
497 cpu_physwindow_init(0);
503 kernel_bootstrap_log("kdp_init");
507 #if ALTERNATE_DEBUGGER
508 alternate_debugger_init();
515 #if CONFIG_ECC_LOGGING
524 kernel_bootstrap_log("bootprofile_init");
528 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
532 kernel_bootstrap_thread_log("ktrace_init");
535 kdebug_init(new_nkdbufs
, trace_typefilter
, trace_wrap
);
537 kernel_bootstrap_log("prng_init");
538 prng_cpu_init(master_cpu
);
541 kernel_bootstrap_log("bsd_early_init");
545 #if defined(__arm64__)
550 kernel_bootstrap_log("PE_init_iokit");
554 assert(ml_get_interrupts_enabled() == FALSE
);
557 * Past this point, kernel subsystems that expect to operate with
558 * interrupts or preemption enabled may begin enforcement.
560 early_boot_complete
= TRUE
;
562 #if INTERRUPT_MASKED_DEBUG
563 // Reset interrupts masked timeout before we enable interrupts
564 ml_spin_debug_clear_self();
566 (void) spllo(); /* Allow interruptions */
568 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
570 * Create and initialize the copy window for processor 0
571 * This also allocates window space for all other processors.
572 * However, this is dependent on the number of processors - so this call
573 * must be after IOKit has been started because IOKit performs processor
576 cpu_userwindow_init(0);
580 * Initialize the shared region module.
582 vm_shared_region_init();
584 vm_commpage_text_init();
587 kernel_bootstrap_log("mac_policy_initmach");
588 mac_policy_initmach();
590 vnguard_policy_init();
594 #if defined(__arm__) || defined(__arm64__)
595 #if CONFIG_KERNEL_INTEGRITY
596 machine_lockdown_preflight();
599 * Finalize protections on statically mapped pages now that comm page mapping is established.
601 arm_vm_prot_finalize(PE_state
.bootArgs
);
605 kernel_bootstrap_log("sfi_init");
610 * Initialize the globals used for permuting kernel
611 * addresses that may be exported to userland as tokens
612 * using VM_KERNEL_ADDRPERM()/VM_KERNEL_ADDRPERM_EXTERNAL().
613 * Force the random number to be odd to avoid mapping a non-zero
614 * word-aligned address to zero via addition.
615 * Note: at this stage we can use the cryptographically secure PRNG
616 * rather than early_random().
618 read_random(&vm_kernel_addrperm
, sizeof(vm_kernel_addrperm
));
619 vm_kernel_addrperm
|= 1;
620 read_random(&buf_kernel_addrperm
, sizeof(buf_kernel_addrperm
));
621 buf_kernel_addrperm
|= 1;
622 read_random(&vm_kernel_addrperm_ext
, sizeof(vm_kernel_addrperm_ext
));
623 vm_kernel_addrperm_ext
|= 1;
624 read_random(&vm_kernel_addrhash_salt
, sizeof(vm_kernel_addrhash_salt
));
625 read_random(&vm_kernel_addrhash_salt_ext
, sizeof(vm_kernel_addrhash_salt_ext
));
627 vm_set_restrictions();
633 * Start the user bootstrap.
640 * Get rid of segments used to bootstrap kext loading. This removes
641 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
643 OSKextRemoveKextBootstrap();
646 * Get rid of pages used for early boot tracing.
648 kdebug_free_early_buf();
650 serial_keyboard_init(); /* Start serial keyboard if wanted */
652 vm_page_init_local_q();
654 thread_bind(PROCESSOR_NULL
);
657 * Become the pageout daemon.
666 * Load the first thread to start a processor.
669 slave_main(void *machine_param
)
671 processor_t processor
= current_processor();
675 * Use the idle processor thread if there
676 * is no dedicated start up thread.
678 if (processor
->next_thread
== THREAD_NULL
) {
679 thread
= processor
->idle_thread
;
680 thread
->continuation
= (thread_continue_t
)processor_start_thread
;
681 thread
->parameter
= machine_param
;
684 thread
= processor
->next_thread
;
685 processor
->next_thread
= THREAD_NULL
;
688 load_context(thread
);
693 * processor_start_thread:
695 * First thread to execute on a started processor.
697 * Called at splsched.
700 processor_start_thread(void *machine_param
)
702 processor_t processor
= current_processor();
703 thread_t self
= current_thread();
705 slave_machine_init(machine_param
);
708 * If running the idle processor thread,
709 * reenter the idle loop, else terminate.
711 if (self
== processor
->idle_thread
)
712 thread_block((thread_continue_t
)idle_thread
);
714 thread_terminate(self
);
721 * Start the first thread on a processor.
723 static void __attribute__((noreturn
))
727 processor_t processor
= current_processor();
730 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
732 load_context_kprintf("machine_set_current_thread\n");
733 machine_set_current_thread(thread
);
735 load_context_kprintf("processor_up\n");
736 processor_up(processor
);
738 PMAP_ACTIVATE_KERNEL(processor
->cpu_id
);
741 * Acquire a stack if none attached. The panic
742 * should never occur since the thread is expected
743 * to have reserved stack.
745 load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread
,
746 thread
->kernel_stack
, thread
->machine
.kstackptr
);
747 if (!thread
->kernel_stack
) {
748 load_context_kprintf("stack_alloc_try\n");
749 if (!stack_alloc_try(thread
))
750 panic("load_context");
754 * The idle processor threads are not counted as
755 * running for load calculations.
757 if (!(thread
->state
& TH_IDLE
))
758 sched_run_incr(thread
);
760 processor
->active_thread
= thread
;
761 processor_state_update_explicit(processor
, thread
->sched_pri
,
762 SFI_CLASS_KERNEL
, PSET_SMP
, thread_get_perfcontrol_class(thread
));
763 processor
->starting_pri
= thread
->sched_pri
;
764 processor
->deadline
= UINT64_MAX
;
765 thread
->last_processor
= processor
;
767 processor
->last_dispatch
= mach_absolute_time();
768 timer_start(&thread
->system_timer
, processor
->last_dispatch
);
769 PROCESSOR_DATA(processor
, thread_timer
) = PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
771 timer_start(&PROCESSOR_DATA(processor
, system_state
), processor
->last_dispatch
);
772 PROCESSOR_DATA(processor
, current_state
) = &PROCESSOR_DATA(processor
, system_state
);
775 PMAP_ACTIVATE_USER(thread
, processor
->cpu_id
);
777 load_context_kprintf("machine_load_context\n");
778 machine_load_context(thread
);
786 #if defined(__LP64__)
787 typeof(task_max
) task_max_base
= task_max
;
789 /* Raise limits for servers with >= 16G */
790 if ((serverperfmode
!= 0) && ((uint64_t)sane_size
>= (uint64_t)(16 * 1024 * 1024 *1024ULL))) {
791 scale
= (int)((uint64_t)sane_size
/ (uint64_t)(8 * 1024 * 1024 *1024ULL));
795 task_max_base
= 2500;
796 /* Raise limits for machines with >= 3GB */
797 } else if ((uint64_t)sane_size
>= (uint64_t)(3 * 1024 * 1024 *1024ULL)) {
798 if ((uint64_t)sane_size
< (uint64_t)(8 * 1024 * 1024 *1024ULL)) {
802 scale
= MIN(16, (int)((uint64_t)sane_size
/ (uint64_t)(4 * 1024 * 1024 *1024ULL)));
806 task_max
= MAX(task_max
, task_max_base
* scale
);
809 task_threadmax
= task_max
;
810 thread_max
= task_max
* 5;
815 bsd_scale_setup(scale
);
817 ipc_space_max
= SPACE_MAX
;
818 ipc_port_max
= PORT_MAX
;
819 ipc_pset_max
= SET_MAX
;
820 semaphore_max
= SEMAPHORE_MAX
;