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

@@ -1,6 +1,7 @@
#include "filesys/inode.h"
#include <list.h>
#include <debug.h>
#include <stdio.h>
#include <round.h>
#include <string.h>
#include "filesys/filesys.h"

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 */

View File

@@ -1,5 +1,11 @@
#include "page.h"
#include <string.h>
#include <stdio.h>
#include "filesys/file.h"
#include "threads/malloc.h"
#include "threads/palloc.h"
#include "userprog/process.h"
#include "vm/frame.h"
/* Hashing function needed for the SPT table. Returns a hash for an entry,
based on its upage. */
@@ -45,7 +51,7 @@ page_insert (struct file *file, off_t ofs, void *upage, uint32_t read_bytes,
/* Gets a page_entry from the starting address of the page. Returns NULL if no
such page_entry exists in the hash map.*/
static struct page_entry *
struct page_entry *
page_get (void *upage)
{
struct page_entry fake_page_entry;
@@ -60,6 +66,36 @@ page_get (void *upage)
return hash_entry (e, struct page_entry, elem);
}
bool
page_load (struct page_entry *page, bool writable)
{
void *frame = frame_alloc (PAL_USER, page->upage, thread_current());
if (frame == NULL)
return false; // TODO: Try to evict a page instead.
if (!install_page (page->upage, frame, writable))
{
frame_free (frame);
return false;
}
printf ("Hi! 3\n");
file_seek (page->file, page->offset);
int read = -1;
if ((read = file_read (page->file, frame, page->read_bytes)) != (int) page->read_bytes)
{
printf ("%p, %p, %d %d\n", page->file, frame, read, page->offset);
frame_free (frame);
return false;
}
printf ("Hi! 4\n");
memset (frame + page->read_bytes, 0, page->zero_bytes);
return true;
}
/* Function to clean up a page_entry. Given the elem of that page_entry, frees
the page_entry itself. */
void

View File

@@ -29,6 +29,8 @@ bool page_less (const struct hash_elem *a_, const struct hash_elem *b_,
struct page_entry *page_insert (struct file *file, off_t ofs, void *upage,
uint32_t read_bytes, uint32_t zero_bytes,
bool writable, enum page_type type);
struct page_entry *page_get (void *upage);
bool page_load (struct page_entry *page, bool writable);
void page_cleanup (struct hash_elem *e, void *aux);
void page_set_swap (struct thread *, void *, size_t);
size_t page_get_swap (struct thread *, void *);