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=1
  28 * this will produce a bunch of warnings about differences
  29 * beween 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 "qemu-common.h"
  61#include "hw/firmware/smbios.h"
  62#include "qemu/bitmap.h"
  63#include "acpi-utils.h"
  64#include "boot-sector.h"
  65#include "tpm-emu.h"
  66#include "hw/acpi/tpm.h"
  67#include "qemu/cutils.h"
  68
  69#define MACHINE_PC "pc"
  70#define MACHINE_Q35 "q35"
  71
  72#define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"
  73
  74#define OEM_ID             "TEST"
  75#define OEM_TABLE_ID       "OEM"
  76#define OEM_TEST_ARGS      "-machine x-oem-id="OEM_ID",x-oem-table-id="OEM_TABLE_ID
  77
  78typedef struct {
  79    bool tcg_only;
  80    const char *machine;
  81    const char *variant;
  82    const char *uefi_fl1;
  83    const char *uefi_fl2;
  84    const char *blkdev;
  85    const char *cd;
  86    const uint64_t ram_start;
  87    const uint64_t scan_len;
  88    uint64_t rsdp_addr;
  89    uint8_t rsdp_table[36 /* ACPI 2.0+ RSDP size */];
  90    GArray *tables;
  91    uint32_t smbios_ep_addr;
  92    struct smbios_21_entry_point smbios_ep_table;
  93    uint16_t smbios_cpu_max_speed;
  94    uint16_t smbios_cpu_curr_speed;
  95    uint8_t *required_struct_types;
  96    int required_struct_types_len;
  97    QTestState *qts;
  98} test_data;
  99
 100static char disk[] = "tests/acpi-test-disk-XXXXXX";
 101static const char *data_dir = "tests/data/acpi";
 102#ifdef CONFIG_IASL
 103static const char *iasl = CONFIG_IASL;
 104#else
 105static const char *iasl;
 106#endif
 107
 108static bool compare_signature(const AcpiSdtTable *sdt, const char *signature)
 109{
 110   return !memcmp(sdt->aml, signature, 4);
 111}
 112
 113static void cleanup_table_descriptor(AcpiSdtTable *table)
 114{
 115    g_free(table->aml);
 116    if (table->aml_file &&
 117        !table->tmp_files_retain &&
 118        g_strstr_len(table->aml_file, -1, "aml-")) {
 119        unlink(table->aml_file);
 120    }
 121    g_free(table->aml_file);
 122    g_free(table->asl);
 123    if (table->asl_file &&
 124        !table->tmp_files_retain) {
 125        unlink(table->asl_file);
 126    }
 127    g_free(table->asl_file);
 128}
 129
 130static void free_test_data(test_data *data)
 131{
 132    int i;
 133
 134    if (!data->tables) {
 135        return;
 136    }
 137    for (i = 0; i < data->tables->len; ++i) {
 138        cleanup_table_descriptor(&g_array_index(data->tables, AcpiSdtTable, i));
 139    }
 140
 141    g_array_free(data->tables, true);
 142}
 143
 144static void test_acpi_rsdp_table(test_data *data)
 145{
 146    uint8_t *rsdp_table = data->rsdp_table;
 147
 148    acpi_fetch_rsdp_table(data->qts, data->rsdp_addr, rsdp_table);
 149
 150    switch (rsdp_table[15 /* Revision offset */]) {
 151    case 0: /* ACPI 1.0 RSDP */
 152        /* With rev 1, checksum is only for the first 20 bytes */
 153        g_assert(!acpi_calc_checksum(rsdp_table,  20));
 154        break;
 155    case 2: /* ACPI 2.0+ RSDP */
 156        /* With revision 2, we have 2 checksums */
 157        g_assert(!acpi_calc_checksum(rsdp_table, 20));
 158        g_assert(!acpi_calc_checksum(rsdp_table, 36));
 159        break;
 160    default:
 161        g_assert_not_reached();
 162    }
 163}
 164
 165static void test_acpi_rxsdt_table(test_data *data)
 166{
 167    const char *sig = "RSDT";
 168    AcpiSdtTable rsdt = {};
 169    int entry_size = 4;
 170    int addr_off = 16 /* RsdtAddress */;
 171    uint8_t *ent;
 172
 173    if (data->rsdp_table[15 /* Revision offset */] != 0) {
 174        addr_off = 24 /* XsdtAddress */;
 175        entry_size = 8;
 176        sig = "XSDT";
 177    }
 178    /* read [RX]SDT table */
 179    acpi_fetch_table(data->qts, &rsdt.aml, &rsdt.aml_len,
 180                     &data->rsdp_table[addr_off], entry_size, sig, true);
 181
 182    /* Load all tables and add to test list directly RSDT referenced tables */
 183    ACPI_FOREACH_RSDT_ENTRY(rsdt.aml, rsdt.aml_len, ent, entry_size) {
 184        AcpiSdtTable ssdt_table = {};
 185
 186        acpi_fetch_table(data->qts, &ssdt_table.aml, &ssdt_table.aml_len, ent,
 187                         entry_size, NULL, true);
 188        /* Add table to ASL test tables list */
 189        g_array_append_val(data->tables, ssdt_table);
 190    }
 191    cleanup_table_descriptor(&rsdt);
 192}
 193
 194static void test_acpi_fadt_table(test_data *data)
 195{
 196    /* FADT table is 1st */
 197    AcpiSdtTable table = g_array_index(data->tables, typeof(table), 0);
 198    uint8_t *fadt_aml = table.aml;
 199    uint32_t fadt_len = table.aml_len;
 200    uint32_t val;
 201    int dsdt_offset = 40 /* DSDT */;
 202    int dsdt_entry_size = 4;
 203
 204    g_assert(compare_signature(&table, "FACP"));
 205
 206    /* Since DSDT/FACS isn't in RSDT, add them to ASL test list manually */
 207    memcpy(&val, fadt_aml + 112 /* Flags */, 4);
 208    val = le32_to_cpu(val);
 209    if (!(val & 1UL << 20 /* HW_REDUCED_ACPI */)) {
 210        acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
 211                         fadt_aml + 36 /* FIRMWARE_CTRL */, 4, "FACS", false);
 212        g_array_append_val(data->tables, table);
 213    }
 214
 215    memcpy(&val, fadt_aml + dsdt_offset, 4);
 216    val = le32_to_cpu(val);
 217    if (!val) {
 218        dsdt_offset = 140 /* X_DSDT */;
 219        dsdt_entry_size = 8;
 220    }
 221    acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
 222                     fadt_aml + dsdt_offset, dsdt_entry_size, "DSDT", true);
 223    g_array_append_val(data->tables, table);
 224
 225    memset(fadt_aml + 36, 0, 4); /* sanitize FIRMWARE_CTRL ptr */
 226    memset(fadt_aml + 40, 0, 4); /* sanitize DSDT ptr */
 227    if (fadt_aml[8 /* FADT Major Version */] >= 3) {
 228        memset(fadt_aml + 132, 0, 8); /* sanitize X_FIRMWARE_CTRL ptr */
 229        memset(fadt_aml + 140, 0, 8); /* sanitize X_DSDT ptr */
 230    }
 231
 232    /* update checksum */
 233    fadt_aml[9 /* Checksum */] = 0;
 234    fadt_aml[9 /* Checksum */] -= acpi_calc_checksum(fadt_aml, fadt_len);
 235}
 236
 237static void dump_aml_files(test_data *data, bool rebuild)
 238{
 239    AcpiSdtTable *sdt;
 240    GError *error = NULL;
 241    gchar *aml_file = NULL;
 242    gint fd;
 243    ssize_t ret;
 244    int i;
 245
 246    for (i = 0; i < data->tables->len; ++i) {
 247        const char *ext = data->variant ? data->variant : "";
 248        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
 249        g_assert(sdt->aml);
 250
 251        if (rebuild) {
 252            aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
 253                                       sdt->aml, ext);
 254            fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
 255                        S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
 256            if (fd < 0) {
 257                perror(aml_file);
 258            }
 259            g_assert(fd >= 0);
 260        } else {
 261            fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
 262            g_assert_no_error(error);
 263        }
 264
 265        ret = qemu_write_full(fd, sdt->aml, sdt->aml_len);
 266        g_assert(ret == sdt->aml_len);
 267
 268        close(fd);
 269
 270        g_free(aml_file);
 271    }
 272}
 273
 274static bool create_tmp_asl(AcpiSdtTable *sdt)
 275{
 276    GError *error = NULL;
 277    gint fd;
 278
 279    fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
 280    g_assert_no_error(error);
 281    close(fd);
 282
 283    return false;
 284}
 285
 286static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
 287{
 288    AcpiSdtTable *temp;
 289    GError *error = NULL;
 290    GString *command_line = g_string_new(iasl);
 291    gchar *out, *out_err;
 292    gboolean ret;
 293    int i;
 294
 295    create_tmp_asl(sdt);
 296
 297    /* build command line */
 298    g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
 299    if (compare_signature(sdt, "DSDT") ||
 300        compare_signature(sdt, "SSDT")) {
 301        for (i = 0; i < sdts->len; ++i) {
 302            temp = &g_array_index(sdts, AcpiSdtTable, i);
 303            if (compare_signature(temp, "DSDT") ||
 304                compare_signature(temp, "SSDT")) {
 305                g_string_append_printf(command_line, "-e %s ", temp->aml_file);
 306            }
 307        }
 308    }
 309    g_string_append_printf(command_line, "-d %s", sdt->aml_file);
 310
 311    /* pass 'out' and 'out_err' in order to be redirected */
 312    ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
 313    g_assert_no_error(error);
 314    if (ret) {
 315        ret = g_file_get_contents(sdt->asl_file, &sdt->asl,
 316                                  &sdt->asl_len, &error);
 317        g_assert(ret);
 318        g_assert_no_error(error);
 319        ret = (sdt->asl_len > 0);
 320    }
 321
 322    g_free(out);
 323    g_free(out_err);
 324    g_string_free(command_line, true);
 325
 326    return !ret;
 327}
 328
 329#define COMMENT_END "*/"
 330#define DEF_BLOCK "DefinitionBlock ("
 331#define BLOCK_NAME_END ","
 332
 333static GString *normalize_asl(gchar *asl_code)
 334{
 335    GString *asl = g_string_new(asl_code);
 336    gchar *comment, *block_name;
 337
 338    /* strip comments (different generation days) */
 339    comment = g_strstr_len(asl->str, asl->len, COMMENT_END);
 340    if (comment) {
 341        comment += strlen(COMMENT_END);
 342        while (*comment == '\n') {
 343            comment++;
 344        }
 345        asl = g_string_erase(asl, 0, comment - asl->str);
 346    }
 347
 348    /* strip def block name (it has file path in it) */
 349    if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
 350        block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
 351        g_assert(block_name);
 352        asl = g_string_erase(asl, 0,
 353                             block_name + sizeof(BLOCK_NAME_END) - asl->str);
 354    }
 355
 356    return asl;
 357}
 358
 359static GArray *load_expected_aml(test_data *data)
 360{
 361    int i;
 362    AcpiSdtTable *sdt;
 363    GError *error = NULL;
 364    gboolean ret;
 365    gsize aml_len;
 366
 367    GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
 368    if (getenv("V")) {
 369        fputc('\n', stderr);
 370    }
 371    for (i = 0; i < data->tables->len; ++i) {
 372        AcpiSdtTable exp_sdt;
 373        gchar *aml_file = NULL;
 374        const char *ext = data->variant ? data->variant : "";
 375
 376        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
 377
 378        memset(&exp_sdt, 0, sizeof(exp_sdt));
 379
 380try_again:
 381        aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
 382                                   sdt->aml, ext);
 383        if (getenv("V")) {
 384            fprintf(stderr, "Looking for expected file '%s'\n", aml_file);
 385        }
 386        if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) {
 387            exp_sdt.aml_file = aml_file;
 388        } else if (*ext != '\0') {
 389            /* try fallback to generic (extension less) expected file */
 390            ext = "";
 391            g_free(aml_file);
 392            goto try_again;
 393        }
 394        g_assert(exp_sdt.aml_file);
 395        if (getenv("V")) {
 396            fprintf(stderr, "Using expected file '%s'\n", aml_file);
 397        }
 398        ret = g_file_get_contents(aml_file, (gchar **)&exp_sdt.aml,
 399                                  &aml_len, &error);
 400        exp_sdt.aml_len = aml_len;
 401        g_assert(ret);
 402        g_assert_no_error(error);
 403        g_assert(exp_sdt.aml);
 404        if (!exp_sdt.aml_len) {
 405            fprintf(stderr, "Warning! zero length expected file '%s'\n",
 406                    aml_file);
 407        }
 408
 409        g_array_append_val(exp_tables, exp_sdt);
 410    }
 411
 412    return exp_tables;
 413}
 414
 415static bool test_acpi_find_diff_allowed(AcpiSdtTable *sdt)
 416{
 417    const gchar *allowed_diff_file[] = {
 418#include "bios-tables-test-allowed-diff.h"
 419        NULL
 420    };
 421    const gchar **f;
 422
 423    for (f = allowed_diff_file; *f; ++f) {
 424        if (!g_strcmp0(sdt->aml_file, *f)) {
 425            return true;
 426        }
 427    }
 428    return false;
 429}
 430
 431/* test the list of tables in @data->tables against reference tables */
 432static void test_acpi_asl(test_data *data)
 433{
 434    int i;
 435    AcpiSdtTable *sdt, *exp_sdt;
 436    test_data exp_data;
 437    gboolean exp_err, err, all_tables_match = true;
 438
 439    memset(&exp_data, 0, sizeof(exp_data));
 440    exp_data.tables = load_expected_aml(data);
 441    dump_aml_files(data, false);
 442    for (i = 0; i < data->tables->len; ++i) {
 443        GString *asl, *exp_asl;
 444
 445        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
 446        exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
 447
 448        if (sdt->aml_len == exp_sdt->aml_len &&
 449            !memcmp(sdt->aml, exp_sdt->aml, sdt->aml_len)) {
 450            /* Identical table binaries: no need to disassemble. */
 451            continue;
 452        }
 453
 454        fprintf(stderr,
 455                "acpi-test: Warning! %.4s binary file mismatch. "
 456                "Actual [aml:%s], Expected [aml:%s].\n"
 457                "See source file tests/qtest/bios-tables-test.c "
 458                "for instructions on how to update expected files.\n",
 459                exp_sdt->aml, sdt->aml_file, exp_sdt->aml_file);
 460
 461        all_tables_match = all_tables_match &&
 462            test_acpi_find_diff_allowed(exp_sdt);
 463
 464        /*
 465         *  don't try to decompile if IASL isn't present, in this case user
 466         * will just 'get binary file mismatch' warnings and test failure
 467         */
 468        if (!iasl) {
 469            continue;
 470        }
 471
 472        err = load_asl(data->tables, sdt);
 473        asl = normalize_asl(sdt->asl);
 474
 475        /*
 476         * If expected file is empty - it's likely that it was a stub just
 477         * created for step 1 above: we do want to decompile the actual one.
 478         */
 479        if (exp_sdt->aml_len) {
 480            exp_err = load_asl(exp_data.tables, exp_sdt);
 481            exp_asl = normalize_asl(exp_sdt->asl);
 482        } else {
 483            exp_err = create_tmp_asl(exp_sdt);
 484            exp_asl = g_string_new("");
 485        }
 486
 487        /* TODO: check for warnings */
 488        g_assert(!err || exp_err || !exp_sdt->aml_len);
 489
 490        if (g_strcmp0(asl->str, exp_asl->str)) {
 491            sdt->tmp_files_retain = true;
 492            if (exp_err) {
 493                fprintf(stderr,
 494                        "Warning! iasl couldn't parse the expected aml\n");
 495            } else {
 496                exp_sdt->tmp_files_retain = true;
 497                fprintf(stderr,
 498                        "acpi-test: Warning! %.4s mismatch. "
 499                        "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
 500                        exp_sdt->aml, sdt->asl_file, sdt->aml_file,
 501                        exp_sdt->asl_file, exp_sdt->aml_file);
 502                fflush(stderr);
 503                if (getenv("V")) {
 504                    const char *diff_env = getenv("DIFF");
 505                    const char *diff_cmd = diff_env ? diff_env : "diff -U 16";
 506                    char *diff = g_strdup_printf("%s %s %s", diff_cmd,
 507                                                 exp_sdt->asl_file, sdt->asl_file);
 508                    int out = dup(STDOUT_FILENO);
 509                    int ret G_GNUC_UNUSED;
 510                    int dupret;
 511
 512                    g_assert(out >= 0);
 513                    dupret = dup2(STDERR_FILENO, STDOUT_FILENO);
 514                    g_assert(dupret >= 0);
 515                    ret = system(diff) ;
 516                    dupret = dup2(out, STDOUT_FILENO);
 517                    g_assert(dupret >= 0);
 518                    close(out);
 519                    g_free(diff);
 520                }
 521            }
 522        }
 523        g_string_free(asl, true);
 524        g_string_free(exp_asl, true);
 525    }
 526    if (!iasl && !all_tables_match) {
 527        fprintf(stderr, "to see ASL diff between mismatched files install IASL,"
 528                " rebuild QEMU from scratch and re-run tests with V=1"
 529                " environment variable set");
 530    }
 531    g_assert(all_tables_match);
 532
 533    free_test_data(&exp_data);
 534}
 535
 536static bool smbios_ep_table_ok(test_data *data)
 537{
 538    struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
 539    uint32_t addr = data->smbios_ep_addr;
 540
 541    qtest_memread(data->qts, addr, ep_table, sizeof(*ep_table));
 542    if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
 543        return false;
 544    }
 545    if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
 546        return false;
 547    }
 548    if (ep_table->structure_table_length == 0) {
 549        return false;
 550    }
 551    if (ep_table->number_of_structures == 0) {
 552        return false;
 553    }
 554    if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
 555        acpi_calc_checksum((uint8_t *)ep_table + 0x10,
 556                           sizeof *ep_table - 0x10)) {
 557        return false;
 558    }
 559    return true;
 560}
 561
 562static void test_smbios_entry_point(test_data *data)
 563{
 564    uint32_t off;
 565
 566    /* find smbios entry point structure */
 567    for (off = 0xf0000; off < 0x100000; off += 0x10) {
 568        uint8_t sig[] = "_SM_";
 569        int i;
 570
 571        for (i = 0; i < sizeof sig - 1; ++i) {
 572            sig[i] = qtest_readb(data->qts, off + i);
 573        }
 574
 575        if (!memcmp(sig, "_SM_", sizeof sig)) {
 576            /* signature match, but is this a valid entry point? */
 577            data->smbios_ep_addr = off;
 578            if (smbios_ep_table_ok(data)) {
 579                break;
 580            }
 581        }
 582    }
 583
 584    g_assert_cmphex(off, <, 0x100000);
 585}
 586
 587static inline bool smbios_single_instance(uint8_t type)
 588{
 589    switch (type) {
 590    case 0:
 591    case 1:
 592    case 2:
 593    case 3:
 594    case 16:
 595    case 32:
 596    case 127:
 597        return true;
 598    default:
 599        return false;
 600    }
 601}
 602
 603static bool smbios_cpu_test(test_data *data, uint32_t addr)
 604{
 605    uint16_t expect_speed[2];
 606    uint16_t real;
 607    int offset[2];
 608    int i;
 609
 610    /* Check CPU speed for backward compatibility */
 611    offset[0] = offsetof(struct smbios_type_4, max_speed);
 612    offset[1] = offsetof(struct smbios_type_4, current_speed);
 613    expect_speed[0] = data->smbios_cpu_max_speed ? : 2000;
 614    expect_speed[1] = data->smbios_cpu_curr_speed ? : 2000;
 615
 616    for (i = 0; i < 2; i++) {
 617        real = qtest_readw(data->qts, addr + offset[i]);
 618        if (real != expect_speed[i]) {
 619            fprintf(stderr, "Unexpected SMBIOS CPU speed: real %u expect %u\n",
 620                    real, expect_speed[i]);
 621            return false;
 622        }
 623    }
 624
 625    return true;
 626}
 627
 628static void test_smbios_structs(test_data *data)
 629{
 630    DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };
 631    struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
 632    uint32_t addr = le32_to_cpu(ep_table->structure_table_address);
 633    int i, len, max_len = 0;
 634    uint8_t type, prv, crt;
 635
 636    /* walk the smbios tables */
 637    for (i = 0; i < le16_to_cpu(ep_table->number_of_structures); i++) {
 638
 639        /* grab type and formatted area length from struct header */
 640        type = qtest_readb(data->qts, addr);
 641        g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
 642        len = qtest_readb(data->qts, addr + 1);
 643
 644        /* single-instance structs must not have been encountered before */
 645        if (smbios_single_instance(type)) {
 646            g_assert(!test_bit(type, struct_bitmap));
 647        }
 648        set_bit(type, struct_bitmap);
 649
 650        if (type == 4) {
 651            g_assert(smbios_cpu_test(data, addr));
 652        }
 653
 654        /* seek to end of unformatted string area of this struct ("\0\0") */
 655        prv = crt = 1;
 656        while (prv || crt) {
 657            prv = crt;
 658            crt = qtest_readb(data->qts, addr + len);
 659            len++;
 660        }
 661
 662        /* keep track of max. struct size */
 663        if (max_len < len) {
 664            max_len = len;
 665            g_assert_cmpuint(max_len, <=, ep_table->max_structure_size);
 666        }
 667
 668        /* start of next structure */
 669        addr += len;
 670    }
 671
 672    /* total table length and max struct size must match entry point values */
 673    g_assert_cmpuint(le16_to_cpu(ep_table->structure_table_length), ==,
 674                     addr - le32_to_cpu(ep_table->structure_table_address));
 675    g_assert_cmpuint(le16_to_cpu(ep_table->max_structure_size), ==, max_len);
 676
 677    /* required struct types must all be present */
 678    for (i = 0; i < data->required_struct_types_len; i++) {
 679        g_assert(test_bit(data->required_struct_types[i], struct_bitmap));
 680    }
 681}
 682
 683static void test_acpi_load_tables(test_data *data, bool use_uefi)
 684{
 685    if (use_uefi) {
 686        g_assert(data->scan_len);
 687        data->rsdp_addr = acpi_find_rsdp_address_uefi(data->qts,
 688            data->ram_start, data->scan_len);
 689    } else {
 690        boot_sector_test(data->qts);
 691        data->rsdp_addr = acpi_find_rsdp_address(data->qts);
 692        g_assert_cmphex(data->rsdp_addr, <, 0x100000);
 693    }
 694
 695    data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
 696    test_acpi_rsdp_table(data);
 697    test_acpi_rxsdt_table(data);
 698    test_acpi_fadt_table(data);
 699}
 700
 701static char *test_acpi_create_args(test_data *data, const char *params,
 702                                   bool use_uefi)
 703{
 704    char *args;
 705
 706    if (use_uefi) {
 707        /*
 708         * TODO: convert '-drive if=pflash' to new syntax (see e33763be7cd3)
 709         * when arm/virt boad starts to support it.
 710         */
 711        if (data->cd) {
 712            args = g_strdup_printf("-machine %s %s -accel tcg "
 713                "-nodefaults -nographic "
 714                "-drive if=pflash,format=raw,file=%s,readonly=on "
 715                "-drive if=pflash,format=raw,file=%s,snapshot=on -cdrom %s %s",
 716                data->machine, data->tcg_only ? "" : "-accel kvm",
 717                data->uefi_fl1, data->uefi_fl2, data->cd, params ? params : "");
 718        } else {
 719            args = g_strdup_printf("-machine %s %s -accel tcg "
 720                "-nodefaults -nographic "
 721                "-drive if=pflash,format=raw,file=%s,readonly=on "
 722                "-drive if=pflash,format=raw,file=%s,snapshot=on %s",
 723                data->machine, data->tcg_only ? "" : "-accel kvm",
 724                data->uefi_fl1, data->uefi_fl2, params ? params : "");
 725        }
 726    } else {
 727        args = g_strdup_printf("-machine %s %s -accel tcg "
 728            "-net none -display none %s "
 729            "-drive id=hd0,if=none,file=%s,format=raw "
 730            "-device %s,drive=hd0 ",
 731             data->machine, data->tcg_only ? "" : "-accel kvm",
 732             params ? params : "", disk,
 733             data->blkdev ?: "ide-hd");
 734    }
 735    return args;
 736}
 737
 738static void test_acpi_one(const char *params, test_data *data)
 739{
 740    char *args;
 741    bool use_uefi = data->uefi_fl1 && data->uefi_fl2;
 742
 743    args = test_acpi_create_args(data, params, use_uefi);
 744    data->qts = qtest_init(args);
 745    test_acpi_load_tables(data, use_uefi);
 746
 747    if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
 748        dump_aml_files(data, true);
 749    } else {
 750        test_acpi_asl(data);
 751    }
 752
 753    /*
 754     * TODO: make SMBIOS tests work with UEFI firmware,
 755     * Bug on uefi-test-tools to provide entry point:
 756     * https://bugs.launchpad.net/qemu/+bug/1821884
 757     */
 758    if (!use_uefi) {
 759        test_smbios_entry_point(data);
 760        test_smbios_structs(data);
 761    }
 762
 763    qtest_quit(data->qts);
 764    g_free(args);
 765}
 766
 767static uint8_t base_required_struct_types[] = {
 768    0, 1, 3, 4, 16, 17, 19, 32, 127
 769};
 770
 771static void test_acpi_piix4_tcg(void)
 772{
 773    test_data data;
 774
 775    /* Supplying -machine accel argument overrides the default (qtest).
 776     * This is to make guest actually run.
 777     */
 778    memset(&data, 0, sizeof(data));
 779    data.machine = MACHINE_PC;
 780    data.required_struct_types = base_required_struct_types;
 781    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
 782    test_acpi_one(NULL, &data);
 783    free_test_data(&data);
 784}
 785
 786static void test_acpi_piix4_tcg_bridge(void)
 787{
 788    test_data data;
 789
 790    memset(&data, 0, sizeof(data));
 791    data.machine = MACHINE_PC;
 792    data.variant = ".bridge";
 793    data.required_struct_types = base_required_struct_types;
 794    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
 795    test_acpi_one("-device pci-bridge,chassis_nr=1", &data);
 796    free_test_data(&data);
 797}
 798
 799static void test_acpi_piix4_no_root_hotplug(void)
 800{
 801    test_data data;
 802
 803    memset(&data, 0, sizeof(data));
 804    data.machine = MACHINE_PC;
 805    data.variant = ".roothp";
 806    data.required_struct_types = base_required_struct_types;
 807    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
 808    test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
 809                  "-device pci-bridge,chassis_nr=1", &data);
 810    free_test_data(&data);
 811}
 812
 813static void test_acpi_piix4_no_bridge_hotplug(void)
 814{
 815    test_data data;
 816
 817    memset(&data, 0, sizeof(data));
 818    data.machine = MACHINE_PC;
 819    data.variant = ".hpbridge";
 820    data.required_struct_types = base_required_struct_types;
 821    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
 822    test_acpi_one("-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
 823                  "-device pci-bridge,chassis_nr=1", &data);
 824    free_test_data(&data);
 825}
 826
 827static void test_acpi_piix4_no_acpi_pci_hotplug(void)
 828{
 829    test_data data;
 830
 831    memset(&data, 0, sizeof(data));
 832    data.machine = MACHINE_PC;
 833    data.variant = ".hpbrroot";
 834    data.required_struct_types = base_required_struct_types;
 835    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
 836    test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
 837                  "-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
 838                  "-device pci-bridge,chassis_nr=1", &data);
 839    free_test_data(&data);
 840}
 841
 842static void test_acpi_q35_tcg(void)
 843{
 844    test_data data;
 845
 846    memset(&data, 0, sizeof(data));
 847    data.machine = MACHINE_Q35;
 848    data.required_struct_types = base_required_struct_types;
 849    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
 850    test_acpi_one(NULL, &data);
 851    free_test_data(&data);
 852
 853    data.smbios_cpu_max_speed = 3000;
 854    data.smbios_cpu_curr_speed = 2600;
 855    test_acpi_one("-smbios type=4,max-speed=3000,current-speed=2600", &data);
 856    free_test_data(&data);
 857}
 858
 859static void test_acpi_q35_tcg_bridge(void)
 860{
 861    test_data data;
 862
 863    memset(&data, 0, sizeof(data));
 864    data.machine = MACHINE_Q35;
 865    data.variant = ".bridge";
 866    data.required_struct_types = base_required_struct_types;
 867    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
 868    test_acpi_one("-device pci-bridge,chassis_nr=1",
 869                  &data);
 870    free_test_data(&data);
 871}
 872
 873static void test_acpi_q35_multif_bridge(void)
 874{
 875    test_data data = {
 876        .machine = MACHINE_Q35,
 877        .variant = ".multi-bridge",
 878    };
 879    test_acpi_one("-device pcie-root-port,id=pcie-root-port-0,"
 880                  "multifunction=on,"
 881                  "port=0x0,chassis=1,addr=0x2,bus=pcie.0 "
 882                  "-device pcie-root-port,id=pcie-root-port-1,"
 883                  "port=0x1,chassis=2,addr=0x3.0x1,bus=pcie.0 "
 884                  "-device virtio-balloon,id=balloon0,"
 885                  "bus=pcie.0,addr=0x4.0x2",
 886                  &data);
 887    free_test_data(&data);
 888}
 889
 890static void test_acpi_q35_tcg_mmio64(void)
 891{
 892    test_data data = {
 893        .machine = MACHINE_Q35,
 894        .variant = ".mmio64",
 895        .required_struct_types = base_required_struct_types,
 896        .required_struct_types_len = ARRAY_SIZE(base_required_struct_types)
 897    };
 898
 899    test_acpi_one("-m 128M,slots=1,maxmem=2G "
 900                  "-object memory-backend-ram,id=ram0,size=128M "
 901                  "-numa node,memdev=ram0 "
 902                  "-device pci-testdev,membar=2G",
 903                  &data);
 904    free_test_data(&data);
 905}
 906
 907static void test_acpi_piix4_tcg_cphp(void)
 908{
 909    test_data data;
 910
 911    memset(&data, 0, sizeof(data));
 912    data.machine = MACHINE_PC;
 913    data.variant = ".cphp";
 914    test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
 915                  " -object memory-backend-ram,id=ram0,size=64M"
 916                  " -object memory-backend-ram,id=ram1,size=64M"
 917                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
 918                  " -numa dist,src=0,dst=1,val=21",
 919                  &data);
 920    free_test_data(&data);
 921}
 922
 923static void test_acpi_q35_tcg_cphp(void)
 924{
 925    test_data data;
 926
 927    memset(&data, 0, sizeof(data));
 928    data.machine = MACHINE_Q35;
 929    data.variant = ".cphp";
 930    test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
 931                  " -object memory-backend-ram,id=ram0,size=64M"
 932                  " -object memory-backend-ram,id=ram1,size=64M"
 933                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
 934                  " -numa dist,src=0,dst=1,val=21",
 935                  &data);
 936    free_test_data(&data);
 937}
 938
 939static uint8_t ipmi_required_struct_types[] = {
 940    0, 1, 3, 4, 16, 17, 19, 32, 38, 127
 941};
 942
 943static void test_acpi_q35_tcg_ipmi(void)
 944{
 945    test_data data;
 946
 947    memset(&data, 0, sizeof(data));
 948    data.machine = MACHINE_Q35;
 949    data.variant = ".ipmibt";
 950    data.required_struct_types = ipmi_required_struct_types;
 951    data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
 952    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
 953                  " -device isa-ipmi-bt,bmc=bmc0",
 954                  &data);
 955    free_test_data(&data);
 956}
 957
 958static void test_acpi_piix4_tcg_ipmi(void)
 959{
 960    test_data data;
 961
 962    /* Supplying -machine accel argument overrides the default (qtest).
 963     * This is to make guest actually run.
 964     */
 965    memset(&data, 0, sizeof(data));
 966    data.machine = MACHINE_PC;
 967    data.variant = ".ipmikcs";
 968    data.required_struct_types = ipmi_required_struct_types;
 969    data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
 970    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
 971                  " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
 972                  &data);
 973    free_test_data(&data);
 974}
 975
 976static void test_acpi_q35_tcg_memhp(void)
 977{
 978    test_data data;
 979
 980    memset(&data, 0, sizeof(data));
 981    data.machine = MACHINE_Q35;
 982    data.variant = ".memhp";
 983    test_acpi_one(" -m 128,slots=3,maxmem=1G"
 984                  " -object memory-backend-ram,id=ram0,size=64M"
 985                  " -object memory-backend-ram,id=ram1,size=64M"
 986                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
 987                  " -numa dist,src=0,dst=1,val=21",
 988                  &data);
 989    free_test_data(&data);
 990}
 991
 992static void test_acpi_piix4_tcg_memhp(void)
 993{
 994    test_data data;
 995
 996    memset(&data, 0, sizeof(data));
 997    data.machine = MACHINE_PC;
 998    data.variant = ".memhp";
 999    test_acpi_one(" -m 128,slots=3,maxmem=1G"
1000                  " -object memory-backend-ram,id=ram0,size=64M"
1001                  " -object memory-backend-ram,id=ram1,size=64M"
1002                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
1003                  " -numa dist,src=0,dst=1,val=21",
1004                  &data);
1005    free_test_data(&data);
1006}
1007
1008static void test_acpi_piix4_tcg_nosmm(void)
1009{
1010    test_data data;
1011
1012    memset(&data, 0, sizeof(data));
1013    data.machine = MACHINE_PC;
1014    data.variant = ".nosmm";
1015    test_acpi_one("-machine smm=off", &data);
1016    free_test_data(&data);
1017}
1018
1019static void test_acpi_piix4_tcg_smm_compat(void)
1020{
1021    test_data data;
1022
1023    memset(&data, 0, sizeof(data));
1024    data.machine = MACHINE_PC;
1025    data.variant = ".smm-compat";
1026    test_acpi_one("-global PIIX4_PM.smm-compat=on", &data);
1027    free_test_data(&data);
1028}
1029
1030static void test_acpi_piix4_tcg_smm_compat_nosmm(void)
1031{
1032    test_data data;
1033
1034    memset(&data, 0, sizeof(data));
1035    data.machine = MACHINE_PC;
1036    data.variant = ".smm-compat-nosmm";
1037    test_acpi_one("-global PIIX4_PM.smm-compat=on -machine smm=off", &data);
1038    free_test_data(&data);
1039}
1040
1041static void test_acpi_piix4_tcg_nohpet(void)
1042{
1043    test_data data;
1044
1045    memset(&data, 0, sizeof(data));
1046    data.machine = MACHINE_PC;
1047    data.variant = ".nohpet";
1048    test_acpi_one("-no-hpet", &data);
1049    free_test_data(&data);
1050}
1051
1052static void test_acpi_q35_tcg_numamem(void)
1053{
1054    test_data data;
1055
1056    memset(&data, 0, sizeof(data));
1057    data.machine = MACHINE_Q35;
1058    data.variant = ".numamem";
1059    test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1060                  " -numa node -numa node,memdev=ram0", &data);
1061    free_test_data(&data);
1062}
1063
1064static void test_acpi_q35_kvm_xapic(void)
1065{
1066    test_data data;
1067
1068    memset(&data, 0, sizeof(data));
1069    data.machine = MACHINE_Q35;
1070    data.variant = ".xapic";
1071    test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1072                  " -numa node -numa node,memdev=ram0"
1073                  " -machine kernel-irqchip=on -smp 1,maxcpus=288", &data);
1074    free_test_data(&data);
1075}
1076
1077static void test_acpi_q35_tcg_nosmm(void)
1078{
1079    test_data data;
1080
1081    memset(&data, 0, sizeof(data));
1082    data.machine = MACHINE_Q35;
1083    data.variant = ".nosmm";
1084    test_acpi_one("-machine smm=off", &data);
1085    free_test_data(&data);
1086}
1087
1088static void test_acpi_q35_tcg_smm_compat(void)
1089{
1090    test_data data;
1091
1092    memset(&data, 0, sizeof(data));
1093    data.machine = MACHINE_Q35;
1094    data.variant = ".smm-compat";
1095    test_acpi_one("-global ICH9-LPC.smm-compat=on", &data);
1096    free_test_data(&data);
1097}
1098
1099static void test_acpi_q35_tcg_smm_compat_nosmm(void)
1100{
1101    test_data data;
1102
1103    memset(&data, 0, sizeof(data));
1104    data.machine = MACHINE_Q35;
1105    data.variant = ".smm-compat-nosmm";
1106    test_acpi_one("-global ICH9-LPC.smm-compat=on -machine smm=off", &data);
1107    free_test_data(&data);
1108}
1109
1110static void test_acpi_q35_tcg_nohpet(void)
1111{
1112    test_data data;
1113
1114    memset(&data, 0, sizeof(data));
1115    data.machine = MACHINE_Q35;
1116    data.variant = ".nohpet";
1117    test_acpi_one("-no-hpet", &data);
1118    free_test_data(&data);
1119}
1120
1121static void test_acpi_q35_kvm_dmar(void)
1122{
1123    test_data data;
1124
1125    memset(&data, 0, sizeof(data));
1126    data.machine = MACHINE_Q35;
1127    data.variant = ".dmar";
1128    test_acpi_one("-machine kernel-irqchip=split -accel kvm"
1129                  " -device intel-iommu,intremap=on,device-iotlb=on", &data);
1130    free_test_data(&data);
1131}
1132
1133static void test_acpi_q35_tcg_ivrs(void)
1134{
1135    test_data data;
1136
1137    memset(&data, 0, sizeof(data));
1138    data.machine = MACHINE_Q35;
1139    data.variant = ".ivrs";
1140    data.tcg_only = true,
1141    test_acpi_one(" -device amd-iommu", &data);
1142    free_test_data(&data);
1143}
1144
1145static void test_acpi_piix4_tcg_numamem(void)
1146{
1147    test_data data;
1148
1149    memset(&data, 0, sizeof(data));
1150    data.machine = MACHINE_PC;
1151    data.variant = ".numamem";
1152    test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1153                  " -numa node -numa node,memdev=ram0", &data);
1154    free_test_data(&data);
1155}
1156
1157uint64_t tpm_tis_base_addr;
1158
1159static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
1160                              uint64_t base, enum TPMVersion tpm_version)
1161{
1162    gchar *tmp_dir_name = g_strdup_printf("qemu-test_acpi_%s_tcg_%s.XXXXXX",
1163                                          machine, tpm_if);
1164    char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
1165    TPMTestState test;
1166    test_data data;
1167    GThread *thread;
1168    const char *suffix = tpm_version == TPM_VERSION_2_0 ? "tpm2" : "tpm12";
1169    char *args, *variant = g_strdup_printf(".%s.%s", tpm_if, suffix);
1170
1171    tpm_tis_base_addr = base;
1172
1173    module_call_init(MODULE_INIT_QOM);
1174
1175    test.addr = g_new0(SocketAddress, 1);
1176    test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
1177    test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
1178    g_mutex_init(&test.data_mutex);
1179    g_cond_init(&test.data_cond);
1180    test.data_cond_signal = false;
1181    test.tpm_version = tpm_version;
1182
1183    thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
1184    tpm_emu_test_wait_cond(&test);
1185
1186    memset(&data, 0, sizeof(data));
1187    data.machine = machine;
1188    data.variant = variant;
1189
1190    args = g_strdup_printf(
1191        " -chardev socket,id=chr,path=%s"
1192        " -tpmdev emulator,id=dev,chardev=chr"
1193        " -device tpm-%s,tpmdev=dev",
1194        test.addr->u.q_unix.path, tpm_if);
1195
1196    test_acpi_one(args, &data);
1197
1198    g_thread_join(thread);
1199    g_unlink(test.addr->u.q_unix.path);
1200    qapi_free_SocketAddress(test.addr);
1201    g_rmdir(tmp_path);
1202    g_free(variant);
1203    g_free(tmp_path);
1204    g_free(tmp_dir_name);
1205    g_free(args);
1206    free_test_data(&data);
1207}
1208
1209static void test_acpi_q35_tcg_tpm2_tis(void)
1210{
1211    test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_2_0);
1212}
1213
1214static void test_acpi_q35_tcg_tpm12_tis(void)
1215{
1216    test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_1_2);
1217}
1218
1219static void test_acpi_tcg_dimm_pxm(const char *machine)
1220{
1221    test_data data;
1222
1223    memset(&data, 0, sizeof(data));
1224    data.machine = machine;
1225    data.variant = ".dimmpxm";
1226    test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
1227                  " -smp 4,sockets=4"
1228                  " -m 128M,slots=3,maxmem=1G"
1229                  " -object memory-backend-ram,id=ram0,size=32M"
1230                  " -object memory-backend-ram,id=ram1,size=32M"
1231                  " -object memory-backend-ram,id=ram2,size=32M"
1232                  " -object memory-backend-ram,id=ram3,size=32M"
1233                  " -numa node,memdev=ram0,nodeid=0"
1234                  " -numa node,memdev=ram1,nodeid=1"
1235                  " -numa node,memdev=ram2,nodeid=2"
1236                  " -numa node,memdev=ram3,nodeid=3"
1237                  " -numa cpu,node-id=0,socket-id=0"
1238                  " -numa cpu,node-id=1,socket-id=1"
1239                  " -numa cpu,node-id=2,socket-id=2"
1240                  " -numa cpu,node-id=3,socket-id=3"
1241                  " -object memory-backend-ram,id=ram4,size=128M"
1242                  " -object memory-backend-ram,id=nvm0,size=128M"
1243                  " -device pc-dimm,id=dimm0,memdev=ram4,node=1"
1244                  " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
1245                  &data);
1246    free_test_data(&data);
1247}
1248
1249static void test_acpi_q35_tcg_dimm_pxm(void)
1250{
1251    test_acpi_tcg_dimm_pxm(MACHINE_Q35);
1252}
1253
1254static void test_acpi_piix4_tcg_dimm_pxm(void)
1255{
1256    test_acpi_tcg_dimm_pxm(MACHINE_PC);
1257}
1258
1259static void test_acpi_virt_tcg_memhp(void)
1260{
1261    test_data data = {
1262        .machine = "virt",
1263        .tcg_only = true,
1264        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1265        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1266        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1267        .ram_start = 0x40000000ULL,
1268        .scan_len = 256ULL * 1024 * 1024,
1269    };
1270
1271    data.variant = ".memhp";
1272    test_acpi_one(" -machine nvdimm=on"
1273                  " -cpu cortex-a57"
1274                  " -m 256M,slots=3,maxmem=1G"
1275                  " -object memory-backend-ram,id=ram0,size=128M"
1276                  " -object memory-backend-ram,id=ram1,size=128M"
1277                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
1278                  " -numa dist,src=0,dst=1,val=21"
1279                  " -object memory-backend-ram,id=ram2,size=128M"
1280                  " -object memory-backend-ram,id=nvm0,size=128M"
1281                  " -device pc-dimm,id=dimm0,memdev=ram2,node=0"
1282                  " -device nvdimm,id=dimm1,memdev=nvm0,node=1",
1283                  &data);
1284
1285    free_test_data(&data);
1286
1287}
1288
1289static void test_acpi_microvm_prepare(test_data *data)
1290{
1291    memset(data, 0, sizeof(*data));
1292    data->machine = "microvm";
1293    data->required_struct_types = NULL; /* no smbios */
1294    data->required_struct_types_len = 0;
1295    data->blkdev = "virtio-blk-device";
1296}
1297
1298static void test_acpi_microvm_tcg(void)
1299{
1300    test_data data;
1301
1302    test_acpi_microvm_prepare(&data);
1303    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off",
1304                  &data);
1305    free_test_data(&data);
1306}
1307
1308static void test_acpi_microvm_usb_tcg(void)
1309{
1310    test_data data;
1311
1312    test_acpi_microvm_prepare(&data);
1313    data.variant = ".usb";
1314    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,usb=on,rtc=off",
1315                  &data);
1316    free_test_data(&data);
1317}
1318
1319static void test_acpi_microvm_rtc_tcg(void)
1320{
1321    test_data data;
1322
1323    test_acpi_microvm_prepare(&data);
1324    data.variant = ".rtc";
1325    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=on",
1326                  &data);
1327    free_test_data(&data);
1328}
1329
1330static void test_acpi_microvm_pcie_tcg(void)
1331{
1332    test_data data;
1333
1334    test_acpi_microvm_prepare(&data);
1335    data.variant = ".pcie";
1336    data.tcg_only = true; /* need constant host-phys-bits */
1337    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off,pcie=on",
1338                  &data);
1339    free_test_data(&data);
1340}
1341
1342static void test_acpi_microvm_ioapic2_tcg(void)
1343{
1344    test_data data;
1345
1346    test_acpi_microvm_prepare(&data);
1347    data.variant = ".ioapic2";
1348    test_acpi_one(" -machine microvm,acpi=on,ioapic2=on,rtc=off",
1349                  &data);
1350    free_test_data(&data);
1351}
1352
1353static void test_acpi_virt_tcg_numamem(void)
1354{
1355    test_data data = {
1356        .machine = "virt",
1357        .tcg_only = true,
1358        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1359        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1360        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1361        .ram_start = 0x40000000ULL,
1362        .scan_len = 128ULL * 1024 * 1024,
1363    };
1364
1365    data.variant = ".numamem";
1366    test_acpi_one(" -cpu cortex-a57"
1367                  " -object memory-backend-ram,id=ram0,size=128M"
1368                  " -numa node,memdev=ram0",
1369                  &data);
1370
1371    free_test_data(&data);
1372
1373}
1374
1375static void test_acpi_virt_tcg_pxb(void)
1376{
1377    test_data data = {
1378        .machine = "virt",
1379        .tcg_only = true,
1380        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1381        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1382        .ram_start = 0x40000000ULL,
1383        .scan_len = 128ULL * 1024 * 1024,
1384    };
1385    /*
1386     * While using -cdrom, the cdrom would auto plugged into pxb-pcie,
1387     * the reason is the bus of pxb-pcie is also root bus, it would lead
1388     * to the error only PCI/PCIE bridge could plug onto pxb.
1389     * Therefore,thr cdrom is defined and plugged onto the scsi controller
1390     * to solve the conflicts.
1391     */
1392    data.variant = ".pxb";
1393    test_acpi_one(" -device pcie-root-port,chassis=1,id=pci.1"
1394                  " -device virtio-scsi-pci,id=scsi0,bus=pci.1"
1395                  " -drive file="
1396                  "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2,"
1397                  "if=none,media=cdrom,id=drive-scsi0-0-0-1,readonly=on"
1398                  " -device scsi-cd,bus=scsi0.0,scsi-id=0,"
1399                  "drive=drive-scsi0-0-0-1,id=scsi0-0-0-1,bootindex=1"
1400                  " -cpu cortex-a57"
1401                  " -device pxb-pcie,bus_nr=128",
1402                  &data);
1403
1404    free_test_data(&data);
1405}
1406
1407static void test_acpi_tcg_acpi_hmat(const char *machine)
1408{
1409    test_data data;
1410
1411    memset(&data, 0, sizeof(data));
1412    data.machine = machine;
1413    data.variant = ".acpihmat";
1414    test_acpi_one(" -machine hmat=on"
1415                  " -smp 2,sockets=2"
1416                  " -m 128M,slots=2,maxmem=1G"
1417                  " -object memory-backend-ram,size=64M,id=m0"
1418                  " -object memory-backend-ram,size=64M,id=m1"
1419                  " -numa node,nodeid=0,memdev=m0"
1420                  " -numa node,nodeid=1,memdev=m1,initiator=0"
1421                  " -numa cpu,node-id=0,socket-id=0"
1422                  " -numa cpu,node-id=0,socket-id=1"
1423                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1424                  "data-type=access-latency,latency=1"
1425                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1426                  "data-type=access-bandwidth,bandwidth=65534M"
1427                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1428                  "data-type=access-latency,latency=65534"
1429                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1430                  "data-type=access-bandwidth,bandwidth=32767M"
1431                  " -numa hmat-cache,node-id=0,size=10K,level=1,"
1432                  "associativity=direct,policy=write-back,line=8"
1433                  " -numa hmat-cache,node-id=1,size=10K,level=1,"
1434                  "associativity=direct,policy=write-back,line=8",
1435                  &data);
1436    free_test_data(&data);
1437}
1438
1439static void test_acpi_q35_tcg_acpi_hmat(void)
1440{
1441    test_acpi_tcg_acpi_hmat(MACHINE_Q35);
1442}
1443
1444static void test_acpi_piix4_tcg_acpi_hmat(void)
1445{
1446    test_acpi_tcg_acpi_hmat(MACHINE_PC);
1447}
1448
1449static void test_acpi_virt_tcg(void)
1450{
1451    test_data data = {
1452        .machine = "virt",
1453        .tcg_only = true,
1454        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1455        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1456        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1457        .ram_start = 0x40000000ULL,
1458        .scan_len = 128ULL * 1024 * 1024,
1459    };
1460
1461    data.smbios_cpu_max_speed = 2900;
1462    data.smbios_cpu_curr_speed = 2700;
1463    test_acpi_one("-cpu cortex-a57 "
1464                  "-smbios type=4,max-speed=2900,current-speed=2700", &data);
1465    free_test_data(&data);
1466}
1467
1468static void test_oem_fields(test_data *data)
1469{
1470    int i;
1471    char oem_id[6];
1472    char oem_table_id[8];
1473
1474    strpadcpy(oem_id, sizeof oem_id, OEM_ID, ' ');
1475    strpadcpy(oem_table_id, sizeof oem_table_id, OEM_TABLE_ID, ' ');
1476    for (i = 0; i < data->tables->len; ++i) {
1477        AcpiSdtTable *sdt;
1478
1479        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
1480        /* FACS doesn't have OEMID and OEMTABLEID fields */
1481        if (compare_signature(sdt, "FACS")) {
1482            continue;
1483        }
1484
1485        g_assert(memcmp(sdt->aml + 10, oem_id, 6) == 0);
1486        g_assert(memcmp(sdt->aml + 16, oem_table_id, 8) == 0);
1487    }
1488}
1489
1490static void test_acpi_oem_fields_pc(void)
1491{
1492    test_data data;
1493    char *args;
1494
1495    memset(&data, 0, sizeof(data));
1496    data.machine = MACHINE_PC;
1497    data.required_struct_types = base_required_struct_types;
1498    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1499
1500    args = test_acpi_create_args(&data,
1501                                 OEM_TEST_ARGS, false);
1502    data.qts = qtest_init(args);
1503    test_acpi_load_tables(&data, false);
1504    test_oem_fields(&data);
1505    qtest_quit(data.qts);
1506    free_test_data(&data);
1507    g_free(args);
1508}
1509
1510static void test_acpi_oem_fields_q35(void)
1511{
1512    test_data data;
1513    char *args;
1514
1515    memset(&data, 0, sizeof(data));
1516    data.machine = MACHINE_Q35;
1517    data.required_struct_types = base_required_struct_types;
1518    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1519
1520    args = test_acpi_create_args(&data,
1521                                 OEM_TEST_ARGS, false);
1522    data.qts = qtest_init(args);
1523    test_acpi_load_tables(&data, false);
1524    test_oem_fields(&data);
1525    qtest_quit(data.qts);
1526    free_test_data(&data);
1527    g_free(args);
1528}
1529
1530static void test_acpi_oem_fields_microvm(void)
1531{
1532    test_data data;
1533    char *args;
1534
1535    test_acpi_microvm_prepare(&data);
1536
1537    args = test_acpi_create_args(&data,
1538                                 OEM_TEST_ARGS",acpi=on", false);
1539    data.qts = qtest_init(args);
1540    test_acpi_load_tables(&data, false);
1541    test_oem_fields(&data);
1542    qtest_quit(data.qts);
1543    free_test_data(&data);
1544    g_free(args);
1545}
1546
1547static void test_acpi_oem_fields_virt(void)
1548{
1549    test_data data = {
1550        .machine = "virt",
1551        .tcg_only = true,
1552        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1553        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1554        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1555        .ram_start = 0x40000000ULL,
1556        .scan_len = 128ULL * 1024 * 1024,
1557    };
1558    char *args;
1559
1560    args = test_acpi_create_args(&data,
1561                                 "-cpu cortex-a57 "OEM_TEST_ARGS, true);
1562    data.qts = qtest_init(args);
1563    test_acpi_load_tables(&data, true);
1564    test_oem_fields(&data);
1565    qtest_quit(data.qts);
1566    free_test_data(&data);
1567    g_free(args);
1568}
1569
1570
1571int main(int argc, char *argv[])
1572{
1573    const char *arch = qtest_get_arch();
1574    const bool has_kvm = qtest_has_accel("kvm");
1575    const bool has_tcg = qtest_has_accel("tcg");
1576    int ret;
1577
1578    g_test_init(&argc, &argv, NULL);
1579
1580    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
1581        ret = boot_sector_init(disk);
1582        if (ret) {
1583            return ret;
1584        }
1585        qtest_add_func("acpi/q35/oem-fields", test_acpi_oem_fields_q35);
1586        if (tpm_model_is_available("-machine q35", "tpm-tis")) {
1587            qtest_add_func("acpi/q35/tpm2-tis", test_acpi_q35_tcg_tpm2_tis);
1588            qtest_add_func("acpi/q35/tpm12-tis", test_acpi_q35_tcg_tpm12_tis);
1589        }
1590        qtest_add_func("acpi/piix4", test_acpi_piix4_tcg);
1591        qtest_add_func("acpi/oem-fields", test_acpi_oem_fields_pc);
1592        qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge);
1593        qtest_add_func("acpi/piix4/pci-hotplug/no_root_hotplug",
1594                       test_acpi_piix4_no_root_hotplug);
1595        qtest_add_func("acpi/piix4/pci-hotplug/no_bridge_hotplug",
1596                       test_acpi_piix4_no_bridge_hotplug);
1597        qtest_add_func("acpi/piix4/pci-hotplug/off",
1598                       test_acpi_piix4_no_acpi_pci_hotplug);
1599        qtest_add_func("acpi/q35", test_acpi_q35_tcg);
1600        qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
1601        qtest_add_func("acpi/q35/multif-bridge", test_acpi_q35_multif_bridge);
1602        qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64);
1603        qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi);
1604        qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi);
1605        qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp);
1606        qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp);
1607        qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
1608        qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
1609        qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
1610        qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
1611        qtest_add_func("acpi/piix4/nosmm", test_acpi_piix4_tcg_nosmm);
1612        qtest_add_func("acpi/piix4/smm-compat",
1613                       test_acpi_piix4_tcg_smm_compat);
1614        qtest_add_func("acpi/piix4/smm-compat-nosmm",
1615                       test_acpi_piix4_tcg_smm_compat_nosmm);
1616        qtest_add_func("acpi/piix4/nohpet", test_acpi_piix4_tcg_nohpet);
1617        qtest_add_func("acpi/q35/nosmm", test_acpi_q35_tcg_nosmm);
1618        qtest_add_func("acpi/q35/smm-compat",
1619                       test_acpi_q35_tcg_smm_compat);
1620        qtest_add_func("acpi/q35/smm-compat-nosmm",
1621                       test_acpi_q35_tcg_smm_compat_nosmm);
1622        qtest_add_func("acpi/q35/nohpet", test_acpi_q35_tcg_nohpet);
1623        qtest_add_func("acpi/piix4/dimmpxm", test_acpi_piix4_tcg_dimm_pxm);
1624        qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm);
1625        qtest_add_func("acpi/piix4/acpihmat", test_acpi_piix4_tcg_acpi_hmat);
1626        qtest_add_func("acpi/q35/acpihmat", test_acpi_q35_tcg_acpi_hmat);
1627        qtest_add_func("acpi/microvm", test_acpi_microvm_tcg);
1628        qtest_add_func("acpi/microvm/usb", test_acpi_microvm_usb_tcg);
1629        qtest_add_func("acpi/microvm/rtc", test_acpi_microvm_rtc_tcg);
1630        qtest_add_func("acpi/microvm/ioapic2", test_acpi_microvm_ioapic2_tcg);
1631        qtest_add_func("acpi/microvm/oem-fields", test_acpi_oem_fields_microvm);
1632        if (has_tcg) {
1633            qtest_add_func("acpi/q35/ivrs", test_acpi_q35_tcg_ivrs);
1634            if (strcmp(arch, "x86_64") == 0) {
1635                qtest_add_func("acpi/microvm/pcie", test_acpi_microvm_pcie_tcg);
1636            }
1637        }
1638        if (has_kvm) {
1639            qtest_add_func("acpi/q35/kvm/xapic", test_acpi_q35_kvm_xapic);
1640            qtest_add_func("acpi/q35/kvm/dmar", test_acpi_q35_kvm_dmar);
1641        }
1642    } else if (strcmp(arch, "aarch64") == 0) {
1643        if (has_tcg) {
1644            qtest_add_func("acpi/virt", test_acpi_virt_tcg);
1645            qtest_add_func("acpi/virt/numamem", test_acpi_virt_tcg_numamem);
1646            qtest_add_func("acpi/virt/memhp", test_acpi_virt_tcg_memhp);
1647            qtest_add_func("acpi/virt/pxb", test_acpi_virt_tcg_pxb);
1648            qtest_add_func("acpi/virt/oem-fields", test_acpi_oem_fields_virt);
1649        }
1650    }
1651    ret = g_test_run();
1652    boot_sector_cleanup(disk);
1653    return ret;
1654}
1655