Docsity

Virtual Memory and Memory Hierarchy, Study notes of Computer Architecture and Organization

Partial preview of the text

Download Virtual Memory and Memory Hierarchy and more Study notes Computer Architecture and Organization in PDF only on Docsity!

CS356 Unit 9

Virtual Memory & Address

Translation

Indirection

• Indirection means using one entity to refer to another

• Examples:

  • A variable name vs. it's physical memory address
  • Phone number vs. cell tower location/phone ID
  • Titles like "CEO" or "head coach" are virtual titles that can be applied

to different people at different times

• The benefits are we can change one without changing the

other

  • We can change the underlying implementation without changing the

higher level task. For example, a job description would read "The CEO

shall perform this duty or that." and it need not be changed if the

company replaces John Doe with Jane Doe.

• "All problems in computer science can be solved by another

level of indirection" – attributed to David Wheeler

Benefits of Address Translation

• What is enabled through virtual memory and address

translation?

– Illusion of more or less memory than physically present

– Isolation

– *Controlled sharing of code or data

– *Efficient I/O (memory-mapped files)

– *Dynamic allocation (Heap / Stack growth)

– *Process migration

• *Will be discussed in a subsequent unit or Operating

Systems class

Memory Hierarchy & Caching

• Lower levels act as a cache for upper levels

Disk / Secondary Storage

~1- 10 ms

Main Memory

~ 100 ns

L2 Cache

~ 10ns

L1 Cache

~ 1ns

Registers

L1/L2 is a

“cache” for

main memory

Virtual memory

provides each

process its own

address space in

secondary storage

and uses main

This Photo by Unknown Author is licensed under CC BY-SA^ memory as a cache

Secondary Storage: Flash

  • Flash (solid-state) drives store bits using special transistors that retain their values even when power is turned off
  • Performance is higher than magnetic disks but still slower comparted to main memory - Better sequential read throughput - HD (Magnetic): 122-204 MB/s - SSD: 210-270 MB/s - MUCH better random read - Max latency for single read/write: 75us - When many requests present we can overlap and

achieve latency of around 26us (1/38500)

  • Flash drives "wear-out" after some number of writes/erasures OS:PP 2nd^ Ed. Fig. 12. Intel 710 SSD specs.

Address Spaces

  • Physical address spaces corresponds

to the actual system address range

(based on the width of the address

bus) of the processor and how much

main memory is physically present

  • Each process/program runs in its own

private "virtual" address space

  • Virtual address space can be larger

(or smaller) than physical memory

  • Virtual address spaces are protected

from each other

32 - bit Physical

Address Space w/

only 1 GB of Mem

0x 0xffff ffff Mem.

I/O

Not used 0x3fffffff Not used 0x 0xbfffffff Code 0x 0xffff ffff

32 - bit Fictitious Virtual

Address Spaces

( > 1GB Mem)

Mapped I/O

Program/Process

Data

Heap

Stack

  • 0xc 0x

Virtual Address Spaces (VAS)

• Virtual address spaces

(VASs) are broken into

blocks called "pages"

• Depending on the

program, much of the

virtual address space will

be unused

• Pages can be allocated

"on demand" (i.e. when

the stack grows, etc.)

• All allocated pages can be

stored in secondary

storage (hard drive)

0 1 2 3 Unalloc 0 1 2 Unalloc

Secondary

Storage

… Unalloc … 0 1 2 3 0 1 2

Used/Unused Blocks in

Virtual Address Space

Code 0x 0xffff ffff Mapped I/O

Program/Process

Data

Heap

Stack

  • 0xc 0x 0 - Code 1 - Code 2 - Data 4 - Heap … 3 - Stack 0 - Code 1 - Code 2 - Data 4 - Heap 3 - Stack

0 1 2 3 Unalloc 0 1 2 Unalloc … Unalloc … 0 1 2 3 0 1 2 0 - Code 1 - Code 2 - Data 4 - Heap … 3 - Stack 0 - Code 1 - Code 2 - Data 4 - Heap 3 - Stack

Physical Address Space (PAS)

  • Physical memory is broken into

page-size blocks called "frames"

  • Multiple programs can be running

and their pages can share the

physical memory

  • Physical memory acts as a cache

for pages with secondary storage

acting as the backing store (next

lower level in the hierarchy)

  • A page can be:
    • Unallocated (not needed

yet…stack/heap)

  • Allocated and residing in secondary

storage ( Uncached )

  • Allocated and residing in main

memory ( Cached )

0x 0x3fffffff

1GB Physical

Memory and

32 - bit Address

Space

0xffffffff

Secondary

Storage

Fictitious Virtual

Address Spaces

frame 0 - Code Pg. 0 Pg. 2 2 - Data Pg. 0 frame

I/O

and un- used area

Virtual vs. Physical Addresses

  • Key : Programs are written using virtual

addresses

  • HW & the OS will translate the virtual

addresses used by the program to the

physical address where that page resides

  • If an attempt is made to access a page

that is not in physical memory, HW

generates a "page fault exception" and

the OS is invoked to bring in the page to

physical memory (possibly evicting

another page)

  • Notice: Virtual addresses are not unique
    • Each program/process has VA: 0x Translation Unit / MMU (Mem. Mgmt. Unit)
Proc.
Core

Memory Data PA: 0x PA:0x3fffffff frame 0 - Code

Physical

Memory and

Address Space

Pg. 0 Pg. 2 2 - Data Pg. 0 frame

I/O

and un- used area 0xffffffff

Secondary

Storage

Fictitious Virtual

Address Spaces

0 1 2 3 Unalloc 0 1 2 Unalloc … Unalloc … 0 1 2 3 0 1 2 0 - Code 1 - Code 2 - Data 4 - Heap … 0 - Code^3 -^ Stack 1 - Code 2 - Data 4 - Heap 3 - Stack PA: 0x11f PA: 0x21b VA: 0x VA: 0x Virtual Addr Physical Addr

Summary

  • Program takes an abstract (virtual) view of

memory and uses virtual addresses and

necessary data is broken into large chunks

called pages

  • HW and OS work together to bring pages into

main memory acting as a cache and allowing

sharing

  • HW and OS work together to perform

translation between:

  • Virtual address : Address used by the process

(programmer)

  • Physical address : Physical memory location of

the desired data

  • Translation allows protection against other

programs

Translation Unit / MMU (Mem. Mgmt. Unit)

Proc.
Core

Virtual Addr Memory Physical Addr Data PA: 0x PA:0x3fffffff frame 0 - Code

Physical

Memory and

Address Space

Pg. 0 Pg. 2 2 - Data Pg. 0 frame

I/O

and un- used area 0xffffffff

Secondary

Storage

Fictitious Virtual

Address Spaces

0 1 2 3 Unalloc 0 1 2 Unalloc … Unalloc … 0 1 2 3 0 1 2 0 - Code 1 - Code 2 - Data 4 - Heap … 0 - Code^3 -^ Stack 1 - Code 2 - Data 4 - Heap 3 - Stack PA: 0x11f PA: 0x21b VA: 0x VA: 0x

ADDRESS TRANSLATION

Page Tables

Page Size and Address Translation

  • Since pages are usually retrieved from disk, we size them to be fairly large

(several KB) to amortize the large access time

  • Virtual page number to physical page frame translation performed by HW

unit = MMU (Mem. Management Unit)

  • Page table is an in-memory data structure that the HW MMU will use to look

up translations from VPN to PPFN

Virtual Address Virtual Page Number^ Offset within page

31 12 11 0

Physical Address Offset within page

Phys. Page Frame
Number

31 30 12 11 0

Copied

12

Translation
Process
(MMU +
Page Table)

29 20 18

0 0 2 1 b 0 0 0

Lookup VPN 0x
to it lives in PPFN: 0x0021b

Analogy for Page Tables

• Suppose we want to build a caller-ID mechanism for your

contacts on your cell phone

  • Let us assume 1000 contacts represented by a 3-digit integer (0-999) in

the cell phone (this ID can be used to look up their names)

  • We want to use a simple array (or Look-Up Table (LUT)) to translate

phone numbers to contact ID’s, how shall we organize/index our LUT

LUT indexed w/

contact ID

LUT indexed w/ all

possible phone #’s

000 - 000 - 0000 null .. … null

Sorted LUT indexed

w/ used phone #’s

O(n) - Doesn’t Work

We are given phone # and

need to translate to ID

(1000 accesses)

O(log n) - Could Work

Since its in sorted order we

could use a binary search

(log 2 1000 accesses)

O(1) - Could Work

Easy to index & find but

LARGE

(1 access)

Page Tables

  • VA is broken into:
    • VPN (upper bits) + Page offset: Based on page size (i.e. 0 to 4K-1 for 4KB page)
  • MMU uses VPN & PTBR to access the page table in memory and lookup physical frame

(i.e. like an array access where VPN is the index: PTBR[VPN] )

  • Each entry is referred to as a Page Table Entry (PTE) and holds the physical frame number &

bookkeeping info

  • Physical frame is combined with offset to form physical address
  • For 20-bit VPN, how big is the page table? (See below)

Virtual Page Number Offset w/in page VA

31 12 11 0

Page Table Size

= 2^20 entries * 18 bits

= approx. 2^20 *4bytes = 4MB

PTBR = Page Table Base Reg.

Phys. Frame # Offset w/in page PA

31 12 11 0

PTE

Page Frame Number PTE … Other flags 20

Page Table in Main Memory

18

Processor

0xc 0xc PTBR[2] PTBR[1] PTBR[0] … 0x0021b 0x0021b 0x00002^ 0x2d 0x2d