+\f
+/* addrspace_load() helpers. */
+
+static bool install_page (struct thread *, void *upage, void *kpage);
+
+/* Loads the segment described by PHDR from FILE into thread T's
+ user address space. Return true if successful, false
+ otherwise. */
+static bool
+load_segment (struct thread *t, struct file *file,
+ const struct Elf32_Phdr *phdr)
+{
+ void *start, *end; /* Page-rounded segment start and end. */
+ uint8_t *upage; /* Iterator from start to end. */
+ off_t filesz_left; /* Bytes left of file data (as opposed to
+ zero-initialized bytes). */
+
+ /* Is this a read-only segment? Not currently used, so it's
+ commented out. You'll want to use it when implementing VM
+ to decide whether to page the segment from its executable or
+ from swap. */
+ //bool read_only = (phdr->p_flags & PF_W) == 0;
+
+ ASSERT (t != NULL);
+ ASSERT (file != NULL);
+ ASSERT (phdr != NULL);
+ ASSERT (phdr->p_type == PT_LOAD);
+
+ /* [ELF1] 2-2 says that p_offset and p_vaddr must be congruent
+ modulo PGSIZE. */
+ if (phdr->p_offset % PGSIZE != phdr->p_vaddr % PGSIZE)
+ {
+ printf ("%#08"PE32Ox" and %#08"PE32Ax" not congruent modulo %#x\n",
+ phdr->p_offset, phdr->p_vaddr, (unsigned) PGSIZE);
+ return false;
+ }
+
+ /* [ELF1] 2-3 says that p_memsz must be at least as big as
+ p_filesz. */
+ if (phdr->p_memsz < phdr->p_filesz)
+ {
+ printf ("p_memsz (%08"PE32Wx") < p_filesz (%08"PE32Wx")\n",
+ phdr->p_memsz, phdr->p_filesz);
+ return false;
+ }
+
+ /* Validate virtual memory region to be mapped.
+ The region must both start and end within the user address
+ space range starting at 0 and ending at PHYS_BASE (typically
+ 3 GB == 0xc0000000). */
+ start = pg_round_down ((void *) phdr->p_vaddr);
+ end = pg_round_up ((void *) (phdr->p_vaddr + phdr->p_memsz));
+ if (start >= PHYS_BASE || end >= PHYS_BASE || end < start)
+ {
+ printf ("bad virtual region %08lx...%08lx\n",
+ (unsigned long) start, (unsigned long) end);
+ return false;
+ }
+
+ /* Load the segment page-by-page into memory. */
+ filesz_left = phdr->p_filesz + (phdr->p_vaddr & PGMASK);
+ file_seek (file, ROUND_DOWN (phdr->p_offset, PGSIZE));
+ for (upage = start; upage < (uint8_t *) end; upage += PGSIZE)
+ {
+ /* We want to read min(PGSIZE, filesz_left) bytes from the
+ 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 (PAL_USER);
+ if (kpage == NULL)
+ return false;
+
+ /* Do the reading and zeroing. */
+ if (file_read (file, kpage, read_bytes) != (int) read_bytes)
+ {
+ palloc_free (kpage);
+ return false;
+ }
+ memset (kpage + read_bytes, 0, zero_bytes);
+ filesz_left -= read_bytes;
+
+ /* Add the page to the process's address space. */
+ if (!install_page (t, upage, kpage))
+ {
+ palloc_free (kpage);
+ return false;
+ }
+ }
+
+ return true;
+}
+
+/* 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)
+{
+ uint8_t *kpage;
+ bool success = false;
+
+ kpage = palloc_get (PAL_USER | PAL_ZERO);
+ if (kpage != NULL)
+ {
+ success = install_page (t, ((uint8_t *) PHYS_BASE) - PGSIZE, kpage);
+ if (!success)
+ palloc_free (kpage);
+ }
+ else
+ printf ("failed to allocate process stack\n");
+
+ return success;
+}
+
+/* Adds a mapping from user virtual address UPAGE to kernel
+ virtual address KPAGE to T's page tables. Fails if UPAGE is
+ already mapped or if memory allocation fails. */
+static bool
+install_page (struct thread *t, void *upage, void *kpage)
+{
+ /* Verify that there's not already a page at that virtual
+ address, then map our page there. */
+ return (pagedir_get_page (t->pagedir, upage) == NULL
+ && pagedir_set_page (t->pagedir, upage, kpage, true));
+}