Skip to contentSkip to conversation

xen/arm: fix guest builder cache cohenrency (again, again)

PATCH Superseded Ian Campbell 4 participants 17 messages Feb 7, 2014 – Feb 11, 2014

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover xen/arm: fix guest builder cache cohenrency (again, again)

George gave a release ack to v3.

The last patch here is new and renames the cache flushing functions to make it clearer what they do (which is clean but not invalidate, which caught me out). In principal this could wait for 4.5 but given it is a pure rename I think it might as well go in along with the rest, so that is what I intend to do unless George objects.

Both 32 and 64 bit have survived ~10,000 boots with this version.

Changes in v4:
make sure to actually invalidate the cache, not just clean it

rename existing cache flush functions to avoid catching me out that way again.

switch to using a start + length in the domctl interface

Changes in v3:
s/cacheflush_page/sync_page_to_ram/

xc interface takes a length instead of an end

make the domctl range inclusive.

make xc interface internal -- it isn't needed from libxl in the current design and it is easier to expose an interface in the future than to hide it.

Changes in v2:
Flush on page alloc and do targeted flushes at domain build time rather than a big flush after domain build. This adds a new call to common code, which is stubbed out on x86. This avoid needing to worry about preemptability of the new domctl and also catches cases related to ballooning where things might not be flushed (e.g. a guest scrubs a page but doesn't clean the cache)

This has done 12000 boot loops on arm32 and 10000 on arm64.

Given the security aspect I would like to put this in 4.4.

Original blurb:

On ARM we need to take care of cache coherency for guests which we have just built because they start with their caches disabled.

Our current strategy for dealing with this, which is to make guest memory default to cacheable regardless of the in guest configuration (the HCR.DC bit), is flawed because it doesn't handle guests which enable their MMU before enabling their caches, which at least FreeBSD does. (NB: Setting HCR.DC while the guest MMU is enabled is UNPREDICTABLE, hence we must disable it when the guest turns its MMU one).

There is also a security aspect here since the current strategy means that a guest which enables its MMU before its caches can potentially see unscrubbed data in RAM (because the scrubbed bytes are still held in the cache).

As well as the new stuff this series removes the HCR.DC support and performs two purely cosmetic renames.

Ian.

#1 lore raw

PATCH v4 1/5 xen: arm: rename create_p2m_entries to apply_p2m_changes

This function hasn't been only about creating for quite a while.

This is purely a rename.

---
 xen/arch/arm/p2m.c |   28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
xen/arch/arm/p2m.c +14 -14
index 85ca330..ace3c54 100644
@@ -230,7 +230,7 @@ enum p2m_operation {
     RELINQUISH,
 };
 
-static int create_p2m_entries(struct domain *d,
+static int apply_p2m_changes(struct domain *d,
                      enum p2m_operation op,
                      paddr_t start_gpaddr,
                      paddr_t end_gpaddr,
@@ -438,8 +438,8 @@ int p2m_populate_ram(struct domain *d,
                      paddr_t start,
                      paddr_t end)
 {
-    return create_p2m_entries(d, ALLOCATE, start, end,
-                              0, MATTR_MEM, p2m_ram_rw);
+    return apply_p2m_changes(d, ALLOCATE, start, end,
+                             0, MATTR_MEM, p2m_ram_rw);
 }
 
 int map_mmio_regions(struct domain *d,
@@ -447,8 +447,8 @@ int map_mmio_regions(struct domain *d,
                      paddr_t end_gaddr,
                      paddr_t maddr)
 {
-    return create_p2m_entries(d, INSERT, start_gaddr, end_gaddr,
-                              maddr, MATTR_DEV, p2m_mmio_direct);
+    return apply_p2m_changes(d, INSERT, start_gaddr, end_gaddr,
+                             maddr, MATTR_DEV, p2m_mmio_direct);
 }
 
 int guest_physmap_add_entry(struct domain *d,
@@ -457,20 +457,20 @@ int guest_physmap_add_entry(struct domain *d,
                             unsigned long page_order,
                             p2m_type_t t)
 {
-    return create_p2m_entries(d, INSERT,
-                              pfn_to_paddr(gpfn),
-                              pfn_to_paddr(gpfn + (1 << page_order)),
-                              pfn_to_paddr(mfn), MATTR_MEM, t);
+    return apply_p2m_changes(d, INSERT,
+                             pfn_to_paddr(gpfn),
+                             pfn_to_paddr(gpfn + (1 << page_order)),
+                             pfn_to_paddr(mfn), MATTR_MEM, t);
 }
 
 void guest_physmap_remove_page(struct domain *d,
                                unsigned long gpfn,
                                unsigned long mfn, unsigned int page_order)
 {
-    create_p2m_entries(d, REMOVE,
-                       pfn_to_paddr(gpfn),
-                       pfn_to_paddr(gpfn + (1<<page_order)),
-                       pfn_to_paddr(mfn), MATTR_MEM, p2m_invalid);
+    apply_p2m_changes(d, REMOVE,
+                      pfn_to_paddr(gpfn),
+                      pfn_to_paddr(gpfn + (1<<page_order)),
+                      pfn_to_paddr(mfn), MATTR_MEM, p2m_invalid);
 }
 
 int p2m_alloc_table(struct domain *d)
@@ -618,7 +618,7 @@ int relinquish_p2m_mapping(struct domain *d)
 {
     struct p2m_domain *p2m = &d->arch.p2m;
 
-    return create_p2m_entries(d, RELINQUISH,
+    return apply_p2m_changes(d, RELINQUISH,
                               pfn_to_paddr(p2m->next_gfn_to_relinquish),
                               pfn_to_paddr(p2m->max_mapped_gfn),
                               pfn_to_paddr(INVALID_MFN),
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH v4 2/5 xen: arm: rename p2m next_gfn_to_relinquish to lowest_mapped_gfn

In reply to Ian Campbell

This has other uses other than during relinquish, so rename it for clarity.

This is a pure rename.

---
 xen/arch/arm/p2m.c        |    9 ++++-----
 xen/include/asm-arm/p2m.h |    8 +++++---
 2 files changed, 9 insertions(+), 8 deletions(-)
xen/arch/arm/p2m.c +4 -5
index ace3c54..a61edeb 100644
@@ -388,7 +388,7 @@ static int apply_p2m_changes(struct domain *d,
         {
             if ( hypercall_preempt_check() )
             {
-                p2m->next_gfn_to_relinquish = addr >> PAGE_SHIFT;
+                p2m->lowest_mapped_gfn = addr >> PAGE_SHIFT;
                 rc = -EAGAIN;
                 goto out;
             }
@@ -415,8 +415,7 @@ static int apply_p2m_changes(struct domain *d,
         unsigned long egfn = paddr_to_pfn(end_gpaddr);
 
         p2m->max_mapped_gfn = MAX(p2m->max_mapped_gfn, egfn);
-        /* Use next_gfn_to_relinquish to store the lowest gfn mapped */
-        p2m->next_gfn_to_relinquish = MIN(p2m->next_gfn_to_relinquish, sgfn);
+        p2m->lowest_mapped_gfn = MIN(p2m->lowest_mapped_gfn, sgfn);
     }
 
     rc = 0;
@@ -606,7 +605,7 @@ int p2m_init(struct domain *d)
     p2m->first_level = NULL;
 
     p2m->max_mapped_gfn = 0;
-    p2m->next_gfn_to_relinquish = ULONG_MAX;
+    p2m->lowest_mapped_gfn = ULONG_MAX;
 
 err:
     spin_unlock(&p2m->lock);
@@ -619,7 +618,7 @@ int relinquish_p2m_mapping(struct domain *d)
     struct p2m_domain *p2m = &d->arch.p2m;
 
     return apply_p2m_changes(d, RELINQUISH,
-                              pfn_to_paddr(p2m->next_gfn_to_relinquish),
+                              pfn_to_paddr(p2m->lowest_mapped_gfn),
                               pfn_to_paddr(p2m->max_mapped_gfn),
                               pfn_to_paddr(INVALID_MFN),
                               MATTR_MEM, p2m_invalid);
xen/include/asm-arm/p2m.h +5 -3
index 53b3266..e9c884a 100644
@@ -24,9 +24,11 @@ struct p2m_domain {
      */
     unsigned long max_mapped_gfn;
 
-    /* When releasing mapped gfn's in a preemptible manner, recall where
-     * to resume the search */
-    unsigned long next_gfn_to_relinquish;
+    /* Lowest mapped gfn in the p2m. When releasing mapped gfn's in a
+     * preemptible manner this is update to track recall where to
+     * resume the search. Apart from during teardown this can only
+     * decrease. */
+    unsigned long lowest_mapped_gfn;
 };
 
 /* List of possible type for each page in the p2m entry.
Signature · 1 line
1.7.10.4
#3 lore raw

PATCH v4 3/5 xen/arm: clean and invalidate all guest caches by VMID after domain build.

In reply to Ian Campbell

Guests are initially started with caches disabled and so we need to make sure they see consistent data in RAM (requiring a cache clean) but also that they do not have old stale data suddenly appear in the caches when they enable their caches (requiring the invalidate).

This can be split into two halves. First we must flush each page as it is allocated to the guest. It is not sufficient to do the flush at scrub time since this will miss pages which are ballooned out by the guest (where the guest must scrub if it cares about not leaking the pagecontent). We need to clean as well as invalidate to make sure that any scrubbing which has occured gets committed to real RAM. To achieve this add a new cacheflush_page function, which is a stub on x86.

Secondly we need to flush anything which the domain builder touches, which we do via a new domctl.

--
v4: introduce a function to clean and invalidate as intended

make the domctl take a length not an end.

v3:
s/cacheflush_page/sync_page_to_ram/

xc interface takes a length instead of an end

make the domctl range inclusive.

make xc interface internal -- it isn't needed from libxl in the current design and it is easier to expose an interface in the future than to hide it.

v2:
Switch to cleaning at page allocation time + explicit flushing of the regions which the toolstack touches.

Add XSM for new domctl.

New domctl restricts the amount of space it is willing to flush, to avoid thinking about preemption.

---
 tools/libxc/xc_dom_boot.c           |    4 ++++
 tools/libxc/xc_dom_core.c           |    2 ++
 tools/libxc/xc_domain.c             |   10 ++++++++++
 tools/libxc/xc_private.c            |    2 ++
 tools/libxc/xc_private.h            |    3 +++
 xen/arch/arm/domctl.c               |   14 ++++++++++++++
 xen/arch/arm/mm.c                   |   12 ++++++++++++
 xen/arch/arm/p2m.c                  |   25 +++++++++++++++++++++++++
 xen/common/page_alloc.c             |    5 +++++
 xen/include/asm-arm/arm32/page.h    |    4 ++++
 xen/include/asm-arm/arm64/page.h    |    4 ++++
 xen/include/asm-arm/p2m.h           |    3 +++
 xen/include/asm-arm/page.h          |    3 +++
 xen/include/asm-x86/page.h          |    3 +++
 xen/include/public/domctl.h         |   13 +++++++++++++
 xen/xsm/flask/hooks.c               |    3 +++
 xen/xsm/flask/policy/access_vectors |    2 ++
 17 files changed, 112 insertions(+)
tools/libxc/xc_dom_boot.c +4 -0
index 5a9cfc6..3d4d107 100644
@@ -351,6 +351,10 @@ int xc_dom_gnttab_seed(xc_interface *xch, domid_t domid,
         return -1;
     }
 
+    /* Guest shouldn't really touch its grant table until it has
+     * enabled its caches. But lets be nice. */
+    xc_domain_cacheflush(xch, domid, gnttab_gmfn, 1);
+
     return 0;
 }
 
tools/libxc/xc_dom_core.c +2 -0
index 77a4e64..b9d1015 100644
@@ -603,6 +603,8 @@ void xc_dom_unmap_one(struct xc_dom_image *dom, xen_pfn_t pfn)
         prev->next = phys->next;
     else
         dom->phys_pages = phys->next;
+
+    xc_domain_cacheflush(dom->xch, dom->guest_domid, phys->first, phys->count);
 }
 
 void xc_dom_unmap_all(struct xc_dom_image *dom)
tools/libxc/xc_domain.c +10 -0
index c2fdd74..f10ec01 100644
@@ -48,6 +48,16 @@ int xc_domain_create(xc_interface *xch,
     return 0;
 }
 
+int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,
+                         xen_pfn_t start_pfn, xen_pfn_t nr_pfns)
+{
+    DECLARE_DOMCTL;
+    domctl.cmd = XEN_DOMCTL_cacheflush;
+    domctl.domain = (domid_t)domid;
+    domctl.u.cacheflush.start_pfn = start_pfn;
+    domctl.u.cacheflush.nr_pfns = nr_pfns;
+    return do_domctl(xch, &domctl);
+}
 
 int xc_domain_pause(xc_interface *xch,
                     uint32_t domid)
tools/libxc/xc_private.c +2 -0
index 838fd21..33ed15b 100644
@@ -628,6 +628,7 @@ int xc_copy_to_domain_page(xc_interface *xch,
         return -1;
     memcpy(vaddr, src_page, PAGE_SIZE);
     munmap(vaddr, PAGE_SIZE);
+    xc_domain_cacheflush(xch, domid, dst_pfn, 1);
     return 0;
 }
 
@@ -641,6 +642,7 @@ int xc_clear_domain_page(xc_interface *xch,
         return -1;
     memset(vaddr, 0, PAGE_SIZE);
     munmap(vaddr, PAGE_SIZE);
+    xc_domain_cacheflush(xch, domid, dst_pfn, 1);
     return 0;
 }
 
tools/libxc/xc_private.h +3 -0
index 92271c9..a610f0c 100644
@@ -304,6 +304,9 @@ void bitmap_byte_to_64(uint64_t *lp, const uint8_t *bp, int nbits);
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush);
 
+int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,
+			 xen_pfn_t start_pfn, xen_pfn_t nr_pfns);
+
 #define MAX_MMU_UPDATES 1024
 struct xc_mmu {
     mmu_update_t updates[MAX_MMU_UPDATES];
xen/arch/arm/domctl.c +14 -0
index 546e86b..ffb68da 100644
@@ -17,6 +17,20 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d,
 {
     switch ( domctl->cmd )
     {
+    case XEN_DOMCTL_cacheflush:
+    {
+        unsigned long s = domctl->u.cacheflush.start_pfn;
+        unsigned long e = s + domctl->u.cacheflush.nr_pfns;
+
+        if ( e < s )
+            return -EINVAL;
+
+        if ( get_order_from_pages(domctl->u.cacheflush.nr_pfns) > MAX_ORDER )
+            return -EINVAL;
+
+        return p2m_cache_flush(d, s, e);
+    }
+
     default:
         return subarch_do_domctl(domctl, d, u_domctl);
     }
xen/arch/arm/mm.c +12 -0
index 2f48347..d2cfe64 100644
@@ -338,6 +338,18 @@ unsigned long domain_page_map_to_mfn(const void *va)
 }
 #endif
 
+void sync_page_to_ram(unsigned long mfn)
+{
+    void *p, *v = map_domain_page(mfn);
+
+    dsb();           /* So the CPU issues all writes to the range */
+    for ( p = v; p < v + PAGE_SIZE ; p += cacheline_bytes )
+        asm volatile (__clean_and_invalidate_xen_dcache_one(0) : : "r" (p));
+    dsb();           /* So we know the flushes happen before continuing */
+
+    unmap_domain_page(v);
+}
+
 void __init arch_init_memory(void)
 {
     /*
xen/arch/arm/p2m.c +25 -0
index a61edeb..86f13e9 100644
@@ -8,6 +8,7 @@
 #include <asm/gic.h>
 #include <asm/event.h>
 #include <asm/hardirq.h>
+#include <asm/page.h>
 
 /* First level P2M is 2 consecutive pages */
 #define P2M_FIRST_ORDER 1
@@ -228,6 +229,7 @@ enum p2m_operation {
     ALLOCATE,
     REMOVE,
     RELINQUISH,
+    CACHEFLUSH,
 };
 
 static int apply_p2m_changes(struct domain *d,
@@ -381,6 +383,15 @@ static int apply_p2m_changes(struct domain *d,
                     count++;
                 }
                 break;
+
+            case CACHEFLUSH:
+                {
+                    if ( !pte.p2m.valid || !p2m_is_ram(pte.p2m.type) )
+                        break;
+
+                    sync_page_to_ram(pte.p2m.base);
+                }
+                break;
         }
 
         /* Preempt every 2MiB (mapped) or 32 MiB (unmapped) - arbitrary */
@@ -624,6 +635,20 @@ int relinquish_p2m_mapping(struct domain *d)
                               MATTR_MEM, p2m_invalid);
 }
 
+int p2m_cache_flush(struct domain *d, xen_pfn_t start_mfn, xen_pfn_t end_mfn)
+{
+    struct p2m_domain *p2m = &d->arch.p2m;
+
+    start_mfn = MAX(start_mfn, p2m->lowest_mapped_gfn);
+    end_mfn = MIN(end_mfn, p2m->max_mapped_gfn);
+
+    return apply_p2m_changes(d, CACHEFLUSH,
+                             pfn_to_paddr(start_mfn),
+                             pfn_to_paddr(end_mfn),
+                             pfn_to_paddr(INVALID_MFN),
+                             MATTR_MEM, p2m_invalid);
+}
+
 unsigned long gmfn_to_mfn(struct domain *d, unsigned long gpfn)
 {
     paddr_t p = p2m_lookup(d, pfn_to_paddr(gpfn), NULL);
xen/common/page_alloc.c +5 -0
index 5f484a2..c73c717 100644
@@ -710,6 +710,11 @@ static struct page_info *alloc_heap_pages(
         /* Initialise fields which have other uses for free pages. */
         pg[i].u.inuse.type_info = 0;
         page_set_owner(&pg[i], NULL);
+
+        /* Ensure cache and RAM are consistent for platforms where the
+         * guest can control its own visibility of/through the cache.
+         */
+        sync_page_to_ram(page_to_mfn(&pg[i]));
     }
 
     spin_unlock(&heap_lock);
xen/include/asm-arm/arm32/page.h +4 -0
index cf12a89..cb6add4 100644
@@ -22,6 +22,10 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
 /* Inline ASM to flush dcache on register R (may be an inline asm operand) */
 #define __flush_xen_dcache_one(R) STORE_CP32(R, DCCMVAC)
 
+/* Inline ASM to clean and invalidate dcache on register R (may be an
+ * inline asm operand) */
+#define __clean_and_invalidate_xen_dcache_one(R) STORE_CP32(R, DCCIMVAC)
+
 /*
  * Flush all hypervisor mappings from the TLB and branch predictor.
  * This is needed after changing Xen code mappings.
xen/include/asm-arm/arm64/page.h +4 -0
index 9551f90..baf8903 100644
@@ -17,6 +17,10 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
 /* Inline ASM to flush dcache on register R (may be an inline asm operand) */
 #define __flush_xen_dcache_one(R) "dc cvac, %" #R ";"
 
+/* Inline ASM to clean and invalidate dcache on register R (may be an
+ * inline asm operand) */
+#define __clean_and_invalidate_xen_dcache_one(R) "dc  civac, %" #R ";"
+
 /*
  * Flush all hypervisor mappings from the TLB
  * This is needed after changing Xen code mappings.
xen/include/asm-arm/p2m.h +3 -0
index e9c884a..3b39c45 100644
@@ -78,6 +78,9 @@ void p2m_load_VTTBR(struct domain *d);
 /* Look up the MFN corresponding to a domain's PFN. */
 paddr_t p2m_lookup(struct domain *d, paddr_t gpfn, p2m_type_t *t);
 
+/* Clean & invalidate caches corresponding to a region of guest address space */
+int p2m_cache_flush(struct domain *d, xen_pfn_t start_mfn, xen_pfn_t end_mfn);
+
 /* Setup p2m RAM mapping for domain d from start-end. */
 int p2m_populate_ram(struct domain *d, paddr_t start, paddr_t end);
 /* Map MMIO regions in the p2m: start_gaddr and end_gaddr is the range
xen/include/asm-arm/page.h +3 -0
index 670d4e7..67d64c9 100644
@@ -253,6 +253,9 @@ static inline void flush_xen_dcache_va_range(void *p, unsigned long size)
             : : "r" (_p), "m" (*_p));                                   \
 } while (0)
 
+/* Flush the dcache for an entire page. */
+void sync_page_to_ram(unsigned long mfn);
+
 /* Print a walk of an arbitrary page table */
 void dump_pt_walk(lpae_t *table, paddr_t addr);
 
xen/include/asm-x86/page.h +3 -0
index 7a46af5..abe35fb 100644
@@ -346,6 +346,9 @@ static inline uint32_t cacheattr_to_pte_flags(uint32_t cacheattr)
     return ((cacheattr & 4) << 5) | ((cacheattr & 3) << 3);
 }
 
+/* No cache maintenance required on x86 architecture. */
+static inline void sync_page_to_ram(unsigned long mfn) {}
+
 /* return true if permission increased */
 static inline bool_t
 perms_strictly_increased(uint32_t old_flags, uint32_t new_flags)
xen/include/public/domctl.h +13 -0
index 91f01fa..f22fe2e 100644
@@ -885,6 +885,17 @@ struct xen_domctl_set_max_evtchn {
 typedef struct xen_domctl_set_max_evtchn xen_domctl_set_max_evtchn_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_set_max_evtchn_t);
 
+/*
+ * ARM: Clean and invalidate caches associated with given region of
+ * guest memory.
+ */
+struct xen_domctl_cacheflush {
+    /* IN: page range to flush. */
+    xen_pfn_t start_pfn, nr_pfns;
+};
+typedef struct xen_domctl_cacheflush xen_domctl_cacheflush_t;
+DEFINE_XEN_GUEST_HANDLE(xen_domctl_cacheflush_t);
+
 struct xen_domctl {
     uint32_t cmd;
 #define XEN_DOMCTL_createdomain                   1
@@ -954,6 +965,7 @@ struct xen_domctl {
 #define XEN_DOMCTL_setnodeaffinity               68
 #define XEN_DOMCTL_getnodeaffinity               69
 #define XEN_DOMCTL_set_max_evtchn                70
+#define XEN_DOMCTL_cacheflush                    71
 #define XEN_DOMCTL_gdbsx_guestmemio            1000
 #define XEN_DOMCTL_gdbsx_pausevcpu             1001
 #define XEN_DOMCTL_gdbsx_unpausevcpu           1002
@@ -1012,6 +1024,7 @@ struct xen_domctl {
         struct xen_domctl_set_max_evtchn    set_max_evtchn;
         struct xen_domctl_gdbsx_memio       gdbsx_guest_memio;
         struct xen_domctl_set_broken_page_p2m set_broken_page_p2m;
+        struct xen_domctl_cacheflush        cacheflush;
         struct xen_domctl_gdbsx_pauseunp_vcpu gdbsx_pauseunp_vcpu;
         struct xen_domctl_gdbsx_domstatus   gdbsx_domstatus;
         uint8_t                             pad[128];
xen/xsm/flask/hooks.c +3 -0
index 50a35fc..1345d7e 100644
@@ -737,6 +737,9 @@ static int flask_domctl(struct domain *d, int cmd)
     case XEN_DOMCTL_set_max_evtchn:
         return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__SET_MAX_EVTCHN);
 
+    case XEN_DOMCTL_cacheflush:
+	    return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__CACHEFLUSH);
+
     default:
         printk("flask_domctl: Unknown op %d\n", cmd);
         return -EPERM;
xen/xsm/flask/policy/access_vectors +2 -0
index 1fbe241..a0ed13d 100644
@@ -196,6 +196,8 @@ class domain2
     setclaim
 # XEN_DOMCTL_set_max_evtchn
     set_max_evtchn
+# XEN_DOMCTL_cacheflush
+    cacheflush
 }
 
 # Similar to class domain, but primarily contains domctls related to HVM domains
Signature · 1 line
1.7.10.4
#4 lore raw

Re: [PATCH v4 3/5] xen/arm: clean and invalidate all guest caches by VMID after domain build.

Quoted 7 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
xen/arch/arm/domctl.c +0 -0
@@ -17,6 +17,20 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d,
 {
     switch ( domctl->cmd )
     {

xen/arch/arm/domctl.c long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, · line 29

Quoted 10 lines
xen/arch/arm/domctl.c +10 -0
+    case XEN_DOMCTL_cacheflush:
+    {
+        unsigned long s = domctl->u.cacheflush.start_pfn;
+        unsigned long e = s + domctl->u.cacheflush.nr_pfns;
+
+        if ( e < s )
+            return -EINVAL;
+
+        if ( get_order_from_pages(domctl->u.cacheflush.nr_pfns) > MAX_ORDER )
+            return -EINVAL;

get_order_from_pages() takes an unsigned long, while xen_pfn_t is - iirc - 64-bits even on arm32. So you're not checking the full passed in value, yet use the full one in the calculation of "e" (which is what gets passed down).

Also, did you consider the nr_pfns == 0 case? At present, due to the way get_order_from_pages() works, this will produce -EINVAL. I'm not sure that's intended.

Quoted 4 lines
xen/include/public/domctl.h +0 -0
@@ -885,6 +885,17 @@ struct xen_domctl_set_max_evtchn {
 typedef struct xen_domctl_set_max_evtchn xen_domctl_set_max_evtchn_t;

xen/include/public/domctl.h struct xen_domctl_set_max_evtchn { · line 895

Quoted 10 lines
xen/include/public/domctl.h +8 -0
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_set_max_evtchn_t);
 
+/*
+ * ARM: Clean and invalidate caches associated with given region of
+ * guest memory.
+ */
+struct xen_domctl_cacheflush {
+    /* IN: page range to flush. */
+    xen_pfn_t start_pfn, nr_pfns;
+};

The name here (and of the libxc interface) is now certainly counterintuitive. But it's a domctl (and an internal interface), which we can change post-4.4 (I'd envision it to actually take a flags parameter indicating the kind of flush that's wanted).

xen/xsm/flask/hooks.c static int flask_domctl(struct domain *d, int cmd) · line 741

Quoted 8 lines
xen/xsm/flask/hooks.c +2 -0
@@ -737,6 +737,9 @@ static int flask_domctl(struct domain *d, int cmd)
     case XEN_DOMCTL_set_max_evtchn:
         return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__SET_MAX_EVTCHN);
 
+    case XEN_DOMCTL_cacheflush:
+	    return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__CACHEFLUSH);

Hard tab.

Jan

#5 lore raw

Re: [PATCH v4 3/5] xen/arm: clean and invalidate all guest caches by VMID after domain build.

Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
xen/arch/arm/domctl.c +7 -0
@@ -17,6 +17,20 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d,
 {
     switch ( domctl->cmd )
     {
+    case XEN_DOMCTL_cacheflush:
+    {
+        unsigned long s = domctl->u.cacheflush.start_pfn;
+        unsigned long e = s + domctl->u.cacheflush.nr_pfns;
+
+        if ( e < s )
+            return -EINVAL;
Quoted 8 lines

xen/arch/arm/domctl.c long arch_do_domctl(struct xen_domctl *domctl, struct domain *d, · line 29

Quoted 3 lines
xen/arch/arm/domctl.c +3 -0
+
+        if ( get_order_from_pages(domctl->u.cacheflush.nr_pfns) > MAX_ORDER )
+            return -EINVAL;

get_order_from_pages() takes an unsigned long, while xen_pfn_t is - iirc - 64-bits even on arm32. So you're not checking the full passed in value, yet use the full one in the calculation of "e" (which is what gets passed down).

Yes, you are right, I should have made nr_mfns be a smaller type.

Quoted 3 lines

Also, did you consider the nr_pfns == 0 case? At present, due to the way get_order_from_pages() works, this will produce -EINVAL. I'm not sure that's intended.

I think nr == 0 => EINVAL is probably ok.

But actually this made me realise that using get_order_from_pages is a bit silly here. It seems more logical to compare nr_pfns with 1<<(MAX_ORDER-PAGE_ORDER) now that we have a length in hand.

Quoted 7 lines
Quoted 7 lines
xen/include/public/domctl.h +0 -0
@@ -885,6 +885,17 @@ struct xen_domctl_set_max_evtchn {
 typedef struct xen_domctl_set_max_evtchn xen_domctl_set_max_evtchn_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_set_max_evtchn_t);
 
Quoted 13 lines

xen/include/public/domctl.h struct xen_domctl_set_max_evtchn { · line 895

Quoted 8 lines
xen/include/public/domctl.h +8 -0
+/*
+ * ARM: Clean and invalidate caches associated with given region of
+ * guest memory.
+ */
+struct xen_domctl_cacheflush {
+    /* IN: page range to flush. */
+    xen_pfn_t start_pfn, nr_pfns;
+};

The name here (and of the libxc interface) is now certainly counterintuitive. But it's a domctl (and an internal interface), which we can change post-4.4 (I'd envision it to actually take a flags parameter indicating the kind of flush that's wanted).

Sounds OK to me, thanks.

Quoted 10 lines

xen/xsm/flask/hooks.c static int flask_domctl(struct domain *d, int cmd) · line 741

Quoted 8 lines
xen/xsm/flask/hooks.c +2 -0
@@ -737,6 +737,9 @@ static int flask_domctl(struct domain *d, int cmd)
     case XEN_DOMCTL_set_max_evtchn:
         return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__SET_MAX_EVTCHN);
 
+    case XEN_DOMCTL_cacheflush:
+	    return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__CACHEFLUSH);

Hard tab.

Well spotted.

This file is missing the emacs magic block. I'll fix this and add one.

Ian.

#6 lore raw

Re: [PATCH v4 3/5] xen/arm: clean and invalidate all guest caches by VMID after domain build.

In reply to Jan Beulich
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Ian Campbell
xen/include/public/domctl.h +0 -0
@@ -885,6 +885,17 @@ struct xen_domctl_set_max_evtchn {
 typedef struct xen_domctl_set_max_evtchn xen_domctl_set_max_evtchn_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_set_max_evtchn_t);
 
Quoted 13 lines

xen/include/public/domctl.h struct xen_domctl_set_max_evtchn { · line 895

Quoted 8 lines
xen/include/public/domctl.h +8 -0
+/*
+ * ARM: Clean and invalidate caches associated with given region of
+ * guest memory.
+ */
+struct xen_domctl_cacheflush {
+    /* IN: page range to flush. */
+    xen_pfn_t start_pfn, nr_pfns;
+};

The name here (and of the libxc interface) is now certainly counterintuitive. But it's a domctl (and an internal interface), which we can change post-4.4 (I'd envision it to actually take a flags parameter indicating the kind of flush that's wanted).

Actually the naming of things in the hypervisor part of the patch is now bogus too - sysc_page_to_ram(), for example, does in no way imply that the cache needs not just flushing, but also invalidating. The need for which, btw, I continue to not
understand: Are there ways in ARM for one guest to observe cache contents put in place by another guest (incl Dom0)?

Jan

#7 lore raw

Re: [PATCH v4 3/5] xen/arm: clean and invalidate all guest caches by VMID after domain build.

Quoted 22 lines · Jan Beulich
Quoted 8 lines
Quoted 8 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Ian Campbell
xen/include/public/domctl.h +0 -0
@@ -885,6 +885,17 @@ struct xen_domctl_set_max_evtchn {
 typedef struct xen_domctl_set_max_evtchn xen_domctl_set_max_evtchn_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_set_max_evtchn_t);
 
Quoted 13 lines

xen/include/public/domctl.h struct xen_domctl_set_max_evtchn { · line 895

Quoted 8 lines
xen/include/public/domctl.h +8 -0
+/*
+ * ARM: Clean and invalidate caches associated with given region of
+ * guest memory.
+ */
+struct xen_domctl_cacheflush {
+    /* IN: page range to flush. */
+    xen_pfn_t start_pfn, nr_pfns;
+};

The name here (and of the libxc interface) is now certainly counterintuitive. But it's a domctl (and an internal interface), which we can change post-4.4 (I'd envision it to actually take a flags parameter indicating the kind of flush that's wanted).

Quoted 4 lines

Actually the naming of things in the hypervisor part of the patch is now bogus too - sysc_page_to_ram(), for example, does in no way imply that the cache needs not just flushing, but also invalidating.

sync_and_clean_page ? Not quite right I think.

Quoted 3 lines

The need for which, btw, I continue to not
understand: Are there ways in ARM for one guest to observe cache contents put in place by another guest (incl Dom0)?

The data cache is physically tagged on armv7+ (which is all we support), so if I understand your question correctly the answer is: a guest cannot see cache contents placed by another guest *unless* that page is freed back to the hypervisor and recycled as a new page for that guest, which is why we want to clean+invalidate in the allocation path, after the page has been scrubbed if required.

If you don't invalidate the cache then a guest which starts without caches enabled and writes to RAM may be surprised when it enables the cache and finds the data it wrote is now shadowed by some stale cache entries.

Ian.

#8 lore raw

Re: [PATCH v4 3/5] xen/arm: clean and invalidate all guest caches by VMID after domain build.

Quoted 5 lines
Quoted 1 line
Quoted 1 line · Ian Campbell

If you don't invalidate the cache then a guest which starts without caches enabled and writes to RAM may be surprised when it enables the cache and finds the data it wrote is now shadowed by some stale cache entries.

Oh, right, I forgot guests get started in cache disabled mode on ARM (for whatever that's good).

Jan

#9 lore raw

Re: [PATCH v4 3/5] xen/arm: clean and invalidate all guest caches by VMID after domain build.

In reply to Ian Campbell
Quoted 23 lines · Ian Campbell
Quoted 22 lines · Jan Beulich
Quoted 21 lines
Quoted 6 lines
Quoted 2 lines · Jan Beulich
Quoted 1 line · Ian Campbell
xen/include/public/domctl.h +0 -0
@@ -885,6 +885,17 @@ struct xen_domctl_set_max_evtchn {
 typedef struct xen_domctl_set_max_evtchn xen_domctl_set_max_evtchn_t;

xen/include/public/domctl.h struct xen_domctl_set_max_evtchn { · line 895

Quoted 10 lines
xen/include/public/domctl.h +8 -0
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_set_max_evtchn_t);
 
+/*
+ * ARM: Clean and invalidate caches associated with given region of
+ * guest memory.
+ */
+struct xen_domctl_cacheflush {
+    /* IN: page range to flush. */
+    xen_pfn_t start_pfn, nr_pfns;
+};

The name here (and of the libxc interface) is now certainly counterintuitive. But it's a domctl (and an internal interface), which we can change post-4.4 (I'd envision it to actually take a flags parameter indicating the kind of flush that's wanted).

Quoted 6 lines
Quoted 4 lines

Actually the naming of things in the hypervisor part of the patch is now bogus too - sysc_page_to_ram(), for example, does in no way imply that the cache needs not just flushing, but also invalidating.

sync_and_clean_page ? Not quite right I think.

Unless there are objections my next posting will use "flush_page_to_ram"..

Acked-by on v4 3/5
#10 lore raw

Re: [PATCH v4 3/5] xen/arm: clean and invalidate all guest caches by VMID after domain build.

In reply to Ian Campbell
Quoted 13 lines · Ian Campbell

Guests are initially started with caches disabled and so we need to make sure they see consistent data in RAM (requiring a cache clean) but also that they do not have old stale data suddenly appear in the caches when they enable their caches (requiring the invalidate).

This can be split into two halves. First we must flush each page as it is allocated to the guest. It is not sufficient to do the flush at scrub time since this will miss pages which are ballooned out by the guest (where the guest must scrub if it cares about not leaking the pagecontent). We need to clean as well as invalidate to make sure that any scrubbing which has occured gets committed to real RAM. To achieve this add a new cacheflush_page function, which is a stub on x86.

Quoted 5 lines

Secondly we need to flush anything which the domain builder touches, which we do via a new domctl.

Quoted 389 lines

Cc: [email protected]
Cc: [email protected]
Cc: [email protected]
--
v4: introduce a function to clean and invalidate as intended

make the domctl take a length not an end.

v3:
s/cacheflush_page/sync_page_to_ram/

xc interface takes a length instead of an end

make the domctl range inclusive.

make xc interface internal -- it isn't needed from libxl in the current design and it is easier to expose an interface in the future than to hide it.

v2:
Switch to cleaning at page allocation time + explicit flushing of the regions which the toolstack touches.

Add XSM for new domctl.

New domctl restricts the amount of space it is willing to flush, to avoid thinking about preemption.

---
  tools/libxc/xc_dom_boot.c           |    4 ++++
  tools/libxc/xc_dom_core.c           |    2 ++
  tools/libxc/xc_domain.c             |   10 ++++++++++
  tools/libxc/xc_private.c            |    2 ++
  tools/libxc/xc_private.h            |    3 +++
  xen/arch/arm/domctl.c               |   14 ++++++++++++++
  xen/arch/arm/mm.c                   |   12 ++++++++++++
  xen/arch/arm/p2m.c                  |   25 +++++++++++++++++++++++++
  xen/common/page_alloc.c             |    5 +++++
  xen/include/asm-arm/arm32/page.h    |    4 ++++
  xen/include/asm-arm/arm64/page.h    |    4 ++++
  xen/include/asm-arm/p2m.h           |    3 +++
  xen/include/asm-arm/page.h          |    3 +++
  xen/include/asm-x86/page.h          |    3 +++
  xen/include/public/domctl.h         |   13 +++++++++++++
  xen/xsm/flask/hooks.c               |    3 +++
  xen/xsm/flask/policy/access_vectors |    2 ++
  17 files changed, 112 insertions(+)
tools/libxc/xc_dom_boot.c +4 -0
index 5a9cfc6..3d4d107 100644
@@ -351,6 +351,10 @@ int xc_dom_gnttab_seed(xc_interface *xch, domid_t domid,
          return -1;
      }
 
+    /* Guest shouldn't really touch its grant table until it has
+     * enabled its caches. But lets be nice. */
+    xc_domain_cacheflush(xch, domid, gnttab_gmfn, 1);
+
      return 0;
  }
 
tools/libxc/xc_dom_core.c +2 -0
index 77a4e64..b9d1015 100644
@@ -603,6 +603,8 @@ void xc_dom_unmap_one(struct xc_dom_image *dom, xen_pfn_t pfn)
          prev->next = phys->next;
      else
          dom->phys_pages = phys->next;
+
+    xc_domain_cacheflush(dom->xch, dom->guest_domid, phys->first, phys->count);
  }
 
  void xc_dom_unmap_all(struct xc_dom_image *dom)
tools/libxc/xc_domain.c +10 -0
index c2fdd74..f10ec01 100644
@@ -48,6 +48,16 @@ int xc_domain_create(xc_interface *xch,
      return 0;
  }
 
+int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,
+                         xen_pfn_t start_pfn, xen_pfn_t nr_pfns)
+{
+    DECLARE_DOMCTL;
+    domctl.cmd = XEN_DOMCTL_cacheflush;
+    domctl.domain = (domid_t)domid;
+    domctl.u.cacheflush.start_pfn = start_pfn;
+    domctl.u.cacheflush.nr_pfns = nr_pfns;
+    return do_domctl(xch, &domctl);
+}
 
  int xc_domain_pause(xc_interface *xch,
                      uint32_t domid)
tools/libxc/xc_private.c +2 -0
index 838fd21..33ed15b 100644
@@ -628,6 +628,7 @@ int xc_copy_to_domain_page(xc_interface *xch,
          return -1;
      memcpy(vaddr, src_page, PAGE_SIZE);
      munmap(vaddr, PAGE_SIZE);
+    xc_domain_cacheflush(xch, domid, dst_pfn, 1);
      return 0;
  }
 
@@ -641,6 +642,7 @@ int xc_clear_domain_page(xc_interface *xch,
          return -1;
      memset(vaddr, 0, PAGE_SIZE);
      munmap(vaddr, PAGE_SIZE);
+    xc_domain_cacheflush(xch, domid, dst_pfn, 1);
      return 0;
  }
 
tools/libxc/xc_private.h +3 -0
index 92271c9..a610f0c 100644
@@ -304,6 +304,9 @@ void bitmap_byte_to_64(uint64_t *lp, const uint8_t *bp, int nbits);
  /* Optionally flush file to disk and discard page cache */
  void discard_file_cache(xc_interface *xch, int fd, int flush);
 
+int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,
+			 xen_pfn_t start_pfn, xen_pfn_t nr_pfns);
+
  #define MAX_MMU_UPDATES 1024
  struct xc_mmu {
      mmu_update_t updates[MAX_MMU_UPDATES];
xen/arch/arm/domctl.c +14 -0
index 546e86b..ffb68da 100644
@@ -17,6 +17,20 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d,
  {
      switch ( domctl->cmd )
      {
+    case XEN_DOMCTL_cacheflush:
+    {
+        unsigned long s = domctl->u.cacheflush.start_pfn;
+        unsigned long e = s + domctl->u.cacheflush.nr_pfns;
+
+        if ( e < s )
+            return -EINVAL;
+
+        if ( get_order_from_pages(domctl->u.cacheflush.nr_pfns) > MAX_ORDER )
+            return -EINVAL;
+
+        return p2m_cache_flush(d, s, e);
+    }
+
      default:
          return subarch_do_domctl(domctl, d, u_domctl);
      }
xen/arch/arm/mm.c +12 -0
index 2f48347..d2cfe64 100644
@@ -338,6 +338,18 @@ unsigned long domain_page_map_to_mfn(const void *va)
  }
  #endif
 
+void sync_page_to_ram(unsigned long mfn)
+{
+    void *p, *v = map_domain_page(mfn);
+
+    dsb();           /* So the CPU issues all writes to the range */
+    for ( p = v; p < v + PAGE_SIZE ; p += cacheline_bytes )
+        asm volatile (__clean_and_invalidate_xen_dcache_one(0) : : "r" (p));
+    dsb();           /* So we know the flushes happen before continuing */
+
+    unmap_domain_page(v);
+}
+
  void __init arch_init_memory(void)
  {
      /*
xen/arch/arm/p2m.c +25 -0
index a61edeb..86f13e9 100644
@@ -8,6 +8,7 @@
  #include <asm/gic.h>
  #include <asm/event.h>
  #include <asm/hardirq.h>
+#include <asm/page.h>
 
  /* First level P2M is 2 consecutive pages */
  #define P2M_FIRST_ORDER 1
@@ -228,6 +229,7 @@ enum p2m_operation {
      ALLOCATE,
      REMOVE,
      RELINQUISH,
+    CACHEFLUSH,
  };
 
  static int apply_p2m_changes(struct domain *d,
@@ -381,6 +383,15 @@ static int apply_p2m_changes(struct domain *d,
                      count++;
                  }
                  break;
+
+            case CACHEFLUSH:
+                {
+                    if ( !pte.p2m.valid || !p2m_is_ram(pte.p2m.type) )
+                        break;
+
+                    sync_page_to_ram(pte.p2m.base);
+                }
+                break;
          }
 
          /* Preempt every 2MiB (mapped) or 32 MiB (unmapped) - arbitrary */
@@ -624,6 +635,20 @@ int relinquish_p2m_mapping(struct domain *d)
                                MATTR_MEM, p2m_invalid);
  }
 
+int p2m_cache_flush(struct domain *d, xen_pfn_t start_mfn, xen_pfn_t end_mfn)
+{
+    struct p2m_domain *p2m = &d->arch.p2m;
+
+    start_mfn = MAX(start_mfn, p2m->lowest_mapped_gfn);
+    end_mfn = MIN(end_mfn, p2m->max_mapped_gfn);
+
+    return apply_p2m_changes(d, CACHEFLUSH,
+                             pfn_to_paddr(start_mfn),
+                             pfn_to_paddr(end_mfn),
+                             pfn_to_paddr(INVALID_MFN),
+                             MATTR_MEM, p2m_invalid);
+}
+
  unsigned long gmfn_to_mfn(struct domain *d, unsigned long gpfn)
  {
      paddr_t p = p2m_lookup(d, pfn_to_paddr(gpfn), NULL);
xen/common/page_alloc.c +5 -0
index 5f484a2..c73c717 100644
@@ -710,6 +710,11 @@ static struct page_info *alloc_heap_pages(
          /* Initialise fields which have other uses for free pages. */
          pg[i].u.inuse.type_info = 0;
          page_set_owner(&pg[i], NULL);
+
+        /* Ensure cache and RAM are consistent for platforms where the
+         * guest can control its own visibility of/through the cache.
+         */
+        sync_page_to_ram(page_to_mfn(&pg[i]));
      }
 
      spin_unlock(&heap_lock);
xen/include/asm-arm/arm32/page.h +4 -0
index cf12a89..cb6add4 100644
@@ -22,6 +22,10 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
  /* Inline ASM to flush dcache on register R (may be an inline asm operand) */
  #define __flush_xen_dcache_one(R) STORE_CP32(R, DCCMVAC)
 
+/* Inline ASM to clean and invalidate dcache on register R (may be an
+ * inline asm operand) */
+#define __clean_and_invalidate_xen_dcache_one(R) STORE_CP32(R, DCCIMVAC)
+
  /*
   * Flush all hypervisor mappings from the TLB and branch predictor.
   * This is needed after changing Xen code mappings.
xen/include/asm-arm/arm64/page.h +4 -0
index 9551f90..baf8903 100644
@@ -17,6 +17,10 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
  /* Inline ASM to flush dcache on register R (may be an inline asm operand) */
  #define __flush_xen_dcache_one(R) "dc cvac, %" #R ";"
 
+/* Inline ASM to clean and invalidate dcache on register R (may be an
+ * inline asm operand) */
+#define __clean_and_invalidate_xen_dcache_one(R) "dc  civac, %" #R ";"
+
  /*
   * Flush all hypervisor mappings from the TLB
   * This is needed after changing Xen code mappings.
xen/include/asm-arm/p2m.h +3 -0
index e9c884a..3b39c45 100644
@@ -78,6 +78,9 @@ void p2m_load_VTTBR(struct domain *d);
  /* Look up the MFN corresponding to a domain's PFN. */
  paddr_t p2m_lookup(struct domain *d, paddr_t gpfn, p2m_type_t *t);
 
+/* Clean & invalidate caches corresponding to a region of guest address space */
+int p2m_cache_flush(struct domain *d, xen_pfn_t start_mfn, xen_pfn_t end_mfn);
+
  /* Setup p2m RAM mapping for domain d from start-end. */
  int p2m_populate_ram(struct domain *d, paddr_t start, paddr_t end);
  /* Map MMIO regions in the p2m: start_gaddr and end_gaddr is the range
xen/include/asm-arm/page.h +3 -0
index 670d4e7..67d64c9 100644
@@ -253,6 +253,9 @@ static inline void flush_xen_dcache_va_range(void *p, unsigned long size)
              : : "r" (_p), "m" (*_p));                                   \
  } while (0)
 
+/* Flush the dcache for an entire page. */
+void sync_page_to_ram(unsigned long mfn);
+
  /* Print a walk of an arbitrary page table */
  void dump_pt_walk(lpae_t *table, paddr_t addr);
 
xen/include/asm-x86/page.h +3 -0
index 7a46af5..abe35fb 100644
@@ -346,6 +346,9 @@ static inline uint32_t cacheattr_to_pte_flags(uint32_t cacheattr)
      return ((cacheattr & 4) << 5) | ((cacheattr & 3) << 3);
  }
 
+/* No cache maintenance required on x86 architecture. */
+static inline void sync_page_to_ram(unsigned long mfn) {}
+
  /* return true if permission increased */
  static inline bool_t
  perms_strictly_increased(uint32_t old_flags, uint32_t new_flags)
xen/include/public/domctl.h +13 -0
index 91f01fa..f22fe2e 100644
@@ -885,6 +885,17 @@ struct xen_domctl_set_max_evtchn {
  typedef struct xen_domctl_set_max_evtchn xen_domctl_set_max_evtchn_t;
  DEFINE_XEN_GUEST_HANDLE(xen_domctl_set_max_evtchn_t);
 
+/*
+ * ARM: Clean and invalidate caches associated with given region of
+ * guest memory.
+ */
+struct xen_domctl_cacheflush {
+    /* IN: page range to flush. */
+    xen_pfn_t start_pfn, nr_pfns;
+};
+typedef struct xen_domctl_cacheflush xen_domctl_cacheflush_t;
+DEFINE_XEN_GUEST_HANDLE(xen_domctl_cacheflush_t);
+
  struct xen_domctl {
      uint32_t cmd;
  #define XEN_DOMCTL_createdomain                   1
@@ -954,6 +965,7 @@ struct xen_domctl {
  #define XEN_DOMCTL_setnodeaffinity               68
  #define XEN_DOMCTL_getnodeaffinity               69
  #define XEN_DOMCTL_set_max_evtchn                70
+#define XEN_DOMCTL_cacheflush                    71
  #define XEN_DOMCTL_gdbsx_guestmemio            1000
  #define XEN_DOMCTL_gdbsx_pausevcpu             1001
  #define XEN_DOMCTL_gdbsx_unpausevcpu           1002
@@ -1012,6 +1024,7 @@ struct xen_domctl {
          struct xen_domctl_set_max_evtchn    set_max_evtchn;
          struct xen_domctl_gdbsx_memio       gdbsx_guest_memio;
          struct xen_domctl_set_broken_page_p2m set_broken_page_p2m;
+        struct xen_domctl_cacheflush        cacheflush;
          struct xen_domctl_gdbsx_pauseunp_vcpu gdbsx_pauseunp_vcpu;
          struct xen_domctl_gdbsx_domstatus   gdbsx_domstatus;
          uint8_t                             pad[128];
xen/xsm/flask/hooks.c +3 -0
index 50a35fc..1345d7e 100644
@@ -737,6 +737,9 @@ static int flask_domctl(struct domain *d, int cmd)
      case XEN_DOMCTL_set_max_evtchn:
          return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__SET_MAX_EVTCHN);
 
+    case XEN_DOMCTL_cacheflush:
+	    return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__CACHEFLUSH);
+
      default:
          printk("flask_domctl: Unknown op %d\n", cmd);
          return -EPERM;
xen/xsm/flask/policy/access_vectors +2 -0
index 1fbe241..a0ed13d 100644
@@ -196,6 +196,8 @@ class domain2
      setclaim
  # XEN_DOMCTL_set_max_evtchn
      set_max_evtchn
+# XEN_DOMCTL_cacheflush
+    cacheflush
  }
 
  # Similar to class domain, but primarily contains domctls related to HVM domains
Signature · 1 line
Julien Grall
#11 lore raw

Re: [PATCH v4 3/5] xen/arm: clean and invalidate all guest caches by VMID after domain build.

In reply to Ian Campbell
Quoted 85 lines · Ian Campbell

Guests are initially started with caches disabled and so we need to make sure they see consistent data in RAM (requiring a cache clean) but also that they do not have old stale data suddenly appear in the caches when they enable their caches (requiring the invalidate).

This can be split into two halves. First we must flush each page as it is allocated to the guest. It is not sufficient to do the flush at scrub time since this will miss pages which are ballooned out by the guest (where the guest must scrub if it cares about not leaking the pagecontent). We need to clean as well as invalidate to make sure that any scrubbing which has occured gets committed to real RAM. To achieve this add a new cacheflush_page function, which is a stub on x86.

Secondly we need to flush anything which the domain builder touches, which we do via a new domctl.

--
v4: introduce a function to clean and invalidate as intended

make the domctl take a length not an end.

v3:
s/cacheflush_page/sync_page_to_ram/

xc interface takes a length instead of an end

make the domctl range inclusive.

make xc interface internal -- it isn't needed from libxl in the current design and it is easier to expose an interface in the future than to hide it.

v2:
Switch to cleaning at page allocation time + explicit flushing of the regions which the toolstack touches.

Add XSM for new domctl.

New domctl restricts the amount of space it is willing to flush, to avoid thinking about preemption.

---
 tools/libxc/xc_dom_boot.c           |    4 ++++
 tools/libxc/xc_dom_core.c           |    2 ++
 tools/libxc/xc_domain.c             |   10 ++++++++++
 tools/libxc/xc_private.c            |    2 ++
 tools/libxc/xc_private.h            |    3 +++
 xen/arch/arm/domctl.c               |   14 ++++++++++++++
 xen/arch/arm/mm.c                   |   12 ++++++++++++
 xen/arch/arm/p2m.c                  |   25 +++++++++++++++++++++++++
 xen/common/page_alloc.c             |    5 +++++
 xen/include/asm-arm/arm32/page.h    |    4 ++++
 xen/include/asm-arm/arm64/page.h    |    4 ++++
 xen/include/asm-arm/p2m.h           |    3 +++
 xen/include/asm-arm/page.h          |    3 +++
 xen/include/asm-x86/page.h          |    3 +++
 xen/include/public/domctl.h         |   13 +++++++++++++
 xen/xsm/flask/hooks.c               |    3 +++
 xen/xsm/flask/policy/access_vectors |    2 ++
 17 files changed, 112 insertions(+)
tools/libxc/xc_dom_boot.c +4 -0
index 5a9cfc6..3d4d107 100644
@@ -351,6 +351,10 @@ int xc_dom_gnttab_seed(xc_interface *xch, domid_t domid,
         return -1;
     }
 
+    /* Guest shouldn't really touch its grant table until it has
+     * enabled its caches. But lets be nice. */
+    xc_domain_cacheflush(xch, domid, gnttab_gmfn, 1);
+
     return 0;
 }
 
tools/libxc/xc_dom_core.c +0 -0
index 77a4e64..b9d1015 100644

tools/libxc/xc_dom_core.c void xc_dom_unmap_one(struct xc_dom_image *dom, xen_pfn_t pfn) · line 607

Quoted 5 lines
tools/libxc/xc_dom_core.c +2 -0
@@ -603,6 +603,8 @@ void xc_dom_unmap_one(struct xc_dom_image *dom, xen_pfn_t pfn)
         prev->next = phys->next;
     else
         dom->phys_pages = phys->next;
+
+    xc_domain_cacheflush(dom->xch, dom->guest_domid, phys->first, phys->count);

I am not sure I understand the semantic of xc_dom_unmap_one: from the name of the function and the parameters I would think that it is supposed to unmap just one page. In that case we should flush just one page. However the implementation calls

munmap(phys->ptr, phys->count << page_shift)

so it actually can unmap more than a single page, so I think that you did the right thing by calling xc_domain_cacheflush with phys->first and phys->count.

Quoted 88 lines
tools/libxc/xc_dom_core.c +0 -0
 }
 
 void xc_dom_unmap_all(struct xc_dom_image *dom)
tools/libxc/xc_domain.c +10 -0
index c2fdd74..f10ec01 100644
@@ -48,6 +48,16 @@ int xc_domain_create(xc_interface *xch,
     return 0;
 }
 
+int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,
+                         xen_pfn_t start_pfn, xen_pfn_t nr_pfns)
+{
+    DECLARE_DOMCTL;
+    domctl.cmd = XEN_DOMCTL_cacheflush;
+    domctl.domain = (domid_t)domid;
+    domctl.u.cacheflush.start_pfn = start_pfn;
+    domctl.u.cacheflush.nr_pfns = nr_pfns;
+    return do_domctl(xch, &domctl);
+}
 
 int xc_domain_pause(xc_interface *xch,
                     uint32_t domid)
tools/libxc/xc_private.c +2 -0
index 838fd21..33ed15b 100644
@@ -628,6 +628,7 @@ int xc_copy_to_domain_page(xc_interface *xch,
         return -1;
     memcpy(vaddr, src_page, PAGE_SIZE);
     munmap(vaddr, PAGE_SIZE);
+    xc_domain_cacheflush(xch, domid, dst_pfn, 1);
     return 0;
 }
 
@@ -641,6 +642,7 @@ int xc_clear_domain_page(xc_interface *xch,
         return -1;
     memset(vaddr, 0, PAGE_SIZE);
     munmap(vaddr, PAGE_SIZE);
+    xc_domain_cacheflush(xch, domid, dst_pfn, 1);
     return 0;
 }
 
tools/libxc/xc_private.h +3 -0
index 92271c9..a610f0c 100644
@@ -304,6 +304,9 @@ void bitmap_byte_to_64(uint64_t *lp, const uint8_t *bp, int nbits);
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush);
 
+int xc_domain_cacheflush(xc_interface *xch, uint32_t domid,
+			 xen_pfn_t start_pfn, xen_pfn_t nr_pfns);
+
 #define MAX_MMU_UPDATES 1024
 struct xc_mmu {
     mmu_update_t updates[MAX_MMU_UPDATES];
xen/arch/arm/domctl.c +14 -0
index 546e86b..ffb68da 100644
@@ -17,6 +17,20 @@ long arch_do_domctl(struct xen_domctl *domctl, struct domain *d,
 {
     switch ( domctl->cmd )
     {
+    case XEN_DOMCTL_cacheflush:
+    {
+        unsigned long s = domctl->u.cacheflush.start_pfn;
+        unsigned long e = s + domctl->u.cacheflush.nr_pfns;
+
+        if ( e < s )
+            return -EINVAL;
+
+        if ( get_order_from_pages(domctl->u.cacheflush.nr_pfns) > MAX_ORDER )
+            return -EINVAL;
+
+        return p2m_cache_flush(d, s, e);
+    }
+
     default:
         return subarch_do_domctl(domctl, d, u_domctl);
     }
xen/arch/arm/mm.c +0 -0
index 2f48347..d2cfe64 100644

xen/arch/arm/mm.c unsigned long domain_page_map_to_mfn(const void *va) · line 346

Quoted 9 lines
xen/arch/arm/mm.c +6 -0
@@ -338,6 +338,18 @@ unsigned long domain_page_map_to_mfn(const void *va)
 }
 #endif
 
+void sync_page_to_ram(unsigned long mfn)
+{
+    void *p, *v = map_domain_page(mfn);
+
+    dsb();           /* So the CPU issues all writes to the range */
+    for ( p = v; p < v + PAGE_SIZE ; p += cacheline_bytes )

What about the last few bytes on a page?
Can we assume that PAGE_SIZE is a multiple of cacheline_bytes?

Quoted 222 lines
xen/arch/arm/mm.c +6 -0
+        asm volatile (__clean_and_invalidate_xen_dcache_one(0) : : "r" (p));
+    dsb();           /* So we know the flushes happen before continuing */
+
+    unmap_domain_page(v);
+}
+
 void __init arch_init_memory(void)
 {
     /*
xen/arch/arm/p2m.c +25 -0
index a61edeb..86f13e9 100644
@@ -8,6 +8,7 @@
 #include <asm/gic.h>
 #include <asm/event.h>
 #include <asm/hardirq.h>
+#include <asm/page.h>
 
 /* First level P2M is 2 consecutive pages */
 #define P2M_FIRST_ORDER 1
@@ -228,6 +229,7 @@ enum p2m_operation {
     ALLOCATE,
     REMOVE,
     RELINQUISH,
+    CACHEFLUSH,
 };
 
 static int apply_p2m_changes(struct domain *d,
@@ -381,6 +383,15 @@ static int apply_p2m_changes(struct domain *d,
                     count++;
                 }
                 break;
+
+            case CACHEFLUSH:
+                {
+                    if ( !pte.p2m.valid || !p2m_is_ram(pte.p2m.type) )
+                        break;
+
+                    sync_page_to_ram(pte.p2m.base);
+                }
+                break;
         }
 
         /* Preempt every 2MiB (mapped) or 32 MiB (unmapped) - arbitrary */
@@ -624,6 +635,20 @@ int relinquish_p2m_mapping(struct domain *d)
                               MATTR_MEM, p2m_invalid);
 }
 
+int p2m_cache_flush(struct domain *d, xen_pfn_t start_mfn, xen_pfn_t end_mfn)
+{
+    struct p2m_domain *p2m = &d->arch.p2m;
+
+    start_mfn = MAX(start_mfn, p2m->lowest_mapped_gfn);
+    end_mfn = MIN(end_mfn, p2m->max_mapped_gfn);
+
+    return apply_p2m_changes(d, CACHEFLUSH,
+                             pfn_to_paddr(start_mfn),
+                             pfn_to_paddr(end_mfn),
+                             pfn_to_paddr(INVALID_MFN),
+                             MATTR_MEM, p2m_invalid);
+}
+
 unsigned long gmfn_to_mfn(struct domain *d, unsigned long gpfn)
 {
     paddr_t p = p2m_lookup(d, pfn_to_paddr(gpfn), NULL);
xen/common/page_alloc.c +5 -0
index 5f484a2..c73c717 100644
@@ -710,6 +710,11 @@ static struct page_info *alloc_heap_pages(
         /* Initialise fields which have other uses for free pages. */
         pg[i].u.inuse.type_info = 0;
         page_set_owner(&pg[i], NULL);
+
+        /* Ensure cache and RAM are consistent for platforms where the
+         * guest can control its own visibility of/through the cache.
+         */
+        sync_page_to_ram(page_to_mfn(&pg[i]));
     }
 
     spin_unlock(&heap_lock);
xen/include/asm-arm/arm32/page.h +4 -0
index cf12a89..cb6add4 100644
@@ -22,6 +22,10 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
 /* Inline ASM to flush dcache on register R (may be an inline asm operand) */
 #define __flush_xen_dcache_one(R) STORE_CP32(R, DCCMVAC)
 
+/* Inline ASM to clean and invalidate dcache on register R (may be an
+ * inline asm operand) */
+#define __clean_and_invalidate_xen_dcache_one(R) STORE_CP32(R, DCCIMVAC)
+
 /*
  * Flush all hypervisor mappings from the TLB and branch predictor.
  * This is needed after changing Xen code mappings.
xen/include/asm-arm/arm64/page.h +4 -0
index 9551f90..baf8903 100644
@@ -17,6 +17,10 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
 /* Inline ASM to flush dcache on register R (may be an inline asm operand) */
 #define __flush_xen_dcache_one(R) "dc cvac, %" #R ";"
 
+/* Inline ASM to clean and invalidate dcache on register R (may be an
+ * inline asm operand) */
+#define __clean_and_invalidate_xen_dcache_one(R) "dc  civac, %" #R ";"
+
 /*
  * Flush all hypervisor mappings from the TLB
  * This is needed after changing Xen code mappings.
xen/include/asm-arm/p2m.h +3 -0
index e9c884a..3b39c45 100644
@@ -78,6 +78,9 @@ void p2m_load_VTTBR(struct domain *d);
 /* Look up the MFN corresponding to a domain's PFN. */
 paddr_t p2m_lookup(struct domain *d, paddr_t gpfn, p2m_type_t *t);
 
+/* Clean & invalidate caches corresponding to a region of guest address space */
+int p2m_cache_flush(struct domain *d, xen_pfn_t start_mfn, xen_pfn_t end_mfn);
+
 /* Setup p2m RAM mapping for domain d from start-end. */
 int p2m_populate_ram(struct domain *d, paddr_t start, paddr_t end);
 /* Map MMIO regions in the p2m: start_gaddr and end_gaddr is the range
xen/include/asm-arm/page.h +3 -0
index 670d4e7..67d64c9 100644
@@ -253,6 +253,9 @@ static inline void flush_xen_dcache_va_range(void *p, unsigned long size)
             : : "r" (_p), "m" (*_p));                                   \
 } while (0)
 
+/* Flush the dcache for an entire page. */
+void sync_page_to_ram(unsigned long mfn);
+
 /* Print a walk of an arbitrary page table */
 void dump_pt_walk(lpae_t *table, paddr_t addr);
 
xen/include/asm-x86/page.h +3 -0
index 7a46af5..abe35fb 100644
@@ -346,6 +346,9 @@ static inline uint32_t cacheattr_to_pte_flags(uint32_t cacheattr)
     return ((cacheattr & 4) << 5) | ((cacheattr & 3) << 3);
 }
 
+/* No cache maintenance required on x86 architecture. */
+static inline void sync_page_to_ram(unsigned long mfn) {}
+
 /* return true if permission increased */
 static inline bool_t
 perms_strictly_increased(uint32_t old_flags, uint32_t new_flags)
xen/include/public/domctl.h +13 -0
index 91f01fa..f22fe2e 100644
@@ -885,6 +885,17 @@ struct xen_domctl_set_max_evtchn {
 typedef struct xen_domctl_set_max_evtchn xen_domctl_set_max_evtchn_t;
 DEFINE_XEN_GUEST_HANDLE(xen_domctl_set_max_evtchn_t);
 
+/*
+ * ARM: Clean and invalidate caches associated with given region of
+ * guest memory.
+ */
+struct xen_domctl_cacheflush {
+    /* IN: page range to flush. */
+    xen_pfn_t start_pfn, nr_pfns;
+};
+typedef struct xen_domctl_cacheflush xen_domctl_cacheflush_t;
+DEFINE_XEN_GUEST_HANDLE(xen_domctl_cacheflush_t);
+
 struct xen_domctl {
     uint32_t cmd;
 #define XEN_DOMCTL_createdomain                   1
@@ -954,6 +965,7 @@ struct xen_domctl {
 #define XEN_DOMCTL_setnodeaffinity               68
 #define XEN_DOMCTL_getnodeaffinity               69
 #define XEN_DOMCTL_set_max_evtchn                70
+#define XEN_DOMCTL_cacheflush                    71
 #define XEN_DOMCTL_gdbsx_guestmemio            1000
 #define XEN_DOMCTL_gdbsx_pausevcpu             1001
 #define XEN_DOMCTL_gdbsx_unpausevcpu           1002
@@ -1012,6 +1024,7 @@ struct xen_domctl {
         struct xen_domctl_set_max_evtchn    set_max_evtchn;
         struct xen_domctl_gdbsx_memio       gdbsx_guest_memio;
         struct xen_domctl_set_broken_page_p2m set_broken_page_p2m;
+        struct xen_domctl_cacheflush        cacheflush;
         struct xen_domctl_gdbsx_pauseunp_vcpu gdbsx_pauseunp_vcpu;
         struct xen_domctl_gdbsx_domstatus   gdbsx_domstatus;
         uint8_t                             pad[128];
xen/xsm/flask/hooks.c +3 -0
index 50a35fc..1345d7e 100644
@@ -737,6 +737,9 @@ static int flask_domctl(struct domain *d, int cmd)
     case XEN_DOMCTL_set_max_evtchn:
         return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__SET_MAX_EVTCHN);
 
+    case XEN_DOMCTL_cacheflush:
+	    return current_has_perm(d, SECCLASS_DOMAIN2, DOMAIN2__CACHEFLUSH);
+
     default:
         printk("flask_domctl: Unknown op %d\n", cmd);
         return -EPERM;
xen/xsm/flask/policy/access_vectors +2 -0
index 1fbe241..a0ed13d 100644
@@ -196,6 +196,8 @@ class domain2
     setclaim
 # XEN_DOMCTL_set_max_evtchn
     set_max_evtchn
+# XEN_DOMCTL_cacheflush
+    cacheflush
 }
 
 # Similar to class domain, but primarily contains domctls related to HVM domains
Signature · 1 line
1.7.10.4
#12 lore raw

Re: [PATCH v4 3/5] xen/arm: clean and invalidate all guest caches by VMID after domain build.

Quoted 16 lines · Stefano Stabellini

tools/libxc/xc_dom_core.c void xc_dom_unmap_one(struct xc_dom_image *dom, xen_pfn_t pfn) · line 607

Quoted 10 lines
tools/libxc/xc_dom_core.c +2 -0
index 77a4e64..b9d1015 100644
@@ -603,6 +603,8 @@ void xc_dom_unmap_one(struct xc_dom_image *dom, xen_pfn_t pfn)
         prev->next = phys->next;
     else
         dom->phys_pages = phys->next;
+
+    xc_domain_cacheflush(dom->xch, dom->guest_domid, phys->first, phys->count);

I am not sure I understand the semantic of xc_dom_unmap_one: from the name of the function and the parameters I would think that it is supposed to unmap just one page. In that case we should flush just one page. However the implementation calls

Quoted 5 lines

munmap(phys->ptr, phys->count << page_shift)

so it actually can unmap more than a single page, so I think that you did the right thing by calling xc_domain_cacheflush with phys->first and phys->count.

"one" in the name means "one struct xc_dom_phys", which is a range starting at the given pfn, which is why using phys->{first,count} is correct.

Quoted 9 lines

tools/libxc/xc_dom_core.c void xc_dom_unmap_one(struct xc_dom_image *dom, xen_pfn_t pfn)

Quoted 6 lines
tools/libxc/xc_dom_core.c +6 -0
+void sync_page_to_ram(unsigned long mfn)
+{
+    void *p, *v = map_domain_page(mfn);
+
+    dsb();           /* So the CPU issues all writes to the range */
+    for ( p = v; p < v + PAGE_SIZE ; p += cacheline_bytes )

What about the last few bytes on a page?
Can we assume that PAGE_SIZE is a multiple of cacheline_bytes?

I sure hope so! A non power of two cache line size would be pretty crazy!

The cacheline is always a 2^N (the register contains log2(cacheline size)) and so is PAGE_SIZE.

Ian.

#13 lore raw

Re: [PATCH v4 3/5] xen/arm: clean and invalidate all guest caches by VMID after domain build.

Quoted 10 lines · Ian Campbell
Quoted 9 lines

xen/arch/arm/mm.c unsigned long domain_page_map_to_mfn(const void *va) · line 346

Quoted 6 lines
xen/arch/arm/mm.c +6 -0
+void sync_page_to_ram(unsigned long mfn)
+{
+    void *p, *v = map_domain_page(mfn);
+
+    dsb();           /* So the CPU issues all writes to the range */
+    for ( p = v; p < v + PAGE_SIZE ; p += cacheline_bytes )

What about the last few bytes on a page?
Can we assume that PAGE_SIZE is a multiple of cacheline_bytes?

Quoted 5 lines

I sure hope so! A non power of two cache line size would be pretty crazy!

The cacheline is always a 2^N (the register contains log2(cacheline size)) and so is PAGE_SIZE.

Thought so.

For the Xen ARM side:

#14 lore raw

PATCH v4 4/5 Revert "xen: arm: force guest memory accesses to cacheable when MMU is disabled"

In reply to Ian Campbell

This reverts commit 89eb02c2204a0b42a0aa169f107bc346a3fef802.

This approach has a short coming in that it breaks when a guest enables its MMU (SCTLR.M, disabling HCR.DC) without enabling caches (SCTLR.C) first/at the same time. It turns out that FreeBSD does this.

This has now been fixed (yet) another way (third time is the charm!) so remove this support. The original commit contained some fixes which are still relevant even with the revert of the bulk of the patch:
- Correction to HSR_SYSREG_CRN_MASK
- Rename of HSR_SYSCTL macros to avoid naming clash
- Definition of some additional cp reg specifications

Since these are still useful they are not reverted.

---
v2: Move to end of series
Do not revert useful bits

---
 xen/arch/arm/domain.c        |    7 --
 xen/arch/arm/traps.c         |  158 ------------------------------------------
 xen/include/asm-arm/domain.h |    2 -
 3 files changed, 167 deletions(-)
xen/arch/arm/domain.c +0 -7
index 635a9a4..124cccf 100644
@@ -19,7 +19,6 @@
 #include <xen/errno.h>
 #include <xen/bitops.h>
 #include <xen/grant_table.h>
-#include <xen/stdbool.h>
 
 #include <asm/current.h>
 #include <asm/event.h>
@@ -220,11 +219,6 @@ static void ctxt_switch_to(struct vcpu *n)
     else
         hcr |= HCR_RW;
 
-    if ( n->arch.default_cache )
-        hcr |= (HCR_TVM|HCR_DC);
-    else
-        hcr &= ~(HCR_TVM|HCR_DC);
-
     WRITE_SYSREG(hcr, HCR_EL2);
     isb();
 
@@ -475,7 +469,6 @@ int vcpu_initialise(struct vcpu *v)
         return rc;
 
     v->arch.sctlr = SCTLR_GUEST_INIT;
-    v->arch.default_cache = true;
 
     /*
      * By default exposes an SMP system with AFF0 set to the VCPU ID
xen/arch/arm/traps.c +0 -158

206 lines not on this page · raw message

xen/include/asm-arm/domain.h +0 -2
index af8c64b..bc20a15 100644
@@ -257,8 +257,6 @@ struct arch_vcpu
     uint64_t event_mask;
     uint64_t lr_mask;
 
-    bool_t default_cache;
-
     struct {
         /*
          * SGIs and PPIs are per-VCPU, SPIs are domain global and in
Signature · 1 line
1.7.10.4
#15 lore raw

PATCH v4 5/5 xen: arm: correct terminology for cache flush macros

In reply to Ian Campbell

The term "flush" is slightly ambiguous. The correct ARM term for for this operaton is clean, as opposed to clean+invalidate for which we also now have a function.

This is a pure rename, no functional change.

---
This could easily be left for 4.5.

---
 xen/arch/arm/guestcopy.c         |    2 +-
 xen/arch/arm/kernel.c            |    2 +-
 xen/arch/arm/mm.c                |   16 ++++++++--------
 xen/arch/arm/smpboot.c           |    2 +-
 xen/include/asm-arm/arm32/page.h |    2 +-
 xen/include/asm-arm/arm64/page.h |    2 +-
 xen/include/asm-arm/page.h       |   10 +++++-----
 7 files changed, 18 insertions(+), 18 deletions(-)
xen/arch/arm/guestcopy.c +1 -1
index bd0a355..af0af6b 100644
@@ -24,7 +24,7 @@ static unsigned long raw_copy_to_guest_helper(void *to, const void *from,
         p += offset;
         memcpy(p, from, size);
         if ( flush_dcache )
-            flush_xen_dcache_va_range(p, size);
+            clean_xen_dcache_va_range(p, size);
 
         unmap_domain_page(p - offset);
         len -= size;
xen/arch/arm/kernel.c +1 -1
index 6a5772b..1e3107d 100644
@@ -58,7 +58,7 @@ void copy_from_paddr(void *dst, paddr_t paddr, unsigned long len, int attrindx)
 
         set_fixmap(FIXMAP_MISC, p, attrindx);
         memcpy(dst, src + s, l);
-        flush_xen_dcache_va_range(dst, l);
+        clean_xen_dcache_va_range(dst, l);
 
         paddr += l;
         dst += l;
xen/arch/arm/mm.c +8 -8
index d2cfe64..4c5cff0 100644
@@ -480,13 +480,13 @@ void __init setup_pagetables(unsigned long boot_phys_offset, paddr_t xen_paddr)
     /* Clear the copy of the boot pagetables. Each secondary CPU
      * rebuilds these itself (see head.S) */
     memset(boot_pgtable, 0x0, PAGE_SIZE);
-    flush_xen_dcache(boot_pgtable);
+    clean_xen_dcache(boot_pgtable);
 #ifdef CONFIG_ARM_64
     memset(boot_first, 0x0, PAGE_SIZE);
-    flush_xen_dcache(boot_first);
+    clean_xen_dcache(boot_first);
 #endif
     memset(boot_second, 0x0, PAGE_SIZE);
-    flush_xen_dcache(boot_second);
+    clean_xen_dcache(boot_second);
 
     /* Break up the Xen mapping into 4k pages and protect them separately. */
     for ( i = 0; i < LPAE_ENTRIES; i++ )
@@ -524,7 +524,7 @@ void __init setup_pagetables(unsigned long boot_phys_offset, paddr_t xen_paddr)
 
     /* Make sure it is clear */
     memset(this_cpu(xen_dommap), 0, DOMHEAP_SECOND_PAGES*PAGE_SIZE);
-    flush_xen_dcache_va_range(this_cpu(xen_dommap),
+    clean_xen_dcache_va_range(this_cpu(xen_dommap),
                               DOMHEAP_SECOND_PAGES*PAGE_SIZE);
 #endif
 }
@@ -535,7 +535,7 @@ int init_secondary_pagetables(int cpu)
     /* Set init_ttbr for this CPU coming up. All CPus share a single setof
      * pagetables, but rewrite it each time for consistency with 32 bit. */
     init_ttbr = (uintptr_t) xen_pgtable + phys_offset;
-    flush_xen_dcache(init_ttbr);
+    clean_xen_dcache(init_ttbr);
     return 0;
 }
 #else
@@ -570,15 +570,15 @@ int init_secondary_pagetables(int cpu)
         write_pte(&first[first_table_offset(DOMHEAP_VIRT_START+i*FIRST_SIZE)], pte);
     }
 
-    flush_xen_dcache_va_range(first, PAGE_SIZE);
-    flush_xen_dcache_va_range(domheap, DOMHEAP_SECOND_PAGES*PAGE_SIZE);
+    clean_xen_dcache_va_range(first, PAGE_SIZE);
+    clean_xen_dcache_va_range(domheap, DOMHEAP_SECOND_PAGES*PAGE_SIZE);
 
     per_cpu(xen_pgtable, cpu) = first;
     per_cpu(xen_dommap, cpu) = domheap;
 
     /* Set init_ttbr for this CPU coming up */
     init_ttbr = __pa(first);
-    flush_xen_dcache(init_ttbr);
+    clean_xen_dcache(init_ttbr);
 
     return 0;
 }
xen/arch/arm/smpboot.c +1 -1
index c53c765..a829957 100644
@@ -378,7 +378,7 @@ int __cpu_up(unsigned int cpu)
 
     /* Open the gate for this CPU */
     smp_up_cpu = cpu_logical_map(cpu);
-    flush_xen_dcache(smp_up_cpu);
+    clean_xen_dcache(smp_up_cpu);
 
     rc = arch_cpu_up(cpu);
 
xen/include/asm-arm/arm32/page.h +1 -1
index cb6add4..b8221ca 100644
@@ -20,7 +20,7 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
 }
 
 /* Inline ASM to flush dcache on register R (may be an inline asm operand) */
-#define __flush_xen_dcache_one(R) STORE_CP32(R, DCCMVAC)
+#define __clean_xen_dcache_one(R) STORE_CP32(R, DCCMVAC)
 
 /* Inline ASM to clean and invalidate dcache on register R (may be an
  * inline asm operand) */
xen/include/asm-arm/arm64/page.h +1 -1
index baf8903..3352821 100644
@@ -15,7 +15,7 @@ static inline void write_pte(lpae_t *p, lpae_t pte)
 }
 
 /* Inline ASM to flush dcache on register R (may be an inline asm operand) */
-#define __flush_xen_dcache_one(R) "dc cvac, %" #R ";"
+#define __clean_xen_dcache_one(R) "dc cvac, %" #R ";"
 
 /* Inline ASM to clean and invalidate dcache on register R (may be an
  * inline asm operand) */
xen/include/asm-arm/page.h +5 -5
index 67d64c9..a577942 100644
@@ -229,26 +229,26 @@ extern size_t cacheline_bytes;
 /* Function for flushing medium-sized areas.
  * if 'range' is large enough we might want to use model-specific
  * full-cache flushes. */
-static inline void flush_xen_dcache_va_range(void *p, unsigned long size)
+static inline void clean_xen_dcache_va_range(void *p, unsigned long size)
 {
     void *end;
     dsb();           /* So the CPU issues all writes to the range */
     for ( end = p + size; p < end; p += cacheline_bytes )
-        asm volatile (__flush_xen_dcache_one(0) : : "r" (p));
+        asm volatile (__clean_xen_dcache_one(0) : : "r" (p));
     dsb();           /* So we know the flushes happen before continuing */
 }
 
 /* Macro for flushing a single small item.  The predicate is always
  * compile-time constant so this will compile down to 3 instructions in
  * the common case. */
-#define flush_xen_dcache(x) do {                                        \
+#define clean_xen_dcache(x) do {                                        \
     typeof(x) *_p = &(x);                                               \
     if ( sizeof(x) > MIN_CACHELINE_BYTES || sizeof(x) > alignof(x) )    \
-        flush_xen_dcache_va_range(_p, sizeof(x));                       \
+        clean_xen_dcache_va_range(_p, sizeof(x));                       \
     else                                                                \
         asm volatile (                                                  \
             "dsb sy;"   /* Finish all earlier writes */                 \
-            __flush_xen_dcache_one(0)                                   \
+            __clean_xen_dcache_one(0)                                   \
             "dsb sy;"   /* Finish flush before continuing */            \
             : : "r" (_p), "m" (*_p));                                   \
 } while (0)
Signature · 1 line
1.7.10.4
Acked-by on v4 5/5
#16 lore raw

Re: [PATCH v4 5/5] xen: arm: correct terminology for cache flush macros

Quoted 7 lines · Ian Campbell

The term "flush" is slightly ambiguous. The correct ARM term for for this operaton is clean, as opposed to clean+invalidate for which we also now have a function.

This is a pure rename, no functional change.

Quoted 2 lines

---
This could easily be left for 4.5.

It would be nice to have a common nomenclature for the functions (a bit like your TLB patch series).

But, if the patch don't go in Xen 4.4, it won't change anything to backporting. The function name is not reused.

Signature · 1 line
Julien Grall