2 * Boot order test cases.
4 * Copyright (c) 2013 Red Hat Inc.
7 * Michael S. Tsirkin <mst@redhat.com>,
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.
14 * How to add or update the tests or commit changes that affect ACPI tables:
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
20 * Contributor or ACPI Maintainer (steps 4-7 need to be redone to resolve conflicts
21 * in binary commit created in step 6):
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:
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
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
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
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.
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
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"
66 #include "hw/acpi/tpm.h"
69 #define MACHINE_PC "pc"
70 #define MACHINE_Q35 "q35"
72 #define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"
82 const uint64_t ram_start
;
83 const uint64_t scan_len
;
85 uint8_t rsdp_table
[36 /* ACPI 2.0+ RSDP size */];
87 uint32_t smbios_ep_addr
;
88 struct smbios_21_entry_point smbios_ep_table
;
89 uint16_t smbios_cpu_max_speed
;
90 uint16_t smbios_cpu_curr_speed
;
91 uint8_t *required_struct_types
;
92 int required_struct_types_len
;
96 static char disk
[] = "tests/acpi-test-disk-XXXXXX";
97 static const char *data_dir
= "tests/data/acpi";
99 static const char *iasl
= CONFIG_IASL
;
101 static const char *iasl
;
104 static bool compare_signature(const AcpiSdtTable
*sdt
, const char *signature
)
106 return !memcmp(sdt
->aml
, signature
, 4);
109 static void cleanup_table_descriptor(AcpiSdtTable
*table
)
112 if (table
->aml_file
&&
113 !table
->tmp_files_retain
&&
114 g_strstr_len(table
->aml_file
, -1, "aml-")) {
115 unlink(table
->aml_file
);
117 g_free(table
->aml_file
);
119 if (table
->asl_file
&&
120 !table
->tmp_files_retain
) {
121 unlink(table
->asl_file
);
123 g_free(table
->asl_file
);
126 static void free_test_data(test_data
*data
)
133 for (i
= 0; i
< data
->tables
->len
; ++i
) {
134 cleanup_table_descriptor(&g_array_index(data
->tables
, AcpiSdtTable
, i
));
137 g_array_free(data
->tables
, true);
140 static void test_acpi_rsdp_table(test_data
*data
)
142 uint8_t *rsdp_table
= data
->rsdp_table
;
144 acpi_fetch_rsdp_table(data
->qts
, data
->rsdp_addr
, rsdp_table
);
146 switch (rsdp_table
[15 /* Revision offset */]) {
147 case 0: /* ACPI 1.0 RSDP */
148 /* With rev 1, checksum is only for the first 20 bytes */
149 g_assert(!acpi_calc_checksum(rsdp_table
, 20));
151 case 2: /* ACPI 2.0+ RSDP */
152 /* With revision 2, we have 2 checksums */
153 g_assert(!acpi_calc_checksum(rsdp_table
, 20));
154 g_assert(!acpi_calc_checksum(rsdp_table
, 36));
157 g_assert_not_reached();
161 static void test_acpi_rxsdt_table(test_data
*data
)
163 const char *sig
= "RSDT";
164 AcpiSdtTable rsdt
= {};
166 int addr_off
= 16 /* RsdtAddress */;
169 if (data
->rsdp_table
[15 /* Revision offset */] != 0) {
170 addr_off
= 24 /* XsdtAddress */;
174 /* read [RX]SDT table */
175 acpi_fetch_table(data
->qts
, &rsdt
.aml
, &rsdt
.aml_len
,
176 &data
->rsdp_table
[addr_off
], entry_size
, sig
, true);
178 /* Load all tables and add to test list directly RSDT referenced tables */
179 ACPI_FOREACH_RSDT_ENTRY(rsdt
.aml
, rsdt
.aml_len
, ent
, entry_size
) {
180 AcpiSdtTable ssdt_table
= {};
182 acpi_fetch_table(data
->qts
, &ssdt_table
.aml
, &ssdt_table
.aml_len
, ent
,
183 entry_size
, NULL
, true);
184 /* Add table to ASL test tables list */
185 g_array_append_val(data
->tables
, ssdt_table
);
187 cleanup_table_descriptor(&rsdt
);
190 static void test_acpi_fadt_table(test_data
*data
)
192 /* FADT table is 1st */
193 AcpiSdtTable table
= g_array_index(data
->tables
, typeof(table
), 0);
194 uint8_t *fadt_aml
= table
.aml
;
195 uint32_t fadt_len
= table
.aml_len
;
197 int dsdt_offset
= 40 /* DSDT */;
198 int dsdt_entry_size
= 4;
200 g_assert(compare_signature(&table
, "FACP"));
202 /* Since DSDT/FACS isn't in RSDT, add them to ASL test list manually */
203 memcpy(&val
, fadt_aml
+ 112 /* Flags */, 4);
204 val
= le32_to_cpu(val
);
205 if (!(val
& 1UL << 20 /* HW_REDUCED_ACPI */)) {
206 acpi_fetch_table(data
->qts
, &table
.aml
, &table
.aml_len
,
207 fadt_aml
+ 36 /* FIRMWARE_CTRL */, 4, "FACS", false);
208 g_array_append_val(data
->tables
, table
);
211 memcpy(&val
, fadt_aml
+ dsdt_offset
, 4);
212 val
= le32_to_cpu(val
);
214 dsdt_offset
= 140 /* X_DSDT */;
217 acpi_fetch_table(data
->qts
, &table
.aml
, &table
.aml_len
,
218 fadt_aml
+ dsdt_offset
, dsdt_entry_size
, "DSDT", true);
219 g_array_append_val(data
->tables
, table
);
221 memset(fadt_aml
+ 36, 0, 4); /* sanitize FIRMWARE_CTRL ptr */
222 memset(fadt_aml
+ 40, 0, 4); /* sanitize DSDT ptr */
223 if (fadt_aml
[8 /* FADT Major Version */] >= 3) {
224 memset(fadt_aml
+ 132, 0, 8); /* sanitize X_FIRMWARE_CTRL ptr */
225 memset(fadt_aml
+ 140, 0, 8); /* sanitize X_DSDT ptr */
228 /* update checksum */
229 fadt_aml
[9 /* Checksum */] = 0;
230 fadt_aml
[9 /* Checksum */] -= acpi_calc_checksum(fadt_aml
, fadt_len
);
233 static void dump_aml_files(test_data
*data
, bool rebuild
)
236 GError
*error
= NULL
;
237 gchar
*aml_file
= NULL
;
242 for (i
= 0; i
< data
->tables
->len
; ++i
) {
243 const char *ext
= data
->variant
? data
->variant
: "";
244 sdt
= &g_array_index(data
->tables
, AcpiSdtTable
, i
);
248 aml_file
= g_strdup_printf("%s/%s/%.4s%s", data_dir
, data
->machine
,
250 fd
= g_open(aml_file
, O_WRONLY
|O_TRUNC
|O_CREAT
,
251 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
);
257 fd
= g_file_open_tmp("aml-XXXXXX", &sdt
->aml_file
, &error
);
258 g_assert_no_error(error
);
261 ret
= qemu_write_full(fd
, sdt
->aml
, sdt
->aml_len
);
262 g_assert(ret
== sdt
->aml_len
);
270 static bool load_asl(GArray
*sdts
, AcpiSdtTable
*sdt
)
273 GError
*error
= NULL
;
274 GString
*command_line
= g_string_new(iasl
);
276 gchar
*out
, *out_err
;
280 fd
= g_file_open_tmp("asl-XXXXXX.dsl", &sdt
->asl_file
, &error
);
281 g_assert_no_error(error
);
284 /* build command line */
285 g_string_append_printf(command_line
, " -p %s ", sdt
->asl_file
);
286 if (compare_signature(sdt
, "DSDT") ||
287 compare_signature(sdt
, "SSDT")) {
288 for (i
= 0; i
< sdts
->len
; ++i
) {
289 temp
= &g_array_index(sdts
, AcpiSdtTable
, i
);
290 if (compare_signature(temp
, "DSDT") ||
291 compare_signature(temp
, "SSDT")) {
292 g_string_append_printf(command_line
, "-e %s ", temp
->aml_file
);
296 g_string_append_printf(command_line
, "-d %s", sdt
->aml_file
);
298 /* pass 'out' and 'out_err' in order to be redirected */
299 ret
= g_spawn_command_line_sync(command_line
->str
, &out
, &out_err
, NULL
, &error
);
300 g_assert_no_error(error
);
302 ret
= g_file_get_contents(sdt
->asl_file
, &sdt
->asl
,
303 &sdt
->asl_len
, &error
);
305 g_assert_no_error(error
);
306 ret
= (sdt
->asl_len
> 0);
311 g_string_free(command_line
, true);
316 #define COMMENT_END "*/"
317 #define DEF_BLOCK "DefinitionBlock ("
318 #define BLOCK_NAME_END ","
320 static GString
*normalize_asl(gchar
*asl_code
)
322 GString
*asl
= g_string_new(asl_code
);
323 gchar
*comment
, *block_name
;
325 /* strip comments (different generation days) */
326 comment
= g_strstr_len(asl
->str
, asl
->len
, COMMENT_END
);
328 comment
+= strlen(COMMENT_END
);
329 while (*comment
== '\n') {
332 asl
= g_string_erase(asl
, 0, comment
- asl
->str
);
335 /* strip def block name (it has file path in it) */
336 if (g_str_has_prefix(asl
->str
, DEF_BLOCK
)) {
337 block_name
= g_strstr_len(asl
->str
, asl
->len
, BLOCK_NAME_END
);
338 g_assert(block_name
);
339 asl
= g_string_erase(asl
, 0,
340 block_name
+ sizeof(BLOCK_NAME_END
) - asl
->str
);
346 static GArray
*load_expected_aml(test_data
*data
)
350 GError
*error
= NULL
;
354 GArray
*exp_tables
= g_array_new(false, true, sizeof(AcpiSdtTable
));
358 for (i
= 0; i
< data
->tables
->len
; ++i
) {
359 AcpiSdtTable exp_sdt
;
360 gchar
*aml_file
= NULL
;
361 const char *ext
= data
->variant
? data
->variant
: "";
363 sdt
= &g_array_index(data
->tables
, AcpiSdtTable
, i
);
365 memset(&exp_sdt
, 0, sizeof(exp_sdt
));
368 aml_file
= g_strdup_printf("%s/%s/%.4s%s", data_dir
, data
->machine
,
371 fprintf(stderr
, "Looking for expected file '%s'\n", aml_file
);
373 if (g_file_test(aml_file
, G_FILE_TEST_EXISTS
)) {
374 exp_sdt
.aml_file
= aml_file
;
375 } else if (*ext
!= '\0') {
376 /* try fallback to generic (extension less) expected file */
381 g_assert(exp_sdt
.aml_file
);
383 fprintf(stderr
, "Using expected file '%s'\n", aml_file
);
385 ret
= g_file_get_contents(aml_file
, (gchar
**)&exp_sdt
.aml
,
387 exp_sdt
.aml_len
= aml_len
;
389 g_assert_no_error(error
);
390 g_assert(exp_sdt
.aml
);
391 if (!exp_sdt
.aml_len
) {
392 fprintf(stderr
, "Warning! zero length expected file '%s'\n",
396 g_array_append_val(exp_tables
, exp_sdt
);
402 static bool test_acpi_find_diff_allowed(AcpiSdtTable
*sdt
)
404 const gchar
*allowed_diff_file
[] = {
405 #include "bios-tables-test-allowed-diff.h"
410 for (f
= allowed_diff_file
; *f
; ++f
) {
411 if (!g_strcmp0(sdt
->aml_file
, *f
)) {
418 /* test the list of tables in @data->tables against reference tables */
419 static void test_acpi_asl(test_data
*data
)
422 AcpiSdtTable
*sdt
, *exp_sdt
;
424 gboolean exp_err
, err
, all_tables_match
= true;
426 memset(&exp_data
, 0, sizeof(exp_data
));
427 exp_data
.tables
= load_expected_aml(data
);
428 dump_aml_files(data
, false);
429 for (i
= 0; i
< data
->tables
->len
; ++i
) {
430 GString
*asl
, *exp_asl
;
432 sdt
= &g_array_index(data
->tables
, AcpiSdtTable
, i
);
433 exp_sdt
= &g_array_index(exp_data
.tables
, AcpiSdtTable
, i
);
435 if (sdt
->aml_len
== exp_sdt
->aml_len
&&
436 !memcmp(sdt
->aml
, exp_sdt
->aml
, sdt
->aml_len
)) {
437 /* Identical table binaries: no need to disassemble. */
442 "acpi-test: Warning! %.4s binary file mismatch. "
443 "Actual [aml:%s], Expected [aml:%s].\n"
444 "See source file tests/qtest/bios-tables-test.c "
445 "for instructions on how to update expected files.\n",
446 exp_sdt
->aml
, sdt
->aml_file
, exp_sdt
->aml_file
);
448 all_tables_match
= all_tables_match
&&
449 test_acpi_find_diff_allowed(exp_sdt
);
452 * don't try to decompile if IASL isn't present, in this case user
453 * will just 'get binary file mismatch' warnings and test failure
459 err
= load_asl(data
->tables
, sdt
);
460 asl
= normalize_asl(sdt
->asl
);
462 exp_err
= load_asl(exp_data
.tables
, exp_sdt
);
463 exp_asl
= normalize_asl(exp_sdt
->asl
);
465 /* TODO: check for warnings */
466 g_assert(!err
|| exp_err
);
468 if (g_strcmp0(asl
->str
, exp_asl
->str
)) {
469 sdt
->tmp_files_retain
= true;
472 "Warning! iasl couldn't parse the expected aml\n");
474 exp_sdt
->tmp_files_retain
= true;
476 "acpi-test: Warning! %.4s mismatch. "
477 "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
478 exp_sdt
->aml
, sdt
->asl_file
, sdt
->aml_file
,
479 exp_sdt
->asl_file
, exp_sdt
->aml_file
);
482 const char *diff_env
= getenv("DIFF");
483 const char *diff_cmd
= diff_env
? diff_env
: "diff -U 16";
484 char *diff
= g_strdup_printf("%s %s %s", diff_cmd
,
485 exp_sdt
->asl_file
, sdt
->asl_file
);
486 int out
= dup(STDOUT_FILENO
);
487 int ret G_GNUC_UNUSED
;
489 dup2(STDERR_FILENO
, STDOUT_FILENO
);
491 dup2(out
, STDOUT_FILENO
);
497 g_string_free(asl
, true);
498 g_string_free(exp_asl
, true);
500 if (!iasl
&& !all_tables_match
) {
501 fprintf(stderr
, "to see ASL diff between mismatched files install IASL,"
502 " rebuild QEMU from scratch and re-run tests with V=1"
503 " environment variable set");
505 g_assert(all_tables_match
);
507 free_test_data(&exp_data
);
510 static bool smbios_ep_table_ok(test_data
*data
)
512 struct smbios_21_entry_point
*ep_table
= &data
->smbios_ep_table
;
513 uint32_t addr
= data
->smbios_ep_addr
;
515 qtest_memread(data
->qts
, addr
, ep_table
, sizeof(*ep_table
));
516 if (memcmp(ep_table
->anchor_string
, "_SM_", 4)) {
519 if (memcmp(ep_table
->intermediate_anchor_string
, "_DMI_", 5)) {
522 if (ep_table
->structure_table_length
== 0) {
525 if (ep_table
->number_of_structures
== 0) {
528 if (acpi_calc_checksum((uint8_t *)ep_table
, sizeof *ep_table
) ||
529 acpi_calc_checksum((uint8_t *)ep_table
+ 0x10,
530 sizeof *ep_table
- 0x10)) {
536 static void test_smbios_entry_point(test_data
*data
)
540 /* find smbios entry point structure */
541 for (off
= 0xf0000; off
< 0x100000; off
+= 0x10) {
542 uint8_t sig
[] = "_SM_";
545 for (i
= 0; i
< sizeof sig
- 1; ++i
) {
546 sig
[i
] = qtest_readb(data
->qts
, off
+ i
);
549 if (!memcmp(sig
, "_SM_", sizeof sig
)) {
550 /* signature match, but is this a valid entry point? */
551 data
->smbios_ep_addr
= off
;
552 if (smbios_ep_table_ok(data
)) {
558 g_assert_cmphex(off
, <, 0x100000);
561 static inline bool smbios_single_instance(uint8_t type
)
577 static bool smbios_cpu_test(test_data
*data
, uint32_t addr
)
579 uint16_t expect_speed
[2];
584 /* Check CPU speed for backward compatibility */
585 offset
[0] = offsetof(struct smbios_type_4
, max_speed
);
586 offset
[1] = offsetof(struct smbios_type_4
, current_speed
);
587 expect_speed
[0] = data
->smbios_cpu_max_speed
? : 2000;
588 expect_speed
[1] = data
->smbios_cpu_curr_speed
? : 2000;
590 for (i
= 0; i
< 2; i
++) {
591 real
= qtest_readw(data
->qts
, addr
+ offset
[i
]);
592 if (real
!= expect_speed
[i
]) {
593 fprintf(stderr
, "Unexpected SMBIOS CPU speed: real %u expect %u\n",
594 real
, expect_speed
[i
]);
602 static void test_smbios_structs(test_data
*data
)
604 DECLARE_BITMAP(struct_bitmap
, SMBIOS_MAX_TYPE
+1) = { 0 };
605 struct smbios_21_entry_point
*ep_table
= &data
->smbios_ep_table
;
606 uint32_t addr
= le32_to_cpu(ep_table
->structure_table_address
);
607 int i
, len
, max_len
= 0;
608 uint8_t type
, prv
, crt
;
610 /* walk the smbios tables */
611 for (i
= 0; i
< le16_to_cpu(ep_table
->number_of_structures
); i
++) {
613 /* grab type and formatted area length from struct header */
614 type
= qtest_readb(data
->qts
, addr
);
615 g_assert_cmpuint(type
, <=, SMBIOS_MAX_TYPE
);
616 len
= qtest_readb(data
->qts
, addr
+ 1);
618 /* single-instance structs must not have been encountered before */
619 if (smbios_single_instance(type
)) {
620 g_assert(!test_bit(type
, struct_bitmap
));
622 set_bit(type
, struct_bitmap
);
625 g_assert(smbios_cpu_test(data
, addr
));
628 /* seek to end of unformatted string area of this struct ("\0\0") */
632 crt
= qtest_readb(data
->qts
, addr
+ len
);
636 /* keep track of max. struct size */
639 g_assert_cmpuint(max_len
, <=, ep_table
->max_structure_size
);
642 /* start of next structure */
646 /* total table length and max struct size must match entry point values */
647 g_assert_cmpuint(le16_to_cpu(ep_table
->structure_table_length
), ==,
648 addr
- le32_to_cpu(ep_table
->structure_table_address
));
649 g_assert_cmpuint(le16_to_cpu(ep_table
->max_structure_size
), ==, max_len
);
651 /* required struct types must all be present */
652 for (i
= 0; i
< data
->required_struct_types_len
; i
++) {
653 g_assert(test_bit(data
->required_struct_types
[i
], struct_bitmap
));
657 static void test_acpi_one(const char *params
, test_data
*data
)
660 bool use_uefi
= data
->uefi_fl1
&& data
->uefi_fl2
;
663 if (data
->tcg_only
) {
664 g_test_skip("TCG disabled, skipping ACPI tcg_only test");
667 #endif /* CONFIG_TCG */
671 * TODO: convert '-drive if=pflash' to new syntax (see e33763be7cd3)
672 * when arm/virt boad starts to support it.
674 args
= g_strdup_printf("-machine %s %s -accel tcg -nodefaults -nographic "
675 "-drive if=pflash,format=raw,file=%s,readonly "
676 "-drive if=pflash,format=raw,file=%s,snapshot=on -cdrom %s %s",
677 data
->machine
, data
->tcg_only
? "" : "-accel kvm",
678 data
->uefi_fl1
, data
->uefi_fl2
, data
->cd
, params
? params
: "");
681 args
= g_strdup_printf("-machine %s %s -accel tcg "
682 "-net none -display none %s "
683 "-drive id=hd0,if=none,file=%s,format=raw "
684 "-device %s,drive=hd0 ",
685 data
->machine
, data
->tcg_only
? "" : "-accel kvm",
686 params
? params
: "", disk
,
687 data
->blkdev
?: "ide-hd");
690 data
->qts
= qtest_init(args
);
693 g_assert(data
->scan_len
);
694 data
->rsdp_addr
= acpi_find_rsdp_address_uefi(data
->qts
,
695 data
->ram_start
, data
->scan_len
);
697 boot_sector_test(data
->qts
);
698 data
->rsdp_addr
= acpi_find_rsdp_address(data
->qts
);
699 g_assert_cmphex(data
->rsdp_addr
, <, 0x100000);
702 data
->tables
= g_array_new(false, true, sizeof(AcpiSdtTable
));
703 test_acpi_rsdp_table(data
);
704 test_acpi_rxsdt_table(data
);
705 test_acpi_fadt_table(data
);
707 if (getenv(ACPI_REBUILD_EXPECTED_AML
)) {
708 dump_aml_files(data
, true);
714 * TODO: make SMBIOS tests work with UEFI firmware,
715 * Bug on uefi-test-tools to provide entry point:
716 * https://bugs.launchpad.net/qemu/+bug/1821884
719 test_smbios_entry_point(data
);
720 test_smbios_structs(data
);
723 qtest_quit(data
->qts
);
727 static uint8_t base_required_struct_types
[] = {
728 0, 1, 3, 4, 16, 17, 19, 32, 127
731 static void test_acpi_piix4_tcg(void)
735 /* Supplying -machine accel argument overrides the default (qtest).
736 * This is to make guest actually run.
738 memset(&data
, 0, sizeof(data
));
739 data
.machine
= MACHINE_PC
;
740 data
.required_struct_types
= base_required_struct_types
;
741 data
.required_struct_types_len
= ARRAY_SIZE(base_required_struct_types
);
742 test_acpi_one(NULL
, &data
);
743 free_test_data(&data
);
746 static void test_acpi_piix4_tcg_bridge(void)
750 memset(&data
, 0, sizeof(data
));
751 data
.machine
= MACHINE_PC
;
752 data
.variant
= ".bridge";
753 data
.required_struct_types
= base_required_struct_types
;
754 data
.required_struct_types_len
= ARRAY_SIZE(base_required_struct_types
);
755 test_acpi_one("-device pci-bridge,chassis_nr=1", &data
);
756 free_test_data(&data
);
759 static void test_acpi_piix4_no_root_hotplug(void)
763 memset(&data
, 0, sizeof(data
));
764 data
.machine
= MACHINE_PC
;
765 data
.variant
= ".roothp";
766 data
.required_struct_types
= base_required_struct_types
;
767 data
.required_struct_types_len
= ARRAY_SIZE(base_required_struct_types
);
768 test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
769 "-device pci-bridge,chassis_nr=1", &data
);
770 free_test_data(&data
);
773 static void test_acpi_piix4_no_bridge_hotplug(void)
777 memset(&data
, 0, sizeof(data
));
778 data
.machine
= MACHINE_PC
;
779 data
.variant
= ".hpbridge";
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("-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
783 "-device pci-bridge,chassis_nr=1", &data
);
784 free_test_data(&data
);
787 static void test_acpi_piix4_no_acpi_pci_hotplug(void)
791 memset(&data
, 0, sizeof(data
));
792 data
.machine
= MACHINE_PC
;
793 data
.variant
= ".hpbrroot";
794 data
.required_struct_types
= base_required_struct_types
;
795 data
.required_struct_types_len
= ARRAY_SIZE(base_required_struct_types
);
796 test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
797 "-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
798 "-device pci-bridge,chassis_nr=1", &data
);
799 free_test_data(&data
);
802 static void test_acpi_q35_tcg(void)
806 memset(&data
, 0, sizeof(data
));
807 data
.machine
= MACHINE_Q35
;
808 data
.required_struct_types
= base_required_struct_types
;
809 data
.required_struct_types_len
= ARRAY_SIZE(base_required_struct_types
);
810 test_acpi_one(NULL
, &data
);
811 free_test_data(&data
);
813 data
.smbios_cpu_max_speed
= 3000;
814 data
.smbios_cpu_curr_speed
= 2600;
815 test_acpi_one("-smbios type=4,max-speed=3000,current-speed=2600", &data
);
816 free_test_data(&data
);
819 static void test_acpi_q35_tcg_bridge(void)
823 memset(&data
, 0, sizeof(data
));
824 data
.machine
= MACHINE_Q35
;
825 data
.variant
= ".bridge";
826 data
.required_struct_types
= base_required_struct_types
;
827 data
.required_struct_types_len
= ARRAY_SIZE(base_required_struct_types
);
828 test_acpi_one("-device pci-bridge,chassis_nr=1",
830 free_test_data(&data
);
833 static void test_acpi_q35_tcg_mmio64(void)
836 .machine
= MACHINE_Q35
,
837 .variant
= ".mmio64",
838 .required_struct_types
= base_required_struct_types
,
839 .required_struct_types_len
= ARRAY_SIZE(base_required_struct_types
)
842 test_acpi_one("-m 128M,slots=1,maxmem=2G "
843 "-object memory-backend-ram,id=ram0,size=128M "
844 "-numa node,memdev=ram0 "
845 "-device pci-testdev,membar=2G",
847 free_test_data(&data
);
850 static void test_acpi_piix4_tcg_cphp(void)
854 memset(&data
, 0, sizeof(data
));
855 data
.machine
= MACHINE_PC
;
856 data
.variant
= ".cphp";
857 test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
858 " -object memory-backend-ram,id=ram0,size=64M"
859 " -object memory-backend-ram,id=ram1,size=64M"
860 " -numa node,memdev=ram0 -numa node,memdev=ram1"
861 " -numa dist,src=0,dst=1,val=21",
863 free_test_data(&data
);
866 static void test_acpi_q35_tcg_cphp(void)
870 memset(&data
, 0, sizeof(data
));
871 data
.machine
= MACHINE_Q35
;
872 data
.variant
= ".cphp";
873 test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
874 " -object memory-backend-ram,id=ram0,size=64M"
875 " -object memory-backend-ram,id=ram1,size=64M"
876 " -numa node,memdev=ram0 -numa node,memdev=ram1"
877 " -numa dist,src=0,dst=1,val=21",
879 free_test_data(&data
);
882 static uint8_t ipmi_required_struct_types
[] = {
883 0, 1, 3, 4, 16, 17, 19, 32, 38, 127
886 static void test_acpi_q35_tcg_ipmi(void)
890 memset(&data
, 0, sizeof(data
));
891 data
.machine
= MACHINE_Q35
;
892 data
.variant
= ".ipmibt";
893 data
.required_struct_types
= ipmi_required_struct_types
;
894 data
.required_struct_types_len
= ARRAY_SIZE(ipmi_required_struct_types
);
895 test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
896 " -device isa-ipmi-bt,bmc=bmc0",
898 free_test_data(&data
);
901 static void test_acpi_piix4_tcg_ipmi(void)
905 /* Supplying -machine accel argument overrides the default (qtest).
906 * This is to make guest actually run.
908 memset(&data
, 0, sizeof(data
));
909 data
.machine
= MACHINE_PC
;
910 data
.variant
= ".ipmikcs";
911 data
.required_struct_types
= ipmi_required_struct_types
;
912 data
.required_struct_types_len
= ARRAY_SIZE(ipmi_required_struct_types
);
913 test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
914 " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
916 free_test_data(&data
);
919 static void test_acpi_q35_tcg_memhp(void)
923 memset(&data
, 0, sizeof(data
));
924 data
.machine
= MACHINE_Q35
;
925 data
.variant
= ".memhp";
926 test_acpi_one(" -m 128,slots=3,maxmem=1G"
927 " -object memory-backend-ram,id=ram0,size=64M"
928 " -object memory-backend-ram,id=ram1,size=64M"
929 " -numa node,memdev=ram0 -numa node,memdev=ram1"
930 " -numa dist,src=0,dst=1,val=21",
932 free_test_data(&data
);
935 static void test_acpi_piix4_tcg_memhp(void)
939 memset(&data
, 0, sizeof(data
));
940 data
.machine
= MACHINE_PC
;
941 data
.variant
= ".memhp";
942 test_acpi_one(" -m 128,slots=3,maxmem=1G"
943 " -object memory-backend-ram,id=ram0,size=64M"
944 " -object memory-backend-ram,id=ram1,size=64M"
945 " -numa node,memdev=ram0 -numa node,memdev=ram1"
946 " -numa dist,src=0,dst=1,val=21",
948 free_test_data(&data
);
951 static void test_acpi_q35_tcg_numamem(void)
955 memset(&data
, 0, sizeof(data
));
956 data
.machine
= MACHINE_Q35
;
957 data
.variant
= ".numamem";
958 test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
959 " -numa node -numa node,memdev=ram0", &data
);
960 free_test_data(&data
);
963 static void test_acpi_piix4_tcg_numamem(void)
967 memset(&data
, 0, sizeof(data
));
968 data
.machine
= MACHINE_PC
;
969 data
.variant
= ".numamem";
970 test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
971 " -numa node -numa node,memdev=ram0", &data
);
972 free_test_data(&data
);
975 uint64_t tpm_tis_base_addr
;
977 static void test_acpi_tcg_tpm(const char *machine
, const char *tpm_if
,
981 gchar
*tmp_dir_name
= g_strdup_printf("qemu-test_acpi_%s_tcg_%s.XXXXXX",
983 char *tmp_path
= g_dir_make_tmp(tmp_dir_name
, NULL
);
987 char *args
, *variant
= g_strdup_printf(".%s", tpm_if
);
989 tpm_tis_base_addr
= base
;
991 module_call_init(MODULE_INIT_QOM
);
993 test
.addr
= g_new0(SocketAddress
, 1);
994 test
.addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
995 test
.addr
->u
.q_unix
.path
= g_build_filename(tmp_path
, "sock", NULL
);
996 g_mutex_init(&test
.data_mutex
);
997 g_cond_init(&test
.data_cond
);
998 test
.data_cond_signal
= false;
1000 thread
= g_thread_new(NULL
, tpm_emu_ctrl_thread
, &test
);
1001 tpm_emu_test_wait_cond(&test
);
1003 memset(&data
, 0, sizeof(data
));
1004 data
.machine
= machine
;
1005 data
.variant
= variant
;
1007 args
= g_strdup_printf(
1008 " -chardev socket,id=chr,path=%s"
1009 " -tpmdev emulator,id=dev,chardev=chr"
1010 " -device tpm-%s,tpmdev=dev",
1011 test
.addr
->u
.q_unix
.path
, tpm_if
);
1013 test_acpi_one(args
, &data
);
1015 g_thread_join(thread
);
1016 g_unlink(test
.addr
->u
.q_unix
.path
);
1017 qapi_free_SocketAddress(test
.addr
);
1021 g_free(tmp_dir_name
);
1023 free_test_data(&data
);
1025 g_test_skip("TPM disabled");
1029 static void test_acpi_q35_tcg_tpm_tis(void)
1031 test_acpi_tcg_tpm("q35", "tis", 0xFED40000);
1034 static void test_acpi_tcg_dimm_pxm(const char *machine
)
1038 memset(&data
, 0, sizeof(data
));
1039 data
.machine
= machine
;
1040 data
.variant
= ".dimmpxm";
1041 test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
1043 " -m 128M,slots=3,maxmem=1G"
1044 " -object memory-backend-ram,id=ram0,size=32M"
1045 " -object memory-backend-ram,id=ram1,size=32M"
1046 " -object memory-backend-ram,id=ram2,size=32M"
1047 " -object memory-backend-ram,id=ram3,size=32M"
1048 " -numa node,memdev=ram0,nodeid=0"
1049 " -numa node,memdev=ram1,nodeid=1"
1050 " -numa node,memdev=ram2,nodeid=2"
1051 " -numa node,memdev=ram3,nodeid=3"
1052 " -numa cpu,node-id=0,socket-id=0"
1053 " -numa cpu,node-id=1,socket-id=1"
1054 " -numa cpu,node-id=2,socket-id=2"
1055 " -numa cpu,node-id=3,socket-id=3"
1056 " -object memory-backend-ram,id=ram4,size=128M"
1057 " -object memory-backend-ram,id=nvm0,size=128M"
1058 " -device pc-dimm,id=dimm0,memdev=ram4,node=1"
1059 " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
1061 free_test_data(&data
);
1064 static void test_acpi_q35_tcg_dimm_pxm(void)
1066 test_acpi_tcg_dimm_pxm(MACHINE_Q35
);
1069 static void test_acpi_piix4_tcg_dimm_pxm(void)
1071 test_acpi_tcg_dimm_pxm(MACHINE_PC
);
1074 static void test_acpi_virt_tcg_memhp(void)
1079 .uefi_fl1
= "pc-bios/edk2-aarch64-code.fd",
1080 .uefi_fl2
= "pc-bios/edk2-arm-vars.fd",
1081 .cd
= "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1082 .ram_start
= 0x40000000ULL
,
1083 .scan_len
= 256ULL * 1024 * 1024,
1086 data
.variant
= ".memhp";
1087 test_acpi_one(" -machine nvdimm=on"
1089 " -m 256M,slots=3,maxmem=1G"
1090 " -object memory-backend-ram,id=ram0,size=128M"
1091 " -object memory-backend-ram,id=ram1,size=128M"
1092 " -numa node,memdev=ram0 -numa node,memdev=ram1"
1093 " -numa dist,src=0,dst=1,val=21"
1094 " -object memory-backend-ram,id=ram2,size=128M"
1095 " -object memory-backend-ram,id=nvm0,size=128M"
1096 " -device pc-dimm,id=dimm0,memdev=ram2,node=0"
1097 " -device nvdimm,id=dimm1,memdev=nvm0,node=1",
1100 free_test_data(&data
);
1104 static void test_acpi_microvm_prepare(test_data
*data
)
1106 memset(data
, 0, sizeof(*data
));
1107 data
->machine
= "microvm";
1108 data
->required_struct_types
= NULL
; /* no smbios */
1109 data
->required_struct_types_len
= 0;
1110 data
->blkdev
= "virtio-blk-device";
1113 static void test_acpi_microvm_tcg(void)
1117 test_acpi_microvm_prepare(&data
);
1118 test_acpi_one(" -machine microvm,acpi=on,rtc=off",
1120 free_test_data(&data
);
1123 static void test_acpi_microvm_usb_tcg(void)
1127 test_acpi_microvm_prepare(&data
);
1128 data
.variant
= ".usb";
1129 test_acpi_one(" -machine microvm,acpi=on,usb=on,rtc=off",
1131 free_test_data(&data
);
1134 static void test_acpi_microvm_rtc_tcg(void)
1138 test_acpi_microvm_prepare(&data
);
1139 data
.variant
= ".rtc";
1140 test_acpi_one(" -machine microvm,acpi=on,rtc=on",
1142 free_test_data(&data
);
1145 static void test_acpi_microvm_pcie_tcg(void)
1149 test_acpi_microvm_prepare(&data
);
1150 data
.variant
= ".pcie";
1151 data
.tcg_only
= true; /* need constant host-phys-bits */
1152 test_acpi_one(" -machine microvm,acpi=on,rtc=off,pcie=on",
1154 free_test_data(&data
);
1157 static void test_acpi_virt_tcg_numamem(void)
1162 .uefi_fl1
= "pc-bios/edk2-aarch64-code.fd",
1163 .uefi_fl2
= "pc-bios/edk2-arm-vars.fd",
1164 .cd
= "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1165 .ram_start
= 0x40000000ULL
,
1166 .scan_len
= 128ULL * 1024 * 1024,
1169 data
.variant
= ".numamem";
1170 test_acpi_one(" -cpu cortex-a57"
1171 " -object memory-backend-ram,id=ram0,size=128M"
1172 " -numa node,memdev=ram0",
1175 free_test_data(&data
);
1179 static void test_acpi_tcg_acpi_hmat(const char *machine
)
1183 memset(&data
, 0, sizeof(data
));
1184 data
.machine
= machine
;
1185 data
.variant
= ".acpihmat";
1186 test_acpi_one(" -machine hmat=on"
1188 " -m 128M,slots=2,maxmem=1G"
1189 " -object memory-backend-ram,size=64M,id=m0"
1190 " -object memory-backend-ram,size=64M,id=m1"
1191 " -numa node,nodeid=0,memdev=m0"
1192 " -numa node,nodeid=1,memdev=m1,initiator=0"
1193 " -numa cpu,node-id=0,socket-id=0"
1194 " -numa cpu,node-id=0,socket-id=1"
1195 " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1196 "data-type=access-latency,latency=1"
1197 " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1198 "data-type=access-bandwidth,bandwidth=65534M"
1199 " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1200 "data-type=access-latency,latency=65534"
1201 " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1202 "data-type=access-bandwidth,bandwidth=32767M"
1203 " -numa hmat-cache,node-id=0,size=10K,level=1,"
1204 "associativity=direct,policy=write-back,line=8"
1205 " -numa hmat-cache,node-id=1,size=10K,level=1,"
1206 "associativity=direct,policy=write-back,line=8",
1208 free_test_data(&data
);
1211 static void test_acpi_q35_tcg_acpi_hmat(void)
1213 test_acpi_tcg_acpi_hmat(MACHINE_Q35
);
1216 static void test_acpi_piix4_tcg_acpi_hmat(void)
1218 test_acpi_tcg_acpi_hmat(MACHINE_PC
);
1221 static void test_acpi_virt_tcg(void)
1226 .uefi_fl1
= "pc-bios/edk2-aarch64-code.fd",
1227 .uefi_fl2
= "pc-bios/edk2-arm-vars.fd",
1228 .cd
= "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1229 .ram_start
= 0x40000000ULL
,
1230 .scan_len
= 128ULL * 1024 * 1024,
1233 test_acpi_one("-cpu cortex-a57", &data
);
1234 free_test_data(&data
);
1236 data
.smbios_cpu_max_speed
= 2900;
1237 data
.smbios_cpu_curr_speed
= 2700;
1238 test_acpi_one("-cpu cortex-a57 "
1239 "-smbios type=4,max-speed=2900,current-speed=2700", &data
);
1240 free_test_data(&data
);
1243 int main(int argc
, char *argv
[])
1245 const char *arch
= qtest_get_arch();
1248 g_test_init(&argc
, &argv
, NULL
);
1250 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
1251 ret
= boot_sector_init(disk
);
1256 qtest_add_func("acpi/q35/tpm-tis", test_acpi_q35_tcg_tpm_tis
);
1257 qtest_add_func("acpi/piix4", test_acpi_piix4_tcg
);
1258 qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge
);
1259 qtest_add_func("acpi/piix4/pci-hotplug/no_root_hotplug",
1260 test_acpi_piix4_no_root_hotplug
);
1261 qtest_add_func("acpi/piix4/pci-hotplug/no_bridge_hotplug",
1262 test_acpi_piix4_no_bridge_hotplug
);
1263 qtest_add_func("acpi/piix4/pci-hotplug/off",
1264 test_acpi_piix4_no_acpi_pci_hotplug
);
1265 qtest_add_func("acpi/q35", test_acpi_q35_tcg
);
1266 qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge
);
1267 qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64
);
1268 qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi
);
1269 qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi
);
1270 qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp
);
1271 qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp
);
1272 qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp
);
1273 qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp
);
1274 qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem
);
1275 qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem
);
1276 qtest_add_func("acpi/piix4/dimmpxm", test_acpi_piix4_tcg_dimm_pxm
);
1277 qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm
);
1278 qtest_add_func("acpi/piix4/acpihmat", test_acpi_piix4_tcg_acpi_hmat
);
1279 qtest_add_func("acpi/q35/acpihmat", test_acpi_q35_tcg_acpi_hmat
);
1280 qtest_add_func("acpi/microvm", test_acpi_microvm_tcg
);
1281 qtest_add_func("acpi/microvm/usb", test_acpi_microvm_usb_tcg
);
1282 qtest_add_func("acpi/microvm/rtc", test_acpi_microvm_rtc_tcg
);
1283 if (strcmp(arch
, "x86_64") == 0) {
1284 qtest_add_func("acpi/microvm/pcie", test_acpi_microvm_pcie_tcg
);
1286 } else if (strcmp(arch
, "aarch64") == 0) {
1287 qtest_add_func("acpi/virt", test_acpi_virt_tcg
);
1288 qtest_add_func("acpi/virt/numamem", test_acpi_virt_tcg_numamem
);
1289 qtest_add_func("acpi/virt/memhp", test_acpi_virt_tcg_memhp
);
1292 boot_sector_cleanup(disk
);