Clean up a little.
[pintos-anon] / src / threads / init.c
index 81188e950c6e8825052c18003d3eeea761ae1435..caacaee20e894b6c0afbedb960540e030a946772 100644 (file)
@@ -7,6 +7,7 @@
 #include "io.h"
 #include "kbd.h"
 #include "lib.h"
+#include "loader.h"
 #include "malloc.h"
 #include "mmu.h"
 #include "paging.h"
 #include "disk.h"
 #endif
 
-/* Size of kernel static code and data, in 4 kB pages. */
-size_t kernel_pages;
-
 /* Amount of physical memory, in 4 kB pages. */
 size_t ram_pages;
 
+static void ram_init (void);
 static void gdt_init (void);
+static void argv_init (void);
 void power_off (void);
 
 static void
 main_thread (void *aux UNUSED) 
 {
-  struct disk *hda;
+#ifdef FILESYS
   disk_init ();
-  hda = disk_get (1);
-  if (hda != NULL) 
-    {
-      char buf[DISK_SECTOR_SIZE];
-      disk_read (hda, 0, buf);
-      //hex_dump (buf, sizeof buf);
-    }
-  else
-    printk ("no hda\n");
-  thread_execute ("a.out");
+  filesys_init (true);
+  filesys_self_test ();
+#endif
 }
 
 int
 main (void)
 {
-  extern char _text, _end, __bss_start;
-  struct thread *t;
-
-  /* Clear out the BSS segment. */
-  memset (&__bss_start, 0, &_end - &__bss_start);
-
-  /* Initialize components needed by printk() very early. */
+  /* Initialize prerequisites for calling printk(). */
+  ram_init ();
   vga_init ();
   serial_init ();
-  printk ("Booting cnachos86...\n");
-
-  /* Calculate how much RAM the kernel uses, and find out from
-     the bootloader how much RAM this machine has. */
-  kernel_pages = (&_end - &_text + 4095) / 4096;
-  ram_pages = *(uint32_t *) (0x7e00 - 6);
-  printk ("ram: detected %'d kB main memory.\n", ram_pages * 4);
-
-  /* Memory from the end of the kernel through the end of memory
-     is free.  Give it to the page allocator. */
-  palloc_init ((void *) (KERN_BASE + kernel_pages * PGSIZE),
-               (void *) (PHYS_BASE + ram_pages * PGSIZE));
+
+  /* Greet user. */
+  printk ("Booting cnachos86 with %'d kB RAM...\n", ram_pages * 4);
+
+  /* Initialize memory system. */
+  palloc_init ();
   paging_init ();
   gdt_init ();
-
   malloc_init ();
+
   random_init ();
+  argv_init ();
 
+  /* Initialize interrupt handlers. */
   intr_init ();
   timer_init ();
   kbd_init ();
 
-#ifdef FILESYS
-  filesys_init (false);
-#endif
-
+  /* Do everything else in a system thread. */
   thread_init ();
-
-  t = thread_create ("main", main_thread, NULL);
-  thread_start (t);
-
-  printk ("Done!\n");
-  return 0;
+  thread_start (thread_create ("main", main_thread, NULL));
 }
 
 static uint64_t
@@ -169,6 +145,44 @@ gdt_init (void)
   asm volatile ("lgdt %0" :: "m" (gdtr_operand));
   asm volatile ("ltr %w0" :: "r" (SEL_TSS));
 }
+
+static void
+ram_init (void) 
+{
+  /* The "BSS" is a segment that should be initialized to zeros.
+     It isn't actually stored on disk or zeroed by the kernel
+     loader, so we have to zero it ourselves.
+
+     The start and end of the BSS segment is recorded by the
+     linker as _start_bss and _end_bss.  See kernel.lds. */
+  extern char _start_bss, _end_bss;
+  memset (&_start_bss, 0, &_end_bss - &_start_bss);
+
+  /* Get RAM size from loader. */
+  ram_pages = *(uint32_t *) ptov (LOADER_RAM_PAGES);
+}
+\f
+/* This should be sufficient because the command line buffer is
+   only 128 bytes and arguments are space-delimited. */
+#define ARGC_MAX 64
+
+int argc;
+char *argv[ARGC_MAX + 1];
+
+static void
+argv_init (void) 
+{
+  char *cmd_line = ptov (LOADER_CMD_LINE);
+  char *arg, *pos;
+
+  for (arg = strtok_r (cmd_line, " \t\r\n\v", &pos); arg != NULL;
+       arg = strtok_r (NULL, " \t\r\n\v", &pos))
+    {
+      ASSERT (argc < ARGC_MAX);
+      argv[argc++] = arg;
+    }
+  argv[argc] = NULL;
+}
 \f
 void
 power_off (void)