Skip to contentSkip to conversation

KVM: SVM: Virtual GIF

PATCH Superseded Janakarajan Natarajan 4 participants 8 messages Aug 16, 2017 – Aug 17, 2017

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover KVM: SVM: Virtual GIF

This patchset adds support for the Virtual GIF (VGIF) feature. This feature allows the STGI and CLGI instructions to be executed in the Guest Mode and not require a #VMEXIT. With this, virtual interrupts can be controlled in the Guest Mode while still allowing physical interrupts to be intercepted by the hypervisor.

In order to provide this ability, two new bits are added to the VMCB at offset 60h:

* Bit 9 - VGIF value

	 : 0 -> Virtual interrupts are masked
	 : 1 -> Virtual interrupts are unmasked

* Bit 25 - AMD Virtual GIF enabled for this guest

	 : 0 -> Disabled
	 : 1 -> Enabled

When a VMRUN is executed and Bit 25 is set, the processor uses Bit 9 as the starting value of the virtual GIF. It then provides masking capability for when virtual interrupts are taken. Bit 9 is writeable by the hypervisor and loaded on VMRUN and saved on #VMEXIT. STGI/CLGI executed in the Guest Mode sets or clear the virtual GIF.

The advantage of this feature will be the greatly reduced number of world switches to support the STGI and CLGI instructions by the outermost hypervisor at Current Privilege Level (CPL) 0.

This has been tested with Xen, Hyper-V and KVM as the nested hypervisor.

Janakarajan Natarajan (2):
KVM: SVM: Add Virtual GIF feature definition
KVM: SVM: Enable Virtual GIF feature

 arch/x86/include/asm/cpufeatures.h |  1 +
 arch/x86/include/asm/svm.h         |  6 +++++
 arch/x86/kvm/svm.c                 | 47 ++++++++++++++++++++++++++++++++------
 3 files changed, 47 insertions(+), 7 deletions(-)
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/2 KVM: SVM: Add Virtual GIF feature definition

Define a new cpufeature definition for Virtual GIF.

---
 arch/x86/include/asm/cpufeatures.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +1 -0
index ca3c48c..58e7211 100644
@@ -287,6 +287,7 @@
 #define X86_FEATURE_PFTHRESHOLD (15*32+12) /* pause filter threshold */
 #define X86_FEATURE_AVIC	(15*32+13) /* Virtual Interrupt Controller */
 #define X86_FEATURE_VIRTUAL_VMLOAD_VMSAVE (15*32+15) /* Virtual VMLOAD VMSAVE */
+#define X86_FEATURE_V_GIF	(15*32+16) /* Virtual GIF */
 
 /* Intel-defined CPU features, CPUID level 0x00000007:0 (ecx), word 16 */
 #define X86_FEATURE_AVX512VBMI  (16*32+ 1) /* AVX512 Vector Bit Manipulation instructions*/
Signature · 1 line
2.7.4
#4 lore raw

PATCH 2/2 KVM: SVM: Enable Virtual GIF feature

In reply to Janakarajan Natarajan

Enable the Virtual GIF feature. This is done by setting bit 25 at position 60h in the vmcb.

With this feature enabled, the processor uses bit 9 at position 60h as the virtual GIF when executing STGI/CLGI instructions.

Since the execution of STGI by the L1 hypervisor does not cause a return to the outermost (L0) hypervisor, the enable_irq_window and enable_nmi_window are modified.

The IRQ and NMI windows will be opened even if GIF is not set, under the assumption that on resuming the L1 hypervisor the IRQ and NMI will be held pending until the processor executes the STGI instruction.

---
 arch/x86/include/asm/svm.h |  6 ++++++
 arch/x86/kvm/svm.c         | 47 +++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 46 insertions(+), 7 deletions(-)
arch/x86/include/asm/svm.h +6 -0
index 58fffe7..14835dd 100644
@@ -107,6 +107,9 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 #define V_IRQ_SHIFT 8
 #define V_IRQ_MASK (1 << V_IRQ_SHIFT)
 
+#define V_GIF_SHIFT 9
+#define V_GIF_MASK (1 << V_GIF_SHIFT)
+
 #define V_INTR_PRIO_SHIFT 16
 #define V_INTR_PRIO_MASK (0x0f << V_INTR_PRIO_SHIFT)
 
@@ -116,6 +119,9 @@ struct __attribute__ ((__packed__)) vmcb_control_area {
 #define V_INTR_MASKING_SHIFT 24
 #define V_INTR_MASKING_MASK (1 << V_INTR_MASKING_SHIFT)
 
+#define V_GIF_ENABLE_SHIFT 25
+#define V_GIF_ENABLE_MASK (1 << V_GIF_ENABLE_SHIFT)
+
 #define AVIC_ENABLE_SHIFT 31
 #define AVIC_ENABLE_MASK (1 << AVIC_ENABLE_SHIFT)
 
arch/x86/kvm/svm.c +40 -7

94 lines not on this page · raw message

Signature · 1 line
2.7.4
#2 lore raw

Re: [PATCH 1/2] KVM: SVM: Add Virtual GIF feature definition

In reply to Janakarajan Natarajan
Quoted 1 line · Janakarajan Natarajan

Define a new cpufeature definition for Virtual GIF.

"Define ... definition" ?

Quoted 11 lines
---
 arch/x86/include/asm/cpufeatures.h | 1 +
 1 file changed, 1 insertion(+)
arch/x86/include/asm/cpufeatures.h +0 -0
index ca3c48c..58e7211 100644

arch/x86/include/asm/cpufeatures.h line 290

Quoted 4 lines
arch/x86/include/asm/cpufeatures.h +1 -0
@@ -287,6 +287,7 @@
 #define X86_FEATURE_PFTHRESHOLD (15*32+12) /* pause filter threshold */
 #define X86_FEATURE_AVIC	(15*32+13) /* Virtual Interrupt Controller */
 #define X86_FEATURE_VIRTUAL_VMLOAD_VMSAVE (15*32+15) /* Virtual VMLOAD VMSAVE */
+#define X86_FEATURE_V_GIF	(15*32+16) /* Virtual GIF */

PPR calls it vGIF so make that X86_FEATURE_VGIF.

Thanks.

Signature · 5 lines
Regards/Gruss,
    Boris.

SUSE Linux GmbH, GF: Felix Imendörffer, Jane Smithard, Graham Norton, HRB 21284 (AG Nürnberg)
-- 
#5 lore raw

Re: [PATCH 2/2] KVM: SVM: Enable Virtual GIF feature

In reply to Janakarajan Natarajan

2017-08-16 10:54-0500, Janakarajan Natarajan:

Quoted 18 lines

Enable the Virtual GIF feature. This is done by setting bit 25 at position 60h in the vmcb.

With this feature enabled, the processor uses bit 9 at position 60h as the virtual GIF when executing STGI/CLGI instructions.

Since the execution of STGI by the L1 hypervisor does not cause a return to the outermost (L0) hypervisor, the enable_irq_window and enable_nmi_window are modified.

The IRQ and NMI windows will be opened even if GIF is not set, under the assumption that on resuming the L1 hypervisor the IRQ and NMI will be held pending until the processor executes the STGI instruction.

---

arch/x86/kvm/svm.c static void enable_nmi_window(struct kvm_vcpu *vcpu) · line 4739

Quoted 7 lines
arch/x86/kvm/svm.c +2 -2
@@ -4702,8 +4735,8 @@ static void enable_nmi_window(struct kvm_vcpu *vcpu)
 	    == HF_NMI_MASK)
 		return; /* IRET will cause a vm exit */
 
-	if ((svm->vcpu.arch.hflags & HF_GIF_MASK) == 0)
-		return; /* STGI will cause a vm exit */
+	if (!vgif_enabled(svm) && !gif_set(svm))
+		return; /* STGI will cause a vm exit or HW will set VGIF*/

Why don't we enable STGI interception to get notified that the window has opened? (I doubt that single stepping would be faster ...)

Thanks.

#6 lore raw

Re: [PATCH 2/2] KVM: SVM: Enable Virtual GIF feature

Quoted 22 lines · Radim Krcmar

2017-08-16 10:54-0500, Janakarajan Natarajan:

Quoted 21 lines

Enable the Virtual GIF feature. This is done by setting bit 25 at position 60h in the vmcb.

With this feature enabled, the processor uses bit 9 at position 60h as the virtual GIF when executing STGI/CLGI instructions.

Since the execution of STGI by the L1 hypervisor does not cause a return to the outermost (L0) hypervisor, the enable_irq_window and enable_nmi_window are modified.

The IRQ and NMI windows will be opened even if GIF is not set, under the assumption that on resuming the L1 hypervisor the IRQ and NMI will be held pending until the processor executes the STGI instruction.

---

arch/x86/kvm/svm.c +0 -0
@@ -4702,8 +4735,8 @@ static void enable_nmi_window(struct kvm_vcpu *vcpu)
  	    == HF_NMI_MASK)
  		return; /* IRET will cause a vm exit */
 
Quoted 6 lines

arch/x86/kvm/svm.c static void enable_nmi_window(struct kvm_vcpu *vcpu) · line 4739

Quoted 4 lines
arch/x86/kvm/svm.c +2 -2
-	if ((svm->vcpu.arch.hflags & HF_GIF_MASK) == 0)
-		return; /* STGI will cause a vm exit */
+	if (!vgif_enabled(svm) && !gif_set(svm))
+		return; /* STGI will cause a vm exit or HW will set VGIF*/

Why don't we enable STGI interception to get notified that the window has opened? (I doubt that single stepping would be faster ...)

It would defeat the purpose of having vGIF - execute STGI/CLGI and not have a world-switch. Plus it would be like implementing the vGIF feature halfway, allowing only CLGI to take advantage of the hardware.

Quoted 2 lines

Thanks.

maintainer
#7 lore raw

Re: [PATCH 2/2] KVM: SVM: Enable Virtual GIF feature

----- Original Message -----

Quoted 40 lines

From: "Janakarajan Natarajan" <[email protected]>
To: "Radim Krcmar" <[email protected]>
Cc: [email protected], [email protected], [email protected], "Paolo Bonzini" <[email protected]>, "Joerg Roedel" <[email protected]>, "Andy Lutomirski" <[email protected]>, "Tony Luck" <[email protected]>, "Borislav Petkov" <[email protected]>, "Thomas Gleixner" <[email protected]>, "Ingo Molnar" <[email protected]>, "H . Peter Anvin" <[email protected]>, "Yazen Ghannam" <[email protected]>
Sent: Thursday, August 17, 2017 12:04:10 AM
Subject: Re: [PATCH 2/2] KVM: SVM: Enable Virtual GIF feature

Quoted 23 lines · Radim Krcmar

2017-08-16 10:54-0500, Janakarajan Natarajan:

Quoted 22 lines

Enable the Virtual GIF feature. This is done by setting bit 25 at position 60h in the vmcb.

With this feature enabled, the processor uses bit 9 at position 60h as the virtual GIF when executing STGI/CLGI instructions.

Since the execution of STGI by the L1 hypervisor does not cause a return to
the outermost (L0) hypervisor, the enable_irq_window and enable_nmi_window are modified.

The IRQ and NMI windows will be opened even if GIF is not set, under the assumption that on resuming the L1 hypervisor the IRQ and NMI will be held pending until the processor executes the STGI instruction.

---

arch/x86/kvm/svm.c +0 -0
@@ -4702,8 +4735,8 @@ static void enable_nmi_window(struct kvm_vcpu *vcpu)
  	    == HF_NMI_MASK)
  		return; /* IRET will cause a vm exit */
 
Quoted 6 lines

arch/x86/kvm/svm.c static void enable_nmi_window(struct kvm_vcpu *vcpu) · line 4739

Quoted 4 lines
arch/x86/kvm/svm.c +2 -2
-	if ((svm->vcpu.arch.hflags & HF_GIF_MASK) == 0)
-		return; /* STGI will cause a vm exit */
+	if (!vgif_enabled(svm) && !gif_set(svm))
+		return; /* STGI will cause a vm exit or HW will set VGIF*/

Why don't we enable STGI interception to get notified that the window has opened? (I doubt that single stepping would be faster ...)

Quoted 3 lines

It would defeat the purpose of having vGIF - execute STGI/CLGI and not have a world-switch. Plus it would be like implementing the vGIF feature halfway, allowing only CLGI to take advantage of the hardware.

No, only enable the STGI intercept to enable the NMI window. Normally you'd still run with free STGI.

This is because if you do not return here, you enter singlestepping mode where each instruction causes a world switch.

Paolo

#3 lore raw

Re: [PATCH 1/2] KVM: SVM: Add Virtual GIF feature definition

In reply to Borislav Petkov
Quoted 9 lines · Borislav Petkov
Quoted 1 line · Janakarajan Natarajan

Define a new cpufeature definition for Virtual GIF.

"Define ... definition" ?

Quoted 5 lines
---
  arch/x86/include/asm/cpufeatures.h | 1 +
  1 file changed, 1 insertion(+)
Quoted 10 lines

arch/x86/include/asm/cpufeatures.h line 290

Quoted 9 lines
arch/x86/include/asm/cpufeatures.h +1 -0
index ca3c48c..58e7211 100644
@@ -287,6 +287,7 @@
  #define X86_FEATURE_PFTHRESHOLD (15*32+12) /* pause filter threshold */
  #define X86_FEATURE_AVIC	(15*32+13) /* Virtual Interrupt Controller */
  #define X86_FEATURE_VIRTUAL_VMLOAD_VMSAVE (15*32+15) /* Virtual VMLOAD VMSAVE */
+#define X86_FEATURE_V_GIF	(15*32+16) /* Virtual GIF */

PPR calls it vGIF so make that X86_FEATURE_VGIF.

I will send a v2 with the changes.

Quoted 3 lines

Thanks.