Docsity

Virtual Memory and Page Tables: An Overview - Prof. Jian-Guo Liu, Study notes of Mathematics

Toggle sidebar
bg1
Virtual Memory
Oct. 29, 2002
Virtual Memory
Oct. 29, 2002
Topics
Topics
nMotivations for VM
nAddress translation
nAccelerating translation with TLBs
class19.ppt
15-213
“The course that gives CMU its Zip!”
– 2 – 15-213, F’02
Motivations for Virtual Memory
Motivations for Virtual Memory
Use Physical DRAM as a Cache for the Disk
Use Physical DRAM as a Cache for the Disk
nAddress space of a process can exceed physical memory size
nSum of address spaces of multiple processes can exceed
physical memory
Simplify Memory Management
Simplify Memory Management
nMultiple processes resident in main memory.
lEach process with its own address space
nOnly “active” code and data is actually in memory
lAllocate more memory to process as needed.
Provide Protection
Provide Protection
nOne process can’t interfere with another.
lbecause they operate in different address spaces.
nUser process cannot access privileged information
ldifferent sections of address spaces have different permissions.
– 3 – 15-213, F’02
Motivation #1: DRAM a “Cache” for
Disk
Motivation #1: DRAM a “Cache” for
Disk
Full address space is quite large:
Full address space is quite large:
n32-bit addresses: ~4,000,000,000 (4 billion) bytes
n64-bit addresses: ~16,000,000,000,000,000,000 (16 quintillion)
bytes
Disk storage is ~300X cheaper than DRAM storage
Disk storage is ~300X cheaper than DRAM storage
n80 GB of DRAM: ~ $33,000
n80 GB of disk: ~ $110
To access large amounts of data in a cost-effective manner,
To access large amounts of data in a cost-effective manner,
the bulk of the data must be stored on disk
the bulk of the data must be stored on disk
1GB: ~$200 80 GB: ~$110
4 MB: ~$500
Disk
DRAMSRAM
– 4 – 15-213, F’02
Levels in Memory Hierarchy
Levels in Memory Hierarchy
CPU
CPU
regs
regs
C
a
c
h
e
Memory
Memory disk
disk
size:
speed:
$/Mbyte:
line size:
32 B
1 ns
8 B
Register Cache Memory Disk Memory
32 KB-4MB
2 ns
$125/MB
32 B
1024 MB
30 ns
$0.20/MB
4 KB
100 GB
8 ms
$0.001/MB
larger, slower, cheaper
8 B 32 B 4 KB
cache virtual memory
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Virtual Memory and Page Tables: An Overview - Prof. Jian-Guo Liu and more Study notes Mathematics in PDF only on Docsity!

Virtual Memory

Oct. 29, 2002

Virtual Memory

Oct. 29, 2002

Topics

Topics

n Motivations for VM

n Address translation

n Accelerating translation with TLBs

class19.ppt

“The course that gives CMU its Zip!”

– 2 – 15-213, F’

Motivations for Virtual Memory

Motivations for Virtual Memory

Use Physical DRAM as a Cache for the DiskUse Physical DRAM as a Cache for the Disk

n Address space of a process can exceed physical memory size

n Sum of address spaces of multiple processes can exceed

physical memory

Simplify Memory Management

Simplify Memory Management

n Multiple processes resident in main memory.

l Each process with its own address space

n Only “active” code and data is actually in memory

l Allocate more memory to process as needed.

Provide Protection

Provide Protection

n One process can’t interfere with another.

l because they operate in different address spaces.

n User process cannot access privileged information

l different sections of address spaces have different permissions.

15-213, F’

Motivation #1: DRAM a “Cache” for

Disk

Motivation #1: DRAM a “Cache” for

Disk

Full address space is quite large:

Full address space is quite large:

n 32-bit addresses: ~4,000,000,000 (4 billion) bytes

n 64-bit addresses: ~16,000,000,000,000,000,000 (16 quintillion)

bytes

Disk storage is ~300X cheaper than DRAM storage Disk storage is ~300X cheaper than DRAM storage

n 80 GB of DRAM: ~ $33,

n 80 GB of disk: ~ $

To access large amounts of data in a cost-effective manner, To access large amounts of data in a cost-effective manner,

the bulk of the data must be stored on disk the bulk of the data must be stored on disk

1GB: ~$

80 GB: ~$

4 MB: ~$

Disk

SRAM DRAM

15-213, F’

Levels in Memory Hierarchy

Levels in Memory Hierarchy

CPU

CPU

regs

regs

C

a

c

h

e

Memory

Memory

disk

disk

size:

speed:

$/Mbyte:

line size:

32 B

1 ns

8 B

Register Cache Memory Disk Memory

32 KB-4MB

2 ns

$125/MB

32 B

1024 MB

30 ns

$0.20/MB

4 KB

100 GB

8 ms

$0.001/MB

larger, slower, cheaper

8 B 32 B 4 KB

cache virtual memory

– 5 – 15-213, F’

DRAM vs. SRAM as a “Cache”

DRAM vs. SRAM as a “Cache”

DRAM vs. disk is more extreme than SRAM vs. DRAM

DRAM vs. disk is more extreme than SRAM vs. DRAM

n Access latencies:

l DRAM ~10X slower than SRAM

l Disk ~100,000X slower than DRAM

n Importance of exploiting spatial locality:

l First byte is ~100,000X slower than successive bytes on disk

» vs. ~4X improvement for page-mode vs. regular accesses to

DRAM

n Bottom line:

l Design decisions made for DRAM caches driven by enormous cost

of misses

DRAM

SRAM

Disk

– 6 – 15-213, F’

Impact of Properties on Design

Impact of Properties on Design

If DRAM was to be organized similar to an SRAM cache, how would If DRAM was to be organized similar to an SRAM cache, how would

we set the following design parameters?we set the following design parameters?

n Line size?

l Large, since disk better at transferring large blocks

n Associativity?

l High, to mimimize miss rate

n Write through or write back?

l Write back, since can’t afford to perform small writes to disk

What would the impact of these choices be on: What would the impact of these choices be on:

n miss rate

l Extremely low. << 1%

n hit time

l Must match cache/DRAM performance

n miss latency

l Very high. ~20ms

n tag storage overhead

l Low, relative to block size

15-213, F’

Locating an Object in a “Cache”

Locating an Object in a “Cache”

SRAM CacheSRAM Cache

n Tag stored with cache line

n Maps from cache block to memory blocks

l From cached to uncached form

l Save a few bits by only storing tag

n No tag for block not in cache

n Hardware retrieves information

l can quickly match against multiple tags

X

Object Name

Tag Data

D 243

X 17

J 105

N-1:

= X?

“Cache”

15-213, F’

Locating an Object in “Cache” (cont.)

Locating an Object in “Cache” (cont.)

Data

N-1:

X

Object Name

Location

D:

J:

X: 1

On Disk

Page Table “Cache”

DRAM Cache DRAM Cache

n Each allocated page of virtual memory has entry in page table

n Mapping from virtual pages to physical pages

l From uncached form to cached form

n Page table entry even if page not in memory

l Specifies disk address

l Only way to indicate where to find page

n OS retrieves information

– 13 – 15-213, F’

Motivation #2: Memory Management

Motivation #2: Memory Management

Multiple processes can reside in physical memory.Multiple processes can reside in physical memory.

How do we resolve address conflicts?How do we resolve address conflicts?

n what if two processes access something at the same

address?

kernel virtual memory

Memory mapped region

forshared libraries

runtime heap (via malloc)

program text (.text)

initialized data (.data)

uninitialized data (.bss)

stack

forbidden

%esp

memory invisible to

user code

the “brk” ptr

Linux/x

process

memory

image

– 14 – 15-213, F’

Virtual

Address

Space for

Process 1:

Physical

Address

Space

(DRAM)

VP 1

VP 2

PP 2

Address Translation 0

N-

N-

M-

VP 1

VP 2

PP 7

PP 10

(e.g., read/only

library code)

Solution: Separate Virt. Addr. Spaces

Solution: Separate Virt. Addr. Spaces

n Virtual and physical address spaces divided into equal-sized

blocks

l blocks are called “pages” (both virtual and physical)

n Each process has its own virtual address space

l operating system controls how virtual pages as assigned to

physical memory

Virtual

Address

Space for

Process 2:

15-213, F’

Contrast: Macintosh Memory Model

Contrast: Macintosh Memory Model

MAC OS 1–9 MAC OS 1–

n Does not use traditional virtual memory

All program objects accessed through “handles” All program objects accessed through “handles”

n Indirect reference through pointer table

n Objects stored in shared global address space

P1 Pointer Table

P2 Pointer Table

Process P

Process P

Shared Address Space

A

B

C

D

E

“Handles”

15-213, F’

Macintosh Memory Management

Macintosh Memory Management

Allocation / Allocation / DeallocationDeallocation

n Similar to free-list management of malloc/free

Compaction

Compaction

n Can move any object and just update the (unique) pointer in

pointer table

“Handles”

P1 Pointer Table

P2 Pointer Table

Process P

Process P

Shared Address Space

A

B

C

D

E

– 17 – 15-213, F’

Mac vs. VM-Based Memory Mgmt

Mac vs. VM-Based Memory Mgmt

Allocating,Allocating, deallocatingdeallocating, and moving memory:, and moving memory:

n can be accomplished by both techniques

Block sizes:

Block sizes:

n Mac: variable-sized

l may be very small or very large

n VM: fixed-size

l size is equal to one page (4KB on x86 Linux systems)

Allocating contiguous chunks of memory:

Allocating contiguous chunks of memory:

n Mac: contiguous allocation is required

n VM: can map contiguous range of virtual addresses to

disjoint ranges of physical addresses

ProtectionProtection

n Mac: “wild write” by one process can corrupt another’s data

– 18 – 15-213, F’

MAC OS X

MAC OS X

“Modern” Operating System

“Modern” Operating System

n Virtual memory with protection

n Preemptive multitasking

l Other versions of MAC OS require processes to voluntarily

relinquish control

Based on MACH OS Based on MACH OS

n Developed at CMU in late 1980’s

15-213, F’

Motivation #3: Protection

Motivation #3: Protection

Page table entry contains access rights information

Page table entry contains access rights information

n hardware enforces this protection (trap into OS if violation

occurs)

Page Tables

Process i:

Read? Write? Physical Addr

Yes No PP 9

Yes Yes PP 4

No No XXXXXXX

VP 0:

VP 1:

VP 2:

Process j:

N-1:

Memory

Read? Write? Physical Addr

Yes Yes PP 6

Yes No PP 9

No No XXXXXXX

VP 0:

VP 1:

VP 2:

15-213, F’

VM Address Translation

VM Address Translation

Virtual Address Space Virtual Address Space

n V = {0, 1, …, N–1}

Physical Address Space

Physical Address Space

n P = {0, 1, …, M–1}

n M < N

Address Translation

Address Translation

n MAP: V → P U {∅}

n For virtual address a:

l MAP(a) = a’ if data at virtual address a at physical address a’

in P

l MAP(a) = ∅ if data at virtual address a not in physical memory

» Either invalid or stored on disk

– 25 – 15-213, F’

Address Translation via Page Table

Address Translation via Page Table

virtual page number (VPN) page offset

virtual address

physical page number (PPN) page offset

physical address

m–1 p p–1 0

n–1 p p–1 0

page table base register

if valid=

then page

not in memory

valid

accessphysical page number (PPN)

VPN acts

as

table index

– 26 – 15-213, F’

Page Table Operation

Page Table Operation

Translation Translation

n Separate (set of) page table(s) per process

n VPN forms index into page table (points to a page table entry)

virtual page number (VPN) page offset

virtual address

physical page number (PPN) page offset

physical address

m–1 pp–1 0

n–1 pp–1 0

page table base register

if valid=

then page

not in memory

valid accessphysical page number (PPN)

VPN acts

as

table index

virtual page number (VPN) page offset

virtual address

physical page number (PPN) page offset

physical address

m–1 pp–1 0

n–1 pp–1 0

page table base register

if valid=

then page

not in memory

valid accessphysical page number (PPN)

VPN acts

as

table index

15-213, F’

Page Table Operation

Page Table Operation

Computing Physical Address

Computing Physical Address

n Page Table Entry (PTE) provides information about page

l if (valid bit = 1) then the page is in memory.

» Use physical page number (PPN) to construct address

l if (valid bit = 0) then the page is on disk

» Page fault

virtual page number (VPN) page offset

virtual address

physical page number (PPN) page offset

physical address

m–1 pp–1 0

n–1 pp–1 0

page table base register

if valid=

then page

not in memory

valid

accessphysical page number (PPN)

VPN acts

as

table index

virtual page number (VPN) page offset

virtual address

physical page number (PPN) page offset

physical address

m–1 pp–1 0

n–1 pp–1 0

page table base register

if valid=

then page

not in memory

valid

accessphysical page number (PPN)

VPN acts

as

table index

15-213, F’

Page Table Operation

Page Table Operation

Checking Protection

Checking Protection

n Access rights field indicate allowable access

l e.g., read-only, read-write, execute-only

l typically support multiple protection modes (e.g., kernel vs. user)

n Protection violation fault if user doesn’t have necessary

permission

virtual page number (VPN) page offset

virtual address

physical page number (PPN) page offset

physical address

m–1 pp–1 0

n–1 pp–1 0

page table base register

if valid=

then page

not in memory

valid accessphysical page number (PPN)

VPN acts

as

table index

virtual page number (VPN) page offset

virtual address

physical page number (PPN) page offset

physical address

m–1 pp–1 0

n–1 pp–1 0

page table base register

if valid=

then page

not in memory

valid accessphysical page number (PPN)

VPN acts

as

table index

– 29 – 15-213, F’

CPU

Trans-

lation

Cache

Main

Memory

VA PA

miss

hit

data

Integrating VM and Cache

Integrating VM and Cache

Most Caches “Physically Addressed”

Most Caches “Physically Addressed”

n Accessed by physical addresses

n Allows multiple processes to have blocks in cache at same time

n Allows multiple processes to share pages

n Cache doesn’t need to be concerned with protection issues

l Access rights checked as part of address translation

Perform Address Translation Before Cache Lookup

Perform Address Translation Before Cache Lookup

n But this could involve a memory access itself (of the PTE)

n Of course, page table entries can also become cached

– 30 – 15-213, F’

CPU

TLB

Lookup

Cache

Main

Memory

VA PA

miss

hit

data

Trans-

lation

hit

miss

Speeding up Translation with a TLB

Speeding up Translation with a TLB

“Translation

“Translation

Lookaside

Lookaside

Buffer” (TLB)

Buffer” (TLB)

n Small hardware cache in MMU

n Maps virtual page numbers to physical page numbers

n Contains complete page table entries for small number of

pages

15-213, F’

Address Translation with a TLB

Address Translation with a TLB

virtual page number page offset virtual address

physical address

n–1 pp–1 0

valid tagphysical page number

valid tag data

data

=

cache hit

tag byte offset

index

=

TLB hit

TLB

Cache

15-213, F’

Simple Memory System Example

Simple Memory System Example

Addressing Addressing

n 14-bit virtual addresses

n 12-bit physical address

n Page size = 64 bytes

VPO

PPN PPO

VPN

(Virtual Page Number) (Virtual Page Offset)

(Physical Page Number)

(Physical Page Offset)

– 37 – 15-213, F’

Address Translation Example

Address Translation Example

Virtual AddressVirtual Address 0x0B8F0x0B8F

VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page Fault? __ PPN: ____

Physical Address

Physical Address

Offset ___ CI___ CT ____ Hit? __ Byte: ____

VPN VPO

TLBI

TLBT

PPN PPO

CO

CI

CT

– 38 – 15-213, F’

Address Translation Example

Address Translation Example

Virtual Address Virtual Address 0x00400x

VPN ___ TLBI ___ TLBT ____ TLB Hit? __ Page Fault? __ PPN: ____

Physical Address

Physical Address

Offset ___ CI___ CT ____ Hit? __ Byte: ____

VPN VPO

TLBI

TLBT

PPN PPO

CO

CI

CT

15-213, F’

Multi-Level Page Tables

Multi-Level Page Tables

Given:Given:

n 4KB (

12

) page size

n 32-bit address space

n 4-byte PTE

Problem:

Problem:

n Would need a 4 MB page table!

l 2

20

*4 bytes

Common solutionCommon solution

n multi-level page tables

n e.g., 2-level table (P6)

l Level 1 table: 1024 entries, each of

which points to a Level 2 page table.

l Level 2 table: 1024 entries, each of

which points to a page

Level 1

Table

Level 2

Tables

15-213, F’

Main Themes

Main Themes

Programmer’s View

Programmer’s View

n Large “flat” address space

l Can allocate large blocks of contiguous addresses

n Processor “owns” machine

l Has private address space

l Unaffected by behavior of other processes

System View System View

n User virtual address space created by mapping to set of

pages

l Need not be contiguous

l Allocated dynamically

l Enforce protection during address translation

n OS manages many processes simultaneously

l Continually switching among processes

l Especially when one must wait for resource

» E.g., disk I/O to handle page fault