<<

CS 152 Architecture and Engineering

Lecture 8 - Address Translation

John Wawrzynek Electrical Engineering and Computer Sciences University of California at Berkeley

http://www.eecs.berkeley.edu/~johnw http://inst.eecs.berkeley.edu/~cs152

9/27/2016 CS152, Fall 2016 CS152 Administrivia

§ Lab 2 due Friday § PS 2 due Tuesday § Quiz 2 next Thursday!

9/27/2016 CS152, Fall 2016 2 Last time in Lecture 7 § 3 C’s of misses – Compulsory, Capacity, Conflict § Write policies – Write back, write-through, write-allocate, no write allocate § Multi-level cache hierarchies reduce miss penalty – 3 levels common in modern systems (some have 4!) – Can change design tradeoffs of L1 cache if known to have L2 § Prefetching: retrieve memory before CPU request – Prefetching can waste bandwidth and cause cache pollution – Software vs hardware prefetching § Software memory hierarchy optimizations – Loop interchange, loop fusion, cache tiling

9/27/2016 CS152, Fall 2016 3 Bare Machine

Physical Inst. Address Data Decode PC Cache D E + M Cache W

Physical Memory Controller Physical Address Address Physical Address

Main Memory (DRAM)

§ In a bare machine, the only of address is a physical address

9/27/2016 CS152, Fall 2016 4 Absolute Addresses

EDSAC, early 50’s

§ Only one program ran at a time, with unrestricted access to entire machine (RAM + I/O devices) § Addresses in a program depended upon where the program was to be loaded in memory § But it was more convenient for programmers to write location-independent subroutines How could location independence be achieved? and/or loader modify addresses of subroutines and callers when building a program memory image

9/27/2016 CS152, Fall 2016 5 Dynamic Address Translation § Motivation – In early machines, I/O was slow and each I/O transfer involved the CPU (programmed I/O) – Higher throughput possible if CPU and I/O of 2 or more programs were overlapped, how? Program 1 ⇒ multiprogramming with DMA I/O devices, § Location-independent programs – Programming and storage management ease ⇒ need for a base register § Protection Program 2 – Independent programs should not affect each other

inadvertently Physical Memory ⇒ need for a bound register § Multiprogramming drives requirement for resident supervisor software to manage context switches between multiple programs OS

9/27/2016 CS152, Fall 2016 6 Simple Base and Bound Translation

Segment Length Bound Bounds Register ≥ Violation?

Physical Current Load X Segment Address +

Base Register Base Physical Address Program Physical Memory registers are visible/accessible only when is running in the supervisor mode

9/27/2016 CS152, Fall 2016 7 Separate Areas for Program and Data (Scheme used on all Cray vector prior to X1, 2002)

Data Bound Bounds Register ≥ Violation? Data Mem. Address Logical Load X Register Address Segment Data Base Physical Register + Address

Program Program Bound Bounds Main Register ≥ Violation? Space Program Counter Logical Program Address Segment Program Base Physical Register + Address

What is an advantage of this separation?

9/27/2016 CS152, Fall 2016 8 Base and Bound Machine Program Bound Data Bound Register Bounds Violation? Register Bounds Violation? ≥ ≥ Logical Logical Address Address

Inst. Data Decode PC + Cache D E + M + Cache W Physical Physical Address Address Program Base Data Base Register Register Physical Physical Address Address Memory Controller

Physical Address

Main Memory (DRAM)

Can fold addition of base register into (register+immediate) address calculation using a carry-save adder (sums three numbers with only a few gate delays more than adding two numbers)

9/27/2016 CS152, Fall 2016 9 Memory Fragmentation

Users 4 & 5 Users 2 & 5 free arrive leave OS OS OS Space Space Space 16K user 1 16K user 1 user 1 16K user 2 24K user 2 24K 24K user 4 16K 24K user 4 16K 8K 8K 32K user 3 32K user 3 user 3 32K

24K user 5 24K 24K

As users come and go, the storage is “fragmented”. Therefore, at some stage programs have to be moved around to compact the storage.

9/27/2016 CS152, Fall 2016 10 Paged Memory Systems § Processor-generated address can be split into:

Page Number Offset • A Table contains the physical address at the start of each page 1 0 0 0 1 1 Physical 2 2 Memory 3 3 3

Address Space 2 of User-1 of User-1 Page tables make it possible to store the pages of a program non-contiguously.

9/27/2016 CS152, Fall 2016 11 Private Address Space per User

OS User 1 VA1 pages Page Table

User 2 VA1

Page Table

User 3 VA1 Physical Memory

Page Table free

• Each user has a page table • Page table contains an entry for each user page

9/27/2016 CS152, Fall 2016 12 Where Should Page Tables Reside?

§ Space required by the page tables (PT) is proportional to the address space, number of users, ... ⇒ Too large to keep in registers

§ Idea: Keep PTs in the main memory – needs one reference to retrieve the page base address and another to access the data word ⇒ doubles the number of memory references!

9/27/2016 CS152, Fall 2016 13 Page Tables in Physical Memory

PT User 1

VA1 PT User 2

User 1 Physical Memory

VA1

User 2 Virtual Address Space

9/27/2016 CS152, Fall 2016 14 A Problem in the Early Sixties

§There were many applications whose data could not fit in the main memory, e.g., payroll – Paged memory system reduced fragmentation but still required the whole program to be resident in the main memory

9/27/2016 CS152, Fall 2016 15 in Atlas (1962) The Atlas Computer was a joint development between the University of Manchester, Ferranti, and Plessey.

“A page from secondary storage is brought into the primary storage whenever it is (implicitly) demanded by the processor.” Tom Kilburn Primary 32 Pages 512 words/page

Primary memory as a cache for secondary memory Secondary Central (Drum) User sees 32 x 6 x 512 words Memory 32x6 pages of storage

9/27/2016 CS152, Fall 2016 17 Hardware Organization of Atlas

Effective 16 ROM pages system code 0.4-1 µsec Address Initial (not swapped) Address Decode 2 subsidiary pages system data PARs 1.4 µsec (not swapped) 48- words 0 Main Drum (4) 512-word pages 8 Tape decks 32 pages 192 pages 88 sec/word 1.4 sec 1 Page Address µ 31 Register (PAR) per “page frame”

On memory access compare the effective page address against all 32 PARs match ⇒ normal access no match ⇒ save the state of the partially executed instruction

9/27/2016 CS152, Fall 2016 18 Atlas Demand Paging Scheme

On a page fault: § Transfer from drum to a free page in primary memory is initiated § The Page Address Register (PAR) is updated § If no free page is left, a page is selected to be replaced (based on usage) § The replaced page is written on the drum – to minimize drum latency effect, the first empty page on the drum was selected § The page table is updated to point to the new location of the page on the drum

9/27/2016 CS152, Fall 2016 19 Linear Page Table

Data Pages § Page Table Entry (PTE) Page Table contains: PPN – A bit to indicate if a page exists PPN DPN – PPN (physical page number) for a PPN memory-resident page Data word – DPN (disk page number) for a page on the disk Offset – Status for protection and usage § OS sets the Page Table Base DPN PPN Register whenever active PPN user changes DPN DPN VPN DPN PPN PT Base Register PPN Supervisor Accessible VPN Offset inside CPU Virtual address from CPU Execute Stage 9/27/2016 CS152, Fall 2016 20 Size of Linear Page Table

§ With 32-bit addresses, 4-KB pages & 4- PTEs: ⇒ 232 / 212 = 220 virtual pages per user, assuming 4-Byte PTEs, ⇒ 220 PTEs, i.e, 4 MB page table per user! – 4 GB of swap needed to back up full virtual address space § Larger pages helps, but: – Internal fragmentation (Not all memory in page is used) – Larger page fault penalty (more time to read from disk) § What about 64-bit virtual address space??? – Even 1MB pages would require 244 8-byte PTEs (35 TB!) What is the “saving grace” ?

9/27/2016 CS152, Fall 2016 21 Hierarchical Page Table Virtual Address from CPU 31 22 21 12 11 0 p1 p2 offset

10-bit 10-bit L1 index L2 index offset Root of the Current Page Table p2 p1

(Processor Level 1 Register) Page Table Physical Memory

Level 2 page in primary memory Page Tables page in secondary memory

PTE of a nonexistent page Data Pages

9/27/2016 CS152, Fall 2016 22 Address Translation & Protection Virtual Address Virtual Page No. (VPN) offset Kernel/User Mode

Read/Write Protection Address Check Translation

Exception? Physical Page No. (PPN) offset Physical Address • Every instruction and data access needs address translation and protection checks

A good VM design needs to be fast (~ one cycle) and space efficient

9/27/2016 CS152, Fall 2016 24 Translation Lookaside Buffers (TLB) Address translation is very expensive! In a two-level page table, each reference becomes several memory accesses Solution: Cache translations in TLB TLB hit ⇒ Single-Cycle Translation TLB miss ⇒ Page-Table Walk to refill

virtual address VPN offset

V R W D tag PPN (VPN = virtual page number)

(PPN = physical page number)

hit? physical address PPN offset

9/27/2016 CS152, Fall 2016 25 TLB Designs

§ Typically 32-128 entries, usually fully associative – Each entry maps a large page, hence less spatial locality across pages è more likely that two entries conflict – Sometimes larger TLBs (256-512 entries) are 4-8 way -associative – Larger systems sometimes have multi-level (L1 and L2) TLBs § Random or FIFO replacement policy § No process information in TLB? § TLB Reach: Size of largest virtual address space that can be simultaneously mapped by TLB

Example: 64 TLB entries, 4KB pages, one page per entry

64 entries * 4 KB = 256 KB (if contiguous) TLB Reach = ______?

9/27/2016 CS152, Fall 2016 26 Handling a TLB Miss §Software (MIPS, DEC/Alpha) – TLB miss causes an exception and the walks the page tables and reloads TLB. A privileged “untranslated” used for walk. §Hardware (SPARC v8, , PowerPC, RISC-V) – A unit (MMU) walks the page tables and reloads the TLB. – If a missing (data or PT) page is encountered during the TLB reloading, MMU gives up and signals a Page Fault exception for the original instruction.

9/27/2016 CS152, Fall 2016 27 Hierarchical Page Table Walk: SPARC v8

Virtual Address Index 1 Index 2 Index 3 Offset 31 23 17 11 0 Context Context Table Table Register L1 Table root ptr Context Register L2 Table PTP L3 Table PTP

PTE

31 11 0 Physical Address PPN Offset MMU does this table walk in hardware on a TLB miss

9/27/2016 CS152, Fall 2016 28 Page-Based Virtual-Memory Machine (Hardware Page-Table Walk) Page Fault? Page Fault? Protection violation? Protection violation? Virtual Virtual Physical Address Address Physical Address Address Inst. Inst. Decode Data Data PC TLB Cache D E + M TLB Cache W

Miss? Miss? Page-Table Base Register Hardware Page Table Walker

Physical Physical Memory Controller Address Address Physical Address Main Memory (DRAM) § Assumes page tables held in untranslated physical memory

9/27/2016 CS152, Fall 2016 29 Address Translation: putting it all together Virtual Address hardware TLB hardware or software software Lookup

miss hit

Page Table Protection Walk Check the page is ∉ memory ∈ memory denied permitted

Page Fault Update TLB Protection Physical (OS loads page) Fault Address (to cache) Where? SEGFAULT

9/27/2016 CS152, Fall 2016 30 Acknowledgements

§ These slides contain material developed and copyright by: – Arvind (MIT) – Krste Asanovic (MIT/UCB) – Joel Emer (/MIT) – James Hoe (CMU) – John Kubiatowicz (UCB) – David Patterson (UCB)

§ MIT material derived from course 6.823 § UCB material derived from course CS252

9/27/2016 CS152, Fall 2016 31