| 1 | /* |
| 2 | * qtest vmcoreinfo test case |
| 3 | * |
| 4 | * Copyright Red Hat. 2025. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Ani Sinha <anisinha@redhat.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
| 14 | #include "qemu/units.h" |
| 15 | #include "libqos/libqos-pc.h" |
| 16 | #include "libqtest.h" |
| 17 | #include "standard-headers/linux/qemu_fw_cfg.h" |
| 18 | #include "libqos/fw_cfg.h" |
| 19 | #include "qemu/bswap.h" |
| 20 | #include "hw/misc/vmcoreinfo.h" |
| 21 | |
| 22 | static void test_vmcoreinfo_write_basic(void) |
| 23 | { |
| 24 | QFWCFG *fw_cfg; |
| 25 | QOSState *qs; |
| 26 | FWCfgVMCoreInfo info; |
| 27 | size_t filesize; |
| 28 | uint16_t guest_format; |
| 29 | uint16_t host_format; |
| 30 | uint32_t size; |
| 31 | uint64_t paddr; |
| 32 | |
| 33 | qs = qtest_pc_boot("-device vmcoreinfo"); |
| 34 | fw_cfg = pc_fw_cfg_init(qs->qts); |
| 35 | |
| 36 | memset(&info, 0 , sizeof(info)); |
| 37 | /* read vmcoreinfo and read back the host format */ |
| 38 | filesize = qfw_cfg_read_file(fw_cfg, qs, FW_CFG_VMCOREINFO_FILENAME, |
| 39 | &info, sizeof(info)); |
| 40 | g_assert_cmpint(filesize, ==, sizeof(info)); |
| 41 | |
| 42 | host_format = le16_to_cpu(info.host_format); |
| 43 | g_assert_cmpint(host_format, ==, FW_CFG_VMCOREINFO_FORMAT_ELF); |
| 44 | |
| 45 | memset(&info, 0 , sizeof(info)); |
| 46 | info.guest_format = cpu_to_le16(FW_CFG_VMCOREINFO_FORMAT_ELF); |
| 47 | info.size = cpu_to_le32(1 * MiB); |
| 48 | info.paddr = cpu_to_le64(0xffffff00); |
| 49 | info.host_format = cpu_to_le16(host_format); |
| 50 | |
| 51 | /* write the values to the host */ |
| 52 | filesize = qfw_cfg_write_file(fw_cfg, qs, FW_CFG_VMCOREINFO_FILENAME, |
| 53 | &info, sizeof(info)); |
| 54 | g_assert_cmpint(filesize, ==, sizeof(info)); |
| 55 | |
| 56 | memset(&info, 0 , sizeof(info)); |
| 57 | |
| 58 | /* now read back the values we wrote and compare that they are the same */ |
| 59 | filesize = qfw_cfg_read_file(fw_cfg, qs, FW_CFG_VMCOREINFO_FILENAME, |
| 60 | &info, sizeof(info)); |
| 61 | g_assert_cmpint(filesize, ==, sizeof(info)); |
| 62 | |
| 63 | size = le32_to_cpu(info.size); |
| 64 | paddr = le64_to_cpu(info.paddr); |
| 65 | guest_format = le16_to_cpu(info.guest_format); |
| 66 | |
| 67 | g_assert_cmpint(size, ==, 1 * MiB); |
| 68 | g_assert_cmpint(paddr, ==, 0xffffff00); |
| 69 | g_assert_cmpint(guest_format, ==, FW_CFG_VMCOREINFO_FORMAT_ELF); |
| 70 | |
| 71 | pc_fw_cfg_uninit(fw_cfg); |
| 72 | qtest_shutdown(qs); |
| 73 | } |
| 74 | |
| 75 | int main(int argc, char **argv) |
| 76 | { |
| 77 | const char *arch = qtest_get_arch(); |
| 78 | |
| 79 | g_test_init(&argc, &argv, NULL); |
| 80 | |
| 81 | if (strcmp(arch, "i386") && strcmp(arch, "x86_64")) { |
| 82 | /* skip for non-x86 */ |
| 83 | exit(EXIT_SUCCESS); |
| 84 | } |
| 85 | |
| 86 | qtest_add_func("vmcoreinfo/basic-write", |
| 87 | test_vmcoreinfo_write_basic); |
| 88 | |
| 89 | return g_test_run(); |
| 90 | } |