KVM: x86: some fixes about msr access emulation
Ping
Thanks
Hou
Quoted 43 lines · Hou Wenlong
From: Hou Wenlong <[email protected]>
When
KVM_CAP_X86_USER_SPACE_MSRcap is enabled, userspace can control MSR accesses. In normal scenario, RDMSR/WRMSR can be interceped, but when kvm.force_emulation_prefix is enabled, RDMSR/WRMSR with kvm prefix would trigger an UD and cause instruction emulation. If MSR accesses is filtered, em_rdmsr()/em_wrmsr() returnsX86EMUL_IO_NEEDED, but it is ignored byx86_emulate_instruction(). Then guest continues execution, but RIP has been updated to point to RDMSR/WRMSR inhandle_ud(), so RDMSR/WRMSR can be interceped and guest exits to userspace finnaly by mistake. Such behaviour leads to two vm exits and wastes one instruction emulation.After let
x86_emulate_instruction()returns 0 for RDMSR/WRMSR emulation, if it needs to exit to userspace, itscomplete_userspace_iocallback would callkvm_skip_instruction()to skip instruction. But for vmx,VMX_EXIT_INSTRUCTION_LENin vmcs is invalid for UD, it can't be used to update RIP,kvm_emulate_instruction()should be used instead. As for svm, nRIP in vmcb is 0 for UD, sokvm_emulate_instruction()is used. But for nested svm, I'm not sure, sincesvm_check_intercept()would change nRIP.Changed from v1:
As Sean suggested, fix the problem within the emulator instead of routing to the vendor callback.
Add a new emulation type to handle completion of user exits. Attach a different callback for msr access emulation in the emulator.Hou Wenlong (3):
KVM: x86: Add an emulation type to handle completion of user exits
KVM: x86: Use different callback if msr access comes from the emulator
KVM: x86: Exit to userspace if RDMSR/WRMSR emulation returns
X86EMUL_IO_NEEDEDSean Christopherson (1):
KVM: x86: Handle 32-bit wrap of EIP forEMULTYPE_SKIPwith flat code
segarch/x86/include/asm/kvm_host.h | 8 ++- arch/x86/kvm/x86.c | 108 ++++++++++++++++++++------------ 2 files changed, 76 insertions(+), 40 deletions(-)