3. THREE PROBLEMS WITH MEMORY
Suppose we have to run a programs which requires 32 bit address space.
What if you don’t have 4GB (2^32 bytes of memory)??
Program can access any bite form the 32 bit address space.
#1:What if we don’t have enough memory?
32 bit program address
space(4GB) 30 bit RAM address space(1GB)
??
Crash if we will try to access
more than 1 GB
4. 1 GB
How do program share the memory?
Where do we put them?
#2:Holes in our address space
32 bit program address
space(4GB)
Program 1
1 GB
Program 3
2 GB
Program 2
2 GB
1.Programs 1 and 2 fit
(they use 3 GB of memory, leaving 1GB free)
2.Quit Program 1
(program 2 uses 2GB of memory, leaving 2GB free)
3.Cant run Program 3
(even though we have enough free space)
1 GB
Memory fragmentation
5. #3:How do we keep program secure?
RAM address
space(4GB)
Suppose we have to run different programs. Each of them may access 32 bit address space.
What if multiple program access the same address?
- Sw R2, 1024(R0) will write to address 1024 regardless of the program that running.
Program 1
1 GB
Program 2
2 GB
1.Programs 1
Stores your bank balance at address 1024
2.Programs 2
Stores your video game score at address 1024
Program
address 1024
47001200089000
They can corrupt or crash each other: security and reliability
6. Problems with memory
If all programs have access to the same 32-bit memory space:
-Can crash if less than 4GB of RAM memory in the system
-Can run out of space if we run multiple programs
-Can corrupt other programs data
How do we solve this?
-key to the problem :”same memory space”
-Can we give each program it’s own virtual memory space?
-If so we can :
Separate map of each program’s memory space to the RAM memory space.
7. How does VM work?
Virtual and Physical address
Basic idea : separate memory spaces
- e.g. ld R4, 1024 (R0) access virtual address R0 + 1024 = 1024
-virtual memory: what the program sees
How does VM works?
-Physical memory: the physical RAM in the computer
- e.g. if you have 2GB of RAM installed, you have physical address 0 to 2^31-1
Virtual address(VA)
-What the program uses
-This is the full 32 bit address space: 0 to 2^31-1
Physical address(PA)
-What the hardware uses to talk to the RAM
-Address space determined by how much RAM is installed
8. Making VM works: translation
How does a program access memory?
1. Program execute a load specifying a virtual address(VA)
Processor
Ld R3, 1024 (R0)
2. Computer translate the address to the physical address (PA) in memory
Translation
from VA to
PA
RAM
Physical address
space
VA PA
512 12
786 DISK
1024 2
MAP
DATA For R3
3. if the physical address (PA) is not in memory, the operating system load it in from disk)
Ld R5, 786 (R0)
Data fro R3
Update
translati
on map
9. Solving the problem: #1 not enough memory
Map some of the programs address space to the disk.
When we need it, we bring it into memory
MAP
30 bit RAM address
space(1GB)
With Virtual Memory
Program address map to RAM address
Program 0
Program 0
VM maps address 0
to RAM 1
Programs loads
address 0
Programs loads
address 1
32 bit program
address space(4GB)
Program 1 Program 1
VM maps address 1
to RAM 0
Programs loads
address 2
Program 2
Program 2
VM maps address 2
to RAM 2
Programs loads
address 3
Program 3
VM doesn't have
any more space
in RAM
VM moves oldest
data (0) to disk
VM update the map to
point data 0 to disk
VM maps address 3 to
RAM 1
Program 3
Mapping lets us use
our disk to give the
illusion of unlimited
memory
10. Question : VM and performance
Q: What is going to happen to the
program performance when the data its
needs is on the disks and not in
memory?
oBetter performance: we can use more
memory than we have
oNothing: mapping to memory or disk
is just as easy
oWorse performance: reading from
disk is slower than RAM
A:Worse performance: reading from
disk is slower than RAM
Remember that disks are 1000x slower
than RAM. Any time you cant fit your
data to memory and have to go to disk
you pay a HUGE performance
penalty(this is why buying more RAM
makes your computer faster)
11. Solving the problem: #2 holes in the address space
•How do we use the holes left when program quit?
•We can map a program’s address to RAM address however we like
1 GB
1 GB
32 bit RAM
address space(4GB)
Program 1
2 GB
Program 2
2 GB
Program 2
2 GB
MAP 2
MAP 3
With Virtual Memory
Program address map to RAM address
12. RAM address
space(4GB)
Program 1’s and program 2’s address map to different RAM addresses
Program 1
1 GB
Program 2
2 GB
1.Programs 1
Stores your bank balance at
address 1024
2.Programs 2
Stores your video game score at
address 1024
Program 1
address 1024
Program 2
address 1024
4700
1200089000
Solving the problem: #3 keeping programs secure
With Virtual Memory
Program address map to RAM address
MAP 1
MAP 2
Because each program has its own address space, they cant access each other’s
data: security and reliability
VM maps it to RAM address 1
VM maps it to RAM address 4
Neither can touch the other’s data
13. Question : is program isolation is always good?
Q: virtual memory lets us isolate programs so
they cant share/corrupt data. What is the down
side of complete isolation?
oPrograms cant corrupt each other.
oProgram cant share data with each other
oProgram uses more space because they have
their own address space
oProgram are slower because they always have
to check the disk for the data
A:Program cant share data with each
other
However we can use the same mapping
to allow programs to share data by
simply having their maps point to the
same data.
14. The map for virtual address to physical address is the page table
So far we have had one page table entry for every virtual address.
Processor
Ld R3, 1024 (R0)
Translation
from VA to PA
RAM
Physical address space
VA PA
512 12
786 DISK
1024 2
MAP
DATA For R3
Ld R5, 786 (R0)
Data fro R3
Page Table
Program virtual
address space
Q: How many entries do we need in
our table?
•1 for every byte = 2^32 = 4 billions
•1 for every word = 2^30= 1 billion
A: 1 for every word = 2^30 = 1 billions
We have a word-aligned memory so
we need to be able to address every
word.
15. Pages:
Each entry now
cover 4KB of
data
Page Table size
We need to translate every possible address:
-Our program have 32-bit virtual address spaces
-That is 2^30 words that needs Page Table Entries (1 Billion entries)
-If they don’t have a page table entry then we cant access them because we
cant find the physical address
How can we make this more manageable ?
VA PA
512 12
786 DISK
1024 2
MAP
Page table
Translate VA to
PA
Fine grain:
Maps each
word address
2^30 words to
map
What if we divide memory up into chunks(pages) instead of words ?
MAP
VA PA
0-4095 4096-8191
Page table
Translate VA to
PA
Coarse grain:
Maps chunks of
address fewer
mapping
16. Coarse grain: pages instead of words
The page table manages the larger chunks (pages) of data
-fewer page table entries needed to cover the whole address space
MAP
VA PA
0-4095 4096-8191
Page table
Translate VA to
PA
Coarse grain:
Maps chunks of
address fewer
mapping
-But, less flexibility in how to use the RAM (have to move a page at a time)
Today:
-typically 4kB pages (1024 per page)
Q: How many entries do
we need in our Page table
for a page of length 4kB ?
•1 for every word = 2^30 =
1 billions
•1 for every 1024 word = 1
million
•1 for every 4096 word
=262144
A: 1 for every 1024 word = 1
million
With 4kB pages we have
1024 words per page. that
means we need one page
table entry for every 1024
words, so 1 billion / 1024 is
1 million page table entries.
This is much more
manageable.
17. Address Translation
What happen to a 32 bit machine with 256 MB of RAM and 4kB pages?
32 bit virtual address.
virtual address
28 bit physical address.
physical address
Page table
But each PTE handles
4kB of address space
For every page we have
4096 address (12 bits)
that don’t get translated
12 bits
12 bits
Page offset
Page offset
20 bits
16 bits
Virtual page address
Physical page number
32 bits
28 bits
18. Page offset and translation
What happen to a 32 bit machine with
256 MB of RAM and 4kB pages?
4kB
4015
0
4kB
8191
4096
4kB
12287
8192
4kB
16383
12288
Page
offset
Which
page
12 bits
Page offset
20 bits
Virtual Page number
Virtual Page number 0
Virtual Page number 1
Virtual Page number 2
Virtual Page number 3
4kB
4015
0
4kB
8191
4096
4kB
12287
8192
Virtual address space physical address space
Page
offset
Which
page
12 bits
Page offset
physical Page number 0
physical Page number 1
physical Page number 2
20 bits
physical Page numberTranslation
19. 20 bits:
virtual
number
How to do a page table lookup
12 bits
12 bits
Page offset
Page offset
20 bits
16 bits
Virtual page address
Physical page number
32 bits
28 bits
Page table
0x00000
0x00001
0x00002
0x00003
……………
0xfffff
DISK
0x0003
0x0004
0x0006
Physical page number
0x000f6
Page table entry
tells us Which
page
11 031 12
11 027 12
0x00003204
Q:Why we have
only five bits in
this page table
for every entry?
A: Because this
representation is
in Hexadecimal
number system
Q:So now tell me
the required bits
for page offset
A: 3, obviously!!
0x204
0x204
0x204
0x00003
0x0006
204
12 bits:
page
offset
0x0006
16 bits:
physical
page
number
Q:find its physical
address
0x00000783