PV Traps and Entrypoints

Note

The details here are specific to 64bit builds of Xen. Details for 32bit builds of Xen are different and not discussed further.

PV guests are subject to Xen's linkage setup for events (interrupts, exceptions and system calls). x86's IDT architecture and limitations are the majority influence on the PV ABI.

All external interrupts are routed to PV guests via the Event Channel interface, and not discussed further here.

What remain are exceptions, and the instructions which cause control transfers. In the x86 architecture, the instructions relevant for PV guests are:

The BOUND instruction is not included. It is a hardware exception and strictly a fault, with no trapping configuraton.

1 Xen's configuration

Xen maintains a complete IDT, with most gates configured with DPL0. This causes most INT $N instructions to #GP. This allows Xen to emulate the instruction, referring to the guest kernels vDPL choice.

A range of the GDT is guest-owned, allowing for call gates. During audit, Xen forces all call gates to DPL0, causing their use to #GP allowing for emulation.

Xen enables SYSCALL in all cases as it is mandatory in 64bit mode, and enables SYSENTER when available in 64bit mode.

When Xen is using FRED delivery the hardware configuration is substantially different, but the behaviour for guests remains as unchanged as possible.

2 PV Guest's configuration

The PV ABI contains the "trap table", modelled closely on the IDT. It is manipulated by HYPERCALL_set_trap_table, has 256 entries, each containing a code segment selector, an address, and flags. A guest is expected to configure handlers for all exceptions; failure to do so is terminal and similar to a Triple Fault.

Part of the GDT is guest owned with descriptors audited by Xen. This range can be manipulated with HYPERVISOR_set_gdt and HYPERVISOR_update_descriptor.

Other entrypoints are configured via HYPERVISOR_callback_op. Of note here are the callback types syscall, syscall32 (relevant for AMD parts) and sysenter (relevant for Intel parts).

Warning

Prior to Xen 4.15, there was no check that the syscall or syscall32 callbacks had been registered before attempting to deliver via them. Guests are strongly advised to ensure the entrypoints are registered before running userspace.

3 Notes

INT3 vs INT $3 and INTO vs INT $4 are hard to distinguish architecturally as both forms have a DPL check and use the same IDT vectors. Because Xen configures both as DPL3, the INT $ forms do not fault for emulation, and are treated as if they were exceptions. This means the guest can't block these instruction by trying to configure them with vDPL0.

The instructions which trap into Xen (INT $0x80, SYSCALL, SYSENTER) but can be disabled by guest configuration need turning back into faults for the guest kernel to process.

While both PV32 and PV64 guests are permitted to write Call Gates into the GDT, emulation is only wired up for PV32. At the time of writing, the x86 maintainers feel no specific need to fix this omission.