]> git.proxmox.com Git - mirror_qemu.git/blame - tests/vmgenid-test.c
tests/acpi-utils: Drop dependence on global_qtest
[mirror_qemu.git] / tests / vmgenid-test.c
CommitLineData
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"
452fcdbc 18#include "qapi/qmp/qdict.h"
83f3c709
BW
19
20#define VGID_GUID "324e6eaf-d1d1-4bf6-bf41-b9bb6c91fb87"
21#define VMGENID_GUID_OFFSET 40 /* allow space for
22 * OVMF SDT Header Probe Supressor
23 */
24#define RSDP_ADDR_INVALID 0x100000 /* RSDP must be below this address */
83f3c709
BW
25
26typedef struct {
27 AcpiTableHeader header;
28 gchar name_op;
29 gchar vgia[4];
30 gchar val_op;
31 uint32_t vgia_val;
32} QEMU_PACKED VgidTable;
33
273e3d92 34static uint32_t acpi_find_vgia(QTestState *qts)
83f3c709
BW
35{
36 uint32_t rsdp_offset;
37 uint32_t guid_offset = 0;
38 AcpiRsdpDescriptor rsdp_table;
3831c07b 39 uint32_t rsdt, rsdt_table_length;
83f3c709 40 AcpiRsdtDescriptorRev1 rsdt_table;
c18aaeb6 41 size_t tables_nr;
83f3c709
BW
42 uint32_t *tables;
43 AcpiTableHeader ssdt_table;
44 VgidTable vgid_table;
45 int i;
46
4871b51b 47 /* Wait for guest firmware to finish and start the payload. */
273e3d92 48 boot_sector_test(qts);
4871b51b
MT
49
50 /* Tables should be initialized now. */
273e3d92 51 rsdp_offset = acpi_find_rsdp_address(qts);
4871b51b 52
83f3c709
BW
53 g_assert_cmphex(rsdp_offset, <, RSDP_ADDR_INVALID);
54
273e3d92 55 acpi_parse_rsdp_table(qts, rsdp_offset, &rsdp_table);
83f3c709 56
3831c07b 57 rsdt = le32_to_cpu(rsdp_table.rsdt_physical_address);
83f3c709 58 /* read the header */
273e3d92 59 ACPI_READ_TABLE_HEADER(qts, &rsdt_table, rsdt);
83f3c709 60 ACPI_ASSERT_CMP(rsdt_table.signature, "RSDT");
3831c07b 61 rsdt_table_length = le32_to_cpu(rsdt_table.length);
83f3c709
BW
62
63 /* compute the table entries in rsdt */
3831c07b
TH
64 g_assert_cmpint(rsdt_table_length, >, sizeof(AcpiRsdtDescriptorRev1));
65 tables_nr = (rsdt_table_length - sizeof(AcpiRsdtDescriptorRev1)) /
83f3c709 66 sizeof(uint32_t);
83f3c709
BW
67
68 /* get the addresses of the tables pointed by rsdt */
69 tables = g_new0(uint32_t, tables_nr);
273e3d92 70 ACPI_READ_ARRAY_PTR(qts, tables, tables_nr, rsdt);
83f3c709
BW
71
72 for (i = 0; i < tables_nr; i++) {
3831c07b 73 uint32_t addr = le32_to_cpu(tables[i]);
273e3d92 74 ACPI_READ_TABLE_HEADER(qts, &ssdt_table, addr);
83f3c709
BW
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 */
273e3d92 79 ACPI_READ_FIELD(qts, vgid_table.name_op, addr);
83f3c709 80 g_assert(vgid_table.name_op == 0x08); /* name */
273e3d92 81 ACPI_READ_ARRAY(qts, vgid_table.vgia, addr);
83f3c709 82 g_assert(memcmp(vgid_table.vgia, "VGIA", 4) == 0);
273e3d92 83 ACPI_READ_FIELD(qts, vgid_table.val_op, addr);
83f3c709 84 g_assert(vgid_table.val_op == 0x0C); /* dword */
273e3d92 85 ACPI_READ_FIELD(qts, vgid_table.vgia_val, addr);
83f3c709
BW
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 */
3831c07b 90 guid_offset = le32_to_cpu(vgid_table.vgia_val) + VMGENID_GUID_OFFSET;
83f3c709
BW
91 break;
92 }
93 }
94 g_free(tables);
95 return guid_offset;
96}
97
98static void read_guid_from_memory(QemuUUID *guid)
99{
100 uint32_t vmgenid_addr;
101 int i;
102
273e3d92 103 vmgenid_addr = acpi_find_vgia(global_qtest);
83f3c709
BW
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
116static 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 }
cb3e7f08 128 qobject_unref(rsp);
83f3c709
BW
129}
130
4871b51b
MT
131static char disk[] = "tests/vmgenid-test-disk-XXXXXX";
132
78b27bad
EB
133#define GUID_CMD(guid) \
134 "-machine accel=kvm:tcg " \
135 "-device vmgenid,id=testvgid,guid=%s " \
136 "-drive id=hd0,if=none,file=%s,format=raw " \
137 "-device ide-hd,drive=hd0 ", guid, disk
4871b51b 138
83f3c709
BW
139static void vmgenid_set_guid_test(void)
140{
141 QemuUUID expected, measured;
83f3c709
BW
142
143 g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
144
88b988c8 145 global_qtest = qtest_initf(GUID_CMD(VGID_GUID));
83f3c709
BW
146
147 /* Read the GUID from accessing guest memory */
148 read_guid_from_memory(&measured);
149 g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);
150
151 qtest_quit(global_qtest);
83f3c709
BW
152}
153
154static void vmgenid_set_guid_auto_test(void)
155{
83f3c709
BW
156 QemuUUID measured;
157
88b988c8 158 global_qtest = qtest_initf(GUID_CMD("auto"));
83f3c709
BW
159
160 read_guid_from_memory(&measured);
161
162 /* Just check that the GUID is non-null */
163 g_assert(!qemu_uuid_is_null(&measured));
164
165 qtest_quit(global_qtest);
166}
167
168static void vmgenid_query_monitor_test(void)
169{
170 QemuUUID expected, measured;
83f3c709
BW
171
172 g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
173
88b988c8 174 global_qtest = qtest_initf(GUID_CMD(VGID_GUID));
83f3c709
BW
175
176 /* Read the GUID via the monitor */
177 read_guid_from_monitor(&measured);
178 g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);
179
180 qtest_quit(global_qtest);
83f3c709
BW
181}
182
183int main(int argc, char **argv)
184{
185 int ret;
186
4871b51b
MT
187 ret = boot_sector_init(disk);
188 if (ret) {
189 return ret;
190 }
191
83f3c709
BW
192 g_test_init(&argc, &argv, NULL);
193
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();
4871b51b 201 boot_sector_cleanup(disk);
83f3c709
BW
202
203 return ret;
204}