]> git.saurik.com Git - apple/xnu.git/blobdiff - osfmk/kern/startup.c
xnu-792.17.14.tar.gz
[apple/xnu.git] / osfmk / kern / startup.c
index 7f94aeb7cb9403ced2f18597a1886612db503189..9f51c90e3d245680050477e6770704cf4f07a23b 100644 (file)
@@ -1,23 +1,29 @@
 /*
- * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
+ * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
  *
- * @APPLE_LICENSE_HEADER_START@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
  * 
- * The contents of this file constitute Original Code as defined in and
- * are subject to the Apple Public Source License Version 1.1 (the
- * "License").  You may not use this file except in compliance with the
- * License.  Please obtain a copy of the License at
- * http://www.apple.com/publicsource and read it before using this file.
+ * This file contains Original Code and/or Modifications of Original Code
+ * as defined in and that are subject to the Apple Public Source License
+ * Version 2.0 (the 'License'). You may not use this file except in
+ * compliance with the License. The rights granted to you under the License
+ * may not be used to create, or enable the creation or redistribution of,
+ * unlawful or unlicensed copies of an Apple operating system, or to
+ * circumvent, violate, or enable the circumvention or violation of, any
+ * terms of an Apple operating system software license agreement.
  * 
- * This Original Code and all software distributed under the License are
- * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
+ * Please obtain a copy of the License at
+ * http://www.opensource.apple.com/apsl/ and read it before using this file.
+ * 
+ * The Original Code and all software distributed under the License are
+ * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT.  Please see the
- * License for the specific language governing rights and limitations
- * under the License.
+ * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
+ * Please see the License for the specific language governing rights and
+ * limitations under the License.
  * 
- * @APPLE_LICENSE_HEADER_END@
+ * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
  */
 /*
  * @OSF_COPYRIGHT@
 #include <debug.h>
 #include <xpr_debug.h>
 #include <mach_kdp.h>
-#include <cpus.h>
 #include <mach_host.h>
 #include <norma_vm.h>
-#include <etap.h>
 
 #include <mach/boolean.h>
 #include <mach/machine.h>
+#include <mach/thread_act.h>
 #include <mach/task_special_ports.h>
 #include <mach/vm_param.h>
 #include <ipc/ipc_init.h>
 #include <kern/misc_protos.h>
 #include <kern/clock.h>
 #include <kern/cpu_number.h>
-#include <kern/etap_macros.h>
+#include <kern/ledger.h>
 #include <kern/machine.h>
 #include <kern/processor.h>
 #include <kern/sched_prim.h>
-#include <kern/sf.h>
 #include <kern/startup.h>
 #include <kern/task.h>
 #include <kern/thread.h>
 #include <kern/timer.h>
-#include <kern/timer_call.h>
 #include <kern/xpr.h>
 #include <kern/zalloc.h>
+#include <kern/locks.h>
+#include <vm/vm_shared_memory_server.h>
 #include <vm/vm_kern.h>
 #include <vm/vm_init.h>
 #include <vm/vm_map.h>
 #include <vm/vm_page.h>
 #include <vm/vm_pageout.h>
 #include <machine/pmap.h>
-#include <sys/version.h>
+#include <machine/commpage.h>
+#include <libkern/version.h>
 
 #ifdef __ppc__
 #include <ppc/Firmware.h>
 #include <ppc/mappings.h>
+#include <ppc/serial_io.h>
 #endif
 
-/* Externs XXX */
-extern void    rtclock_reset(void);
+static void            kernel_bootstrap_thread(void);
 
-/* Forwards */
-void           cpu_launch_first_thread(
-                       thread_t                        thread);
-void           start_kernel_threads(void);
-void        swapin_thread();
+static void            load_context(
+                                       thread_t        thread);
 
 /*
  *     Running in virtual memory, on the interrupt stack.
- *     Does not return.  Dispatches initial thread.
- *
- *     Assumes that master_cpu is set.
  */
 void
-setup_main(void)
+kernel_bootstrap(void)
 {
-       thread_t        startup_thread;
+       kern_return_t   result;
+       thread_t                thread;
 
+       lck_mod_init();
        sched_init();
        vm_mem_bootstrap();
        ipc_bootstrap();
        vm_mem_init();
        ipc_init();
-       pager_mux_hash_init();
 
        /*
         * As soon as the virtual memory system is up, we record
@@ -130,126 +131,71 @@ setup_main(void)
         */
        PMAP_ACTIVATE_KERNEL(master_cpu);
 
-#ifdef __ppc__
        mapping_free_prime();                                           /* Load up with temporary mapping blocks */
-#endif
 
        machine_init();
        kmod_init();
        clock_init();
 
-       init_timers();
-       timer_call_initialize();
-
-       machine_info.max_cpus = NCPUS;
        machine_info.memory_size = mem_size;
-       machine_info.avail_cpus = 0;
-       machine_info.major_version = KERNEL_MAJOR_VERSION;
-       machine_info.minor_version = KERNEL_MINOR_VERSION;
+       machine_info.max_mem = max_mem;
+       machine_info.major_version = version_major;
+       machine_info.minor_version = version_minor;
 
        /*
         *      Initialize the IPC, task, and thread subsystems.
         */
        ledger_init();
-       swapper_init();
        task_init();
-       act_init();
        thread_init();
-       subsystem_init();
-
-       /*
-        *      Initialize the Event Trace Analysis Package.
-        *      Dynamic Phase: 2 of 2
-        */
-       etap_init_phase2();
        
        /*
-        *      Create a kernel thread to start the other kernel
-        *      threads.  Thread_resume (from kernel_thread) calls
-        *      thread_setrun, which may look at current thread;
-        *      we must avoid this, since there is no current thread.
+        *      Create a kernel thread to execute the kernel bootstrap.
         */
-       startup_thread = kernel_thread_with_priority(kernel_task, MAXPRI_KERNBAND,
-                                                                                       start_kernel_threads, FALSE);
-                                                       
-       /*
-        * Pretend it is already running, and resume it.
-        * Since it looks as if it is running, thread_resume
-        * will not try to put it on the run queues.
-        *
-        * We can do all of this without locking, because nothing
-        * else is running yet.
-        */
-       startup_thread->state = TH_RUN;
-       (void) thread_resume(startup_thread->top_act);
-       /*
-        * Start the thread.
-        */
-       cpu_launch_first_thread(startup_thread);
+       result = kernel_thread_create((thread_continue_t)kernel_bootstrap_thread, NULL, MAXPRI_KERNEL, &thread);
+       if (result != KERN_SUCCESS)
+               panic("kernel_bootstrap");
+
+       thread->state = TH_RUN;
+       thread_deallocate(thread);
+
+       load_context(thread);
        /*NOTREACHED*/
-       panic("cpu_launch_first_thread returns!");
 }
 
 /*
- * Now running in a thread.  Create the rest of the kernel threads
- * and the bootstrap task.
+ * Now running in a thread.  Kick off other services,
+ * invoke user bootstrap, enter pageout loop.
  */
-void
-start_kernel_threads(void)
+static void
+kernel_bootstrap_thread(void)
 {
-       register int                            i;
-
-       thread_bind(current_thread(), cpu_to_processor(cpu_number()));
-
-       /*
-        *      Create the idle threads and the other
-        *      service threads.
-        */
-       for (i = 0; i < NCPUS; i++) {
-         if (1 /*machine_slot[i].is_cpu*/) {
-                       processor_t             processor = cpu_to_processor(i);
-                       thread_t                thread;
-                       spl_t                   s;
-
-                       thread = kernel_thread_with_priority(kernel_task,
-                                                                               MAXPRI_KERNBAND, idle_thread, FALSE);
-                       s = splsched();
-                       thread_lock(thread);
-                       thread_bind_locked(thread, processor);
-                       processor->idle_thread = thread;
-                       thread->state |= TH_IDLE;
-                       thread_go_locked(thread, THREAD_AWAKENED);
-                       thread_unlock(thread);
-                       splx(s);
-           }
-       }
-
-       /*
-        * Initialize the thread callout mechanism.
-        */
-       thread_call_initialize();
+       processor_t             processor = current_processor();
+       thread_t                self = current_thread();
 
        /*
-        * Invoke some black magic.
+        * Create the idle processor thread.
         */
-#if __ppc__
-       mapping_adjust();
-#endif
+       idle_thread_create(processor);
 
        /*
-        *      Invoke the thread reaper mechanism.
+        * N.B. Do not stick anything else
+        * before this point.
+        *
+        * Start up the scheduler services.
         */
-       thread_reaper();
+       sched_startup();
 
        /*
-        *      Start the stack swapin thread
+        * Remain on current processor as
+        * additional processors come online.
         */
-       kernel_thread(kernel_task, swapin_thread);
+       thread_bind(self, processor);
 
        /*
-        *      Invoke the periodic scheduler mechanism.
+        * Kick off memory mapping adjustments.
         */
-       kernel_thread(kernel_task, sched_tick_thread);
+       mapping_adjust();
 
        /*
         *      Create the clock service.
@@ -261,112 +207,142 @@ start_kernel_threads(void)
         */
        device_service_create();
 
-       shared_file_boot_time_init();
+       shared_file_boot_time_init(ENV_DEFAULT_ROOT, cpu_type());
 
 #ifdef IOKIT
        {
                PE_init_iokit();
        }
 #endif
+       
+       (void) spllo();         /* Allow interruptions */
+
+    /*
+     * Fill in the comm area (mapped into every task address space.)
+     */
+    commpage_populate();
 
        /*
         *      Start the user bootstrap.
         */
-       
-       (void) spllo();         /* Allow interruptions */
-       
 #ifdef MACH_BSD
        { 
-               extern void bsd_init(void);
                bsd_init();
        }
 #endif
 
-       thread_bind(current_thread(), PROCESSOR_NULL);
+#if __ppc__
+       serial_keyboard_init();         /* Start serial keyboard if wanted */
+#endif
+
+       thread_bind(self, PROCESSOR_NULL);
 
        /*
         *      Become the pageout daemon.
         */
-
        vm_pageout();
        /*NOTREACHED*/
 }
 
+/*
+ *     slave_main:
+ *
+ *     Load the first thread to start a processor.
+ */
 void
 slave_main(void)
 {
-       processor_t             myprocessor = current_processor();
+       processor_t             processor = current_processor();
        thread_t                thread;
 
-       thread = myprocessor->next_thread;
-       myprocessor->next_thread = THREAD_NULL;
-       if (thread == THREAD_NULL) {
-               thread = machine_wake_thread;
-               machine_wake_thread = THREAD_NULL;
+       /*
+        *      Use the idle processor thread if there
+        *      is no dedicated start up thread.
+        */
+       if (processor->next_thread == THREAD_NULL) {
+               thread = processor->idle_thread;
+               thread->continuation = (thread_continue_t)processor_start_thread;
+               thread->parameter = NULL;
        }
-       cpu_launch_first_thread(thread);
+       else {
+               thread = processor->next_thread;
+               processor->next_thread = THREAD_NULL;
+       }
+
+       load_context(thread);
        /*NOTREACHED*/
-       panic("slave_main");
 }
 
 /*
- * Now running in a thread context
+ *     processor_start_thread:
+ *
+ *     First thread to execute on a started processor.
+ *
+ *     Called at splsched.
  */
 void
-start_cpu_thread(void)
+processor_start_thread(void)
 {
-       processor_t     processor;
-
-       processor = cpu_to_processor(cpu_number());
+       processor_t             processor = current_processor();
+       thread_t                self = current_thread();
 
        slave_machine_init();
 
-       if (processor->processor_self == IP_NULL) {
-               ipc_processor_init(processor);
-               ipc_processor_enable(processor);
-       }
-
-#if 0
-       printf("start_cpu_thread done on cpu %x\n", cpu_number());
-#endif
-       /* TODO: Mark this processor ready to dispatch threads */
+       /*
+        *      If running the idle processor thread,
+        *      reenter the idle loop, else terminate.
+        */
+       if (self == processor->idle_thread)
+               thread_block((thread_continue_t)idle_thread);
 
-       (void) thread_terminate(current_act());
+       thread_terminate(self);
+       /*NOTREACHED*/
 }
 
 /*
- *     Start up the first thread on a CPU.
+ *     load_context:
+ *
+ *     Start the first thread on a processor.
  */
-void
-cpu_launch_first_thread(
+static void
+load_context(
        thread_t                thread)
 {
-       register int    mycpu = cpu_number();
+       processor_t             processor = current_processor();
 
-       /* initialize preemption disabled */
-       cpu_data[mycpu].preemption_level = 1;
+       machine_set_current_thread(thread);
+       processor_up(processor);
 
-       cpu_up(mycpu);
-       start_timer(&kernel_timer[mycpu]);
+       PMAP_ACTIVATE_KERNEL(PROCESSOR_DATA(processor, slot_num));
 
-       if (thread == THREAD_NULL) {
-           thread = cpu_to_processor(mycpu)->idle_thread;
-               if (thread == THREAD_NULL)
-                   panic("cpu_launch_first_thread");
+       /*
+        * Acquire a stack if none attached.  The panic
+        * should never occur since the thread is expected
+        * to have reserved stack.
+        */
+       if (!thread->kernel_stack) {
+               if (!stack_alloc_try(thread))
+                       panic("load_context");
        }
 
-       rtclock_reset();                /* start realtime clock ticking */
-       PMAP_ACTIVATE_KERNEL(mycpu);
+       /*
+        * The idle processor threads are not counted as
+        * running for load calculations.
+        */
+       if (!(thread->state & TH_IDLE))
+               pset_run_incr(thread->processor_set);
 
-       thread_machine_set_current(thread);
-       thread_lock(thread);
-       thread->state &= ~TH_UNINT;
-       thread_unlock(thread);
-       timer_switch(&thread->system_timer);
+       processor->active_thread = thread;
+       processor->current_pri = thread->sched_pri;
+       processor->deadline = UINT64_MAX;
+       thread->last_processor = processor;
 
-       PMAP_ACTIVATE_USER(thread->top_act, mycpu);
+       processor->last_dispatch = mach_absolute_time();
+       timer_switch((uint32_t)processor->last_dispatch,
+                                                       &PROCESSOR_DATA(processor, offline_timer));
 
-       /* preemption enabled by load_context */
-       load_context(thread);
+       PMAP_ACTIVATE_USER(thread, PROCESSOR_DATA(processor, slot_num));
+
+       machine_load_context(thread);
        /*NOTREACHED*/
 }