[PATCH v1 12/13] xen/arm: implement "xen,offset" feature when host address not provided

Penny Zheng posted 13 patches 2 months, 2 weeks ago
[PATCH v1 12/13] xen/arm: implement "xen,offset" feature when host address not provided
Posted by Penny Zheng 2 months, 2 weeks ago
When host address is not provided in "xen,shared-mem" property, shared
memory region is allocated from heap by Xen. It is normally not
contiguous and consisted of multiple memory blocks.
Under above scenario, when "xen,offset" is also offered, we need to find at
which memory block the offset locates, and the borrower memory map shall start
at this block offset.

Signed-off-by: Penny Zheng <penny.zheng@arm.com>
---
 xen/arch/arm/domain_build.c | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c
index 95600c640c..494f6aff2e 100644
--- a/xen/arch/arm/domain_build.c
+++ b/xen/arch/arm/domain_build.c
@@ -48,6 +48,10 @@ boolean_param("ext_regions", opt_ext_regions);
 static u64 __initdata dom0_mem;
 static bool __initdata dom0_mem_set;
 
+#ifdef CONFIG_STATIC_SHM
+static uint64_t __initdata allocated = 0UL;
+#endif
+
 static int __init parse_dom0_mem(const char *s)
 {
     dom0_mem_set = true;
@@ -1110,6 +1114,26 @@ static int __init assign_shared_memory(struct domain *d,
     return ret;
 }
 
+static bool __init find_anchor_offset(paddr_t *bank_start, paddr_t *bank_size,
+                                      uint64_t offset)
+{
+    uint64_t bank_offset;
+
+    allocated += *bank_size;
+    if ( allocated <= offset )
+        return false;
+
+    /* Find the bank when offset locates */
+    bank_offset = *bank_size - (allocated - offset);
+    *bank_start += bank_offset;
+    *bank_size = allocated - offset;
+
+    /* Reset after finding the anchor */
+    allocated = 0UL;
+
+    return true;
+}
+
 static int __init borrower_physmap_add_memory(struct domain *d,
                                               struct shm_membank *shm_membank,
                                               paddr_t gbase, paddr_t offset)
@@ -1123,12 +1147,17 @@ static int __init borrower_physmap_add_memory(struct domain *d,
     /* Host address is not provided in "xen,shared-mem" */
     if ( shm_membank->mem.banks.meminfo )
     {
+        bool found = false;
         meminfo = shm_membank->mem.banks.meminfo;
+        sgfn = _gfn(PFN_UP(gbase));
+
         for ( i = 0; i < meminfo->nr_banks; i++ )
         {
             start = meminfo->bank[i].start;
             size = meminfo->bank[i].size;
-            sgfn = _gfn(PFN_UP(gbase));
+
+            if ( offset && !found )
+                found = find_anchor_offset(&start, &size, offset);
 
             /* Set up P2M foreign mapping for borrower domain. */
             ret = map_regions_p2mt(d, sgfn, PFN_DOWN(size), _mfn(PFN_UP(start)),
-- 
2.25.1