Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each...

30
Computer Systems Organization Mohamed Zahran (aka Z) [email protected] http://www.mzahran.com CSCI-UA.0201-003 Lecture 17: Virtual Memory: Concepts Some slides adapted (and slightly modified) from: Clark Barrett Jinyang Li Randy Bryant Dave O’Hallaron

Transcript of Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each...

Page 1: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Computer Systems Organization

Mohamed Zahran (aka Z)

[email protected]

http://www.mzahran.com

CSCI-UA.0201-003

Lecture 17: Virtual Memory: Concepts

Some slides adapted (and slightly modified) from: • Clark Barrett • Jinyang Li • Randy Bryant • Dave O’Hallaron

Page 2: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Physical Addressing

• Used by microcontrollers like those in Arduino, cars etc.

• Simple but fragile to program for: – Buffer overrun in buflab corrupts Firefox memory!

0: 1:

M-1:

Main memory

CPU

2: 3: 4: 5: 6: 7:

Physical address (PA)

Data word

8: ...

4

Page 3: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

All problems in CS can be solved by another

level of indirection

Butler Lampson, co-inventor of PC

Page 4: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Virtual Addressing

• Used in all modern servers, desktops, and laptops

0: 1:

M-1:

Main memory

MMU

2: 3: 4: 5: 6: 7:

Physical address (PA)

Data word

8: ...

CPU

Virtual address (VA)

CPU Chip

4 4100

Page 5: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Why Virtual Memory (VM)? • Simplified memory management

– Each process gets an ``exclusive’’ linear address space

• Process Isolation – Different processes have different virtual

address spaces – One process can’t interfere with another’s

memory

• Uses main memory efficiently – Use DRAM as a cache for the parts of a virtual

address space

Page 6: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Address translation • Key idea of VM: each process has its own virtual address space

Granularity of mapping? Byte-level: Map each byte in VA to a byte in PA

VA=0x08 PA=0x98 VA PA

… ..

0x08 0x98

.. ..

.. ..

.. invalid

Strawman view of addr translation

Valid address: • [0,2^32) for 32-bit machine • [0,2^64) for 64-bit machine

Valid address: • [0,2^m) depending on how much memory your machine has

Page-level: Map each consecutive 2^p-byte address range in VA to a 2^p-byte address range in PA

Page 7: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Address Translation w/ a Page Table

Virtual page number (VPN) Virtual page offset (VPO)

Physical page number (PPN) Physical page offset (PPO)

Virtual address

Physical address

Valid Physical page number (PPN)

Page table

Page table base register

(PTBR)

Page table address for process

Valid bit = 0: page not in memory

(page fault)

0 p-1 p n-1

0 p-1 p m-1

PTE (page table entry)

How kernel tells h/w where to find the page

table

Page 8: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Page Table Base Register (PTBR)

• The operating system maintains information about each process in a process control block.

• The page table base address for the process is stored there.

• The operating system loads this address into the PTBR whenever a process is schedule for execution.

• Only the kernel can access PTBR

Page 9: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Address Translation: Page Hit

1) Processor sends virtual address to MMU

2-3) MMU fetches PTE from page table in memory

4) MMU sends physical address to cache/memory

5) Cache/memory sends data word to processor

MMU Cache/ Memory PA

Data

CPU VA

CPU Chip PTEA

PTE 1

2

3

4

5

Page 10: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Address Translation: Page Fault

1) Processor sends virtual address to MMU

2-3) MMU fetches PTE from page table in memory

4) Valid bit is zero, so MMU triggers page fault exception in kernel

If VA is invalid, then kill process (SIGSEGV)

If VA has been paged out to disk, then swaps in faulted page, update page table, resume faulted process

MMU Cache/ Memory

CPU VA

CPU Chip PTEA

PTE

1

2

3

4 Page fault handler

Exception

7

Page 11: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Speeding up Translation with a TLB

• VA->PA translation can be expensive – One additional memory reference for every

normal memory reference!

– Not too bad? Page table entries (PTEs) are cached in L1 like others • PTEs may be evicted by other data references • PTE hit still requires a small L1 delay

• Solution: Translation Lookaside Buffer (TLB) – Small hardware cache in MMU – Maps virtual page numbers to physical page

numbers – Contains complete page table entries for small

number of pages

Page 12: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

TLB Hit

MMU Cache/ Memory

PA

Data

CPU VA

CPU Chip

PTE

1

2

4

5

A TLB hit eliminates a memory access

TLB

VPN 3

Page 13: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

TLB Miss

MMU Cache/ Memory PA

Data

CPU VA

CPU Chip

PTE

1

2

5

6

TLB

VPN

4

PTEA

3

A TLB miss incurs an additional memory access (the PTE) Fortunately, TLB misses are rare. Why?

Page 14: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Reduce Page Table Size • 4KB-page, 48-bit address space, 8-byte PTE • Size of page table needed?

– 248-12 * 23 = 239 = 512 GB

• Wasteful: most PTEs are invalid… • Solution: multi-level page table

– Example: 2-level page table • Level 1 table: each PTE points to a page

table • Level 2 table: each PTE points to a page

Level 1

Table

...

Level 2

Tables

...

Page 15: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

A Two-Level Page Table Hierarchy Level 1

page table

...

Level 2

page tables

VP 0

...

VP 1023

VP 1024

...

VP 2047

Gap

0

PTE 0

...

PTE 1023

PTE 0

...

PTE 1023

1023 null PTEs

PTE 1023 1023 unallocated

pages

VP 9215

Virtual

memory

(1K - 9) null PTEs

PTE 0

PTE 1

PTE 2 (null)

PTE 3 (null)

PTE 4 (null)

PTE 5 (null)

PTE 6 (null)

PTE 7 (null)

PTE 8

2K allocated VM pages for code and data

6K unallocated VM pages

1023 unallocated pages

1 allocated VM page for the stack

32 bit addresses, 4KB pages, 4-byte PTEs

Page 16: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Why Two-level Page Table Reduces Memory Requirement?

• if a PTE in the level 1 table is null, then the corresponding level 2 page table does not even have to exist.

• Only the level 1 table needs to be in main memory at all times.

• The level 2 page tables can be created and paged in and out by theVMsystem as they are needed.

Page 17: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

VM

For memory management and protection

For caching

Page 18: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Memory management and protection • Each process has an exclusive VA space

– One process cannot overwrite another one’s memory! • Sharing among processes

– Map different virtual pages to the same physical page

Virtual Address Space for Process 1:

Physical Address Space (DRAM)

0

(e.g., libc code, kernel)

Virtual Address Space for Process 2:

VP 1 VP 2 ...

0

VP 1 VP 2 ...

PP 2

PP 6

PP 8

...

0

Page 19: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Simplified Linking and Loading

• Linking – Each program has similar virtual

address space

– Code, stack, and shared libraries always start at the same address

• Loading – execve()causes kernel to

allocate virtual pages

– Kernel copies.text and .data sections, page by page, from disk to memory

Kernel virtual memory

Memory-mapped region for shared libraries

Run-time heap (created by malloc)

User stack (created at runtime)

Unused 0

%esp (stack pointer)

Memory invisible to user code

0xc0000000

0x08048000

0x40000000

Read/write segment (.data, .bss)

Read-only segment (.init, .text, .rodata)

Loaded from the executable file

Page 20: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Memory Protection • How to protect shared pages from corruption?

– E.g. bad process overwrites shared kernel code/data, shared libc code etc.

• Extend PTEs with permission bits

Process i: Address READ WRITE

PP 6 Yes No

PP 4 Yes Yes

PP 2 Yes

VP 0:

VP 1:

VP 2:

• • •

Process j:

Yes

SUP

No

No

Yes

Address READ WRITE

PP 6 Yes No

PP 9 Yes Yes

PP 11 Yes Yes

SUP

No

Yes

No

VP 0:

VP 1:

VP 2:

Physical Address Space

PP 2

PP 4

PP 6

PP 8

PP 9

PP 11

SUP: whether processes must be running in kernel (supervisor) mode to access the page.

Page 21: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

VM as a Tool for Caching • Not all processes’ valid VA pages fit in physical memory • Key idea: treat DRAM-resident pages as a cache of on-disk

pages

Physical memory

Empty

Empty

Uncached

VP 0

VP 1

Virtual memory

Unallocated

Cached

Uncached

Unallocated

Cached

Uncached

PP 0

PP 1

Empty

Cached

0

0

Virtual pages (VPs) stored on disk

Physical pages (PPs) cached in DRAM

Page 22: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

VM for Caching

• General mechanism: – On page fault, load corresponding on-disk page to

memory, evict a previously memory-resident to disk, set appropriate PTE entry

• Which entity should be performing this task? – User-level process? OS? Hardware?

• VM Caching policy (more sophisticated than CPU cache) – Fully associative: any VP can be mapped to any PP – Write-back

Page 23: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Page Hit • Page hit: reference to VM word that is in physical memory

(DRAM cache hit)

null

null

Memory resident page table

(DRAM)

Physical memory (DRAM)

VP 7 VP 4

Virtual memory (disk)

Valid 0

1

0 1

0

1

0

1

Physical page number or

disk address PTE 0

PTE 7

PP 0 VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

Page 24: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Page Fault • Page fault: reference to VM word that is not in

physical memory (DRAM cache miss)

null

null

Memory resident page table

(DRAM)

Physical memory (DRAM)

VP 7 VP 4

Virtual memory (disk)

Valid 0

1

0 1

0

1

0

1

Physical page number or

disk address PTE 0

PTE 7

PP 0 VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

Kernel figures out where to find the corresponding

on-disk page

Page 25: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Handling Page Fault • Page miss causes page fault (an exception)

null

null

Memory resident page table

(DRAM)

Physical memory (DRAM)

VP 7 VP 4

Virtual memory (disk)

Valid 0

1

0 1

0

1

0

1

Physical page number or

disk address PTE 0

PTE 7

PP 0 VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

Page 26: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Handling Page Fault • Page miss causes page fault (an exception) • Page fault handler selects a victim to be evicted (here VP 4)

null

null

Memory resident page table

(DRAM)

Physical memory (DRAM)

VP 7 VP 4

Virtual memory (disk)

Valid 0

1

0 1

0

1

0

1

Physical page number or

disk address PTE 0

PTE 7

PP 0 VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

Page 27: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Handling Page Fault • Page miss causes page fault (an exception) • Page fault handler selects a victim to be evicted (here VP 4)

null

null

Memory resident page table

(DRAM)

Physical memory (DRAM)

VP 7 VP 3

Virtual memory (disk)

Valid 0

1

1 0

0

1

0

1

Physical page number or

disk address PTE 0

PTE 7

PP 0 VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

Page 28: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Handling Page Fault • Page miss causes page fault (an exception) • Page fault handler selects a victim to be evicted (here VP 4) • Offending instruction is restarted: page hit!

null

null

Memory resident page table

(DRAM)

Physical memory (DRAM)

VP 7 VP 3

Virtual memory (disk)

Valid 0

1

1 0

0

1

0

1

Physical page number or

disk address PTE 0

PTE 7

PP 0 VP 2

VP 1

PP 3

VP 1

VP 2

VP 4

VP 6

VP 7

VP 3

Virtual address

Page 29: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Why should VM caching work? • Locality!

• At any point in time, programs tend to access a set of active virtual pages called the working set – Programs with better temporal locality will have

smaller working sets

• If (working set size < main memory size) – Good performance for one process after

compulsory misses

• If (working set sizes > main memory size ) – Thrashing: Performance meltdown where pages

are swapped (copied) in and out continuously

Page 30: Lecture 17: Virtual Memory: Concepts · 2013-04-07 · Address translation • Key idea of VM: each process has its own virtual address space Granularity of mapping? Byte-level: Map

Conclusions

• Programmer’s view of virtual memory – Each process has its own private linear

address space

– Cannot be corrupted by other processes

• System view of virtual memory – Simplifies memory management &

protection

– Uses memory efficiently by caching virtual memory pages