tutorial8

advertisement
CSC369: OPERATING SYSTEM
Tutorial 8
March 9, 2012
TA: Europa Shang
ASSIGNMENT 3

Implement paging by writing the following functions:
lpage_fault: handles a page fault
 lpage_evit: evicts an logical page from physical memory
 page_replace: implements the page replacement policy

Sequential Replacement: FIFO
 Second Chance Replacement: Clock


No code reading questions
VIRTUAL MEMORY

Processes need more logical address
space than physically available
Need to manage what is in memory
 Swap pages on request


When a page is requested but not in
memory?


Page fault
When there is no more space in main
memory to bring in pages?

Replace and evict
MIPS TLB


TLB keeps track of mapping from virtual to physical pages
On memory read/write, check the entries in the TLB in parallel
(associative lookup):


Entry is found: TLB hit
Entry not found: TLB miss




Causes EX_TLBL for loads: reads
Causes EX_TLBS for stores: writes
Protection fault: trying to write to read-only memory causes EX_MOD (modify)
TLB Exceptions

mips_trap calls vm_fault for any TLB exception


Different types of VM_FAULT_* are passed on
Eventually gets to lpage_fault
LPAGE

The lpage entry maps the virtual address to the address in
physical memory and in swap space

On creation,




Request a block of swap space
Set physical address to INVALID
Update swap pointer
The lpage structure keeps track of where the page stores
In physical memory : lp_paddr
 On disk: lp_swapaddr



Low bits of lp_addr used to hold flags, e.g. DIRTY, PINNED
Read comments in src/kern/include/vmprivate.h
LPAGE OPERATIONS









lpage_create: creates an logical page object
lpage_destroy: deallocates a logical page, releases any RAM
or swap pages involved
lpage_lock: acquires the lock on an lpage
lpage_unlock: releases the lock on an lpage
lpage_lock_and_pin: locks the lpage page and pins the
underlying physical page in the coremap.
lpage_copy: creates a new lpage and copy data from another
lpage
lpage_zerofill: creates a new lpage and arrange for it to be
cleared to all zeros
lpage_fault: handles a fault on a specific lpage
lpage_evict: evicts an lpage from physical memory
VM_OBJECT

A vm_object is a data structure that defines a a valid block of
process virtual memory
Defiend in src/kern/include/vmprivate.h
 Created by as_define_region


Each vm_object contains a base virtual address and an array
of logical pages

The address space is a collection of vm_objects

Redzone: a possible guard band against other vm_objects
Simple operations: create, destroy, copy, etc.

lpage and vm_object

From virtual address, can the corresponding vm_object
 In the vm_object, can find the index of the corresponding lpage
structure

COREMAP

Logical pages are nice, but we ultimately need to work
with physical memory


Need to keep track of physical pages
Coremap maps pages in memory to their virtual address.

Reverse page table
One entry per page frame
 Indexed by physical page number
 Contain the virtual page number of address space that is occupying
the page frame.

COREMAP
TLB management and handling
 Page replacement policy
 Keep track of the number of

Maximum number of physical pages
 Number of kernel pages
 Number of user pages
 Number of pages that are free


Maintain information whether a page has been pinned
for manipulation
COREMAP_ENTRY
struct coremap_entry {
struct lpage *cm_lpage;
volatile
int cm_tlbix:7;
unsigned cm_cpunum:5;
unsigned cm_kernel:1,
cm_notlast:1,
cm_allocated:1;
volatile
unsigned cm_pinned:1;
};

A coremap entry has bit flags that indicate if pages are
kernel pages, pinned (busy), etc.

May add other flags to support your design
COREMAP_ENTRY V.S. LPAGE

Each lpage is a logical piece of memory
The memory may be in memory or in swap (on disk)
 A lpage points to the location of its data


The coremap maps the physical memory storage
When you need physical memory, consult the coremap to
see what memory is free.
 A coremap entry points to a lpage

COREMAP OPERATIONS

tlb_replace: returns index of TLB entry to replace

TLB replacement algorithm.
tlb_invalidate: marks a given TLB entry as invalid
 tlb_clear: flushes the TLB by loading it with invalid
entries
 tlb_unmap: searches the TLB for a specific virtual
address translation and invalidates it if it exits
 mips_getslot: gets a TLB slot for use, replacing an
existing one if necessary and performing any atreplacement actions
 page_replace: returns an index into the coremap for
the selected victim page

COREMAP OPERATIONS




coremap_bootstrap: allocates memory for the coremap and
initlaizes its entries
do_evict: performs the eviction of the specific slot in coremap
do_page_replace: starting point for the page replacement code,
calls page_replace and do_evict
mark_page_allocated: update coremap entries for newly allocated
pages and update coremap counters





Called from coremap_alloca_one_page and coremap_alloc_multipages
coremap_free: deallocates a specific physical address and any
subsequent pages allocated in the same block
coremap_pin: marks a specific page pinned for manipulation of its
contents
coremap_unpin: unpins a specific page
mmu_map: enters a translation into the MMU.
PAGE FAULTS

Minor fault: desired page is in memory but not found in
TLB

Update the TLB and coremap


coremap.c : mmu_map
Major fault: desired page is not in memory
It’s either in swap space, or hasn’t been created yet
 How do you know if it’s a major fault?



Page hasn’t been created yet


lp_paddr is INVALID_PADDR if the page is not in memory
A new page is allocated to the process and initialized (zero-filled)
 In src/kern/vm/addrspace.c: as_fault
Page is on disk

We need to swap the page into memory from swap space
PAGE REPLACEMENT
Updating the victim’s PTE to show that it is in swap
 Evicting/invalidating victim’s PTE from the TLB


Copying it to disk iff its dirty
Loading the new page into memory
 Updating the new page’s PTE and inserting it into the
TLB

SYNCHRONIZATION


OS/161 assumes that lpage, vm_objects and address spaces
are not shared
But one thread may access an lpage belonging to another
thread, in order to evict a page

Don’t need locks when accessing address space and vm_objects,
but lpages do need synchronization


Bit lock is used to save space: lpage_lock, lpage_unlock
Locks for the Virtual Memory system


global_paging_lock: limits number of pages pinned at any one time
swaplock: synchronizes swapmap and counters




Used by swap_alloc, swap_free, swap_reserve, swap_unreserve
cm_pinned: locks pages that are being used
One bit lock per lpage
Lock ordering, i.e., you should acquire in this order

global_paging_lock BEFORE coremap pages BEFORE lpages
PAGE_REPLACE

page_replace takes no arguments and returns an index
into the coremap for the selected victim page.

You will write two versions of this function.
Sequential Page Replacement
 Clock Page Replacement


Configuration


ASST3-FIFO
ASST3-CLOCK
PAGE_REPLACE: FIFO

Sequential Page Replacement
Select page to be evicted from the coremap sequentially.
 Skip pinned pages and kernel pages


Tips

Map need a global variable for the head of the queue
PAGE_REPLACE: LRU

Clock Page Replacement
Scans pages in coremap sequentially.
 Skip pinned pages, kernel pages, and pages with the
reference bit set.
 Turn off the reference bit if a page is skipped because the
reference bit was 1.


Tips
May need a bit field in coremap_entry for the reference bit
 May need a global variable for the clock hand
 May need to modify several functions for the reference bit

LPAGE_FAULT AND LPAGE_EVICT

lpage_fault: handles a fault on a specific lpage.


If a page is resident in memory, get a physical page from coremap
and swap it in.
lpage_evict: evicts an lpage from physical memory to swap
space

Action:



Evict the contents of the page at lp_paddr by writing it to lp_swapaddr
on the swap device (if it is dirty), and mark lp_paddr as invalid
Called by do_evict when a physical page is chosen for eviction
Synchronization
Stats counters should be updated under global lock protection
 Read the comments in the code

Download