From owner-svn-src-head@freebsd.org Sun Dec 22 06:25:21 2019 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id EE2661CA057; Sun, 22 Dec 2019 06:25:21 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47gXXd5zXRz45mK; Sun, 22 Dec 2019 06:25:21 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id C3AFE236E0; Sun, 22 Dec 2019 06:25:21 +0000 (UTC) (envelope-from cem@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id xBM6PLkR069189; Sun, 22 Dec 2019 06:25:21 GMT (envelope-from cem@FreeBSD.org) Received: (from cem@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id xBM6PLME069187; Sun, 22 Dec 2019 06:25:21 GMT (envelope-from cem@FreeBSD.org) Message-Id: <201912220625.xBM6PLME069187@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: cem set sender to cem@FreeBSD.org using -f From: Conrad Meyer Date: Sun, 22 Dec 2019 06:25:21 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r356001 - in head/sys: conf dev/vmgenc X-SVN-Group: head X-SVN-Commit-Author: cem X-SVN-Commit-Paths: in head/sys: conf dev/vmgenc X-SVN-Commit-Revision: 356001 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 22 Dec 2019 06:25:22 -0000 Author: cem Date: Sun Dec 22 06:25:20 2019 New Revision: 356001 URL: https://svnweb.freebsd.org/changeset/base/356001 Log: Add vmgenc(4) driver for ACPI VM generation counter The VM generation counter is a 128-bit value exposed by the BIOS via ACPI. The value changes to another unique identifier whenever a VM is duplicated. Additionally, ACPI provides notification events when such events occur. The driver decodes the pointer to the UUID, exports the value to userspace via OPAQUE sysctl blob, and forwards the ACPI notifications in the form of an EVENTHANDLER invocation as well as userspace devctl events. See design paper: https://go.microsoft.com/fwlink/p/?LinkID=260709 Added: head/sys/dev/vmgenc/ head/sys/dev/vmgenc/vmgenc_acpi.c (contents, props changed) head/sys/dev/vmgenc/vmgenc_acpi.h (contents, props changed) Modified: head/sys/conf/files Modified: head/sys/conf/files ============================================================================== --- head/sys/conf/files Sun Dec 22 05:44:29 2019 (r356000) +++ head/sys/conf/files Sun Dec 22 06:25:20 2019 (r356001) @@ -3398,6 +3398,7 @@ dev/virtio/scsi/virtio_scsi.c optional virtio_scsi dev/virtio/random/virtio_random.c optional virtio_random dev/virtio/console/virtio_console.c optional virtio_console dev/vkbd/vkbd.c optional vkbd +dev/vmgenc/vmgenc_acpi.c optional acpi dev/vr/if_vr.c optional vr pci dev/vt/colors/vt_termcolors.c optional vt dev/vt/font/vt_font_default.c optional vt Added: head/sys/dev/vmgenc/vmgenc_acpi.c ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/vmgenc/vmgenc_acpi.c Sun Dec 22 06:25:20 2019 (r356001) @@ -0,0 +1,240 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2019 Conrad Meyer . 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR 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. + */ + +/* + * VM Generation Counter driver + * + * See, e.g., the "Virtual Machine Generation ID" white paper: + * https://go.microsoft.com/fwlink/p/?LinkID=260709 , and perhaps also: + * https://docs.microsoft.com/en-us/windows/win32/hyperv_v2/virtual-machine-generation-identifier , + * https://azure.microsoft.com/en-us/blog/accessing-and-using-azure-vm-unique-id/ + * + * Microsoft introduced the concept in 2013 or so and seems to have + * successfully driven it to a consensus standard among hypervisors, not just + * HyperV/Azure: + * - QEMU: https://bugzilla.redhat.com/show_bug.cgi?id=1118834 + * - VMware/ESXi: https://kb.vmware.com/s/article/2032586 + * - Xen: https://github.com/xenserver/xen-4.5/blob/master/tools/firmware/hvmloader/acpi/dsdt.asl#L456 + */ + +#include +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include + +#ifndef ACPI_NOTIFY_STATUS_CHANGED +#define ACPI_NOTIFY_STATUS_CHANGED 0x80 +#endif + +#define GUID_BYTES 16 + +static const char *vmgenc_ids[] = { + "VM_GEN_COUNTER", + NULL +}; +#if 0 +MODULE_PNP_INFO("Z:_CID", acpi, vmgenc, vmgenc_ids, nitems(vmgenc_ids) - 1); +#endif + +struct vmgenc_softc { + volatile void *vmg_pguid; + uint8_t vmg_cache_guid[GUID_BYTES]; +}; + +static void +vmgenc_status_changed(void *context) +{ + uint8_t guid[GUID_BYTES]; + struct vmgenc_softc *sc; + device_t dev; + + dev = context; + sc = device_get_softc(dev); + + /* Check for spurious notify events. */ + memcpy(guid, __DEVOLATILE(void *, sc->vmg_pguid), sizeof(guid)); + if (memcmp(guid, sc->vmg_cache_guid, GUID_BYTES) == 0) + return; /* No change. */ + + /* Update cache. */ + memcpy(sc->vmg_cache_guid, guid, GUID_BYTES); + + EVENTHANDLER_INVOKE(acpi_vmgenc_event); + acpi_UserNotify("VMGenerationCounter", acpi_get_handle(dev), 0); +} + +static void +vmgenc_notify(ACPI_HANDLE h, UINT32 notify, void *context) +{ + device_t dev; + + dev = context; + switch (notify) { + case ACPI_NOTIFY_STATUS_CHANGED: + /* + * We're possibly in GPE / interrupt context, kick the event up + * to a taskqueue. + */ + AcpiOsExecute(OSL_NOTIFY_HANDLER, vmgenc_status_changed, dev); + break; + default: + device_printf(dev, "unknown notify %#x\n", notify); + break; + } +} + +static int +vmgenc_probe(device_t dev) +{ + int rv; + + if (acpi_disabled("vmgenc")) + return (ENXIO); + rv = ACPI_ID_PROBE(device_get_parent(dev), dev, + __DECONST(char **, vmgenc_ids), NULL); + if (rv <= 0) + device_set_desc(dev, "VM Generation Counter"); + return (rv); +} + +static const char * +vmgenc_acpi_getname(ACPI_HANDLE handle, char data[static 256]) +{ + ACPI_BUFFER buf; + + buf.Length = 256; + buf.Pointer = data; + + if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) + return (data); + return ("(unknown)"); +} + +static int +acpi_GetPackedUINT64(device_t dev, ACPI_HANDLE handle, char *path, + uint64_t *out) +{ + char hpath[256]; + ACPI_STATUS status; + ACPI_BUFFER buf; + ACPI_OBJECT param[3]; + + buf.Pointer = param; + buf.Length = sizeof(param); + status = AcpiEvaluateObject(handle, path, NULL, &buf); + if (!ACPI_SUCCESS(status)) { + device_printf(dev, "%s(%s::%s()): %s\n", __func__, + vmgenc_acpi_getname(handle, hpath), path, + AcpiFormatException(status)); + return (ENXIO); + } + if (param[0].Type != ACPI_TYPE_PACKAGE) { + device_printf(dev, "%s(%s::%s()): Wrong type %#x\n", __func__, + vmgenc_acpi_getname(handle, hpath), path, + param[0].Type); + return (ENXIO); + } + if (param[0].Package.Count != 2) { + device_printf(dev, "%s(%s::%s()): Wrong number of results %u\n", + __func__, vmgenc_acpi_getname(handle, hpath), path, + param[0].Package.Count); + return (ENXIO); + } + if (param[0].Package.Elements[0].Type != ACPI_TYPE_INTEGER || + param[0].Package.Elements[1].Type != ACPI_TYPE_INTEGER) { + device_printf(dev, "%s(%s::%s()): Wrong type results %#x, %#x\n", + __func__, vmgenc_acpi_getname(handle, hpath), path, + param[0].Package.Elements[0].Type, + param[0].Package.Elements[1].Type); + return (ENXIO); + } + + *out = (param[0].Package.Elements[0].Integer.Value & UINT32_MAX) | + ((uint64_t)param[0].Package.Elements[1].Integer.Value << 32); + if (*out == 0) + return (ENXIO); + return (0); + +} + +static int +vmgenc_attach(device_t dev) +{ + struct vmgenc_softc *sc; + uint64_t guid_physaddr; + ACPI_HANDLE h; + int error; + + h = acpi_get_handle(dev); + sc = device_get_softc(dev); + + error = acpi_GetPackedUINT64(dev, h, "ADDR", &guid_physaddr); + if (error != 0) + return (error); + + SYSCTL_ADD_OPAQUE(device_get_sysctl_ctx(dev), + SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "guid", + CTLFLAG_RD, sc->vmg_cache_guid, GUID_BYTES, "", + "latest cached VM generation counter (128-bit UUID)"); + + sc->vmg_pguid = AcpiOsMapMemory(guid_physaddr, GUID_BYTES); + memcpy(sc->vmg_cache_guid, __DEVOLATILE(void *, sc->vmg_pguid), + sizeof(sc->vmg_cache_guid)); + + AcpiInstallNotifyHandler(h, ACPI_DEVICE_NOTIFY, vmgenc_notify, dev); + return (0); +} + +static device_method_t vmgenc_methods[] = { + DEVMETHOD(device_probe, vmgenc_probe), + DEVMETHOD(device_attach, vmgenc_attach), + DEVMETHOD_END +}; + +static driver_t vmgenc_driver = { + "vmgenc", + vmgenc_methods, + sizeof(struct vmgenc_softc), +}; + +static devclass_t vmgenc_devclass; +DRIVER_MODULE(vmgenc, acpi, vmgenc_driver, vmgenc_devclass, NULL, NULL); +MODULE_DEPEND(vmgenc, acpi, 1, 1, 1); Added: head/sys/dev/vmgenc/vmgenc_acpi.h ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/sys/dev/vmgenc/vmgenc_acpi.h Sun Dec 22 06:25:20 2019 (r356001) @@ -0,0 +1,47 @@ +/*- + * SPDX-License-Identifier: BSD-2-Clause-FreeBSD + * + * Copyright (c) 2019 Conrad Meyer . 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 THE AUTHOR AND CONTRIBUTORS ``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 THE AUTHOR 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$ + */ +#include + +/* + * VM Generation Counter driver + * + * This driver has two functions: first, it provides a system event (both in + * the kernel EVENTHANDLER framework as well as a userspace devctl event) + * whenever a VM has been (HyperV doc, for example): + * - Snapped and the snapshot is executing + * - Recovered from backup + * - Failed over in a HA setup + * - Imported, copied, or cloned + * - And unspecified if VM configuration changes might cause a changed + * generation counter + * + * Second, it can read the current VM generation counter, a 128-bit GUID. + */ +typedef void (*vmgenc_handler)(void *); +EVENTHANDLER_DECLARE(acpi_vmgenc_event, vmgenc_handler);