A Few Problems with Physical Addressing Main memory 0: 1: - - PowerPoint PPT Presentation

a few problems with physical addressing
SMART_READER_LITE
LIVE PREVIEW

A Few Problems with Physical Addressing Main memory 0: 1: - - PowerPoint PPT Presentation

A Few Problems with Physical Addressing Main memory 0: 1: Physical address 2: Virtual Memory 3: (PA) CPU 4: 4 5: Process Abstraction, Part 2: Private Address Space 6: 7: 8: ... M-1: Motivation : why not direct physical memory


slide-1
SLIDE 1

Virtual Memory

Process Abstraction, Part 2: Private Address Space

Motivation: why not direct physical memory access? Address translation with pages Optimizing translation: translation lookaside buffer Extra benefits: sharing and protection Memory as a contiguous array of bytes is a lie! Why?

1

A Few Problems with Physical Addressing

2 0: 1: M-1:

Main memory

CPU

2: 3: 4: 5: 6: 7:

Physical address (PA)

Data

8:

...

4

Solution: Virtual Memory (address indirection)

6

Private virtual address space per process.

Physical memory

Virtual address space

Process 1 Process n

virtual-to-physical

mapping

virtual addresses physical addresses virtual addresses

Single physical address space managed by OS/hardware.

Virtual address space

data data

Virtual Addressing and Address Translation

9

Physical addresses are invisible to programs.

0: 1: M-1:

Main memory MMU

2: 3: 4: 5: 6: 7:

Physical address (PA)

Data

8:

...

CPU

Virtual address (VA) CPU Chip

4 4100

Memory Management Unit

translates virtual address to physical address

slide-2
SLIDE 2

Page-based Mapping

Physical Address Space

Physical

Page

Physical

Page 1

Physical

Page 2p - 1

2m - 1 Virtual Address Space

Virtual Page Virtual Page 1

Virtual Page 2v - 1

2n - 1

Virtual Page 2 Virtual Page 3

fixed-size, aligned pages

page size = power of two

Map virtual pages

  • nto physical pages.

Some virtual pages do not fit! Where are they stored?

Design for a Slow Disk: Exploit Locality

Physical Memory Address Space

Physical

Page

Physical

Page 1

Physical

Page 2p - 1

2m - 1 Virtual Memory Address Space

Virtual Page Virtual Page 1

Virtual Page 2v - 1

2n - 1

Virtual Page 2 Virtual Page 3

  • n disk

Page Table

array of page table entries (PTEs) mapping virtual page to where it is stored

17

Physical pages (Physical memory) Swap space (Disk) VP 7 VP 4 PP 0 VP 2 VP 1 PP 3

How many page tables are in the system?

null null

page table

1 1 1 1

Valid

Physical Page Number

  • r disk address

PTE 0 PTE 7

Memory resident, managed by HW (MMU), OS

VP 3 VP 6

Address Translation with a Page Table

18 Virtual page number (VPN) Virtual page offset (VPO) Physical page number (PPN) Physical page offset (PPO)

Virtual address (VA) Physical address (PA)

Valid Physical page number (PPN) Page table base register (PTBR)

Page table

Base address

  • f current process's

page table

Virtual page mapped to physical page?

Yes = Page Hit

slide-3
SLIDE 3

On disk

Page Hit: virtual page in memory

19

Physical pages (Physical memory) Swap space (Disk) VP 7 VP 4 PP 0 VP 1 PP 3

On disk PP 2 null null PP 0 PP 1 PP 3

page table

1 1 1 1

Valid

Physical Page Number

  • r disk address

PTE 0 PTE 7

Virtual Page Number VP 2 VP 3 VP 6

Process

Page Fault: exceptional control flow

Process accessed virtual address in a page that is not in physical memory.

21

User Code OS exception handler exception: page fault Load page into memory return movl Returns to faulting instruction: movl is executed again!

PP 1 PP 3 On disk

Page Fault: 1. page not in memory

22

Physical pages (Physical memory) Swap space (Disk) VP 7 VP 4 PP 0 VP 1 PP 3

On disk PP 2 null null PP 0

page table

1 1 1 1

Valid

Physical Page Number

  • r disk address

PTE 0 PTE 7

Virtual Page Number VP 2

What now? OS handles fault

VP 3 VP 6

null On disk PP 1 On disk PP 3

Page Fault: 2. OS evicts another page.

23

Physical pages (Physical memory) Swap space (Disk) VP 7 VP 4 PP 0 VP 1 PP 3 VP 3

On disk PP 2 null

page table

1 1 1

Valid

Physical Page Number

  • r disk address

PTE 0 PTE 7

VP 6 Virtual Page Number VP 2

"Page out"

VP 1

slide-4
SLIDE 4

PP 1 1 PP 3 PP 0

Page Fault: 3. OS loads needed page.

24

Physical pages (Physical memory) Swap space (Disk) VP 7 VP 4 PP 0 VP 2 PP 3

On disk PP 2 null null On disk

page table

1 1 1 1

Valid

Physical Page Number

  • r disk address

PTE 0 PTE 7

Virtual Page Number VP 3 VP 6 VP 1 VP 3

Finally: Re-execute faulting instruction. Page hit! "Page in"

Terminology

context switch

Switch control between processes on the same CPU.

page in

Move page of virtual memory from disk to physical memory.

page out

Move page of virtual memory from physical memory to disk.

thrash

Total working set size of processes is larger than physical memory. Most time is spent paging in and out instead of doing useful computation.

(I find all these terms useful when talking to other computer scientists about my brain…) 25

swap

Useful for "real life" too.

Address Translation: Page Hit

26

1) Processor sends virtual address to MMU (memory management unit) 2-3) MMU fetches PTE from page table in cache/memory 4) MMU sends physical address to cache/memory 5) Cache/memory sends data word to processor MMU

Cache/ Memory

PA Data

CPU

VA

CPU Chip

PTEA PTE 1 2 3 4 5

Address Translation: Page Fault

27

1) Processor sends virtual address to MMU 2-3) MMU fetches PTE from page table in cache/memory 4) Valid bit is zero, so MMU triggers page fault exception 5) Handler identifies victim (and, if dirty, pages it out to disk) 6) Handler pages in new page and updates PTE in memory 7) Handler returns to original process, restarting faulting instruction MMU

Cache/ Memory

CPU

VA

CPU Chip

PTEA PTE 1 2 3 4 5

Disk

Page fault handler

Victim page New page

Exception

6 7

slide-5
SLIDE 5

Translation Lookaside Buffer (TLB)

Small hardware cache in MMU just for page table entries

e.g., 128 or 256 entries

Much faster than a page table lookup in memory. In the running for "un/classiest name of a thing in CS"

28

How fast is translation?

How many physical memory accesses are required to complete

  • ne virtual memory access?

TLB Hit

29

MMU

Cache/ Memory

PA Data

CPU

VA

CPU Chip

PTE 1 2 4 5

A TLB hit eliminates a memory access

TLB

VPN 3

TLB Miss

30

MMU

Cache/ Memory

PA Data

CPU

VA

CPU Chip

PTE 1 2 5 6

TLB

VPN 4 PTEA 3

A TLB miss incurs an additional memory access (the PTE)

Fortunately, TLB misses are rare. Does a TLB miss require disk access?

Simple Memory System Example (small)

Addressing

14-bit virtual addresses 12-bit physical address Page size = 64 bytes

31

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

VPO PPO PPN VPN Virtual Page Number Virtual Page Offset Physical Page Number Physical Page Offset Simulate accessing these virtual addresses on the system: 0x03D4, 0x0B8F, 0x0020

slide-6
SLIDE 6

Simple Memory System Page Table

Only showing first 16 entries (out of 256 = 28) What about a real address space? Read more in the book…

32 1 0D 0F 1 11 0E 1 2D 0D – 0C – 0B 1 09 0A 1 17 09 1 13 08 Valid PPN VPN – 07 – 06 1 16 05 – 04 1 02 03 1 33 02 – 01 1 28 00 Valid PPN VPN virtual page #___ TLB index___ TLB tag ____ TLB Hit? __ Page Fault? __ physical page #: ____

Simple Memory System TLB

16 entries 4-way associative

33

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

virtual page offset virtual page number

TLB index TLB tag

– 02 1 34 0A 1 0D 03 – 07 3 – 03 – 06 – 08 – 02 2 – 0A – 04 – 02 1 2D 03 1 1 02 07 – 00 1 0D 09 – 03 Valid PPN Tag Valid PPN Tag Valid PPN Tag Valid PPN Tag Set

TLB ignores page offset. Why? virtual page #___ TLB index___ TLB tag ____ TLB Hit? __ Page Fault? __ physical page #: ____

Simple Memory System Cache

16 lines 4-byte block size Physically addressed Direct mapped

34

11 10 9 8 7 6 5 4 3 2 1

physical page offset physical page number

cache offset cache index cache tag

03 DF C2 11 1 16 7 – – – – 31 6 1D F0 72 36 1 0D 5 09 8F 6D 43 1 32 4 – – – – 36 3 08 04 02 00 1 1B 2 – – – – 15 1 11 23 11 99 1 19 B3 B2 B1 B0 Valid Tag Idx – – – – 14 F D3 1B 77 83 1 13 E 15 34 96 04 1 16 D – – – – 12 C – – – – 0B B 3B DA 15 93 1 2D A – – – – 2D 9 89 51 00 3A 1 24 8 B3 B2 B1 B0 Valid Tag Idx

cache offset___ cache index___ cache tag____ Hit? __ Byte: ____