Skip to contentSkip to conversation

x86/hap: use the right cache attributes when MTRR is disabled

PATCH In review Roger Pau Monne 2 participants 2 messages Aug 2, 2016 – Aug 3, 2016

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/hap: use the right cache attributes when MTRR is disabled

Currently the code that calculates the cache attributes of the HAP page tables assume that if MTRR are disabled the memory type is UC, this can cause issues if MTRR are never enabled because the guest only plans to use PAT.

In order to solve this modify epte_get_entry_emt so that is takes into account that MTRR can be disabled and that PAT should be used instead, this also implies that when page tables are enabled inside the guest a recalculation of the HAP page table cache attributes must be performed.

The special casing that was previously done for PVHv1 guests can now be removed, since PVHv1 guest cannot have MTRR enabled or paging disabled, so the memory type is always going to be MTRR_TYPE_WRBACK in that case.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
---
Changes since v1:
- Fix comments.
- Slightly change the if ... else logic.
- Correctly check if MTRR are enabled.
- Expand description.

---
 xen/arch/x86/hvm/hvm.c  |  4 ++++
 xen/arch/x86/hvm/mtrr.c | 17 +++++++++++++----
 2 files changed, 17 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/hvm.c +4 -0
index daaee1d..db4b2d6 100644
@@ -2283,7 +2283,11 @@ int hvm_set_cr0(unsigned long value, bool_t may_defer)
         if ( !nestedhvm_vmswitch_in_progress(v) && nestedhvm_vcpu_in_guestmode(v) )
             paging_update_nestedmode(v);
         else
+        {
             paging_update_paging_modes(v);
+            /* Force an update of the memory cache attributes. */
+            memory_type_changed(d);
+        }
     }
 
     return X86EMUL_OKAY;
xen/arch/x86/hvm/mtrr.c +13 -4
index f7831ff..bf3ab72 100644
@@ -814,10 +814,19 @@ int epte_get_entry_emt(struct domain *d, unsigned long gfn, mfn_t mfn,
     if ( gmtrr_mtype == -EADDRNOTAVAIL )
         return -1;
 
-    gmtrr_mtype = is_hvm_domain(d) && v ?
-                  get_mtrr_type(&v->arch.hvm_vcpu.mtrr,
-                                gfn << PAGE_SHIFT, order) :
-                  MTRR_TYPE_WRBACK;
+    if ( !v )
+        gmtrr_mtype = MTRR_TYPE_WRBACK;
+    else if ( v->arch.hvm_vcpu.mtrr.enabled & 0x2 )
+        /* MTRR is enabled, use MTRR. */
+        gmtrr_mtype = get_mtrr_type(&v->arch.hvm_vcpu.mtrr, gfn << PAGE_SHIFT,
+                                    order);
+    else if ( !hvm_paging_enabled(v) )
+        /* MTRR is not enabled and paging is disabled, force UC. */
+        gmtrr_mtype = MTRR_TYPE_UNCACHABLE;
+    else
+        /* MTRR is not enabled and paging is enabled, use PAT. */
+        gmtrr_mtype = MTRR_TYPE_WRBACK;
+
     hmtrr_mtype = get_mtrr_type(&mtrr_state, mfn_x(mfn) << PAGE_SHIFT, order);
     if ( gmtrr_mtype < 0 || hmtrr_mtype < 0 )
         return -1;
Signature · 7 lines
2.7.4 (Apple Git-66)


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

xen/arch/x86/hvm/hvm.c int hvm_set_cr0(unsigned long value, bool_t may_defer) · line 2290

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 02.08.16 at 15:55, <[email protected]> wrote:
xen/arch/x86/hvm/hvm.c +4 -0
@@ -2283,7 +2283,11 @@ int hvm_set_cr0(unsigned long value, bool_t may_defer)
         if ( !nestedhvm_vmswitch_in_progress(v) && nestedhvm_vcpu_in_guestmode(v) )
             paging_update_nestedmode(v);
         else
+        {
             paging_update_paging_modes(v);
+            /* Force an update of the memory cache attributes. */
+            memory_type_changed(d);
+        }

Why only in the "else" case, rather than after the if/else block?

Quoted 12 lines
xen/arch/x86/hvm/mtrr.c +2 -4
@@ -814,10 +814,19 @@ int epte_get_entry_emt(struct domain *d, unsigned long gfn, mfn_t mfn,
     if ( gmtrr_mtype == -EADDRNOTAVAIL )
         return -1;
 
-    gmtrr_mtype = is_hvm_domain(d) && v ?
-                  get_mtrr_type(&v->arch.hvm_vcpu.mtrr,
-                                gfn << PAGE_SHIFT, order) :
-                  MTRR_TYPE_WRBACK;
+    if ( !v )
+        gmtrr_mtype = MTRR_TYPE_WRBACK;

xen/arch/x86/hvm/mtrr.c int epte_get_entry_emt(struct domain *d, unsigned long gfn, mfn_t mfn, · line 828

Quoted 10 lines
xen/arch/x86/hvm/mtrr.c +10 -0
+    else if ( v->arch.hvm_vcpu.mtrr.enabled & 0x2 )
+        /* MTRR is enabled, use MTRR. */
+        gmtrr_mtype = get_mtrr_type(&v->arch.hvm_vcpu.mtrr, gfn << PAGE_SHIFT,
+                                    order);
+    else if ( !hvm_paging_enabled(v) )
+        /* MTRR is not enabled and paging is disabled, force UC. */
+        gmtrr_mtype = MTRR_TYPE_UNCACHABLE;
+    else
+        /* MTRR is not enabled and paging is enabled, use PAT. */
+        gmtrr_mtype = MTRR_TYPE_WRBACK;

I'm afraid this needs to be UC, or else you provide non-architectural behavior to the guest (see the SDM's description of the enable flag being clear). Which means the original code was correct. I'm sorry for not having checked the SDM upon looking at v1 already.

Jan

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