qemu/tests/qtest/bios-tables-test.c
<<
>>
Prefs
   1/*
   2 * Boot order test cases.
   3 *
   4 * Copyright (c) 2013 Red Hat Inc.
   5 *
   6 * Authors:
   7 *  Michael S. Tsirkin <mst@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/*
  14 * How to add or update the tests or commit changes that affect ACPI tables:
  15 * Contributor:
  16 * 1. add empty files for new tables, if any, under tests/data/acpi
  17 * 2. list any changed files in tests/qtest/bios-tables-test-allowed-diff.h
  18 * 3. commit the above *before* making changes that affect the tables
  19 *
  20 * Contributor or ACPI Maintainer (steps 4-7 need to be redone to resolve conflicts
  21 * in binary commit created in step 6):
  22 *
  23 * After 1-3 above tests will pass but ignore differences with the expected files.
  24 * You will also notice that tests/qtest/bios-tables-test-allowed-diff.h lists
  25 * a bunch of files. This is your hint that you need to do the below:
  26 * 4. Run
  27 *      make check V=2
  28 * this will produce a bunch of warnings about differences
  29 * between actual and expected ACPI tables. If you have IASL installed,
  30 * they will also be disassembled so you can look at the disassembled
  31 * output. If not - disassemble them yourself in any way you like.
  32 * Look at the differences - make sure they make sense and match what the
  33 * changes you are merging are supposed to do.
  34 * Save the changes, preferably in form of ASL diff for the commit log in
  35 * step 6.
  36 *
  37 * 5. From build directory, run:
  38 *      $(SRC_PATH)/tests/data/acpi/rebuild-expected-aml.sh
  39 * 6. Now commit any changes to the expected binary, include diff from step 4
  40 *    in commit log.
  41 *    Expected binary updates needs to be a separate patch from the code that
  42 *    introduces changes to ACPI tables. It lets the maintainer drop
  43 *    and regenerate binary updates in case of merge conflicts. Further, a code
  44 *    change is easily reviewable but a binary blob is not (without doing a
  45 *    disassembly).
  46 * 7. Before sending patches to the list (Contributor)
  47 *    or before doing a pull request (Maintainer), make sure
  48 *    tests/qtest/bios-tables-test-allowed-diff.h is empty - this will ensure
  49 *    following changes to ACPI tables will be noticed.
  50 *
  51 * The resulting patchset/pull request then looks like this:
  52 * - patch 1: list changed files in tests/qtest/bios-tables-test-allowed-diff.h.
  53 * - patches 2 - n: real changes, may contain multiple patches.
  54 * - patch n + 1: update golden master binaries and empty
  55 *   tests/qtest/bios-tables-test-allowed-diff.h
  56 */
  57
  58#include "qemu/osdep.h"
  59#include <glib/gstdio.h>
  60#include "hw/firmware/smbios.h"
  61#include "qemu/bitmap.h"
  62#include "acpi-utils.h"
  63#include "boot-sector.h"
  64#include "tpm-emu.h"
  65#include "hw/acpi/tpm.h"
  66#include "qemu/cutils.h"
  67
  68#define MACHINE_PC "pc"
  69#define MACHINE_Q35 "q35"
  70
  71#define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"
  72
  73#define OEM_ID             "TEST"
  74#define OEM_TABLE_ID       "OEM"
  75#define OEM_TEST_ARGS      "-machine x-oem-id=" OEM_ID ",x-oem-table-id=" \
  76                           OEM_TABLE_ID
  77
  78typedef struct {
  79    bool tcg_only;
  80    const char *machine;
  81    const char *arch;
  82    const char *machine_param;
  83    const char *variant;
  84    const char *uefi_fl1;
  85    const char *uefi_fl2;
  86    const char *blkdev;
  87    const char *cd;
  88    const uint64_t ram_start;
  89    const uint64_t scan_len;
  90    uint64_t rsdp_addr;
  91    uint8_t rsdp_table[36 /* ACPI 2.0+ RSDP size */];
  92    GArray *tables;
  93    uint64_t smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE__MAX];
  94    SmbiosEntryPoint smbios_ep_table;
  95    uint16_t smbios_cpu_max_speed;
  96    uint16_t smbios_cpu_curr_speed;
  97    uint8_t smbios_core_count;
  98    uint16_t smbios_core_count2;
  99    uint8_t smbios_thread_count;
 100    uint16_t smbios_thread_count2;
 101    uint8_t *required_struct_types;
 102    int required_struct_types_len;
 103    int type4_count;
 104    QTestState *qts;
 105} test_data;
 106
 107static char disk[] = "tests/acpi-test-disk-XXXXXX";
 108static const char *data_dir = "tests/data/acpi";
 109#ifdef CONFIG_IASL
 110static const char *iasl = CONFIG_IASL;
 111#else
 112static const char *iasl;
 113#endif
 114
 115static int verbosity_level;
 116static GArray *load_expected_aml(test_data *data);
 117
 118static bool compare_signature(const AcpiSdtTable *sdt, const char *signature)
 119{
 120   return !memcmp(sdt->aml, signature, 4);
 121}
 122
 123static void cleanup_table_descriptor(AcpiSdtTable *table)
 124{
 125    g_free(table->aml);
 126    if (table->aml_file &&
 127        !table->tmp_files_retain &&
 128        g_strstr_len(table->aml_file, -1, "aml-")) {
 129        unlink(table->aml_file);
 130    }
 131    g_free(table->aml_file);
 132    g_free(table->asl);
 133    if (table->asl_file &&
 134        !table->tmp_files_retain) {
 135        unlink(table->asl_file);
 136    }
 137    g_free(table->asl_file);
 138}
 139
 140static void free_test_data(test_data *data)
 141{
 142    int i;
 143
 144    if (!data->tables) {
 145        return;
 146    }
 147    for (i = 0; i < data->tables->len; ++i) {
 148        cleanup_table_descriptor(&g_array_index(data->tables, AcpiSdtTable, i));
 149    }
 150
 151    g_array_free(data->tables, true);
 152}
 153
 154static void test_acpi_rsdp_table(test_data *data)
 155{
 156    uint8_t *rsdp_table = data->rsdp_table;
 157
 158    acpi_fetch_rsdp_table(data->qts, data->rsdp_addr, rsdp_table);
 159
 160    switch (rsdp_table[15 /* Revision offset */]) {
 161    case 0: /* ACPI 1.0 RSDP */
 162        /* With rev 1, checksum is only for the first 20 bytes */
 163        g_assert(!acpi_calc_checksum(rsdp_table,  20));
 164        break;
 165    case 2: /* ACPI 2.0+ RSDP */
 166        /* With revision 2, we have 2 checksums */
 167        g_assert(!acpi_calc_checksum(rsdp_table, 20));
 168        g_assert(!acpi_calc_checksum(rsdp_table, 36));
 169        break;
 170    default:
 171        g_assert_not_reached();
 172    }
 173}
 174
 175static void test_acpi_rxsdt_table(test_data *data)
 176{
 177    const char *sig = "RSDT";
 178    AcpiSdtTable rsdt = {};
 179    int entry_size = 4;
 180    int addr_off = 16 /* RsdtAddress */;
 181    uint8_t *ent;
 182
 183    if (data->rsdp_table[15 /* Revision offset */] != 0) {
 184        addr_off = 24 /* XsdtAddress */;
 185        entry_size = 8;
 186        sig = "XSDT";
 187    }
 188    /* read [RX]SDT table */
 189    acpi_fetch_table(data->qts, &rsdt.aml, &rsdt.aml_len,
 190                     &data->rsdp_table[addr_off], entry_size, sig, true);
 191
 192    /* Load all tables and add to test list directly RSDT referenced tables */
 193    ACPI_FOREACH_RSDT_ENTRY(rsdt.aml, rsdt.aml_len, ent, entry_size) {
 194        AcpiSdtTable ssdt_table = {};
 195
 196        acpi_fetch_table(data->qts, &ssdt_table.aml, &ssdt_table.aml_len, ent,
 197                         entry_size, NULL, true);
 198        /* Add table to ASL test tables list */
 199        g_array_append_val(data->tables, ssdt_table);
 200    }
 201    cleanup_table_descriptor(&rsdt);
 202}
 203
 204static void test_acpi_fadt_table(test_data *data)
 205{
 206    /* FADT table is 1st */
 207    AcpiSdtTable table = g_array_index(data->tables, typeof(table), 0);
 208    uint8_t *fadt_aml = table.aml;
 209    uint32_t fadt_len = table.aml_len;
 210    uint32_t val;
 211    int dsdt_offset = 40 /* DSDT */;
 212    int dsdt_entry_size = 4;
 213
 214    g_assert(compare_signature(&table, "FACP"));
 215
 216    /* Since DSDT/FACS isn't in RSDT, add them to ASL test list manually */
 217    memcpy(&val, fadt_aml + 112 /* Flags */, 4);
 218    val = le32_to_cpu(val);
 219    if (!(val & 1UL << 20 /* HW_REDUCED_ACPI */)) {
 220        acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
 221                         fadt_aml + 36 /* FIRMWARE_CTRL */, 4, "FACS", false);
 222        g_array_append_val(data->tables, table);
 223    }
 224
 225    memcpy(&val, fadt_aml + dsdt_offset, 4);
 226    val = le32_to_cpu(val);
 227    if (!val) {
 228        dsdt_offset = 140 /* X_DSDT */;
 229        dsdt_entry_size = 8;
 230    }
 231    acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
 232                     fadt_aml + dsdt_offset, dsdt_entry_size, "DSDT", true);
 233    g_array_append_val(data->tables, table);
 234
 235    memset(fadt_aml + 36, 0, 4); /* sanitize FIRMWARE_CTRL ptr */
 236    memset(fadt_aml + 40, 0, 4); /* sanitize DSDT ptr */
 237    if (fadt_aml[8 /* FADT Major Version */] >= 3) {
 238        memset(fadt_aml + 132, 0, 8); /* sanitize X_FIRMWARE_CTRL ptr */
 239        memset(fadt_aml + 140, 0, 8); /* sanitize X_DSDT ptr */
 240    }
 241
 242    /* update checksum */
 243    fadt_aml[9 /* Checksum */] = 0;
 244    fadt_aml[9 /* Checksum */] -= acpi_calc_checksum(fadt_aml, fadt_len);
 245}
 246
 247static void dump_aml_files(test_data *data, bool rebuild)
 248{
 249    AcpiSdtTable *sdt, *exp_sdt;
 250    GError *error = NULL;
 251    gchar *aml_file = NULL;
 252    test_data exp_data = {};
 253    gint fd;
 254    ssize_t ret;
 255    int i;
 256
 257    exp_data.tables = load_expected_aml(data);
 258    for (i = 0; i < data->tables->len; ++i) {
 259        const char *ext = data->variant ? data->variant : "";
 260        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
 261        exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
 262        g_assert(sdt->aml);
 263        g_assert(exp_sdt->aml);
 264
 265        if (rebuild) {
 266            aml_file = g_strdup_printf("%s/%s/%s/%.4s%s", data_dir,
 267                                       data->arch, data->machine,
 268                                       sdt->aml, ext);
 269
 270            if (!g_file_test(aml_file, G_FILE_TEST_EXISTS) &&
 271                sdt->aml_len == exp_sdt->aml_len &&
 272                !memcmp(sdt->aml, exp_sdt->aml, sdt->aml_len)) {
 273                /* identical tables, no need to write new files */
 274                g_free(aml_file);
 275                continue;
 276            }
 277            fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
 278                        S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
 279            if (fd < 0) {
 280                perror(aml_file);
 281            }
 282            g_assert(fd >= 0);
 283        } else {
 284            fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
 285            g_assert_no_error(error);
 286        }
 287
 288        ret = qemu_write_full(fd, sdt->aml, sdt->aml_len);
 289        g_assert(ret == sdt->aml_len);
 290
 291        close(fd);
 292
 293        g_free(aml_file);
 294    }
 295    free_test_data(&exp_data);
 296}
 297
 298static bool create_tmp_asl(AcpiSdtTable *sdt)
 299{
 300    GError *error = NULL;
 301    gint fd;
 302
 303    fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
 304    g_assert_no_error(error);
 305    close(fd);
 306
 307    return false;
 308}
 309
 310static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
 311{
 312    AcpiSdtTable *temp;
 313    GError *error = NULL;
 314    GString *command_line = g_string_new(iasl);
 315    gchar *out, *out_err;
 316    gboolean ret;
 317    int i;
 318
 319    create_tmp_asl(sdt);
 320
 321    /* build command line */
 322    g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
 323    if (compare_signature(sdt, "DSDT") ||
 324        compare_signature(sdt, "SSDT")) {
 325        for (i = 0; i < sdts->len; ++i) {
 326            temp = &g_array_index(sdts, AcpiSdtTable, i);
 327            if (compare_signature(temp, "DSDT") ||
 328                compare_signature(temp, "SSDT")) {
 329                g_string_append_printf(command_line, "-e %s ", temp->aml_file);
 330            }
 331        }
 332    }
 333    g_string_append_printf(command_line, "-d %s", sdt->aml_file);
 334
 335    /* pass 'out' and 'out_err' in order to be redirected */
 336    ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
 337    g_assert_no_error(error);
 338    if (ret) {
 339        ret = g_file_get_contents(sdt->asl_file, &sdt->asl,
 340                                  &sdt->asl_len, &error);
 341        g_assert(ret);
 342        g_assert_no_error(error);
 343        ret = (sdt->asl_len > 0);
 344    }
 345
 346    g_free(out);
 347    g_free(out_err);
 348    g_string_free(command_line, true);
 349
 350    return !ret;
 351}
 352
 353#define COMMENT_END "*/"
 354#define DEF_BLOCK "DefinitionBlock ("
 355#define BLOCK_NAME_END ","
 356
 357static GString *normalize_asl(gchar *asl_code)
 358{
 359    GString *asl = g_string_new(asl_code);
 360    gchar *comment, *block_name;
 361
 362    /* strip comments (different generation days) */
 363    comment = g_strstr_len(asl->str, asl->len, COMMENT_END);
 364    if (comment) {
 365        comment += strlen(COMMENT_END);
 366        while (*comment == '\n') {
 367            comment++;
 368        }
 369        asl = g_string_erase(asl, 0, comment - asl->str);
 370    }
 371
 372    /* strip def block name (it has file path in it) */
 373    if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
 374        block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
 375        g_assert(block_name);
 376        asl = g_string_erase(asl, 0,
 377                             block_name + sizeof(BLOCK_NAME_END) - asl->str);
 378    }
 379
 380    return asl;
 381}
 382
 383static GArray *load_expected_aml(test_data *data)
 384{
 385    int i;
 386    AcpiSdtTable *sdt;
 387    GError *error = NULL;
 388    gboolean ret;
 389    gsize aml_len;
 390
 391    GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
 392    if (verbosity_level >= 2) {
 393        fputc('\n', stderr);
 394    }
 395    for (i = 0; i < data->tables->len; ++i) {
 396        AcpiSdtTable exp_sdt;
 397        gchar *aml_file = NULL;
 398        const char *ext = data->variant ? data->variant : "";
 399
 400        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
 401
 402        memset(&exp_sdt, 0, sizeof(exp_sdt));
 403
 404try_again:
 405        aml_file = g_strdup_printf("%s/%s/%s/%.4s%s", data_dir, data->arch,
 406                                   data->machine, sdt->aml, ext);
 407        if (verbosity_level >= 2) {
 408            fprintf(stderr, "Looking for expected file '%s'\n", aml_file);
 409        }
 410        if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) {
 411            exp_sdt.aml_file = aml_file;
 412        } else if (*ext != '\0') {
 413            /* try fallback to generic (extension less) expected file */
 414            ext = "";
 415            g_free(aml_file);
 416            goto try_again;
 417        }
 418        g_assert(exp_sdt.aml_file);
 419        if (verbosity_level >= 2) {
 420            fprintf(stderr, "Using expected file '%s'\n", aml_file);
 421        }
 422        ret = g_file_get_contents(aml_file, (gchar **)&exp_sdt.aml,
 423                                  &aml_len, &error);
 424        exp_sdt.aml_len = aml_len;
 425        g_assert(ret);
 426        g_assert_no_error(error);
 427        g_assert(exp_sdt.aml);
 428        if (!exp_sdt.aml_len) {
 429            fprintf(stderr, "Warning! zero length expected file '%s'\n",
 430                    aml_file);
 431        }
 432
 433        g_array_append_val(exp_tables, exp_sdt);
 434    }
 435
 436    return exp_tables;
 437}
 438
 439static bool test_acpi_find_diff_allowed(AcpiSdtTable *sdt)
 440{
 441    const gchar *allowed_diff_file[] = {
 442#include "bios-tables-test-allowed-diff.h"
 443        NULL
 444    };
 445    const gchar **f;
 446
 447    for (f = allowed_diff_file; *f; ++f) {
 448        if (!g_strcmp0(sdt->aml_file, *f)) {
 449            return true;
 450        }
 451    }
 452    return false;
 453}
 454
 455/* test the list of tables in @data->tables against reference tables */
 456static void test_acpi_asl(test_data *data)
 457{
 458    int i;
 459    AcpiSdtTable *sdt, *exp_sdt;
 460    test_data exp_data = {};
 461    gboolean exp_err, err, all_tables_match = true;
 462
 463    exp_data.tables = load_expected_aml(data);
 464    dump_aml_files(data, false);
 465    for (i = 0; i < data->tables->len; ++i) {
 466        GString *asl, *exp_asl;
 467
 468        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
 469        exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
 470
 471        if (sdt->aml_len == exp_sdt->aml_len &&
 472            !memcmp(sdt->aml, exp_sdt->aml, sdt->aml_len)) {
 473            /* Identical table binaries: no need to disassemble. */
 474            continue;
 475        }
 476
 477        fprintf(stderr,
 478                "acpi-test: Warning! %.4s binary file mismatch. "
 479                "Actual [aml:%s], Expected [aml:%s].\n"
 480                "See source file tests/qtest/bios-tables-test.c "
 481                "for instructions on how to update expected files.\n",
 482                exp_sdt->aml, sdt->aml_file, exp_sdt->aml_file);
 483
 484        all_tables_match = all_tables_match &&
 485            test_acpi_find_diff_allowed(exp_sdt);
 486
 487        /*
 488         *  don't try to decompile if IASL isn't present, in this case user
 489         * will just 'get binary file mismatch' warnings and test failure
 490         */
 491        if (!iasl) {
 492            continue;
 493        }
 494
 495        err = load_asl(data->tables, sdt);
 496        asl = normalize_asl(sdt->asl);
 497
 498        /*
 499         * If expected file is empty - it's likely that it was a stub just
 500         * created for step 1 above: we do want to decompile the actual one.
 501         */
 502        if (exp_sdt->aml_len) {
 503            exp_err = load_asl(exp_data.tables, exp_sdt);
 504            exp_asl = normalize_asl(exp_sdt->asl);
 505        } else {
 506            exp_err = create_tmp_asl(exp_sdt);
 507            exp_asl = g_string_new("");
 508        }
 509
 510        /* TODO: check for warnings */
 511        g_assert(!err || exp_err || !exp_sdt->aml_len);
 512
 513        if (g_strcmp0(asl->str, exp_asl->str)) {
 514            sdt->tmp_files_retain = true;
 515            if (exp_err) {
 516                fprintf(stderr,
 517                        "Warning! iasl couldn't parse the expected aml\n");
 518            } else {
 519                exp_sdt->tmp_files_retain = true;
 520                fprintf(stderr,
 521                        "acpi-test: Warning! %.4s mismatch. "
 522                        "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
 523                        exp_sdt->aml, sdt->asl_file, sdt->aml_file,
 524                        exp_sdt->asl_file, exp_sdt->aml_file);
 525                fflush(stderr);
 526                if (verbosity_level >= 1) {
 527                    const char *diff_env = getenv("DIFF");
 528                    const char *diff_cmd = diff_env ? diff_env : "diff -U 16";
 529                    char *diff = g_strdup_printf("%s %s %s", diff_cmd,
 530                                                 exp_sdt->asl_file, sdt->asl_file);
 531                    int out = dup(STDOUT_FILENO);
 532                    int ret G_GNUC_UNUSED;
 533                    int dupret;
 534
 535                    g_assert(out >= 0);
 536                    dupret = dup2(STDERR_FILENO, STDOUT_FILENO);
 537                    g_assert(dupret >= 0);
 538                    ret = system(diff) ;
 539                    dupret = dup2(out, STDOUT_FILENO);
 540                    g_assert(dupret >= 0);
 541                    close(out);
 542                    g_free(diff);
 543                }
 544            }
 545        }
 546        g_string_free(asl, true);
 547        g_string_free(exp_asl, true);
 548    }
 549    if (!iasl && !all_tables_match) {
 550        fprintf(stderr, "to see ASL diff between mismatched files install IASL,"
 551                " rebuild QEMU from scratch and re-run tests with V=1"
 552                " environment variable set");
 553    }
 554    g_assert(all_tables_match);
 555
 556    free_test_data(&exp_data);
 557}
 558
 559static bool smbios_ep2_table_ok(test_data *data, uint32_t addr)
 560{
 561    struct smbios_21_entry_point *ep_table = &data->smbios_ep_table.ep21;
 562
 563    qtest_memread(data->qts, addr, ep_table, sizeof(*ep_table));
 564    if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
 565        return false;
 566    }
 567    if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
 568        return false;
 569    }
 570    if (ep_table->structure_table_length == 0) {
 571        return false;
 572    }
 573    if (ep_table->number_of_structures == 0) {
 574        return false;
 575    }
 576    if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
 577        acpi_calc_checksum((uint8_t *)ep_table + 0x10,
 578                           sizeof *ep_table - 0x10)) {
 579        return false;
 580    }
 581    return true;
 582}
 583
 584static bool smbios_ep3_table_ok(test_data *data, uint64_t addr)
 585{
 586    struct smbios_30_entry_point *ep_table = &data->smbios_ep_table.ep30;
 587
 588    qtest_memread(data->qts, addr, ep_table, sizeof(*ep_table));
 589    if (memcmp(ep_table->anchor_string, "_SM3_", 5)) {
 590        return false;
 591    }
 592
 593    if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table)) {
 594        return false;
 595    }
 596
 597    return true;
 598}
 599
 600static SmbiosEntryPointType test_smbios_entry_point(test_data *data)
 601{
 602    uint32_t off;
 603
 604    /* find smbios entry point structure */
 605    for (off = 0xf0000; off < 0x100000; off += 0x10) {
 606        uint8_t sig[] = "_SM_", sig3[] = "_SM3_";
 607        int i;
 608
 609        for (i = 0; i < sizeof sig - 1; ++i) {
 610            sig[i] = qtest_readb(data->qts, off + i);
 611        }
 612
 613        if (!memcmp(sig, "_SM_", sizeof sig)) {
 614            /* signature match, but is this a valid entry point? */
 615            if (smbios_ep2_table_ok(data, off)) {
 616                data->smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE_32] = off;
 617            }
 618        }
 619
 620        for (i = 0; i < sizeof sig3 - 1; ++i) {
 621            sig3[i] = qtest_readb(data->qts, off + i);
 622        }
 623
 624        if (!memcmp(sig3, "_SM3_", sizeof sig3)) {
 625            if (smbios_ep3_table_ok(data, off)) {
 626                data->smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE_64] = off;
 627                /* found 64-bit entry point, no need to look for 32-bit one */
 628                break;
 629            }
 630        }
 631    }
 632
 633    /* found at least one entry point */
 634    g_assert_true(data->smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE_32] ||
 635                  data->smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE_64]);
 636
 637    return data->smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE_64] ?
 638           SMBIOS_ENTRY_POINT_TYPE_64 : SMBIOS_ENTRY_POINT_TYPE_32;
 639}
 640
 641static inline bool smbios_single_instance(uint8_t type)
 642{
 643    switch (type) {
 644    case 0:
 645    case 1:
 646    case 2:
 647    case 3:
 648    case 16:
 649    case 32:
 650    case 127:
 651        return true;
 652    default:
 653        return false;
 654    }
 655}
 656
 657static void smbios_cpu_test(test_data *data, uint32_t addr,
 658                            SmbiosEntryPointType ep_type)
 659{
 660    uint8_t core_count, expected_core_count = data->smbios_core_count;
 661    uint8_t thread_count, expected_thread_count = data->smbios_thread_count;
 662    uint16_t speed, expected_speed[2];
 663    uint16_t core_count2, expected_core_count2 = data->smbios_core_count2;
 664    uint16_t thread_count2, expected_thread_count2 = data->smbios_thread_count2;
 665    int offset[2];
 666    int i;
 667
 668    /* Check CPU speed for backward compatibility */
 669    offset[0] = offsetof(struct smbios_type_4, max_speed);
 670    offset[1] = offsetof(struct smbios_type_4, current_speed);
 671    expected_speed[0] = data->smbios_cpu_max_speed ? : 2000;
 672    expected_speed[1] = data->smbios_cpu_curr_speed ? : 2000;
 673
 674    for (i = 0; i < 2; i++) {
 675        speed = qtest_readw(data->qts, addr + offset[i]);
 676        g_assert_cmpuint(speed, ==, expected_speed[i]);
 677    }
 678
 679    core_count = qtest_readb(data->qts,
 680                    addr + offsetof(struct smbios_type_4, core_count));
 681
 682    if (expected_core_count) {
 683        g_assert_cmpuint(core_count, ==, expected_core_count);
 684    }
 685
 686    thread_count = qtest_readb(data->qts,
 687                       addr + offsetof(struct smbios_type_4, thread_count));
 688
 689    if (expected_thread_count) {
 690        g_assert_cmpuint(thread_count, ==, expected_thread_count);
 691    }
 692
 693    if (ep_type == SMBIOS_ENTRY_POINT_TYPE_64) {
 694        core_count2 = qtest_readw(data->qts,
 695                          addr + offsetof(struct smbios_type_4, core_count2));
 696
 697        /* Core Count has reached its limit, checking Core Count 2 */
 698        if (expected_core_count == 0xFF && expected_core_count2) {
 699            g_assert_cmpuint(core_count2, ==, expected_core_count2);
 700        }
 701
 702        thread_count2 = qtest_readw(data->qts,
 703                            addr + offsetof(struct smbios_type_4,
 704                            thread_count2));
 705
 706        /* Thread Count has reached its limit, checking Thread Count 2 */
 707        if (expected_thread_count == 0xFF && expected_thread_count2) {
 708            g_assert_cmpuint(thread_count2, ==, expected_thread_count2);
 709        }
 710    }
 711}
 712
 713static void smbios_type4_count_test(test_data *data, int type4_count)
 714{
 715    int expected_type4_count = data->type4_count;
 716
 717    if (expected_type4_count) {
 718        g_assert_cmpuint(type4_count, ==, expected_type4_count);
 719    }
 720}
 721
 722static void test_smbios_structs(test_data *data, SmbiosEntryPointType ep_type)
 723{
 724    DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };
 725
 726    SmbiosEntryPoint *ep_table = &data->smbios_ep_table;
 727    int i = 0, len, max_len = 0, type4_count = 0;
 728    uint8_t type, prv, crt;
 729    uint64_t addr;
 730
 731    if (ep_type == SMBIOS_ENTRY_POINT_TYPE_32) {
 732        addr = le32_to_cpu(ep_table->ep21.structure_table_address);
 733    } else {
 734        addr = le64_to_cpu(ep_table->ep30.structure_table_address);
 735    }
 736
 737    /* walk the smbios tables */
 738    do {
 739
 740        /* grab type and formatted area length from struct header */
 741        type = qtest_readb(data->qts, addr);
 742        g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
 743        len = qtest_readb(data->qts, addr + 1);
 744
 745        /* single-instance structs must not have been encountered before */
 746        if (smbios_single_instance(type)) {
 747            g_assert(!test_bit(type, struct_bitmap));
 748        }
 749        set_bit(type, struct_bitmap);
 750
 751        if (type == 4) {
 752            smbios_cpu_test(data, addr, ep_type);
 753            type4_count++;
 754        }
 755
 756        /* seek to end of unformatted string area of this struct ("\0\0") */
 757        prv = crt = 1;
 758        while (prv || crt) {
 759            prv = crt;
 760            crt = qtest_readb(data->qts, addr + len);
 761            len++;
 762        }
 763
 764        /* keep track of max. struct size */
 765        if (ep_type == SMBIOS_ENTRY_POINT_TYPE_32 && max_len < len) {
 766            max_len = len;
 767            g_assert_cmpuint(max_len, <=, ep_table->ep21.max_structure_size);
 768        }
 769
 770        /* start of next structure */
 771        addr += len;
 772
 773    /*
 774     * Until all structures have been scanned (ep21)
 775     * or an EOF structure is found (ep30)
 776     */
 777    } while (ep_type == SMBIOS_ENTRY_POINT_TYPE_32 ?
 778                ++i < le16_to_cpu(ep_table->ep21.number_of_structures) :
 779                type != 127);
 780
 781    if (ep_type == SMBIOS_ENTRY_POINT_TYPE_32) {
 782        /*
 783         * Total table length and max struct size
 784         * must match entry point values
 785         */
 786        g_assert_cmpuint(le16_to_cpu(ep_table->ep21.structure_table_length), ==,
 787            addr - le32_to_cpu(ep_table->ep21.structure_table_address));
 788
 789        g_assert_cmpuint(le16_to_cpu(ep_table->ep21.max_structure_size), ==,
 790            max_len);
 791    }
 792
 793    /* required struct types must all be present */
 794    for (i = 0; i < data->required_struct_types_len; i++) {
 795        g_assert(test_bit(data->required_struct_types[i], struct_bitmap));
 796    }
 797
 798    smbios_type4_count_test(data, type4_count);
 799}
 800
 801static void test_acpi_load_tables(test_data *data)
 802{
 803    if (data->uefi_fl1 && data->uefi_fl2) { /* use UEFI */
 804        g_assert(data->scan_len);
 805        data->rsdp_addr = acpi_find_rsdp_address_uefi(data->qts,
 806            data->ram_start, data->scan_len);
 807    } else {
 808        boot_sector_test(data->qts);
 809        data->rsdp_addr = acpi_find_rsdp_address(data->qts);
 810        g_assert_cmphex(data->rsdp_addr, <, 0x100000);
 811    }
 812
 813    data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
 814    test_acpi_rsdp_table(data);
 815    test_acpi_rxsdt_table(data);
 816    test_acpi_fadt_table(data);
 817}
 818
 819static char *test_acpi_create_args(test_data *data, const char *params)
 820{
 821    char *args;
 822
 823    if (data->uefi_fl1 && data->uefi_fl2) { /* use UEFI */
 824        /*
 825         * TODO: convert '-drive if=pflash' to new syntax (see e33763be7cd3)
 826         * when arm/virt boad starts to support it.
 827         */
 828        if (data->cd) {
 829            args = g_strdup_printf("-machine %s%s %s -accel tcg "
 830                "-nodefaults -nographic "
 831                "-drive if=pflash,format=raw,file=%s,readonly=on "
 832                "-drive if=pflash,format=raw,file=%s,snapshot=on -cdrom %s %s",
 833                data->machine, data->machine_param ?: "",
 834                data->tcg_only ? "" : "-accel kvm",
 835                data->uefi_fl1, data->uefi_fl2, data->cd, params ? params : "");
 836        } else {
 837            args = g_strdup_printf("-machine %s%s %s -accel tcg "
 838                "-nodefaults -nographic "
 839                "-drive if=pflash,format=raw,file=%s,readonly=on "
 840                "-drive if=pflash,format=raw,file=%s,snapshot=on %s",
 841                data->machine, data->machine_param ?: "",
 842                data->tcg_only ? "" : "-accel kvm",
 843                data->uefi_fl1, data->uefi_fl2, params ? params : "");
 844        }
 845    } else {
 846        args = g_strdup_printf("-machine %s%s %s -accel tcg "
 847            "-net none %s "
 848            "-drive id=hd0,if=none,file=%s,format=raw "
 849            "-device %s,drive=hd0 ",
 850             data->machine, data->machine_param ?: "",
 851             data->tcg_only ? "" : "-accel kvm",
 852             params ? params : "", disk,
 853             data->blkdev ?: "ide-hd");
 854    }
 855    return args;
 856}
 857
 858static void test_vm_prepare(const char *params, test_data *data)
 859{
 860    char *args = test_acpi_create_args(data, params);
 861    data->qts = qtest_init(args);
 862    g_free(args);
 863}
 864
 865static void process_smbios_tables_noexit(test_data *data)
 866{
 867    /*
 868     * TODO: make SMBIOS tests work with UEFI firmware,
 869     * Bug on uefi-test-tools to provide entry point:
 870     * https://bugs.launchpad.net/qemu/+bug/1821884
 871     */
 872    if (!(data->uefi_fl1 && data->uefi_fl2)) {
 873        SmbiosEntryPointType ep_type = test_smbios_entry_point(data);
 874        test_smbios_structs(data, ep_type);
 875    }
 876}
 877
 878static void test_smbios(const char *params, test_data *data)
 879{
 880    test_vm_prepare(params, data);
 881    boot_sector_test(data->qts);
 882    process_smbios_tables_noexit(data);
 883    qtest_quit(data->qts);
 884}
 885
 886static void process_acpi_tables_noexit(test_data *data)
 887{
 888    test_acpi_load_tables(data);
 889
 890    if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
 891        dump_aml_files(data, true);
 892    } else {
 893        test_acpi_asl(data);
 894    }
 895
 896    process_smbios_tables_noexit(data);
 897}
 898
 899static void process_acpi_tables(test_data *data)
 900{
 901    process_acpi_tables_noexit(data);
 902    qtest_quit(data->qts);
 903}
 904
 905static void test_acpi_one(const char *params, test_data *data)
 906{
 907    test_vm_prepare(params, data);
 908    process_acpi_tables(data);
 909}
 910
 911static uint8_t base_required_struct_types[] = {
 912    0, 1, 3, 4, 16, 17, 19, 32, 127
 913};
 914
 915static void test_acpi_piix4_tcg(void)
 916{
 917    test_data data = {};
 918
 919    /* Supplying -machine accel argument overrides the default (qtest).
 920     * This is to make guest actually run.
 921     */
 922    data.machine = MACHINE_PC;
 923    data.arch    = "x86";
 924    data.required_struct_types = base_required_struct_types;
 925    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
 926    test_acpi_one(NULL, &data);
 927    free_test_data(&data);
 928}
 929
 930static void test_acpi_piix4_tcg_bridge(void)
 931{
 932    test_data data = {};
 933
 934    data.machine = MACHINE_PC;
 935    data.arch    = "x86";
 936    data.variant = ".bridge";
 937    data.required_struct_types = base_required_struct_types;
 938    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
 939    test_vm_prepare("-S"
 940        " -device pci-bridge,chassis_nr=1"
 941        " -device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2"
 942        " -device pci-testdev,bus=pci.0,addr=5.0"
 943        " -device pci-testdev,bus=pci.1", &data);
 944
 945    /* hotplugged bridges section */
 946    qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr",
 947        "{'bus': 'pci.1', 'addr': '2.0', 'chassis_nr': 3 }");
 948    qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr_multifunc",
 949        "{'bus': 'pci.1', 'addr': '0xf.1', 'chassis_nr': 4 }");
 950    qtest_qmp_device_add(data.qts, "pci-bridge", "hpbrhost",
 951        "{'bus': 'pci.0', 'addr': '4.0', 'chassis_nr': 5 }");
 952    qtest_qmp_device_add(data.qts, "pci-testdev", "d1", "{'bus': 'pci.0' }");
 953    qtest_qmp_device_add(data.qts, "pci-testdev", "d2", "{'bus': 'pci.1' }");
 954    qtest_qmp_device_add(data.qts, "pci-testdev", "d3", "{'bus': 'hpbr', "
 955                                   "'addr': '1.0' }");
 956    qtest_qmp_send(data.qts, "{'execute':'cont' }");
 957    qtest_qmp_eventwait(data.qts, "RESUME");
 958
 959    process_acpi_tables_noexit(&data);
 960    free_test_data(&data);
 961
 962    /* check that reboot/reset doesn't change any ACPI tables  */
 963    qtest_system_reset(data.qts);
 964    process_acpi_tables(&data);
 965    free_test_data(&data);
 966}
 967
 968static void test_acpi_piix4_no_root_hotplug(void)
 969{
 970    test_data data = {};
 971
 972    data.machine = MACHINE_PC;
 973    data.arch    = "x86";
 974    data.variant = ".roothp";
 975    data.required_struct_types = base_required_struct_types;
 976    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
 977    test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
 978                  "-device pci-bridge,chassis_nr=1 "
 979                  "-device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2 "
 980                  "-device pci-testdev,bus=pci.0 "
 981                  "-device pci-testdev,bus=pci.1", &data);
 982    free_test_data(&data);
 983}
 984
 985static void test_acpi_piix4_no_bridge_hotplug(void)
 986{
 987    test_data data = {};
 988
 989    data.machine = MACHINE_PC;
 990    data.arch    = "x86";
 991    data.variant = ".hpbridge";
 992    data.required_struct_types = base_required_struct_types;
 993    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
 994    test_acpi_one("-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
 995                  "-device pci-bridge,chassis_nr=1 "
 996                  "-device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2 "
 997                  "-device pci-testdev,bus=pci.0 "
 998                  "-device pci-testdev,bus=pci.1,addr=2.0", &data);
 999    free_test_data(&data);
1000}
1001
1002static void test_acpi_piix4_no_acpi_pci_hotplug(void)
1003{
1004    test_data data = {};
1005
1006    data.machine = MACHINE_PC;
1007    data.arch    = "x86";
1008    data.variant = ".hpbrroot";
1009    data.required_struct_types = base_required_struct_types;
1010    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1011    test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
1012                  "-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
1013                  "-device pci-bridge,chassis_nr=1,addr=4.0 "
1014                  "-device pci-testdev,bus=pci.0,addr=5.0 "
1015                  "-device pci-testdev,bus=pci.0,addr=6.0,acpi-index=101 "
1016                  "-device pci-testdev,bus=pci.1,addr=1.0 "
1017                  "-device pci-testdev,bus=pci.1,addr=2.0,acpi-index=201 "
1018                  "-device pci-bridge,id=nhpbr,chassis_nr=2,shpc=off,addr=7.0 "
1019                  "-device pci-testdev,bus=nhpbr,addr=1.0,acpi-index=301 "
1020                  , &data);
1021    free_test_data(&data);
1022}
1023
1024static void test_acpi_q35_tcg(void)
1025{
1026    test_data data = {};
1027
1028    data.machine = MACHINE_Q35;
1029    data.arch = "x86";
1030    data.required_struct_types = base_required_struct_types;
1031    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1032    test_acpi_one(NULL, &data);
1033    free_test_data(&data);
1034
1035    data.smbios_cpu_max_speed = 3000;
1036    data.smbios_cpu_curr_speed = 2600;
1037    test_acpi_one("-smbios type=4,max-speed=3000,current-speed=2600", &data);
1038    free_test_data(&data);
1039}
1040
1041static void test_acpi_q35_kvm_type4_count(void)
1042{
1043    test_data data = {
1044        .machine = MACHINE_Q35,
1045        .arch    = "x86",
1046        .variant = ".type4-count",
1047        .required_struct_types = base_required_struct_types,
1048        .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1049        .type4_count = 5,
1050    };
1051
1052    test_acpi_one("-machine smbios-entry-point-type=64 "
1053                  "-smp cpus=100,maxcpus=120,sockets=5,"
1054                  "dies=2,cores=4,threads=3", &data);
1055    free_test_data(&data);
1056}
1057
1058static void test_acpi_q35_kvm_core_count(void)
1059{
1060    test_data data = {
1061        .machine = MACHINE_Q35,
1062        .arch    = "x86",
1063        .variant = ".core-count",
1064        .required_struct_types = base_required_struct_types,
1065        .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1066        .smbios_core_count = 9,
1067        .smbios_core_count2 = 9,
1068    };
1069
1070    test_acpi_one("-machine smbios-entry-point-type=64 "
1071                  "-smp 54,sockets=2,dies=3,cores=3,threads=3",
1072                  &data);
1073    free_test_data(&data);
1074}
1075
1076static void test_acpi_q35_kvm_core_count2(void)
1077{
1078    test_data data = {
1079        .machine = MACHINE_Q35,
1080        .arch    = "x86",
1081        .variant = ".core-count2",
1082        .required_struct_types = base_required_struct_types,
1083        .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1084        .smbios_core_count = 0xFF,
1085        .smbios_core_count2 = 260,
1086    };
1087
1088    test_acpi_one("-machine smbios-entry-point-type=64 "
1089                  "-smp 260,dies=2,cores=130,threads=1",
1090                  &data);
1091    free_test_data(&data);
1092}
1093
1094static void test_acpi_q35_kvm_thread_count(void)
1095{
1096    test_data data = {
1097        .machine = MACHINE_Q35,
1098        .arch    = "x86",
1099        .variant = ".thread-count",
1100        .required_struct_types = base_required_struct_types,
1101        .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1102        .smbios_thread_count = 27,
1103        .smbios_thread_count2 = 27,
1104    };
1105
1106    test_acpi_one("-machine smbios-entry-point-type=64 "
1107                  "-smp cpus=15,maxcpus=54,sockets=2,dies=3,cores=3,threads=3",
1108                  &data);
1109    free_test_data(&data);
1110}
1111
1112static void test_acpi_q35_kvm_thread_count2(void)
1113{
1114    test_data data = {
1115        .machine = MACHINE_Q35,
1116        .arch    = "x86",
1117        .variant = ".thread-count2",
1118        .required_struct_types = base_required_struct_types,
1119        .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1120        .smbios_thread_count = 0xFF,
1121        .smbios_thread_count2 = 260,
1122    };
1123
1124    test_acpi_one("-machine smbios-entry-point-type=64 "
1125                  "-smp cpus=210,maxcpus=260,dies=2,cores=65,threads=2",
1126                  &data);
1127    free_test_data(&data);
1128}
1129
1130static void test_acpi_q35_tcg_bridge(void)
1131{
1132    test_data data = {};
1133
1134    data.machine = MACHINE_Q35;
1135    data.arch    = "x86",
1136    data.variant = ".bridge";
1137    data.required_struct_types = base_required_struct_types;
1138    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1139    test_acpi_one("-device pci-bridge,chassis_nr=1,id=br1"
1140                  " -device pci-testdev,bus=pcie.0"
1141                  " -device pci-testdev,bus=br1", &data);
1142    free_test_data(&data);
1143}
1144
1145static void test_acpi_q35_tcg_no_acpi_hotplug(void)
1146{
1147    test_data data = {};
1148
1149    data.machine = MACHINE_Q35;
1150    data.arch    = "x86",
1151    data.variant = ".noacpihp";
1152    data.required_struct_types = base_required_struct_types;
1153    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1154    test_acpi_one("-global ICH9-LPC.acpi-pci-hotplug-with-bridge-support=off"
1155        " -device pci-testdev,bus=pcie.0,acpi-index=101,addr=3.0"
1156        " -device pci-bridge,chassis_nr=1,id=shpcbr,addr=4.0"
1157        " -device pci-testdev,bus=shpcbr,addr=1.0,acpi-index=201"
1158        " -device pci-bridge,chassis_nr=2,shpc=off,id=noshpcbr,addr=5.0"
1159        " -device pci-testdev,bus=noshpcbr,addr=1.0,acpi-index=301"
1160        " -device pcie-root-port,id=hprp,port=0x0,chassis=1,addr=6.0"
1161        " -device pci-testdev,bus=hprp,acpi-index=401"
1162        " -device pcie-root-port,id=nohprp,port=0x0,chassis=2,hotplug=off,"
1163                                 "addr=7.0"
1164        " -device pci-testdev,bus=nohprp,acpi-index=501"
1165        " -device pcie-root-port,id=nohprpint,port=0x0,chassis=3,hotplug=off,"
1166                                 "multifunction=on,addr=8.0"
1167        " -device pci-testdev,bus=nohprpint,acpi-index=601,addr=0.1"
1168        " -device pcie-root-port,id=hprp2,port=0x0,chassis=4,bus=nohprpint,"
1169                                 "addr=0.2"
1170        " -device pci-testdev,bus=hprp2,acpi-index=602"
1171        , &data);
1172    free_test_data(&data);
1173}
1174
1175static void test_acpi_q35_multif_bridge(void)
1176{
1177    test_data data = {
1178        .machine = MACHINE_Q35,
1179        .arch    = "x86",
1180        .variant = ".multi-bridge",
1181    };
1182    test_vm_prepare("-S"
1183        " -device virtio-balloon,id=balloon0,addr=0x4.0x2"
1184        " -device pcie-root-port,id=rp0,multifunction=on,"
1185                  "port=0x0,chassis=1,addr=0x2"
1186        " -device pcie-root-port,id=rp1,port=0x1,chassis=2,addr=0x3.0x1"
1187        " -device pcie-root-port,id=rp2,port=0x0,chassis=3,bus=rp1,addr=0.0"
1188        " -device pci-bridge,bus=rp2,chassis_nr=4,id=br1"
1189        " -device pcie-root-port,id=rphptgt1,port=0x0,chassis=5,addr=2.1"
1190        " -device pcie-root-port,id=rphptgt2,port=0x0,chassis=6,addr=2.2"
1191        " -device pcie-root-port,id=rphptgt3,port=0x0,chassis=7,addr=2.3"
1192        " -device pci-testdev,bus=pcie.0,addr=2.4"
1193        " -device pci-testdev,bus=pcie.0,addr=2.5,acpi-index=102"
1194        " -device pci-testdev,bus=pcie.0,addr=5.0"
1195        " -device pci-testdev,bus=pcie.0,addr=0xf.0,acpi-index=101"
1196        " -device pci-testdev,bus=rp0,addr=0.0"
1197        " -device pci-testdev,bus=br1"
1198        " -device pcie-root-port,id=rpnohp,chassis=8,addr=0xA.0,hotplug=off"
1199        " -device pcie-root-port,id=rp3,chassis=9,bus=rpnohp"
1200        , &data);
1201
1202    /* hotplugged bridges section */
1203    qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr1",
1204        "{'bus': 'br1', 'addr': '6.0', 'chassis_nr': 128 }");
1205    qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr2-multiif",
1206        "{ 'bus': 'br1', 'addr': '2.2', 'chassis_nr': 129 }");
1207    qtest_qmp_device_add(data.qts, "pcie-pci-bridge", "hpbr3",
1208        "{'bus': 'rphptgt1', 'addr': '0.0' }");
1209    qtest_qmp_device_add(data.qts, "pcie-root-port", "hprp",
1210        "{'bus': 'rphptgt2', 'addr': '0.0' }");
1211    qtest_qmp_device_add(data.qts, "pci-testdev", "hpnic",
1212        "{'bus': 'rphptgt3', 'addr': '0.0' }");
1213    qtest_qmp_send(data.qts, "{'execute':'cont' }");
1214    qtest_qmp_eventwait(data.qts, "RESUME");
1215
1216    process_acpi_tables_noexit(&data);
1217    free_test_data(&data);
1218
1219    /* check that reboot/reset doesn't change any ACPI tables  */
1220    qtest_system_reset(data.qts);
1221    process_acpi_tables(&data);
1222    free_test_data(&data);
1223}
1224
1225static void test_acpi_q35_tcg_mmio64(void)
1226{
1227    test_data data = {
1228        .machine = MACHINE_Q35,
1229        .arch    = "x86",
1230        .variant = ".mmio64",
1231        .tcg_only = true,
1232        .required_struct_types = base_required_struct_types,
1233        .required_struct_types_len = ARRAY_SIZE(base_required_struct_types)
1234    };
1235
1236    test_acpi_one("-m 128M,slots=1,maxmem=2G "
1237                  "-cpu Opteron_G1 "
1238                  "-object memory-backend-ram,id=ram0,size=128M "
1239                  "-numa node,memdev=ram0 "
1240                  "-device pci-testdev,membar=2G",
1241                  &data);
1242    free_test_data(&data);
1243}
1244
1245static void test_acpi_piix4_tcg_cphp(void)
1246{
1247    test_data data = {};
1248
1249    data.machine = MACHINE_PC;
1250    data.arch    = "x86";
1251    data.variant = ".cphp";
1252    test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
1253                  " -object memory-backend-ram,id=ram0,size=64M"
1254                  " -object memory-backend-ram,id=ram1,size=64M"
1255                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
1256                  " -numa dist,src=0,dst=1,val=21",
1257                  &data);
1258    free_test_data(&data);
1259}
1260
1261static void test_acpi_q35_tcg_cphp(void)
1262{
1263    test_data data = {};
1264
1265    data.machine = MACHINE_Q35;
1266    data.arch    = "x86",
1267    data.variant = ".cphp";
1268    test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
1269                  " -object memory-backend-ram,id=ram0,size=64M"
1270                  " -object memory-backend-ram,id=ram1,size=64M"
1271                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
1272                  " -numa dist,src=0,dst=1,val=21",
1273                  &data);
1274    free_test_data(&data);
1275}
1276
1277static uint8_t ipmi_required_struct_types[] = {
1278    0, 1, 3, 4, 16, 17, 19, 32, 38, 127
1279};
1280
1281static void test_acpi_q35_tcg_ipmi(void)
1282{
1283    test_data data = {};
1284
1285    data.machine = MACHINE_Q35;
1286    data.arch    = "x86",
1287    data.variant = ".ipmibt";
1288    data.required_struct_types = ipmi_required_struct_types;
1289    data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1290    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1291                  " -device isa-ipmi-bt,bmc=bmc0",
1292                  &data);
1293    free_test_data(&data);
1294}
1295
1296static void test_acpi_q35_tcg_smbus_ipmi(void)
1297{
1298    test_data data = {};
1299
1300    data.machine = MACHINE_Q35;
1301    data.arch    = "x86",
1302    data.variant = ".ipmismbus";
1303    data.required_struct_types = ipmi_required_struct_types;
1304    data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1305    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1306                  " -device smbus-ipmi,bmc=bmc0",
1307                  &data);
1308    free_test_data(&data);
1309}
1310
1311static void test_acpi_piix4_tcg_ipmi(void)
1312{
1313    test_data data = {};
1314
1315    /* Supplying -machine accel argument overrides the default (qtest).
1316     * This is to make guest actually run.
1317     */
1318    data.machine = MACHINE_PC;
1319    data.arch    = "x86";
1320    data.variant = ".ipmikcs";
1321    data.required_struct_types = ipmi_required_struct_types;
1322    data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1323    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1324                  " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
1325                  &data);
1326    free_test_data(&data);
1327}
1328
1329static void test_acpi_q35_tcg_memhp(void)
1330{
1331    test_data data = {};
1332
1333    data.machine = MACHINE_Q35;
1334    data.arch    = "x86",
1335    data.variant = ".memhp";
1336    test_acpi_one(" -m 128,slots=3,maxmem=1G"
1337                  " -object memory-backend-ram,id=ram0,size=64M"
1338                  " -object memory-backend-ram,id=ram1,size=64M"
1339                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
1340                  " -numa dist,src=0,dst=1,val=21",
1341                  &data);
1342    free_test_data(&data);
1343}
1344
1345static void test_acpi_piix4_tcg_memhp(void)
1346{
1347    test_data data = {};
1348
1349    data.machine = MACHINE_PC;
1350    data.arch    = "x86";
1351    data.variant = ".memhp";
1352    test_acpi_one(" -m 128,slots=3,maxmem=1G"
1353                  " -object memory-backend-ram,id=ram0,size=64M"
1354                  " -object memory-backend-ram,id=ram1,size=64M"
1355                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
1356                  " -numa dist,src=0,dst=1,val=21",
1357                  &data);
1358    free_test_data(&data);
1359}
1360
1361static void test_acpi_piix4_tcg_nosmm(void)
1362{
1363    test_data data = {};
1364
1365    data.machine = MACHINE_PC;
1366    data.arch    = "x86";
1367    data.variant = ".nosmm";
1368    test_acpi_one("-machine smm=off", &data);
1369    free_test_data(&data);
1370}
1371
1372static void test_acpi_piix4_tcg_smm_compat(void)
1373{
1374    test_data data = {};
1375
1376    data.machine = MACHINE_PC;
1377    data.arch    = "x86";
1378    data.variant = ".smm-compat";
1379    test_acpi_one("-global PIIX4_PM.smm-compat=on", &data);
1380    free_test_data(&data);
1381}
1382
1383static void test_acpi_piix4_tcg_smm_compat_nosmm(void)
1384{
1385    test_data data = {};
1386
1387    data.machine = MACHINE_PC;
1388    data.arch    = "x86";
1389    data.variant = ".smm-compat-nosmm";
1390    test_acpi_one("-global PIIX4_PM.smm-compat=on -machine smm=off", &data);
1391    free_test_data(&data);
1392}
1393
1394static void test_acpi_piix4_tcg_nohpet(void)
1395{
1396    test_data data = {};
1397
1398    data.machine = MACHINE_PC;
1399    data.arch    = "x86";
1400    data.machine_param = ",hpet=off";
1401    data.variant = ".nohpet";
1402    test_acpi_one(NULL, &data);
1403    free_test_data(&data);
1404}
1405
1406static void test_acpi_q35_tcg_numamem(void)
1407{
1408    test_data data = {};
1409
1410    data.machine = MACHINE_Q35;
1411    data.arch    = "x86",
1412    data.variant = ".numamem";
1413    test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1414                  " -numa node -numa node,memdev=ram0", &data);
1415    free_test_data(&data);
1416}
1417
1418static void test_acpi_q35_kvm_xapic(void)
1419{
1420    test_data data = {};
1421
1422    data.machine = MACHINE_Q35;
1423    data.arch    = "x86",
1424    data.variant = ".xapic";
1425    test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1426                  " -numa node -numa node,memdev=ram0"
1427                  " -machine kernel-irqchip=on -smp 1,maxcpus=288", &data);
1428    free_test_data(&data);
1429}
1430
1431static void test_acpi_q35_tcg_nosmm(void)
1432{
1433    test_data data = {};
1434
1435    data.machine = MACHINE_Q35;
1436    data.arch    = "x86",
1437    data.variant = ".nosmm";
1438    test_acpi_one("-machine smm=off", &data);
1439    free_test_data(&data);
1440}
1441
1442static void test_acpi_q35_tcg_smm_compat(void)
1443{
1444    test_data data = {};
1445
1446    data.machine = MACHINE_Q35;
1447    data.arch    = "x86",
1448    data.variant = ".smm-compat";
1449    test_acpi_one("-global ICH9-LPC.smm-compat=on", &data);
1450    free_test_data(&data);
1451}
1452
1453static void test_acpi_q35_tcg_smm_compat_nosmm(void)
1454{
1455    test_data data = {};
1456
1457    data.machine = MACHINE_Q35;
1458    data.arch    = "x86",
1459    data.variant = ".smm-compat-nosmm";
1460    test_acpi_one("-global ICH9-LPC.smm-compat=on -machine smm=off", &data);
1461    free_test_data(&data);
1462}
1463
1464static void test_acpi_q35_tcg_nohpet(void)
1465{
1466    test_data data = {};
1467
1468    data.machine = MACHINE_Q35;
1469    data.arch    = "x86",
1470    data.machine_param = ",hpet=off";
1471    data.variant = ".nohpet";
1472    test_acpi_one(NULL, &data);
1473    free_test_data(&data);
1474}
1475
1476static void test_acpi_q35_kvm_dmar(void)
1477{
1478    test_data data = {};
1479
1480    data.machine = MACHINE_Q35;
1481    data.arch    = "x86",
1482    data.variant = ".dmar";
1483    test_acpi_one("-machine kernel-irqchip=split -accel kvm"
1484                  " -device intel-iommu,intremap=on,device-iotlb=on", &data);
1485    free_test_data(&data);
1486}
1487
1488static void test_acpi_q35_tcg_ivrs(void)
1489{
1490    test_data data = {};
1491
1492    data.machine = MACHINE_Q35;
1493    data.arch    = "x86",
1494    data.variant = ".ivrs";
1495    data.tcg_only = true,
1496    test_acpi_one(" -device amd-iommu", &data);
1497    free_test_data(&data);
1498}
1499
1500static void test_acpi_piix4_tcg_numamem(void)
1501{
1502    test_data data = {};
1503
1504    data.machine = MACHINE_PC;
1505    data.arch    = "x86";
1506    data.variant = ".numamem";
1507    test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1508                  " -numa node -numa node,memdev=ram0", &data);
1509    free_test_data(&data);
1510}
1511
1512uint64_t tpm_tis_base_addr;
1513
1514static void test_acpi_tcg_tpm(const char *machine, const char *arch,
1515                              const char *tpm_if, uint64_t base,
1516                              enum TPMVersion tpm_version)
1517{
1518    gchar *tmp_dir_name = g_strdup_printf("qemu-test_acpi_%s_tcg_%s.XXXXXX",
1519                                          machine, tpm_if);
1520    char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
1521    TPMTestState test;
1522    test_data data = {};
1523    GThread *thread;
1524    const char *suffix = tpm_version == TPM_VERSION_2_0 ? "tpm2" : "tpm12";
1525    char *args, *variant = g_strdup_printf(".%s.%s", tpm_if, suffix);
1526
1527    tpm_tis_base_addr = base;
1528
1529    module_call_init(MODULE_INIT_QOM);
1530
1531    test.addr = g_new0(SocketAddress, 1);
1532    test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
1533    test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
1534    g_mutex_init(&test.data_mutex);
1535    g_cond_init(&test.data_cond);
1536    test.data_cond_signal = false;
1537    test.tpm_version = tpm_version;
1538
1539    thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
1540    tpm_emu_test_wait_cond(&test);
1541
1542    data.machine = machine;
1543    data.arch = arch;
1544    data.variant = variant;
1545
1546    args = g_strdup_printf(
1547        " -chardev socket,id=chr,path=%s"
1548        " -tpmdev emulator,id=dev,chardev=chr"
1549        " -device tpm-%s,tpmdev=dev",
1550        test.addr->u.q_unix.path, tpm_if);
1551
1552    test_acpi_one(args, &data);
1553
1554    g_thread_join(thread);
1555    g_unlink(test.addr->u.q_unix.path);
1556    qapi_free_SocketAddress(test.addr);
1557    g_rmdir(tmp_path);
1558    g_free(variant);
1559    g_free(tmp_path);
1560    g_free(tmp_dir_name);
1561    g_free(args);
1562    free_test_data(&data);
1563}
1564
1565static void test_acpi_q35_tcg_tpm2_tis(void)
1566{
1567    test_acpi_tcg_tpm("q35", "x86", "tis", 0xFED40000, TPM_VERSION_2_0);
1568}
1569
1570static void test_acpi_q35_tcg_tpm12_tis(void)
1571{
1572    test_acpi_tcg_tpm("q35", "x86", "tis", 0xFED40000, TPM_VERSION_1_2);
1573}
1574
1575static void test_acpi_tcg_dimm_pxm(const char *machine, const char *arch)
1576{
1577    test_data data = {};
1578
1579    data.machine = machine;
1580    data.arch    = arch;
1581    data.variant = ".dimmpxm";
1582    test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
1583                  " -smp 4,sockets=4"
1584                  " -m 128M,slots=3,maxmem=1G"
1585                  " -object memory-backend-ram,id=ram0,size=32M"
1586                  " -object memory-backend-ram,id=ram1,size=32M"
1587                  " -object memory-backend-ram,id=ram2,size=32M"
1588                  " -object memory-backend-ram,id=ram3,size=32M"
1589                  " -numa node,memdev=ram0,nodeid=0"
1590                  " -numa node,memdev=ram1,nodeid=1"
1591                  " -numa node,memdev=ram2,nodeid=2"
1592                  " -numa node,memdev=ram3,nodeid=3"
1593                  " -numa cpu,node-id=0,socket-id=0"
1594                  " -numa cpu,node-id=1,socket-id=1"
1595                  " -numa cpu,node-id=2,socket-id=2"
1596                  " -numa cpu,node-id=3,socket-id=3"
1597                  " -object memory-backend-ram,id=ram4,size=128M"
1598                  " -object memory-backend-ram,id=nvm0,size=128M"
1599                  " -device pc-dimm,id=dimm0,memdev=ram4,node=1"
1600                  " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
1601                  &data);
1602    free_test_data(&data);
1603}
1604
1605static void test_acpi_q35_tcg_dimm_pxm(void)
1606{
1607    test_acpi_tcg_dimm_pxm(MACHINE_Q35, "x86");
1608}
1609
1610static void test_acpi_piix4_tcg_dimm_pxm(void)
1611{
1612    test_acpi_tcg_dimm_pxm(MACHINE_PC, "x86");
1613}
1614
1615static void test_acpi_aarch64_virt_tcg_memhp(void)
1616{
1617    test_data data = {
1618        .machine = "virt",
1619        .arch = "aarch64",
1620        .tcg_only = true,
1621        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1622        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1623        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1624        .ram_start = 0x40000000ULL,
1625        .scan_len = 256ULL * MiB,
1626    };
1627
1628    data.variant = ".memhp";
1629    test_acpi_one(" -machine nvdimm=on"
1630                  " -cpu cortex-a57"
1631                  " -m 256M,slots=3,maxmem=1G"
1632                  " -object memory-backend-ram,id=ram0,size=128M"
1633                  " -object memory-backend-ram,id=ram1,size=128M"
1634                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
1635                  " -numa dist,src=0,dst=1,val=21"
1636                  " -object memory-backend-ram,id=ram2,size=128M"
1637                  " -object memory-backend-ram,id=nvm0,size=128M"
1638                  " -device pc-dimm,id=dimm0,memdev=ram2,node=0"
1639                  " -device nvdimm,id=dimm1,memdev=nvm0,node=1",
1640                  &data);
1641
1642    free_test_data(&data);
1643
1644}
1645
1646static void test_acpi_aarch64_virt_acpi_pci_hotplug(void)
1647{
1648    test_data data = {
1649        .machine = "virt",
1650        .arch = "aarch64",
1651        .tcg_only = true,
1652        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1653        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1654        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1655        .ram_start = 0x40000000ULL,
1656        .scan_len = 256ULL * MiB,
1657        .variant = ".acpipcihp",
1658    };
1659
1660   /* Use ACPI PCI Hotplug */
1661   test_acpi_one(" -global acpi-ged.acpi-pci-hotplug-with-bridge-support=on"
1662                 " -cpu cortex-a57"
1663                 " -device pcie-root-port,id=pcie.1,bus=pcie.0,chassis=0,slot=1,addr=7.0"
1664                 " -device pci-testdev,bus=pcie.1",
1665                 &data);
1666
1667    free_test_data(&data);
1668}
1669
1670static void test_acpi_aarch64_virt_pcie_root_port_hpoff(void)
1671{
1672    test_data data = {
1673        .machine = "virt",
1674        .arch = "aarch64",
1675        .tcg_only = true,
1676        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1677        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1678        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1679        .ram_start = 0x40000000ULL,
1680        .scan_len = 256ULL * MiB,
1681        .variant = ".hpoffacpiindex",
1682    };
1683
1684   /* turn hotplug off on the pcie-root-port and use static acpi-index*/
1685   test_acpi_one(" -device pcie-root-port,id=pcie.1,chassis=0,"
1686                                          "slot=1,hotplug=off,addr=7.0"
1687                 " -device pci-testdev,bus=pcie.1,acpi-index=12"
1688                 " -cpu cortex-a57",
1689                 &data);
1690
1691    free_test_data(&data);
1692}
1693
1694static void test_acpi_microvm_prepare(test_data *data)
1695{
1696    data->machine = "microvm";
1697    data->arch = "x86";
1698    data->required_struct_types = NULL; /* no smbios */
1699    data->required_struct_types_len = 0;
1700    data->blkdev = "virtio-blk-device";
1701}
1702
1703static void test_acpi_microvm_tcg(void)
1704{
1705    test_data data = {};
1706
1707    test_acpi_microvm_prepare(&data);
1708    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off",
1709                  &data);
1710    free_test_data(&data);
1711}
1712
1713static void test_acpi_microvm_usb_tcg(void)
1714{
1715    test_data data = {};
1716
1717    test_acpi_microvm_prepare(&data);
1718    data.variant = ".usb";
1719    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,usb=on,rtc=off",
1720                  &data);
1721    free_test_data(&data);
1722}
1723
1724static void test_acpi_microvm_rtc_tcg(void)
1725{
1726    test_data data = {};
1727
1728    test_acpi_microvm_prepare(&data);
1729    data.variant = ".rtc";
1730    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=on",
1731                  &data);
1732    free_test_data(&data);
1733}
1734
1735static void test_acpi_microvm_pcie_tcg(void)
1736{
1737    test_data data = {};
1738
1739    test_acpi_microvm_prepare(&data);
1740    data.variant = ".pcie";
1741    data.tcg_only = true; /* need constant host-phys-bits */
1742    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off,pcie=on",
1743                  &data);
1744    free_test_data(&data);
1745}
1746
1747static void test_acpi_microvm_ioapic2_tcg(void)
1748{
1749    test_data data = {};
1750
1751    test_acpi_microvm_prepare(&data);
1752    data.variant = ".ioapic2";
1753    test_acpi_one(" -machine microvm,acpi=on,ioapic2=on,rtc=off",
1754                  &data);
1755    free_test_data(&data);
1756}
1757
1758static void test_acpi_riscv64_virt_tcg_numamem(void)
1759{
1760    test_data data = {
1761        .machine = "virt",
1762        .arch = "riscv64",
1763        .tcg_only = true,
1764        .uefi_fl1 = "pc-bios/edk2-riscv-code.fd",
1765        .uefi_fl2 = "pc-bios/edk2-riscv-vars.fd",
1766        .cd = "tests/data/uefi-boot-images/bios-tables-test.riscv64.iso.qcow2",
1767        .ram_start = 0x80000000ULL,
1768        .scan_len = 128ULL * MiB,
1769    };
1770
1771    data.variant = ".numamem";
1772    /*
1773     * RHCT will have ISA string encoded. To reduce the effort
1774     * of updating expected AML file for any new default ISA extension,
1775     * use the profile rva22s64.
1776     */
1777    test_acpi_one(" -cpu rva22s64"
1778                  " -object memory-backend-ram,id=ram0,size=128M"
1779                  " -numa node,memdev=ram0",
1780                  &data);
1781    free_test_data(&data);
1782}
1783
1784static void test_acpi_aarch64_virt_tcg_numamem(void)
1785{
1786    test_data data = {
1787        .machine = "virt",
1788        .arch = "aarch64",
1789        .tcg_only = true,
1790        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1791        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1792        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1793        .ram_start = 0x40000000ULL,
1794        .scan_len = 128ULL * MiB,
1795    };
1796
1797    data.variant = ".numamem";
1798    test_acpi_one(" -cpu cortex-a57"
1799                  " -object memory-backend-ram,id=ram0,size=128M"
1800                  " -numa node,memdev=ram0",
1801                  &data);
1802
1803    free_test_data(&data);
1804
1805}
1806
1807static void test_acpi_aarch64_virt_tcg_pxb(void)
1808{
1809    test_data data = {
1810        .machine = "virt",
1811        .arch = "aarch64",
1812        .tcg_only = true,
1813        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1814        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1815        .ram_start = 0x40000000ULL,
1816        .scan_len = 128ULL * MiB,
1817    };
1818    /*
1819     * While using -cdrom, the cdrom would auto plugged into pxb-pcie,
1820     * the reason is the bus of pxb-pcie is also root bus, it would lead
1821     * to the error only PCI/PCIE bridge could plug onto pxb.
1822     * Therefore,thr cdrom is defined and plugged onto the scsi controller
1823     * to solve the conflicts.
1824     */
1825    data.variant = ".pxb";
1826    test_acpi_one(" -device pcie-root-port,chassis=1,id=pci.1"
1827                  " -device virtio-scsi-pci,id=scsi0,bus=pci.1"
1828                  " -drive file="
1829                  "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2,"
1830                  "if=none,media=cdrom,id=drive-scsi0-0-0-1,readonly=on"
1831                  " -device scsi-cd,bus=scsi0.0,scsi-id=0,"
1832                  "drive=drive-scsi0-0-0-1,id=scsi0-0-0-1,bootindex=1"
1833                  " -cpu cortex-a57"
1834                  " -device pxb-pcie,bus_nr=128",
1835                  &data);
1836
1837    free_test_data(&data);
1838}
1839
1840static void test_acpi_aarch64_virt_tcg_acpi_spcr(void)
1841{
1842    test_data data = {
1843        .machine = "virt",
1844        .arch = "aarch64",
1845        .tcg_only = true,
1846        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1847        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1848        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1849        .ram_start = 0x40000000ULL,
1850        .scan_len = 128ULL * 1024 * 1024,
1851        .variant = ".acpispcr",
1852    };
1853
1854    test_acpi_one("-cpu cortex-a57 "
1855                  " -machine spcr=off", &data);
1856    free_test_data(&data);
1857}
1858
1859static void test_acpi_riscv64_virt_tcg_acpi_spcr(void)
1860{
1861    test_data data = {
1862        .machine = "virt",
1863        .arch = "riscv64",
1864        .tcg_only = true,
1865        .uefi_fl1 = "pc-bios/edk2-riscv-code.fd",
1866        .uefi_fl2 = "pc-bios/edk2-riscv-vars.fd",
1867        .cd = "tests/data/uefi-boot-images/bios-tables-test.riscv64.iso.qcow2",
1868        .ram_start = 0x80000000ULL,
1869        .scan_len = 128ULL * 1024 * 1024,
1870        .variant = ".acpispcr",
1871    };
1872
1873    test_acpi_one("-cpu rva22s64 "
1874                  "-machine spcr=off", &data);
1875    free_test_data(&data);
1876}
1877
1878static void test_acpi_tcg_acpi_hmat(const char *machine, const char *arch)
1879{
1880    test_data data = {};
1881
1882    data.machine = machine;
1883    data.arch    = arch;
1884    data.variant = ".acpihmat";
1885    test_acpi_one(" -machine hmat=on"
1886                  " -smp 2,sockets=2"
1887                  " -m 128M,slots=2,maxmem=1G"
1888                  " -object memory-backend-ram,size=64M,id=m0"
1889                  " -object memory-backend-ram,size=64M,id=m1"
1890                  " -numa node,nodeid=0,memdev=m0"
1891                  " -numa node,nodeid=1,memdev=m1,initiator=0"
1892                  " -numa cpu,node-id=0,socket-id=0"
1893                  " -numa cpu,node-id=0,socket-id=1"
1894                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1895                  "data-type=access-latency,latency=1"
1896                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1897                  "data-type=access-bandwidth,bandwidth=65534M"
1898                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1899                  "data-type=access-latency,latency=65534"
1900                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1901                  "data-type=access-bandwidth,bandwidth=32767M"
1902                  " -numa hmat-cache,node-id=0,size=10K,level=1,"
1903                  "associativity=direct,policy=write-back,line=8"
1904                  " -numa hmat-cache,node-id=1,size=10K,level=1,"
1905                  "associativity=direct,policy=write-back,line=8",
1906                  &data);
1907    free_test_data(&data);
1908}
1909
1910static void test_acpi_q35_tcg_acpi_hmat(void)
1911{
1912    test_acpi_tcg_acpi_hmat(MACHINE_Q35, "x86");
1913}
1914
1915static void test_acpi_piix4_tcg_acpi_hmat(void)
1916{
1917    test_acpi_tcg_acpi_hmat(MACHINE_PC, "x86");
1918}
1919
1920static void test_acpi_aarch64_virt_tcg_acpi_hmat(void)
1921{
1922    test_data data = {
1923        .machine = "virt",
1924        .arch = "aarch64",
1925        .tcg_only = true,
1926        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1927        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1928        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1929        .ram_start = 0x40000000ULL,
1930        .scan_len = 128ULL * MiB,
1931    };
1932
1933    data.variant = ".acpihmatvirt";
1934
1935    test_acpi_one(" -machine hmat=on"
1936                  " -cpu cortex-a57"
1937                  " -smp 4,sockets=2"
1938                  " -m 384M"
1939                  " -object memory-backend-ram,size=128M,id=ram0"
1940                  " -object memory-backend-ram,size=128M,id=ram1"
1941                  " -object memory-backend-ram,size=128M,id=ram2"
1942                  " -numa node,nodeid=0,memdev=ram0"
1943                  " -numa node,nodeid=1,memdev=ram1"
1944                  " -numa node,nodeid=2,memdev=ram2"
1945                  " -numa cpu,node-id=0,socket-id=0"
1946                  " -numa cpu,node-id=0,socket-id=0"
1947                  " -numa cpu,node-id=1,socket-id=1"
1948                  " -numa cpu,node-id=1,socket-id=1"
1949                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1950                  "data-type=access-latency,latency=10"
1951                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1952                  "data-type=access-bandwidth,bandwidth=10485760"
1953                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1954                  "data-type=access-latency,latency=20"
1955                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1956                  "data-type=access-bandwidth,bandwidth=5242880"
1957                  " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1958                  "data-type=access-latency,latency=30"
1959                  " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1960                  "data-type=access-bandwidth,bandwidth=1048576"
1961                  " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1962                  "data-type=access-latency,latency=20"
1963                  " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1964                  "data-type=access-bandwidth,bandwidth=5242880"
1965                  " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1966                  "data-type=access-latency,latency=10"
1967                  " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1968                  "data-type=access-bandwidth,bandwidth=10485760"
1969                  " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1970                  "data-type=access-latency,latency=30"
1971                  " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1972                  "data-type=access-bandwidth,bandwidth=1048576",
1973                  &data);
1974
1975    free_test_data(&data);
1976}
1977
1978static void test_acpi_q35_tcg_acpi_hmat_noinitiator(void)
1979{
1980    test_data data = {};
1981
1982    data.machine = MACHINE_Q35;
1983    data.arch    = "x86";
1984    data.variant = ".acpihmat-noinitiator";
1985    test_acpi_one(" -machine hmat=on"
1986                  " -smp 4,sockets=2"
1987                  " -m 128M"
1988                  " -object memory-backend-ram,size=32M,id=ram0"
1989                  " -object memory-backend-ram,size=32M,id=ram1"
1990                  " -object memory-backend-ram,size=64M,id=ram2"
1991                  " -numa node,nodeid=0,memdev=ram0"
1992                  " -numa node,nodeid=1,memdev=ram1"
1993                  " -numa node,nodeid=2,memdev=ram2"
1994                  " -numa cpu,node-id=0,socket-id=0"
1995                  " -numa cpu,node-id=0,socket-id=0"
1996                  " -numa cpu,node-id=1,socket-id=1"
1997                  " -numa cpu,node-id=1,socket-id=1"
1998                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1999                  "data-type=access-latency,latency=10"
2000                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
2001                  "data-type=access-bandwidth,bandwidth=10485760"
2002                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
2003                  "data-type=access-latency,latency=20"
2004                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
2005                  "data-type=access-bandwidth,bandwidth=5242880"
2006                  " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
2007                  "data-type=access-latency,latency=30"
2008                  " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
2009                  "data-type=access-bandwidth,bandwidth=1048576"
2010                  " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
2011                  "data-type=access-latency,latency=20"
2012                  " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
2013                  "data-type=access-bandwidth,bandwidth=5242880"
2014                  " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
2015                  "data-type=access-latency,latency=10"
2016                  " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
2017                  "data-type=access-bandwidth,bandwidth=10485760"
2018                  " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
2019                  "data-type=access-latency,latency=30"
2020                  " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
2021                  "data-type=access-bandwidth,bandwidth=1048576",
2022                  &data);
2023    free_test_data(&data);
2024}
2025
2026/* Test intended to hit corner cases of SRAT and HMAT */
2027static void test_acpi_q35_tcg_acpi_hmat_generic_x(void)
2028{
2029    test_data data = {};
2030
2031    data.machine = MACHINE_Q35;
2032    data.arch    = "x86";
2033    data.variant = ".acpihmat-generic-x";
2034    test_acpi_one(" -machine hmat=on,cxl=on"
2035                  " -smp 3,sockets=3"
2036                  " -m 128M,maxmem=384M,slots=2"
2037                  " -device pcie-root-port,chassis=1,id=pci.1"
2038                  " -device pci-testdev,bus=pci.1,"
2039                  "multifunction=on,addr=00.0"
2040                  " -device pci-testdev,bus=pci.1,addr=00.1"
2041                  " -device pci-testdev,bus=pci.1,id=gidev,addr=00.2"
2042                  " -device pxb-cxl,bus_nr=64,bus=pcie.0,id=cxl.1"
2043                  " -object memory-backend-ram,size=64M,id=ram0"
2044                  " -object memory-backend-ram,size=64M,id=ram1"
2045                  " -numa node,nodeid=0,cpus=0,memdev=ram0"
2046                  " -numa node,nodeid=1"
2047                  " -object acpi-generic-initiator,id=gi0,pci-dev=gidev,node=1"
2048                  " -numa node,nodeid=2"
2049                  " -object acpi-generic-port,id=gp0,pci-bus=cxl.1,node=2"
2050                  " -numa node,nodeid=3,cpus=1"
2051                  " -numa node,nodeid=4,memdev=ram1"
2052                  " -numa node,nodeid=5,cpus=2"
2053                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
2054                  "data-type=access-latency,latency=10"
2055                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
2056                  "data-type=access-bandwidth,bandwidth=800M"
2057                  " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
2058                  "data-type=access-latency,latency=100"
2059                  " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
2060                  "data-type=access-bandwidth,bandwidth=200M"
2061                  " -numa hmat-lb,initiator=0,target=4,hierarchy=memory,"
2062                  "data-type=access-latency,latency=100"
2063                  " -numa hmat-lb,initiator=0,target=4,hierarchy=memory,"
2064                  "data-type=access-bandwidth,bandwidth=200M"
2065                  " -numa hmat-lb,initiator=0,target=5,hierarchy=memory,"
2066                  "data-type=access-latency,latency=200"
2067                  " -numa hmat-lb,initiator=0,target=5,hierarchy=memory,"
2068                  "data-type=access-bandwidth,bandwidth=400M"
2069                  " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
2070                  "data-type=access-latency,latency=500"
2071                  " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
2072                  "data-type=access-bandwidth,bandwidth=100M"
2073                  " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
2074                  "data-type=access-latency,latency=50"
2075                  " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
2076                  "data-type=access-bandwidth,bandwidth=400M"
2077                  " -numa hmat-lb,initiator=1,target=4,hierarchy=memory,"
2078                  "data-type=access-latency,latency=50"
2079                  " -numa hmat-lb,initiator=1,target=4,hierarchy=memory,"
2080                  "data-type=access-bandwidth,bandwidth=800M"
2081                  " -numa hmat-lb,initiator=1,target=5,hierarchy=memory,"
2082                  "data-type=access-latency,latency=500"
2083                  " -numa hmat-lb,initiator=1,target=5,hierarchy=memory,"
2084                  "data-type=access-bandwidth,bandwidth=100M"
2085                  " -numa hmat-lb,initiator=3,target=0,hierarchy=memory,"
2086                  "data-type=access-latency,latency=20"
2087                  " -numa hmat-lb,initiator=3,target=0,hierarchy=memory,"
2088                  "data-type=access-bandwidth,bandwidth=400M"
2089                  " -numa hmat-lb,initiator=3,target=2,hierarchy=memory,"
2090                  "data-type=access-latency,latency=80"
2091                  " -numa hmat-lb,initiator=3,target=2,hierarchy=memory,"
2092                  "data-type=access-bandwidth,bandwidth=200M"
2093                  " -numa hmat-lb,initiator=3,target=4,hierarchy=memory,"
2094                  "data-type=access-latency,latency=80"
2095                  " -numa hmat-lb,initiator=3,target=4,hierarchy=memory,"
2096                  "data-type=access-bandwidth,bandwidth=200M"
2097                  " -numa hmat-lb,initiator=3,target=5,hierarchy=memory,"
2098                  "data-type=access-latency,latency=20"
2099                  " -numa hmat-lb,initiator=3,target=5,hierarchy=memory,"
2100                  "data-type=access-bandwidth,bandwidth=400M"
2101                  " -numa hmat-lb,initiator=5,target=0,hierarchy=memory,"
2102                  "data-type=access-latency,latency=20"
2103                  " -numa hmat-lb,initiator=5,target=0,hierarchy=memory,"
2104                  "data-type=access-bandwidth,bandwidth=400M"
2105                  " -numa hmat-lb,initiator=5,target=2,hierarchy=memory,"
2106                  "data-type=access-latency,latency=80"
2107                  " -numa hmat-lb,initiator=5,target=4,hierarchy=memory,"
2108                  "data-type=access-bandwidth,bandwidth=200M"
2109                  " -numa hmat-lb,initiator=5,target=4,hierarchy=memory,"
2110                  "data-type=access-latency,latency=80"
2111                  " -numa hmat-lb,initiator=5,target=2,hierarchy=memory,"
2112                  "data-type=access-bandwidth,bandwidth=200M"
2113                  " -numa hmat-lb,initiator=5,target=5,hierarchy=memory,"
2114                  "data-type=access-latency,latency=10"
2115                  " -numa hmat-lb,initiator=5,target=5,hierarchy=memory,"
2116                  "data-type=access-bandwidth,bandwidth=800M",
2117                  &data);
2118    free_test_data(&data);
2119}
2120
2121#ifdef CONFIG_POSIX
2122static void test_acpi_erst(const char *machine, const char *arch)
2123{
2124    gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
2125    gchar *params;
2126    test_data data = {};
2127
2128    data.machine = machine;
2129    data.arch    = arch;
2130    data.variant = ".acpierst";
2131    params = g_strdup_printf(
2132        " -object memory-backend-file,id=erstnvram,"
2133            "mem-path=%s,size=0x10000,share=on"
2134        " -device acpi-erst,memdev=erstnvram", tmp_path);
2135    test_acpi_one(params, &data);
2136    free_test_data(&data);
2137    g_free(params);
2138    g_assert(g_rmdir(tmp_path) == 0);
2139    g_free(tmp_path);
2140}
2141
2142static void test_acpi_piix4_acpi_erst(void)
2143{
2144    test_acpi_erst(MACHINE_PC, "x86");
2145}
2146
2147static void test_acpi_q35_acpi_erst(void)
2148{
2149    test_acpi_erst(MACHINE_Q35, "x86");
2150}
2151
2152static void test_acpi_microvm_acpi_erst(void)
2153{
2154    gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
2155    gchar *params;
2156    test_data data = {};
2157
2158    test_acpi_microvm_prepare(&data);
2159    data.variant = ".pcie";
2160    data.tcg_only = true; /* need constant host-phys-bits */
2161    params = g_strdup_printf(" -machine microvm,"
2162        "acpi=on,ioapic2=off,rtc=off,pcie=on"
2163        " -object memory-backend-file,id=erstnvram,"
2164           "mem-path=%s,size=0x10000,share=on"
2165        " -device acpi-erst,memdev=erstnvram", tmp_path);
2166    test_acpi_one(params, &data);
2167    g_free(params);
2168    g_assert(g_rmdir(tmp_path) == 0);
2169    g_free(tmp_path);
2170    free_test_data(&data);
2171}
2172#endif /* CONFIG_POSIX */
2173
2174static void test_acpi_riscv64_virt_tcg(void)
2175{
2176    test_data data = {
2177        .machine = "virt",
2178        .arch = "riscv64",
2179        .tcg_only = true,
2180        .uefi_fl1 = "pc-bios/edk2-riscv-code.fd",
2181        .uefi_fl2 = "pc-bios/edk2-riscv-vars.fd",
2182        .cd = "tests/data/uefi-boot-images/bios-tables-test.riscv64.iso.qcow2",
2183        .ram_start = 0x80000000ULL,
2184        .scan_len = 128ULL * MiB,
2185    };
2186
2187    /*
2188     * RHCT will have ISA string encoded. To reduce the effort
2189     * of updating expected AML file for any new default ISA extension,
2190     * use the profile rva22s64.
2191     */
2192    test_acpi_one("-cpu rva22s64 ", &data);
2193    free_test_data(&data);
2194}
2195
2196static void test_acpi_aarch64_virt_tcg(void)
2197{
2198    test_data data = {
2199        .machine = "virt",
2200        .arch = "aarch64",
2201        .tcg_only = true,
2202        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
2203        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
2204        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
2205        .ram_start = 0x40000000ULL,
2206        .scan_len = 128ULL * MiB,
2207    };
2208
2209    data.smbios_cpu_max_speed = 2900;
2210    data.smbios_cpu_curr_speed = 2700;
2211    test_acpi_one("-cpu cortex-a57 -machine ras=on "
2212                  "-smbios type=4,max-speed=2900,current-speed=2700", &data);
2213    free_test_data(&data);
2214}
2215
2216static void test_acpi_aarch64_virt_tcg_topology(void)
2217{
2218    test_data data = {
2219        .machine = "virt",
2220        .arch = "aarch64",
2221        .variant = ".topology",
2222        .tcg_only = true,
2223        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
2224        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
2225        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
2226        .ram_start = 0x40000000ULL,
2227        .scan_len = 128ULL * MiB,
2228    };
2229
2230    test_acpi_one("-cpu cortex-a57 "
2231                  "-smp sockets=1,clusters=2,cores=2,threads=2", &data);
2232    free_test_data(&data);
2233}
2234
2235static void test_acpi_aarch64_virt_tcg_its_off(void)
2236{
2237    test_data data = {
2238        .machine = "virt",
2239        .arch = "aarch64",
2240        .variant = ".its_off",
2241        .tcg_only = true,
2242        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
2243        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
2244        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
2245        .ram_start = 0x40000000ULL,
2246        .scan_len = 128ULL * 1024 * 1024,
2247    };
2248
2249    test_acpi_one("-cpu cortex-a57 "
2250                  "-M gic-version=3,iommu=smmuv3,its=off", &data);
2251    free_test_data(&data);
2252}
2253
2254static void test_acpi_q35_viot(void)
2255{
2256    test_data data = {
2257        .machine = MACHINE_Q35,
2258        .arch    = "x86",
2259        .variant = ".viot",
2260    };
2261
2262    /*
2263     * To keep things interesting, two buses bypass the IOMMU.
2264     * VIOT should only describes the other two buses.
2265     */
2266    test_acpi_one("-machine default_bus_bypass_iommu=on "
2267                  "-device virtio-iommu-pci "
2268                  "-device pxb-pcie,bus_nr=0x10,id=pcie.100,bus=pcie.0 "
2269                  "-device pxb-pcie,bus_nr=0x20,id=pcie.200,bus=pcie.0,bypass_iommu=on "
2270                  "-device pxb-pcie,bus_nr=0x30,id=pcie.300,bus=pcie.0",
2271                  &data);
2272    free_test_data(&data);
2273}
2274
2275#ifdef CONFIG_POSIX
2276static void test_acpi_q35_cxl(void)
2277{
2278    gchar *tmp_path = g_dir_make_tmp("qemu-test-cxl.XXXXXX", NULL);
2279    gchar *params;
2280
2281    test_data data = {
2282        .machine = MACHINE_Q35,
2283        .arch    = "x86",
2284        .variant = ".cxl",
2285    };
2286    /*
2287     * A complex CXL setup.
2288     */
2289    params = g_strdup_printf(" -machine cxl=on"
2290                             " -object memory-backend-file,id=cxl-mem1,mem-path=%s,size=256M"
2291                             " -object memory-backend-file,id=cxl-mem2,mem-path=%s,size=256M"
2292                             " -object memory-backend-file,id=cxl-mem3,mem-path=%s,size=256M"
2293                             " -object memory-backend-file,id=cxl-mem4,mem-path=%s,size=256M"
2294                             " -object memory-backend-file,id=lsa1,mem-path=%s,size=256M"
2295                             " -object memory-backend-file,id=lsa2,mem-path=%s,size=256M"
2296                             " -object memory-backend-file,id=lsa3,mem-path=%s,size=256M"
2297                             " -object memory-backend-file,id=lsa4,mem-path=%s,size=256M"
2298                             " -device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1"
2299                             " -device pxb-cxl,bus_nr=222,bus=pcie.0,id=cxl.2"
2300                             " -device cxl-rp,port=0,bus=cxl.1,id=rp1,chassis=0,slot=2"
2301                             " -device cxl-type3,bus=rp1,persistent-memdev=cxl-mem1,lsa=lsa1"
2302                             " -device cxl-rp,port=1,bus=cxl.1,id=rp2,chassis=0,slot=3"
2303                             " -device cxl-type3,bus=rp2,persistent-memdev=cxl-mem2,lsa=lsa2"
2304                             " -device cxl-rp,port=0,bus=cxl.2,id=rp3,chassis=0,slot=5"
2305                             " -device cxl-type3,bus=rp3,persistent-memdev=cxl-mem3,lsa=lsa3"
2306                             " -device cxl-rp,port=1,bus=cxl.2,id=rp4,chassis=0,slot=6"
2307                             " -device cxl-type3,bus=rp4,persistent-memdev=cxl-mem4,lsa=lsa4"
2308                             " -M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G,cxl-fmw.0.interleave-granularity=8k,"
2309                             "cxl-fmw.1.targets.0=cxl.1,cxl-fmw.1.targets.1=cxl.2,cxl-fmw.1.size=4G,cxl-fmw.1.interleave-granularity=8k",
2310                             tmp_path, tmp_path, tmp_path, tmp_path,
2311                             tmp_path, tmp_path, tmp_path, tmp_path);
2312    test_acpi_one(params, &data);
2313
2314    g_free(params);
2315    g_assert(g_rmdir(tmp_path) == 0);
2316    g_free(tmp_path);
2317    free_test_data(&data);
2318}
2319#endif /* CONFIG_POSIX */
2320
2321static void test_acpi_aarch64_virt_viot(void)
2322{
2323    test_data data = {
2324        .machine = "virt",
2325        .arch = "aarch64",
2326        .variant = ".viot",
2327        .tcg_only = true,
2328        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
2329        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
2330        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
2331        .ram_start = 0x40000000ULL,
2332        .scan_len = 128ULL * MiB,
2333    };
2334
2335    test_acpi_one("-cpu cortex-a57 "
2336                  "-device virtio-iommu-pci", &data);
2337    free_test_data(&data);
2338}
2339
2340#ifndef _WIN32
2341# define DEV_NULL "/dev/null"
2342#else
2343# define DEV_NULL "nul"
2344#endif
2345
2346static void test_acpi_q35_slic(void)
2347{
2348    test_data data = {
2349        .machine = MACHINE_Q35,
2350        .arch    = "x86",
2351        .variant = ".slic",
2352    };
2353
2354    test_acpi_one("-acpitable sig=SLIC,oem_id=\"CRASH \",oem_table_id=ME,"
2355                  "oem_rev=00002210,asl_compiler_id=qemu,"
2356                  "asl_compiler_rev=00000000,data=" DEV_NULL,
2357                  &data);
2358    free_test_data(&data);
2359}
2360
2361static void test_acpi_q35_applesmc(void)
2362{
2363    test_data data = {
2364        .machine = MACHINE_Q35,
2365        .arch    = "x86",
2366        .variant = ".applesmc",
2367    };
2368
2369    /* supply fake 64-byte OSK to silence missing key warning */
2370    test_acpi_one("-device isa-applesmc,osk=any64characterfakeoskisenough"
2371                  "topreventinvalidkeywarningsonstderr", &data);
2372    free_test_data(&data);
2373}
2374
2375static void test_acpi_q35_pvpanic_isa(void)
2376{
2377    test_data data = {
2378        .machine = MACHINE_Q35,
2379        .arch    = "x86",
2380        .variant = ".pvpanic-isa",
2381    };
2382
2383    test_acpi_one("-device pvpanic", &data);
2384    free_test_data(&data);
2385}
2386
2387static void test_acpi_pc_smbios_options(void)
2388{
2389    uint8_t req_type11[] = { 11 };
2390    test_data data = {
2391        .machine = MACHINE_PC,
2392        .arch    = "x86",
2393        .variant = ".pc_smbios_options",
2394        .required_struct_types = req_type11,
2395        .required_struct_types_len = ARRAY_SIZE(req_type11),
2396    };
2397
2398    test_smbios("-smbios type=11,value=TEST", &data);
2399    free_test_data(&data);
2400}
2401
2402static void test_acpi_pc_smbios_blob(void)
2403{
2404    uint8_t req_type11[] = { 11 };
2405    test_data data = {
2406        .machine = MACHINE_PC,
2407        .arch    = "x86",
2408        .variant = ".pc_smbios_blob",
2409        .required_struct_types = req_type11,
2410        .required_struct_types_len = ARRAY_SIZE(req_type11),
2411    };
2412
2413    test_smbios("-machine smbios-entry-point-type=32 "
2414                "-smbios file=tests/data/smbios/type11_blob", &data);
2415    free_test_data(&data);
2416}
2417
2418static void test_acpi_isapc_smbios_legacy(void)
2419{
2420    uint8_t req_type11[] = { 1, 11 };
2421    test_data data = {
2422        .machine = "isapc",
2423        .variant = ".pc_smbios_legacy",
2424        .required_struct_types = req_type11,
2425        .required_struct_types_len = ARRAY_SIZE(req_type11),
2426    };
2427
2428    test_smbios("-smbios file=tests/data/smbios/type11_blob.legacy "
2429                "-smbios type=1,family=TEST", &data);
2430    free_test_data(&data);
2431}
2432
2433static void test_oem_fields(test_data *data)
2434{
2435    int i;
2436
2437    for (i = 0; i < data->tables->len; ++i) {
2438        AcpiSdtTable *sdt;
2439
2440        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
2441        /* FACS doesn't have OEMID and OEMTABLEID fields */
2442        if (compare_signature(sdt, "FACS")) {
2443            continue;
2444        }
2445
2446        g_assert(strncmp((char *)sdt->aml + 10, OEM_ID, 6) == 0);
2447        g_assert(strncmp((char *)sdt->aml + 16, OEM_TABLE_ID, 8) == 0);
2448    }
2449}
2450
2451static void test_acpi_piix4_oem_fields(void)
2452{
2453    char *args;
2454    test_data data = {};
2455
2456    data.machine = MACHINE_PC;
2457    data.arch    = "x86";
2458    data.required_struct_types = base_required_struct_types;
2459    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
2460
2461    args = test_acpi_create_args(&data, OEM_TEST_ARGS);
2462    data.qts = qtest_init(args);
2463    test_acpi_load_tables(&data);
2464    test_oem_fields(&data);
2465    qtest_quit(data.qts);
2466    free_test_data(&data);
2467    g_free(args);
2468}
2469
2470static void test_acpi_q35_oem_fields(void)
2471{
2472    char *args;
2473    test_data data = {};
2474
2475    data.machine = MACHINE_Q35;
2476    data.arch    = "x86";
2477    data.required_struct_types = base_required_struct_types;
2478    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
2479
2480    args = test_acpi_create_args(&data, OEM_TEST_ARGS);
2481    data.qts = qtest_init(args);
2482    test_acpi_load_tables(&data);
2483    test_oem_fields(&data);
2484    qtest_quit(data.qts);
2485    free_test_data(&data);
2486    g_free(args);
2487}
2488
2489static void test_acpi_microvm_oem_fields(void)
2490{
2491    test_data data = {};
2492    char *args;
2493
2494    test_acpi_microvm_prepare(&data);
2495
2496    args = test_acpi_create_args(&data,
2497                                 OEM_TEST_ARGS",acpi=on");
2498    data.qts = qtest_init(args);
2499    test_acpi_load_tables(&data);
2500    test_oem_fields(&data);
2501    qtest_quit(data.qts);
2502    free_test_data(&data);
2503    g_free(args);
2504}
2505
2506static void test_acpi_aarch64_virt_oem_fields(void)
2507{
2508    test_data data = {
2509        .machine = "virt",
2510        .arch = "aarch64",
2511        .tcg_only = true,
2512        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
2513        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
2514        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
2515        .ram_start = 0x40000000ULL,
2516        .scan_len = 128ULL * MiB,
2517    };
2518    char *args;
2519
2520    args = test_acpi_create_args(&data, "-cpu cortex-a57 "OEM_TEST_ARGS);
2521    data.qts = qtest_init(args);
2522    test_acpi_load_tables(&data);
2523    test_oem_fields(&data);
2524    qtest_quit(data.qts);
2525    free_test_data(&data);
2526    g_free(args);
2527}
2528
2529#define LOONGARCH64_INIT_TEST_DATA(data)                          \
2530    test_data data = {                                            \
2531        .machine = "virt",                                        \
2532        .arch    = "loongarch64",                                 \
2533        .tcg_only = true,                                         \
2534        .uefi_fl1 = "pc-bios/edk2-loongarch64-code.fd",           \
2535        .uefi_fl2 = "pc-bios/edk2-loongarch64-vars.fd",           \
2536        .cd = "tests/data/uefi-boot-images/"                      \
2537              "bios-tables-test.loongarch64.iso.qcow2",           \
2538        .ram_start = 0,                                           \
2539        .scan_len = 128ULL * MiB,                                 \
2540    }
2541
2542static void test_acpi_loongarch64_virt(void)
2543{
2544    LOONGARCH64_INIT_TEST_DATA(data);
2545
2546    test_acpi_one("-cpu la464 ", &data);
2547    free_test_data(&data);
2548}
2549
2550static void test_acpi_loongarch64_virt_topology(void)
2551{
2552    LOONGARCH64_INIT_TEST_DATA(data);
2553
2554    data.variant = ".topology";
2555    test_acpi_one("-cpu la464 -smp sockets=1,cores=2,threads=2", &data);
2556    free_test_data(&data);
2557}
2558
2559static void test_acpi_loongarch64_virt_numamem(void)
2560{
2561    LOONGARCH64_INIT_TEST_DATA(data);
2562
2563    data.variant = ".numamem";
2564    test_acpi_one(" -cpu la464 -m 128"
2565                  " -object memory-backend-ram,id=ram0,size=64M"
2566                  " -object memory-backend-ram,id=ram1,size=64M"
2567                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
2568                  " -numa dist,src=0,dst=1,val=21",
2569                  &data);
2570    free_test_data(&data);
2571}
2572
2573static void test_acpi_loongarch64_virt_memhp(void)
2574{
2575    LOONGARCH64_INIT_TEST_DATA(data);
2576
2577    data.variant = ".memhp";
2578    test_acpi_one(" -cpu la464 -m 128,slots=2,maxmem=256M"
2579                  " -object memory-backend-ram,id=ram0,size=128M",
2580                  &data);
2581    free_test_data(&data);
2582}
2583
2584static void test_acpi_loongarch64_virt_oem_fields(void)
2585{
2586    LOONGARCH64_INIT_TEST_DATA(data);
2587    char *args;
2588
2589    args = test_acpi_create_args(&data, "-cpu la464 "OEM_TEST_ARGS);
2590    data.qts = qtest_init(args);
2591    test_acpi_load_tables(&data);
2592    test_oem_fields(&data);
2593    qtest_quit(data.qts);
2594    free_test_data(&data);
2595    g_free(args);
2596}
2597
2598int main(int argc, char *argv[])
2599{
2600    const char *arch = qtest_get_arch();
2601    bool has_kvm, has_tcg;
2602    char *v_env = getenv("V");
2603    int ret;
2604
2605    if (v_env) {
2606        verbosity_level = atoi(v_env);
2607    }
2608
2609    g_test_init(&argc, &argv, NULL);
2610
2611    has_kvm = qtest_has_accel("kvm");
2612    has_tcg = qtest_has_accel("tcg");
2613
2614    if (!has_tcg && !has_kvm) {
2615        g_test_skip("No KVM or TCG accelerator available");
2616        return 0;
2617    }
2618
2619    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
2620        ret = boot_sector_init(disk);
2621        if (ret) {
2622            return ret;
2623        }
2624        if (qtest_has_machine(MACHINE_PC)) {
2625            qtest_add_func("acpi/piix4", test_acpi_piix4_tcg);
2626            qtest_add_func("acpi/piix4/oem-fields", test_acpi_piix4_oem_fields);
2627            qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge);
2628            qtest_add_func("acpi/piix4/pci-hotplug/no_root_hotplug",
2629                           test_acpi_piix4_no_root_hotplug);
2630            qtest_add_func("acpi/piix4/pci-hotplug/no_bridge_hotplug",
2631                           test_acpi_piix4_no_bridge_hotplug);
2632            qtest_add_func("acpi/piix4/pci-hotplug/off",
2633                           test_acpi_piix4_no_acpi_pci_hotplug);
2634            qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi);
2635            qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp);
2636            qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
2637            qtest_add_func("acpi/piix4/nosmm", test_acpi_piix4_tcg_nosmm);
2638            qtest_add_func("acpi/piix4/smm-compat",
2639                           test_acpi_piix4_tcg_smm_compat);
2640            qtest_add_func("acpi/piix4/smm-compat-nosmm",
2641                           test_acpi_piix4_tcg_smm_compat_nosmm);
2642            qtest_add_func("acpi/piix4/nohpet", test_acpi_piix4_tcg_nohpet);
2643
2644            /* i386 does not support memory hotplug */
2645            if (strcmp(arch, "i386")) {
2646                qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
2647                qtest_add_func("acpi/piix4/dimmpxm",
2648                               test_acpi_piix4_tcg_dimm_pxm);
2649                qtest_add_func("acpi/piix4/acpihmat",
2650                               test_acpi_piix4_tcg_acpi_hmat);
2651            }
2652#ifdef CONFIG_POSIX
2653            qtest_add_func("acpi/piix4/acpierst", test_acpi_piix4_acpi_erst);
2654#endif
2655            qtest_add_func("acpi/piix4/smbios-options",
2656                           test_acpi_pc_smbios_options);
2657            qtest_add_func("acpi/piix4/smbios-blob",
2658                           test_acpi_pc_smbios_blob);
2659            qtest_add_func("acpi/piix4/smbios-legacy",
2660                           test_acpi_isapc_smbios_legacy);
2661        }
2662        if (qtest_has_machine(MACHINE_Q35)) {
2663            qtest_add_func("acpi/q35", test_acpi_q35_tcg);
2664            qtest_add_func("acpi/q35/oem-fields", test_acpi_q35_oem_fields);
2665            if (tpm_model_is_available("-machine q35", "tpm-tis")) {
2666                qtest_add_func("acpi/q35/tpm2-tis", test_acpi_q35_tcg_tpm2_tis);
2667                qtest_add_func("acpi/q35/tpm12-tis",
2668                               test_acpi_q35_tcg_tpm12_tis);
2669            }
2670            qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
2671            qtest_add_func("acpi/q35/no-acpi-hotplug",
2672                           test_acpi_q35_tcg_no_acpi_hotplug);
2673            qtest_add_func("acpi/q35/multif-bridge",
2674                           test_acpi_q35_multif_bridge);
2675            qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi);
2676            qtest_add_func("acpi/q35/smbus/ipmi", test_acpi_q35_tcg_smbus_ipmi);
2677            qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp);
2678            qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
2679            qtest_add_func("acpi/q35/nosmm", test_acpi_q35_tcg_nosmm);
2680            qtest_add_func("acpi/q35/smm-compat",
2681                           test_acpi_q35_tcg_smm_compat);
2682            qtest_add_func("acpi/q35/smm-compat-nosmm",
2683                           test_acpi_q35_tcg_smm_compat_nosmm);
2684            qtest_add_func("acpi/q35/nohpet", test_acpi_q35_tcg_nohpet);
2685            qtest_add_func("acpi/q35/acpihmat-noinitiator",
2686                           test_acpi_q35_tcg_acpi_hmat_noinitiator);
2687            qtest_add_func("acpi/q35/acpihmat-genericx",
2688                           test_acpi_q35_tcg_acpi_hmat_generic_x);
2689
2690            /* i386 does not support memory hotplug */
2691            if (strcmp(arch, "i386")) {
2692                qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
2693                qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm);
2694                qtest_add_func("acpi/q35/acpihmat",
2695                               test_acpi_q35_tcg_acpi_hmat);
2696                qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64);
2697            }
2698#ifdef CONFIG_POSIX
2699            qtest_add_func("acpi/q35/acpierst", test_acpi_q35_acpi_erst);
2700#endif
2701            qtest_add_func("acpi/q35/applesmc", test_acpi_q35_applesmc);
2702            qtest_add_func("acpi/q35/pvpanic-isa", test_acpi_q35_pvpanic_isa);
2703            if (has_tcg) {
2704                qtest_add_func("acpi/q35/ivrs", test_acpi_q35_tcg_ivrs);
2705            }
2706            if (has_kvm) {
2707                qtest_add_func("acpi/q35/kvm/xapic", test_acpi_q35_kvm_xapic);
2708                qtest_add_func("acpi/q35/kvm/dmar", test_acpi_q35_kvm_dmar);
2709                qtest_add_func("acpi/q35/type4-count",
2710                               test_acpi_q35_kvm_type4_count);
2711                qtest_add_func("acpi/q35/core-count",
2712                               test_acpi_q35_kvm_core_count);
2713                qtest_add_func("acpi/q35/core-count2",
2714                               test_acpi_q35_kvm_core_count2);
2715                qtest_add_func("acpi/q35/thread-count",
2716                               test_acpi_q35_kvm_thread_count);
2717                qtest_add_func("acpi/q35/thread-count2",
2718                               test_acpi_q35_kvm_thread_count2);
2719            }
2720            if (qtest_has_device("virtio-iommu-pci")) {
2721                qtest_add_func("acpi/q35/viot", test_acpi_q35_viot);
2722            }
2723#ifdef CONFIG_POSIX
2724            qtest_add_func("acpi/q35/cxl", test_acpi_q35_cxl);
2725#endif
2726            qtest_add_func("acpi/q35/slic", test_acpi_q35_slic);
2727        }
2728        if (qtest_has_machine("microvm")) {
2729            qtest_add_func("acpi/microvm", test_acpi_microvm_tcg);
2730            qtest_add_func("acpi/microvm/usb", test_acpi_microvm_usb_tcg);
2731            qtest_add_func("acpi/microvm/rtc", test_acpi_microvm_rtc_tcg);
2732            qtest_add_func("acpi/microvm/ioapic2",
2733                           test_acpi_microvm_ioapic2_tcg);
2734            qtest_add_func("acpi/microvm/oem-fields",
2735                           test_acpi_microvm_oem_fields);
2736            if (has_tcg) {
2737                if (strcmp(arch, "x86_64") == 0) {
2738                    qtest_add_func("acpi/microvm/pcie",
2739                                   test_acpi_microvm_pcie_tcg);
2740#ifdef CONFIG_POSIX
2741                    qtest_add_func("acpi/microvm/acpierst",
2742                                   test_acpi_microvm_acpi_erst);
2743#endif
2744                }
2745            }
2746        }
2747    } else if (strcmp(arch, "aarch64") == 0) {
2748        if (has_tcg && qtest_has_device("virtio-blk-pci")) {
2749            qtest_add_func("acpi/virt", test_acpi_aarch64_virt_tcg);
2750            qtest_add_func("acpi/virt/acpihmatvirt",
2751                           test_acpi_aarch64_virt_tcg_acpi_hmat);
2752            qtest_add_func("acpi/virt/topology",
2753                           test_acpi_aarch64_virt_tcg_topology);
2754            qtest_add_func("acpi/virt/its_off",
2755                           test_acpi_aarch64_virt_tcg_its_off);
2756            qtest_add_func("acpi/virt/numamem",
2757                           test_acpi_aarch64_virt_tcg_numamem);
2758            qtest_add_func("acpi/virt/memhp", test_acpi_aarch64_virt_tcg_memhp);
2759            qtest_add_func("acpi/virt/acpipcihp",
2760                           test_acpi_aarch64_virt_acpi_pci_hotplug);
2761            qtest_add_func("acpi/virt/hpoffacpiindex",
2762                          test_acpi_aarch64_virt_pcie_root_port_hpoff);
2763            qtest_add_func("acpi/virt/pxb", test_acpi_aarch64_virt_tcg_pxb);
2764            qtest_add_func("acpi/virt/oem-fields",
2765                           test_acpi_aarch64_virt_oem_fields);
2766            qtest_add_func("acpi/virt/acpispcr",
2767                           test_acpi_aarch64_virt_tcg_acpi_spcr);
2768            if (qtest_has_device("virtio-iommu-pci")) {
2769                qtest_add_func("acpi/virt/viot", test_acpi_aarch64_virt_viot);
2770            }
2771        }
2772    } else if (strcmp(arch, "riscv64") == 0) {
2773        if (has_tcg && qtest_has_device("virtio-blk-pci")) {
2774            qtest_add_func("acpi/virt", test_acpi_riscv64_virt_tcg);
2775            qtest_add_func("acpi/virt/numamem",
2776                           test_acpi_riscv64_virt_tcg_numamem);
2777            qtest_add_func("acpi/virt/acpispcr",
2778                           test_acpi_riscv64_virt_tcg_acpi_spcr);
2779        }
2780    } else if (strcmp(arch, "loongarch64") == 0) {
2781        if (has_tcg) {
2782            qtest_add_func("acpi/virt", test_acpi_loongarch64_virt);
2783            qtest_add_func("acpi/virt/topology",
2784                           test_acpi_loongarch64_virt_topology);
2785            qtest_add_func("acpi/virt/numamem",
2786                           test_acpi_loongarch64_virt_numamem);
2787            qtest_add_func("acpi/virt/memhp", test_acpi_loongarch64_virt_memhp);
2788            qtest_add_func("acpi/virt/oem-fields",
2789                           test_acpi_loongarch64_virt_oem_fields);
2790        }
2791    }
2792    ret = g_test_run();
2793    boot_sector_cleanup(disk);
2794    return ret;
2795}
2796