bios-tables-test.c 27.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/smbios/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 30 31
    uint32_t rsdp_addr;
    AcpiRsdpDescriptor rsdp_table;
    AcpiRsdtDescriptorRev1 rsdt_table;
32
    AcpiFadtDescriptorRev3 fadt_table;
33
    AcpiFacsDescriptorRev1 facs_table;
34 35
    uint32_t *rsdt_tables_addr;
    int rsdt_tables_nr;
36
    GArray *tables;
G
Gabriel L. Somlo 已提交
37
    uint32_t smbios_ep_addr;
W
Wei Huang 已提交
38
    struct smbios_21_entry_point smbios_ep_table;
39 40
    uint8_t *required_struct_types;
    int required_struct_types_len;
41
} test_data;
42

43
static char disk[] = "tests/acpi-test-disk-XXXXXX";
44
static const char *data_dir = "tests/acpi-test-data";
45 46 47 48 49
#ifdef CONFIG_IASL
static const char *iasl = stringify(CONFIG_IASL);
#else
static const char *iasl;
#endif
50

51 52
static void free_test_data(test_data *data)
{
53
    AcpiSdtTable *temp;
54 55
    int i;

56
    g_free(data->rsdt_tables_addr);
57

58 59
    for (i = 0; i < data->tables->len; ++i) {
        temp = &g_array_index(data->tables, AcpiSdtTable, i);
60 61 62 63 64
        g_free(temp->aml);
        if (temp->aml_file &&
            !temp->tmp_files_retain &&
            g_strstr_len(temp->aml_file, -1, "aml-")) {
            unlink(temp->aml_file);
65
        }
66 67 68 69 70
        g_free(temp->aml_file);
        g_free(temp->asl);
        if (temp->asl_file &&
            !temp->tmp_files_retain) {
            unlink(temp->asl_file);
71
        }
72
        g_free(temp->asl_file);
73
    }
74

75
    g_array_free(data->tables, true);
76 77
}

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

static void test_acpi_rsdp_table(test_data *data)
{
    AcpiRsdpDescriptor *rsdp_table = &data->rsdp_table;
    uint32_t addr = data->rsdp_addr;

90
    acpi_parse_rsdp_table(addr, rsdp_table);
91 92

    /* rsdp checksum is not for the whole table, but for the first 20 bytes */
93
    g_assert(!acpi_calc_checksum((uint8_t *)rsdp_table, 20));
94 95 96 97 98
}

static void test_acpi_rsdt_table(test_data *data)
{
    AcpiRsdtDescriptorRev1 *rsdt_table = &data->rsdt_table;
99
    uint32_t addr = le32_to_cpu(data->rsdp_table.rsdt_physical_address);
100 101 102
    uint32_t *tables;
    int tables_nr;
    uint8_t checksum;
103
    uint32_t rsdt_table_length;
104 105 106

    /* read the header */
    ACPI_READ_TABLE_HEADER(rsdt_table, addr);
107
    ACPI_ASSERT_CMP(rsdt_table->signature, "RSDT");
108

109 110
    rsdt_table_length = le32_to_cpu(rsdt_table->length);

111
    /* compute the table entries in rsdt */
112
    tables_nr = (rsdt_table_length - sizeof(AcpiRsdtDescriptorRev1)) /
113
                sizeof(uint32_t);
114
    g_assert(tables_nr > 0);
115 116 117 118 119

    /* get the addresses of the tables pointed by rsdt */
    tables = g_new0(uint32_t, tables_nr);
    ACPI_READ_ARRAY_PTR(tables, tables_nr, addr);

120
    checksum = acpi_calc_checksum((uint8_t *)rsdt_table, rsdt_table_length) +
121 122
               acpi_calc_checksum((uint8_t *)tables,
                                  tables_nr * sizeof(uint32_t));
123 124 125 126 127 128 129 130 131
    g_assert(!checksum);

   /* SSDT tables after FADT */
    data->rsdt_tables_addr = tables;
    data->rsdt_tables_nr = tables_nr;
}

static void test_acpi_fadt_table(test_data *data)
{
132
    AcpiFadtDescriptorRev3 *fadt_table = &data->fadt_table;
133 134 135
    uint32_t addr;

    /* FADT table comes first */
136
    addr = le32_to_cpu(data->rsdt_tables_addr[0]);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
    ACPI_READ_TABLE_HEADER(fadt_table, addr);

    ACPI_READ_FIELD(fadt_table->firmware_ctrl, addr);
    ACPI_READ_FIELD(fadt_table->dsdt, addr);
    ACPI_READ_FIELD(fadt_table->model, addr);
    ACPI_READ_FIELD(fadt_table->reserved1, addr);
    ACPI_READ_FIELD(fadt_table->sci_int, addr);
    ACPI_READ_FIELD(fadt_table->smi_cmd, addr);
    ACPI_READ_FIELD(fadt_table->acpi_enable, addr);
    ACPI_READ_FIELD(fadt_table->acpi_disable, addr);
    ACPI_READ_FIELD(fadt_table->S4bios_req, addr);
    ACPI_READ_FIELD(fadt_table->reserved2, addr);
    ACPI_READ_FIELD(fadt_table->pm1a_evt_blk, addr);
    ACPI_READ_FIELD(fadt_table->pm1b_evt_blk, addr);
    ACPI_READ_FIELD(fadt_table->pm1a_cnt_blk, addr);
    ACPI_READ_FIELD(fadt_table->pm1b_cnt_blk, addr);
    ACPI_READ_FIELD(fadt_table->pm2_cnt_blk, addr);
    ACPI_READ_FIELD(fadt_table->pm_tmr_blk, addr);
    ACPI_READ_FIELD(fadt_table->gpe0_blk, addr);
    ACPI_READ_FIELD(fadt_table->gpe1_blk, addr);
    ACPI_READ_FIELD(fadt_table->pm1_evt_len, addr);
    ACPI_READ_FIELD(fadt_table->pm1_cnt_len, addr);
    ACPI_READ_FIELD(fadt_table->pm2_cnt_len, addr);
    ACPI_READ_FIELD(fadt_table->pm_tmr_len, addr);
    ACPI_READ_FIELD(fadt_table->gpe0_blk_len, addr);
    ACPI_READ_FIELD(fadt_table->gpe1_blk_len, addr);
    ACPI_READ_FIELD(fadt_table->gpe1_base, addr);
    ACPI_READ_FIELD(fadt_table->reserved3, addr);
    ACPI_READ_FIELD(fadt_table->plvl2_lat, addr);
    ACPI_READ_FIELD(fadt_table->plvl3_lat, addr);
    ACPI_READ_FIELD(fadt_table->flush_size, addr);
    ACPI_READ_FIELD(fadt_table->flush_stride, addr);
    ACPI_READ_FIELD(fadt_table->duty_offset, addr);
    ACPI_READ_FIELD(fadt_table->duty_width, addr);
    ACPI_READ_FIELD(fadt_table->day_alrm, addr);
    ACPI_READ_FIELD(fadt_table->mon_alrm, addr);
    ACPI_READ_FIELD(fadt_table->century, addr);
174 175
    ACPI_READ_FIELD(fadt_table->boot_flags, addr);
    ACPI_READ_FIELD(fadt_table->reserved, addr);
176
    ACPI_READ_FIELD(fadt_table->flags, addr);
177 178 179 180
    ACPI_READ_GENERIC_ADDRESS(fadt_table->reset_register, addr);
    ACPI_READ_FIELD(fadt_table->reset_value, addr);
    ACPI_READ_FIELD(fadt_table->arm_boot_flags, addr);
    ACPI_READ_FIELD(fadt_table->minor_revision, addr);
181 182
    ACPI_READ_FIELD(fadt_table->x_facs, addr);
    ACPI_READ_FIELD(fadt_table->x_dsdt, addr);
183 184 185 186 187 188 189 190
    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1a_event_block, addr);
    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1b_event_block, addr);
    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1a_control_block, addr);
    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1b_control_block, addr);
    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm2_control_block, addr);
    ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm_timer_block, addr);
    ACPI_READ_GENERIC_ADDRESS(fadt_table->xgpe0_block, addr);
    ACPI_READ_GENERIC_ADDRESS(fadt_table->xgpe1_block, addr);
191

192
    ACPI_ASSERT_CMP(fadt_table->signature, "FACP");
193 194
    g_assert(!acpi_calc_checksum((uint8_t *)fadt_table,
                                 le32_to_cpu(fadt_table->length)));
195 196
}

197 198 199
static void test_acpi_facs_table(test_data *data)
{
    AcpiFacsDescriptorRev1 *facs_table = &data->facs_table;
200
    uint32_t addr = le32_to_cpu(data->fadt_table.firmware_ctrl);
201 202 203 204 205 206 207 208 209

    ACPI_READ_FIELD(facs_table->signature, addr);
    ACPI_READ_FIELD(facs_table->length, addr);
    ACPI_READ_FIELD(facs_table->hardware_signature, addr);
    ACPI_READ_FIELD(facs_table->firmware_waking_vector, addr);
    ACPI_READ_FIELD(facs_table->global_lock, addr);
    ACPI_READ_FIELD(facs_table->flags, addr);
    ACPI_READ_ARRAY(facs_table->resverved3, addr);

210
    ACPI_ASSERT_CMP(facs_table->signature, "FACS");
211 212
}

213 214 215 216 217
/** fetch_table
 *   load ACPI table at @addr into table descriptor @sdt_table
 *   and check that header checksum matches actual one.
 */
static void fetch_table(AcpiSdtTable *sdt_table, uint32_t addr)
218 219 220
{
    uint8_t checksum;

221
    memset(sdt_table, 0, sizeof(*sdt_table));
222 223
    ACPI_READ_TABLE_HEADER(&sdt_table->header, addr);

224 225
    sdt_table->aml_len = le32_to_cpu(sdt_table->header.length)
                         - sizeof(AcpiTableHeader);
226 227 228
    sdt_table->aml = g_malloc0(sdt_table->aml_len);
    ACPI_READ_ARRAY_PTR(sdt_table->aml, sdt_table->aml_len, addr);

229 230 231 232
    checksum = acpi_calc_checksum((uint8_t *)sdt_table,
                                  sizeof(AcpiTableHeader)) +
               acpi_calc_checksum((uint8_t *)sdt_table->aml,
                                  sdt_table->aml_len);
233 234 235 236 237
    g_assert(!checksum);
}

static void test_acpi_dsdt_table(test_data *data)
{
238
    AcpiSdtTable dsdt_table;
239
    uint32_t addr = le32_to_cpu(data->fadt_table.dsdt);
240

241
    fetch_table(&dsdt_table, addr);
242
    ACPI_ASSERT_CMP(dsdt_table.header.signature, "DSDT");
243

244
    /* Since DSDT isn't in RSDT, add DSDT to ASL test tables list manually */
245
    g_array_append_val(data->tables, dsdt_table);
246 247
}

248 249
/* Load all tables and add to test list directly RSDT referenced tables */
static void fetch_rsdt_referenced_tables(test_data *data)
250
{
251
    int tables_nr = data->rsdt_tables_nr - 1; /* fadt is first */
252 253
    int i;

254
    for (i = 0; i < tables_nr; i++) {
255
        AcpiSdtTable ssdt_table;
256
        uint32_t addr;
257

258
        addr = le32_to_cpu(data->rsdt_tables_addr[i + 1]); /* fadt is first */
259
        fetch_table(&ssdt_table, addr);
260 261

        /* Add table to ASL test tables list */
262
        g_array_append_val(data->tables, ssdt_table);
263
    }
264 265
}

266
static void dump_aml_files(test_data *data, bool rebuild)
267 268 269
{
    AcpiSdtTable *sdt;
    GError *error = NULL;
270
    gchar *aml_file = NULL;
271 272 273 274
    gint fd;
    ssize_t ret;
    int i;

275
    for (i = 0; i < data->tables->len; ++i) {
276
        const char *ext = data->variant ? data->variant : "";
277
        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
278 279
        g_assert(sdt->aml);

280
        if (rebuild) {
281
            aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
282
                                       (gchar *)&sdt->header.signature, ext);
283 284 285 286 287 288 289
            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);
290 291 292 293 294 295 296

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

        close(fd);
297

298
        g_free(aml_file);
299 300 301
    }
}

302
static bool compare_signature(AcpiSdtTable *sdt, const char *signature)
303
{
304
   return !memcmp(&sdt->header.signature, signature, 4);
305 306
}

307
static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
308 309 310
{
    AcpiSdtTable *temp;
    GError *error = NULL;
311
    GString *command_line = g_string_new(iasl);
312 313 314 315 316 317 318 319 320 321
    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 */
322
    g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
323 324
    if (compare_signature(sdt, "DSDT") ||
        compare_signature(sdt, "SSDT")) {
325 326
        for (i = 0; i < sdts->len; ++i) {
            temp = &g_array_index(sdts, AcpiSdtTable, i);
327 328
            if (compare_signature(temp, "DSDT") ||
                compare_signature(temp, "SSDT")) {
329 330 331
                g_string_append_printf(command_line, "-e %s ", temp->aml_file);
            }
        }
332 333 334 335
    }
    g_string_append_printf(command_line, "-d %s", sdt->aml_file);

    /* 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 339 340 341 342
    if (ret) {
        ret = g_file_get_contents(sdt->asl_file, (gchar **)&sdt->asl,
                                  &sdt->asl_len, &error);
        g_assert(ret);
        g_assert_no_error(error);
343
        ret = (sdt->asl_len > 0);
344
    }
345 346 347 348

    g_free(out);
    g_free(out_err);
    g_string_free(command_line, true);
349 350

    return !ret;
351 352 353 354
}

#define COMMENT_END "*/"
#define DEF_BLOCK "DefinitionBlock ("
355
#define BLOCK_NAME_END ","
356 357 358 359 360 361 362 363 364

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) {
365 366 367 368 369
        comment += strlen(COMMENT_END);
        while (*comment == '\n') {
            comment++;
        }
        asl = g_string_erase(asl, 0, comment - asl->str);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    }

    /* 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;

390 391
    GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
    for (i = 0; i < data->tables->len; ++i) {
392
        AcpiSdtTable exp_sdt;
393
        gchar *aml_file = NULL;
394
        const char *ext = data->variant ? data->variant : "";
395

396
        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
397 398 399 400

        memset(&exp_sdt, 0, sizeof(exp_sdt));
        exp_sdt.header.signature = sdt->header.signature;

401 402
try_again:
        aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
403
                                   (gchar *)&sdt->header.signature, ext);
404 405 406 407 408 409 410
        if (getenv("V")) {
            fprintf(stderr, "\nLooking for expected file '%s'\n", aml_file);
        }
        if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) {
            exp_sdt.aml_file = aml_file;
        } else if (*ext != '\0') {
            /* try fallback to generic (extention less) expected file */
411
            ext = "";
412
            g_free(aml_file);
413 414
            goto try_again;
        }
415 416 417 418
        g_assert(exp_sdt.aml_file);
        if (getenv("V")) {
            fprintf(stderr, "\nUsing expected file '%s'\n", aml_file);
        }
419 420 421 422 423 424 425
        ret = g_file_get_contents(aml_file, &exp_sdt.aml,
                                  &exp_sdt.aml_len, &error);
        g_assert(ret);
        g_assert_no_error(error);
        g_assert(exp_sdt.aml);
        g_assert(exp_sdt.aml_len);

426
        g_array_append_val(exp_tables, exp_sdt);
427 428
    }

429
    return exp_tables;
430 431
}

432
/* test the list of tables in @data->tables against reference tables */
433 434 435 436 437
static void test_acpi_asl(test_data *data)
{
    int i;
    AcpiSdtTable *sdt, *exp_sdt;
    test_data exp_data;
438
    gboolean exp_err, err;
439 440

    memset(&exp_data, 0, sizeof(exp_data));
441
    exp_data.tables = load_expected_aml(data);
442
    dump_aml_files(data, false);
443
    for (i = 0; i < data->tables->len; ++i) {
444 445
        GString *asl, *exp_asl;

446 447
        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
        exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
448

449
        err = load_asl(data->tables, sdt);
450 451
        asl = normalize_asl(sdt->asl);

452
        exp_err = load_asl(exp_data.tables, exp_sdt);
453 454
        exp_asl = normalize_asl(exp_sdt->asl);

455 456 457
        /* TODO: check for warnings */
        g_assert(!err || exp_err);

458
        if (g_strcmp0(asl->str, exp_asl->str)) {
459 460 461 462 463 464 465 466 467 468 469 470 471
            if (exp_err) {
                fprintf(stderr,
                        "Warning! iasl couldn't parse the expected aml\n");
            } else {
                uint32_t signature = cpu_to_le32(exp_sdt->header.signature);
                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",
                        (gchar *)&signature,
                        sdt->asl_file, sdt->aml_file,
                        exp_sdt->asl_file, exp_sdt->aml_file);
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
                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.");
                    }
                }
488
          }
489
        }
490 491 492 493 494
        g_string_free(asl, true);
        g_string_free(exp_asl, true);
    }

    free_test_data(&exp_data);
495 496
}

497
static bool smbios_ep_table_ok(test_data *data)
G
Gabriel L. Somlo 已提交
498
{
W
Wei Huang 已提交
499
    struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
G
Gabriel L. Somlo 已提交
500 501 502
    uint32_t addr = data->smbios_ep_addr;

    ACPI_READ_ARRAY(ep_table->anchor_string, addr);
503 504 505
    if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
        return false;
    }
G
Gabriel L. Somlo 已提交
506 507 508 509 510 511 512 513
    ACPI_READ_FIELD(ep_table->checksum, addr);
    ACPI_READ_FIELD(ep_table->length, addr);
    ACPI_READ_FIELD(ep_table->smbios_major_version, addr);
    ACPI_READ_FIELD(ep_table->smbios_minor_version, addr);
    ACPI_READ_FIELD(ep_table->max_structure_size, addr);
    ACPI_READ_FIELD(ep_table->entry_point_revision, addr);
    ACPI_READ_ARRAY(ep_table->formatted_area, addr);
    ACPI_READ_ARRAY(ep_table->intermediate_anchor_string, addr);
514 515 516
    if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
        return false;
    }
G
Gabriel L. Somlo 已提交
517 518
    ACPI_READ_FIELD(ep_table->intermediate_checksum, addr);
    ACPI_READ_FIELD(ep_table->structure_table_length, addr);
519 520 521
    if (ep_table->structure_table_length == 0) {
        return false;
    }
G
Gabriel L. Somlo 已提交
522 523
    ACPI_READ_FIELD(ep_table->structure_table_address, addr);
    ACPI_READ_FIELD(ep_table->number_of_structures, addr);
524 525 526
    if (ep_table->number_of_structures == 0) {
        return false;
    }
G
Gabriel L. Somlo 已提交
527
    ACPI_READ_FIELD(ep_table->smbios_bcd_revision, addr);
528 529 530
    if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
        acpi_calc_checksum((uint8_t *)ep_table + 0x10,
                           sizeof *ep_table - 0x10)) {
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
        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) {
            sig[i] = readb(off + i);
        }

        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 已提交
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
}

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 已提交
580
    struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
581
    uint32_t addr = le32_to_cpu(ep_table->structure_table_address);
G
Gabriel L. Somlo 已提交
582 583 584 585
    int i, len, max_len = 0;
    uint8_t type, prv, crt;

    /* walk the smbios tables */
586
    for (i = 0; i < le16_to_cpu(ep_table->number_of_structures); i++) {
G
Gabriel L. Somlo 已提交
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

        /* grab type and formatted area length from struct header */
        type = readb(addr);
        g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
        len = readb(addr + 1);

        /* 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;
            crt = readb(addr + len);
            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 */
618 619 620
    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 已提交
621 622

    /* required struct types must all be present */
623 624
    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 已提交
625 626 627
    }
}

628
static void test_acpi_one(const char *params, test_data *data)
629 630 631
{
    char *args;

632 633 634
    /* 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 "
635
                           "-drive id=hd0,if=none,file=%s,format=raw "
636
                           "-device ide-hd,drive=hd0 ",
637
                           data->machine, "kvm:tcg",
638
                           params ? params : "", disk);
639

640 641
    qtest_start(args);

642
    boot_sector_test();
643

644
    data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
645 646 647 648
    test_acpi_rsdp_address(data);
    test_acpi_rsdp_table(data);
    test_acpi_rsdt_table(data);
    test_acpi_fadt_table(data);
649
    test_acpi_facs_table(data);
650
    test_acpi_dsdt_table(data);
651
    fetch_rsdt_referenced_tables(data);
652

653
    if (iasl) {
654 655 656 657 658
        if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
            dump_aml_files(data, true);
        } else {
            test_acpi_asl(data);
        }
659 660
    }

661
    test_smbios_entry_point(data);
G
Gabriel L. Somlo 已提交
662 663
    test_smbios_structs(data);

664 665 666 667
    qtest_quit(global_qtest);
    g_free(args);
}

668 669 670 671
static uint8_t base_required_struct_types[] = {
    0, 1, 3, 4, 16, 17, 19, 32, 127
};

672
static void test_acpi_piix4_tcg(void)
673
{
674 675
    test_data data;

676 677 678
    /* Supplying -machine accel argument overrides the default (qtest).
     * This is to make guest actually run.
     */
679 680
    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_PC;
681 682
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
683
    test_acpi_one(NULL, &data);
684
    free_test_data(&data);
685 686
}

687 688 689 690 691 692 693
static void test_acpi_piix4_tcg_bridge(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_PC;
    data.variant = ".bridge";
694 695
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
696
    test_acpi_one("-device pci-bridge,chassis_nr=1", &data);
697 698 699
    free_test_data(&data);
}

700 701 702
static void test_acpi_q35_tcg(void)
{
    test_data data;
703

704 705
    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_Q35;
706 707
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
708
    test_acpi_one(NULL, &data);
709
    free_test_data(&data);
710 711
}

712 713 714 715 716 717 718
static void test_acpi_q35_tcg_bridge(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_Q35;
    data.variant = ".bridge";
719 720
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
721
    test_acpi_one("-device pci-bridge,chassis_nr=1",
722 723 724 725
                  &data);
    free_test_data(&data);
}

726 727 728 729 730 731 732
static void test_acpi_piix4_tcg_cphp(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_PC;
    data.variant = ".cphp";
733
    test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
734 735
                  " -numa node -numa node"
                  " -numa dist,src=0,dst=1,val=21",
736 737 738 739 740 741 742 743 744 745 746
                  &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";
747
    test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
748 749
                  " -numa node -numa node"
                  " -numa dist,src=0,dst=1,val=21",
750 751 752 753
                  &data);
    free_test_data(&data);
}

754 755 756 757 758 759 760 761 762 763 764 765 766
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);
767
    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
                  " -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);
785
    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
786 787 788 789 790
                  " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
                  &data);
    free_test_data(&data);
}

791 792 793 794 795 796 797
static void test_acpi_q35_tcg_memhp(void)
{
    test_data data;

    memset(&data, 0, sizeof(data));
    data.machine = MACHINE_Q35;
    data.variant = ".memhp";
798 799 800 801
    test_acpi_one(" -m 128,slots=3,maxmem=1G"
                  " -numa node -numa node"
                  " -numa dist,src=0,dst=1,val=21",
                  &data);
802 803 804 805 806 807 808 809 810 811
    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";
812 813 814 815
    test_acpi_one(" -m 128,slots=3,maxmem=1G"
                  " -numa node -numa node"
                  " -numa dist,src=0,dst=1,val=21",
                  &data);
816 817 818
    free_test_data(&data);
}

819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
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);
}

841 842 843
int main(int argc, char *argv[])
{
    const char *arch = qtest_get_arch();
844
    int ret;
845

846 847 848
    ret = boot_sector_init(disk);
    if(ret)
        return ret;
849 850 851 852

    g_test_init(&argc, &argv, NULL);

    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
853 854 855 856 857 858 859 860
        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);
        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);
861 862
        qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
        qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
863 864
        qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
        qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
865
    }
866
    ret = g_test_run();
867
    boot_sector_cleanup(disk);
868
    return ret;
869
}