Implement page fault for lazy loading executables, w/ G

This commit is contained in:
sBubshait
2024-11-28 20:03:50 +00:00
parent df20e0fdfe
commit 801fd7d310
136 changed files with 15097 additions and 5 deletions

View File

@@ -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;
}
}

View File

@@ -119,7 +119,7 @@ process_execute (const char *cmd)
static void *get_usr_kpage (enum palloc_flags flags, void *upage);
static void free_usr_kpage (void *kpage);
static bool install_page (void *upage, void *kpage, bool writable);
bool install_page (void *upage, void *kpage, bool writable);
static bool process_init_stack (char *cmd_saveptr, void **esp, char *file_name);
static void *push_to_stack (void **esp, void *data, size_t data_size);
@@ -707,11 +707,12 @@ load_segment (struct file *file, off_t ofs, uint8_t *upage,
size_t page_zero_bytes = PGSIZE - page_read_bytes;
/* Add the page to the SPT */
if (page_insert (file, ofs, upage, read_bytes, zero_bytes, writable,
PAGE_EXECUTABLE) == NULL)
if (page_insert (file, ofs, upage, page_read_bytes, page_zero_bytes,
writable, PAGE_EXECUTABLE) == NULL)
return false;
/* Advance. */
ofs += page_read_bytes;
read_bytes -= page_read_bytes;
zero_bytes -= page_zero_bytes;
upage += PGSIZE;
@@ -782,7 +783,7 @@ free_usr_kpage (void *kpage)
with palloc_get_page().
Returns true on success, false if UPAGE is already mapped or
if memory allocation fails. */
static bool
bool
install_page (void *upage, void *kpage, bool writable)
{
struct thread *t = thread_current ();

View File

@@ -8,4 +8,6 @@ int process_wait (tid_t);
void process_exit (void);
void process_activate (void);
bool install_page (void *upage, void *kpage, bool writable);
#endif /* userprog/process.h */