You are on page 1of 23

Lecture 25

Virtual Memory

Why go Virtual ? Because sometimes real is not good enough.


Physical memory

Case 1: My program is larger than physical memory. Now what?

Physical memory

Case 1: My program is larger than physical memory. Now what? Consideration: Will the user program need the entire physical memory all at once?

U S E R P R O G R A M

Physical memory

Case 1: My program is larger than physical memory. Now what? Fact: The user program will not need the entire physical memory all at once. Solution: overlays.

OVERLAY #1

Physical memory

Case 1: My program is larger than physical memory. Now what?


OVERLAY #2

Fact: The user program will not need the entire physical memory all at once. Problem: the burden is all on the programmer (loading, unloading, partitioning,etc).

The Principle of Locality

Temporal Locality

1) When a memory item is referenced once, its likely to be referenced again in the near future. 2) When I access a memory location, it is likely that I will access also other locations in the vicinity of the first.

Spatial Locality

Case 2: Several programs are running concurrently. XWindow A window manager (twm, fvwm, sawfish, etc) A command shell (csh, bash, tcsh, etc) xemacs xspim

Physical memory

OS (Unix kernel) XWindow Window manager shell emacs xspim

Case 2: Several programs are running concurrently. XWindow A window manager (twm, fvwm, sawfish, etc) A command shell (csh, bash, tcsh, etc) xemacs xspim

Physical memory

OS (Unix kernel) XWindow Window manager shell emacs xspim

Problem: I have less physical memory than I need.

Case 2: Several programs are running concurrently. XWindow A window manager (twm, fvwm, sawfish, etc) A command shell (csh, bash, tcsh, etc) xemacs xspim

Physical memory

OS (Unix kernel) XWindow Window manager shell xemacs xspim

Fact: I dont need all of everything at the same time.

The Principle of Locality

Temporal Locality

1) When a memory item is referenced once, its likely to be referenced again in the near future. 2) When I access a memory location, it is likely that I will access also other locations in the vicinity of the first.

Spatial Locality

10

The Basics of Virtual Memory


The main idea is to design a scheme that: 1) Supports a larger address space than the physical, 2) Achieves this by swapping data in and out of disk, 3) Gives each program the illusion of owning all memory, 4) Is transparent to the programmer, 5) On top of it all, is efficient!

11

The Basics of Virtual Memory


Remember the costs of disk usage in terms of access time and monetary units:
Technology Access Time Cost per Mbyte

SRAM DRAM Mag. Disk

5-25ns 60-120ns 10-20ms

$100-$250 $5-$10 $0.10-$0.20

Disk space is cheap, but disk access is not! Side comment: Disk access cost has three components: the time to move the disk head to the right cylinder, the time to reach the sector in the cylinder and transfer time.

12

The Basics of Virtual Memory


Pages may reside in physical memory when their contents are being actively used or in disk when their contents have not been used for a while.

Divide physical memory into equally sized portions called pages.


Physical memory

page i

DISK

13

A program may use a large number N of pages, even if (N*page size) is greater than the physical memory. The pages that a program uses need not be contiguous nor in order in physical memory. The programmer doesnt need to explicitly move pages to and from disk. The programmer sees the address space as if it were in a memory much larger than the physical memory. Question: What do we need to create this illusion for the programmer?

Physical memory

xemacs 3

xemacs 1 xemacs 0 xemacs 2

14

We need a mapping mechanism


Virtual memory Physical memory

xemacs 3 xemacs 0 xemacs 1 xemacs 2 xemacs 3


Address Translation: from virtual address to physical address

xemacs 1 xemacs 0 xemacs 2

15

Virtual memory

Points to notice: 1) Virtual memory defines a virtual address space. xemacs 0 xemacs 1 xemacs 2 xemacs 3 2) Virtual address space is made of pages of the same size as the physical address space. 3) The pages in virtual address space are nicely contiguous and ordered.

16

Address Translation for Virtual Memory


Virtual address Virtual address
31 30 29 28 27 15 14 13 12 11 10 9 8 Page offset 3210

Virtual page number

Translation 29 28 27 15 14 13 12 Physical page number Physical address 11 10 9 8 Page offset 3210

Example: Page size = 4KB = 2^12 Bytes Physical memory size = 2^18 pages = 2^30 Bytes = 1 GB Virtual memory size = 2^32 Bytes = 4 GB Question: What about this mapping from a larger to a smaller space?

17

Address (showing bit positions) Address (showing bit positions) 31 30 13 12 11 210


Byte offset

Remember cache mapping?


Hit Tag 20 Index Index Valid Tag 0 1 2 Data 10

Data

1021 1022 1023 20 32

18

Important Considerations
Cache miss Page Fault: how do the penalties compare?

Pages should be large to amortize access time, but not too large. Why?) What about writing? Should we think write through or write back? Should we use a fully associative or a direct mapping scheme? Why? Should we leave page fault handling to software or should we make it a hardware task?

19

Mapping Mechanism for Virtual Memory


Page table register

Virtual address
31 30 29 28 27 15 14 13 12 11 10 9 8 Virtual page number 3 2 1 0 Page offset 12 Physical page number

20
Valid

Page table
(each program has its own)

Page table

18 If 0 then page is not present in memory 29 28 27 15 14 13 12 11 10 9 8 Physical page number Physical address 3 2 1 0 Page offset

We use fully associative mapping.

20

Mapping Mechanism for Virtual Memory


Page table register

Virtual address
31 30 29 28 27 15 14 13 12 11 10 9 8 Virtual page number 3 2 1 0 Page offset 12 Physical page number

20
Valid

Page table
(each program has its own)

Page table

18 If 0 then page is not present in memory 29 28 27 15 14 13 12 11 10 9 8 Physical page number Physical address 3 2 1 0 Page offset

Question: How many entries in the page table?

21

Interlude: Program, Process and Context


Each program uses the CPU registers as it wants. Each program has its own page table, its own value for the page table register.. If theres more than one program executing under the same CPU, they share the CPU. Sharing means that all the represents one process is swapped in and out of the CPU, depending on whether the process is active or inactive. The operating system handles CPU sharing and, thus, also process context management.

22

The Virtual Memory System


Virtual page number number
Valid 1 1 1 1 0 1 1 0 1 1 0 1 Virtual page Page table Physical page or disk address Physical memory

Disk storage

Page faults are exceptions by definition.

23

You might also like