Skip to contentSkip to conversation

x86/vmx: correct the SMEP logic for HVM_CR0_GUEST_RESERVED_BITS

PATCH Superseded Feng Wu 3 participants 3 messages Apr 23, 2014

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Feng Wu

author
#0 lore raw

PATCH 1/1 x86/vmx: correct the SMEP logic for HVM_CR0_GUEST_RESERVED_BITS

When checking the SMEP feature for HVM guests, we should check the VCPU instead of the host CPU.

---
 xen/include/asm-x86/hvm/hvm.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
xen/include/asm-x86/hvm/hvm.h +10 -1
index dcc3483..74a09ef 100644
@@ -351,6 +351,15 @@ static inline int hvm_event_pending(struct vcpu *v)
     return hvm_funcs.event_pending(v);
 }
 
+static inline bool_t hvm_vcpu_has_smep(void)
+{
+    unsigned int ebx = 0, leaf = 0x7;
+
+    hvm_cpuid(leaf, NULL, &ebx, NULL, NULL);
+
+    return !!(ebx & cpufeat_mask(X86_FEATURE_SMEP));
+}
+
 /* These reserved bits in lower 32 remain 0 after any load of CR0 */
 #define HVM_CR0_GUEST_RESERVED_BITS             \
     (~((unsigned long)                          \
@@ -370,7 +379,7 @@ static inline int hvm_event_pending(struct vcpu *v)
         X86_CR4_DE  | X86_CR4_PSE | X86_CR4_PAE |       \
         X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE |       \
         X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT |           \
-        (cpu_has_smep ? X86_CR4_SMEP : 0) |             \
+        (hvm_vcpu_has_smep() ? X86_CR4_SMEP : 0) |      \
         (cpu_has_fsgsbase ? X86_CR4_FSGSBASE : 0) |     \
         ((nestedhvm_enabled((_v)->domain) && cpu_has_vmx)\
                       ? X86_CR4_VMXE : 0)  |             \
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 13 lines · Feng Wu

When checking the SMEP feature for HVM guests, we should check the VCPU instead of the host CPU.

---
 xen/include/asm-x86/hvm/hvm.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
xen/include/asm-x86/hvm/hvm.h +0 -0
index dcc3483..74a09ef 100644

xen/include/asm-x86/hvm/hvm.h static inline int hvm_event_pending(struct vcpu *v) · line 356

Quoted 6 lines
xen/include/asm-x86/hvm/hvm.h +3 -0
@@ -351,6 +351,15 @@ static inline int hvm_event_pending(struct vcpu *v)
     return hvm_funcs.event_pending(v);
 }
 
+static inline bool_t hvm_vcpu_has_smep(void)
+{
+    unsigned int ebx = 0, leaf = 0x7;

You need to check hvm_cpuid.0.eax for max leaf.

~Andrew

Quoted 18 lines
xen/include/asm-x86/hvm/hvm.h +7 -1
+
+    hvm_cpuid(leaf, NULL, &ebx, NULL, NULL);
+
+    return !!(ebx & cpufeat_mask(X86_FEATURE_SMEP));
+}
+
 /* These reserved bits in lower 32 remain 0 after any load of CR0 */
 #define HVM_CR0_GUEST_RESERVED_BITS             \
     (~((unsigned long)                          \
@@ -370,7 +379,7 @@ static inline int hvm_event_pending(struct vcpu *v)
         X86_CR4_DE  | X86_CR4_PSE | X86_CR4_PAE |       \
         X86_CR4_MCE | X86_CR4_PGE | X86_CR4_PCE |       \
         X86_CR4_OSFXSR | X86_CR4_OSXMMEXCPT |           \
-        (cpu_has_smep ? X86_CR4_SMEP : 0) |             \
+        (hvm_vcpu_has_smep() ? X86_CR4_SMEP : 0) |      \
         (cpu_has_fsgsbase ? X86_CR4_FSGSBASE : 0) |     \
         ((nestedhvm_enabled((_v)->domain) && cpu_has_vmx)\
                       ? X86_CR4_VMXE : 0)  |             \
#2 lore raw
In reply to Feng Wu

xen/include/asm-x86/hvm/hvm.h static inline int hvm_event_pending(struct vcpu *v) · line 356

Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 23.04.14 at 16:32, <[email protected]> wrote:
xen/include/asm-x86/hvm/hvm.h +3 -0
@@ -351,6 +351,15 @@ static inline int hvm_event_pending(struct vcpu *v)
     return hvm_funcs.event_pending(v);
 }
 
+static inline bool_t hvm_vcpu_has_smep(void)
+{
+    unsigned int ebx = 0, leaf = 0x7;

Pointless initializer for ebx, and pointless variable leaf.

xen/include/asm-x86/hvm/hvm.h static inline int hvm_event_pending(struct vcpu *v) · line 358

Quoted 1 line
xen/include/asm-x86/hvm/hvm.h +1 -0
+    hvm_cpuid(leaf, NULL, &ebx, NULL, NULL);

And, as I just saw Andrew already pointed out, this needs gating on there being a (visible) leaf 7 in the first place.

Jan