You are on page 1of 26

Pintos Project #3-1: Virtual Memory

Computer Science Department, KAIST

Virtual Memory(VM)
Why should we use VM? To use memory larger than physical memory To protect user programs memory area(isolation)
Each programs cannot access others memory

To make user programming easy(abstraction)


Regardless of physical memory size

Virtual Memory(VM)

Virtual Memory(VM)
OS make an illusion of using large pages Each processes think they have 4GB own memory VM makes it possible Maps from user memory to physical memory If there is excess memory usage, evict it to secondary storage

Virtual Memory(VM)

Page Mapping
Page A continuous region of virtual memory processing Usually 4KB (12-bit offset) in length Frame in physical memory

Page Table Management

Find vaddr.h and pagedir.c/h for its interface

Pintos Project #3-1


To implement virtual memory on Pintos Project #3-1(3 weeks)
Page Table Management Page Swapping Stack Growth

Project #3-2(3 weeks)


Lazy Loading Memory Mapped File

What to do?
Current Pintos VM 2-level page table is already implemented
pagedir.c, pte.h *pagedir in struct thread

But incomplete page table management Nothing is done when page fault occurs(just failure) Cannot use virtual memory larger than physical memory You should implement... Supplement page table to manage swap
Hash structure is useful

Swap in/out page


Using current interface is strongly recommended

Stack growth

Before you start


Must read Pintos document carefully!! 4. Project 3: Virtual Memory A.5 Memory Allocation ~ A.8. Hash Table userprog/pagedir.c Page table management code threads/palloc.c Functions to allocate/free page Need to understand relation between user-pool & kernel-pool
10

Before you start


userprog/exception.c page_fault() threads/pte.h Need to understand means of each flags
PG_A, PG_W, PG_P, etc..

These flags are useful for finding victim and evicting pages device/disk.c Low level disk read/write functions(using sector number) lib/kernel/hash.c, lib/kernel/bitmap

11

1) Page Table Management


Implement mapping table 2-level page table is already implemented
Just direct mapping from VA to PA Cannot use virtual memory larger than physical memory

Implement supplemental page table


Use hash structure

Translate from VA to PA
Store this mapping information in also hash table What is virtual address and what is physical address in Pintos? How can we catch the moment that page is allocated?

Why cant we use current page table implementation?


How does current pintos map from VA to PA? Read source code very carefully!
12

1) Page Table Management

13

1) Page Table Management


Implement mapping table Related functions:
pagedir.c
pagedir_set_page, pagedir_get_page pagedir_clear_page, lookup_page ...

palloc.c
palloc_get_multiple, palloc_free_multiple

Most of functions in pagedir.h, bitmap.c and hash.c

14

2) Swap-In & Swap-Out


Evict page(Swap-out) When physical memory is full Find victim page to swap out
Policy is up to you (LFU, LRU, FIFO, second chance) But you should consider performance because of timeout

Write victim page on swap-disk


You should consider that page size is 4KB but sector size of disk is 512bytes

Modify PTE(original page table) and (your) page table Replace new page(if needed) Related functions :
disk.c
disk_get, disk_read, disk_write

15

2) Swap-In & Swap-Out

16

2) Swap-In & Swap-Out


Page fault(Swap-in + Swap-out caused by swap-in) When page fault occurs Check that this page is evicted to swap-disk
If invalid page, exit with status -1

If evicted page, read it from disk to physical memory


But!! check that physical memory is full or not. If full, find victim and evict it before read.

Modify PTE(original page table) and (your) page table Be careful for thrashing
Why? When does thrashing occur?

Related functions :
exception.c
page_fault

17

2) Swap-In & Swap-Out

18

2) Swap-In & Swap-Out


Pintos has 4 hard-disk devices There are two numbers that has value 0 or 1. Former is channel number and latter is device number. 0 is master and 1 is slave. Swap-disk is 1:1
This is needed when use disk_get

Creating swap-disk
pintos-mkdisk swap.dsk n
n is size of disk (MB)

Pintos --swap-disk=n
19

3) Stack Growth
Current Pintos Implementation The stack consists of only a single page(4KB)
This is set as stack_setup() in process.c

If program tried to use stack larger than 4KB, page fault occurs Your Mission Make stack grow up on demand Just allocate a new page contains faulted address for stack
20

3) Stack Growth
How can we know that faulted address is for stack or not? This page fault is caused by stack growth? Or caused by accessing just invalid page near to stack? This problem is very tricky!! Recognizing algorithm is up to you f->esp is useful Add algorithm to page fault handler
21

Tip
-ul option is very useful Limit the size of user pool Easy test for swap in/out ex> pintos -ul=16

22

Etc.
This project should be built on top of project 2. Test programs from project 2 should work with project 3 We wont provide project 2 source code for you. You should complete project 2 before project 3 You must pass 15 tests + userprog tests tests/vm/pt-bad-addr, tests/vm/pt-bad-read tests/vm/pt-write-code, tests/vm/pt-write-code2 tests/vm/page-linear, tests/vm/page-parallel tests/vm/page-merge-seq, tests/vm/page-merge-par tests/vm/page-merge-stk, tests/vm/page-shuffle tests/vm/pt-grow-stack, tests/vm/stk-sc tests/vm/pt-big-stk-obj, tests/vm/pt-grow-pusha tests/vm/pt-grow-bad

23

Etc.
There is no file in VM directory You should create from nothing!! Fortunately, there is some hint in pintos document

24

Etc.
We recommend you to use the course board to ask question!! So, other team can share your question!!

25

Submission
Due 11/4 (Mon) 23:59
Note that therell be a blackout at 11/3

Submit to KLMS Contents


Source code (archive of pintos/src directory) Design Documentation

Follow the submission format. Cheating will not be forgiven.


26

You might also like