]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/startup.c
xnu-4570.71.2.tar.gz
[apple/xnu.git] / osfmk / kern / startup.c
index 6ddd0389c8526a9c1b6acf60efab0e16d3310ff3..c9cfd5167356d5a3f007e7b60ce31dbcf598c865 100644 (file)
@@ -86,7 +86,9 @@
 #include <kern/machine.h>
 #include <kern/processor.h>
 #include <kern/sched_prim.h>
+#if CONFIG_SCHED_SFI
 #include <kern/sfi.h>
+#endif
 #include <kern/startup.h>
 #include <kern/task.h>
 #include <kern/thread.h>
 #if CONFIG_TELEMETRY
 #include <kern/telemetry.h>
 #endif
-#include <kern/wait_queue.h>
 #include <kern/xpr.h>
 #include <kern/zalloc.h>
 #include <kern/locks.h>
+#include <kern/debug.h>
+#include <corpses/task_corpse.h>
 #include <prng/random.h>
 #include <console/serial_protos.h>
 #include <vm/vm_kern.h>
 #include <sys/codesign.h>
 #include <sys/kdebug.h>
 #include <sys/random.h>
+#include <sys/ktrace.h>
+
+#include <kern/ltable.h>
+#include <kern/waitq.h>
+#include <ipc/ipc_voucher.h>
+#include <voucher/ipc_pthread_priority_internal.h>
+#include <mach/host_info.h>
+
 
 #if CONFIG_ATM
 #include <atm/atm_internal.h>
 #include <sys/csr.h>
 #endif
 
-#if CONFIG_BANK
 #include <bank/bank_internal.h>
-#endif
 
 #if ALTERNATE_DEBUGGER
 #include <arm64/alternate_debugger.h>
 
 #if CONFIG_MACF
 #include <security/mac_mach_internal.h>
+#if CONFIG_VNGUARD
+extern void vnguard_policy_init(void);
 #endif
-
-#if CONFIG_COUNTERS
-#include <pmc/pmc.h>
 #endif
 
 #if KPC
 #include <kern/kpc.h>
 #endif
 
-#if KPERF
-#include <kperf/kperf.h>
-#endif
-
 #if HYPERVISOR
 #include <kern/hv_support.h>
 #endif
 
+#include <san/kasan.h>
+
+#if defined(__arm__) || defined(__arm64__)
+#include <arm/misc_protos.h> // for arm_vm_prot_finalize
+#endif
 
 #include <i386/pmCPU.h>
 static void            kernel_bootstrap_thread(void);
@@ -179,7 +188,9 @@ extern void OSKextRemoveKextBootstrap(void);
 void scale_setup(void);
 extern void bsd_scale_setup(int);
 extern unsigned int semaphore_max;
-extern void stackshot_lock_init(void);
+extern void stackshot_init(void);
+extern void ktrace_init(void);
+extern void oslog_init(void);
 
 /*
  *     Running in virtual memory, on the interrupt stack.
@@ -191,25 +202,26 @@ extern int serverperfmode;
 unsigned int new_nkdbufs = 0;
 unsigned int wake_nkdbufs = 0;
 unsigned int write_trace_on_panic = 0;
-unsigned int trace_typefilter = 0;
-boolean_t    trace_serial = FALSE;
+static char trace_typefilter[64] = { 0 };
+unsigned int trace_wrap = 0;
+boolean_t trace_serial = FALSE;
+boolean_t early_boot_complete = FALSE;
 
 /* mach leak logging */
 int log_leaks = 0;
-int turn_on_log_leaks = 0;
 
 static inline void
 kernel_bootstrap_log(const char *message)
 {
 //     kprintf("kernel_bootstrap: %s\n", message);
-       kernel_debug_string(message);
+       kernel_debug_string_early(message);
 }
 
 static inline void
 kernel_bootstrap_thread_log(const char *message)
 {
 //     kprintf("kernel_bootstrap_thread: %s\n", message);
-       kernel_debug_string(message);
+       kernel_debug_string_early(message);
 }
 
 void
@@ -227,12 +239,16 @@ kernel_early_bootstrap(void)
         */
        timer_call_init();
 
+#if CONFIG_SCHED_SFI
        /*
         * Configure SFI classes
         */
        sfi_early_init();
+#endif
 }
 
+extern boolean_t IORamDiskBSDRoot(void);
+extern kern_return_t cpm_preallocate_early(void);
 
 void
 kernel_bootstrap(void)
@@ -244,12 +260,13 @@ kernel_bootstrap(void)
        printf("%s\n", version); /* log kernel version */
 
        if (PE_parse_boot_argn("-l", namep, sizeof (namep))) /* leaks logging */
-               turn_on_log_leaks = 1;
+               log_leaks = 1;
 
        PE_parse_boot_argn("trace", &new_nkdbufs, sizeof (new_nkdbufs));
        PE_parse_boot_argn("trace_wake", &wake_nkdbufs, sizeof (wake_nkdbufs));
        PE_parse_boot_argn("trace_panic", &write_trace_on_panic, sizeof(write_trace_on_panic));
-       PE_parse_boot_argn("trace_typefilter", &trace_typefilter, sizeof(trace_typefilter));
+       PE_parse_boot_arg_str("trace_typefilter", trace_typefilter, sizeof(trace_typefilter));
+       PE_parse_boot_argn("trace_wrap", &trace_wrap, sizeof(trace_wrap));
 
        scale_setup();
 
@@ -267,26 +284,51 @@ kernel_bootstrap(void)
        machine_info.major_version = version_major;
        machine_info.minor_version = version_minor;
 
+       oslog_init();
+
+#if KASAN
+       kernel_bootstrap_log("kasan_late_init");
+       kasan_late_init();
+#endif
+
 #if CONFIG_TELEMETRY
        kernel_bootstrap_log("telemetry_init");
        telemetry_init();
 #endif
 
-       kernel_bootstrap_log("stackshot_lock_init");    
-       stackshot_lock_init();
+#if CONFIG_CSR
+       kernel_bootstrap_log("csr_init");
+       csr_init();
+#endif
+
+       if (PE_i_can_has_debugger(NULL) &&
+           PE_parse_boot_argn("-show_pointers", &namep, sizeof (namep))) {
+               doprnt_hide_pointers = FALSE;
+       }
+
+       kernel_bootstrap_log("console_init");
+       console_init();
+
+       kernel_bootstrap_log("stackshot_init");
+       stackshot_init();
 
        kernel_bootstrap_log("sched_init");
        sched_init();
 
-       kernel_bootstrap_log("wait_queue_bootstrap");
-       wait_queue_bootstrap();
+       kernel_bootstrap_log("ltable_bootstrap");
+       ltable_bootstrap();
+
+       kernel_bootstrap_log("waitq_bootstrap");
+       waitq_bootstrap();
 
        kernel_bootstrap_log("ipc_bootstrap");
        ipc_bootstrap();
 
 #if CONFIG_MACF
+       kernel_bootstrap_log("mac_policy_init");
        mac_policy_init();
 #endif
+
        kernel_bootstrap_log("ipc_init");
        ipc_init();
 
@@ -311,9 +353,10 @@ kernel_bootstrap(void)
        /*
         *      Initialize the IPC, task, and thread subsystems.
         */
+
 #if CONFIG_COALITIONS
-       kernel_bootstrap_log("coalition_init");
-       coalition_init();
+       kernel_bootstrap_log("coalitions_init");
+       coalitions_init();
 #endif
 
        kernel_bootstrap_log("task_init");
@@ -327,18 +370,22 @@ kernel_bootstrap(void)
        kernel_bootstrap_log("atm_init");
        atm_init();
 #endif
+       kernel_bootstrap_log("mach_init_activity_id");
+       mach_init_activity_id();
 
-#if CONFIG_CSR
-       kernel_bootstrap_log("csr_init");
-       csr_init();
-#endif
-
-#if CONFIG_BANK
        /* Initialize the BANK Manager. */
        kernel_bootstrap_log("bank_init");
        bank_init();
-#endif
-       
+
+       kernel_bootstrap_log("ipc_pthread_priority_init");
+       ipc_pthread_priority_init();
+
+       /* initialize the corpse config based on boot-args */
+       corpses_init();
+
+       /* initialize host_statistics */
+       host_statistics_init();
+
        /*
         *      Create a kernel thread to execute the kernel bootstrap.
         */
@@ -348,6 +395,7 @@ kernel_bootstrap(void)
        if (result != KERN_SUCCESS) panic("kernel_bootstrap: result = %08X\n", result);
 
        thread->state = TH_RUN;
+       thread->last_made_runnable_time = mach_absolute_time();
        thread_deallocate(thread);
 
        kernel_bootstrap_log("load_context - done");
@@ -359,6 +407,9 @@ int kth_started = 0;
 
 vm_offset_t vm_kernel_addrperm;
 vm_offset_t buf_kernel_addrperm;
+vm_offset_t vm_kernel_addrperm_ext;
+uint64_t vm_kernel_addrhash_salt;
+uint64_t vm_kernel_addrhash_salt_ext;
 
 /*
  * Now running in a thread.  Kick off other services,
@@ -407,6 +458,12 @@ kernel_bootstrap_thread(void)
        kernel_bootstrap_thread_log("thread_bind");
        thread_bind(processor);
 
+#if __arm64__
+       if (IORamDiskBSDRoot()) {
+               cpm_preallocate_early();
+       }
+#endif /* __arm64__ */
+
        /*
         * Initialize ipc thread call support.
         */
@@ -431,7 +488,7 @@ kernel_bootstrap_thread(void)
        device_service_create();
 
        kth_started = 1;
-               
+
 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
        /*
         * Create and initialize the physical copy window for processor 0
@@ -451,10 +508,6 @@ kernel_bootstrap_thread(void)
        alternate_debugger_init();
 #endif
 
-#if CONFIG_COUNTERS
-       pmc_bootstrap();
-#endif
-
 #if KPC
        kpc_init();
 #endif
@@ -463,10 +516,6 @@ kernel_bootstrap_thread(void)
        ecc_log_init();
 #endif 
 
-#if KPERF
-       kperf_bootstrap();
-#endif
-
 #if HYPERVISOR
        hv_support_init();
 #endif
@@ -480,33 +529,40 @@ kernel_bootstrap_thread(void)
        vmx_init();
 #endif
 
-#if (defined(__i386__) || defined(__x86_64__))
-       if (kdebug_serial) {
-               new_nkdbufs = 1;
-               if (trace_typefilter == 0)
-                       trace_typefilter = 1;
-       }
-       if (turn_on_log_leaks && !new_nkdbufs)
-               new_nkdbufs = 200000;
-       if (trace_typefilter)
-               start_kern_tracing_with_typefilter(new_nkdbufs,
-                                                  FALSE,
-                                                  trace_typefilter);
-       else
-               start_kern_tracing(new_nkdbufs, FALSE);
-       if (turn_on_log_leaks)
-               log_leaks = 1;
+       kernel_bootstrap_thread_log("ktrace_init");
+       ktrace_init();
 
-#endif
+       kdebug_init(new_nkdbufs, trace_typefilter, trace_wrap);
 
        kernel_bootstrap_log("prng_init");
        prng_cpu_init(master_cpu);
 
+#ifdef MACH_BSD
+       kernel_bootstrap_log("bsd_early_init");
+       bsd_early_init();
+#endif
+
+#if defined(__arm64__)
+    ml_lockdown_init();
+#endif
+
 #ifdef IOKIT
+       kernel_bootstrap_log("PE_init_iokit");
        PE_init_iokit();
 #endif
 
        assert(ml_get_interrupts_enabled() == FALSE);
+
+       /*
+        * Past this point, kernel subsystems that expect to operate with
+        * interrupts or preemption enabled may begin enforcement.
+        */
+       early_boot_complete = TRUE;
+
+#if INTERRUPT_MASKED_DEBUG
+       // Reset interrupts masked timeout before we enable interrupts
+       ml_spin_debug_clear_self();
+#endif
        (void) spllo();         /* Allow interruptions */
 
 #if (defined(__i386__) || defined(__x86_64__)) && NCOPY_WINDOWS > 0
@@ -520,17 +576,6 @@ kernel_bootstrap_thread(void)
        cpu_userwindow_init(0);
 #endif
 
-#if (!defined(__i386__) && !defined(__x86_64__))
-       if (turn_on_log_leaks && !new_nkdbufs)
-               new_nkdbufs = 200000;
-       if (trace_typefilter)
-               start_kern_tracing_with_typefilter(new_nkdbufs, FALSE, trace_typefilter);
-       else
-               start_kern_tracing(new_nkdbufs, FALSE);
-       if (turn_on_log_leaks)
-               log_leaks = 1;
-#endif
-
        /*
         *      Initialize the shared region module.
         */
@@ -538,20 +583,34 @@ kernel_bootstrap_thread(void)
        vm_commpage_init();
        vm_commpage_text_init();
 
-
 #if CONFIG_MACF
        kernel_bootstrap_log("mac_policy_initmach");
        mac_policy_initmach();
+#if CONFIG_VNGUARD
+       vnguard_policy_init();
+#endif
+#endif
+
+#if defined(__arm__) || defined(__arm64__)
+#if CONFIG_KERNEL_INTEGRITY
+        machine_lockdown_preflight();
+#endif
+       /*
+        *  Finalize protections on statically mapped pages now that comm page mapping is established.
+        */
+       arm_vm_prot_finalize(PE_state.bootArgs); 
 #endif
 
+#if CONFIG_SCHED_SFI
        kernel_bootstrap_log("sfi_init");
        sfi_init();
+#endif
 
        /*
-        * Initialize the global used for permuting kernel
+        * Initialize the globals used for permuting kernel
         * addresses that may be exported to userland as tokens
-        * using VM_KERNEL_ADDRPERM(). Force the random number
-        * to be odd to avoid mapping a non-zero
+        * using VM_KERNEL_ADDRPERM()/VM_KERNEL_ADDRPERM_EXTERNAL().
+        * Force the random number to be odd to avoid mapping a non-zero
         * word-aligned address to zero via addition.
         * Note: at this stage we can use the cryptographically secure PRNG
         * rather than early_random().
@@ -560,6 +619,15 @@ kernel_bootstrap_thread(void)
        vm_kernel_addrperm |= 1;
        read_random(&buf_kernel_addrperm, sizeof(buf_kernel_addrperm));
        buf_kernel_addrperm |= 1;
+       read_random(&vm_kernel_addrperm_ext, sizeof(vm_kernel_addrperm_ext));
+       vm_kernel_addrperm_ext |= 1;
+       read_random(&vm_kernel_addrhash_salt, sizeof(vm_kernel_addrhash_salt));
+       read_random(&vm_kernel_addrhash_salt_ext, sizeof(vm_kernel_addrhash_salt_ext));
+
+       vm_set_restrictions();
+
+
+
 
        /*
         *      Start the user bootstrap.
@@ -574,10 +642,15 @@ kernel_bootstrap_thread(void)
      */
        OSKextRemoveKextBootstrap();
 
+       /*
+        * Get rid of pages used for early boot tracing.
+        */
+       kdebug_free_early_buf();
+
        serial_keyboard_init();         /* Start serial keyboard if wanted */
 
        vm_page_init_local_q();
-       
+
        thread_bind(PROCESSOR_NULL);
 
        /*
@@ -647,7 +720,7 @@ processor_start_thread(void *machine_param)
  *
  *     Start the first thread on a processor.
  */
-static void
+static void __attribute__((noreturn))
 load_context(
        thread_t                thread)
 {
@@ -685,8 +758,9 @@ load_context(
                sched_run_incr(thread);
 
        processor->active_thread = thread;
-       processor->current_pri = thread->sched_pri;
-       processor->current_thmode = thread->sched_mode;
+       processor_state_update_explicit(processor, thread->sched_pri, 
+               SFI_CLASS_KERNEL, PSET_SMP, thread_get_perfcontrol_class(thread));
+       processor->starting_pri = thread->sched_pri;
        processor->deadline = UINT64_MAX;
        thread->last_processor = processor;
 
@@ -697,6 +771,7 @@ load_context(
        timer_start(&PROCESSOR_DATA(processor, system_state), processor->last_dispatch);
        PROCESSOR_DATA(processor, current_state) = &PROCESSOR_DATA(processor, system_state);
 
+
        PMAP_ACTIVATE_USER(thread, processor->cpu_id);
 
        load_context_kprintf("machine_load_context\n");
@@ -718,8 +793,15 @@ scale_setup()
                if (scale > 16)
                        scale = 16;
                task_max_base = 2500;
-       } else if ((uint64_t)sane_size >= (uint64_t)(3 * 1024 * 1024 *1024ULL))
-               scale = 2;
+       /* Raise limits for machines with >= 3GB */
+       } else if ((uint64_t)sane_size >= (uint64_t)(3 * 1024 * 1024 *1024ULL)) {
+               if ((uint64_t)sane_size < (uint64_t)(8 * 1024 * 1024 *1024ULL)) {
+                       scale = 2;
+               } else {
+                       /* limit to 64GB */
+                       scale = MIN(16, (int)((uint64_t)sane_size / (uint64_t)(4 * 1024 * 1024 *1024ULL)));
+               }
+       }
 
        task_max = MAX(task_max, task_max_base * scale);