• Sean Christopherson's avatar
    KVM: nVMX: Intercept VMWRITEs to GUEST_{CS,SS}_AR_BYTES · b6437805
    Sean Christopherson authored
    VMMs frequently read the guest's CS and SS AR bytes to detect 64-bit
    mode and CPL respectively, but effectively never write said fields once
    the VM is initialized.  Intercepting VMWRITEs for the two fields saves
    ~55 cycles in copy_shadow_to_vmcs12().
    
    Because some Intel CPUs, e.g. Haswell, drop the reserved bits of the
    guest access rights fields on VMWRITE, exposing the fields to L1 for
    VMREAD but not VMWRITE leads to inconsistent behavior between L1 and L2.
    On hardware that drops the bits, L1 will see the stripped down value due
    to reading the value from hardware, while L2 will see the full original
    value as stored by KVM.  To avoid such an inconsistency, emulate the
    behavior on all CPUS, but only for intercepted VMWRITEs so as to avoid
    introducing pointless latency into copy_shadow_to_vmcs12(), e.g. if the
    emulation were added to vmcs12_write_any().
    
    Since the AR_BYTES emulation is done only for intercepted VMWRITE, if a
    future patch (re)exposed AR_BYTES for both VMWRITE and VMREAD, then KVM
    would end up with incosistent behavior on pre-Haswell hardware, e.g. KVM
    would drop the reserved bits on intercepted VMWRITE, but direct VMWRITE
    to the shadow VMCS would not drop the bits.  Add a WARN in the shadow
    field initialization to detect any attempt to expose an AR_BYTES field
    without updating vmcs12_write_any().
    
    Note, emulation of the AR_BYTES reserved bit behavior is based on a
    patch[1] from Jim Mattson that applied the emulation to all writes to
    vmcs12 so that live migration across different generations of hardware
    would not introduce divergent behavior.  But given that live migration
    of nested state has already been enabled, that ship has sailed (not to
    mention that no sane VMM will be affected by this behavior).
    
    [1] https://patchwork.kernel.org/patch/10483321/
    
    Cc: Jim Mattson <jmattson@google.com>
    Cc: Liran Alon <liran.alon@oracle.com>
    Signed-off-by: default avatarSean Christopherson <sean.j.christopherson@intel.com>
    Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
    b6437805
nested.c 176 KB