From 723055f4855ed8f2237cb9d4629518a5b76cbd0f Mon Sep 17 00:00:00 2001 From: Themis Demetriades Date: Wed, 4 Dec 2024 21:33:21 +0000 Subject: [PATCH] fix: only use lazy loading if VM flag is enabled --- src/threads/thread.c | 13 ++++++--- src/userprog/exception.c | 3 +++ src/userprog/process.c | 57 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/src/threads/thread.c b/src/threads/thread.c index c2944cc..b512aee 100644 --- a/src/threads/thread.c +++ b/src/threads/thread.c @@ -259,14 +259,19 @@ thread_create (const char *name, int priority, return TID_ERROR; } +#define USERPROG #ifdef USERPROG /* Initialize the thread's file descriptor table. */ t->fd_counter = MINIMUM_USER_FD; - if (!hash_init (&t->open_files, fd_hash, fd_less, NULL) - || !hash_init (&t->child_results, process_result_hash, - process_result_less, t) - || !hash_init (&t->pages, page_hash, page_less, NULL)) + bool success = hash_init (&t->open_files, fd_hash, fd_less, NULL); + success = success && hash_init (&t->child_results, process_result_hash, + process_result_less, t); +#ifdef VM + success = success && hash_init (&t->pages, page_hash, page_less, NULL); +#endif + + if (!success) { palloc_free_page (t); free (t->result); diff --git a/src/userprog/exception.c b/src/userprog/exception.c index ed6a6ba..4e104d5 100644 --- a/src/userprog/exception.c +++ b/src/userprog/exception.c @@ -215,6 +215,7 @@ page_fault (struct intr_frame *f) kill (f); } +#ifdef VM bool try_fetch_page (void *upage, bool write) { @@ -244,3 +245,5 @@ try_fetch_page (void *upage, bool write) return success; } +#endif + diff --git a/src/userprog/process.c b/src/userprog/process.c index f4a7439..9ef5282 100644 --- a/src/userprog/process.c +++ b/src/userprog/process.c @@ -365,7 +365,9 @@ process_exit (void) /* Clean up all open files */ hash_destroy (&cur->open_files, fd_cleanup); +#ifdef VM hash_destroy (&cur->pages, page_cleanup); +#endif /* Close the executable file, implicitly allowing it to be written to. */ if (cur->exec_file != NULL) @@ -623,6 +625,9 @@ load (const char *file_name, void (**eip) (void), void **esp) done: /* We arrive here whether the load is successful or not. */ +#ifndef VM + file_close (file); +#endif lock_release (&filesys_lock); return success; } @@ -696,6 +701,7 @@ load_segment (struct file *file, off_t ofs, uint8_t *upage, ASSERT (pg_ofs (upage) == 0); ASSERT (ofs % PGSIZE == 0); +#ifdef VM while (read_bytes > 0 || zero_bytes > 0) { /* Calculate how to fill this page. @@ -716,6 +722,57 @@ load_segment (struct file *file, off_t ofs, uint8_t *upage, upage += PGSIZE; } return true; +#else + 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; + + /* Check if virtual page already allocated */ + struct thread *t = thread_current (); + uint8_t *kpage = pagedir_get_page (t->pagedir, upage); + + if (kpage == NULL){ + + /* Get a new page of memory. */ + kpage = get_usr_kpage (0, upage); + if (kpage == NULL){ + return false; + } + + /* Add the page to the process's address space. */ + if (!install_page (upage, kpage, writable)) + { + free_usr_kpage (kpage); + return false; + } + + } else { + + /* Check if writable flag for the page should be updated */ + if(writable && !pagedir_is_writable(t->pagedir, upage)){ + pagedir_set_writable(t->pagedir, upage, writable); + } + + } + + /* Load data into the page. */ + if (file_read (file, kpage, page_read_bytes) != (int) page_read_bytes){ + return false; + } + memset (kpage + page_read_bytes, 0, page_zero_bytes); + + /* Advance. */ + read_bytes -= page_read_bytes; + zero_bytes -= page_zero_bytes; + upage += PGSIZE; + } + return true; +#endif } /* Create a minimal stack by mapping a zeroed page at the top of