]>
Commit | Line | Data |
---|---|---|
d03637bc BW |
1 | /* |
2 | * Virtual Machine Generation ID Device | |
3 | * | |
4 | * Copyright (C) 2017 Skyport Systems. | |
5 | * | |
6 | * Author: Ben Warren <[email protected]> | |
7 | * | |
8 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
9 | * See the COPYING file in the top-level directory. | |
10 | * | |
11 | */ | |
12 | ||
13 | #include "qemu/osdep.h" | |
e688df6b | 14 | #include "qapi/error.h" |
112ed241 | 15 | #include "qapi/qapi-commands-misc.h" |
d03637bc BW |
16 | #include "hw/acpi/acpi.h" |
17 | #include "hw/acpi/aml-build.h" | |
18 | #include "hw/acpi/vmgenid.h" | |
19 | #include "hw/nvram/fw_cfg.h" | |
20 | #include "sysemu/sysemu.h" | |
21 | ||
22 | void vmgenid_build_acpi(VmGenIdState *vms, GArray *table_data, GArray *guid, | |
23 | BIOSLinker *linker) | |
24 | { | |
25 | Aml *ssdt, *dev, *scope, *method, *addr, *if_ctx; | |
26 | uint32_t vgia_offset; | |
27 | QemuUUID guid_le; | |
28 | ||
29 | /* Fill in the GUID values. These need to be converted to little-endian | |
30 | * first, since that's what the guest expects | |
31 | */ | |
32 | g_array_set_size(guid, VMGENID_FW_CFG_SIZE - ARRAY_SIZE(guid_le.data)); | |
1324f063 | 33 | guid_le = qemu_uuid_bswap(vms->guid); |
d03637bc BW |
34 | /* The GUID is written at a fixed offset into the fw_cfg file |
35 | * in order to implement the "OVMF SDT Header probe suppressor" | |
36 | * see docs/specs/vmgenid.txt for more details | |
37 | */ | |
38 | g_array_insert_vals(guid, VMGENID_GUID_OFFSET, guid_le.data, | |
39 | ARRAY_SIZE(guid_le.data)); | |
40 | ||
41 | /* Put this in a separate SSDT table */ | |
42 | ssdt = init_aml_allocator(); | |
43 | ||
44 | /* Reserve space for header */ | |
45 | acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader)); | |
46 | ||
47 | /* Storage for the GUID address */ | |
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 | /* Simple status method to check that address is linked and non-zero */ | |
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 | /* the ADDR method returns two 32-bit words representing the lower and | |
67 | * upper halves * of the physical address of the fw_cfg blob | |
68 | * (holding the GUID) | |
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 | /* attach an ACPI notify */ | |
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 | /* Allocate guest memory for the Data fw_cfg blob */ | |
93 | bios_linker_loader_alloc(linker, VMGENID_GUID_FW_CFG_FILE, guid, 4096, | |
94 | false /* page boundary, high memory */); | |
95 | ||
96 | /* Patch address of GUID fw_cfg blob into the ADDR fw_cfg blob | |
97 | * so QEMU can write the GUID there. The address is expected to be | |
98 | * < 4GB, but write 64 bits anyway. | |
99 | * The address that is patched in is offset in order to implement | |
100 | * the "OVMF SDT Header probe suppressor" | |
101 | * see docs/specs/vmgenid.txt for more details. | |
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 | /* Patch address of GUID fw_cfg blob into the AML so OSPM can retrieve | |
108 | * and read it. Note that while we provide storage for 64 bits, only | |
109 | * the least-signficant 32 get patched into AML. | |
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 | ||
121 | void vmgenid_add_fw_cfg(VmGenIdState *vms, FWCfgState *s, GArray *guid) | |
122 | { | |
123 | /* Create a read-only fw_cfg file for GUID */ | |
124 | fw_cfg_add_file(s, VMGENID_GUID_FW_CFG_FILE, guid->data, | |
125 | VMGENID_FW_CFG_SIZE); | |
126 | /* Create a read-write fw_cfg file for Address */ | |
5f9252f7 | 127 | fw_cfg_add_file_callback(s, VMGENID_ADDR_FW_CFG_FILE, NULL, NULL, NULL, |
d03637bc BW |
128 | vms->vmgenid_addr_le, |
129 | ARRAY_SIZE(vms->vmgenid_addr_le), false); | |
130 | } | |
131 | ||
132 | static 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 | /* Write the GUID to guest memory */ | |
140 | memcpy(&vmgenid_addr, vms->vmgenid_addr_le, sizeof(vmgenid_addr)); | |
141 | vmgenid_addr = le32_to_cpu(vmgenid_addr); | |
142 | /* A zero value in vmgenid_addr means that BIOS has not yet written | |
143 | * the address | |
144 | */ | |
145 | if (vmgenid_addr) { | |
146 | /* QemuUUID has the first three words as big-endian, and expect | |
147 | * that any GUIDs passed in will always be BE. The guest, | |
148 | * however, will expect the fields to be little-endian. | |
149 | * Perform a byte swap immediately before writing. | |
150 | */ | |
1324f063 | 151 | guid_le = qemu_uuid_bswap(vms->guid); |
d03637bc BW |
152 | /* The GUID is written at a fixed offset into the fw_cfg file |
153 | * in order to implement the "OVMF SDT Header probe suppressor" | |
154 | * see docs/specs/vmgenid.txt for more details. | |
155 | */ | |
156 | cpu_physical_memory_write(vmgenid_addr, guid_le.data, | |
157 | sizeof(guid_le.data)); | |
158 | /* Send _GPE.E05 event */ | |
159 | acpi_send_event(DEVICE(obj), ACPI_VMGENID_CHANGE_STATUS); | |
160 | } | |
161 | } | |
162 | } | |
163 | ||
d03637bc BW |
164 | /* After restoring an image, we need to update the guest memory and notify |
165 | * it of a potential change to VM Generation ID | |
166 | */ | |
167 | static int vmgenid_post_load(void *opaque, int version_id) | |
168 | { | |
169 | VmGenIdState *vms = opaque; | |
170 | vmgenid_update_guest(vms); | |
171 | return 0; | |
172 | } | |
173 | ||
174 | static const VMStateDescription vmstate_vmgenid = { | |
175 | .name = "vmgenid", | |
176 | .version_id = 1, | |
177 | .minimum_version_id = 1, | |
178 | .post_load = vmgenid_post_load, | |
179 | .fields = (VMStateField[]) { | |
180 | VMSTATE_UINT8_ARRAY(vmgenid_addr_le, VmGenIdState, sizeof(uint64_t)), | |
181 | VMSTATE_END_OF_LIST() | |
182 | }, | |
183 | }; | |
184 | ||
185 | static void vmgenid_handle_reset(void *opaque) | |
186 | { | |
187 | VmGenIdState *vms = VMGENID(opaque); | |
188 | /* Clear the guest-allocated GUID address when the VM resets */ | |
189 | memset(vms->vmgenid_addr_le, 0, ARRAY_SIZE(vms->vmgenid_addr_le)); | |
190 | } | |
191 | ||
192 | static void vmgenid_realize(DeviceState *dev, Error **errp) | |
193 | { | |
194 | VmGenIdState *vms = VMGENID(dev); | |
f2a1ae45 | 195 | |
c8389550 | 196 | if (!bios_linker_loader_can_write_pointer()) { |
f2a1ae45 LE |
197 | error_setg(errp, "%s requires DMA write support in fw_cfg, " |
198 | "which this machine type does not provide", VMGENID_DEVICE); | |
199 | return; | |
200 | } | |
201 | ||
f9206302 LE |
202 | /* Given that this function is executing, there is at least one VMGENID |
203 | * device. Check if there are several. | |
204 | */ | |
205 | if (!find_vmgenid_dev()) { | |
206 | error_setg(errp, "at most one %s device is permitted", VMGENID_DEVICE); | |
207 | return; | |
208 | } | |
209 | ||
d03637bc | 210 | qemu_register_reset(vmgenid_handle_reset, vms); |
939dd2d3 RK |
211 | |
212 | vmgenid_update_guest(vms); | |
d03637bc BW |
213 | } |
214 | ||
939dd2d3 RK |
215 | static Property vmgenid_device_properties[] = { |
216 | DEFINE_PROP_UUID(VMGENID_GUID, VmGenIdState, guid), | |
217 | DEFINE_PROP_END_OF_LIST(), | |
218 | }; | |
219 | ||
d03637bc BW |
220 | static void vmgenid_device_class_init(ObjectClass *klass, void *data) |
221 | { | |
222 | DeviceClass *dc = DEVICE_CLASS(klass); | |
223 | ||
224 | dc->vmsd = &vmstate_vmgenid; | |
225 | dc->realize = vmgenid_realize; | |
939dd2d3 | 226 | dc->props = vmgenid_device_properties; |
d03637bc | 227 | dc->hotpluggable = false; |
0b4a7751 | 228 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
d03637bc BW |
229 | } |
230 | ||
231 | static const TypeInfo vmgenid_device_info = { | |
232 | .name = VMGENID_DEVICE, | |
233 | .parent = TYPE_DEVICE, | |
234 | .instance_size = sizeof(VmGenIdState), | |
235 | .class_init = vmgenid_device_class_init, | |
236 | }; | |
237 | ||
238 | static void vmgenid_register_types(void) | |
239 | { | |
240 | type_register_static(&vmgenid_device_info); | |
241 | } | |
242 | ||
243 | type_init(vmgenid_register_types) | |
39164c13 IM |
244 | |
245 | GuidInfo *qmp_query_vm_generation_id(Error **errp) | |
246 | { | |
247 | GuidInfo *info; | |
248 | VmGenIdState *vms; | |
249 | Object *obj = find_vmgenid_dev(); | |
250 | ||
251 | if (!obj) { | |
72d9196f | 252 | error_setg(errp, "VM Generation ID device not found"); |
39164c13 IM |
253 | return NULL; |
254 | } | |
255 | vms = VMGENID(obj); | |
256 | ||
257 | info = g_malloc0(sizeof(*info)); | |
258 | info->guid = qemu_uuid_unparse_strdup(&vms->guid); | |
259 | return info; | |
260 | } |