Move user exception support into userprog.
[pintos-anon] / src / threads / init.c
index b70c16c43bee2046f4b134427dc60e811953b4e5..ec71cec8432b4670a5473f0486760dea595489c1 100644 (file)
@@ -3,7 +3,6 @@
 #include <stddef.h>
 #include <limits.h>
 #include "debug.h"
-#include "gdt.h"
 #include "interrupt.h"
 #include "io.h"
 #include "kbd.h"
 #include "thread.h"
 #include "timer.h"
 #include "vga.h"
+#ifdef USERPROG
+#include "exception.h"
+#include "gdt.h"
+#include "tss.h"
+#endif
 #ifdef FILESYS
 #include "filesys.h"
 #include "disk.h"
@@ -37,30 +41,14 @@ static bool format_filesys;
 static char *initial_program;
 #endif
 
+static thread_func main_thread;
 static void ram_init (void);
 static void argv_init (void);
 
-static void
-main_thread (void *aux UNUSED) 
-{
-#ifdef FILESYS
-  disk_init ();
-  filesys_init (format_filesys);
-  fsutil_run ();
-#endif
-
-#ifdef USERPROG
-  if (initial_program != NULL)
-    thread_execute (initial_program);
-  else
-    PANIC ("no initial program specified");
-#endif
-}
-
 int
 main (void)
 {
-  /* Initialize prerequisites for calling printk(). */
+  /* Needed by printk(), so initialize them very early. */
   ram_init ();
   vga_init ();
   serial_init ();
@@ -71,18 +59,25 @@ main (void)
   /* Parse command line. */
   argv_init ();
 
-  /* Initialize memory system. */
+  /* Initialize memory system, segments, paging. */
   palloc_init ();
   paging_init ();
+#ifdef USERPROG
+  tss_init ();
   gdt_init ();
+#endif
   malloc_init ();
 
+  /* Set random seed if not already done. */
   random_init (0);
 
   /* Initialize interrupt handlers. */
   intr_init ();
   timer_init ();
   kbd_init ();
+#ifdef USERPROG
+  exception_init ();
+#endif
 
   /* Do everything else in a system thread. */
   thread_init ();
@@ -90,7 +85,27 @@ main (void)
   thread_start ();
 }
 
+/* Initial thread. */
+static void
+main_thread (void *aux UNUSED) 
+{
+#ifdef FILESYS
+  disk_init ();
+  filesys_init (format_filesys);
+  fsutil_run ();
+#endif
 
+#ifdef USERPROG
+  if (initial_program != NULL)
+    thread_execute (initial_program);
+  else
+    PANIC ("no initial program specified");
+#else
+  PANIC ("boot successful");
+#endif
+}
+\f
+/* Clear BSS and obtain RAM size from loader. */
 static void
 ram_init (void) 
 {
@@ -103,10 +118,11 @@ ram_init (void)
   extern char _start_bss, _end_bss;
   memset (&_start_bss, 0, &_end_bss - &_start_bss);
 
-  /* Get RAM size from loader. */
+  /* Get RAM size from loader.  See loader.S. */
   ram_pages = *(uint32_t *) ptov (LOADER_RAM_PAGES);
 }
-\f
+
+/* Parses the command line. */
 static void
 argv_init (void) 
 {
@@ -168,8 +184,9 @@ argv_init (void)
           " -p FILENAME         Print the contents of FILENAME\n"
           " -r FILENAME         Delete FILENAME\n"
           " -ls                 List the files in the filesystem\n"
-          " -D                  Dump complete filesystem contents\n");
+          " -D                  Dump complete filesystem contents\n"
 #endif
+          );
       }
     else 
       PANIC ("unknown option `%s'", argv[i]);