Skip to contentSkip to conversation

x86/svm: Fold nsvm_{wr, rd}msr() into svm_msr_{read, write}_intercept()

PATCH In review Andrew Cooper 3 participants 7 messages Jul 21, 2020 – Jul 22, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/svm: Fold nsvm_{wr, rd}msr() into svm_msr_{read, write}_intercept()

... to simplify the default cases.

There are multiple errors with the handling of these three MSRs, but they are deliberately not addressed this point.

This removes the dance converting -1/0/1 into X86EMUL_*, allowing for the removal of the 'ret' variable.

While cleaning this up, drop the gdprintk()'s for #GP conditions, and the 'result' variable from svm_msr_write_intercept() is it is never modified.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Wei Liu <[email protected]>
CC: Roger Pau Monné <[email protected]>

---
 xen/arch/x86/hvm/svm/nestedsvm.c        | 77 ---------------------------------
 xen/arch/x86/hvm/svm/svm.c              | 46 +++++++++++++-------
 xen/include/asm-x86/hvm/svm/nestedsvm.h |  4 --
 3 files changed, 31 insertions(+), 96 deletions(-)
xen/arch/x86/hvm/svm/nestedsvm.c +0 -77
index 11dc9c089c..a193d9de45 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);
@@ -1263,67 +1247,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 +31 -15

94 lines not on this page · raw message

xen/include/asm-x86/hvm/svm/nestedsvm.h +0 -4
index 31fb4bfeb4..0873698457 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);
Signature · 1 line
2.11.0
Reviewed-by on v1 1/1
#1 lore raw

Re: [PATCH] x86/svm: Fold nsvm_{wr,rd}msr() into svm_msr_{read,write}_intercept()

Quoted 8 lines · Andrew Cooper

... to simplify the default cases.

There are multiple errors with the handling of these three MSRs, but they are deliberately not addressed this point.

This removes the dance converting -1/0/1 into X86EMUL_*, allowing for the removal of the 'ret' variable.

Quoted 6 lines

While cleaning this up, drop the gdprintk()'s for #GP conditions, and the 'result' variable from svm_msr_write_intercept() is it is never modified.

No functional change.

However, ...

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 2099

Quoted 10 lines
xen/arch/x86/hvm/svm/svm.c +6 -0
@@ -2085,6 +2091,22 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
             goto gpf;
         break;
 
+    case MSR_K8_VM_CR:
+        /* ignore write. handle all bits as read-only. */
+        break;
+
+    case MSR_K8_VM_HSAVE_PA:
+        if ( (msr_content & ~PAGE_MASK) || msr_content > 0xfd00000000ULL )

... while you're moving this code here, wouldn't it be worthwhile to at least fix the > to be >= ?

Jan

#2 lore raw

Re: [PATCH] x86/svm: Fold nsvm_{wr,rd}msr() into svm_msr_{read,write}_intercept()

Quoted 23 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper

... to simplify the default cases.

There are multiple errors with the handling of these three MSRs, but they are deliberately not addressed this point.

This removes the dance converting -1/0/1 into X86EMUL_*, allowing for the removal of the 'ret' variable.

Quoted 6 lines

While cleaning this up, drop the gdprintk()'s for #GP conditions, and the 'result' variable from svm_msr_write_intercept() is it is never modified.

No functional change.

However, ...

Quoted 4 lines
xen/arch/x86/hvm/svm/svm.c +0 -0
@@ -2085,6 +2091,22 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
             goto gpf;
         break;
 
Quoted 8 lines

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 2099

Quoted 6 lines
xen/arch/x86/hvm/svm/svm.c +6 -0
+    case MSR_K8_VM_CR:
+        /* ignore write. handle all bits as read-only. */
+        break;
+
+    case MSR_K8_VM_HSAVE_PA:
+        if ( (msr_content & ~PAGE_MASK) || msr_content > 0xfd00000000ULL )

... while you're moving this code here, wouldn't it be worthwhile to at least fix the > to be >= ?

I'd prefer not to, to avoid breaking the "No Functional Change" aspect.

In reality, this needs to be a path which takes an extra ref on the nominated frame and globally maps it, seeing as we memcpy to/from it on every virtual vmentry/exit.  The check against the HT range is quite bogus.

~Andrew

#3 lore raw

Re: [PATCH] x86/svm: Fold nsvm_{wr,rd}msr() into svm_msr_{read,write}_intercept()

Quoted 33 lines · Andrew Cooper
Quoted 23 lines · Jan Beulich
Quoted 8 lines · Andrew Cooper

... to simplify the default cases.

There are multiple errors with the handling of these three MSRs, but they are deliberately not addressed this point.

This removes the dance converting -1/0/1 into X86EMUL_*, allowing for the removal of the 'ret' variable.

Quoted 6 lines

While cleaning this up, drop the gdprintk()'s for #GP conditions, and the 'result' variable from svm_msr_write_intercept() is it is never modified.

No functional change.

However, ...

Quoted 4 lines
xen/arch/x86/hvm/svm/svm.c +0 -0
@@ -2085,6 +2091,22 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
             goto gpf;
         break;
 
Quoted 8 lines

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 2099

Quoted 6 lines
xen/arch/x86/hvm/svm/svm.c +6 -0
+    case MSR_K8_VM_CR:
+        /* ignore write. handle all bits as read-only. */
+        break;
+
+    case MSR_K8_VM_HSAVE_PA:
+        if ( (msr_content & ~PAGE_MASK) || msr_content > 0xfd00000000ULL )

... while you're moving this code here, wouldn't it be worthwhile to at least fix the > to be >= ?

Quoted 1 line

I'd prefer not to, to avoid breaking the "No Functional Change" aspect.

Well, so be it then.

Quoted 3 lines

In reality, this needs to be a path which takes an extra ref on the nominated frame and globally maps it, seeing as we memcpy to/from it on every virtual vmentry/exit.  The check against the HT range is quite bogus.

I agree; I merely found the > so very obviously off-by-one that I thought I'd at least inquire.

Jan

Reviewed-by on v1 1/1
#4 lore raw

Re: [PATCH] x86/svm: Fold nsvm_{wr,rd}msr() into svm_msr_{read,write}_intercept()

In reply to Andrew Cooper
Quoted 4 lines · Andrew Cooper

... to simplify the default cases.

There are multiple errors with the handling of these three MSRs, but they are deliberately not addressed this point.

^ at

Quoted 6 lines

This removes the dance converting -1/0/1 into X86EMUL_*, allowing for the removal of the 'ret' variable.

While cleaning this up, drop the gdprintk()'s for #GP conditions, and the 'result' variable from svm_msr_write_intercept() is it is never modified.

^ extra is

Quoted 4 lines

No functional change.

I've got one question (not really patch related).

Quoted 13 lines
xen/arch/x86/hvm/svm/svm.c +1 -1
@@ -1956,10 +1962,10 @@ static int svm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
 
 static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
 {
-    int ret, result = X86EMUL_OKAY;
     struct vcpu *v = current;
     struct domain *d = v->domain;
     struct vmcb_struct *vmcb = v->arch.hvm.svm.vmcb;
+    struct nestedsvm *nsvm = &vcpu_nestedsvm(v);
 
     switch ( msr )
     {

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 2099

Quoted 9 lines
xen/arch/x86/hvm/svm/svm.c +6 -0
@@ -2085,6 +2091,22 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
             goto gpf;
         break;
 
+    case MSR_K8_VM_CR:
+        /* ignore write. handle all bits as read-only. */
+        break;
+
+    case MSR_K8_VM_HSAVE_PA:
+        if ( (msr_content & ~PAGE_MASK) || msr_content > 0xfd00000000ULL )

Regarding the address check, the PM states "the maximum supported physical address for this implementation", but I don't seem to be able to find where is this actually announced.

Roger.

#5 lore raw

Re: [PATCH] x86/svm: Fold nsvm_{wr,rd}msr() into svm_msr_{read,write}_intercept()

Quoted 5 lines · Roger Pau Monné
Quoted 4 lines · Andrew Cooper
xen/arch/x86/hvm/svm/svm.c +0 -0
@@ -2085,6 +2091,22 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
             goto gpf;
         break;
 
Quoted 10 lines

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 2099

Quoted 6 lines
xen/arch/x86/hvm/svm/svm.c +6 -0
+    case MSR_K8_VM_CR:
+        /* ignore write. handle all bits as read-only. */
+        break;
+
+    case MSR_K8_VM_HSAVE_PA:
+        if ( (msr_content & ~PAGE_MASK) || msr_content > 0xfd00000000ULL )

Regarding the address check, the PM states "the maximum supported physical address for this implementation", but I don't seem to be able to find where is this actually announced.

I think you'd typically find this information in the BKDG or PPR only. The PM is generic, while the named two are specific to particular families or even just models.

Jan

#6 lore raw

Re: [PATCH] x86/svm: Fold nsvm_{wr,rd}msr() into svm_msr_{read,write}_intercept()

Quoted 15 lines · Jan Beulich
Quoted 5 lines · Roger Pau Monné
Quoted 4 lines · Andrew Cooper
xen/arch/x86/hvm/svm/svm.c +0 -0
@@ -2085,6 +2091,22 @@ static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content)
             goto gpf;
         break;
 
Quoted 9 lines

xen/arch/x86/hvm/svm/svm.c static int svm_msr_write_intercept(unsigned int msr, uint64_t msr_content) · line 2099

Quoted 6 lines
xen/arch/x86/hvm/svm/svm.c +6 -0
+    case MSR_K8_VM_CR:
+        /* ignore write. handle all bits as read-only. */
+        break;
+
+    case MSR_K8_VM_HSAVE_PA:
+        if ( (msr_content & ~PAGE_MASK) || msr_content > 0xfd00000000ULL )

Regarding the address check, the PM states "the maximum supported physical address for this implementation", but I don't seem to be able to find where is this actually announced.

Quoted 3 lines

I think you'd typically find this information in the BKDG or PPR only. The PM is generic, while the named two are specific to particular families or even just models.

Furthermore, the BKDG/PPR's are misleading/wrong.

For pre Fam17h, it is MAXPHYSADDR - 12G, which gives a limit lower than 0xfd00000000 on various SoC and embedded platforms.

On Fam17h, it is also lowered dynamically by how much memory encryption is turned on (and therefore steals bits from the upper end of MAXPHYSADDR).

However, neither of these points are relevant in the slightest to nested-svm because we don't ever map the HyperTransport range into guests to start with - we'd get #PF[Rsvd] if we ever tried to use these mappings.

Last time I presented this patch (nearly 2 years ago, and in the middle of a series), it got very bogged down in a swamp of nested virt work, which is why this time I've gone for no functional change, and punting all the nested virt work to some future point where I've got time to deal with it, and its not blocking the improvement wanted here.

~Andrew