1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "qmp-commands.h"
15#include "hw/acpi/acpi.h"
16#include "hw/acpi/aml-build.h"
17#include "hw/acpi/vmgenid.h"
18#include "hw/nvram/fw_cfg.h"
19#include "sysemu/sysemu.h"
20
21void vmgenid_build_acpi(VmGenIdState *vms, GArray *table_data, GArray *guid,
22 BIOSLinker *linker)
23{
24 Aml *ssdt, *dev, *scope, *method, *addr, *if_ctx;
25 uint32_t vgia_offset;
26 QemuUUID guid_le;
27
28
29
30
31 g_array_set_size(guid, VMGENID_FW_CFG_SIZE - ARRAY_SIZE(guid_le.data));
32 guid_le = vms->guid;
33 qemu_uuid_bswap(&guid_le);
34
35
36
37
38 g_array_insert_vals(guid, VMGENID_GUID_OFFSET, guid_le.data,
39 ARRAY_SIZE(guid_le.data));
40
41
42 ssdt = init_aml_allocator();
43
44
45 acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader));
46
47
48 vgia_offset = table_data->len +
49 build_append_named_dword(ssdt->buf, "VGIA");
50 scope = aml_scope("\\_SB");
51 dev = aml_device("VGEN");
52 aml_append(dev, aml_name_decl("_HID", aml_string("QEMUVGID")));
53 aml_append(dev, aml_name_decl("_CID", aml_string("VM_Gen_Counter")));
54 aml_append(dev, aml_name_decl("_DDN", aml_string("VM_Gen_Counter")));
55
56
57 method = aml_method("_STA", 0, AML_NOTSERIALIZED);
58 addr = aml_local(0);
59 aml_append(method, aml_store(aml_int(0xf), addr));
60 if_ctx = aml_if(aml_equal(aml_name("VGIA"), aml_int(0)));
61 aml_append(if_ctx, aml_store(aml_int(0), addr));
62 aml_append(method, if_ctx);
63 aml_append(method, aml_return(addr));
64 aml_append(dev, method);
65
66
67
68
69
70 method = aml_method("ADDR", 0, AML_NOTSERIALIZED);
71
72 addr = aml_local(0);
73 aml_append(method, aml_store(aml_package(2), addr));
74
75 aml_append(method, aml_store(aml_add(aml_name("VGIA"),
76 aml_int(VMGENID_GUID_OFFSET), NULL),
77 aml_index(addr, aml_int(0))));
78 aml_append(method, aml_store(aml_int(0), aml_index(addr, aml_int(1))));
79 aml_append(method, aml_return(addr));
80
81 aml_append(dev, method);
82 aml_append(scope, dev);
83 aml_append(ssdt, scope);
84
85
86 method = aml_method("\\_GPE._E05", 0, AML_NOTSERIALIZED);
87 aml_append(method, aml_notify(aml_name("\\_SB.VGEN"), aml_int(0x80)));
88 aml_append(ssdt, method);
89
90 g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len);
91
92
93 bios_linker_loader_alloc(linker, VMGENID_GUID_FW_CFG_FILE, guid, 4096,
94 false );
95
96
97
98
99
100
101
102
103 bios_linker_loader_write_pointer(linker,
104 VMGENID_ADDR_FW_CFG_FILE, 0, sizeof(uint64_t),
105 VMGENID_GUID_FW_CFG_FILE, VMGENID_GUID_OFFSET);
106
107
108
109
110
111 bios_linker_loader_add_pointer(linker,
112 ACPI_BUILD_TABLE_FILE, vgia_offset, sizeof(uint32_t),
113 VMGENID_GUID_FW_CFG_FILE, 0);
114
115 build_header(linker, table_data,
116 (void *)(table_data->data + table_data->len - ssdt->buf->len),
117 "SSDT", ssdt->buf->len, 1, NULL, "VMGENID");
118 free_aml_allocator();
119}
120
121void vmgenid_add_fw_cfg(VmGenIdState *vms, FWCfgState *s, GArray *guid)
122{
123
124 fw_cfg_add_file(s, VMGENID_GUID_FW_CFG_FILE, guid->data,
125 VMGENID_FW_CFG_SIZE);
126
127 fw_cfg_add_file_callback(s, VMGENID_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
128 vms->vmgenid_addr_le,
129 ARRAY_SIZE(vms->vmgenid_addr_le), false);
130}
131
132static void vmgenid_update_guest(VmGenIdState *vms)
133{
134 Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, NULL);
135 uint32_t vmgenid_addr;
136 QemuUUID guid_le;
137
138 if (obj) {
139
140 memcpy(&vmgenid_addr, vms->vmgenid_addr_le, sizeof(vmgenid_addr));
141 vmgenid_addr = le32_to_cpu(vmgenid_addr);
142
143
144
145 if (vmgenid_addr) {
146
147
148
149
150
151 guid_le = vms->guid;
152 qemu_uuid_bswap(&guid_le);
153
154
155
156
157 cpu_physical_memory_write(vmgenid_addr, guid_le.data,
158 sizeof(guid_le.data));
159
160 acpi_send_event(DEVICE(obj), ACPI_VMGENID_CHANGE_STATUS);
161 }
162 }
163}
164
165static void vmgenid_set_guid(Object *obj, const char *value, Error **errp)
166{
167 VmGenIdState *vms = VMGENID(obj);
168
169 if (!strcmp(value, "auto")) {
170 qemu_uuid_generate(&vms->guid);
171 } else if (qemu_uuid_parse(value, &vms->guid) < 0) {
172 error_setg(errp, "'%s. %s': Failed to parse GUID string: %s",
173 object_get_typename(OBJECT(vms)), VMGENID_GUID, value);
174 return;
175 }
176
177 vmgenid_update_guest(vms);
178}
179
180
181
182
183static int vmgenid_post_load(void *opaque, int version_id)
184{
185 VmGenIdState *vms = opaque;
186 vmgenid_update_guest(vms);
187 return 0;
188}
189
190static const VMStateDescription vmstate_vmgenid = {
191 .name = "vmgenid",
192 .version_id = 1,
193 .minimum_version_id = 1,
194 .post_load = vmgenid_post_load,
195 .fields = (VMStateField[]) {
196 VMSTATE_UINT8_ARRAY(vmgenid_addr_le, VmGenIdState, sizeof(uint64_t)),
197 VMSTATE_END_OF_LIST()
198 },
199};
200
201static void vmgenid_handle_reset(void *opaque)
202{
203 VmGenIdState *vms = VMGENID(opaque);
204
205 memset(vms->vmgenid_addr_le, 0, ARRAY_SIZE(vms->vmgenid_addr_le));
206}
207
208static void vmgenid_realize(DeviceState *dev, Error **errp)
209{
210 VmGenIdState *vms = VMGENID(dev);
211
212 if (!bios_linker_loader_can_write_pointer()) {
213 error_setg(errp, "%s requires DMA write support in fw_cfg, "
214 "which this machine type does not provide", VMGENID_DEVICE);
215 return;
216 }
217
218
219
220
221 if (!find_vmgenid_dev()) {
222 error_setg(errp, "at most one %s device is permitted", VMGENID_DEVICE);
223 return;
224 }
225
226 qemu_register_reset(vmgenid_handle_reset, vms);
227}
228
229static void vmgenid_device_class_init(ObjectClass *klass, void *data)
230{
231 DeviceClass *dc = DEVICE_CLASS(klass);
232
233 dc->vmsd = &vmstate_vmgenid;
234 dc->realize = vmgenid_realize;
235 dc->hotpluggable = false;
236 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
237
238 object_class_property_add_str(klass, VMGENID_GUID, NULL,
239 vmgenid_set_guid, NULL);
240 object_class_property_set_description(klass, VMGENID_GUID,
241 "Set Global Unique Identifier "
242 "(big-endian) or auto for random value",
243 NULL);
244}
245
246static const TypeInfo vmgenid_device_info = {
247 .name = VMGENID_DEVICE,
248 .parent = TYPE_DEVICE,
249 .instance_size = sizeof(VmGenIdState),
250 .class_init = vmgenid_device_class_init,
251};
252
253static void vmgenid_register_types(void)
254{
255 type_register_static(&vmgenid_device_info);
256}
257
258type_init(vmgenid_register_types)
259
260GuidInfo *qmp_query_vm_generation_id(Error **errp)
261{
262 GuidInfo *info;
263 VmGenIdState *vms;
264 Object *obj = find_vmgenid_dev();
265
266 if (!obj) {
267 error_setg(errp, "VM Generation ID device not found");
268 return NULL;
269 }
270 vms = VMGENID(obj);
271
272 info = g_malloc0(sizeof(*info));
273 info->guid = qemu_uuid_unparse_strdup(&vms->guid);
274 return info;
275}
276