Skip to contentSkip to conversation

x86/svm: Fixes to nested-svm MSR handing

PATCH In review Andrew Cooper 3 participants 3 messages Dec 10, 2018 – Dec 11, 2018

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/svm: Fixes to nested-svm MSR handing

The intention of this patch was to remove the calls to nsvm_{rd,wr}msr() from the default cases of svm_msr_{read,write}_intercept(), but it has turned into a more corrective patch than just code motion.

First, collect the VM_CR bit definitions next to the main define, and simplify the naming. The SVM MSRs should be entirely unavailable when SVM isn't enumerated in CPUID.

When SVM is available, Xen only supports the "Enabled" mode as described in the BKGD/PPRs, which means VM_CR.LOCK should be set. This in turn means the MSR_SVM_LOCK_KEY should be implemented. It is read-as-0/write-discard as Xen doesn't implement the "disabled with user supplied key" mode.

The correct reset value for the HSAVE address is 0, not ~0. Xen doesn't use the guest frame for anything, and doesn't implement TSEG/ASEG/HyperTransport in the guest physical address space. Drop the arbitrary upper bounds check which appears to be an incorrect attempt to exclude the HT range below the 4G boundary, and accept any frame within MAXPHYSADDR. When we get a usable physmap layout in Xen, this should be restricted to RAM pages.

Remove the now-unused ret variables from the intercept functions, as well as the unnecessary result variable in svm_msr_write_intercept().

---
CC: Boris Ostrovsky <[email protected]>
CC: Suravee Suthikulpanit <[email protected]>
CC: Brian Woods <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/hvm/svm/nestedsvm.c        | 79 +------------------------------
 xen/arch/x86/hvm/svm/svm.c              | 82 +++++++++++++++++++++++++--------
 xen/include/asm-x86/hvm/svm/nestedsvm.h |  4 --
 xen/include/asm-x86/msr-index.h         | 13 +++---
 4 files changed, 72 insertions(+), 106 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +1 -78
index 9660202..6357bea 100644
@@ -47,22 +47,6 @@ nestedsvm_vcpu_stgi(struct vcpu *v)
     local_event_delivery_enable(); /* unmask events for PV drivers */
 }
 
-static int
-nestedsvm_vmcb_isvalid(struct vcpu *v, uint64_t vmcxaddr)
-{
-    /* Address must be 4k aligned */
-    if ( (vmcxaddr & ~PAGE_MASK) != 0 )
-        return 0;
-
-    /* Maximum valid physical address.
-     * See AMD BKDG for HSAVE_PA MSR.
-     */
-    if ( vmcxaddr > 0xfd00000000ULL )
-        return 0;
-
-    return 1;
-}
-
 int nestedsvm_vmcb_map(struct vcpu *v, uint64_t vmcbaddr)
 {
     struct nestedvcpu *nv = &vcpu_nestedhvm(v);
@@ -164,7 +148,7 @@ int nsvm_vcpu_reset(struct vcpu *v)
 {
     struct nestedsvm *svm = &vcpu_nestedsvm(v);
 
-    svm->ns_msr_hsavepa = INVALID_PADDR;
+    svm->ns_msr_hsavepa = 0;
     svm->ns_ovvmcb_pa = INVALID_PADDR;
 
     svm->ns_tscratio = DEFAULT_TSC_RATIO;
@@ -1267,67 +1251,6 @@ enum hvm_intblk nsvm_intr_blocked(struct vcpu *v)
     return hvm_intblk_none;
 }
 
-/* MSR handling */
-int nsvm_rdmsr(struct vcpu *v, unsigned int msr, uint64_t *msr_content)
-{
-    struct nestedsvm *svm = &vcpu_nestedsvm(v);
-    int ret = 1;
-
-    *msr_content = 0;
-
-    switch (msr) {
-    case MSR_K8_VM_CR:
-        break;
-    case MSR_K8_VM_HSAVE_PA:
-        *msr_content = svm->ns_msr_hsavepa;
-        break;
-    case MSR_AMD64_TSC_RATIO:
-        *msr_content = svm->ns_tscratio;
-        break;
-    default:
-        ret = 0;
-        break;
-    }
-
-    return ret;
-}
-
-int nsvm_wrmsr(struct vcpu *v, unsigned int msr, uint64_t msr_content)
-{
-    int ret = 1;
-    struct nestedsvm *svm = &vcpu_nestedsvm(v);
-
-    switch (msr) {
-    case MSR_K8_VM_CR:
-        /* ignore write. handle all bits as read-only. */
-        break;
-    case MSR_K8_VM_HSAVE_PA:
-        if (!nestedsvm_vmcb_isvalid(v, msr_content)) {
-            gdprintk(XENLOG_ERR,
-                "MSR_K8_VM_HSAVE_PA value invalid %#"PRIx64"\n", msr_content);
-            ret = -1; /* inject #GP */
-            break;
-        }
-        svm->ns_msr_hsavepa = msr_content;
-        break;
-    case MSR_AMD64_TSC_RATIO:
-        if ((msr_content & ~TSC_RATIO_RSVD_BITS) != msr_content) {
-            gdprintk(XENLOG_ERR,
-                "reserved bits set in MSR_AMD64_TSC_RATIO %#"PRIx64"\n",
-                msr_content);
-            ret = -1; /* inject #GP */
-            break;
-        }
-        svm->ns_tscratio = msr_content;
-        break;
-    default:
-        ret = 0;
-        break;
-    }
-
-    return ret;
-}
-
 /* VMEXIT emulation */
 void
 nestedsvm_vmexit_defer(struct vcpu *v,
xen/arch/x86/hvm/svm/svm.c +64 -18

142 lines not on this page · raw message

xen/include/asm-x86/hvm/svm/nestedsvm.h +0 -4
index 31fb4bf..0873698 100644
@@ -118,10 +118,6 @@ bool_t nsvm_vmcb_guest_intercepts_event(
 bool_t nsvm_vmcb_hap_enabled(struct vcpu *v);
 enum hvm_intblk nsvm_intr_blocked(struct vcpu *v);
 
-/* MSRs */
-int nsvm_rdmsr(struct vcpu *v, unsigned int msr, uint64_t *msr_content);
-int nsvm_wrmsr(struct vcpu *v, unsigned int msr, uint64_t msr_content);
-
 /* Interrupts, vGIF */
 void svm_vmexit_do_clgi(struct cpu_user_regs *regs, struct vcpu *v);
 void svm_vmexit_do_stgi(struct cpu_user_regs *regs, struct vcpu *v);
xen/include/asm-x86/msr-index.h +7 -6
index 24d783a..4f43a16 100644
@@ -199,8 +199,13 @@
 #define MSR_K8_PSTATE6			0xc001006A
 #define MSR_K8_PSTATE7			0xc001006B
 #define MSR_K8_ENABLE_C1E		0xc0010055
-#define MSR_K8_VM_CR			0xc0010114
-#define MSR_K8_VM_HSAVE_PA		0xc0010117
+
+#define MSR_VM_CR                       0xc0010114
+#define  VM_CR_LOCK                     (_AC(1, ULL) << 3)
+#define  VM_CR_SVME_DISABLE             (_AC(1, ULL) << 4)
+
+#define MSR_VM_HSAVE_ADDR               0xc0010117
+#define MSR_SVM_LOCK_KEY                0xc0010118
 
 #define MSR_AMD_FAM15H_EVNTSEL0		0xc0010200
 #define MSR_AMD_FAM15H_PERFCTR0		0xc0010201
@@ -220,10 +225,6 @@
 #define MSR_K8_FEATURE_MASK		0xc0011004
 #define MSR_K8_EXT_FEATURE_MASK		0xc0011005
 
-/* MSR_K8_VM_CR bits: */
-#define _K8_VMCR_SVME_DISABLE		4
-#define K8_VMCR_SVME_DISABLE		(1 << _K8_VMCR_SVME_DISABLE)
-
 /* AMD64 MSRs */
 #define MSR_AMD64_NB_CFG		0xc001001f
 #define AMD64_NB_CFG_CF8_EXT_ENABLE_BIT	46
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 4 lines · Andrew Cooper

The intention of this patch was to remove the calls to nsvm_{rd,wr}msr() from the default cases of svm_msr_{read,write}_intercept(), but it has turned into a more corrective patch than just code motion.

Quoted 8 lines

First, collect the VM_CR bit definitions next to the main define, and simplify the naming. The SVM MSRs should be entirely unavailable when SVM isn't enumerated in CPUID.

When SVM is available, Xen only supports the "Enabled" mode as described in the BKGD/PPRs, which means VM_CR.LOCK should be set. This in turn means the MSR_SVM_LOCK_KEY should be implemented. It is read-as-0/write-discard as Xen doesn't implement the "disabled with user supplied key" mode.

What about bits 0-2? I am not sure what a guest can do with them but they are writable.

-boris

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

Acked-by on v1 1/1
#2 lore raw
In reply to Andrew Cooper
Quoted 21 lines
Quoted 1 line
Quoted 1 line · On 10.12.18 at 12:52, <[email protected]> wrote:

The intention of this patch was to remove the calls to nsvm_{rd,wr}msr() from the default cases of svm_msr_{read,write}_intercept(), but it has turned into a more corrective patch than just code motion.

First, collect the VM_CR bit definitions next to the main define, and simplify the naming. The SVM MSRs should be entirely unavailable when SVM isn't enumerated in CPUID.

When SVM is available, Xen only supports the "Enabled" mode as described in the BKGD/PPRs, which means VM_CR.LOCK should be set. This in turn means the MSR_SVM_LOCK_KEY should be implemented. It is read-as-0/write-discard as Xen doesn't implement the "disabled with user supplied key" mode.

The correct reset value for the HSAVE address is 0, not ~0. Xen doesn't use the guest frame for anything, and doesn't implement TSEG/ASEG/HyperTransport in the guest physical address space. Drop the arbitrary upper bounds check which appears to be an incorrect attempt to exclude the HT range below the 4G boundary, and accept any frame within MAXPHYSADDR. When we get a usable physmap layout in Xen, this should be restricted to RAM pages.

Quoted 4 lines

Remove the now-unused ret variables from the intercept functions, as well as the unnecessary result variable in svm_msr_write_intercept().

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