Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN...

117
Memory Hierarchy Virtual Memory, Address Translation Slides contents from: Hennessy & Patterson, 5ed. Appendix B and Chapter 2. David Wentzlaff, ELE 475 – Computer Architecture. MJT, High Performance Computing, NPTEL.

Transcript of Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN...

Page 1: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Memory Hierarchy

Virtual Memory, Address Translation

Slides contents from:

Hennessy & Patterson, 5ed. Appendix B and Chapter 2.

David Wentzlaff, ELE 475 – Computer Architecture.

MJT, High Performance Computing, NPTEL.

Page 2: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Process/Program Address Space

0

232-1

Byte Address

.

.

.

CODE

DATA

STACK

HEAP

Page 3: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Process/Program Address Space

0

232-1

Byte Address

.

.

.

● Compiler assumes a linear address space

– Byte 0 to Byte 232-1

CODE

DATA

STACK

HEAP

Page 4: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Process/Program Address Space

0

232-1

Byte Address

.

.

.

● Compiler assumes a linear address space

– Byte 0 to Byte 232-1● Virtual Address space

CODE

DATA

STACK

HEAP

Page 5: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Process/Program Address Space

0

232-1

Byte Address

.

.

.

● Compiler assumes a linear address space – Byte 0 to Byte 232-1

● Virtual Address space● The entire process data

structure may not be present in MM at all times.

CODE

DATA

STACK

HEAP

Page 6: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Paged Virtual Memory

0

232-1

Byte

.

.

.

Virtual PageNumber 0

Virtual PageNumber 1

Virtual PageNumber N-1

Virtual Address SpaceVirtual Address Space

.

.

.

Page 7: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Paged Virtual Memory

0

232-1

Byte

.

.

.

Virtual PageNumber 0

Virtual PageNumber 1

Virtual PageNumber N-1

Virtual Address SpaceVirtual Address Space

.

.

.

Page Number 0

Page Number 13

MA

IN M

EM

OR

YM

AIN

ME

MO

RY

Page 8: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Paged Virtual Memory

0

232-1

Byte

.

.

.

Virtual PageNumber 0

Virtual PageNumber 1

Virtual PageNumber N-1

Virtual Address SpaceVirtual Address Space

.

.

.

Page Number 0

Page Number 13

MA

IN M

EM

OR

YM

AIN

ME

MO

RY

Page Number 1

Page Number 2

HA

RD

DIS

KH

AR

D D

ISK

.

.

.Page Number N-1

Page 9: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Paged Virtual Memory

0

232-1

Byte

.

.

.

Virtual PageNumber 0

Virtual PageNumber 1

Virtual PageNumber N-1

Virtual Address SpaceVirtual Address Space

.

.

.

Page Number 0

Page Number 13

MA

IN M

EM

OR

YM

AIN

ME

MO

RY

Page Number 1

Page Number 2

HA

RD

DIS

KH

AR

D D

ISK

PhysicalAddress Space

PhysicalAddress Space

.

.

.Page Number N-1

Page 10: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

The Memory Hierarchy

Pages Lines

Words

Registers

Main memory

Cache

Virtual memory

(transferred explicitly

via load/store) (transferred automatically

upon cache miss) (transferred automatically

upon page fault)

VirtualAddresses

VirtualAddresses

PhysicalAddresses

PhysicalAddresses

VirtualAddresses

VirtualAddresses

Page 11: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Address Translation Table

Physical Page Numbers

VPN 0

VPN 1

VPN N-1

.

.

.

.

PPN 0

Page 12: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Address Translation Table

Physical Page Numbers

VPN PO

Virtual Address

PPN

VPN 0

VPN 1

VPN N-1

.

.

.

.

PPN 0

Page 13: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Address Translation Table

Physical Page Numbers

VPN PO

Virtual Address

PPN

VPN 0

VPN 1

VPN N-1

.

.

.

.

PPN 0

Page 14: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Address Translation Table

Physical Page Numbers

VPN PO

Virtual Address

PPN

VPN 0

VPN 1

VPN N-1

PPN PO

Physical Address

.

.

.

.

PPN 0

Page 15: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Virtual Memory

Page 16: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Virtual Memory

Page 17: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Virtual Memory

Page 18: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Address Translation Table

Physical Page Numbers

VPN PO

Virtual Address

PPN

VPN 0

VPN 1

VPN N

PPN PO

Physical Address

MAIN MEMORYMAIN MEMORY

Page 19: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Address Translation Table

Physical Page Numbers

VPN PO

Virtual Address

PPN

VPN 0

VPN 1

VPN N

PPN PO

Physical Address

Disk Addresses

Disk Address

MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK

Page 20: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Address Translation Table

Physical Page Numbers

VPN PO

Virtual Address

PPN

VPN 0

VPN 1

VPN N

PPN PO

Physical Address

Disk Addresses

Disk Address

V

MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK

Page 21: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Implementation of Address Translation● Process always uses virtual addresses

Page 22: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Implementation of Address Translation● Process always uses virtual addresses● Memory Management Unit (MMU): part of

CPU; hardware that does address translation

Page 23: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Implementation of Address Translation● Process always uses virtual addresses● Memory Management Unit (MMU): part of

CPU; hardware that does address translation● The page tables are (at best) present in the MM

(OS virtual address space)– One main memory reference per address

translation!●

Page 24: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Implementation of Address Translation● Process always uses virtual addresses● Memory Management Unit (MMU): part of

CPU; hardware that does address translation● To translate a virtual memory address, the MMU

has to read the relevant page table entry out of memory–

Page 25: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Implementation of Address Translation● Process always uses virtual addresses● Memory Management Unit (MMU): part of

CPU; hardware that does address translation● To translate a virtual memory address, the MMU

has to read the relevant page table entry out of memory– Caches recently used translations in a Translation

Lookaside Buffer (Page Table Cache)

Page 26: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Caches and Address Translation

CPU MMU

VirtualAddress

PhysicalAddress

Cache

Page 27: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Caches and Address Translation

CPU MMU

VirtualAddress

PhysicalAddress

Cache

Physically Addressed CachePhysically Addressed Cache

Page 28: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Caches and Address Translation

CPU MMU

VirtualAddress

PhysicalAddress

Cache

CPU Cache

VirtualAddress

PhysicalAddress

MMU

CacheMiss Main

Memory

Physically Addressed CachePhysically Addressed Cache

Page 29: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Caches and Address Translation

CPU MMU

VirtualAddress

PhysicalAddress

Cache

CPU Cache

VirtualAddress

PhysicalAddress

MMU

CacheMiss Main

Memory

Physically Addressed CachePhysically Addressed Cache

Virtually Addressed CacheVirtually Addressed Cache

Page 30: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Which is less preferable?● Physical addressed cache

● Virtual addressed cache–

Page 31: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Which is less preferable?● Physical addressed cache

– Hit time higher (cache access after translation)● Virtual addressed cache

Page 32: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Which is less preferable?● Physical addressed cache

– Hit time higher (cache access after translation)● Virtual addressed cache

– Data/instruction of different processes with same virtual address in cache at the same time ...

Page 33: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Which is less preferable?● Physical addressed cache

– Hit time higher (cache access after translation)● Virtual addressed cache

– Data/instruction of different processes with same virtual address in cache at the same time ...

● Flush cache on context switch, or● Include Process id as part of each cache directory entry

Page 34: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Which is less preferable?● Physical addressed cache

– Hit time higher (cache access after translation)● Virtual addressed cache

– Data/instruction of different processes with same virtual address in cache at the same time ...

● Flush cache on context switch, or● Include Process id as part of each cache directory entry

– Synonyms

Page 35: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Synonyms (Aliases)

P1t1

P2t2

VA-T1 VA-T2

X

Shared L2

t1: Read xt1: Read x

Page 36: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Synonyms (Aliases)

Shared L2

P1t1

P2t2

VA-T1 VA-T2

X

t1: Read xt1: Read x

Page 37: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Synonyms (Aliases)

Shared L2

P1t1

P2t2

VA-T1 VA-T2

X

t1: Read xt1: Read x

t2: Read xt2: Read x

Page 38: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Synonyms (Aliases)

P1t1

P2t2

VA-T1 VA-T2

Shared L2

X

t1: Read xt1: Read x

t2: Read xt2: Read x

Page 39: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Synonyms (Aliases)

P1t1

P2t2

VA-T1 VA-T2

Shared L2

X

● L2 uses virtual addresses

2 copies of one physicalpage in the cache!

2 copies of one physicalpage in the cache!

Page 40: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Overlapped Operation

CPU

MMU

VirtualAddress

CacheIndexingusing VA

Tag checkusing PA

Page 41: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Overlapped Operation

CPU

MMU

VirtualAddress

CacheIndexingusing VA

Tag checkusing PA

Virtually Indexed Physically Tagged Cache (VIPT)Virtually Indexed Physically Tagged Cache (VIPT)

Other options: PIPT, VIVTOther options: PIPT, VIVT

Page 42: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Recall – Cache AccessDirect mapped, 32 KB, 32B block,

32b main memory address

Page 43: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Recall – Cache AccessDirect mapped, 32 KB, 32B block,

32b main memory address

32 b address32 b address

Page 44: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Recall – Cache Access

TagTag Index (10b)Index (10b)

Direct mapped, 32 KB, 32B block,32b main memory address

OffsetOffset

32 b address32 b address

33 22

Page 45: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Recall – Cache Access

TagTag Index (10b)Index (10b)

Direct mapped, 32 KB, 32B block,32b main memory address

OffsetOffset

32 b address32 b address

33 22

Page 46: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Recall – Cache Access

TagTag Index (10b)Index (10b)

Direct mapped, 32 KB, 32B block,32b main memory address

OffsetOffset

32 b address32 b address

33 22

32B Block32B Block

Page 47: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Recall – Cache Access

TagTag Index (10b)Index (10b)

Direct mapped, 32 KB, 32B block,32b main memory address

OffsetOffset

32 b address32 b address

W 1W 1

33 22

W 2 ... 7W 2 ... 7 W 8W 8

Page 48: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Recall – Cache Access

TagTag Index (10b)Index (10b)

Direct mapped, 32 KB, 32B block,32b main memory address

OffsetOffset

32 b address32 b address

32B Block32B BlockW 1W 1

=?

33 22

W 1W 1 W 2 ... 7W 2 ... 7 W 8W 8

W 2 ... 7W 2 ... 7 W 8W 8

Page 49: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Recall – Cache Access

TagTag Index (10b)Index (10b)

Direct mapped, 32 KB, 32B block,32b main memory address

OffsetOffset

32 b address32 b address

32B Block32B BlockW 1W 1

=?

NoCache Miss

33 22

YesCache Hit

W 1W 1 W 2 ... 7W 2 ... 7 W 8W 8

. . .

W 2 ... 7W 2 ... 7 W 8W 8

Page 50: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Recall – Cache Access

TagTag Index (10b)Index (10b)

Direct mapped, 32 KB, 32B block,32b main memory address

OffsetOffset

32 b address32 b address

32B Block32B BlockW 1W 1

=?

NoCache Miss

33 22

YesCache Hit

W 1W 1 W 2 ... 7W 2 ... 7 W 8W 8

. . .

W 2 ... 7W 2 ... 7 W 8W 8

To Processor

Page 51: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Recall – Cache Access

TagTag Index (10b)Index (10b)

Direct mapped, 32 KB, 32B block,32b main memory address

OffsetOffset

32 b address32 b address

32B Block32B BlockW 1W 1

=?

NoCache Miss

33 22

YesCache Hit

Tag is not needed until the cache linehas been read

W 1W 1 W 2 ... 7W 2 ... 7 W 8W 8

. . .

W 2 ... 7W 2 ... 7 W 8W 8

To Processor

Page 52: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example64 b VM address

Page 53: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example64 b VM address

Virtual Page No. (50) Page Offset (14)

Page 54: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example

Index (8) Offset (6)

64 b VM address

Virtual Page No. (50) Page Offset (14)

Page 55: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example

Index (8) Offset (6)

64 b VM address

26b26b

Virtual Page No. (50) Page Offset (14)

Cache BlockCache Block

Page 56: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example

Index (8) Offset (6)

64 b VM address

26b26b

=?

Virtual Page No. (50) Page Offset (14)

Cache BlockCache Block

L1 Hit/Miss

Page 57: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example

Index (8) Offset (6)

64 b VM address

26b26b

=?

Virtual Page No. (50) Page Offset (14)

Cache BlockCache BlockPhysical Address (Tag)Physical Address (Tag)

L1 Hit/Miss

(26)

Page 58: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example

Index (8) Offset (6)

64 b VM address

26b26b

=?

Virtual Page No. (50) Page Offset (14)

Cache BlockCache Block

Index comes from the Virtual Address(Virtually Indexed)

Physical Address (Tag)Physical Address (Tag)

Tag comes from the Physical Address(Physically tagged)

L1 Hit/Miss

(26)

Page 59: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example

Index (8) Offset (6)

64 b VM address

26b26b

=?

Virtual Page No. (50) Page Offset (14)

Cache BlockCache Block

Index comes from the Virtual Address(Virtually Indexed)

TLB Tag TLB Index

Physical Address (Tag)Physical Address (Tag)

Tag comes from the Physical Address(Physically tagged)

L1 Hit/Miss

(7)(43)

(26)

Page 60: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example

Index (8) Offset (6)

64 b VM address

26b26b

=?

Virtual Page No. (50) Page Offset (14)

Cache BlockCache Block

Index comes from the Virtual Address(Virtually Indexed)

TLB Tag TLB Index

Physical Address (Tag)Physical Address (Tag)

=?

Tag comes from the Physical Address(Physically tagged)

TLB Hit/Page Fault

L1 Hit/Miss

(7)(43)

(26)

Page 61: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example

Index (8) Offset (6)

64 b VM address

26b26b

=?

Virtual Page No. (50) Page Offset (14)

Cache BlockCache Block

Index comes from the Virtual Address(Virtually Indexed)

TLB Tag TLB Index

Physical Address (Tag)Physical Address (Tag)

=?

Physical Address (40)

Tag comes from the Physical Address(Physically tagged)

TLB Hit/Page Fault

L1 Hit/Miss

(7)(43)

(26)

Page 62: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example

Index (8) Offset (6)

64 b VM address

26b26b

=?

Virtual Page No. (50) Page Offset (14)

Cache BlockCache Block

Index comes from the Virtual Address(Virtually Indexed)

TLB Tag TLB Index

Physical Address (Tag)Physical Address (Tag)

=?

Physical Address (40)

Tag comes from the Physical Address(Physically tagged)

TLB Hit/Page Fault

L1 Hit/Miss

To L2

Tag Index Offset

(7)(43)

(26)

Page 63: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM Example

Index (8) Offset (6)

64 b VM address

26b26b

=?

Virtual Page No. (50) Page Offset (14)

Cache BlockCache Block

Index comes from the Virtual Address(Virtually Indexed)

TLB Tag TLB Index

Physical Address (Tag)Physical Address (Tag)

=?

Physical Address (40)

Tag comes from the Physical Address(Physically tagged)

TLB Hit/Page Fault

L1 Hit/Miss

To L2

Tag Index Offset

=?

(7)(43)

(26)

L2 Cache BlockL2 Cache Block

Page 64: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

VM – Hardware vs. Software● Operating system creates Virtual to physical

address mappings.● Hardware performs the VA to PA translation at

runtime.

Page 65: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Why Virtual Memory?

● Relocation– Progammer has contiguous view of memory– Physical memory is scattered

Page 66: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Why Virtual Memory?

● Relocation– Progammer has contiguous view of memory– Physical memory is scattered

● Resource management– Programmer can assume there is enough memory– System can manage physical memory across

concurrent processes

Page 67: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Why Virtual Memory?

● Relocation– Progammer has contiguous view of memory– Physical memory is scattered

● Resource management– Programmer can assume there is enough memory– System can manage physical memory across concurrent

processes● Isolation

– Bug in one process should not corrupt memory of another– Provide each process with separate virtual address space

Page 68: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Why Virtual Memory?● Relocation

– Progammer has contiguous view of memory– Physical memory is scattered

● Resource management– Programmer can assume there is enough memory– System can manage physical memory across concurrent processes

● Isolation– Bug in one process should not corrupt memory of another– Provide each process with separate virtual address space

● Protection– Enforce rules on what memory a process can or cannot access

Page 69: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Size of the Page Table● Consider a typical 48-bit virtual address space,

4KB page size and 8 byte long page table entry (PTE) – Size of the page table = 512GB !– Many processes ==> many page tables

Page 70: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Size of the Page Table● Consider a typical 48-bit virtual address space,

4KB page size and 8 byte long page table entry (PTE) – Size of the page table = 512GB !– Many processes ==> many page tables

● Single level page table has high cost

Page 71: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Page Table Implementation● Single level page table has high cost● Virtual address space is sparsely allocated

Page 72: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Page Table Implementation● Single level page table has high cost● Virtual address space is sparsely allocated● Multi-level radix tree

Page 73: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Toy Example● Toy Page table implementation using radix

tree● 16b Virtual Address, 4KB page size

Page 74: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Toy Example● Toy Page table implementation using radix

tree● 16b Virtual Address, 4KB page size

4b4b 12b12b

Page OffsetVPA

Page 75: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Toy Example● Toy Page table implementation using radix

tree● 16b Virtual Address, 4KB page size

4b4b 12b12b

Page OffsetVPAVPN0

VPN1

... ...

VPN15

Process Memory Layout

Page 76: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Toy Example● Toy Page table implementation using radix

tree● 16b Virtual Address, 4KB page size

4b4b 12b12b

Page OffsetVPA

PPNPPN Page TableVPN 0000

VPN 1111

.

.

.

Page 77: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Toy Example4b4b 12b12b

Page OffsetVPA

PPNPPNVPN 0000

VPN 1111

.

.

.● Assume: Page Table is split into 4● Placed at addresses A, B, C, and

D in Physical memory.

A

B

C

D

Page 78: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Toy Example4b4b 12b12b

Page OffsetVPA

PPNPPNVPN 0000

VPN 1111

.

.

.● Placed at addresses A, B, C, and

D in Physical memory.

A

B

C

D

Page 79: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Toy Example4b4b 12b12b

Page OffsetVPA

PPNPPNVPN 0000

VPN 1111

.

.

.

AA BB CC DD

Page 80: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Toy Example4b4b 12b12b

Page OffsetVPA

PPNPPNVPN 0000

VPN 1111

.

.

.

AA BB CC DD

PPNPPN

Page 81: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Toy Example4b4b 12b12b

Page OffsetVPA

PPNPPNVPN 0000

VPN 1111

.

.

.

AA BB CC DD

PPNPPN

PT-BasePT-Base

Page 82: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

2 Level, 4-ary Radix Tree4b4b 12b12b

Page OffsetVPA

PPNPPNVPN 0000

VPN 1111

.

.

.

AA BB CC DD

PPNPPN

PT-BasePT-BaseVPN

b0

b3

Page 83: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

64bit-x86 Radix Tree● Is a 4 Level, 512-ary Radix Tree●

Page 84: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

64bit-x86 Radix Tree● Is a 4 Level, 512-ary Radix Tree●

... ...

0511

PPNPPN

...... ...

...... ......

Upto 512 children

Page 85: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

64bit-x86 Radix Tree● 48b VA, 4KB page● 36b VPN

b0

b35

9b

Page 86: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

64bit-x86 Radix Tree● 48b VA, 4KB page● 36b VPN

cr3cr3

b0

b35

0511

Page 87: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

64bit-x86 Radix Tree● 48b VA, 4KB page● 36b VPN

cr3cr3

b0

b35

0511

Page 88: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

64bit-x86 Radix Tree● 48b VA, 4KB page● 36b VPN

cr3cr3

b0

b35

... ...

0511

Page 89: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

64bit-x86 Radix Tree● 48b VA, 4KB page● 36b VPN

cr3cr3

b0

b35

... ...

0511

Page 90: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

64bit-x86 Radix Tree● 48b VA, 4KB page● 36b VPN

cr3cr3

b0

b35

... ...

0511

b0

b8

b9

b17

PPNPPN

...... ...

...... ......

Page 91: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Page Table Walk● 48b VA, 4KB page● 36b VPN

cr3cr3

b0

b35

... ...

0511

b0

b8

b9

b17

PPNPPN

...... ...

...... ......

Page 92: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Page Table Entry

● P (Present bit): If the address present in memory● ‘U/S’ (User/Supervisor bit): Is the page accessible in

supervisor mode (e.g., by OS) only?● ‘R/W’ (Read/Write): Is the page read-only?● A (Access bit): Is this page has ever been accessed

(load/stored to)?● D (Dirty bit): Is the page has been written to?● X/D (Executable bit): Does the page contains executable?

Page 93: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Role of the OS● Maintains one page table per process (a.k.a.,

per virtual address space)

Page 94: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Role of the OS● Maintains one page table per process (a.k.a.,

per virtual address space)● Creates, updates, deletes page table entries

(PTEs)

Page 95: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Role of the OS● Maintains one page table per process (a.k.a.,

per virtual address space)● Creates, updates, deletes page table entries

(PTEs)● Page table structure is part of agreement

between OS and Hardware– page table structure is ISA specific

Page 96: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Page Table Walk● Hardware Page Table Walker (PTW)

– Input: Root of page table (cr3) and VPN– Output: Physical page frame number or fault

Page 97: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Page Table Walk● Hardware Page Table Walker (PTW)

– Input: Root of page table (cr3) and VPN– Output: Physical page frame number or fault– A hardware fine-state-automata in each CPU core

Generates load-like “instructions” to access page table

– Eg. x86, POWER, and ARM processors

Page 98: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Page Table Walk● Hardware Page Table Walker (PTW)

– Input: Root of page table (cr3) and VPN– Output: Physical page frame number or fault– A hardware fine-state-automata in each CPU core

Generates load-like “instructions” to access page table– Eg. x86, POWER, and ARM processors

● Software page table walker– A OS handler walks the page table– Slow. Large address translation overhead– Example: SPARC (Sun/Oracle) machines

Page 99: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Fast Translation● Address translation is on the critical path● Paging – 4+1 memory accesses!

– Address Translation Table Accesses + Data● Translation Lookaside Buffer (TLB): Part of

MMU that caches address translations

Page 100: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

TLB & PTW

Core 0Core 0

L1L1

L2L2

Core 1Core 1

L1L1

L2L2

Core 2Core 2

L1L1

L2L2

Core 3Core 3

L1L1

L2L2

InterconnectInterconnect

L3L3

DRAMDRAM

Page 101: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

TLB & PTW

Core 0Core 0

L1L1

L2L2

Core 1Core 1

L1L1

L2L2

Core 2Core 2

L1L1

L2L2

Core 3Core 3

L1L1

L2L2

InterconnectInterconnect

L3L3

DRAMDRAM

PTW

L1 TLB

L2 TLB

Page 102: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

TLB Hierarchy● Each core typically has:

– 32-64-entry L1 TLB (fully associative/4-8 way set-associative)

– 1500-2500 entry L2 TLB (8-16 way set-associative)

– One to two PTWs

Page 103: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Address Translation Steps● Load/store instructions carries virtual address

– VA= VPN + PO

Page 104: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Address Translation Steps● Load/store instructions carries virtual address● Search TLBs forVPN to physical page (page

frame number) mapping

Page 105: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Address Translation Steps● Load/store instructions carries virtual address● Search TLBs forVPN to physical page (page

frame number) mapping ● On hit, Tag match with Tag from cache

(Virtually indexed Physically Tagged)

Page 106: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Address Translation Steps● Load/store instructions carries virtual address● Search TLBs forVPN to physical page (page

frame number) mapping ● On hit, Tag match with Tag from cache

(Virtually indexed Physically Tagged)● On miss, PTW starts walking page table to get

PFN– Found: Return mapping to TLB– Not Found: page fault raised to the OS

Page 107: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Page Fault● Must be `handled’ by operating system

– Identify slot in main memory to be used– Get page contents from disk– Update page table entry

Page 108: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Abstraction: Virtual vs. Physical Memory

● Programmer sees virtual memory– Can assume the memory is “infinite”

Page 109: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Abstraction: Virtual vs. Physical Memory

● Programmer sees virtual memory– Can assume the memory is “infinite”

● Reality: Physical memory size is much smaller than what the programmer assumes

Page 110: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

● The system (system software + hardware, cooperatively) maps virtual memory addresses to physical memory– System manages the physical memory space

transparently to the programmer

Abstraction: Virtual vs. Physical Memory

Page 111: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Extra

Page 112: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Size of a Page● Page is the unit of Memory Management● Too large vs. Too small.● Page Offset field need not be translated● What if the Page Offset field was 12 bits?

(Page size = 4KB)

Page 113: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Paged Virtual Memory● 48 bit Virtual Addresses, 40 bit Physical

Addresses. Page size = 16KB. How many entries in a process's Page Translation Table? What is the size of the Page Translation Table?

Page 114: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Virtual vs. Physical Memory

+ Programmer does not need to know the physical size of memory nor manage it A small physical memory can appear as a huge one to the

programmer Life is easier for the programmer

-- More complex system software and architecture

A classic example of the programmer/(micro)architecture tradeoff

Page 115: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Translation Lookaside Buffer● Cache of page table mappings● 32 – 4096 entries long

– SA, FA, or DM● Dirty flag – use during page write back● Ref – used for LRU

VPN(tag)

PPN(data)

Valid Ref Dirty Access Rights

Page 116: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Page Fault● Must be `handled’ by operating system

– Identify slot in main memory to be used– Get page contents from disk– Update page table entry

● Provide data to the processor

Page 117: Virtual Memory, Address Translation · PPN PO Physical Address Disk Addresses Disk Address MAIN MEMORYMAIN MEMORY HARD DISKHARD DISK. Address Translation Table Physical Page Numbers

Virtual Memory● What is the size of the Page Table?● Where is it stored?● What factors decide the size of a page?

– What are its side effects?● Page size is constant/variable?●