main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * Main.
  31. */
  32. #include <types.h>
  33. #include <kern/errno.h>
  34. #include <kern/reboot.h>
  35. #include <kern/unistd.h>
  36. #include <lib.h>
  37. #include <spl.h>
  38. #include <clock.h>
  39. #include <thread.h>
  40. #include <proc.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 pseudoconfig
  51. /*
  52. * These two pieces of data are maintained by the makefiles and build system.
  53. * buildconfig is the name of the config file the kernel was configured with.
  54. * buildversion starts at 1 and is incremented every time you link a kernel.
  55. *
  56. * The purpose is not to show off how many kernels you've linked, but
  57. * to make it easy to make sure that the kernel you just booted is the
  58. * same one you just built.
  59. */
  60. extern const int buildversion;
  61. extern const char buildconfig[];
  62. /*
  63. * Copyright message for the OS/161 base code.
  64. */
  65. static const char harvard_copyright[] =
  66. "Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009\n"
  67. " President and Fellows of Harvard College. All rights reserved.\n";
  68. /*
  69. * Initial boot sequence.
  70. */
  71. static
  72. void
  73. boot(void)
  74. {
  75. /*
  76. * The order of these is important!
  77. * Don't go changing it without thinking about the consequences.
  78. *
  79. * Among other things, be aware that console output gets
  80. * buffered up at first and does not actually appear until
  81. * mainbus_bootstrap() attaches the console device. This can
  82. * be remarkably confusing if a bug occurs at this point. So
  83. * don't put new code before mainbus_bootstrap if you don't
  84. * absolutely have to.
  85. *
  86. * Also note that the buffer for this is only 1k. If you
  87. * overflow it, the system will crash without printing
  88. * anything at all. You can make it larger though (it's in
  89. * dev/generic/console.c).
  90. */
  91. kprintf("\n");
  92. kprintf("OS/161 base system version %s\n", BASE_VERSION);
  93. kprintf("%s", harvard_copyright);
  94. kprintf("\n");
  95. kprintf("Tareef Dedhar's system version %s (%s #%d)\n",
  96. GROUP_VERSION, buildconfig, buildversion);
  97. kprintf("\n");
  98. /* Early initialization. */
  99. ram_bootstrap();
  100. proc_bootstrap();
  101. thread_bootstrap();
  102. hardclock_bootstrap();
  103. vfs_bootstrap();
  104. /* Probe and initialize devices. Interrupts should come on. */
  105. kprintf("Device probe...\n");
  106. KASSERT(curthread->t_curspl > 0);
  107. mainbus_bootstrap();
  108. KASSERT(curthread->t_curspl == 0);
  109. /* Now do pseudo-devices. */
  110. pseudoconfig();
  111. kprintf("\n");
  112. /* Late phase of initialization. */
  113. vm_bootstrap();
  114. kprintf_bootstrap();
  115. thread_start_cpus();
  116. /* Default bootfs - but ignore failure, in case emu0 doesn't exist */
  117. vfs_setbootfs("emu0");
  118. /*
  119. * Make sure various things aren't screwed up.
  120. */
  121. COMPILE_ASSERT(sizeof(userptr_t) == sizeof(char *));
  122. COMPILE_ASSERT(sizeof(*(userptr_t)0) == sizeof(char));
  123. }
  124. /*
  125. * Shutdown sequence. Opposite to boot().
  126. */
  127. static
  128. void
  129. shutdown(void)
  130. {
  131. kprintf("Shutting down.\n");
  132. vfs_clearbootfs();
  133. vfs_clearcurdir();
  134. vfs_unmountall();
  135. thread_shutdown();
  136. splhigh();
  137. }
  138. /*****************************************/
  139. /*
  140. * reboot() system call.
  141. *
  142. * Note: this is here because it's directly related to the code above,
  143. * not because this is where system call code should go. Other syscall
  144. * code should probably live in the "syscall" directory.
  145. */
  146. int
  147. sys_reboot(int code)
  148. {
  149. switch (code) {
  150. case RB_REBOOT:
  151. case RB_HALT:
  152. case RB_POWEROFF:
  153. break;
  154. default:
  155. return EINVAL;
  156. }
  157. shutdown();
  158. switch (code) {
  159. case RB_HALT:
  160. kprintf("The system is halted.\n");
  161. mainbus_halt();
  162. break;
  163. case RB_REBOOT:
  164. kprintf("Rebooting...\n");
  165. mainbus_reboot();
  166. break;
  167. case RB_POWEROFF:
  168. kprintf("The system is halted.\n");
  169. mainbus_poweroff();
  170. break;
  171. }
  172. panic("reboot operation failed\n");
  173. return 0;
  174. }
  175. /*
  176. * Kernel main. Boot up, then fork the menu thread; wait for a reboot
  177. * request, and then shut down.
  178. */
  179. void
  180. kmain(char *arguments)
  181. {
  182. boot();
  183. menu(arguments);
  184. /* Should not get here */
  185. }