git: 4fe5b70cae67 - main - bhyve: Move more amd64-specific code under amd64/

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Wed, 04 Oct 2023 16:53:57 UTC
The branch main has been updated by markj:

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

commit 4fe5b70cae6761b9205ff35b72ccfe60d7326301
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2023-10-04 16:21:20 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2023-10-04 16:53:16 +0000

    bhyve: Move more amd64-specific code under amd64/
    
    mptable and the e820 are both rather amd64-specific and can be moved
    easily.
    
    In the case of e820, move the registration with qemu_fwcfg into e820.c,
    as it simplifies bhyverun.c a bit and I can't see any downsides.
    
    No functional change intended.
    
    Reviewed by:    corvink, jhb, emaste
    MFC after:      1 week
    Sponsored by:   Innovate UK
    Differential Revision:  https://reviews.freebsd.org/D40552
---
 usr.sbin/bhyve/Makefile            |  2 --
 usr.sbin/bhyve/amd64/Makefile.inc  |  2 ++
 usr.sbin/bhyve/{ => amd64}/e820.c  | 24 +++++++++++++++++++++++-
 usr.sbin/bhyve/{ => amd64}/e820.h  |  2 +-
 usr.sbin/bhyve/{ => amd64}/mptbl.c |  0
 usr.sbin/bhyve/{ => amd64}/mptbl.h |  0
 usr.sbin/bhyve/bhyverun.c          | 30 ++++++++++++++----------------
 usr.sbin/bhyve/pci_gvt-d.c         |  2 +-
 usr.sbin/bhyve/snapshot.c          |  1 -
 9 files changed, 41 insertions(+), 22 deletions(-)

diff --git a/usr.sbin/bhyve/Makefile b/usr.sbin/bhyve/Makefile
index 86039a8c45da..499c0d1bb89d 100644
--- a/usr.sbin/bhyve/Makefile
+++ b/usr.sbin/bhyve/Makefile
@@ -28,7 +28,6 @@ SRCS=	\
 	crc16.c			\
 	ctl_scsi_all.c		\
 	ctl_util.c		\
-	e820.c			\
 	gdb.c			\
 	hda_codec.c		\
 	inout.c			\
@@ -37,7 +36,6 @@ SRCS=	\
 	kernemu_dev.c		\
 	mem.c			\
 	mevent.c		\
-	mptbl.c			\
 	net_backends.c		\
 	net_utils.c		\
 	pci_ahci.c		\
diff --git a/usr.sbin/bhyve/amd64/Makefile.inc b/usr.sbin/bhyve/amd64/Makefile.inc
index c52219cb8dd1..e0cb84e32b85 100644
--- a/usr.sbin/bhyve/amd64/Makefile.inc
+++ b/usr.sbin/bhyve/amd64/Makefile.inc
@@ -1,6 +1,8 @@
 SRCS+=	\
 	atkbdc.c	\
+	e820.c		\
 	fwctl.c		\
+	mptbl.c		\
 	post.c		\
 	ps2kbd.c	\
 	ps2mouse.c	\
diff --git a/usr.sbin/bhyve/e820.c b/usr.sbin/bhyve/amd64/e820.c
similarity index 95%
rename from usr.sbin/bhyve/e820.c
rename to usr.sbin/bhyve/amd64/e820.c
index 99a66645f70f..545878aad39f 100644
--- a/usr.sbin/bhyve/e820.c
+++ b/usr.sbin/bhyve/amd64/e820.c
@@ -105,7 +105,7 @@ e820_dump_table(void)
 	}
 }
 
-struct qemu_fwcfg_item *
+static struct qemu_fwcfg_item *
 e820_get_fwcfg_item(void)
 {
 	struct qemu_fwcfg_item *fwcfg_item;
@@ -466,3 +466,25 @@ e820_init(struct vmctx *const ctx)
 
 	return (0);
 }
+
+int
+e820_finalize(void)
+{
+	struct qemu_fwcfg_item *e820_fwcfg_item;
+	int error;
+
+	e820_fwcfg_item = e820_get_fwcfg_item();
+	if (e820_fwcfg_item == NULL) {
+		warnx("invalid e820 table");
+		return (ENOMEM);
+	}
+	error = qemu_fwcfg_add_file("etc/e820",
+	    e820_fwcfg_item->size, e820_fwcfg_item->data);
+	if (error != 0) {
+		warnx("could not add qemu fwcfg etc/e820");
+		return (error);
+	}
+	free(e820_fwcfg_item);
+
+	return (0);
+}
diff --git a/usr.sbin/bhyve/e820.h b/usr.sbin/bhyve/amd64/e820.h
similarity index 95%
rename from usr.sbin/bhyve/e820.h
rename to usr.sbin/bhyve/amd64/e820.h
index 8703a55115cd..ae68fe9040a9 100644
--- a/usr.sbin/bhyve/e820.h
+++ b/usr.sbin/bhyve/amd64/e820.h
@@ -41,5 +41,5 @@ uint64_t e820_alloc(const uint64_t address, const uint64_t length,
     const uint64_t alignment, const enum e820_memory_type type,
     const enum e820_allocation_strategy strategy);
 void e820_dump_table(void);
-struct qemu_fwcfg_item *e820_get_fwcfg_item(void);
 int e820_init(struct vmctx *const ctx);
+int e820_finalize(void);
diff --git a/usr.sbin/bhyve/mptbl.c b/usr.sbin/bhyve/amd64/mptbl.c
similarity index 100%
rename from usr.sbin/bhyve/mptbl.c
rename to usr.sbin/bhyve/amd64/mptbl.c
diff --git a/usr.sbin/bhyve/mptbl.h b/usr.sbin/bhyve/amd64/mptbl.h
similarity index 100%
rename from usr.sbin/bhyve/mptbl.h
rename to usr.sbin/bhyve/amd64/mptbl.h
diff --git a/usr.sbin/bhyve/bhyverun.c b/usr.sbin/bhyve/bhyverun.c
index 707e21caf506..7672c577da66 100644
--- a/usr.sbin/bhyve/bhyverun.c
+++ b/usr.sbin/bhyve/bhyverun.c
@@ -89,8 +89,8 @@
 #include "config.h"
 #include "inout.h"
 #include "debug.h"
-#include "e820.h"
 #ifdef __amd64__
+#include "amd64/e820.h"
 #include "amd64/fwctl.h"
 #endif
 #include "gdb.h"
@@ -98,7 +98,9 @@
 #include "kernemu_dev.h"
 #include "mem.h"
 #include "mevent.h"
-#include "mptbl.h"
+#ifdef __amd64__
+#include "amd64/mptbl.h"
+#endif
 #include "pci_emul.h"
 #include "pci_irq.h"
 #include "pci_lpc.h"
@@ -1221,7 +1223,6 @@ main(int argc, char *argv[])
 	int max_vcpus, memflags;
 	struct vcpu *bsp;
 	struct vmctx *ctx;
-	struct qemu_fwcfg_item *e820_fwcfg_item;
 	size_t memsize;
 	const char *optstr, *value, *vmname;
 #ifdef BHYVE_SNAPSHOT
@@ -1349,9 +1350,11 @@ main(int argc, char *argv[])
 		case 'x':
 			set_config_bool("x86.x2apic", true);
 			break;
+#ifdef __amd64__
 		case 'Y':
 			set_config_bool("x86.mptable", false);
 			break;
+#endif
 		case 'h':
 			usage(0);
 		default:
@@ -1476,10 +1479,12 @@ main(int argc, char *argv[])
 		exit(4);
 	}
 
+#ifdef __amd64__
 	if (e820_init(ctx) != 0) {
 		fprintf(stderr, "Unable to setup E820");
 		exit(4);
 	}
+#endif
 
 	/*
 	 * Exit if a device emulation finds an error in its initialization
@@ -1552,9 +1557,7 @@ main(int argc, char *argv[])
 	}
 #endif
 
-	/*
-	 * build the guest tables, MP etc.
-	 */
+#ifdef __amd64__
 	if (get_config_bool_default("x86.mptable", true)) {
 		error = mptable_build(ctx, guest_ncpus);
 		if (error) {
@@ -1562,6 +1565,7 @@ main(int argc, char *argv[])
 			exit(4);
 		}
 	}
+#endif
 
 	error = smbios_build(ctx);
 	if (error != 0)
@@ -1572,17 +1576,11 @@ main(int argc, char *argv[])
 		assert(error == 0);
 	}
 
-	e820_fwcfg_item = e820_get_fwcfg_item();
-	if (e820_fwcfg_item == NULL) {
-	    fprintf(stderr, "invalid e820 table");
-		exit(4);
-	}
-	if (qemu_fwcfg_add_file("etc/e820", e820_fwcfg_item->size,
-		e820_fwcfg_item->data) != 0) {
-		fprintf(stderr, "could not add qemu fwcfg etc/e820");
+#ifdef __amd64__
+	error = e820_finalize();
+	if (error != 0)
 		exit(4);
-	}
-	free(e820_fwcfg_item);
+#endif
 
 #ifdef __amd64__
 	if (lpc_bootrom() && strcmp(lpc_fwcfg(), "bhyve") == 0) {
diff --git a/usr.sbin/bhyve/pci_gvt-d.c b/usr.sbin/bhyve/pci_gvt-d.c
index 78fa9878358e..f64cc5984352 100644
--- a/usr.sbin/bhyve/pci_gvt-d.c
+++ b/usr.sbin/bhyve/pci_gvt-d.c
@@ -17,7 +17,7 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "e820.h"
+#include "amd64/e820.h"
 #include "pci_gvt-d-opregion.h"
 #include "pci_passthru.h"
 
diff --git a/usr.sbin/bhyve/snapshot.c b/usr.sbin/bhyve/snapshot.c
index 593930c7f8a3..2daef0ef74f0 100644
--- a/usr.sbin/bhyve/snapshot.c
+++ b/usr.sbin/bhyve/snapshot.c
@@ -84,7 +84,6 @@
 #include "ioapic.h"
 #include "mem.h"
 #include "mevent.h"
-#include "mptbl.h"
 #include "pci_emul.h"
 #include "pci_irq.h"
 #include "pci_lpc.h"