Skip to contentSkip to conversation

x86/pv: Hide more EFER bits from PV guests

PATCH In review Andrew Cooper 3 participants 4 messages May 7, 2018

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/pv: Hide more EFER bits from PV guests

We don't advertise SVM in CPUID so a PV guest shouldn't be under the impression that it can use SVM functionality, but despite this, it really shouldn't see SVME set when reading EFER.

On Intel processors, 32bit PV guests don't see, and can't use SYSCALL.

Introduce EFER_KNOWN_MASK to whitelist the features Xen knows about, and use this to clamp the guests view.

Take the opportunity to reuse the mask to simplify svm_vmcb_isvalid(), and change "undefined" to "unknown" in the print message, as there is at least EFER.TCE (Translation Cache Extension) defined but unknown to Xen.

---
CC: Jan Beulich <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Brian Woods <[email protected]>

Arguably, this wants backporting to the stable trees, so should be considered for 4.11 at this point.

---
 xen/arch/x86/hvm/svm/svmdebug.c |  5 ++---
 xen/arch/x86/pv/emul-priv-op.c  | 11 +++++++++--
 xen/include/asm-x86/msr-index.h |  3 +++
 3 files changed, 14 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/svm/svmdebug.c +2 -3
index 6c215d1..d35e405 100644
@@ -133,9 +133,8 @@ bool svm_vmcb_isvalid(const char *from, const struct vmcb_struct *vmcb,
         PRINTF("DR7: bits [63:32] are not zero (%#"PRIx64")\n",
                vmcb_get_dr7(vmcb));
 
-    if ( efer & ~(EFER_SCE | EFER_LME | EFER_LMA | EFER_NX | EFER_SVME |
-                  EFER_LMSLE | EFER_FFXSE) )
-        PRINTF("EFER: undefined bits are not zero (%#"PRIx64")\n", efer);
+    if ( efer & ~EFER_KNOWN_MASK )
+        PRINTF("EFER: unknown bits are not zero (%#"PRIx64")\n", efer);
 
     if ( hvm_efer_valid(v, efer, -1) )
         PRINTF("EFER: %s (%"PRIx64")\n", hvm_efer_valid(v, efer, -1), efer);
xen/arch/x86/pv/emul-priv-op.c +9 -2
index 15f42b3..ce2ec76 100644
@@ -867,9 +867,16 @@ static int read_msr(unsigned int reg, uint64_t *val,
         return X86EMUL_OKAY;
 
     case MSR_EFER:
-        *val = read_efer();
+        /* Hide unknown bits, and unconditionally hide SVME from guests. */
+        *val = read_efer() & EFER_KNOWN_MASK & ~EFER_SVME;
+        /*
+         * Hide the 64-bit features from 32-bit guests.  SCE has
+         * vendor-dependent behaviour.
+         */
         if ( is_pv_32bit_domain(currd) )
-            *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE);
+            *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE |
+                      (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL
+                       ? EFER_SCE : 0));
         return X86EMUL_OKAY;
 
     case MSR_K7_FID_VID_CTL:
xen/include/asm-x86/msr-index.h +3 -0
index c9f44eb..6d94d65 100644
@@ -31,6 +31,9 @@
 #define EFER_LMSLE		(1<<_EFER_LMSLE)
 #define EFER_FFXSE		(1<<_EFER_FFXSE)
 
+#define EFER_KNOWN_MASK		(EFER_SCE | EFER_LME | EFER_LMA | EFER_NX | \
+				 EFER_SVME | EFER_LMSLE | EFER_FFXSE)
+
 /* Speculation Controls. */
 #define MSR_SPEC_CTRL			0x00000048
 #define SPEC_CTRL_IBRS			(_AC(1, ULL) << 0)
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 1/1
#1 lore raw
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 07.05.18 at 12:00, <[email protected]> wrote:
xen/arch/x86/pv/emul-priv-op.c +2 -1
@@ -867,9 +867,16 @@ static int read_msr(unsigned int reg, uint64_t *val,
         return X86EMUL_OKAY;
 
     case MSR_EFER:
-        *val = read_efer();
+        /* Hide unknown bits, and unconditionally hide SVME from guests. */
+        *val = read_efer() & EFER_KNOWN_MASK & ~EFER_SVME;

xen/arch/x86/pv/emul-priv-op.c static int read_msr(unsigned int reg, uint64_t *val, · line 880

Quoted 10 lines
xen/arch/x86/pv/emul-priv-op.c +7 -1
+        /*
+         * Hide the 64-bit features from 32-bit guests.  SCE has
+         * vendor-dependent behaviour.
+         */
         if ( is_pv_32bit_domain(currd) )
-            *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE);
+            *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE |
+                      (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL
+                       ? EFER_SCE : 0));
         return X86EMUL_OKAY;

Ideally this would check the domain's x86_vendor, but that would require wiring up emulation afaict, so fwiw

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 22 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 07.05.18 at 12:00, <[email protected]> wrote:
xen/arch/x86/pv/emul-priv-op.c +2 -1
@@ -867,9 +867,16 @@ static int read_msr(unsigned int reg, uint64_t *val,
         return X86EMUL_OKAY;
 
     case MSR_EFER:
-        *val = read_efer();
+        /* Hide unknown bits, and unconditionally hide SVME from guests. */
+        *val = read_efer() & EFER_KNOWN_MASK & ~EFER_SVME;

xen/arch/x86/pv/emul-priv-op.c static int read_msr(unsigned int reg, uint64_t *val, · line 880

Quoted 10 lines
xen/arch/x86/pv/emul-priv-op.c +7 -1
+        /*
+         * Hide the 64-bit features from 32-bit guests.  SCE has
+         * vendor-dependent behaviour.
+         */
         if ( is_pv_32bit_domain(currd) )
-            *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE);
+            *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE |
+                      (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL
+                       ? EFER_SCE : 0));
         return X86EMUL_OKAY;

Ideally this would check the domain's x86_vendor, but that would require wiring up emulation afaict, so fwiw

Quoted 1 line

Yes - we'd have to hook SYSCALL/SYSRET off the #UD handler, and teach x86_emulate() to understand PV's idea of guest kernel mode.  That is a complete can of worms.

If someone feels like making cross-vendor PV work then great, but I don't think we can plausibly claim that it might work atm.  (Same for HVM, despite the code we actually have.)

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

Reviewed-by on v2 1/1
#3 lore raw
In reply to Andrew Cooper
Quoted 85 lines · Andrew Cooper

We don't advertise SVM in CPUID so a PV guest shouldn't be under the impression that it can use SVM functionality, but despite this, it really shouldn't see SVME set when reading EFER.

On Intel processors, 32bit PV guests don't see, and can't use SYSCALL.

Introduce EFER_KNOWN_MASK to whitelist the features Xen knows about, and use this to clamp the guests view.

Take the opportunity to reuse the mask to simplify svm_vmcb_isvalid(), and change "undefined" to "unknown" in the print message, as there is at least EFER.TCE (Translation Cache Extension) defined but unknown to Xen.

---
CC: Jan Beulich <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Brian Woods <[email protected]>

Arguably, this wants backporting to the stable trees, so should be considered for 4.11 at this point.

---
 xen/arch/x86/hvm/svm/svmdebug.c |  5 ++---
 xen/arch/x86/pv/emul-priv-op.c  | 11 +++++++++--
 xen/include/asm-x86/msr-index.h |  3 +++
 3 files changed, 14 insertions(+), 5 deletions(-)
xen/arch/x86/hvm/svm/svmdebug.c +2 -3
index 6c215d1..d35e405 100644
@@ -133,9 +133,8 @@ bool svm_vmcb_isvalid(const char *from, const struct vmcb_struct *vmcb,
         PRINTF("DR7: bits [63:32] are not zero (%#"PRIx64")\n",
                vmcb_get_dr7(vmcb));
 
-    if ( efer & ~(EFER_SCE | EFER_LME | EFER_LMA | EFER_NX | EFER_SVME |
-                  EFER_LMSLE | EFER_FFXSE) )
-        PRINTF("EFER: undefined bits are not zero (%#"PRIx64")\n", efer);
+    if ( efer & ~EFER_KNOWN_MASK )
+        PRINTF("EFER: unknown bits are not zero (%#"PRIx64")\n", efer);
 
     if ( hvm_efer_valid(v, efer, -1) )
         PRINTF("EFER: %s (%"PRIx64")\n", hvm_efer_valid(v, efer, -1), efer);
xen/arch/x86/pv/emul-priv-op.c +9 -2
index 15f42b3..ce2ec76 100644
@@ -867,9 +867,16 @@ static int read_msr(unsigned int reg, uint64_t *val,
         return X86EMUL_OKAY;
 
     case MSR_EFER:
-        *val = read_efer();
+        /* Hide unknown bits, and unconditionally hide SVME from guests. */
+        *val = read_efer() & EFER_KNOWN_MASK & ~EFER_SVME;
+        /*
+         * Hide the 64-bit features from 32-bit guests.  SCE has
+         * vendor-dependent behaviour.
+         */
         if ( is_pv_32bit_domain(currd) )
-            *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE);
+            *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE |
+                      (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL
+                       ? EFER_SCE : 0));
         return X86EMUL_OKAY;
 
     case MSR_K7_FID_VID_CTL:
xen/include/asm-x86/msr-index.h +3 -0
index c9f44eb..6d94d65 100644
@@ -31,6 +31,9 @@
 #define EFER_LMSLE		(1<<_EFER_LMSLE)
 #define EFER_FFXSE		(1<<_EFER_FFXSE)
 
+#define EFER_KNOWN_MASK		(EFER_SCE | EFER_LME | EFER_LMA | EFER_NX | \
+				 EFER_SVME | EFER_LMSLE | EFER_FFXSE)
+
 /* Speculation Controls. */
 #define MSR_SPEC_CTRL			0x00000048
 #define SPEC_CTRL_IBRS			(_AC(1, ULL) << 0)
Signature · 1 line
2.1.4
Signature · 6 lines
Brian Woods

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel