| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * QTest testcase for VM Generation ID | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Copyright (c) 2016 Red Hat, Inc. | 
					
						
							|  |  |  |  * Copyright (c) 2017 Skyport Systems | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This work is licensed under the terms of the GNU GPL, version 2 or later. | 
					
						
							|  |  |  |  * See the COPYING file in the top-level directory. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "qemu/osdep.h"
 | 
					
						
							|  |  |  | #include "qemu/bitmap.h"
 | 
					
						
							|  |  |  | #include "qemu/uuid.h"
 | 
					
						
							|  |  |  | #include "hw/acpi/acpi-defs.h"
 | 
					
						
							| 
									
										
										
										
											2017-07-14 18:30:45 +03:00
										 |  |  | #include "boot-sector.h"
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | #include "acpi-utils.h"
 | 
					
						
							| 
									
										
										
										
											2022-03-30 13:39:05 +04:00
										 |  |  | #include "libqtest.h"
 | 
					
						
							| 
									
										
										
										
											2018-02-01 12:18:39 +01:00
										 |  |  | #include "qapi/qmp/qdict.h"
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define VGID_GUID "324e6eaf-d1d1-4bf6-bf41-b9bb6c91fb87"
 | 
					
						
							|  |  |  | #define VMGENID_GUID_OFFSET 40   /* allow space for
 | 
					
						
							|  |  |  |                                   * OVMF SDT Header Probe Supressor | 
					
						
							|  |  |  |                                   */ | 
					
						
							|  |  |  | #define RSDP_ADDR_INVALID 0x100000 /* RSDP must be below this address */
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-11 12:20:08 -05:00
										 |  |  | static uint32_t acpi_find_vgia(QTestState *qts) | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     uint32_t rsdp_offset; | 
					
						
							|  |  |  |     uint32_t guid_offset = 0; | 
					
						
							| 
									
										
										
										
											2018-12-20 16:02:55 +01:00
										 |  |  |     uint8_t rsdp_table[36 /* ACPI 2.0+ RSDP size */]; | 
					
						
							| 
									
										
										
										
											2018-12-27 15:13:31 +01:00
										 |  |  |     uint32_t rsdt_len, table_length; | 
					
						
							|  |  |  |     uint8_t *rsdt, *ent; | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-14 18:30:45 +03:00
										 |  |  |     /* Wait for guest firmware to finish and start the payload. */ | 
					
						
							| 
									
										
										
										
											2017-09-11 12:20:08 -05:00
										 |  |  |     boot_sector_test(qts); | 
					
						
							| 
									
										
										
										
											2017-07-14 18:30:45 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Tables should be initialized now. */ | 
					
						
							| 
									
										
										
										
											2017-09-11 12:20:08 -05:00
										 |  |  |     rsdp_offset = acpi_find_rsdp_address(qts); | 
					
						
							| 
									
										
										
										
											2017-07-14 18:30:45 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |     g_assert_cmphex(rsdp_offset, <, RSDP_ADDR_INVALID); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 16:51:49 +02:00
										 |  |  |     acpi_fetch_rsdp_table(qts, rsdp_offset, rsdp_table); | 
					
						
							| 
									
										
										
										
											2018-12-27 15:13:31 +01:00
										 |  |  |     acpi_fetch_table(qts, &rsdt, &rsdt_len, &rsdp_table[16 /* RsdtAddress */], | 
					
						
							| 
									
										
										
										
											2019-05-02 16:51:50 +02:00
										 |  |  |                      4, "RSDT", true); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-27 15:13:31 +01:00
										 |  |  |     ACPI_FOREACH_RSDT_ENTRY(rsdt, rsdt_len, ent, 4 /* Entry size */) { | 
					
						
							|  |  |  |         uint8_t *table_aml; | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-05-02 16:51:50 +02:00
										 |  |  |         acpi_fetch_table(qts, &table_aml, &table_length, ent, 4, NULL, true); | 
					
						
							| 
									
										
										
										
											2018-12-27 15:13:31 +01:00
										 |  |  |         if (!memcmp(table_aml + 16 /* OEM Table ID */, "VMGENID", 7)) { | 
					
						
							|  |  |  |             uint32_t vgia_val; | 
					
						
							|  |  |  |             uint8_t *aml = &table_aml[36 /* AML byte-code start */]; | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |             /* the first entry in the table should be VGIA
 | 
					
						
							|  |  |  |              * That's all we need | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2018-12-27 15:13:31 +01:00
										 |  |  |             g_assert(aml[0 /* name_op*/] == 0x08); | 
					
						
							|  |  |  |             g_assert(memcmp(&aml[1 /* name */], "VGIA", 4) == 0); | 
					
						
							|  |  |  |             g_assert(aml[5 /* value op */] == 0x0C /* dword */); | 
					
						
							|  |  |  |             memcpy(&vgia_val, &aml[6 /* value */], 4); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |             /* The GUID is written at a fixed offset into the fw_cfg file
 | 
					
						
							|  |  |  |              * in order to implement the "OVMF SDT Header probe suppressor" | 
					
						
							|  |  |  |              * see docs/specs/vmgenid.txt for more details | 
					
						
							|  |  |  |              */ | 
					
						
							| 
									
										
										
										
											2018-12-27 15:13:31 +01:00
										 |  |  |             guid_offset = le32_to_cpu(vgia_val) + VMGENID_GUID_OFFSET; | 
					
						
							|  |  |  |             g_free(table_aml); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |             break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-12-27 15:13:31 +01:00
										 |  |  |         g_free(table_aml); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-12-27 15:13:31 +01:00
										 |  |  |     g_free(rsdt); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |     return guid_offset; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  | static void read_guid_from_memory(QTestState *qts, QemuUUID *guid) | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     uint32_t vmgenid_addr; | 
					
						
							|  |  |  |     int i; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     vmgenid_addr = acpi_find_vgia(qts); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |     g_assert(vmgenid_addr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* Read the GUID directly from guest memory */ | 
					
						
							|  |  |  |     for (i = 0; i < 16; i++) { | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |         guid->data[i] = qtest_readb(qts, vmgenid_addr + i); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |     } | 
					
						
							|  |  |  |     /* The GUID is in little-endian format in the guest, while QEMU
 | 
					
						
							|  |  |  |      * uses big-endian.  Swap after reading. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2018-12-10 11:26:49 +00:00
										 |  |  |     *guid = qemu_uuid_bswap(*guid); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  | static void read_guid_from_monitor(QTestState *qts, QemuUUID *guid) | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | { | 
					
						
							|  |  |  |     QDict *rsp, *rsp_ret; | 
					
						
							|  |  |  |     const char *guid_str; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     rsp = qtest_qmp(qts, "{ 'execute': 'query-vm-generation-id' }"); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |     if (qdict_haskey(rsp, "return")) { | 
					
						
							|  |  |  |         rsp_ret = qdict_get_qdict(rsp, "return"); | 
					
						
							|  |  |  |         g_assert(qdict_haskey(rsp_ret, "guid")); | 
					
						
							|  |  |  |         guid_str = qdict_get_str(rsp_ret, "guid"); | 
					
						
							|  |  |  |         g_assert(qemu_uuid_parse(guid_str, guid) == 0); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-04-19 17:01:43 +02:00
										 |  |  |     qobject_unref(rsp); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-14 18:30:45 +03:00
										 |  |  | static char disk[] = "tests/vmgenid-test-disk-XXXXXX"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 16:20:27 +02:00
										 |  |  | #define GUID_CMD(guid)                          \
 | 
					
						
							| 
									
										
										
										
											2019-11-13 10:10:47 +01:00
										 |  |  |     "-accel kvm -accel tcg "                    \ | 
					
						
							| 
									
										
										
										
											2017-10-18 16:20:27 +02:00
										 |  |  |     "-device vmgenid,id=testvgid,guid=%s "      \ | 
					
						
							|  |  |  |     "-drive id=hd0,if=none,file=%s,format=raw " \ | 
					
						
							|  |  |  |     "-device ide-hd,drive=hd0 ", guid, disk | 
					
						
							| 
									
										
										
										
											2017-07-14 18:30:45 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | static void vmgenid_set_guid_test(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QemuUUID expected, measured; | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     QTestState *qts; | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     qts = qtest_initf(GUID_CMD(VGID_GUID)); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Read the GUID from accessing guest memory */ | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     read_guid_from_memory(qts, &measured); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |     g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     qtest_quit(qts); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void vmgenid_set_guid_auto_test(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QemuUUID measured; | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     QTestState *qts; | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     qts = qtest_initf(GUID_CMD("auto")); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     read_guid_from_memory(qts, &measured); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Just check that the GUID is non-null */ | 
					
						
							|  |  |  |     g_assert(!qemu_uuid_is_null(&measured)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     qtest_quit(qts); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void vmgenid_query_monitor_test(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     QemuUUID expected, measured; | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     QTestState *qts; | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     g_assert(qemu_uuid_parse(VGID_GUID, &expected) == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     qts = qtest_initf(GUID_CMD(VGID_GUID)); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* Read the GUID via the monitor */ | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     read_guid_from_monitor(qts, &measured); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |     g_assert(memcmp(measured.data, expected.data, sizeof(measured.data)) == 0); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-12 19:46:20 +01:00
										 |  |  |     qtest_quit(qts); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int main(int argc, char **argv) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-26 15:00:08 -03:00
										 |  |  |     g_test_init(&argc, &argv, NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!qtest_has_accel("tcg") && !qtest_has_accel("kvm")) { | 
					
						
							|  |  |  |         g_test_skip("No KVM or TCG accelerator available"); | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-14 18:30:45 +03:00
										 |  |  |     ret = boot_sector_init(disk); | 
					
						
							|  |  |  |     if (ret) { | 
					
						
							|  |  |  |         return ret; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  |     qtest_add_func("/vmgenid/vmgenid/set-guid", | 
					
						
							|  |  |  |                    vmgenid_set_guid_test); | 
					
						
							|  |  |  |     qtest_add_func("/vmgenid/vmgenid/set-guid-auto", | 
					
						
							|  |  |  |                    vmgenid_set_guid_auto_test); | 
					
						
							|  |  |  |     qtest_add_func("/vmgenid/vmgenid/query-monitor", | 
					
						
							|  |  |  |                    vmgenid_query_monitor_test); | 
					
						
							|  |  |  |     ret = g_test_run(); | 
					
						
							| 
									
										
										
										
											2017-07-14 18:30:45 +03:00
										 |  |  |     boot_sector_cleanup(disk); | 
					
						
							| 
									
										
										
										
											2017-07-01 23:54:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return ret; | 
					
						
							|  |  |  | } |