Virtual Memory - Computer Systems Programming - Lecture Notes | CS 201, Study notes of Computer Science

Material Type: Notes; Class: COMPUTER SYSTEMS PROG; Subject: Computer Science; University: Portland State University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-fwl
koofers-user-fwl 🇺🇸

5

(1)

9 documents

1 / 40

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 201
Virtual Memory
Gerson Robboy
Portland State University
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28

Partial preview of the text

Download Virtual Memory - Computer Systems Programming - Lecture Notes | CS 201 and more Study notes Computer Science in PDF only on Docsity!

CS 201

Virtual Memory

Gerson Robboy

Portland State University

15-213, F’ 02

Motivations for Virtual Memory

Use Physical DRAM as a Cache for the Disk

Use Physical DRAM as a Cache for the Disk

Simplify Memory Management

Simplify Memory Management

Provide Protection

Provide Protection

15-213, F’ 02

Levels in Memory Hierarchy

CPU
regs

C

a

c

h

e

Memory
disk
size:
speed:
$/Mbyte:
line size:
32 B
1 ns
8 B
Register Cache Memory Disk Memory
32 KB-4MB
2 ns
$125/MB
32 B
1024 MB
30 ns
$0.20/MB
4 KB
100 GB
8 ms
$0.001/MB
larger, slower, cheaper

8 B 32 B 4 KB

cache virtual memory

15-213, F’ 02

DRAM as a “Cache”

DRAM vs. disk is more extreme than SRAM vs. DRAM

DRAM vs. disk is more extreme than SRAM vs. DRAM

 Access latencies:

 DRAM ~10X slower than SRAM
 Disk ~100,000X slower than DRAM

 Importance of exploiting spatial locality:

 First byte is ~100,000X slower than successive bytes on disk

 Bottom line:

 Design decisions driven by enormous cost of misses
DRAM
SRAM

Disk

15-213, F’ 02

Locating an Object in “Cache” (cont.)

Data
N-1:
X

Object Name

Location
D:
J:
X: 1
On Disk
Page Table “Cache”

DRAM Cache

DRAM Cache

 Each allocated page of virtual memory has entry in page table

 Mapping from virtual pages to physical pages

 From uncached form to cached form

 If the page is not in memory

 “Present” bit is not set
 Page table entry gives disk address

 OS retrieves information

15-213, F’ 02

Page Faults (like “Cache Misses”)

What if an object is not in memory?

What if an object is not in memory?

 Page table entry indicates virtual address not present

 Page fault

 OS exception handler imoves data from disk into memory

 current process suspends, others can resume
 OS has full control over placement, etc.

CPU

Memory

Page Table

Disk

Virtual

Addresses

Physical

Addresses

CPU

Memory

Page Table

Disk

Virtual

Addresses

Physical

Addresses

Before fault

After fault

15-213, F’ 02

Virtual
Address
Space for
Process 1:
Physical
Address
Space
(DRAM)

VP 1

VP 2

PP 2

Address Translation 0

0

N-

0

N-

M-

VP 1

VP 2

PP 7

PP 10

(e.g., read/only

library code)

Solution: Separate Virt. Addr. Spaces

 Virtual and physical address spaces divided into equal-sized

blocks

 blocks are called “pages” (both virtual and physical)

 Each process has its own virtual address space

 operating system controls how virtual pages as assigned to
physical memory
Virtual
Address
Space for
Process 2:

15-213, F’ 02

What this means for linking/loading

The linker binds programs to absolute addresses.

The linker binds programs to absolute addresses.

 Nothing is left relocatable.

 No relocation at load time.

 No allocation of memory segments at load time.

kernel virtual memory

Memory mapped region

forshared libraries

runtime heap (via malloc)

program text (.text)

initialized data (.data)

uninitialized data (.bss)

stack

forbidden

0

%esp

memory invisible to

user code

the “brk” ptr

All

processes

look just

like this

15-213, F’ 02

Motivation #3: Protection

Page table entry contains access rights information

Page table entry contains access rights information

 hardware enforces this protection (trap into OS if violation

occurs)

Page Tables

Process i:

Read? Write? Physical Addr
Yes No PP 9
Yes Yes PP 4
No No XXXXXXX
VP 0:
VP 1:
VP 2:

Process j:

N-1:
Memory
Read? Write? Physical Addr
Yes Yes PP 6
Yes No PP 9
No No XXXXXXX
VP 0:
VP 1:
VP 2:

15-213, F’ 02

Protection

The O. S. kernel gives each process a virtual memory

The O. S. kernel gives each process a virtual memory

space

space

 Each process has its own set of page tables

 Page tables for other processes are not visible

 Process B ’ s memory is not merely protected from Process A

by permissions

 It doesn ’ t even exist in Process A ’ s memory space

Physical memory belonging to another process is

Physical memory belonging to another process is

completely outside the memory space.

completely outside the memory space.

 Note: sharing is also possible

 In Linux, threads are a kind of process with shared memory.

15-213, F’ 02

What happens when a new process

starts running?

No pages are in memory.

No pages are in memory.

The first access to an instruction causes a page fault.

The first access to an instruction causes a page fault.

Pages are pulled in as needed (

Pages are pulled in as needed (

demand paged

demand paged

15-213, F’ 02

What happens when you say

malloc(32000000)?

What exactly does the O. S. allocate at that time?

What exactly does the O. S. allocate at that time?

Is it necessary to allocate 32 MB of physical memory?

Is it necessary to allocate 32 MB of physical memory?

15-213, F’ 02

Page Replacement algorithms

Analogous to cache line replacement.

Analogous to cache line replacement.

A complex topic.

A complex topic.

 Beyond the scope of this class

 A popular topic with computer scientists because it lends

itself to research.

15-213, F’ 02

What about code?

Code is read-only.

Code is read-only.

We execute it but we don

We execute it but we don

t write it.

t write it.

What happens when the O. S. must evict a page of

What happens when the O. S. must evict a page of

code?

code?

 Does the O. S. write the page out to disk?