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

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

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.

Show the virtualization support named by lscpu
lscpu | grep -i virtualization

The 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.

Count the threads that report the virtualization flag
grep -c -E 'vmx|svm' /proc/cpuinfo

What trapping costs, and what goes wrong

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.

  1. Show the virtualization support that lscpu names for the chip. Intel shows VT-x, and AMD shows AMD-V.

    lscpu | grep -i virtualization

    What you should see: A Virtualization line with VT-x on an Intel machine, or AMD-V on an AMD machine.

  2. Count the threads that report the virtualization flag in the processor information file.

    grep -c -E 'vmx|svm' /proc/cpuinfo

    What 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.