gdbstub: don't fail on vCont; C04:0; c packets
[qemu/ar7.git] / tests / vmgenid-test.c
blobe7ba38c9d1ceb25c93fa6827f95b77e07f583184
1 /*
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.
9 */
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"
18 #include "acpi-utils.h"
19 #include "libqtest.h"
21 #define VGID_GUID "324e6eaf-d1d1-4bf6-bf41-b9bb6c91fb87"
22 #define VMGENID_GUID_OFFSET 40 /* allow space for
23 * OVMF SDT Header Probe Supressor
25 #define RSDP_ADDR_INVALID 0x100000 /* RSDP must be below this address */
26 #define RSDP_SLEEP_US 100000 /* Sleep for 100ms between tries */
27 #define RSDP_TRIES_MAX 100 /* Max total time is 10 seconds */
29 typedef struct {
30 AcpiTableHeader header;
31 gchar name_op;
32 gchar vgia[4];
33 gchar val_op;
34 uint32_t vgia_val;
35 } QEMU_PACKED VgidTable;
37 static uint32_t acpi_find_vgia(void)
39 uint32_t rsdp_offset;
40 uint32_t guid_offset = 0;
41 AcpiRsdpDescriptor rsdp_table;
42 uint32_t rsdt;
43 AcpiRsdtDescriptorRev1 rsdt_table;
44 int tables_nr;
45 uint32_t *tables;
46 AcpiTableHeader ssdt_table;
47 VgidTable vgid_table;
48 int i;
50 /* Tables may take a short time to be set up by the guest */
51 for (i = 0; i < RSDP_TRIES_MAX; i++) {
52 rsdp_offset = acpi_find_rsdp_address();
53 if (rsdp_offset < RSDP_ADDR_INVALID) {
54 break;
56 g_usleep(RSDP_SLEEP_US);
58 g_assert_cmphex(rsdp_offset, <, RSDP_ADDR_INVALID);
60 acpi_parse_rsdp_table(rsdp_offset, &rsdp_table);
62 rsdt = rsdp_table.rsdt_physical_address;
63 /* read the header */
64 ACPI_READ_TABLE_HEADER(&rsdt_table, rsdt);
65 ACPI_ASSERT_CMP(rsdt_table.signature, "RSDT");
67 /* compute the table entries in rsdt */
68 tables_nr = (rsdt_table.length - sizeof(AcpiRsdtDescriptorRev1)) /
69 sizeof(uint32_t);
70 g_assert_cmpint(tables_nr, >, 0);
72 /* get the addresses of the tables pointed by rsdt */
73 tables = g_new0(uint32_t, tables_nr);
74 ACPI_READ_ARRAY_PTR(tables, tables_nr, rsdt);
76 for (i = 0; i < tables_nr; i++) {
77 ACPI_READ_TABLE_HEADER(&ssdt_table, tables[i]);
78 if (!strncmp((char *)ssdt_table.oem_table_id, "VMGENID", 7)) {
79 /* the first entry in the table should be VGIA
80 * That's all we need
82 ACPI_READ_FIELD(vgid_table.name_op, tables[i]);
83 g_assert(vgid_table.name_op == 0x08); /* name */
84 ACPI_READ_ARRAY(vgid_table.vgia, tables[i]);
85 g_assert(memcmp(vgid_table.vgia, "VGIA", 4) == 0);
86 ACPI_READ_FIELD(vgid_table.val_op, tables[i]);
87 g_assert(vgid_table.val_op == 0x0C); /* dword */
88 ACPI_READ_FIELD(vgid_table.vgia_val, tables[i]);
89 /* The GUID is written at a fixed offset into the fw_cfg file
90 * in order to implement the "OVMF SDT Header probe suppressor"
91 * see docs/specs/vmgenid.txt for more details
93 guid_offset = vgid_table.vgia_val + VMGENID_GUID_OFFSET;
94 break;
97 g_free(tables);
98 return guid_offset;
101 static void read_guid_from_memory(QemuUUID *guid)
103 uint32_t vmgenid_addr;
104 int i;
106 vmgenid_addr = acpi_find_vgia();
107 g_assert(vmgenid_addr);
109 /* Read the GUID directly from guest memory */
110 for (i = 0; i < 16; i++) {
111 guid->data[i] = readb(vmgenid_addr + i);
113 /* The GUID is in little-endian format in the guest, while QEMU
114 * uses big-endian. Swap after reading.
116 qemu_uuid_bswap(guid);
119 static void read_guid_from_monitor(QemuUUID *guid)
121 QDict *rsp, *rsp_ret;
122 const char *guid_str;
124 rsp = qmp("{ 'execute': 'query-vm-generation-id' }");
125 if (qdict_haskey(rsp, "return")) {
126 rsp_ret = qdict_get_qdict(rsp, "return");
127 g_assert(qdict_haskey(rsp_ret, "guid"));
128 guid_str = qdict_get_str(rsp_ret, "guid");
129 g_assert(qemu_uuid_parse(guid_str, guid) == 0);
131 QDECREF(rsp);
134 static void vmgenid_set_guid_test(void)
136 QemuUUID expected, measured;
137 gchar *cmd;
139 g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
141 cmd = g_strdup_printf("-machine accel=tcg -device vmgenid,id=testvgid,"
142 "guid=%s", VGID_GUID);
143 qtest_start(cmd);
145 /* Read the GUID from accessing guest memory */
146 read_guid_from_memory(&measured);
147 g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);
149 qtest_quit(global_qtest);
150 g_free(cmd);
153 static void vmgenid_set_guid_auto_test(void)
155 const char *cmd;
156 QemuUUID measured;
158 cmd = "-machine accel=tcg -device vmgenid,id=testvgid," "guid=auto";
159 qtest_start(cmd);
161 read_guid_from_memory(&measured);
163 /* Just check that the GUID is non-null */
164 g_assert(!qemu_uuid_is_null(&measured));
166 qtest_quit(global_qtest);
169 static void vmgenid_query_monitor_test(void)
171 QemuUUID expected, measured;
172 gchar *cmd;
174 g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
176 cmd = g_strdup_printf("-machine accel=tcg -device vmgenid,id=testvgid,"
177 "guid=%s", VGID_GUID);
178 qtest_start(cmd);
180 /* Read the GUID via the monitor */
181 read_guid_from_monitor(&measured);
182 g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);
184 qtest_quit(global_qtest);
185 g_free(cmd);
188 int main(int argc, char **argv)
190 int ret;
192 g_test_init(&argc, &argv, NULL);
194 qtest_add_func("/vmgenid/vmgenid/set-guid",
195 vmgenid_set_guid_test);
196 qtest_add_func("/vmgenid/vmgenid/set-guid-auto",
197 vmgenid_set_guid_auto_test);
198 qtest_add_func("/vmgenid/vmgenid/query-monitor",
199 vmgenid_query_monitor_test);
200 ret = g_test_run();
202 return ret;