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