Implement page fault for lazy loading executables, w/ G
This commit is contained in:
@@ -2,14 +2,19 @@
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include "userprog/gdt.h"
|
||||
#include "userprog/pagedir.h"
|
||||
#include "threads/interrupt.h"
|
||||
#include "threads/thread.h"
|
||||
#include "threads/vaddr.h"
|
||||
#include "vm/page.h"
|
||||
|
||||
/* Number of page faults processed. */
|
||||
static long long page_fault_cnt;
|
||||
|
||||
static void kill (struct intr_frame *);
|
||||
static void page_fault (struct intr_frame *);
|
||||
static bool try_fetch_page (void *upage, bool not_present, bool write,
|
||||
bool user);
|
||||
|
||||
/* Registers handlers for interrupts that can be caused by user
|
||||
programs.
|
||||
@@ -145,6 +150,10 @@ page_fault (struct intr_frame *f)
|
||||
write = (f->error_code & PF_W) != 0;
|
||||
user = (f->error_code & PF_U) != 0;
|
||||
|
||||
void *upage = pg_round_down (fault_addr);
|
||||
if (try_fetch_page (upage, not_present, write, user))
|
||||
return;
|
||||
|
||||
/* To implement virtual memory, delete the rest of the function
|
||||
body, and replace it with code that brings in the page to
|
||||
which fault_addr refers. */
|
||||
@@ -156,3 +165,27 @@ page_fault (struct intr_frame *f)
|
||||
kill (f);
|
||||
}
|
||||
|
||||
static bool
|
||||
try_fetch_page (void *upage, bool not_present, bool write, bool user)
|
||||
{
|
||||
if (!not_present || !is_user_vaddr (upage) || upage == NULL)
|
||||
return false;
|
||||
|
||||
struct page_entry *page = page_get (upage);
|
||||
if (page == NULL)
|
||||
return false;
|
||||
|
||||
if (write && !page->writable)
|
||||
{
|
||||
pagedir_set_writable(thread_current()->pagedir, upage, write);
|
||||
page->writable = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (page->type) {
|
||||
case PAGE_EXECUTABLE:
|
||||
return page_load (page, write);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user