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