2 * QTest testcase for VM Generation ID
4 * Copyright (c) 2016 Red Hat, Inc.
5 * Copyright (c) 2017 Skyport Systems
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
15 #include "qemu/bitmap.h"
16 #include "qemu/uuid.h"
17 #include "hw/acpi/acpi-defs.h"
18 #include "boot-sector.h"
19 #include "acpi-utils.h"
22 #define VGID_GUID "324e6eaf-d1d1-4bf6-bf41-b9bb6c91fb87"
23 #define VMGENID_GUID_OFFSET 40 /* allow space for
24 * OVMF SDT Header Probe Supressor
26 #define RSDP_ADDR_INVALID 0x100000 /* RSDP must be below this address */
29 AcpiTableHeader header
;
34 } QEMU_PACKED VgidTable
;
36 static uint32_t acpi_find_vgia(void)
39 uint32_t guid_offset
= 0;
40 AcpiRsdpDescriptor rsdp_table
;
41 uint32_t rsdt
, rsdt_table_length
;
42 AcpiRsdtDescriptorRev1 rsdt_table
;
45 AcpiTableHeader ssdt_table
;
49 /* Wait for guest firmware to finish and start the payload. */
52 /* Tables should be initialized now. */
53 rsdp_offset
= acpi_find_rsdp_address();
55 g_assert_cmphex(rsdp_offset
, <, RSDP_ADDR_INVALID
);
57 acpi_parse_rsdp_table(rsdp_offset
, &rsdp_table
);
59 rsdt
= le32_to_cpu(rsdp_table
.rsdt_physical_address
);
61 ACPI_READ_TABLE_HEADER(&rsdt_table
, rsdt
);
62 ACPI_ASSERT_CMP(rsdt_table
.signature
, "RSDT");
63 rsdt_table_length
= le32_to_cpu(rsdt_table
.length
);
65 /* compute the table entries in rsdt */
66 g_assert_cmpint(rsdt_table_length
, >, sizeof(AcpiRsdtDescriptorRev1
));
67 tables_nr
= (rsdt_table_length
- sizeof(AcpiRsdtDescriptorRev1
)) /
70 /* get the addresses of the tables pointed by rsdt */
71 tables
= g_new0(uint32_t, tables_nr
);
72 ACPI_READ_ARRAY_PTR(tables
, tables_nr
, rsdt
);
74 for (i
= 0; i
< tables_nr
; i
++) {
75 uint32_t addr
= le32_to_cpu(tables
[i
]);
76 ACPI_READ_TABLE_HEADER(&ssdt_table
, addr
);
77 if (!strncmp((char *)ssdt_table
.oem_table_id
, "VMGENID", 7)) {
78 /* the first entry in the table should be VGIA
81 ACPI_READ_FIELD(vgid_table
.name_op
, addr
);
82 g_assert(vgid_table
.name_op
== 0x08); /* name */
83 ACPI_READ_ARRAY(vgid_table
.vgia
, addr
);
84 g_assert(memcmp(vgid_table
.vgia
, "VGIA", 4) == 0);
85 ACPI_READ_FIELD(vgid_table
.val_op
, addr
);
86 g_assert(vgid_table
.val_op
== 0x0C); /* dword */
87 ACPI_READ_FIELD(vgid_table
.vgia_val
, addr
);
88 /* The GUID is written at a fixed offset into the fw_cfg file
89 * in order to implement the "OVMF SDT Header probe suppressor"
90 * see docs/specs/vmgenid.txt for more details
92 guid_offset
= le32_to_cpu(vgid_table
.vgia_val
) + VMGENID_GUID_OFFSET
;
100 static void read_guid_from_memory(QemuUUID
*guid
)
102 uint32_t vmgenid_addr
;
105 vmgenid_addr
= acpi_find_vgia();
106 g_assert(vmgenid_addr
);
108 /* Read the GUID directly from guest memory */
109 for (i
= 0; i
< 16; i
++) {
110 guid
->data
[i
] = readb(vmgenid_addr
+ i
);
112 /* The GUID is in little-endian format in the guest, while QEMU
113 * uses big-endian. Swap after reading.
115 qemu_uuid_bswap(guid
);
118 static void read_guid_from_monitor(QemuUUID
*guid
)
120 QDict
*rsp
, *rsp_ret
;
121 const char *guid_str
;
123 rsp
= qmp("{ 'execute': 'query-vm-generation-id' }");
124 if (qdict_haskey(rsp
, "return")) {
125 rsp_ret
= qdict_get_qdict(rsp
, "return");
126 g_assert(qdict_haskey(rsp_ret
, "guid"));
127 guid_str
= qdict_get_str(rsp_ret
, "guid");
128 g_assert(qemu_uuid_parse(guid_str
, guid
) == 0);
133 static char disk
[] = "tests/vmgenid-test-disk-XXXXXX";
135 #define GUID_CMD(guid) \
136 "-machine accel=kvm:tcg " \
137 "-device vmgenid,id=testvgid,guid=%s " \
138 "-drive id=hd0,if=none,file=%s,format=raw " \
139 "-device ide-hd,drive=hd0 ", guid, disk
141 static void vmgenid_set_guid_test(void)
143 QemuUUID expected
, measured
;
145 g_assert(qemu_uuid_parse(VGID_GUID
, &expected
) == 0);
147 global_qtest
= qtest_startf(GUID_CMD(VGID_GUID
));
149 /* Read the GUID from accessing guest memory */
150 read_guid_from_memory(&measured
);
151 g_assert(memcmp(measured
.data
, expected
.data
, sizeof(measured
.data
)) == 0);
153 qtest_quit(global_qtest
);
156 static void vmgenid_set_guid_auto_test(void)
160 global_qtest
= qtest_startf(GUID_CMD("auto"));
162 read_guid_from_memory(&measured
);
164 /* Just check that the GUID is non-null */
165 g_assert(!qemu_uuid_is_null(&measured
));
167 qtest_quit(global_qtest
);
170 static void vmgenid_query_monitor_test(void)
172 QemuUUID expected
, measured
;
174 g_assert(qemu_uuid_parse(VGID_GUID
, &expected
) == 0);
176 global_qtest
= qtest_startf(GUID_CMD(VGID_GUID
));
178 /* Read the GUID via the monitor */
179 read_guid_from_monitor(&measured
);
180 g_assert(memcmp(measured
.data
, expected
.data
, sizeof(measured
.data
)) == 0);
182 qtest_quit(global_qtest
);
185 int main(int argc
, char **argv
)
189 ret
= boot_sector_init(disk
);
194 g_test_init(&argc
, &argv
, NULL
);
196 qtest_add_func("/vmgenid/vmgenid/set-guid",
197 vmgenid_set_guid_test
);
198 qtest_add_func("/vmgenid/vmgenid/set-guid-auto",
199 vmgenid_set_guid_auto_test
);
200 qtest_add_func("/vmgenid/vmgenid/query-monitor",
201 vmgenid_query_monitor_test
);
203 boot_sector_cleanup(disk
);