Levture 4
Levture 4
Definition of Virtualization
Guest Applications
Java Program
Guest Operating System
System VM Process VM
Non- Privilege
Privilege
Privilege and Sensitive
ps: whole circle is a
set of all instructions
Non- Privilege
Privilege
Sensitive
Non-
Privilege Privilege
Sensitive
Non-
Privilege Privilege
Critical Sensitive
SENSITIVE INSTRUCTION
CPU Architecture
• What is trap ?
When CPU is running in user mode, some internal or external
events, which need to be handled in kernel mode, take place.
Then CPU will jump to hardware exception handler vector, and
execute system operations in kernel mode.
• Trap types :
System Call
• Invoked by application in user mode.
• For example, application ask OS for system IO.
Hardware Interrupts
• Invoked by some hardware events in any mode.
• For example, hardware clock timer trigger event.
Exception
• Invoked when unexpected error or system malfunction occur.
• For example, execute privilege instructions in user mode.
Trap and Emulate Model
• If we want CPU virtualization to be efficient, how should we
implement the VMM ?
We should make guest binaries run on CPU as fast as possible.
Theoretically speaking, if we can run all guest binaries natively,
there will NO overhead at all.
But we cannot let guest OS handle everything, VMM should be able
to control all hardware resources.
• Solution :
Ring Compression
• Shift traditional OS from kernel mode(Ring 0) to user mode(Ring 1), and
run VMM in kernel mode.
• Then VMM will be able to intercept all trapping event.
Trap and Emulate Model
• VMM virtualization paradigm (trap and emulate) :
1. Let normal instructions of guest OS run directly on processor in
user mode.
2. When executing privilege instructions, hardware will make
processor trap into the VMM.
3. The VMM emulates the effect of the privilege instructions for the
guest OS and return to guest.
Trap and Emulate Model
• Traditional OS :
When application invoke a
system call :
• CPU will trap to interrupt
handler vector in OS.
• CPU will switch to kernel
mode (Ring 0) and execute
OS instructions.
When hardware event :
• Hardware will interrupt CPU
execution, and jump to
interrupt handler in OS.
Trap and Emulate Model
• VMM and Guest OS :
System Call
• CPU will trap to interrupt
handler vector of VMM.
• VMM jump back into guest OS.
Hardware Interrupt
• Hardware make CPU trap to
interrupt handler of VMM.
• VMM jump to corresponding
interrupt handler of guest OS.
Privilege Instruction
• Running privilege instructions
in guest OS will be trapped to
VMM for instruction emulation.
• After emulation, VMM jump
back to guest OS.
Context Switch
• Steps of VMM switch different virtual machines :
1. Timer Interrupt occurs in running VM.
2. Context switch to VMM.
3. VMM saves state of running VM.
4. VMM determines next VM to execute.
5. VMM sets timer interrupt.
6. VMM restores state of next VM.
7. VMM sets PC to timer interrupt handler of next VM.
8. Next VM active.
System State Management
• Virtualizing system state :
VMM will hold the system states
of all virtual machines in memory.
When VMM context switch from
one virtual machine to another
• Write the register values back to memory
• Copy the register values of next guest OS
to CPU registers.
Virtualization Theorem
• Subset theorem :
For any conventional third-generation computer, a VMM may be
constructed if the set of sensitive instructions for that computer
is a subset of the set of privileged instructions.
• Recursive Emulation :
A conventional third-generation computer is recursively
virtualizable if
• It is virtualizable
• VMM without any timing dependencies can be constructed for it.
Binary translation
• Use emulation technique to make hardware virtualizable.
• Skip the critical instructions by means of these translations.
Hardware assistance
• Modify or enhance ISA of hardware to provide virtualizable architecture.
• Reduce the complexity of VMM implementation.
Para-Virtualization:
Patch Guest OS
Para-Virtualization
• Para-Virtualization implementation :
In para-virtualization technique, guest OS should be modified to
prevent invoking critical instructions.
Instead of knowing nothing about hypervisor, guest OS will be aware
of the existence of VMM, and collaborate with VMM smoothly.
VMM will provide the hyper-call interfaces, which will be the
communication channel between guest and host.
Some Difficulties
• Difficulty of para-virtualization :
Guest OS modification
• User should at least has the source code of guest OS; otherwise, para-
virtualization cannot be used.
Full-Virtualization:
Dynamic Binary Translation
Binary Translation
• In emulation techniques :
Binary translation module is used to optimize binary code
blocks, and translates binaries from guest ISA to host ISA.
• In virtualization techniques :
Binary translation module is used to skip or modify the guest
OS binary code blocks which include critical instructions.
Translate those critical instructions into some privilege
instructions which will be trapped to VMM for further
emulation.
Binary Translation (revisited)
• Static approach vs. Dynamic approach :
Static binary translation
• The entire executable file is translated into an executable of the target
architecture.
• This is very difficult to do correctly, since not all the code can be
discovered by the translator.
Dynamic binary translation
• Looks at a short sequence of code, typically on the order of a single basic
block, translates it and caches the resulting sequence.
• Code is only translated as it is discovered and when possible, branch
instructions are made to point to already translated and saved code.
Dynamic Binary Translation (revisited)
• Solution :
We need to redefine the semantic of some instructions.
We need to introduce new CPU control paradigm.
Intel VT-x
• In order to straighten those problems out, Intel introduces
one more operation mode of x86 architecture.
VMX Root Operation (Root Mode)
• All instructions in this mode are no different to traditional ones.
• All legacy software can run in this mode correctly.
• VMM should run in this mode and control all system resources.
VMX Non-Root Operation (Non-Root Mode)
• All sensitive instructions in this mode are redefined.
• The sensitive instructions will trap to Root Mode.
• Guest OS should run in this mode and be fully virtualized through typical
“trap and emulation model”.
Intel VT-x
• VMM with VT-x :
System Call
• CPU will directly trap to
interrupt handler vector of
guest OS.
Hardware Interrupt
• Still, hardware events
need to be handled by
VMM first.
Sensitive Instruction
• Instead of trap all privilege
instructions, running guest
OS in Non-root mode will
trap sensitive instruction
only.
Context Switch
• VMM switches different virtual machines with Intel VT-x :
VMXON/VMXOFF
• These two instructions are used to turn on/off CPU Root Mode.
VM Entry
• This is usually caused by the execution of VMLAUNCH/VMRESUME
instructions, which will switch CPU mode from Root Mode to Non-Root
Mode.
VM Exit
• This may be caused by many reasons, such as hardware interrupts or
sensitive instruction executions.
• Switch CPU mode from Non-Root Mode to Root Mode.
System State Management
• Intel introduces a more efficient hardware approach for
register switching, VMCS (Virtual Machine Control Structure) :
State Area
• Store Host OS system state when VM-Entry.
• Store Guest OS system state when VM-Exit.
Control Area
• Control instruction behaviors in Non-Root Mode.
• Control VM-Entry and VM-Exit process.
Exit Information
• Provide the VM-Exit reason and some hardware information.