Operating Systems CMPSC 473
CPU virtualization
January 21, 2021 – Lecture 2 Instructor: Bhuvan Urgaonkar
Administrative stuff
• Did you log in to the dept Linux machines?
• Did you get an invitation for the course’s Piazza page?
• Email etiquette (re)reminder: please always CC all TAs when emailing me
• My office hours will start from next Thursday; meeting with TAs today
What an OS does
Software that offers the following functionality
• Virtualization
– Hides hardware details and offers programs easier interfaces to work with
Examples of virtualization by OS
Resource
Virtualized resource
Virtualization technique/subsyste m
CPU
Process, thread
Time-sharing, context switching
Main memory
Address space
Virtual memory
Disk;
Network interface
File; Socket
File system; TCP/IP
What an OS does
Software that offers the following functionality
• Virtualization
– Hides hardware details and offers programs easier
interfaces to work with
• Resourceallocation
– Divides up resources among programs
– Decides between conflicting requests for efficient and fair resource use
What an OS does
Software that offers the following functionality
• Virtualization
– Hides hardware details and offers programs easier
interfaces to work with
• Resourceallocation
– Divides up resources among programs
– Decides between conflicting requests for efficient and fair resource use
• Controlprogram
– Controls execution of programs to prevent errors and improper use of the computer
What’s Special About the OS?
• There are other entities that carry out virtualization, resource allocation, and control
• Consider these examples:
– C standard library helps virtualize system calls into HLL functions
– malloc/freehelpmanagememory
– gdbcancontroltheexecutionofyourprogram
• In what way is the OS special then?
– Answerscomingupinthenextfewlectures!
• Today
– CPU virtualization
• Nexttopics:
– Memory virtualization
– Concurrency
– IO virtualization (focus on persistence)
Outline
Process: a fundamental OS abstraction
• The canonical virtual machine created by the OS for a program
• An executing program
• Revise:
– fork(),wait(),exec(),kill(),exit()
• Chapter5ofOS3
– Infoonprocessesrunningonyourcomputer
• Task manager, top, /proc, etc.
• Aprocessisabundleofresources – Address space
Process
– Oneormorethreadsofexecution
– A variety of bookkeeping information stored by the OS, e.g.,
• Open file descriptors (e.g., pipes, network sockets, on-disk files) • Process id (pid)
• Process state (running, blocked, etc.)
• A single “application” contains one or more processes
Recall: Process Address Space
• Thesetofvirtualmemory addresses that a process can access
– Alargearrayofbytesstartingat0 and going up to some large number (e.g., 4 GB)
– Differentpartsofvirtualmemory hold different parts of the program
0xffffffff
Virtual addresses
0x00000000
Recall: Process Address Space
• Thesetofvirtualmemory addresses that a process can access
– Alargearrayofbytesstartingat0 and going up to some large number (e.g., 4 GB)
– Differentpartsofvirtualmemory hold different parts of the program
Code + static data
0xffffffff
data code
push %ebp
mov %esp,%ebp sub $0x18,%esp
// At top of .c file int foo = 42;
0x00000000
Virtual addresses
Recall: Process Address Space
• Thesetofvirtualmemory addresses that a process can access
– Alargearrayofbytesstartingat0 and going up to some large number (e.g., 4 GB)
– Differentpartsofvirtualmemory hold different parts of the program
int main( ) { …..
ptr = malloc (….); }
0xffffffff
ptr
heap
data code
0x00000000
Virtual addresses
Recall: Process Address Space
• Thesetofvirtualmemory addresses that a process can access
– Alargearrayofbytesstartingat0 and going up to some large number (e.g., 4 GB)
– Differentpartsofvirtualmemory hold different parts of the program
int bar( ) {return 1;}
int foo() {return bar ();}
int main( ) { foo();
}
0xffffffff
stack
stack frame for main
stack frame for foo
stack frame for bar
ptr
heap
data code
0x00000000
Virtual addresses
0xffffffff
Recall: Process Address Space
• Some registers typically present on CPUs and used for storing
stack frame for main
stack frame for foo
stack frame for bar
stack
0x00000000
ptr
heap
data code
important address space information for the currently executing process
– Program counter (PC): has the address of the next instruction to execute
• e.g., %eip on x86
– Stackpointer(SP):addressofthe “top” of the stack
• e.g., %esp on x86
Virtual addresses
0xffffffff
Recall: Process Address Space
•
• •
stack frame for main
stack frame for foo
stack frame for bar
stack
Eachfunctioninvocation results in one frame being created
Eachfunctionreturncauses the top frame to be removed
Whatdoesthestackframe contain?
– Argumentsgiventothefunction
– Function’s local variables
– Return address
0x00000000
ptr
heap
data code
Virtual addresses
Quiz 2.1
• Write a C function that will, when invoked, will eventually cause a stack overflow.
• Describe how the stack overflow might lead to corruption of heap objects.
• What remedies come to mind?
Quiz 2.2
• Show the composition of the address space of this program at the indicated points A, B, and C. Also describe the contents of the PC and SP registers at these points.
int foo (int *); int bar (int); int *a;
int main () {
int i, j; int *c;
a = (int *) malloc (sizeof (int));
c = (int *) malloc (sizeof (int)); // A i = foo (a);
j = foo (c);
}
int foo (int *x) { // B return bar (*x);
}
int bar (int x) { // C return x;
}
Address space layout randomization