Skip to contentSkip to conversation

PVH MTRR initial stat

PATCH Superseded Roger Pau Monne 3 participants 19 messages May 15, 2018 – May 16, 2018

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover PVH MTRR initial stat

Hello,

The following patches set a sane initial MTRR state for both Dom0 and DomU PVH guests. Note that for Dom0 the host MTRR state is used, OTOH for DomU the default MTRR type is set to write-back.

This should avoid guests having to setup some kind of MTRR state in order to boot.

Thanks, Roger.

Roger Pau Monne (6):
hvm/mtrr: add emacs local variables block with formatting info
mtrr: introduce mask to get VCNT from MTRRcap MSR hvm/mtrr: use the hardware number of variable ranges for Dom0 hvm/mtrr: copy hardware state for Dom0
libxc/pvh: set default MTRR type to write-back docs/pvh: document initial MTRR state

 docs/misc/pvh.markdown          | 18 ++++++++
 tools/libxc/xc_dom_x86.c        | 44 ++++++++++++++++++++
 xen/arch/x86/cpu/mtrr/main.c    |  2 +-
 xen/arch/x86/hvm/hvm.c          |  7 +++-
 xen/arch/x86/hvm/mtrr.c         | 74 +++++++++++++++++++++++++++++----
 xen/include/asm-x86/msr-index.h |  4 ++
 xen/include/asm-x86/mtrr.h      |  2 +
 7 files changed, 141 insertions(+), 10 deletions(-)
Signature · 7 lines
2.17.0


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

PATCH v2 1/6 hvm/mtrr: add emacs local variables block with formatting info

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

---
 xen/arch/x86/hvm/mtrr.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
xen/arch/x86/hvm/mtrr.c +10 -0
index b721c6330f..b3c08c3977 100644
@@ -853,3 +853,13 @@ int epte_get_entry_emt(struct domain *d, unsigned long gfn, mfn_t mfn,
 
     return MTRR_TYPE_UNCACHABLE;
 }
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
Signature · 7 lines
2.17.0


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

PATCH v2 2/6 mtrr: introduce mask to get VCNT from MTRRcap MSR

In reply to Roger Pau Monne

No functional change.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

---
 xen/arch/x86/cpu/mtrr/main.c    | 2 +-
 xen/arch/x86/hvm/mtrr.c         | 6 +++---
 xen/include/asm-x86/msr-index.h | 2 ++
 3 files changed, 6 insertions(+), 4 deletions(-)
xen/arch/x86/cpu/mtrr/main.c +1 -1
index 56f71a6e1f..e9df53f00d 100644
@@ -95,7 +95,7 @@ static void __init set_num_var_ranges(void)
 		config = 2;
 	else if (is_cpu(CENTAUR))
 		config = 8;
-	num_var_ranges = config & 0xff;
+	num_var_ranges = MASK_EXTR(config, MTRRcap_VCNT);
 }
 
 static void __init init_table(void)
xen/arch/x86/hvm/mtrr.c +3 -3
index b3c08c3977..bdff56a912 100644
@@ -78,7 +78,7 @@ static uint8_t __read_mostly pat_entry_tbl[PAT_TYPE_NUMS] =
 bool_t is_var_mtrr_overlapped(const struct mtrr_state *m)
 {
     unsigned int seg, i;
-    unsigned int num_var_ranges = (uint8_t)m->mtrr_cap;
+    unsigned int num_var_ranges = MASK_EXTR(m->mtrr_cap, MTRRcap_VCNT);
 
     for ( i = 0; i < num_var_ranges; i++ )
     {
@@ -193,7 +193,7 @@ static int get_mtrr_type(const struct mtrr_state *m,
    uint8_t     overlap_mtrr = 0;
    uint8_t     overlap_mtrr_pos = 0;
    uint64_t    mask = -(uint64_t)PAGE_SIZE << order;
-   unsigned int seg, num_var_ranges = m->mtrr_cap & 0xff;
+   unsigned int seg, num_var_ranges = MASK_EXTR(m->mtrr_cap, MTRRcap_VCNT);
 
    if ( unlikely(!(m->enabled & 0x2)) )
        return MTRR_TYPE_UNCACHABLE;
@@ -478,7 +478,7 @@ bool_t mtrr_pat_not_equal(struct vcpu *vd, struct vcpu *vs)
     struct mtrr_state *md = &vd->arch.hvm_vcpu.mtrr;
     struct mtrr_state *ms = &vs->arch.hvm_vcpu.mtrr;
     int32_t res;
-    uint8_t num_var_ranges = (uint8_t)md->mtrr_cap;
+    uint8_t num_var_ranges = MASK_EXTR(md->mtrr_cap, MTRRcap_VCNT);
 
     /* Test fixed ranges. */
     res = memcmp(md->fixed_ranges, ms->fixed_ranges,
xen/include/asm-x86/msr-index.h +2 -0
index 6d94d65575..f385fbdc73 100644
@@ -58,6 +58,8 @@
 #define ATM_LNC_C6_AUTO_DEMOTE		(1UL << 25)
 
 #define MSR_MTRRcap			0x000000fe
+#define MTRRcap_VCNT			0x000000ff
+
 #define MSR_IA32_BBL_CR_CTL		0x00000119
 
 #define MSR_IA32_SYSENTER_CS		0x00000174
Signature · 7 lines
2.17.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Reviewed-by on v2 2/6
#3 lore raw

Re: [PATCH v2 2/6] mtrr: introduce mask to get VCNT from MTRRcap MSR

xen/arch/x86/hvm/mtrr.c bool_t mtrr_pat_not_equal(struct vcpu *vd, struct vcpu *vs) · line 481

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:36, <[email protected]> wrote:
xen/arch/x86/hvm/mtrr.c +1 -1
@@ -478,7 +478,7 @@ bool_t mtrr_pat_not_equal(struct vcpu *vd, struct vcpu *vs)
     struct mtrr_state *md = &vd->arch.hvm_vcpu.mtrr;
     struct mtrr_state *ms = &vs->arch.hvm_vcpu.mtrr;
     int32_t res;
-    uint8_t num_var_ranges = (uint8_t)md->mtrr_cap;
+    uint8_t num_var_ranges = MASK_EXTR(md->mtrr_cap, MTRRcap_VCNT);

With the type changed to unsigned int here

Of course this is easy enough to do while committing, should no other need for a v3 arise.

Jan

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

#4 lore raw

PATCH v2 3/6 hvm/mtrr: use the hardware number of variable ranges for Dom0

In reply to Roger Pau Monne

Expand the size of the variable ranges array to match the size of the underlying hardware, this is a preparatory change for copying the hardware MTRR state for Dom0.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
Changes since v1:
- Fix hvm_msr_{read,write}_intercept().
- Relax the checks in hvm_{save/load}_mtrr_msr.

---
 xen/arch/x86/hvm/hvm.c     |  7 +++++--
 xen/arch/x86/hvm/mtrr.c    | 34 ++++++++++++++++++++++++++++++----
 xen/include/asm-x86/mtrr.h |  2 ++
 3 files changed, 37 insertions(+), 6 deletions(-)
xen/arch/x86/hvm/hvm.c +5 -2
index c23983cdff..8b30c93ec6 100644
@@ -3489,10 +3489,13 @@ int hvm_msr_read_intercept(unsigned int msr, uint64_t *msr_content)
         index = msr - MSR_MTRRfix4K_C0000;
         *msr_content = fixed_range_base[index + 3];
         break;
-    case MSR_IA32_MTRR_PHYSBASE(0)...MSR_IA32_MTRR_PHYSMASK(MTRR_VCNT-1):
+    case MSR_IA32_MTRR_PHYSBASE(0)...MSR_IA32_MTRR_PHYSMASK(MTRR_VCNT_MAX - 1):
         if ( !d->arch.cpuid->basic.mtrr )
             goto gp_fault;
         index = msr - MSR_IA32_MTRR_PHYSBASE(0);
+        if ( (index / 2) >=
+             MASK_EXTR(v->arch.hvm_vcpu.mtrr.mtrr_cap, MTRRcap_VCNT) )
+            goto gp_fault;
         *msr_content = var_range_base[index];
         break;
 
@@ -3650,7 +3653,7 @@ int hvm_msr_write_intercept(unsigned int msr, uint64_t msr_content,
                                      index, msr_content) )
             goto gp_fault;
         break;
-    case MSR_IA32_MTRR_PHYSBASE(0)...MSR_IA32_MTRR_PHYSMASK(MTRR_VCNT-1):
+    case MSR_IA32_MTRR_PHYSBASE(0)...MSR_IA32_MTRR_PHYSMASK(MTRR_VCNT_MAX - 1):
         if ( !d->arch.cpuid->basic.mtrr )
             goto gp_fault;
         if ( !mtrr_var_range_msr_set(v->domain, &v->arch.hvm_vcpu.mtrr,
xen/arch/x86/hvm/mtrr.c +30 -4
index bdff56a912..e71f428a3d 100644
@@ -154,14 +154,26 @@ uint8_t pat_type_2_pte_flags(uint8_t pat_type)
 int hvm_vcpu_cacheattr_init(struct vcpu *v)
 {
     struct mtrr_state *m = &v->arch.hvm_vcpu.mtrr;
+    unsigned int num_var_ranges =
+        is_hardware_domain(v->domain) ? MASK_EXTR(mtrr_state.mtrr_cap,
+                                                  MTRRcap_VCNT)
+                                      : MTRR_VCNT;
+
+    if ( num_var_ranges > MTRR_VCNT_MAX )
+    {
+        ASSERT(is_hardware_domain(v->domain));
+        printk("WARNING: limited Dom0 variable range MTRRs from %u to %u\n",
+               num_var_ranges, MTRR_VCNT_MAX);
+        num_var_ranges = MTRR_VCNT_MAX;
+    }
 
     memset(m, 0, sizeof(*m));
 
-    m->var_ranges = xzalloc_array(struct mtrr_var_range, MTRR_VCNT);
+    m->var_ranges = xzalloc_array(struct mtrr_var_range, num_var_ranges);
     if ( m->var_ranges == NULL )
         return -ENOMEM;
 
-    m->mtrr_cap = (1u << 10) | (1u << 8) | MTRR_VCNT;
+    m->mtrr_cap = (1u << 10) | (1u << 8) | num_var_ranges;
 
     v->arch.hvm_vcpu.pat_cr =
         ((uint64_t)PAT_TYPE_WRBACK) |               /* PAT0: WB */
@@ -445,6 +457,9 @@ bool_t mtrr_var_range_msr_set(
     uint64_t *var_range_base = (uint64_t*)m->var_ranges;
 
     index = msr - MSR_IA32_MTRR_PHYSBASE(0);
+    if ( (index / 2) >= MASK_EXTR(m->mtrr_cap, MTRRcap_VCNT) )
+        return 0;
+
     if ( var_range_base[index] == msr_content )
         return 1;
 
@@ -675,6 +690,8 @@ static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
     /* save mtrr&pat */
     for_each_vcpu(d, v)
     {
+        unsigned int num_var_ranges;
+
         mtrr_state = &v->arch.hvm_vcpu.mtrr;
 
         hvm_get_guest_pat(v, &hw_mtrr.msr_pat_cr);
@@ -683,7 +700,11 @@ static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
                                 | (mtrr_state->enabled << 10);
         hw_mtrr.msr_mtrr_cap = mtrr_state->mtrr_cap;
 
-        for ( i = 0; i < MTRR_VCNT; i++ )
+        num_var_ranges = MASK_EXTR(mtrr_state->mtrr_cap, MTRRcap_VCNT);
+        if ( num_var_ranges > MTRR_VCNT )
+            return -EINVAL;
+
+        for ( i = 0; i < num_var_ranges; i++ )
         {
             /* save physbase */
             hw_mtrr.msr_mtrr_var[i*2] =
@@ -709,6 +730,7 @@ static int hvm_load_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
     struct vcpu *v;
     struct mtrr_state *mtrr_state;
     struct hvm_hw_mtrr hw_mtrr;
+    unsigned int num_var_ranges;
 
     vcpuid = hvm_load_instance(h);
     if ( vcpuid >= d->max_vcpus || (v = d->vcpu[vcpuid]) == NULL )
@@ -727,10 +749,14 @@ static int hvm_load_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
 
     mtrr_state->mtrr_cap = hw_mtrr.msr_mtrr_cap;
 
+    num_var_ranges = MASK_EXTR(mtrr_state->mtrr_cap, MTRRcap_VCNT);
+    if ( num_var_ranges > MTRR_VCNT )
+        return -EINVAL;
+
     for ( i = 0; i < NUM_FIXED_MSR; i++ )
         mtrr_fix_range_msr_set(d, mtrr_state, i, hw_mtrr.msr_mtrr_fixed[i]);
 
-    for ( i = 0; i < MTRR_VCNT; i++ )
+    for ( i = 0; i < num_var_ranges; i++ )
     {
         mtrr_var_range_msr_set(d, mtrr_state,
                                MSR_IA32_MTRR_PHYSBASE(i),
xen/include/asm-x86/mtrr.h +2 -0
index 69cf68cf7b..e44c3c28bd 100644
@@ -39,6 +39,8 @@ typedef u8 mtrr_type;
 #define MTRR_PHYSBASE_SHIFT      12
 /* Number of variable range MSR pairs we emulate for HVM guests: */
 #define MTRR_VCNT                8
+/* Maximum number of variable range MSR pairs if FE is supported. */
+#define MTRR_VCNT_MAX            40
 
 struct mtrr_var_range {
 	uint64_t base;
Signature · 7 lines
2.17.0


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

Re: [PATCH v2 3/6] hvm/mtrr: use the hardware number of variable ranges for Dom0

Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:36, <[email protected]> wrote:
xen/arch/x86/hvm/mtrr.c +0 -0
@@ -154,14 +154,26 @@ uint8_t pat_type_2_pte_flags(uint8_t pat_type)
 int hvm_vcpu_cacheattr_init(struct vcpu *v)
 {
     struct mtrr_state *m = &v->arch.hvm_vcpu.mtrr;

xen/arch/x86/hvm/mtrr.c uint8_t pat_type_2_pte_flags(uint8_t pat_type) · line 166

Quoted 10 lines
xen/arch/x86/hvm/mtrr.c +10 -0
+    unsigned int num_var_ranges =
+        is_hardware_domain(v->domain) ? MASK_EXTR(mtrr_state.mtrr_cap,
+                                                  MTRRcap_VCNT)
+                                      : MTRR_VCNT;
+
+    if ( num_var_ranges > MTRR_VCNT_MAX )
+    {
+        ASSERT(is_hardware_domain(v->domain));
+        printk("WARNING: limited Dom0 variable range MTRRs from %u to %u\n",
+               num_var_ranges, MTRR_VCNT_MAX);

Please log v->domain->domain_id here instead of hard coded Dom0, to cope with the hardware domain being different from Dom0.

Quoted 10 lines
xen/arch/x86/hvm/mtrr.c +2 -0
@@ -675,6 +690,8 @@ static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
     /* save mtrr&pat */
     for_each_vcpu(d, v)
     {
+        unsigned int num_var_ranges;
+
         mtrr_state = &v->arch.hvm_vcpu.mtrr;
 
         hvm_get_guest_pat(v, &hw_mtrr.msr_pat_cr);

xen/arch/x86/hvm/mtrr.c static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h) · line 705

Quoted 7 lines
xen/arch/x86/hvm/mtrr.c +3 -1
@@ -683,7 +700,11 @@ static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
                                 | (mtrr_state->enabled << 10);
         hw_mtrr.msr_mtrr_cap = mtrr_state->mtrr_cap;
 
-        for ( i = 0; i < MTRR_VCNT; i++ )
+        num_var_ranges = MASK_EXTR(mtrr_state->mtrr_cap, MTRRcap_VCNT);
+        if ( num_var_ranges > MTRR_VCNT )
+            return -EINVAL;

This would better be accompanied by a dprintk().

xen/arch/x86/hvm/mtrr.c static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h) · line 706

Quoted 1 line
xen/arch/x86/hvm/mtrr.c +1 -0
+        for ( i = 0; i < num_var_ranges; i++ )

Following your v1 I had already put together a patch to change just the save and load functions here, as the adjustments are necessary independent of the Dom0 aspect. Should num_var_ranges indeed be below MTRR_VCNT, there's an information leak here (of hypervisor stack data) without pre-initializing hw_mtrr. Here's the hunk from my patch, in case you care to re-use parts of it:

@@ -676,22 +676,22 @@ int hvm_set_mem_pinned_cacheattr(struct

static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h) {

-    int i;
     struct vcpu *v;
-    struct hvm_hw_mtrr hw_mtrr;
-    struct mtrr_state *mtrr_state;
+
     /* save mtrr&pat */
     for_each_vcpu(d, v)
     {
-        mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        const struct mtrr_state *mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        struct hvm_hw_mtrr hw_mtrr = {

+ .msr_mtrr_def_type = mtrr_state->def_type |
+ (mtrr_state->enabled << 10),
+ .msr_mtrr_cap = mtrr_state->mtrr_cap,
+ };
+ unsigned int i;

         hvm_get_guest_pat(v, &hw_mtrr.msr_pat_cr);

- hw_mtrr.msr_mtrr_def_type = mtrr_state->def_type
- | (mtrr_state->enabled << 10);
- hw_mtrr.msr_mtrr_cap = mtrr_state->mtrr_cap;
-
- for ( i = 0; i < MTRR_VCNT; i++ )
+ for ( i = 0; i < (uint8_t)hw_mtrr.msr_mtrr_cap; i++ )

         {
             /* save physbase */
             hw_mtrr.msr_mtrr_var[i*2] =

(I didn't send it out yet as I'm generally of the opinion that prior to branching focus should be on the code to be released rather than the next following version.)

xen/include/asm-x86/mtrr.h typedef u8 mtrr_type; · line 43

Quoted 8 lines
xen/include/asm-x86/mtrr.h +2 -0
@@ -39,6 +39,8 @@ typedef u8 mtrr_type;
 #define MTRR_PHYSBASE_SHIFT      12
 /* Number of variable range MSR pairs we emulate for HVM guests: */
 #define MTRR_VCNT                8
+/* Maximum number of variable range MSR pairs if FE is supported. */
+#define MTRR_VCNT_MAX            40

((MSR_MTRRfix64K_00000 - MSR_IA32_MTRR_PHYSBASE(0)) / 2)

or some such please (to make obvious where the value is coming from).

Jan

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

#6 lore raw

Re: [PATCH v2 3/6] hvm/mtrr: use the hardware number of variable ranges for Dom0

Quoted 43 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:36, <[email protected]> wrote:

+ for ( i = 0; i < num_var_ranges; i++ )

Following your v1 I had already put together a patch to change just the save and load functions here, as the adjustments are necessary independent of the Dom0 aspect. Should num_var_ranges indeed be below MTRR_VCNT, there's an information leak here (of hypervisor stack data) without pre-initializing hw_mtrr. Here's the hunk from my patch, in case you care to re-use parts of it:

(quoted diff) +9 -9
@@ -676,22 +676,22 @@ int hvm_set_mem_pinned_cacheattr(struct
 
 static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
 {
-    int i;
     struct vcpu *v;
-    struct hvm_hw_mtrr hw_mtrr;
-    struct mtrr_state *mtrr_state;
+
     /* save mtrr&pat */
     for_each_vcpu(d, v)
     {
-        mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        const struct mtrr_state *mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        struct hvm_hw_mtrr hw_mtrr = {
+            .msr_mtrr_def_type = mtrr_state->def_type |
+                                 (mtrr_state->enabled << 10),
+            .msr_mtrr_cap      = mtrr_state->mtrr_cap,
+        };
+        unsigned int i;
 
         hvm_get_guest_pat(v, &hw_mtrr.msr_pat_cr);
 
-        hw_mtrr.msr_mtrr_def_type = mtrr_state->def_type
-                                | (mtrr_state->enabled << 10);
-        hw_mtrr.msr_mtrr_cap = mtrr_state->mtrr_cap;
-
-        for ( i = 0; i < MTRR_VCNT; i++ )
+        for ( i = 0; i < (uint8_t)hw_mtrr.msr_mtrr_cap; i++ )
         {
             /* save physbase */
             hw_mtrr.msr_mtrr_var[i*2] =
Quoted 3 lines

(I didn't send it out yet as I'm generally of the opinion that prior to branching focus should be on the code to be released rather than the next following version.)

Would you be OK if I integrate this as a pre-patch to this one in my series?

Thanks, Roger.

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

#7 lore raw

Re: [PATCH v2 3/6] hvm/mtrr: use the hardware number of variable ranges for Dom0

Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 16.05.18 at 13:53, <[email protected]> wrote:
Quoted 43 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:36, <[email protected]> wrote:

+ for ( i = 0; i < num_var_ranges; i++ )

Following your v1 I had already put together a patch to change just the save and load functions here, as the adjustments are necessary independent of the Dom0 aspect. Should num_var_ranges indeed be below MTRR_VCNT, there's an information leak here (of hypervisor stack data) without pre-initializing hw_mtrr. Here's the hunk from my patch, in case you care to re-use parts of it:

(quoted diff) +9 -9
@@ -676,22 +676,22 @@ int hvm_set_mem_pinned_cacheattr(struct
 
 static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
 {
-    int i;
     struct vcpu *v;
-    struct hvm_hw_mtrr hw_mtrr;
-    struct mtrr_state *mtrr_state;
+
     /* save mtrr&pat */
     for_each_vcpu(d, v)
     {
-        mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        const struct mtrr_state *mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        struct hvm_hw_mtrr hw_mtrr = {
+            .msr_mtrr_def_type = mtrr_state->def_type |
+                                 (mtrr_state->enabled << 10),
+            .msr_mtrr_cap      = mtrr_state->mtrr_cap,
+        };
+        unsigned int i;
 
         hvm_get_guest_pat(v, &hw_mtrr.msr_pat_cr);
 
-        hw_mtrr.msr_mtrr_def_type = mtrr_state->def_type
-                                | (mtrr_state->enabled << 10);
-        hw_mtrr.msr_mtrr_cap = mtrr_state->mtrr_cap;
-
-        for ( i = 0; i < MTRR_VCNT; i++ )
+        for ( i = 0; i < (uint8_t)hw_mtrr.msr_mtrr_cap; i++ )
         {
             /* save physbase */
             hw_mtrr.msr_mtrr_var[i*2] =
Quoted 6 lines
Quoted 3 lines

(I didn't send it out yet as I'm generally of the opinion that prior to branching focus should be on the code to be released rather than the next following version.)

Would you be OK if I integrate this as a pre-patch to this one in my series?

Sure, but then maybe better use the full one:

x86/HVM: improve MTRR load checks

We should not assume that the incoming set of values contains exactly MTRR_VCNT variable range MSRs. Permit a smaller amount and reject a bigger one. As a result the save path then also needs to no longer use a fixed upper bound, in turn requiring unused space in the save record to be zeroed up front.

Also slightly refine types where appropriate.

xen/arch/x86/hvm/mtrr.c +18 -10
@@ -676,22 +676,22 @@ int hvm_set_mem_pinned_cacheattr(struct
 
 static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
 {
-    int i;
     struct vcpu *v;
-    struct hvm_hw_mtrr hw_mtrr;
-    struct mtrr_state *mtrr_state;
+
     /* save mtrr&pat */
     for_each_vcpu(d, v)
     {
-        mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        const struct mtrr_state *mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        struct hvm_hw_mtrr hw_mtrr = {
+            .msr_mtrr_def_type = mtrr_state->def_type |
+                                 (mtrr_state->enabled << 10),
+            .msr_mtrr_cap      = mtrr_state->mtrr_cap,
+        };
+        unsigned int i;
 
         hvm_get_guest_pat(v, &hw_mtrr.msr_pat_cr);
 
-        hw_mtrr.msr_mtrr_def_type = mtrr_state->def_type
-                                | (mtrr_state->enabled << 10);
-        hw_mtrr.msr_mtrr_cap = mtrr_state->mtrr_cap;
-
-        for ( i = 0; i < MTRR_VCNT; i++ )
+        for ( i = 0; i < (uint8_t)hw_mtrr.msr_mtrr_cap; i++ )
         {
             /* save physbase */
             hw_mtrr.msr_mtrr_var[i*2] =
@@ -729,6 +729,14 @@ static int hvm_load_mtrr_msr(struct doma
     if ( hvm_load_entry(MTRR, h, &hw_mtrr) != 0 )
         return -EINVAL;
 
+    if ( (uint8_t)hw_mtrr.msr_mtrr_cap > MTRR_VCNT )
+    {
+        dprintk(XENLOG_G_ERR,
+                "HVM restore: %pv: too many (%d) variable range MTRRs\n",
+                v, (uint8_t)hw_mtrr.msr_mtrr_cap);
+        return -EINVAL;
+    }
+
     mtrr_state = &v->arch.hvm_vcpu.mtrr;
 
     hvm_set_guest_pat(v, hw_mtrr.msr_pat_cr);
@@ -738,7 +746,7 @@ static int hvm_load_mtrr_msr(struct doma
     for ( i = 0; i < NUM_FIXED_MSR; i++ )
         mtrr_fix_range_msr_set(d, mtrr_state, i, hw_mtrr.msr_mtrr_fixed[i]);
 
-    for ( i = 0; i < MTRR_VCNT; i++ )
+    for ( i = 0; i < (uint8_t)hw_mtrr.msr_mtrr_cap; i++ )
     {
         mtrr_var_range_msr_set(d, mtrr_state,
                                MSR_IA32_MTRR_PHYSBASE(i),

Jan

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

#8 lore raw

Re: [PATCH v2 3/6] hvm/mtrr: use the hardware number of variable ranges for Dom0

Quoted 45 lines · Jan Beulich
Quoted 45 lines
Quoted 1 line
Quoted 1 line · On 16.05.18 at 13:53, <[email protected]> wrote:
Quoted 43 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:36, <[email protected]> wrote:

+ for ( i = 0; i < num_var_ranges; i++ )

Following your v1 I had already put together a patch to change just the save and load functions here, as the adjustments are necessary independent of the Dom0 aspect. Should num_var_ranges indeed be below MTRR_VCNT, there's an information leak here (of hypervisor stack data) without pre-initializing hw_mtrr. Here's the hunk from my patch, in case you care to re-use parts of it:

xen/arch/x86/hvm/mtrr.c +9 -9
@@ -676,22 +676,22 @@ int hvm_set_mem_pinned_cacheattr(struct
 
 static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
 {
-    int i;
     struct vcpu *v;
-    struct hvm_hw_mtrr hw_mtrr;
-    struct mtrr_state *mtrr_state;
+
     /* save mtrr&pat */
     for_each_vcpu(d, v)
     {
-        mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        const struct mtrr_state *mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        struct hvm_hw_mtrr hw_mtrr = {
+            .msr_mtrr_def_type = mtrr_state->def_type |
+                                 (mtrr_state->enabled << 10),
+            .msr_mtrr_cap      = mtrr_state->mtrr_cap,
+        };
+        unsigned int i;
 
         hvm_get_guest_pat(v, &hw_mtrr.msr_pat_cr);
 
-        hw_mtrr.msr_mtrr_def_type = mtrr_state->def_type
-                                | (mtrr_state->enabled << 10);
-        hw_mtrr.msr_mtrr_cap = mtrr_state->mtrr_cap;
-
-        for ( i = 0; i < MTRR_VCNT; i++ )
+        for ( i = 0; i < (uint8_t)hw_mtrr.msr_mtrr_cap; i++ )
         {
             /* save physbase */
             hw_mtrr.msr_mtrr_var[i*2] =
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

(I didn't send it out yet as I'm generally of the opinion that prior to branching focus should be on the code to be released rather than the next following version.)

Would you be OK if I integrate this as a pre-patch to this one in my series?

Sure, but then maybe better use the full one:

Thanks, I've added this to my MTRR series just after the switch to use MASK_EXTR to get the VCNT.

Quoted 33 lines

x86/HVM: improve MTRR load checks

We should not assume that the incoming set of values contains exactly MTRR_VCNT variable range MSRs. Permit a smaller amount and reject a bigger one. As a result the save path then also needs to no longer use a fixed upper bound, in turn requiring unused space in the save record to be zeroed up front.

Also slightly refine types where appropriate.

xen/arch/x86/hvm/mtrr.c +7 -4
@@ -676,22 +676,22 @@ int hvm_set_mem_pinned_cacheattr(struct
 
 static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
 {
-    int i;
     struct vcpu *v;
-    struct hvm_hw_mtrr hw_mtrr;
-    struct mtrr_state *mtrr_state;
+
     /* save mtrr&pat */
     for_each_vcpu(d, v)
     {
-        mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        const struct mtrr_state *mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        struct hvm_hw_mtrr hw_mtrr = {
+            .msr_mtrr_def_type = mtrr_state->def_type |
+                                 (mtrr_state->enabled << 10),
+            .msr_mtrr_cap      = mtrr_state->mtrr_cap,
+        };

xen/arch/x86/hvm/mtrr.c int hvm_set_mem_pinned_cacheattr(struct · line 694

Quoted 10 lines
xen/arch/x86/hvm/mtrr.c +2 -5
+        unsigned int i;
 
         hvm_get_guest_pat(v, &hw_mtrr.msr_pat_cr);
 
-        hw_mtrr.msr_mtrr_def_type = mtrr_state->def_type
-                                | (mtrr_state->enabled << 10);
-        hw_mtrr.msr_mtrr_cap = mtrr_state->mtrr_cap;
-
-        for ( i = 0; i < MTRR_VCNT; i++ )
+        for ( i = 0; i < (uint8_t)hw_mtrr.msr_mtrr_cap; i++ )

I've changed this to use MASK_EXTR instead (and switched the cases below).

xen/arch/x86/hvm/mtrr.c static int hvm_load_mtrr_msr(struct doma · line 732

Quoted 8 lines
xen/arch/x86/hvm/mtrr.c +1 -0
         {
             /* save physbase */
             hw_mtrr.msr_mtrr_var[i*2] =
@@ -729,6 +729,14 @@ static int hvm_load_mtrr_msr(struct doma
     if ( hvm_load_entry(MTRR, h, &hw_mtrr) != 0 )
         return -EINVAL;
 
+    if ( (uint8_t)hw_mtrr.msr_mtrr_cap > MTRR_VCNT )

And here I would compare against the VCNT in mtrr_state->mtrr_cat instead of MTRR_VCNT if you agree.

Thanks, Roger.

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

#9 lore raw

Re: [PATCH v2 3/6] hvm/mtrr: use the hardware number of variable ranges for Dom0

Quoted 27 lines
Quoted 1 line
Quoted 1 line · On 16.05.18 at 15:41, <[email protected]> wrote:
Quoted 25 lines · Jan Beulich
xen/arch/x86/hvm/mtrr.c +8 -4
@@ -676,22 +676,22 @@ int hvm_set_mem_pinned_cacheattr(struct
 
 static int hvm_save_mtrr_msr(struct domain *d, hvm_domain_context_t *h)
 {
-    int i;
     struct vcpu *v;
-    struct hvm_hw_mtrr hw_mtrr;
-    struct mtrr_state *mtrr_state;
+
     /* save mtrr&pat */
     for_each_vcpu(d, v)
     {
-        mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        const struct mtrr_state *mtrr_state = &v->arch.hvm_vcpu.mtrr;
+        struct hvm_hw_mtrr hw_mtrr = {
+            .msr_mtrr_def_type = mtrr_state->def_type |
+                                 (mtrr_state->enabled << 10),
+            .msr_mtrr_cap      = mtrr_state->mtrr_cap,
+        };
+        unsigned int i;
 
         hvm_get_guest_pat(v, &hw_mtrr.msr_pat_cr);
 
Quoted 9 lines

xen/arch/x86/hvm/mtrr.c int hvm_set_mem_pinned_cacheattr(struct · line 694

Quoted 6 lines
xen/arch/x86/hvm/mtrr.c +1 -5
-        hw_mtrr.msr_mtrr_def_type = mtrr_state->def_type
-                                | (mtrr_state->enabled << 10);
-        hw_mtrr.msr_mtrr_cap = mtrr_state->mtrr_cap;
-
-        for ( i = 0; i < MTRR_VCNT; i++ )
+        for ( i = 0; i < (uint8_t)hw_mtrr.msr_mtrr_cap; i++ )

I've changed this to use MASK_EXTR instead (and switched the cases below).

Thanks, that was the intention anyway.

Quoted 8 lines

xen/arch/x86/hvm/mtrr.c static int hvm_load_mtrr_msr(struct doma · line 732

Quoted 5 lines
xen/arch/x86/hvm/mtrr.c +1 -0
@@ -729,6 +729,14 @@ static int hvm_load_mtrr_msr(struct doma
     if ( hvm_load_entry(MTRR, h, &hw_mtrr) != 0 )
         return -EINVAL;
 
+    if ( (uint8_t)hw_mtrr.msr_mtrr_cap > MTRR_VCNT )

And here I would compare against the VCNT in mtrr_state->mtrr_cat instead of MTRR_VCNT if you agree.

No, I don't agree: We're _defining_ the number of MTRRs for this vCPU here. There's no difference if such a record is loaded only once, or if all such records agree in count. But I don't see why we should refuse loading a record with a count higher than that provided by an earlier record, but no larger than MTRR_VCNT.

Jan

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

#10 lore raw

Re: [PATCH v2 3/6] hvm/mtrr: use the hardware number of variable ranges for Dom0

Quoted 11 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 16.05.18 at 15:41, <[email protected]> wrote:

xen/arch/x86/hvm/mtrr.c static int hvm_load_mtrr_msr(struct doma · line 732

Quoted 5 lines · Jan Beulich
xen/arch/x86/hvm/mtrr.c +1 -0
@@ -729,6 +729,14 @@ static int hvm_load_mtrr_msr(struct doma
     if ( hvm_load_entry(MTRR, h, &hw_mtrr) != 0 )
         return -EINVAL;
 
+    if ( (uint8_t)hw_mtrr.msr_mtrr_cap > MTRR_VCNT )

And here I would compare against the VCNT in mtrr_state->mtrr_cat instead of MTRR_VCNT if you agree.

Quoted 5 lines

No, I don't agree: We're _defining_ the number of MTRRs for this vCPU here. There's no difference if such a record is loaded only once, or if all such records agree in count. But I don't see why we should refuse loading a record with a count higher than that provided by an earlier record, but no larger than MTRR_VCNT.

You are right, a previous load could have changed mtrr_state->mtrr_cap to < MTRR_VCNT, and then subsequent intents to load MTRR_VCNT number of MTRRs would fail.

Thanks, Roger.

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

#11 lore raw

PATCH v2 4/6 hvm/mtrr: copy hardware state for Dom0

In reply to Roger Pau Monne

Copy the state found on the hardware when creating a PVH Dom0. Since the memory map provided to a PVH Dom0 is based on the native one using the same set of MTRR ranges should provide Dom0 with a sane MTRR state without having to manually build it in Xen.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
Changes since v1:
- Introduce and use the FE shift into the deftype MTRR MSR.

---
 xen/arch/x86/hvm/mtrr.c         | 24 ++++++++++++++++++++++++
 xen/include/asm-x86/msr-index.h |  2 ++
 2 files changed, 26 insertions(+)
xen/arch/x86/hvm/mtrr.c +24 -0
index e71f428a3d..1e185d29c1 100644
@@ -185,6 +185,30 @@ int hvm_vcpu_cacheattr_init(struct vcpu *v)
         ((uint64_t)PAT_TYPE_UC_MINUS << 48) |       /* PAT6: UC- */
         ((uint64_t)PAT_TYPE_UNCACHABLE << 56);      /* PAT7: UC */
 
+    if ( is_hardware_domain(v->domain) )
+    {
+        /* Copy values from the host. */
+        struct domain *d = v->domain;
+        unsigned int i;
+
+        if ( mtrr_state.have_fixed )
+            for ( i = 0; i < NUM_FIXED_MSR; i++ )
+                mtrr_fix_range_msr_set(d, m, i,
+                                      ((uint64_t *)mtrr_state.fixed_ranges)[i]);
+
+        for ( i = 0; i < num_var_ranges; i++ )
+        {
+            mtrr_var_range_msr_set(d, m, MSR_IA32_MTRR_PHYSBASE(i),
+                                   mtrr_state.var_ranges[i].base);
+            mtrr_var_range_msr_set(d, m, MSR_IA32_MTRR_PHYSMASK(i),
+                                   mtrr_state.var_ranges[i].mask);
+        }
+
+        mtrr_def_type_msr_set(d, m,
+                              mtrr_state.def_type |
+                              (mtrr_state.enabled << MTRRdefType_FE_SHIFT));
+    }
+
     return 0;
 }
 
xen/include/asm-x86/msr-index.h +2 -0
index f385fbdc73..9539d6f42e 100644
@@ -96,6 +96,8 @@
 #define MSR_MTRRfix4K_F0000		0x0000026e
 #define MSR_MTRRfix4K_F8000		0x0000026f
 #define MSR_MTRRdefType			0x000002ff
+#define MTRRdefType_FE_SHIFT		10
+#define MTRRdefType_FE			(1u << MTRRdefType_FE_SHIFT)
 
 #define MSR_IA32_DEBUGCTLMSR		0x000001d9
 #define IA32_DEBUGCTLMSR_LBR		(1<<0) /* Last Branch Record */
Signature · 7 lines
2.17.0


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

Re: [PATCH v2 4/6] hvm/mtrr: copy hardware state for Dom0

Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:36, <[email protected]> wrote:
xen/arch/x86/hvm/mtrr.c +12 -0
@@ -185,6 +185,30 @@ int hvm_vcpu_cacheattr_init(struct vcpu *v)
         ((uint64_t)PAT_TYPE_UC_MINUS << 48) |       /* PAT6: UC- */
         ((uint64_t)PAT_TYPE_UNCACHABLE << 56);      /* PAT7: UC */
 
+    if ( is_hardware_domain(v->domain) )
+    {
+        /* Copy values from the host. */
+        struct domain *d = v->domain;
+        unsigned int i;
+
+        if ( mtrr_state.have_fixed )
+            for ( i = 0; i < NUM_FIXED_MSR; i++ )
+                mtrr_fix_range_msr_set(d, m, i,
+                                      ((uint64_t *)mtrr_state.fixed_ranges)[i]);
+
+        for ( i = 0; i < num_var_ranges; i++ )

xen/arch/x86/hvm/mtrr.c int hvm_vcpu_cacheattr_init(struct vcpu *v) · line 209

Quoted 10 lines
xen/arch/x86/hvm/mtrr.c +10 -0
+        {
+            mtrr_var_range_msr_set(d, m, MSR_IA32_MTRR_PHYSBASE(i),
+                                   mtrr_state.var_ranges[i].base);
+            mtrr_var_range_msr_set(d, m, MSR_IA32_MTRR_PHYSMASK(i),
+                                   mtrr_state.var_ranges[i].mask);
+        }
+
+        mtrr_def_type_msr_set(d, m,
+                              mtrr_state.def_type |
+                              (mtrr_state.enabled << MTRRdefType_FE_SHIFT));

This is all very clumsy (not your fault of course) - in particular, the "enabled" field is a two-bit value. I'd rather not see this to continue to be that way, and hence I've created a patch to clean this up (see below; I'm intentionally retaining my own TODO notes in there for your reference, and it's also unlikely to apply as is because it sits on top of other changes). In that light I think I'd prefer if you either (later) re-based this onto my patch or reverted back to the use of the literal 10 here.

Jan

TODO: rename _enabled to enabled for submission (but keep it this way here to
make sure further possibly necessary adjustments are noticed)
TODO: replace 10 and 11 by manifest constants (Roger supposedly will add those)

x86/mtrr: split "enabled" field into two boolean flags

The code hopefully is more readable this way.

Also switch have_fixed to bool, seeing that it already is used as a boolean.

xen/arch/x86/cpu/mtrr/generic.c +9 -5
@@ -79,7 +79,8 @@ void __init get_mtrr_state(void)
 
 	rdmsrl(MSR_MTRRdefType, msr_content);
 	mtrr_state.def_type = (msr_content & 0xff);
-	mtrr_state.enabled = (msr_content & 0xc00) >> 10;
+	mtrr_state._enabled = (msr_content >> 11) & 1;
+	mtrr_state.fixed_enabled = (msr_content >> 10) & 1;
 
 	/* Store mtrr_cap for HVM MTRR virtualisation. */
 	rdmsrl(MSR_MTRRcap, mtrr_state.mtrr_cap);
@@ -158,7 +159,7 @@ static void __init print_mtrr_state(cons
 		unsigned int base = 0, step = 0x10000;
 
 		printk("%sMTRR fixed ranges %sabled:\n", level,
-		       mtrr_state.enabled & 1 ? "en" : "dis");
+		       mtrr_state.fixed_enabled ? "en" : "dis");
 		for (; block->ranges; ++block, step >>= 2) {
 			for (i = 0; i < block->ranges; ++i, fr += 8) {
 				print_fixed(base, step, fr, level);
@@ -168,7 +169,7 @@ static void __init print_mtrr_state(cons
 		print_fixed_last(level);
 	}
 	printk("%sMTRR variable ranges %sabled:\n", level,
-	       mtrr_state.enabled & 2 ? "en" : "dis");
+	       mtrr_state._enabled ? "en" : "dis");
 	width = (paddr_bits - PAGE_SHIFT + 3) / 4;
 
 	for (i = 0; i < num_var_ranges; ++i) {
@@ -382,8 +383,11 @@ static unsigned long set_mtrr_state(void
 	/*  Set_mtrr_restore restores the old value of MTRRdefType,
 	   so to set it we fiddle with the saved value  */
 	if ((deftype & 0xff) != mtrr_state.def_type
-	    || ((deftype & 0xc00) >> 10) != mtrr_state.enabled) {
-		deftype = (deftype & ~0xcff) | mtrr_state.def_type | (mtrr_state.enabled << 10);
+	    || ((deftype >> 11) & 1) != mtrr_state._enabled
+	    || ((deftype >> 10) & 1) != mtrr_state.fixed_enabled) {
+		deftype = (deftype & ~0xcff) | mtrr_state.def_type |
+		          (mtrr_state._enabled << 11) |
+		          (mtrr_state.fixed_enabled << 10);
 		change_mask |= MTRR_CHANGE_MASK_DEFTYPE;
 	}
 
xen/arch/x86/hvm/hvm.c +3 -2
@@ -3497,8 +3497,9 @@ int hvm_msr_read_intercept(unsigned int
     case MSR_MTRRdefType:
         if ( !d->arch.cpuid->basic.mtrr )
             goto gp_fault;
-        *msr_content = v->arch.hvm_vcpu.mtrr.def_type
-                        | (v->arch.hvm_vcpu.mtrr.enabled << 10);
+        *msr_content = v->arch.hvm_vcpu.mtrr.def_type |
+                       (v->arch.hvm_vcpu.mtrr._enabled << 11) |
+                       (v->arch.hvm_vcpu.mtrr.fixed_enabled << 10);
         break;
     case MSR_MTRRfix64K_00000:
         if ( !d->arch.cpuid->basic.mtrr )
xen/arch/x86/hvm/mtrr.c +16 -12
@@ -195,11 +195,11 @@ static int get_mtrr_type(const struct mt
    uint64_t    mask = -(uint64_t)PAGE_SIZE << order;
    unsigned int seg, num_var_ranges = m->mtrr_cap & 0xff;
 
-   if ( unlikely(!(m->enabled & 0x2)) )
+   if ( unlikely(!m->_enabled) )
        return MTRR_TYPE_UNCACHABLE;
 
    pa &= mask;
-   if ( (pa < 0x100000) && (m->enabled & 1) )
+   if ( (pa < 0x100000) && m->fixed_enabled )
    {
        /* Fixed range MTRR takes effect. */
        uint32_t addr = (uint32_t)pa, index;
@@ -391,7 +391,8 @@ bool_t mtrr_def_type_msr_set(struct doma
                              uint64_t msr_content)
 {
     uint8_t def_type = msr_content & 0xff;
-    uint8_t enabled = (msr_content >> 10) & 0x3;
+    uint8_t fixed_enabled = (msr_content >> 10) & 1;
+    uint8_t enabled = (msr_content >> 11) & 1;
 
     if ( unlikely(!valid_mtrr_type(def_type)) )
     {
@@ -406,10 +407,12 @@ bool_t mtrr_def_type_msr_set(struct doma
          return 0;
     }
 
-    if ( m->enabled != enabled || m->def_type != def_type )
+    if ( m->_enabled != enabled || m->def_type != def_type ||
+         m->fixed_enabled != fixed_enabled )
     {
-        m->enabled = enabled;
+        m->_enabled = enabled;
         m->def_type = def_type;
+        m->fixed_enabled = fixed_enabled;
         memory_type_changed(d);
     }
 
@@ -432,7 +435,7 @@ bool_t mtrr_fix_range_msr_set(struct dom
 
         fixed_range_base[row] = msr_content;
 
-        if ( m->enabled == 3 )
+        if ( m->_enabled && m->fixed_enabled )
             memory_type_changed(d);
     }
 
@@ -470,7 +473,7 @@ bool_t mtrr_var_range_msr_set(
 
     m->overlapped = is_var_mtrr_overlapped(m);
 
-    if ( m->enabled & 2 )
+    if ( m->_enabled )
         memory_type_changed(d);
 
     return 1;
@@ -481,10 +484,10 @@ bool mtrr_pat_not_equal(const struct vcp
     const struct mtrr_state *md = &vd->arch.hvm_vcpu.mtrr;
     const struct mtrr_state *ms = &vs->arch.hvm_vcpu.mtrr;
 
-    if ( (md->enabled ^ ms->enabled) & 2 )
+    if ( md->_enabled != ms->_enabled )
         return true;
 
-    if ( md->enabled & 2 )
+    if ( md->_enabled )
     {
         unsigned int num_var_ranges = (uint8_t)md->mtrr_cap;
 
@@ -493,10 +496,10 @@ bool mtrr_pat_not_equal(const struct vcp
             return true;
 
         /* Test fixed ranges. */
-        if ( (md->enabled ^ ms->enabled) & 1 )
+        if ( md->fixed_enabled != ms->fixed_enabled )
             return true;
 
-        if ( (md->enabled & 1) &&
+        if ( md->fixed_enabled &&
              memcmp(md->fixed_ranges, ms->fixed_ranges,
                     sizeof(md->fixed_ranges)) )
             return true;
@@ -684,7 +687,8 @@ static int hvm_save_mtrr_msr(struct doma
         const struct mtrr_state *mtrr_state = &v->arch.hvm_vcpu.mtrr;
         struct hvm_hw_mtrr hw_mtrr = {
             .msr_mtrr_def_type = mtrr_state->def_type |
-                                 (mtrr_state->enabled << 10),
+                                 (mtrr_state->fixed_enabled << 10) |
+                                 (mtrr_state->_enabled << 11),
             .msr_mtrr_cap      = mtrr_state->mtrr_cap,
         };
         unsigned int i;
xen/include/asm-x86/mtrr.h +3 -2
@@ -50,8 +50,9 @@ struct mtrr_var_range {
 struct mtrr_state {
 	struct mtrr_var_range *var_ranges;
 	mtrr_type fixed_ranges[NUM_FIXED_RANGES];
-	unsigned char enabled;
-	unsigned char have_fixed;
+	bool _enabled;
+	bool fixed_enabled;
+	bool have_fixed;
 	mtrr_type def_type;
 
 	u64       mtrr_cap;

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

#13 lore raw

Re: [PATCH v2 4/6] hvm/mtrr: copy hardware state for Dom0

Quoted 30 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:36, <[email protected]> wrote:
xen/arch/x86/hvm/mtrr.c +12 -0
@@ -185,6 +185,30 @@ int hvm_vcpu_cacheattr_init(struct vcpu *v)
         ((uint64_t)PAT_TYPE_UC_MINUS << 48) |       /* PAT6: UC- */
         ((uint64_t)PAT_TYPE_UNCACHABLE << 56);      /* PAT7: UC */
 
+    if ( is_hardware_domain(v->domain) )
+    {
+        /* Copy values from the host. */
+        struct domain *d = v->domain;
+        unsigned int i;
+
+        if ( mtrr_state.have_fixed )
+            for ( i = 0; i < NUM_FIXED_MSR; i++ )
+                mtrr_fix_range_msr_set(d, m, i,
+                                      ((uint64_t *)mtrr_state.fixed_ranges)[i]);
+
+        for ( i = 0; i < num_var_ranges; i++ )

xen/arch/x86/hvm/mtrr.c int hvm_vcpu_cacheattr_init(struct vcpu *v) · line 209

Quoted 10 lines
xen/arch/x86/hvm/mtrr.c +10 -0
+        {
+            mtrr_var_range_msr_set(d, m, MSR_IA32_MTRR_PHYSBASE(i),
+                                   mtrr_state.var_ranges[i].base);
+            mtrr_var_range_msr_set(d, m, MSR_IA32_MTRR_PHYSMASK(i),
+                                   mtrr_state.var_ranges[i].mask);
+        }
+
+        mtrr_def_type_msr_set(d, m,
+                              mtrr_state.def_type |
+                              (mtrr_state.enabled << MTRRdefType_FE_SHIFT));
Quoted 7 lines

This is all very clumsy (not your fault of course) - in particular, the "enabled" field is a two-bit value. I'd rather not see this to continue to be that way, and hence I've created a patch to clean this up (see below; I'm intentionally retaining my own TODO notes in there for your reference, and it's also unlikely to apply as is because it sits on top of other changes). In that light I think I'd prefer if you either (later) re-based this onto my patch or reverted back to the use of the literal 10 here.

I've also realized this but thought about fixing it later.

I don't mind adding this patch to my MTRR series in order to make rebasing easier on my side if it's OK for you.

I would likely add the enabled and fixed_enabled masks to msr-index.h and use MASK_* instead of shifts while there.

Thanks, Roger.

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

#14 lore raw

Re: [PATCH v2 4/6] hvm/mtrr: copy hardware state for Dom0

Quoted 38 lines
Quoted 1 line
Quoted 1 line · On 16.05.18 at 13:58, <[email protected]> wrote:
Quoted 28 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:36, <[email protected]> wrote:
xen/arch/x86/hvm/mtrr.c +21 -0
@@ -185,6 +185,30 @@ int hvm_vcpu_cacheattr_init(struct vcpu *v)
         ((uint64_t)PAT_TYPE_UC_MINUS << 48) |       /* PAT6: UC- */
         ((uint64_t)PAT_TYPE_UNCACHABLE << 56);      /* PAT7: UC */
 
+    if ( is_hardware_domain(v->domain) )
+    {
+        /* Copy values from the host. */
+        struct domain *d = v->domain;
+        unsigned int i;
+
+        if ( mtrr_state.have_fixed )
+            for ( i = 0; i < NUM_FIXED_MSR; i++ )
+                mtrr_fix_range_msr_set(d, m, i,
+                                      ((uint64_t *)mtrr_state.fixed_ranges)[i]);
+
+        for ( i = 0; i < num_var_ranges; i++ )
+        {
+            mtrr_var_range_msr_set(d, m, MSR_IA32_MTRR_PHYSBASE(i),
+                                   mtrr_state.var_ranges[i].base);
+            mtrr_var_range_msr_set(d, m, MSR_IA32_MTRR_PHYSMASK(i),
+                                   mtrr_state.var_ranges[i].mask);
+        }
+
+        mtrr_def_type_msr_set(d, m,
+                              mtrr_state.def_type |
Quoted 8 lines

xen/arch/x86/hvm/mtrr.c int hvm_vcpu_cacheattr_init(struct vcpu *v) · line 209

Quoted 1 line
xen/arch/x86/hvm/mtrr.c +1 -0
+                              (mtrr_state.enabled << MTRRdefType_FE_SHIFT));

This is all very clumsy (not your fault of course) - in particular, the "enabled" field is a two-bit value. I'd rather not see this to continue to be that way, and hence I've created a patch to clean this up (see below; I'm intentionally retaining my own TODO notes in there for your reference, and it's also unlikely to apply as is because it sits on top of other changes). In that light I think I'd prefer if you either (later) re-based this onto my patch or

Quoted 7 lines

reverted

Quoted 1 line

back to the use of the literal 10 here.

I've also realized this but thought about fixing it later.

I don't mind adding this patch to my MTRR series in order to make rebasing easier on my side if it's OK for you.

Feel free.

Quoted 2 lines

I would likely add the enabled and fixed_enabled masks to msr-index.h and use MASK_* instead of shifts while there.

Right, that is the net effect of one of the two TODOs I have in there.

Jan

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

#15 lore raw

PATCH v2 5/6 libxc/pvh: set default MTRR type to write-back

In reply to Roger Pau Monne

And enable MTRR. This allows to provide a sane initial MTRR state for PVH DomUs. This will have to be expanded when pci-passthrough support is added to PVH guests, so that MMIO regions of devices are set as UC.

Note that initial MTRR setup is done by hvmloader for HVM guests, that's not used by PVH guests.

----
Cc: Ian Jackson <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>

---
 tools/libxc/xc_dom_x86.c | 44 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
tools/libxc/xc_dom_x86.c +44 -0
index e33a28847d..d28ff4d7e9 100644
@@ -53,6 +53,9 @@
 #define X86_CR0_PE 0x01
 #define X86_CR0_ET 0x10
 
+#define MTRR_TYPE_WRBACK     6
+#define MTRR_DEF_TYPE_ENABLE (1u << 11)
+
 #define SPECIALPAGE_PAGING   0
 #define SPECIALPAGE_ACCESS   1
 #define SPECIALPAGE_SHARING  2
@@ -931,6 +934,20 @@ static int vcpu_x86_64(struct xc_dom_image *dom)
     return rc;
 }
 
+const static void *hvm_get_save_record(const void *ctx, unsigned int type,
+                                       unsigned int instance)
+{
+    const struct hvm_save_descriptor *header;
+
+    for ( header = ctx;
+          header->typecode != HVM_SAVE_CODE(END);
+          ctx += sizeof(*header) + header->length, header = ctx )
+        if ( header->typecode == type && header->instance == instance )
+            return ctx + sizeof(*header);
+
+    return NULL;
+}
+
 static int vcpu_hvm(struct xc_dom_image *dom)
 {
     struct {
@@ -938,9 +955,12 @@ static int vcpu_hvm(struct xc_dom_image *dom)
         HVM_SAVE_TYPE(HEADER) header;
         struct hvm_save_descriptor cpu_d;
         HVM_SAVE_TYPE(CPU) cpu;
+        struct hvm_save_descriptor mtrr_d;
+        HVM_SAVE_TYPE(MTRR) mtrr;
         struct hvm_save_descriptor end_d;
         HVM_SAVE_TYPE(END) end;
     } bsp_ctx;
+    const HVM_SAVE_TYPE(MTRR) *mtrr_record;
     uint8_t *full_ctx = NULL;
     int rc;
 
@@ -1014,6 +1034,30 @@ static int vcpu_hvm(struct xc_dom_image *dom)
     if ( dom->start_info_seg.pfn )
         bsp_ctx.cpu.rbx = dom->start_info_seg.pfn << PAGE_SHIFT;
 
+    /* Set the MTRR. */
+    bsp_ctx.mtrr_d.typecode = HVM_SAVE_CODE(MTRR);
+    bsp_ctx.mtrr_d.instance = 0;
+    bsp_ctx.mtrr_d.length = HVM_SAVE_LENGTH(MTRR);
+
+    mtrr_record = hvm_get_save_record(full_ctx, HVM_SAVE_CODE(MTRR), 0);
+    if ( !mtrr_record )
+    {
+        xc_dom_panic(dom->xch, XC_INTERNAL_ERROR,
+                     "%s: unable to get MTRR save record", __func__);
+        goto out;
+    }
+
+    memcpy(&bsp_ctx.mtrr, mtrr_record, sizeof(bsp_ctx.mtrr));
+
+    /* TODO: maybe this should be a firmware option instead? */
+    if ( !dom->device_model )
+        /*
+         * Enable MTRR, set default type to WB.
+         * TODO: add MMIO areas as UC when passthrough is supported.
+         */
+        bsp_ctx.mtrr.msr_mtrr_def_type = MTRR_TYPE_WRBACK |
+                                         MTRR_DEF_TYPE_ENABLE;
+
     /* Set the end descriptor. */
     bsp_ctx.end_d.typecode = HVM_SAVE_CODE(END);
     bsp_ctx.end_d.instance = 0;
Signature · 7 lines
2.17.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Acked-by on v2 5/6
#16 lore raw

Re: [PATCH v2 5/6] libxc/pvh: set default MTRR type to write-back

Quoted 9 lines · Roger Pau Monne

And enable MTRR. This allows to provide a sane initial MTRR state for PVH DomUs. This will have to be expanded when pci-passthrough support is added to PVH guests, so that MMIO regions of devices are set as UC.

Note that initial MTRR setup is done by hvmloader for HVM guests, that's not used by PVH guests.

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

#17 lore raw

PATCH v2 6/6 docs/pvh: document initial MTRR state

In reply to Roger Pau Monne

Provided to both Dom0 and DomUs.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v1:
- Add an extra paragraph to clarify the initial MTRR state.

---
 docs/misc/pvh.markdown | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
docs/misc/pvh.markdown +18 -0
index e85fb15374..fc31132618 100644
@@ -92,3 +92,21 @@ event channels. Delivery of those interrupts can be configured in the same way
 as HVM guests, check xen/include/public/hvm/params.h and
 xen/include/public/hvm/hvm\_op.h for more information about available delivery
 methods.
+
+## MTRR ##
+
+### Unprivileged guests ###
+
+PVH guests are booted with the default MTRR type set to write-back and MTRR
+enabled. This allows DomUs to start with a sane MTRR state. Note that this will
+have to be revisited when pci-passthrough is added to PVH in order to set MMIO
+regions as UC.
+
+Xen guarantees that RAM regions will always have the WB cache type set in the
+initial MTRR state, either set by the default MTRR type or by other means.
+
+### Hardware domain ###
+
+A PVH hardware domain is booted with the same MTRR state as the one found on
+the host. This is done because the hardware domain memory map is already a
+modified copy of the host memory map, so the same MTRR setup should work.
Signature · 7 lines
2.17.0


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

Re: [PATCH v2 6/6] docs/pvh: document initial MTRR state

Quoted 31 lines
Quoted 1 line
Quoted 1 line · On 15.05.18 at 16:36, <[email protected]> wrote:

Provided to both Dom0 and DomUs.

---
Cc: Andrew Cooper <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Wei Liu <[email protected]>
---
Changes since v1:
- Add an extra paragraph to clarify the initial MTRR state.

---
 docs/misc/pvh.markdown | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
docs/misc/pvh.markdown +2 -0
index e85fb15374..fc31132618 100644
@@ -92,3 +92,21 @@ event channels. Delivery of those interrupts can be configured in the same way
 as HVM guests, check xen/include/public/hvm/params.h and
 xen/include/public/hvm/hvm\_op.h for more information about available delivery
 methods.
+
+## MTRR ##

docs/misc/pvh.markdown event channels. Delivery of those interrupts can be configured in the same way · line 106

Quoted 10 lines
docs/misc/pvh.markdown +10 -0
+
+### Unprivileged guests ###
+
+PVH guests are booted with the default MTRR type set to write-back and MTRR
+enabled. This allows DomUs to start with a sane MTRR state. Note that this will
+have to be revisited when pci-passthrough is added to PVH in order to set MMIO
+regions as UC.
+
+Xen guarantees that RAM regions will always have the WB cache type set in the
+initial MTRR state, either set by the default MTRR type or by other means.

While adding the second paragraph has helped, I still think the first one is lacking "currently" or some such in the first sentence.

Jan

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