root/kern/startup/main.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. boot
  2. shutdown
  3. sys_reboot
  4. kmain

   1 /*
   2  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
   3  *      The President and Fellows of Harvard College.
   4  *
   5  * Redistribution and use in source and binary forms, with or without
   6  * modification, are permitted provided that the following conditions
   7  * are met:
   8  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the University nor the names of its contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 /*
  31  * Main.
  32  */
  33 
  34 #include <types.h>
  35 #include <kern/errno.h>
  36 #include <kern/reboot.h>
  37 #include <kern/unistd.h>
  38 #include <lib.h>
  39 #include <spl.h>
  40 #include <clock.h>
  41 #include <thread.h>
  42 #include <proc.h>
  43 #include <current.h>
  44 #include <synch.h>
  45 #include <vm.h>
  46 #include <mainbus.h>
  47 #include <vfs.h>
  48 #include <device.h>
  49 #include <syscall.h>
  50 #include <test.h>
  51 #include <version.h>
  52 #include "autoconf.h"  // for pseudoconfig
  53 
  54 
  55 /*
  56  * These two pieces of data are maintained by the makefiles and build system.
  57  * buildconfig is the name of the config file the kernel was configured with.
  58  * buildversion starts at 1 and is incremented every time you link a kernel. 
  59  *
  60  * The purpose is not to show off how many kernels you've linked, but
  61  * to make it easy to make sure that the kernel you just booted is the
  62  * same one you just built.
  63  */
  64 extern const int buildversion;
  65 extern const char buildconfig[];
  66 
  67 /*
  68  * Copyright message for the OS/161 base code.
  69  */
  70 static const char harvard_copyright[] =
  71     "Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009\n"
  72     "   President and Fellows of Harvard College.  All rights reserved.\n";
  73 
  74 
  75 /*
  76  * Initial boot sequence.
  77  */
  78 static
  79 void
  80 boot(void)
  81 {
  82         /*
  83          * The order of these is important!
  84          * Don't go changing it without thinking about the consequences.
  85          *
  86          * Among other things, be aware that console output gets
  87          * buffered up at first and does not actually appear until
  88          * mainbus_bootstrap() attaches the console device. This can
  89          * be remarkably confusing if a bug occurs at this point. So
  90          * don't put new code before mainbus_bootstrap if you don't
  91          * absolutely have to.
  92          *
  93          * Also note that the buffer for this is only 1k. If you
  94          * overflow it, the system will crash without printing
  95          * anything at all. You can make it larger though (it's in
  96          * dev/generic/console.c).
  97          */
  98 
  99         kprintf("\n");
 100         kprintf("OS/161 base system version %s\n", BASE_VERSION);
 101         kprintf("%s", harvard_copyright);
 102         kprintf("\n");
 103 
 104         kprintf("Put-your-group-name-here's system version %s (%s #%d)\n", 
 105                 GROUP_VERSION, buildconfig, buildversion);
 106         kprintf("\n");
 107 
 108         /* Early initialization. */
 109         ram_bootstrap();
 110         proc_bootstrap();
 111         thread_bootstrap();
 112         hardclock_bootstrap();
 113         vfs_bootstrap();
 114 
 115         /* Probe and initialize devices. Interrupts should come on. */
 116         kprintf("Device probe...\n");
 117         KASSERT(curthread->t_curspl > 0);
 118         mainbus_bootstrap();
 119         KASSERT(curthread->t_curspl == 0);
 120         /* Now do pseudo-devices. */
 121         pseudoconfig();
 122         kprintf("\n");
 123 
 124         /* Late phase of initialization. */
 125         vm_bootstrap();
 126         kprintf_bootstrap();
 127         thread_start_cpus();
 128 
 129         /* Default bootfs - but ignore failure, in case emu0 doesn't exist */
 130         vfs_setbootfs("emu0");
 131 
 132 
 133         /*
 134          * Make sure various things aren't screwed up.
 135          */
 136         COMPILE_ASSERT(sizeof(userptr_t) == sizeof(char *));
 137         COMPILE_ASSERT(sizeof(*(userptr_t)0) == sizeof(char));
 138 }
 139 
 140 /*
 141  * Shutdown sequence. Opposite to boot().
 142  */
 143 static
 144 void
 145 shutdown(void)
 146 {
 147 
 148         kprintf("Shutting down.\n");
 149         
 150         vfs_clearbootfs();
 151         vfs_clearcurdir();
 152         vfs_unmountall();
 153 
 154         thread_shutdown();
 155 
 156         splhigh();
 157 }
 158 
 159 /*****************************************/
 160 
 161 /*
 162  * reboot() system call.
 163  *
 164  * Note: this is here because it's directly related to the code above,
 165  * not because this is where system call code should go. Other syscall
 166  * code should probably live in the "syscall" directory.
 167  */
 168 int
 169 sys_reboot(int code)
 170 {
 171         switch (code) {
 172             case RB_REBOOT:
 173             case RB_HALT:
 174             case RB_POWEROFF:
 175                 break;
 176             default:
 177                 return EINVAL;
 178         }
 179 
 180         shutdown();
 181 
 182         switch (code) {
 183             case RB_HALT:
 184                 kprintf("The system is halted.\n");
 185                 mainbus_halt();
 186                 break;
 187             case RB_REBOOT:
 188                 kprintf("Rebooting...\n");
 189                 mainbus_reboot();
 190                 break;
 191             case RB_POWEROFF:
 192                 kprintf("The system is halted.\n");
 193                 mainbus_poweroff();
 194                 break;
 195         }
 196 
 197         panic("reboot operation failed\n");
 198         return 0;
 199 }
 200 
 201 /*
 202  * Kernel main. Boot up, then fork the menu thread; wait for a reboot
 203  * request, and then shut down.
 204  */
 205 void
 206 kmain(char *arguments)
 207 {
 208         boot();
 209 
 210         menu(arguments);
 211 
 212         /* Should not get here */
 213 }

/* [<][>][^][v][top][bottom][index][help] */