Techniques for Efficient Virtualization


Scenario: a type 1 hypervisor is running a guest OS that thinks it is the kernel, but really is in user mode. The guest runs in virtual kernel mode. Its apps think they are in user mode (and really are).

What happens when the guest executes a privileged instruction? Normally, this would cause the program to crash. But with VT, the instruction traps to the hypervisor, and the hypervisor determines if the guest OS (or at least its kernel) called it -- OK! -- or a guest application did -- not OK!

Virtualizing the Unvirtualizable

So how was this handled before VT was available?

Using techniques like binary translation and protection rings.

Binary translator rewriting the guest OS, which runs in ring 1, while the hypervisor runs in ring 0. The user programs remain in ring 3.

Sensitive instructions are eliminated by re-writing the guest's kernel code, one basic block at a time. A basic block is a run of consecutive instructions, where the instruction pointer advances a single instruction at a time. Sensitive instructions are replaced with calls to hypervisor procedures.
Translated blocks are cached, so that they do not need to be translated again.

Type II hypervisors are little bit different, in that they rely upon the host's device drivers. They are like teenagers hosting a party while their parents are away: they can move everything, so long as it is all back in place when the parents return. This restoration is called a world switch.

The Cost of Virtualization

Do CPUs with VT outperform software-based approaches?
Not necessarily! VT generates lots of traps, and traps are expensive.
Sometimes software beats hardware: so sometimes hypervisors translate anyway, even if there is hardware support.

An example where translation is cheaper:
Turning off interrupts may be very expensive, if there are deep caches and out-of-order execution.
But a hypervisor need not really turn off interrupts: it only needs to make it look to the guest OS as if they are off. So it can just keep a binary flag per guest OS: much cheaper!

Quiz
  1. What is a basic block of assembler?
    1. the most fundamental instructions on any machine
    2. the boot block on the disk
    3. a short straight-line sequence of instructions
    4. a group of assembly language code re-written in BASIC
  2. The guest OS runs
    1. in virtual kernel mode
    2. in kernel mode
    3. as an ordinary application
    4. very very slowly
  3. Without VT one thing hypervisors did was to rely on
    1. automatic trapping of sensitive instructions
    2. the native x86 virtualization technology
    3. protection rings
    4. all of the above
  4. When virtualizing without VT support sensitive instructions in basic blocks are replaced by
    1. calls to the hypervisor
    2. interrupts
    3. page faults
    4. segementation errors.
  5. When a type 2 hypervisor needs to pass control to the host OS to handle a hardware interrupt it executes
    1. a trap
    2. a hypercall
    3. a prisoner
    4. a world switch.
  6. Virtualization relying on VT hardware
    1. always beats translation
    2. never beats translation
    3. sometimes beats translation
    4. no one knows.
Answers

1. c; 2. a; 3. c; 4. a; 5. d; 6. c;