+ All Categories
Home > Documents > Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs,...

Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs,...

Date post: 04-Jan-2016
Category:
Upload: phyllis-bishop
View: 217 times
Download: 0 times
Share this document with a friend
Popular Tags:
70
Virtual Memory
Transcript
Page 1: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory

Page 2: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

DRAM as cache

• What about programs larger than DRAM?

• When we run multiple programs, all must fit in DRAM!

• Add another larger, slower level to the memory hierarchy - use part of the hard drive.

Page 3: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory

• Memory Size -

• Protection -

Page 4: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory

• Memory Size - allows total memory allocation to exceed DRAM capacity.

• Protection -

Page 5: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory

• Memory Size - allows total memory allocation to exceed DRAM capacity.

• Protection - programs may not access each other’s memory.

Page 6: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - no VM

• Program A begins Prog A

Page 7: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - no VM

• Program A begins

• Program B beginsProg A

Prog BWhat happens if A wants more memory?

Page 8: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - no VM

• Program A begins

• Program B beginsProg A

Prog BWhat happens if A wants more memory? Out of luck. If we gave A memory after the end of B, then A would be able to access all of B’s memory.

Page 9: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - no VM

• Program A begins

• Program B begins

• Program A endsProg B

Page 10: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - no VM

• Program A begins

• Program B begins

• Program A ends

• Program C ready Prog B

Prog C

Page 11: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - no VM

• Program A begins

• Program B begins

• Program A ends

• Program C ready– It can not run even

though there is enough free space

– Fragmentation

Prog B

Prog C

Page 12: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory

• Use hard drive to store running programs and use DRAM on demand

• Allocate memory in pages

• Provide protection by page

Page 13: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Address Space

• Virtual Address Space

• Physical Address Space

Page 14: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Address Space

• Virtual Address Space– Located on Hard Drive (essentially)– Starts at 0 for each program

• Physical Address Space

Page 15: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Address Space

• Virtual Address Space– Located on Hard Drive (essentially) – Starts at 0 for each program

• Physical Address Space– Located in DRAM– The “cache” for the Hard Drive

Page 16: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - VM

• Program A begins

• Program B beginsProg A

Hard Drive

Virtual Address0

16K

Physical Address

Page 17: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - VM

• Program A begins

• Program B beginsProg A

Hard Drive

Prog B

0

0

16K

24K

Virtual Address Physical Address

Page 18: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - VM

• Program A begins

• Program B beginsProg A

Prog B

0

0

16K

24K

What happens if A wants more memory?

Virtual Address Physical Address

Hard Drive

Page 19: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - VM

• Program A begins

• Program B beginsProg A

Hard Drive

Prog B

0

020K

24K

What happens if A wants more memory?Allocate another virtual page.

Virtual Address Physical Address

Page 20: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - VM

• Program A begins

• Program B begins

• Program A ends

Hard Drive

Prog B

0

24K

Virtual Address Physical Address

Page 21: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Multi-Processing - VM

• Program A begins

• Program B begins

• Program A ends

• Program C begins– Not all placed in

DRAM– DRAM use need not

be contiguous Hard Drive

Prog B

0

24K

Prog C

Virtual AddressPhysical Address0

28K

Page 22: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory is like caching…

• _______ is the cache for the __________– It contains only a subset of the total space

• Given an address, determine whether it is currently in the “cache”

• On a miss, obtain data and place in “cache”

Page 23: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory is like caching…

• DRAM is the cache for the hard drive– It contains only a subset of the total space

• Given an address, determine whether it is currently in the “cache”

• On a miss, obtain data and place in “cache”

Page 24: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory is not like caching…

• The miss penalty is orders of magnitude larger than for the cache

• You must know where it resides in DRAM before you can look it up in L1 cache

• This leads to a much different implementation

Page 25: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

The Search

• Cache – search each block in set for the proper tag

• Virtual Memory – store a table that is a mapping from virtual address to physical location (DRAM location).

Page 26: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory Implementation

• Programs use virtual addresses

• VM Block is called a __________

• A VM DRAM “cache miss” is called a ______________.

• To access data, the address must translate it to a _______________.

• This translation is called _______________ or ________________.

Page 27: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory Implementation

• Programs use virtual addresses

• VM Block is called a page

• A VM DRAM “cache miss” is called a ______________.

• To access data, the address must translate it to a _______________.

• This translation is called _______________ or ________________.

Page 28: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory Implementation

• Programs use virtual addresses

• VM Block is called a page

• A VM DRAM “cache miss” is called a page fault.

• To access data, the address must translate it to a _______________.

• This translation is called _______________ or ________________.

Page 29: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory Implementation

• Programs use virtual addresses

• VM Block is called a page

• A VM DRAM “cache miss” is called a page fault.

• To access data, the address must translate it to a _physical address_.

• This translation is called _______________ or ________________.

Page 30: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory Implementation

• Programs use virtual addresses

• VM Block is called a page

• A VM DRAM “cache miss” is called a page fault.

• To access data, the address must translate it to a _physical address_.

• This translation is called memory mapping_ or _virtual to physical translation.

Page 31: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Translation

Virtual Memory Implementation• Translation process:

• Why is Physical address smaller than Virtual?

Page offset

Page offset

Virtual page number

Physical page number

Page 32: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Translation

Virtual Memory Implementation• Translation process:

• Why is Physical address smaller than Virtual? DRAM is the cache – should be smaller than the total virtual address space

Page offset

Page offset

Virtual page number

Physical page number

Page 33: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory ImplementationPage faults incredibly costly

• DRAM is a cache - – Direct-mapped?– Set-associative?– Fully associative?

• Low associativity – miss rate, search time

• High associativity– miss rate, search time

Page 34: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory ImplementationPage faults incredibly costly

• DRAM is a cache - – Direct-mapped?– Set-associative?– Fully associative?

• Low associativity – high miss rate, low search time

• High associativity– low miss rate, high search time

Page 35: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory ImplementationPage faults incredibly costly

• DRAM is a cache - – Direct-mapped?– Set-associative?– Fully associative?

• Low associativity – high miss rate, low search time

• High associativity– low miss rate, high search time

Access time: ~50 cycles + search, miss penalty: hundreds of cycles

Page 36: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory ImplementationPage fault incredibly costly

• DRAM is a cache - – Direct-mapped?– Set-associative?– Fully associative!!!!

• Low associativity – high miss rate, low search time

• High associativity– low miss rate, high search time

Access time: ~50 cycles + search, miss penalty: thousand? cycles

Page 37: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory ImplementationPage fault incredibly costly

• Fully associative!!!!

• Large block size (4KB-16KB)

• Sophisticated software to implement replacement policy– updates are hidden under page fault penalty

• Definitely NOT write-through - only write-back.

Page 38: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Translation

• Does the same virtual address (in all processes) always translate to the same physical address (at one moment in time)?

• How often does a translation occur?

Page 39: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Translation

• Does the same virtual address always translate to the same physical address?– No - each process has same Virtual addresses

• How often does a translation occur?

Page 40: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Translation

• Does the same virtual address always translate to the same physical address?– No - each process has same Virtual addresses– Store translations in process page table

• How often does a translation occur?

Page 41: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Translation

• Does the same virtual address always translate to the same physical address?– No - each process has same Virtual addresses– Store translations in process page table

• How often does a translation occur?– At least once per instruction

Page 42: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Translation

• Does the same virtual address always translate to the same physical address?– No - each process has same Virtual addresses– Store translations in process page table

• How often does a translation occur?– At least once per instruction– Need to perform translation quickly – Cache recent translations!

Page 43: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Translation

• Maintaining per process page tables – Process page table maintained by ____ -

___________________________

• Making translations fast– Use a TLB (__________________________)

to cache recent translations– Fully associative but ____________________

Page 44: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Translation

• Maintaining per process page tables – Process page table maintained by OS - some

pinned into DRAM

• Making translations fast– Use a TLB (__________________________)

to cache recent translations– Fully associative but ___________________

Page 45: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Translation

• Maintaining per process page tables – Process page table maintained by OS - some

pinned into DRAM

• Making translations fast– Use a TLB (Translation Lookaside Buffer)

to cache recent translations– Fully associative but ___________________

Page 46: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Translation

• Maintaining per process page tables – Process page table maintained by OS - some

pinned into DRAM

• Making translations fast– Use a Translation Lookaside Buffer (TLB)

to cache recent translations– Fully associative but very small - 16-64 entries

Page 47: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Step 1: TLB• Search all locations of TLB in parallel

(fully-associative)Page offsetVirtual page number

VPN PPN

Page offsetPhysical page number

Page 48: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Step 1: TLB

• Search all locations of TLB in parallel

• Hit -

• Miss -

Page 49: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Step 1: TLB

• Search all locations of TLB in parallel

• Hit - return address, proceed with memory access

• Miss -

Page 50: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Step 1: TLB

• Search all locations of TLB in parallel

• Hit - return address, proceed with memory access

• Miss - retrieve translation from process page table –

Page 51: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Step 1: TLB

• Search all locations of TLB in parallel

• Hit - return address, proceed with memory access

• Miss - retrieve translation from process page table – Exception/Trap to OS

Page 52: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Step 2: TLB Miss

• Access the process’ page table to retrieve translation

• If valid (DRAM hit) – fill in TLB with this entry– restart TLB access and translation

• If invalid, page fault (DRAM miss)

Page 53: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Step 3: Page Fault

• Operating System invoked to swap a page in DRAM with the page requested on hard drive.

• Operating system looks up page’s location on hard drive.

• Operating system maintains replacement algorithm

• OS updates the process’ page tables

Page 54: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Putting it all together

Write?

No

No

No

No

Yes

Yes

Yes

YesWrite access bit on?

Page 55: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Putting it all together

TLB Access

Write?

No

No

No

No

Yes

Yes

Yes

YesWrite access bit on?

Page 56: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Putting it all together

TLB Access

Write?

TLB Hit?No

No

No

No

Yes

Yes

Yes

YesWrite access bit on?

Page 57: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Putting it all together

TLB Access

Write?

TLB Hit?No

TLB MissException

No

No

No

Yes

Yes

Yes

YesWrite access bit on?

Page 58: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Putting it all together

TLB Access

Write?

TLB Hit?

Cache read

No

TLB MissException

No

No

No

Yes

Yes

Yes

YesWrite access bit on?

Page 59: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Putting it all together

TLB Access

Cache Hit?

Write?

TLB Hit?

Cache read

No

TLB MissException

No

No

No

Yes

Yes

Yes

YesWrite access bit on?

Page 60: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Putting it all together

TLB Access

Cache Hit?

Write?

TLB Hit?

Cache read

No

TLB MissException

No

No

No

Return data

Yes

Yes

Yes

YesWrite access bit on?

Page 61: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Putting it all together

TLB Access

Cache Hit?

Write?

TLB Hit?

Stall

Cache read

No

TLB MissException

No

No

No

Return data

Yes

Yes

Yes

YesWrite access bit on?

Page 62: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Putting it all together

TLB Access

Write access bit on?

Cache Hit?

Write?

TLB Hit?

Stall

Cache read

Write to buffer

No

TLB MissException

No

No

No

Return data

Yes

Yes

Yes

Yes

Page 63: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Putting it all together

TLB Access

Write access bit on?

Cache Hit?

Write?

TLB Hit?

Stall

Cache read

Write to buffer

No

TLB MissException

No

No

No

Write protectionException

Return data

Yes

Yes

Yes

Yes

Page 64: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

TLB

• How can a 64 entry TLB be enough for 512MB of RAM? (L2 cache-512KB)

Translation

Page offsetVirtual page number

Page 65: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Protection

• Preventing A from reading B’s data

• Allowing A to share data with B

• Prevent overwriting instructions

• Prevent stack-smashing (popular attack)

Page 66: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Protection

• Preventing A from reading B’s data– Only OS may change page tables (supervisor)

• Allowing A to share data with B

• Prevent overwriting instructions

• Prevent stack-smashing (popular attack)

Page 67: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Protection

• Preventing A from reading B’s data– Only OS may change page tables (supervisor)

• Allowing A to share data with B– OS must point both to same physical page

• Prevent overwriting instructions

• Prevent stack-smashing (popular attack)

Page 68: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Protection

• Preventing A from reading B’s data– Only OS may change page tables (supervisor)

• Allowing A to share data with B– OS must point both to same physical page

• Prevent overwriting instructions– attach a write enable bit to each page

• Prevent stack-smashing (popular attack)

Page 69: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Protection

• Preventing A from reading B’s data– Only OS may change page tables (supervisor)

• Allowing A to share data with B– OS must point both to same physical page

• Prevent overwriting instructions– attach a write enable bit to each page

• Prevent stack-smashing (popular attack)– NX bit – “non-execute”

Page 70: Virtual Memory. DRAM as cache What about programs larger than DRAM? When we run multiple programs, all must fit in DRAM! Add another larger, slower level.

Virtual Memory Summary• VM increases total available memory• VM provides multi-process protection• TLB is necessary for fast translations• OS manages virtual memory

– therefore it is as slow as molasses in January in Minnesota

– therefore it is as slow as Java without the JIT– therefore it is as slow as the clock while you’re

sitting through CS 154.– as slow as Microsoft releasing security patches– as slow as a 154 student trudging to class

through 99 degree weather


Recommended