Comments.
[pintos-anon] / src / userprog / addrspace.c
index ea8767c84e2f4f33e5bd4e5a1f28f20c59960c62..ccbb356ef894d271563aca81ca6e535726efa1ea 100644 (file)
@@ -4,12 +4,13 @@
 #include <round.h>
 #include <stdio.h>
 #include <string.h>
+#include "userprog/pagedir.h"
 #include "userprog/tss.h"
+#include "filesys/directory.h"
 #include "filesys/file.h"
 #include "filesys/filesys.h"
 #include "threads/init.h"
 #include "threads/mmu.h"
-#include "threads/paging.h"
 #include "threads/palloc.h"
 #include "threads/thread.h"
 
@@ -78,7 +79,7 @@ struct Elf32_Phdr
 
 static bool load_segment (struct thread *, struct file *,
                           const struct Elf32_Phdr *);
-static bool setup_stack (struct thread *);
+static bool setup_stack (struct thread *, void **esp);
 
 /* Aborts loading an executable, with an error message. */
 #define LOAD_ERROR(MSG)                                         \
@@ -90,14 +91,15 @@ static bool setup_stack (struct thread *);
         } while (0)
 
 /* Loads an ELF executable from FILENAME into T,
-   and stores the executable's entry point into *START.
+   Stores the executable's entry point into *EIP
+   and its initial stack pointer into *ESP.
    Returns true if successful, false otherwise. */
 bool
-addrspace_load (struct thread *t, const char *filename, void (**start) (void)) 
+addrspace_load (struct thread *t, const char *filename,
+                void (**eip) (void), void **esp) 
 {
   struct Elf32_Ehdr ehdr;
-  struct file file;
-  bool file_open = false;
+  struct file *file = NULL;
   off_t file_ofs;
   bool success = false;
   int i;
@@ -108,12 +110,12 @@ addrspace_load (struct thread *t, const char *filename, void (**start) (void))
     LOAD_ERROR (("page directory allocation failed"));
 
   /* Open executable file. */
-  file_open = filesys_open (filename, &file);
-  if (!file_open)
+  file = filesys_open (filename);
+  if (file == NULL)
     LOAD_ERROR (("open failed"));
 
   /* Read and verify executable header. */
-  if (file_read (&file, &ehdr, sizeof ehdr) != sizeof ehdr) 
+  if (file_read (file, &ehdr, sizeof ehdr) != sizeof ehdr) 
     LOAD_ERROR (("error reading executable header"));
   if (memcmp (ehdr.e_ident, "\177ELF\1\1\1", 7) != 0)
     LOAD_ERROR (("file is not ELF"));
@@ -135,8 +137,8 @@ addrspace_load (struct thread *t, const char *filename, void (**start) (void))
     {
       struct Elf32_Phdr phdr;
 
-      file_seek (&file, file_ofs);
-      if (file_read (&file, &phdr, sizeof phdr) != sizeof phdr)
+      file_seek (file, file_ofs);
+      if (file_read (file, &phdr, sizeof phdr) != sizeof phdr)
         LOAD_ERROR (("error reading program header"));
       file_ofs += sizeof phdr;
       switch (phdr.p_type) 
@@ -157,26 +159,25 @@ addrspace_load (struct thread *t, const char *filename, void (**start) (void))
           printf ("unknown ELF segment type %08x\n", phdr.p_type);
           break;
         case PT_LOAD:
-          if (!load_segment (t, &file, &phdr))
+          if (!load_segment (t, file, &phdr))
             goto done;
           break;
         }
     }
 
   /* Set up stack. */
-  if (!setup_stack (t))
+  if (!setup_stack (t, esp))
     goto done;
 
   /* Start address. */
-  *start = (void (*) (void)) ehdr.e_entry;
+  *eip = (void (*) (void)) ehdr.e_entry;
 
   success = true;
 
  done:
   /* We arrive here whether the load is successful or not.
      We can distinguish based on `success'. */
-  if (file_open)
-    file_close (&file);
+  file_close (file);
   if (!success) 
     addrspace_destroy (t);
   return success;
@@ -274,7 +275,7 @@ load_segment (struct thread *t, struct file *file,
          file into the page and zero the rest. */
       size_t read_bytes = filesz_left >= PGSIZE ? PGSIZE : filesz_left;
       size_t zero_bytes = PGSIZE - read_bytes;
-      uint8_t *kpage = palloc_get (0);
+      uint8_t *kpage = palloc_get (PAL_USER);
       if (kpage == NULL)
         return false;
 
@@ -301,16 +302,18 @@ load_segment (struct thread *t, struct file *file,
 /* Create a minimal stack for T by mapping a zeroed page at the
    top of user virtual memory. */
 static bool
-setup_stack (struct thread *t) 
+setup_stack (struct thread *t, void **esp
 {
   uint8_t *kpage;
   bool success = false;
 
-  kpage = palloc_get (PAL_ZERO);
+  kpage = palloc_get (PAL_USER | PAL_ZERO);
   if (kpage != NULL) 
     {
       success = install_page (t, ((uint8_t *) PHYS_BASE) - PGSIZE, kpage);
-      if (!success)
+      if (success)
+        *esp = PHYS_BASE;
+      else
         palloc_free (kpage);
     }
   else