xref: /linux/drivers/virt/vmgenid.c (revision f0cd69b8cca6a5096463644d6dacc9f991bfa521)
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 or DT 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/acpi.h>
11 #include <linux/interrupt.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/random.h>
16 
17 ACPI_MODULE_NAME("vmgenid");
18 
19 enum { VMGENID_SIZE = 16 };
20 
21 struct vmgenid_state {
22 	u8 *next_id;
23 	u8 this_id[VMGENID_SIZE];
24 };
25 
vmgenid_notify(struct device * device)26 static void vmgenid_notify(struct device *device)
27 {
28 	struct vmgenid_state *state = device->driver_data;
29 	u8 old_id[VMGENID_SIZE];
30 
31 	memcpy(old_id, state->this_id, sizeof(old_id));
32 	memcpy(state->this_id, state->next_id, sizeof(state->this_id));
33 	if (!memcmp(old_id, state->this_id, sizeof(old_id)))
34 		return;
35 	add_vmfork_randomness(state->this_id, sizeof(state->this_id));
36 }
37 
setup_vmgenid_state(struct vmgenid_state * state,void * virt_addr)38 static void setup_vmgenid_state(struct vmgenid_state *state, void *virt_addr)
39 {
40 	state->next_id = virt_addr;
41 	memcpy(state->this_id, state->next_id, sizeof(state->this_id));
42 	add_device_randomness(state->this_id, sizeof(state->this_id));
43 }
44 
45 #ifdef CONFIG_ACPI
vmgenid_acpi_handler(acpi_handle __always_unused handle,u32 __always_unused event,void * dev)46 static void vmgenid_acpi_handler(acpi_handle __always_unused handle,
47 				 u32 __always_unused event, void *dev)
48 {
49 	vmgenid_notify(dev);
50 }
51 
vmgenid_add_acpi(struct device * dev,struct vmgenid_state * state)52 static int vmgenid_add_acpi(struct device *dev, struct vmgenid_state *state)
53 {
54 	struct acpi_device *device = ACPI_COMPANION(dev);
55 	struct acpi_buffer parsed = { ACPI_ALLOCATE_BUFFER };
56 	union acpi_object *obj;
57 	phys_addr_t phys_addr;
58 	acpi_status status;
59 	void *virt_addr;
60 	int ret = 0;
61 
62 	status = acpi_evaluate_object(device->handle, "ADDR", NULL, &parsed);
63 	if (ACPI_FAILURE(status)) {
64 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating ADDR"));
65 		return -ENODEV;
66 	}
67 	obj = parsed.pointer;
68 	if (!obj || obj->type != ACPI_TYPE_PACKAGE || obj->package.count != 2 ||
69 	    obj->package.elements[0].type != ACPI_TYPE_INTEGER ||
70 	    obj->package.elements[1].type != ACPI_TYPE_INTEGER) {
71 		ret = -EINVAL;
72 		goto out;
73 	}
74 
75 	phys_addr = (obj->package.elements[0].integer.value << 0) |
76 		    (obj->package.elements[1].integer.value << 32);
77 
78 	virt_addr = devm_memremap(&device->dev, phys_addr, VMGENID_SIZE, MEMREMAP_WB);
79 	if (IS_ERR(virt_addr)) {
80 		ret = PTR_ERR(virt_addr);
81 		goto out;
82 	}
83 	setup_vmgenid_state(state, virt_addr);
84 
85 	status = acpi_install_notify_handler(device->handle, ACPI_DEVICE_NOTIFY,
86 					     vmgenid_acpi_handler, dev);
87 	if (ACPI_FAILURE(status)) {
88 		ret = -ENODEV;
89 		goto out;
90 	}
91 
92 	dev->driver_data = state;
93 out:
94 	ACPI_FREE(parsed.pointer);
95 	return ret;
96 }
97 #else
vmgenid_add_acpi(struct device * dev,struct vmgenid_state * state)98 static int vmgenid_add_acpi(struct device *dev, struct vmgenid_state *state)
99 {
100 	return -EINVAL;
101 }
102 #endif
103 
vmgenid_of_irq_handler(int __always_unused irq,void * dev)104 static irqreturn_t vmgenid_of_irq_handler(int __always_unused irq, void *dev)
105 {
106 	vmgenid_notify(dev);
107 	return IRQ_HANDLED;
108 }
109 
vmgenid_add_of(struct platform_device * pdev,struct vmgenid_state * state)110 static int vmgenid_add_of(struct platform_device *pdev,
111 			  struct vmgenid_state *state)
112 {
113 	void *virt_addr;
114 	int ret;
115 
116 	virt_addr = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
117 	if (IS_ERR(virt_addr))
118 		return PTR_ERR(virt_addr);
119 
120 	setup_vmgenid_state(state, virt_addr);
121 
122 	ret = platform_get_irq(pdev, 0);
123 	if (ret < 0)
124 		return ret;
125 
126 	ret = devm_request_irq(&pdev->dev, ret, vmgenid_of_irq_handler,
127 			       IRQF_SHARED, "vmgenid", &pdev->dev);
128 	if (ret < 0)
129 		return ret;
130 
131 	pdev->dev.driver_data = state;
132 	return 0;
133 }
134 
vmgenid_add(struct platform_device * pdev)135 static int vmgenid_add(struct platform_device *pdev)
136 {
137 	struct device *dev = &pdev->dev;
138 	struct vmgenid_state *state;
139 	int ret;
140 
141 	state = devm_kmalloc(dev, sizeof(*state), GFP_KERNEL);
142 	if (!state)
143 		return -ENOMEM;
144 
145 	if (dev->of_node)
146 		ret = vmgenid_add_of(pdev, state);
147 	else
148 		ret = vmgenid_add_acpi(dev, state);
149 
150 	if (ret < 0)
151 		devm_kfree(dev, state);
152 	return ret;
153 }
154 
155 static const struct of_device_id vmgenid_of_ids[] = {
156 	{ .compatible = "microsoft,vmgenid", },
157 	{ },
158 };
159 MODULE_DEVICE_TABLE(of, vmgenid_of_ids);
160 
161 static const struct acpi_device_id vmgenid_acpi_ids[] = {
162 	{ "VMGENCTR", 0 },
163 	{ "VM_GEN_COUNTER", 0 },
164 	{ }
165 };
166 MODULE_DEVICE_TABLE(acpi, vmgenid_acpi_ids);
167 
168 static struct platform_driver vmgenid_plaform_driver = {
169 	.probe      = vmgenid_add,
170 	.driver     = {
171 		.name   = "vmgenid",
172 		.acpi_match_table = vmgenid_acpi_ids,
173 		.of_match_table = vmgenid_of_ids,
174 	},
175 };
176 
177 module_platform_driver(vmgenid_plaform_driver)
178 
179 MODULE_DESCRIPTION("Virtual Machine Generation ID");
180 MODULE_LICENSE("GPL v2");
181 MODULE_AUTHOR("Jason A. Donenfeld <Jason@zx2c4.com>");
182