vmgenid.c 8.6 KB

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