Skip to contentSkip to conversation

x86/pv: Unconditionally hide EFER.SVME from PV guests

PATCH Superseded Andrew Cooper 4 participants 8 messages May 4, 2018 – May 7, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/pv: Unconditionally hide EFER.SVME 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.

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: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Brian Woods <[email protected]>
CC: Juergen Gross <[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  | 4 +++-
 xen/include/asm-x86/msr-index.h | 3 +++
 3 files changed, 8 insertions(+), 4 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 +3 -1
index 15f42b3..8293f31 100644
@@ -867,7 +867,9 @@ 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. */
         if ( is_pv_32bit_domain(currd) )
             *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE);
         return X86EMUL_OKAY;
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 v1 1/1
#1 lore raw

xen/include/asm-x86/msr-index.h line 34

Quoted 7 lines · Andrew Cooper
xen/include/asm-x86/msr-index.h +1 -0
@@ -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 | \

I think there is an extra tab here (but this may be my email client not showing it properly)

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

#2 lore raw
Quoted 11 lines · Boris Ostrovsky

xen/include/asm-x86/msr-index.h line 34

Quoted 7 lines · Andrew Cooper
xen/include/asm-x86/msr-index.h +1 -0
@@ -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 | \

I think there is an extra tab here (but this may be my email client not showing it properly)

Its correct in the file, but renders incorrectly everywhere else.  (I did a doubletake the first time I saw the email.)

Quoted 1 line

Thanks,

~Andrew

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

#3 lore raw
In reply to Andrew Cooper
Quoted 4 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.

Quoted 8 lines

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.

Juergen

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

#4 lore raw
In reply to Andrew Cooper

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

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 04.05.18 at 19:28, <[email protected]> wrote:
xen/arch/x86/pv/emul-priv-op.c +3 -1
@@ -867,7 +867,9 @@ 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. */
         if ( is_pv_32bit_domain(currd) )
             *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE);

Wouldn't it be better then to also move the LMSLE hiding up? And what about SCE? PV guests not being allowed to write EFER, I would think they shouldn't see bits they aren't supposed to care about and aren't able to set. If we were to allow such writes, I assume it would only be NX and maybe FFXSE which we'd permit the guest to control. Obviously (I think) LME and LMA ought to be seen set by 64-bit guests.

Jan

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

#5 lore raw
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 04.05.18 at 19:28, <[email protected]> wrote:
xen/arch/x86/pv/emul-priv-op.c +0 -0
@@ -867,7 +867,9 @@ static int read_msr(unsigned int reg, uint64_t *val,
         return X86EMUL_OKAY;
 
Quoted 8 lines

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

Quoted 7 lines
xen/arch/x86/pv/emul-priv-op.c +3 -1
     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. */
         if ( is_pv_32bit_domain(currd) )
             *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE);

Wouldn't it be better then to also move the LMSLE hiding up?

Actually, on second consideration, LMSLE shouldn't be hidden.  If LMSLE is actually active, then segment descriptors behave differently whether the guest knows about it or not.

Quoted 1 line

And what about SCE?

Rather more difficult to say, given its cross-vendor behaviour.  It should probably be clobbered if 32bit && Intel, but 32bit AMD can use SYSCALL if it wishes.

Quoted 5 lines

PV guests not being allowed to write EFER, I would think they shouldn't
see bits they aren't supposed to care about and aren't able to set. If we were to allow such writes, I assume it would only be NX and maybe FFXSE which we'd permit the guest to control. Obviously (I think) LME and LMA ought to be seen set by 64-bit guests.

I don't see any value in letting PV guests write to EFER.  They never have been able to in the past, and letting them play with NX has a direct safety impact on Xen when XPTI is not in use.

~Andrew

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

#6 lore raw
Quoted 17 lines
Quoted 1 line
Quoted 1 line · On 07.05.18 at 09:30, <[email protected]> wrote:
Quoted 6 lines · Jan Beulich
Quoted 6 lines
Quoted 1 line
Quoted 1 line · On 04.05.18 at 19:28, <[email protected]> wrote:
xen/arch/x86/pv/emul-priv-op.c +0 -0
@@ -867,7 +867,9 @@ static int read_msr(unsigned int reg, uint64_t *val,
         return X86EMUL_OKAY;
 
Quoted 8 lines

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

Quoted 7 lines
xen/arch/x86/pv/emul-priv-op.c +3 -1
     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. */
         if ( is_pv_32bit_domain(currd) )
             *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE);

Wouldn't it be better then to also move the LMSLE hiding up?

Quoted 3 lines

Actually, on second consideration, LMSLE shouldn't be hidden. If LMSLE is actually active, then segment descriptors behave differently whether the guest knows about it or not.

Actually - the placement shouldn't matter at all: I think it can't be set anyway, as we would only possibly allow HVM guests to set it, i.e. the EFER value loaded during #VMEXIT won't ever have the bit set at present. And even the HVM side code uniformly forces cpu_has_lmsl to false right now.

Quoted 5 lines
Quoted 1 line

And what about SCE?

Rather more difficult to say, given its cross-vendor behaviour. It should probably be clobbered if 32bit && Intel, but 32bit AMD can use SYSCALL if it wishes.

Ah, yes, agreed.

Jan

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

#7 lore raw
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 07.05.18 at 09:30, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich

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

Quoted 13 lines
Quoted 1 line
Quoted 1 line · On 04.05.18 at 19:28, <[email protected]> wrote:
xen/arch/x86/pv/emul-priv-op.c +3 -1
@@ -867,7 +867,9 @@ 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. */
         if ( is_pv_32bit_domain(currd) )
             *val &= ~(EFER_LME | EFER_LMA | EFER_LMSLE);

Wouldn't it be better then to also move the LMSLE hiding up?

Quoted 7 lines
Quoted 3 lines

Actually, on second consideration, LMSLE shouldn't be hidden. If LMSLE is actually active, then segment descriptors behave differently whether the guest knows about it or not.

Actually - the placement shouldn't matter at all: I think it can't be set anyway, as we would only possibly allow HVM guests to set it, i.e. the EFER value loaded during #VMEXIT won't ever have the bit set at present. And even the HVM side code uniformly forces cpu_has_lmsl to false right now.

On 3rd consideration, the current placement is correct.  LMSLE, if set, only has any effect on data segment when running with a long mode %cs. Therefore, the clobber is in the right place.  Were we ever to enable it in Xen, this code would be correct.

On the HVM side, yes - we still have it disabled for migration reasons. That said, I think we should consider dropping it entirely.  Even AMD think it is an unused feature.

~Andrew

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