Skip to contentSkip to conversation

KVM: VMX: Use wrapper macro ~RMODE_GUEST_OWNED_EFLAGS_BITS directly

PATCH In review linmiaohe 3 participants 3 messages Mar 5, 2020

Subsystems (3)
lore
Share & tools
lore

linmiaohe

author
#0 lore raw

PATCH 1/1 KVM: VMX: Use wrapper macro ~RMODE_GUEST_OWNED_EFLAGS_BITS directly

From: Miaohe Lin <[email protected]>

(X86_EFLAGS_IOPL | X86_EFLAGS_VM) indicates the eflag bits that can not be owned by realmode guest, i.e. ~RMODE_GUEST_OWNED_EFLAGS_BITS. Use wrapper macro directly to make it clear and also improve readability.

---
 arch/x86/kvm/vmx/vmx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +2 -2
index 743b81642ce2..9571f8dea016 100644
@@ -1466,7 +1466,7 @@ void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
 	vmx->rflags = rflags;
 	if (vmx->rmode.vm86_active) {
 		vmx->rmode.save_rflags = rflags;
-		rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
+		rflags |= ~RMODE_GUEST_OWNED_EFLAGS_BITS;
 	}
 	vmcs_writel(GUEST_RFLAGS, rflags);
 
@@ -2797,7 +2797,7 @@ static void enter_rmode(struct kvm_vcpu *vcpu)
 	flags = vmcs_readl(GUEST_RFLAGS);
 	vmx->rmode.save_rflags = flags;
 
-	flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
+	flags |= ~RMODE_GUEST_OWNED_EFLAGS_BITS;
 
 	vmcs_writel(GUEST_RFLAGS, flags);
 	vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Signature · 1 line
2.19.1
#1 lore raw
Quoted 25 lines · linmiaohe

From: Miaohe Lin <[email protected]>

(X86_EFLAGS_IOPL | X86_EFLAGS_VM) indicates the eflag bits that can not be owned by realmode guest, i.e. ~RMODE_GUEST_OWNED_EFLAGS_BITS. Use wrapper macro directly to make it clear and also improve readability.

---
 arch/x86/kvm/vmx/vmx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +1 -1
index 743b81642ce2..9571f8dea016 100644
@@ -1466,7 +1466,7 @@ void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
 	vmx->rflags = rflags;
 	if (vmx->rmode.vm86_active) {
 		vmx->rmode.save_rflags = rflags;
-		rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
+		rflags |= ~RMODE_GUEST_OWNED_EFLAGS_BITS;
 	}
 	vmcs_writel(GUEST_RFLAGS, rflags);
 

arch/x86/kvm/vmx/vmx.c static void enter_rmode(struct kvm_vcpu *vcpu) · line 2803

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -2797,7 +2797,7 @@ static void enter_rmode(struct kvm_vcpu *vcpu)
 	flags = vmcs_readl(GUEST_RFLAGS);
 	vmx->rmode.save_rflags = flags;
 
-	flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
+	flags |= ~RMODE_GUEST_OWNED_EFLAGS_BITS;
 
 	vmcs_writel(GUEST_RFLAGS, flags);
 	vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);

Double negations are evil, let's define a macro for 'X86_EFLAGS_IOPL | X86_EFLAGS_VM' instead (completely untested):

arch/x86/kvm/vmx/vmx.c +4 -3
index 4ee19fb35cde..d838f93bd6d2 100644
@@ -139,7 +139,8 @@ module_param_named(preemption_timer, enable_preemption_timer, bool, S_IRUGO);
 #define KVM_PMODE_VM_CR4_ALWAYS_ON (X86_CR4_PAE | X86_CR4_VMXE)
 #define KVM_RMODE_VM_CR4_ALWAYS_ON (X86_CR4_VME | X86_CR4_PAE | X86_CR4_VMXE)
 
-#define RMODE_GUEST_OWNED_EFLAGS_BITS (~(X86_EFLAGS_IOPL | X86_EFLAGS_VM))
+#define RMODE_HOST_OWNED_EFLAGS_BITS (X86_EFLAGS_IOPL | X86_EFLAGS_VM)
+#define RMODE_GUEST_OWNED_EFLAGS_BITS (~RMODE_HOST_OWNED_EFLAGS_BITS)
 
 #define MSR_IA32_RTIT_STATUS_MASK (~(RTIT_STATUS_FILTEREN | \
        RTIT_STATUS_CONTEXTEN | RTIT_STATUS_TRIGGEREN | \
@@ -1468,7 +1469,7 @@ void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
        vmx->rflags = rflags;
        if (vmx->rmode.vm86_active) {
                vmx->rmode.save_rflags = rflags;
-               rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
+               rflags |= RMODE_HOST_OWNED_EFLAGS_BITS;
        }
        vmcs_writel(GUEST_RFLAGS, rflags);
 
@@ -2794,7 +2795,7 @@ static void enter_rmode(struct kvm_vcpu *vcpu)
        flags = vmcs_readl(GUEST_RFLAGS);
        vmx->rmode.save_rflags = flags;
 
-       flags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
+       flags |= RMODE_HOST_OWNED_EFLAGS_BITS;
 
        vmcs_writel(GUEST_RFLAGS, flags);
        vmcs_writel(GUEST_CR4, vmcs_readl(GUEST_CR4) | X86_CR4_VME);
Signature · 1 line
Vitaly
maintainer
#2 lore raw
In reply to linmiaohe
Quoted 2 lines · linmiaohe

(X86_EFLAGS_IOPL | X86_EFLAGS_VM) indicates the eflag bits that can not be owned by realmode guest, i.e. ~RMODE_GUEST_OWNED_EFLAGS_BITS.

... but ~RMODE_GUEST_OWNED_EFLAGS_BITS is the bits that are owned by the host; they could be 0 or 1 and that's why the code was using X86_EFLAGS_IOPL | X86_EFLAGS_VM.

I understand where ~RMODE_GUEST_OWNED_EFLAGS_BITS is better than X86_EFLAGS_IOPL | X86_EFLAGS_VM, but I cannot think of a way to express it that is the best of both worlds.

Paolo

Use wrapper

Quoted 19 lines

macro directly to make it clear and also improve readability.

---
 arch/x86/kvm/vmx/vmx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +1 -1
index 743b81642ce2..9571f8dea016 100644
@@ -1466,7 +1466,7 @@ void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags)
 	vmx->rflags = rflags;
 	if (vmx->rmode.vm86_active) {
 		vmx->rmode.save_rflags = rflags;
-		rflags |= X86_EFLAGS_IOPL | X86_EFLAGS_VM;
+		rflags |= ~RMODE_GUEST_OWNED_EFLAGS_BITS;
 	}
 	vmcs_writel(GUEST_RFLAGS, rflags);