Download Virtual Memory: Understanding the Use of Main Memory as a Cache for Secondary Storage and more Study notes Computer Science in PDF only on Docsity!
Lecture: Chp 7.
Virtual Memory
Memory Hierarchy
CPU Core
L1 Data
L1 Instr
L2 I&D (^) MemoryMain Disk
Typically, on-chip $$$$, Fastest!
$$$, Fast
$$, Slow
$, Slowest
What Access Time SRAM 5-25 ns DRAM 60-120 ns Magnetic disk 10-20 million ns
Larger memories Faster memories
Virtual Memory
• The secondary storage (disk) is part of the
memory hierarchy
• Certainly, disks can hold files
• But can we do more?
….. Yes!
• Virtual memory: Treat main memory as a
cache for secondary storage
Virtual Memory
Processor L2 Cache
Physical Memory
Disk Storage
Virtual Memory
Virtual Memory
Processor L2 Cache
Physical Memory
Disk Storage
Virtual Memory
Physical memory caches currently used portion of virtual memory
Virtual Memory
Processor L2 Cache
Physical Memory
Disk Storage
Virtual Memory
Processor hardware caches works as always: hold a portion of physical memory
Virtual Memory
Advantages to virtual memory:
1. Illusion of more main memory
2. More than one program can share memory
3. Program relocation
4. Protection
Illusion of More Memory
• Physical (main) memory space doesn’t have
to be fully populated
- E.g., 32 bit address space is 4 GB, but we don’t
need that much actual main memory (modules)
• The “virtual memory space” can be larger
than the actual physical address space
- E.g., virtual address space of 42 bits vs. actual
main memory address space of 32 bits
Virtual Memory
Physical Memory
Disk Storage
Virtual Memory
Two programs are mapped into physical memory
Relocation
• VM is an easy way to support relocation
• Relocation: Program can be loaded into
physical memory at different starting
addresses each time the program is run -
noncontiguous
• Useful for multiprogramming - at run time,
you don’t know what is running, so it’s hard
to determine starting address permanently
when program is compiled
Virtual Memory
START
CODE
DATA
START
DATA
CODE
Physical Memory
Disk Storage
Virtual Memory
Here, the first virtual block has been mapped to the first physical memory block
Virtual Memory
START
CODE
DATA
DATA
START
CODE
Physical Memory
Disk Storage
Virtual Memory
Now, the data is the first physical block of memory. The program is unaware!!
Virtual Memory
CODE
DATA
DATA
CODE
DATA
DATA
Physical Memory
Disk Storage
Virtual Memory
Supposed you have a memory write to the virtual memory block marked as “code”.
Virtual Memory
WRITE
DATA
DATA
WRITE
DATA
DATA
Physical Memory
Disk Storage
Virtual Memory
Supposed you have a memory write to the virtual memory block marked as “code”.
Assuming no self-modifying code, that write shouldn’t be allowed
Virtual Memory
Virtual addresses
Physical memory caches part of the virtual space into a physical memory
Disk storage contains the virtual address space
Address translation (^) physical addresses
Processor generates the virtual address
Virtual address translated into actual address in memory or possibly disk storage
Virtual Memory Addresses
Virtual memory with four virtual pages, where each page is 16 bytes big
Virtual Memory Addresses
Virtual page number 0
Virtual page number 1
Virtual page number 2
Virtual page number 3
Page offset selects a byte on a page
Virtual page 0 Offset 3
Virtual Memory Addresses
Virtual page number 0
Virtual page number 1
Virtual page number 2
Virtual page number 3
Page offset selects a byte on a page
Virtual page 0 Offset 3
Virtual address (^) 00 0011 2-bit vpage number 4-bit vpage offset
Virtual Memory Addresses
Virtual page number 0
Virtual page number 1
Virtual page number 2
Virtual page number 3
Page offset selects a byte on a page
Virtual page ?? Offset ??
Virtual address (^) ?? ???? 2-bit vpage number 4-bit vpage offset
Virtual Memory Addresses
Virtual page number 0
Virtual page number 1
Virtual page number 2
Virtual page number 3
Page offset selects a byte on a page
Virtual page 2 Offset 7
Virtual address (^) 10 0111 2-bit vpage number 4-bit vpage offset
Page Faults
• What happens when vpage is on disk???
Page must be brought into memory!
• Page fault: A vpage needed is not in
memory but is on disk; I.e., a miss.
• Page faults are expensive:
- Millions of cycles for a disk access
Consequences
Large page sizes - amortize high cost
Reduce page fault rate
- fully associative structure
Page faults handled by SW (the OS)
- use clever algorithms; reduce page fault rate
Use a write-back strategy
- too expensive for write-through
Page Replacement
• High page fault cost
- Find ways to improve miss rate
• Let OS place page any where in memory
- Can be intelligent and pick page to replace that
is mostly likely not needed again (soon)
- Fully associative placement gives OS freedom
to pick location
Fully Associative Replacement
• Expensive - requires searching for match
- I.e., is a given vpage mapped to a ppage?
• A better way - use a table lookup
- So-called “page table”
- One entry per vpage in the PT
- Page table indexed by vpage number
- (Yes, the table is large….)
Page Tables
• Each process has its own PT stored in
memory starting at address in PAR
• PT and PAR are part of the process context
(along with the PC, SP, registers …)
• Each memory reference (hit) requires two
memory operations???
• Each page fault requires a memory
operation and a disk operation.
Page Tables
Page table register
(^20 )
18
31 30 29 28 27 15 14 13 12 11 10 9 8 3 2 1 0
29 28 27 15 14 13 12 11 10 9 8 3 2 1 0
Virtual address Virtual page number page offset Physical page number
Physical page number page offset Physical address
If 0 then page is not in memory
page table
Valid
Address Translation & the PT
(1) HW uses PAR to find the process’ PT
(2) In PT, find PTE for the given vpage
(3) If PTE.valid == 1, page is in memory and
use page to determine physical address
(4) If PTE.valid == 0, page is on disk and
must be loaded into memory (replacing an
existing page mapped in memory)
Page Faults
• When virtual page is not mapped to a
physical page, then OS gains control and
loads that page into memory
Two issues:
(1) Where is vpage on the disk?
(2) Where to place vpage in memory?