xref: /openbmc/linux/drivers/virt/vmgenid.c (revision ecefa105)
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 	u8 old_id[VMGENID_SIZE];
72 
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)))
76 		return;
77 	add_vmfork_randomness(state->this_id, sizeof(state->this_id));
78 }
79 
80 static const struct acpi_device_id vmgenid_ids[] = {
81 	{ "VMGENCTR", 0 },
82 	{ "VM_GEN_COUNTER", 0 },
83 	{ }
84 };
85 
86 static struct acpi_driver vmgenid_driver = {
87 	.name = "vmgenid",
88 	.ids = vmgenid_ids,
89 	.owner = THIS_MODULE,
90 	.ops = {
91 		.add = vmgenid_add,
92 		.notify = vmgenid_notify
93 	}
94 };
95 
96 module_acpi_driver(vmgenid_driver);
97 
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>");
102