Virtual Memory Philipp Koehn 25 April 2018 Philipp Koehn Computer - - PowerPoint PPT Presentation

virtual memory
SMART_READER_LITE
LIVE PREVIEW

Virtual Memory Philipp Koehn 25 April 2018 Philipp Koehn Computer - - PowerPoint PPT Presentation

Virtual Memory Philipp Koehn 25 April 2018 Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018 Recall: Process Address Space 1 Kernel memory ffffffff User stack Stack pointer Memory-mapped region for shared


slide-1
SLIDE 1

Virtual Memory

Philipp Koehn 25 April 2018

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-2
SLIDE 2

1

Recall: Process Address Space

Kernel memory User stack Memory-mapped region for shared libraries Run time heap (created by malloc) Read/write segment (.data / .bss) Read-only code segment (.init, .text., .rodata)

Stack pointer Loaded from executable 400000 ffffffff

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-3
SLIDE 3

2

Virtual Memory

  • Abstraction of physical memory
  • Purpose

– appearance of more available memory than physically exists (DRAM) – handles disk caching / loading – insulates memory of each process

  • Page table:

maps from virtual address to physical addresses

  • Memory management unit (MMU):

hardware implementation of address translation

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-4
SLIDE 4

3

Warning

  • This is going to get very complex
  • Closely tied with multi-tasking (multiple processes)
  • Partly managed by hardware,

partly managed by software

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-5
SLIDE 5

4

virtual addressing

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-6
SLIDE 6

5

Physical Addressing

CPU

0: 1: 2: 3: 4: 5: 6: 7:

Physical address (PA) Main memory Data CPU chip

  • So far, assumed CPU addresses physical memory

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-7
SLIDE 7

6

Virtual Addressing

CPU MMU

0: 1: 2: 3: 4: 5: 6: 7:

Address translation Virtual address (VA) Physical address (PA) CPU chip Main memory Data

  • Memory management unit (MMU): maps virtual to physical addresses

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-8
SLIDE 8

7

Address Space

  • Virtual memory size:

N = 2n bytes, e.g., 256TB

  • Physical memory size:

M = 2m bytes, e.g., 16GB

  • Page (block of memory):

P = 2p bytes, e.g., 4KB

  • A virtual address can be encoded in n bits

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-9
SLIDE 9

8

caching

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-10
SLIDE 10

9

Caching... Again?

  • Yes, we already discussed caching

but: for on-chip cache of DRAM memory

  • Now

– caching between RAM and disk – driven by a large virtual memory address space – to avoid unnecessary and duplicate loading

  • Jargon

– previously "block", now "page" – now: "swapping" or "paging"

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-11
SLIDE 11

10

Mapping

empty empty empty empty empty Physical memory unallocated cached uncached cached unallocated uncached cached unallocated unallocated unallocated

0: 1: 2: 3: 4: 5: 6: 7:

Virtual memory

8: 15:

Virtual pages (VP) stored on disk Physical pages (PP) cached in DRAM

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-12
SLIDE 12

11

State of Virtual Memory Page

  • Cached

– allocated page – stored in physical memory

  • Uncached

– allocated page – not in physical memory

  • Unallocated

– not used by virtual memory system so far

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-13
SLIDE 13

12

Page Table

  • Array of page table entries (PTE)
  • Valid bit

– set if PTE currently maps to physical address (cached) – not set otherwise (uncached or unallocated)

  • Mapped address

– if cached: physical address in DRAM – if not cached: physical address on disk

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-14
SLIDE 14

13

Page Table

VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null

0: 1: 2: 3: 4: 5: 6: 7:

Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-15
SLIDE 15

14

Page Hit

VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null

0: 1: 2: 3: 4: 5: 6: 7:

Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-16
SLIDE 16

15

Page Fault

VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null

0: 1: 2: 3: 4: 5: 6: 7:

Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address

  • Valid bit = 0
  • Page not in RAM

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-17
SLIDE 17

16

Page Fault

VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null

0: 1: 2: 3: 4: 5: 6: 7:

Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address

  • Page is on disk

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-18
SLIDE 18

17

Page Fault

VP1 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null

0: 1: 2: 3: 4: 5: 6: 7:

Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address

  • Make space in RAM
  • Pre-empt "victim" page
  • Typically out-dated cached page

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-19
SLIDE 19

18

Page Fault

VP1 VP2 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null

0: 1: 2: 3: 4: 5: 6: 7:

Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address

  • Load page into RAM

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-20
SLIDE 20

19

Page Fault

VP1 VP2 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null

0: 1: 2: 3: 4: 5: 6: 7:

Page table Disk 1 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address Virtual address

  • Update page table entry

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-21
SLIDE 21

20

Allocating Pages

  • What happens when we load a program?
  • We need to load its executable into memory
  • Similar:

create data objects when program is running ("allocating" memory)

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-22
SLIDE 22

21

Allocating Page

VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null null

0: 1: 2: 3: 4: 5: 6: 7:

Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address

  • Identify space in virtual memory

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-23
SLIDE 23

22

Allocating Page

VP1 VP6 VP3 VP7 VP0 VP1 VP2 VP3 Physical memory DRAM null

0: 1: 2: 3: 4: 5: 6: 7:

Page table Disk 1 1 1 1 VP4 VP5 VP6 VP7 Valid Address

  • Map to data on disk

– do not actual load – just create page table entries – let virtual memory system handle loading ⇒ On-demand loading

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-24
SLIDE 24

23

Process Memory

  • Nothing loaded at startup
  • Working set (or resident set)

– pages of a process that are currently in DRAM – loaded by virtual memory system on demand

  • Thrashing

– memory actively required by all processes larger than physically available – frequent swapping of memory to/from disk – very bad: slows down machine dramatically

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-25
SLIDE 25

24

memory management

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-26
SLIDE 26

25

One Page Table per Process

PP10 VP1 VP2

0: 1: 2: 3:

Process 1 1 1 VP1 VP2

0: 1: 2: 3:

Process 2 1 1 PP2 PP7 Physical memory Shared page

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-27
SLIDE 27

26

Process Address Space

Kernel memory User stack Memory-mapped region for shared libraries Run time heap (created by malloc) Read/write segment (.data / .bss) Read-only code segment (.init, .text., .rodata)

Stack pointer Loaded from executable 400000 ffffffff

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-28
SLIDE 28

27

Simplified Linking

Data Code

400000

  • Each process has its code in address 0x400000
  • Easy linking:

Linker can establish fixed addresses

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-29
SLIDE 29

28

Simplified Loading

  • When loading process into memory...
  • Enter .data and .text section into page table
  • Mark them as invalid (= not actually in RAM)
  • Called memory mapping (more on that later)

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-30
SLIDE 30

29

Simplified Sharing

Shared libraries Shared libraries

Physical memory

  • Shared libraries

used by several processes e.g., stdio providing printf, e.g., scanf, open, close, ...

  • Not copied multiple times

into RAM

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-31
SLIDE 31

30

Simplified Memory Allocation

  • Process may need more memory (e.g., malloc call)

⇒ New entry in page table

  • Mapped to arbitrary pages in physical memory
  • Do not have to be contiguous

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-32
SLIDE 32

31

Memory Protection

Physical memory

yes no no PP 6 yes yes no PP 4 VP 0 VP 1 yes yes yes PP 2 VP 2 READ WRT SUP Address yes no no PP 9 yes yes yes PP 6 VP 0 VP 1 yes yes no PP 11 VP 2 READ WRT SUP Address

Process 1 Process 2

PP 0 PP 2 PP 4 PP 6 PP 9 PP 11

  • Page may be kernel only:

SUP=yes

  • Page may be read-only (e.g., code)

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-33
SLIDE 33

32

address translation

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-34
SLIDE 34

33

Address Space

  • Virtual memory size:

N = 2n bytes

  • Physical memory size:

M = 2m bytes

  • Page (block of memory):

P = 2p bytes

  • A virtual address can be encoded in n bits

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-35
SLIDE 35

34

Address Translation

  • Task:

mapping virtual address to physical address – virtual address (VA): used by machine code instructions – physical address (PA): location in RAM

  • Formally

MAP: VA → PA ∪ 0 where: MAP(A) = PA if in RAM xxxxx = 0 otherwise

  • Note:

this happens very frequently in machine code

  • We will do this in hardware:

Memory Management Unit (MMU)

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-36
SLIDE 36

35

Basic Architecture

page table base register

Virtual address Physical address

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-37
SLIDE 37

36

Basic Architecture

page table base register

Valid Physical page number

Virtual address Physical address

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-38
SLIDE 38

37

Basic Architecture

page table base register virtual page number page offset physical page number page offset

Valid Physical page number

Virtual address Physical address

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018

slide-39
SLIDE 39

38

Basic Architecture

page table base register virtual page number page offset physical page number page offset

Valid Physical page number valid = 0?

  • > page fault

Virtual address Physical address

Philipp Koehn Computer Systems Fundamentals: Virtual Memory 25 April 2018