xref: /openbmc/qemu/hw/acpi/vmgenid.c (revision ec17de0a)
1 /*
2  *  Virtual Machine Generation ID Device
3  *
4  *  Copyright (C) 2017 Skyport Systems.
5  *
6  *  Author: Ben Warren <ben@skyportsystems.com>
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"
14 #include "qapi/error.h"
15 #include "qapi/qapi-commands-machine.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 "hw/qdev-properties-system.h"
23 #include "migration/vmstate.h"
24 #include "sysemu/reset.h"
25 
26 void vmgenid_build_acpi(VmGenIdState *vms, GArray *table_data, GArray *guid,
27                         BIOSLinker *linker, const char *oem_id)
28 {
29     Aml *ssdt, *dev, *scope, *method, *addr, *if_ctx;
30     uint32_t vgia_offset;
31     QemuUUID guid_le;
32 
33     /* Fill in the GUID values.  These need to be converted to little-endian
34      * first, since that's what the guest expects
35      */
36     g_array_set_size(guid, VMGENID_FW_CFG_SIZE - ARRAY_SIZE(guid_le.data));
37     guid_le = qemu_uuid_bswap(vms->guid);
38     /* The GUID is written at a fixed offset into the fw_cfg file
39      * in order to implement the "OVMF SDT Header probe suppressor"
40      * see docs/specs/vmgenid.txt for more details
41      */
42     g_array_insert_vals(guid, VMGENID_GUID_OFFSET, guid_le.data,
43                         ARRAY_SIZE(guid_le.data));
44 
45     /* Put this in a separate SSDT table */
46     ssdt = init_aml_allocator();
47 
48     /* Reserve space for header */
49     acpi_data_push(ssdt->buf, sizeof(AcpiTableHeader));
50 
51     /* Storage for the GUID address */
52     vgia_offset = table_data->len +
53         build_append_named_dword(ssdt->buf, "VGIA");
54     scope = aml_scope("\\_SB");
55     dev = aml_device("VGEN");
56     aml_append(dev, aml_name_decl("_HID", aml_string("QEMUVGID")));
57     aml_append(dev, aml_name_decl("_CID", aml_string("VM_Gen_Counter")));
58     aml_append(dev, aml_name_decl("_DDN", aml_string("VM_Gen_Counter")));
59 
60     /* Simple status method to check that address is linked and non-zero */
61     method = aml_method("_STA", 0, AML_NOTSERIALIZED);
62     addr = aml_local(0);
63     aml_append(method, aml_store(aml_int(0xf), addr));
64     if_ctx = aml_if(aml_equal(aml_name("VGIA"), aml_int(0)));
65     aml_append(if_ctx, aml_store(aml_int(0), addr));
66     aml_append(method, if_ctx);
67     aml_append(method, aml_return(addr));
68     aml_append(dev, method);
69 
70     /* the ADDR method returns two 32-bit words representing the lower and
71      * upper halves * of the physical address of the fw_cfg blob
72      * (holding the GUID)
73      */
74     method = aml_method("ADDR", 0, AML_NOTSERIALIZED);
75 
76     addr = aml_local(0);
77     aml_append(method, aml_store(aml_package(2), addr));
78 
79     aml_append(method, aml_store(aml_add(aml_name("VGIA"),
80                                          aml_int(VMGENID_GUID_OFFSET), NULL),
81                                  aml_index(addr, aml_int(0))));
82     aml_append(method, aml_store(aml_int(0), aml_index(addr, aml_int(1))));
83     aml_append(method, aml_return(addr));
84 
85     aml_append(dev, method);
86     aml_append(scope, dev);
87     aml_append(ssdt, scope);
88 
89     /* attach an ACPI notify */
90     method = aml_method("\\_GPE._E05", 0, AML_NOTSERIALIZED);
91     aml_append(method, aml_notify(aml_name("\\_SB.VGEN"), aml_int(0x80)));
92     aml_append(ssdt, method);
93 
94     g_array_append_vals(table_data, ssdt->buf->data, ssdt->buf->len);
95 
96     /* Allocate guest memory for the Data fw_cfg blob */
97     bios_linker_loader_alloc(linker, VMGENID_GUID_FW_CFG_FILE, guid, 4096,
98                              false /* page boundary, high memory */);
99 
100     /* Patch address of GUID fw_cfg blob into the ADDR fw_cfg blob
101      * so QEMU can write the GUID there.  The address is expected to be
102      * < 4GB, but write 64 bits anyway.
103      * The address that is patched in is offset in order to implement
104      * the "OVMF SDT Header probe suppressor"
105      * see docs/specs/vmgenid.txt for more details.
106      */
107     bios_linker_loader_write_pointer(linker,
108         VMGENID_ADDR_FW_CFG_FILE, 0, sizeof(uint64_t),
109         VMGENID_GUID_FW_CFG_FILE, VMGENID_GUID_OFFSET);
110 
111     /* Patch address of GUID fw_cfg blob into the AML so OSPM can retrieve
112      * and read it.  Note that while we provide storage for 64 bits, only
113      * the least-signficant 32 get patched into AML.
114      */
115     bios_linker_loader_add_pointer(linker,
116         ACPI_BUILD_TABLE_FILE, vgia_offset, sizeof(uint32_t),
117         VMGENID_GUID_FW_CFG_FILE, 0);
118 
119     build_header(linker, table_data,
120         (void *)(table_data->data + table_data->len - ssdt->buf->len),
121         "SSDT", ssdt->buf->len, 1, oem_id, "VMGENID");
122     free_aml_allocator();
123 }
124 
125 void vmgenid_add_fw_cfg(VmGenIdState *vms, FWCfgState *s, GArray *guid)
126 {
127     /* Create a read-only fw_cfg file for GUID */
128     fw_cfg_add_file(s, VMGENID_GUID_FW_CFG_FILE, guid->data,
129                     VMGENID_FW_CFG_SIZE);
130     /* Create a read-write fw_cfg file for Address */
131     fw_cfg_add_file_callback(s, VMGENID_ADDR_FW_CFG_FILE, NULL, NULL, NULL,
132                              vms->vmgenid_addr_le,
133                              ARRAY_SIZE(vms->vmgenid_addr_le), false);
134 }
135 
136 static void vmgenid_update_guest(VmGenIdState *vms)
137 {
138     Object *obj = object_resolve_path_type("", TYPE_ACPI_DEVICE_IF, NULL);
139     uint32_t vmgenid_addr;
140     QemuUUID guid_le;
141 
142     if (obj) {
143         /* Write the GUID to guest memory */
144         memcpy(&vmgenid_addr, vms->vmgenid_addr_le, sizeof(vmgenid_addr));
145         vmgenid_addr = le32_to_cpu(vmgenid_addr);
146         /* A zero value in vmgenid_addr means that BIOS has not yet written
147          * the address
148          */
149         if (vmgenid_addr) {
150             /* QemuUUID has the first three words as big-endian, and expect
151              * that any GUIDs passed in will always be BE.  The guest,
152              * however, will expect the fields to be little-endian.
153              * Perform a byte swap immediately before writing.
154              */
155             guid_le = qemu_uuid_bswap(vms->guid);
156             /* The GUID is written at a fixed offset into the fw_cfg file
157              * in order to implement the "OVMF SDT Header probe suppressor"
158              * see docs/specs/vmgenid.txt for more details.
159              */
160             cpu_physical_memory_write(vmgenid_addr, guid_le.data,
161                                       sizeof(guid_le.data));
162             /* Send _GPE.E05 event */
163             acpi_send_event(DEVICE(obj), ACPI_VMGENID_CHANGE_STATUS);
164         }
165     }
166 }
167 
168 /* After restoring an image, we need to update the guest memory and notify
169  * it of a potential change to VM Generation ID
170  */
171 static int vmgenid_post_load(void *opaque, int version_id)
172 {
173     VmGenIdState *vms = opaque;
174     vmgenid_update_guest(vms);
175     return 0;
176 }
177 
178 static const VMStateDescription vmstate_vmgenid = {
179     .name = "vmgenid",
180     .version_id = 1,
181     .minimum_version_id = 1,
182     .post_load = vmgenid_post_load,
183     .fields = (VMStateField[]) {
184         VMSTATE_UINT8_ARRAY(vmgenid_addr_le, VmGenIdState, sizeof(uint64_t)),
185         VMSTATE_END_OF_LIST()
186     },
187 };
188 
189 static void vmgenid_handle_reset(void *opaque)
190 {
191     VmGenIdState *vms = VMGENID(opaque);
192     /* Clear the guest-allocated GUID address when the VM resets */
193     memset(vms->vmgenid_addr_le, 0, ARRAY_SIZE(vms->vmgenid_addr_le));
194 }
195 
196 static void vmgenid_realize(DeviceState *dev, Error **errp)
197 {
198     VmGenIdState *vms = VMGENID(dev);
199 
200     if (!bios_linker_loader_can_write_pointer()) {
201         error_setg(errp, "%s requires DMA write support in fw_cfg, "
202                    "which this machine type does not provide", TYPE_VMGENID);
203         return;
204     }
205 
206     /* Given that this function is executing, there is at least one VMGENID
207      * device. Check if there are several.
208      */
209     if (!find_vmgenid_dev()) {
210         error_setg(errp, "at most one %s device is permitted", TYPE_VMGENID);
211         return;
212     }
213 
214     qemu_register_reset(vmgenid_handle_reset, vms);
215 
216     vmgenid_update_guest(vms);
217 }
218 
219 static Property vmgenid_device_properties[] = {
220     DEFINE_PROP_UUID(VMGENID_GUID, VmGenIdState, guid),
221     DEFINE_PROP_END_OF_LIST(),
222 };
223 
224 static void vmgenid_device_class_init(ObjectClass *klass, void *data)
225 {
226     DeviceClass *dc = DEVICE_CLASS(klass);
227 
228     dc->vmsd = &vmstate_vmgenid;
229     dc->realize = vmgenid_realize;
230     device_class_set_props(dc, vmgenid_device_properties);
231     dc->hotpluggable = false;
232     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
233 }
234 
235 static const TypeInfo vmgenid_device_info = {
236     .name          = TYPE_VMGENID,
237     .parent        = TYPE_DEVICE,
238     .instance_size = sizeof(VmGenIdState),
239     .class_init    = vmgenid_device_class_init,
240 };
241 
242 static void vmgenid_register_types(void)
243 {
244     type_register_static(&vmgenid_device_info);
245 }
246 
247 type_init(vmgenid_register_types)
248 
249 GuidInfo *qmp_query_vm_generation_id(Error **errp)
250 {
251     GuidInfo *info;
252     VmGenIdState *vms;
253     Object *obj = find_vmgenid_dev();
254 
255     if (!obj) {
256         error_setg(errp, "VM Generation ID device not found");
257         return NULL;
258     }
259     vms = VMGENID(obj);
260 
261     info = g_malloc0(sizeof(*info));
262     info->guid = qemu_uuid_unparse_strdup(&vms->guid);
263     return info;
264 }
265