Compare commits
4 Commits
virtual-me
...
vm/page-sw
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1b73e415d7 | ||
|
|
47a7dfae04 | ||
|
|
9a3c8a1c38 | ||
|
|
08eafcf7ef |
@@ -1,10 +1,18 @@
|
||||
#include "userprog/exception.h"
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include "stdbool.h"
|
||||
#include "userprog/gdt.h"
|
||||
#include "threads/interrupt.h"
|
||||
#include "threads/thread.h"
|
||||
#include "userprog/pagedir.h"
|
||||
#ifdef VM
|
||||
#include "vm/stackgrowth.h"
|
||||
#include "vm/frame.h"
|
||||
#include "vm/page.h"
|
||||
#include "devices/swap.h"
|
||||
#include "threads/vaddr.h"
|
||||
#endif
|
||||
|
||||
/* Number of page faults processed. */
|
||||
static long long page_fault_cnt;
|
||||
@@ -147,12 +155,39 @@ page_fault (struct intr_frame *f)
|
||||
user = (f->error_code & PF_U) != 0;
|
||||
|
||||
#ifdef VM
|
||||
if (user && not_present)
|
||||
struct thread *t = thread_current ();
|
||||
if (user)
|
||||
{
|
||||
if (not_present)
|
||||
{
|
||||
/* Check if the non-present user page is in the swap partition.
|
||||
If so, swap it back into main memory, updating the PTE for
|
||||
the faulted virtual address to point to the newly allocated
|
||||
frame. */
|
||||
if (page_in_swap (t, fault_addr))
|
||||
{
|
||||
size_t swap_slot = page_get_swap (t, fault_addr);
|
||||
void *upage = pg_round_down (fault_addr);
|
||||
void *kpage = frame_alloc (0, upage, t);
|
||||
swap_in (kpage, swap_slot);
|
||||
|
||||
bool writeable = pagedir_is_writable (t->pagedir, upage);
|
||||
if (pagedir_set_page (t->pagedir, upage, kpage, writeable)) return;
|
||||
}
|
||||
|
||||
/* Handle user page faults that need to be resolved by dynamic
|
||||
stack growth by checking if this is such a fault and responding
|
||||
accordingly. */
|
||||
if (handle_stack_fault (fault_addr, f->esp)) return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Handle kernel page faults that need to be resolved by dynamic stack
|
||||
growth by checking if this is such a fault and responding
|
||||
accordingly. */
|
||||
if (not_present && handle_stack_fault (fault_addr, t->curr_esp)) return;
|
||||
|
||||
f->eip = (void *)f->eax;
|
||||
f->eax = 0xffffffff;
|
||||
return;
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
#include "threads/palloc.h"
|
||||
|
||||
static uint32_t *active_pd (void);
|
||||
static void invalidate_pagedir (uint32_t *);
|
||||
|
||||
/* Creates a new page directory that has mappings for kernel
|
||||
virtual addresses, but none for user virtual addresses.
|
||||
@@ -278,7 +277,7 @@ active_pd (void)
|
||||
This function invalidates the TLB if PD is the active page
|
||||
directory. (If PD is not active then its entries are not in
|
||||
the TLB, so there is no need to invalidate anything.) */
|
||||
static void
|
||||
void
|
||||
invalidate_pagedir (uint32_t *pd)
|
||||
{
|
||||
if (active_pd () == pd)
|
||||
|
||||
@@ -17,5 +17,6 @@ void pagedir_set_accessed (uint32_t *pd, const void *upage, bool accessed);
|
||||
bool pagedir_is_writable (uint32_t *pd, const void *upage);
|
||||
void pagedir_set_writable (uint32_t *pd, const void *upage, bool writable);
|
||||
void pagedir_activate (uint32_t *pd);
|
||||
void invalidate_pagedir (uint32_t *pd);
|
||||
|
||||
#endif /* userprog/pagedir.h */
|
||||
|
||||
@@ -93,6 +93,13 @@ frame_alloc (enum palloc_flags flags, void *upage, struct thread *owner)
|
||||
ASSERT (victim != NULL); /* get_victim () should never return null. */
|
||||
|
||||
/* 2. Swap out victim into disk. */
|
||||
/* Mark page as 'not present' and flag the page directory as having
|
||||
been modified *before* eviction begins to prevent the owner of the
|
||||
victim page from accessing/modifying it mid-eviction. */
|
||||
pagedir_clear_page (owner->pagedir, upage);
|
||||
|
||||
// TODO: Lock PTE of victim page for victim process.
|
||||
|
||||
size_t swap_slot = swap_out (victim->frame);
|
||||
page_set_swap (victim->owner, victim->upage, swap_slot);
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
#define ADDR_START_BIT 12
|
||||
|
||||
/* 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. */
|
||||
to flag the page as being stored in swap, and stores the specified swap slot
|
||||
value in the entry at the address bits for later retrieval from disk. */
|
||||
void
|
||||
page_set_swap (struct thread *owner, void *upage, size_t swap_slot)
|
||||
{
|
||||
@@ -19,9 +19,17 @@ page_set_swap (struct thread *owner, void *upage, size_t swap_slot)
|
||||
uint32_t swap_slot_bits = (swap_slot << ADDR_START_BIT) & PTE_ADDR;
|
||||
*pte = (*pte & PTE_FLAGS) | swap_slot_bits;
|
||||
|
||||
/* Mark page as 'not present' and flag the page directory as having
|
||||
been modified. */
|
||||
pagedir_clear_page (owner->pagedir, upage);
|
||||
invalidate_pagedir (owner->pagedir);
|
||||
}
|
||||
|
||||
/* Returns true iff the page with user address 'upage' owned by 'owner'
|
||||
is flagged to be in the swap disk via the owner's page table. */
|
||||
bool
|
||||
page_in_swap (struct thread *owner, void *upage)
|
||||
{
|
||||
uint32_t *pte = lookup_page (owner->pagedir, upage, false);
|
||||
return pte != NULL &&
|
||||
(*pte & (1 << SWAP_FLAG_BIT)) != 0;
|
||||
}
|
||||
|
||||
/* Given that the page with user address 'upage' owned by 'owner' is flagged
|
||||
@@ -32,8 +40,8 @@ page_get_swap (struct thread *owner, void *upage)
|
||||
{
|
||||
uint32_t *pte = lookup_page (owner->pagedir, upage, false);
|
||||
|
||||
ASSERT (pte != NULL);
|
||||
ASSERT ((*pte & PTE_P) == 0);
|
||||
ASSERT ((*pte & (1 << SWAP_FLAG_BIT)) != 0);
|
||||
|
||||
/* Masks the address bits and returns truncated value. */
|
||||
return ((*pte & PTE_ADDR) >> ADDR_START_BIT);
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "threads/thread.h"
|
||||
|
||||
void page_set_swap (struct thread *, void *, size_t);
|
||||
bool page_in_swap (struct thread *, void *);
|
||||
size_t page_get_swap (struct thread *, void *);
|
||||
|
||||
#endif /* vm/frame.h */
|
||||
|
||||
Reference in New Issue
Block a user