git: 9acf6422208a - main - kboot/amd64: Use common routines for memory map parsing

From: Warner Losh <imp_at_FreeBSD.org>
Date: Thu, 17 Apr 2025 21:59:18 UTC
The branch main has been updated by imp:

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

commit 9acf6422208a7b43b2ba7d3e66f13f2874d4a3aa
Author:     Warner Losh <imp@FreeBSD.org>
AuthorDate: 2025-04-17 04:04:39 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2025-04-17 21:56:46 +0000

    kboot/amd64: Use common routines for memory map parsing
    
    Use populate_avail_from_iomem and efi_read_from_sysfs from the
    refactored work rather than replicating them (imperfectly) here.
    
    Note: memmap might need to be revisited. EFI memory maps are complex on
    x86 and we might need to reconstruct it from /sys/firmware/memmap as
    well as using that for the BIOS case, should we ever want to support
    that again (hardware makes no sense, but many VM hosting services use
    that). For now, we're going all in on EFI, though, and will revisit what
    to do about BIOS later. The zfsboot project suggests BIOS support isn't
    really that hard (but is a distraction atm).
    
    Sponsored by:           Netflix
    Reviewed by:            kevans, jhibbits
    Differential Revision:  https://reviews.freebsd.org/D49864
---
 stand/kboot/kboot/arch/amd64/load_addr.c | 111 ++-----------------------------
 1 file changed, 6 insertions(+), 105 deletions(-)

diff --git a/stand/kboot/kboot/arch/amd64/load_addr.c b/stand/kboot/kboot/arch/amd64/load_addr.c
index 4bd2a19dab48..ae53aa75da19 100644
--- a/stand/kboot/kboot/arch/amd64/load_addr.c
+++ b/stand/kboot/kboot/arch/amd64/load_addr.c
@@ -29,117 +29,18 @@
 
 #include "stand.h"
 #include "host_syscall.h"
+#include "efi.h"
 #include "kboot.h"
 #include "bootstrap.h"
 
-/* Refactor when we do arm64 */
-
-enum types {
-	system_ram = 1,
-	acpi_tables,
-	acpi_nv_storage,
-	unusable,
-	persistent_old,
-	persistent,
-	soft_reserved,
-	reserved,
-};
-
-struct kv
-{
-	uint64_t	type;
-	char *		name;
-} str2type_kv[] = {
-	{ system_ram,		"System RAM" },
-	{ acpi_tables,		"ACPI Tables" },
-	{ acpi_nv_storage,	"ACPI Non-volatile Storage" },
-	{ unusable,		"Unusable memory" },
-	{ persistent_old,	"Persistent Memory (legacy)" },
-	{ persistent,		"Persistent Memory" },
-	{ soft_reserved,	"Soft Reserved" },
-	{ reserved,		"reserved" },
-	{ 0, NULL },
-};
-
-#define MEMMAP "/sys/firmware/memmap"
-
-static struct memory_segments segs[64];	/* make dynamic later */
-static int nr_seg;
-
-static bool
-str2type(struct kv *kv, const char *buf, uint64_t *value)
-{
-	while (kv->name != NULL) {
-		if (strcmp(kv->name, buf) == 0) {
-			*value = kv->type;
-			return true;
-		}
-		kv++;
-	}
-
-	return false;
-}
-
 bool
 enumerate_memory_arch(void)
 {
-	int n;
-	char name[MAXPATHLEN];
-	char buf[80];
-
-	for (n = 0; n < nitems(segs); n++) {
-		snprintf(name, sizeof(name), "%s/%d/start", MEMMAP, n);
-		if (!file2u64(name, &segs[n].start))
-			break;
-		snprintf(name, sizeof(name), "%s/%d/end", MEMMAP, n);
-		if (!file2u64(name, &segs[n].end))
-			break;
-		snprintf(name, sizeof(name), "%s/%d/type", MEMMAP, n);
-		if (!file2str(name, buf, sizeof(buf)))
-			break;
-		if (!str2type(str2type_kv, buf, &segs[n].type))
-			break;
-	}
-
-	nr_seg = n;
-
-	return true;
-}
-
-#define BAD_SEG ~0ULL
-
-#define SZ(s) (((s).end - (s).start) + 1)
-
-static uint64_t
-find_ram(struct memory_segments *segs, int nr_seg, uint64_t minpa, uint64_t align,
-    uint64_t sz, uint64_t maxpa)
-{
-	uint64_t start;
-
-	printf("minpa %#jx align %#jx sz %#jx maxpa %#jx\n",
-	    (uintmax_t)minpa,
-	    (uintmax_t)align,
-	    (uintmax_t)sz,
-	    (uintmax_t)maxpa);
-	/* XXX assume segs are sorted in numeric order -- assumed not ensured */
-	for (int i = 0; i < nr_seg; i++) {
-		if (segs[i].type != system_ram ||
-		    SZ(segs[i]) < sz ||
-		    minpa + sz > segs[i].end ||
-		    maxpa < segs[i].start)
-			continue;
-		start = roundup(segs[i].start, align);
-		if (start < minpa)	/* Too small, round up and try again */
-			start = (roundup(minpa, align));
-		if (start + sz > segs[i].end)	/* doesn't fit in seg */
-			continue;
-		if (start > maxpa ||		/* Over the edge */
-		    start + sz > maxpa)		/* on the edge */
-			break;			/* No hope to continue */
-		return start;
-	}
-
-	return BAD_SEG;
+	efi_read_from_sysfs();
+	if (!populate_avail_from_iomem())
+		return (false);
+	print_avail();
+	return (true);
 }
 
 uint64_t