xref: /qemu/tests/qtest/bios-tables-test.c (revision 193e4b90d60a3a976ee7940d6e318ebab4db00e9)
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.required_struct_types = base_required_struct_types;
937     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
938     test_acpi_one(NULL, &data);
939     free_test_data(&data);
940 }
941 
942 static void test_acpi_piix4_tcg_bridge(void)
943 {
944     test_data data = {};
945 
946     data.machine = MACHINE_PC;
947     data.variant = ".bridge";
948     data.required_struct_types = base_required_struct_types;
949     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
950     test_vm_prepare("-S"
951         " -device pci-bridge,chassis_nr=1"
952         " -device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2"
953         " -device pci-testdev,bus=pci.0,addr=5.0"
954         " -device pci-testdev,bus=pci.1", &data);
955 
956     /* hotplugged bridges section */
957     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr",
958         "{'bus': 'pci.1', 'addr': '2.0', 'chassis_nr': 3 }");
959     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr_multifunc",
960         "{'bus': 'pci.1', 'addr': '0xf.1', 'chassis_nr': 4 }");
961     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbrhost",
962         "{'bus': 'pci.0', 'addr': '4.0', 'chassis_nr': 5 }");
963     qtest_qmp_device_add(data.qts, "pci-testdev", "d1", "{'bus': 'pci.0' }");
964     qtest_qmp_device_add(data.qts, "pci-testdev", "d2", "{'bus': 'pci.1' }");
965     qtest_qmp_device_add(data.qts, "pci-testdev", "d3", "{'bus': 'hpbr', "
966                                    "'addr': '1.0' }");
967     qtest_qmp_send(data.qts, "{'execute':'cont' }");
968     qtest_qmp_eventwait(data.qts, "RESUME");
969 
970     process_acpi_tables_noexit(&data);
971     free_test_data(&data);
972 
973     /* check that reboot/reset doesn't change any ACPI tables  */
974     qtest_qmp_send(data.qts, "{'execute':'system_reset' }");
975     process_acpi_tables(&data);
976     free_test_data(&data);
977 }
978 
979 static void test_acpi_piix4_no_root_hotplug(void)
980 {
981     test_data data = {};
982 
983     data.machine = MACHINE_PC;
984     data.variant = ".roothp";
985     data.required_struct_types = base_required_struct_types;
986     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
987     test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
988                   "-device pci-bridge,chassis_nr=1 "
989                   "-device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2 "
990                   "-device pci-testdev,bus=pci.0 "
991                   "-device pci-testdev,bus=pci.1", &data);
992     free_test_data(&data);
993 }
994 
995 static void test_acpi_piix4_no_bridge_hotplug(void)
996 {
997     test_data data = {};
998 
999     data.machine = MACHINE_PC;
1000     data.variant = ".hpbridge";
1001     data.required_struct_types = base_required_struct_types;
1002     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1003     test_acpi_one("-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
1004                   "-device pci-bridge,chassis_nr=1 "
1005                   "-device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2 "
1006                   "-device pci-testdev,bus=pci.0 "
1007                   "-device pci-testdev,bus=pci.1,addr=2.0", &data);
1008     free_test_data(&data);
1009 }
1010 
1011 static void test_acpi_piix4_no_acpi_pci_hotplug(void)
1012 {
1013     test_data data = {};
1014 
1015     data.machine = MACHINE_PC;
1016     data.variant = ".hpbrroot";
1017     data.required_struct_types = base_required_struct_types;
1018     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1019     test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
1020                   "-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
1021                   "-device pci-bridge,chassis_nr=1,addr=4.0 "
1022                   "-device pci-testdev,bus=pci.0,addr=5.0 "
1023                   "-device pci-testdev,bus=pci.0,addr=6.0,acpi-index=101 "
1024                   "-device pci-testdev,bus=pci.1,addr=1.0 "
1025                   "-device pci-testdev,bus=pci.1,addr=2.0,acpi-index=201 "
1026                   "-device pci-bridge,id=nhpbr,chassis_nr=2,shpc=off,addr=7.0 "
1027                   "-device pci-testdev,bus=nhpbr,addr=1.0,acpi-index=301 "
1028                   , &data);
1029     free_test_data(&data);
1030 }
1031 
1032 static void test_acpi_q35_tcg(void)
1033 {
1034     test_data data = {};
1035 
1036     data.machine = MACHINE_Q35;
1037     data.required_struct_types = base_required_struct_types;
1038     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1039     test_acpi_one(NULL, &data);
1040     free_test_data(&data);
1041 
1042     data.smbios_cpu_max_speed = 3000;
1043     data.smbios_cpu_curr_speed = 2600;
1044     test_acpi_one("-smbios type=4,max-speed=3000,current-speed=2600", &data);
1045     free_test_data(&data);
1046 }
1047 
1048 static void test_acpi_q35_kvm_type4_count(void)
1049 {
1050     test_data data = {
1051         .machine = MACHINE_Q35,
1052         .variant = ".type4-count",
1053         .required_struct_types = base_required_struct_types,
1054         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1055         .type4_count = 5,
1056     };
1057 
1058     test_acpi_one("-machine smbios-entry-point-type=64 "
1059                   "-smp cpus=100,maxcpus=120,sockets=5,"
1060                   "dies=2,cores=4,threads=3", &data);
1061     free_test_data(&data);
1062 }
1063 
1064 static void test_acpi_q35_kvm_core_count(void)
1065 {
1066     test_data data = {
1067         .machine = MACHINE_Q35,
1068         .variant = ".core-count",
1069         .required_struct_types = base_required_struct_types,
1070         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1071         .smbios_core_count = 9,
1072         .smbios_core_count2 = 9,
1073     };
1074 
1075     test_acpi_one("-machine smbios-entry-point-type=64 "
1076                   "-smp 54,sockets=2,dies=3,cores=3,threads=3",
1077                   &data);
1078     free_test_data(&data);
1079 }
1080 
1081 static void test_acpi_q35_kvm_core_count2(void)
1082 {
1083     test_data data = {
1084         .machine = MACHINE_Q35,
1085         .variant = ".core-count2",
1086         .required_struct_types = base_required_struct_types,
1087         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1088         .smbios_core_count = 0xFF,
1089         .smbios_core_count2 = 260,
1090     };
1091 
1092     test_acpi_one("-machine smbios-entry-point-type=64 "
1093                   "-smp 260,dies=2,cores=130,threads=1",
1094                   &data);
1095     free_test_data(&data);
1096 }
1097 
1098 static void test_acpi_q35_kvm_thread_count(void)
1099 {
1100     test_data data = {
1101         .machine = MACHINE_Q35,
1102         .variant = ".thread-count",
1103         .required_struct_types = base_required_struct_types,
1104         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1105         .smbios_thread_count = 27,
1106         .smbios_thread_count2 = 27,
1107     };
1108 
1109     test_acpi_one("-machine smbios-entry-point-type=64 "
1110                   "-smp cpus=15,maxcpus=54,sockets=2,dies=3,cores=3,threads=3",
1111                   &data);
1112     free_test_data(&data);
1113 }
1114 
1115 static void test_acpi_q35_kvm_thread_count2(void)
1116 {
1117     test_data data = {
1118         .machine = MACHINE_Q35,
1119         .variant = ".thread-count2",
1120         .required_struct_types = base_required_struct_types,
1121         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1122         .smbios_thread_count = 0xFF,
1123         .smbios_thread_count2 = 260,
1124     };
1125 
1126     test_acpi_one("-machine smbios-entry-point-type=64 "
1127                   "-smp cpus=210,maxcpus=260,dies=2,cores=65,threads=2",
1128                   &data);
1129     free_test_data(&data);
1130 }
1131 
1132 static void test_acpi_q35_tcg_bridge(void)
1133 {
1134     test_data data = {};
1135 
1136     data.machine = MACHINE_Q35;
1137     data.variant = ".bridge";
1138     data.required_struct_types = base_required_struct_types;
1139     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1140     test_acpi_one("-device pci-bridge,chassis_nr=1,id=br1"
1141                   " -device pci-testdev,bus=pcie.0"
1142                   " -device pci-testdev,bus=br1", &data);
1143     free_test_data(&data);
1144 }
1145 
1146 static void test_acpi_q35_tcg_no_acpi_hotplug(void)
1147 {
1148     test_data data = {};
1149 
1150     data.machine = MACHINE_Q35;
1151     data.variant = ".noacpihp";
1152     data.required_struct_types = base_required_struct_types;
1153     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1154     test_acpi_one("-global ICH9-LPC.acpi-pci-hotplug-with-bridge-support=off"
1155         " -device pci-testdev,bus=pcie.0,acpi-index=101,addr=3.0"
1156         " -device pci-bridge,chassis_nr=1,id=shpcbr,addr=4.0"
1157         " -device pci-testdev,bus=shpcbr,addr=1.0,acpi-index=201"
1158         " -device pci-bridge,chassis_nr=2,shpc=off,id=noshpcbr,addr=5.0"
1159         " -device pci-testdev,bus=noshpcbr,addr=1.0,acpi-index=301"
1160         " -device pcie-root-port,id=hprp,port=0x0,chassis=1,addr=6.0"
1161         " -device pci-testdev,bus=hprp,acpi-index=401"
1162         " -device pcie-root-port,id=nohprp,port=0x0,chassis=2,hotplug=off,"
1163                                  "addr=7.0"
1164         " -device pci-testdev,bus=nohprp,acpi-index=501"
1165         " -device pcie-root-port,id=nohprpint,port=0x0,chassis=3,hotplug=off,"
1166                                  "multifunction=on,addr=8.0"
1167         " -device pci-testdev,bus=nohprpint,acpi-index=601,addr=0.1"
1168         " -device pcie-root-port,id=hprp2,port=0x0,chassis=4,bus=nohprpint,"
1169                                  "addr=0.2"
1170         " -device pci-testdev,bus=hprp2,acpi-index=602"
1171         , &data);
1172     free_test_data(&data);
1173 }
1174 
1175 static void test_acpi_q35_multif_bridge(void)
1176 {
1177     test_data data = {
1178         .machine = MACHINE_Q35,
1179         .variant = ".multi-bridge",
1180     };
1181     test_vm_prepare("-S"
1182         " -device virtio-balloon,id=balloon0,addr=0x4.0x2"
1183         " -device pcie-root-port,id=rp0,multifunction=on,"
1184                   "port=0x0,chassis=1,addr=0x2"
1185         " -device pcie-root-port,id=rp1,port=0x1,chassis=2,addr=0x3.0x1"
1186         " -device pcie-root-port,id=rp2,port=0x0,chassis=3,bus=rp1,addr=0.0"
1187         " -device pci-bridge,bus=rp2,chassis_nr=4,id=br1"
1188         " -device pcie-root-port,id=rphptgt1,port=0x0,chassis=5,addr=2.1"
1189         " -device pcie-root-port,id=rphptgt2,port=0x0,chassis=6,addr=2.2"
1190         " -device pcie-root-port,id=rphptgt3,port=0x0,chassis=7,addr=2.3"
1191         " -device pci-testdev,bus=pcie.0,addr=2.4"
1192         " -device pci-testdev,bus=pcie.0,addr=2.5,acpi-index=102"
1193         " -device pci-testdev,bus=pcie.0,addr=5.0"
1194         " -device pci-testdev,bus=pcie.0,addr=0xf.0,acpi-index=101"
1195         " -device pci-testdev,bus=rp0,addr=0.0"
1196         " -device pci-testdev,bus=br1"
1197         " -device pcie-root-port,id=rpnohp,chassis=8,addr=0xA.0,hotplug=off"
1198         " -device pcie-root-port,id=rp3,chassis=9,bus=rpnohp"
1199         , &data);
1200 
1201     /* hotplugged bridges section */
1202     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr1",
1203         "{'bus': 'br1', 'addr': '6.0', 'chassis_nr': 128 }");
1204     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr2-multiif",
1205         "{ 'bus': 'br1', 'addr': '2.2', 'chassis_nr': 129 }");
1206     qtest_qmp_device_add(data.qts, "pcie-pci-bridge", "hpbr3",
1207         "{'bus': 'rphptgt1', 'addr': '0.0' }");
1208     qtest_qmp_device_add(data.qts, "pcie-root-port", "hprp",
1209         "{'bus': 'rphptgt2', 'addr': '0.0' }");
1210     qtest_qmp_device_add(data.qts, "pci-testdev", "hpnic",
1211         "{'bus': 'rphptgt3', 'addr': '0.0' }");
1212     qtest_qmp_send(data.qts, "{'execute':'cont' }");
1213     qtest_qmp_eventwait(data.qts, "RESUME");
1214 
1215     process_acpi_tables_noexit(&data);
1216     free_test_data(&data);
1217 
1218     /* check that reboot/reset doesn't change any ACPI tables  */
1219     qtest_qmp_send(data.qts, "{'execute':'system_reset' }");
1220     process_acpi_tables(&data);
1221     free_test_data(&data);
1222 }
1223 
1224 static void test_acpi_q35_tcg_mmio64(void)
1225 {
1226     test_data data = {
1227         .machine = MACHINE_Q35,
1228         .variant = ".mmio64",
1229         .tcg_only = true,
1230         .required_struct_types = base_required_struct_types,
1231         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types)
1232     };
1233 
1234     test_acpi_one("-m 128M,slots=1,maxmem=2G "
1235                   "-cpu Opteron_G1 "
1236                   "-object memory-backend-ram,id=ram0,size=128M "
1237                   "-numa node,memdev=ram0 "
1238                   "-device pci-testdev,membar=2G",
1239                   &data);
1240     free_test_data(&data);
1241 }
1242 
1243 static void test_acpi_piix4_tcg_cphp(void)
1244 {
1245     test_data data = {};
1246 
1247     data.machine = MACHINE_PC;
1248     data.variant = ".cphp";
1249     test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
1250                   " -object memory-backend-ram,id=ram0,size=64M"
1251                   " -object memory-backend-ram,id=ram1,size=64M"
1252                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1253                   " -numa dist,src=0,dst=1,val=21",
1254                   &data);
1255     free_test_data(&data);
1256 }
1257 
1258 static void test_acpi_q35_tcg_cphp(void)
1259 {
1260     test_data data = {};
1261 
1262     data.machine = MACHINE_Q35;
1263     data.variant = ".cphp";
1264     test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
1265                   " -object memory-backend-ram,id=ram0,size=64M"
1266                   " -object memory-backend-ram,id=ram1,size=64M"
1267                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1268                   " -numa dist,src=0,dst=1,val=21",
1269                   &data);
1270     free_test_data(&data);
1271 }
1272 
1273 static uint8_t ipmi_required_struct_types[] = {
1274     0, 1, 3, 4, 16, 17, 19, 32, 38, 127
1275 };
1276 
1277 static void test_acpi_q35_tcg_ipmi(void)
1278 {
1279     test_data data = {};
1280 
1281     data.machine = MACHINE_Q35;
1282     data.variant = ".ipmibt";
1283     data.required_struct_types = ipmi_required_struct_types;
1284     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1285     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1286                   " -device isa-ipmi-bt,bmc=bmc0",
1287                   &data);
1288     free_test_data(&data);
1289 }
1290 
1291 static void test_acpi_q35_tcg_smbus_ipmi(void)
1292 {
1293     test_data data = {};
1294 
1295     data.machine = MACHINE_Q35;
1296     data.variant = ".ipmismbus";
1297     data.required_struct_types = ipmi_required_struct_types;
1298     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1299     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1300                   " -device smbus-ipmi,bmc=bmc0",
1301                   &data);
1302     free_test_data(&data);
1303 }
1304 
1305 static void test_acpi_piix4_tcg_ipmi(void)
1306 {
1307     test_data data = {};
1308 
1309     /* Supplying -machine accel argument overrides the default (qtest).
1310      * This is to make guest actually run.
1311      */
1312     data.machine = MACHINE_PC;
1313     data.variant = ".ipmikcs";
1314     data.required_struct_types = ipmi_required_struct_types;
1315     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1316     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1317                   " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
1318                   &data);
1319     free_test_data(&data);
1320 }
1321 
1322 static void test_acpi_q35_tcg_memhp(void)
1323 {
1324     test_data data = {};
1325 
1326     data.machine = MACHINE_Q35;
1327     data.variant = ".memhp";
1328     test_acpi_one(" -m 128,slots=3,maxmem=1G"
1329                   " -object memory-backend-ram,id=ram0,size=64M"
1330                   " -object memory-backend-ram,id=ram1,size=64M"
1331                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1332                   " -numa dist,src=0,dst=1,val=21",
1333                   &data);
1334     free_test_data(&data);
1335 }
1336 
1337 static void test_acpi_piix4_tcg_memhp(void)
1338 {
1339     test_data data = {};
1340 
1341     data.machine = MACHINE_PC;
1342     data.variant = ".memhp";
1343     test_acpi_one(" -m 128,slots=3,maxmem=1G"
1344                   " -object memory-backend-ram,id=ram0,size=64M"
1345                   " -object memory-backend-ram,id=ram1,size=64M"
1346                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1347                   " -numa dist,src=0,dst=1,val=21",
1348                   &data);
1349     free_test_data(&data);
1350 }
1351 
1352 static void test_acpi_piix4_tcg_nosmm(void)
1353 {
1354     test_data data = {};
1355 
1356     data.machine = MACHINE_PC;
1357     data.variant = ".nosmm";
1358     test_acpi_one("-machine smm=off", &data);
1359     free_test_data(&data);
1360 }
1361 
1362 static void test_acpi_piix4_tcg_smm_compat(void)
1363 {
1364     test_data data = {};
1365 
1366     data.machine = MACHINE_PC;
1367     data.variant = ".smm-compat";
1368     test_acpi_one("-global PIIX4_PM.smm-compat=on", &data);
1369     free_test_data(&data);
1370 }
1371 
1372 static void test_acpi_piix4_tcg_smm_compat_nosmm(void)
1373 {
1374     test_data data = {};
1375 
1376     data.machine = MACHINE_PC;
1377     data.variant = ".smm-compat-nosmm";
1378     test_acpi_one("-global PIIX4_PM.smm-compat=on -machine smm=off", &data);
1379     free_test_data(&data);
1380 }
1381 
1382 static void test_acpi_piix4_tcg_nohpet(void)
1383 {
1384     test_data data = {};
1385 
1386     data.machine = MACHINE_PC;
1387     data.machine_param = ",hpet=off";
1388     data.variant = ".nohpet";
1389     test_acpi_one(NULL, &data);
1390     free_test_data(&data);
1391 }
1392 
1393 static void test_acpi_q35_tcg_numamem(void)
1394 {
1395     test_data data = {};
1396 
1397     data.machine = MACHINE_Q35;
1398     data.variant = ".numamem";
1399     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1400                   " -numa node -numa node,memdev=ram0", &data);
1401     free_test_data(&data);
1402 }
1403 
1404 static void test_acpi_q35_kvm_xapic(void)
1405 {
1406     test_data data = {};
1407 
1408     data.machine = MACHINE_Q35;
1409     data.variant = ".xapic";
1410     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1411                   " -numa node -numa node,memdev=ram0"
1412                   " -machine kernel-irqchip=on -smp 1,maxcpus=288", &data);
1413     free_test_data(&data);
1414 }
1415 
1416 static void test_acpi_q35_tcg_nosmm(void)
1417 {
1418     test_data data = {};
1419 
1420     data.machine = MACHINE_Q35;
1421     data.variant = ".nosmm";
1422     test_acpi_one("-machine smm=off", &data);
1423     free_test_data(&data);
1424 }
1425 
1426 static void test_acpi_q35_tcg_smm_compat(void)
1427 {
1428     test_data data = {};
1429 
1430     data.machine = MACHINE_Q35;
1431     data.variant = ".smm-compat";
1432     test_acpi_one("-global ICH9-LPC.smm-compat=on", &data);
1433     free_test_data(&data);
1434 }
1435 
1436 static void test_acpi_q35_tcg_smm_compat_nosmm(void)
1437 {
1438     test_data data = {};
1439 
1440     data.machine = MACHINE_Q35;
1441     data.variant = ".smm-compat-nosmm";
1442     test_acpi_one("-global ICH9-LPC.smm-compat=on -machine smm=off", &data);
1443     free_test_data(&data);
1444 }
1445 
1446 static void test_acpi_q35_tcg_nohpet(void)
1447 {
1448     test_data data = {};
1449 
1450     data.machine = MACHINE_Q35;
1451     data.machine_param = ",hpet=off";
1452     data.variant = ".nohpet";
1453     test_acpi_one(NULL, &data);
1454     free_test_data(&data);
1455 }
1456 
1457 static void test_acpi_q35_kvm_dmar(void)
1458 {
1459     test_data data = {};
1460 
1461     data.machine = MACHINE_Q35;
1462     data.variant = ".dmar";
1463     test_acpi_one("-machine kernel-irqchip=split -accel kvm"
1464                   " -device intel-iommu,intremap=on,device-iotlb=on", &data);
1465     free_test_data(&data);
1466 }
1467 
1468 static void test_acpi_q35_tcg_ivrs(void)
1469 {
1470     test_data data = {};
1471 
1472     data.machine = MACHINE_Q35;
1473     data.variant = ".ivrs";
1474     data.tcg_only = true,
1475     test_acpi_one(" -device amd-iommu", &data);
1476     free_test_data(&data);
1477 }
1478 
1479 static void test_acpi_piix4_tcg_numamem(void)
1480 {
1481     test_data data = {};
1482 
1483     data.machine = MACHINE_PC;
1484     data.variant = ".numamem";
1485     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1486                   " -numa node -numa node,memdev=ram0", &data);
1487     free_test_data(&data);
1488 }
1489 
1490 uint64_t tpm_tis_base_addr;
1491 
1492 static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
1493                               uint64_t base, enum TPMVersion tpm_version)
1494 {
1495     gchar *tmp_dir_name = g_strdup_printf("qemu-test_acpi_%s_tcg_%s.XXXXXX",
1496                                           machine, tpm_if);
1497     char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
1498     TPMTestState test;
1499     test_data data = {};
1500     GThread *thread;
1501     const char *suffix = tpm_version == TPM_VERSION_2_0 ? "tpm2" : "tpm12";
1502     char *args, *variant = g_strdup_printf(".%s.%s", tpm_if, suffix);
1503 
1504     tpm_tis_base_addr = base;
1505 
1506     module_call_init(MODULE_INIT_QOM);
1507 
1508     test.addr = g_new0(SocketAddress, 1);
1509     test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
1510     test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
1511     g_mutex_init(&test.data_mutex);
1512     g_cond_init(&test.data_cond);
1513     test.data_cond_signal = false;
1514     test.tpm_version = tpm_version;
1515 
1516     thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
1517     tpm_emu_test_wait_cond(&test);
1518 
1519     data.machine = machine;
1520     data.variant = variant;
1521 
1522     args = g_strdup_printf(
1523         " -chardev socket,id=chr,path=%s"
1524         " -tpmdev emulator,id=dev,chardev=chr"
1525         " -device tpm-%s,tpmdev=dev",
1526         test.addr->u.q_unix.path, tpm_if);
1527 
1528     test_acpi_one(args, &data);
1529 
1530     g_thread_join(thread);
1531     g_unlink(test.addr->u.q_unix.path);
1532     qapi_free_SocketAddress(test.addr);
1533     g_rmdir(tmp_path);
1534     g_free(variant);
1535     g_free(tmp_path);
1536     g_free(tmp_dir_name);
1537     g_free(args);
1538     free_test_data(&data);
1539 }
1540 
1541 static void test_acpi_q35_tcg_tpm2_tis(void)
1542 {
1543     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_2_0);
1544 }
1545 
1546 static void test_acpi_q35_tcg_tpm12_tis(void)
1547 {
1548     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_1_2);
1549 }
1550 
1551 static void test_acpi_tcg_dimm_pxm(const char *machine)
1552 {
1553     test_data data = {};
1554 
1555     data.machine = machine;
1556     data.variant = ".dimmpxm";
1557     test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
1558                   " -smp 4,sockets=4"
1559                   " -m 128M,slots=3,maxmem=1G"
1560                   " -object memory-backend-ram,id=ram0,size=32M"
1561                   " -object memory-backend-ram,id=ram1,size=32M"
1562                   " -object memory-backend-ram,id=ram2,size=32M"
1563                   " -object memory-backend-ram,id=ram3,size=32M"
1564                   " -numa node,memdev=ram0,nodeid=0"
1565                   " -numa node,memdev=ram1,nodeid=1"
1566                   " -numa node,memdev=ram2,nodeid=2"
1567                   " -numa node,memdev=ram3,nodeid=3"
1568                   " -numa cpu,node-id=0,socket-id=0"
1569                   " -numa cpu,node-id=1,socket-id=1"
1570                   " -numa cpu,node-id=2,socket-id=2"
1571                   " -numa cpu,node-id=3,socket-id=3"
1572                   " -object memory-backend-ram,id=ram4,size=128M"
1573                   " -object memory-backend-ram,id=nvm0,size=128M"
1574                   " -device pc-dimm,id=dimm0,memdev=ram4,node=1"
1575                   " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
1576                   &data);
1577     free_test_data(&data);
1578 }
1579 
1580 static void test_acpi_q35_tcg_dimm_pxm(void)
1581 {
1582     test_acpi_tcg_dimm_pxm(MACHINE_Q35);
1583 }
1584 
1585 static void test_acpi_piix4_tcg_dimm_pxm(void)
1586 {
1587     test_acpi_tcg_dimm_pxm(MACHINE_PC);
1588 }
1589 
1590 static void test_acpi_aarch64_virt_tcg_memhp(void)
1591 {
1592     test_data data = {
1593         .machine = "virt",
1594         .arch = "aarch64",
1595         .tcg_only = true,
1596         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1597         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1598         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1599         .ram_start = 0x40000000ULL,
1600         .scan_len = 256ULL * 1024 * 1024,
1601     };
1602 
1603     data.variant = ".memhp";
1604     test_acpi_one(" -machine nvdimm=on"
1605                   " -cpu cortex-a57"
1606                   " -m 256M,slots=3,maxmem=1G"
1607                   " -object memory-backend-ram,id=ram0,size=128M"
1608                   " -object memory-backend-ram,id=ram1,size=128M"
1609                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1610                   " -numa dist,src=0,dst=1,val=21"
1611                   " -object memory-backend-ram,id=ram2,size=128M"
1612                   " -object memory-backend-ram,id=nvm0,size=128M"
1613                   " -device pc-dimm,id=dimm0,memdev=ram2,node=0"
1614                   " -device nvdimm,id=dimm1,memdev=nvm0,node=1",
1615                   &data);
1616 
1617     free_test_data(&data);
1618 
1619 }
1620 
1621 static void test_acpi_microvm_prepare(test_data *data)
1622 {
1623     data->machine = "microvm";
1624     data->required_struct_types = NULL; /* no smbios */
1625     data->required_struct_types_len = 0;
1626     data->blkdev = "virtio-blk-device";
1627 }
1628 
1629 static void test_acpi_microvm_tcg(void)
1630 {
1631     test_data data = {};
1632 
1633     test_acpi_microvm_prepare(&data);
1634     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off",
1635                   &data);
1636     free_test_data(&data);
1637 }
1638 
1639 static void test_acpi_microvm_usb_tcg(void)
1640 {
1641     test_data data = {};
1642 
1643     test_acpi_microvm_prepare(&data);
1644     data.variant = ".usb";
1645     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,usb=on,rtc=off",
1646                   &data);
1647     free_test_data(&data);
1648 }
1649 
1650 static void test_acpi_microvm_rtc_tcg(void)
1651 {
1652     test_data data = {};
1653 
1654     test_acpi_microvm_prepare(&data);
1655     data.variant = ".rtc";
1656     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=on",
1657                   &data);
1658     free_test_data(&data);
1659 }
1660 
1661 static void test_acpi_microvm_pcie_tcg(void)
1662 {
1663     test_data data = {};
1664 
1665     test_acpi_microvm_prepare(&data);
1666     data.variant = ".pcie";
1667     data.tcg_only = true; /* need constant host-phys-bits */
1668     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off,pcie=on",
1669                   &data);
1670     free_test_data(&data);
1671 }
1672 
1673 static void test_acpi_microvm_ioapic2_tcg(void)
1674 {
1675     test_data data = {};
1676 
1677     test_acpi_microvm_prepare(&data);
1678     data.variant = ".ioapic2";
1679     test_acpi_one(" -machine microvm,acpi=on,ioapic2=on,rtc=off",
1680                   &data);
1681     free_test_data(&data);
1682 }
1683 
1684 static void test_acpi_aarch64_virt_tcg_numamem(void)
1685 {
1686     test_data data = {
1687         .machine = "virt",
1688         .arch = "aarch64",
1689         .tcg_only = true,
1690         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1691         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1692         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1693         .ram_start = 0x40000000ULL,
1694         .scan_len = 128ULL * 1024 * 1024,
1695     };
1696 
1697     data.variant = ".numamem";
1698     test_acpi_one(" -cpu cortex-a57"
1699                   " -object memory-backend-ram,id=ram0,size=128M"
1700                   " -numa node,memdev=ram0",
1701                   &data);
1702 
1703     free_test_data(&data);
1704 
1705 }
1706 
1707 static void test_acpi_aarch64_virt_tcg_pxb(void)
1708 {
1709     test_data data = {
1710         .machine = "virt",
1711         .arch = "aarch64",
1712         .tcg_only = true,
1713         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1714         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1715         .ram_start = 0x40000000ULL,
1716         .scan_len = 128ULL * 1024 * 1024,
1717     };
1718     /*
1719      * While using -cdrom, the cdrom would auto plugged into pxb-pcie,
1720      * the reason is the bus of pxb-pcie is also root bus, it would lead
1721      * to the error only PCI/PCIE bridge could plug onto pxb.
1722      * Therefore,thr cdrom is defined and plugged onto the scsi controller
1723      * to solve the conflicts.
1724      */
1725     data.variant = ".pxb";
1726     test_acpi_one(" -device pcie-root-port,chassis=1,id=pci.1"
1727                   " -device virtio-scsi-pci,id=scsi0,bus=pci.1"
1728                   " -drive file="
1729                   "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2,"
1730                   "if=none,media=cdrom,id=drive-scsi0-0-0-1,readonly=on"
1731                   " -device scsi-cd,bus=scsi0.0,scsi-id=0,"
1732                   "drive=drive-scsi0-0-0-1,id=scsi0-0-0-1,bootindex=1"
1733                   " -cpu cortex-a57"
1734                   " -device pxb-pcie,bus_nr=128",
1735                   &data);
1736 
1737     free_test_data(&data);
1738 }
1739 
1740 static void test_acpi_tcg_acpi_hmat(const char *machine)
1741 {
1742     test_data data = {};
1743 
1744     data.machine = machine;
1745     data.variant = ".acpihmat";
1746     test_acpi_one(" -machine hmat=on"
1747                   " -smp 2,sockets=2"
1748                   " -m 128M,slots=2,maxmem=1G"
1749                   " -object memory-backend-ram,size=64M,id=m0"
1750                   " -object memory-backend-ram,size=64M,id=m1"
1751                   " -numa node,nodeid=0,memdev=m0"
1752                   " -numa node,nodeid=1,memdev=m1,initiator=0"
1753                   " -numa cpu,node-id=0,socket-id=0"
1754                   " -numa cpu,node-id=0,socket-id=1"
1755                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1756                   "data-type=access-latency,latency=1"
1757                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1758                   "data-type=access-bandwidth,bandwidth=65534M"
1759                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1760                   "data-type=access-latency,latency=65534"
1761                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1762                   "data-type=access-bandwidth,bandwidth=32767M"
1763                   " -numa hmat-cache,node-id=0,size=10K,level=1,"
1764                   "associativity=direct,policy=write-back,line=8"
1765                   " -numa hmat-cache,node-id=1,size=10K,level=1,"
1766                   "associativity=direct,policy=write-back,line=8",
1767                   &data);
1768     free_test_data(&data);
1769 }
1770 
1771 static void test_acpi_q35_tcg_acpi_hmat(void)
1772 {
1773     test_acpi_tcg_acpi_hmat(MACHINE_Q35);
1774 }
1775 
1776 static void test_acpi_piix4_tcg_acpi_hmat(void)
1777 {
1778     test_acpi_tcg_acpi_hmat(MACHINE_PC);
1779 }
1780 
1781 static void test_acpi_aarch64_virt_tcg_acpi_hmat(void)
1782 {
1783     test_data data = {
1784         .machine = "virt",
1785         .arch = "aarch64",
1786         .tcg_only = true,
1787         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1788         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1789         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1790         .ram_start = 0x40000000ULL,
1791         .scan_len = 128ULL * 1024 * 1024,
1792     };
1793 
1794     data.variant = ".acpihmatvirt";
1795 
1796     test_acpi_one(" -machine hmat=on"
1797                   " -cpu cortex-a57"
1798                   " -smp 4,sockets=2"
1799                   " -m 384M"
1800                   " -object memory-backend-ram,size=128M,id=ram0"
1801                   " -object memory-backend-ram,size=128M,id=ram1"
1802                   " -object memory-backend-ram,size=128M,id=ram2"
1803                   " -numa node,nodeid=0,memdev=ram0"
1804                   " -numa node,nodeid=1,memdev=ram1"
1805                   " -numa node,nodeid=2,memdev=ram2"
1806                   " -numa cpu,node-id=0,socket-id=0"
1807                   " -numa cpu,node-id=0,socket-id=0"
1808                   " -numa cpu,node-id=1,socket-id=1"
1809                   " -numa cpu,node-id=1,socket-id=1"
1810                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1811                   "data-type=access-latency,latency=10"
1812                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1813                   "data-type=access-bandwidth,bandwidth=10485760"
1814                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1815                   "data-type=access-latency,latency=20"
1816                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1817                   "data-type=access-bandwidth,bandwidth=5242880"
1818                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1819                   "data-type=access-latency,latency=30"
1820                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1821                   "data-type=access-bandwidth,bandwidth=1048576"
1822                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1823                   "data-type=access-latency,latency=20"
1824                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1825                   "data-type=access-bandwidth,bandwidth=5242880"
1826                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1827                   "data-type=access-latency,latency=10"
1828                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1829                   "data-type=access-bandwidth,bandwidth=10485760"
1830                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1831                   "data-type=access-latency,latency=30"
1832                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1833                   "data-type=access-bandwidth,bandwidth=1048576",
1834                   &data);
1835 
1836     free_test_data(&data);
1837 }
1838 
1839 static void test_acpi_q35_tcg_acpi_hmat_noinitiator(void)
1840 {
1841     test_data data = {};
1842 
1843     data.machine = MACHINE_Q35;
1844     data.variant = ".acpihmat-noinitiator";
1845     test_acpi_one(" -machine hmat=on"
1846                   " -smp 4,sockets=2"
1847                   " -m 128M"
1848                   " -object memory-backend-ram,size=32M,id=ram0"
1849                   " -object memory-backend-ram,size=32M,id=ram1"
1850                   " -object memory-backend-ram,size=64M,id=ram2"
1851                   " -numa node,nodeid=0,memdev=ram0"
1852                   " -numa node,nodeid=1,memdev=ram1"
1853                   " -numa node,nodeid=2,memdev=ram2"
1854                   " -numa cpu,node-id=0,socket-id=0"
1855                   " -numa cpu,node-id=0,socket-id=0"
1856                   " -numa cpu,node-id=1,socket-id=1"
1857                   " -numa cpu,node-id=1,socket-id=1"
1858                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1859                   "data-type=access-latency,latency=10"
1860                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1861                   "data-type=access-bandwidth,bandwidth=10485760"
1862                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1863                   "data-type=access-latency,latency=20"
1864                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1865                   "data-type=access-bandwidth,bandwidth=5242880"
1866                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1867                   "data-type=access-latency,latency=30"
1868                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1869                   "data-type=access-bandwidth,bandwidth=1048576"
1870                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1871                   "data-type=access-latency,latency=20"
1872                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1873                   "data-type=access-bandwidth,bandwidth=5242880"
1874                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1875                   "data-type=access-latency,latency=10"
1876                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1877                   "data-type=access-bandwidth,bandwidth=10485760"
1878                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1879                   "data-type=access-latency,latency=30"
1880                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1881                   "data-type=access-bandwidth,bandwidth=1048576",
1882                   &data);
1883     free_test_data(&data);
1884 }
1885 
1886 #ifdef CONFIG_POSIX
1887 static void test_acpi_erst(const char *machine)
1888 {
1889     gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
1890     gchar *params;
1891     test_data data = {};
1892 
1893     data.machine = machine;
1894     data.variant = ".acpierst";
1895     params = g_strdup_printf(
1896         " -object memory-backend-file,id=erstnvram,"
1897             "mem-path=%s,size=0x10000,share=on"
1898         " -device acpi-erst,memdev=erstnvram", tmp_path);
1899     test_acpi_one(params, &data);
1900     free_test_data(&data);
1901     g_free(params);
1902     g_assert(g_rmdir(tmp_path) == 0);
1903     g_free(tmp_path);
1904 }
1905 
1906 static void test_acpi_piix4_acpi_erst(void)
1907 {
1908     test_acpi_erst(MACHINE_PC);
1909 }
1910 
1911 static void test_acpi_q35_acpi_erst(void)
1912 {
1913     test_acpi_erst(MACHINE_Q35);
1914 }
1915 
1916 static void test_acpi_microvm_acpi_erst(void)
1917 {
1918     gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
1919     gchar *params;
1920     test_data data = {};
1921 
1922     test_acpi_microvm_prepare(&data);
1923     data.variant = ".pcie";
1924     data.tcg_only = true; /* need constant host-phys-bits */
1925     params = g_strdup_printf(" -machine microvm,"
1926         "acpi=on,ioapic2=off,rtc=off,pcie=on"
1927         " -object memory-backend-file,id=erstnvram,"
1928            "mem-path=%s,size=0x10000,share=on"
1929         " -device acpi-erst,memdev=erstnvram", tmp_path);
1930     test_acpi_one(params, &data);
1931     g_free(params);
1932     g_assert(g_rmdir(tmp_path) == 0);
1933     g_free(tmp_path);
1934     free_test_data(&data);
1935 }
1936 #endif /* CONFIG_POSIX */
1937 
1938 static void test_acpi_aarch64_virt_tcg(void)
1939 {
1940     test_data data = {
1941         .machine = "virt",
1942         .arch = "aarch64",
1943         .tcg_only = true,
1944         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1945         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1946         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1947         .ram_start = 0x40000000ULL,
1948         .scan_len = 128ULL * 1024 * 1024,
1949     };
1950 
1951     data.smbios_cpu_max_speed = 2900;
1952     data.smbios_cpu_curr_speed = 2700;
1953     test_acpi_one("-cpu cortex-a57 "
1954                   "-smbios type=4,max-speed=2900,current-speed=2700", &data);
1955     free_test_data(&data);
1956 }
1957 
1958 static void test_acpi_aarch64_virt_tcg_topology(void)
1959 {
1960     test_data data = {
1961         .machine = "virt",
1962         .arch = "aarch64",
1963         .variant = ".topology",
1964         .tcg_only = true,
1965         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1966         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1967         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1968         .ram_start = 0x40000000ULL,
1969         .scan_len = 128ULL * 1024 * 1024,
1970     };
1971 
1972     test_acpi_one("-cpu cortex-a57 "
1973                   "-smp sockets=1,clusters=2,cores=2,threads=2", &data);
1974     free_test_data(&data);
1975 }
1976 
1977 static void test_acpi_q35_viot(void)
1978 {
1979     test_data data = {
1980         .machine = MACHINE_Q35,
1981         .variant = ".viot",
1982     };
1983 
1984     /*
1985      * To keep things interesting, two buses bypass the IOMMU.
1986      * VIOT should only describes the other two buses.
1987      */
1988     test_acpi_one("-machine default_bus_bypass_iommu=on "
1989                   "-device virtio-iommu-pci "
1990                   "-device pxb-pcie,bus_nr=0x10,id=pcie.100,bus=pcie.0 "
1991                   "-device pxb-pcie,bus_nr=0x20,id=pcie.200,bus=pcie.0,bypass_iommu=on "
1992                   "-device pxb-pcie,bus_nr=0x30,id=pcie.300,bus=pcie.0",
1993                   &data);
1994     free_test_data(&data);
1995 }
1996 
1997 #ifdef CONFIG_POSIX
1998 static void test_acpi_q35_cxl(void)
1999 {
2000     gchar *tmp_path = g_dir_make_tmp("qemu-test-cxl.XXXXXX", NULL);
2001     gchar *params;
2002 
2003     test_data data = {
2004         .machine = MACHINE_Q35,
2005         .variant = ".cxl",
2006     };
2007     /*
2008      * A complex CXL setup.
2009      */
2010     params = g_strdup_printf(" -machine cxl=on"
2011                              " -object memory-backend-file,id=cxl-mem1,mem-path=%s,size=256M"
2012                              " -object memory-backend-file,id=cxl-mem2,mem-path=%s,size=256M"
2013                              " -object memory-backend-file,id=cxl-mem3,mem-path=%s,size=256M"
2014                              " -object memory-backend-file,id=cxl-mem4,mem-path=%s,size=256M"
2015                              " -object memory-backend-file,id=lsa1,mem-path=%s,size=256M"
2016                              " -object memory-backend-file,id=lsa2,mem-path=%s,size=256M"
2017                              " -object memory-backend-file,id=lsa3,mem-path=%s,size=256M"
2018                              " -object memory-backend-file,id=lsa4,mem-path=%s,size=256M"
2019                              " -device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1"
2020                              " -device pxb-cxl,bus_nr=222,bus=pcie.0,id=cxl.2"
2021                              " -device cxl-rp,port=0,bus=cxl.1,id=rp1,chassis=0,slot=2"
2022                              " -device cxl-type3,bus=rp1,persistent-memdev=cxl-mem1,lsa=lsa1"
2023                              " -device cxl-rp,port=1,bus=cxl.1,id=rp2,chassis=0,slot=3"
2024                              " -device cxl-type3,bus=rp2,persistent-memdev=cxl-mem2,lsa=lsa2"
2025                              " -device cxl-rp,port=0,bus=cxl.2,id=rp3,chassis=0,slot=5"
2026                              " -device cxl-type3,bus=rp3,persistent-memdev=cxl-mem3,lsa=lsa3"
2027                              " -device cxl-rp,port=1,bus=cxl.2,id=rp4,chassis=0,slot=6"
2028                              " -device cxl-type3,bus=rp4,persistent-memdev=cxl-mem4,lsa=lsa4"
2029                              " -M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G,cxl-fmw.0.interleave-granularity=8k,"
2030                              "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",
2031                              tmp_path, tmp_path, tmp_path, tmp_path,
2032                              tmp_path, tmp_path, tmp_path, tmp_path);
2033     test_acpi_one(params, &data);
2034 
2035     g_free(params);
2036     g_assert(g_rmdir(tmp_path) == 0);
2037     g_free(tmp_path);
2038     free_test_data(&data);
2039 }
2040 #endif /* CONFIG_POSIX */
2041 
2042 static void test_acpi_aarch64_virt_viot(void)
2043 {
2044     test_data data = {
2045         .machine = "virt",
2046         .arch = "aarch64",
2047         .tcg_only = true,
2048         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
2049         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
2050         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
2051         .ram_start = 0x40000000ULL,
2052         .scan_len = 128ULL * 1024 * 1024,
2053     };
2054 
2055     test_acpi_one("-cpu cortex-a57 "
2056                   "-device virtio-iommu-pci", &data);
2057     free_test_data(&data);
2058 }
2059 
2060 #ifndef _WIN32
2061 # define DEV_NULL "/dev/null"
2062 #else
2063 # define DEV_NULL "nul"
2064 #endif
2065 
2066 static void test_acpi_q35_slic(void)
2067 {
2068     test_data data = {
2069         .machine = MACHINE_Q35,
2070         .variant = ".slic",
2071     };
2072 
2073     test_acpi_one("-acpitable sig=SLIC,oem_id=\"CRASH \",oem_table_id=ME,"
2074                   "oem_rev=00002210,asl_compiler_id=qemu,"
2075                   "asl_compiler_rev=00000000,data=" DEV_NULL,
2076                   &data);
2077     free_test_data(&data);
2078 }
2079 
2080 static void test_acpi_q35_applesmc(void)
2081 {
2082     test_data data = {
2083         .machine = MACHINE_Q35,
2084         .variant = ".applesmc",
2085     };
2086 
2087     /* supply fake 64-byte OSK to silence missing key warning */
2088     test_acpi_one("-device isa-applesmc,osk=any64characterfakeoskisenough"
2089                   "topreventinvalidkeywarningsonstderr", &data);
2090     free_test_data(&data);
2091 }
2092 
2093 static void test_acpi_q35_pvpanic_isa(void)
2094 {
2095     test_data data = {
2096         .machine = MACHINE_Q35,
2097         .variant = ".pvpanic-isa",
2098     };
2099 
2100     test_acpi_one("-device pvpanic", &data);
2101     free_test_data(&data);
2102 }
2103 
2104 static void test_acpi_pc_smbios_options(void)
2105 {
2106     uint8_t req_type11[] = { 11 };
2107     test_data data = {
2108         .machine = MACHINE_PC,
2109         .variant = ".pc_smbios_options",
2110         .required_struct_types = req_type11,
2111         .required_struct_types_len = ARRAY_SIZE(req_type11),
2112     };
2113 
2114     test_smbios("-smbios type=11,value=TEST", &data);
2115     free_test_data(&data);
2116 }
2117 
2118 static void test_acpi_pc_smbios_blob(void)
2119 {
2120     uint8_t req_type11[] = { 11 };
2121     test_data data = {
2122         .machine = MACHINE_PC,
2123         .variant = ".pc_smbios_blob",
2124         .required_struct_types = req_type11,
2125         .required_struct_types_len = ARRAY_SIZE(req_type11),
2126     };
2127 
2128     test_smbios("-machine smbios-entry-point-type=32 "
2129                 "-smbios file=tests/data/smbios/type11_blob", &data);
2130     free_test_data(&data);
2131 }
2132 
2133 static void test_acpi_isapc_smbios_legacy(void)
2134 {
2135     uint8_t req_type11[] = { 1, 11 };
2136     test_data data = {
2137         .machine = "isapc",
2138         .variant = ".pc_smbios_legacy",
2139         .required_struct_types = req_type11,
2140         .required_struct_types_len = ARRAY_SIZE(req_type11),
2141     };
2142 
2143     test_smbios("-smbios file=tests/data/smbios/type11_blob.legacy "
2144                 "-smbios type=1,family=TEST", &data);
2145     free_test_data(&data);
2146 }
2147 
2148 static void test_oem_fields(test_data *data)
2149 {
2150     int i;
2151 
2152     for (i = 0; i < data->tables->len; ++i) {
2153         AcpiSdtTable *sdt;
2154 
2155         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
2156         /* FACS doesn't have OEMID and OEMTABLEID fields */
2157         if (compare_signature(sdt, "FACS")) {
2158             continue;
2159         }
2160 
2161         g_assert(strncmp((char *)sdt->aml + 10, OEM_ID, 6) == 0);
2162         g_assert(strncmp((char *)sdt->aml + 16, OEM_TABLE_ID, 8) == 0);
2163     }
2164 }
2165 
2166 static void test_acpi_piix4_oem_fields(void)
2167 {
2168     char *args;
2169     test_data data = {};
2170 
2171     data.machine = MACHINE_PC;
2172     data.required_struct_types = base_required_struct_types;
2173     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
2174 
2175     args = test_acpi_create_args(&data, OEM_TEST_ARGS);
2176     data.qts = qtest_init(args);
2177     test_acpi_load_tables(&data);
2178     test_oem_fields(&data);
2179     qtest_quit(data.qts);
2180     free_test_data(&data);
2181     g_free(args);
2182 }
2183 
2184 static void test_acpi_q35_oem_fields(void)
2185 {
2186     char *args;
2187     test_data data = {};
2188 
2189     data.machine = MACHINE_Q35;
2190     data.required_struct_types = base_required_struct_types;
2191     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
2192 
2193     args = test_acpi_create_args(&data, OEM_TEST_ARGS);
2194     data.qts = qtest_init(args);
2195     test_acpi_load_tables(&data);
2196     test_oem_fields(&data);
2197     qtest_quit(data.qts);
2198     free_test_data(&data);
2199     g_free(args);
2200 }
2201 
2202 static void test_acpi_microvm_oem_fields(void)
2203 {
2204     test_data data = {};
2205     char *args;
2206 
2207     test_acpi_microvm_prepare(&data);
2208 
2209     args = test_acpi_create_args(&data,
2210                                  OEM_TEST_ARGS",acpi=on");
2211     data.qts = qtest_init(args);
2212     test_acpi_load_tables(&data);
2213     test_oem_fields(&data);
2214     qtest_quit(data.qts);
2215     free_test_data(&data);
2216     g_free(args);
2217 }
2218 
2219 static void test_acpi_aarch64_virt_oem_fields(void)
2220 {
2221     test_data data = {
2222         .machine = "virt",
2223         .arch = "aarch64",
2224         .tcg_only = true,
2225         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
2226         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
2227         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
2228         .ram_start = 0x40000000ULL,
2229         .scan_len = 128ULL * 1024 * 1024,
2230     };
2231     char *args;
2232 
2233     args = test_acpi_create_args(&data, "-cpu cortex-a57 "OEM_TEST_ARGS);
2234     data.qts = qtest_init(args);
2235     test_acpi_load_tables(&data);
2236     test_oem_fields(&data);
2237     qtest_quit(data.qts);
2238     free_test_data(&data);
2239     g_free(args);
2240 }
2241 
2242 
2243 int main(int argc, char *argv[])
2244 {
2245     const char *arch = qtest_get_arch();
2246     bool has_kvm, has_tcg;
2247     char *v_env = getenv("V");
2248     int ret;
2249 
2250     if (v_env) {
2251         verbosity_level = atoi(v_env);
2252     }
2253 
2254     g_test_init(&argc, &argv, NULL);
2255 
2256     has_kvm = qtest_has_accel("kvm");
2257     has_tcg = qtest_has_accel("tcg");
2258 
2259     if (!has_tcg && !has_kvm) {
2260         g_test_skip("No KVM or TCG accelerator available");
2261         return 0;
2262     }
2263 
2264     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
2265         ret = boot_sector_init(disk);
2266         if (ret) {
2267             return ret;
2268         }
2269         if (qtest_has_machine(MACHINE_PC)) {
2270             qtest_add_func("acpi/piix4", test_acpi_piix4_tcg);
2271             qtest_add_func("acpi/piix4/oem-fields", test_acpi_piix4_oem_fields);
2272             qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge);
2273             qtest_add_func("acpi/piix4/pci-hotplug/no_root_hotplug",
2274                            test_acpi_piix4_no_root_hotplug);
2275             qtest_add_func("acpi/piix4/pci-hotplug/no_bridge_hotplug",
2276                            test_acpi_piix4_no_bridge_hotplug);
2277             qtest_add_func("acpi/piix4/pci-hotplug/off",
2278                            test_acpi_piix4_no_acpi_pci_hotplug);
2279             qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi);
2280             qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp);
2281             qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
2282             qtest_add_func("acpi/piix4/nosmm", test_acpi_piix4_tcg_nosmm);
2283             qtest_add_func("acpi/piix4/smm-compat",
2284                            test_acpi_piix4_tcg_smm_compat);
2285             qtest_add_func("acpi/piix4/smm-compat-nosmm",
2286                            test_acpi_piix4_tcg_smm_compat_nosmm);
2287             qtest_add_func("acpi/piix4/nohpet", test_acpi_piix4_tcg_nohpet);
2288 
2289             /* i386 does not support memory hotplug */
2290             if (strcmp(arch, "i386")) {
2291                 qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
2292                 qtest_add_func("acpi/piix4/dimmpxm",
2293                                test_acpi_piix4_tcg_dimm_pxm);
2294                 qtest_add_func("acpi/piix4/acpihmat",
2295                                test_acpi_piix4_tcg_acpi_hmat);
2296             }
2297 #ifdef CONFIG_POSIX
2298             qtest_add_func("acpi/piix4/acpierst", test_acpi_piix4_acpi_erst);
2299 #endif
2300             qtest_add_func("acpi/piix4/smbios-options",
2301                            test_acpi_pc_smbios_options);
2302             qtest_add_func("acpi/piix4/smbios-blob",
2303                            test_acpi_pc_smbios_blob);
2304             qtest_add_func("acpi/piix4/smbios-legacy",
2305                            test_acpi_isapc_smbios_legacy);
2306         }
2307         if (qtest_has_machine(MACHINE_Q35)) {
2308             qtest_add_func("acpi/q35", test_acpi_q35_tcg);
2309             qtest_add_func("acpi/q35/oem-fields", test_acpi_q35_oem_fields);
2310             if (tpm_model_is_available("-machine q35", "tpm-tis")) {
2311                 qtest_add_func("acpi/q35/tpm2-tis", test_acpi_q35_tcg_tpm2_tis);
2312                 qtest_add_func("acpi/q35/tpm12-tis",
2313                                test_acpi_q35_tcg_tpm12_tis);
2314             }
2315             qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
2316             qtest_add_func("acpi/q35/no-acpi-hotplug",
2317                            test_acpi_q35_tcg_no_acpi_hotplug);
2318             qtest_add_func("acpi/q35/multif-bridge",
2319                            test_acpi_q35_multif_bridge);
2320             qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi);
2321             qtest_add_func("acpi/q35/smbus/ipmi", test_acpi_q35_tcg_smbus_ipmi);
2322             qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp);
2323             qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
2324             qtest_add_func("acpi/q35/nosmm", test_acpi_q35_tcg_nosmm);
2325             qtest_add_func("acpi/q35/smm-compat",
2326                            test_acpi_q35_tcg_smm_compat);
2327             qtest_add_func("acpi/q35/smm-compat-nosmm",
2328                            test_acpi_q35_tcg_smm_compat_nosmm);
2329             qtest_add_func("acpi/q35/nohpet", test_acpi_q35_tcg_nohpet);
2330             qtest_add_func("acpi/q35/acpihmat-noinitiator",
2331                            test_acpi_q35_tcg_acpi_hmat_noinitiator);
2332 
2333             /* i386 does not support memory hotplug */
2334             if (strcmp(arch, "i386")) {
2335                 qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
2336                 qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm);
2337                 qtest_add_func("acpi/q35/acpihmat",
2338                                test_acpi_q35_tcg_acpi_hmat);
2339                 qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64);
2340             }
2341 #ifdef CONFIG_POSIX
2342             qtest_add_func("acpi/q35/acpierst", test_acpi_q35_acpi_erst);
2343 #endif
2344             qtest_add_func("acpi/q35/applesmc", test_acpi_q35_applesmc);
2345             qtest_add_func("acpi/q35/pvpanic-isa", test_acpi_q35_pvpanic_isa);
2346             if (has_tcg) {
2347                 qtest_add_func("acpi/q35/ivrs", test_acpi_q35_tcg_ivrs);
2348             }
2349             if (has_kvm) {
2350                 qtest_add_func("acpi/q35/kvm/xapic", test_acpi_q35_kvm_xapic);
2351                 qtest_add_func("acpi/q35/kvm/dmar", test_acpi_q35_kvm_dmar);
2352                 qtest_add_func("acpi/q35/type4-count",
2353                                test_acpi_q35_kvm_type4_count);
2354                 qtest_add_func("acpi/q35/core-count",
2355                                test_acpi_q35_kvm_core_count);
2356                 qtest_add_func("acpi/q35/core-count2",
2357                                test_acpi_q35_kvm_core_count2);
2358                 qtest_add_func("acpi/q35/thread-count",
2359                                test_acpi_q35_kvm_thread_count);
2360                 qtest_add_func("acpi/q35/thread-count2",
2361                                test_acpi_q35_kvm_thread_count2);
2362             }
2363             if (qtest_has_device("virtio-iommu-pci")) {
2364                 qtest_add_func("acpi/q35/viot", test_acpi_q35_viot);
2365             }
2366 #ifdef CONFIG_POSIX
2367             qtest_add_func("acpi/q35/cxl", test_acpi_q35_cxl);
2368 #endif
2369             qtest_add_func("acpi/q35/slic", test_acpi_q35_slic);
2370         }
2371         if (qtest_has_machine("microvm")) {
2372             qtest_add_func("acpi/microvm", test_acpi_microvm_tcg);
2373             qtest_add_func("acpi/microvm/usb", test_acpi_microvm_usb_tcg);
2374             qtest_add_func("acpi/microvm/rtc", test_acpi_microvm_rtc_tcg);
2375             qtest_add_func("acpi/microvm/ioapic2",
2376                            test_acpi_microvm_ioapic2_tcg);
2377             qtest_add_func("acpi/microvm/oem-fields",
2378                            test_acpi_microvm_oem_fields);
2379             if (has_tcg) {
2380                 if (strcmp(arch, "x86_64") == 0) {
2381                     qtest_add_func("acpi/microvm/pcie",
2382                                    test_acpi_microvm_pcie_tcg);
2383 #ifdef CONFIG_POSIX
2384                     qtest_add_func("acpi/microvm/acpierst",
2385                                    test_acpi_microvm_acpi_erst);
2386 #endif
2387                 }
2388             }
2389         }
2390     } else if (strcmp(arch, "aarch64") == 0) {
2391         if (has_tcg && qtest_has_device("virtio-blk-pci")) {
2392             qtest_add_func("acpi/virt", test_acpi_aarch64_virt_tcg);
2393             qtest_add_func("acpi/virt/acpihmatvirt",
2394                            test_acpi_aarch64_virt_tcg_acpi_hmat);
2395             qtest_add_func("acpi/virt/topology",
2396                            test_acpi_aarch64_virt_tcg_topology);
2397             qtest_add_func("acpi/virt/numamem",
2398                            test_acpi_aarch64_virt_tcg_numamem);
2399             qtest_add_func("acpi/virt/memhp", test_acpi_aarch64_virt_tcg_memhp);
2400             qtest_add_func("acpi/virt/pxb", test_acpi_aarch64_virt_tcg_pxb);
2401             qtest_add_func("acpi/virt/oem-fields",
2402                            test_acpi_aarch64_virt_oem_fields);
2403             if (qtest_has_device("virtio-iommu-pci")) {
2404                 qtest_add_func("acpi/virt/viot", test_acpi_aarch64_virt_viot);
2405             }
2406         }
2407     }
2408     ret = g_test_run();
2409     boot_sector_cleanup(disk);
2410     return ret;
2411 }
2412