git: 3ef46195ac37 - main - bhyve: add helper to add fwcfg items

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Tue, 28 Feb 2023 12:38:36 UTC
The branch main has been updated by corvink:

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

commit 3ef46195ac37111ab247cfabed735d3ee5e7f5b5
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2021-08-11 08:01:19 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-02-28 12:37:03 +0000

    bhyve: add helper to add fwcfg items
    
    This helper makes it easier to add multiple fwcfg items. You can pass an
    index and some data to the helper. The helper adds these information to
    the fwcfg emulation so that the guest reads the given data on the
    specified index.
    
    Reviewed by:            <If someone else reviewed your modification.>
    MFC after:              1 week
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D38334
---
 usr.sbin/bhyve/qemu_fwcfg.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/usr.sbin/bhyve/qemu_fwcfg.c b/usr.sbin/bhyve/qemu_fwcfg.c
index 13e21daca7ff..8cb71b819956 100644
--- a/usr.sbin/bhyve/qemu_fwcfg.c
+++ b/usr.sbin/bhyve/qemu_fwcfg.c
@@ -130,6 +130,31 @@ qemu_fwcfg_data_port_handler(struct vmctx *const ctx __unused, const int in,
 	return (0);
 }
 
+static int
+qemu_fwcfg_add_item(const uint16_t architecture, const uint16_t index,
+    const uint32_t size, void *const data)
+{
+	/* truncate architecture and index to their desired size */
+	const uint16_t arch = architecture & QEMU_FWCFG_ARCHITECTURE_MASK;
+	const uint16_t idx = index & QEMU_FWCFG_INDEX_MASK;
+
+	/* get pointer to item specified by selector */
+	struct qemu_fwcfg_item *const fwcfg_item = &fwcfg_sc.items[arch][idx];
+
+	/* check if item is already used */
+	if (fwcfg_item->data != NULL) {
+		warnx("%s: qemu fwcfg item exists (architecture %s index 0x%x)",
+		    __func__, arch ? "specific" : "generic", idx);
+		return (-1);
+	}
+
+	/* save data of the item */
+	fwcfg_item->size = size;
+	fwcfg_item->data = data;
+
+	return (0);
+}
+
 static int
 qemu_fwcfg_register_port(const char *const name, const int port, const int size,
     const int flags, const inout_func_t handler)