Docsity

Virtual Memory: Understanding Indirection and Implementation, Slides of Computer Science

Partial preview of the text

Download Virtual Memory: Understanding Indirection and Implementation and more Slides Computer Science in PDF only on Docsity!

CSE 410Computer Systems

L

t^

Vi t

l M

Lecture 20 – Virtual Memory

Virtual Memory (and Indirection)Virtual

Memory (and Indirection)

-^

Virtual Memory– We’ll talk about the motivations for virtual memory– We’ll talk about how it is implemented– Lastly, we’ll talk about how to make virtual memory fast:

Translation Lookaside Buffers (TLBs).

a s at o

oo as de

u e s (

s)

3

More Real ProblemsMore

Real Problems

Running multiple programs at the same time brings up more problems.1. Even if each program fits in memory, running 10 programs might

not.^ –

This is really the same problem as on the previous slide.

y^

p^

p

  1. Multiple programs may want to store something at the same

address.

i e

what if both Program A and B want to use address

-^

i.e., what if both Program A and B want to use address0x10000000 as the base of their stack?

-^

It is impractical (if not impossible) to compile every pair ofprograms that could get executed together to use distinct setsof addressesof addresses.

  1. How do we protect one program’s data from being read or written

by another program?

5

The Big IdeaThe

Big Idea

-^

Separate program notion of memory addresses from

p

p

g

y

actual physical memory locations– Program memory = virtual addresses– Physical memory = real addresses– Use hardware to map between the two

6

IndirectionIndirection •^

“Any problem in CS can be solved by adding a level

y p

y

g

of indirection”

-^

Without Indirection

Name

Thing

Name

Thing

-^

With Indirection

Thing

8

IndirectionIndirection

-^

Indirection

: Indirection is the ability to reference

thi

i^

f^

t i

i^

t^

d

something using a name, reference, or container insteadthe value itself. A flexible mapping between a name anda thing allows changing the thing without notifying

g

g

g

g

y

g

holders of the name.

Name

Thing

-^

Without Indirection

Thing

-^

With Indirection

N

ame

ThingThingThing

9

Virtual MemoryVirtual

Memory

•^

Because different processes will have different mappings fromvirtual to physical addresses, two programs can freely use thevirtual to physical addresses, two programs can freely use thesame virtual address.

-^

By allocating distinct regions of physical memory to A and B,they are prevented from reading/writing each others data.they are prevented from reading/writing each others data.

ess r

PhysicalMemory

Virt

Program A

Program B

tual Addr r

tual Addre

Vir

Disk

ess

11

Caching revisitedCaching

revisited

•^

Once the translation infrastructure is in place, the problem boilsdown to cachingdown to caching.– We want the size of disk, but the performance of memory.

-^

The design of virtual memory systems is really motivated by theThe design of virtual memory systems is really motivated by thehigh cost of accessing disk.– While memory latency is ~100 times that of cache, disk

latency is ~100,000 times that of memory.

•^

Hence, we try to minimize the miss rate:– VM “pages” are much larger than cache blocks. Why?

A fully associative policy is used

  • A fully associative policy is used.
    • With approximate LRU

•^

Should a write-through or write-back policy be used?Should a write through or write back policy be used?

12

Finding the right pageFinding

the right page

•^

If it is fully associative, how do we find the right page without scanningall of memory?all of memory?– Use an index, just like you would for a book.Our index happens to be called the page table:

-^

Our index happens to be called the page table:– Each process has a separate page table

  • A “page table register” points to the current process’s page

tabletable

  • The page table is indexed with the virtual page number (VPN)
    • The VPN is all of the bits that aren’t part of the page offset. Each entry contains a valid bit and a physical page number (PPN)
      • Each entry contains a valid bit, and a physical page number (PPN)
        • The PPN is concatenated with the page offset to get the

physical address No tag is needed because the index is the full VPN

  • No tag is needed because the index is the full VPN.

14

Page Table picturePage

Table picture

Virtual address

Page table register

Page offset

Virtual page number

Virtual address

20

12

31

30 29

28 27

15 14

13

12

11 10

9

8

3 2 1 0

Physical page number

Valid

Page table

If 0 then page is notpresent in memory

18

29

28

27

15

14

13

12

11

10

9 8

3 2

1

0

Page offset

Physical page number

Physical address

15

Dealing with large page tablesDealing

with large page tables

•^

Multi-level page tables– “Any problem in CS can be solved by adding a level of indirection”

Any problem in CS can be solved by adding a level of indirection

  • or two…

Page TableB

P i t

2nd

A 3 l

l^

bl

Base Pointer

1st

3rd

A 3-level page table

VPN

VPN

VPN

offset PPN

PPN

offset

•^

Since most processes don’t use the whole address space, you don’tll

t^

th

t bl

th t

’t^

d d

allocate the tables that aren’t needed– Also, the 2nd and 3rd level page tables can be “paged” to disk.

17

Waitaminute!Waitaminute! •^

We’ve just replaced every memory access MEM[addr] with:MEM[MEM[MEM[MEM[PTBR + VPN1<<2] + VPN2<<2] +

VPN3<<2] + offset]

-^

i.e.

, 4 memory accesses

•^

And we haven’t talked about the bad case yet (

i.e.

,^ page

y^

(^

, p g

faults)… “Any problem in CS can be solved by adding a level of indirection”

y p

y^

g

  • except too many levels of indirection…

•^

How do we deal with too many levels of indirection?

y

18

What about a TLB miss?What

about a TLB miss?

-^

If we miss in the TLB, we need to “walk the page table”– In MIPS, an exception is raised and software fills the TLB– In x86, a “hardware page table walker” fills the TLB

,^

p g

-^

What if the page is not in memory?

This situation is called a

page fault

  • This situation is called a

page

fault

  • The operating system will have to request the page from

disk.It

ill

d t

l^

t^

t^

l

  • It will need to select a page to replace.
    • The O/S tries to approximate LRU (coming next)
      • The replaced page will need to be written back if dirty.

20

Memory ProtectionMemory

Protection

-^

In order to prevent one process from reading/writing

p

p

g

g

another process’s memory, we must ensure that aprocess cannot change its virtual-to-physical translations.

-^

Typically this is done by:Typically, this is done by:– Having two processor modes: user & kernel.

  • Only the O/S runs in kernel mode
    • Only allowing kernel mode to write to the virtual

memory state,

e.g.

  • The page table

The page table

  • The page table base pointer• The TLB

21