ms108 computer system i lecture 11 virtual memory prof. xiaoyao liang 2015/5/22 1
TRANSCRIPT
![Page 1: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/1.jpg)
MS108 Computer System I
Lecture 11 Virtual Memory
Prof. Xiaoyao Liang 2015/5/22 1
![Page 2: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/2.jpg)
2
Why Virtual Memory?Why Virtual Memory?
• An example: MIPS64 is a 64-bit architecture allowing an address space defined by 64 bits– Maximum address space:
• 264 = 16 x 1018 =16,000 petabytes• peta = 1015
• This is several orders of magnitude larger than any realistic and economical (or necessary for that matter) physical memory system
![Page 3: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/3.jpg)
3
Virtual MemoryVirtual Memory• Originally invented to support program sizes larger
than then-available physical memory– later on it finds applications in multi-programming and
virtual machines• Virtual memory is as large as the address space
allowed by the ISA…but– only a portion of the address space resides in physical
memory at any given time– the rest is kept on disks and brought into physical
memory as needed– virtual memory can be viewed as providing an interfaceinterface
between the physical main memory and disk storage• this is similar to the role that cache plays for main memory
(recall that cache size << main memory size)
![Page 4: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/4.jpg)
4
Motivations for Virtual MemoryMotivations for Virtual Memory
• (1) Use Physical DRAM as a Cache for the Disk– Address space of a process (program) can exceed
physical memory size– Sum of address spaces of multiple processes can
exceed physical memory• (2) Simplify Memory Management
– Multiple processes reside in main memory.• Each process with its own address space
– Only “active” code and data are actually in memory• Allocate more memory to process as needed.
![Page 5: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/5.jpg)
5
Motivation #1: DRAM a “Cache” for Motivation #1: DRAM a “Cache” for DiskDisk
• Full address space is quite large:–32-bit addresses: 0—4,294,967,295 (~ 4 billion bytes)–64-bit addresses: 0—18,446,744,073,709,551,615 (~ 16,000 petabytes)
• Disk storage is ~500X cheaper than DRAM storage–80 GB of DRAM: ~ $25,000–80 GB of disk: ~ $50
• To access large amounts of data in a cost-effective manner, the bulk of the data must be stored on disk
1GB: ~$300 160 GB: ~$100
4 MB: ~$500
DiskDRAMSRAM
![Page 6: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/6.jpg)
6
DRAM vs. SRAM as a DRAM vs. SRAM as a “Cache”“Cache”
• DRAM vs. disk is more extreme than SRAM vs. DRAM– Access latencies:
• DRAM ~10X slower than SRAM• Disk ~100,000X slower than DRAM
– Bottom line: • Design decisions made for DRAM caches driven by enormous cost of
misses
DRAMSRAM Disk
![Page 7: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/7.jpg)
7
Multi-programmingMulti-programming
• Multiple programs run concurrently, each with the illusion that it owns all the machine resources
• Each program requires memory space
![Page 8: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/8.jpg)
8
Multiprogramming and Multiprogramming and Memory ProtectionMemory Protection
• In the early machines, I/O operations were slow– having the CPU wait pending completion of I/O operation was a
waste of precious machine cycles
• Higher throughput is achieved if CPU and I/O of 2 or more programs were overlapped
• How to overlap execution of multiple programs?– use multiprogramming
• How to protect programs from one another in a multiprogramming environment?– use a bound register
CPU CPU CPU
I/O I/O I/O
![Page 9: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/9.jpg)
9
Away With Absolute AddressingAway With Absolute Addressing
• In early (1950) machines only one program ran at any given time, with unrestricted access to the entire machine (RAM + I/O devices)
• Addresses in a program were location-dependent– they depended upon where the program was to be loaded in memory
– so when programmers wrote code, they had to have a pretty good idea where in memory they should load the program
– but it was more convenient for programmers to write location-independent programs
• How to achieve location-independence?– a base register was used to support re-locatable code
![Page 10: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/10.jpg)
10
Base & Bound RegistersBase & Bound Registers
• Base & bound registers loaded from the process table when the program is context-switched
• Permits multiple memory-resident concurrent programs• Protects programs from one another
![Page 11: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/11.jpg)
11
Program SizeProgram Size
• Program is copied from disk storage to main memory by the operating system– program counter is then set
to the beginning of the program
• What if program is too big?– original DOS would not let
you runMIPS64 address space is:264 = 16 x 1018 =16,000 petabytes (peta = 1015)
This is several orders of magnitude larger thanany realistic memory device .
MIPS64 address space is:264 = 16 x 1018 =16,000 petabytes (peta = 1015)
This is several orders of magnitude larger thanany realistic memory device .
![Page 12: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/12.jpg)
12
Virtual MemoryVirtual Memory
• Address translation • Program “sees” entire VM address
space• Program is run in physical memory
which is typically smaller than the address space
• Pages of address space are swapped in/out of disk storage as needed
• Strictly speaking VM is required to overcome limitation on the size of physical memory
– but VM is extended in a natural way to support multiprogramming & memory protection
– There are machines where the physical memory space is larger than the virtual memory space (e.g., PDP-11)
Disk
Physical memory
Virtual memory
Addresstranslation
![Page 13: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/13.jpg)
13
Advantages of Virtual Advantages of Virtual MemoryMemory
• Translation– program has a consistent view of a contiguous memory, even though
physical memory is scrambled
– Allows multi-programming
– relocation: allows the same program to run in any location in physical memory
• Protection– different processes are protected from each other
– different pages can have different behavior (read-only; user/supervisor)• kernel code/data protected from user programs
• Sharing– can map same physical memory to multiple processes (shared memory)
![Page 14: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/14.jpg)
14
How VM WorksHow VM Works
• On program startup– OS loads as much of the program as possible into RAM;
this includes enough code to start execution– if program size exceeds allocated RAM space the
remainder is maintained on disk
• During execution– if program needs a code segment not resident in RAM, it
fetches the segment from disk into RAM– if there is not enough room in RAM, some resident pages
must be evicted to make room– if evicted pages are “dirty”, they must be updated on disk
![Page 15: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/15.jpg)
15
Address Translation
![Page 16: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/16.jpg)
16
VA-to-PA Address TranslationVA-to-PA Address Translation
Processor0x00 0x040x04 0x000x08 0x120x12 Disk
Lw R1, 0(R4)
add R1, R2, R4
Sub R5, R2, R1
BNEZ R5, 0x64
0x00
0x04
0x08
0x12
Disk
Virtualaddress
VA PA
Main memory
• Programs use virtual addresses (VA) for data & instructions• VA translated to physical addresses (PA) using the Page Table• Instruction/data fetched/updated in memory
Page table
![Page 17: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/17.jpg)
17
Page Table
Virtual page number Page offset
0111231
Physical page number Page offset
0111223
Page table
• Memory organized in pages (similar to blocks in cache)
• Page size is 4-8 Kbytes
Virtual address
Physical address
![Page 18: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/18.jpg)
18
VA to PA TranslationPage o
ffse
t
Virtual address space
Physical address space
Page frame
Virtual page number Page offset
0111263
• Page table maps the base address of virtual and physical page frames
• Page offset need not be used in VA=>PA translation because virtual and physical block sizes are the same
• Page table maps the base address of virtual and physical page frames
• Page offset need not be used in VA=>PA translation because virtual and physical block sizes are the same
PageTable
Page o
ffse
t
![Page 19: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/19.jpg)
19
Multiprogramming View of VM
VA 1
VA 1
VA 1
Program 1
Program 2
Program 3
Page Table 1
1
2
3
Page Table 2
1
2
3
4
Page Table 3
1
2
3
4
Physical Memory
• Each application has a separate page table
• Page table contains an entry for each user page
• Page table base pointer maintained by OS for each process
• Each application has a separate page table
• Page table contains an entry for each user page
• Page table base pointer maintained by OS for each process
Page table base pointer 3
Page table base pointer 2
Page table base pointer 1
![Page 20: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/20.jpg)
20
Page Table Structure
0x00000
0x00001
0x00002
0x00003
0x00004
0x00005
0x00006
0x00007
V Physical page number
0xFFFFF
... ......
0111231Page offsetVirtual page number
0111223
Page offset <=> page size12 bits: 4096 bytes
Page offset <=> page size12 bits: 4096 bytes
Virtual page number <=> page table size20 bits: ~1 million
Virtual page number <=> page table size20 bits: ~1 million
Physical address
![Page 21: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/21.jpg)
21
Determining Page Table Size• Assume
– 32-bit virtual address– 30-bit physical address– 4 KB pages => 12 bit page offset– Each page table entry is one word (4 bytes)
• How large is the page table?– Virtual page number = 32 - 12 = 20 bits– Number of entries = number of pages = 2^20– Total size = number of entries x bytes/entry = 2^20 x 4 = 4 Mbytes– Each process running needs its own page table
![Page 22: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/22.jpg)
22
Page Fault• How is it known whether the page is in memory?
– Maintain a valid bit per page table entry– valid bit is set to INVALID if the page is not in memory – valid bit is set to VALID if the page is in memory
• Page fault occurs when a page is not in memory– fault results in OS fetching the page from disk into DRAM– if DRAM is full, OS must evict a page (victim) to make room– if victim is dirty OS updates the page on disk before fetch– OS changes page table to reflect turnover
• After a page fault and page-fetching, execution resumes at the instruction which caused the fault
![Page 23: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/23.jpg)
23
Page Faults (like “Cache Misses”)
CPU
Memory
Page Table
Disk
VirtualAddresses
PhysicalAddresses
CPU
Memory
Page Table
Disk
VirtualAddresses
PhysicalAddresses
Before fault After fault
![Page 24: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/24.jpg)
24
• Processor Signals Controller– Read block of length P starting at
disk address X and store starting at memory address Y
• Read Occurs– Direct Memory Access (DMA)– Under control of I/O controller
• I / O Controller Signals Completion– Interrupt processor
– OS resumes suspended process diskDiskdiskDisk
Memory-I/O busMemory-I/O bus
ProcessorProcessor
CacheCache
MemoryMemoryI/Ocontroller
I/Ocontroller
Reg
(2) DMA Transfer
(1) Initiate Block Read
(3) Read Done
Page Fault HandlingPage Fault Handling
![Page 25: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/25.jpg)
25
Page Fault HandlingPage Fault Handling
User program runs
Page fault
OS requests page
Disk read
Another user program runs
Disk interrupt
OS installs page
User program resumes
![Page 26: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/26.jpg)
26
Accelerating Virtual
Memory Operations
![Page 27: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/27.jpg)
Virtual Memory Hardware• Protection via virtual memory
– Keeps processes in their own memory space
• Role of architecture:– Provide user mode and supervisor mode– Protect certain aspects of CPU state– Provide mechanisms for switching between user mode
and supervisor mode– Provide mechanisms to limit memory accesses– Provide TLB to accelerate the address translation
![Page 28: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/28.jpg)
28
VM => PM TranslationVM => PM Translation
• Each program memory reference requires two memory accesses: one for VM => PM mapping and one for actual data/instruction– must make page table lookup as fast as possible
• Page table too big to keep in fast memory (SRAM) in its entirety– store page table in main memory– cache a portion of the page table in TLB (Translation Look-Aside Buffer)
Virtual page number Page offset
0111231
Physical page number Page offset
0111224
Page table
Virtual address
Physical address
![Page 29: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/29.jpg)
Virtual Addressing with a Cache• Thus it takes an extra memory access to
translate a VA to a PA
CPUTrans-lation
Cache MainMemory
VA PA miss
hitdata
• This makes memory (cache) accesses very expensive (if every access was really two accesses)
• Translation Lookaside Buffer (TLB) keeps track of recently used address mappings to avoid having to do a page table lookup
![Page 30: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/30.jpg)
30
Speeding Up Address Translation:Speeding Up Address Translation:
Translation Lookaside Buffer (TLB)Translation Lookaside Buffer (TLB)• TLB: A small and fast on-chip memory structure used for address translations.• If a virtual address is found in TLB (a TLB hit), the page table in main memory is not
accessed.
1
1
1
1
0
1
1
0
1
1
0
1
1
1
1
1
0
1
Physical Memory
Disk Storage
TLB (on-chip)128-256 Entries
Physical Page Address
Virtual Page Number
Page Table(in main memory)
Physical Pageor Disk Address
TagValid
Valid
128-256TLB Entries
![Page 31: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/31.jpg)
31
Translation Look-Aside Table (TLB)
Processor
TLB
Memory
• TLB maintains a list of most-recently used pages• Similar to instruction & data cache
– virtual address is used to index into the TLB– TLB entry contains physical address– takes ~ 1 clock cycle
• What if VM=>PM lookup and data/instruction lookup takes more than 1 clock cycle?– To avoid TLB lookup, remember the last VM =>
PM translation– if same page is referenced, TLB lookup can be
avoided
• TLB maintains a list of most-recently used pages• Similar to instruction & data cache
– virtual address is used to index into the TLB– TLB entry contains physical address– takes ~ 1 clock cycle
• What if VM=>PM lookup and data/instruction lookup takes more than 1 clock cycle?– To avoid TLB lookup, remember the last VM =>
PM translation– if same page is referenced, TLB lookup can be
avoided
![Page 32: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/32.jpg)
TLB / Cache Interaction
Valid Tag Data
Page offset
Page offset
Virtual page number
Virtual address
Physical page numberValid
1220
20
16 14
Cache index
32
Cache
DataCache hit
2
Byteoffset
Dirty Tag
TLB hit
Physical page number
Physical address tag
TLB
Physical address
31 30 29 15 14 13 12 11 10 9 8 3 2 1 0
![Page 33: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/33.jpg)
• Just like any other cache, the TLB can be organized as fully associative, set associative, or direct mapped
• TLBs are usually small, typically not more than 128 - 256 entries even on high end machines. This permits fully associative lookup on these machines. Most mid-range machines use small n-way set associative organizations.
CPUTLBLookup
Cache MainMemory
VA PA miss
hit
data
Trans-lation
hit
miss
20 tt1/2 t
Translationwith a TLB
Techniques for Fast Address Translation
![Page 34: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/34.jpg)
![Page 35: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/35.jpg)
35
TLB and Context Switch
• In multi-programming we need to use TLB for the active process – What to do with TLB at context switch?
• Too costly to clear TLB on every context switch• Keep track of page table entry in TLB per
process using ASID ( Address Space ID)
![Page 36: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/36.jpg)
36
TLB Organization
Virtual address Physical address Used Dirty Valid Access ASID 0xFA00 0x0003 N Y Y R/W 34 0x0040 0x0010 Y N Y R 0 0x0041 0x0011 Y1 N Y R 0
• Additional info per page– Dirty: page modified (if the page is swapped out, should the disk
copy be updated?)– Valid: TLB entry valid– Used: Recently used or not (for selecting a page for eviction)– Access: Read/Write– ASID:Address Space ID
• Additional info per page– Dirty: page modified (if the page is swapped out, should the disk
copy be updated?)– Valid: TLB entry valid– Used: Recently used or not (for selecting a page for eviction)– Access: Read/Write– ASID:Address Space ID
TagTag
![Page 37: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/37.jpg)
37
Example Consider a virtual memory system with the following properties:
32-bit virtual address (4 Gbytes) 26-bit physical memory (64 Mbytes) 4 Kbyte pages (12 bits)
a. How big is the page table for this memory system, assuming each page table entry has 4 overhead bits (valid, protection, dirty, use) and holds one physical page number? Give your answer in bits. b. Approximately how many pages of user data, at maximum, can we have in physical memory? c. If we wanted to keep the page table under 1Mbit in size, how big would we have to make the pages? Assume a page needs to be 2K bytes, and find the correct value of K.
![Page 38: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/38.jpg)
38
Part a32 30 9
32 3220
12
2 4 2 4 10
2 22
4 1000 2
1,000,000
virtual
virtual
AddressSpaceSize Bytes
AddressSpaceSizeNumberOfPages
PageSize
NumberOfPages
The page table must have 1,000,000 entries. To compute the size in bits note that the page offset need not be stored in the page table. There are 4 Kbytes per page requiring 12 bits of offset ( 3 124000 4 10 2 ). Thus each page table entry must map 20 bits of virtual page number (32-12=20) to (26-12=14) bits of physical page number. Taking into account the 4 overhead bits, each entry requires 18 bits. With 1,000,000 entries, the page table size is 18 megabits.
![Page 39: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/39.jpg)
39
Part b
26 2614
12
2 22
4 1000 2
16,000
physicalphysical
physical
AddressSpaceSizeNumberOfPages
PageSize
NumberOfPages
![Page 40: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/40.jpg)
40
Part c6 20
173
10 21 Megabits= Megabytes 2 Megabytes
8 2PageTableSize
Assuming that the page is 2K bytes, we need K bits for page offset in both the physical and virtual addresses. Thus each page entry requires:
30(26 ) 4 30 bits=
8
KPageEntrySize K K Bytes
Now we must calculate the number of entries (corresponding to the number of virtual pages) that must be kept in the page table:
32322
22
Kvirtual K
AddressSpaceSizeNumberOfPages
PageSize
The following relationship holds:
17 32
12
302 2
8
2 30 15 16
virtual
K
K
PageTableSize PageEntrySize NumberOfPages
K
K K
We must pick K=16 to stay under the specified limit of 1 Mbits of page table.
![Page 41: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/41.jpg)
41
Cache, Virtual Cache, Virtual Memory and Memory and
Virtual Virtual MachinesMachines
![Page 42: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/42.jpg)
42
Cache and Virtual Memory
• Which address to use to index to cache sets?– Physical address? May be slow due to translation– Virtual? Protection, process switching, aliasing
• Get the best of both: virtually indexed, physically tagged cache
• How to make a virtual cache work?– Page coloring
![Page 43: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/43.jpg)
43
Cache vs. Virtual Memory• Concept behind VM is almost identical to concept behind
cache.• But different terminology
– Cache: Block VM: Page– Cache: Cache Miss VM: Page Fault
• Caches implemented completely in hardware. VM implemented in software, with hardware support from the processor.
• Cache speeds up main memory access, while main memory speeds up VM access.
![Page 44: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/44.jpg)
44
VM & Caching ComparisonParameter First-level cache Virtual memory
Block (page) 16-128 bytes 4096-65,536 bytes Hit time 1-3 clock cycles 50-150 clock cycles Miss penalty 8-150 clock cycles 1,000,000-10,000,000 clock cycles Access time 6-130 clock cycles 800,000-8,000,000 clock cycles Transfer time 2-20 clock cycles 200,000-2,000,000 clock cycles Miss rate 0.1-10% 0.00001-0.001% Address mapping 25-45 bit physical address to 14-20 bit
cache address 32-64 bit virtual address to 25-45 bit physical address
CPU CacheMainMemory
Caching
CPUMainMemory
Virtual Memory
Disk
![Page 45: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/45.jpg)
45
Impact of These Properties on Design• Comparing to cache, how would we set the following design
parameters?– block size?
• Large, since disks perform better at transferring large blocks– Associativity?
• High, to minimize miss rate– Write through or write back?
• Write back, since can’t afford to perform small writes to disk• What would the impact of these choices be on:
– miss rate• Extremely low. << 1%
– hit time• Must match cache/DRAM performance
– miss latency (penalty)• Very high. ~20ms
![Page 46: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/46.jpg)
46
Associativity of VM • Cache miss penalty: 8-150 clock cycles• VM miss penalty: 1,000,000 - 10,000,000
clock cycles• Because of the high miss penalty, VM design
minimizes miss rate by allowing full associativity for page placement
![Page 47: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/47.jpg)
47
Write Strategies• Disk I/O slow (millions of clock cycles)• Always write-back; never write-through• Use dirty bit to decide whether to write disk
before eviction• Smart disk controllers buffers writes
– copy replaced page in buffer – read new page into main memory– write from buffer to disk
![Page 48: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/48.jpg)
Virtual Machines• Supports isolation and security• Sharing a computer among many unrelated users• Enabled by raw speed of processors, making the overhead
more acceptable
• Allows different ISAs and operating systems to be presented to user programs– “System Virtual Machines”– SVM software is called “virtual machine monitor” or “hypervisor”– Individual virtual machines run under the monitor are called
“guest VMs”
![Page 49: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/49.jpg)
VMM/Hypervisor• Managing HW and SW• Require system mode and user mode• Virtualize processors – privileged instructions will trap• Virtualize memory -- indirection/shadow page table• Virtualize I/O— space sharing or time multiplexing• Virtualize interrupt/exception – emulate
![Page 50: MS108 Computer System I Lecture 11 Virtual Memory Prof. Xiaoyao Liang 2015/5/22 1](https://reader036.vdocuments.site/reader036/viewer/2022062519/5697bfc51a28abf838ca6b7d/html5/thumbnails/50.jpg)
Impact of VMs on Virtual Memory
Each guest OS maintains its own set of page tables– VMM adds a level of memory between physical and
virtual memory called “real memory”– VMM maintains shadow page table that maps guest
virtual addresses to physical addresses• Requires VMM to detect guest’s changes to its own page
table• Occurs naturally if accessing the page table pointer is a
privileged operation
•Protections– Intel IA-64 four protection ring scheme– AMD X86-64 protection bit