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