master
c 191 lines 5.44 KB
Raw
1 /*
2 * QTest testcase for VM Generation ID
3 *
4 * Copyright (c) 2016 Red Hat, Inc.
5 * Copyright (c) 2017 Skyport Systems
6 *
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.
9 */
10
11 #include "qemu/osdep.h"
12 #include "qemu/bitmap.h"
13 #include "qemu/bswap.h"
14 #include "qemu/uuid.h"
15 #include "hw/acpi/acpi-defs.h"
16 #include "boot-sector.h"
17 #include "acpi-utils.h"
18 #include "libqtest.h"
19 #include "qobject/qdict.h"
20
21 #define VGID_GUID "324e6eaf-d1d1-4bf6-bf41-b9bb6c91fb87"
22 #define VMGENID_GUID_OFFSET 40 /* allow space for
23 * OVMF SDT Header Probe Suppressor
24 */
25 #define RSDP_ADDR_INVALID 0x100000 /* RSDP must be below this address */
26
27 static uint32_t acpi_find_vgia(QTestState *qts)
28 {
29 uint32_t rsdp_offset;
30 uint32_t guid_offset = 0;
31 uint8_t rsdp_table[36 /* ACPI 2.0+ RSDP size */];
32 uint32_t rsdt_len, table_length;
33 uint8_t *rsdt, *ent;
34
35 /* Wait for guest firmware to finish and start the payload. */
36 boot_sector_test(qts);
37
38 /* Tables should be initialized now. */
39 rsdp_offset = acpi_find_rsdp_address(qts);
40
41 g_assert_cmphex(rsdp_offset, <, RSDP_ADDR_INVALID);
42
43
44 acpi_fetch_rsdp_table(qts, rsdp_offset, rsdp_table);
45 acpi_fetch_table(qts, &rsdt, &rsdt_len, &rsdp_table[16 /* RsdtAddress */],
46 4, "RSDT", true);
47
48 ACPI_FOREACH_RSDT_ENTRY(rsdt, rsdt_len, ent, 4 /* Entry size */) {
49 uint8_t *table_aml;
50
51 acpi_fetch_table(qts, &table_aml, &table_length, ent, 4, NULL, true);
52 if (!memcmp(table_aml + 16 /* OEM Table ID */, "VMGENID", 7)) {
53 uint32_t vgia_val;
54 uint8_t *aml = &table_aml[36 /* AML byte-code start */];
55 /* the first entry in the table should be VGIA
56 * That's all we need
57 */
58 g_assert(aml[0 /* name_op*/] == 0x08);
59 g_assert(memcmp(&aml[1 /* name */], "VGIA", 4) == 0);
60 g_assert(aml[5 /* value op */] == 0x0C /* dword */);
61 memcpy(&vgia_val, &aml[6 /* value */], 4);
62
63 /* The GUID is written at a fixed offset into the fw_cfg file
64 * in order to implement the "OVMF SDT Header probe suppressor"
65 * see docs/specs/vmgenid.rst for more details
66 */
67 guid_offset = le32_to_cpu(vgia_val) + VMGENID_GUID_OFFSET;
68 g_free(table_aml);
69 break;
70 }
71 g_free(table_aml);
72 }
73 g_free(rsdt);
74 return guid_offset;
75 }
76
77 static void read_guid_from_memory(QTestState *qts, QemuUUID *guid)
78 {
79 uint32_t vmgenid_addr;
80 int i;
81
82 vmgenid_addr = acpi_find_vgia(qts);
83 g_assert(vmgenid_addr);
84
85 /* Read the GUID directly from guest memory */
86 for (i = 0; i < 16; i++) {
87 guid->data[i] = qtest_readb(qts, vmgenid_addr + i);
88 }
89 /* The GUID is in little-endian format in the guest, while QEMU
90 * uses big-endian. Swap after reading.
91 */
92 *guid = qemu_uuid_bswap(*guid);
93 }
94
95 static void read_guid_from_monitor(QTestState *qts, QemuUUID *guid)
96 {
97 QDict *rsp, *rsp_ret;
98 const char *guid_str;
99
100 rsp = qtest_qmp(qts, "{ 'execute': 'query-vm-generation-id' }");
101 if (qdict_haskey(rsp, "return")) {
102 rsp_ret = qdict_get_qdict(rsp, "return");
103 g_assert(qdict_haskey(rsp_ret, "guid"));
104 guid_str = qdict_get_str(rsp_ret, "guid");
105 g_assert(qemu_uuid_parse(guid_str, guid) == 0);
106 }
107 qobject_unref(rsp);
108 }
109
110 static char disk[] = "tests/vmgenid-test-disk-XXXXXX";
111
112 #define GUID_CMD(guid) \
113 "-accel kvm -accel tcg " \
114 "-device vmgenid,id=testvgid,guid=%s " \
115 "-drive id=hd0,if=none,file=%s,format=raw " \
116 "-device ide-hd,drive=hd0 ", guid, disk
117
118 static void vmgenid_set_guid_test(void)
119 {
120 QemuUUID expected, measured;
121 QTestState *qts;
122
123 g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
124
125 qts = qtest_initf(GUID_CMD(VGID_GUID));
126
127 /* Read the GUID from accessing guest memory */
128 read_guid_from_memory(qts, &measured);
129 g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);
130
131 qtest_quit(qts);
132 }
133
134 static void vmgenid_set_guid_auto_test(void)
135 {
136 QemuUUID measured;
137 QTestState *qts;
138
139 qts = qtest_initf(GUID_CMD("auto"));
140
141 read_guid_from_memory(qts, &measured);
142
143 /* Just check that the GUID is non-null */
144 g_assert(!qemu_uuid_is_null(&measured));
145
146 qtest_quit(qts);
147 }
148
149 static void vmgenid_query_monitor_test(void)
150 {
151 QemuUUID expected, measured;
152 QTestState *qts;
153
154 g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
155
156 qts = qtest_initf(GUID_CMD(VGID_GUID));
157
158 /* Read the GUID via the monitor */
159 read_guid_from_monitor(qts, &measured);
160 g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);
161
162 qtest_quit(qts);
163 }
164
165 int main(int argc, char **argv)
166 {
167 int ret;
168
169 g_test_init(&argc, &argv, NULL);
170
171 if (!qtest_has_accel("tcg") && !qtest_has_accel("kvm")) {
172 g_test_skip("No KVM or TCG accelerator available");
173 return 0;
174 }
175
176 ret = boot_sector_init(disk);
177 if (ret) {
178 return ret;
179 }
180
181 qtest_add_func("/vmgenid/vmgenid/set-guid",
182 vmgenid_set_guid_test);
183 qtest_add_func("/vmgenid/vmgenid/set-guid-auto",
184 vmgenid_set_guid_auto_test);
185 qtest_add_func("/vmgenid/vmgenid/query-monitor",
186 vmgenid_query_monitor_test);
187 ret = g_test_run();
188 boot_sector_cleanup(disk);
189
190 return ret;
191 }