svn commit: r359950 - head/usr.sbin/bhyve

Conrad Meyer cem at FreeBSD.org
Wed Apr 15 02:00:19 UTC 2020


Author: cem
Date: Wed Apr 15 02:00:17 2020
New Revision: 359950
URL: https://svnweb.freebsd.org/changeset/base/359950

Log:
  bhyve(8): Add VM Generation Counter ACPI device
  
  Add an implementatation of the 'Virtual Machine Generation ID' spec to
  Bhyve.  The spec provides a randomly generated GUID (at bhyve start) in
  device memory, along with an ACPI device with _CID VM_Gen_Counter and ADDR
  evaluating to a Package pointing at that GUID.
  
  A GPE is defined which Notifies the ACPI Device when the generation changes
  (such as when a snapshot is rolled back).  At this time, Bhyve does not
  support snapshotting, so the GPE is never actually raised.
  
  Suggested by:	rpokala
  Discussed with:	grehan
  Differential Revision:	https://reviews.freebsd.org/D23165

Added:
  head/usr.sbin/bhyve/vmgenc.c   (contents, props changed)
  head/usr.sbin/bhyve/vmgenc.h   (contents, props changed)
Modified:
  head/usr.sbin/bhyve/Makefile
  head/usr.sbin/bhyve/acpi.c
  head/usr.sbin/bhyve/acpi.h
  head/usr.sbin/bhyve/bhyverun.c
  head/usr.sbin/bhyve/pm.c

Modified: head/usr.sbin/bhyve/Makefile
==============================================================================
--- head/usr.sbin/bhyve/Makefile	Wed Apr 15 01:58:51 2020	(r359949)
+++ head/usr.sbin/bhyve/Makefile	Wed Apr 15 02:00:17 2020	(r359950)
@@ -67,6 +67,7 @@ SRCS=	\
 	usb_mouse.c		\
 	virtio.c		\
 	vga.c			\
+	vmgenc.c		\
 	xmsr.c			\
 	spinup_ap.c		\
 	iov.c

Modified: head/usr.sbin/bhyve/acpi.c
==============================================================================
--- head/usr.sbin/bhyve/acpi.c	Wed Apr 15 01:58:51 2020	(r359949)
+++ head/usr.sbin/bhyve/acpi.c	Wed Apr 15 02:00:17 2020	(r359950)
@@ -74,6 +74,7 @@ __FBSDID("$FreeBSD$");
 #include "bhyverun.h"
 #include "acpi.h"
 #include "pci_emul.h"
+#include "vmgenc.h"
 
 /*
  * Define the base address of the ACPI tables, the sizes of some tables, 
@@ -367,13 +368,13 @@ basl_fwrite_fadt(FILE *fp)
 	EFPRINTF(fp, "[0004]\t\tPM2 Control Block Address : 00000000\n");
 	EFPRINTF(fp, "[0004]\t\tPM Timer Block Address : %08X\n",
 	    IO_PMTMR);
-	EFPRINTF(fp, "[0004]\t\tGPE0 Block Address : 00000000\n");
+	EFPRINTF(fp, "[0004]\t\tGPE0 Block Address : %08X\n", IO_GPE0_BLK);
 	EFPRINTF(fp, "[0004]\t\tGPE1 Block Address : 00000000\n");
 	EFPRINTF(fp, "[0001]\t\tPM1 Event Block Length : 04\n");
 	EFPRINTF(fp, "[0001]\t\tPM1 Control Block Length : 02\n");
 	EFPRINTF(fp, "[0001]\t\tPM2 Control Block Length : 00\n");
 	EFPRINTF(fp, "[0001]\t\tPM Timer Block Length : 04\n");
-	EFPRINTF(fp, "[0001]\t\tGPE0 Block Length : 00\n");
+	EFPRINTF(fp, "[0001]\t\tGPE0 Block Length : %02x\n", IO_GPE0_LEN);
 	EFPRINTF(fp, "[0001]\t\tGPE1 Block Length : 00\n");
 	EFPRINTF(fp, "[0001]\t\tGPE1 Base Offset : 00\n");
 	EFPRINTF(fp, "[0001]\t\t_CST Support : 00\n");
@@ -501,10 +502,10 @@ basl_fwrite_fadt(FILE *fp)
 
 	EFPRINTF(fp, "[0012]\t\tGPE0 Block : [Generic Address Structure]\n");
 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
-	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
+	EFPRINTF(fp, "[0001]\t\tBit Width : %02x\n", IO_GPE0_LEN * 8);
 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
-	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
+	EFPRINTF(fp, "[0008]\t\tAddress : %016X\n", IO_GPE0_BLK);
 	EFPRINTF(fp, "\n");
 
 	EFPRINTF(fp, "[0012]\t\tGPE1 Block : [Generic Address Structure]\n");
@@ -756,6 +757,9 @@ basl_fwrite_dsdt(FILE *fp)
 	dsdt_line("      })");
 	dsdt_line("    }");
 	dsdt_line("  }");
+
+	vmgenc_write_dsdt();
+
 	dsdt_line("}");
 
 	if (dsdt_error != 0)

Modified: head/usr.sbin/bhyve/acpi.h
==============================================================================
--- head/usr.sbin/bhyve/acpi.h	Wed Apr 15 01:58:51 2020	(r359949)
+++ head/usr.sbin/bhyve/acpi.h	Wed Apr 15 02:00:17 2020	(r359950)
@@ -42,9 +42,19 @@
 
 #define	IO_PMTMR		0x408	/* 4-byte i/o port for the timer */
 
+#define	IO_GPE0_BLK		0x40c	/* 2x 1-byte IO port for GPE0_STS/EN */
+#define	IO_GPE0_LEN		0x2
+
+#define	IO_GPE0_STS		IO_GPE0_BLK
+#define	IO_GPE0_EN		(IO_GPE0_BLK + (IO_GPE0_LEN / 2))
+
+/* Allocated GPE bits. */
+#define	GPE_VMGENC		0
+
 struct vmctx;
 
 int	acpi_build(struct vmctx *ctx, int ncpu);
+void	acpi_raise_gpe(struct vmctx *ctx, unsigned bit);
 void	dsdt_line(const char *fmt, ...);
 void	dsdt_fixed_ioport(uint16_t iobase, uint16_t length);
 void	dsdt_fixed_irq(uint8_t irq);

Modified: head/usr.sbin/bhyve/bhyverun.c
==============================================================================
--- head/usr.sbin/bhyve/bhyverun.c	Wed Apr 15 01:58:51 2020	(r359949)
+++ head/usr.sbin/bhyve/bhyverun.c	Wed Apr 15 02:00:17 2020	(r359950)
@@ -85,6 +85,7 @@ __FBSDID("$FreeBSD$");
 #include "xmsr.h"
 #include "spinup_ap.h"
 #include "rtc.h"
+#include "vmgenc.h"
 
 #define GUEST_NIO_PORT		0x488	/* guest upcalls via i/o port */
 
@@ -1173,6 +1174,13 @@ main(int argc, char *argv[])
 		perror("device emulation initialization error");
 		exit(4);
 	}
+
+	/*
+	 * Initialize after PCI, to allow a bootrom file to reserve the high
+	 * region.
+	 */
+	if (acpi)
+		vmgenc_init(ctx);
 
 	if (dbg_port != 0)
 		init_dbgport(dbg_port);

Modified: head/usr.sbin/bhyve/pm.c
==============================================================================
--- head/usr.sbin/bhyve/pm.c	Wed Apr 15 01:58:51 2020	(r359949)
+++ head/usr.sbin/bhyve/pm.c	Wed Apr 15 02:00:17 2020	(r359950)
@@ -49,6 +49,10 @@ static pthread_mutex_t pm_lock = PTHREAD_MUTEX_INITIAL
 static struct mevent *power_button;
 static sig_t old_power_handler;
 
+static unsigned gpe0_active;
+static unsigned gpe0_enabled;
+static const unsigned gpe0_valid = (1u << GPE_VMGENC);
+
 /*
  * Reset Control register at I/O port 0xcf9.  Bit 2 forces a system
  * reset when it transitions from 0 to 1.  Bit 1 selects the type of
@@ -144,6 +148,9 @@ sci_update(struct vmctx *ctx)
 		need_sci = 1;
 	if ((pm1_enable & PM1_RTC_EN) && (pm1_status & PM1_RTC_STS))
 		need_sci = 1;
+	if ((gpe0_enabled & gpe0_active) != 0)
+		need_sci = 1;
+
 	if (need_sci)
 		sci_assert(ctx);
 	else
@@ -261,6 +268,64 @@ pm1_control_handler(struct vmctx *ctx, int vcpu, int i
 }
 INOUT_PORT(pm1_control, PM1A_CNT_ADDR, IOPORT_F_INOUT, pm1_control_handler);
 SYSRES_IO(PM1A_EVT_ADDR, 8);
+
+void
+acpi_raise_gpe(struct vmctx *ctx, unsigned bit)
+{
+	unsigned mask;
+
+	assert(bit < (IO_GPE0_LEN * (8 / 2)));
+	mask = (1u << bit);
+	assert((mask & ~gpe0_valid) == 0);
+
+	pthread_mutex_lock(&pm_lock);
+	gpe0_active |= mask;
+	sci_update(ctx);
+	pthread_mutex_unlock(&pm_lock);
+}
+
+static int
+gpe0_sts(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+    uint32_t *eax, void *arg)
+{
+	/*
+	 * ACPI 6.2 specifies the GPE register blocks are accessed
+	 * byte-at-a-time.
+	 */
+	if (bytes != 1)
+		return (-1);
+
+	pthread_mutex_lock(&pm_lock);
+	if (in)
+		*eax = gpe0_active;
+	else {
+		/* W1C */
+		gpe0_active &= ~(*eax & gpe0_valid);
+		sci_update(ctx);
+	}
+	pthread_mutex_unlock(&pm_lock);
+	return (0);
+}
+INOUT_PORT(gpe0_sts, IO_GPE0_STS, IOPORT_F_INOUT, gpe0_sts);
+
+static int
+gpe0_en(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
+    uint32_t *eax, void *arg)
+{
+	if (bytes != 1)
+		return (-1);
+
+	pthread_mutex_lock(&pm_lock);
+	if (in)
+		*eax = gpe0_enabled;
+	else {
+		gpe0_enabled = (*eax & gpe0_valid);
+		sci_update(ctx);
+	}
+	pthread_mutex_unlock(&pm_lock);
+	return (0);
+}
+INOUT_PORT(gpe0_en, IO_GPE0_EN, IOPORT_F_INOUT, gpe0_en);
 
 /*
  * ACPI SMI Command Register

Added: head/usr.sbin/bhyve/vmgenc.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bhyve/vmgenc.c	Wed Apr 15 02:00:17 2020	(r359950)
@@ -0,0 +1,119 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright 2020 Conrad Meyer <cem at FreeBSD.org>.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/mman.h>
+#include <sys/uuid.h>
+
+#include <assert.h>
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <stdbool.h>
+#include <unistd.h>
+
+#include <machine/vmm.h>
+#include <vmmapi.h>
+
+#include "acpi.h"
+#include "bootrom.h"
+#include "vmgenc.h"
+
+static uint64_t	vmgen_gpa;
+
+void
+vmgenc_init(struct vmctx *ctx)
+{
+	char *region;
+	int error;
+
+	error = bootrom_alloc(ctx, PAGE_SIZE, PROT_READ, 0, &region,
+	    &vmgen_gpa);
+	if (error != 0)
+		errx(4, "%s: bootrom_alloc", __func__);
+
+	/*
+	 * It is basically harmless to always generate a random ID when
+	 * starting a VM.
+	 */
+	error = getentropy(region, sizeof(struct uuid));
+	if (error == -1)
+		err(4, "%s: getentropy", __func__);
+
+	/* XXX When we have suspend/resume/rollback. */
+#if 0
+	acpi_raise_gpe(ctx, GPE_VMGENC);
+#endif
+}
+
+void
+vmgenc_write_dsdt(void)
+{
+	dsdt_line("");
+	dsdt_indent(1);
+	dsdt_line("Scope (_SB)");
+	dsdt_line("{");
+
+	dsdt_line("  Device (GENC)");
+	dsdt_line("  {");
+
+	dsdt_indent(2);
+	dsdt_line("Name (_CID, \"VM_Gen_Counter\")");
+	dsdt_line("Method (_HID, 0, NotSerialized)");
+	dsdt_line("{");
+	dsdt_line("  Return (\"Bhyve_V_Gen_Counter_V1\")");
+	dsdt_line("}");
+	dsdt_line("Name (_UID, 0)");
+	dsdt_line("Name (_DDN, \"VM_Gen_Counter\")");
+	dsdt_line("Name (ADDR, Package (0x02)");
+	dsdt_line("{");
+	dsdt_line("  0x%08x,", (uint32_t)vmgen_gpa);
+	dsdt_line("  0x%08x", (uint32_t)(vmgen_gpa >> 32));
+	dsdt_line("})");
+
+	dsdt_unindent(2);
+	dsdt_line("  }");	/* Device (GENC) */
+
+	dsdt_line("}");		/* Scope (_SB) */
+	dsdt_line("");
+
+	dsdt_line("Scope (_GPE)");
+	dsdt_line("{");
+	dsdt_line("  Method (_E%02x, 0, NotSerialized)", GPE_VMGENC);
+	dsdt_line("  {");
+	dsdt_line("    Notify (\\_SB.GENC, 0x80)");
+	dsdt_line("  }");
+	dsdt_line("}");
+	dsdt_unindent(1);
+}

Added: head/usr.sbin/bhyve/vmgenc.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/usr.sbin/bhyve/vmgenc.h	Wed Apr 15 02:00:17 2020	(r359950)
@@ -0,0 +1,31 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
+ *
+ * Copyright 2020 Conrad Meyer <cem at FreeBSD.org>.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+#pragma once
+void	vmgenc_init(struct vmctx *);
+void	vmgenc_write_dsdt(void);


More information about the svn-src-head mailing list