Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory...

19
Main Memory: Address Translation CS 4410 Operating Systems

Transcript of Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory...

Page 1: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

Main Memory: Address Translation

CS 4410Operating Systems

Page 2: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

• Paged Translation• Efficient Address Translation• Multi-Level Page Tables• Inverted Page Tables• TLBs

Address Translation

2

Page 3: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

Memory Consumption:

• Internal Fragmentation• Make pages smaller? But then…

• Page Table Space: consider 32-bit address space, 4KB page size, each PTE 8 bytes• How big is this page table?

• How many pages in memory does it need?

Performance: every data/instruction access requires two memory accesses:

• One for the page table

• One for the data/instruction

Downsides to Paging

3

Page 4: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

Internal Fragmentation Example

4

stack

text

data

heap

Virtual Memory

STACK 0

TEXT 0

DATA 0

HEAP 1

Physical Memory

HEAP 0

TEXT 1

STACK 1

stack

text

data

heap

Virtual Memory

STACK 0

TEXT 0

DATA 0

HEAP 1

Physical Memory

HEAP 0

TEXT 1

STACK 1

HEAP 2

Mostly empty

malloc

Page 5: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

5

+ Allocate only PTEs in use+ Can use smaller pages+ Simple memory allocation− more lookups per memory reference

Multi-Level Page Tables to the Rescue!

index 1 | index 2 | offset

Frame | Access

Frame

Page 6: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

32-bit machine, 1KB page size

• Logical address is divided into:– a page offset of 10 bits (1024 = 2^10)

– a page number of 22 bits (32-10)

• Since the page table is paged, the page number is further divided into:– a 12-bit first index

– a 10-bit second index

• Thus, a logical address is as follows:

Two-Level Paging Example

page number page offset

12 10 106

index 1 index 2 offset

Page 7: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

7

+ First Level requires less contiguous memory− even more lookups per memory reference

This one goes to three!

Page 8: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

Index is an index into:• table of memory frames (if bottom level)

• table of page table frames (if multilevel page table)

• backing store (if page was swapped out)

Synonyms:• Valid bit == Present bit

• Dirty bit == Modified bit

• Referenced bit == Accessed bit

Complete Page Table Entry (PTE)

8

Valid ProtectionR/W/X Ref Dirty Index

Page 9: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

• Paged Translation• Efficient Address Translation• Multi-Level Page Tables• Inverted Page Tables• TLBs

Address Translation

9

Page 10: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

So many virtual pages…

… comparatively few physical frames

Traditional Page Tables:• map pages to frames• are numerous and sparse

Inverted Page Table: Motivation

10

physical address space

P1 virtual address space

P2 virtual

address space

P3 virtual address space P5 virtual

address space

P4 virtual address space

Why not map frames to pages? (How?)

Page 11: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

11

Inverted Page Table: Implementation

Page-table Physical Memory

pid

page pid

Not to scale! Page table << Memory

Implementation:

• 1 Page Table for entire system

• 1 entry per frame in memory

• Why don’t we store the frame #?

page # offset

Virtual address

frame

offset

pid page

Page 12: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

Tradeoffs:

↓ memory to store page tables

↑ time to search page tables

Solution: hashing

• hash(page,pid) → PT entry (or chain of entries)

• What about:• collisions…

• sharing…

Inverted Page Table: Discussion

12

Page 13: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

• Paged Translation• Efficient Address Translation• Multi-Level Page Tables• Inverted Page Tables• TLBs

Address Translation

13

Page 14: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

How many memory accesses per data/instruction access?

• One per level of the page table

• One for the data/instruction

Workarounds

• CPU cache: Recently accessed data is still there, at virtual address

• Does this help for instructions?

Page Table Memory Lookups

14

Page 15: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

Cache of virtual to physical page translationsMajor efficiency improvement

15

Translation Lookaside Buffer (TLB)

Page 16: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

Access TLB before you access memory.

Address Translation with TLB

16

What happens on a TLB miss?

What happens on context switch?

When memory is freed?

Page 17: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

Access TLB before you access memory.

Address Translation with TLB

17

Trick: access TLB while you access the cache.

What if data is in the CPU cache?

Page 18: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

Process isolation• Keep a process from touching anyone else’s memory, or

the kernel’s

Efficient inter-process communication• Shared regions of memory between processes

Shared code segments• common libraries used by many different programs

Program initialization• Start running a program before it is entirely in memory

Dynamic memory allocation• Allocate and initialize stack/heap pages on demand

Address Translation Uses!

18

Page 19: Main Memory: Address Translation - Cornell University · 2019-08-12 · Demand-paged virtual memory •Illusion of near-infinite memory, backed by disk or memory on other machines

Program debugging• Data breakpoints when address is accessed

Memory mapped files• Access file data using load/store instructions

Demand-paged virtual memory• Illusion of near-infinite memory, backed by disk or

memory on other machines

Checkpointing/restart• Transparently save a copy of a process, without stopping

the program while the save happens

Distributed shared memory• Illusion of memory that is shared between machines

MORE Address Translation Uses!

19