0% found this document useful (0 votes)
35 views45 pages

09 Virtualization Memory Address Space Translation

The document discusses memory virtualization in operating systems. It explains that the OS virtualizes physical memory to provide each process with its own isolated address space. This allows multiple processes to run concurrently while sharing physical memory efficiently. The OS translates virtual addresses used by processes to physical addresses in memory. This memory virtualization provides benefits like ease of programming, memory efficiency, and protection between processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views45 pages

09 Virtualization Memory Address Space Translation

The document discusses memory virtualization in operating systems. It explains that the OS virtualizes physical memory to provide each process with its own isolated address space. This allows multiple processes to run concurrently while sharing physical memory efficiently. The OS translates virtual addresses used by processes to physical addresses in memory. This memory virtualization provides benefits like ease of programming, memory efficiency, and protection between processes.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Lecture 9: Virtualizing Memory – Address

Space and Address Translation


The Course Organization (Bottom-up)

Race Conditions, Lock/Semaphore


Concurrency
Thread

Memory Management HW#4


Virtualization
Process and CPU Scheduling HW#3

IO Devices and Storage HW#2


Persistence &
File System Project

System Calls (User-level Programming) HW#1


OS – Resource management via virtualization

OS provides services via


System Call (typically a few
hundred) to run process,
access memory/devices/files,
etc.

The OS manages resources such as


CPU, memory and disk via
virtualization.
 many programs to run
(processes)  Sharing the CPU
 many processes to concurrently
access their own instructions
and data  Sharing memory
 many processes to access
devices  Sharing disks
The Design Of The Unix Operating System (Maurice Bach, 1986)
Part I: Address Space
Memory Virtualization

 What is memory virtualization?


 OS virtualizes its physical memory.

 OS provides an illusion memory space per each process.

 It seems to be seen like each process uses the whole memory .


Benefit of Memory Virtualization

 Ease of use in programming


 Memory efficiency in terms of time and space
 The guarantee of isolation for processes as well as OS
 Protection from errant accesses of other processes
OS in The Early System

 Load only one process in memory.


 Poor utilization and efficiency
0KB O p e r a t in g S y s t e m
(co d e , d a ta , e tc.)

Operating System
(code, data, etc.)

64KB C u rre n t
P ro g ra m
(co d e , d a ta , e tc.)

Current
Program
(code, data, etc.)

max
Physical Memory
Multiprogramming and Time Sharing

 Load multiple processes in memory. 0KB O p e r a t in g S y s t e m


(co d e , d a ta , e tc.)

Operating System
 Execute one for a short while. 64KB F re e
(code, data, etc.)

 Switch processes between them in memory. Free


128KB
Process C
 Increase utilization and efficiency. (code, data, etc.)
192KB
Process B
(code, data, etc.)
256KB F re e

 Cause an important protection issue. Free


320KB
 Errant memory accesses from other processes Process A
(code, data, etc.)
384KB F re e

Free
448KB F re e

Free
512KB
Physical Memory
Address Space

 OS creates an abstraction of physical memory.


 The address space contains all about a running process.

 That is consist of program code, data, heap, and stack

0KB
P ro g ra m
C o d e /D a ta

Program
Code /Data
1KB H eap

Heap
2KB (fre e )

(free)

15KB
S ta c k

Stack
16KB
Address Space
Address Space(Cont.)

 Code
P ro g ra m
C o d e /D a ta

 Where instructions live Program


H eap
Code /Data
 Data Heap
(fre e )

 Global or static local variables

 Heap
(free)
 Dynamically allocate memory.
 malloc in C language
 new in object-oriented language S ta c k

Stack
 Stack
Address Space
 Store return addresses or values.

 Contain local variables arguments to routines.


Example from Linux
Virtual Address

 Every address in a running program is virtual.


 OS translates the virtual address to physical address

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){

printf("location of code : %p\n", (void *) main);


printf("location of heap : %p\n", (void *) malloc(1));
int x = 3;
printf("location of stack : %p\n", (void *) &x);

return x;
}

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){

printf("location of code : %p\n", (void *) main);


printf("location of heap : %p\n", (void *) malloc(1));
int x = 3;
printf("location of stack : %p\n", (void *) &x);

return x;
}

A simple program that prints out addresses


Virtual Address(Cont.)

Address Space
 The output in 64-bit Linux machine Code
(Tex t)

Code
D a ta
(Text)

Data
H eap

(fre e )
Heap

heap

(free)

stack

S ta c k

Stack
Example: pmap and cat /proc/<pid>/maps

cat /proc/<pid_number>/maps pmap -x <pid_number>


Part II: Address Translation
Memory Virtualizing with Efficiency and Control

 Memory virtualizing takes a similar strategy known as limited direct


execution(LDE) for efficiency and control.
 In memory virtualizing, efficiency and control are attained by hardware support.
 e.g., registers, TLB(Translation Look-aside Buffer)s, page-table
Address Translation

 Hardware transforms a virtual address to a physical address.


 The desired information is actually stored in a physical address.

 The OS must get involved at key points to set up the hardware.


 The OS must manage memory to judiciously intervene.
Relocation Address Space

 The address space start at address 0.


 The OS wants to place the process somewhere else in physical memory, not at
address 0.
A Single Relocated Process

0KB P ro g ra m C o d e

0KB
O p e r a t in g S y s t e m

Program Code Operating System

16KB
H eap

( n o t in u s e )

Heap
(not in use)
(fre e )

32KB

Relocated Process
Code
Code

Heap
H eap

(a llo ca ted
b u t n o t in u se )

heap
(allocated
(free) but not in use)

Stack
Sta ck

stack 48KB ( n o t in u s e )

S ta c k

(not in use)

Stack
64KB
16KB Physical Memory
Address Space
Base and Bounds Register

0KB P ro g ra m C o d e

0KB
O p e r a t in g S y s t e m

Program Code Operating System

16KB
H eap

( n o t in u s e )

Heap
(not in use) base register
(fre e )

32K B

32KB Code

Code
32KB
Heap
H eap

(a llo ca ted
b u t n o t in u se )

heap
(allocated
(free) but not in use)

Stack
Sta ck

stack 48KB ( n o t in u s e )

S ta c k

(not in use)

bounds register Stack


16KB

64KB
16KB Physical Memory
16KB Address Space
𝑝h𝑦𝑐𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠=𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠+𝑏𝑎𝑠𝑒

0≤𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠<𝑏𝑜𝑢𝑛𝑑𝑠

Dynamic(Hardware base) Relocation

 When a program starts running, the OS decides where in physical memory a


process should be loaded.
 Set the base register a value.

𝑝h𝑦𝑐𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠=𝑣𝑖𝑟𝑡𝑢𝑎𝑙𝑎𝑑𝑑𝑟𝑒𝑠𝑠+𝑏𝑎𝑠𝑒
 Every virtual address must not be greater than bound and negative.

0≤𝑣𝑖𝑟𝑡𝑢𝑎𝑙 𝑎𝑑𝑑𝑟𝑒𝑠𝑠<𝑏𝑜𝑢𝑛𝑑𝑠
Two ways of Bounds Register

0KB P ro g ra m C o d e

0KB
O p e r a t in g S y s t e m

Program Code Operating System

16KB
H eap

( n o t in u s e )

Heap
(not in use)
(fre e )

32KB Code

Code
Heap
H eap

(a llo c a te d
b u t n o t in u s e )

(allocated
(free) but not in use)
bounds bounds
Stack
Sta ck

16KB 48KB

16KB 48KB ( n o t in u s e )

48KB

S ta c k

(not in use)

Stack
64KB
Physical Memory
16KB Address Space
Base Register

Physical
Memory

0 KB

1 KB
P1
2 KB

3 KB same code
4 KB
P2
5 KB

6 KB
Base Register

0 KB

1 KB base register
P1
2 KB

3 KB
P1 is running

4 KB
P2
5 KB

6 KB
Base Register

0 KB

1 KB
P1
2 KB

3 KB
P2 is running

4 KB base register
P2
5 KB

6 KB
Base Register
(Decimal notation)
0 KB Virtual Physical
P1: load 100, R1
1 KB
P1
2 KB

3 KB

4 KB
P2
5 KB

6 KB
Base Register

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1 (1024 + 100)
1 KB
P1
2 KB

3 KB

4 KB
P2
5 KB

6 KB
Base Register

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1
2 KB

3 KB

4 KB
P2
5 KB

6 KB
Base Register

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1 (4096 + 100)
2 KB

3 KB

4 KB
P2
5 KB

6 KB
Base Register

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1

3 KB

4 KB
P2
5 KB

6 KB
Base Register

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1

3 KB

4 KB
P2
5 KB

6 KB
Base Register

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1
3 KB

4 KB
P2
5 KB

6 KB
Base Register

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB

4 KB
P2
5 KB

6 KB
Base Register

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB

4 KB
P2
5 KB Can P2 hurt P1?
Can P1 hurt P2?
6 KB

How to protect process with base register?


Base Register

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB
P1: store 3072, R1 store 4096, R1 (3072 + 1024)
4 KB
P2
5 KB Can P2 hurt P1?
Can P1 hurt P2?
6 KB

How to protect process with base register?


Base + Bounds

0 KB

1 KB base register
P1
2 KB bounds register
3 KB
P1 is running

4 KB
P2
5 KB

6 KB
Base + Bounds

0 KB

1 KB
P1
2 KB

3 KB
P2 is running

4 KB base register
P2
5 KB bounds register
6 KB
Base + Bounds

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB
P1: store 3072, R1
4 KB
P2
5 KB
Can P1 hurt P2?
6 KB
Base + Bounds

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB
P1: store 3072, R1 Exception 3072 > 1024
4 KB
P2
5 KB
Can P1 hurt P2?
6 KB
Base + Bounds

0 KB Virtual Physical
P1: load 100, R1 load 1124, R1
1 KB
P1 P2: load 100, R1 load 4196, R1
2 KB P2: load 1000, R1 load 5096, R1
P1: load 1000, R1 load 2024, R1
3 KB
P1: store 3072, R1 Exception 3072 > 1024
4 KB
P2
5 KB
Can P1 hurt P2?
6 KB
OS Issues for Memory Virtualizing

 The OS must take action to implement base-and-bounds approach.


 Three critical junctures:
 When a process starts running:
 Finding space for address space in physical memory

 When a process is terminated:


 Reclaiming the memory for use

 When context switch occurs:


 Saving and storing the base-and-bounds pair
OS Issues: When a Process Starts Running

 The OS must find a room for a new address space.


 free list : A list of the range of the physical memory which are not in use.

0KB
O p e r a t in g S y s t e m

Operating System
The OS lookup the free list
16KB ( n o t in u s e )

Free list
(not in use)
16KB

16KB 32KB Code

Code
Heap
H eap

(allocated but not in use)

(allocated but not in use)


48KB

Stack
Sta ck

48KB 48KB ( n o t in u s e )

(not in use)

64KB
Physical Memory
OS Issues: When a Process Is Terminated

 The OS must put the memory back on the free list.

Free list 0KB


O p e r a t in g S y s t e m O p e r a t in g S y s t e m

Free list 0KB


Operating System Operating System
16KB

16KB
16KB

16KB
( n o t in u s e ) ( n o t in u s e )

16KB 16KB
(not in use) (not in use)

32KB
P ro c e s s A

32KB
32KB ( n o t in u s e )

48KB

Process A 32KB (not in use)


48KB
48KB
( n o t in u s e )

48KB ( n o t in u s e )

(not in use) 48KB

(not in use)
64KB 48KB 64KB
Physical Memory Physical Memory
OS Issues: When Context Switch Occurs

 The OS must save and restore the base-and-bounds pair.


 In process structure or process control block(PCB)
Process A PCB

base : 32KB
bounds :
O p e r a t in g S y s t e m O p e r a t in g S y s t e m

0KB 0KB
Context Switching 48KB …
Operating System Operating System

16KB 16KB
( n o t in u s e ) ( n o t in u s e )

(not in use) base (not in use) base


32KB

48KB

32KB
P ro c e s s A
C u r r e n t ly R u n n in g

32KB 32KB
P ro c e s s A

48KB
Process A
bounds Process A bounds
Currently Running 48KB

64KB

48KB
P ro c e s s B

48KB 48KB
P ro c e s s B
C u r r e n t ly R u n n in g

64KB
Process B
Process B
Currently Running
64KB 64KB

Physical Memory Physical Memory


Summary

 Address Space – an abstraction of physical memory created by OS


 The address space contains all about a running process.

 That is consist of program code, data, heap, and stack


 Address Translation – Hardware-based Address Translation
 The hardware transforms each memory access (e.g., an instruction fetch, load, or store),
changing the virtual address provided by the instruction to a physical address where the desired
information is actually located .
 OS performs memory management – keeping track of which locations are free and which are in
use, and judiciously intervening to maintain control over how memory is used.
 The Base and Bounds Approach
 When a program starts running, the OS decides where in physical memory a process should be loaded
 The hardware checks whether virtual addresses are in bounds.

 Next: Memory Management (Chapters 16, 17, 18, 19, 20, 21, 22, 23)

You might also like