Skip to contentSkip to conversation

KVM: x86: Fix goofs with MONITOR/MWAIT quirk

PATCH New Sean Christopherson 1 participant 4 messages Jul 11, 2022

Subsystems (5)
Documentation x86 KVM Kselftest +1 more subsystems
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: x86: Fix goofs with MONITOR/MWAIT quirk

Fix two bugs in the MONITOR/MWAIT selftest, and tweak the name of the quirk (which isn't yet set in stone) to clarify that it only applies to the #UD behavior, i.e. to reserve the right to correctly emulate #GP and other faults in the future even if the quirk is enabled.

Sean Christopherson (3):
KVM: selftests: Test MONITOR and MWAIT, not just MONITOR for quirk
KVM: selftests: Provide valid inputs for MONITOR/MWAIT regs
KVM: x86: Tweak name of MONITOR/MWAIT #UD quirk to make it #UD
specific

 Documentation/virt/kvm/api.rst                         |  2 +-
 arch/x86/include/asm/kvm_host.h                        |  2 +-
 arch/x86/include/uapi/asm/kvm.h                        |  2 +-
 arch/x86/kvm/x86.c                                     |  2 +-
 .../testing/selftests/kvm/x86_64/monitor_mwait_test.c  | 10 +++++++---
 5 files changed, 11 insertions(+), 7 deletions(-)

base-commit: b9b71f43683ae9d76b0989249607bbe8c9eb6c5c

Signature · 1 line
2.37.0.144.g8ac04bfd2-goog
author maintainer
#1 lore raw

PATCH 1/3 KVM: selftests: Test MONITOR and MWAIT, not just MONITOR for quirk

Fix a copy+paste error in monitor_mwait_test by switching one of the two "monitor" instructions to an "mwait". The intent of the test is very much to verify the quirk handles both MONITOR and MWAIT.

Fixes 2325d4dd7321 ("KVM: selftests: Add MONITOR/MWAIT quirk test")
---
 tools/testing/selftests/kvm/x86_64/monitor_mwait_test.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/kvm/x86_64/monitor_mwait_test.c +1 -1
index 49f2ed1c53fe..f5c09cb528ae 100644
@@ -34,7 +34,7 @@ static void guest_monitor_wait(int testcase)
 	else
 		GUEST_ASSERT_2(!vector, testcase, vector);
 
-	vector = kvm_asm_safe("monitor");
+	vector = kvm_asm_safe("mwait");
 	if (fault_wanted)
 		GUEST_ASSERT_2(vector == UD_VECTOR, testcase, vector);
 	else
Signature · 1 line
2.37.0.144.g8ac04bfd2-goog
author maintainer
#2 lore raw

PATCH 2/3 KVM: selftests: Provide valid inputs for MONITOR/MWAIT regs

In reply to Sean Christopherson

Provide valid inputs for RAX, RCX, and RDX when testing whether or not KVM injects a #UD on MONITOR/MWAIT. SVM has a virtualization hole and checks for _all_ faults before checking for intercepts, e.g. MONITOR with an unsupported RCX will #GP before KVM gets a chance to intercept and emulate.

Fixes 2325d4dd7321 ("KVM: selftests: Add MONITOR/MWAIT quirk test")
---
 tools/testing/selftests/kvm/x86_64/monitor_mwait_test.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
tools/testing/selftests/kvm/x86_64/monitor_mwait_test.c +6 -2
index f5c09cb528ae..6a4ebcdfa374 100644
@@ -28,13 +28,17 @@ static void guest_monitor_wait(int testcase)
 
 	GUEST_SYNC(testcase);
 
-	vector = kvm_asm_safe("monitor");
+	/*
+	 * Arbitrarily MONITOR this function, SVM performs fault checks before
+	 * intercept checks, so the inputs for MONITOR and MWAIT must be valid.
+	 */
+	vector = kvm_asm_safe("monitor", "a"(guest_monitor_wait), "c"(0), "d"(0));
 	if (fault_wanted)
 		GUEST_ASSERT_2(vector == UD_VECTOR, testcase, vector);
 	else
 		GUEST_ASSERT_2(!vector, testcase, vector);
 
-	vector = kvm_asm_safe("mwait");
+	vector = kvm_asm_safe("mwait", "a"(guest_monitor_wait), "c"(0), "d"(0));
 	if (fault_wanted)
 		GUEST_ASSERT_2(vector == UD_VECTOR, testcase, vector);
 	else
Signature · 1 line
2.37.0.144.g8ac04bfd2-goog
author maintainer
#3 lore raw

PATCH 3/3 KVM: x86: Tweak name of MONITOR/MWAIT #UD quirk to make it #UD specific

In reply to Sean Christopherson

Add a "UD" clause to KVM_X86_QUIRK_MWAIT_NEVER_FAULTS to make it clear that the quirk only controls the #UD behavior of MONITOR/MWAIT. KVM doesn't currently enforce fault checks when MONITOR/MWAIT are supported, but that could change in the future. SVM also has a virtualization hole in that it checks all faults before intercepts, and so "never faults" is already a lie when running on SVM.

Fixes bfbcc81bb82c ("KVM: x86: Add a quirk for KVM's "MONITOR/MWAIT are NOPs!" behavior")
---
 Documentation/virt/kvm/api.rst                          | 2 +-
 arch/x86/include/asm/kvm_host.h                         | 2 +-
 arch/x86/include/uapi/asm/kvm.h                         | 2 +-
 arch/x86/kvm/x86.c                                      | 2 +-
 tools/testing/selftests/kvm/x86_64/monitor_mwait_test.c | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)
Documentation/virt/kvm/api.rst +1 -1
index bafaeedd455c..cd9361f22530 100644
@@ -7523,7 +7523,7 @@ The valid bits in cap.args[0] are:
                                     incorrect hypercall instruction will
                                     generate a #UD within the guest.
 
-KVM_X86_QUIRK_MWAIT_NEVER_FAULTS    By default, KVM emulates MONITOR/MWAIT (if
+KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS By default, KVM emulates MONITOR/MWAIT (if
                                     they are intercepted) as NOPs regardless of
                                     whether or not MONITOR/MWAIT are supported
                                     according to guest CPUID.  When this quirk
arch/x86/include/asm/kvm_host.h +1 -1
index de5a149d0971..b9e85049743f 100644
@@ -2097,6 +2097,6 @@ int memslot_rmap_alloc(struct kvm_memory_slot *slot, unsigned long npages);
 	 KVM_X86_QUIRK_OUT_7E_INC_RIP |		\
 	 KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT |	\
 	 KVM_X86_QUIRK_FIX_HYPERCALL_INSN |	\
-	 KVM_X86_QUIRK_MWAIT_NEVER_FAULTS)
+	 KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS)
 
 #endif /* _ASM_X86_KVM_HOST_H */
arch/x86/include/uapi/asm/kvm.h +1 -1
index ee3896416c68..a0c0ab0c898e 100644
@@ -439,7 +439,7 @@ struct kvm_sync_regs {
 #define KVM_X86_QUIRK_OUT_7E_INC_RIP		(1 << 3)
 #define KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT	(1 << 4)
 #define KVM_X86_QUIRK_FIX_HYPERCALL_INSN	(1 << 5)
-#define KVM_X86_QUIRK_MWAIT_NEVER_FAULTS	(1 << 6)
+#define KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS	(1 << 6)
 
 #define KVM_STATE_NESTED_FORMAT_VMX	0
 #define KVM_STATE_NESTED_FORMAT_SVM	1
arch/x86/kvm/x86.c +1 -1
index 567d13405445..8065998c5bb6 100644
@@ -2046,7 +2046,7 @@ EXPORT_SYMBOL_GPL(kvm_handle_invalid_op);
 
 static int kvm_emulate_monitor_mwait(struct kvm_vcpu *vcpu, const char *insn)
 {
-	if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MWAIT_NEVER_FAULTS) &&
+	if (!kvm_check_has_quirk(vcpu->kvm, KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS) &&
 	    !guest_cpuid_has(vcpu, X86_FEATURE_MWAIT))
 		return kvm_handle_invalid_op(vcpu);
 
tools/testing/selftests/kvm/x86_64/monitor_mwait_test.c +1 -1
index 6a4ebcdfa374..094c68d744c0 100644
@@ -113,7 +113,7 @@ int main(int argc, char *argv[])
 
 		disabled_quirks = 0;
 		if (testcase & MWAIT_QUIRK_DISABLED)
-			disabled_quirks |= KVM_X86_QUIRK_MWAIT_NEVER_FAULTS;
+			disabled_quirks |= KVM_X86_QUIRK_MWAIT_NEVER_UD_FAULTS;
 		if (testcase & MISC_ENABLES_QUIRK_DISABLED)
 			disabled_quirks |= KVM_X86_QUIRK_MISC_ENABLE_NO_MWAIT;
 		vm_enable_cap(vm, KVM_CAP_DISABLE_QUIRKS2, disabled_quirks);
Signature · 1 line
2.37.0.144.g8ac04bfd2-goog