git: d5edf13d013f - main - bhyve: add basic TPM device

From: Corvin Köhne <corvink_at_FreeBSD.org>
Date: Mon, 12 Jun 2023 11:04:45 UTC
The branch main has been updated by corvink:

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

commit d5edf13d013f3298e7f703c406223feb84810a73
Author:     Corvin Köhne <corvink@FreeBSD.org>
AuthorDate: 2023-05-15 11:00:06 +0000
Commit:     Corvin Köhne <corvink@FreeBSD.org>
CommitDate: 2023-06-12 11:04:35 +0000

    bhyve: add basic TPM device
    
    Add an empty TPM device struct which will be used for TPM emulation in
    subsequent commits.
    
    Reviewed by:            markj
    MFC after:              1 week
    Sponsored by:           Beckhoff Automation GmbH & Co. KG
    Differential Revision:  https://reviews.freebsd.org/D40452
---
 usr.sbin/bhyve/Makefile     |  1 +
 usr.sbin/bhyve/tpm_device.c | 68 +++++++++++++++++++++++++++++++++++++++++++++
 usr.sbin/bhyve/tpm_device.h | 18 ++++++++++++
 3 files changed, 87 insertions(+)

diff --git a/usr.sbin/bhyve/Makefile b/usr.sbin/bhyve/Makefile
index 65a32d2fb0b9..ae1e82e7b828 100644
--- a/usr.sbin/bhyve/Makefile
+++ b/usr.sbin/bhyve/Makefile
@@ -72,6 +72,7 @@ SRCS=	\
 	smbiostbl.c		\
 	sockstream.c		\
 	task_switch.c		\
+	tpm_device.c		\
 	uart_emul.c		\
 	usb_emul.c		\
 	usb_mouse.c		\
diff --git a/usr.sbin/bhyve/tpm_device.c b/usr.sbin/bhyve/tpm_device.c
new file mode 100644
index 000000000000..77fd1ccf6a52
--- /dev/null
+++ b/usr.sbin/bhyve/tpm_device.c
@@ -0,0 +1,68 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Beckhoff Automation GmbH & Co. KG
+ * Author: Corvin Köhne <corvink@FreeBSD.org>
+ */
+
+#include <sys/types.h>
+
+#include <err.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <vmmapi.h>
+
+#include "config.h"
+#include "tpm_device.h"
+
+struct tpm_device {
+	struct vmctx *vm_ctx;
+};
+
+void
+tpm_device_destroy(struct tpm_device *const dev)
+{
+	if (dev == NULL)
+		return;
+
+	free(dev);
+}
+
+int
+tpm_device_create(struct tpm_device **const new_dev, struct vmctx *const vm_ctx,
+    nvlist_t *const nvl)
+{
+	struct tpm_device *dev = NULL;
+	const char *value;
+	int error;
+
+	if (new_dev == NULL || vm_ctx == NULL) {
+		error = EINVAL;
+		goto err_out;
+	}
+
+	value = get_config_value_node(nvl, "version");
+	if (value == NULL || strcmp(value, "2.0")) {
+		warnx("%s: unsupported tpm version %s", __func__, value);
+		error = EINVAL;
+		goto err_out;
+	}
+
+	dev = calloc(1, sizeof(*dev));
+	if (dev == NULL) {
+		error = ENOMEM;
+		goto err_out;
+	}
+
+	dev->vm_ctx = vm_ctx;
+
+	*new_dev = dev;
+
+	return (0);
+
+err_out:
+	tpm_device_destroy(dev);
+
+	return (error);
+}
diff --git a/usr.sbin/bhyve/tpm_device.h b/usr.sbin/bhyve/tpm_device.h
new file mode 100644
index 000000000000..a17c85c2ed47
--- /dev/null
+++ b/usr.sbin/bhyve/tpm_device.h
@@ -0,0 +1,18 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2023 Beckhoff Automation GmbH & Co. KG
+ * Author: Corvin Köhne <corvink@FreeBSD.org>
+ */
+
+#pragma once
+
+#include <vmmapi.h>
+
+#include "config.h"
+
+struct tpm_device;
+
+int tpm_device_create(struct tpm_device **new_dev, struct vmctx *vm_ctx,
+    nvlist_t *nvl);
+void tpm_device_destroy(struct tpm_device *dev);