Skip to contentSkip to conversation

Boot modules for Hyperlaunch

PATCH In review Daniel P. Smith 4 participants 54 messages Nov 2, 2024 – Nov 11, 2024

lore
Share & tools
lore
#0 lore raw

cover Boot modules for Hyperlaunch

The Boot Modules for Hyperlaunch series is an effort to split out preliminary changes necessary for the introduction of the Hyperlaunch domain builder logic. These preliminary changes revolve around introducing the struct boot_module and struct boot_domain structures. This includes converting the dom0 construction path to use these structures. These abstractions lay the groundwork to transform and extend the dom0 construction logic into a limited, but general domain builder.

The splitting of Hyperlaunch into a set of series are twofold, to reduce the effort in reviewing a much larger series, and to reduce the effort in handling the knock-on effects to the construction logic from requested review changes.

Much thanks to AMD for supporting this work.

!!NOTE on v8!!
For this version a complete re-organization of the changes were made with the concept that three tasks were being carried out that were intertwined in the past versions. These three tasks were 1. capturing the state tracking and enrichment, 2. removing the use of module_t to track where a boot module is located in memory, and 3. abstract the domain construction state. The first eight patches address task 1, task 2 is address with patch nine, and the remaining addresses task 3.

Lastly it should be noted that this series builds/relies upon Andy Cooper's "x86/ucode: Fix module-handling use-after-free's" series.

Documentation on Hyperlaunch:
https://wiki.xenproject.org/wiki/Hyperlaunch

Original Hyperlaunch v1 patch series:
https://lists.xenproject.org/archives/html/xen-devel/2022-07/msg00345.html

V/r,
Daniel P. Smith

Changes since v7:
- patches re-organized with some collapsing into others

Changes since v6:
- Dropped patches that were merged from v5
- patch 8 title and commit message rewritten
- boot module interator patch merged with xsm patch, its first use
- incorporated review requests
- made additional style optimizations as a result of review requests

Changes since v5:
- switched to per patch change logs
- incorporated review requests

Changes since v4:
- added requested inline code comments
- moved instance of struct boot_info to unit level and extern'ed
- array of struct boot_module moved into struct boot_info
- renamed function to multiboot_fill_bootinfo, now returns *struct boot_info
- multiboot_fill_bootinfo changed to take multiboot_info_t addr as param
- added missing guard that checked there were multiboot1/2 modules passed
- renmaed struct elements per the review
- fixed errant commit messages per the review
- corrected coding style per review
- attempted to repalce all open codings of page/addr translations touched
- unified use of bi as var name for pointer ref to struct boot_info
- when appropriate, ensure variables where typed, eg size_t, paddr_t, etc.
- dropped all uses of "a = b = c"

Changes since v3:
- reduced scope to x86 only
- broke changes into a smaller chunks with a linear progression
- concerns about deconflicting with Arm deferred
- conversion from mb1 to boot modules no longer attempted at entry points
- the temporary conversion function is now the permenant means to convert
- incorporated suggestion from Andy Cooper for handling bootstrap_map

Changes since v2:
- combined v2 patches 7 and 8 for common review
- rebased the v2 series onto the current tip of staging (sorry)
- fixed the placement of the patch changelogs
- provided the changes description in the cover letter

Changes since v1:
- the v2 and v3 series implement functionality from v1 patches 2-4
- v2 series objective is to enable efficient patch review in support of merging the functionality into the hypervisor. It implements a subset of the v1 series, incorporating changes from community feedback.
- the bootstrap map is made accessible early in the v2 series via both multiboot and boot module arguments until later in the series where multiboot use is retired. This allows for incremental conversion across several patches from multiboot to boot modules.
- the 32-bit x86 boot environment header is removed, and changes are made to allow the new common bootinfo headers to be used instead.
- Arm and RISC-V architecture bootinfo headers are added to ensure that builds on those architectures can complete correctly.
- The KConfig patch to set the maximum number of boot modules allowed is not included in this series, replaced with a static maximum define.

Daniel P. Smith (12):
x86/boot: introduce boot module types
x86/boot: eliminate module_map
x86/boot: introduce boot module flags
x86/boot: introduce module release
x86/boot: move headroom to boot modules x86/boot: relocate kextra into boot info x86/boot: add cmdline_pa to struct boot_module x86/boot: convert domain construction to use boot info x86/boot: add start and size fields to struct boot_module x86/boot: introduce boot domain
x86/boot: introduce domid field to struct boot_domain x86/boot: add cmdline to struct boot_domain

 xen/arch/x86/cpu/microcode/core.c     |  23 +--
 xen/arch/x86/dom0_build.c             |  10 +-
 xen/arch/x86/hvm/dom0_build.c         |  46 ++---
 xen/arch/x86/include/asm/bootdomain.h |  37 ++++
 xen/arch/x86/include/asm/bootinfo.h   |  96 ++++++++++-
 xen/arch/x86/include/asm/dom0_build.h |  12 +-
 xen/arch/x86/include/asm/setup.h      |  10 +-
 xen/arch/x86/pv/dom0_build.c          |  72 ++++----
 xen/arch/x86/setup.c                  | 233 +++++++++++++++-----------
 xen/xsm/xsm_core.c                    |   5 +
 xen/xsm/xsm_policy.c                  |  18 +-
 11 files changed, 356 insertions(+), 206 deletions(-)
 create mode 100644 xen/arch/x86/include/asm/bootdomain.h
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/12 x86/boot: introduce boot module types

This commit introduces module types for the types of boot modules that may be passed to Xen. These include xen, kernel, ramdisk, microcode, and xsm policy. This reduces the need for hard coded order assumptions and global variables to be used by consumers of boot modules, such as domain construction.

---
Changes since v7:
- merged the addition of microcode and xsm boot mod types

Changes since v5:
- added guard around initrd type assignment
- removed a missed rebase artifact

---
 xen/arch/x86/cpu/microcode/core.c   |  1 +
 xen/arch/x86/include/asm/bootinfo.h | 12 ++++++++++++
 xen/arch/x86/setup.c                |  4 ++++
 xen/xsm/xsm_policy.c                |  1 +
 4 files changed, 18 insertions(+)
xen/arch/x86/cpu/microcode/core.c +1 -0
index 1fa6cbf3d364..f46464241557 100644
@@ -845,6 +845,7 @@ static int __init early_microcode_load(struct boot_info *bi)
             printk(XENLOG_WARNING "Microcode: Chosen module %d already used\n", idx);
             return -ENODEV;
         }
+        bi->mods[idx].type = BOOTMOD_MICROCODE;
 
         size = bi->mods[idx].mod->mod_end;
         data = bootstrap_map_bm(&bi->mods[idx]);
xen/arch/x86/include/asm/bootinfo.h +12 -0
index 6237da7e4d86..fc74e3b224e7 100644
@@ -14,9 +14,21 @@
 /* Max number of boot modules a bootloader can provide in addition to Xen */
 #define MAX_NR_BOOTMODS 63
 
+/* Boot module binary type / purpose */
+enum bootmod_type {
+    BOOTMOD_UNKNOWN,
+    BOOTMOD_XEN,
+    BOOTMOD_KERNEL,
+    BOOTMOD_RAMDISK,
+    BOOTMOD_MICROCODE,
+    BOOTMOD_XSM_POLICY,
+};
+
 struct boot_module {
     /* Transitionary only */
     module_t *mod;
+
+    enum bootmod_type type;
 };
 
 /*
xen/arch/x86/setup.c +4 -0
index 5db57e1b1af2..fac08b6242e9 100644
@@ -316,6 +316,7 @@ static struct boot_info *__init multiboot_fill_boot_info(
 
     /* Variable 'i' should be one entry past the last module. */
     bi->mods[i].mod = &mods[bi->nr_modules];
+    bi->mods[i].type = BOOTMOD_XEN;
 
     return bi;
 }
@@ -1217,6 +1218,7 @@ void asmlinkage __init noreturn __start_xen(void)
 
     bitmap_fill(module_map, bi->nr_modules);
     __clear_bit(0, module_map); /* Dom0 kernel is always first */
+    bi->mods[0].type = BOOTMOD_KERNEL;
 
     if ( pvh_boot )
     {
@@ -2097,6 +2099,8 @@ void asmlinkage __init noreturn __start_xen(void)
            cpu_has_nx ? "" : "not ");
 
     initrdidx = find_first_bit(module_map, bi->nr_modules);
+    if ( initrdidx < bi->nr_modules )
+        bi->mods[initrdidx].type = BOOTMOD_RAMDISK;
     if ( bitmap_weight(module_map, bi->nr_modules) > 1 )
         printk(XENLOG_WARNING
                "Multiple initrd candidates, picking module #%u\n",
xen/xsm/xsm_policy.c +1 -0
index 35f36c6f2359..4c195411d05b 100644
@@ -59,6 +59,7 @@ int __init xsm_multiboot_policy_init(
                    _policy_len,_policy_start);
 
             __clear_bit(i, bi->module_map);
+            bi->mods[i].type = BOOTMOD_XSM_POLICY;
             break;
 
         }
Signature · 1 line
2.30.2
#4 lore raw

PATCH 2/12 x86/boot: eliminate module_map

In reply to Daniel P. Smith

With all boot modules now labeled by type, it is no longer necessary to track whether a boot module was identified via the module_map bitmap.

Introduce a set of helpers to search the list of boot modules based on type and the reference type, pointer or array index, desired. Then drop all uses of setting a bit in module_map and replace its use for looping with the helpers.

---
Changes since v7:
- collapse the three module_map patches into one,
- x86/boot: remove module_map usage from microcode loading
- x86/boot: remove module_map usage from xsm policy loading
- x86/boot: remove module_map usage by ramdisk loading

---
 xen/arch/x86/cpu/microcode/core.c   | 12 ++++-----
 xen/arch/x86/include/asm/bootinfo.h | 42 ++++++++++++++++++++++++++++-
 xen/arch/x86/setup.c                | 28 +++++++++++--------
 xen/xsm/xsm_policy.c                | 19 +++++--------
 4 files changed, 70 insertions(+), 31 deletions(-)
xen/arch/x86/cpu/microcode/core.c +5 -7
index f46464241557..b09cf83249f6 100644
@@ -790,15 +790,13 @@ static int __init early_microcode_load(struct boot_info *bi)
 
     if ( opt_scan ) /* Scan for a CPIO archive */
     {
-        for ( idx = 1; idx < bi->nr_modules; ++idx )
+        for_each_boot_module_by_type(idx, bi, BOOTMOD_UNKNOWN)
         {
+            struct boot_module *bm = &bi->mods[idx];
             struct cpio_data cd;
 
-            if ( !test_bit(idx, bi->module_map) )
-                continue;
-
-            size = bi->mods[idx].mod->mod_end;
-            data = bootstrap_map_bm(&bi->mods[idx]);
+            size = bm->mod->mod_end;
+            data = bootstrap_map_bm(bm);
             if ( !data )
             {
                 printk(XENLOG_WARNING "Microcode: Could not map module %d, size %zu\n",
@@ -840,7 +838,7 @@ static int __init early_microcode_load(struct boot_info *bi)
             return -ENODEV;
         }
 
-        if ( !__test_and_clear_bit(idx, bi->module_map) )
+        if ( bi->mods[idx].type != BOOTMOD_UNKNOWN )
         {
             printk(XENLOG_WARNING "Microcode: Chosen module %d already used\n", idx);
             return -ENODEV;
xen/arch/x86/include/asm/bootinfo.h +41 -1

51 lines not on this page · raw message

xen/arch/x86/setup.c +17 -11
index fac08b6242e9..92dbdfe12a3d 100644
@@ -1034,7 +1034,7 @@ void asmlinkage __init noreturn __start_xen(void)
     struct cpu_info *info = get_cpu_info(), *bsp_info;
     unsigned int initrdidx, num_parked = 0;
     struct boot_info *bi;
-    unsigned long nr_pages, raw_max_page, modules_headroom, module_map[1];
+    unsigned long nr_pages, raw_max_page, modules_headroom;
     int i, j, e820_warn = 0, bytes = 0;
     unsigned long eb_start, eb_end;
     bool acpi_boot_table_init_done = false, relocated = false;
@@ -1097,8 +1097,6 @@ void asmlinkage __init noreturn __start_xen(void)
         ASSERT(multiboot_ptr < MB(1) || xen_phys_start);
     }
 
-    bi->module_map = module_map; /* Temporary */
-
     /* Parse the command-line options. */
     if ( (kextra = strstr(bi->cmdline, " -- ")) != NULL )
     {
@@ -1216,8 +1214,7 @@ void asmlinkage __init noreturn __start_xen(void)
                bi->nr_modules);
     }
 
-    bitmap_fill(module_map, bi->nr_modules);
-    __clear_bit(0, module_map); /* Dom0 kernel is always first */
+    /* Dom0 kernel is always first */
     bi->mods[0].type = BOOTMOD_KERNEL;
 
     if ( pvh_boot )
@@ -2098,13 +2095,22 @@ void asmlinkage __init noreturn __start_xen(void)
            cpu_has_nx ? XENLOG_INFO : XENLOG_WARNING "Warning: ",
            cpu_has_nx ? "" : "not ");
 
-    initrdidx = find_first_bit(module_map, bi->nr_modules);
-    if ( initrdidx < bi->nr_modules )
+    /*
+     * At this point all capabilities that consume boot modules should have
+     * claimed their boot modules. Find the first unclaimed boot module and
+     * claim it as the initrd ramdisk. Do a second search to see if there are
+     * any remaining unclaimed boot modules, and report them as unusued initrd
+     * candidates.
+     */
+    initrdidx = first_boot_module_index(bi, BOOTMOD_UNKNOWN);
+    if ( initrdidx < MAX_NR_BOOTMODS )
+    {
         bi->mods[initrdidx].type = BOOTMOD_RAMDISK;
-    if ( bitmap_weight(module_map, bi->nr_modules) > 1 )
-        printk(XENLOG_WARNING
-               "Multiple initrd candidates, picking module #%u\n",
-               initrdidx);
+        if ( first_boot_module_index(bi, BOOTMOD_UNKNOWN) < MAX_NR_BOOTMODS )
+            printk(XENLOG_WARNING
+                   "Multiple initrd candidates, picking module #%u\n",
+                   initrdidx);
+    }
 
     /*
      * We're going to setup domain0 using the module(s) that we stashed safely
xen/xsm/xsm_policy.c +7 -12
index 4c195411d05b..12c9de5a1fbf 100644
@@ -33,22 +33,18 @@
 int __init xsm_multiboot_policy_init(
     struct boot_info *bi, void **policy_buffer, size_t *policy_size)
 {
-    int i;
+    unsigned int i;
     int rc = 0;
     u32 *_policy_start;
     unsigned long _policy_len;
 
-    /*
-     * Try all modules and see whichever could be the binary policy.
-     * Adjust module_map for the module that is the binary policy.
-     */
-    for ( i = bi->nr_modules - 1; i >= 1; i-- )
+    /* Try all unknown modules and see whichever could be the binary policy. */
+    for_each_boot_module_by_type(i, bi, BOOTMOD_UNKNOWN)
     {
-        if ( !test_bit(i, bi->module_map) )
-            continue;
+        struct boot_module *bm = &bi->mods[i];
 
-        _policy_start = bootstrap_map(bi->mods[i].mod);
-        _policy_len   = bi->mods[i].mod->mod_end;
+        _policy_start = bootstrap_map(bm->mod);
+        _policy_len   = bm->mod->mod_end;
 
         if ( (xsm_magic_t)(*_policy_start) == XSM_MAGIC )
         {
@@ -58,8 +54,7 @@ int __init xsm_multiboot_policy_init(
             printk("Policy len %#lx, start at %p.\n",
                    _policy_len,_policy_start);
 
-            __clear_bit(i, bi->module_map);
-            bi->mods[i].type = BOOTMOD_XSM_POLICY;
+            bm->type = BOOTMOD_XSM_POLICY;
             break;
 
         }
Signature · 1 line
2.30.2
#14 lore raw

PATCH 3/12 x86/boot: introduce boot module flags

In reply to Daniel P. Smith

The existing startup code employs various ad-hoc state tracking about certain boot module types by each area of the code. A boot module flags bitfield is added to enable tracking these different states. The first state to be transition by this commit is module relocation.

---
Changes since v6:
- replaced bitmask flags field with the bitfield, relocated
- slight update to commit message to mention bitfield
- dropped Rb and Ab due to approach change

Changes since v5:
- removed trailing blank line.

---
 xen/arch/x86/include/asm/bootinfo.h | 6 ++++++
 xen/arch/x86/setup.c                | 7 +++----
 2 files changed, 9 insertions(+), 4 deletions(-)
xen/arch/x86/include/asm/bootinfo.h +6 -0
index 37dfcc14fa7d..0fbf36739782 100644
@@ -29,6 +29,12 @@ struct boot_module {
     module_t *mod;
 
     enum bootmod_type type;
+
+    /*
+     * Module State Flags:
+     *   relocated: indicates module has been relocated in memory.
+     */
+    bool relocated:1;
 };
 
 /*
xen/arch/x86/setup.c +3 -4
index 92dbdfe12a3d..d061ece0541f 100644
@@ -1392,7 +1392,6 @@ void asmlinkage __init noreturn __start_xen(void)
             panic("Bootloader didn't honor module alignment request\n");
         bi->mods[i].mod->mod_end -= bi->mods[i].mod->mod_start;
         bi->mods[i].mod->mod_start >>= PAGE_SHIFT;
-        bi->mods[i].mod->reserved = 0;
     }
 
     /*
@@ -1508,7 +1507,7 @@ void asmlinkage __init noreturn __start_xen(void)
             unsigned long headroom = j ? 0 : modules_headroom;
             unsigned long size = PAGE_ALIGN(headroom + bm->mod->mod_end);
 
-            if ( bm->mod->reserved )
+            if ( bm->relocated )
                 continue;
 
             /* Don't overlap with other modules (or Xen itself). */
@@ -1526,7 +1525,7 @@ void asmlinkage __init noreturn __start_xen(void)
                             pfn_to_paddr(bm->mod->mod_start), bm->mod->mod_end);
                 bm->mod->mod_start = (end - size) >> PAGE_SHIFT;
                 bm->mod->mod_end += headroom;
-                bm->mod->reserved = 1;
+                bm->relocated = true;
             }
         }
 
@@ -1552,7 +1551,7 @@ void asmlinkage __init noreturn __start_xen(void)
 #endif
     }
 
-    if ( modules_headroom && !bi->mods[0].mod->reserved )
+    if ( modules_headroom && !bi->mods[0].relocated )
         panic("Not enough memory to relocate the dom0 kernel image\n");
     for ( i = 0; i < bi->nr_modules; ++i )
     {
Signature · 1 line
2.30.2
#17 lore raw

PATCH 4/12 x86/boot: introduce module release

In reply to Daniel P. Smith

A precarious approach was used to release the pages used to hold a boot module. The precariousness stemmed from the fact that in the case of PV dom0, the initrd module pages may be either mapped or explicitly copied into the dom0 address space. So to handle this situation, the PV dom0 construction code will set the size of the module to zero, relying on discard_initial_images() to skip any modules with a size of zero.

A function is introduced to release a module when it is no longer needed that accepts a boolean parameter, free_mem, to indicate if the corresponding pages can be freed. To track that a module has been released, the boot module flag released is introduced.

The previous release model was a free all at once except those of size zeros, which would handle any unused modules passed. The new model is one of, free consumed module after usage is complete, thus unconsumed modules do not have a consumer to free them. To address this, the discard_uknown_boot_modules() is introduced and called after the last module identification occurs, initrd, to free the pages of any boot modules that are identified as not being released. After domain construction completes, all modules should be freed. A walk of the boot modules is added after domain construction to validate and notify if a module is found not to have been released.

---
Changes since v7:
- This is a new approach as an alternative to the consumed flag.

---
 xen/arch/x86/cpu/microcode/core.c   |  4 ++
 xen/arch/x86/hvm/dom0_build.c       |  7 ++--
 xen/arch/x86/include/asm/bootinfo.h |  2 +
 xen/arch/x86/include/asm/setup.h    |  3 +-
 xen/arch/x86/pv/dom0_build.c        | 20 ++--------
 xen/arch/x86/setup.c                | 57 +++++++++++++++++++++++------
 xen/xsm/xsm_core.c                  |  5 +++
 7 files changed, 67 insertions(+), 31 deletions(-)
xen/arch/x86/cpu/microcode/core.c +4 -0
index b09cf83249f6..349e857f539a 100644
@@ -756,6 +756,10 @@ static int __init cf_check microcode_init_cache(void)
         return rc;
     }
 
+    /* If raw module, we can free it now */
+    if ( !opt_scan )
+        release_boot_module(&bi->mods[early_mod_idx], true);
+
     if ( !patch )
         return -ENOENT;
 
xen/arch/x86/hvm/dom0_build.c +4 -3
index 3dd913bdb029..a4ac262db463 100644
@@ -704,6 +704,8 @@ static int __init pvh_load_kernel(struct domain *d, const module_t *image,
         return rc;
     }
 
+    release_module(image, true);
+
     /*
      * Find a RAM region big enough (and that doesn't overlap with the loaded
      * kernel) in order to load the initrd and the metadata. Note it could be
@@ -751,10 +753,9 @@ static int __init pvh_load_kernel(struct domain *d, const module_t *image,
             last_addr += len;
         }
         last_addr = ROUNDUP(last_addr, PAGE_SIZE);
-    }
 
-    /* Free temporary buffers. */
-    discard_initial_images();
+        release_module(initrd, true);
+    }
 
     if ( cmdline != NULL )
     {
xen/arch/x86/include/asm/bootinfo.h +2 -0
index 0fbf36739782..b1549d8c8f93 100644
@@ -33,8 +33,10 @@ struct boot_module {
     /*
      * Module State Flags:
      *   relocated: indicates module has been relocated in memory.
+     *   released:  indicates module's pages have been freed.
      */
     bool relocated:1;
+    bool released:1;
 };
 
 /*
xen/arch/x86/include/asm/setup.h +2 -1
index d7ed4f40024c..d68d37a5293b 100644
@@ -36,12 +36,13 @@ void setup_io_bitmap(struct domain *d);
 extern struct boot_info xen_boot_info;
 
 unsigned long initial_images_nrpages(nodeid_t node);
-void discard_initial_images(void);
+void release_module(const module_t *m, bool mapped);
 
 struct boot_module;
 void *bootstrap_map_bm(const struct boot_module *bm);
 void *bootstrap_map(const module_t *mod);
 void bootstrap_unmap(void);
+void release_boot_module(struct boot_module *bm, bool mapped);
 
 struct rangeset;
 int remove_xen_ranges(struct rangeset *r);
xen/arch/x86/pv/dom0_build.c +4 -16
index cc882bee61c3..c1f44502a1ac 100644
@@ -630,9 +630,7 @@ static int __init dom0_construct(struct domain *d,
                 }
             memcpy(page_to_virt(page), mfn_to_virt(initrd->mod_start),
                    initrd_len);
-            mpt_alloc = (paddr_t)initrd->mod_start << PAGE_SHIFT;
-            init_domheap_pages(mpt_alloc,
-                               mpt_alloc + PAGE_ALIGN(initrd_len));
+            release_module(initrd, true);
             initrd->mod_start = initrd_mfn = mfn_x(page_to_mfn(page));
         }
         else
@@ -640,18 +638,9 @@ static int __init dom0_construct(struct domain *d,
             while ( count-- )
                 if ( assign_pages(mfn_to_page(_mfn(mfn++)), 1, d, 0) )
                     BUG();
+            release_module(initrd, false);
         }
 
-        /*
-         * We have either:
-         * - Mapped the initrd directly into dom0, or
-         * - Copied it and freed the module.
-         *
-         * Either way, tell discard_initial_images() to not free it a second
-         * time.
-         */
-        initrd->mod_end = 0;
-
         iommu_memory_setup(d, "initrd", mfn_to_page(_mfn(initrd_mfn)),
                            PFN_UP(initrd_len), &flush_flags);
     }
@@ -839,7 +828,9 @@ static int __init dom0_construct(struct domain *d,
         printk("Failed to load the kernel binary\n");
         goto out;
     }
+    /* All done with kernel, release the module pages */
     bootstrap_unmap();
+    release_module(image, true);
 
     if ( UNSET_ADDR != parms.virt_hypercall )
     {
@@ -854,9 +845,6 @@ static int __init dom0_construct(struct domain *d,
         init_hypercall_page(d, _p(parms.virt_hypercall));
     }
 
-    /* Free temporary buffers. */
-    discard_initial_images();
-
     /* Set up start info area. */
     si = (start_info_t *)vstartinfo_start;
     clear_page(si);
xen/arch/x86/setup.c +46 -11

85 lines not on this page · raw message

xen/xsm/xsm_core.c +5 -0
index f255fb63bf6f..d5875599b63a 100644
@@ -162,6 +162,11 @@ int __init xsm_multiboot_init(struct boot_info *bi)
     ret = xsm_core_init(policy_buffer, policy_size);
     bootstrap_unmap();
 
+    /* If the policy was loaded from a boot module, release it's memory */
+    ret = first_boot_module_index(bi, BOOTMOD_XSM_POLICY);
+    if ( ret >= 0 && ret < bi->nr_modules )
+        release_boot_module(&bi->mods[ret], true);
+
     return 0;
 }
 #endif
Signature · 1 line
2.30.2
#23 lore raw

PATCH 5/12 x86/boot: move headroom to boot modules

In reply to Daniel P. Smith

The purpose of struct boot_module is to encapsulate the state of boot module as it is processed by Xen. Locating boot module state struct boot_module reduces the number of global variables as well as the number of state variables that must be passed around. It also lays the groundwork for hyperlaunch mult-domain construction, where multiple instances of state variables like headroom will be needed.

---
Changes since v7:
- adopted headroom comment from Andy Cooper

Changes since v6:
- add blank line to separate comment from line above it

Changes since v5:
- reword and expand comment on headroom
- consolidated declaration and assignment

---
 xen/arch/x86/include/asm/bootinfo.h | 22 ++++++++++++++++++++++
 xen/arch/x86/setup.c                | 23 ++++++++---------------
 2 files changed, 30 insertions(+), 15 deletions(-)
xen/arch/x86/include/asm/bootinfo.h +22 -0
index b1549d8c8f93..e337baa905f0 100644
@@ -30,6 +30,28 @@ struct boot_module {
 
     enum bootmod_type type;
 
+    /*
+     * A boot module may need decompressing by Xen.  Headroom is an estimate of
+     * the additional space required to decompress the module.
+     *
+     * Headroom is accounted for at the start of the module.  Decompressing is
+     * done in-place with input=start, output=start-headroom, expecting the
+     * pointers to become equal (give or take some rounding) when decompression
+     * is complete.
+     *
+     * Memory layout at boot:
+     *
+     *               start ----+
+     *                         v
+     *   |<-----headroom------>|<------size------->|
+     *                         +-------------------+
+     *                         | Compressed Module |
+     *   +---------------------+-------------------+
+     *   |           Decompressed Module           |
+     *   +-----------------------------------------+
+     */
+    unsigned long headroom;
+
     /*
      * Module State Flags:
      *   relocated: indicates module has been relocated in memory.
xen/arch/x86/setup.c +8 -15
index e6d2d25fd038..633319d39d80 100644
@@ -1062,7 +1062,7 @@ void asmlinkage __init noreturn __start_xen(void)
     struct cpu_info *info = get_cpu_info(), *bsp_info;
     unsigned int initrdidx, num_parked = 0;
     struct boot_info *bi;
-    unsigned long nr_pages, raw_max_page, modules_headroom;
+    unsigned long nr_pages, raw_max_page;
     int i, j, e820_warn = 0, bytes = 0;
     unsigned long eb_start, eb_end;
     bool acpi_boot_table_init_done = false, relocated = false;
@@ -1443,7 +1443,7 @@ void asmlinkage __init noreturn __start_xen(void)
         xen->mod->mod_end   = __2M_rwdata_end - _stext;
     }
 
-    modules_headroom =
+    bi->mods[0].headroom =
         bzimage_headroom(bootstrap_map_bm(&bi->mods[0]),
                          bi->mods[0].mod->mod_end);
     bootstrap_unmap();
@@ -1526,14 +1526,7 @@ void asmlinkage __init noreturn __start_xen(void)
         for ( j = bi->nr_modules - 1; j >= 0; j-- )
         {
             struct boot_module *bm = &bi->mods[j];
-
-            /*
-             * 'headroom' is a guess for the decompressed size and
-             * decompressor overheads of mod[0] (the dom0 kernel).  When we
-             * move mod[0], we incorporate this as extra space at the start.
-             */
-            unsigned long headroom = j ? 0 : modules_headroom;
-            unsigned long size = PAGE_ALIGN(headroom + bm->mod->mod_end);
+            unsigned long size = PAGE_ALIGN(bm->headroom + bm->mod->mod_end);
 
             if ( bm->relocated )
                 continue;
@@ -1546,13 +1539,13 @@ void asmlinkage __init noreturn __start_xen(void)
                 continue;
 
             if ( s < end &&
-                 (headroom ||
+                 (bm->headroom ||
                   ((end - size) >> PAGE_SHIFT) > bm->mod->mod_start) )
             {
-                move_memory(end - size + headroom,
+                move_memory(end - size + bm->headroom,
                             pfn_to_paddr(bm->mod->mod_start), bm->mod->mod_end);
                 bm->mod->mod_start = (end - size) >> PAGE_SHIFT;
-                bm->mod->mod_end += headroom;
+                bm->mod->mod_end += bm->headroom;
                 bm->relocated = true;
             }
         }
@@ -1579,7 +1572,7 @@ void asmlinkage __init noreturn __start_xen(void)
 #endif
     }
 
-    if ( modules_headroom && !bi->mods[0].relocated )
+    if ( bi->mods[0].headroom && !bi->mods[0].relocated )
         panic("Not enough memory to relocate the dom0 kernel image\n");
     for ( i = 0; i < bi->nr_modules; ++i )
     {
@@ -2145,7 +2138,7 @@ void asmlinkage __init noreturn __start_xen(void)
      * We're going to setup domain0 using the module(s) that we stashed safely
      * above our heap. The second module, if present, is an initrd ramdisk.
      */
-    dom0 = create_dom0(bi->mods[0].mod, modules_headroom,
+    dom0 = create_dom0(bi->mods[0].mod, bi->mods[0].headroom,
                        initrdidx < bi->nr_modules ? bi->mods[initrdidx].mod
                                                   : NULL,
                        kextra, bi->loader);
Signature · 1 line
2.30.2
#32 lore raw

PATCH 8/12 x86/boot: convert domain construction to use boot info

In reply to Daniel P. Smith

With all the components used to construct dom0 encapsulated in struct boot_info and struct boot_module, it is no longer necessary to pass all them as parameters down the domain construction call chain. Change the parameter list to pass the struct boot_info instance and the struct domain reference.

---
Changes since v5:
- renamed from "x86/boot: convert create_dom0 to use boot info"

Changes since v5:
- change headroom back to unsigned long
- make mod_idx unsigned int

---
 xen/arch/x86/dom0_build.c             |  9 ++--
 xen/arch/x86/hvm/dom0_build.c         | 49 +++++++++++++---------
 xen/arch/x86/include/asm/dom0_build.h | 13 ++----
 xen/arch/x86/include/asm/setup.h      |  7 ++--
 xen/arch/x86/pv/dom0_build.c          | 59 ++++++++++++++++-----------
 xen/arch/x86/setup.c                  | 33 ++++++++-------
 6 files changed, 95 insertions(+), 75 deletions(-)
xen/arch/x86/dom0_build.c +4 -5
index 8d56705a0861..2276965c02bd 100644
@@ -13,6 +13,7 @@
 #include <xen/softirq.h>
 
 #include <asm/amd.h>
+#include <asm/bootinfo.h>
 #include <asm/dom0_build.h>
 #include <asm/guest.h>
 #include <asm/hpet.h>
@@ -596,9 +597,7 @@ int __init dom0_setup_permissions(struct domain *d)
     return rc;
 }
 
-int __init construct_dom0(struct domain *d, const module_t *image,
-                          unsigned long image_headroom, module_t *initrd,
-                          const char *cmdline)
+int __init construct_dom0(struct boot_info *bi, struct domain *d)
 {
     int rc;
 
@@ -610,9 +609,9 @@ int __init construct_dom0(struct domain *d, const module_t *image,
     process_pending_softirqs();
 
     if ( is_hvm_domain(d) )
-        rc = dom0_construct_pvh(d, image, image_headroom, initrd, cmdline);
+        rc = dom0_construct_pvh(bi, d);
     else if ( is_pv_domain(d) )
-        rc = dom0_construct_pv(d, image, image_headroom, initrd, cmdline);
+        rc = dom0_construct_pv(bi, d);
     else
         panic("Cannot construct Dom0. No guest interface available\n");
 
xen/arch/x86/hvm/dom0_build.c +29 -20

105 lines not on this page · raw message

xen/arch/x86/include/asm/dom0_build.h +4 -9
index 107c1ff98367..4c2193c4a982 100644
@@ -13,15 +13,10 @@ unsigned long dom0_compute_nr_pages(struct domain *d,
                                     unsigned long initrd_len);
 int dom0_setup_permissions(struct domain *d);
 
-int dom0_construct_pv(struct domain *d, const module_t *image,
-                      unsigned long image_headroom,
-                      module_t *initrd,
-                      const char *cmdline);
-
-int dom0_construct_pvh(struct domain *d, const module_t *image,
-                       unsigned long image_headroom,
-                       module_t *initrd,
-                       const char *cmdline);
+struct boot_info;
+int dom0_construct_pv(struct boot_info *bi, struct domain *d);
+
+int dom0_construct_pvh(struct boot_info *bi, struct domain *d);
 
 unsigned long dom0_paging_pages(const struct domain *d,
                                 unsigned long nr_pages);
xen/arch/x86/include/asm/setup.h +3 -4
index d68d37a5293b..1e23f55be51b 100644
@@ -26,11 +26,10 @@ void subarch_init_memory(void);
 
 void init_IRQ(void);
 
+struct boot_info;
 int construct_dom0(
-    struct domain *d,
-    const module_t *image, unsigned long image_headroom,
-    module_t *initrd,
-    const char *cmdline);
+    struct boot_info *bi,
+    struct domain *d);
 void setup_io_bitmap(struct domain *d);
 
 extern struct boot_info xen_boot_info;
xen/arch/x86/pv/dom0_build.c +36 -23

132 lines not on this page · raw message

xen/arch/x86/setup.c +19 -14
index aba9df8620ef..d9785acf89b6 100644
@@ -977,10 +977,7 @@ static unsigned int __init copy_bios_e820(struct e820entry *map, unsigned int li
     return n;
 }
 
-static struct domain *__init create_dom0(const module_t *image,
-                                         unsigned long headroom,
-                                         module_t *initrd, const char *kextra,
-                                         const char *loader)
+static struct domain *__init create_dom0(struct boot_info *bi)
 {
     static char __initdata cmdline[MAX_GUEST_CMDLINE];
 
@@ -997,6 +994,14 @@ static struct domain *__init create_dom0(const module_t *image,
     };
     struct domain *d;
     domid_t domid;
+    struct boot_module *image;
+    unsigned int idx;
+
+    idx = first_boot_module_index(bi, BOOTMOD_KERNEL);
+    if ( unlikely(idx < 0 || idx > bi->nr_modules) )
+        panic("Missing kernel boot module for building Dom0\n");
+
+    image = &bi->mods[idx];
 
     if ( opt_dom0_pvh )
     {
@@ -1023,14 +1028,15 @@ static struct domain *__init create_dom0(const module_t *image,
         panic("Error creating d%uv0\n", domid);
 
     /* Grab the DOM0 command line. */
-    if ( image->string || kextra )
+    if ( image->cmdline_pa || bi->kextra )
     {
-        if ( image->string )
-            safe_strcpy(cmdline, cmdline_cook(__va(image->string), loader));
+        if ( image->cmdline_pa )
+            safe_strcpy(
+                cmdline, cmdline_cook(__va(image->cmdline_pa), bi->loader));
 
-        if ( kextra )
+        if ( bi->kextra )
             /* kextra always includes exactly one leading space. */
-            safe_strcat(cmdline, kextra);
+            safe_strcat(cmdline, bi->kextra);
 
         /* Append any extra parameters. */
         if ( skip_ioapic_setup && !strstr(cmdline, "noapic") )
@@ -1047,9 +1053,11 @@ static struct domain *__init create_dom0(const module_t *image,
             safe_strcat(cmdline, " acpi=");
             safe_strcat(cmdline, acpi_param);
         }
+
+        image->cmdline_pa = __pa(cmdline);
     }
 
-    if ( construct_dom0(d, image, headroom, initrd, cmdline) != 0 )
+    if ( construct_dom0(bi, d) != 0 )
         panic("Could not construct domain 0\n");
 
     return d;
@@ -2143,10 +2151,7 @@ void asmlinkage __init noreturn __start_xen(void)
      * We're going to setup domain0 using the module(s) that we stashed safely
      * above our heap. The second module, if present, is an initrd ramdisk.
      */
-    dom0 = create_dom0(bi->mods[0].mod, bi->mods[0].headroom,
-                       initrdidx < bi->nr_modules ? bi->mods[initrdidx].mod
-                                                  : NULL,
-                       bi->kextra, bi->loader);
+    dom0 = create_dom0(bi);
     if ( !dom0 )
         panic("Could not set up DOM0 guest OS\n");
 
Signature · 1 line
2.30.2
#45 lore raw

PATCH 10/12 x86/boot: introduce boot domain

In reply to Daniel P. Smith

To begin moving toward allowing the hypervisor to construct more than one domain at boot, a container is needed for a domain's build information. Introduce a new header, <xen/asm/bootdomain.h>, that contains the initial struct boot_domain that encapsulate the build information for a domain.

Add a kernel and ramdisk boot module reference along with a struct domain reference to the new struct boot_domain. This allows a struct boot_domain reference to be the only parameter necessary to pass down through the domain construction call chain.

---
Changes since v7
- collapsed the incremental changes to the call chain into this commit

---
 xen/arch/x86/dom0_build.c             |  7 +++---
 xen/arch/x86/hvm/dom0_build.c         | 16 +++++---------
 xen/arch/x86/include/asm/bootdomain.h | 31 +++++++++++++++++++++++++++
 xen/arch/x86/include/asm/bootinfo.h   |  5 +++++
 xen/arch/x86/include/asm/dom0_build.h |  6 +++---
 xen/arch/x86/include/asm/setup.h      |  6 ++----
 xen/arch/x86/pv/dom0_build.c          | 24 +++++++--------------
 xen/arch/x86/setup.c                  | 24 +++++++++------------
 8 files changed, 68 insertions(+), 51 deletions(-)
 create mode 100644 xen/arch/x86/include/asm/bootdomain.h
xen/arch/x86/dom0_build.c +4 -3
index 2276965c02bd..2f71d01a03e2 100644
@@ -597,9 +597,10 @@ int __init dom0_setup_permissions(struct domain *d)
     return rc;
 }
 
-int __init construct_dom0(struct boot_info *bi, struct domain *d)
+int __init construct_dom0(struct boot_domain *bd)
 {
     int rc;
+    const struct domain *d = bd->d;
 
     /* Sanity! */
     BUG_ON(!pv_shim && d->domain_id != 0);
@@ -609,9 +610,9 @@ int __init construct_dom0(struct boot_info *bi, struct domain *d)
     process_pending_softirqs();
 
     if ( is_hvm_domain(d) )
-        rc = dom0_construct_pvh(bi, d);
+        rc = dom0_construct_pvh(bd);
     else if ( is_pv_domain(d) )
-        rc = dom0_construct_pv(bi, d);
+        rc = dom0_construct_pv(bd);
     else
         panic("Cannot construct Dom0. No guest interface available\n");
 
xen/arch/x86/hvm/dom0_build.c +5 -11
index e74684f10b12..5aacfeb21fcf 100644
@@ -1302,25 +1302,19 @@ static void __hwdom_init pvh_setup_mmcfg(struct domain *d)
     }
 }
 
-int __init dom0_construct_pvh(struct boot_info *bi, struct domain *d)
+int __init dom0_construct_pvh(struct boot_domain *bd)
 {
     paddr_t entry, start_info;
-    struct boot_module *image;
-    struct boot_module *initrd = NULL;
+    struct boot_module *image = bd->kernel;
+    struct boot_module *initrd = bd->ramdisk;
+    struct domain *d = bd->d;
     int rc;
 
     printk(XENLOG_INFO "*** Building a PVH Dom%d ***\n", d->domain_id);
 
-    rc = first_boot_module_index(bi, BOOTMOD_KERNEL);
-    if ( unlikely(rc < 0 || rc > bi->nr_modules) )
+    if ( unlikely(image == NULL) )
         panic("Missing kernel boot module for %pd construction\n", d);
 
-    image = &bi->mods[rc];
-
-    rc = first_boot_module_index(bi, BOOTMOD_RAMDISK);
-    if ( rc > 0 || rc < bi->nr_modules )
-        initrd = &bi->mods[rc];
-
     if ( is_hardware_domain(d) )
     {
         /*
xen/arch/x86/include/asm/bootdomain.h new file +31 -0
new file mode 100644
index 000000000000..12c19ab37bd8
@@ -0,0 +1,31 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (c) 2024 Apertus Solutions, LLC
+ * Author: Daniel P. Smith <[email protected]>
+ * Copyright (c) 2024 Christopher Clark <[email protected]>
+ */
+
+#ifndef __XEN_X86_BOOTDOMAIN_H__
+#define __XEN_X86_BOOTDOMAIN_H__
+
+struct boot_module;
+struct domain;
+
+struct boot_domain {
+    struct boot_module *kernel;
+    struct boot_module *ramdisk;
+
+    struct domain *d;
+};
+
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/x86/include/asm/bootinfo.h +5 -0
index a5ee90a36607..7b793ca62f19 100644
@@ -10,10 +10,14 @@
 
 #include <xen/multiboot.h>
 #include <xen/types.h>
+#include <asm/bootdomain.h>
 
 /* Max number of boot modules a bootloader can provide in addition to Xen */
 #define MAX_NR_BOOTMODS 63
 
+/* Max number of boot domains that Xen can construct */
+#define MAX_NR_BOOTDOMS 1
+
 /* Boot module binary type / purpose */
 enum bootmod_type {
     BOOTMOD_UNKNOWN,
@@ -77,6 +81,7 @@ struct boot_info {
 
     unsigned int nr_modules;
     struct boot_module mods[MAX_NR_BOOTMODS + 1];
+    struct boot_domain domains[MAX_NR_BOOTDOMS];
 };
 
 /*
xen/arch/x86/include/asm/dom0_build.h +3 -3
index 4c2193c4a982..4a75fb25a801 100644
@@ -13,10 +13,10 @@ unsigned long dom0_compute_nr_pages(struct domain *d,
                                     unsigned long initrd_len);
 int dom0_setup_permissions(struct domain *d);
 
-struct boot_info;
-int dom0_construct_pv(struct boot_info *bi, struct domain *d);
+struct boot_domain;
+int dom0_construct_pv(struct boot_domain *bd);
 
-int dom0_construct_pvh(struct boot_info *bi, struct domain *d);
+int dom0_construct_pvh(struct boot_domain *bd);
 
 unsigned long dom0_paging_pages(const struct domain *d,
                                 unsigned long nr_pages);
xen/arch/x86/include/asm/setup.h +2 -4
index 0591847c57e6..4968ac38398b 100644
@@ -26,10 +26,8 @@ void subarch_init_memory(void);
 
 void init_IRQ(void);
 
-struct boot_info;
-int construct_dom0(
-    struct boot_info *bi,
-    struct domain *d);
+struct boot_domain;
+int construct_dom0(struct boot_domain *bd);
 void setup_io_bitmap(struct domain *d);
 
 extern struct boot_info xen_boot_info;
xen/arch/x86/pv/dom0_build.c +8 -16
index 5efc650eeff0..28257bf13127 100644
@@ -355,7 +355,7 @@ static struct page_info * __init alloc_chunk(struct domain *d,
     return page;
 }
 
-static int __init dom0_construct(struct boot_info *bi, struct domain *d)
+static int __init dom0_construct(struct boot_domain *bd)
 {
     int i, rc, order, machine;
     bool compatible, compat;
@@ -370,13 +370,14 @@ static int __init dom0_construct(struct boot_info *bi, struct domain *d)
     struct page_info *page = NULL;
     unsigned int flush_flags = 0;
     start_info_t *si;
+    struct domain *d = bd->d;
     struct vcpu *v = d->vcpu[0];
-    struct boot_module *image;
-    struct boot_module *initrd = NULL;
+    struct boot_module *image = bd->kernel;
+    struct boot_module *initrd = bd->ramdisk;
     void *image_base;
     unsigned long image_len;
     void *image_start;
-    unsigned long initrd_len = 0;
+    unsigned long initrd_len = initrd ? initrd->size : 0;
     const char *cmdline;
     l4_pgentry_t *l4tab = NULL, *l4start = NULL;
     l3_pgentry_t *l3tab = NULL, *l3start = NULL;
@@ -414,23 +415,14 @@ static int __init dom0_construct(struct boot_info *bi, struct domain *d)
 
     printk(XENLOG_INFO "*** Building a PV Dom%d ***\n", d->domain_id);
 
-    i = first_boot_module_index(bi, BOOTMOD_KERNEL);
-    if ( unlikely(i < 0 || i > bi->nr_modules) )
+    if ( unlikely(image == NULL) )
         panic("Missing kernel boot module for %pd construction\n", d);
 
-    image = &bi->mods[i];
     image_base = bootstrap_map_bm(image);
     image_len = image->size;
     image_start = image_base + image->headroom;
     cmdline = __va(image->cmdline_pa);
 
-    i = first_boot_module_index(bi, BOOTMOD_RAMDISK);
-    if ( i > 0 || i < bi->nr_modules )
-    {
-        initrd = &bi->mods[i];
-        initrd_len = initrd->size;
-    }
-
     d->max_pages = ~0U;
 
     if ( (rc = bzimage_parse(image_base, &image_start, &image_len)) != 0 )
@@ -1066,7 +1058,7 @@ out:
     return rc;
 }
 
-int __init dom0_construct_pv(struct boot_info *bi, struct domain *d)
+int __init dom0_construct_pv(struct boot_domain *bd)
 {
     unsigned long cr4 = read_cr4();
     int rc;
@@ -1084,7 +1076,7 @@ int __init dom0_construct_pv(struct boot_info *bi, struct domain *d)
         write_cr4(cr4 & ~X86_CR4_SMAP);
     }
 
-    rc = dom0_construct(bi, d);
+    rc = dom0_construct(bd);
 
     if ( cr4 & X86_CR4_SMAP )
     {
xen/arch/x86/setup.c +10 -14
index 18b93d6a272a..6e25a9f726c7 100644
@@ -990,16 +990,9 @@ static struct domain *__init create_dom0(struct boot_info *bi)
             .misc_flags = opt_dom0_msr_relaxed ? XEN_X86_MSR_RELAXED : 0,
         },
     };
+    struct boot_domain *bd = &bi->domains[0];
     struct domain *d;
     domid_t domid;
-    struct boot_module *image;
-    unsigned int idx;
-
-    idx = first_boot_module_index(bi, BOOTMOD_KERNEL);
-    if ( unlikely(idx < 0 || idx > bi->nr_modules) )
-        panic("Missing kernel boot module for building Dom0\n");
-
-    image = &bi->mods[idx];
 
     if ( opt_dom0_pvh )
     {
@@ -1026,11 +1019,11 @@ static struct domain *__init create_dom0(struct boot_info *bi)
         panic("Error creating d%uv0\n", domid);
 
     /* Grab the DOM0 command line. */
-    if ( image->cmdline_pa || bi->kextra )
+    if ( bd->kernel->cmdline_pa || bi->kextra )
     {
-        if ( image->cmdline_pa )
-            safe_strcpy(
-                cmdline, cmdline_cook(__va(image->cmdline_pa), bi->loader));
+        if ( bd->kernel->cmdline_pa )
+            safe_strcpy(cmdline, cmdline_cook(__va(bd->kernel->cmdline_pa),
+                        bi->loader));
 
         if ( bi->kextra )
             /* kextra always includes exactly one leading space. */
@@ -1052,10 +1045,11 @@ static struct domain *__init create_dom0(struct boot_info *bi)
             safe_strcat(cmdline, acpi_param);
         }
 
-        image->cmdline_pa = __pa(cmdline);
+        bd->kernel->cmdline_pa = __pa(cmdline);
     }
 
-    if ( construct_dom0(bi, d) != 0 )
+    bd->d = d;
+    if ( construct_dom0(bd) != 0 )
         panic("Could not construct domain 0\n");
 
     return d;
@@ -1255,6 +1249,7 @@ void asmlinkage __init noreturn __start_xen(void)
 
     /* Dom0 kernel is always first */
     bi->mods[0].type = BOOTMOD_KERNEL;
+    bi->domains[0].kernel = &bi->mods[0];
 
     if ( pvh_boot )
     {
@@ -2125,6 +2120,7 @@ void asmlinkage __init noreturn __start_xen(void)
     if ( initrdidx < MAX_NR_BOOTMODS )
     {
         bi->mods[initrdidx].type = BOOTMOD_RAMDISK;
+        bi->domains[0].ramdisk = &bi->mods[initrdidx];
         if ( first_boot_module_index(bi, BOOTMOD_UNKNOWN) < MAX_NR_BOOTMODS )
             printk(XENLOG_WARNING
                    "Multiple initrd candidates, picking module #%u\n",
Signature · 1 line
2.30.2
#39 lore raw

PATCH 9/12 x86/boot: add start and size fields to struct boot_module

In reply to Daniel P. Smith

Introduce the start and size fields to struct boot_module and assigns their value during boot_info construction. All uses of module_t to get the address and size of a module are replaced with start and size.

The EFI entry point is a special case, as the EFI file loading boot service may load a file beyond the 4G barrier. As a result, to make the address fit in the 32bit integer used by the MB1 module_t structure, the frame number is stored in mod_start and size in mod_end. Until the EFI entry point is enlightened to work with boot_info and boot_module, multiboot_fill_boot_info will handle the alternate values in mod_start and mod_end when EFI is detected.

A result of the switch to start/size removes all uses of the mod field in struct boot_modules, along with the uses of bootstra_map() and release_module() functions. With all usage gone, they all are dropped here.

--
Changes since v7:
- add the start/size change to bootstrap_map_bm()
- convert all BM start/size when introduced, consolidates:
x86/boot: populate boot module for xen entry
x86/boot: transition relocation calculations to struct boot_module
- consolidates all the removal commits

Changes since v6:
- put the efi conversion for mod_start and mod_end back along with check
- dropped unnecessary cast
- updated the population of start and size fields to take into account efi

Changes since v5:
- switched EFI population of mod_start/mod_end to addresses

a# edit 336ac1fc0019 x86/boot: introduce boot domain

---
 xen/arch/x86/cpu/microcode/core.c   |   8 +--
 xen/arch/x86/hvm/dom0_build.c       |   6 +-
 xen/arch/x86/include/asm/bootinfo.h |   6 +-
 xen/arch/x86/include/asm/setup.h    |   2 -
 xen/arch/x86/pv/dom0_build.c        |  17 ++---
 xen/arch/x86/setup.c                | 100 ++++++++++++----------------
 xen/xsm/xsm_policy.c                |   4 +-
 7 files changed, 64 insertions(+), 79 deletions(-)
xen/arch/x86/cpu/microcode/core.c +4 -4
index 349e857f539a..891e6d29c7f4 100644
@@ -726,8 +726,8 @@ static int __init cf_check microcode_init_cache(void)
         /* early_microcode_load() didn't leave us any work to do. */
         return 0;
 
-    size = bi->mods[early_mod_idx].mod->mod_end;
-    data = __mfn_to_virt(bi->mods[early_mod_idx].mod->mod_start);
+    size = bi->mods[early_mod_idx].size;
+    data = __va(bi->mods[early_mod_idx].start);
 
     /*
      * If opt_scan is set, we're looking for a CPIO archive rather than a raw
@@ -799,7 +799,7 @@ static int __init early_microcode_load(struct boot_info *bi)
             struct boot_module *bm = &bi->mods[idx];
             struct cpio_data cd;
 
-            size = bm->mod->mod_end;
+            size = bm->size;
             data = bootstrap_map_bm(bm);
             if ( !data )
             {
@@ -849,7 +849,7 @@ static int __init early_microcode_load(struct boot_info *bi)
         }
         bi->mods[idx].type = BOOTMOD_MICROCODE;
 
-        size = bi->mods[idx].mod->mod_end;
+        size = bi->mods[idx].size;
         data = bootstrap_map_bm(&bi->mods[idx]);
         if ( !data )
         {
xen/arch/x86/hvm/dom0_build.c +3 -3
index cd97f94a168a..e74684f10b12 100644
@@ -649,8 +649,8 @@ static int __init pvh_load_kernel(
 {
     void *image_base = bootstrap_map_bm(image);
     void *image_start = image_base + image->headroom;
-    unsigned long image_len = image->mod->mod_end;
-    unsigned long initrd_len = initrd ? initrd->mod->mod_end : 0;
+    unsigned long image_len = image->size;
+    unsigned long initrd_len = initrd ? initrd->size : 0;
     const char *cmdline = __va(image->cmdline_pa);
     struct elf_binary elf;
     struct elf_dom_parms parms;
@@ -729,7 +729,7 @@ static int __init pvh_load_kernel(
     if ( initrd != NULL )
     {
         rc = hvm_copy_to_guest_phys(
-            last_addr, mfn_to_virt(initrd->mod->mod_start), initrd_len, v);
+            last_addr, __va(initrd->start), initrd_len, v);
         if ( rc )
         {
             printk("Unable to copy initrd to guest\n");
xen/arch/x86/include/asm/bootinfo.h +3 -3
index 819c8224d715..a5ee90a36607 100644
@@ -25,9 +25,6 @@ enum bootmod_type {
 };
 
 struct boot_module {
-    /* Transitionary only */
-    module_t *mod;
-
     enum bootmod_type type;
 
     /*
@@ -61,6 +58,9 @@ struct boot_module {
     bool released:1;
 
     paddr_t cmdline_pa;
+
+    paddr_t start;
+    size_t size;
 };
 
 /*
xen/arch/x86/include/asm/setup.h +0 -2
index 1e23f55be51b..0591847c57e6 100644
@@ -35,11 +35,9 @@ void setup_io_bitmap(struct domain *d);
 extern struct boot_info xen_boot_info;
 
 unsigned long initial_images_nrpages(nodeid_t node);
-void release_module(const module_t *m, bool mapped);
 
 struct boot_module;
 void *bootstrap_map_bm(const struct boot_module *bm);
-void *bootstrap_map(const module_t *mod);
 void bootstrap_unmap(void);
 void release_boot_module(struct boot_module *bm, bool mapped);
 
xen/arch/x86/pv/dom0_build.c +9 -8
index 594874cd8d85..5efc650eeff0 100644
@@ -420,7 +420,7 @@ static int __init dom0_construct(struct boot_info *bi, struct domain *d)
 
     image = &bi->mods[i];
     image_base = bootstrap_map_bm(image);
-    image_len = image->mod->mod_end;
+    image_len = image->size;
     image_start = image_base + image->headroom;
     cmdline = __va(image->cmdline_pa);
 
@@ -428,7 +428,7 @@ static int __init dom0_construct(struct boot_info *bi, struct domain *d)
     if ( i > 0 || i < bi->nr_modules )
     {
         initrd = &bi->mods[i];
-        initrd_len = initrd->mod->mod_end;
+        initrd_len = initrd->size;
     }
 
     d->max_pages = ~0U;
@@ -630,7 +630,8 @@ static int __init dom0_construct(struct boot_info *bi, struct domain *d)
         initrd_pfn = vinitrd_start ?
                      (vinitrd_start - v_start) >> PAGE_SHIFT :
                      domain_tot_pages(d);
-        initrd_mfn = mfn = initrd->mod->mod_start;
+        initrd_mfn = paddr_to_pfn(initrd->start);
+        mfn = initrd_mfn;
         count = PFN_UP(initrd_len);
         if ( d->arch.physaddr_bitsize &&
              ((mfn + count - 1) >> (d->arch.physaddr_bitsize - PAGE_SHIFT)) )
@@ -645,10 +646,10 @@ static int __init dom0_construct(struct boot_info *bi, struct domain *d)
                     free_domheap_pages(page, order);
                     page += 1UL << order;
                 }
-            memcpy(page_to_virt(page), mfn_to_virt(initrd->mod->mod_start),
-                   initrd_len);
+            memcpy(page_to_virt(page), __va(initrd->start), initrd_len);
             release_boot_module(initrd, true);
-            initrd->mod->mod_start = initrd_mfn = mfn_x(page_to_mfn(page));
+            initrd_mfn = mfn_x(page_to_mfn(page));
+            initrd->start = pfn_to_paddr(initrd_mfn);
         }
         else
         {
@@ -670,7 +671,7 @@ static int __init dom0_construct(struct boot_info *bi, struct domain *d)
                nr_pages - domain_tot_pages(d));
     if ( initrd )
     {
-        mpt_alloc = (paddr_t)initrd->mod->mod_start << PAGE_SHIFT;
+        mpt_alloc = initrd->start;
         printk("\n Init. ramdisk: %"PRIpaddr"->%"PRIpaddr,
                mpt_alloc, mpt_alloc + initrd_len);
     }
@@ -897,7 +898,7 @@ static int __init dom0_construct(struct boot_info *bi, struct domain *d)
         if ( pfn >= initrd_pfn )
         {
             if ( pfn < initrd_pfn + PFN_UP(initrd_len) )
-                mfn = initrd->mod->mod_start + (pfn - initrd_pfn);
+                mfn = paddr_to_pfn(initrd->start) + (pfn - initrd_pfn);
             else
                 mfn -= PFN_UP(initrd_len);
         }
xen/arch/x86/setup.c +43 -57

191 lines not on this page · raw message

xen/xsm/xsm_policy.c +2 -2
index 12c9de5a1fbf..f8836026f63b 100644
@@ -43,8 +43,8 @@ int __init xsm_multiboot_policy_init(
     {
         struct boot_module *bm = &bi->mods[i];
 
-        _policy_start = bootstrap_map(bm->mod);
-        _policy_len   = bm->mod->mod_end;
+        _policy_start = bootstrap_map_bm(bm);
+        _policy_len   = bm->size;
 
         if ( (xsm_magic_t)(*_policy_start) == XSM_MAGIC )
         {
Signature · 1 line
2.30.2
#26 lore raw

PATCH 6/12 x86/boot: relocate kextra into boot info

In reply to Daniel P. Smith

Move kextra into struct boot_info, thus no longer needed to be passed as a parameter to create_dom0.

---
 xen/arch/x86/include/asm/bootinfo.h | 1 +
 xen/arch/x86/setup.c                | 3 ++-
 2 files changed, 3 insertions(+), 1 deletion(-)
xen/arch/x86/include/asm/bootinfo.h +1 -0
index e337baa905f0..6638da597073 100644
@@ -68,6 +68,7 @@ struct boot_module {
 struct boot_info {
     const char *loader;
     const char *cmdline;
+    const char *kextra;
 
     paddr_t memmap_addr;
     size_t memmap_length;
xen/arch/x86/setup.c +2 -1
index 633319d39d80..425467a0d977 100644
@@ -1136,6 +1136,7 @@ void asmlinkage __init noreturn __start_xen(void)
         *kextra = '\0';
         kextra += 3;
         while ( kextra[1] == ' ' ) kextra++;
+        bi->kextra = kextra;
     }
     cmdline_parse(bi->cmdline);
 
@@ -2141,7 +2142,7 @@ void asmlinkage __init noreturn __start_xen(void)
     dom0 = create_dom0(bi->mods[0].mod, bi->mods[0].headroom,
                        initrdidx < bi->nr_modules ? bi->mods[initrdidx].mod
                                                   : NULL,
-                       kextra, bi->loader);
+                       bi->kextra, bi->loader);
     if ( !dom0 )
         panic("Could not set up DOM0 guest OS\n");
 
Signature · 1 line
2.30.2
#48 lore raw

PATCH 11/12 x86/boot: introduce domid field to struct boot_domain

In reply to Daniel P. Smith

Add a domid field to struct boot_domain to hold the assigned domain id for the domain. During initialization, ensure all instances of struct boot_domain have the invalid domid to ensure that the domid must be set either by convention or configuration.

---
 xen/arch/x86/include/asm/bootdomain.h |  2 ++
 xen/arch/x86/setup.c                  | 12 +++++++-----
 2 files changed, 9 insertions(+), 5 deletions(-)
xen/arch/x86/include/asm/bootdomain.h +2 -0
index 12c19ab37bd8..3873f916f854 100644
@@ -12,6 +12,8 @@ struct boot_module;
 struct domain;
 
 struct boot_domain {
+    domid_t domid;
+
     struct boot_module *kernel;
     struct boot_module *ramdisk;
 
xen/arch/x86/setup.c +7 -5
index 6e25a9f726c7..7b78bd9c7c8d 100644
@@ -338,6 +338,9 @@ static struct boot_info *__init multiboot_fill_boot_info(
     /* Variable 'i' should be one entry past the last module. */
     bi->mods[i].type = BOOTMOD_XEN;
 
+    for ( i = 0; i < MAX_NR_BOOTDOMS; i++ )
+        bi->domains[i].domid = DOMID_INVALID;
+
     return bi;
 }
 
@@ -992,7 +995,6 @@ static struct domain *__init create_dom0(struct boot_info *bi)
     };
     struct boot_domain *bd = &bi->domains[0];
     struct domain *d;
-    domid_t domid;
 
     if ( opt_dom0_pvh )
     {
@@ -1008,15 +1010,15 @@ static struct domain *__init create_dom0(struct boot_info *bi)
         dom0_cfg.flags |= XEN_DOMCTL_CDF_iommu;
 
     /* Create initial domain.  Not d0 for pvshim. */
-    domid = get_initial_domain_id();
-    d = domain_create(domid, &dom0_cfg, pv_shim ? 0 : CDF_privileged);
+    bd->domid = get_initial_domain_id();
+    d = domain_create(bd->domid, &dom0_cfg, pv_shim ? 0 : CDF_privileged);
     if ( IS_ERR(d) )
-        panic("Error creating d%u: %ld\n", domid, PTR_ERR(d));
+        panic("Error creating d%u: %ld\n", bd->domid, PTR_ERR(d));
 
     init_dom0_cpuid_policy(d);
 
     if ( alloc_dom0_vcpu0(d) == NULL )
-        panic("Error creating d%uv0\n", domid);
+        panic("Error creating d%uv0\n", bd->domid);
 
     /* Grab the DOM0 command line. */
     if ( bd->kernel->cmdline_pa || bi->kextra )
Signature · 1 line
2.30.2
#29 lore raw

PATCH 7/12 x86/boot: add cmdline_pa to struct boot_module

In reply to Daniel P. Smith

Add an address field, cmdline_pa, to struct boot_module to hold the address of the string field from struct mod.

---
Changes since v7:
- renamed cmdline to cmdline_pa
- reword commit message for field rename

Changes since v5:
- changed boot_module element cmdline to paddr_t

---
 xen/arch/x86/include/asm/bootinfo.h | 2 ++
 xen/arch/x86/setup.c                | 4 ++++
 2 files changed, 6 insertions(+)
xen/arch/x86/include/asm/bootinfo.h +2 -0
index 6638da597073..819c8224d715 100644
@@ -59,6 +59,8 @@ struct boot_module {
      */
     bool relocated:1;
     bool released:1;
+
+    paddr_t cmdline_pa;
 };
 
 /*
xen/arch/x86/setup.c +4 -0
index 425467a0d977..aba9df8620ef 100644
@@ -312,8 +312,12 @@ static struct boot_info *__init multiboot_fill_boot_info(
      * reserved for Xen.
      */
     for ( i = 0; i < MAX_NR_BOOTMODS && i < bi->nr_modules; i++ )
+    {
         bi->mods[i].mod = &mods[i];
 
+        bi->mods[i].cmdline_pa = mods[i].string;
+    }
+
     /* Variable 'i' should be one entry past the last module. */
     bi->mods[i].mod = &mods[bi->nr_modules];
     bi->mods[i].type = BOOTMOD_XEN;
Signature · 1 line
2.30.2
#49 lore raw

PATCH 12/12 x86/boot: add cmdline to struct boot_domain

In reply to Daniel P. Smith

Add a container for the "cooked" command line for a domain. This provides for the backing memory to be directly associated with the domain being constructed. This is done in anticipation that the domain construction path may need to be invoked multiple times, thus ensuring each instance had a distinct memory allocation.

---
Changes since v7:
- updated commit message to expand on intent and purpose

---
 xen/arch/x86/include/asm/bootdomain.h |  4 ++++
 xen/arch/x86/include/asm/dom0_build.h |  1 +
 xen/arch/x86/pv/dom0_build.c          |  4 ++--
 xen/arch/x86/setup.c                  | 18 ++++++++----------
 4 files changed, 15 insertions(+), 12 deletions(-)
xen/arch/x86/include/asm/bootdomain.h +4 -0
index 3873f916f854..bc51f04a1df6 100644
@@ -8,10 +8,14 @@
 #ifndef __XEN_X86_BOOTDOMAIN_H__
 #define __XEN_X86_BOOTDOMAIN_H__
 
+#include <public/xen.h>
+
 struct boot_module;
 struct domain;
 
 struct boot_domain {
+    char cmdline[MAX_GUEST_CMDLINE];
+
     domid_t domid;
 
     struct boot_module *kernel;
xen/arch/x86/include/asm/dom0_build.h +1 -0
index 4a75fb25a801..982bc1fa9e6b 100644
@@ -4,6 +4,7 @@
 #include <xen/libelf.h>
 #include <xen/sched.h>
 
+#include <asm/bootinfo.h>
 #include <asm/setup.h>
 
 extern unsigned int dom0_memflags;
xen/arch/x86/pv/dom0_build.c +2 -2
index 28257bf13127..2c84af52de3e 100644
@@ -960,8 +960,8 @@ static int __init dom0_construct(struct boot_domain *bd)
     }
 
     memset(si->cmd_line, 0, sizeof(si->cmd_line));
-    if ( cmdline != NULL )
-        strlcpy((char *)si->cmd_line, cmdline, sizeof(si->cmd_line));
+    if ( cmdline[0] != '\0' )
+        strlcpy((char *)si->cmd_line, bd->cmdline, sizeof(si->cmd_line));
 
 #ifdef CONFIG_VIDEO
     if ( !pv_shim && fill_console_start_info((void *)(si + 1)) )
xen/arch/x86/setup.c +8 -10
index 7b78bd9c7c8d..9db8a650ecc2 100644
@@ -980,8 +980,6 @@ static unsigned int __init copy_bios_e820(struct e820entry *map, unsigned int li
 
 static struct domain *__init create_dom0(struct boot_info *bi)
 {
-    static char __initdata cmdline[MAX_GUEST_CMDLINE];
-
     struct xen_domctl_createdomain dom0_cfg = {
         .flags = IS_ENABLED(CONFIG_TBOOT) ? XEN_DOMCTL_CDF_s3_integrity : 0,
         .max_evtchn_port = -1,
@@ -1024,16 +1022,16 @@ static struct domain *__init create_dom0(struct boot_info *bi)
     if ( bd->kernel->cmdline_pa || bi->kextra )
     {
         if ( bd->kernel->cmdline_pa )
-            safe_strcpy(cmdline, cmdline_cook(__va(bd->kernel->cmdline_pa),
+            safe_strcpy(bd->cmdline, cmdline_cook(__va(bd->kernel->cmdline_pa),
                         bi->loader));
 
         if ( bi->kextra )
             /* kextra always includes exactly one leading space. */
-            safe_strcat(cmdline, bi->kextra);
+            safe_strcat(bd->cmdline, bi->kextra);
 
         /* Append any extra parameters. */
-        if ( skip_ioapic_setup && !strstr(cmdline, "noapic") )
-            safe_strcat(cmdline, " noapic");
+        if ( skip_ioapic_setup && !strstr(bd->cmdline, "noapic") )
+            safe_strcat(bd->cmdline, " noapic");
 
         if ( (strlen(acpi_param) == 0) && acpi_disabled )
         {
@@ -1041,13 +1039,13 @@ static struct domain *__init create_dom0(struct boot_info *bi)
             safe_strcpy(acpi_param, "off");
         }
 
-        if ( (strlen(acpi_param) != 0) && !strstr(cmdline, "acpi=") )
+        if ( (strlen(acpi_param) != 0) && !strstr(bd->cmdline, "acpi=") )
         {
-            safe_strcat(cmdline, " acpi=");
-            safe_strcat(cmdline, acpi_param);
+            safe_strcat(bd->cmdline, " acpi=");
+            safe_strcat(bd->cmdline, acpi_param);
         }
 
-        bd->kernel->cmdline_pa = __pa(cmdline);
+        bd->kernel->cmdline_pa = __pa(bd->cmdline);
     }
 
     bd->d = d;
Signature · 1 line
2.30.2
#52 lore raw
In reply to Daniel P. Smith
Quoted 14 lines · Daniel P. Smith

The Boot Modules for Hyperlaunch series is an effort to split out preliminary changes necessary for the introduction of the Hyperlaunch domain builder logic. These preliminary changes revolve around introducing the struct boot_module and struct boot_domain structures. This includes converting the dom0 construction path to use these structures. These abstractions lay the groundwork to transform and extend the dom0 construction logic into a limited, but general domain builder.

The splitting of Hyperlaunch into a set of series are twofold, to reduce the effort in reviewing a much larger series, and to reduce the effort in handling the knock-on effects to the construction logic from requested review changes.

Much thanks to AMD for supporting this work.

Quoted 8 lines

!!NOTE on v8!!
For this version a complete re-organization of the changes were made with the concept that three tasks were being carried out that were intertwined in the past versions. These three tasks were 1. capturing the state tracking and enrichment, 2. removing the use of module_t to track where a boot module is located in memory, and 3. abstract the domain construction state. The first eight patches address task 1, task 2 is address with patch nine, and the remaining addresses task 3.

Just to mention it - a v8 tag would also have been nice in the mail thread subjects.

Jan

#53 lore raw
Quoted 24 lines · Jan Beulich
Quoted 14 lines · Daniel P. Smith

The Boot Modules for Hyperlaunch series is an effort to split out preliminary changes necessary for the introduction of the Hyperlaunch domain builder logic. These preliminary changes revolve around introducing the struct boot_module and struct boot_domain structures. This includes converting the dom0 construction path to use these structures. These abstractions lay the groundwork to transform and extend the dom0 construction logic into a limited, but general domain builder.

The splitting of Hyperlaunch into a set of series are twofold, to reduce the effort in reviewing a much larger series, and to reduce the effort in handling the knock-on effects to the construction logic from requested review changes.

Much thanks to AMD for supporting this work.

Quoted 8 lines

!!NOTE on v8!!
For this version a complete re-organization of the changes were made with the concept that three tasks were being carried out that were intertwined in the past versions. These three tasks were 1. capturing the state tracking and enrichment, 2. removing the use of module_t to track where a boot module is located in memory, and 3. abstract the domain construction state. The first eight patches address task 1, task 2 is address with patch nine, and the remaining addresses task 3.

Quoted 2 lines

Just to mention it - a v8 tag would also have been nice in the mail thread subjects.

Absolutely, my apologies, I totally missed that I hadn't tagged the series.

v/r,
dps

Reviewed-by on v1 1/12
#2 lore raw

Re: [PATCH 01/12] x86/boot: introduce boot module types

In reply to Daniel P. Smith
Quoted 6 lines · Daniel P. Smith

This commit introduces module types for the types of boot modules that may be passed to Xen. These include xen, kernel, ramdisk, microcode, and xsm policy. This reduces the need for hard coded order assumptions and global variables to be used by consumers of boot modules, such as domain construction.

one extra suggestion.

xen/arch/x86/cpu/microcode/core.c

Quoted 18 lines

---
Changes since v7:
- merged the addition of microcode and xsm boot mod types

Changes since v5:
- added guard around initrd type assignment
- removed a missed rebase artifact

---
 xen/arch/x86/cpu/microcode/core.c   |  1 +
 xen/arch/x86/include/asm/bootinfo.h | 12 ++++++++++++
 xen/arch/x86/setup.c                |  4 ++++
 xen/xsm/xsm_policy.c                |  1 +
 4 files changed, 18 insertions(+)
xen/arch/x86/cpu/microcode/core.c +0 -0
index 1fa6cbf3d364..f46464241557 100644

@@ -831,6 +831,10 @@ static int __init early_microcode_load(struct boot_info *bi)
                 continue;
             }

+            /*
+             * Do not alter this boot module's type.  We're most likely +             * peeking at dom0's initrd.
+             */
             data = cd.data;
             size = cd.size;
             goto found;

To make it absolutely crystal clear that ...

xen/arch/x86/cpu/microcode/core.c static int __init early_microcode_load(struct boot_info *bi) · line 848

Quoted 5 lines
xen/arch/x86/cpu/microcode/core.c +1 -0
@@ -845,6 +845,7 @@ static int __init early_microcode_load(struct boot_info *bi)
             printk(XENLOG_WARNING "Microcode: Chosen module %d already used\n", idx);
             return -ENODEV;
         }
+        bi->mods[idx].type = BOOTMOD_MICROCODE;

... this is deliberately not done on the scan path.

~Andrew

#5 lore raw

Re: [PATCH 02/12] x86/boot: eliminate module_map

In reply to Daniel P. Smith
Quoted 7 lines · Daniel P. Smith

With all boot modules now labeled by type, it is no longer necessary to track whether a boot module was identified via the module_map bitmap.

Introduce a set of helpers to search the list of boot modules based on type and the reference type, pointer or array index, desired. Then drop all uses of setting a bit in module_map and replace its use for looping with the helpers.

Quoted 7 lines

---
Changes since v7:
- collapse the three module_map patches into one,
- x86/boot: remove module_map usage from microcode loading
- x86/boot: remove module_map usage from xsm policy loading
- x86/boot: remove module_map usage by ramdisk loading

Definitely nicer for having been collapsed together.

Quoted 12 lines
---
 xen/arch/x86/cpu/microcode/core.c   | 12 ++++-----
 xen/arch/x86/include/asm/bootinfo.h | 42 ++++++++++++++++++++++++++++-
 xen/arch/x86/setup.c                | 28 +++++++++++--------
 xen/xsm/xsm_policy.c                | 19 +++++--------
 4 files changed, 70 insertions(+), 31 deletions(-)
xen/arch/x86/cpu/microcode/core.c +0 -0
index f46464241557..b09cf83249f6 100644

xen/arch/x86/cpu/microcode/core.c static int __init early_microcode_load(struct boot_info *bi) · line 793

Quoted 5 lines
xen/arch/x86/cpu/microcode/core.c +1 -1
@@ -790,15 +790,13 @@ static int __init early_microcode_load(struct boot_info *bi)
 
     if ( opt_scan ) /* Scan for a CPIO archive */
     {
-        for ( idx = 1; idx < bi->nr_modules; ++idx )
+        for_each_boot_module_by_type(idx, bi, BOOTMOD_UNKNOWN)

Minor, but we treat for_each_* as if they were for loops, so this either wants to be

for_each_boot_module_by_type ( idx, bi, BOOTMOD_UNKNOWN )

or

for_each_boot_module_by_type (idx, bi, BOOTMOD_UNKNOWN)

spacing wise.  There's no agreement between maintainers on the extra spaces inside brackets or not.

However, despite looking at this many times, I've only just realised... This semantically changes things in a direction that we won't want.

Today, BOOTMOD_RAMDISK only happens a side effect of being "first BOOTMOD_UNKNOWN standing at the end".

But the EFI boot code ought to set bi->type=RAMDISK explicitly from the ramdisk= argument (it can probably set type=MICROCODE too), and future plans with a large HL config probably will be similar.

Anything which sets type=, and type=RAMDISK in particular, prior to early_microcode_load() excludes it from the search.  This is definitely not what we want.

It's a latent bug for now, but I'd suggest keeping the plain for loop, with

            /* Search anything unclaimed or likely to be a CPIO archive. */ if ( bm->type != BOOTMOD_UNKNOWN &&
                 bm->type != BOOTMOD_RAMDISK )
                continue;

as the selection criteria.  Probably also want to start from idx=0 to remove assumptions about the dom0 kernel.

Thoughts?

Quoted 65 lines
xen/arch/x86/cpu/microcode/core.c +4 -6
         {
+            struct boot_module *bm = &bi->mods[idx];
             struct cpio_data cd;
 
-            if ( !test_bit(idx, bi->module_map) )
-                continue;
-
-            size = bi->mods[idx].mod->mod_end;
-            data = bootstrap_map_bm(&bi->mods[idx]);
+            size = bm->mod->mod_end;
+            data = bootstrap_map_bm(bm);
             if ( !data )
             {
                 printk(XENLOG_WARNING "Microcode: Could not map module %d, size %zu\n",
@@ -840,7 +838,7 @@ static int __init early_microcode_load(struct boot_info *bi)
             return -ENODEV;
         }
 
-        if ( !__test_and_clear_bit(idx, bi->module_map) )
+        if ( bi->mods[idx].type != BOOTMOD_UNKNOWN )
         {
             printk(XENLOG_WARNING "Microcode: Chosen module %d already used\n", idx);
             return -ENODEV;
xen/arch/x86/include/asm/bootinfo.h +30 -1
index fc74e3b224e7..37dfcc14fa7d 100644
@@ -43,10 +43,50 @@ struct boot_info {
     size_t memmap_length;
 
     unsigned int nr_modules;
-    unsigned long *module_map; /* Temporary */
     struct boot_module mods[MAX_NR_BOOTMODS + 1];
 };
 
+/*
+ * next_boot_module_index:
+ *     Finds the next boot module of type t, starting at array index start.
+ *
+ * Returns:
+ *      Success - index in boot_module array
+ *      Failure - a value greater than MAX_NR_BOOTMODS
+ */
+static inline unsigned int __init next_boot_module_index(
+    const struct boot_info *bi, enum bootmod_type t, unsigned int start)
+{
+    unsigned int i;
+
+    if ( t == BOOTMOD_XEN )
+        return bi->nr_modules;
+
+    for ( i = start; i < bi->nr_modules; i++ )
+    {
+        if ( bi->mods[i].type == t )
+            return i;
+    }
+
+    return MAX_NR_BOOTMODS + 1;
+}
+
+/*
+ * first_boot_module_index:
+ *     Finds the first boot module of type t.
+ *
+ * Returns:

xen/arch/x86/include/asm/bootinfo.h struct boot_info { · line 88

Quoted 10 lines
xen/arch/x86/include/asm/bootinfo.h +10 -0
+ *      Success - index in boot_module array
+ *      Failure - a value greater than MAX_NR_BOOTMODS
+ */
+#define first_boot_module_index(bi, t)              \
+    next_boot_module_index(bi, t, 0)
+
+#define for_each_boot_module_by_type(i, b, t)       \
+    for ( i = first_boot_module_index(b, t);        \
+          i <= (b)->nr_modules;                     \
+          i = next_boot_module_index(b, t, i + 1) )

(i) = first_...

Quoted 20 lines
xen/xsm/xsm_policy.c +2 -6
index 4c195411d05b..12c9de5a1fbf 100644
@@ -33,22 +33,18 @@
 int __init xsm_multiboot_policy_init(
     struct boot_info *bi, void **policy_buffer, size_t *policy_size)
 {
-    int i;
+    unsigned int i;
     int rc = 0;
     u32 *_policy_start;
     unsigned long _policy_len;
 
-    /*
-     * Try all modules and see whichever could be the binary policy.
-     * Adjust module_map for the module that is the binary policy.
-     */
-    for ( i = bi->nr_modules - 1; i >= 1; i-- )
+    /* Try all unknown modules and see whichever could be the binary policy. */

xen/xsm/xsm_policy.c line 47

Quoted 10 lines
xen/xsm/xsm_policy.c +4 -4
+    for_each_boot_module_by_type(i, bi, BOOTMOD_UNKNOWN)
     {
-        if ( !test_bit(i, bi->module_map) )
-            continue;
+        struct boot_module *bm = &bi->mods[i];
 
-        _policy_start = bootstrap_map(bi->mods[i].mod);
-        _policy_len   = bi->mods[i].mod->mod_end;
+        _policy_start = bootstrap_map(bm->mod);
+        _policy_len   = bm->mod->mod_end;

Minor, but you ought to switch to bootstrap_map_bm() here straight away, which reduces the churn in patch 9.

~Andrew

Reviewed-by on v1 3/12
#15 lore raw

Re: [PATCH 03/12] x86/boot: introduce boot module flags

In reply to Daniel P. Smith
Quoted 6 lines · Daniel P. Smith

The existing startup code employs various ad-hoc state tracking about certain boot module types by each area of the code. A boot module flags bitfield is added to enable tracking these different states. The first state to be transition by this commit is module relocation.

This looks safe to rebase ahead of patch 2, making it able to be taken without delay.  (With only a trivial adjustment vs bootmod_type in patch 1, it could probably go into staging without waiting for the rest of the ucode-uaf fixes.)

#3 lore raw

Re: [PATCH 01/12] x86/boot: introduce boot module types

In reply to Andrew Cooper
Quoted 10 lines · Andrew Cooper
Quoted 6 lines · Daniel P. Smith

This commit introduces module types for the types of boot modules that may be passed to Xen. These include xen, kernel, ramdisk, microcode, and xsm policy. This reduces the need for hard coded order assumptions and global variables to be used by consumers of boot modules, such as domain construction.

one extra suggestion.

Sure.

Quoted 33 lines
Quoted 18 lines

---
Changes since v7:
- merged the addition of microcode and xsm boot mod types

Changes since v5:
- added guard around initrd type assignment
- removed a missed rebase artifact

---
  xen/arch/x86/cpu/microcode/core.c   |  1 +
  xen/arch/x86/include/asm/bootinfo.h | 12 ++++++++++++
  xen/arch/x86/setup.c                |  4 ++++
  xen/xsm/xsm_policy.c                |  1 +
  4 files changed, 18 insertions(+)
xen/arch/x86/cpu/microcode/core.c +0 -0
index 1fa6cbf3d364..f46464241557 100644
(quoted diff) +4 -0
@@ -831,6 +831,10 @@ static int __init early_microcode_load(struct
 boot_info *bi)
                  continue;
              }
 
+            /*
+             * Do not alter this boot module's type.  We're most likely
+             * peeking at dom0's initrd.
+             */
              data = cd.data;
              size = cd.size;

             goto found;

Quoted 2 lines

To make it absolutely crystal clear that ...

Absolutely, as it might not be clear to a future editor of the code.

Quoted 7 lines

xen/arch/x86/cpu/microcode/core.c static int __init early_microcode_load(struct boot_info *bi) · line 848

Quoted 5 lines
xen/arch/x86/cpu/microcode/core.c +1 -0
@@ -845,6 +845,7 @@ static int __init early_microcode_load(struct boot_info *bi)
              printk(XENLOG_WARNING "Microcode: Chosen module %d already used\n", idx);
              return -ENODEV;
          }
+        bi->mods[idx].type = BOOTMOD_MICROCODE;

... this is deliberately not done on the scan path.

v/r,
dps

#6 lore raw

Re: [PATCH 02/12] x86/boot: eliminate module_map

In reply to Andrew Cooper
Quoted 16 lines · Andrew Cooper
Quoted 7 lines · Daniel P. Smith

With all boot modules now labeled by type, it is no longer necessary to track whether a boot module was identified via the module_map bitmap.

Introduce a set of helpers to search the list of boot modules based on type and the reference type, pointer or array index, desired. Then drop all uses of setting a bit in module_map and replace its use for looping with the helpers.

Quoted 7 lines

---
Changes since v7:
- collapse the three module_map patches into one,
- x86/boot: remove module_map usage from microcode loading
- x86/boot: remove module_map usage from xsm policy loading
- x86/boot: remove module_map usage by ramdisk loading

Quoted 1 line

Definitely nicer for having been collapsed together.

Most definitely.

Quoted 21 lines
Quoted 12 lines
---
  xen/arch/x86/cpu/microcode/core.c   | 12 ++++-----
  xen/arch/x86/include/asm/bootinfo.h | 42 ++++++++++++++++++++++++++++-
  xen/arch/x86/setup.c                | 28 +++++++++++--------
  xen/xsm/xsm_policy.c                | 19 +++++--------
  4 files changed, 70 insertions(+), 31 deletions(-)
xen/arch/x86/cpu/microcode/core.c +0 -0
index f46464241557..b09cf83249f6 100644

xen/arch/x86/cpu/microcode/core.c static int __init early_microcode_load(struct boot_info *bi) · line 793

Quoted 5 lines
xen/arch/x86/cpu/microcode/core.c +1 -1
@@ -790,15 +790,13 @@ static int __init early_microcode_load(struct boot_info *bi)
 
      if ( opt_scan ) /* Scan for a CPIO archive */
      {
-        for ( idx = 1; idx < bi->nr_modules; ++idx )
+        for_each_boot_module_by_type(idx, bi, BOOTMOD_UNKNOWN)

Minor, but we treat for_each_* as if they were for loops, so this either wants to be

Quoted 8 lines

for_each_boot_module_by_type ( idx, bi, BOOTMOD_UNKNOWN )

or

for_each_boot_module_by_type (idx, bi, BOOTMOD_UNKNOWN)

spacing wise.  There's no agreement between maintainers on the extra spaces inside brackets or not.

If it is considered a for loop, then I would feel obliged to add the spacing per the coding style.

Quoted 22 lines

However, despite looking at this many times, I've only just realised... This semantically changes things in a direction that we won't want.

Today, BOOTMOD_RAMDISK only happens a side effect of being "first BOOTMOD_UNKNOWN standing at the end".

But the EFI boot code ought to set bi->type=RAMDISK explicitly from the ramdisk= argument (it can probably set type=MICROCODE too), and future plans with a large HL config probably will be similar.

Anything which sets type=, and type=RAMDISK in particular, prior to early_microcode_load() excludes it from the search.  This is definitely not what we want.

It's a latent bug for now, but I'd suggest keeping the plain for loop, with

            /* Search anything unclaimed or likely to be a CPIO archive. */ if ( bm->type != BOOTMOD_UNKNOWN &&
                 bm->type != BOOTMOD_RAMDISK )
                continue;

Quoted 4 lines

as the selection criteria.  Probably also want to start from idx=0 to remove assumptions about the dom0 kernel.

Thoughts?

Yah, as much as it would be nice to use the helper, this is the exception where there is a complex match condition to be handled. This will be switched over to an explicit for loop.

Quoted 69 lines
Quoted 69 lines
xen/arch/x86/cpu/microcode/core.c +4 -6
          {
+            struct boot_module *bm = &bi->mods[idx];
              struct cpio_data cd;
 
-            if ( !test_bit(idx, bi->module_map) )
-                continue;
-
-            size = bi->mods[idx].mod->mod_end;
-            data = bootstrap_map_bm(&bi->mods[idx]);
+            size = bm->mod->mod_end;
+            data = bootstrap_map_bm(bm);
              if ( !data )
              {
                  printk(XENLOG_WARNING "Microcode: Could not map module %d, size %zu\n",
@@ -840,7 +838,7 @@ static int __init early_microcode_load(struct boot_info *bi)
              return -ENODEV;
          }
 
-        if ( !__test_and_clear_bit(idx, bi->module_map) )
+        if ( bi->mods[idx].type != BOOTMOD_UNKNOWN )
          {
              printk(XENLOG_WARNING "Microcode: Chosen module %d already used\n", idx);
              return -ENODEV;
xen/arch/x86/include/asm/bootinfo.h +34 -1
index fc74e3b224e7..37dfcc14fa7d 100644
@@ -43,10 +43,50 @@ struct boot_info {
      size_t memmap_length;
 
      unsigned int nr_modules;
-    unsigned long *module_map; /* Temporary */
      struct boot_module mods[MAX_NR_BOOTMODS + 1];
  };
 
+/*
+ * next_boot_module_index:
+ *     Finds the next boot module of type t, starting at array index start.
+ *
+ * Returns:
+ *      Success - index in boot_module array
+ *      Failure - a value greater than MAX_NR_BOOTMODS
+ */
+static inline unsigned int __init next_boot_module_index(
+    const struct boot_info *bi, enum bootmod_type t, unsigned int start)
+{
+    unsigned int i;
+
+    if ( t == BOOTMOD_XEN )
+        return bi->nr_modules;
+
+    for ( i = start; i < bi->nr_modules; i++ )
+    {
+        if ( bi->mods[i].type == t )
+            return i;
+    }
+
+    return MAX_NR_BOOTMODS + 1;
+}
+
+/*
+ * first_boot_module_index:
+ *     Finds the first boot module of type t.
+ *
+ * Returns:
+ *      Success - index in boot_module array
+ *      Failure - a value greater than MAX_NR_BOOTMODS
+ */
+#define first_boot_module_index(bi, t)              \
Quoted 8 lines

xen/arch/x86/include/asm/bootinfo.h struct boot_info { · line 88

Quoted 6 lines
xen/arch/x86/include/asm/bootinfo.h +6 -0
+    next_boot_module_index(bi, t, 0)
+
+#define for_each_boot_module_by_type(i, b, t)       \
+    for ( i = first_boot_module_index(b, t);        \
+          i <= (b)->nr_modules;                     \
+          i = next_boot_module_index(b, t, i + 1) )

(i) = first_...

Ack.

Quoted 26 lines
Quoted 26 lines
xen/xsm/xsm_policy.c +4 -8
index 4c195411d05b..12c9de5a1fbf 100644
@@ -33,22 +33,18 @@
  int __init xsm_multiboot_policy_init(
      struct boot_info *bi, void **policy_buffer, size_t *policy_size)
  {
-    int i;
+    unsigned int i;
      int rc = 0;
      u32 *_policy_start;
      unsigned long _policy_len;
 
-    /*
-     * Try all modules and see whichever could be the binary policy.
-     * Adjust module_map for the module that is the binary policy.
-     */
-    for ( i = bi->nr_modules - 1; i >= 1; i-- )
+    /* Try all unknown modules and see whichever could be the binary policy. */
+    for_each_boot_module_by_type(i, bi, BOOTMOD_UNKNOWN)
      {
-        if ( !test_bit(i, bi->module_map) )
-            continue;
+        struct boot_module *bm = &bi->mods[i];
 
Quoted 7 lines

xen/xsm/xsm_policy.c line 47

Quoted 4 lines
xen/xsm/xsm_policy.c +2 -2
-        _policy_start = bootstrap_map(bi->mods[i].mod);
-        _policy_len   = bi->mods[i].mod->mod_end;
+        _policy_start = bootstrap_map(bm->mod);
+        _policy_len   = bm->mod->mod_end;

Minor, but you ought to switch to bootstrap_map_bm() here straight away, which reduces the churn in patch 9.

Ack.

v/r,
dps

#16 lore raw

Re: [PATCH 03/12] x86/boot: introduce boot module flags

In reply to Andrew Cooper
Quoted 6 lines · Andrew Cooper
Quoted 5 lines · Daniel P. Smith

The existing startup code employs various ad-hoc state tracking about certain boot module types by each area of the code. A boot module flags bitfield is added to enable tracking these different states. The first state to be transition by this commit is module relocation.

Quoted 8 lines
Quoted 1 line

This looks safe to rebase ahead of patch 2, making it able to be taken without delay.  (With only a trivial adjustment vs bootmod_type in patch 1, it could probably go into staging without waiting for the rest of the ucode-uaf fixes.)

Thank you and I have no objections as it would reduce the size of v9.

v/r,
dps

#7 lore raw

Re: [PATCH 02/12] x86/boot: eliminate module_map

In reply to Daniel P. Smith
Quoted 25 lines · Daniel P. Smith
Quoted 24 lines · Andrew Cooper

However, despite looking at this many times, I've only just realised... This semantically changes things in a direction that we won't want.

Today, BOOTMOD_RAMDISK only happens a side effect of being "first BOOTMOD_UNKNOWN standing at the end".

But the EFI boot code ought to set bi->type=RAMDISK explicitly from the ramdisk= argument (it can probably set type=MICROCODE too), and future plans with a large HL config probably will be similar.

Anything which sets type=, and type=RAMDISK in particular, prior to early_microcode_load() excludes it from the search.  This is definitely not what we want.

It's a latent bug for now, but I'd suggest keeping the plain for loop, with

             /* Search anything unclaimed or likely to be a CPIO archive. */
             if ( bm->type != BOOTMOD_UNKNOWN && bm->type != BOOTMOD_RAMDISK ) continue;

Quoted 8 lines
Quoted 4 lines

as the selection criteria.  Probably also want to start from idx=0 to remove assumptions about the dom0 kernel.

Thoughts?

Yah, as much as it would be nice to use the helper, this is the exception where there is a complex match condition to be handled. This will be switched over to an explicit for loop.

This is simple enough, and I'm happy to fix this all up on commit.  Save it going around the loop yet again.

~Andrew

#8 lore raw

Re: [PATCH 02/12] x86/boot: eliminate module_map

Quoted 29 lines · Andrew Cooper
Quoted 28 lines · Daniel P. Smith
Quoted 27 lines · Andrew Cooper

However, despite looking at this many times, I've only just realised... This semantically changes things in a direction that we won't want.

Today, BOOTMOD_RAMDISK only happens a side effect of being "first BOOTMOD_UNKNOWN standing at the end".

But the EFI boot code ought to set bi->type=RAMDISK explicitly from the ramdisk= argument (it can probably set type=MICROCODE too), and future plans with a large HL config probably will be similar.

Anything which sets type=, and type=RAMDISK in particular, prior to early_microcode_load() excludes it from the search.  This is definitely not what we want.

It's a latent bug for now, but I'd suggest keeping the plain for loop, with

             /* Search anything unclaimed or likely to be a CPIO
archive. */
             if ( bm->type != BOOTMOD_UNKNOWN && bm->type != BOOTMOD_RAMDISK ) continue;

as the selection criteria.  Probably also want to start from idx=0 to remove assumptions about the dom0 kernel.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Thoughts?

Yah, as much as it would be nice to use the helper, this is the exception where there is a complex match condition to be handled. This will be switched over to an explicit for loop.

This is simple enough, and I'm happy to fix this all up on commit.  Save it going around the loop yet again.

No objection on my part, as I was just going to make the changes as you suggested.

v/r,
dps

#18 lore raw

Re: [PATCH 04/12] x86/boot: introduce module release

In reply to Daniel P. Smith
Quoted 12 lines · Daniel P. Smith

A precarious approach was used to release the pages used to hold a boot module. The precariousness stemmed from the fact that in the case of PV dom0, the initrd module pages may be either mapped or explicitly copied into the dom0 address space. So to handle this situation, the PV dom0 construction code will set the size of the module to zero, relying on discard_initial_images() to skip any modules with a size of zero.

A function is introduced to release a module when it is no longer needed that accepts a boolean parameter, free_mem, to indicate if the corresponding pages can be freed. To track that a module has been released, the boot module flag released is introduced.

Quoted 4 lines

The previous release model was a free all at once except those of size zeros, which would handle any unused modules passed. The new model is one of, free consumed module after usage is complete, thus unconsumed modules do not have a consumer to free them.

Slightly confusing. Maybe just "The new model is to free modules after they are consumed. Thus unconsumed modules are not freed."

Quoted 1 line

To address this, the discard_uknown_boot_modules() is

"unknown"

Quoted 64 lines

introduced and called after the last module identification occurs, initrd, to free the pages of any boot modules that are identified as not being released. After domain construction completes, all modules should be freed. A walk of the boot modules is added after domain construction to validate and notify if a module is found not to have been released.

---
Changes since v7:
- This is a new approach as an alternative to the consumed flag.

---
  xen/arch/x86/cpu/microcode/core.c   |  4 ++
  xen/arch/x86/hvm/dom0_build.c       |  7 ++--
  xen/arch/x86/include/asm/bootinfo.h |  2 +
  xen/arch/x86/include/asm/setup.h    |  3 +-
  xen/arch/x86/pv/dom0_build.c        | 20 ++--------
  xen/arch/x86/setup.c                | 57 +++++++++++++++++++++++------
  xen/xsm/xsm_core.c                  |  5 +++
  7 files changed, 67 insertions(+), 31 deletions(-)
xen/arch/x86/setup.c +24 -5
index d061ece0541f..e6d2d25fd038 100644
@@ -341,27 +341,55 @@ unsigned long __init initial_images_nrpages(nodeid_t node)
      return nr;
  }
 
-void __init discard_initial_images(void) /* a.k.a. Free boot modules */
+void __init release_boot_module(struct boot_module *bm, bool free_mem)
+{
+    uint64_t start = pfn_to_paddr(bm->mod->mod_start);
+    uint64_t size  = bm->mod->mod_end;
+
+    if ( bm->released )
+    {
+        printk(XENLOG_WARNING "Attempt second release boot module of type %d\n",
+               bm->type);
+        return;
+    }
+
+    if ( free_mem )
+        init_domheap_pages(start, start + PAGE_ALIGN(size));
+
+    bm->released = true;
+}
+
+void __init release_module(const module_t *m, bool free_mem)
  {
      struct boot_info *bi = &xen_boot_info;
      unsigned int i;
 
-    for ( i = 0; i < bi->nr_modules; ++i )
+    for ( i = 0; i < bi->nr_modules; i++ )
      {
-        uint64_t start = pfn_to_paddr(bi->mods[i].mod->mod_start);
-        uint64_t size  = bi->mods[i].mod->mod_end;
+        if ( bi->mods[i].mod == m )
+            release_boot_module(&bi->mods[i], free_mem);
+    }
+}
 
-        /*

xen/arch/x86/setup.c unsigned long __init initial_images_nrpages(nodeid_t node) · line 379

Quoted 10 lines
xen/arch/x86/setup.c +6 -4
-         * Sometimes the initrd is mapped, rather than copied, into dom0.
-         * Size being 0 is how we're instructed to leave the module alone.
-         */
-        if ( size == 0 )
+static void __init discard_unknown_boot_modules(void)
+{
+    struct boot_info *bi = &xen_boot_info;
+    unsigned int i, count = 0;
+
+    for_each_boot_module_by_type(i, bi, BOOTMOD_UNKNOWN)

for_each_boot_module_by_type ( i, bi, BOOTMOD_UNKNOWN )

To match style from 74af2d98276d ("x86/boot: eliminate module_map")

Quoted 4 lines
xen/arch/x86/setup.c +4 -0
+    {
+        struct boot_module *bm = &bi->mods[i];
+
+        if ( bm == NULL || bm->released )

xen/arch/x86/setup.c unsigned long __init initial_images_nrpages(nodeid_t node) · line 391

Quoted 10 lines
xen/arch/x86/setup.c +4 -2
              continue;
 
-        init_domheap_pages(start, start + PAGE_ALIGN(size));
+        release_boot_module(bm, true);
+        count++;
      }
 
-    bi->nr_modules = 0;
+    if ( count )
+        printk(XENLOG_DEBUG "Releasing pages for uknown boot module %d\n",

"unknown". Since the operation already happened, maybe "Released pages for %d unknown boot modules"? I'm not sure of the value of that message. It would be more informative to provide the module index and maybe a page count. The index would at least point to which module is unused.

Quoted 14 lines
xen/arch/x86/setup.c +3 -0
+               count);
  }
 
  static void __init init_idle_domain(void)
@@ -2111,6 +2139,8 @@ void asmlinkage __init noreturn __start_xen(void)
                     initrdidx);
      }
 
+    discard_unknown_boot_modules();
+
      /*
       * We're going to setup domain0 using the module(s) that we stashed safely
       * above our heap. The second module, if present, is an initrd ramdisk.

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 2159

Quoted 8 lines
xen/arch/x86/setup.c +5 -0
@@ -2122,6 +2152,11 @@ void asmlinkage __init noreturn __start_xen(void)
      if ( !dom0 )
          panic("Could not set up DOM0 guest OS\n");
 
+    /* Check and warn if any modules did not get released */
+    for ( i = 0; i < bi->nr_modules; i++ )
+        if ( !bi->mods[i].released )
+            printk(XENLOG_ERR "Boot module %d not released, memory leaked", i);
+

Why not release the memory here instead of leaking it?

I feel like the corner case of mapping the dom0 initrd is leading to this manual approach or releasing modules individually. That logic then has to be spread around. discard_initial_images() kept the logic centralized. Maybe just replace the mod_end == 0 special case with a "don't release me" flag that is checked in discard_initial_images()?

Regards,
Jason

#19 lore raw

Re: [PATCH v8 04/12] x86/boot: introduce module release

In reply to Jason Andryuk
Quoted 13 lines · Jason Andryuk
Quoted 9 lines · Daniel P. Smith
xen/arch/x86/setup.c +0 -0
@@ -2122,6 +2152,11 @@ void asmlinkage __init noreturn __start_xen(void)
       if ( !dom0 )
           panic("Could not set up DOM0 guest OS\n");

  +    /* Check and warn if any modules did not get released */ +    for ( i = 0; i < bi->nr_modules; i++ )
+        if ( !bi->mods[i].released )
+            printk(XENLOG_ERR "Boot module %d not released, memory leaked", i);
+

Why not release the memory here instead of leaking it?

Quoted 5 lines

I feel like the corner case of mapping the dom0 initrd is leading to this manual approach or releasing modules individually.  That logic then has to be spread around.  discard_initial_images() kept the logic centralized.  Maybe just replace the mod_end == 0 special case with a "don't release me" flag that is checked in discard_initial_images()?

I've been conflicted on this for ages, but I agree.

There are many wonky things with the current arrangement.

First(ish), discard_initial_images() should be named discard_boot_modules() (if it's saying a separate function).

Second, the individual dom0_build.c's should not be calling this directly.  They're both right at the end of construction, so it would make far more sense for __start_xen() to do this after create_dom0(). That also avoids needing to export the function.

And yes, as Jason says, the "initrd mapped instead of copied" is a weird corner case, and the other path manually releasing and also saying "don't free" is just pointless complexity.  I do like the idea of a "dont_free" flag.

Thoughts?

I know this would be moving back to a more v7-like approach, but I think the end approach is cleaner.

~Andrew

#24 lore raw

Re: [PATCH 05/12] x86/boot: move headroom to boot modules

In reply to Daniel P. Smith
Quoted 23 lines · Daniel P. Smith
xen/arch/x86/include/asm/bootinfo.h +15 -0
index b1549d8c8f93..e337baa905f0 100644
@@ -30,6 +30,28 @@ struct boot_module {
 
     enum bootmod_type type;
 
+    /*
+     * A boot module may need decompressing by Xen.  Headroom is an estimate of
+     * the additional space required to decompress the module.
+     *
+     * Headroom is accounted for at the start of the module.  Decompressing is
+     * done in-place with input=start, output=start-headroom, expecting the
+     * pointers to become equal (give or take some rounding) when decompression
+     * is complete.
+     *
+     * Memory layout at boot:
+     *
+     *               start ----+
+     *                         v
+     *   |<-----headroom------>|<------size------->|
+     *                         +-------------------+

xen/arch/x86/include/asm/bootinfo.h struct boot_module { · line 57

Quoted 10 lines
xen/arch/x86/include/asm/bootinfo.h +7 -0
+     *                         | Compressed Module |
+     *   +---------------------+-------------------+
+     *   |           Decompressed Module           |
+     *   +-----------------------------------------+
+     */
+    unsigned long headroom;
+
     /*
      * Module State Flags:
      *   relocated: indicates module has been relocated in memory.

By the end of the series, this ends very poorly laid out.

It works a whole lot better when putting the bitfields between type and headroom.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

#27 lore raw

Re: [PATCH 06/12] x86/boot: relocate kextra into boot info

In reply to Daniel P. Smith
Quoted 5 lines · Daniel P. Smith

Move kextra into struct boot_info, thus no longer needed to be passed as a parameter to create_dom0.

Technically "..., so it can be removed as a distinct parameter to create_dom0()".

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

#33 lore raw

Re: [PATCH 08/12] x86/boot: convert domain construction to use boot info

In reply to Daniel P. Smith
Quoted 37 lines · Daniel P. Smith

With all the components used to construct dom0 encapsulated in struct boot_info and struct boot_module, it is no longer necessary to pass all them as parameters down the domain construction call chain. Change the parameter list to pass the struct boot_info instance and the struct domain reference.

---
Changes since v5:
- renamed from "x86/boot: convert create_dom0 to use boot info"

Changes since v5:
- change headroom back to unsigned long
- make mod_idx unsigned int

---
  xen/arch/x86/dom0_build.c             |  9 ++--
  xen/arch/x86/hvm/dom0_build.c         | 49 +++++++++++++---------
  xen/arch/x86/include/asm/dom0_build.h | 13 ++----
  xen/arch/x86/include/asm/setup.h      |  7 ++--
  xen/arch/x86/pv/dom0_build.c          | 59 ++++++++++++++++-----------
  xen/arch/x86/setup.c                  | 33 ++++++++-------
  6 files changed, 95 insertions(+), 75 deletions(-)
xen/arch/x86/hvm/dom0_build.c +1 -4
index a4ac262db463..cd97f94a168a 100644
@@ -1301,16 +1302,25 @@ static void __hwdom_init pvh_setup_mmcfg(struct domain *d)
      }
  }
 
-int __init dom0_construct_pvh(struct domain *d, const module_t *image,
-                              unsigned long image_headroom,
-                              module_t *initrd,
-                              const char *cmdline)
+int __init dom0_construct_pvh(struct boot_info *bi, struct domain *d)

xen/arch/x86/hvm/dom0_build.c static void __hwdom_init pvh_setup_mmcfg(struct domain *d) · line 1315

Quoted 10 lines
xen/arch/x86/hvm/dom0_build.c +4 -0
  {
      paddr_t entry, start_info;
+    struct boot_module *image;
+    struct boot_module *initrd = NULL;
      int rc;
 
      printk(XENLOG_INFO "*** Building a PVH Dom%d ***\n", d->domain_id);
 
+    rc = first_boot_module_index(bi, BOOTMOD_KERNEL);
+    if ( unlikely(rc < 0 || rc > bi->nr_modules) )

Here and ...

xen/arch/x86/hvm/dom0_build.c static void __hwdom_init pvh_setup_mmcfg(struct domain *d) · line 1321

Quoted 6 lines
xen/arch/x86/hvm/dom0_build.c +6 -0
+        panic("Missing kernel boot module for %pd construction\n", d);
+
+    image = &bi->mods[rc];
+
+    rc = first_boot_module_index(bi, BOOTMOD_RAMDISK);
+    if ( rc > 0 || rc < bi->nr_modules )

... here. Can we just check rc < bi->nr_modules for validity? Valid modules are 0...nr_modules and not found is MAX_NR_BOOTMODS + 1. It eliminates these unecessary double checks. This would apply to 04/12 "x86/boot: introduce module release" as well.

Quoted 31 lines
xen/arch/x86/hvm/dom0_build.c +2 -0
+        initrd = &bi->mods[rc];
+
      if ( is_hardware_domain(d) )
      {
          /*
xen/arch/x86/pv/dom0_build.c +7 -4
index c1f44502a1ac..594874cd8d85 100644
@@ -374,10 +371,13 @@ static int __init dom0_construct(struct domain *d,
      unsigned int flush_flags = 0;
      start_info_t *si;
      struct vcpu *v = d->vcpu[0];
-    void *image_base = bootstrap_map(image);
-    unsigned long image_len = image->mod_end;
-    void *image_start = image_base + image_headroom;
-    unsigned long initrd_len = initrd ? initrd->mod_end : 0;
+    struct boot_module *image;
+    struct boot_module *initrd = NULL;
+    void *image_base;
+    unsigned long image_len;
+    void *image_start;
+    unsigned long initrd_len = 0;
+    const char *cmdline;
      l4_pgentry_t *l4tab = NULL, *l4start = NULL;
      l3_pgentry_t *l3tab = NULL, *l3start = NULL;
      l2_pgentry_t *l2tab = NULL, *l2start = NULL;

xen/arch/x86/pv/dom0_build.c static int __init dom0_construct(struct domain *d, · line 418

Quoted 5 lines
xen/arch/x86/pv/dom0_build.c +2 -0
@@ -414,6 +414,23 @@ static int __init dom0_construct(struct domain *d,
 
      printk(XENLOG_INFO "*** Building a PV Dom%d ***\n", d->domain_id);
 
+    i = first_boot_module_index(bi, BOOTMOD_KERNEL);
+    if ( unlikely(i < 0 || i > bi->nr_modules) )

Single check here.

Quoted 19 lines
xen/arch/x86/pv/dom0_build.c +15 -0
+        panic("Missing kernel boot module for %pd construction\n", d);
+
+    image = &bi->mods[i];
+    image_base = bootstrap_map_bm(image);
+    image_len = image->mod->mod_end;
+    image_start = image_base + image->headroom;
+    cmdline = __va(image->cmdline_pa);
+
+    i = first_boot_module_index(bi, BOOTMOD_RAMDISK);
+    if ( i > 0 || i < bi->nr_modules )
+    {
+        initrd = &bi->mods[i];
+        initrd_len = initrd->mod->mod_end;
+    }
+
      d->max_pages = ~0U;
 
      if ( (rc = bzimage_parse(image_base, &image_start, &image_len)) != 0 )

xen/arch/x86/pv/dom0_build.c static int __init dom0_construct(struct domain *d, · line 633

Quoted 5 lines
xen/arch/x86/pv/dom0_build.c +1 -1
@@ -613,7 +630,7 @@ static int __init dom0_construct(struct domain *d,
          initrd_pfn = vinitrd_start ?
                       (vinitrd_start - v_start) >> PAGE_SHIFT :
                       domain_tot_pages(d);
-        initrd_mfn = mfn = initrd->mod_start;
+        initrd_mfn = mfn = initrd->mod->mod_start;

MISRA doesn't like these assignment chains?

Quoted 4 lines
xen/arch/x86/pv/dom0_build.c +0 -0
          count = PFN_UP(initrd_len);
          if ( d->arch.physaddr_bitsize &&
               ((mfn + count - 1) >> (d->arch.physaddr_bitsize - PAGE_SHIFT)) )

xen/arch/x86/pv/dom0_build.c static int __init dom0_construct(struct domain *d, · line 651

Quoted 10 lines
xen/arch/x86/pv/dom0_build.c +3 -3
@@ -628,17 +645,17 @@ static int __init dom0_construct(struct domain *d,
                      free_domheap_pages(page, order);
                      page += 1UL << order;
                  }
-            memcpy(page_to_virt(page), mfn_to_virt(initrd->mod_start),
+            memcpy(page_to_virt(page), mfn_to_virt(initrd->mod->mod_start),
                     initrd_len);
-            release_module(initrd, true);
-            initrd->mod_start = initrd_mfn = mfn_x(page_to_mfn(page));
+            release_boot_module(initrd, true);
+            initrd->mod->mod_start = initrd_mfn = mfn_x(page_to_mfn(page));

Assignment chain here.

Quoted 29 lines
xen/arch/x86/pv/dom0_build.c +1 -1
          }
          else
          {
              while ( count-- )
                  if ( assign_pages(mfn_to_page(_mfn(mfn++)), 1, d, 0) )
                      BUG();
-            release_module(initrd, false);
+            release_boot_module(initrd, false);
          }
 
          iommu_memory_setup(d, "initrd", mfn_to_page(_mfn(initrd_mfn)),
xen/arch/x86/setup.c +1 -4
index aba9df8620ef..d9785acf89b6 100644
@@ -977,10 +977,7 @@ static unsigned int __init copy_bios_e820(struct e820entry *map, unsigned int li
      return n;
  }
 
-static struct domain *__init create_dom0(const module_t *image,
-                                         unsigned long headroom,
-                                         module_t *initrd, const char *kextra,
-                                         const char *loader)
+static struct domain *__init create_dom0(struct boot_info *bi)
  {
      static char __initdata cmdline[MAX_GUEST_CMDLINE];
 

xen/arch/x86/setup.c static struct domain *__init create_dom0(const module_t *image, · line 1001

Quoted 8 lines
xen/arch/x86/setup.c +5 -0
@@ -997,6 +994,14 @@ static struct domain *__init create_dom0(const module_t *image,
      };
      struct domain *d;
      domid_t domid;
+    struct boot_module *image;
+    unsigned int idx;
+
+    idx = first_boot_module_index(bi, BOOTMOD_KERNEL);
+    if ( unlikely(idx < 0 || idx > bi->nr_modules) )

Single check here please.

Regards,
Jason

Quoted 6 lines
xen/arch/x86/setup.c +3 -0
+        panic("Missing kernel boot module for building Dom0\n");
+
+    image = &bi->mods[idx];
 
      if ( opt_dom0_pvh )
      {
#34 lore raw

Re: [PATCH 08/12] x86/boot: convert domain construction to use boot info

Quoted 54 lines · Jason Andryuk
Quoted 42 lines · Daniel P. Smith

With all the components used to construct dom0 encapsulated in struct boot_info
and struct boot_module, it is no longer necessary to pass all them as parameters down the domain construction call chain. Change the parameter list
to pass the struct boot_info instance and the struct domain reference.

---
Changes since v5:
- renamed from "x86/boot: convert create_dom0 to use boot info"

Changes since v5:
- change headroom back to unsigned long
- make mod_idx unsigned int
---
  xen/arch/x86/dom0_build.c             |  9 ++-- xen/arch/x86/hvm/dom0_build.c         | 49 +++++++++++++--------- xen/arch/x86/include/asm/dom0_build.h | 13 ++---- xen/arch/x86/include/asm/setup.h      |  7 ++-- xen/arch/x86/pv/dom0_build.c          | 59 ++++++++++++++++----------- xen/arch/x86/setup.c                  | 33 ++++++++-------

  6 files changed, 95 insertions(+), 75 deletions(-)
xen/arch/x86/hvm/dom0_build.c +0 -0
index a4ac262db463..cd97f94a168a 100644
@@ -1301,16 +1302,25 @@ static void __hwdom_init
 pvh_setup_mmcfg(struct domain *d)
       }
   }

  -int __init dom0_construct_pvh(struct domain *d, const module_t *image,
-                              unsigned long image_headroom, -                              module_t *initrd,
-                              const char *cmdline)
+int __init dom0_construct_pvh(struct boot_info *bi, struct domain *d) {

Quoted 8 lines

      paddr_t entry, start_info;
+    struct boot_module *image;
+    struct boot_module *initrd = NULL;
      int rc;
        printk(XENLOG_INFO "*** Building a PVH Dom%d ***\n", d->domain_id);
  +    rc = first_boot_module_index(bi, BOOTMOD_KERNEL); +    if ( unlikely(rc < 0 || rc > bi->nr_modules) )

Here and ...

Quoted 8 lines

xen/arch/x86/hvm/dom0_build.c static void __hwdom_init pvh_setup_mmcfg(struct domain *d) · line 1321

Quoted 6 lines

+        panic("Missing kernel boot module for %pd construction\n", d);

xen/arch/x86/hvm/dom0_build.c +5 -0
+
+    image = &bi->mods[rc];
+
+    rc = first_boot_module_index(bi, BOOTMOD_RAMDISK);
+    if ( rc > 0 || rc < bi->nr_modules )

... here.  Can we just check rc < bi->nr_modules for validity?

You could, but eventually MISRA will say no and I suspect it will then be made your problem to fix.

In this case, we ought to have an unsigned int idx and not (re)use rc.

Also, we panic far earlier in __start_xen() if there's no dom0 kernel, so I think we can just assert that rather than having a logically dead panic() path.

Quoted 33 lines

  Valid modules are 0...nr_modules and not found is MAX_NR_BOOTMODS + 1.  It eliminates these unecessary double checks.  This would apply to 04/12 "x86/boot: introduce module release" as well.

Quoted 29 lines

+        initrd = &bi->mods[rc];
+
      if ( is_hardware_domain(d) )
      {
          /*

xen/arch/x86/pv/dom0_build.c +7 -4
index c1f44502a1ac..594874cd8d85 100644
@@ -374,10 +371,13 @@ static int __init dom0_construct(struct domain *d,
       unsigned int flush_flags = 0;
       start_info_t *si;
       struct vcpu *v = d->vcpu[0];
-    void *image_base = bootstrap_map(image);
-    unsigned long image_len = image->mod_end;
-    void *image_start = image_base + image_headroom;
-    unsigned long initrd_len = initrd ? initrd->mod_end : 0;
+    struct boot_module *image;
+    struct boot_module *initrd = NULL;
+    void *image_base;
+    unsigned long image_len;
+    void *image_start;
+    unsigned long initrd_len = 0;
+    const char *cmdline;
       l4_pgentry_t *l4tab = NULL, *l4start = NULL;
       l3_pgentry_t *l3tab = NULL, *l3start = NULL;
Quoted 8 lines

xen/arch/x86/pv/dom0_build.c static int __init dom0_construct(struct domain *d, · line 417

Quoted 6 lines
xen/arch/x86/pv/dom0_build.c +1 -0
       l2_pgentry_t *l2tab = NULL, *l2start = NULL;
@@ -414,6 +414,23 @@ static int __init dom0_construct(struct domain *d,
         printk(XENLOG_INFO "*** Building a PV Dom%d ***\n",
 d->domain_id);
   +    i = first_boot_module_index(bi, BOOTMOD_KERNEL);
+    if ( unlikely(i < 0 || i > bi->nr_modules) )

Single check here.

Similar argument.  Except it turns out that i is used for precisely two loops in dom0_construct() both of which are from 0 to either 4 or 32.

So it very much can be converted to being an unsigned variable, and then this works nicely.

That said, drop the unlikely().   This is an init function run once, and all it is doing is reducing legibility.

Quoted 21 lines
Quoted 21 lines
xen/arch/x86/setup.c +0 -0
index aba9df8620ef..d9785acf89b6 100644
@@ -977,10 +977,7 @@ static unsigned int __init copy_bios_e820(struct

e820entry *map, unsigned int li
      return n;
  }
  -static struct domain *__init create_dom0(const module_t *image, -                                         unsigned long headroom, -                                         module_t *initrd, const char *kextra,
-                                         const char *loader) +static struct domain *__init create_dom0(struct boot_info *bi) {
      static char __initdata cmdline[MAX_GUEST_CMDLINE]; @@ -997,6 +994,14 @@ static struct domain *__init create_dom0(const module_t *image,
      };
      struct domain *d;

Quoted 8 lines
Quoted 6 lines

      domid_t domid;
+    struct boot_module *image;
+    unsigned int idx;
+
+    idx = first_boot_module_index(bi, BOOTMOD_KERNEL); +    if ( unlikely(idx < 0 || idx > bi->nr_modules) )

Single check here please.

I'm surprised that the compiler didn't complain about "idx < 0" being tautological here.

~Andrew

#37 lore raw

Re: [PATCH 08/12] x86/boot: convert domain construction to use boot info

In reply to Daniel P. Smith
Quoted 10 lines · Daniel P. Smith
xen/arch/x86/hvm/dom0_build.c +1 -4
@@ -1301,16 +1302,25 @@ static void __hwdom_init pvh_setup_mmcfg(struct domain *d)
     }
 }
 
-int __init dom0_construct_pvh(struct domain *d, const module_t *image,
-                              unsigned long image_headroom,
-                              module_t *initrd,
-                              const char *cmdline)
+int __init dom0_construct_pvh(struct boot_info *bi, struct domain *d)
 {

xen/arch/x86/hvm/dom0_build.c static void __hwdom_init pvh_setup_mmcfg(struct domain *d) · line 1316

Quoted 10 lines
xen/arch/x86/hvm/dom0_build.c +5 -0
     paddr_t entry, start_info;
+    struct boot_module *image;
+    struct boot_module *initrd = NULL;
     int rc;
 
     printk(XENLOG_INFO "*** Building a PVH Dom%d ***\n", d->domain_id);
 
+    rc = first_boot_module_index(bi, BOOTMOD_KERNEL);
+    if ( unlikely(rc < 0 || rc > bi->nr_modules) )
+        panic("Missing kernel boot module for %pd construction\n", d);

Just noticed while experimenting.  The upper bound (for the error case) should be >= because nothing good can come of being handed the Xen module.

xen/arch/x86/hvm/dom0_build.c static void __hwdom_init pvh_setup_mmcfg(struct domain *d) · line 1322

Quoted 6 lines
xen/arch/x86/hvm/dom0_build.c +6 -0
+
+    image = &bi->mods[rc];
+
+    rc = first_boot_module_index(bi, BOOTMOD_RAMDISK);
+    if ( rc > 0 || rc < bi->nr_modules )
+        initrd = &bi->mods[rc];

This range check is tautologically true.  You want && instead of ||.

Same elsewhere, although this goes away when the variable is unsigned (and we remove assumptions about the dom0 kernel's index).

~Andrew

#21 lore raw

Re: [PATCH 04/12] x86/boot: introduce module release

In reply to Jason Andryuk
Quoted 13 lines · Jason Andryuk
Quoted 12 lines · Daniel P. Smith

A precarious approach was used to release the pages used to hold a boot module. The precariousness stemmed from the fact that in the case of PV dom0, the initrd module pages may be either mapped or explicitly copied into the dom0 address space. So to handle this situation, the PV dom0 construction code will set the size of the module to zero, relying on discard_initial_images() to skip any modules with a size of zero.

A function is introduced to release a module when it is no longer needed that accepts a boolean parameter, free_mem, to indicate if the corresponding pages can be freed. To track that a module has been released, the boot module flag released is introduced.

Quoted 6 lines
Quoted 4 lines

The previous release model was a free all at once except those of size zeros, which would handle any unused modules passed. The new model is one of, free consumed module after usage is complete, thus unconsumed modules do not have a consumer to free them.

Slightly confusing.  Maybe just "The new model is to free modules after they are consumed.  Thus unconsumed modules are not freed."

okay.

Quoted 3 lines
Quoted 1 line

To address this, the discard_uknown_boot_modules() is

"unknown"

Ack

Quoted 78 lines
Quoted 69 lines

introduced and called after the last module identification occurs, initrd, to free the pages of any boot modules that are identified as not being released. After domain construction completes, all modules should be freed. A walk of the boot modules is added after domain construction to validate and notify if a module is found not to have been released.

---
Changes since v7:
- This is a new approach as an alternative to the consumed flag.
---
  xen/arch/x86/cpu/microcode/core.c   |  4 ++ xen/arch/x86/hvm/dom0_build.c       |  7 ++-- xen/arch/x86/include/asm/bootinfo.h |  2 + xen/arch/x86/include/asm/setup.h    |  3 +- xen/arch/x86/pv/dom0_build.c        | 20 ++-------- xen/arch/x86/setup.c                | 57 +++++++++++++++++++++++------ xen/xsm/xsm_core.c                  |  5 +++

  7 files changed, 67 insertions(+), 31 deletions(-)
xen/arch/x86/setup.c +24 -6
index d061ece0541f..e6d2d25fd038 100644
@@ -341,27 +341,55 @@ unsigned long __init 
 initial_images_nrpages(nodeid_t node)
       return nr;
   }
-void __init discard_initial_images(void) /* a.k.a. Free boot modules */
+void __init release_boot_module(struct boot_module *bm, bool free_mem)
+{
+    uint64_t start = pfn_to_paddr(bm->mod->mod_start);
+    uint64_t size  = bm->mod->mod_end;
+
+    if ( bm->released )
+    {
+        printk(XENLOG_WARNING "Attempt second release boot module of 
 type %d\n",
+               bm->type);
+        return;
+    }
+
+    if ( free_mem )
+        init_domheap_pages(start, start + PAGE_ALIGN(size));
+
+    bm->released = true;
+}
+
+void __init release_module(const module_t *m, bool free_mem)
   {
       struct boot_info *bi = &xen_boot_info;
       unsigned int i;
-    for ( i = 0; i < bi->nr_modules; ++i )
+    for ( i = 0; i < bi->nr_modules; i++ )
       {
-        uint64_t start = pfn_to_paddr(bi->mods[i].mod->mod_start);
-        uint64_t size  = bi->mods[i].mod->mod_end;
+        if ( bi->mods[i].mod == m )
+            release_boot_module(&bi->mods[i], free_mem);
+    }
+}
-        /*
-         * Sometimes the initrd is mapped, rather than copied, into 

dom0.
-         * Size being 0 is how we're instructed to leave the module alone.
-         */
-        if ( size == 0 )

xen/arch/x86/setup.c unsigned long __init initial_images_nrpages(nodeid_t node) · line 379

Quoted 6 lines
xen/arch/x86/setup.c +6 -0
+static void __init discard_unknown_boot_modules(void)
+{
+    struct boot_info *bi = &xen_boot_info;
+    unsigned int i, count = 0;
+
+    for_each_boot_module_by_type(i, bi, BOOTMOD_UNKNOWN)

for_each_boot_module_by_type ( i, bi, BOOTMOD_UNKNOWN )

Quoted 1 line

To match style from 74af2d98276d ("x86/boot: eliminate module_map")

Ack.

Quoted 14 lines
Quoted 5 lines

+    {
+        struct boot_module *bm = &bi->mods[i];
+
+        if ( bm == NULL || bm->released )
              continue;

Quoted 8 lines

-        init_domheap_pages(start, start + PAGE_ALIGN(size)); +        release_boot_module(bm, true);
+        count++;
      }
-    bi->nr_modules = 0;
+    if ( count )
+        printk(XENLOG_DEBUG "Releasing pages for uknown boot module %d\n",

Quoted 1 line

"unknown".  Since the operation already happened, maybe "Released pages for %d unknown boot modules"?  I'm not sure of the value of that message.  It would be more informative to provide the module index and maybe a page count.  The index would at least point to which module is unused.

Ack to unknown.

Can adjust the phrasing, the question is there a desire to have a message for every boot module freed. Guess I could do a single log line split across multiple printks, Thinking about the case where someone tried to abuse the interface by loading a bunch of unused modules.

Quoted 14 lines
Quoted 14 lines

+               count);
  }
  static void __init init_idle_domain(void)

xen/arch/x86/setup.c +2 -0
@@ -2111,6 +2139,8 @@ void asmlinkage __init noreturn __start_xen(void)
                      initrdidx);
       }
+    discard_unknown_boot_modules();
+
       /*
        * We're going to setup domain0 using the module(s) that we stashed safely
        * above our heap. The second module, if present, is an initrd ramdisk.
@@ -2122,6 +2152,11 @@ void asmlinkage __init noreturn __start_xen(void)
       if ( !dom0 )
           panic("Could not set up DOM0 guest OS\n");
Quoted 8 lines

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 2159

Quoted 6 lines
xen/arch/x86/setup.c +5 -0
+    /* Check and warn if any modules did not get released */
+    for ( i = 0; i < bi->nr_modules; i++ )
+        if ( !bi->mods[i].released )
+            printk(XENLOG_ERR "Boot module %d not released, memory 
 leaked", i);
+

Why not release the memory here instead of leaking it?

Because you don't know if it was mapped or consumed.

Quoted 1 line

I feel like the corner case of mapping the dom0 initrd is leading to this manual approach or releasing modules individually.  That logic then has to be spread around.  discard_initial_images() kept the logic centralized.  Maybe just replace the mod_end == 0 special case with a "don't release me" flag that is checked in discard_initial_images()?

That is what started me at the options to deal with it. The two I came up with was a flag and this approach. Weighing the pros/cons of the two, the deciding factor is when multi-domain construction is introduced. With multi-domain with a large number of domains, a balance has to be struck between holding all the kernels and ramdisks in memory plus being able to allocate the desired amount of memory for each domain. Perhaps a balance can be struck, with a discard_consumed_boot_modules() that walks the boot module list, and discard the ones consumed. While only dom0 can be constructed, it would be called once after create_dom0 call returns (per Andy's suggestion in response to this comment). An expansion on this could be that instead of using a free flag, use a ref count that is incremented as it is claimed and the decremented when it has been consumed.

v/r,
dps

#20 lore raw

Re: [PATCH v8 04/12] x86/boot: introduce module release

In reply to Andrew Cooper
Quoted 32 lines · Andrew Cooper
Quoted 13 lines · Jason Andryuk

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 2159

Quoted 9 lines · Daniel P. Smith
xen/arch/x86/setup.c +4 -0
@@ -2122,6 +2152,11 @@ void asmlinkage __init noreturn __start_xen(void)
       if ( !dom0 )
           panic("Could not set up DOM0 guest OS\n");
   +    /* Check and warn if any modules did not get released */
+    for ( i = 0; i < bi->nr_modules; i++ )
+        if ( !bi->mods[i].released )
+            printk(XENLOG_ERR "Boot module %d not released, memory
 leaked", i);
+

Why not release the memory here instead of leaking it?

Quoted 5 lines

I feel like the corner case of mapping the dom0 initrd is leading to this manual approach or releasing modules individually.  That logic then has to be spread around.  discard_initial_images() kept the logic centralized.  Maybe just replace the mod_end == 0 special case with a "don't release me" flag that is checked in discard_initial_images()?

I've been conflicted on this for ages, but I agree.

There are many wonky things with the current arrangement.

First(ish), discard_initial_images() should be named discard_boot_modules() (if it's saying a separate function).

Second, the individual dom0_build.c's should not be calling this directly.  They're both right at the end of construction, so it would make far more sense for __start_xen() to do this after create_dom0(). That also avoids needing to export the function.

Quoted 6 lines

And yes, as Jason says, the "initrd mapped instead of copied" is a weird corner case, and the other path manually releasing and also saying "don't free" is just pointless complexity.  I do like the idea of a "dont_free" flag.

Thoughts?

How about the ref count I suggested in response to Jason?

Quoted 2 lines

I know this would be moving back to a more v7-like approach, but I think the end approach is cleaner.

No worries, the series is looking better each time.

v/r,
dps

#25 lore raw

Re: [PATCH 05/12] x86/boot: move headroom to boot modules

In reply to Andrew Cooper
Quoted 37 lines · Andrew Cooper
Quoted 23 lines · Daniel P. Smith
xen/arch/x86/include/asm/bootinfo.h +15 -0
index b1549d8c8f93..e337baa905f0 100644
@@ -30,6 +30,28 @@ struct boot_module {
 
      enum bootmod_type type;
 
+    /*
+     * A boot module may need decompressing by Xen.  Headroom is an estimate of
+     * the additional space required to decompress the module.
+     *
+     * Headroom is accounted for at the start of the module.  Decompressing is
+     * done in-place with input=start, output=start-headroom, expecting the
+     * pointers to become equal (give or take some rounding) when decompression
+     * is complete.
+     *
+     * Memory layout at boot:
+     *
+     *               start ----+
+     *                         v
+     *   |<-----headroom------>|<------size------->|
+     *                         +-------------------+

xen/arch/x86/include/asm/bootinfo.h struct boot_module { · line 57

Quoted 10 lines
xen/arch/x86/include/asm/bootinfo.h +7 -0
+     *                         | Compressed Module |
+     *   +---------------------+-------------------+
+     *   |           Decompressed Module           |
+     *   +-----------------------------------------+
+     */
+    unsigned long headroom;
+
      /*
       * Module State Flags:
       *   relocated: indicates module has been relocated in memory.

By the end of the series, this ends very poorly laid out.

Quoted 2 lines

It works a whole lot better when putting the bitfields between type and headroom.

Ack.

Quoted 1 line

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Thanks.

v/r,
dps

#28 lore raw

Re: [PATCH 06/12] x86/boot: relocate kextra into boot info

In reply to Andrew Cooper
Quoted 9 lines · Andrew Cooper
Quoted 5 lines · Daniel P. Smith

Move kextra into struct boot_info, thus no longer needed to be passed as a parameter to create_dom0.

Technically "..., so it can be removed as a distinct parameter to create_dom0()".

Ack.

Quoted 1 line

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Thank you.

v/r,
dps

#36 lore raw

Re: [PATCH 08/12] x86/boot: convert domain construction to use boot info

In reply to Jason Andryuk
Quoted 49 lines · Jason Andryuk
Quoted 35 lines · Daniel P. Smith

With all the components used to construct dom0 encapsulated in struct boot_info and struct boot_module, it is no longer necessary to pass all them as parameters down the domain construction call chain. Change the parameter list to pass the struct boot_info instance and the struct domain reference.

---
Changes since v5:
- renamed from "x86/boot: convert create_dom0 to use boot info"

Changes since v5:
- change headroom back to unsigned long
- make mod_idx unsigned int
---
  xen/arch/x86/dom0_build.c             |  9 ++-- xen/arch/x86/hvm/dom0_build.c         | 49 +++++++++++++--------- xen/arch/x86/include/asm/dom0_build.h | 13 ++---- xen/arch/x86/include/asm/setup.h      |  7 ++-- xen/arch/x86/pv/dom0_build.c          | 59 ++++++++++++++++----------- xen/arch/x86/setup.c                  | 33 ++++++++-------

  6 files changed, 95 insertions(+), 75 deletions(-)
xen/arch/x86/hvm/dom0_build.c +0 -3
index a4ac262db463..cd97f94a168a 100644
@@ -1301,16 +1302,25 @@ static void __hwdom_init 
 pvh_setup_mmcfg(struct domain *d)
       }
   }
-int __init dom0_construct_pvh(struct domain *d, const module_t *image,
-                              unsigned long image_headroom,
-                              module_t *initrd,

xen/arch/x86/hvm/dom0_build.c static void __hwdom_init · line 1313

Quoted 10 lines
xen/arch/x86/hvm/dom0_build.c +5 -1
-                              const char *cmdline)
+int __init dom0_construct_pvh(struct boot_info *bi, struct domain *d)
   {
       paddr_t entry, start_info;
+    struct boot_module *image;
+    struct boot_module *initrd = NULL;
       int rc;
       printk(XENLOG_INFO "*** Building a PVH Dom%d ***\n", d->domain_id);
+    rc = first_boot_module_index(bi, BOOTMOD_KERNEL);
+    if ( unlikely(rc < 0 || rc > bi->nr_modules) )

Here and ...

Quoted 8 lines

xen/arch/x86/hvm/dom0_build.c static void __hwdom_init pvh_setup_mmcfg(struct domain *d) · line 1321

Quoted 6 lines
xen/arch/x86/hvm/dom0_build.c +6 -0
+        panic("Missing kernel boot module for %pd construction\n", d);
+
+    image = &bi->mods[rc];
+
+    rc = first_boot_module_index(bi, BOOTMOD_RAMDISK);
+    if ( rc > 0 || rc < bi->nr_modules )

... here.  Can we just check rc < bi->nr_modules for validity?  Valid modules are 0...nr_modules and not found is MAX_NR_BOOTMODS + 1.  It eliminates these unecessary double checks.  This would apply to 04/12 "x86/boot: introduce module release" as well.

Please see my response to Andy's response.

Quoted 8 lines
Quoted 6 lines
xen/arch/x86/hvm/dom0_build.c +0 -0
@@ -613,7 +630,7 @@ static int __init dom0_construct(struct domain *d,

          initrd_pfn = vinitrd_start ?
                       (vinitrd_start - v_start) >> PAGE_SHIFT :
                       domain_tot_pages(d);
-        initrd_mfn = mfn = initrd->mod_start;
+        initrd_mfn = mfn = initrd->mod->mod_start;

MISRA doesn't like these assignment chains?

Ugh, correct. Regression, not sure why, from previous review.

Quoted 10 lines
Quoted 10 lines

          count = PFN_UP(initrd_len);
          if ( d->arch.physaddr_bitsize &&
               ((mfn + count - 1) >> (d->arch.physaddr_bitsize - PAGE_SHIFT)) )

xen/arch/x86/hvm/dom0_build.c +1 -1
@@ -628,17 +645,17 @@ static int __init dom0_construct(struct domain *d,
                       free_domheap_pages(page, order);
                       page += 1UL << order;
                   }
-            memcpy(page_to_virt(page), mfn_to_virt(initrd->mod_start),
+            memcpy(page_to_virt(page), mfn_to_virt(initrd->mod- 
 >mod_start),
Quoted 8 lines

xen/arch/x86/hvm/dom0_build.c static int __init dom0_construct(struct domain *d, · line 653

Quoted 6 lines
xen/arch/x86/hvm/dom0_build.c +2 -2
                      initrd_len);
-            release_module(initrd, true);
-            initrd->mod_start = initrd_mfn = mfn_x(page_to_mfn(page));
+            release_boot_module(initrd, true);
+            initrd->mod->mod_start = initrd_mfn = 
 mfn_x(page_to_mfn(page));

Assignment chain here.

Ack.

v/r,
dps

#35 lore raw

Re: [PATCH 08/12] x86/boot: convert domain construction to use boot info

In reply to Andrew Cooper
Quoted 64 lines · Andrew Cooper
Quoted 54 lines · Jason Andryuk
Quoted 40 lines · Daniel P. Smith

With all the components used to construct dom0 encapsulated in struct boot_info
and struct boot_module, it is no longer necessary to pass all them as parameters down the domain construction call chain. Change the parameter list
to pass the struct boot_info instance and the struct domain reference.

---
Changes since v5:
- renamed from "x86/boot: convert create_dom0 to use boot info"

Changes since v5:
- change headroom back to unsigned long
- make mod_idx unsigned int

---
   xen/arch/x86/dom0_build.c             |  9 ++--
   xen/arch/x86/hvm/dom0_build.c         | 49 +++++++++++++---------
   xen/arch/x86/include/asm/dom0_build.h | 13 ++----
   xen/arch/x86/include/asm/setup.h      |  7 ++--
   xen/arch/x86/pv/dom0_build.c          | 59 ++++++++++++++++-----------
   xen/arch/x86/setup.c                  | 33 ++++++++-------
   6 files changed, 95 insertions(+), 75 deletions(-)
xen/arch/x86/hvm/dom0_build.c +0 -3
index a4ac262db463..cd97f94a168a 100644
@@ -1301,16 +1302,25 @@ static void __hwdom_init
 pvh_setup_mmcfg(struct domain *d)
       }
   }
   -int __init dom0_construct_pvh(struct domain *d, const module_t
 *image,
-                              unsigned long image_headroom,
-                              module_t *initrd,
-                              const char *cmdline)

xen/arch/x86/hvm/dom0_build.c static void __hwdom_init · line 1316

Quoted 10 lines
xen/arch/x86/hvm/dom0_build.c +4 -0
+int __init dom0_construct_pvh(struct boot_info *bi, struct domain *d)
   {
       paddr_t entry, start_info;
+    struct boot_module *image;
+    struct boot_module *initrd = NULL;
       int rc;
         printk(XENLOG_INFO "*** Building a PVH Dom%d ***\n",
 d->domain_id);
   +    rc = first_boot_module_index(bi, BOOTMOD_KERNEL);
+    if ( unlikely(rc < 0 || rc > bi->nr_modules) )

Here and ...

Quoted 8 lines

xen/arch/x86/hvm/dom0_build.c static void __hwdom_init pvh_setup_mmcfg(struct domain *d) · line 1321

Quoted 6 lines
xen/arch/x86/hvm/dom0_build.c +6 -0
+        panic("Missing kernel boot module for %pd construction\n", d);
+
+    image = &bi->mods[rc];
+
+    rc = first_boot_module_index(bi, BOOTMOD_RAMDISK);
+    if ( rc > 0 || rc < bi->nr_modules )

... here.  Can we just check rc < bi->nr_modules for validity?

Quoted 4 lines

You could, but eventually MISRA will say no and I suspect it will then be made your problem to fix.

In this case, we ought to have an unsigned int idx and not (re)use rc.

I can move to using an unsigned and ...

Quoted 3 lines

Also, we panic far earlier in __start_xen() if there's no dom0 kernel, so I think we can just assert that rather than having a logically dead panic() path.

convert it to an ASSERT().

Quoted 42 lines
Quoted 33 lines

  Valid modules are 0...nr_modules and not found is MAX_NR_BOOTMODS +
1.  It eliminates these unecessary double checks.  This would apply to 04/12 "x86/boot: introduce module release" as well.

Quoted 29 lines
xen/arch/x86/hvm/dom0_build.c +2 -0
+        initrd = &bi->mods[rc];
+
       if ( is_hardware_domain(d) )
       {

          /*

xen/arch/x86/pv/dom0_build.c +7 -4
index c1f44502a1ac..594874cd8d85 100644
@@ -374,10 +371,13 @@ static int __init dom0_construct(struct domain *d,
       unsigned int flush_flags = 0;
       start_info_t *si;
       struct vcpu *v = d->vcpu[0];
-    void *image_base = bootstrap_map(image);
-    unsigned long image_len = image->mod_end;
-    void *image_start = image_base + image_headroom;
-    unsigned long initrd_len = initrd ? initrd->mod_end : 0;
+    struct boot_module *image;
+    struct boot_module *initrd = NULL;
+    void *image_base;
+    unsigned long image_len;
+    void *image_start;
+    unsigned long initrd_len = 0;
+    const char *cmdline;
       l4_pgentry_t *l4tab = NULL, *l4start = NULL;
       l3_pgentry_t *l3tab = NULL, *l3start = NULL;
Quoted 8 lines

xen/arch/x86/pv/dom0_build.c static int __init dom0_construct(struct domain *d, · line 417

Quoted 6 lines
xen/arch/x86/pv/dom0_build.c +1 -0
       l2_pgentry_t *l2tab = NULL, *l2start = NULL;
@@ -414,6 +414,23 @@ static int __init dom0_construct(struct domain *d,
         printk(XENLOG_INFO "*** Building a PV Dom%d ***\n",
 d->domain_id);
   +    i = first_boot_module_index(bi, BOOTMOD_KERNEL);
+    if ( unlikely(i < 0 || i > bi->nr_modules) )

Single check here.

Quoted 5 lines

Similar argument.  Except it turns out that i is used for precisely two loops in dom0_construct() both of which are from 0 to either 4 or 32.

So it very much can be converted to being an unsigned variable, and then this works nicely.

Ack.

Quoted 2 lines

That said, drop the unlikely().   This is an init function run once, and all it is doing is reducing legibility.

Ack.

Quoted 30 lines
Quoted 21 lines
Quoted 21 lines
xen/arch/x86/setup.c +0 -0
index aba9df8620ef..d9785acf89b6 100644
@@ -977,10 +977,7 @@ static unsigned int __init copy_bios_e820(struct

e820entry *map, unsigned int li
      return n;
  }
  -static struct domain *__init create_dom0(const module_t *image,
-                                         unsigned long headroom, -                                         module_t *initrd, const char *kextra,
-                                         const char *loader) +static struct domain *__init create_dom0(struct boot_info *bi)
  {
      static char __initdata cmdline[MAX_GUEST_CMDLINE]; @@ -997,6 +994,14 @@ static struct domain *__init create_dom0(const
module_t *image,
      };
      struct domain *d;

Quoted 8 lines
Quoted 6 lines

      domid_t domid;
+    struct boot_module *image;
+    unsigned int idx;
+
+    idx = first_boot_module_index(bi, BOOTMOD_KERNEL); +    if ( unlikely(idx < 0 || idx > bi->nr_modules) )

Single check here please.

Quoted 2 lines

I'm surprised that the compiler didn't complain about "idx < 0" being tautological here.

As above, unsigned and ASSERT.

v/r,
dps

#38 lore raw

Re: [PATCH 08/12] x86/boot: convert domain construction to use boot info

In reply to Andrew Cooper
Quoted 16 lines · Andrew Cooper
Quoted 15 lines · Daniel P. Smith
xen/arch/x86/hvm/dom0_build.c +3 -4
@@ -1301,16 +1302,25 @@ static void __hwdom_init pvh_setup_mmcfg(struct domain *d)
      }
  }
 
-int __init dom0_construct_pvh(struct domain *d, const module_t *image,
-                              unsigned long image_headroom,
-                              module_t *initrd,
-                              const char *cmdline)
+int __init dom0_construct_pvh(struct boot_info *bi, struct domain *d)
  {
      paddr_t entry, start_info;
+    struct boot_module *image;
+    struct boot_module *initrd = NULL;
      int rc;
 
Quoted 8 lines

xen/arch/x86/hvm/dom0_build.c static void __hwdom_init pvh_setup_mmcfg(struct domain *d) · line 1316

Quoted 5 lines
xen/arch/x86/hvm/dom0_build.c +3 -0
      printk(XENLOG_INFO "*** Building a PVH Dom%d ***\n", d->domain_id);
 
+    rc = first_boot_module_index(bi, BOOTMOD_KERNEL);
+    if ( unlikely(rc < 0 || rc > bi->nr_modules) )
+        panic("Missing kernel boot module for %pd construction\n", d);

Just noticed while experimenting.  The upper bound (for the error case) should be >= because nothing good can come of being handed the Xen module.

Ack.

Quoted 11 lines

xen/arch/x86/hvm/dom0_build.c static void __hwdom_init pvh_setup_mmcfg(struct domain *d) · line 1322

Quoted 6 lines
xen/arch/x86/hvm/dom0_build.c +6 -0
+
+    image = &bi->mods[rc];
+
+    rc = first_boot_module_index(bi, BOOTMOD_RAMDISK);
+    if ( rc > 0 || rc < bi->nr_modules )
+        initrd = &bi->mods[rc];

This range check is tautologically true.  You want && instead of ||.

Same elsewhere, although this goes away when the variable is unsigned (and we remove assumptions about the dom0 kernel's index).

Ack.

v/r,
dps

#9 lore raw

Re: [PATCH 02/12] x86/boot: eliminate module_map

In reply to Andrew Cooper
Quoted 39 lines · Andrew Cooper
Quoted 7 lines · Daniel P. Smith

With all boot modules now labeled by type, it is no longer necessary to track whether a boot module was identified via the module_map bitmap.

Introduce a set of helpers to search the list of boot modules based on type and the reference type, pointer or array index, desired. Then drop all uses of setting a bit in module_map and replace its use for looping with the helpers.

Quoted 7 lines

---
Changes since v7:
- collapse the three module_map patches into one,
- x86/boot: remove module_map usage from microcode loading
- x86/boot: remove module_map usage from xsm policy loading
- x86/boot: remove module_map usage by ramdisk loading

Definitely nicer for having been collapsed together.

Quoted 12 lines
---
 xen/arch/x86/cpu/microcode/core.c   | 12 ++++-----
 xen/arch/x86/include/asm/bootinfo.h | 42 ++++++++++++++++++++++++++++-
 xen/arch/x86/setup.c                | 28 +++++++++++--------
 xen/xsm/xsm_policy.c                | 19 +++++--------
 4 files changed, 70 insertions(+), 31 deletions(-)
xen/arch/x86/cpu/microcode/core.c +0 -0
index f46464241557..b09cf83249f6 100644

xen/arch/x86/cpu/microcode/core.c static int __init early_microcode_load(struct boot_info *bi) · line 793

Quoted 5 lines
xen/arch/x86/cpu/microcode/core.c +1 -1
@@ -790,15 +790,13 @@ static int __init early_microcode_load(struct boot_info *bi)
 
     if ( opt_scan ) /* Scan for a CPIO archive */
     {
-        for ( idx = 1; idx < bi->nr_modules; ++idx )
+        for_each_boot_module_by_type(idx, bi, BOOTMOD_UNKNOWN)

Minor, but we treat for_each_* as if they were for loops, so this either wants to be

Quoted 8 lines

for_each_boot_module_by_type ( idx, bi, BOOTMOD_UNKNOWN )

or

for_each_boot_module_by_type (idx, bi, BOOTMOD_UNKNOWN)

spacing wise.  There's no agreement between maintainers on the extra spaces inside brackets or not.

Just to clarify - no, the latter form you suggest is not okay to use. Daniel's form is, as is the first one you suggest. The choice is between "like a for() loop" (your 1st form) and "just another macro invocation" (Daniel's form).

Jan

#10 lore raw

Re: [PATCH 02/12] x86/boot: eliminate module_map

In reply to Daniel P. Smith

xen/arch/x86/cpu/microcode/core.c static int __init early_microcode_load(struct boot_info *bi) · line 795

Quoted 10 lines · Daniel P. Smith
xen/arch/x86/cpu/microcode/core.c +2 -1
@@ -790,15 +790,13 @@ static int __init early_microcode_load(struct boot_info *bi)
 
     if ( opt_scan ) /* Scan for a CPIO archive */
     {
-        for ( idx = 1; idx < bi->nr_modules; ++idx )
+        for_each_boot_module_by_type(idx, bi, BOOTMOD_UNKNOWN)
         {
+            struct boot_module *bm = &bi->mods[idx];

pointer-to-const? You really want to get used to applying const to pointed-to types whenever possible. IOW ...

Quoted 13 lines
xen/xsm/xsm_policy.c +1 -2
@@ -33,22 +33,18 @@
 int __init xsm_multiboot_policy_init(
     struct boot_info *bi, void **policy_buffer, size_t *policy_size)
 {
-    int i;
+    unsigned int i;
     int rc = 0;
     u32 *_policy_start;
     unsigned long _policy_len;
 
-    /*

xen/xsm/xsm_policy.c line 44

Quoted 10 lines
xen/xsm/xsm_policy.c +3 -6
-     * Try all modules and see whichever could be the binary policy.
-     * Adjust module_map for the module that is the binary policy.
-     */
-    for ( i = bi->nr_modules - 1; i >= 1; i-- )
+    /* Try all unknown modules and see whichever could be the binary policy. */
+    for_each_boot_module_by_type(i, bi, BOOTMOD_UNKNOWN)
     {
-        if ( !test_bit(i, bi->module_map) )
-            continue;
+        struct boot_module *bm = &bi->mods[i];

... same here (and likely elsewhere in the series).

Jan

#11 lore raw

Re: [PATCH 02/12] x86/boot: eliminate module_map

Quoted 5 lines · Jan Beulich
Quoted 4 lines · Daniel P. Smith
xen/arch/x86/cpu/microcode/core.c +0 -0
@@ -790,15 +790,13 @@ static int __init early_microcode_load(struct boot_info *bi)
 
Quoted 9 lines

xen/arch/x86/cpu/microcode/core.c static int __init early_microcode_load(struct boot_info *bi) · line 795

Quoted 6 lines
xen/arch/x86/cpu/microcode/core.c +2 -1
      if ( opt_scan ) /* Scan for a CPIO archive */
      {
-        for ( idx = 1; idx < bi->nr_modules; ++idx )
+        for_each_boot_module_by_type(idx, bi, BOOTMOD_UNKNOWN)
          {
+            struct boot_module *bm = &bi->mods[idx];

pointer-to-const? You really want to get used to applying const to pointed-to types whenever possible. IOW ...

Yes, I have been trying to be more diligent to add them when possible. To the point that I have had to unwind some due to writes that must be done in later function calls they get passed. Obviously this is not one of those case and can fix this one.

Quoted 12 lines
Quoted 12 lines
xen/xsm/xsm_policy.c +1 -1
@@ -33,22 +33,18 @@
  int __init xsm_multiboot_policy_init(
      struct boot_info *bi, void **policy_buffer, size_t *policy_size)
  {
-    int i;
+    unsigned int i;
      int rc = 0;
      u32 *_policy_start;
      unsigned long _policy_len;
 
Quoted 13 lines

xen/xsm/xsm_policy.c line 44

Quoted 11 lines
xen/xsm/xsm_policy.c +3 -7
-    /*
-     * Try all modules and see whichever could be the binary policy.
-     * Adjust module_map for the module that is the binary policy.
-     */
-    for ( i = bi->nr_modules - 1; i >= 1; i-- )
+    /* Try all unknown modules and see whichever could be the binary policy. */
+    for_each_boot_module_by_type(i, bi, BOOTMOD_UNKNOWN)
      {
-        if ( !test_bit(i, bi->module_map) )
-            continue;
+        struct boot_module *bm = &bi->mods[i];

... same here (and likely elsewhere in the series).

Nope, you can't const this one as that will cause this is at the tail end of the loop to fail:

+ bm->type = BOOTMOD_XSM_POLICY;

v/r,
dps

#12 lore raw

Re: [PATCH 02/12] x86/boot: eliminate module_map

Quoted 28 lines · Daniel P. Smith
Quoted 13 lines · Jan Beulich
Quoted 12 lines · Daniel P. Smith
xen/xsm/xsm_policy.c +1 -1
@@ -33,22 +33,18 @@
  int __init xsm_multiboot_policy_init(
      struct boot_info *bi, void **policy_buffer, size_t *policy_size)
  {
-    int i;
+    unsigned int i;
      int rc = 0;
      u32 *_policy_start;
      unsigned long _policy_len;
 
Quoted 13 lines

xen/xsm/xsm_policy.c line 44

Quoted 11 lines
xen/xsm/xsm_policy.c +3 -7
-    /*
-     * Try all modules and see whichever could be the binary policy.
-     * Adjust module_map for the module that is the binary policy.
-     */
-    for ( i = bi->nr_modules - 1; i >= 1; i-- )
+    /* Try all unknown modules and see whichever could be the binary policy. */
+    for_each_boot_module_by_type(i, bi, BOOTMOD_UNKNOWN)
      {
-        if ( !test_bit(i, bi->module_map) )
-            continue;
+        struct boot_module *bm = &bi->mods[i];

... same here (and likely elsewhere in the series).

Quoted 4 lines

Nope, you can't const this one as that will cause this is at the tail end of the loop to fail:

+ bm->type = BOOTMOD_XSM_POLICY;

Oh, I had managed to not spot that.

Jan

#13 lore raw

Re: [PATCH 02/12] x86/boot: eliminate module_map

Quoted 29 lines · Jan Beulich
Quoted 28 lines · Daniel P. Smith
Quoted 26 lines · Jan Beulich
Quoted 13 lines · Daniel P. Smith
xen/xsm/xsm_policy.c +1 -2
@@ -33,22 +33,18 @@
   int __init xsm_multiboot_policy_init(
       struct boot_info *bi, void **policy_buffer, size_t *policy_size)
   {
-    int i;
+    unsigned int i;
       int rc = 0;
       u32 *_policy_start;
       unsigned long _policy_len;
 
-    /*

xen/xsm/xsm_policy.c line 44

Quoted 10 lines
xen/xsm/xsm_policy.c +3 -6
-     * Try all modules and see whichever could be the binary policy.
-     * Adjust module_map for the module that is the binary policy.
-     */
-    for ( i = bi->nr_modules - 1; i >= 1; i-- )
+    /* Try all unknown modules and see whichever could be the binary policy. */
+    for_each_boot_module_by_type(i, bi, BOOTMOD_UNKNOWN)
       {
-        if ( !test_bit(i, bi->module_map) )
-            continue;
+        struct boot_module *bm = &bi->mods[i];

... same here (and likely elsewhere in the series).

Quoted 6 lines
Quoted 4 lines

Nope, you can't const this one as that will cause this is at the tail end of the loop to fail:

+ bm->type = BOOTMOD_XSM_POLICY;

Oh, I had managed to not spot that.

No worries. As I said, I have been trying to develop the habit to check if write usage will be needed, const-ing and then finding myself often greeted with gcc error about write to const. Wish gcc could warn about pointers that have no writes in scope to help catch viable candidates.

v/r,
dps

#22 lore raw

Re: [PATCH 04/12] x86/boot: introduce module release

In reply to Daniel P. Smith
Quoted 52 lines · Daniel P. Smith
Quoted 13 lines · Jason Andryuk
Quoted 12 lines · Daniel P. Smith

A precarious approach was used to release the pages used to hold a boot module. The precariousness stemmed from the fact that in the case of PV dom0, the initrd module pages may be either mapped or explicitly copied into the dom0 address space. So to handle this situation, the PV dom0 construction code will set the size of the module to zero, relying on discard_initial_images() to skip any modules with a size of zero.

A function is introduced to release a module when it is no longer needed that accepts a boolean parameter, free_mem, to indicate if the corresponding pages can be freed. To track that a module has been released, the boot module flag released is introduced.

Quoted 6 lines
Quoted 4 lines

The previous release model was a free all at once except those of size zeros, which would handle any unused modules passed. The new model is one of, free consumed module after usage is complete, thus unconsumed modules do not have a consumer to free them.

Slightly confusing.  Maybe just "The new model is to free modules after they are consumed.  Thus unconsumed modules are not freed."

okay.

Quoted 3 lines
Quoted 1 line

To address this, the discard_uknown_boot_modules() is

"unknown"

Ack

Quoted 23 lines
Quoted 22 lines

introduced and called after the last module identification occurs, initrd, to free the pages of any boot modules that are identified as not being released. After domain construction completes, all modules should be freed. A walk of the boot modules is added after domain construction to validate and notify if a module is found not to have been released.

---

+    {
+        struct boot_module *bm = &bi->mods[i];
+
+        if ( bm == NULL || bm->released )
              continue;
-        init_domheap_pages(start, start + PAGE_ALIGN(size)); +        release_boot_module(bm, true);
+        count++;
      }
-    bi->nr_modules = 0;
+    if ( count )
+        printk(XENLOG_DEBUG "Releasing pages for uknown boot module %d\n",

Quoted 5 lines
Quoted 1 line

"unknown".  Since the operation already happened, maybe "Released pages for %d unknown boot modules"?  I'm not sure of the value of that message.  It would be more informative to provide the module index and maybe a page count.  The index would at least point to which module is unused.

Ack to unknown.

Can adjust the phrasing, the question is there a desire to have a message for every boot module freed. Guess I could do a single log line split across multiple printks, Thinking about the case where someone tried to abuse the interface by loading a bunch of unused modules.

It's 63 modules, so not too many. And this is the boot path, so it's administrator controlled and shouldn't really be subject to abuse.

I think printing the index of unused modules makes the message useful. If a module is provided to Xen, but Xen doesn't use it, it is worth flagging which one. As an example, if I thought module 3 was ucode, but it isn't getting used, that is useful information for further investigation.

Quoted 23 lines
Quoted 14 lines
Quoted 14 lines

+               count);
  }
  static void __init init_idle_domain(void)

xen/arch/x86/setup.c +2 -0
@@ -2111,6 +2139,8 @@ void asmlinkage __init noreturn __start_xen(void)
                      initrdidx);
       }
+    discard_unknown_boot_modules();
+
       /*
        * We're going to setup domain0 using the module(s) that we stashed safely
        * above our heap. The second module, if present, is an initrd ramdisk.
@@ -2122,6 +2152,11 @@ void asmlinkage __init noreturn __start_xen(void)
       if ( !dom0 )
           panic("Could not set up DOM0 guest OS\n");
Quoted 8 lines

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 2159

Quoted 6 lines
xen/arch/x86/setup.c +5 -0
+    /* Check and warn if any modules did not get released */
+    for ( i = 0; i < bi->nr_modules; i++ )
+        if ( !bi->mods[i].released )
+            printk(XENLOG_ERR "Boot module %d not released, memory 
 leaked", i);
+

Why not release the memory here instead of leaking it?

Quoted 1 line

Because you don't know if it was mapped or consumed.

So this is more of a sanity check since it should not trigger? i.e. it's a bug to see this message.

Quoted 3 lines
Quoted 1 line

I feel like the corner case of mapping the dom0 initrd is leading to this manual approach or releasing modules individually.  That logic then has to be spread around.  discard_initial_images() kept the logic centralized.  Maybe just replace the mod_end == 0 special case with a "don't release me" flag that is checked in discard_initial_images()?

That is what started me at the options to deal with it. The two I came up with was a flag and this approach. Weighing the pros/cons of the two, the deciding factor is when multi-domain construction is introduced. With multi-domain with a large number of domains, a balance has to be struck between holding all the kernels and ramdisks in memory plus being able to allocate the desired amount of memory for each domain.

So you're saying that by piece-wise free-ing memory, you can have more domUs loaded by the boot loader? If free-ing is delayed to the end, memory is tied up that could otherwise be assigned to domUs?

Is this the real motivation? If so, it belongs in the commit message. As it is, the commit message is lacking in a specify reason why this change is needed.

Quoted 1 line

Perhaps a balance can be struck, with a discard_consumed_boot_modules() that walks the boot module list, and discard the ones consumed. While only dom0 can be constructed, it would be called once after create_dom0 call returns (per Andy's suggestion in response to this comment). An expansion on this could be that instead of using a free flag, use a ref count that is incremented as it is claimed and the decremented when it has been consumed.

Are you expecting 1 kernel image gets used to create N domUs? Or are you only handling 1:1?

Is mapping the initrd only expected for a single PV dom0, or do you envision mapping multiple initrds for multiple PV domUs?

It seems to me that more mapping might help alleviate keeping memory tied up. Freeing XSM and ucode early helps, but those aren't particularly big. An initrd in the 100s of MBs is significant though. If you hyperlaunch Xen and 2 domUs, you'd want to be able to assign ~all the memory. Losing 2x ~100MB as unassigned for copies of the initrds is wasteful.

Regards,
Jason

#40 lore raw

Re: [PATCH 09/12] x86/boot: add start and size fields to struct boot_module

In reply to Daniel P. Smith
Quoted 43 lines · Daniel P. Smith

Introduce the start and size fields to struct boot_module and assigns their value during boot_info construction. All uses of module_t to get the address and size of a module are replaced with start and size.

The EFI entry point is a special case, as the EFI file loading boot service may load a file beyond the 4G barrier. As a result, to make the address fit in the 32bit integer used by the MB1 module_t structure, the frame number is stored in mod_start and size in mod_end. Until the EFI entry point is enlightened to work with boot_info and boot_module, multiboot_fill_boot_info will handle the alternate values in mod_start and mod_end when EFI is detected.

A result of the switch to start/size removes all uses of the mod field in struct boot_modules, along with the uses of bootstra_map() and release_module() functions. With all usage gone, they all are dropped here.

--
Changes since v7:
- add the start/size change to bootstrap_map_bm()
- convert all BM start/size when introduced, consolidates:
x86/boot: populate boot module for xen entry
x86/boot: transition relocation calculations to struct boot_module
- consolidates all the removal commits

Changes since v6:
- put the efi conversion for mod_start and mod_end back along with check
- dropped unnecessary cast
- updated the population of start and size fields to take into account efi

Changes since v5:
- switched EFI population of mod_start/mod_end to addresses

a# edit 336ac1fc0019 x86/boot: introduce boot domain
---

xen/arch/x86/setup.c +0 -0
index d9785acf89b6..18b93d6a272a 100644
@@ -313,13 +313,29 @@ static struct boot_info *__init multiboot_fill_boot_info(
       */
      for ( i = 0; i < MAX_NR_BOOTMODS && i < bi->nr_modules; i++ )
      {

xen/arch/x86/setup.c static struct boot_info *__init multiboot_fill_boot_info( · line 323

Quoted 10 lines
xen/arch/x86/setup.c +7 -2
-        bi->mods[i].mod = &mods[i];
-
          bi->mods[i].cmdline_pa = mods[i].string;
+
+        if ( !efi_enabled(EFI_LOADER) )
+        {
+            /*
+             * The EFI loader gives us modules which are already frame/size.
+             * Switch back to address/size.
+             */

This comment...

xen/arch/x86/setup.c static struct boot_info *__init multiboot_fill_boot_info( · line 332

Quoted 9 lines
xen/arch/x86/setup.c +9 -0
+            bi->mods[i].start = mods[i].mod_start;
+            bi->mods[i].size = mods[i].mod_end - mods[i].mod_start;
+        }
+        else
+        {
+            /*
+             * PVH and BIOS loaders give us modules which are start/end.
+             * Switch to address/size.
+             */

And this comment are reversed? But I would just use positive logic:

if ( efi_enabled(EFI_LOADER) )
/* EFI case */
else
/* non-EFI */

Quoted 11 lines
xen/arch/x86/setup.c +3 -1
+            bi->mods[i].start = pfn_to_paddr(mods[i].mod_start);
+            bi->mods[i].size = mods[i].mod_end;
+        }
      }
 
      /* Variable 'i' should be one entry past the last module. */
-    bi->mods[i].mod = &mods[bi->nr_modules];
      bi->mods[i].type = BOOTMOD_XEN;
 
      return bi;

xen/arch/x86/setup.c unsigned long __init initial_images_nrpages(nodeid_t node) · line 354

Quoted 6 lines
xen/arch/x86/setup.c +1 -2
@@ -335,8 +351,8 @@ unsigned long __init initial_images_nrpages(nodeid_t node)
 
      for ( nr = i = 0; i < bi->nr_modules; ++i )
      {
-        unsigned long start = bi->mods[i].mod->mod_start;
-        unsigned long end   = start + PFN_UP(bi->mods[i].mod->mod_end);
+        unsigned long start = bi->mods[i].start;

This should be paddr_to_pfn(bi->mods[i].start)?

Quoted 8 lines
xen/arch/x86/setup.c +1 -0
+        unsigned long end   = start + PFN_UP(bi->mods[i].size);
 
          if ( end > node_start && node_end > start )
              nr += min(node_end, end) - max(node_start, start);
@@ -1745,13 +1733,11 @@ void asmlinkage __init noreturn __start_xen(void)
 
      for ( i = 0; i < bi->nr_modules; ++i )

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 1738

Quoted 10 lines
xen/arch/x86/setup.c +2 -6
      {
-        const struct boot_module *bm = &bi->mods[i];
+        unsigned long s = bi->mods[i].start, l = bi->mods[i].size;
 
-        set_pdx_range(bm->mod->mod_start,
-                      bm->mod->mod_start + PFN_UP(bm->mod->mod_end));
-        map_pages_to_xen((unsigned long)mfn_to_virt(bm->mod->mod_start),
-                         _mfn(bm->mod->mod_start),
-                         PFN_UP(bm->mod->mod_end), PAGE_HYPERVISOR);
+        set_pdx_range(paddr_to_pfn(s), paddr_to_pfn(s) + PFN_UP(l));

This is fine today since s (.start) is checked for page alignment. The other option would be paddr_to_pfn(s + l) + 1, but I'm not sure that is an improvement.

You don't have to change anything. Just noting something I noticed.

Regards,
Jason

Quoted 5 lines
xen/arch/x86/setup.c +2 -0
+        map_pages_to_xen((unsigned long)maddr_to_virt(s), maddr_to_mfn(s),
+                         PFN_UP(l), PAGE_HYPERVISOR);
      }
 
  #ifdef CONFIG_KEXEC
Reviewed-by on v1 10/12
#46 lore raw

Re: [PATCH 10/12] x86/boot: introduce boot domain

In reply to Daniel P. Smith
Quoted 24 lines · Daniel P. Smith

To begin moving toward allowing the hypervisor to construct more than one domain at boot, a container is needed for a domain's build information. Introduce a new header, <xen/asm/bootdomain.h>, that contains the initial struct boot_domain that encapsulate the build information for a domain.

Add a kernel and ramdisk boot module reference along with a struct domain reference to the new struct boot_domain. This allows a struct boot_domain reference to be the only parameter necessary to pass down through the domain construction call chain.

---
Changes since v7
- collapsed the incremental changes to the call chain into this commit
---

xen/arch/x86/setup.c +0 -0
index 18b93d6a272a..6e25a9f726c7 100644
@@ -1026,11 +1019,11 @@ static struct domain *__init create_dom0(struct boot_info *bi)
          panic("Error creating d%uv0\n", domid);
 

xen/arch/x86/setup.c static struct domain *__init create_dom0(struct boot_info *bi) · line 1026

Quoted 10 lines
xen/arch/x86/setup.c +4 -4
      /* Grab the DOM0 command line. */
-    if ( image->cmdline_pa || bi->kextra )
+    if ( bd->kernel->cmdline_pa || bi->kextra )
      {
-        if ( image->cmdline_pa )
-            safe_strcpy(
-                cmdline, cmdline_cook(__va(image->cmdline_pa), bi->loader));
+        if ( bd->kernel->cmdline_pa )
+            safe_strcpy(cmdline, cmdline_cook(__va(bd->kernel->cmdline_pa),
+                        bi->loader));

I'd indent bi->loader so it lines up with __va, since bi->loader is the second argument to cmdline_cook().

With that:

Quoted 3 lines
xen/arch/x86/setup.c +0 -0
          if ( bi->kextra )
              /* kextra always includes exactly one leading space. */
#50 lore raw

Re: [PATCH 12/12] x86/boot: add cmdline to struct boot_domain

In reply to Daniel P. Smith
Quoted 5 lines · Daniel P. Smith

Add a container for the "cooked" command line for a domain. This provides for the backing memory to be directly associated with the domain being constructed. This is done in anticipation that the domain construction path may need to be invoked multiple times, thus ensuring each instance had a distinct memory allocation.

Is the area only used at domain construction time? If that runs sequentially, then only a single cmdline buffer would be needed. cmdline_pa can keep pointing to individual cmdlines. Unless the multi-domain builder needs to keep multiple? But that could maybe keep cmdline_pa pointing into the device tree?

Quoted 17 lines

---
Changes since v7:
- updated commit message to expand on intent and purpose

---
  xen/arch/x86/include/asm/bootdomain.h |  4 ++++
  xen/arch/x86/include/asm/dom0_build.h |  1 +
  xen/arch/x86/pv/dom0_build.c          |  4 ++--
  xen/arch/x86/setup.c                  | 18 ++++++++----------
  4 files changed, 15 insertions(+), 12 deletions(-)
xen/arch/x86/include/asm/bootdomain.h +0 -0
index 3873f916f854..bc51f04a1df6 100644
@@ -8,10 +8,14 @@
  #ifndef __XEN_X86_BOOTDOMAIN_H__

xen/arch/x86/include/asm/bootdomain.h line 18

Quoted 10 lines
xen/arch/x86/include/asm/bootdomain.h +4 -0
  #define __XEN_X86_BOOTDOMAIN_H__
 
+#include <public/xen.h>
+
  struct boot_module;
  struct domain;
 
  struct boot_domain {
+    char cmdline[MAX_GUEST_CMDLINE];
+

You might want to put the 1024 byte chars at the end of the structure. Having the other pointers at the start is probably more useful.

Quoted 9 lines
xen/arch/x86/include/asm/bootdomain.h +0 -0
      domid_t domid;
 
      struct boot_module *kernel;
xen/arch/x86/pv/dom0_build.c +0 -0
index 28257bf13127..2c84af52de3e 100644

xen/arch/x86/pv/dom0_build.c static int __init dom0_construct(struct boot_domain *bd) · line 963

Quoted 6 lines
xen/arch/x86/pv/dom0_build.c +1 -2
@@ -960,8 +960,8 @@ static int __init dom0_construct(struct boot_domain *bd)
      }
 
      memset(si->cmd_line, 0, sizeof(si->cmd_line));
-    if ( cmdline != NULL )
-        strlcpy((char *)si->cmd_line, cmdline, sizeof(si->cmd_line));
+    if ( cmdline[0] != '\0' )

Shouldn't this check bd->cmdline[0] ? There is a const char *cmdline in this function that should probably be removed.

Regards,
Jason

Quoted 4 lines
xen/arch/x86/pv/dom0_build.c +1 -0
+        strlcpy((char *)si->cmd_line, bd->cmdline, sizeof(si->cmd_line));
 
  #ifdef CONFIG_VIDEO
      if ( !pv_shim && fill_console_start_info((void *)(si + 1
#41 lore raw

Re: [PATCH 09/12] x86/boot: add start and size fields to struct boot_module

In reply to Jason Andryuk
Quoted 70 lines · Jason Andryuk
Quoted 48 lines · Daniel P. Smith

Introduce the start and size fields to struct boot_module and assigns their value during boot_info construction. All uses of module_t to get the address and size of a module are replaced with start and size.

The EFI entry point is a special case, as the EFI file loading boot service may load a file beyond the 4G barrier. As a result, to make the address fit in the 32bit integer used by the MB1 module_t structure, the frame number is stored in mod_start and size in mod_end. Until the EFI entry point is enlightened to work with boot_info and boot_module, multiboot_fill_boot_info will handle the alternate values in mod_start and mod_end when EFI is detected.

A result of the switch to start/size removes all uses of the mod field in struct boot_modules, along with the uses of bootstra_map() and release_module() functions. With all usage gone, they all are dropped here.

Signature · 18 lines
Changes since v7:
- add the start/size change to bootstrap_map_bm()
- convert all BM start/size when introduced, consolidates:
     x86/boot: populate boot module for xen entry
     x86/boot: transition relocation calculations to struct boot_module
- consolidates all the removal commits

Changes since v6:
- put the efi conversion for mod_start and mod_end back along with check
- dropped unnecessary cast
- updated the population of start and size fields to take into account 
efi

Changes since v5:
- switched EFI population of mod_start/mod_end to addresses

a#    edit 336ac1fc0019 x86/boot: introduce boot domain
---
xen/arch/x86/setup.c +0 -0
index d9785acf89b6..18b93d6a272a 100644
@@ -313,13 +313,29 @@ static struct boot_info *__init 

multiboot_fill_boot_info(
       */
      for ( i = 0; i < MAX_NR_BOOTMODS && i < bi->nr_modules; i++ ) {
-        bi->mods[i].mod = &mods[i];
-
          bi->mods[i].cmdline_pa = mods[i].string;

Quoted 8 lines

+
+        if ( !efi_enabled(EFI_LOADER) )
+        {
+            /*
+             * The EFI loader gives us modules which are already frame/size.
+             * Switch back to address/size.
+             */

This comment...

Quoted 9 lines

+            bi->mods[i].start = mods[i].mod_start;
+            bi->mods[i].size = mods[i].mod_end - mods[i].mod_start; +        }
+        else
+        {
+            /*
+             * PVH and BIOS loaders give us modules which are start/end. +             * Switch to address/size.
+             */

Quoted 6 lines

And this comment are reversed?  But I would just use positive logic:

if ( efi_enabled(EFI_LOADER) )
   /* EFI case */
else
   /* non-EFI */

Ack.

Quoted 9 lines
Quoted 9 lines

+            bi->mods[i].start = pfn_to_paddr(mods[i].mod_start); +            bi->mods[i].size = mods[i].mod_end;
+        }
      }
      /* Variable 'i' should be one entry past the last module. */ -    bi->mods[i].mod = &mods[bi->nr_modules]; bi->mods[i].type = BOOTMOD_XEN;
      return bi;

xen/arch/x86/setup.c +0 -0
@@ -335,8 +351,8 @@ unsigned long __init 
Quoted 8 lines

xen/arch/x86/setup.c unsigned long __init initial_images_nrpages(nodeid_t node) · line 354

Quoted 6 lines
xen/arch/x86/setup.c +1 -2
 initial_images_nrpages(nodeid_t node)
       for ( nr = i = 0; i < bi->nr_modules; ++i )
       {
-        unsigned long start = bi->mods[i].mod->mod_start;
-        unsigned long end   = start + PFN_UP(bi->mods[i].mod->mod_end);
+        unsigned long start = bi->mods[i].start;

This should be paddr_to_pfn(bi->mods[i].start)?

Yes, and honestly probably a bad names for the vars as start/end tends to refer to addresses while pages usually have mfn/pfn in their name. The question is the churn from variable name not desired.

Quoted 4 lines
Quoted 3 lines
xen/arch/x86/setup.c +1 -0
+        unsigned long end   = start + PFN_UP(bi->mods[i].size);
           if ( end > node_start && node_end > start )
               nr += min(node_end, end) - max(node_start, start);
Quoted 13 lines

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 1736

Quoted 11 lines
xen/arch/x86/setup.c +2 -6
@@ -1745,13 +1733,11 @@ void asmlinkage __init noreturn __start_xen(void)
       for ( i = 0; i < bi->nr_modules; ++i )
       {
-        const struct boot_module *bm = &bi->mods[i];
+        unsigned long s = bi->mods[i].start, l = bi->mods[i].size;
-        set_pdx_range(bm->mod->mod_start,
-                      bm->mod->mod_start + PFN_UP(bm->mod->mod_end));
-        map_pages_to_xen((unsigned long)mfn_to_virt(bm->mod->mod_start),
-                         _mfn(bm->mod->mod_start),
-                         PFN_UP(bm->mod->mod_end), PAGE_HYPERVISOR);
+        set_pdx_range(paddr_to_pfn(s), paddr_to_pfn(s) + PFN_UP(l));

This is fine today since s (.start) is checked for page alignment.  The other option would be paddr_to_pfn(s + l) + 1, but I'm not sure that is an improvement.

Out of curiosity, why are you thinking that module start would never be paged aligned?

Quoted 1 line

You don't have to change anything.  Just noting something I noticed.

Ack.

v/r,
dps

#47 lore raw

Re: [PATCH 10/12] x86/boot: introduce boot domain

In reply to Jason Andryuk
Quoted 32 lines · Jason Andryuk
Quoted 31 lines · Daniel P. Smith

To begin moving toward allowing the hypervisor to construct more than one domain at boot, a container is needed for a domain's build information. Introduce a new header, <xen/asm/bootdomain.h>, that contains the initial struct boot_domain that encapsulate the build information for a domain.

Add a kernel and ramdisk boot module reference along with a struct domain reference to the new struct boot_domain. This allows a struct boot_domain reference to be the only parameter necessary to pass down through the domain construction call chain.

---
Changes since v7
- collapsed the incremental changes to the call chain into this commit
---

xen/arch/x86/setup.c +1 -4
index 18b93d6a272a..6e25a9f726c7 100644
@@ -1026,11 +1019,11 @@ static struct domain *__init 
 create_dom0(struct boot_info *bi)
           panic("Error creating d%uv0\n", domid);
       /* Grab the DOM0 command line. */
-    if ( image->cmdline_pa || bi->kextra )
+    if ( bd->kernel->cmdline_pa || bi->kextra )
       {
-        if ( image->cmdline_pa )
-            safe_strcpy(
-                cmdline, cmdline_cook(__va(image->cmdline_pa), bi- 
Quoted 7 lines

xen/arch/x86/setup.c static struct domain *__init · line 1028

Quoted 5 lines
xen/arch/x86/setup.c +3 -0
 >loader));
+        if ( bd->kernel->cmdline_pa )
+            safe_strcpy(cmdline, cmdline_cook(__va(bd->kernel- 
 >cmdline_pa),
+                        bi->loader));

I'd indent bi->loader so it lines up with __va, since bi->loader is the second argument to cmdline_cook().

Ack.

Quoted 3 lines

With that:

Thanks,

v/r,
dps

#51 lore raw

Re: [PATCH 12/12] x86/boot: add cmdline to struct boot_domain

In reply to Jason Andryuk
Quoted 8 lines · Jason Andryuk
Quoted 5 lines · Daniel P. Smith

Add a container for the "cooked" command line for a domain. This provides for the backing memory to be directly associated with the domain being constructed. This is done in anticipation that the domain construction path may need to be invoked multiple times, thus ensuring each instance had a distinct memory allocation.

Is the area only used at domain construction time?  If that runs sequentially, then only a single cmdline buffer would be needed. cmdline_pa can keep pointing to individual cmdlines.  Unless the multi- domain builder needs to keep multiple?  But that could maybe keep cmdline_pa pointing into the device tree?

It turns out that 1024 may not be large enough for all use cases. Instead of trying to make an educated guess, this is being switched to dynamic allocation for v9.

Quoted 11 lines
Quoted 11 lines

---
Changes since v7:
- updated commit message to expand on intent and purpose
---
  xen/arch/x86/include/asm/bootdomain.h |  4 ++++ xen/arch/x86/include/asm/dom0_build.h |  1 + xen/arch/x86/pv/dom0_build.c          |  4 ++-- xen/arch/x86/setup.c                  | 18 ++++++++----------

  4 files changed, 15 insertions(+), 12 deletions(-)
Quoted 16 lines
Quoted 5 lines
xen/arch/x86/include/asm/bootdomain.h +0 -0
index 3873f916f854..bc51f04a1df6 100644

xen/arch/x86/include/asm/bootdomain.h line 16

Quoted 9 lines
xen/arch/x86/include/asm/bootdomain.h +4 -0
@@ -8,10 +8,14 @@
   #ifndef __XEN_X86_BOOTDOMAIN_H__
   #define __XEN_X86_BOOTDOMAIN_H__
+#include <public/xen.h>
+
   struct boot_module;
   struct domain;
   struct boot_domain {
+    char cmdline[MAX_GUEST_CMDLINE];
+

You might want to put the 1024 byte chars at the end of the structure. Having the other pointers at the start is probably more useful.

OBE.

Quoted 16 lines
Quoted 8 lines
xen/arch/x86/include/asm/bootdomain.h +0 -0
       domid_t domid;
       struct boot_module *kernel;
 
xen/arch/x86/pv/dom0_build.c +0 -0
index 28257bf13127..2c84af52de3e 100644

xen/arch/x86/pv/dom0_build.c static int __init dom0_construct(struct · line 963

Quoted 6 lines
xen/arch/x86/pv/dom0_build.c +1 -2
@@ -960,8 +960,8 @@ static int __init dom0_construct(struct 
 boot_domain *bd)
       }
       memset(si->cmd_line, 0, sizeof(si->cmd_line));
-    if ( cmdline != NULL )
-        strlcpy((char *)si->cmd_line, cmdline, sizeof(si->cmd_line));
+    if ( cmdline[0] != '\0' )

Shouldn't this check bd->cmdline[0] ?  There is a const char *cmdline in this function that should probably be removed.

This was caught in the switch to dynamic allocation and the local var was dropped.

v/r,
dps

#42 lore raw

Re: [PATCH 09/12] x86/boot: add start and size fields to struct boot_module

In reply to Daniel P. Smith
Quoted 11 lines · Daniel P. Smith
Quoted 10 lines · Jason Andryuk
Quoted 9 lines · Daniel P. Smith
xen/arch/x86/setup.c +1 -4
@@ -1745,13 +1733,11 @@ void asmlinkage __init noreturn 
 __start_xen(void)
       for ( i = 0; i < bi->nr_modules; ++i )
       {
-        const struct boot_module *bm = &bi->mods[i];
+        unsigned long s = bi->mods[i].start, l = bi->mods[i].size;
-        set_pdx_range(bm->mod->mod_start,
-                      bm->mod->mod_start + PFN_UP(bm->mod->mod_end));
-        map_pages_to_xen((unsigned long)mfn_to_virt(bm->mod- 
Quoted 8 lines
Quoted 6 lines

xen/arch/x86/setup.c void asmlinkage __init noreturn __start_xen(void) · line 1738

Quoted 4 lines
xen/arch/x86/setup.c +1 -2
 >mod_start),
-                         _mfn(bm->mod->mod_start),
-                         PFN_UP(bm->mod->mod_end), PAGE_HYPERVISOR);
+        set_pdx_range(paddr_to_pfn(s), paddr_to_pfn(s) + PFN_UP(l));

This is fine today since s (.start) is checked for page alignment. The other option would be paddr_to_pfn(s + l) + 1, but I'm not sure that is an improvement.

Out of curiosity, why are you thinking that module start would never be paged aligned?

I think you have an extra negation - the module start is always page aligned as checked elsewhere.

While reviewing, I was just noting that this code starts rounding addresses when it previously operated on pfns. Non page-aligned s + l could then cross a page boundary.

Regards,
Jason

#43 lore raw

Re: [PATCH 09/12] x86/boot: add start and size fields to struct boot_module

Quoted 24 lines · Jason Andryuk
Quoted 16 lines · Daniel P. Smith
Quoted 15 lines · Jason Andryuk
Quoted 13 lines · Daniel P. Smith
xen/arch/x86/setup.c +2 -6
@@ -1745,13 +1733,11 @@ void asmlinkage __init noreturn
 __start_xen(void)
       for ( i = 0; i < bi->nr_modules; ++i )
       {
-        const struct boot_module *bm = &bi->mods[i];
+        unsigned long s = bi->mods[i].start, l = bi->mods[i].size;
-        set_pdx_range(bm->mod->mod_start,
-                      bm->mod->mod_start + PFN_UP(bm->mod->mod_end));
-        map_pages_to_xen((unsigned long)mfn_to_virt(bm->mod-
 >mod_start),
-                         _mfn(bm->mod->mod_start),
-                         PFN_UP(bm->mod->mod_end), PAGE_HYPERVISOR);
+        set_pdx_range(paddr_to_pfn(s), paddr_to_pfn(s) + PFN_UP(l));
Quoted 6 lines
Quoted 3 lines

This is fine today since s (.start) is checked for page alignment. The other option would be paddr_to_pfn(s + l) + 1, but I'm not sure that is an improvement.

Out of curiosity, why are you thinking that module start would never be paged aligned?

Quoted 6 lines

I think you have an extra negation - the module start is always page aligned as checked elsewhere.

While reviewing, I was just noting that this code starts rounding addresses when it previously operated on pfns.  Non page-aligned s + l could then cross a page boundary.

It's worth saying that until this patch, Xen critically depends on modules having 4k alignment.

This patch finally breaks that dependency, so we can e.g. load microcode actually-early on boot.

While the modules are currently aligned, lets try and write code which doesn't assume it.

So yes, probably best to have

    set_pdx_range(paddr_to_pfn(s), PFN_UP(s + l));

here.  (I think?)

~Andrew

#44 lore raw

Re: [PATCH 09/12] x86/boot: add start and size fields to struct boot_module

Quoted 38 lines · Andrew Cooper
Quoted 24 lines · Jason Andryuk
Quoted 16 lines · Daniel P. Smith
Quoted 15 lines · Jason Andryuk
Quoted 13 lines · Daniel P. Smith
xen/arch/x86/setup.c +2 -6
@@ -1745,13 +1733,11 @@ void asmlinkage __init noreturn
 __start_xen(void)
       for ( i = 0; i < bi->nr_modules; ++i )
       {
-        const struct boot_module *bm = &bi->mods[i];
+        unsigned long s = bi->mods[i].start, l = bi->mods[i].size;
-        set_pdx_range(bm->mod->mod_start,
-                      bm->mod->mod_start + PFN_UP(bm->mod->mod_end));
-        map_pages_to_xen((unsigned long)mfn_to_virt(bm->mod-
 > mod_start),
-                         _mfn(bm->mod->mod_start),
-                         PFN_UP(bm->mod->mod_end), PAGE_HYPERVISOR);
+        set_pdx_range(paddr_to_pfn(s), paddr_to_pfn(s) + PFN_UP(l));
Quoted 6 lines
Quoted 3 lines

This is fine today since s (.start) is checked for page alignment. The other option would be paddr_to_pfn(s + l) + 1, but I'm not sure that is an improvement.

Out of curiosity, why are you thinking that module start would never be paged aligned?

Quoted 6 lines

I think you have an extra negation - the module start is always page aligned as checked elsewhere.

While reviewing, I was just noting that this code starts rounding addresses when it previously operated on pfns.  Non page-aligned s + l could then cross a page boundary.

It's worth saying that until this patch, Xen critically depends on modules having 4k alignment.

This patch finally breaks that dependency, so we can e.g. load microcode actually-early on boot.

Quoted 8 lines

While the modules are currently aligned, lets try and write code which doesn't assume it.

So yes, probably best to have

    set_pdx_range(paddr_to_pfn(s), PFN_UP(s + l));

here.  (I think?)

I think PFN_UP(s + l) is wrong when it ends on an address ending in 0x1000 since it will not be rounded up.

s=0x1000
l=0x1000
PFN_UP(0x2000) = 2, but I think we want to pass 3 set_pdx_range.

So I think we want:

    paddr_to_pfn(s + l) + 1

I was wondering if we needed s + l - 1. But size is set as mod_end - mod_start, so s + l is correct.

Regards,
Jason