diff mbox series

[v1,12/13] xen/arm: defer foreign memory map in shm_init_late

Message ID 20220311061123.1883189-13-Penny.Zheng@arm.com (mailing list archive)
State New, archived
Headers show
Series Static shared memory on dom0less system | expand

Commit Message

Penny Zheng March 11, 2022, 6:11 a.m. UTC
From: Penny Zheng <penny.zheng@arm.com>

This commit introduces a new helper shm_init_late to implement
deferred foreign memory mapping of static shared memory.

Signed-off-by: Penny Zheng <penny.zheng@arm.com>
---
 xen/arch/arm/domain_build.c       | 51 +++++++++++++++++++++++++++++++
 xen/arch/arm/include/asm/domain.h |  1 +
 xen/arch/arm/setup.c              |  6 ++++
 3 files changed, 58 insertions(+)
diff mbox series

Patch

diff --git a/xen/arch/arm/domain_build.c b/xen/arch/arm/domain_build.c
index 4b19160674..f6ef5a702f 100644
--- a/xen/arch/arm/domain_build.c
+++ b/xen/arch/arm/domain_build.c
@@ -1006,6 +1006,57 @@  static int __init process_shm(struct domain *d,
 
     return 0;
 }
+
+int __init shm_init_late(void)
+{
+    unsigned long i = 0UL, shm_id;
+    int ret = 0;
+    struct domain *od, **bd = NULL;
+    unsigned long o_gfn, b_gfn, nr_gfns;
+
+    for ( shm_id = find_first_bit(shm_list_mask, NR_MEM_BANKS);
+          shm_id < NR_MEM_BANKS;
+          shm_id = find_next_bit(shm_list_mask, NR_MEM_BANKS, shm_id + 1) )
+
+    {
+        /* Acquire the only owner domain. */
+        od = get_domain_by_id(shm_list[shm_id].owner_dom);
+        if ( od == NULL )
+            return -ESRCH;
+        o_gfn = PFN_DOWN(shm_list[shm_id].owner_gbase);
+        nr_gfns = PFN_DOWN(shm_list[shm_id].size);
+
+        bd = xmalloc_array(struct domain *, shm_list[shm_id].nr_borrower);
+        if ( !bd )
+            return -ENOMEM;
+        /* Set up foreign memory map for all borrower domains. */
+        for ( i = 0; i < shm_list[shm_id].nr_borrower; i++ )
+        {
+            bd[i] = get_domain_by_id(shm_list[shm_id].borrower_dom[i]);
+            if ( bd[i] == NULL )
+            {
+                return -ESRCH;
+                goto fail;
+            }
+
+            b_gfn = PFN_DOWN(shm_list[shm_id].borrower_gbase[i]);
+            ret = guest_physmap_add_shm(od, bd[i], o_gfn, b_gfn, nr_gfns);
+            if ( ret )
+            {
+                ret = -EINVAL;
+                goto fail;
+            }
+        }
+
+        xfree(bd);
+    }
+    return ret;
+
+ fail:
+    xfree(bd);
+
+    return ret;
+}
 #endif /* CONFIG_STATIC_SHM */
 #else
 static void __init allocate_static_memory(struct domain *d,
diff --git a/xen/arch/arm/include/asm/domain.h b/xen/arch/arm/include/asm/domain.h
index 1c0f2e22ca..c3f2155f5c 100644
--- a/xen/arch/arm/include/asm/domain.h
+++ b/xen/arch/arm/include/asm/domain.h
@@ -58,6 +58,7 @@  typedef struct {
 } shm_info_t;
 extern shm_info_t shm_list[NR_MEM_BANKS];
 extern unsigned long shm_list_mask[BITS_TO_LONGS(NR_MEM_BANKS)];
+extern int shm_init_late(void);
 #else
 #define dom_shared NULL
 #endif
diff --git a/xen/arch/arm/setup.c b/xen/arch/arm/setup.c
index f6a3b04958..4987b71111 100644
--- a/xen/arch/arm/setup.c
+++ b/xen/arch/arm/setup.c
@@ -1051,7 +1051,13 @@  void __init start_xen(unsigned long boot_phys_offset,
         printk(XENLOG_INFO "Xen dom0less mode detected\n");
 
     if ( acpi_disabled )
+    {
         create_domUs();
+#ifdef CONFIG_STATIC_SHM
+        if ( shm_init_late() )
+            panic("Failed to set up deferred foreign memory mapping of static shared memory.\n");
+#endif
+    }
 
     /*
      * This needs to be called **before** heap_init_late() so modules