1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
5 * The "Virtual Machine Generation ID" is exposed via ACPI and changes when a
6 * virtual machine forks or is cloned. This driver exists for shepherding that
7 * information to random.c.
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/acpi.h>
13 #include <linux/random.h>
15 ACPI_MODULE_NAME("vmgenid");
17 enum { VMGENID_SIZE = 16 };
19 struct vmgenid_state {
21 u8 this_id[VMGENID_SIZE];
24 static int vmgenid_add(struct acpi_device *device)
26 struct acpi_buffer parsed = { ACPI_ALLOCATE_BUFFER };
27 struct vmgenid_state *state;
28 union acpi_object *obj;
29 phys_addr_t phys_addr;
33 state = devm_kmalloc(&device->dev, sizeof(*state), GFP_KERNEL);
37 status = acpi_evaluate_object(device->handle, "ADDR", NULL, &parsed);
38 if (ACPI_FAILURE(status)) {
39 ACPI_EXCEPTION((AE_INFO, status, "Evaluating ADDR"));
43 if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 2 ||
44 obj->package.elements[0].type != ACPI_TYPE_INTEGER ||
45 obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
50 phys_addr = (obj->package.elements[0].integer.value << 0) |
51 (obj->package.elements[1].integer.value << 32);
52 state->next_id = devm_memremap(&device->dev, phys_addr, VMGENID_SIZE, MEMREMAP_WB);
53 if (IS_ERR(state->next_id)) {
54 ret = PTR_ERR(state->next_id);
58 memcpy(state->this_id, state->next_id, sizeof(state->this_id));
59 add_device_randomness(state->this_id, sizeof(state->this_id));
61 device->driver_data = state;
64 ACPI_FREE(parsed.pointer);
68 static void vmgenid_notify(struct acpi_device *device, u32 event)
70 struct vmgenid_state *state = acpi_driver_data(device);
71 u8 old_id[VMGENID_SIZE];
73 memcpy(old_id, state->this_id, sizeof(old_id));
74 memcpy(state->this_id, state->next_id, sizeof(state->this_id));
75 if (!memcmp(old_id, state->this_id, sizeof(old_id)))
77 add_vmfork_randomness(state->this_id, sizeof(state->this_id));
80 static const struct acpi_device_id vmgenid_ids[] = {
82 { "VM_GEN_COUNTER", 0 },
86 static struct acpi_driver vmgenid_driver = {
92 .notify = vmgenid_notify
96 module_acpi_driver(vmgenid_driver);
98 MODULE_DEVICE_TABLE(acpi, vmgenid_ids);
99 MODULE_DESCRIPTION("Virtual Machine Generation ID");
100 MODULE_LICENSE("GPL v2");
101 MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");