Skip to contentSkip to conversation

x86/boot: Remove the mbi/mod pointers

PATCH In review Andrew Cooper 2 participants 11 messages Oct 24, 2024

lore
Share & tools
lore
#0 lore raw

cover x86/boot: Remove the mbi/mod pointers

This prevents the reintroduction of patterns which turn out to be unsafe for PVH boots. Various bits extracted/rearranged from the Hyperlaunch v7 series.

https://gitlab.com/xen-project/people/andyhhp/xen/-/pipelines/1509397930

Andrew Cooper (2):
x86/boot: Introduce bootstrap_unmap()
x86/boot: Drop the mbi and mod pointers in __start_xen()

Daniel P. Smith (3):
x86/boot: Drop initial_images pointer
x86/boot: Introduce bootstrap_map_bm() to map boot_module's x86/boot: Convert mod[] to bi->mods[] in __start_xen()

 xen/arch/x86/cpu/microcode/core.c |   6 +-
 xen/arch/x86/include/asm/setup.h  |   4 ++
 xen/arch/x86/pv/dom0_build.c      |   2 +-
 xen/arch/x86/setup.c              | 107 ++++++++++++++++--------------
 xen/xsm/xsm_core.c                |   4 +-
 xen/xsm/xsm_policy.c              |   2 +-
 6 files changed, 70 insertions(+), 55 deletions(-)
Signature · 1 line
2.39.5
#1 lore raw

PATCH 1/5 x86/boot: Drop initial_images pointer

From: "Daniel P. Smith" <[email protected]>

Now that the module list is encapsulated inside boot_info, we can do away with the initial_images pointer.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

v7.5:
* Rearranged from several later patches

---
 xen/arch/x86/setup.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
xen/arch/x86/setup.c +4 -9
index fc18ba3e35b5..8c10fe51dfad 100644
@@ -276,8 +276,6 @@ custom_param("acpi", parse_acpi_param);
 
 static const char *cmdline_cook(const char *p, const char *loader_name);
 
-static const module_t *__initdata initial_images;
-
 struct boot_info __initdata xen_boot_info = {
     .loader = "unknown",
     .cmdline = "",
@@ -332,8 +330,8 @@ unsigned long __init initial_images_nrpages(nodeid_t node)
 
     for ( nr = i = 0; i < bi->nr_modules; ++i )
     {
-        unsigned long start = initial_images[i].mod_start;
-        unsigned long end = start + PFN_UP(initial_images[i].mod_end);
+        unsigned long start = bi->mods[i].mod->mod_start;
+        unsigned long end   = start + PFN_UP(bi->mods[i].mod->mod_end);
 
         if ( end > node_start && node_end > start )
             nr += min(node_end, end) - max(node_start, start);
@@ -349,14 +347,13 @@ void __init discard_initial_images(void)
 
     for ( i = 0; i < bi->nr_modules; ++i )
     {
-        uint64_t start = (uint64_t)initial_images[i].mod_start << PAGE_SHIFT;
+        uint64_t start = pfn_to_paddr(bi->mods[i].mod->mod_start);
 
         init_domheap_pages(start,
-                           start + PAGE_ALIGN(initial_images[i].mod_end));
+                           start + PAGE_ALIGN(bi->mods[i].mod->mod_end));
     }
 
     bi->nr_modules = 0;
-    initial_images = NULL;
 }
 
 static void __init init_idle_domain(void)
@@ -1376,8 +1373,6 @@ void asmlinkage __init noreturn __start_xen(void)
     set_kexec_crash_area_size((u64)nr_pages << PAGE_SHIFT);
     kexec_reserve_area();
 
-    initial_images = mod;
-
     for ( i = 0; !efi_enabled(EFI_LOADER) && i < bi->nr_modules; i++ )
     {
         if ( mod[i].mod_start & (PAGE_SIZE - 1) )
Signature · 1 line
2.39.5
Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] x86/boot: Drop initial_images pointer

Quoted 5 lines · Andrew Cooper

From: "Daniel P. Smith" <[email protected]>

Now that the module list is encapsulated inside boot_info, we can do away with the initial_images pointer.

Quoted 8 lines

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

#3 lore raw

PATCH 2/5 x86/boot: Introduce bootstrap_unmap()

In reply to Andrew Cooper

We're about to introduce alternative mapping functions, and passing NULL was always a slightly weird way to express unmap. Make an explicit unmap function, to avoid having two different valid ways of unmapping.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

v7.5:
* New

---
 xen/arch/x86/cpu/microcode/core.c |  6 +++---
 xen/arch/x86/include/asm/setup.h  |  2 ++
 xen/arch/x86/pv/dom0_build.c      |  2 +-
 xen/arch/x86/setup.c              | 10 ++++++----
 xen/xsm/xsm_core.c                |  4 ++--
 xen/xsm/xsm_policy.c              |  2 +-
 6 files changed, 15 insertions(+), 11 deletions(-)
xen/arch/x86/cpu/microcode/core.c +3 -3
index 1d58cb0f3bc1..cf6517293375 100644
@@ -198,7 +198,7 @@ static void __init microcode_scan_module(struct boot_info *bi)
             ucode_blob.data = cd.data;
             break;
         }
-        bootstrap_map(NULL);
+        bootstrap_unmap();
     }
 }
 
@@ -763,13 +763,13 @@ static int __init cf_check microcode_init(void)
      */
     if ( ucode_blob.size )
     {
-        bootstrap_map(NULL);
+        bootstrap_unmap();
         ucode_blob.size = 0;
         ucode_blob.data = NULL;
     }
     else if ( ucode_mod.mod_end )
     {
-        bootstrap_map(NULL);
+        bootstrap_unmap();
         ucode_mod.mod_end = 0;
     }
 
xen/arch/x86/include/asm/setup.h +2 -0
index 811855e57478..8b7843104ff7 100644
@@ -37,7 +37,9 @@ extern struct boot_info xen_boot_info;
 
 unsigned long initial_images_nrpages(nodeid_t node);
 void discard_initial_images(void);
+
 void *bootstrap_map(const module_t *mod);
+void bootstrap_unmap(void);
 
 int remove_xen_ranges(struct rangeset *r);
 
xen/arch/x86/pv/dom0_build.c +1 -1
index ee9ecdc2abbf..cdae17b27654 100644
@@ -830,7 +830,7 @@ static int __init dom0_construct(struct domain *d,
         printk("Failed to load the kernel binary\n");
         goto out;
     }
-    bootstrap_map(NULL);
+    bootstrap_unmap();
 
     if ( UNSET_ADDR != parms.virt_hypercall )
     {
xen/arch/x86/setup.c +6 -4
index 8c10fe51dfad..8e32d6c49c54 100644
@@ -471,13 +471,15 @@ static void *__init bootstrap_map_addr(paddr_t start, paddr_t end)
 
 void *__init bootstrap_map(const module_t *mod)
 {
-    if ( !mod )
-        return bootstrap_map_addr(0, 0);
-
     return bootstrap_map_addr(pfn_to_paddr(mod->mod_start),
                               pfn_to_paddr(mod->mod_start) + mod->mod_end);
 }
 
+void __init bootstrap_unmap(void)
+{
+    bootstrap_map_addr(0, 0);
+}
+
 static void __init move_memory(
     uint64_t dst, uint64_t src, unsigned int size)
 {
@@ -1402,7 +1404,7 @@ void asmlinkage __init noreturn __start_xen(void)
     }
 
     modules_headroom = bzimage_headroom(bootstrap_map(mod), mod->mod_end);
-    bootstrap_map(NULL);
+    bootstrap_unmap();
 
 #ifndef highmem_start
     /* Don't allow split below 4Gb. */
xen/xsm/xsm_core.c +2 -2
index 6e3fac68c057..f255fb63bf6f 100644
@@ -153,14 +153,14 @@ int __init xsm_multiboot_init(struct boot_info *bi)
         ret = xsm_multiboot_policy_init(bi, &policy_buffer, &policy_size);
         if ( ret )
         {
-            bootstrap_map(NULL);
+            bootstrap_unmap();
             printk(XENLOG_ERR "Error %d initializing XSM policy\n", ret);
             return -EINVAL;
         }
     }
 
     ret = xsm_core_init(policy_buffer, policy_size);
-    bootstrap_map(NULL);
+    bootstrap_unmap();
 
     return 0;
 }
xen/xsm/xsm_policy.c +1 -1
index 6f799dd28f5b..35f36c6f2359 100644
@@ -63,7 +63,7 @@ int __init xsm_multiboot_policy_init(
 
         }
 
-        bootstrap_map(NULL);
+        bootstrap_unmap();
     }
 
     return rc;
Signature · 1 line
2.39.5
Reviewed-by on v1 2/5
#4 lore raw

Re: [PATCH 2/5] x86/boot: Introduce bootstrap_unmap()

Quoted 11 lines · Andrew Cooper

We're about to introduce alternative mapping functions, and passing NULL was always a slightly weird way to express unmap. Make an explicit unmap function, to avoid having two different valid ways of unmapping.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

#5 lore raw

PATCH 3/5 x86/boot: Introduce bootstrap_map_bm() to map boot_module's

In reply to Andrew Cooper

From: "Daniel P. Smith" <[email protected]>

Convert converting the call to bzimage_headroom(), as well as using bi->mod[0]. It will be used externally by later changes.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

v7.5:
* Refectored/extracted from later in the series.

---
 xen/arch/x86/include/asm/setup.h | 2 ++
 xen/arch/x86/setup.c             | 9 ++++++++-
 2 files changed, 10 insertions(+), 1 deletion(-)
xen/arch/x86/include/asm/setup.h +2 -0
index 8b7843104ff7..4874ee8936fc 100644
@@ -38,6 +38,8 @@ extern struct boot_info xen_boot_info;
 unsigned long initial_images_nrpages(nodeid_t node);
 void discard_initial_images(void);
 
+struct boot_module;
+void *bootstrap_map_bm(const struct boot_module *bm);
 void *bootstrap_map(const module_t *mod);
 void bootstrap_unmap(void);
 
xen/arch/x86/setup.c +8 -1
index 8e32d6c49c54..ee03725af380 100644
@@ -475,6 +475,11 @@ void *__init bootstrap_map(const module_t *mod)
                               pfn_to_paddr(mod->mod_start) + mod->mod_end);
 }
 
+void *__init bootstrap_map_bm(const struct boot_module *bm)
+{
+    return bootstrap_map(bm->mod);
+}
+
 void __init bootstrap_unmap(void)
 {
     bootstrap_map_addr(0, 0);
@@ -1403,7 +1408,9 @@ void asmlinkage __init noreturn __start_xen(void)
         mod[bi->nr_modules].mod_end = __2M_rwdata_end - _stext;
     }
 
-    modules_headroom = bzimage_headroom(bootstrap_map(mod), mod->mod_end);
+    modules_headroom =
+        bzimage_headroom(bootstrap_map_bm(&bi->mods[0]),
+                         bi->mods[0].mod->mod_end);
     bootstrap_unmap();
 
 #ifndef highmem_start
Signature · 1 line
2.39.5
Reviewed-by on v1 3/5
#6 lore raw

Re: [PATCH 3/5] x86/boot: Introduce bootstrap_map_bm() to map boot_module's

Quoted 5 lines · Andrew Cooper

From: "Daniel P. Smith" <[email protected]>

Convert converting the call to bzimage_headroom(), as well as using bi->mod[0]. It will be used externally by later changes.

Quoted 8 lines

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

#7 lore raw

PATCH 4/5 x86/boot: Convert mod[] to bi->mods[] in __start_xen()

In reply to Andrew Cooper

From: "Daniel P. Smith" <[email protected]>

The former is about to disappear.

In some cases, introduce a local struct boot_module pointer. Judgement on where to do this, and on constness, is based on what creates least churn overall.

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

v7.5:
* Rearranged from several later patches

---
 xen/arch/x86/setup.c | 59 +++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 26 deletions(-)
xen/arch/x86/setup.c +33 -26
index ee03725af380..5413e1c71be6 100644
@@ -1382,11 +1382,11 @@ void asmlinkage __init noreturn __start_xen(void)
 
     for ( i = 0; !efi_enabled(EFI_LOADER) && i < bi->nr_modules; i++ )
     {
-        if ( mod[i].mod_start & (PAGE_SIZE - 1) )
+        if ( bi->mods[i].mod->mod_start & (PAGE_SIZE - 1) )
             panic("Bootloader didn't honor module alignment request\n");
-        mod[i].mod_end -= mod[i].mod_start;
-        mod[i].mod_start >>= PAGE_SHIFT;
-        mod[i].reserved = 0;
+        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;
     }
 
     /*
@@ -1397,6 +1397,8 @@ void asmlinkage __init noreturn __start_xen(void)
 
     if ( xen_phys_start )
     {
+        struct boot_module *xen = &bi->mods[bi->nr_modules];
+
         relocated = true;
 
         /*
@@ -1404,8 +1406,8 @@ void asmlinkage __init noreturn __start_xen(void)
          * respective reserve_e820_ram() invocation below. No need to
          * query efi_boot_mem_unused() here, though.
          */
-        mod[bi->nr_modules].mod_start = virt_to_mfn(_stext);
-        mod[bi->nr_modules].mod_end = __2M_rwdata_end - _stext;
+        xen->mod->mod_start = virt_to_mfn(_stext);
+        xen->mod->mod_end   = __2M_rwdata_end - _stext;
     }
 
     modules_headroom =
@@ -1490,15 +1492,17 @@ void asmlinkage __init noreturn __start_xen(void)
         /* Is the region suitable for relocating the multiboot modules? */
         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 + mod[j].mod_end);
+            unsigned long size = PAGE_ALIGN(headroom + bm->mod->mod_end);
 
-            if ( mod[j].reserved )
+            if ( bm->mod->reserved )
                 continue;
 
             /* Don't overlap with other modules (or Xen itself). */
@@ -1510,14 +1514,13 @@ void asmlinkage __init noreturn __start_xen(void)
 
             if ( s < end &&
                  (headroom ||
-                  ((end - size) >> PAGE_SHIFT) > mod[j].mod_start) )
+                  ((end - size) >> PAGE_SHIFT) > bm->mod->mod_start) )
             {
                 move_memory(end - size + headroom,
-                            (uint64_t)mod[j].mod_start << PAGE_SHIFT,
-                            mod[j].mod_end);
-                mod[j].mod_start = (end - size) >> PAGE_SHIFT;
-                mod[j].mod_end += headroom;
-                mod[j].reserved = 1;
+                            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;
             }
         }
 
@@ -1543,13 +1546,14 @@ void asmlinkage __init noreturn __start_xen(void)
 #endif
     }
 
-    if ( modules_headroom && !mod->reserved )
+    if ( modules_headroom && !bi->mods[0].mod->reserved )
         panic("Not enough memory to relocate the dom0 kernel image\n");
     for ( i = 0; i < bi->nr_modules; ++i )
     {
-        uint64_t s = (uint64_t)mod[i].mod_start << PAGE_SHIFT;
+        const struct boot_module *bm = &bi->mods[i];
+        uint64_t s = pfn_to_paddr(bm->mod->mod_start);
 
-        reserve_e820_ram(&boot_e820, s, s + PAGE_ALIGN(mod[i].mod_end));
+        reserve_e820_ram(&boot_e820, s, s + PAGE_ALIGN(bm->mod->mod_end));
     }
 
     if ( !xen_phys_start )
@@ -1627,8 +1631,8 @@ void asmlinkage __init noreturn __start_xen(void)
                 map_e = boot_e820.map[j].addr + boot_e820.map[j].size;
                 for ( j = 0; j < bi->nr_modules; ++j )
                 {
-                    uint64_t end = pfn_to_paddr(mod[j].mod_start) +
-                                   mod[j].mod_end;
+                    uint64_t end = pfn_to_paddr(bi->mods[j].mod->mod_start) +
+                                   bi->mods[j].mod->mod_end;
 
                     if ( map_e < end )
                         map_e = end;
@@ -1702,11 +1706,13 @@ void asmlinkage __init noreturn __start_xen(void)
 
     for ( i = 0; i < bi->nr_modules; ++i )
     {
-        set_pdx_range(mod[i].mod_start,
-                      mod[i].mod_start + PFN_UP(mod[i].mod_end));
-        map_pages_to_xen((unsigned long)mfn_to_virt(mod[i].mod_start),
-                         _mfn(mod[i].mod_start),
-                         PFN_UP(mod[i].mod_end), PAGE_HYPERVISOR);
+        const struct boot_module *bm = &bi->mods[i];
+
+        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);
     }
 
 #ifdef CONFIG_KEXEC
@@ -2095,8 +2101,9 @@ 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(mod, modules_headroom,
-                       initrdidx < bi->nr_modules ? mod + initrdidx : NULL,
+    dom0 = create_dom0(bi->mods[0].mod, modules_headroom,
+                       initrdidx < bi->nr_modules ? bi->mods[initrdidx].mod
+                                                  : NULL,
                        kextra, bi->loader);
     if ( !dom0 )
         panic("Could not set up DOM0 guest OS\n");
Signature · 1 line
2.39.5
Reviewed-by on v1 4/5
#8 lore raw

Re: [PATCH 4/5] x86/boot: Convert mod[] to bi->mods[] in __start_xen()

Quoted 8 lines · Andrew Cooper

From: "Daniel P. Smith" <[email protected]>

The former is about to disappear.

In some cases, introduce a local struct boot_module pointer. Judgement on where to do this, and on constness, is based on what creates least churn overall.

Quoted 8 lines

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

#9 lore raw

PATCH 5/5 x86/boot: Drop the mbi and mod pointers in __start_xen()

In reply to Andrew Cooper

We can't drop them fully yet, but we can limit their scope to almost nothing, which serves the same purpose.

This removes the ability to accidentally reintroduce buggy uses of __va(mbi->mods_addr).

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>

v7.5:
* New

---
 xen/arch/x86/setup.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
xen/arch/x86/setup.c +8 -8
index 5413e1c71be6..511cf5b97909 100644
@@ -1026,8 +1026,6 @@ 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;
-    multiboot_info_t *mbi;
-    module_t *mod;
     unsigned long nr_pages, raw_max_page, modules_headroom, module_map[1];
     int i, j, e820_warn = 0, bytes = 0;
     unsigned long eb_start, eb_end;
@@ -1063,16 +1061,22 @@ void asmlinkage __init noreturn __start_xen(void)
 
     if ( pvh_boot )
     {
+        multiboot_info_t *mbi;
+        module_t *mod;
+
         pvh_init(&mbi, &mod);
         /*
          * mbi and mod are regular pointers to .initdata.  These remain valid
          * across move_xen().
          */
+
+        bi = multiboot_fill_boot_info(mbi, mod);
     }
     else
     {
-        mbi = __va(multiboot_ptr);
-        mod = __va(mbi->mods_addr);
+        multiboot_info_t *mbi = __va(multiboot_ptr);
+
+        bi = multiboot_fill_boot_info(mbi, __va(mbi->mods_addr));
 
         /*
          * For MB1/2, mbi and mod are directmap pointers into the trampoline.
@@ -1085,12 +1089,8 @@ void asmlinkage __init noreturn __start_xen(void)
         ASSERT(multiboot_ptr < MB(1) || xen_phys_start);
     }
 
-    bi = multiboot_fill_boot_info(mbi, mod);
     bi->module_map = module_map; /* Temporary */
 
-    /* Use bi-> instead */
-#define mbi DO_NOT_USE
-
     /* Parse the command-line options. */
     if ( (kextra = strstr(bi->cmdline, " -- ")) != NULL )
     {
Signature · 1 line
2.39.5
Reviewed-by on v1 5/5
#10 lore raw

Re: [PATCH 5/5] x86/boot: Drop the mbi and mod pointers in __start_xen()

Quoted 6 lines · Andrew Cooper

We can't drop them fully yet, but we can limit their scope to almost nothing, which serves the same purpose.

This removes the ability to accidentally reintroduce buggy uses of __va(mbi->mods_addr).

Quoted 7 lines

No functional change.

---
CC: Jan Beulich <[email protected]>
CC: Roger Pau Monné <[email protected]>
CC: Daniel P. Smith <[email protected]>