]>
Commit | Line | Data |
---|---|---|
83f3c709 BW |
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 <glib.h> | |
12 | #include <string.h> | |
13 | #include <unistd.h> | |
14 | #include "qemu/osdep.h" | |
15 | #include "qemu/bitmap.h" | |
16 | #include "qemu/uuid.h" | |
17 | #include "hw/acpi/acpi-defs.h" | |
4871b51b | 18 | #include "boot-sector.h" |
83f3c709 BW |
19 | #include "acpi-utils.h" |
20 | #include "libqtest.h" | |
21 | ||
22 | #define VGID_GUID "324e6eaf-d1d1-4bf6-bf41-b9bb6c91fb87" | |
23 | #define VMGENID_GUID_OFFSET 40 /* allow space for | |
24 | * OVMF SDT Header Probe Supressor | |
25 | */ | |
26 | #define RSDP_ADDR_INVALID 0x100000 /* RSDP must be below this address */ | |
83f3c709 BW |
27 | |
28 | typedef struct { | |
29 | AcpiTableHeader header; | |
30 | gchar name_op; | |
31 | gchar vgia[4]; | |
32 | gchar val_op; | |
33 | uint32_t vgia_val; | |
34 | } QEMU_PACKED VgidTable; | |
35 | ||
36 | static uint32_t acpi_find_vgia(void) | |
37 | { | |
38 | uint32_t rsdp_offset; | |
39 | uint32_t guid_offset = 0; | |
40 | AcpiRsdpDescriptor rsdp_table; | |
41 | uint32_t rsdt; | |
42 | AcpiRsdtDescriptorRev1 rsdt_table; | |
c18aaeb6 | 43 | size_t tables_nr; |
83f3c709 BW |
44 | uint32_t *tables; |
45 | AcpiTableHeader ssdt_table; | |
46 | VgidTable vgid_table; | |
47 | int i; | |
48 | ||
4871b51b MT |
49 | /* Wait for guest firmware to finish and start the payload. */ |
50 | boot_sector_test(); | |
51 | ||
52 | /* Tables should be initialized now. */ | |
53 | rsdp_offset = acpi_find_rsdp_address(); | |
54 | ||
83f3c709 BW |
55 | g_assert_cmphex(rsdp_offset, <, RSDP_ADDR_INVALID); |
56 | ||
57 | acpi_parse_rsdp_table(rsdp_offset, &rsdp_table); | |
58 | ||
59 | rsdt = rsdp_table.rsdt_physical_address; | |
60 | /* read the header */ | |
61 | ACPI_READ_TABLE_HEADER(&rsdt_table, rsdt); | |
62 | ACPI_ASSERT_CMP(rsdt_table.signature, "RSDT"); | |
63 | ||
64 | /* compute the table entries in rsdt */ | |
c18aaeb6 | 65 | g_assert_cmpint(rsdt_table.length, >, sizeof(AcpiRsdtDescriptorRev1)); |
83f3c709 BW |
66 | tables_nr = (rsdt_table.length - sizeof(AcpiRsdtDescriptorRev1)) / |
67 | sizeof(uint32_t); | |
83f3c709 BW |
68 | |
69 | /* get the addresses of the tables pointed by rsdt */ | |
70 | tables = g_new0(uint32_t, tables_nr); | |
71 | ACPI_READ_ARRAY_PTR(tables, tables_nr, rsdt); | |
72 | ||
73 | for (i = 0; i < tables_nr; i++) { | |
74 | ACPI_READ_TABLE_HEADER(&ssdt_table, tables[i]); | |
75 | if (!strncmp((char *)ssdt_table.oem_table_id, "VMGENID", 7)) { | |
76 | /* the first entry in the table should be VGIA | |
77 | * That's all we need | |
78 | */ | |
79 | ACPI_READ_FIELD(vgid_table.name_op, tables[i]); | |
80 | g_assert(vgid_table.name_op == 0x08); /* name */ | |
81 | ACPI_READ_ARRAY(vgid_table.vgia, tables[i]); | |
82 | g_assert(memcmp(vgid_table.vgia, "VGIA", 4) == 0); | |
83 | ACPI_READ_FIELD(vgid_table.val_op, tables[i]); | |
84 | g_assert(vgid_table.val_op == 0x0C); /* dword */ | |
85 | ACPI_READ_FIELD(vgid_table.vgia_val, tables[i]); | |
86 | /* The GUID is written at a fixed offset into the fw_cfg file | |
87 | * in order to implement the "OVMF SDT Header probe suppressor" | |
88 | * see docs/specs/vmgenid.txt for more details | |
89 | */ | |
90 | guid_offset = vgid_table.vgia_val + VMGENID_GUID_OFFSET; | |
91 | break; | |
92 | } | |
93 | } | |
94 | g_free(tables); | |
95 | return guid_offset; | |
96 | } | |
97 | ||
98 | static void read_guid_from_memory(QemuUUID *guid) | |
99 | { | |
100 | uint32_t vmgenid_addr; | |
101 | int i; | |
102 | ||
103 | vmgenid_addr = acpi_find_vgia(); | |
104 | g_assert(vmgenid_addr); | |
105 | ||
106 | /* Read the GUID directly from guest memory */ | |
107 | for (i = 0; i < 16; i++) { | |
108 | guid->data[i] = readb(vmgenid_addr + i); | |
109 | } | |
110 | /* The GUID is in little-endian format in the guest, while QEMU | |
111 | * uses big-endian. Swap after reading. | |
112 | */ | |
113 | qemu_uuid_bswap(guid); | |
114 | } | |
115 | ||
116 | static void read_guid_from_monitor(QemuUUID *guid) | |
117 | { | |
118 | QDict *rsp, *rsp_ret; | |
119 | const char *guid_str; | |
120 | ||
121 | rsp = qmp("{ 'execute': 'query-vm-generation-id' }"); | |
122 | if (qdict_haskey(rsp, "return")) { | |
123 | rsp_ret = qdict_get_qdict(rsp, "return"); | |
124 | g_assert(qdict_haskey(rsp_ret, "guid")); | |
125 | guid_str = qdict_get_str(rsp_ret, "guid"); | |
126 | g_assert(qemu_uuid_parse(guid_str, guid) == 0); | |
127 | } | |
128 | QDECREF(rsp); | |
129 | } | |
130 | ||
4871b51b MT |
131 | static char disk[] = "tests/vmgenid-test-disk-XXXXXX"; |
132 | ||
133 | static char *guid_cmd_strdup(const char *guid) | |
134 | { | |
2cef91cf | 135 | return g_strdup_printf("-machine accel=kvm:tcg " |
4871b51b MT |
136 | "-device vmgenid,id=testvgid,guid=%s " |
137 | "-drive id=hd0,if=none,file=%s,format=raw " | |
138 | "-device ide-hd,drive=hd0 ", | |
139 | guid, disk); | |
140 | } | |
141 | ||
142 | ||
83f3c709 BW |
143 | static void vmgenid_set_guid_test(void) |
144 | { | |
145 | QemuUUID expected, measured; | |
146 | gchar *cmd; | |
147 | ||
148 | g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0); | |
149 | ||
4871b51b | 150 | cmd = guid_cmd_strdup(VGID_GUID); |
83f3c709 BW |
151 | qtest_start(cmd); |
152 | ||
153 | /* Read the GUID from accessing guest memory */ | |
154 | read_guid_from_memory(&measured); | |
155 | g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0); | |
156 | ||
157 | qtest_quit(global_qtest); | |
158 | g_free(cmd); | |
159 | } | |
160 | ||
161 | static void vmgenid_set_guid_auto_test(void) | |
162 | { | |
4871b51b | 163 | char *cmd; |
83f3c709 BW |
164 | QemuUUID measured; |
165 | ||
4871b51b | 166 | cmd = guid_cmd_strdup("auto"); |
83f3c709 BW |
167 | qtest_start(cmd); |
168 | ||
169 | read_guid_from_memory(&measured); | |
170 | ||
171 | /* Just check that the GUID is non-null */ | |
172 | g_assert(!qemu_uuid_is_null(&measured)); | |
173 | ||
174 | qtest_quit(global_qtest); | |
4871b51b | 175 | g_free(cmd); |
83f3c709 BW |
176 | } |
177 | ||
178 | static void vmgenid_query_monitor_test(void) | |
179 | { | |
180 | QemuUUID expected, measured; | |
181 | gchar *cmd; | |
182 | ||
183 | g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0); | |
184 | ||
4871b51b | 185 | cmd = guid_cmd_strdup(VGID_GUID); |
83f3c709 BW |
186 | qtest_start(cmd); |
187 | ||
188 | /* Read the GUID via the monitor */ | |
189 | read_guid_from_monitor(&measured); | |
190 | g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0); | |
191 | ||
192 | qtest_quit(global_qtest); | |
193 | g_free(cmd); | |
194 | } | |
195 | ||
196 | int main(int argc, char **argv) | |
197 | { | |
198 | int ret; | |
199 | ||
4871b51b MT |
200 | ret = boot_sector_init(disk); |
201 | if (ret) { | |
202 | return ret; | |
203 | } | |
204 | ||
83f3c709 BW |
205 | g_test_init(&argc, &argv, NULL); |
206 | ||
207 | qtest_add_func("/vmgenid/vmgenid/set-guid", | |
208 | vmgenid_set_guid_test); | |
209 | qtest_add_func("/vmgenid/vmgenid/set-guid-auto", | |
210 | vmgenid_set_guid_auto_test); | |
211 | qtest_add_func("/vmgenid/vmgenid/query-monitor", | |
212 | vmgenid_query_monitor_test); | |
213 | ret = g_test_run(); | |
4871b51b | 214 | boot_sector_cleanup(disk); |
83f3c709 BW |
215 | |
216 | return ret; | |
217 | } |