blob: 85d8e6463efcc7e0602bdcd97d26e8d710dc8f61 [file] [log] [blame]
Ben Warren83f3c702017-07-01 23:54:03 -04001/*
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
Ben Warren83f3c702017-07-01 23:54:03 -040011#include "qemu/osdep.h"
12#include "qemu/bitmap.h"
13#include "qemu/uuid.h"
14#include "hw/acpi/acpi-defs.h"
Michael S. Tsirkin4871b512017-07-14 18:30:45 +030015#include "boot-sector.h"
Ben Warren83f3c702017-07-01 23:54:03 -040016#include "acpi-utils.h"
17#include "libqtest.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010018#include "qapi/qmp/qdict.h"
Ben Warren83f3c702017-07-01 23:54:03 -040019
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 */
Ben Warren83f3c702017-07-01 23:54:03 -040025
Eric Blake273e3d92017-09-11 12:20:08 -050026static uint32_t acpi_find_vgia(QTestState *qts)
Ben Warren83f3c702017-07-01 23:54:03 -040027{
28 uint32_t rsdp_offset;
29 uint32_t guid_offset = 0;
Samuel Ortizd6caf362018-12-20 16:02:55 +010030 uint8_t rsdp_table[36 /* ACPI 2.0+ RSDP size */];
Igor Mammedovacee7742018-12-27 15:13:31 +010031 uint32_t rsdt_len, table_length;
32 uint8_t *rsdt, *ent;
Ben Warren83f3c702017-07-01 23:54:03 -040033
Michael S. Tsirkin4871b512017-07-14 18:30:45 +030034 /* Wait for guest firmware to finish and start the payload. */
Eric Blake273e3d92017-09-11 12:20:08 -050035 boot_sector_test(qts);
Michael S. Tsirkin4871b512017-07-14 18:30:45 +030036
37 /* Tables should be initialized now. */
Eric Blake273e3d92017-09-11 12:20:08 -050038 rsdp_offset = acpi_find_rsdp_address(qts);
Michael S. Tsirkin4871b512017-07-14 18:30:45 +030039
Ben Warren83f3c702017-07-01 23:54:03 -040040 g_assert_cmphex(rsdp_offset, <, RSDP_ADDR_INVALID);
41
Igor Mammedovacee7742018-12-27 15:13:31 +010042
Igor Mammedov38fb3d72019-05-02 16:51:49 +020043 acpi_fetch_rsdp_table(qts, rsdp_offset, rsdp_table);
Igor Mammedovacee7742018-12-27 15:13:31 +010044 acpi_fetch_table(qts, &rsdt, &rsdt_len, &rsdp_table[16 /* RsdtAddress */],
Igor Mammedov4b14d542019-05-02 16:51:50 +020045 4, "RSDT", true);
Ben Warren83f3c702017-07-01 23:54:03 -040046
Igor Mammedovacee7742018-12-27 15:13:31 +010047 ACPI_FOREACH_RSDT_ENTRY(rsdt, rsdt_len, ent, 4 /* Entry size */) {
48 uint8_t *table_aml;
Samuel Ortizd6caf362018-12-20 16:02:55 +010049
Igor Mammedov4b14d542019-05-02 16:51:50 +020050 acpi_fetch_table(qts, &table_aml, &table_length, ent, 4, NULL, true);
Igor Mammedovacee7742018-12-27 15:13:31 +010051 if (!memcmp(table_aml + 16 /* OEM Table ID */, "VMGENID", 7)) {
52 uint32_t vgia_val;
53 uint8_t *aml = &table_aml[36 /* AML byte-code start */];
Ben Warren83f3c702017-07-01 23:54:03 -040054 /* the first entry in the table should be VGIA
55 * That's all we need
56 */
Igor Mammedovacee7742018-12-27 15:13:31 +010057 g_assert(aml[0 /* name_op*/] == 0x08);
58 g_assert(memcmp(&aml[1 /* name */], "VGIA", 4) == 0);
59 g_assert(aml[5 /* value op */] == 0x0C /* dword */);
60 memcpy(&vgia_val, &aml[6 /* value */], 4);
61
Ben Warren83f3c702017-07-01 23:54:03 -040062 /* The GUID is written at a fixed offset into the fw_cfg file
63 * in order to implement the "OVMF SDT Header probe suppressor"
64 * see docs/specs/vmgenid.txt for more details
65 */
Igor Mammedovacee7742018-12-27 15:13:31 +010066 guid_offset = le32_to_cpu(vgia_val) + VMGENID_GUID_OFFSET;
67 g_free(table_aml);
Ben Warren83f3c702017-07-01 23:54:03 -040068 break;
69 }
Igor Mammedovacee7742018-12-27 15:13:31 +010070 g_free(table_aml);
Ben Warren83f3c702017-07-01 23:54:03 -040071 }
Igor Mammedovacee7742018-12-27 15:13:31 +010072 g_free(rsdt);
Ben Warren83f3c702017-07-01 23:54:03 -040073 return guid_offset;
74}
75
Thomas Huthac16ab72018-11-12 19:46:20 +010076static void read_guid_from_memory(QTestState *qts, QemuUUID *guid)
Ben Warren83f3c702017-07-01 23:54:03 -040077{
78 uint32_t vmgenid_addr;
79 int i;
80
Thomas Huthac16ab72018-11-12 19:46:20 +010081 vmgenid_addr = acpi_find_vgia(qts);
Ben Warren83f3c702017-07-01 23:54:03 -040082 g_assert(vmgenid_addr);
83
84 /* Read the GUID directly from guest memory */
85 for (i = 0; i < 16; i++) {
Thomas Huthac16ab72018-11-12 19:46:20 +010086 guid->data[i] = qtest_readb(qts, vmgenid_addr + i);
Ben Warren83f3c702017-07-01 23:54:03 -040087 }
88 /* The GUID is in little-endian format in the guest, while QEMU
89 * uses big-endian. Swap after reading.
90 */
Peter Maydell1324f062018-12-10 11:26:49 +000091 *guid = qemu_uuid_bswap(*guid);
Ben Warren83f3c702017-07-01 23:54:03 -040092}
93
Thomas Huthac16ab72018-11-12 19:46:20 +010094static void read_guid_from_monitor(QTestState *qts, QemuUUID *guid)
Ben Warren83f3c702017-07-01 23:54:03 -040095{
96 QDict *rsp, *rsp_ret;
97 const char *guid_str;
98
Thomas Huthac16ab72018-11-12 19:46:20 +010099 rsp = qtest_qmp(qts, "{ 'execute': 'query-vm-generation-id' }");
Ben Warren83f3c702017-07-01 23:54:03 -0400100 if (qdict_haskey(rsp, "return")) {
101 rsp_ret = qdict_get_qdict(rsp, "return");
102 g_assert(qdict_haskey(rsp_ret, "guid"));
103 guid_str = qdict_get_str(rsp_ret, "guid");
104 g_assert(qemu_uuid_parse(guid_str, guid) == 0);
105 }
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200106 qobject_unref(rsp);
Ben Warren83f3c702017-07-01 23:54:03 -0400107}
108
Michael S. Tsirkin4871b512017-07-14 18:30:45 +0300109static char disk[] = "tests/vmgenid-test-disk-XXXXXX";
110
Eric Blake78b27ba2017-10-18 16:20:27 +0200111#define GUID_CMD(guid) \
112 "-machine accel=kvm:tcg " \
113 "-device vmgenid,id=testvgid,guid=%s " \
114 "-drive id=hd0,if=none,file=%s,format=raw " \
115 "-device ide-hd,drive=hd0 ", guid, disk
Michael S. Tsirkin4871b512017-07-14 18:30:45 +0300116
Ben Warren83f3c702017-07-01 23:54:03 -0400117static void vmgenid_set_guid_test(void)
118{
119 QemuUUID expected, measured;
Thomas Huthac16ab72018-11-12 19:46:20 +0100120 QTestState *qts;
Ben Warren83f3c702017-07-01 23:54:03 -0400121
122 g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
123
Thomas Huthac16ab72018-11-12 19:46:20 +0100124 qts = qtest_initf(GUID_CMD(VGID_GUID));
Ben Warren83f3c702017-07-01 23:54:03 -0400125
126 /* Read the GUID from accessing guest memory */
Thomas Huthac16ab72018-11-12 19:46:20 +0100127 read_guid_from_memory(qts, &measured);
Ben Warren83f3c702017-07-01 23:54:03 -0400128 g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);
129
Thomas Huthac16ab72018-11-12 19:46:20 +0100130 qtest_quit(qts);
Ben Warren83f3c702017-07-01 23:54:03 -0400131}
132
133static void vmgenid_set_guid_auto_test(void)
134{
Ben Warren83f3c702017-07-01 23:54:03 -0400135 QemuUUID measured;
Thomas Huthac16ab72018-11-12 19:46:20 +0100136 QTestState *qts;
Ben Warren83f3c702017-07-01 23:54:03 -0400137
Thomas Huthac16ab72018-11-12 19:46:20 +0100138 qts = qtest_initf(GUID_CMD("auto"));
Ben Warren83f3c702017-07-01 23:54:03 -0400139
Thomas Huthac16ab72018-11-12 19:46:20 +0100140 read_guid_from_memory(qts, &measured);
Ben Warren83f3c702017-07-01 23:54:03 -0400141
142 /* Just check that the GUID is non-null */
143 g_assert(!qemu_uuid_is_null(&measured));
144
Thomas Huthac16ab72018-11-12 19:46:20 +0100145 qtest_quit(qts);
Ben Warren83f3c702017-07-01 23:54:03 -0400146}
147
148static void vmgenid_query_monitor_test(void)
149{
150 QemuUUID expected, measured;
Thomas Huthac16ab72018-11-12 19:46:20 +0100151 QTestState *qts;
Ben Warren83f3c702017-07-01 23:54:03 -0400152
153 g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0);
154
Thomas Huthac16ab72018-11-12 19:46:20 +0100155 qts = qtest_initf(GUID_CMD(VGID_GUID));
Ben Warren83f3c702017-07-01 23:54:03 -0400156
157 /* Read the GUID via the monitor */
Thomas Huthac16ab72018-11-12 19:46:20 +0100158 read_guid_from_monitor(qts, &measured);
Ben Warren83f3c702017-07-01 23:54:03 -0400159 g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0);
160
Thomas Huthac16ab72018-11-12 19:46:20 +0100161 qtest_quit(qts);
Ben Warren83f3c702017-07-01 23:54:03 -0400162}
163
164int main(int argc, char **argv)
165{
166 int ret;
167
Michael S. Tsirkin4871b512017-07-14 18:30:45 +0300168 ret = boot_sector_init(disk);
169 if (ret) {
170 return ret;
171 }
172
Ben Warren83f3c702017-07-01 23:54:03 -0400173 g_test_init(&argc, &argv, NULL);
174
175 qtest_add_func("/vmgenid/vmgenid/set-guid",
176 vmgenid_set_guid_test);
177 qtest_add_func("/vmgenid/vmgenid/set-guid-auto",
178 vmgenid_set_guid_auto_test);
179 qtest_add_func("/vmgenid/vmgenid/query-monitor",
180 vmgenid_query_monitor_test);
181 ret = g_test_run();
Michael S. Tsirkin4871b512017-07-14 18:30:45 +0300182 boot_sector_cleanup(disk);
Ben Warren83f3c702017-07-01 23:54:03 -0400183
184 return ret;
185}