xref: /linux/drivers/virt/vmgenid.c (revision 1553a1c48281243359a9529a10ddb551f3b967ab)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2022 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
4  *
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.
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/acpi.h>
13 #include <linux/random.h>
14 
15 ACPI_MODULE_NAME("vmgenid");
16 
17 enum { VMGENID_SIZE = 16 };
18 
19 struct vmgenid_state {
20 	u8 *next_id;
21 	u8 this_id[VMGENID_SIZE];
22 };
23 
24 static int vmgenid_add(struct acpi_device *device)
25 {
26 	struct acpi_buffer parsed = { ACPI_ALLOCATE_BUFFER };
27 	struct vmgenid_state *state;
28 	union acpi_object *obj;
29 	phys_addr_t phys_addr;
30 	acpi_status status;
31 	int ret = 0;
32 
33 	state = devm_kmalloc(&device->dev, sizeof(*state), GFP_KERNEL);
34 	if (!state)
35 		return -ENOMEM;
36 
37 	status = acpi_evaluate_object(device->handle, "ADDR", NULL, &parsed);
38 	if (ACPI_FAILURE(status)) {
39 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating ADDR"));
40 		return -ENODEV;
41 	}
42 	obj = parsed.pointer;
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) {
46 		ret = -EINVAL;
47 		goto out;
48 	}
49 
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);
55 		goto out;
56 	}
57 
58 	memcpy(state->this_id, state->next_id, sizeof(state->this_id));
59 	add_device_randomness(state->this_id, sizeof(state->this_id));
60 
61 	device->driver_data = state;
62 
63 out:
64 	ACPI_FREE(parsed.pointer);
65 	return ret;
66 }
67 
68 static void vmgenid_notify(struct acpi_device *device, u32 event)
69 {
70 	struct vmgenid_state *state = acpi_driver_data(device);
71 	char *envp[] = { "NEW_VMGENID=1", NULL };
72 	u8 old_id[VMGENID_SIZE];
73 
74 	memcpy(old_id, state->this_id, sizeof(old_id));
75 	memcpy(state->this_id, state->next_id, sizeof(state->this_id));
76 	if (!memcmp(old_id, state->this_id, sizeof(old_id)))
77 		return;
78 	add_vmfork_randomness(state->this_id, sizeof(state->this_id));
79 	kobject_uevent_env(&device->dev.kobj, KOBJ_CHANGE, envp);
80 }
81 
82 static const struct acpi_device_id vmgenid_ids[] = {
83 	{ "VMGENCTR", 0 },
84 	{ "VM_GEN_COUNTER", 0 },
85 	{ }
86 };
87 
88 static struct acpi_driver vmgenid_driver = {
89 	.name = "vmgenid",
90 	.ids = vmgenid_ids,
91 	.owner = THIS_MODULE,
92 	.ops = {
93 		.add = vmgenid_add,
94 		.notify = vmgenid_notify
95 	}
96 };
97 
98 module_acpi_driver(vmgenid_driver);
99 
100 MODULE_DEVICE_TABLE(acpi, vmgenid_ids);
101 MODULE_DESCRIPTION("Virtual Machine Generation ID");
102 MODULE_LICENSE("GPL v2");
103 MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");
104