Skip to contentSkip to conversation

Early Boot Allocation on Power

PATCH Superseded Shawn Anastasio 3 participants 14 messages Apr 12, 2024 – Sep 27, 2024

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover Early Boot Allocation on Power

Hello all,

This series enables the Xen boot time allocator on Power by parsing the available memory regions from the firmware-provided device tree.

Thanks,
Shawn

Shawn Anastasio (6):
xen/ppc: Introduce stub asm/static-shmem.h xen/device-tree: Move Arm's setup.c bootinfo functions to common xen/common: Move Arm's bootfdt.c to common
xen/ppc: Enable bootfdt and boot allocator
xen/ppc: mm-radix: Replace debug printing code with printk xen/ppc: mm-radix: Allocate all paging structures at runtime

 MAINTAINERS                                   |   1 +
 xen/arch/arm/Makefile                         |   1 -
 xen/arch/arm/include/asm/setup.h              | 122 +----
 xen/arch/arm/setup.c                          | 419 ----------------
 xen/arch/ppc/include/asm/static-shmem.h       |  12 +
 xen/arch/ppc/mm-radix.c                       | 267 +++++------
 xen/arch/ppc/setup.c                          |  22 +-
 xen/common/Makefile                           |   1 +
 xen/common/device-tree/Makefile               |   2 +
 .../arm => common/device-tree}/bootfdt.c      |  22 +-
 xen/common/device-tree/bootinfo.c             | 446 ++++++++++++++++++
 xen/include/xen/bootfdt.h                     | 137 ++++++
 12 files changed, 759 insertions(+), 693 deletions(-)
 create mode 100644 xen/arch/ppc/include/asm/static-shmem.h
 create mode 100644 xen/common/device-tree/Makefile
 rename xen/{arch/arm => common/device-tree}/bootfdt.c (97%)
 create mode 100644 xen/common/device-tree/bootinfo.c
 create mode 100644 xen/include/xen/bootfdt.h

--
2.30.2

#6 lore raw

PATCH v4 3/6 xen/common: Move Arm's bootfdt.c to common

Move Arm's bootfdt.c to xen/common so that it can be used by other device tree architectures like PPC and RISCV.

---
Changes in v4:
- move function prototypes to patch 2's xen/include/bootfdt.h
- clean up #includes

 xen/arch/arm/Makefile                          |  1 -
 xen/arch/arm/include/asm/setup.h               | 13 -------------
 xen/common/device-tree/Makefile                |  1 +
 xen/{arch/arm => common/device-tree}/bootfdt.c | 11 +++++------
 xen/include/xen/bootfdt.h                      | 14 ++++++++++++++
 5 files changed, 20 insertions(+), 20 deletions(-)
 rename xen/{arch/arm => common/device-tree}/bootfdt.c (99%)
xen/arch/arm/Makefile +0 -1
index 7b1350e2ef..9e1548378c 100644
@@ -10,7 +10,6 @@ obj-$(CONFIG_TEE) += tee/
 obj-$(CONFIG_HAS_VPCI) += vpci.o
 
 obj-$(CONFIG_HAS_ALTERNATIVE) += alternative.o
-obj-y += bootfdt.init.o
 obj-y += cpuerrata.o
 obj-y += cpufeature.o
 obj-y += decode.o
xen/arch/arm/include/asm/setup.h +0 -13
index bf16afe31c..f955b7653a 100644
@@ -39,19 +39,6 @@ void fw_unreserved_regions(paddr_t s, paddr_t e,
                            void (*cb)(paddr_t ps, paddr_t pe),
                            unsigned int first);
 
-bool check_reserved_regions_overlap(paddr_t region_start, paddr_t region_size);
-
-struct bootmodule *add_boot_module(bootmodule_kind kind,
-                                   paddr_t start, paddr_t size, bool domU);
-struct bootmodule *boot_module_find_by_kind(bootmodule_kind kind);
-struct bootmodule * boot_module_find_by_addr_and_kind(bootmodule_kind kind,
-                                                             paddr_t start);
-void add_boot_cmdline(const char *name, const char *cmdline,
-                      bootmodule_kind kind, paddr_t start, bool domU);
-struct bootcmdline *boot_cmdline_find_by_kind(bootmodule_kind kind);
-struct bootcmdline * boot_cmdline_find_by_name(const char *name);
-const char *boot_module_kind_as_string(bootmodule_kind kind);
-
 void init_pdx(void);
 void setup_mm(void);
 
xen/common/device-tree/Makefile +1 -0
index 947bad979c..ff2de71c96 100644
@@ -1 +1,2 @@
+obj-y += bootfdt.init.o
 obj-y += bootinfo.init.o
xen/common/device-tree/bootfdt.c renamed from xen/arch/arm/bootfdt.c +5 -6
similarity index 99%
rename from xen/arch/arm/bootfdt.c
rename to xen/common/device-tree/bootfdt.c
index 35dbdf3384..f01a5b5d76 100644
@@ -4,13 +4,12 @@
  *
  * Copyright (C) 2012-2014 Citrix Systems, Inc.
  */
-#include <xen/types.h>
-#include <xen/lib.h>
-#include <xen/kernel.h>
-#include <xen/init.h>
-#include <xen/efi.h>
+
+#include <xen/bootfdt.h>
 #include <xen/device_tree.h>
-#include <xen/lib.h>
+#include <xen/efi.h>
+#include <xen/init.h>
+#include <xen/kernel.h>
 #include <xen/libfdt/libfdt-xen.h>
 #include <xen/sort.h>
 #include <xsm/xsm.h>
xen/include/xen/bootfdt.h +14 -0
index b0487bdbbd..577618da16 100644
@@ -107,6 +107,20 @@ struct bootinfo {
 
 extern struct bootinfo bootinfo;
 
+bool check_reserved_regions_overlap(paddr_t region_start,
+                                    paddr_t region_size);
+
+struct bootmodule *add_boot_module(bootmodule_kind kind,
+                                   paddr_t start, paddr_t size, bool domU);
+struct bootmodule *boot_module_find_by_kind(bootmodule_kind kind);
+struct bootmodule * boot_module_find_by_addr_and_kind(bootmodule_kind kind,
+                                                             paddr_t start);
+void add_boot_cmdline(const char *name, const char *cmdline,
+                      bootmodule_kind kind, paddr_t start, bool domU);
+struct bootcmdline *boot_cmdline_find_by_kind(bootmodule_kind kind);
+struct bootcmdline * boot_cmdline_find_by_name(const char *name);
+const char *boot_module_kind_as_string(bootmodule_kind kind);
+
 void populate_boot_allocator(void);
 
 size_t boot_fdt_info(const void *fdt, paddr_t paddr);

--
2.30.2

#9 lore raw

PATCH v4 5/6 xen/ppc: mm-radix: Replace debug printing code with printk

In reply to Shawn Anastasio

Now that we have common code building, there's no need to keep the old itoa64+debug print function in mm-radix.c

---
Changes in v4: none

 xen/arch/ppc/mm-radix.c | 58 +++++++++--------------------------------
 1 file changed, 12 insertions(+), 46 deletions(-)
xen/arch/ppc/mm-radix.c +12 -46
index daa411a6fa..ab5a10695c 100644
@@ -15,6 +15,12 @@
 
 void enable_mmu(void);
 
+#ifdef CONFIG_DEBUG
+#define radix_dprintk(msg, ...) printk(XENLOG_DEBUG msg, ## __VA_ARGS__)
+#else
+#define radix_dprintk(...)
+#endif
+
 #define INITIAL_LVL1_PD_COUNT      1
 #define INITIAL_LVL2_LVL3_PD_COUNT 2
 #define INITIAL_LVL4_PT_COUNT      256
@@ -80,45 +86,6 @@ static __init struct lvl4_pt *lvl4_pt_pool_alloc(void)
     return &initial_lvl4_pt_pool[initial_lvl4_pt_pool_used++];
 }
 
-#ifndef NDEBUG
-/* TODO: Remove once we get common/ building */
-static char *__init itoa64_hex(uint64_t val, char *out_buf, size_t buf_len)
-{
-    uint64_t cur;
-    size_t i = buf_len - 1;
-
-    /* Null terminate buffer */
-    out_buf[i] = '\0';
-
-    /* Add digits in reverse */
-    cur = val;
-    while ( cur && i > 0 )
-    {
-        out_buf[--i] = "0123456789ABCDEF"[cur % 16];
-        cur /= 16;
-    }
-
-    /* Pad to 16 digits */
-    while ( i > 0 )
-        out_buf[--i] = '0';
-
-    return out_buf + i;
-}
-#endif
-
-static void __init radix_dprint(uint64_t addr, const char *msg)
-{
-#ifndef NDEBUG
-    char buf[sizeof("DEADBEEFCAFEBABA")];
-    char *addr_s = itoa64_hex(addr, buf, sizeof(buf));
-
-    early_printk("(0x");
-    early_printk(addr_s);
-    early_printk(") ");
-    early_printk(msg);
-#endif
-}
-
 static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
                                          vaddr_t map_start,
                                          vaddr_t map_end,
@@ -186,27 +153,26 @@ static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
             unsigned long paddr = (page_addr - map_start) + phys_base;
             unsigned long flags;
 
-            radix_dprint(paddr, "being mapped to ");
-            radix_dprint(page_addr, "!\n");
+            radix_dprintk("%016lx being mapped to %016lx\n", paddr, page_addr);
             if ( is_kernel_text(page_addr) || is_kernel_inittext(page_addr) )
             {
-                radix_dprint(page_addr, "being marked as TEXT (RX)\n");
+                radix_dprintk("%016lx being marked as TEXT (RX)\n", page_addr);
                 flags = PTE_XEN_RX;
             }
             else if ( is_kernel_rodata(page_addr) )
             {
-                radix_dprint(page_addr, "being marked as RODATA (RO)\n");
+                radix_dprintk("%016lx being marked as RODATA (RO)\n", page_addr);
                 flags = PTE_XEN_RO;
             }
             else
             {
-                radix_dprint(page_addr, "being marked as DEFAULT (RW)\n");
+                radix_dprintk("%016lx being marked as DEFAULT (RW)\n", page_addr);
                 flags = PTE_XEN_RW;
             }
 
             *pte = paddr_to_pte(paddr, flags);
-            radix_dprint(paddr_to_pte(paddr, flags).pte,
-                             "is result of PTE map!\n");
+            radix_dprintk("%016lx is the result of PTE map\n",
+                paddr_to_pte(paddr, flags).pte);
         }
         else
         {

--
2.30.2

#10 lore raw

PATCH v4 6/6 xen/ppc: mm-radix: Allocate all paging structures at runtime

In reply to Shawn Anastasio

In the initial mm-radix implementation, the in-memory partition and process tables required to configure the MMU, as well as the page tables themselves were all allocated statically since the boot allocator was not yet available.

Now that it is, allocate these structures at runtime and bump the size of the Process Table to its maximum supported value (on POWER9).

---
Changes in v4:
- use mfn_add in initial_page_alloc()
- zero pages returned by initial_page_alloc()

 xen/arch/ppc/mm-radix.c | 231 +++++++++++++++++++++-------------------
 1 file changed, 123 insertions(+), 108 deletions(-)
xen/arch/ppc/mm-radix.c +123 -108
index ab5a10695c..8f4bfa87c0 100644
@@ -21,69 +21,105 @@ void enable_mmu(void);
 #define radix_dprintk(...)
 #endif
 
-#define INITIAL_LVL1_PD_COUNT      1
-#define INITIAL_LVL2_LVL3_PD_COUNT 2
-#define INITIAL_LVL4_PT_COUNT      256
-
-static size_t __initdata initial_lvl1_pd_pool_used;
-static struct lvl1_pd initial_lvl1_pd_pool[INITIAL_LVL1_PD_COUNT];
-
-static size_t __initdata initial_lvl2_lvl3_pd_pool_used;
-static struct lvl2_pd initial_lvl2_lvl3_pd_pool[INITIAL_LVL2_LVL3_PD_COUNT];
-
-static size_t __initdata initial_lvl4_pt_pool_used;
-static struct lvl4_pt initial_lvl4_pt_pool[INITIAL_LVL4_PT_COUNT];
-
-/* Only reserve minimum Partition and Process tables  */
 #define PATB_SIZE_LOG2 16 /* Only supported partition table size on POWER9 */
 #define PATB_SIZE      (1UL << PATB_SIZE_LOG2)
-#define PRTB_SIZE_LOG2 12
+#define PRTB_SIZE_LOG2 24 /* Maximum process table size on POWER9 */
 #define PRTB_SIZE      (1UL << PRTB_SIZE_LOG2)
 
-static struct patb_entry
-    __aligned(PATB_SIZE) initial_patb[PATB_SIZE / sizeof(struct patb_entry)];
+static struct patb_entry *initial_patb;
+static struct prtb_entry *initial_prtb;
 
-static struct prtb_entry
-    __aligned(PRTB_SIZE) initial_prtb[PRTB_SIZE / sizeof(struct prtb_entry)];
+static mfn_t __initdata min_alloc_mfn = {-1};
+static mfn_t __initdata max_alloc_mfn = {0};
 
-static __init struct lvl1_pd *lvl1_pd_pool_alloc(void)
+/*
+ * A thin wrapper for alloc_boot_pages that keeps track of the maximum and
+ * minimum mfns that have been allocated. This information is used by
+ * setup_initial_mapping to include the allocated pages in the initial
+ * page mapping.
+ *
+ * Additionally, allocated pages are zeroed before return.
+ */
+static mfn_t __init initial_page_alloc(unsigned long nr_pfns,
+                                       unsigned long pfn_align)
 {
-    if ( initial_lvl1_pd_pool_used >= INITIAL_LVL1_PD_COUNT )
-    {
-        early_printk("Ran out of space for LVL1 PD!\n");
-        die();
-    }
+    mfn_t mfn_first, mfn_last;
 
-    return &initial_lvl1_pd_pool[initial_lvl1_pd_pool_used++];
-}
+    mfn_first = alloc_boot_pages(nr_pfns, pfn_align);
+    mfn_last = mfn_add(mfn_first, nr_pfns - 1);
 
-static __init struct lvl2_pd *lvl2_pd_pool_alloc(void)
-{
-    if ( initial_lvl2_lvl3_pd_pool_used >= INITIAL_LVL2_LVL3_PD_COUNT )
-    {
-        early_printk("Ran out of space for LVL2/3 PD!\n");
-        die();
-    }
+    min_alloc_mfn = _mfn(min(mfn_x(min_alloc_mfn), mfn_x(mfn_first)));
+    max_alloc_mfn = _mfn(max(mfn_x(max_alloc_mfn), mfn_x(mfn_last)));
 
-    return &initial_lvl2_lvl3_pd_pool[initial_lvl2_lvl3_pd_pool_used++];
+    memset(__va(mfn_to_maddr(mfn_first)), 0, nr_pfns << PAGE_SHIFT);
+
+    return mfn_first;
 }
 
-static __init struct lvl3_pd *lvl3_pd_pool_alloc(void)
+static __init void *initial_pd_pt_alloc(void)
 {
-    BUILD_BUG_ON(sizeof(struct lvl3_pd) != sizeof(struct lvl2_pd));
+    BUILD_BUG_ON(sizeof(struct lvl1_pd) > PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct lvl2_pd) > PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct lvl3_pd) > PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct lvl4_pt) > PAGE_SIZE);
 
-    return (struct lvl3_pd *) lvl2_pd_pool_alloc();
+    return __va(mfn_to_maddr(initial_page_alloc(1, 1)));
 }
 
-static __init struct lvl4_pt *lvl4_pt_pool_alloc(void)
+static void map_page_initial(struct lvl1_pd *lvl1, vaddr_t virt, paddr_t phys,
+                             unsigned long flags)
 {
-    if ( initial_lvl4_pt_pool_used >= INITIAL_LVL4_PT_COUNT )
+    struct lvl2_pd *lvl2;
+    struct lvl3_pd *lvl3;
+    struct lvl4_pt *lvl4;
+    pde_t *pde;
+    pte_t *pte;
+
+    /* Allocate LVL 2 PD if necessary */
+    pde = pt_entry(lvl1, virt);
+    if ( !pde_is_valid(*pde) )
     {
-        early_printk("Ran out of space for LVL4 PT!\n");
-        die();
+        lvl2 = initial_pd_pt_alloc();
+        *pde = paddr_to_pde(__pa(lvl2), PDE_VALID,
+                            XEN_PT_ENTRIES_LOG2_LVL_2);
+    }
+    else
+        lvl2 = __va(pde_to_paddr(*pde));
+
+    /* Allocate LVL 3 PD if necessary */
+    pde = pt_entry(lvl2, virt);
+    if ( !pde_is_valid(*pde) )
+    {
+        lvl3 = initial_pd_pt_alloc();
+        *pde = paddr_to_pde(__pa(lvl3), PDE_VALID,
+                            XEN_PT_ENTRIES_LOG2_LVL_3);
+    }
+    else
+        lvl3 = __va(pde_to_paddr(*pde));
+
+    /* Allocate LVL 4 PT if necessary */
+    pde = pt_entry(lvl3, virt);
+    if ( !pde_is_valid(*pde) )
+    {
+        lvl4 = initial_pd_pt_alloc();
+        *pde = paddr_to_pde(__pa(lvl4), PDE_VALID,
+                            XEN_PT_ENTRIES_LOG2_LVL_4);
     }
+    else
+        lvl4 = __va(pde_to_paddr(*pde));
 
-    return &initial_lvl4_pt_pool[initial_lvl4_pt_pool_used++];
+    /* Finally, create PTE in LVL 4 PT */
+    pte = pt_entry(lvl4, virt);
+    if ( !pte_is_valid(*pte) )
+    {
+        radix_dprintk("%016lx being mapped to %016lx\n", phys, virt);
+        *pte = paddr_to_pte(phys, flags);
+    }
+    else
+    {
+        early_printk("BUG: Tried to create PTE for already-mapped page!");
+        die();
+    }
 }
 
 static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
@@ -92,6 +128,7 @@ static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
                                          paddr_t phys_base)
 {
     uint64_t page_addr;
+    mfn_t previous_max_alloc_mfn;
 
     if ( map_start & ~PAGE_MASK )
     {
@@ -105,81 +142,47 @@ static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
         die();
     }
 
+    /* Identity map Xen itself */
     for ( page_addr = map_start; page_addr < map_end; page_addr += PAGE_SIZE )
     {
-        struct lvl2_pd *lvl2;
-        struct lvl3_pd *lvl3;
-        struct lvl4_pt *lvl4;
-        pde_t *pde;
-        pte_t *pte;
-
-        /* Allocate LVL 2 PD if necessary */
-        pde = pt_entry(lvl1, page_addr);
-        if ( !pde_is_valid(*pde) )
-        {
-            lvl2 = lvl2_pd_pool_alloc();
-            *pde = paddr_to_pde(__pa(lvl2), PDE_VALID,
-                                XEN_PT_ENTRIES_LOG2_LVL_2);
-        }
-        else
-            lvl2 = __va(pde_to_paddr(*pde));
+        unsigned long flags;
 
-        /* Allocate LVL 3 PD if necessary */
-        pde = pt_entry(lvl2, page_addr);
-        if ( !pde_is_valid(*pde) )
+        if ( is_kernel_text(page_addr) || is_kernel_inittext(page_addr) )
         {
-            lvl3 = lvl3_pd_pool_alloc();
-            *pde = paddr_to_pde(__pa(lvl3), PDE_VALID,
-                                XEN_PT_ENTRIES_LOG2_LVL_3);
+            radix_dprintk("%016lx being marked as TEXT (RX)\n", page_addr);
+            flags = PTE_XEN_RX;
         }
-        else
-            lvl3 = __va(pde_to_paddr(*pde));
-
-        /* Allocate LVL 4 PT if necessary */
-        pde = pt_entry(lvl3, page_addr);
-        if ( !pde_is_valid(*pde) )
+        else if ( is_kernel_rodata(page_addr) )
         {
-            lvl4 = lvl4_pt_pool_alloc();
-            *pde = paddr_to_pde(__pa(lvl4), PDE_VALID,
-                                XEN_PT_ENTRIES_LOG2_LVL_4);
+            radix_dprintk("%016lx being marked as RODATA (RO)\n", page_addr);
+            flags = PTE_XEN_RO;
         }
         else
-            lvl4 = __va(pde_to_paddr(*pde));
-
-        /* Finally, create PTE in LVL 4 PT */
-        pte = pt_entry(lvl4, page_addr);
-        if ( !pte_is_valid(*pte) )
         {
-            unsigned long paddr = (page_addr - map_start) + phys_base;
-            unsigned long flags;
-
-            radix_dprintk("%016lx being mapped to %016lx\n", paddr, page_addr);
-            if ( is_kernel_text(page_addr) || is_kernel_inittext(page_addr) )
-            {
-                radix_dprintk("%016lx being marked as TEXT (RX)\n", page_addr);
-                flags = PTE_XEN_RX;
-            }
-            else if ( is_kernel_rodata(page_addr) )
-            {
-                radix_dprintk("%016lx being marked as RODATA (RO)\n", page_addr);
-                flags = PTE_XEN_RO;
-            }
-            else
-            {
-                radix_dprintk("%016lx being marked as DEFAULT (RW)\n", page_addr);
-                flags = PTE_XEN_RW;
-            }
-
-            *pte = paddr_to_pte(paddr, flags);
-            radix_dprintk("%016lx is the result of PTE map\n",
-                paddr_to_pte(paddr, flags).pte);
-        }
-        else
-        {
-            early_printk("BUG: Tried to create PTE for already-mapped page!");
-            die();
+            radix_dprintk("%016lx being marked as DEFAULT (RW)\n", page_addr);
+            flags = PTE_XEN_RW;
         }
+
+        map_page_initial(lvl1, page_addr, (page_addr - map_start) + phys_base, flags);
+    }
+
+    previous_max_alloc_mfn = max_alloc_mfn;
+
+    /*
+     * Identity map all pages we've allocated for paging structures. This act
+     * itself will allocate more pages, so continue until we've mapped from
+     * `max_alloc_mfn` down to `min_alloc_mfn`. This assumes that the heap grows
+     * downwards, which matches the behavior of alloc_boot_pages.
+     */
+    for ( page_addr = (vaddr_t)__va(mfn_to_maddr(max_alloc_mfn));
+          mfn_to_maddr(min_alloc_mfn) <= __pa(page_addr);
+          page_addr -= PAGE_SIZE)
+    {
+        map_page_initial(lvl1, page_addr, __pa(page_addr), PTE_XEN_RW);
     }
+
+    if ( mfn_x(previous_max_alloc_mfn) != mfn_x(max_alloc_mfn) )
+        panic("Early page heap unexpectedly grew upwards\n");
 }
 
 static void __init setup_partition_table(struct lvl1_pd *root)
@@ -208,9 +211,21 @@ static void __init setup_process_table(struct lvl1_pd *root)
 
 void __init setup_initial_pagetables(void)
 {
-    struct lvl1_pd *root = lvl1_pd_pool_alloc();
+    struct lvl1_pd *root;
     unsigned long lpcr;
+    mfn_t patb_mfn, prtb_mfn;
+
+    /* Allocate mfns for in-memory tables using the boot allocator */
+    prtb_mfn = initial_page_alloc(PRTB_SIZE / PAGE_SIZE,
+                                  1 << (PRTB_SIZE_LOG2 - PAGE_SHIFT));
+    patb_mfn = initial_page_alloc(PATB_SIZE / PAGE_SIZE,
+                                  1 << (PATB_SIZE_LOG2 - PAGE_SHIFT));
+
+    initial_patb = __va(mfn_to_maddr(patb_mfn));
+    initial_prtb = __va(mfn_to_maddr(prtb_mfn));
 
+    /* Allocate and create page tables */
+    root = initial_pd_pt_alloc();
     setup_initial_mapping(root, (vaddr_t)_start, (vaddr_t)_end, __pa(_start));
 
     /* Enable Radix mode in LPCR */

--
2.30.2

#1 lore raw

PATCH v4 1/6 xen/ppc: Introduce stub asm/static-shmem.h

In reply to Shawn Anastasio

Required for bootfdt.c to build.

---
Changes in v4: none

 xen/arch/ppc/include/asm/static-shmem.h | 12 ++++++++++++
 1 file changed, 12 insertions(+)
 create mode 100644 xen/arch/ppc/include/asm/static-shmem.h
xen/arch/ppc/include/asm/static-shmem.h new file +12 -0
new file mode 100644
index 0000000000..84370d6e6c
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier:  GPL-2.0-only */
+
+#ifndef __ASM_PPC_STATIC_SHMEM_H__
+#define __ASM_PPC_STATIC_SHMEM_H__
+
+static inline int process_shm_node(const void *fdt, int node,
+                                   uint32_t address_cells, uint32_t size_cells)
+{
+    return -EINVAL;
+}
+
+#endif /* __ASM_PPC_STATIC_SHMEM_H__ */

--
2.30.2

#3 lore raw

PATCH v4 2/6 xen/device-tree: Move Arm's setup.c bootinfo functions to common

In reply to Shawn Anastasio

Arm's setup.c contains a collection of functions for parsing memory map and other boot information from a device tree. Since these routines are generally useful on any architecture that supports device tree booting, move them into xen/common/device-tree.

---
Changes in v4:
- create new xen/include/bootinfo.h rather than relying on arch's asm/setup.h to provide required definitions for bootinfo.c
- build bootinfo.c as .init.o
- clean up and sort bootinfo.c's #includes
- use CONFIG_SEPARATE_XENHEAP rather than CONFIG_ARM_32 to guard xenheap-specific behavior of populate_boot_allocator
- (MAINTAINERS) include all of common/device-tree rather than just bootinfo.c

 MAINTAINERS                       |   1 +
 xen/arch/arm/include/asm/setup.h  | 109 +-------
 xen/arch/arm/setup.c              | 419 ----------------------------
 xen/common/Makefile               |   1 +
 xen/common/device-tree/Makefile   |   1 +
 xen/common/device-tree/bootinfo.c | 446 ++++++++++++++++++++++++++++++
 xen/include/xen/bootfdt.h         | 116 ++++++++
 7 files changed, 566 insertions(+), 527 deletions(-)
 create mode 100644 xen/common/device-tree/Makefile
 create mode 100644 xen/common/device-tree/bootinfo.c
 create mode 100644 xen/include/xen/bootfdt.h
MAINTAINERS +1 -0
index d1850c134d..13adb794b3 100644
@@ -295,6 +295,7 @@ M:	Stefano Stabellini <[email protected]>
 M:	Julien Grall <[email protected]>
 S:	Supported
 F:	xen/common/libfdt/
+F:	xen/common/device-tree/
 F:	xen/common/device_tree.c
 F:	xen/common/dt-overlay.c
 F:	xen/include/xen/libfdt/
xen/arch/arm/include/asm/setup.h +1 -108

133 lines not on this page · raw message

xen/arch/arm/setup.c +0 -419

443 lines not on this page · raw message

xen/common/Makefile +1 -0
index e5eee19a85..3a39dd35f2 100644
@@ -76,6 +76,7 @@ obj-$(CONFIG_UBSAN) += ubsan/
 
 obj-$(CONFIG_NEEDS_LIBELF) += libelf/
 obj-$(CONFIG_HAS_DEVICE_TREE) += libfdt/
+obj-$(CONFIG_HAS_DEVICE_TREE) += device-tree/
 
 CONF_FILE := $(if $(patsubst /%,,$(KCONFIG_CONFIG)),$(objtree)/)$(KCONFIG_CONFIG)
 $(obj)/config.gz: $(CONF_FILE)
xen/common/device-tree/Makefile new file +1 -0
new file mode 100644
index 0000000000..947bad979c
@@ -0,0 +1 @@
+obj-y += bootinfo.init.o
xen/common/device-tree/bootinfo.c new file +446 -0

446 lines not on this page · raw message

xen/include/xen/bootfdt.h new file +116 -0

116 lines not on this page · raw message

--
2.30.2

#7 lore raw

PATCH v4 4/6 xen/ppc: Enable bootfdt and boot allocator

In reply to Shawn Anastasio

Enable usage of bootfdt for populating the boot info struct from the firmware-provided device tree. Also enable the Xen boot page allocator.

Additionally, modify bootfdt.c's boot_fdt_info() to tolerate the scenario in which the FDT overlaps a reserved memory region, as is the case on PPC when booted directly from skiboot. Since this means that Xen can now boot without a BOOTMOD_FDT present in bootinfo, clarify this fact in a comment above BOOTMOD_FDT's definition.

---
Changes in v4:
- drop unnecessary libfdt.h include in setup.c
- make boot_fdt and xen_bootmodule const
- more clearly document that BOOTMOD_FDT is now optional
- add explicit (void) cast to BOOTMOD_FDT creation

 xen/arch/ppc/setup.c             | 22 +++++++++++++++++++++-
 xen/common/device-tree/bootfdt.c | 11 +++++++++--
 xen/include/xen/bootfdt.h        |  7 +++++++
 3 files changed, 37 insertions(+), 3 deletions(-)
xen/arch/ppc/setup.c +21 -1
index 101bdd8bb6..47e997969f 100644
@@ -1,12 +1,15 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
 #include <xen/init.h>
 #include <xen/lib.h>
+#include <xen/bootfdt.h>
+#include <xen/device_tree.h>
 #include <xen/mm.h>
 #include <public/version.h>
 #include <asm/boot.h>
 #include <asm/early_printk.h>
 #include <asm/mm.h>
 #include <asm/processor.h>
+#include <asm/setup.h>
 
 /* Xen stack for bringing up the first CPU. */
 unsigned char __initdata cpu0_boot_stack[STACK_SIZE] __aligned(STACK_SIZE);
@@ -24,6 +27,9 @@ void __init noreturn start_xen(unsigned long r3, unsigned long r4,
                                unsigned long r5, unsigned long r6,
                                unsigned long r7)
 {
+    const void *boot_fdt;
+    const struct bootmodule *xen_bootmodule;
+
     if ( r5 )
     {
         /* Unsupported OpenFirmware boot protocol */
@@ -32,11 +38,25 @@ void __init noreturn start_xen(unsigned long r3, unsigned long r4,
     else
     {
         /* kexec boot protocol */
-        boot_opal_init((void *)r3);
+        boot_fdt = (void *)r3;
+        boot_opal_init(boot_fdt);
     }
 
     setup_exceptions();
 
+    device_tree_flattened = boot_fdt;
+    boot_fdt_info(boot_fdt, r3);
+
+    /*
+     * Xen relocates itself at the ppc64 entrypoint, so we need to manually mark
+     * the kernel module.
+     */
+    xen_bootmodule = add_boot_module(BOOTMOD_XEN, __pa(_start),
+                                     PAGE_ALIGN(__pa(_end)), false);
+    BUG_ON(!xen_bootmodule);
+
+    populate_boot_allocator();
+
     setup_initial_pagetables();
 
     early_printk("Hello, ppc64le!\n");
xen/common/device-tree/bootfdt.c +9 -2
index f01a5b5d76..76d0f72ef9 100644
@@ -542,12 +542,19 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
     if ( ret < 0 )
         panic("No valid device tree\n");
 
-    add_boot_module(BOOTMOD_FDT, paddr, fdt_totalsize(fdt), false);
-
     ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);
     if ( ret )
         panic("Early FDT parsing failed (%d)\n", ret);
 
+    /*
+     * Add module for the FDT itself after the device tree has been parsed. This
+     * is required on ppc64le where the device tree passed to Xen may have been
+     * allocated by skiboot, in which case it will exist within a reserved
+     * region and this call will fail. This is fine, however, since either way
+     * the allocator will know not to step on the device tree.
+     */
+    (void)add_boot_module(BOOTMOD_FDT, paddr, fdt_totalsize(fdt), false);
+
     /*
      * On Arm64 setup_directmap_mappings() expects to be called with the lowest
      * bank in memory first. There is no requirement that the DT will provide
xen/include/xen/bootfdt.h +7 -0
index 577618da16..ea3ad96bb9 100644
@@ -13,7 +13,14 @@
 
 typedef enum {
     BOOTMOD_XEN,
+
+    /*
+     * The BOOTMOD_FDT type will only be present when the firmware-provided FDT
+     * blob exists outside of a reserved memory region which is platform-
+     * dependent and may not be relied upon.
+     */
     BOOTMOD_FDT,
+
     BOOTMOD_KERNEL,
     BOOTMOD_RAMDISK,
     BOOTMOD_XSM,

--
2.30.2

#4 lore raw

Re: [PATCH v4 2/6] xen/device-tree: Move Arm's setup.c bootinfo functions to common

In reply to Shawn Anastasio
Quoted 18 lines · Shawn Anastasio

Arm's setup.c contains a collection of functions for parsing memory map and other boot information from a device tree. Since these routines are generally useful on any architecture that supports device tree booting, move them into xen/common/device-tree.

---
Changes in v4:
- create new xen/include/bootinfo.h rather than relying on arch's asm/setup.h to provide required definitions for bootinfo.c
- build bootinfo.c as .init.o
- clean up and sort bootinfo.c's #includes
- use CONFIG_SEPARATE_XENHEAP rather than CONFIG_ARM_32 to guard xenheap-specific behavior of populate_boot_allocator
- (MAINTAINERS) include all of common/device-tree rather than just bootinfo.c

Quoted 11 lines
 MAINTAINERS                       |   1 +
 xen/arch/arm/include/asm/setup.h  | 109 +-------
 xen/arch/arm/setup.c              | 419 ----------------------------
 xen/common/Makefile               |   1 +
 xen/common/device-tree/Makefile   |   1 +
 xen/common/device-tree/bootinfo.c | 446 ++++++++++++++++++++++++++++++
 xen/include/xen/bootfdt.h         | 116 ++++++++
 7 files changed, 566 insertions(+), 527 deletions(-)
 create mode 100644 xen/common/device-tree/Makefile
 create mode 100644 xen/common/device-tree/bootinfo.c
 create mode 100644 xen/include/xen/bootfdt.h

Should this further new file ...

MAINTAINERS M: Stefano Stabellini <[email protected]> · line 301

Quoted 10 lines
MAINTAINERS +1 -0
@@ -295,6 +295,7 @@ M:	Stefano Stabellini <[email protected]>
 M:	Julien Grall <[email protected]>
 S:	Supported
 F:	xen/common/libfdt/
+F:	xen/common/device-tree/
 F:	xen/common/device_tree.c
 F:	xen/common/dt-overlay.c
 F:	xen/include/xen/libfdt/

... perhaps also be added here?

Jan

#11 lore raw

Re: [PATCH v4 6/6] xen/ppc: mm-radix: Allocate all paging structures at runtime

In reply to Shawn Anastasio
Quoted 51 lines · Shawn Anastasio

In the initial mm-radix implementation, the in-memory partition and process tables required to configure the MMU, as well as the page tables themselves were all allocated statically since the boot allocator was not yet available.

Now that it is, allocate these structures at runtime and bump the size of the Process Table to its maximum supported value (on POWER9).

---
Changes in v4:
- use mfn_add in initial_page_alloc()
- zero pages returned by initial_page_alloc()

 xen/arch/ppc/mm-radix.c | 231 +++++++++++++++++++++-------------------
 1 file changed, 123 insertions(+), 108 deletions(-)
xen/arch/ppc/mm-radix.c +3 -18
index ab5a10695c..8f4bfa87c0 100644
@@ -21,69 +21,105 @@ void enable_mmu(void);
 #define radix_dprintk(...)
 #endif
 
-#define INITIAL_LVL1_PD_COUNT      1
-#define INITIAL_LVL2_LVL3_PD_COUNT 2
-#define INITIAL_LVL4_PT_COUNT      256
-
-static size_t __initdata initial_lvl1_pd_pool_used;
-static struct lvl1_pd initial_lvl1_pd_pool[INITIAL_LVL1_PD_COUNT];
-
-static size_t __initdata initial_lvl2_lvl3_pd_pool_used;
-static struct lvl2_pd initial_lvl2_lvl3_pd_pool[INITIAL_LVL2_LVL3_PD_COUNT];
-
-static size_t __initdata initial_lvl4_pt_pool_used;
-static struct lvl4_pt initial_lvl4_pt_pool[INITIAL_LVL4_PT_COUNT];
-
-/* Only reserve minimum Partition and Process tables  */
 #define PATB_SIZE_LOG2 16 /* Only supported partition table size on POWER9 */
 #define PATB_SIZE      (1UL << PATB_SIZE_LOG2)
-#define PRTB_SIZE_LOG2 12
+#define PRTB_SIZE_LOG2 24 /* Maximum process table size on POWER9 */
 #define PRTB_SIZE      (1UL << PRTB_SIZE_LOG2)
 
-static struct patb_entry
-    __aligned(PATB_SIZE) initial_patb[PATB_SIZE / sizeof(struct patb_entry)];
+static struct patb_entry *initial_patb;
+static struct prtb_entry *initial_prtb;
 
-static struct prtb_entry

xen/arch/ppc/mm-radix.c void enable_mmu(void); · line 39

Quoted 10 lines
xen/arch/ppc/mm-radix.c +7 -2
-    __aligned(PRTB_SIZE) initial_prtb[PRTB_SIZE / sizeof(struct prtb_entry)];
+static mfn_t __initdata min_alloc_mfn = {-1};
+static mfn_t __initdata max_alloc_mfn = {0};
 
-static __init struct lvl1_pd *lvl1_pd_pool_alloc(void)
+/*
+ * A thin wrapper for alloc_boot_pages that keeps track of the maximum and
+ * minimum mfns that have been allocated. This information is used by
+ * setup_initial_mapping to include the allocated pages in the initial
+ * page mapping.

Plus everything in between. Together with the further comment below I'm afraid I still think that the constraints (and the justification for them being tolerable at least for the time being) aren't made sufficiently clear (perhaps in another code comment next to the respective two variables' definitions).

Quoted 205 lines
xen/arch/ppc/mm-radix.c +80 -87
+ * Additionally, allocated pages are zeroed before return.
+ */
+static mfn_t __init initial_page_alloc(unsigned long nr_pfns,
+                                       unsigned long pfn_align)
 {
-    if ( initial_lvl1_pd_pool_used >= INITIAL_LVL1_PD_COUNT )
-    {
-        early_printk("Ran out of space for LVL1 PD!\n");
-        die();
-    }
+    mfn_t mfn_first, mfn_last;
 
-    return &initial_lvl1_pd_pool[initial_lvl1_pd_pool_used++];
-}
+    mfn_first = alloc_boot_pages(nr_pfns, pfn_align);
+    mfn_last = mfn_add(mfn_first, nr_pfns - 1);
 
-static __init struct lvl2_pd *lvl2_pd_pool_alloc(void)
-{
-    if ( initial_lvl2_lvl3_pd_pool_used >= INITIAL_LVL2_LVL3_PD_COUNT )
-    {
-        early_printk("Ran out of space for LVL2/3 PD!\n");
-        die();
-    }
+    min_alloc_mfn = _mfn(min(mfn_x(min_alloc_mfn), mfn_x(mfn_first)));
+    max_alloc_mfn = _mfn(max(mfn_x(max_alloc_mfn), mfn_x(mfn_last)));
 
-    return &initial_lvl2_lvl3_pd_pool[initial_lvl2_lvl3_pd_pool_used++];
+    memset(__va(mfn_to_maddr(mfn_first)), 0, nr_pfns << PAGE_SHIFT);
+
+    return mfn_first;
 }
 
-static __init struct lvl3_pd *lvl3_pd_pool_alloc(void)
+static __init void *initial_pd_pt_alloc(void)
 {
-    BUILD_BUG_ON(sizeof(struct lvl3_pd) != sizeof(struct lvl2_pd));
+    BUILD_BUG_ON(sizeof(struct lvl1_pd) > PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct lvl2_pd) > PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct lvl3_pd) > PAGE_SIZE);
+    BUILD_BUG_ON(sizeof(struct lvl4_pt) > PAGE_SIZE);
 
-    return (struct lvl3_pd *) lvl2_pd_pool_alloc();
+    return __va(mfn_to_maddr(initial_page_alloc(1, 1)));
 }
 
-static __init struct lvl4_pt *lvl4_pt_pool_alloc(void)
+static void map_page_initial(struct lvl1_pd *lvl1, vaddr_t virt, paddr_t phys,
+                             unsigned long flags)
 {
-    if ( initial_lvl4_pt_pool_used >= INITIAL_LVL4_PT_COUNT )
+    struct lvl2_pd *lvl2;
+    struct lvl3_pd *lvl3;
+    struct lvl4_pt *lvl4;
+    pde_t *pde;
+    pte_t *pte;
+
+    /* Allocate LVL 2 PD if necessary */
+    pde = pt_entry(lvl1, virt);
+    if ( !pde_is_valid(*pde) )
     {
-        early_printk("Ran out of space for LVL4 PT!\n");
-        die();
+        lvl2 = initial_pd_pt_alloc();
+        *pde = paddr_to_pde(__pa(lvl2), PDE_VALID,
+                            XEN_PT_ENTRIES_LOG2_LVL_2);
+    }
+    else
+        lvl2 = __va(pde_to_paddr(*pde));
+
+    /* Allocate LVL 3 PD if necessary */
+    pde = pt_entry(lvl2, virt);
+    if ( !pde_is_valid(*pde) )
+    {
+        lvl3 = initial_pd_pt_alloc();
+        *pde = paddr_to_pde(__pa(lvl3), PDE_VALID,
+                            XEN_PT_ENTRIES_LOG2_LVL_3);
+    }
+    else
+        lvl3 = __va(pde_to_paddr(*pde));
+
+    /* Allocate LVL 4 PT if necessary */
+    pde = pt_entry(lvl3, virt);
+    if ( !pde_is_valid(*pde) )
+    {
+        lvl4 = initial_pd_pt_alloc();
+        *pde = paddr_to_pde(__pa(lvl4), PDE_VALID,
+                            XEN_PT_ENTRIES_LOG2_LVL_4);
     }
+    else
+        lvl4 = __va(pde_to_paddr(*pde));
 
-    return &initial_lvl4_pt_pool[initial_lvl4_pt_pool_used++];
+    /* Finally, create PTE in LVL 4 PT */
+    pte = pt_entry(lvl4, virt);
+    if ( !pte_is_valid(*pte) )
+    {
+        radix_dprintk("%016lx being mapped to %016lx\n", phys, virt);
+        *pte = paddr_to_pte(phys, flags);
+    }
+    else
+    {
+        early_printk("BUG: Tried to create PTE for already-mapped page!");
+        die();
+    }
 }
 
 static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
@@ -92,6 +128,7 @@ static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
                                          paddr_t phys_base)
 {
     uint64_t page_addr;
+    mfn_t previous_max_alloc_mfn;
 
     if ( map_start & ~PAGE_MASK )
     {
@@ -105,81 +142,47 @@ static void __init setup_initial_mapping(struct lvl1_pd *lvl1,
         die();
     }
 
+    /* Identity map Xen itself */
     for ( page_addr = map_start; page_addr < map_end; page_addr += PAGE_SIZE )
     {
-        struct lvl2_pd *lvl2;
-        struct lvl3_pd *lvl3;
-        struct lvl4_pt *lvl4;
-        pde_t *pde;
-        pte_t *pte;
-
-        /* Allocate LVL 2 PD if necessary */
-        pde = pt_entry(lvl1, page_addr);
-        if ( !pde_is_valid(*pde) )
-        {
-            lvl2 = lvl2_pd_pool_alloc();
-            *pde = paddr_to_pde(__pa(lvl2), PDE_VALID,
-                                XEN_PT_ENTRIES_LOG2_LVL_2);
-        }
-        else
-            lvl2 = __va(pde_to_paddr(*pde));
+        unsigned long flags;
 
-        /* Allocate LVL 3 PD if necessary */
-        pde = pt_entry(lvl2, page_addr);
-        if ( !pde_is_valid(*pde) )
+        if ( is_kernel_text(page_addr) || is_kernel_inittext(page_addr) )
         {
-            lvl3 = lvl3_pd_pool_alloc();
-            *pde = paddr_to_pde(__pa(lvl3), PDE_VALID,
-                                XEN_PT_ENTRIES_LOG2_LVL_3);
+            radix_dprintk("%016lx being marked as TEXT (RX)\n", page_addr);
+            flags = PTE_XEN_RX;
         }
-        else
-            lvl3 = __va(pde_to_paddr(*pde));
-
-        /* Allocate LVL 4 PT if necessary */
-        pde = pt_entry(lvl3, page_addr);
-        if ( !pde_is_valid(*pde) )
+        else if ( is_kernel_rodata(page_addr) )
         {
-            lvl4 = lvl4_pt_pool_alloc();
-            *pde = paddr_to_pde(__pa(lvl4), PDE_VALID,
-                                XEN_PT_ENTRIES_LOG2_LVL_4);
+            radix_dprintk("%016lx being marked as RODATA (RO)\n", page_addr);
+            flags = PTE_XEN_RO;
         }
         else
-            lvl4 = __va(pde_to_paddr(*pde));
-
-        /* Finally, create PTE in LVL 4 PT */
-        pte = pt_entry(lvl4, page_addr);
-        if ( !pte_is_valid(*pte) )
         {
-            unsigned long paddr = (page_addr - map_start) + phys_base;
-            unsigned long flags;
-
-            radix_dprintk("%016lx being mapped to %016lx\n", paddr, page_addr);
-            if ( is_kernel_text(page_addr) || is_kernel_inittext(page_addr) )
-            {
-                radix_dprintk("%016lx being marked as TEXT (RX)\n", page_addr);
-                flags = PTE_XEN_RX;
-            }
-            else if ( is_kernel_rodata(page_addr) )
-            {
-                radix_dprintk("%016lx being marked as RODATA (RO)\n", page_addr);
-                flags = PTE_XEN_RO;
-            }
-            else
-            {
-                radix_dprintk("%016lx being marked as DEFAULT (RW)\n", page_addr);
-                flags = PTE_XEN_RW;
-            }
-
-            *pte = paddr_to_pte(paddr, flags);
-            radix_dprintk("%016lx is the result of PTE map\n",
-                paddr_to_pte(paddr, flags).pte);
-        }
-        else
-        {
-            early_printk("BUG: Tried to create PTE for already-mapped page!");
-            die();
+            radix_dprintk("%016lx being marked as DEFAULT (RW)\n", page_addr);
+            flags = PTE_XEN_RW;
         }
+

xen/arch/ppc/mm-radix.c static void __init setup_initial_mapping(struct lvl1_pd *lvl1, · line 175

Quoted 10 lines
xen/arch/ppc/mm-radix.c +10 -0
+        map_page_initial(lvl1, page_addr, (page_addr - map_start) + phys_base, flags);
+    }
+
+    previous_max_alloc_mfn = max_alloc_mfn;
+
+    /*
+     * Identity map all pages we've allocated for paging structures. This act
+     * itself will allocate more pages, so continue until we've mapped from
+     * `max_alloc_mfn` down to `min_alloc_mfn`. This assumes that the heap grows
+     * downwards, which matches the behavior of alloc_boot_pages.

... as long as you only ever allocate individual pages (which looks to be the case for such incremental allocations, but that imo doesn't go without saying).

Jan

#5 lore raw

Re: [PATCH v4 2/6] xen/device-tree: Move Arm's setup.c bootinfo functions to common

In reply to Shawn Anastasio

Hi Shawn,

Quoted 13 lines · Shawn Anastasio
xen/common/device-tree/Makefile new file +1 -0
new file mode 100644
index 0000000000..947bad979c
@@ -0,0 +1 @@
+obj-y += bootinfo.init.o
xen/common/device-tree/bootinfo.c new file +0 -0
new file mode 100644
index 0000000000..914f876d29

xen/common/device-tree/bootinfo.c line 5

Quoted 5 lines
xen/common/device-tree/bootinfo.c new file +5 -0
@@ -0,0 +1,446 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Derived from $xen/arch/arm/setup.c.
+ *
+ * Early device tree parsing and bookkeeping routines.

Based on the discussion in v3, I think this is only containing bookkepping routines. So you probably want to remove "Early device tree parsing".

This would also make a bit clearer the difference bettwen bootfdt.c and bootinfo.c.

[...]

xen/common/device-tree/bootinfo.c line 368

Quoted 10 lines
xen/common/device-tree/bootinfo.c new file +10 -0
+/*
+ * Populate the boot allocator.
+ * If a static heap was not provided by the admin, all the RAM but the
+ * following regions will be added:
+ *  - Modules (e.g., Xen, Kernel)
+ *  - Reserved regions
+ *  - Xenheap (CONFIG_SEPARATE_XENHEAP only)
+ * If a static heap was provided by the admin, populate the boot
+ * allocator with the corresponding regions only, but with Xenheap excluded
+ * on arm32.

Now this is moved in common code, I think we want to s/arm32/CONFIG_SEPARATE_XENHEAP/. This could be done on a follow-up commit.

[...]

Quoted 57 lines
xen/include/xen/bootfdt.h new file +51 -0
new file mode 100644
index 0000000000..b0487bdbbd
@@ -0,0 +1,116 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __XEN_BOOTFDT_H__
+#define __XEN_BOOTFDT_H__
+
+#include <xen/types.h>
+
+#define MIN_FDT_ALIGN 8
+#define MAX_FDT_SIZE SZ_2M
+
+#define NR_MEM_BANKS 256
+
+#define MAX_MODULES 32 /* Current maximum useful modules */
+
+typedef enum {
+    BOOTMOD_XEN,
+    BOOTMOD_FDT,
+    BOOTMOD_KERNEL,
+    BOOTMOD_RAMDISK,
+    BOOTMOD_XSM,
+    BOOTMOD_GUEST_DTB,
+    BOOTMOD_UNKNOWN
+} bootmodule_kind;
+
+enum membank_type {
+    /*
+     * The MEMBANK_DEFAULT type refers to either reserved memory for the
+     * device/firmware (when the bank is in 'reserved_mem') or any RAM (when
+     * the bank is in 'mem').
+     */
+    MEMBANK_DEFAULT,
+    /*
+     * The MEMBANK_STATIC_DOMAIN type is used to indicate whether the memory
+     * bank is bound to a static Xen domain. It is only valid when the bank
+     * is in reserved_mem.
+     */
+    MEMBANK_STATIC_DOMAIN,
+    /*
+     * The MEMBANK_STATIC_HEAP type is used to indicate whether the memory
+     * bank is reserved as static heap. It is only valid when the bank is
+     * in reserved_mem.
+     */
+    MEMBANK_STATIC_HEAP,
+};
+
+/* Indicates the maximum number of characters(\0 included) for shm_id */
+#define MAX_SHM_ID_LENGTH 16
+
+struct membank {
+    paddr_t start;
+    paddr_t size;
+    enum membank_type type;

xen/include/xen/bootfdt.h line 61

Quoted 10 lines
xen/include/xen/bootfdt.h new file +10 -0
+#ifdef CONFIG_STATIC_SHM
+    char shm_id[MAX_SHM_ID_LENGTH];
+    unsigned int nr_shm_borrowers;
+#endif
+};
+
+struct meminfo {
+    unsigned int nr_banks;
+    struct membank bank[NR_MEM_BANKS];
+};

I have just committed some code which is modifying membank/meminfo. I think this may need to be rebased. I have CCed the author of the series (Luca) who may be able to advise/help.

Cheers,

Signature · 1 line
Julien Grall
#2 lore raw

Re: [PATCH v4 1/6] xen/ppc: Introduce stub asm/static-shmem.h

In reply to Shawn Anastasio

Hi Shawn,

Quoted 1 line · Shawn Anastasio

Required for bootfdt.c to build.

AFAIU, this patch is only necessary in #4. So I would consider to fold it there as it doesn't seem to add any value alone.

xen/arch/ppc/include/asm/static-shmem.h

Quoted 14 lines

---
Changes in v4: none

  xen/arch/ppc/include/asm/static-shmem.h | 12 ++++++++++++
  1 file changed, 12 insertions(+)

create mode 100644 xen/arch/ppc/include/asm/static-shmem.h

xen/arch/ppc/include/asm/static-shmem.h new file +0 -0
new file mode 100644
index 0000000000..84370d6e6c

Just an idea to avoid introducing static-shmem.h for PPC (and I guess RISC-V). Could we instead ifdef the inclusion and in bootfdt.c provide a stub for...

xen/arch/ppc/include/asm/static-shmem.h line 10

Quoted 11 lines
xen/arch/ppc/include/asm/static-shmem.h new file +10 -0
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier:  GPL-2.0-only */
+
+#ifndef __ASM_PPC_STATIC_SHMEM_H__
+#define __ASM_PPC_STATIC_SHMEM_H__
+
+static inline int process_shm_node(const void *fdt, int node,
+                                   uint32_t address_cells, uint32_t size_cells)
+{
+    return -EINVAL;
+}

... this helper? Something like [1].

Cheers,

[1]

xen/arch/arm/bootfdt.c +11 -0
index 4d708442a19e..26dada1e3a1e 100644
@@ -15,7 +15,9 @@
 #include <xen/sort.h>
 #include <xsm/xsm.h>
 #include <asm/setup.h>
+#ifdef CONFIG_STATIC_SHM
 #include <asm/static-shmem.h>
+#endif
 
 static void __init __maybe_unused build_assertions(void)
 {
@@ -436,6 +438,15 @@ static int __init process_domain_node(const void *fdt, int node,
                                    MEMBANK_STATIC_DOMAIN);
 }
 
+#ifndef CONFIG_STATIC_SHM
+static inline int process_shm_node(const void *fdt, int node,
+                                   uint32_t address_cells, uint32_t size_cells)
+{
+    printk("CONFIG_STATIC_SHM must be enabled for parsing static shared memory nodes\n");
+    return -EINVAL;
+}
+#endif
+
 static int __init early_scan_node(const void *fdt,
                                   int node, const char *name, int depth,
                                   u32 address_cells, u32 size_cells,
xen/arch/arm/include/asm/static-shmem.h +0 -7
index 3b6569e5703f..39b5881d24aa 100644
@@ -68,13 +68,6 @@ static inline int process_shm_chosen(struct domain *d,
     return 0;
 }
 
-static inline int process_shm_node(const void *fdt, int node,
-                                   uint32_t address_cells, uint32_t size_cells)
-{
-    printk("CONFIG_STATIC_SHM must be enabled for parsing static shared memory nodes\n");
-    return -EINVAL;
-}
-
 static inline void early_print_info_shmem(void) {};
 
 static inline void init_sharedmem_pages(void) {};

Cheers,

Signature · 1 line
Julien Grall
Acked-by on v4 4/6
#8 lore raw

Re: [PATCH v4 4/6] xen/ppc: Enable bootfdt and boot allocator

In reply to Shawn Anastasio

Hi Shawn,

Quoted 17 lines · Shawn Anastasio

Enable usage of bootfdt for populating the boot info struct from the firmware-provided device tree. Also enable the Xen boot page allocator.

Additionally, modify bootfdt.c's boot_fdt_info() to tolerate the scenario in which the FDT overlaps a reserved memory region, as is the case on PPC when booted directly from skiboot. Since this means that Xen can now boot without a BOOTMOD_FDT present in bootinfo, clarify this fact in a comment above BOOTMOD_FDT's definition.

---
Changes in v4:
- drop unnecessary libfdt.h include in setup.c
- make boot_fdt and xen_bootmodule const
- more clearly document that BOOTMOD_FDT is now optional
- add explicit (void) cast to BOOTMOD_FDT creation

Quoted 4 lines
  xen/arch/ppc/setup.c             | 22 +++++++++++++++++++++-
  xen/common/device-tree/bootfdt.c | 11 +++++++++--
  xen/include/xen/bootfdt.h        |  7 +++++++
  3 files changed, 37 insertions(+), 3 deletions(-)

The changes look overall good. I have a few remark belows. But you can have my acked-by with or without the NIT addressed:

xen/arch/ppc/setup.c line 5

Quoted 11 lines
xen/arch/ppc/setup.c +2 -0
index 101bdd8bb6..47e997969f 100644
@@ -1,12 +1,15 @@
  /* SPDX-License-Identifier: GPL-2.0-or-later */
  #include <xen/init.h>
  #include <xen/lib.h>
+#include <xen/bootfdt.h>
+#include <xen/device_tree.h>

We are tryying to keep the header order alphabetically. It looks like the existing code is respecting that. So can this be done for the two new headers?

Quoted 21 lines
xen/arch/ppc/setup.c +4 -0
  #include <xen/mm.h>
  #include <public/version.h>
  #include <asm/boot.h>
  #include <asm/early_printk.h>
  #include <asm/mm.h>
  #include <asm/processor.h>
+#include <asm/setup.h>
 
  /* Xen stack for bringing up the first CPU. */
  unsigned char __initdata cpu0_boot_stack[STACK_SIZE] __aligned(STACK_SIZE);
@@ -24,6 +27,9 @@ void __init noreturn start_xen(unsigned long r3, unsigned long r4,
                                 unsigned long r5, unsigned long r6,
                                 unsigned long r7)
  {
+    const void *boot_fdt;
+    const struct bootmodule *xen_bootmodule;
+
      if ( r5 )
      {
          /* Unsupported OpenFirmware boot protocol */

xen/arch/ppc/setup.c void __init noreturn start_xen(unsigned long r3, unsigned long r4, · line 41

Quoted 5 lines
xen/arch/ppc/setup.c +1 -1
@@ -32,11 +38,25 @@ void __init noreturn start_xen(unsigned long r3, unsigned long r4,
      else
      {
          /* kexec boot protocol */
-        boot_opal_init((void *)r3);
+        boot_fdt = (void *)r3;

NIT: Sorry I should have noticed it earlier. boot_fdt is only used to ..

xen/arch/ppc/setup.c void __init noreturn start_xen(unsigned long r3, unsigned long r4, · line 47

Quoted 6 lines
xen/arch/ppc/setup.c +2 -0
+        boot_opal_init(boot_fdt);
      }
 
      setup_exceptions();
 
+    device_tree_flattened = boot_fdt;

... set device_tree_flattened and ...

xen/arch/ppc/setup.c void __init noreturn start_xen(unsigned long r3, unsigned long r4, · line 48

Quoted 1 line
xen/arch/ppc/setup.c +1 -0
+    boot_fdt_info(boot_fdt, r3);

... used here. Is there any plan to have device_tree_flattened != boot_fdt? If not, then I would suggest to simply drop boot_fdt.

Quoted 60 lines
xen/arch/ppc/setup.c +11 -0
+
+    /*
+     * Xen relocates itself at the ppc64 entrypoint, so we need to manually mark
+     * the kernel module.
+     */
+    xen_bootmodule = add_boot_module(BOOTMOD_XEN, __pa(_start),
+                                     PAGE_ALIGN(__pa(_end)), false);
+    BUG_ON(!xen_bootmodule);
+
+    populate_boot_allocator();
+
      setup_initial_pagetables();
 
      early_printk("Hello, ppc64le!\n");
xen/common/device-tree/bootfdt.c +9 -2
index f01a5b5d76..76d0f72ef9 100644
@@ -542,12 +542,19 @@ size_t __init boot_fdt_info(const void *fdt, paddr_t paddr)
      if ( ret < 0 )
          panic("No valid device tree\n");
 
-    add_boot_module(BOOTMOD_FDT, paddr, fdt_totalsize(fdt), false);
-
      ret = device_tree_for_each_node(fdt, 0, early_scan_node, NULL);
      if ( ret )
          panic("Early FDT parsing failed (%d)\n", ret);
 
+    /*
+     * Add module for the FDT itself after the device tree has been parsed. This
+     * is required on ppc64le where the device tree passed to Xen may have been
+     * allocated by skiboot, in which case it will exist within a reserved
+     * region and this call will fail. This is fine, however, since either way
+     * the allocator will know not to step on the device tree.
+     */
+    (void)add_boot_module(BOOTMOD_FDT, paddr, fdt_totalsize(fdt), false);
+
      /*
       * On Arm64 setup_directmap_mappings() expects to be called with the lowest
       * bank in memory first. There is no requirement that the DT will provide
xen/include/xen/bootfdt.h +7 -1
index 577618da16..ea3ad96bb9 100644
@@ -13,7 +13,14 @@
 
  typedef enum {
      BOOTMOD_XEN,
+
+    /*
+     * The BOOTMOD_FDT type will only be present when the firmware-provided FDT
+     * blob exists outside of a reserved memory region which is platform-
+     * dependent and may not be relied upon.
+     */
      BOOTMOD_FDT,
+
      BOOTMOD_KERNEL,
      BOOTMOD_RAMDISK,
      BOOTMOD_XSM,
--
Quoted 2 lines

2.30.2

Cheers,

Signature · 1 line
Julien Grall
#12 lore raw

Re: [PATCH v4 6/6] xen/ppc: mm-radix: Allocate all paging structures at runtime

In reply to Jan Beulich

Hi Jan,

I'm revisiting this series and wanted to follow up on your comments.

Quoted 7 lines · Jan Beulich
Quoted 5 lines · Shawn Anastasio

+/*
+ * A thin wrapper for alloc_boot_pages that keeps track of the maximum and
+ * minimum mfns that have been allocated. This information is used by
+ * setup_initial_mapping to include the allocated pages in the initial
+ * page mapping.

Quoted 6 lines

Plus everything in between. Together with the further comment below I'm afraid I still think that the constraints (and the justification for them being tolerable at least for the time being) aren't made sufficiently clear (perhaps in another code comment next to the respective two variables' definitions).

Sure, I'll elaborate a bit further in a new comment above those two variables.

Quoted 10 lines

xen/arch/ppc/mm-radix.c static void __init setup_initial_mapping(struct lvl1_pd *lvl1, · line 175

Quoted 5 lines
xen/arch/ppc/mm-radix.c +5 -0
+    /*
+     * Identity map all pages we've allocated for paging structures. This act
+     * itself will allocate more pages, so continue until we've mapped from
+     * `max_alloc_mfn` down to `min_alloc_mfn`. This assumes that the heap grows
+     * downwards, which matches the behavior of alloc_boot_pages.

... as long as you only ever allocate individual pages (which looks to be the case for such incremental allocations, but that imo doesn't go without saying).

I'm not sure I'm following you. initial_page_alloc as well as its underlying alloc_boot_pages can only ever allocate individual pages (or contiguous ranges of pages). If the assumption about growing downwards is not met, the code catches it and panics.

Quoted 1 line

Jan

Thanks,
Shawn

#13 lore raw

Re: [PATCH v4 6/6] xen/ppc: mm-radix: Allocate all paging structures at runtime

Quoted 8 lines · Shawn Anastasio
Quoted 7 lines · Jan Beulich
Quoted 5 lines · Shawn Anastasio
xen/arch/ppc/mm-radix.c +5 -0
+    /*
+     * Identity map all pages we've allocated for paging structures. This act
+     * itself will allocate more pages, so continue until we've mapped from
+     * `max_alloc_mfn` down to `min_alloc_mfn`. This assumes that the heap grows
+     * downwards, which matches the behavior of alloc_boot_pages.
Quoted 8 lines
Quoted 4 lines

... as long as you only ever allocate individual pages (which looks to be the case for such incremental allocations, but that imo doesn't go without saying).

I'm not sure I'm following you. initial_page_alloc as well as its underlying alloc_boot_pages can only ever allocate individual pages (or contiguous ranges of pages).

It's this "or contiguous ranges of pages" which constitutes a problem. Once that happens, a portion of un-allocated pages may remain above that block. A subsequent single-page allocation may then use one of those pages, resulting in strict ordering to no longer hold. Or the space may remain un-allocated, and hence being mapped for no reason.

Jan