#include "debug.h"
#include "file.h"
#include "filesys.h"
+#include "init.h"
#include "lib.h"
#include "mmu.h"
-#include "malloc.h"
#include "paging.h"
#include "palloc.h"
+#include "thread.h"
/* We load ELF binaries. The following definitions are taken
from the ELF specification more-or-less verbatim. */
} while (0)
static bool
-load_segment (struct addrspace *as, struct file *file,
+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. */
+ if (pagedir_get_page (t->pagedir, upage) == NULL
+ && pagedir_set_page (t->pagedir, upage, kpage, true))
+ return true;
+ else
+ {
+ palloc_free (kpage);
+ return false;
+ }
+}
+
+static bool
+load_segment (struct thread *t, struct file *file,
const struct Elf32_Phdr *phdr)
{
- uintptr_t start, end;
+ void *start, *end;
uint8_t *upage;
off_t filesz_left;
- ASSERT (as != NULL);
+ ASSERT (t != NULL);
ASSERT (file != NULL);
ASSERT (phdr != NULL);
ASSERT (phdr->p_type == PT_LOAD);
- /* p_offset and p_vaddr must be congruent modulo NBPG. */
- if (phdr->p_offset % NBPG != phdr->p_vaddr % NBPG)
+ /* p_offset and p_vaddr must be congruent modulo PGSIZE. */
+ if (phdr->p_offset % PGSIZE != phdr->p_vaddr % PGSIZE)
{
printk ("%#08"PE32Ox" and %#08"PE32Ax" not congruent modulo %#x\n",
- phdr->p_offset, phdr->p_vaddr, (unsigned) NBPG);
+ phdr->p_offset, phdr->p_vaddr, (unsigned) PGSIZE);
return false;
}
}
/* Validate virtual memory region to be mapped. */
- start = PGROUNDDOWN (phdr->p_vaddr);
- end = PGROUNDUP (phdr->p_vaddr + phdr->p_memsz);
+ 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)
{
printk ("bad virtual region %08lx...%08lx\n",
return false;
}
- filesz_left = phdr->p_filesz + (phdr->p_vaddr - start);
- file_seek (file, ROUND_DOWN (phdr->p_offset, NBPG));
- for (upage = (uint8_t *) start; upage < (uint8_t *) end; upage += NBPG)
+ 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)
{
- size_t read_bytes = filesz_left >= NBPG ? NBPG : filesz_left;
- size_t zero_bytes = NBPG - read_bytes;
+ size_t read_bytes = filesz_left >= PGSIZE ? PGSIZE : filesz_left;
+ size_t zero_bytes = PGSIZE - read_bytes;
uint8_t *kpage = palloc_get (0);
if (kpage == NULL)
return false;
memset (kpage + read_bytes, 0, zero_bytes);
filesz_left -= read_bytes;
- if (pagedir_get_page (as->pagedir, upage))
- {
- palloc_free (kpage);
- return false;
- }
- pagedir_set_page (as->pagedir, upage, kpage, true);
+ if (!install_page (t, upage, kpage))
+ return false;
}
return true;
}
+static bool
+setup_stack (struct thread *t)
+{
+ uint8_t *kpage = palloc_get (PAL_ZERO);
+ if (kpage == NULL)
+ {
+ printk ("failed to allocate process stack\n");
+ return false;
+ }
+
+ return install_page (t, ((uint8_t *) PHYS_BASE) - PGSIZE, kpage);
+}
+
bool
-addrspace_load (struct addrspace *as, const char *filename)
+addrspace_load (struct thread *t, const char *filename,
+ void (**start) (void))
{
struct Elf32_Ehdr ehdr;
- struct file *file = NULL;
+ struct file file;
+ bool file_open = false;
off_t file_ofs;
bool success = false;
int i;
- as->pagedir = pagedir_create ();
+ /* Allocate page directory. */
+ t->pagedir = pagedir_create ();
+ if (t->pagedir == NULL)
+ LOAD_ERROR (("page directory allocation failed"));
- file = filesys_open (filename);
- if (file == NULL)
+ /* Open executable file. */
+ file_open = filesys_open (filename, &file);
+ if (!file_open)
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"));
/* Read program headers. */
file_ofs = ehdr.e_phoff;
- printk ("e_phnum=%d\n", ehdr.e_phnum);
for (i = 0; i < ehdr.e_phnum; i++)
{
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"));
- printk ("%x: %08x, %08x %08x %08x %05x %05x\n",
- file_tell (file),
- phdr.p_type,
- phdr.p_offset, phdr.p_vaddr, phdr.p_paddr,
- phdr.p_filesz, phdr.p_memsz);
file_ofs += sizeof phdr;
switch (phdr.p_type)
{
case PT_NULL:
case PT_NOTE:
case PT_PHDR:
- case PT_STACK: /* Stack segment. */
+ case PT_STACK:
/* Ignore this segment. */
break;
case PT_DYNAMIC:
printk ("unknown ELF segment type %08x\n", phdr.p_type);
break;
case PT_LOAD:
- if (!load_segment (as, file, &phdr))
+ if (!load_segment (t, &file, &phdr))
goto error;
break;
}
}
+
+ /* Set up stack. */
+ if (!setup_stack (t))
+ goto error;
+
+ /* Start address. */
+ *start = (void (*) (void)) ehdr.e_entry;
+
success = true;
error:
- if (file != NULL)
- file_close (file);
+ if (file_open)
+ file_close (&file);
if (!success)
- addrspace_destroy (as);
+ addrspace_destroy (t);
return success;
}
void
-addrspace_destroy (struct addrspace *as)
+addrspace_destroy (struct thread *t)
+{
+ if (t->pagedir != NULL)
+ {
+ pagedir_destroy (t->pagedir);
+ t->pagedir = NULL;
+ }
+}
+
+void
+addrspace_activate (struct thread *t)
{
- if (as != NULL && as->pagedir != NULL)
- pagedir_destroy (as->pagedir);
+ ASSERT (t != NULL);
+
+ if (t->pagedir != NULL)
+ pagedir_activate (t->pagedir);
+ tss->esp0 = (uint32_t) t + PGSIZE;
}