/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* Main.31*/3233#include <types.h>34#include <kern/errno.h>35#include <kern/reboot.h>36#include <kern/unistd.h>37#include <lib.h>38#include <spl.h>39#include <clock.h>40#include <thread.h>41#include <current.h>42#include <synch.h>43#include <vm.h>44#include <mainbus.h>45#include <vfs.h>46#include <device.h>47#include <syscall.h>48#include <test.h>49#include <version.h>50#include "autoconf.h" // for pseudoconfig515253/*54* These two pieces of data are maintained by the makefiles and build system.55* buildconfig is the name of the config file the kernel was configured with.56* buildversion starts at 1 and is incremented every time you link a kernel.57*58* The purpose is not to show off how many kernels you've linked, but59* to make it easy to make sure that the kernel you just booted is the60* same one you just built.61*/62extern const int buildversion;63extern const char buildconfig[];6465/*66* Copyright message for the OS/161 base code.67*/68static const char harvard_copyright[] =69"Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009\n"70" President and Fellows of Harvard College. All rights reserved.\n";717273/*74* Initial boot sequence.75*/76static77void78boot(void)79{80/*81* The order of these is important!82* Don't go changing it without thinking about the consequences.83*84* Among other things, be aware that console output gets85* buffered up at first and does not actually appear until86* mainbus_bootstrap() attaches the console device. This can87* be remarkably confusing if a bug occurs at this point. So88* don't put new code before mainbus_bootstrap if you don't89* absolutely have to.90*91* Also note that the buffer for this is only 1k. If you92* overflow it, the system will crash without printing93* anything at all. You can make it larger though (it's in94* dev/generic/console.c).95*/9697kprintf("\n");98kprintf("OS/161 base version %s ASST1 solution version %s\n", BASE_VERSION, ASST1SOL_VERSION);99kprintf("%s", harvard_copyright);100kprintf("\n");101102kprintf("Put-your-group-name-here's system version %s (%s #%d)\n",103GROUP_VERSION, buildconfig, buildversion);104kprintf("\n");105106/* Early initialization. */107ram_bootstrap();108thread_bootstrap();109hardclock_bootstrap();110vfs_bootstrap();111112/* Probe and initialize devices. Interrupts should come on. */113kprintf("Device probe...\n");114KASSERT(curthread->t_curspl > 0);115mainbus_bootstrap();116KASSERT(curthread->t_curspl == 0);117/* Now do pseudo-devices. */118pseudoconfig();119kprintf("\n");120121/* Late phase of initialization. */122vm_bootstrap();123kprintf_bootstrap();124thread_start_cpus();125126/* Default bootfs - but ignore failure, in case emu0 doesn't exist */127vfs_setbootfs("emu0");128129130/*131* Make sure various things aren't screwed up.132*/133COMPILE_ASSERT(sizeof(userptr_t) == sizeof(char *));134COMPILE_ASSERT(sizeof(*(userptr_t)0) == sizeof(char));135}136137/*138* Shutdown sequence. Opposite to boot().139*/140static141void142shutdown(void)143{144145kprintf("Shutting down.\n");146147vfs_clearbootfs();148vfs_clearcurdir();149vfs_unmountall();150151thread_shutdown();152153splhigh();154}155156/*****************************************/157158/*159* reboot() system call.160*161* Note: this is here because it's directly related to the code above,162* not because this is where system call code should go. Other syscall163* code should probably live in the "syscall" directory.164*/165int166sys_reboot(int code)167{168switch (code) {169case RB_REBOOT:170case RB_HALT:171case RB_POWEROFF:172break;173default:174return EINVAL;175}176177shutdown();178179switch (code) {180case RB_HALT:181kprintf("The system is halted.\n");182mainbus_halt();183break;184case RB_REBOOT:185kprintf("Rebooting...\n");186mainbus_reboot();187break;188case RB_POWEROFF:189kprintf("The system is halted.\n");190mainbus_poweroff();191break;192}193194panic("reboot operation failed\n");195return 0;196}197198/*199* Kernel main. Boot up, then fork the menu thread; wait for a reboot200* request, and then shut down.201*/202void203kmain(char *arguments)204{205boot();206207menu(arguments);208209/* Should not get here */210}211212213