From abd273ce0a9ae9267f8b0a144ea9b56d8912f9b5 Mon Sep 17 00:00:00 2001 From: manuel Date: Tue, 19 Jun 2012 23:31:28 +0200 Subject: add dynamic stack growing --- userprog/exception.c | 80 +++++++++++++++++++++++++++++++++------------------- userprog/process.c | 54 +++++++++++++++++++++++------------ userprog/process.h | 3 ++ userprog/syscall.c | 9 ++++-- userprog/syscall.h | 2 ++ 5 files changed, 98 insertions(+), 50 deletions(-) (limited to 'userprog') diff --git a/userprog/exception.c b/userprog/exception.c index 54621fa..debe7f0 100644 --- a/userprog/exception.c +++ b/userprog/exception.c @@ -2,6 +2,8 @@ #include #include #include "userprog/gdt.h" +#include "userprog/process.h" +#include "userprog/syscall.h" #include "threads/interrupt.h" #include "threads/thread.h" #include "threads/vaddr.h" @@ -128,7 +130,8 @@ page_fault (struct intr_frame *f) bool write; /* True: access was write, false: access was read. */ bool user; /* True: access by user, false: access by kernel. */ void *fault_addr; /* Fault address. */ - struct page_table_entry *pte; + void *sp; /* Stack pointer. */ + struct page_table_entry *pte; /* Page table entry. */ /* Obtain faulting address, the virtual address that was accessed to cause the fault. It may point to code or to @@ -151,38 +154,57 @@ page_fault (struct intr_frame *f) write = (f->error_code & PF_W) != 0; user = (f->error_code & PF_U) != 0; - /* To implement virtual memory, adapt the rest of the function - body, adding code that brings in the page to - which fault_addr refers. */ - if (is_user_vaddr(fault_addr)) { - pte = page_table_fetch (&thread_current ()->page_table, pg_round_down (fault_addr)); - if (pte != NULL) - page_load (pte); - else + /* accessing r/o page is always wrong */ + if (!not_present) + thread_exit (); + + if (is_user_vaddr (fault_addr)) { - printf ("Page fault %p\n", pte); - kill (f); - } + /* if page fault occurs during syscall, use saved stack pointer */ + sp = (!user) ? syscall_sp : f->esp; + + /* try to fetch page entry */ + pte = page_table_fetch (&thread_current ()->page_table, + pg_round_down (fault_addr)); + + /* we got a page entry so try to load the page */ + if (pte != NULL) + { + if (page_load (pte)) + return; + printf ("Unable to load page at %p in %s context.\n", + fault_addr, user ? "user" : "kernel"); + } + /* there's no page in our page table but we might still have an valid + stack access and need to expand our stack. so just check for that. + the maxium offset we consider as a valid access is caused by the PUSHA + instruction. it's 32 bytes below the current stack pointer */ + else if (fault_addr >= (sp - 32) && (PHYS_BASE - fault_addr) <= STACK_SIZE) + { + if (process_grow_stack (pg_round_down (fault_addr))) + return; + printf ("Unable to grow stack %p in %s context.\n", + fault_addr, user ? "user" : "kernel"); + } - //TODO -#if 0 - if (! user) { /* syscall exception; set eax and eip */ - f->eip = (void*)f->eax; - f->eax = 0xFFFFFFFF; - return; - } else { + if (!user) + { + f->eip = (void*)f->eax; + f->eax = 0xFFFFFFFF; + return; + } + /* user process access violation */ - thread_exit(); + thread_exit (); + return; } -#endif - } else { - printf ("Page fault at %p: %s error %s page in %s context.\n", - fault_addr, - not_present ? "not present" : "rights violation", - write ? "writing" : "reading", - user ? "user" : "kernel"); - kill (f); - } + + printf ("Page fault at %p: %s error %s page in %s context.\n", + fault_addr, + not_present ? "not present" : "rights violation", + write ? "writing" : "reading", + user ? "user" : "kernel"); + kill (f); } diff --git a/userprog/process.c b/userprog/process.c index 15883d9..2771e76 100644 --- a/userprog/process.c +++ b/userprog/process.c @@ -552,24 +552,24 @@ load_segment (struct file *file, off_t ofs, uint8_t *upage, file_seek (file, ofs); while (read_bytes > 0 || zero_bytes > 0) - { - /* Calculate how to fill this page. - We will read PAGE_READ_BYTES bytes from FILE - and zero the final PAGE_ZERO_BYTES bytes. */ - size_t page_read_bytes = read_bytes < PGSIZE ? read_bytes : PGSIZE; - size_t page_zero_bytes = PGSIZE - page_read_bytes; - - /* add segment to page table for on demand loading */ - if (!page_table_insert_segment (file, ofs, upage, page_read_bytes, - page_zero_bytes, writable)) - return false; - - /* Advance. */ - read_bytes -= page_read_bytes; - zero_bytes -= page_zero_bytes; - ofs += page_read_bytes; - upage += PGSIZE; - } + { + /* Calculate how to fill this page. + We will read PAGE_READ_BYTES bytes from FILE + and zero the final PAGE_ZERO_BYTES bytes. */ + size_t page_read_bytes = read_bytes < PGSIZE ? read_bytes : PGSIZE; + size_t page_zero_bytes = PGSIZE - page_read_bytes; + + /* add segment to page table for on demand loading */ + if (!page_table_insert_segment (file, ofs, upage, page_read_bytes, + page_zero_bytes, writable)) + return false; + + /* Advance. */ + read_bytes -= page_read_bytes; + zero_bytes -= page_zero_bytes; + ofs += page_read_bytes; + upage += PGSIZE; + } return true; } @@ -686,6 +686,24 @@ setup_stack (uint32_t **esp, const char *args) return true; } +/* expand the stack of the process by one new page + which will be installed at the address of UPAGE */ +bool +process_grow_stack (void *upage) +{ + uint8_t *kpage = palloc_get_page (PAL_USER | PAL_ZERO); + if (kpage == NULL) + return false; + + if (!process_install_page (upage, kpage, true)) + { + palloc_free_page (kpage); + return false; + } + + return true; +} + /* Adds a mapping from user virtual address UPAGE to kernel virtual address KPAGE to the page table. If WRITABLE is true, the user process may modify the page; diff --git a/userprog/process.h b/userprog/process.h index b7bca5d..ccb94cb 100644 --- a/userprog/process.h +++ b/userprog/process.h @@ -3,6 +3,8 @@ #include "threads/thread.h" +#define STACK_SIZE (1 << 23) /* 8MB maxiumum stack size */ + /* In the current implementation, the capacity is fixed to 1024 (PGSIZE/4) */ struct fd_table { struct file** fds; @@ -37,6 +39,7 @@ tid_t process_execute (const char *file_name); int process_wait (tid_t); void process_exit (void); void process_activate (void); +bool process_grow_stack (void *upage); bool process_install_page (void *upage, void *kpage, bool writable); int process_open_file(const char* fname); diff --git a/userprog/syscall.c b/userprog/syscall.c index f8e0197..541668d 100644 --- a/userprog/syscall.c +++ b/userprog/syscall.c @@ -17,6 +17,9 @@ #define STACK_SLOT_SIZE sizeof(int) +/* stored stack pointer for the "page fault inside syscall/kernel"-case */ +void *syscall_sp; + /* Prototypes for Utilities */ static int get_user (const uint8_t *uaddr); static bool put_user (uint8_t *udst, uint8_t byte); @@ -195,10 +198,10 @@ syscall_handler (struct intr_frame *f) handler* fp; bool segfault = false; int result; - void *sp = f->esp; + syscall_sp = f->esp; /* The system call number and the arguments are on the stack */ - if (! copy_from_user (&syscall_nr,sp)) + if (! copy_from_user (&syscall_nr,syscall_sp)) goto fail; switch (syscall_nr) { case SYS_HALT: fp = syscall_halt; break; @@ -217,7 +220,7 @@ syscall_handler (struct intr_frame *f) default: goto fail; } - result = fp (sp, &segfault); + result = fp (syscall_sp, &segfault); if (segfault) goto fail; f->eax = result; diff --git a/userprog/syscall.h b/userprog/syscall.h index f7ab2f3..987ceaa 100644 --- a/userprog/syscall.h +++ b/userprog/syscall.h @@ -2,4 +2,6 @@ #define USERPROG_SYSCALL_H void syscall_init (void); +extern void *syscall_sp; + #endif /* userprog/syscall.h */ -- cgit v1.2.3