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