Virtual Memory 1

To do … q Segmentation q Paging q A hybrid system Address spaces and multiple processes

IBM OS/360 – Split memory in n parts (possible != sizes)

0 – A per partition Program 64K Code Partition 1 Heap 128K Partition 2

(free)

256K Stack Partition 3 320K Partition 4

512K How do deal with large address spaces with potentially a lot of free space?

2 The overlay solution

Split programs into pieces (overlays), keep in memory only what’s needed Overlay approach (~1960s) – Swapping was done by the OS, but – Splitting was done manually by the user – Easy on the OS, hard on programmers

Symbol tables 20K Overlay for a two-pass assembler:

Pass 1 70KB Common Pass 2 80KB routines 30K Symbol Table 20KB Common Routines 30KB Overlay 10K Total 200KB driver

Two overlays: 120 + 130KB Pass 2 Pass 1 70K 80K 3 – Goals

Transparency – Programs should not know that memory is virtualized – Program flexibility – processes can run in machines with less physical memory than they need

Efficiency – Both in time and space; not making processes too slow and using physical memory efficiently

Protection – Isolating processes from each other; i.e., a process should not be able to access the memory of any other or the OS itself

4 Virtual memory – Segmentation

Hide the complexity, let the OS do the job How? A related idea, to generalize base/limit – Each segment can grow independently – Only used space is allocated Program Code – Yes, (segfault) comes Heap from this!

(free) Stack

Heap Stack Stack Code

Heap

Program Code

5 Which segment?

How does the HW know which segment an address refers to? Explicit approach – With three segments, use two bits of the virtual address – The rest are offset into the segment

// Get segment number Segment = (VirtAddr & SEG_MASK) >> SEG_SHIFT; // Now get offset Offset = VirtAddr & OFFSET_MASK; 13 12 11 10 9 8 7 6 5 4 3 2 1 0 if (Offset >= Limit[Segment]) /* protection fault */ else PhysAddr = Base[Segment] + Offset Segment Offset ...

Implicit – how was the address formed? – PC? ; from the stack or base pointer? Stack ...

6 Sharing and fragmentation

With a little bit more HW support – sharing memory segments (e.g., code) – Just a few bits per segments for read/write/execute rights Rather than code, stack, … finer grain segmentation – OS can keep track of segments in use and manage memory more efficiently – A segment table to track them – Need more HW support Many small variable-sized segments … – External fragmentation! – Compaction is expensive, free-list management may help a bit

7 And then came paging

Avoid fragmentation from variable-sized blocks – Use fixed-sized blocks – pages split into pages – Each a contiguous range of addresses Physical memory split into frames

Virtual memory Page 0 Pages mapped Physical memory to page frames Page frame 0 Page 1

Page frame 1 Page 2 … …

Page frame M Page N

8 Virtual memory – paging

Pages and page frames are, typically, = size – Many processors support multiple page sizes (e.g., -64: 4KB, 2MB, 1GB)

Pages are mapped onto frames – Doing the translation – OS + MMU – Key – not all pages have to be in at once – If page is in memory, system does the mapping else, OS is told to get the missing page and re-execute the failed instruction

9 Paging – Good for everyone

For developers – Memory seems a contiguous address space with size independent of hardware – Simple and flexible – no assumptions on how memory is used For memory manager – Physical memory can be used efficiently with minimal internal (small units) & no external fragmentation (fixed size units) Protection is easy since processes can’t access each other’s memory

10 Address translation with paging

Virtual to – Two parts – virtual page number and offset

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

Virtual Page # Offset

– Virtual page number – index into a – Page table maps virtual pages to page frames • Managed by the OS • One entry per page in virtual address space – Physical address – page number and offset

11 Address translation with paging

Virtual address Physical memory Virtual page # offset Page frame 0 Page table

Physical address Page frame i Page frame # Page frame # offset

Page frame N

Each process has its own page table

12 Translation in action

MMU with 16 4KB pages Page # (first 4 bits) index into page table Physical Page # Offset If not there 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 – Page fault 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 Else 15 000 0 – Output register + 14 000 0 – 12 bit offset → 13 000 0 12 000 0 – 15 bit physical 11 111 1 address 2 110 1 110 1 001 1 Present Bit 0 010 1

15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0

Virtual Page # Offset 13 Pages, page frames and tables

A simple example with 64KB virtual address space 4KB pages Virtual address 32KB physical address space space 60-64K X 16 pages and 8 page frames 56-60K X 52-56K X 48-52K X 44-48K 7 40-44K X Physical 36-40K 5 memory Try to access : address 32-36K X • MOV REG, 0 28-32K X 28-32K Virtual address 0 24-28K X 24-28K Page frame 2 20-24K 3 20-24K Physical address 8192 16-20K 4 16-20K 12-16K 0 12-16K 8-12K 6 8-12K 4-8K 1 4-8K 0-4K 2 0-4K

14 Pages, page frames and tables

A simple example with 64KB virtual address space 4KB pages Virtual address 32KB physical address space space 60-64K X 16 pages and 8 page frames 56-60K X 52-56K X 48-52K X 44-48K 7 40-44K X Physical Try to access : 36-40K 5 • MOV REG, 8192 32-36K X Virtual address 8192 28-32K X 28-32K Page frame 6 24-28K X 24-28K Physical address 24576 20-24K 3 20-24K 16-20K 4 16-20K • MOV REG, 20500 12-16K 0 12-16K Virtual address 20500 (20480 + 20) 8-12K 6 8-12K Page frame 3 4-8K 1 4-8K Physical address 20+12288 0-4K 2 0-4K

15 Since virtual memory >> physical memory

Use a present/absent bit

MMU checks – Virtual address – If not there, “page fault” to the OS space (trap) 60-64K X 56-60K X – OS picks a victim (?) 52-56K X – … sends victim to disk 48-52K X 44-48K 7 – … brings new one 40-44K X Physical 36-40K 5 memory address – … updates page table 32-36K X 28-32K X 28-32K 24-28K X 24-28K 20-24K 3 20-24K 16-20K 4 16-20K MOVE REG, 32780 12-16K 0 12-16K Virtual address 32780 8-12K 6 8-12K Virtual page 8, 12 (32768+12) 4-8K 1 4-8K Page is unmapped – page fault! 0-4K 2 0-4K

16 Page table entry

An opportunity – there’s a PTE lookup per memory ref., what else can we do with it? Looking at the details

Caching disabled Modified Present/absent

Page frame number

Referenced Protection

– Page frame number – the most important field – Protection – 1 bit for R&W or R or 3 bits for RWX

17 Page table entry

Looking at the details

Caching disabled Modified Present/absent

Page frame number

Referenced Protection – Present/absent bit • Says whether or not the virtual address is used – Modified (M): dirty bit • Set when a write to the page has occurred – Referenced (R): Has it being used? – To ensure we are not reading from cache (D) • Key for pages that map onto device registers rather than memory 18

Segmentation and paging

Segmentation pros and cons ✔– It’s more logical –✔ Facilitates sharing and reuse –✗ But all the problems of variable partitions

Paging pros and cons –✔ Easy to allocate physical memory –✔ Naturally leads to virtual memory –✗ Address translation time ✗ – Page tables can be large

19 Segmentation w/ paging -

Large segment? Page them – MULTICS, x86 and x86-64 MULTICS: process’ virtual memory – 218 segments of ~64K words (36-bit) – Each process has a segment table (itself a paged segment) – Segment table address found in a Descriptor segment Descriptor Base Register; one entry …. per segment Segment desc. Segment desc. Segment desc. Segment desc. Segment desc. Segment desc. Segment desc.

20 Segmentation w/ paging - MULTICS

Most (but not all) segments are paged Segment descriptor indicates if in memory and points to page table – Address of segment in secondary memory in another table Segment # to get segment descriptor

Page table Virtual Address Descriptor …. segment Segment # (18b) PageSegment # (6b) offset Offset # (16b) (10b) Page entry …. Page entry Segment desc. Page entry Address within Segment desc. the segment …. Segment desc. Page entry

Page entry Page entry 21 Segmentation w/ paging - MULTICS

If segment in mem, segment’s page table is in violation? Fault Look at the page table’s entry - is page in memory? Page fault Add offset to page origin to get word location … to speed things up – cache (TLB, first system)

18 9 1 1 1 3 3 Segment Main memory address Segment length Descriptor of the page table (in pages)

Page size 0 – 1024 words 1 = 64 words Segment paged? Misc bits Protection bits

22 Next … considerations with page tables

Two key issues with page tables Mapping must be fast – Done on every memory reference, at least 1 per instruction With large address spaces, page tables will be large – 32b & 4KB page → 12 bit offset, 20 bit page # ~ 1million PTE – 64b & 4KB page → 212 (offset) + 252 pages ~ 4.5x1015!!!

Simplest solutions – Page table in registers • Fast during execution, $$$ & slow to context switch – Page table in memory & Page Table Base Register (PTBR) • Fast to context switch & cheap, but slow during execution

23 And now a short break …

xkcd 24