The branch main has been updated by mhorne:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=a2e2178402afff939cb90e75c65ded0a9da8ac5c

commit a2e2178402afff939cb90e75c65ded0a9da8ac5c
Author:     Mitchell Horne <mho...@freebsd.org>
AuthorDate: 2025-03-03 15:49:17 +0000
Commit:     Mitchell Horne <mho...@freebsd.org>
CommitDate: 2025-03-03 16:12:15 +0000

    riscv: parse memory regions from EFI map header
    
    Using the newly isolated efi_map interface (subr_efi_map.c).
    
    The primary goal is a greater adherence to the EFI firmware's view of
    memory, and protection of its reserved regions. This is a better source
    of truth than the device tree.
    
    For example, regions marked as RuntimeServicesData must be excluded from
    general memory allocations for EFI runtime services to eventually be
    enabled on the platform (planned but not currently in progress).
    
    The secondary impact is when loading a new/custom device tree via
    loader(8); use of the EFI map ensures that reservations made by firmware
    (OpenSBI, u-boot) are maintained.
    
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D49131
---
 sys/conf/files.riscv      |  1 +
 sys/riscv/riscv/machdep.c | 63 ++++++++++++++++++++++++++++-------------------
 2 files changed, 38 insertions(+), 26 deletions(-)

diff --git a/sys/conf/files.riscv b/sys/conf/files.riscv
index cc18ecb6eb36..0d782239c89c 100644
--- a/sys/conf/files.riscv
+++ b/sys/conf/files.riscv
@@ -30,6 +30,7 @@ kern/msi_if.m                 standard
 kern/pic_if.m                  standard
 kern/subr_devmap.c             standard
 kern/subr_dummy_vdso_tc.c      standard
+kern/subr_efi_map.c            standard
 kern/subr_intr.c               standard
 kern/subr_physmem.c            standard
 libkern/bcopy.c                        standard
diff --git a/sys/riscv/riscv/machdep.c b/sys/riscv/riscv/machdep.c
index 8d32d348df7d..03f5926c3739 100644
--- a/sys/riscv/riscv/machdep.c
+++ b/sys/riscv/riscv/machdep.c
@@ -45,6 +45,7 @@
 #include <sys/cons.h>
 #include <sys/cpu.h>
 #include <sys/devmap.h>
+#include <sys/efi_map.h>
 #include <sys/exec.h>
 #include <sys/imgact.h>
 #include <sys/kdb.h>
@@ -468,6 +469,7 @@ void
 initriscv(struct riscv_bootparams *rvbp)
 {
        struct mem_region mem_regions[FDT_MEM_REGIONS];
+       struct efi_map_header *efihdr;
        struct pcpu *pcpup;
        int mem_regions_sz;
        vm_offset_t lastaddr;
@@ -516,21 +518,40 @@ initriscv(struct riscv_bootparams *rvbp)
        }
        pcpup->pc_hart = boot_hart;
 
-#ifdef FDT
-       /*
-        * Exclude reserved memory specified by the device tree. Typically,
-        * this contains an entry for memory used by the runtime SBI firmware.
-        */
-       if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0) {
-               physmem_exclude_regions(mem_regions, mem_regions_sz,
-                   EXFLAG_NODUMP | EXFLAG_NOALLOC);
+       efihdr = (struct efi_map_header *)preload_search_info(preload_kmdp,
+           MODINFO_METADATA | MODINFOMD_EFI_MAP);
+       if (efihdr != NULL) {
+               efi_map_add_entries(efihdr);
+               efi_map_exclude_entries(efihdr);
        }
+#ifdef FDT
+       else {
+               /* Exclude reserved memory specified by the device tree. */
+               if (fdt_get_reserved_mem(mem_regions, &mem_regions_sz) == 0) {
+                       physmem_exclude_regions(mem_regions, mem_regions_sz,
+                           EXFLAG_NODUMP | EXFLAG_NOALLOC);
+               }
 
-       /* Grab physical memory regions information from device tree. */
-       if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 0) {
-               panic("Cannot get physical memory regions");
+               /* Grab physical memory regions information from device tree. */
+               if (fdt_get_mem_regions(mem_regions, &mem_regions_sz, NULL) != 
0)
+                       panic("Cannot get physical memory regions");
+               physmem_hardware_regions(mem_regions, mem_regions_sz);
+
+               /*
+                * XXX: Unconditionally exclude the lowest 2MB of physical
+                * memory, as this area is assumed to contain the SBI firmware,
+                * and this is not properly reserved in all cases (e.g. in
+                * older firmware like BBL).
+                *
+                * This is a little fragile, but it is consistent with the
+                * platforms we support so far.
+                *
+                * TODO: remove this when the all regular booting methods
+                * properly report their reserved memory in the device tree.
+                */
+               physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE,
+                   EXFLAG_NODUMP | EXFLAG_NOALLOC);
        }
-       physmem_hardware_regions(mem_regions, mem_regions_sz);
 #endif
 
        /*
@@ -541,19 +562,6 @@ initriscv(struct riscv_bootparams *rvbp)
        /* Do basic tuning, hz etc */
        init_param1();
 
-#ifdef FDT
-       /*
-        * XXX: Unconditionally exclude the lowest 2MB of physical memory, as
-        * this area is assumed to contain the SBI firmware. This is a little
-        * fragile, but it is consistent with the platforms we support so far.
-        *
-        * TODO: remove this when the all regular booting methods properly
-        * report their reserved memory in the device tree.
-        */
-       physmem_exclude_region(mem_regions[0].mr_start, L2_SIZE,
-           EXFLAG_NODUMP | EXFLAG_NOALLOC);
-#endif
-
        /* Bootstrap enough of pmap to enter the kernel proper */
        kernlen = (lastaddr - KERNBASE);
        pmap_bootstrap(rvbp->kern_phys, kernlen);
@@ -588,8 +596,11 @@ initriscv(struct riscv_bootparams *rvbp)
        if (env != NULL)
                strlcpy(kernelname, env, sizeof(kernelname));
 
-       if (boothowto & RB_VERBOSE)
+       if (boothowto & RB_VERBOSE) {
+               if (efihdr != NULL)
+                       efi_map_print_entries(efihdr);
                physmem_print_tables();
+       }
 
        early_boot = 0;
 

Reply via email to