From 14fc96febf3658d3e00ac3ebe50f45a76930c8cf Mon Sep 17 00:00:00 2001 From: sBubshait Date: Thu, 28 Nov 2024 17:54:15 +0000 Subject: [PATCH] Implement Supplemental Page Table (SPT) per thread along with structure for its entries, w/ G --- src/threads/thread.c | 4 +++- src/threads/thread.h | 2 ++ src/vm/page.c | 23 +++++++++++++++++++++-- src/vm/page.h | 23 +++++++++++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/threads/thread.c b/src/threads/thread.c index 91a12b5..c2944cc 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -15,6 +15,7 @@ #include "threads/switch.h" #include "threads/synch.h" #include "threads/vaddr.h" +#include "vm/page.h" #ifdef USERPROG #include "userprog/process.h" #include "userprog/syscall.h" @@ -264,7 +265,8 @@ thread_create (const char *name, int priority, if (!hash_init (&t->open_files, fd_hash, fd_less, NULL) || !hash_init (&t->child_results, process_result_hash, - process_result_less, t)) + process_result_less, t) + || !hash_init (&t->pages, page_hash, page_less, NULL)) { palloc_free_page (t); free (t->result); diff --git a/src/threads/thread.h b/src/threads/thread.h index 4a88577..bdfad35 100644 --- a/src/threads/thread.h +++ b/src/threads/thread.h @@ -135,6 +135,8 @@ struct thread /* Shared between thread.c and synch.c. */ struct list_elem elem; /* List element. */ + struct hash pages; /* Table of open user pages. */ + #ifdef USERPROG /* Owned by userprog/process.c. */ uint32_t *pagedir; /* Page directory. */ diff --git a/src/vm/page.c b/src/vm/page.c index 8ebc71c..9ed026f 100644 --- a/src/vm/page.c +++ b/src/vm/page.c @@ -1,5 +1,25 @@ #include "page.h" +/* Hashing function needed for the SPT table. Returns a hash for an entry, + based on its upage. */ +unsigned +page_hash (const struct hash_elem *e, UNUSED void *aux) +{ + struct page_entry *page = hash_entry (e, struct page_entry, elem); + return hash_ptr(page->upage); +} + +/* Comparator function for the SPT table. Compares two entries based on their + upages. */ +bool +page_less (const struct hash_elem *a_, const struct hash_elem *b_, UNUSED void *aux) +{ + const struct page_entry *a = hash_entry (a_, struct page_entry, elem); + const struct page_entry *b = hash_entry (b_, struct page_entry, elem); + return a->upage < b->upage; +} + + /* Updates the 'owner' thread's page table entry for virtual address 'upage' to have a present bit of 0 and stores the specified swap slot value in the entry for later retrieval from disk. */ @@ -16,5 +36,4 @@ size_t page_get_swap (struct thread *owner, void *upage) { -} - +} \ No newline at end of file diff --git a/src/vm/page.h b/src/vm/page.h index 7259ca9..81a80ba 100644 --- a/src/vm/page.h +++ b/src/vm/page.h @@ -2,6 +2,29 @@ #define VM_PAGE_H #include "threads/thread.h" +#include "filesys/off_t.h" + +enum page_type { + PAGE_EXECUTABLE, + PAGE_EMPTY +}; + +struct page_entry { + enum page_type type; /* Type of Data that should go into the page */ + void *upage; /* Start Address of the User Page (Key of hash table). */ + + /* File Data */ + struct file *file; /* Pointer to the file for executables. */ + off_t offset; /* Offset of the page content within the file. */ + uint32_t read_bytes; /* Number of bytes to read within the page. */ + uint32_t zero_bytes; /* Number of bytes to zero within the page. */ + bool writable; /* Flag for whether this page is writable or not. */ + + struct hash_elem elem; /* An elem for the hash table. */ +}; + +unsigned page_hash (const struct hash_elem *e, void *aux); +bool page_less (const struct hash_elem *a_, const struct hash_elem *b_, void *aux); void page_set_swap (struct thread *, void *, size_t); size_t page_get_swap (struct thread *, void *);