Why privilege matters for virtualization
Video 034 of 140 in Virtualization Foundations, in the unit “Privilege, system calls and interrupts”. 4:36, with English and Hindi audio.
A virtual machine runs its own operating system. That operating system expects to run in kernel mode, the privileged mode of the core, and to control the real hardware. But the real control must belong to someone else. The answer decides how virtualization works. This article explains the problem, how a hypervisor takes control safely through trap and emulate, how hardware support made this easier, and how to check a Linux host for that support.
What you will learn
- A guest kernel expects full control of the hardware, but the hypervisor must keep the real control.
- Risky instructions trap to the hypervisor, which does the work safely and returns control to the guest.
- Hardware support, Intel VT-x and AMD-V, makes this faster and simpler.
- Each trap still costs time, and too many traps show up as steal time in a guest.
The problem: a guest that believes it owns the machine
Consider the guest, the operating system inside a virtual machine. It believes it has full control of the machine. If its privileged instructions ran directly, they would change the real hardware, and that would hit every other virtual machine on the server.
So the hypervisor must hold the real control. It is the program that creates and runs the virtual machines, and it shares the hardware among them.
Trap and emulate: how the hypervisor takes control safely
- The guest runs. Most of its instructions run straight on the processor, at full speed.
- A sensitive instruction appears. It touches real hardware or the processor's controls. The processor traps it.
- Control passes to the hypervisor, because the processor does not let the guest reach the real hardware on its own.
- The hypervisor works out what the guest wanted, and does it in a safe way.
- The hypervisor returns control, and the guest carries on, seeing the result it expected.
This is called trap and emulate. The guest usually does not know it was stopped, and the other virtual machines were never touched.
How the problem was solved over time
The x86 chips of the time had a problem. A study from the year 2000 found seventeen sensitive instructions on the Pentium that did not trap, so a hypervisor could not see them. In 1999, VMware Workstation had already solved the problem in software, by translating risky code as the guest ran.
In 2005, Intel added hardware support, called VT-x, with a new mode for the hypervisor. In 2006, AMD brought its own version, called AMD-V. With that support, both main x86 makers offered hardware help for virtual machines.
How to check a Linux host for hardware support
On Ubuntu, lscpu names the virtualization support of the chip. Filter its output for the word virtualization. Intel machines show VT-x, and AMD machines show AMD-V.
lscpu | grep -i virtualizationThe other check counts the processor threads that report the flag. The flag is vmx on Intel and svm on AMD. Hardware support is what lets a guest run with less help from software.
grep -c -E 'vmx|svm' /proc/cpuinfoWhat trapping costs, and what goes wrong
- Traps take time: each trap to the hypervisor costs processor time, so a guest that makes many risky calls can run noticeably slower.
- A shared fault: the hypervisor is shared by every guest on the host, so a bug there can reach all of them.
- How you notice it: slow guests, with steal time rising. Steal time is the processor time the hypervisor took away from a guest.
Check hardware support on your host
You need a Linux terminal on the Ubuntu 24.04 host you want to check. Run the two commands. They should agree: one names the support, the other counts the flag.
Show the virtualization support that lscpu names for the chip. Intel shows VT-x, and AMD shows AMD-V.
lscpu | grep -i virtualizationWhat you should see: A Virtualization line with VT-x on an Intel machine, or AMD-V on an AMD machine.
Count the threads that report the virtualization flag in the processor information file.
grep -c -E 'vmx|svm' /proc/cpuinfoWhat you should see: A count above zero means the threads report the flag: vmx on Intel, svm on AMD. Zero means the support is missing or switched off in the firmware.
Terms
- Hypervisor
- The program that creates and runs virtual machines, and that holds the real control of the hardware.
- Guest
- The operating system that runs inside a virtual machine.
- Trap and emulate
- A way to virtualize in which risky instructions trap to the hypervisor, which does the work safely for the guest.
- Steal time
- The share of processor time that the hypervisor took away from a guest.
Check yourself
Why must the hypervisor keep the real control of the hardware?
A guest kernel running its privileged instructions directly would change the real hardware and affect every other virtual machine on the server.
What does trap and emulate mean?
A risky instruction traps to the hypervisor, which works out what the guest wanted and does it safely, then returns control to the guest.
What does steal time show?
The processor time the hypervisor took away from a guest. Rising steal time can mean the guest is making many traps.
Previous: Context switches and what they cost. Next: What RAM is, and how it is fitted.