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