Skip to contentSkip to conversation

KVM: SVM: Always intercept ICEBP, add INT1 selftests

PATCH In review David Woodhouse 5 participants 10 messages May 7 – Jul 31

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: SVM: Always intercept ICEBP, add INT1 selftests

From: David Woodhouse <[email protected]>

ICEBP (INT1, opcode 0xF1) generates a #DB that is architecturally a trap, but on SVM it was not always intercepted. Unconditionally intercept ICEBP on SVM to match VMX behaviour and ensure correct event delivery semantics.

Add two selftests exercising ICEBP:

- int1_ept_test: verifies that ICEBP works correctly when the exception stack page is not present (EPT/NPT fault during #DB delivery). The IST stack is evicted via MADV_DONTNEED before executing INT1.

- int1_task_gate_test: verifies ICEBP delivery through a 32-bit task gate, exercising the legacy task-switch path for #DB.

Tested on Intel Sapphire Rapids and AMD Genoa. Without the SVM fix, int1_task_gate_test fails on AMD with EIP pointing at ICEBP instead of after it. With the fix, both tests pass on both platforms.

---
 arch/x86/kvm/svm/svm.c                        |  21 ++
 tools/testing/selftests/kvm/Makefile.kvm      |   2 +
 .../testing/selftests/kvm/x86/int1_ept_test.c | 116 +++++++
 .../selftests/kvm/x86/int1_task_gate_test.c   | 298 ++++++++++++++++++
 4 files changed, 437 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/int1_ept_test.c
 create mode 100644 tools/testing/selftests/kvm/x86/int1_task_gate_test.c
arch/x86/kvm/svm/svm.c +21 -0
index e7fdd7a9c280..a820d47a4bca 100644
@@ -1159,6 +1159,7 @@ static void init_vmcb(struct kvm_vcpu *vcpu, bool init_event)
 	svm_set_intercept(svm, INTERCEPT_SKINIT);
 	svm_set_intercept(svm, INTERCEPT_WBINVD);
 	svm_set_intercept(svm, INTERCEPT_XSETBV);
+	svm_set_intercept(svm, INTERCEPT_ICEBP);
 	svm_set_intercept(svm, INTERCEPT_RDPRU);
 	svm_set_intercept(svm, INTERCEPT_RSM);
 
@@ -2045,6 +2046,25 @@ static int bp_interception(struct kvm_vcpu *vcpu)
 	return 0;
 }
 
+static int icebp_interception(struct kvm_vcpu *vcpu)
+{
+	/*
+	 * ICEBP (INT1, opcode 0xF1) generates #DB as a trap. Intercept it
+	 * unconditionally so that RIP is advanced past the instruction
+	 * *before* #DB is injected. This is necessary because SVM reports
+	 * the wrong RIP for ICEBP-induced #DB when delivered via a task
+	 * gate: RIP points at the ICEBP instruction instead of after it.
+	 *
+	 * By intercepting ICEBP, NRIP (next_rip) gives us the correct
+	 * post-instruction RIP. Advancing RIP first and then injecting #DB
+	 * ensures consistent behaviour regardless of how the guest's IDT
+	 * delivers the exception.
+	 */
+	svm_skip_emulated_instruction(vcpu);
+	kvm_queue_exception(vcpu, DB_VECTOR);
+	return 1;
+}
+
 static int ud_interception(struct kvm_vcpu *vcpu)
 {
 	return handle_ud(vcpu);
@@ -3333,6 +3353,7 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[SVM_EXIT_MONITOR]			= kvm_emulate_monitor,
 	[SVM_EXIT_MWAIT]			= kvm_emulate_mwait,
 	[SVM_EXIT_XSETBV]			= kvm_emulate_xsetbv,
+	[SVM_EXIT_ICEBP]			= icebp_interception,
 	[SVM_EXIT_RDPRU]			= kvm_handle_invalid_op,
 	[SVM_EXIT_EFER_WRITE_TRAP]		= efer_trap,
 	[SVM_EXIT_CR0_WRITE_TRAP]		= cr_trap,
tools/testing/selftests/kvm/Makefile.kvm +2 -0
index 9118a5a51b89..1b89bf0e5cf1 100644
@@ -86,6 +86,8 @@ TEST_GEN_PROGS_x86 += x86/hyperv_features
 TEST_GEN_PROGS_x86 += x86/hyperv_ipi
 TEST_GEN_PROGS_x86 += x86/hyperv_svm_test
 TEST_GEN_PROGS_x86 += x86/hyperv_tlb_flush
+TEST_GEN_PROGS_x86 += x86/int1_ept_test
+TEST_GEN_PROGS_x86 += x86/int1_task_gate_test
 TEST_GEN_PROGS_x86 += x86/kvm_clock_test
 TEST_GEN_PROGS_x86 += x86/kvm_pv_test
 TEST_GEN_PROGS_x86 += x86/kvm_buslock_test
tools/testing/selftests/kvm/x86/int1_ept_test.c new file +116 -0

116 lines not on this page · raw message

tools/testing/selftests/kvm/x86/int1_task_gate_test.c new file +298 -0

298 lines not on this page · raw message

Signature · 1 line
2.43.0
#1 lore raw
Quoted 14 lines · David Woodhouse

From: David Woodhouse <[email protected]>

ICEBP (INT1, opcode 0xF1) generates a #DB that is architecturally a trap, but on SVM it was not always intercepted. Unconditionally intercept ICEBP on SVM to match VMX behaviour and ensure correct event delivery semantics.

Add two selftests exercising ICEBP:

- int1_ept_test: verifies that ICEBP works correctly when the exception stack page is not present (EPT/NPT fault during #DB delivery). The IST stack is evicted via MADV_DONTNEED before executing INT1.

Quoted 6 lines

- int1_task_gate_test: verifies ICEBP delivery through a 32-bit task gate, exercising the legacy task-switch path for #DB.

Tested on Intel Sapphire Rapids and AMD Genoa. Without the SVM fix, int1_task_gate_test fails on AMD with EIP pointing at ICEBP instead of after it. With the fix, both tests pass on both platforms.

Hmm, but KVM unconditionally intercepts task switches. Is this effectively working around a bug in task_switch_interception()?

Quoted 8 lines
---
 arch/x86/kvm/svm/svm.c                        |  21 ++
 tools/testing/selftests/kvm/Makefile.kvm      |   2 +
 .../testing/selftests/kvm/x86/int1_ept_test.c | 116 +++++++
 .../selftests/kvm/x86/int1_task_gate_test.c   | 298 ++++++++++++++++++
 4 files changed, 437 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/int1_ept_test.c

int1_host_page_fault_test? I definitely dont' want to call it "ept" since it's valid for both Intel (EPT) and AMD (NPT), but int1_tdp_test isn't accurate either since the test is also valid for shadow paging (at least, as written).

#2 lore raw
Quoted 15 lines · Sean Christopherson
Quoted 14 lines · David Woodhouse

From: David Woodhouse <[email protected]>

ICEBP (INT1, opcode 0xF1) generates a #DB that is architecturally a trap, but on SVM it was not always intercepted. Unconditionally intercept ICEBP on SVM to match VMX behaviour and ensure correct event delivery semantics.

Add two selftests exercising ICEBP:

- int1_ept_test: verifies that ICEBP works correctly when the exception stack page is not present (EPT/NPT fault during #DB delivery). The IST stack is evicted via MADV_DONTNEED before executing INT1.

Quoted 8 lines
Quoted 6 lines

- int1_task_gate_test: verifies ICEBP delivery through a 32-bit task gate, exercising the legacy task-switch path for #DB.

Tested on Intel Sapphire Rapids and AMD Genoa. Without the SVM fix, int1_task_gate_test fails on AMD with EIP pointing at ICEBP instead of after it. With the fix, both tests pass on both platforms.

Hmm, but KVM unconditionally intercepts task switches. Is this effectively working around a bug in task_switch_interception()?

Not really.  It's a bug/misfeature in AMD CPUs.

When you get TASK_SWITCH (which always has fault semantics), you look at the vectoring event type to decide whether it was logically caused by a trap, and therefore whether to move %rip forwards before entering the new task.

AMD CPUs don't distinguish instruction-induced #DBs (i.e. ICEBP) from exception-induced #DBs (all others), and also don't report an instruction length for an ICEBP-induced TASK_SWITCH.

The workaround is to intercept ICEBP unconditionally, handle the FAULT->TRAP conversion in the hypervisor, at which point the #DB-induced TASK_SWITCH occurs with %rip on the correct instruction boundary whether it was instruction-induced or exception-induced.

~Andrew

#3 lore raw
Quoted 27 lines · Andrew Cooper
Quoted 15 lines · Sean Christopherson
Quoted 14 lines · David Woodhouse

From: David Woodhouse <[email protected]>

ICEBP (INT1, opcode 0xF1) generates a #DB that is architecturally a trap, but on SVM it was not always intercepted. Unconditionally intercept ICEBP on SVM to match VMX behaviour and ensure correct event delivery semantics.

Add two selftests exercising ICEBP:

- int1_ept_test: verifies that ICEBP works correctly when the exception stack page is not present (EPT/NPT fault during #DB delivery). The IST stack is evicted via MADV_DONTNEED before executing INT1.

Quoted 8 lines
Quoted 6 lines

- int1_task_gate_test: verifies ICEBP delivery through a 32-bit task gate, exercising the legacy task-switch path for #DB.

Tested on Intel Sapphire Rapids and AMD Genoa. Without the SVM fix, int1_task_gate_test fails on AMD with EIP pointing at ICEBP instead of after it. With the fix, both tests pass on both platforms.

Hmm, but KVM unconditionally intercepts task switches. Is this effectively working around a bug in task_switch_interception()?

Not really.  It's a bug/misfeature in AMD CPUs.

Quoted 8 lines

When you get TASK_SWITCH (which always has fault semantics), you look at the vectoring event type to decide whether it was logically caused by a trap, and therefore whether to move %rip forwards before entering the new task.

AMD CPUs don't distinguish instruction-induced #DBs (i.e. ICEBP) from exception-induced #DBs (all others), and also don't report an instruction length for an ICEBP-induced TASK_SWITCH.

Heh, that explains why I couldn't find an equivalent of INTR_TYPE_PRIV_SW_EXCEPTION in the SVM code.

Quoted 6 lines

The workaround is to intercept ICEBP unconditionally, handle the FAULT->TRAP conversion in the hypervisor, at which point the #DB-induced TASK_SWITCH occurs with %rip on the correct instruction boundary whether it was instruction-induced or exception-induced.

~Andrew

#4 lore raw

+AMD folks

Quoted 33 lines · Sean Christopherson
Quoted 32 lines · Andrew Cooper
Quoted 15 lines · Sean Christopherson
Quoted 14 lines · David Woodhouse

From: David Woodhouse <[email protected]>

ICEBP (INT1, opcode 0xF1) generates a #DB that is architecturally a trap, but on SVM it was not always intercepted. Unconditionally intercept ICEBP on SVM to match VMX behaviour and ensure correct event delivery semantics.

Add two selftests exercising ICEBP:

- int1_ept_test: verifies that ICEBP works correctly when the exception stack page is not present (EPT/NPT fault during #DB delivery). The IST stack is evicted via MADV_DONTNEED before executing INT1.

Quoted 8 lines
Quoted 6 lines

- int1_task_gate_test: verifies ICEBP delivery through a 32-bit task gate, exercising the legacy task-switch path for #DB.

Tested on Intel Sapphire Rapids and AMD Genoa. Without the SVM fix, int1_task_gate_test fails on AMD with EIP pointing at ICEBP instead of after it. With the fix, both tests pass on both platforms.

Hmm, but KVM unconditionally intercepts task switches. Is this effectively working around a bug in task_switch_interception()?

Not really.  It's a bug/misfeature in AMD CPUs.

When you get TASK_SWITCH (which always has fault semantics), you look at the vectoring event type to decide whether it was logically caused by a trap, and therefore whether to move %rip forwards before entering the new task.

Quoted 6 lines
Quoted 3 lines

AMD CPUs don't distinguish instruction-induced #DBs (i.e. ICEBP) from exception-induced #DBs (all others), and also don't report an instruction length for an ICEBP-induced TASK_SWITCH.

Heh, that explains why I couldn't find an equivalent of INTR_TYPE_PRIV_SW_EXCEPTION in the SVM code.

Dragging in a comment/concern Andrew raised offlist. If AMD doesn't provide or *allow* the equivalent of INTR_TYPE_PRIV_SW_EXCEPTION, i.e. type 5, then what happens when KVM needs to inject an INT1 #DB with FRED enabled? Per Intel's FRED spec, which presumably AMD is following, the event type is shoved onto the stack:

— For INT1, the event stack level is IA32_FRED_STKLVLS[3:2]. The event type is
5 (privileged software exception) and the vector is 1.

But if SVM doesn't support SVM_EVTINJ_TYPE_INT1, then realistically this can't work (no way in hell is KVM going to emulate FRED event delivery). Does FRED on AMD even do the right thing for INT1 without SVM?

Quoted 6 lines
Quoted 6 lines

The workaround is to intercept ICEBP unconditionally, handle the FAULT->TRAP conversion in the hypervisor, at which point the #DB-induced TASK_SWITCH occurs with %rip on the correct instruction boundary whether it was instruction-induced or exception-induced.

~Andrew

#5 lore raw
Quoted 40 lines · Sean Christopherson

+AMD folks

Quoted 37 lines · Sean Christopherson
Quoted 35 lines · Andrew Cooper
Quoted 15 lines · Sean Christopherson
Quoted 14 lines · David Woodhouse

From: David Woodhouse <[email protected]>

ICEBP (INT1, opcode 0xF1) generates a #DB that is architecturally a trap, but on SVM it was not always intercepted. Unconditionally intercept ICEBP on SVM to match VMX behaviour and ensure correct event delivery semantics.

Add two selftests exercising ICEBP:

- int1_ept_test: verifies that ICEBP works correctly when the exception stack page is not present (EPT/NPT fault during #DB delivery). The IST stack is evicted via MADV_DONTNEED before executing INT1.

Quoted 8 lines
Quoted 6 lines

- int1_task_gate_test: verifies ICEBP delivery through a 32-bit task gate, exercising the legacy task-switch path for #DB.

Tested on Intel Sapphire Rapids and AMD Genoa. Without the SVM fix, int1_task_gate_test fails on AMD with EIP pointing at ICEBP instead of after it. With the fix, both tests pass on both platforms.

Hmm, but KVM unconditionally intercepts task switches. Is this effectively working around a bug in task_switch_interception()?

Not really.  It's a bug/misfeature in AMD CPUs.

When you get TASK_SWITCH (which always has fault semantics), you look at the vectoring event type to decide whether it was logically caused by a trap, and therefore whether to move %rip forwards before entering the new task.

AMD CPUs don't distinguish instruction-induced #DBs (i.e. ICEBP) from exception-induced #DBs (all others), and also don't report an instruction length for an ICEBP-induced TASK_SWITCH.

Quoted 6 lines
Quoted 2 lines

Heh, that explains why I couldn't find an equivalent of INTR_TYPE_PRIV_SW_EXCEPTION in the SVM code.

Dragging in a comment/concern Andrew raised offlist. If AMD doesn't provide or *allow* the equivalent of INTR_TYPE_PRIV_SW_EXCEPTION, i.e. type 5, then what happens when KVM needs to inject an INT1 #DB with FRED enabled?

I'm checking internally to get clarification on this. Will follow up once I have more details.

Regards,
Nikunj

#6 lore raw
Quoted 45 lines · Nikunj A Dadhania
Quoted 43 lines · Sean Christopherson

+AMD folks

Quoted 39 lines · Sean Christopherson
Quoted 27 lines · Andrew Cooper
Quoted 15 lines · Sean Christopherson
Quoted 14 lines · David Woodhouse

From: David Woodhouse <[email protected]>

ICEBP (INT1, opcode 0xF1) generates a #DB that is architecturally a trap, but on SVM it was not always intercepted. Unconditionally intercept ICEBP on SVM to match VMX behaviour and ensure correct event delivery semantics.

Add two selftests exercising ICEBP:

- int1_ept_test: verifies that ICEBP works correctly when the exception stack page is not present (EPT/NPT fault during #DB delivery). The IST stack is evicted via MADV_DONTNEED before executing INT1.

Quoted 8 lines
Quoted 6 lines

- int1_task_gate_test: verifies ICEBP delivery through a 32-bit task gate, exercising the legacy task-switch path for #DB.

Tested on Intel Sapphire Rapids and AMD Genoa. Without the SVM fix, int1_task_gate_test fails on AMD with EIP pointing at ICEBP instead of after it. With the fix, both tests pass on both platforms.

Hmm, but KVM unconditionally intercepts task switches. Is this effectively working around a bug in task_switch_interception()?

Not really.  It's a bug/misfeature in AMD CPUs.

Quoted 8 lines

When you get TASK_SWITCH (which always has fault semantics), you look at the vectoring event type to decide whether it was logically caused by a trap, and therefore whether to move %rip forwards before entering the new task.

AMD CPUs don't distinguish instruction-induced #DBs (i.e. ICEBP) from exception-induced #DBs (all others), and also don't report an instruction length for an ICEBP-induced TASK_SWITCH.

Heh, that explains why I couldn't find an equivalent of INTR_TYPE_PRIV_SW_EXCEPTION in the SVM code.

Quoted 6 lines
Quoted 3 lines

Dragging in a comment/concern Andrew raised offlist. If AMD doesn't provide or *allow* the equivalent of INTR_TYPE_PRIV_SW_EXCEPTION, i.e. type 5, then what happens when KVM needs to inject an INT1 #DB with FRED enabled?

I'm checking internally to get clarification on this. Will follow up once I have more details.

Following up on my earlier response — this has been addressed in the AMD FRED Virtualization specification. Type 5 (privileged software exception) is now supported in both EXITINTINFO and EVENTINJ for FRED-enabled guests.

Details are documented in the AMD FRED Virtualization spec (Rev 1.10):
https://docs.amd.com/v/u/en-US/69191-PUB

Regards,
Nikunj

maintainer
#7 lore raw
In reply to David Woodhouse
Quoted 21 lines · David Woodhouse

From: David Woodhouse <[email protected]>

ICEBP (INT1, opcode 0xF1) generates a #DB that is architecturally a trap, but on SVM it was not always intercepted. Unconditionally intercept ICEBP on SVM to match VMX behaviour and ensure correct event delivery semantics.

Add two selftests exercising ICEBP:

- int1_ept_test: verifies that ICEBP works correctly when the exception stack page is not present (EPT/NPT fault during #DB delivery). The IST stack is evicted via MADV_DONTNEED before executing INT1.

- int1_task_gate_test: verifies ICEBP delivery through a 32-bit task gate, exercising the legacy task-switch path for #DB.

Tested on Intel Sapphire Rapids and AMD Genoa. Without the SVM fix, int1_task_gate_test fails on AMD with EIP pointing at ICEBP instead of after it. With the fix, both tests pass on both platforms.

Quoted 6 lines
---
  arch/x86/kvm/svm/svm.c                        |  21 ++
  tools/testing/selftests/kvm/Makefile.kvm      |   2 +
  .../testing/selftests/kvm/x86/int1_ept_test.c | 116 +++++++
  .../selftests/kvm/x86/int1_task_gate_test.c   | 298 ++++++++++++++++++

y u no like kvm-unit-tests? For the task gate test it's much simpler, as all the infrastructure for 32-bit IDT and GDT is already there, and there is no real KVM corner case being tested.

See https://lore.kernel.org/r/[email protected]/ for a conversion, which is about 1/6th of the size of int1_task_gate_test.c.

Paolo

#8 lore raw
Quoted 4 lines · Paolo Bonzini

y u no like kvm-unit-tests?  For the task gate test it's much simpler, as all the infrastructure for 32-bit IDT and GDT is already there, and there is no real KVM corner case being tested.

No good reason; I just find the selftests a bit more convenient to run on a barebones test instance, or sometimes even throw in an initramfs and spawn a kernel just to run them.

Quoted 3 lines

See
https://lore.kernel.org/r/[email protected]/
for a conversion, which is about 1/6th of the size of int1_task_gate_test.c.

#9 lore raw
In reply to David Woodhouse
Quoted 8 lines · David Woodhouse

ICEBP (INT1, opcode 0xF1) generates a #DB that is architecturally a trap, but on SVM it was not always intercepted. Unconditionally intercept ICEBP on SVM to match VMX behaviour and ensure correct event delivery semantics.

Add two selftests exercising ICEBP:

[...]

Applied the KVM change to kvm-x86 svm, but omitted the selftests, at least for now.

[1/1] KVM: SVM: Always intercept ICEBP, add INT1 selftests

      https://github.com/kvm-x86/linux/commit/ec9a16c6aeba

--
https://github.com/kvm-x86/linux/tree/next