160 likes | 383 Views
Project 3: Demand Paging. Tingxin Yan 4/13/2011. Processor. Control. Tertiary Storage (Tape). Secondary Storage (Disk). Second Level Cache (SRAM). Main Memory (DRAM). On-Chip Cache. Datapath. Registers. 10,000,000s (10s ms). Speed (ns):. 1s. 10s-100s. 100s.
E N D
Project 3: Demand Paging Tingxin Yan 4/13/2011
Processor Control Tertiary Storage (Tape) Secondary Storage (Disk) Second Level Cache (SRAM) Main Memory (DRAM) On-Chip Cache Datapath Registers 10,000,000s (10s ms) Speed (ns): 1s 10s-100s 100s 10,000,000,000s (10s sec) Size (bytes): 100s Ks-Ms Ms Gs Ts Memory Hierarchy of a Modern Computer System • Take advantage of the principle of locality to:
Caching Applied to Address Translation • TLB (Translation Lookaside Buffer)
Nachos: Software-Loaded TLB • How can a process run without access to page table? • Fast path (TLB hit with valid=1): • Translation to physical page done by hardware • Slow path (TLB hit with valid=0 or TLB miss) • Hardware receives a “TLB Fault” • What does OS do on a TLB Fault? • Traverse page table to find appropriate PTE • If valid=1, load page table entry into TLB, continue thread • If valid=0, perform “Page Fault” detailed previously • Continue thread • Everything is transparent to the user process: • It doesn’t know about paging to/from disk • It doesn’t even know about software TLB handling
Virtual Memory Access @Override handleRead(int a0,int a1,int a2) • Check if address is valid (already provided!) • Look up address • UtilizeVMKernelvmk • VMKernal method: lookupAddress(intpid, intvpn) • Pin the read page to avoid swapping out. • Call super.handleRead • Pin Pages • Unpin the read page. (Similar to a lock) • 5 lines of code. • handleWrite: very similar.
Virtual Memory Access @Override readVirtualMemory(intvaddr, byte[] data, int offset, int length) • Check if address is valid (already provided!) • Set Used Bits • Call member function directly. • call the original readVirtualMemory in the UserProcess class. • Super.readVirtualMemory()… • 2 lines of code.
Swap pages Memory Swap file
Swap pages Memory Swap file
Swap pages Memory Swap file
Swap pages in swapInPage(intpid, intvpn) • Find the file position in swap file (denoted by swapfilepositions) • Read the page from the file. • Get a new free physical page, swapping something else out if needed. • Copy contents of the read-in page to memory. • Place a new entry in the global page table for thenew page. • Remove the page from the list of swapped out pages. • ~ 20 lines of code
Swap Page in – Data Structures • OpenFileswapFile: Stores pages that needed to be written to disk. • freeSwapPages: Stores a list of available swap pages in the swap fileso that pages can be written to disk • LinkedList<Integer> • swapPagePositions: Keeps track of where a given page is stored in the swap file • HashMap<PageId, Integer> • pagesInSwap: Stores all of the pages that are currently swapped out to disk • HashMap<PageId, TranslationEntry> • TranslationEntrygetFreePage(): method to get a free physical page. • swappablePages: Stores a list of swappable pages • ArrayList<TranslationEntry>
Swap page out • Find one page from swappablePages to swap out. • Opposite sequence of operations against swap page in. • Find and remove an entry in the global pagetable. • Copy the contents of the page in memory to a buffer for writing. • Write the page to a free position in the swap file. • Need to lock your operations. • Shared resources: swapPages, pinnedPages. • Notice that pinned pages should not be swapped out.
TLB miss handler • Software traversed Page tables (like Nachos) • On TLB miss, processor receives TLB fault • handleException(int cause) • cause = Processor.exceptionTLBMiss • Again, use vmk.lookupAddress() to find the address. • handlePageFault.