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