Skip to contentSkip to conversation

Add support for the Idle HLT intercept feature

PATCH Superseded Manali Shukla 2 participants 8 messages May 1, 2024 – May 21, 2024

Subsystems (4)

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover Add support for the Idle HLT intercept feature

The upcoming new Idle HLT Intercept feature allows for the HLT instruction execution by a vCPU to be intercepted by the hypervisor only if there are no pending V_INTR and V_NMI events for the vCPU. When the vCPU is expected to service the pending V_INTR and V_NMI events, the Idle HLT intercept won’t trigger. The feature allows the hypervisor to determine if the vCPU is actually idle and reduces wasteful VMEXITs.

Presence of the Idle HLT Intercept feature is indicated via CPUID function Fn8000_000A_EDX[30].

Document for the Idle HLT intercept feature is available at [1].

[1]: AMD64 Architecture Programmer's Manual Pub. 24593, April 2024,

     Vol 2, 15.9 Instruction Intercepts (Table 15-7: IDLE_HLT).
     https://bugzilla.kernel.org/attachment.cgi?id=306250

Testing Done:
Added a selftest to test the Idle HLT intercept functionality. Tested SEV and SEV-ES guest for the Idle HLT intercept functionality.

v1 -> v2
- Done changes in svm_idle_hlt_test based on the review comments from Sean.
- Added an enum based approach to get binary stats in vcpu_get_stat() which doesn't use string to get stat data based on the comments from Sean.
- Added self_halt() and cli() helpers based on the comments from Sean.

Manali Shukla (5):
x86/cpufeatures: Add CPUID feature bit for Idle HLT intercept
KVM: SVM: Add Idle HLT intercept support
KVM: selftests: Add safe_halt() and cli() helpers to common code
KVM: selftests: Add an interface to read the data of named vcpu stat
KVM: selftests: KVM: SVM: Add Idle HLT intercept test

 arch/x86/include/asm/cpufeatures.h            |  1 +
 arch/x86/include/asm/svm.h                    |  1 +
 arch/x86/include/uapi/asm/svm.h               |  2 +
 arch/x86/kvm/svm/svm.c                        | 15 +++-
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../testing/selftests/kvm/include/kvm_util.h  | 66 ++++++++++++++
 .../selftests/kvm/include/x86_64/processor.h  | 18 ++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 32 +++++++
 .../selftests/kvm/x86_64/svm_idle_hlt_test.c  | 87 +++++++++++++++++++
 9 files changed, 220 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86_64/svm_idle_hlt_test.c

base-commit: 2489e6c9ebb57d6d0e98936479b5f586201379c7

Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/5 x86/cpufeatures: Add CPUID feature bit for Idle HLT intercept

From: Manali Shukla <[email protected]>

The Idle HLT Intercept feature allows for the HLT instruction execution by a vCPU to be intercepted by the hypervisor only if there are no pending events (V_INTR and V_NMI) for the vCPU. When the vCPU is expected to service the pending events (V_INTR and V_NMI), the Idle HLT intercept won’t trigger. The feature allows the hypervisor to determine if the vCPU is idle and reduces wasteful VMEXITs.

Presence of Idle HLT intercept feature for guests is indicated via CPUID function 0x8000000A_EDX[30].

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index a38f8f9ba657..a8c5dec042dc 100644
@@ -381,6 +381,7 @@
 #define X86_FEATURE_V_SPEC_CTRL		(15*32+20) /* Virtual SPEC_CTRL */
 #define X86_FEATURE_VNMI		(15*32+25) /* Virtual NMI */
 #define X86_FEATURE_SVME_ADDR_CHK	(15*32+28) /* "" SVME addr check */
+#define X86_FEATURE_IDLE_HLT		(15*32+30) /* "" IDLE HLT intercept */
 
 /* Intel-defined CPU features, CPUID level 0x00000007:0 (ECX), word 16 */
 #define X86_FEATURE_AVX512VBMI		(16*32+ 1) /* AVX512 Vector Bit Manipulation instructions*/

base-commit: 2489e6c9ebb57d6d0e98936479b5f586201379c7

Signature · 1 line
2.34.1
#2 lore raw

PATCH v2 2/5 KVM: SVM: Add Idle HLT intercept support

In reply to Manali Shukla

From: Manali Shukla <[email protected]>

Execution of the HLT instruction by a vCPU can be intercepted by the hypervisor by setting the HLT-Intercept Bit in VMCB, thus resulting in a VMEXIT. It can be possible that soon after the VMEXIT, hypervisor observes that there are pending V_INTR and V_NMI events for the vCPU and causes it to perform a VMRUN to service those events. In that case VMEXIT is wasteful.

The Idle HLT intercept feature allows for the HLT instruction execution by a vCPU to be intercepted by hypervisor only if there are no pending V_INTR and V_NMI events for the vCPU. The Idle HLT intercept will not be triggerred, when vCPU is expected to have pending events (V_INR and V_NMI).

The feature allows the hypervisor to determine whether vCPU is idle and reduces wasteful VMEXITs.

Details about Idle HLT intercept can be found in AMD APM [1].

[1]: AMD64 Architecture Programmer's Manual Pub. 24593, April

     2024, Vol 2, 15.9 Instruction Intercepts (Table 15-7: IDLE_HLT).
     https://bugzilla.kernel.org/attachment.cgi?id=306250
---
 arch/x86/include/asm/svm.h      |  1 +
 arch/x86/include/uapi/asm/svm.h |  2 ++
 arch/x86/kvm/svm/svm.c          | 11 ++++++++---
 3 files changed, 11 insertions(+), 3 deletions(-)
arch/x86/include/asm/svm.h +1 -0
index 728c98175b9c..3a91928a4060 100644
@@ -116,6 +116,7 @@ enum {
 	INTERCEPT_INVPCID,
 	INTERCEPT_MCOMMIT,
 	INTERCEPT_TLBSYNC,
+	INTERCEPT_IDLE_HLT = 166,
 };
 
 
arch/x86/include/uapi/asm/svm.h +2 -0
index 80e1df482337..9910f86a2cef 100644
@@ -95,6 +95,7 @@
 #define SVM_EXIT_CR14_WRITE_TRAP		0x09e
 #define SVM_EXIT_CR15_WRITE_TRAP		0x09f
 #define SVM_EXIT_INVPCID       0x0a2
+#define SVM_EXIT_IDLE_HLT      0x0a6
 #define SVM_EXIT_NPF           0x400
 #define SVM_EXIT_AVIC_INCOMPLETE_IPI		0x401
 #define SVM_EXIT_AVIC_UNACCELERATED_ACCESS	0x402
@@ -223,6 +224,7 @@
 	{ SVM_EXIT_CR4_WRITE_TRAP,	"write_cr4_trap" }, \
 	{ SVM_EXIT_CR8_WRITE_TRAP,	"write_cr8_trap" }, \
 	{ SVM_EXIT_INVPCID,     "invpcid" }, \
+	{ SVM_EXIT_IDLE_HLT,     "idle-halt" }, \
 	{ SVM_EXIT_NPF,         "npf" }, \
 	{ SVM_EXIT_AVIC_INCOMPLETE_IPI,		"avic_incomplete_ipi" }, \
 	{ SVM_EXIT_AVIC_UNACCELERATED_ACCESS,   "avic_unaccelerated_access" }, \
arch/x86/kvm/svm/svm.c +8 -3
index 0f3b59da0d4a..223c670bf986 100644
@@ -1289,8 +1289,12 @@ static void init_vmcb(struct kvm_vcpu *vcpu)
 		svm_set_intercept(svm, INTERCEPT_MWAIT);
 	}
 
-	if (!kvm_hlt_in_guest(vcpu->kvm))
-		svm_set_intercept(svm, INTERCEPT_HLT);
+	if (!kvm_hlt_in_guest(vcpu->kvm)) {
+		if (cpu_feature_enabled(X86_FEATURE_IDLE_HLT))
+			svm_set_intercept(svm, INTERCEPT_IDLE_HLT);
+		else
+			svm_set_intercept(svm, INTERCEPT_HLT);
+	}
 
 	control->iopm_base_pa = __sme_set(iopm_base);
 	control->msrpm_base_pa = __sme_set(__pa(svm->msrpm));
@@ -3291,6 +3295,7 @@ static int (*const svm_exit_handlers[])(struct kvm_vcpu *vcpu) = {
 	[SVM_EXIT_CR4_WRITE_TRAP]		= cr_trap,
 	[SVM_EXIT_CR8_WRITE_TRAP]		= cr_trap,
 	[SVM_EXIT_INVPCID]                      = invpcid_interception,
+	[SVM_EXIT_IDLE_HLT]			= kvm_emulate_halt,
 	[SVM_EXIT_NPF]				= npf_interception,
 	[SVM_EXIT_RSM]                          = rsm_interception,
 	[SVM_EXIT_AVIC_INCOMPLETE_IPI]		= avic_incomplete_ipi_interception,
@@ -3453,7 +3458,7 @@ int svm_invoke_exit_handler(struct kvm_vcpu *vcpu, u64 exit_code)
 		return interrupt_window_interception(vcpu);
 	else if (exit_code == SVM_EXIT_INTR)
 		return intr_interception(vcpu);
-	else if (exit_code == SVM_EXIT_HLT)
+	else if (exit_code == SVM_EXIT_HLT || exit_code == SVM_EXIT_IDLE_HLT)
 		return kvm_emulate_halt(vcpu);
 	else if (exit_code == SVM_EXIT_NPF)
 		return npf_interception(vcpu);
Signature · 1 line
2.34.1
#3 lore raw

PATCH v2 3/5 KVM: selftests: Add safe_halt() and cli() helpers to common code

In reply to Manali Shukla

Add safe_halt() and cli() helpers to processor.h to make them broadly available in KVM selftests.

---
 .../selftests/kvm/include/x86_64/processor.h    | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
tools/testing/selftests/kvm/include/x86_64/processor.h +17 -0
index 8eb57de0b587..f74f31df96d2 100644
@@ -1305,6 +1305,23 @@ static inline void kvm_hypercall_map_gpa_range(uint64_t gpa, uint64_t size,
 	GUEST_ASSERT(!ret);
 }
 
+/*
+ * Execute HLT in an STI interrupt shadow to ensure that a pending IRQ that's
+ * intended to be a wake event arrives *after* HLT is executed.  Modern CPUs,
+ * except for a few oddballs that KVM is unlikely to run on, block IRQs for one
+ * instruction after STI, *if* RFLAGS.IF=0 before STI.  Note, Intel CPUs may
+ * block other events beyond regular IRQs, e.g. may block NMIs and SMIs too.
+ */
+static inline void safe_halt(void)
+{
+	asm volatile("sti; hlt");
+}
+
+static inline void cli(void)
+{
+	asm volatile ("cli");
+}
+
 void __vm_xsave_require_permission(uint64_t xfeature, const char *name);
 
 #define vm_xsave_require_permission(xfeature)	\
Signature · 1 line
2.34.1
#4 lore raw

PATCH v2 4/5 KVM: selftests: Add an interface to read the data of named vcpu stat

In reply to Manali Shukla

From: Manali Shukla <[email protected]>

The interface is used to read the data values of a specified vcpu stat from the currenly available binary stats interface.

---
 .../testing/selftests/kvm/include/kvm_util.h  | 66 +++++++++++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 32 +++++++++
 2 files changed, 98 insertions(+)
tools/testing/selftests/kvm/include/kvm_util.h +66 -0
index 63c2aaae51f3..7dad3275a4d3 100644
@@ -518,6 +518,72 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name)
 	return data;
 }
 
+/*
+ * Ensure that the sequence of the enum vcpu_stat_types matches the order of
+ * kvm_vcpu_stats_desc[].  Otherwise, vcpu_get_stat() may return incorrect data
+ * because __vcpu_get_stat() uses the enum type as an index to get the
+ * descriptor for a given stat and then uses read_stat_data() to get the stats
+ * from the descriptor.
+ */
+enum vcpu_stat_types {
+	HALT_SUCCESSFUL_POLL,
+	HALT_ATTEMPTED_POLL,
+	HALT_POLL_INVALID,
+	HALT_WAKEUP,
+	HALT_POLL_SUCCESS_NS,
+	HALT_POLL_FAIL_NS,
+	HALT_WAIT_NS,
+	HALT_POLL_SUCCESS_HIST,
+	HALT_POLL_FAIL_HIST,
+	HALT_WAIT_HIST,
+	BLOCKING,
+	PF_TAKEN,
+	PF_FIXED,
+	PF_EMULATE,
+	PF_SPURIOUS,
+	PF_FAST,
+	PF_MMIO_SPTE_CREATED,
+	PF_GUEST,
+	TLB_FLUSH,
+	INVLPG,
+	EXITS,
+	IO_EXITS,
+	MMIO_EXITS,
+	SIGNAL_EXITS,
+	IRQ_WINDOW_EXITS,
+	NMI_WINDOW_EXITS,
+	LD_FLUSH,
+	HALT_EXITS,
+	REQUEST_IRQ_EXITS,
+	IRQ_EXITS,
+	HOST_STATE_RELOAD,
+	FPU_RELOAD,
+	INSN_EMULATION,
+	INSN_EMULATION_FAIL,
+	HYPERCALLS,
+	IRQ_INJECTIONS,
+	NMI_INJECTIONS,
+	REQ_EVENT,
+	NESTED_RUN,
+	DIRECTED_YIELD_ATTEMPTED,
+	DIRECTED_YIELD_SUCCESSFUL,
+	PREEMPTION_REPORTED,
+	PREEMPTION_OTHER,
+	GUEST_MODE,
+	NOTIFY_WINDOW_EXITS,
+};
+
+void __vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type, uint64_t *data,
+		   size_t max_elements);
+
+static inline uint64_t vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type)
+{
+	uint64_t data;
+
+	__vcpu_get_stat(vcpu, type, &data, 1);
+	return data;
+}
+
 void vm_create_irqchip(struct kvm_vm *vm);
 
 static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
tools/testing/selftests/kvm/lib/kvm_util.c +32 -0
index 6b2158655baa..3de292ca9280 100644
@@ -2256,6 +2256,38 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header,
 		    desc->name, size, ret);
 }
 
+/*
+ * Read the data of the named vcpu stat
+ *
+ * Input Args:
+ *   vcpu - the vcpu for which the stat should be read
+ *   stat_name - the name of the stat to read
+ *   max_elements - the maximum number of 8-byte values to read into data
+ *
+ * Output Args:
+ *   data - the buffer into which stat data should be read
+ *
+ * Read the data values of a specified stat from the binary stats interface.
+ */
+void __vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type, uint64_t *data,
+		   size_t max_elements)
+{
+	int vcpu_stats_fd;
+	struct kvm_stats_header header;
+	struct kvm_stats_desc *desc, *t_desc;
+	size_t size_desc;
+
+	vcpu_stats_fd = vcpu_get_stats_fd(vcpu);
+	read_stats_header(vcpu_stats_fd, &header);
+
+	desc = read_stats_descriptors(vcpu_stats_fd, &header);
+	size_desc = get_stats_descriptor_size(&header);
+
+	t_desc = (void *)desc + (type * size_desc);
+	read_stat_data(vcpu_stats_fd, &header, t_desc,
+			data, max_elements);
+}
+
 /*
  * Read the data of the named stat
  *
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH v2 4/5] KVM: selftests: Add an interface to read the data of named vcpu stat

Quoted 28 lines · Manali Shukla

From: Manali Shukla <[email protected]>

The interface is used to read the data values of a specified vcpu stat from the currenly available binary stats interface.

---
 .../testing/selftests/kvm/include/kvm_util.h  | 66 +++++++++++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 32 +++++++++
 2 files changed, 98 insertions(+)
tools/testing/selftests/kvm/include/kvm_util.h +9 -0
index 63c2aaae51f3..7dad3275a4d3 100644
@@ -518,6 +518,72 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name)
 	return data;
 }
 
+/*
+ * Ensure that the sequence of the enum vcpu_stat_types matches the order of
+ * kvm_vcpu_stats_desc[].  Otherwise, vcpu_get_stat() may return incorrect data
+ * because __vcpu_get_stat() uses the enum type as an index to get the
+ * descriptor for a given stat and then uses read_stat_data() to get the stats
+ * from the descriptor.
+ */
+enum vcpu_stat_types {
+	HALT_SUCCESSFUL_POLL,

tools/testing/selftests/kvm/include/kvm_util.h static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name) · line 539

Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util.h +10 -0
+	HALT_ATTEMPTED_POLL,
+	HALT_POLL_INVALID,
+	HALT_WAKEUP,
+	HALT_POLL_SUCCESS_NS,
+	HALT_POLL_FAIL_NS,
+	HALT_WAIT_NS,
+	HALT_POLL_SUCCESS_HIST,
+	HALT_POLL_FAIL_HIST,
+	HALT_WAIT_HIST,
+	BLOCKING,

Everything below here is x86 specific, but this is an arch-neutral file. Please structure this in a way that each architecture can share the generic types and also provide its own.

Thanks,
drew

Quoted 96 lines
tools/testing/selftests/kvm/include/kvm_util.h +47 -0
+	PF_TAKEN,
+	PF_FIXED,
+	PF_EMULATE,
+	PF_SPURIOUS,
+	PF_FAST,
+	PF_MMIO_SPTE_CREATED,
+	PF_GUEST,
+	TLB_FLUSH,
+	INVLPG,
+	EXITS,
+	IO_EXITS,
+	MMIO_EXITS,
+	SIGNAL_EXITS,
+	IRQ_WINDOW_EXITS,
+	NMI_WINDOW_EXITS,
+	LD_FLUSH,
+	HALT_EXITS,
+	REQUEST_IRQ_EXITS,
+	IRQ_EXITS,
+	HOST_STATE_RELOAD,
+	FPU_RELOAD,
+	INSN_EMULATION,
+	INSN_EMULATION_FAIL,
+	HYPERCALLS,
+	IRQ_INJECTIONS,
+	NMI_INJECTIONS,
+	REQ_EVENT,
+	NESTED_RUN,
+	DIRECTED_YIELD_ATTEMPTED,
+	DIRECTED_YIELD_SUCCESSFUL,
+	PREEMPTION_REPORTED,
+	PREEMPTION_OTHER,
+	GUEST_MODE,
+	NOTIFY_WINDOW_EXITS,
+};
+
+void __vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type, uint64_t *data,
+		   size_t max_elements);
+
+static inline uint64_t vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type)
+{
+	uint64_t data;
+
+	__vcpu_get_stat(vcpu, type, &data, 1);
+	return data;
+}
+
 void vm_create_irqchip(struct kvm_vm *vm);
 
 static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
tools/testing/selftests/kvm/lib/kvm_util.c +32 -0
index 6b2158655baa..3de292ca9280 100644
@@ -2256,6 +2256,38 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header,
 		    desc->name, size, ret);
 }
 
+/*
+ * Read the data of the named vcpu stat
+ *
+ * Input Args:
+ *   vcpu - the vcpu for which the stat should be read
+ *   stat_name - the name of the stat to read
+ *   max_elements - the maximum number of 8-byte values to read into data
+ *
+ * Output Args:
+ *   data - the buffer into which stat data should be read
+ *
+ * Read the data values of a specified stat from the binary stats interface.
+ */
+void __vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type, uint64_t *data,
+		   size_t max_elements)
+{
+	int vcpu_stats_fd;
+	struct kvm_stats_header header;
+	struct kvm_stats_desc *desc, *t_desc;
+	size_t size_desc;
+
+	vcpu_stats_fd = vcpu_get_stats_fd(vcpu);
+	read_stats_header(vcpu_stats_fd, &header);
+
+	desc = read_stats_descriptors(vcpu_stats_fd, &header);
+	size_desc = get_stats_descriptor_size(&header);
+
+	t_desc = (void *)desc + (type * size_desc);
+	read_stat_data(vcpu_stats_fd, &header, t_desc,
+			data, max_elements);
+}
+
 /*
  * Read the data of the named stat
  *
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH v2 4/5] KVM: selftests: Add an interface to read the data of named vcpu stat

Quoted 44 lines · Andrew Jones
Quoted 28 lines · Manali Shukla

From: Manali Shukla <[email protected]>

The interface is used to read the data values of a specified vcpu stat from the currenly available binary stats interface.

---
 .../testing/selftests/kvm/include/kvm_util.h  | 66 +++++++++++++++++++
 tools/testing/selftests/kvm/lib/kvm_util.c    | 32 +++++++++
 2 files changed, 98 insertions(+)
tools/testing/selftests/kvm/include/kvm_util.h +9 -0
index 63c2aaae51f3..7dad3275a4d3 100644
@@ -518,6 +518,72 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name)
 	return data;
 }
 
+/*
+ * Ensure that the sequence of the enum vcpu_stat_types matches the order of
+ * kvm_vcpu_stats_desc[].  Otherwise, vcpu_get_stat() may return incorrect data
+ * because __vcpu_get_stat() uses the enum type as an index to get the
+ * descriptor for a given stat and then uses read_stat_data() to get the stats
+ * from the descriptor.
+ */
+enum vcpu_stat_types {
+	HALT_SUCCESSFUL_POLL,

tools/testing/selftests/kvm/include/kvm_util.h static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name) · line 539

Quoted 10 lines
tools/testing/selftests/kvm/include/kvm_util.h +10 -0
+	HALT_ATTEMPTED_POLL,
+	HALT_POLL_INVALID,
+	HALT_WAKEUP,
+	HALT_POLL_SUCCESS_NS,
+	HALT_POLL_FAIL_NS,
+	HALT_WAIT_NS,
+	HALT_POLL_SUCCESS_HIST,
+	HALT_POLL_FAIL_HIST,
+	HALT_WAIT_HIST,
+	BLOCKING,

Everything below here is x86 specific, but this is an arch-neutral file. Please structure this in a way that each architecture can share the generic types and also provide its own.

Quoted 2 lines

Thanks,
drew

Thank you for reviewing my patches.
Sure. I will take care of it in the next version.

-Manali

Quoted 97 lines
Quoted 97 lines
tools/testing/selftests/kvm/include/kvm_util.h +47 -0
+	PF_TAKEN,
+	PF_FIXED,
+	PF_EMULATE,
+	PF_SPURIOUS,
+	PF_FAST,
+	PF_MMIO_SPTE_CREATED,
+	PF_GUEST,
+	TLB_FLUSH,
+	INVLPG,
+	EXITS,
+	IO_EXITS,
+	MMIO_EXITS,
+	SIGNAL_EXITS,
+	IRQ_WINDOW_EXITS,
+	NMI_WINDOW_EXITS,
+	LD_FLUSH,
+	HALT_EXITS,
+	REQUEST_IRQ_EXITS,
+	IRQ_EXITS,
+	HOST_STATE_RELOAD,
+	FPU_RELOAD,
+	INSN_EMULATION,
+	INSN_EMULATION_FAIL,
+	HYPERCALLS,
+	IRQ_INJECTIONS,
+	NMI_INJECTIONS,
+	REQ_EVENT,
+	NESTED_RUN,
+	DIRECTED_YIELD_ATTEMPTED,
+	DIRECTED_YIELD_SUCCESSFUL,
+	PREEMPTION_REPORTED,
+	PREEMPTION_OTHER,
+	GUEST_MODE,
+	NOTIFY_WINDOW_EXITS,
+};
+
+void __vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type, uint64_t *data,
+		   size_t max_elements);
+
+static inline uint64_t vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type)
+{
+	uint64_t data;
+
+	__vcpu_get_stat(vcpu, type, &data, 1);
+	return data;
+}
+
 void vm_create_irqchip(struct kvm_vm *vm);
 
 static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
tools/testing/selftests/kvm/lib/kvm_util.c +32 -0
index 6b2158655baa..3de292ca9280 100644
@@ -2256,6 +2256,38 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header,
 		    desc->name, size, ret);
 }
 
+/*
+ * Read the data of the named vcpu stat
+ *
+ * Input Args:
+ *   vcpu - the vcpu for which the stat should be read
+ *   stat_name - the name of the stat to read
+ *   max_elements - the maximum number of 8-byte values to read into data
+ *
+ * Output Args:
+ *   data - the buffer into which stat data should be read
+ *
+ * Read the data values of a specified stat from the binary stats interface.
+ */
+void __vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type, uint64_t *data,
+		   size_t max_elements)
+{
+	int vcpu_stats_fd;
+	struct kvm_stats_header header;
+	struct kvm_stats_desc *desc, *t_desc;
+	size_t size_desc;
+
+	vcpu_stats_fd = vcpu_get_stats_fd(vcpu);
+	read_stats_header(vcpu_stats_fd, &header);
+
+	desc = read_stats_descriptors(vcpu_stats_fd, &header);
+	size_desc = get_stats_descriptor_size(&header);
+
+	t_desc = (void *)desc + (type * size_desc);
+	read_stat_data(vcpu_stats_fd, &header, t_desc,
+			data, max_elements);
+}
+
 /*
  * Read the data of the named stat
  *
Signature · 1 line
2.34.1
#7 lore raw

PATCH v2 5/5 KVM: selftests: KVM: SVM: Add Idle HLT intercept test

In reply to Manali Shukla

From: Manali Shukla <[email protected]>

Execution of the HLT instruction results in VMEXIT. Hypervisor observes pending V_INTR and V_NMI events just after VMEXIT generated by HLT for the vCPU and causes VM entry to service the pending events. The Idle HLT intercept feature avoids the wasteful VMEXIT during halt if there are pending V_INTR and V_NMI events for the vCPU.

The selftest for Idle HLT intercept instruments above-mentioned scenario.

---
 tools/testing/selftests/kvm/Makefile          |  1 +
 .../selftests/kvm/include/x86_64/processor.h  |  1 +
 .../selftests/kvm/x86_64/svm_idle_hlt_test.c  | 89 +++++++++++++++++++
 3 files changed, 91 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86_64/svm_idle_hlt_test.c
tools/testing/selftests/kvm/Makefile +1 -0
index 6de9994971c9..bd97586d7c04 100644
@@ -93,6 +93,7 @@ TEST_GEN_PROGS_x86_64 += x86_64/smaller_maxphyaddr_emulation_test
 TEST_GEN_PROGS_x86_64 += x86_64/smm_test
 TEST_GEN_PROGS_x86_64 += x86_64/state_test
 TEST_GEN_PROGS_x86_64 += x86_64/vmx_preemption_timer_test
+TEST_GEN_PROGS_x86_64 += x86_64/svm_idle_hlt_test
 TEST_GEN_PROGS_x86_64 += x86_64/svm_vmcall_test
 TEST_GEN_PROGS_x86_64 += x86_64/svm_int_ctl_test
 TEST_GEN_PROGS_x86_64 += x86_64/svm_nested_shutdown_test
tools/testing/selftests/kvm/include/x86_64/processor.h +1 -0
index f74f31df96d2..0036937b1be4 100644
@@ -192,6 +192,7 @@ struct kvm_x86_cpu_feature {
 #define X86_FEATURE_PAUSEFILTER         KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 10)
 #define X86_FEATURE_PFTHRESHOLD         KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 12)
 #define	X86_FEATURE_VGIF		KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 16)
+#define X86_FEATURE_IDLE_HLT		KVM_X86_CPU_FEATURE(0x8000000A, 0, EDX, 30)
 #define X86_FEATURE_SEV			KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 1)
 #define X86_FEATURE_SEV_ES		KVM_X86_CPU_FEATURE(0x8000001F, 0, EAX, 3)
 
tools/testing/selftests/kvm/x86_64/svm_idle_hlt_test.c new file +89 -0

89 lines not on this page · raw message

Signature · 1 line
2.34.1