bios-tables-test.c 24.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Boot order test cases.
 *
 * Copyright (c) 2013 Red Hat Inc.
 *
 * Authors:
 *  Michael S. Tsirkin <mst@redhat.com>,
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

P
Peter Maydell 已提交
13
#include "qemu/osdep.h"
14
#include <glib/gstdio.h>
15
#include "qemu-common.h"
16
#include "hw/firmware/smbios.h"
G
Gabriel L. Somlo 已提交
17
#include "qemu/bitmap.h"
18
#include "acpi-utils.h"
19
#include "boot-sector.h"
20

21 22 23
#define MACHINE_PC "pc"
#define MACHINE_Q35 "q35"

24 25
#define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"

26
typedef struct {
27
    const char *machine;
28
    const char *variant;
29
    uint64_t rsdp_addr;
30
    uint8_t rsdp_table[36 /* ACPI 2.0+ RSDP size */];
31
    GArray *tables;
G
Gabriel L. Somlo 已提交
32
    uint32_t smbios_ep_addr;
W
Wei Huang 已提交
33
    struct smbios_21_entry_point smbios_ep_table;
34 35
    uint8_t *required_struct_types;
    int required_struct_types_len;
36
    QTestState *qts;
37
} test_data;
38

39
static char disk[] = "tests/acpi-test-disk-XXXXXX";
40
static const char *data_dir = "tests/data/acpi";
41 42 43 44 45
#ifdef CONFIG_IASL
static const char *iasl = stringify(CONFIG_IASL);
#else
static const char *iasl;
#endif
46

47 48 49 50 51
static bool compare_signature(const AcpiSdtTable *sdt, const char *signature)
{
   return !memcmp(sdt->aml, signature, 4);
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
static void cleanup_table_descriptor(AcpiSdtTable *table)
{
    g_free(table->aml);
    if (table->aml_file &&
        !table->tmp_files_retain &&
        g_strstr_len(table->aml_file, -1, "aml-")) {
        unlink(table->aml_file);
    }
    g_free(table->aml_file);
    g_free(table->asl);
    if (table->asl_file &&
        !table->tmp_files_retain) {
        unlink(table->asl_file);
    }
    g_free(table->asl_file);
}

69 70 71 72
static void free_test_data(test_data *data)
{
    int i;

73
    for (i = 0; i < data->tables->len; ++i) {
74
        cleanup_table_descriptor(&g_array_index(data->tables, AcpiSdtTable, i));
75
    }
76

77
    g_array_free(data->tables, true);
78 79
}

80 81
static void test_acpi_rsdp_address(test_data *data)
{
82
    uint32_t off = acpi_find_rsdp_address(data->qts);
83 84 85 86 87 88
    g_assert_cmphex(off, <, 0x100000);
    data->rsdp_addr = off;
}

static void test_acpi_rsdp_table(test_data *data)
{
89
    uint8_t *rsdp_table = data->rsdp_table;
90

91
    acpi_fetch_rsdp_table(data->qts, data->rsdp_addr, rsdp_table);
92

93
    switch (rsdp_table[15 /* Revision offset */]) {
94 95 96 97 98 99 100 101 102 103 104 105
    case 0: /* ACPI 1.0 RSDP */
        /* With rev 1, checksum is only for the first 20 bytes */
        g_assert(!acpi_calc_checksum(rsdp_table,  20));
        break;
    case 2: /* ACPI 2.0+ RSDP */
        /* With revision 2, we have 2 checksums */
        g_assert(!acpi_calc_checksum(rsdp_table, 20));
        g_assert(!acpi_calc_checksum(rsdp_table, 36));
        break;
    default:
        g_assert_not_reached();
    }
106 107
}

108
static void test_acpi_rxsdt_table(test_data *data)
109
{
110
    const char *sig = "RSDT";
111
    AcpiSdtTable rsdt = {};
112 113
    int entry_size = 4;
    int addr_off = 16 /* RsdtAddress */;
114
    uint8_t *ent;
115

116 117 118 119 120 121
    if (data->rsdp_table[15 /* Revision offset */] != 0) {
        addr_off = 24 /* XsdtAddress */;
        entry_size = 8;
        sig = "XSDT";
    }
    /* read [RX]SDT table */
122
    acpi_fetch_table(data->qts, &rsdt.aml, &rsdt.aml_len,
123
                     &data->rsdp_table[addr_off], entry_size, sig, true);
124 125

    /* Load all tables and add to test list directly RSDT referenced tables */
126
    ACPI_FOREACH_RSDT_ENTRY(rsdt.aml, rsdt.aml_len, ent, entry_size) {
127 128
        AcpiSdtTable ssdt_table = {};

129
        acpi_fetch_table(data->qts, &ssdt_table.aml, &ssdt_table.aml_len, ent,
130
                         entry_size, NULL, true);
131 132 133 134
        /* Add table to ASL test tables list */
        g_array_append_val(data->tables, ssdt_table);
    }
    cleanup_table_descriptor(&rsdt);
135 136
}

137
static void test_acpi_fadt_table(test_data *data)
138
{
139
    /* FADT table is 1st */
140 141
    AcpiSdtTable table = g_array_index(data->tables, typeof(table), 0);
    uint8_t *fadt_aml = table.aml;
142
    uint32_t fadt_len = table.aml_len;
143 144 145
    uint32_t val;
    int dsdt_offset = 40 /* DSDT */;
    int dsdt_entry_size = 4;
146

147
    g_assert(compare_signature(&table, "FACP"));
148

149
    /* Since DSDT/FACS isn't in RSDT, add them to ASL test list manually */
150 151 152 153 154 155 156
    memcpy(&val, fadt_aml + 112 /* Flags */, 4);
    val = le32_to_cpu(val);
    if (!(val & 1UL << 20 /* HW_REDUCED_ACPI */)) {
        acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
                         fadt_aml + 36 /* FIRMWARE_CTRL */, 4, "FACS", false);
        g_array_append_val(data->tables, table);
    }
157

158 159 160 161 162 163
    memcpy(&val, fadt_aml + dsdt_offset, 4);
    val = le32_to_cpu(val);
    if (!val) {
        dsdt_offset = 140 /* X_DSDT */;
        dsdt_entry_size = 8;
    }
164
    acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
165
                     fadt_aml + dsdt_offset, dsdt_entry_size, "DSDT", true);
166
    g_array_append_val(data->tables, table);
167

168 169 170 171 172
    memset(fadt_aml + 36, 0, 4); /* sanitize FIRMWARE_CTRL ptr */
    memset(fadt_aml + 40, 0, 4); /* sanitize DSDT ptr */
    if (fadt_aml[8 /* FADT Major Version */] >= 3) {
        memset(fadt_aml + 132, 0, 8); /* sanitize X_FIRMWARE_CTRL ptr */
        memset(fadt_aml + 140, 0, 8); /* sanitize X_DSDT ptr */
173
    }
174 175 176 177

    /* update checksum */
    fadt_aml[9 /* Checksum */] = 0;
    fadt_aml[9 /* Checksum */] -= acpi_calc_checksum(fadt_aml, fadt_len);
178 179
}

180
static void dump_aml_files(test_data *data, bool rebuild)
181 182 183
{
    AcpiSdtTable *sdt;
    GError *error = NULL;
184
    gchar *aml_file = NULL;
185 186 187 188
    gint fd;
    ssize_t ret;
    int i;

189
    for (i = 0; i < data->tables->len; ++i) {
190
        const char *ext = data->variant ? data->variant : "";
191
        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
192 193
        g_assert(sdt->aml);

194
        if (rebuild) {
195
            aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
196
                                       sdt->aml, ext);
197 198 199 200 201 202 203
            fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
                        S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
        } else {
            fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
            g_assert_no_error(error);
        }
        g_assert(fd >= 0);
204 205 206 207 208

        ret = qemu_write_full(fd, sdt->aml, sdt->aml_len);
        g_assert(ret == sdt->aml_len);

        close(fd);
209

210
        g_free(aml_file);
211 212 213
    }
}

214
static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
215 216 217
{
    AcpiSdtTable *temp;
    GError *error = NULL;
218
    GString *command_line = g_string_new(iasl);
219 220 221 222 223 224 225 226 227 228
    gint fd;
    gchar *out, *out_err;
    gboolean ret;
    int i;

    fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
    g_assert_no_error(error);
    close(fd);

    /* build command line */
229
    g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
230 231
    if (compare_signature(sdt, "DSDT") ||
        compare_signature(sdt, "SSDT")) {
232 233
        for (i = 0; i < sdts->len; ++i) {
            temp = &g_array_index(sdts, AcpiSdtTable, i);
234 235
            if (compare_signature(temp, "DSDT") ||
                compare_signature(temp, "SSDT")) {
236 237 238
                g_string_append_printf(command_line, "-e %s ", temp->aml_file);
            }
        }
239 240 241 242
    }
    g_string_append_printf(command_line, "-d %s", sdt->aml_file);

    /* pass 'out' and 'out_err' in order to be redirected */
243
    ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
244
    g_assert_no_error(error);
245
    if (ret) {
246
        ret = g_file_get_contents(sdt->asl_file, &sdt->asl,
247 248 249
                                  &sdt->asl_len, &error);
        g_assert(ret);
        g_assert_no_error(error);
250
        ret = (sdt->asl_len > 0);
251
    }
252 253 254 255

    g_free(out);
    g_free(out_err);
    g_string_free(command_line, true);
256 257

    return !ret;
258 259 260 261
}

#define COMMENT_END "*/"
#define DEF_BLOCK "DefinitionBlock ("
262
#define BLOCK_NAME_END ","
263 264 265 266 267 268 269 270 271

static GString *normalize_asl(gchar *asl_code)
{
    GString *asl = g_string_new(asl_code);
    gchar *comment, *block_name;

    /* strip comments (different generation days) */
    comment = g_strstr_len(asl->str, asl->len, COMMENT_END);
    if (comment) {
272 273 274 275 276
        comment += strlen(COMMENT_END);
        while (*comment == '\n') {
            comment++;
        }
        asl = g_string_erase(asl, 0, comment - asl->str);
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
    }

    /* strip def block name (it has file path in it) */
    if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
        block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
        g_assert(block_name);
        asl = g_string_erase(asl, 0,
                             block_name + sizeof(BLOCK_NAME_END) - asl->str);
    }

    return asl;
}

static GArray *load_expected_aml(test_data *data)
{
    int i;
    AcpiSdtTable *sdt;
    GError *error = NULL;
    gboolean ret;
296
    gsize aml_len;
297

298
    GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
299 300 301
    if (getenv("V")) {
        fputc('\n', stderr);
    }
302
    for (i = 0; i < data->tables->len; ++i) {
303
        AcpiSdtTable exp_sdt;
304
        gchar *aml_file = NULL;
305
        const char *ext = data->variant ? data->variant : "";
306

307
        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
308 309 310

        memset(&exp_sdt, 0, sizeof(exp_sdt));

311 312
try_again:
        aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
313
                                   sdt->aml, ext);
314
        if (getenv("V")) {
315
            fprintf(stderr, "Looking for expected file '%s'\n", aml_file);
316 317 318 319
        }
        if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) {
            exp_sdt.aml_file = aml_file;
        } else if (*ext != '\0') {
320
            /* try fallback to generic (extension less) expected file */
321
            ext = "";
322
            g_free(aml_file);
323 324
            goto try_again;
        }
325 326
        g_assert(exp_sdt.aml_file);
        if (getenv("V")) {
327
            fprintf(stderr, "Using expected file '%s'\n", aml_file);
328
        }
329
        ret = g_file_get_contents(aml_file, (gchar **)&exp_sdt.aml,
330 331
                                  &aml_len, &error);
        exp_sdt.aml_len = aml_len;
332 333 334 335 336
        g_assert(ret);
        g_assert_no_error(error);
        g_assert(exp_sdt.aml);
        g_assert(exp_sdt.aml_len);

337
        g_array_append_val(exp_tables, exp_sdt);
338 339
    }

340
    return exp_tables;
341 342
}

343
/* test the list of tables in @data->tables against reference tables */
344 345 346 347 348
static void test_acpi_asl(test_data *data)
{
    int i;
    AcpiSdtTable *sdt, *exp_sdt;
    test_data exp_data;
349
    gboolean exp_err, err;
350 351

    memset(&exp_data, 0, sizeof(exp_data));
352
    exp_data.tables = load_expected_aml(data);
353
    dump_aml_files(data, false);
354
    for (i = 0; i < data->tables->len; ++i) {
355 356
        GString *asl, *exp_asl;

357 358
        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
        exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
359

360
        err = load_asl(data->tables, sdt);
361 362
        asl = normalize_asl(sdt->asl);

363
        exp_err = load_asl(exp_data.tables, exp_sdt);
364 365
        exp_asl = normalize_asl(exp_sdt->asl);

366 367 368
        /* TODO: check for warnings */
        g_assert(!err || exp_err);

369
        if (g_strcmp0(asl->str, exp_asl->str)) {
370 371 372 373 374 375 376 377 378
            if (exp_err) {
                fprintf(stderr,
                        "Warning! iasl couldn't parse the expected aml\n");
            } else {
                sdt->tmp_files_retain = true;
                exp_sdt->tmp_files_retain = true;
                fprintf(stderr,
                        "acpi-test: Warning! %.4s mismatch. "
                        "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
379
                        exp_sdt->aml, sdt->asl_file, sdt->aml_file,
380
                        exp_sdt->asl_file, exp_sdt->aml_file);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
                if (getenv("V")) {
                    const char *diff_cmd = getenv("DIFF");
                    if (diff_cmd) {
                        int ret G_GNUC_UNUSED;
                        char *diff = g_strdup_printf("%s %s %s", diff_cmd,
                            exp_sdt->asl_file, sdt->asl_file);
                        ret = system(diff) ;
                        g_free(diff);
                    } else {
                        fprintf(stderr, "acpi-test: Warning. not showing "
                            "difference since no diff utility is specified. "
                            "Set 'DIFF' environment variable to a preferred "
                            "diff utility and run 'make V=1 check' again to "
                            "see ASL difference.");
                    }
                }
397
          }
398
        }
399 400 401 402 403
        g_string_free(asl, true);
        g_string_free(exp_asl, true);
    }

    free_test_data(&exp_data);
404 405
}

406
static bool smbios_ep_table_ok(test_data *data)
G
Gabriel L. Somlo 已提交
407
{
W
Wei Huang 已提交
408
    struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
G
Gabriel L. Somlo 已提交
409 410
    uint32_t addr = data->smbios_ep_addr;

411
    qtest_memread(data->qts, addr, ep_table, sizeof(*ep_table));
412 413 414 415 416 417 418 419 420 421 422 423
    if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
        return false;
    }
    if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
        return false;
    }
    if (ep_table->structure_table_length == 0) {
        return false;
    }
    if (ep_table->number_of_structures == 0) {
        return false;
    }
424 425 426
    if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
        acpi_calc_checksum((uint8_t *)ep_table + 0x10,
                           sizeof *ep_table - 0x10)) {
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
        return false;
    }
    return true;
}

static void test_smbios_entry_point(test_data *data)
{
    uint32_t off;

    /* find smbios entry point structure */
    for (off = 0xf0000; off < 0x100000; off += 0x10) {
        uint8_t sig[] = "_SM_";
        int i;

        for (i = 0; i < sizeof sig - 1; ++i) {
442
            sig[i] = qtest_readb(data->qts, off + i);
443 444 445 446 447 448 449 450 451 452 453 454
        }

        if (!memcmp(sig, "_SM_", sizeof sig)) {
            /* signature match, but is this a valid entry point? */
            data->smbios_ep_addr = off;
            if (smbios_ep_table_ok(data)) {
                break;
            }
        }
    }

    g_assert_cmphex(off, <, 0x100000);
G
Gabriel L. Somlo 已提交
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
}

static inline bool smbios_single_instance(uint8_t type)
{
    switch (type) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 16:
    case 32:
    case 127:
        return true;
    default:
        return false;
    }
}

static void test_smbios_structs(test_data *data)
{
    DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };
W
Wei Huang 已提交
476
    struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
477
    uint32_t addr = le32_to_cpu(ep_table->structure_table_address);
G
Gabriel L. Somlo 已提交
478 479 480 481
    int i, len, max_len = 0;
    uint8_t type, prv, crt;

    /* walk the smbios tables */
482
    for (i = 0; i < le16_to_cpu(ep_table->number_of_structures); i++) {
G
Gabriel L. Somlo 已提交
483 484

        /* grab type and formatted area length from struct header */
485
        type = qtest_readb(data->qts, addr);
G
Gabriel L. Somlo 已提交
486
        g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
487
        len = qtest_readb(data->qts, addr + 1);
G
Gabriel L. Somlo 已提交
488 489 490 491 492 493 494 495 496 497 498

        /* single-instance structs must not have been encountered before */
        if (smbios_single_instance(type)) {
            g_assert(!test_bit(type, struct_bitmap));
        }
        set_bit(type, struct_bitmap);

        /* seek to end of unformatted string area of this struct ("\0\0") */
        prv = crt = 1;
        while (prv || crt) {
            prv = crt;
499
            crt = qtest_readb(data->qts, addr + len);
G
Gabriel L. Somlo 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513
            len++;
        }

        /* keep track of max. struct size */
        if (max_len < len) {
            max_len = len;
            g_assert_cmpuint(max_len, <=, ep_table->max_structure_size);
        }

        /* start of next structure */
        addr += len;
    }

    /* total table length and max struct size must match entry point values */
514 515 516
    g_assert_cmpuint(le16_to_cpu(ep_table->structure_table_length), ==,
                     addr - le32_to_cpu(ep_table->structure_table_address));
    g_assert_cmpuint(le16_to_cpu(ep_table->max_structure_size), ==, max_len);
G
Gabriel L. Somlo 已提交
517 518

    /* required struct types must all be present */
519 520
    for (i = 0; i < data->required_struct_types_len; i++) {
        g_assert(test_bit(data->required_struct_types[i], struct_bitmap));
G
Gabriel L. Somlo 已提交
521 522 523
    }
}

524
static void test_acpi_one(const char *params, test_data *data)
525 526 527
{
    char *args;

528 529 530
    /* Disable kernel irqchip to be able to override apic irq0. */
    args = g_strdup_printf("-machine %s,accel=%s,kernel-irqchip=off "
                           "-net none -display none %s "
531
                           "-drive id=hd0,if=none,file=%s,format=raw "
532
                           "-device ide-hd,drive=hd0 ",
533
                           data->machine, "kvm:tcg",
534
                           params ? params : "", disk);
535

536
    data->qts = qtest_init(args);
537

538
    boot_sector_test(data->qts);
539

540
    data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
541 542
    test_acpi_rsdp_address(data);
    test_acpi_rsdp_table(data);
543
    test_acpi_rxsdt_table(data);
544
    test_acpi_fadt_table(data);
545

546
    if (iasl) {
547 548 549 550 551
        if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
            dump_aml_files(data, true);
        } else {
            test_acpi_asl(data);
        }
552 553
    }

554
    test_smbios_entry_point(data);
G
Gabriel L. Somlo 已提交
555 556
    test_smbios_structs(data);

557 558
    assert(!global_qtest);
    qtest_quit(data->qts);
559 560 561
    g_free(args);
}

562 563 564 565
static uint8_t base_required_struct_types[] = {
    0, 1, 3, 4, 16, 17, 19, 32, 127
};

566
static void test_acpi_piix4_tcg(void)
567
{
568 569
    test_data data;

570 571 572
    /* Supplying -machine accel argument overrides the default (qtest).
     * This is to make guest actually run.
     */
573 574
    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_PC;
575 576
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
577
    test_acpi_one(NULL, &data);
578
    free_test_data(&data);
579 580
}

581 582 583 584 585 586 587
static void test_acpi_piix4_tcg_bridge(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_PC;
    data.variant = ".bridge";
588 589
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
590
    test_acpi_one("-device pci-bridge,chassis_nr=1", &data);
591 592 593
    free_test_data(&data);
}

594 595 596
static void test_acpi_q35_tcg(void)
{
    test_data data;
597

598 599
    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_Q35;
600 601
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
602
    test_acpi_one(NULL, &data);
603
    free_test_data(&data);
604 605
}

606 607 608 609 610 611 612
static void test_acpi_q35_tcg_bridge(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_Q35;
    data.variant = ".bridge";
613 614
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
615
    test_acpi_one("-device pci-bridge,chassis_nr=1",
616 617 618 619
                  &data);
    free_test_data(&data);
}

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
static void test_acpi_q35_tcg_mmio64(void)
{
    test_data data = {
        .machine = MACHINE_Q35,
        .variant = ".mmio64",
        .required_struct_types = base_required_struct_types,
        .required_struct_types_len = ARRAY_SIZE(base_required_struct_types)
    };

    test_acpi_one("-m 128M,slots=1,maxmem=2G "
                  "-device pci-testdev,membar=2G",
                  &data);
    free_test_data(&data);
}

635 636 637 638 639 640 641
static void test_acpi_piix4_tcg_cphp(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_PC;
    data.variant = ".cphp";
642
    test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
643 644
                  " -numa node -numa node"
                  " -numa dist,src=0,dst=1,val=21",
645 646 647 648 649 650 651 652 653 654 655
                  &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_cphp(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_Q35;
    data.variant = ".cphp";
656
    test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
657 658
                  " -numa node -numa node"
                  " -numa dist,src=0,dst=1,val=21",
659 660 661 662
                  &data);
    free_test_data(&data);
}

663 664 665 666 667 668 669 670 671 672 673 674 675
static uint8_t ipmi_required_struct_types[] = {
    0, 1, 3, 4, 16, 17, 19, 32, 38, 127
};

static void test_acpi_q35_tcg_ipmi(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_Q35;
    data.variant = ".ipmibt";
    data.required_struct_types = ipmi_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
676
    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
                  " -device isa-ipmi-bt,bmc=bmc0",
                  &data);
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_ipmi(void)
{
    test_data data;

    /* Supplying -machine accel argument overrides the default (qtest).
     * This is to make guest actually run.
     */
    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_PC;
    data.variant = ".ipmikcs";
    data.required_struct_types = ipmi_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
694
    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
695 696 697 698 699
                  " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
                  &data);
    free_test_data(&data);
}

700 701 702 703 704 705 706
static void test_acpi_q35_tcg_memhp(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_Q35;
    data.variant = ".memhp";
707 708 709 710
    test_acpi_one(" -m 128,slots=3,maxmem=1G"
                  " -numa node -numa node"
                  " -numa dist,src=0,dst=1,val=21",
                  &data);
711 712 713 714 715 716 717 718 719 720
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_memhp(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_PC;
    data.variant = ".memhp";
721 722 723 724
    test_acpi_one(" -m 128,slots=3,maxmem=1G"
                  " -numa node -numa node"
                  " -numa dist,src=0,dst=1,val=21",
                  &data);
725 726 727
    free_test_data(&data);
}

728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
static void test_acpi_q35_tcg_numamem(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_Q35;
    data.variant = ".numamem";
    test_acpi_one(" -numa node -numa node,mem=128", &data);
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_numamem(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_PC;
    data.variant = ".numamem";
    test_acpi_one(" -numa node -numa node,mem=128", &data);
    free_test_data(&data);
}

750 751 752 753 754 755 756
static void test_acpi_tcg_dimm_pxm(const char *machine)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = machine;
    data.variant = ".dimmpxm";
757
    test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
                  " -smp 4,sockets=4"
                  " -m 128M,slots=3,maxmem=1G"
                  " -numa node,mem=32M,nodeid=0"
                  " -numa node,mem=32M,nodeid=1"
                  " -numa node,mem=32M,nodeid=2"
                  " -numa node,mem=32M,nodeid=3"
                  " -numa cpu,node-id=0,socket-id=0"
                  " -numa cpu,node-id=1,socket-id=1"
                  " -numa cpu,node-id=2,socket-id=2"
                  " -numa cpu,node-id=3,socket-id=3"
                  " -object memory-backend-ram,id=ram0,size=128M"
                  " -object memory-backend-ram,id=nvm0,size=128M"
                  " -device pc-dimm,id=dimm0,memdev=ram0,node=1"
                  " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
                  &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_dimm_pxm(void)
{
    test_acpi_tcg_dimm_pxm(MACHINE_Q35);
}

static void test_acpi_piix4_tcg_dimm_pxm(void)
{
    test_acpi_tcg_dimm_pxm(MACHINE_PC);
}

786 787 788
int main(int argc, char *argv[])
{
    const char *arch = qtest_get_arch();
789
    int ret;
790

791 792 793
    g_test_init(&argc, &argv, NULL);

    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
794 795 796 797 798
        ret = boot_sector_init(disk);
        if (ret) {
            return ret;
        }

799 800 801 802
        qtest_add_func("acpi/piix4", test_acpi_piix4_tcg);
        qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge);
        qtest_add_func("acpi/q35", test_acpi_q35_tcg);
        qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
803
        qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64);
804 805 806 807
        qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi);
        qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi);
        qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp);
        qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp);
808 809
        qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
        qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
810 811
        qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
        qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
812 813
        qtest_add_func("acpi/piix4/dimmpxm", test_acpi_piix4_tcg_dimm_pxm);
        qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm);
814
    }
815
    ret = g_test_run();
816
    boot_sector_cleanup(disk);
817
    return ret;
818
}