Implement skeleton for swapping frames into disk

This commit is contained in:
Themis Demetriades
2024-11-26 18:59:46 +00:00
parent ea2725f606
commit 1e6b90da0d
6 changed files with 106 additions and 25 deletions

View File

@@ -116,7 +116,7 @@ process_execute (const char *cmd)
return tid;
}
static void *get_usr_kpage (enum palloc_flags flags);
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);
@@ -257,12 +257,13 @@ process_init_stack (char *cmd_saveptr, void **esp, char *file_name)
int pages_needed = DIV_CEIL (overflow_bytes, PGSIZE);
/* Allocate the pages and map them to the user process. */
void *upage;
uint8_t *kpage;
for (int i = 1; i < pages_needed + 1; i++)
{
uint8_t *kpage = get_usr_kpage (PAL_ZERO);
if (!install_page (((uint8_t *) PHYS_BASE) - PGSIZE * (i + 1),
kpage, true))
return false;
upage = ((uint8_t *) PHYS_BASE) - PGSIZE * (i + 1);
kpage = get_usr_kpage (PAL_ZERO, upage);
if (!install_page (upage, kpage, true)) return false;
}
}
@@ -710,7 +711,7 @@ load_segment (struct file *file, off_t ofs, uint8_t *upage,
if (kpage == NULL){
/* Get a new page of memory. */
kpage = get_usr_kpage (0);
kpage = get_usr_kpage (0, upage);
if (kpage == NULL){
return false;
}
@@ -752,11 +753,13 @@ setup_stack (void **esp)
{
uint8_t *kpage;
bool success = false;
kpage = get_usr_kpage (PAL_ZERO);
void *upage = ((uint8_t *) PHYS_BASE) - PGSIZE;
kpage = get_usr_kpage (PAL_ZERO, upage);
if (kpage != NULL)
{
success = install_page (((uint8_t *) PHYS_BASE) - PGSIZE, kpage, true);
success = install_page (upage, kpage, true);
if (success)
*esp = PHYS_BASE;
else
@@ -765,14 +768,20 @@ setup_stack (void **esp)
return success;
}
/* Claims a page from the user pool and returns its kernel address,
updating the frame table if VM is enabled. */
/* Claims a page from the user pool for ownership by the current thread
and returns its kernel address, updating the frame table if VM
is enabled. Requires the intended virtual address for where the page
will be installed. */
static void *
get_usr_kpage (enum palloc_flags flags)
get_usr_kpage (enum palloc_flags flags, void *upage)
{
void *page;
#ifdef VM
page = frame_alloc (flags);
struct thread *t = thread_current ();
if (pagedir_get_page (t->pagedir, upage) != NULL)
return NULL;
else
page = frame_alloc (flags, upage, t);
#else
page = palloc_get_page (flags | PAL_USER);
#endif