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