CS 152 Computer Architecture and Engineering Lecture …cs152/sp05/lecnotes/lec8-2.pdf · CS 152...

20
UC Regents Spring 2005 © UCB CS 152 L16: Virtual Memory 2005-3-10 John Lazzaro (www.cs.berkeley.edu/~lazzaro) CS 152 Computer Architecture and Engineering Lecture 16 Virtual Memory www-inst.eecs.berkeley.edu/~cs152/ TAs: Ted Hong and David Marquardt

Transcript of CS 152 Computer Architecture and Engineering Lecture …cs152/sp05/lecnotes/lec8-2.pdf · CS 152...

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

2005-3-10John Lazzaro

(www.cs.berkeley.edu/~lazzaro)

CS 152 Computer Architecture and Engineering

Lecture 16 – Virtual Memory

www-inst.eecs.berkeley.edu/~cs152/

TAs: Ted Hong and David Marquardt

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

Last Time: Practical Cache Design

ToCPU

ToLowerLevelMemory

ToCPU

ToLowerLevelMemory

TagsBlocks

Addr

Din

Dout

Addr

Din

Dout

State Machine

Control

Control Control

Just like pipelining lab, you will write assembly language programs to test the “edge cases” that will find bugs in your state machine ... TAs have their own test suite for checkoffs.

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

Today’s Lecture - Virtual Memory

Virtual address spaces

Page table layout

TLB design options

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

The Limits of Physical Addressing

CPU Memory

A0-A31 A0-A31

D0-D31 D0-D31

“Physical addresses” of memory locations

Data

All programs share one address space: The physical address space

No way to prevent a program from accessing any machine resource

Machine language programs must beaware of the machine organization

Where we are in CS 152 ...

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

Solution: Add a Layer of Indirection

CPU Memory

A0-A31 A0-A31

D0-D31 D0-D31

Data

User programs run in an standardizedvirtual address space

Address Translation hardware managed by the operating system (OS)

maps virtual address to physical memory

“Physical Addresses”

AddressTranslation

Virtual Physical

“Virtual Addresses”

Hardware supports “modern” OS features:Protection, Translation, Sharing

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

MIPS R4000: Address Space ModelProcess A

AddressError

2 GB

0

231

232- 1

Process B

AddressError

2 GB

0

231

232- 1

Process A and B have independent address spaces

ASID = 12 ASID = 13

ASID = Address Space Identifier

When Process A writes its address 9, it writes to a different physical memory location than

Process B’s address 9

May only be accessed by kernel/supervisor

To let Process A and B share memory, OS maps parts of

ASID 12 and ASID 13 to the same physical memory locations.

All address spacesuse a standard memory map

Still works (slowly!) if a process accesses more virtual memory than the machine has physical memory

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

MIPS R4000: Who’s Running on the CPU?System Control Registers

Chapter 4

80 MIPS R4000 Microprocessor User' s Manual

4.3 System Control Coprocessor

The System Control Coprocessor (CP0) is implemented as an integral part

of the CPU, and supports memory management, address translation,

exception handling, and other privileged operations. CP0 contains the

registers shown in Figure 4-7 plus a 48-entry TLB. The sections that follow

describe how the processor uses the memory management-related

registers†.

Each CP0 register has a unique number that identifies it; this number is

referred to as the register number. For instance, the Page Mask register is

register number 5.

Figure 4-7 CP0 Registers and the TLB

† For a description of CP0 data dependencies and hazards, please see Appendix F.

EntryLo02*

EntryHi

Page Mask

Index

Random

Wired

Count

47

0

BadVAddr

TLB

(“Safe” entries)

(See Random Register,

PRId

0127

8*

15*

Compare

11*

Config

16*

LLAddr

17*

WatchLo

18*

WatchHi

19*

TagLo

28*TagHi

29*

contents of TLB Wired)ECC

26*

*Register number

Used with exception

processing. SeeUsed with memory

Chapter 5 for details.

EntryLo0

2*

3*

EntryLo1

EntryHi

10*

5*Page Mask

Index

0*

Random1*

Wired6*

ErrorEPC

30*

Context

4*

Status

12*Cause

13*

EPC14*

management system.

CacheErr

27*

XContext

20*

9*

User cannot write supervisor/kernel bits. Supervisor cannot write kernel bit.

User cannot change address translation configuration

Status (12): Indicatesuser, supervisor, or

kernel mode

EntryLo0 (2): 8-bit ASID field codes virtualaddress space ID.

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

MIPS Address Translation: How it works“Physical Addresses”

CPU Memory

A0-A31 A0-A31

D0-D31 D0-D31

Data

TLB also contains ASID andkernel/supervisor bits for virtual address

Virtual Physical

“Virtual Addresses”

TranslationLook-Aside

Buffer(TLB)

Translation Look-Aside Buffer (TLB)A small fully-associative cache of

mappings from virtual to physical addresses

Fast common case: Virtual address is in TLB, process has permission to read/write it.

What is the table

ofmappings

that it caches?

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

Page tables encode virtual address spaces

A machine usually supports

pages of a few sizes

(MIPS R4000):

MIPS R4000 Microprocessor User' s Manual 87

Memory Management

EntryLo0 (2), and EntryLo1 (3) Registers

The EntryLo register consists of two registers that have identical formats:

• EntryLo0 is used for even virtual pages.

• EntryLo1 is used for odd virtual pages.

The EntryLo0 and EntryLo1 registers are read / write registers. They hold

the physical page frame number (PFN) of the TLB entry for even and odd

pages, respectively, when performing TLB read and write operations.

Figure 4-10 shows the format of these registers.

PageMask Register (5)

The PageMask register is a read / write register used for read ing from or

writing to the TLB; it holds a comparison mask that sets the variable page

size for each TLB entry, as shown in Table 4-9.

TLB read and write operations use this register as either a source or a

destination; when virtual addresses are presented for translation into

physical address, the corresponding bits in the TLB identify which virtual

address bits among bits 24:13 are used in the comparison. When the Mask

field is not one of the values shown in Table 4-9, the operation of the TLB

is undefined .

Table 4-9 Mask Field Values for Page Sizes

Page SizeBi t

24 23 22 21 20 19 18 17 16 15 14 13

4 Kbytes 0 0 0 0 0 0 0 0 0 0 0 0

16 Kbytes 0 0 0 0 0 0 0 0 0 0 1 1

64 Kbytes 0 0 0 0 0 0 0 0 1 1 1 1

256 Kbytes 0 0 0 0 0 0 1 1 1 1 1 1

1 Mbyte 0 0 0 0 1 1 1 1 1 1 1 1

4 Mbytes 0 0 1 1 1 1 1 1 1 1 1 1

16 Mbytes 1 1 1 1 1 1 1 1 1 1 1 1

PhysicalMemory Space

A valid page table entry codes physical memory “frame” address for the page

A virtual address spaceis divided into blocks

of memory called pagesframeframe

frame

frame

A page table is indexed by a virtual address

virtual address

Page Table(One per ASID)

OS manages the page table for each ASID

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

4/19/04 ©UCB Spring 2004CS152 / Kubiatowicz

Lec21.21

CPU Registers100s Bytes<10s ns

CacheK Bytes10-100 ns$.01-.001/bit

Main MemoryM Bytes100ns-1us$.01-.001

DiskG Bytesms10 - 10 cents

-3 -4

CapacityAccess TimeCost

Tapeinfinitesec-min10-6

Registers

Cache

Memory

Disk

Tape

Instr. Operands

Blocks

Pages

Files

StagingXfer Unit

prog./compiler1-8 bytes

cache cntl8-128 bytes

OS512-4K bytes

user/operatorMbytes

Upper Level

Lower Level

faster

Larger

Recall: Levels of the Memory Hierarchy

4/19/04 ©UCB Spring 2004CS152 / Kubiatowicz

Lec21.22

° Virtual memory => treat memory as a cache for the disk° Terminology: blocks in this cache are called “Pages”

• Typical size of a page: 1K — 8K

° Page table maps virtual page numbers to physical frames• “PTE” = Page Table Entry

Physical Address Space

Virtual Address Space

What is virtual memory?

Virtual Address

Page Table

indexintopagetable

Page TableBase Reg

VAccessRights PA

V page no. offset10

table locatedin physicalmemory

P page no. offset

10

Physical Address

4/19/04 ©UCB Spring 2004CS152 / Kubiatowicz

Lec21.23

Three Advantages of Virtual Memory

° Translation:• Program can be given consistent view of memory, even

though physical memory is scrambled

• Makes multithreading reasonable (now used a lot!)

• Only the most important part of program (“Working Set”) must be in physical memory.

• Contiguous structures (like stacks) use only as much physical memory as necessary yet still grow later.

° Protection:• Different threads (or processes) protected from each other.

• Different pages can be given special behavior- (Read Only, Invisible to user programs, etc).

• Kernel data protected from User programs

• Very important for protection from malicious programs=> Far more “viruses” under Microsoft Windows

° Sharing:• Can map same physical page to multiple users

(“Shared memory”) 4/19/04 ©UCB Spring 2004CS152 / Kubiatowicz

Lec21.24

What is the size of information blocks that are transferred from secondary to main storage (M)? ⇒ page size(Contrast with physical block size on disk, I.e. sector size)

Which region of M is to hold the new block ⇒ placement policy

How do we find a page when we look for it? ⇒ block identification

Block of information brought into M, and M is full, then some region of M must be released to make room for the new block ⇒ replacement policy

What do we do on a write? ⇒ write policy

Missing item fetched from secondary memory only on the occurrence of a fault ⇒ demand load policy

pages

reg

cachemem disk

frame

Issues in Virtual Memory System Design

V=0 pages either reside on disk or

have not yet been allocated.

OS handles V=0“Page fault”

Physical and virtual pages must be the

same size!

The TLB caches page table entries

TLB

Page Table

2

0

1

3

virtual address

page off

2frame page

250

physical address

page off

TLB caches page table

entries.

MIPS handles TLB misses in software (random replacement). Other

machines use hardware.

for ASID

Physicalframe

address

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

Page tables may not fit in memory!A table for 4KB pages for a 32-bit

address space has 1M entries Each process needs its own address space!

P1 index P2 index Page Offset

31 12 11 02122

32 bit virtual address

Top-level table wired in main memory

Subset of 1024 second-level tables in main memory; rest are on disk or

unallocated

Two-level Page Tables

4/19/04 ©UCB Spring 2004CS152 / Kubiatowicz

Lec21.25

How big is the translation (page) table?

° Simplest way to implement “fully associative” lookup policy is with large lookup table.

° Each entry in table is some number of bytes, say 4

° With 4K pages, 32- bit address space, need:232/4K = 220 = 1 Meg entries x 4 bytes = 4MB

° With 4K pages, 64-bit address space, need:264/4K = 252 entries = BIG!

° Can’t keep whole page table in memory!

Virtual Page Number Page Offset

4/19/04 ©UCB Spring 2004CS152 / Kubiatowicz

Lec21.26

Large Address Spaces

Two-level Page Tables

32-bit address:

P1 index P2 index page offest

4 bytes

4 bytes

4KB

10 10 12

1KPTEs

° 2 GB virtual address space

° 4 MB of PTE2

– paged, holes

° 4 KB of PTE1

What about a 48-64 bit address space?

4/19/04 ©UCB Spring 2004CS152 / Kubiatowicz

Lec21.27

Inverted Page Tables

V.Page P. FramehashVirtual

Page

=

IBM System 38 (AS400) implements 64-bit addresses.

48 bits translated

start of object contains a 12-bit tag

=> TLBs or virtually addressed caches are critical

4/19/04 ©UCB Spring 2004CS152 / Kubiatowicz

Lec21.28

Virtual Address and a Cache: Step backward???

° Virtual memory seems to be really slow:

• Must access memory on load/store -- even cache hits!

• Worse, if translation not completely in memory, may need to go to disk before hitting in cache!

° Solution: Caching! (surprise!)

• Keep track of most common translations and place them in a “Translation Lookaside Buffer” (TLB)

CPUTrans-lation

CacheMain

Memory

VA PA miss

hit

data

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

VM and Disk: Page replacement policy

...

Page Table

1 0

useddirty

1 00 11 10 0

Set of all pagesin Memory Tail pointer:

Clear the usedbit in thepage table

Head pointerPlace pages on free list if used bitis still clear.Schedule pages with dirty bit set tobe written to disk.

Freelist

Free Pages

Dirty bit: page written.

Used bit: set to1 on any reference

Architect’s role: support setting dirty

and used bits

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

Admin: Final Xilinx Checkoff Friday

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

Next week: Midterms, then spring break!

Tuesday review session

Midterm is one week from today, in evening, no class that day.

Will cover format, material, and ground rules for test in this session.

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

TLB Design Concepts

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

MIPS R4000 TLB: A closer look ...“Physical Addresses”

CPU MemorySystem

A0-A31 A0-A31

D0-D31 D0-D31

Data

TranslationLook-Aside

Buffer(TLB)

Virtual Physical

“Virtual Addresses”

MIPS R4000 Microprocessor User' s Manual 65

Memory Management

32-bit Mode Address Translation

Figure 4-2 shows the virtual-to-physical-address translation of a 32-bit

mode address.

• The top portion of Figure 4-2 shows a virtual add ress w ith a

12-bit, or 4-Kbyte, page size, labelled Offset. The remaining 20

bits of the add ress represent the VPN, and index the 1M-entry

page table.

• The bottom portion of Figure 4-2 shows a virtual add ress w ith

a 24-bit, or 16-Mbyte, page size, labelled Offset. The remaining

8 bits of the add ress represent the VPN, and index the 256-

entry page table.

Figure 4-2 32-bit Mode Virtual Address Translation

28 11 0

20 12

2931

VPN Offset

3239

ASID

8

Virtual Address with 1M (220) 4-Kbyte pages

23 0

8 24

Offset

39

Virtual Address with 256 (28)16-Mbyte pages

8 bits = 256 pages

20 bits = 1M pages 12

ASID

8

28 293132

VPN

24

Virtual-to-physicaltranslation in TLB

Bits 31, 30 and 29 of the virtualaddress select user, supervisor,or kernel address spaces.

Offset passedunchanged tophysicalmemory

Virtual-to-physicaltranslation in TLB

TLB

TLB

35 0

PFN Offset

Offset passedunchanged tophysicalmemory

36-bit Physical Address

Physical space larger than virtual space!Checked

againstCPO ASID

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

Can TLB and caching be overlapped?

Index Byte Select

Valid

Cache Block

Cache Block

Cache Tags Cache Data

Data out

Virtual Page Number Page Offset

TranslationLook-Aside

Buffer(TLB)

Virtual

Physical

=

Hit

Cache Tag

This works, but ...

Q. What is the downside?

A. Inflexibility. VPN size locked to cache tag size.

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

Can we cache virtual addresses?“Physical Addresses”

CPU Main Memory

A0-A31 A0-A31

D0-D31 D0-D31

Only use TLB on a cache miss !

TranslationLook-Aside

Buffer(TLB)

Virtual Physical

“Virtual Addresses”

A. Synonym problem. If two address spaces share a physical frame, data may be in cache twice.

Maintaining consistency is a nightmare.

CacheVirtual

D0-D31

Downside: a subtle, fatal problem. What is it?

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

Conclusions

VM: Uniform memory models,protection, sharing.

Operating systems manage the page table and (often) the TLB

A TLB acts as a fast cache forrecent address translations.

UC Regents Spring 2005 © UCBCS 152 L16: Virtual Memory

Next week: Midterms, then spring break!

Tuesday review session

Midterm is one week from today, in evening, no class that day.

Will cover format, material, and ground rules for test in this session.