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.
11 #include "qemu/osdep.h"
12 #include "qemu/bitmap.h"
13 #include "qemu/uuid.h"
14 #include "hw/acpi/acpi-defs.h"
15 #include "boot-sector.h"
16 #include "acpi-utils.h"
18 #include "qapi/qmp/qdict.h"
20 #define VGID_GUID "324e6eaf-d1d1-4bf6-bf41-b9bb6c91fb87"
21 #define VMGENID_GUID_OFFSET 40 /* allow space for
22 * OVMF SDT Header Probe Suppressor
24 #define RSDP_ADDR_INVALID 0x100000 /* RSDP must be below this address */
26 static uint32_t acpi_find_vgia(QTestState
*qts
)
29 uint32_t guid_offset
= 0;
30 uint8_t rsdp_table
[36 /* ACPI 2.0+ RSDP size */];
31 uint32_t rsdt_len
, table_length
;
34 /* Wait for guest firmware to finish and start the payload. */
35 boot_sector_test(qts
);
37 /* Tables should be initialized now. */
38 rsdp_offset
= acpi_find_rsdp_address(qts
);
40 g_assert_cmphex(rsdp_offset
, <, RSDP_ADDR_INVALID
);
43 acpi_fetch_rsdp_table(qts
, rsdp_offset
, rsdp_table
);
44 acpi_fetch_table(qts
, &rsdt
, &rsdt_len
, &rsdp_table
[16 /* RsdtAddress */],
47 ACPI_FOREACH_RSDT_ENTRY(rsdt
, rsdt_len
, ent
, 4 /* Entry size */) {
50 acpi_fetch_table(qts
, &table_aml
, &table_length
, ent
, 4, NULL
, true);
51 if (!memcmp(table_aml
+ 16 /* OEM Table ID */, "VMGENID", 7)) {
53 uint8_t *aml
= &table_aml
[36 /* AML byte-code start */];
54 /* the first entry in the table should be VGIA
57 g_assert(aml
[0 /* name_op*/] == 0x08);
58 g_assert(memcmp(&aml
[1 /* name */], "VGIA", 4) == 0);
59 g_assert(aml
[5 /* value op */] == 0x0C /* dword */);
60 memcpy(&vgia_val
, &aml
[6 /* value */], 4);
62 /* The GUID is written at a fixed offset into the fw_cfg file
63 * in order to implement the "OVMF SDT Header probe suppressor"
64 * see docs/specs/vmgenid.txt for more details
66 guid_offset
= le32_to_cpu(vgia_val
) + VMGENID_GUID_OFFSET
;
76 static void read_guid_from_memory(QTestState
*qts
, QemuUUID
*guid
)
78 uint32_t vmgenid_addr
;
81 vmgenid_addr
= acpi_find_vgia(qts
);
82 g_assert(vmgenid_addr
);
84 /* Read the GUID directly from guest memory */
85 for (i
= 0; i
< 16; i
++) {
86 guid
->data
[i
] = qtest_readb(qts
, vmgenid_addr
+ i
);
88 /* The GUID is in little-endian format in the guest, while QEMU
89 * uses big-endian. Swap after reading.
91 *guid
= qemu_uuid_bswap(*guid
);
94 static void read_guid_from_monitor(QTestState
*qts
, QemuUUID
*guid
)
99 rsp
= qtest_qmp(qts
, "{ 'execute': 'query-vm-generation-id' }");
100 if (qdict_haskey(rsp
, "return")) {
101 rsp_ret
= qdict_get_qdict(rsp
, "return");
102 g_assert(qdict_haskey(rsp_ret
, "guid"));
103 guid_str
= qdict_get_str(rsp_ret
, "guid");
104 g_assert(qemu_uuid_parse(guid_str
, guid
) == 0);
109 static char disk
[] = "tests/vmgenid-test-disk-XXXXXX";
111 #define GUID_CMD(guid) \
112 "-accel kvm -accel tcg " \
113 "-device vmgenid,id=testvgid,guid=%s " \
114 "-drive id=hd0,if=none,file=%s,format=raw " \
115 "-device ide-hd,drive=hd0 ", guid, disk
117 static void vmgenid_set_guid_test(void)
119 QemuUUID expected
, measured
;
122 g_assert(qemu_uuid_parse(VGID_GUID
, &expected
) == 0);
124 qts
= qtest_initf(GUID_CMD(VGID_GUID
));
126 /* Read the GUID from accessing guest memory */
127 read_guid_from_memory(qts
, &measured
);
128 g_assert(memcmp(measured
.data
, expected
.data
, sizeof(measured
.data
)) == 0);
133 static void vmgenid_set_guid_auto_test(void)
138 qts
= qtest_initf(GUID_CMD("auto"));
140 read_guid_from_memory(qts
, &measured
);
142 /* Just check that the GUID is non-null */
143 g_assert(!qemu_uuid_is_null(&measured
));
148 static void vmgenid_query_monitor_test(void)
150 QemuUUID expected
, measured
;
153 g_assert(qemu_uuid_parse(VGID_GUID
, &expected
) == 0);
155 qts
= qtest_initf(GUID_CMD(VGID_GUID
));
157 /* Read the GUID via the monitor */
158 read_guid_from_monitor(qts
, &measured
);
159 g_assert(memcmp(measured
.data
, expected
.data
, sizeof(measured
.data
)) == 0);
164 int main(int argc
, char **argv
)
168 g_test_init(&argc
, &argv
, NULL
);
170 if (!qtest_has_accel("tcg") && !qtest_has_accel("kvm")) {
171 g_test_skip("No KVM or TCG accelerator available");
175 ret
= boot_sector_init(disk
);
180 qtest_add_func("/vmgenid/vmgenid/set-guid",
181 vmgenid_set_guid_test
);
182 qtest_add_func("/vmgenid/vmgenid/set-guid-auto",
183 vmgenid_set_guid_auto_test
);
184 qtest_add_func("/vmgenid/vmgenid/query-monitor",
185 vmgenid_query_monitor_test
);
187 boot_sector_cleanup(disk
);