1 1.3 riastrad /* $NetBSD: acpi_vmgenid.c,v 1.3 2024/08/27 00:56:46 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /*- 4 1.1 riastrad * Copyright (c) 2024 The NetBSD Foundation, Inc. 5 1.1 riastrad * All rights reserved. 6 1.1 riastrad * 7 1.1 riastrad * Redistribution and use in source and binary forms, with or without 8 1.1 riastrad * modification, are permitted provided that the following conditions 9 1.1 riastrad * are met: 10 1.1 riastrad * 1. Redistributions of source code must retain the above copyright 11 1.1 riastrad * notice, this list of conditions and the following disclaimer. 12 1.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 riastrad * notice, this list of conditions and the following disclaimer in the 14 1.1 riastrad * documentation and/or other materials provided with the distribution. 15 1.1 riastrad * 16 1.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 1.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 1.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 1.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 1.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 1.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 1.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 1.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 1.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 1.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 1.1 riastrad * POSSIBILITY OF SUCH DAMAGE. 27 1.1 riastrad */ 28 1.1 riastrad 29 1.1 riastrad /* 30 1.1 riastrad * Virtual Machine Generation ID 31 1.1 riastrad * 32 1.1 riastrad * The VMGENID is an 8-byte cookie shared between a VM host and VM 33 1.1 riastrad * guest. Whenever the host clones a VM, it changes the VMGENID 34 1.1 riastrad * and sends an ACPI notification to the guest. 35 1.1 riastrad * 36 1.1 riastrad * References: 37 1.1 riastrad * 38 1.1 riastrad * `Virtual Machine Generation ID', Microsoft, 2012-08-01. 39 1.1 riastrad * http://go.microsoft.com/fwlink/?LinkId=260709 40 1.1 riastrad * 41 1.1 riastrad * `Virtual Machine Generation ID Device', The QEMU Project 42 1.1 riastrad * Developers. 43 1.1 riastrad * https://www.qemu.org/docs/master/specs/vmgenid.html 44 1.1 riastrad */ 45 1.1 riastrad 46 1.1 riastrad #include <sys/cdefs.h> 47 1.3 riastrad __KERNEL_RCSID(0, "$NetBSD: acpi_vmgenid.c,v 1.3 2024/08/27 00:56:46 riastradh Exp $"); 48 1.1 riastrad 49 1.1 riastrad #include <sys/device.h> 50 1.1 riastrad #include <sys/entropy.h> 51 1.1 riastrad #include <sys/module.h> 52 1.1 riastrad #include <sys/rndsource.h> 53 1.1 riastrad #include <sys/sysctl.h> 54 1.1 riastrad 55 1.1 riastrad #include <dev/acpi/acpireg.h> 56 1.1 riastrad #include <dev/acpi/acpivar.h> 57 1.1 riastrad 58 1.1 riastrad #define _COMPONENT ACPI_RESOURCE_COMPONENT 59 1.1 riastrad ACPI_MODULE_NAME ("acpi_vmgenid") 60 1.1 riastrad 61 1.1 riastrad struct acpivmgenid { 62 1.1 riastrad uint8_t id[16]; 63 1.1 riastrad } __aligned(8); 64 1.1 riastrad 65 1.1 riastrad struct acpivmgenid_softc { 66 1.1 riastrad device_t sc_dev; 67 1.1 riastrad struct acpi_devnode *sc_node; 68 1.1 riastrad uint64_t sc_paddr; 69 1.1 riastrad struct acpivmgenid *sc_vaddr; 70 1.1 riastrad struct krndsource sc_rndsource; 71 1.1 riastrad struct sysctllog *sc_sysctllog; 72 1.1 riastrad const struct sysctlnode *sc_sysctlroot; 73 1.1 riastrad }; 74 1.1 riastrad 75 1.1 riastrad static int acpivmgenid_match(device_t, cfdata_t, void *); 76 1.1 riastrad static void acpivmgenid_attach(device_t, device_t, void *); 77 1.1 riastrad static int acpivmgenid_detach(device_t, int); 78 1.1 riastrad static void acpivmgenid_set(struct acpivmgenid_softc *, const char *); 79 1.1 riastrad static void acpivmgenid_notify(ACPI_HANDLE, uint32_t, void *); 80 1.1 riastrad static void acpivmgenid_reset(void *); 81 1.1 riastrad static int acpivmgenid_sysctl(SYSCTLFN_ARGS); 82 1.1 riastrad 83 1.1 riastrad static const struct device_compatible_entry compat_data[] = { 84 1.1 riastrad { .compat = "VM_Gen_Counter" }, /* from the Microsoft spec */ 85 1.1 riastrad { .compat = "VM_GEN_COUNTER" }, /* used by qemu */ 86 1.1 riastrad { .compat = "VMGENCTR" }, /* recognized by Linux */ 87 1.1 riastrad DEVICE_COMPAT_EOL 88 1.1 riastrad }; 89 1.1 riastrad 90 1.1 riastrad CFATTACH_DECL_NEW(acpivmgenid, sizeof(struct acpivmgenid_softc), 91 1.1 riastrad acpivmgenid_match, acpivmgenid_attach, acpivmgenid_detach, NULL); 92 1.1 riastrad 93 1.1 riastrad static int 94 1.1 riastrad acpivmgenid_match(device_t parent, cfdata_t match, void *aux) 95 1.1 riastrad { 96 1.1 riastrad const struct acpi_attach_args *const aa = aux; 97 1.1 riastrad 98 1.1 riastrad return acpi_compatible_match(aa, compat_data); 99 1.1 riastrad } 100 1.1 riastrad 101 1.1 riastrad static void 102 1.1 riastrad acpivmgenid_attach(device_t parent, device_t self, void *aux) 103 1.1 riastrad { 104 1.1 riastrad struct acpivmgenid_softc *const sc = device_private(self); 105 1.1 riastrad const struct acpi_attach_args *const aa = aux; 106 1.1 riastrad ACPI_BUFFER addrbuf = { 107 1.1 riastrad .Pointer = NULL, 108 1.1 riastrad .Length = ACPI_ALLOCATE_BUFFER, 109 1.1 riastrad }; 110 1.1 riastrad ACPI_OBJECT *addrobj, *addrarr; 111 1.1 riastrad ACPI_STATUS rv; 112 1.1 riastrad int error; 113 1.1 riastrad 114 1.1 riastrad aprint_naive(": ACPI VM Generation ID\n"); 115 1.1 riastrad aprint_normal(": ACPI VM Generation ID\n"); 116 1.1 riastrad 117 1.1 riastrad sc->sc_dev = self; 118 1.1 riastrad sc->sc_node = aa->aa_node; 119 1.1 riastrad 120 1.1 riastrad /* 121 1.1 riastrad * Get the address from the ADDR object, which is a package of 122 1.1 riastrad * two 32-bit integers representing the low and high halves of 123 1.1 riastrad * a 64-bit physical address. 124 1.1 riastrad */ 125 1.1 riastrad rv = AcpiEvaluateObjectTyped(sc->sc_node->ad_handle, "ADDR", NULL, 126 1.1 riastrad &addrbuf, ACPI_TYPE_PACKAGE); 127 1.1 riastrad if (ACPI_FAILURE(rv)) { 128 1.1 riastrad aprint_error_dev(self, "failed to get ADDR: %s\n", 129 1.1 riastrad AcpiFormatException(rv)); 130 1.1 riastrad goto out; 131 1.1 riastrad } 132 1.1 riastrad addrobj = addrbuf.Pointer; 133 1.1 riastrad if (addrobj->Type != ACPI_TYPE_PACKAGE || 134 1.1 riastrad addrobj->Package.Count != 2) { 135 1.1 riastrad aprint_error_dev(self, "invalid ADDR\n"); 136 1.1 riastrad goto out; 137 1.1 riastrad } 138 1.1 riastrad addrarr = addrobj->Package.Elements; 139 1.1 riastrad if (addrarr[0].Type != ACPI_TYPE_INTEGER || 140 1.1 riastrad addrarr[1].Type != ACPI_TYPE_INTEGER || 141 1.1 riastrad addrarr[0].Integer.Value > UINT32_MAX || 142 1.1 riastrad addrarr[1].Integer.Value > UINT32_MAX) { 143 1.1 riastrad aprint_error_dev(self, "invalid ADDR\n"); 144 1.1 riastrad goto out; 145 1.1 riastrad } 146 1.1 riastrad sc->sc_paddr = (ACPI_PHYSICAL_ADDRESS)addrarr[0].Integer.Value; 147 1.1 riastrad sc->sc_paddr |= (ACPI_PHYSICAL_ADDRESS)addrarr[1].Integer.Value << 32; 148 1.1 riastrad aprint_normal_dev(self, "paddr=0x%"PRIx64"\n", (uint64_t)sc->sc_paddr); 149 1.1 riastrad 150 1.1 riastrad /* 151 1.1 riastrad * Map the physical address into virtual address space. 152 1.1 riastrad */ 153 1.1 riastrad sc->sc_vaddr = AcpiOsMapMemory(sc->sc_paddr, sizeof(*sc->sc_vaddr)); 154 1.1 riastrad if (sc->sc_vaddr == NULL) { 155 1.1 riastrad aprint_error_dev(self, "failed to map address\n"); 156 1.1 riastrad goto out; 157 1.1 riastrad } 158 1.1 riastrad 159 1.1 riastrad /* 160 1.1 riastrad * Register a random source so we can attribute samples. 161 1.1 riastrad */ 162 1.1 riastrad rnd_attach_source(&sc->sc_rndsource, device_xname(self), 163 1.1 riastrad RND_TYPE_UNKNOWN, RND_FLAG_COLLECT_TIME|RND_FLAG_COLLECT_VALUE); 164 1.1 riastrad 165 1.1 riastrad /* 166 1.1 riastrad * Register an ACPI notifier so that we can detect changes. 167 1.1 riastrad */ 168 1.1 riastrad (void)acpi_register_notify(sc->sc_node, acpivmgenid_notify); 169 1.1 riastrad 170 1.1 riastrad /* 171 1.1 riastrad * Now that we have registered a random source and a notifier, 172 1.1 riastrad * read out the first value. 173 1.1 riastrad */ 174 1.1 riastrad acpivmgenid_set(sc, "initial"); 175 1.1 riastrad 176 1.1 riastrad /* 177 1.1 riastrad * Attach a sysctl tree, rooted at hw.acpivmgenidN. 178 1.1 riastrad */ 179 1.1 riastrad error = sysctl_createv(&sc->sc_sysctllog, 0, NULL, &sc->sc_sysctlroot, 180 1.1 riastrad CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(self), 181 1.1 riastrad SYSCTL_DESCR("Virtual Machine Generation ID device"), 182 1.1 riastrad NULL, 0, NULL, 0, 183 1.1 riastrad CTL_HW, CTL_CREATE, CTL_EOL); 184 1.1 riastrad if (error) { 185 1.1 riastrad aprint_error_dev(self, "failed to create sysctl hw.%s: %d\n", 186 1.1 riastrad device_xname(self), error); 187 1.1 riastrad goto out; 188 1.1 riastrad } 189 1.1 riastrad 190 1.1 riastrad /* 191 1.1 riastrad * hw.acpivmgenidN.id (`struct', 16-byte array) 192 1.1 riastrad */ 193 1.1 riastrad error = sysctl_createv(&sc->sc_sysctllog, 0, &sc->sc_sysctlroot, NULL, 194 1.1 riastrad CTLFLAG_PERMANENT|CTLFLAG_READONLY|CTLFLAG_PRIVATE, CTLTYPE_STRUCT, 195 1.1 riastrad "id", SYSCTL_DESCR("Virtual Machine Generation ID"), 196 1.1 riastrad &acpivmgenid_sysctl, 0, sc, sizeof(struct acpivmgenid), 197 1.1 riastrad CTL_CREATE, CTL_EOL); 198 1.1 riastrad if (error) { 199 1.1 riastrad aprint_error_dev(self, 200 1.1 riastrad "failed to create sysctl hw.%s.id: %d\n", 201 1.1 riastrad device_xname(self), error); 202 1.1 riastrad goto out; 203 1.1 riastrad } 204 1.1 riastrad 205 1.1 riastrad /* 206 1.1 riastrad * hw.acpivmgenidN.paddr (64-bit integer) 207 1.1 riastrad */ 208 1.1 riastrad __CTASSERT(sizeof(ACPI_PHYSICAL_ADDRESS) == sizeof(quad_t)); 209 1.1 riastrad error = sysctl_createv(&sc->sc_sysctllog, 0, &sc->sc_sysctlroot, NULL, 210 1.1 riastrad CTLFLAG_PERMANENT|CTLFLAG_READONLY|CTLFLAG_PRIVATE, CTLTYPE_QUAD, 211 1.1 riastrad "paddr", SYSCTL_DESCR("Physical address of VM Generation ID"), 212 1.1 riastrad NULL, 0, &sc->sc_paddr, sizeof(sc->sc_paddr), 213 1.1 riastrad CTL_CREATE, CTL_EOL); 214 1.1 riastrad if (error) { 215 1.1 riastrad aprint_error_dev(self, 216 1.1 riastrad "failed to create sysctl hw.%s.paddr: %d\n", 217 1.1 riastrad device_xname(self), error); 218 1.1 riastrad goto out; 219 1.1 riastrad } 220 1.1 riastrad 221 1.1 riastrad out: ACPI_FREE(addrbuf.Pointer); 222 1.1 riastrad } 223 1.1 riastrad 224 1.1 riastrad static int 225 1.1 riastrad acpivmgenid_detach(device_t self, int flags) 226 1.1 riastrad { 227 1.1 riastrad struct acpivmgenid_softc *const sc = device_private(self); 228 1.1 riastrad int error; 229 1.1 riastrad 230 1.1 riastrad error = config_detach_children(self, flags); 231 1.1 riastrad if (error) 232 1.1 riastrad return error; 233 1.1 riastrad 234 1.1 riastrad sysctl_teardown(&sc->sc_sysctllog); 235 1.1 riastrad acpi_deregister_notify(sc->sc_node); 236 1.1 riastrad rnd_detach_source(&sc->sc_rndsource); 237 1.1 riastrad if (sc->sc_vaddr) { 238 1.1 riastrad AcpiOsUnmapMemory(sc->sc_vaddr, sizeof(*sc->sc_vaddr)); 239 1.1 riastrad sc->sc_vaddr = NULL; /* paranoia */ 240 1.1 riastrad } 241 1.1 riastrad sc->sc_paddr = 0; /* paranoia */ 242 1.1 riastrad 243 1.1 riastrad return 0; 244 1.1 riastrad } 245 1.1 riastrad 246 1.1 riastrad static void 247 1.1 riastrad acpivmgenid_set(struct acpivmgenid_softc *sc, const char *prefix) 248 1.1 riastrad { 249 1.1 riastrad struct acpivmgenid vmgenid; 250 1.1 riastrad char vmgenidstr[2*__arraycount(vmgenid.id) + 1]; 251 1.1 riastrad unsigned i; 252 1.1 riastrad 253 1.1 riastrad /* 254 1.1 riastrad * Grab the current VM generation ID. No obvious way to make 255 1.1 riastrad * this atomic, so let's hope if it changes in the middle we'll 256 1.1 riastrad * get another notification. 257 1.1 riastrad */ 258 1.1 riastrad memcpy(&vmgenid, sc->sc_vaddr, sizeof(vmgenid)); 259 1.1 riastrad 260 1.1 riastrad /* 261 1.1 riastrad * Print the VM generation ID to the console for posterity. 262 1.1 riastrad */ 263 1.1 riastrad for (i = 0; i < __arraycount(vmgenid.id); i++) { 264 1.1 riastrad vmgenidstr[2*i] = "0123456789abcdef"[vmgenid.id[i] >> 4]; 265 1.1 riastrad vmgenidstr[2*i + 1] = "0123456789abcdef"[vmgenid.id[i] & 0xf]; 266 1.1 riastrad } 267 1.1 riastrad vmgenidstr[2*sizeof(vmgenid)] = '\0'; 268 1.1 riastrad aprint_verbose_dev(sc->sc_dev, "%s: %s\n", prefix, vmgenidstr); 269 1.1 riastrad 270 1.1 riastrad /* 271 1.1 riastrad * Enter the new VM generation ID into the entropy pool. 272 1.1 riastrad */ 273 1.1 riastrad rnd_add_data(&sc->sc_rndsource, &vmgenid, sizeof(vmgenid), 0); 274 1.1 riastrad } 275 1.1 riastrad 276 1.1 riastrad static void 277 1.1 riastrad acpivmgenid_notify(ACPI_HANDLE hdl, uint32_t notify, void *opaque) 278 1.1 riastrad { 279 1.1 riastrad const device_t self = opaque; 280 1.1 riastrad struct acpivmgenid_softc *const sc = device_private(self); 281 1.1 riastrad 282 1.1 riastrad if (notify != 0x80) { 283 1.1 riastrad aprint_debug_dev(self, "unknown notify 0x%02x\n", notify); 284 1.1 riastrad return; 285 1.1 riastrad } 286 1.1 riastrad 287 1.1 riastrad (void)AcpiOsExecute(OSL_NOTIFY_HANDLER, &acpivmgenid_reset, sc); 288 1.1 riastrad } 289 1.1 riastrad 290 1.1 riastrad static void 291 1.1 riastrad acpivmgenid_reset(void *cookie) 292 1.1 riastrad { 293 1.1 riastrad struct acpivmgenid_softc *const sc = cookie; 294 1.1 riastrad 295 1.1 riastrad /* 296 1.2 riastrad * Reset the system entropy pool's measure of entropy (not the 297 1.2 riastrad * data, just the system's assessment of whether it has 298 1.2 riastrad * entropy), and gather more entropy from any synchronous 299 1.2 riastrad * sources we have available like CPU RNG instructions. We 300 1.2 riastrad * can't be interrupted by a signal so ignore return value. 301 1.2 riastrad */ 302 1.2 riastrad entropy_reset(); 303 1.2 riastrad (void)entropy_gather(); 304 1.2 riastrad 305 1.2 riastrad /* 306 1.1 riastrad * Grab the current VM generation ID to put it into the entropy 307 1.1 riastrad * pool; then force consolidation so it affects all subsequent 308 1.1 riastrad * draws from the entropy pool and the entropy epoch advances. 309 1.3 riastrad * Again we can't be interrupted by a signal so ignore return 310 1.3 riastrad * value. 311 1.1 riastrad */ 312 1.1 riastrad acpivmgenid_set(sc, "cloned"); 313 1.3 riastrad (void)entropy_consolidate(); 314 1.1 riastrad } 315 1.1 riastrad 316 1.1 riastrad static int 317 1.1 riastrad acpivmgenid_sysctl(SYSCTLFN_ARGS) 318 1.1 riastrad { 319 1.1 riastrad struct sysctlnode node = *rnode; 320 1.1 riastrad struct acpivmgenid_softc *const sc = node.sysctl_data; 321 1.1 riastrad 322 1.1 riastrad node.sysctl_data = sc->sc_vaddr; 323 1.1 riastrad return sysctl_lookup(SYSCTLFN_CALL(&node)); 324 1.1 riastrad } 325 1.1 riastrad 326 1.1 riastrad MODULE(MODULE_CLASS_DRIVER, acpivmgenid, NULL); 327 1.1 riastrad 328 1.1 riastrad #ifdef _MODULE 329 1.1 riastrad #include "ioconf.c" 330 1.1 riastrad #endif 331 1.1 riastrad 332 1.1 riastrad static int 333 1.1 riastrad acpivmgenid_modcmd(modcmd_t cmd, void *opaque) 334 1.1 riastrad { 335 1.1 riastrad int error = 0; 336 1.1 riastrad 337 1.1 riastrad switch (cmd) { 338 1.1 riastrad case MODULE_CMD_INIT: 339 1.1 riastrad #ifdef _MODULE 340 1.1 riastrad error = config_init_component(cfdriver_ioconf_acpivmgenid, 341 1.1 riastrad cfattach_ioconf_acpivmgenid, cfdata_ioconf_acpivmgenid); 342 1.1 riastrad #endif 343 1.1 riastrad return error; 344 1.1 riastrad case MODULE_CMD_FINI: 345 1.1 riastrad #ifdef _MODULE 346 1.1 riastrad error = config_fini_component(cfdriver_ioconf_acpivmgenid, 347 1.1 riastrad cfattach_ioconf_acpivmgenid, cfdata_ioconf_acpivmgenid); 348 1.1 riastrad #endif 349 1.1 riastrad return error; 350 1.1 riastrad default: 351 1.1 riastrad return ENOTTY; 352 1.1 riastrad } 353 1.1 riastrad } 354