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/cpu_quiesce.h>
86 #include <kern/ledger.h>
87 #include <kern/machine.h>
88 #include <kern/processor.h>
89 #include <kern/sched_prim.h>
90 #include <kern/turnstile.h>
94 #include <kern/startup.h>
95 #include <kern/task.h>
96 #include <kern/thread.h>
97 #include <kern/timer.h>
99 #include <kern/telemetry.h>
101 #include <kern/xpr.h>
102 #include <kern/zalloc.h>
103 #include <kern/locks.h>
104 #include <kern/debug.h>
105 #include <corpses/task_corpse.h>
106 #include <prng/random.h>
107 #include <console/serial_protos.h>
108 #include <vm/vm_kern.h>
109 #include <vm/vm_init.h>
110 #include <vm/vm_map.h>
111 #include <vm/vm_object.h>
112 #include <vm/vm_page.h>
113 #include <vm/vm_pageout.h>
114 #include <vm/vm_shared_region.h>
115 #include <machine/pmap.h>
116 #include <machine/commpage.h>
117 #include <libkern/version.h>
118 #include <sys/codesign.h>
119 #include <sys/kdebug.h>
120 #include <sys/random.h>
121 #include <sys/ktrace.h>
122 #include <libkern/section_keywords.h>
124 #include <kern/ltable.h>
125 #include <kern/waitq.h>
126 #include <ipc/ipc_voucher.h>
127 #include <voucher/ipc_pthread_priority_internal.h>
128 #include <mach/host_info.h>
129 #include <pthread/workqueue_internal.h>
132 #include <tests/ktest.h>
133 #include <tests/xnupost.h>
137 #include <atm/atm_internal.h>
144 #include <bank/bank_internal.h>
146 #if ALTERNATE_DEBUGGER
147 #include <arm64/alternate_debugger.h>
155 #include <security/mac_mach_internal.h>
157 extern void vnguard_policy_init(void);
162 #include <kern/kpc.h>
166 #include <kern/hv_support.h>
169 #include <san/kasan.h>
171 #if defined(__arm__) || defined(__arm64__)
172 #include <arm/misc_protos.h> // for arm_vm_prot_finalize
175 #include <i386/pmCPU.h>
176 static void kernel_bootstrap_thread(void);
178 static void load_context(
180 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
181 extern void cpu_userwindow_init(int);
182 extern void cpu_physwindow_init(int);
185 #if CONFIG_ECC_LOGGING
186 #include <kern/ecc.h>
189 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
190 #include <i386/vmx/vmx_cpu.h>
194 extern void dtrace_early_init(void);
195 extern void sdt_early_init(void);
198 // libkern/OSKextLib.cpp
199 extern void OSKextRemoveKextBootstrap(void);
201 void scale_setup(void);
202 extern void bsd_scale_setup(int);
203 extern unsigned int semaphore_max
;
204 extern void stackshot_init(void);
205 extern void ktrace_init(void);
206 extern void oslog_init(void);
209 * Running in virtual memory, on the interrupt stack.
212 extern int serverperfmode
;
214 /* size of kernel trace buffer, disabled by default */
215 unsigned int new_nkdbufs
= 0;
216 unsigned int wake_nkdbufs
= 0;
217 unsigned int write_trace_on_panic
= 0;
218 static char trace_typefilter
[64] = { 0 };
219 unsigned int trace_wrap
= 0;
220 boolean_t trace_serial
= FALSE
;
221 boolean_t early_boot_complete
= FALSE
;
223 /* physically contiguous carveouts */
224 SECURITY_READ_ONLY_LATE(uintptr_t) phys_carveout
= 0;
225 SECURITY_READ_ONLY_LATE(uintptr_t) phys_carveout_pa
= 0;
226 SECURITY_READ_ONLY_LATE(size_t) phys_carveout_size
= 0;
228 /* mach leak logging */
232 kernel_bootstrap_log(const char *message
)
234 // kprintf("kernel_bootstrap: %s\n", message);
235 kernel_debug_string_early(message
);
239 kernel_bootstrap_thread_log(const char *message
)
241 // kprintf("kernel_bootstrap_thread: %s\n", message);
242 kernel_debug_string_early(message
);
246 kernel_early_bootstrap(void)
248 /* serverperfmode is needed by timer setup */
249 if (PE_parse_boot_argn("serverperfmode", &serverperfmode
, sizeof (serverperfmode
))) {
256 * Initialize the timer callout world
262 * Configure SFI classes
268 extern boolean_t
IORamDiskBSDRoot(void);
269 extern kern_return_t
cpm_preallocate_early(void);
272 kernel_bootstrap(void)
274 kern_return_t result
;
278 printf("%s\n", version
); /* log kernel version */
280 if (PE_parse_boot_argn("-l", namep
, sizeof (namep
))) /* leaks logging */
283 PE_parse_boot_argn("trace", &new_nkdbufs
, sizeof (new_nkdbufs
));
284 PE_parse_boot_argn("trace_wake", &wake_nkdbufs
, sizeof (wake_nkdbufs
));
285 PE_parse_boot_argn("trace_panic", &write_trace_on_panic
, sizeof(write_trace_on_panic
));
286 PE_parse_boot_arg_str("trace_typefilter", trace_typefilter
, sizeof(trace_typefilter
));
287 PE_parse_boot_argn("trace_wrap", &trace_wrap
, sizeof(trace_wrap
));
291 kernel_bootstrap_log("vm_mem_bootstrap");
294 kernel_bootstrap_log("cs_init");
297 kernel_bootstrap_log("vm_mem_init");
300 machine_info
.memory_size
= (uint32_t)mem_size
;
301 machine_info
.max_mem
= max_mem
;
302 machine_info
.major_version
= version_major
;
303 machine_info
.minor_version
= version_minor
;
308 kernel_bootstrap_log("kasan_late_init");
313 kernel_bootstrap_log("telemetry_init");
318 kernel_bootstrap_log("csr_init");
322 if (PE_i_can_has_debugger(NULL
)) {
323 if (PE_parse_boot_argn("-show_pointers", &namep
, sizeof(namep
))) {
324 doprnt_hide_pointers
= FALSE
;
326 if (PE_parse_boot_argn("-no_slto_panic", &namep
, sizeof(namep
))) {
327 extern boolean_t spinlock_timeout_panic
;
328 spinlock_timeout_panic
= FALSE
;
332 kernel_bootstrap_log("console_init");
335 kernel_bootstrap_log("stackshot_init");
338 kernel_bootstrap_log("sched_init");
341 kernel_bootstrap_log("ltable_bootstrap");
344 kernel_bootstrap_log("waitq_bootstrap");
347 kernel_bootstrap_log("ipc_bootstrap");
351 kernel_bootstrap_log("mac_policy_init");
355 kernel_bootstrap_log("ipc_init");
359 * As soon as the virtual memory system is up, we record
360 * that this CPU is using the kernel pmap.
362 kernel_bootstrap_log("PMAP_ACTIVATE_KERNEL");
363 PMAP_ACTIVATE_KERNEL(master_cpu
);
365 kernel_bootstrap_log("mapping_free_prime");
366 mapping_free_prime(); /* Load up with temporary mapping blocks */
368 kernel_bootstrap_log("machine_init");
371 kernel_bootstrap_log("clock_init");
377 * Initialize the IPC, task, and thread subsystems.
380 #if CONFIG_COALITIONS
381 kernel_bootstrap_log("coalitions_init");
385 kernel_bootstrap_log("task_init");
388 kernel_bootstrap_log("thread_init");
391 kernel_bootstrap_log("workq_init");
394 kernel_bootstrap_log("turnstiles_init");
398 /* Initialize the Activity Trace Resource Manager. */
399 kernel_bootstrap_log("atm_init");
402 kernel_bootstrap_log("mach_init_activity_id");
403 mach_init_activity_id();
405 /* Initialize the BANK Manager. */
406 kernel_bootstrap_log("bank_init");
409 kernel_bootstrap_log("ipc_pthread_priority_init");
410 ipc_pthread_priority_init();
412 /* initialize the corpse config based on boot-args */
415 /* initialize host_statistics */
416 host_statistics_init();
419 * Create a kernel thread to execute the kernel bootstrap.
421 kernel_bootstrap_log("kernel_thread_create");
422 result
= kernel_thread_create((thread_continue_t
)kernel_bootstrap_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
424 if (result
!= KERN_SUCCESS
) panic("kernel_bootstrap: result = %08X\n", result
);
426 thread
->state
= TH_RUN
;
427 thread
->last_made_runnable_time
= mach_absolute_time();
428 thread_deallocate(thread
);
430 kernel_bootstrap_log("load_context - done");
431 load_context(thread
);
437 vm_offset_t vm_kernel_addrperm
;
438 vm_offset_t buf_kernel_addrperm
;
439 vm_offset_t vm_kernel_addrperm_ext
;
440 uint64_t vm_kernel_addrhash_salt
;
441 uint64_t vm_kernel_addrhash_salt_ext
;
444 * Now running in a thread. Kick off other services,
445 * invoke user bootstrap, enter pageout loop.
448 kernel_bootstrap_thread(void)
450 processor_t processor
= current_processor();
452 #define kernel_bootstrap_thread_kprintf(x...) /* kprintf("kernel_bootstrap_thread: " x) */
453 kernel_bootstrap_thread_log("idle_thread_create");
455 * Create the idle processor thread.
457 idle_thread_create(processor
);
460 * N.B. Do not stick anything else
463 * Start up the scheduler services.
465 kernel_bootstrap_thread_log("sched_startup");
469 * Thread lifecycle maintenance (teardown, stack allocation)
471 kernel_bootstrap_thread_log("thread_daemon_init");
472 thread_daemon_init();
474 /* Create kernel map entry reserve */
475 vm_kernel_reserved_entry_init();
478 * Thread callout service.
480 kernel_bootstrap_thread_log("thread_call_initialize");
481 thread_call_initialize();
484 * Remain on current processor as
485 * additional processors come online.
487 kernel_bootstrap_thread_log("thread_bind");
488 thread_bind(processor
);
491 if (IORamDiskBSDRoot()) {
492 cpm_preallocate_early();
494 #endif /* __arm64__ */
497 * Initialize ipc thread call support.
499 kernel_bootstrap_thread_log("ipc_thread_call_init");
500 ipc_thread_call_init();
503 * Kick off memory mapping adjustments.
505 kernel_bootstrap_thread_log("mapping_adjust");
509 * Create the clock service.
511 kernel_bootstrap_thread_log("clock_service_create");
512 clock_service_create();
515 * Create the device service.
517 device_service_create();
521 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
523 * Create and initialize the physical copy window for processor 0
524 * This is required before starting kicking off IOKit.
526 cpu_physwindow_init(0);
529 if (PE_i_can_has_debugger(NULL
)) {
530 unsigned int phys_carveout_mb
= 0;
531 if (PE_parse_boot_argn("phys_carveout_mb", &phys_carveout_mb
,
532 sizeof(phys_carveout_mb
)) && phys_carveout_mb
> 0) {
533 phys_carveout_size
= phys_carveout_mb
* 1024 * 1024;
534 kern_return_t kr
= kmem_alloc_contig(kernel_map
,
535 (vm_offset_t
*)&phys_carveout
, phys_carveout_size
,
536 VM_MAP_PAGE_MASK(kernel_map
), 0, 0, KMA_NOPAGEWAIT
,
537 VM_KERN_MEMORY_DIAG
);
538 if (kr
!= KERN_SUCCESS
) {
539 kprintf("failed to allocate %uMB for phys_carveout_mb: %u\n",
540 phys_carveout_mb
, (unsigned int)kr
);
542 phys_carveout_pa
= kvtophys((vm_offset_t
)phys_carveout
);
548 kernel_bootstrap_log("kdp_init");
552 #if ALTERNATE_DEBUGGER
553 alternate_debugger_init();
560 #if CONFIG_ECC_LOGGING
569 kernel_bootstrap_log("bootprofile_init");
573 #if (defined(__i386__) || defined(__x86_64__)) && CONFIG_VMX
577 kernel_bootstrap_thread_log("ktrace_init");
580 kdebug_init(new_nkdbufs
, trace_typefilter
, trace_wrap
);
583 kernel_bootstrap_log("bsd_early_init");
587 #if defined(__arm64__)
592 kernel_bootstrap_log("PE_init_iokit");
596 assert(ml_get_interrupts_enabled() == FALSE
);
599 * Past this point, kernel subsystems that expect to operate with
600 * interrupts or preemption enabled may begin enforcement.
602 early_boot_complete
= TRUE
;
604 #if INTERRUPT_MASKED_DEBUG
605 // Reset interrupts masked timeout before we enable interrupts
606 ml_spin_debug_clear_self();
608 (void) spllo(); /* Allow interruptions */
610 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
612 * Create and initialize the copy window for processor 0
613 * This also allocates window space for all other processors.
614 * However, this is dependent on the number of processors - so this call
615 * must be after IOKit has been started because IOKit performs processor
618 cpu_userwindow_init(0);
622 * Initialize the shared region module.
624 vm_shared_region_init();
626 vm_commpage_text_init();
629 kernel_bootstrap_log("mac_policy_initmach");
630 mac_policy_initmach();
632 vnguard_policy_init();
643 * Get rid of segments used to bootstrap kext loading. This removes
644 * the KLD, PRELINK symtab, LINKEDIT, and symtab segments/load commands.
645 * Must be done prior to lockdown so that we can free (and possibly relocate)
646 * the static KVA mappings used for the jettisoned bootstrap segments.
648 OSKextRemoveKextBootstrap();
649 #if defined(__arm__) || defined(__arm64__)
650 #if CONFIG_KERNEL_INTEGRITY
651 machine_lockdown_preflight();
654 * Finalize protections on statically mapped pages now that comm page mapping is established.
656 arm_vm_prot_finalize(PE_state
.bootArgs
);
660 kernel_bootstrap_log("sfi_init");
665 * Initialize the globals used for permuting kernel
666 * addresses that may be exported to userland as tokens
667 * using VM_KERNEL_ADDRPERM()/VM_KERNEL_ADDRPERM_EXTERNAL().
668 * Force the random number to be odd to avoid mapping a non-zero
669 * word-aligned address to zero via addition.
670 * Note: at this stage we can use the cryptographically secure PRNG
671 * rather than early_random().
673 read_random(&vm_kernel_addrperm
, sizeof(vm_kernel_addrperm
));
674 vm_kernel_addrperm
|= 1;
675 read_random(&buf_kernel_addrperm
, sizeof(buf_kernel_addrperm
));
676 buf_kernel_addrperm
|= 1;
677 read_random(&vm_kernel_addrperm_ext
, sizeof(vm_kernel_addrperm_ext
));
678 vm_kernel_addrperm_ext
|= 1;
679 read_random(&vm_kernel_addrhash_salt
, sizeof(vm_kernel_addrhash_salt
));
680 read_random(&vm_kernel_addrhash_salt_ext
, sizeof(vm_kernel_addrhash_salt_ext
));
682 vm_set_restrictions();
685 #ifdef CONFIG_XNUPOST
686 kern_return_t result
= kernel_list_tests();
687 result
= kernel_do_post();
688 if (result
!= KERN_SUCCESS
) {
689 panic("kernel_do_post: Tests failed with result = 0x%08x\n", result
);
691 kernel_bootstrap_log("kernel_do_post - done");
692 #endif /* CONFIG_XNUPOST */
696 * Start the user bootstrap.
704 * Get rid of pages used for early boot tracing.
706 kdebug_free_early_buf();
708 serial_keyboard_init(); /* Start serial keyboard if wanted */
710 vm_page_init_local_q();
712 thread_bind(PROCESSOR_NULL
);
715 * Become the pageout daemon.
724 * Load the first thread to start a processor.
727 slave_main(void *machine_param
)
729 processor_t processor
= current_processor();
733 * Use the idle processor thread if there
734 * is no dedicated start up thread.
736 if (processor
->next_thread
== THREAD_NULL
) {
737 thread
= processor
->idle_thread
;
738 thread
->continuation
= (thread_continue_t
)processor_start_thread
;
739 thread
->parameter
= machine_param
;
742 thread
= processor
->next_thread
;
743 processor
->next_thread
= THREAD_NULL
;
746 load_context(thread
);
751 * processor_start_thread:
753 * First thread to execute on a started processor.
755 * Called at splsched.
758 processor_start_thread(void *machine_param
)
760 processor_t processor
= current_processor();
761 thread_t self
= current_thread();
763 slave_machine_init(machine_param
);
766 * If running the idle processor thread,
767 * reenter the idle loop, else terminate.
769 if (self
== processor
->idle_thread
)
770 thread_block((thread_continue_t
)idle_thread
);
772 thread_terminate(self
);
779 * Start the first thread on a processor.
781 static void __attribute__((noreturn
))
785 processor_t processor
= current_processor();
788 #define load_context_kprintf(x...) /* kprintf("load_context: " x) */
790 load_context_kprintf("machine_set_current_thread\n");
791 machine_set_current_thread(thread
);
793 load_context_kprintf("processor_up\n");
794 processor_up(processor
);
796 PMAP_ACTIVATE_KERNEL(processor
->cpu_id
);
799 * Acquire a stack if none attached. The panic
800 * should never occur since the thread is expected
801 * to have reserved stack.
803 load_context_kprintf("thread %p, stack %lx, stackptr %lx\n", thread
,
804 thread
->kernel_stack
, thread
->machine
.kstackptr
);
805 if (!thread
->kernel_stack
) {
806 load_context_kprintf("stack_alloc_try\n");
807 if (!stack_alloc_try(thread
))
808 panic("load_context");
812 * The idle processor threads are not counted as
813 * running for load calculations.
815 if (!(thread
->state
& TH_IDLE
))
816 sched_run_incr(thread
);
818 processor
->active_thread
= thread
;
819 processor_state_update_explicit(processor
, thread
->sched_pri
,
820 SFI_CLASS_KERNEL
, PSET_SMP
, thread_get_perfcontrol_class(thread
));
821 processor
->starting_pri
= thread
->sched_pri
;
822 processor
->deadline
= UINT64_MAX
;
823 thread
->last_processor
= processor
;
825 processor
->last_dispatch
= mach_absolute_time();
826 timer_start(&thread
->system_timer
, processor
->last_dispatch
);
827 PROCESSOR_DATA(processor
, thread_timer
) = PROCESSOR_DATA(processor
, kernel_timer
) = &thread
->system_timer
;
829 timer_start(&PROCESSOR_DATA(processor
, system_state
), processor
->last_dispatch
);
830 PROCESSOR_DATA(processor
, current_state
) = &PROCESSOR_DATA(processor
, system_state
);
833 cpu_quiescent_counter_join(processor
->last_dispatch
);
835 PMAP_ACTIVATE_USER(thread
, processor
->cpu_id
);
837 load_context_kprintf("machine_load_context\n");
838 machine_load_context(thread
);
846 #if defined(__LP64__)
847 typeof(task_max
) task_max_base
= task_max
;
849 /* Raise limits for servers with >= 16G */
850 if ((serverperfmode
!= 0) && ((uint64_t)sane_size
>= (uint64_t)(16 * 1024 * 1024 *1024ULL))) {
851 scale
= (int)((uint64_t)sane_size
/ (uint64_t)(8 * 1024 * 1024 *1024ULL));
855 task_max_base
= 2500;
856 /* Raise limits for machines with >= 3GB */
857 } else if ((uint64_t)sane_size
>= (uint64_t)(3 * 1024 * 1024 *1024ULL)) {
858 if ((uint64_t)sane_size
< (uint64_t)(8 * 1024 * 1024 *1024ULL)) {
862 scale
= MIN(16, (int)((uint64_t)sane_size
/ (uint64_t)(4 * 1024 * 1024 *1024ULL)));
866 task_max
= MAX(task_max
, task_max_base
* scale
);
869 task_threadmax
= task_max
;
870 thread_max
= task_max
* 5;
875 bsd_scale_setup(scale
);
877 ipc_space_max
= SPACE_MAX
;
878 ipc_port_max
= PORT_MAX
;
879 ipc_pset_max
= SET_MAX
;
880 semaphore_max
= SEMAPHORE_MAX
;