xref: /openbmc/qemu/tests/qtest/bios-tables-test.c (revision 41bcea7b)
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=1
28  * this will produce a bunch of warnings about differences
29  * beween actual and expected ACPI tables. If you have IASL installed,
30  * they will also be disassembled so you can look at the disassembled
31  * output. If not - disassemble them yourself in any way you like.
32  * Look at the differences - make sure they make sense and match what the
33  * changes you are merging are supposed to do.
34  * Save the changes, preferably in form of ASL diff for the commit log in
35  * step 6.
36  *
37  * 5. From build directory, run:
38  *      $(SRC_PATH)/tests/data/acpi/rebuild-expected-aml.sh
39  * 6. Now commit any changes to the expected binary, include diff from step 4
40  *    in commit log.
41  *    Expected binary updates needs to be a separate patch from the code that
42  *    introduces changes to ACPI tables. It lets the maintainer drop
43  *    and regenerate binary updates in case of merge conflicts. Further, a code
44  *    change is easily reviewable but a binary blob is not (without doing a
45  *    disassembly).
46  * 7. Before sending patches to the list (Contributor)
47  *    or before doing a pull request (Maintainer), make sure
48  *    tests/qtest/bios-tables-test-allowed-diff.h is empty - this will ensure
49  *    following changes to ACPI tables will be noticed.
50  *
51  * The resulting patchset/pull request then looks like this:
52  * - patch 1: list changed files in tests/qtest/bios-tables-test-allowed-diff.h.
53  * - patches 2 - n: real changes, may contain multiple patches.
54  * - patch n + 1: update golden master binaries and empty
55  *   tests/qtest/bios-tables-test-allowed-diff.h
56  */
57 
58 #include "qemu/osdep.h"
59 #include <glib/gstdio.h>
60 #include "qemu-common.h"
61 #include "hw/firmware/smbios.h"
62 #include "qemu/bitmap.h"
63 #include "acpi-utils.h"
64 #include "boot-sector.h"
65 #include "tpm-emu.h"
66 #include "hw/acpi/tpm.h"
67 #include "qemu/cutils.h"
68 
69 #define MACHINE_PC "pc"
70 #define MACHINE_Q35 "q35"
71 
72 #define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"
73 
74 #define OEM_ID             "TEST"
75 #define OEM_TABLE_ID       "OEM"
76 #define OEM_TEST_ARGS      "-machine x-oem-id=" OEM_ID ",x-oem-table-id=" \
77                            OEM_TABLE_ID
78 
79 typedef struct {
80     bool tcg_only;
81     const char *machine;
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     uint32_t smbios_ep_addr;
93     struct smbios_21_entry_point smbios_ep_table;
94     uint16_t smbios_cpu_max_speed;
95     uint16_t smbios_cpu_curr_speed;
96     uint8_t *required_struct_types;
97     int required_struct_types_len;
98     QTestState *qts;
99 } test_data;
100 
101 static char disk[] = "tests/acpi-test-disk-XXXXXX";
102 static const char *data_dir = "tests/data/acpi";
103 #ifdef CONFIG_IASL
104 static const char *iasl = CONFIG_IASL;
105 #else
106 static const char *iasl;
107 #endif
108 
109 static bool compare_signature(const AcpiSdtTable *sdt, const char *signature)
110 {
111    return !memcmp(sdt->aml, signature, 4);
112 }
113 
114 static void cleanup_table_descriptor(AcpiSdtTable *table)
115 {
116     g_free(table->aml);
117     if (table->aml_file &&
118         !table->tmp_files_retain &&
119         g_strstr_len(table->aml_file, -1, "aml-")) {
120         unlink(table->aml_file);
121     }
122     g_free(table->aml_file);
123     g_free(table->asl);
124     if (table->asl_file &&
125         !table->tmp_files_retain) {
126         unlink(table->asl_file);
127     }
128     g_free(table->asl_file);
129 }
130 
131 static void free_test_data(test_data *data)
132 {
133     int i;
134 
135     if (!data->tables) {
136         return;
137     }
138     for (i = 0; i < data->tables->len; ++i) {
139         cleanup_table_descriptor(&g_array_index(data->tables, AcpiSdtTable, i));
140     }
141 
142     g_array_free(data->tables, true);
143 }
144 
145 static void test_acpi_rsdp_table(test_data *data)
146 {
147     uint8_t *rsdp_table = data->rsdp_table;
148 
149     acpi_fetch_rsdp_table(data->qts, data->rsdp_addr, rsdp_table);
150 
151     switch (rsdp_table[15 /* Revision offset */]) {
152     case 0: /* ACPI 1.0 RSDP */
153         /* With rev 1, checksum is only for the first 20 bytes */
154         g_assert(!acpi_calc_checksum(rsdp_table,  20));
155         break;
156     case 2: /* ACPI 2.0+ RSDP */
157         /* With revision 2, we have 2 checksums */
158         g_assert(!acpi_calc_checksum(rsdp_table, 20));
159         g_assert(!acpi_calc_checksum(rsdp_table, 36));
160         break;
161     default:
162         g_assert_not_reached();
163     }
164 }
165 
166 static void test_acpi_rxsdt_table(test_data *data)
167 {
168     const char *sig = "RSDT";
169     AcpiSdtTable rsdt = {};
170     int entry_size = 4;
171     int addr_off = 16 /* RsdtAddress */;
172     uint8_t *ent;
173 
174     if (data->rsdp_table[15 /* Revision offset */] != 0) {
175         addr_off = 24 /* XsdtAddress */;
176         entry_size = 8;
177         sig = "XSDT";
178     }
179     /* read [RX]SDT table */
180     acpi_fetch_table(data->qts, &rsdt.aml, &rsdt.aml_len,
181                      &data->rsdp_table[addr_off], entry_size, sig, true);
182 
183     /* Load all tables and add to test list directly RSDT referenced tables */
184     ACPI_FOREACH_RSDT_ENTRY(rsdt.aml, rsdt.aml_len, ent, entry_size) {
185         AcpiSdtTable ssdt_table = {};
186 
187         acpi_fetch_table(data->qts, &ssdt_table.aml, &ssdt_table.aml_len, ent,
188                          entry_size, NULL, true);
189         /* Add table to ASL test tables list */
190         g_array_append_val(data->tables, ssdt_table);
191     }
192     cleanup_table_descriptor(&rsdt);
193 }
194 
195 static void test_acpi_fadt_table(test_data *data)
196 {
197     /* FADT table is 1st */
198     AcpiSdtTable table = g_array_index(data->tables, typeof(table), 0);
199     uint8_t *fadt_aml = table.aml;
200     uint32_t fadt_len = table.aml_len;
201     uint32_t val;
202     int dsdt_offset = 40 /* DSDT */;
203     int dsdt_entry_size = 4;
204 
205     g_assert(compare_signature(&table, "FACP"));
206 
207     /* Since DSDT/FACS isn't in RSDT, add them to ASL test list manually */
208     memcpy(&val, fadt_aml + 112 /* Flags */, 4);
209     val = le32_to_cpu(val);
210     if (!(val & 1UL << 20 /* HW_REDUCED_ACPI */)) {
211         acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
212                          fadt_aml + 36 /* FIRMWARE_CTRL */, 4, "FACS", false);
213         g_array_append_val(data->tables, table);
214     }
215 
216     memcpy(&val, fadt_aml + dsdt_offset, 4);
217     val = le32_to_cpu(val);
218     if (!val) {
219         dsdt_offset = 140 /* X_DSDT */;
220         dsdt_entry_size = 8;
221     }
222     acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
223                      fadt_aml + dsdt_offset, dsdt_entry_size, "DSDT", true);
224     g_array_append_val(data->tables, table);
225 
226     memset(fadt_aml + 36, 0, 4); /* sanitize FIRMWARE_CTRL ptr */
227     memset(fadt_aml + 40, 0, 4); /* sanitize DSDT ptr */
228     if (fadt_aml[8 /* FADT Major Version */] >= 3) {
229         memset(fadt_aml + 132, 0, 8); /* sanitize X_FIRMWARE_CTRL ptr */
230         memset(fadt_aml + 140, 0, 8); /* sanitize X_DSDT ptr */
231     }
232 
233     /* update checksum */
234     fadt_aml[9 /* Checksum */] = 0;
235     fadt_aml[9 /* Checksum */] -= acpi_calc_checksum(fadt_aml, fadt_len);
236 }
237 
238 static void dump_aml_files(test_data *data, bool rebuild)
239 {
240     AcpiSdtTable *sdt;
241     GError *error = NULL;
242     gchar *aml_file = NULL;
243     gint fd;
244     ssize_t ret;
245     int i;
246 
247     for (i = 0; i < data->tables->len; ++i) {
248         const char *ext = data->variant ? data->variant : "";
249         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
250         g_assert(sdt->aml);
251 
252         if (rebuild) {
253             aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
254                                        sdt->aml, ext);
255             fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
256                         S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
257             if (fd < 0) {
258                 perror(aml_file);
259             }
260             g_assert(fd >= 0);
261         } else {
262             fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
263             g_assert_no_error(error);
264         }
265 
266         ret = qemu_write_full(fd, sdt->aml, sdt->aml_len);
267         g_assert(ret == sdt->aml_len);
268 
269         close(fd);
270 
271         g_free(aml_file);
272     }
273 }
274 
275 static bool create_tmp_asl(AcpiSdtTable *sdt)
276 {
277     GError *error = NULL;
278     gint fd;
279 
280     fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
281     g_assert_no_error(error);
282     close(fd);
283 
284     return false;
285 }
286 
287 static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
288 {
289     AcpiSdtTable *temp;
290     GError *error = NULL;
291     GString *command_line = g_string_new(iasl);
292     gchar *out, *out_err;
293     gboolean ret;
294     int i;
295 
296     create_tmp_asl(sdt);
297 
298     /* build command line */
299     g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
300     if (compare_signature(sdt, "DSDT") ||
301         compare_signature(sdt, "SSDT")) {
302         for (i = 0; i < sdts->len; ++i) {
303             temp = &g_array_index(sdts, AcpiSdtTable, i);
304             if (compare_signature(temp, "DSDT") ||
305                 compare_signature(temp, "SSDT")) {
306                 g_string_append_printf(command_line, "-e %s ", temp->aml_file);
307             }
308         }
309     }
310     g_string_append_printf(command_line, "-d %s", sdt->aml_file);
311 
312     /* pass 'out' and 'out_err' in order to be redirected */
313     ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
314     g_assert_no_error(error);
315     if (ret) {
316         ret = g_file_get_contents(sdt->asl_file, &sdt->asl,
317                                   &sdt->asl_len, &error);
318         g_assert(ret);
319         g_assert_no_error(error);
320         ret = (sdt->asl_len > 0);
321     }
322 
323     g_free(out);
324     g_free(out_err);
325     g_string_free(command_line, true);
326 
327     return !ret;
328 }
329 
330 #define COMMENT_END "*/"
331 #define DEF_BLOCK "DefinitionBlock ("
332 #define BLOCK_NAME_END ","
333 
334 static GString *normalize_asl(gchar *asl_code)
335 {
336     GString *asl = g_string_new(asl_code);
337     gchar *comment, *block_name;
338 
339     /* strip comments (different generation days) */
340     comment = g_strstr_len(asl->str, asl->len, COMMENT_END);
341     if (comment) {
342         comment += strlen(COMMENT_END);
343         while (*comment == '\n') {
344             comment++;
345         }
346         asl = g_string_erase(asl, 0, comment - asl->str);
347     }
348 
349     /* strip def block name (it has file path in it) */
350     if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
351         block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
352         g_assert(block_name);
353         asl = g_string_erase(asl, 0,
354                              block_name + sizeof(BLOCK_NAME_END) - asl->str);
355     }
356 
357     return asl;
358 }
359 
360 static GArray *load_expected_aml(test_data *data)
361 {
362     int i;
363     AcpiSdtTable *sdt;
364     GError *error = NULL;
365     gboolean ret;
366     gsize aml_len;
367 
368     GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
369     if (getenv("V")) {
370         fputc('\n', stderr);
371     }
372     for (i = 0; i < data->tables->len; ++i) {
373         AcpiSdtTable exp_sdt;
374         gchar *aml_file = NULL;
375         const char *ext = data->variant ? data->variant : "";
376 
377         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
378 
379         memset(&exp_sdt, 0, sizeof(exp_sdt));
380 
381 try_again:
382         aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
383                                    sdt->aml, ext);
384         if (getenv("V")) {
385             fprintf(stderr, "Looking for expected file '%s'\n", aml_file);
386         }
387         if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) {
388             exp_sdt.aml_file = aml_file;
389         } else if (*ext != '\0') {
390             /* try fallback to generic (extension less) expected file */
391             ext = "";
392             g_free(aml_file);
393             goto try_again;
394         }
395         g_assert(exp_sdt.aml_file);
396         if (getenv("V")) {
397             fprintf(stderr, "Using expected file '%s'\n", aml_file);
398         }
399         ret = g_file_get_contents(aml_file, (gchar **)&exp_sdt.aml,
400                                   &aml_len, &error);
401         exp_sdt.aml_len = aml_len;
402         g_assert(ret);
403         g_assert_no_error(error);
404         g_assert(exp_sdt.aml);
405         if (!exp_sdt.aml_len) {
406             fprintf(stderr, "Warning! zero length expected file '%s'\n",
407                     aml_file);
408         }
409 
410         g_array_append_val(exp_tables, exp_sdt);
411     }
412 
413     return exp_tables;
414 }
415 
416 static bool test_acpi_find_diff_allowed(AcpiSdtTable *sdt)
417 {
418     const gchar *allowed_diff_file[] = {
419 #include "bios-tables-test-allowed-diff.h"
420         NULL
421     };
422     const gchar **f;
423 
424     for (f = allowed_diff_file; *f; ++f) {
425         if (!g_strcmp0(sdt->aml_file, *f)) {
426             return true;
427         }
428     }
429     return false;
430 }
431 
432 /* test the list of tables in @data->tables against reference tables */
433 static void test_acpi_asl(test_data *data)
434 {
435     int i;
436     AcpiSdtTable *sdt, *exp_sdt;
437     test_data exp_data;
438     gboolean exp_err, err, all_tables_match = true;
439 
440     memset(&exp_data, 0, sizeof(exp_data));
441     exp_data.tables = load_expected_aml(data);
442     dump_aml_files(data, false);
443     for (i = 0; i < data->tables->len; ++i) {
444         GString *asl, *exp_asl;
445 
446         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
447         exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
448 
449         if (sdt->aml_len == exp_sdt->aml_len &&
450             !memcmp(sdt->aml, exp_sdt->aml, sdt->aml_len)) {
451             /* Identical table binaries: no need to disassemble. */
452             continue;
453         }
454 
455         fprintf(stderr,
456                 "acpi-test: Warning! %.4s binary file mismatch. "
457                 "Actual [aml:%s], Expected [aml:%s].\n"
458                 "See source file tests/qtest/bios-tables-test.c "
459                 "for instructions on how to update expected files.\n",
460                 exp_sdt->aml, sdt->aml_file, exp_sdt->aml_file);
461 
462         all_tables_match = all_tables_match &&
463             test_acpi_find_diff_allowed(exp_sdt);
464 
465         /*
466          *  don't try to decompile if IASL isn't present, in this case user
467          * will just 'get binary file mismatch' warnings and test failure
468          */
469         if (!iasl) {
470             continue;
471         }
472 
473         err = load_asl(data->tables, sdt);
474         asl = normalize_asl(sdt->asl);
475 
476         /*
477          * If expected file is empty - it's likely that it was a stub just
478          * created for step 1 above: we do want to decompile the actual one.
479          */
480         if (exp_sdt->aml_len) {
481             exp_err = load_asl(exp_data.tables, exp_sdt);
482             exp_asl = normalize_asl(exp_sdt->asl);
483         } else {
484             exp_err = create_tmp_asl(exp_sdt);
485             exp_asl = g_string_new("");
486         }
487 
488         /* TODO: check for warnings */
489         g_assert(!err || exp_err || !exp_sdt->aml_len);
490 
491         if (g_strcmp0(asl->str, exp_asl->str)) {
492             sdt->tmp_files_retain = true;
493             if (exp_err) {
494                 fprintf(stderr,
495                         "Warning! iasl couldn't parse the expected aml\n");
496             } else {
497                 exp_sdt->tmp_files_retain = true;
498                 fprintf(stderr,
499                         "acpi-test: Warning! %.4s mismatch. "
500                         "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
501                         exp_sdt->aml, sdt->asl_file, sdt->aml_file,
502                         exp_sdt->asl_file, exp_sdt->aml_file);
503                 fflush(stderr);
504                 if (getenv("V")) {
505                     const char *diff_env = getenv("DIFF");
506                     const char *diff_cmd = diff_env ? diff_env : "diff -U 16";
507                     char *diff = g_strdup_printf("%s %s %s", diff_cmd,
508                                                  exp_sdt->asl_file, sdt->asl_file);
509                     int out = dup(STDOUT_FILENO);
510                     int ret G_GNUC_UNUSED;
511                     int dupret;
512 
513                     g_assert(out >= 0);
514                     dupret = dup2(STDERR_FILENO, STDOUT_FILENO);
515                     g_assert(dupret >= 0);
516                     ret = system(diff) ;
517                     dupret = dup2(out, STDOUT_FILENO);
518                     g_assert(dupret >= 0);
519                     close(out);
520                     g_free(diff);
521                 }
522             }
523         }
524         g_string_free(asl, true);
525         g_string_free(exp_asl, true);
526     }
527     if (!iasl && !all_tables_match) {
528         fprintf(stderr, "to see ASL diff between mismatched files install IASL,"
529                 " rebuild QEMU from scratch and re-run tests with V=1"
530                 " environment variable set");
531     }
532     g_assert(all_tables_match);
533 
534     free_test_data(&exp_data);
535 }
536 
537 static bool smbios_ep_table_ok(test_data *data)
538 {
539     struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
540     uint32_t addr = data->smbios_ep_addr;
541 
542     qtest_memread(data->qts, addr, ep_table, sizeof(*ep_table));
543     if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
544         return false;
545     }
546     if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
547         return false;
548     }
549     if (ep_table->structure_table_length == 0) {
550         return false;
551     }
552     if (ep_table->number_of_structures == 0) {
553         return false;
554     }
555     if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
556         acpi_calc_checksum((uint8_t *)ep_table + 0x10,
557                            sizeof *ep_table - 0x10)) {
558         return false;
559     }
560     return true;
561 }
562 
563 static void test_smbios_entry_point(test_data *data)
564 {
565     uint32_t off;
566 
567     /* find smbios entry point structure */
568     for (off = 0xf0000; off < 0x100000; off += 0x10) {
569         uint8_t sig[] = "_SM_";
570         int i;
571 
572         for (i = 0; i < sizeof sig - 1; ++i) {
573             sig[i] = qtest_readb(data->qts, off + i);
574         }
575 
576         if (!memcmp(sig, "_SM_", sizeof sig)) {
577             /* signature match, but is this a valid entry point? */
578             data->smbios_ep_addr = off;
579             if (smbios_ep_table_ok(data)) {
580                 break;
581             }
582         }
583     }
584 
585     g_assert_cmphex(off, <, 0x100000);
586 }
587 
588 static inline bool smbios_single_instance(uint8_t type)
589 {
590     switch (type) {
591     case 0:
592     case 1:
593     case 2:
594     case 3:
595     case 16:
596     case 32:
597     case 127:
598         return true;
599     default:
600         return false;
601     }
602 }
603 
604 static bool smbios_cpu_test(test_data *data, uint32_t addr)
605 {
606     uint16_t expect_speed[2];
607     uint16_t real;
608     int offset[2];
609     int i;
610 
611     /* Check CPU speed for backward compatibility */
612     offset[0] = offsetof(struct smbios_type_4, max_speed);
613     offset[1] = offsetof(struct smbios_type_4, current_speed);
614     expect_speed[0] = data->smbios_cpu_max_speed ? : 2000;
615     expect_speed[1] = data->smbios_cpu_curr_speed ? : 2000;
616 
617     for (i = 0; i < 2; i++) {
618         real = qtest_readw(data->qts, addr + offset[i]);
619         if (real != expect_speed[i]) {
620             fprintf(stderr, "Unexpected SMBIOS CPU speed: real %u expect %u\n",
621                     real, expect_speed[i]);
622             return false;
623         }
624     }
625 
626     return true;
627 }
628 
629 static void test_smbios_structs(test_data *data)
630 {
631     DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };
632     struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
633     uint32_t addr = le32_to_cpu(ep_table->structure_table_address);
634     int i, len, max_len = 0;
635     uint8_t type, prv, crt;
636 
637     /* walk the smbios tables */
638     for (i = 0; i < le16_to_cpu(ep_table->number_of_structures); i++) {
639 
640         /* grab type and formatted area length from struct header */
641         type = qtest_readb(data->qts, addr);
642         g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
643         len = qtest_readb(data->qts, addr + 1);
644 
645         /* single-instance structs must not have been encountered before */
646         if (smbios_single_instance(type)) {
647             g_assert(!test_bit(type, struct_bitmap));
648         }
649         set_bit(type, struct_bitmap);
650 
651         if (type == 4) {
652             g_assert(smbios_cpu_test(data, addr));
653         }
654 
655         /* seek to end of unformatted string area of this struct ("\0\0") */
656         prv = crt = 1;
657         while (prv || crt) {
658             prv = crt;
659             crt = qtest_readb(data->qts, addr + len);
660             len++;
661         }
662 
663         /* keep track of max. struct size */
664         if (max_len < len) {
665             max_len = len;
666             g_assert_cmpuint(max_len, <=, ep_table->max_structure_size);
667         }
668 
669         /* start of next structure */
670         addr += len;
671     }
672 
673     /* total table length and max struct size must match entry point values */
674     g_assert_cmpuint(le16_to_cpu(ep_table->structure_table_length), ==,
675                      addr - le32_to_cpu(ep_table->structure_table_address));
676     g_assert_cmpuint(le16_to_cpu(ep_table->max_structure_size), ==, max_len);
677 
678     /* required struct types must all be present */
679     for (i = 0; i < data->required_struct_types_len; i++) {
680         g_assert(test_bit(data->required_struct_types[i], struct_bitmap));
681     }
682 }
683 
684 static void test_acpi_load_tables(test_data *data, bool use_uefi)
685 {
686     if (use_uefi) {
687         g_assert(data->scan_len);
688         data->rsdp_addr = acpi_find_rsdp_address_uefi(data->qts,
689             data->ram_start, data->scan_len);
690     } else {
691         boot_sector_test(data->qts);
692         data->rsdp_addr = acpi_find_rsdp_address(data->qts);
693         g_assert_cmphex(data->rsdp_addr, <, 0x100000);
694     }
695 
696     data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
697     test_acpi_rsdp_table(data);
698     test_acpi_rxsdt_table(data);
699     test_acpi_fadt_table(data);
700 }
701 
702 static char *test_acpi_create_args(test_data *data, const char *params,
703                                    bool use_uefi)
704 {
705     char *args;
706 
707     if (use_uefi) {
708         /*
709          * TODO: convert '-drive if=pflash' to new syntax (see e33763be7cd3)
710          * when arm/virt boad starts to support it.
711          */
712         if (data->cd) {
713             args = g_strdup_printf("-machine %s %s -accel tcg "
714                 "-nodefaults -nographic "
715                 "-drive if=pflash,format=raw,file=%s,readonly=on "
716                 "-drive if=pflash,format=raw,file=%s,snapshot=on -cdrom %s %s",
717                 data->machine, data->tcg_only ? "" : "-accel kvm",
718                 data->uefi_fl1, data->uefi_fl2, data->cd, params ? params : "");
719         } else {
720             args = g_strdup_printf("-machine %s %s -accel tcg "
721                 "-nodefaults -nographic "
722                 "-drive if=pflash,format=raw,file=%s,readonly=on "
723                 "-drive if=pflash,format=raw,file=%s,snapshot=on %s",
724                 data->machine, data->tcg_only ? "" : "-accel kvm",
725                 data->uefi_fl1, data->uefi_fl2, params ? params : "");
726         }
727     } else {
728         args = g_strdup_printf("-machine %s %s -accel tcg "
729             "-net none -display none %s "
730             "-drive id=hd0,if=none,file=%s,format=raw "
731             "-device %s,drive=hd0 ",
732              data->machine, data->tcg_only ? "" : "-accel kvm",
733              params ? params : "", disk,
734              data->blkdev ?: "ide-hd");
735     }
736     return args;
737 }
738 
739 static void test_acpi_one(const char *params, test_data *data)
740 {
741     char *args;
742     bool use_uefi = data->uefi_fl1 && data->uefi_fl2;
743 
744     args = test_acpi_create_args(data, params, use_uefi);
745     data->qts = qtest_init(args);
746     test_acpi_load_tables(data, use_uefi);
747 
748     if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
749         dump_aml_files(data, true);
750     } else {
751         test_acpi_asl(data);
752     }
753 
754     /*
755      * TODO: make SMBIOS tests work with UEFI firmware,
756      * Bug on uefi-test-tools to provide entry point:
757      * https://bugs.launchpad.net/qemu/+bug/1821884
758      */
759     if (!use_uefi) {
760         test_smbios_entry_point(data);
761         test_smbios_structs(data);
762     }
763 
764     qtest_quit(data->qts);
765     g_free(args);
766 }
767 
768 static uint8_t base_required_struct_types[] = {
769     0, 1, 3, 4, 16, 17, 19, 32, 127
770 };
771 
772 static void test_acpi_piix4_tcg(void)
773 {
774     test_data data;
775 
776     /* Supplying -machine accel argument overrides the default (qtest).
777      * This is to make guest actually run.
778      */
779     memset(&data, 0, sizeof(data));
780     data.machine = MACHINE_PC;
781     data.required_struct_types = base_required_struct_types;
782     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
783     test_acpi_one(NULL, &data);
784     free_test_data(&data);
785 }
786 
787 static void test_acpi_piix4_tcg_bridge(void)
788 {
789     test_data data;
790 
791     memset(&data, 0, sizeof(data));
792     data.machine = MACHINE_PC;
793     data.variant = ".bridge";
794     data.required_struct_types = base_required_struct_types;
795     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
796     test_acpi_one("-device pci-bridge,chassis_nr=1", &data);
797     free_test_data(&data);
798 }
799 
800 static void test_acpi_piix4_no_root_hotplug(void)
801 {
802     test_data data;
803 
804     memset(&data, 0, sizeof(data));
805     data.machine = MACHINE_PC;
806     data.variant = ".roothp";
807     data.required_struct_types = base_required_struct_types;
808     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
809     test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
810                   "-device pci-bridge,chassis_nr=1", &data);
811     free_test_data(&data);
812 }
813 
814 static void test_acpi_piix4_no_bridge_hotplug(void)
815 {
816     test_data data;
817 
818     memset(&data, 0, sizeof(data));
819     data.machine = MACHINE_PC;
820     data.variant = ".hpbridge";
821     data.required_struct_types = base_required_struct_types;
822     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
823     test_acpi_one("-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
824                   "-device pci-bridge,chassis_nr=1", &data);
825     free_test_data(&data);
826 }
827 
828 static void test_acpi_piix4_no_acpi_pci_hotplug(void)
829 {
830     test_data data;
831 
832     memset(&data, 0, sizeof(data));
833     data.machine = MACHINE_PC;
834     data.variant = ".hpbrroot";
835     data.required_struct_types = base_required_struct_types;
836     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
837     test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
838                   "-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
839                   "-device pci-bridge,chassis_nr=1", &data);
840     free_test_data(&data);
841 }
842 
843 static void test_acpi_q35_tcg(void)
844 {
845     test_data data;
846 
847     memset(&data, 0, sizeof(data));
848     data.machine = MACHINE_Q35;
849     data.required_struct_types = base_required_struct_types;
850     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
851     test_acpi_one(NULL, &data);
852     free_test_data(&data);
853 
854     data.smbios_cpu_max_speed = 3000;
855     data.smbios_cpu_curr_speed = 2600;
856     test_acpi_one("-smbios type=4,max-speed=3000,current-speed=2600", &data);
857     free_test_data(&data);
858 }
859 
860 static void test_acpi_q35_tcg_bridge(void)
861 {
862     test_data data;
863 
864     memset(&data, 0, sizeof(data));
865     data.machine = MACHINE_Q35;
866     data.variant = ".bridge";
867     data.required_struct_types = base_required_struct_types;
868     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
869     test_acpi_one("-device pci-bridge,chassis_nr=1",
870                   &data);
871     free_test_data(&data);
872 }
873 
874 static void test_acpi_q35_multif_bridge(void)
875 {
876     test_data data = {
877         .machine = MACHINE_Q35,
878         .variant = ".multi-bridge",
879     };
880     test_acpi_one("-device pcie-root-port,id=pcie-root-port-0,"
881                   "multifunction=on,"
882                   "port=0x0,chassis=1,addr=0x2,bus=pcie.0 "
883                   "-device pcie-root-port,id=pcie-root-port-1,"
884                   "port=0x1,chassis=2,addr=0x3.0x1,bus=pcie.0 "
885                   "-device virtio-balloon,id=balloon0,"
886                   "bus=pcie.0,addr=0x4.0x2",
887                   &data);
888     free_test_data(&data);
889 }
890 
891 static void test_acpi_q35_tcg_mmio64(void)
892 {
893     test_data data = {
894         .machine = MACHINE_Q35,
895         .variant = ".mmio64",
896         .required_struct_types = base_required_struct_types,
897         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types)
898     };
899 
900     test_acpi_one("-m 128M,slots=1,maxmem=2G "
901                   "-object memory-backend-ram,id=ram0,size=128M "
902                   "-numa node,memdev=ram0 "
903                   "-device pci-testdev,membar=2G",
904                   &data);
905     free_test_data(&data);
906 }
907 
908 static void test_acpi_piix4_tcg_cphp(void)
909 {
910     test_data data;
911 
912     memset(&data, 0, sizeof(data));
913     data.machine = MACHINE_PC;
914     data.variant = ".cphp";
915     test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
916                   " -object memory-backend-ram,id=ram0,size=64M"
917                   " -object memory-backend-ram,id=ram1,size=64M"
918                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
919                   " -numa dist,src=0,dst=1,val=21",
920                   &data);
921     free_test_data(&data);
922 }
923 
924 static void test_acpi_q35_tcg_cphp(void)
925 {
926     test_data data;
927 
928     memset(&data, 0, sizeof(data));
929     data.machine = MACHINE_Q35;
930     data.variant = ".cphp";
931     test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
932                   " -object memory-backend-ram,id=ram0,size=64M"
933                   " -object memory-backend-ram,id=ram1,size=64M"
934                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
935                   " -numa dist,src=0,dst=1,val=21",
936                   &data);
937     free_test_data(&data);
938 }
939 
940 static uint8_t ipmi_required_struct_types[] = {
941     0, 1, 3, 4, 16, 17, 19, 32, 38, 127
942 };
943 
944 static void test_acpi_q35_tcg_ipmi(void)
945 {
946     test_data data;
947 
948     memset(&data, 0, sizeof(data));
949     data.machine = MACHINE_Q35;
950     data.variant = ".ipmibt";
951     data.required_struct_types = ipmi_required_struct_types;
952     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
953     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
954                   " -device isa-ipmi-bt,bmc=bmc0",
955                   &data);
956     free_test_data(&data);
957 }
958 
959 static void test_acpi_piix4_tcg_ipmi(void)
960 {
961     test_data data;
962 
963     /* Supplying -machine accel argument overrides the default (qtest).
964      * This is to make guest actually run.
965      */
966     memset(&data, 0, sizeof(data));
967     data.machine = MACHINE_PC;
968     data.variant = ".ipmikcs";
969     data.required_struct_types = ipmi_required_struct_types;
970     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
971     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
972                   " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
973                   &data);
974     free_test_data(&data);
975 }
976 
977 static void test_acpi_q35_tcg_memhp(void)
978 {
979     test_data data;
980 
981     memset(&data, 0, sizeof(data));
982     data.machine = MACHINE_Q35;
983     data.variant = ".memhp";
984     test_acpi_one(" -m 128,slots=3,maxmem=1G"
985                   " -object memory-backend-ram,id=ram0,size=64M"
986                   " -object memory-backend-ram,id=ram1,size=64M"
987                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
988                   " -numa dist,src=0,dst=1,val=21",
989                   &data);
990     free_test_data(&data);
991 }
992 
993 static void test_acpi_piix4_tcg_memhp(void)
994 {
995     test_data data;
996 
997     memset(&data, 0, sizeof(data));
998     data.machine = MACHINE_PC;
999     data.variant = ".memhp";
1000     test_acpi_one(" -m 128,slots=3,maxmem=1G"
1001                   " -object memory-backend-ram,id=ram0,size=64M"
1002                   " -object memory-backend-ram,id=ram1,size=64M"
1003                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1004                   " -numa dist,src=0,dst=1,val=21",
1005                   &data);
1006     free_test_data(&data);
1007 }
1008 
1009 static void test_acpi_piix4_tcg_nosmm(void)
1010 {
1011     test_data data;
1012 
1013     memset(&data, 0, sizeof(data));
1014     data.machine = MACHINE_PC;
1015     data.variant = ".nosmm";
1016     test_acpi_one("-machine smm=off", &data);
1017     free_test_data(&data);
1018 }
1019 
1020 static void test_acpi_piix4_tcg_smm_compat(void)
1021 {
1022     test_data data;
1023 
1024     memset(&data, 0, sizeof(data));
1025     data.machine = MACHINE_PC;
1026     data.variant = ".smm-compat";
1027     test_acpi_one("-global PIIX4_PM.smm-compat=on", &data);
1028     free_test_data(&data);
1029 }
1030 
1031 static void test_acpi_piix4_tcg_smm_compat_nosmm(void)
1032 {
1033     test_data data;
1034 
1035     memset(&data, 0, sizeof(data));
1036     data.machine = MACHINE_PC;
1037     data.variant = ".smm-compat-nosmm";
1038     test_acpi_one("-global PIIX4_PM.smm-compat=on -machine smm=off", &data);
1039     free_test_data(&data);
1040 }
1041 
1042 static void test_acpi_piix4_tcg_nohpet(void)
1043 {
1044     test_data data;
1045 
1046     memset(&data, 0, sizeof(data));
1047     data.machine = MACHINE_PC;
1048     data.variant = ".nohpet";
1049     test_acpi_one("-no-hpet", &data);
1050     free_test_data(&data);
1051 }
1052 
1053 static void test_acpi_q35_tcg_numamem(void)
1054 {
1055     test_data data;
1056 
1057     memset(&data, 0, sizeof(data));
1058     data.machine = MACHINE_Q35;
1059     data.variant = ".numamem";
1060     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1061                   " -numa node -numa node,memdev=ram0", &data);
1062     free_test_data(&data);
1063 }
1064 
1065 static void test_acpi_q35_kvm_xapic(void)
1066 {
1067     test_data data;
1068 
1069     memset(&data, 0, sizeof(data));
1070     data.machine = MACHINE_Q35;
1071     data.variant = ".xapic";
1072     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1073                   " -numa node -numa node,memdev=ram0"
1074                   " -machine kernel-irqchip=on -smp 1,maxcpus=288", &data);
1075     free_test_data(&data);
1076 }
1077 
1078 static void test_acpi_q35_tcg_nosmm(void)
1079 {
1080     test_data data;
1081 
1082     memset(&data, 0, sizeof(data));
1083     data.machine = MACHINE_Q35;
1084     data.variant = ".nosmm";
1085     test_acpi_one("-machine smm=off", &data);
1086     free_test_data(&data);
1087 }
1088 
1089 static void test_acpi_q35_tcg_smm_compat(void)
1090 {
1091     test_data data;
1092 
1093     memset(&data, 0, sizeof(data));
1094     data.machine = MACHINE_Q35;
1095     data.variant = ".smm-compat";
1096     test_acpi_one("-global ICH9-LPC.smm-compat=on", &data);
1097     free_test_data(&data);
1098 }
1099 
1100 static void test_acpi_q35_tcg_smm_compat_nosmm(void)
1101 {
1102     test_data data;
1103 
1104     memset(&data, 0, sizeof(data));
1105     data.machine = MACHINE_Q35;
1106     data.variant = ".smm-compat-nosmm";
1107     test_acpi_one("-global ICH9-LPC.smm-compat=on -machine smm=off", &data);
1108     free_test_data(&data);
1109 }
1110 
1111 static void test_acpi_q35_tcg_nohpet(void)
1112 {
1113     test_data data;
1114 
1115     memset(&data, 0, sizeof(data));
1116     data.machine = MACHINE_Q35;
1117     data.variant = ".nohpet";
1118     test_acpi_one("-no-hpet", &data);
1119     free_test_data(&data);
1120 }
1121 
1122 static void test_acpi_q35_kvm_dmar(void)
1123 {
1124     test_data data;
1125 
1126     memset(&data, 0, sizeof(data));
1127     data.machine = MACHINE_Q35;
1128     data.variant = ".dmar";
1129     test_acpi_one("-machine kernel-irqchip=split -accel kvm"
1130                   " -device intel-iommu,intremap=on,device-iotlb=on", &data);
1131     free_test_data(&data);
1132 }
1133 
1134 static void test_acpi_q35_tcg_ivrs(void)
1135 {
1136     test_data data;
1137 
1138     memset(&data, 0, sizeof(data));
1139     data.machine = MACHINE_Q35;
1140     data.variant = ".ivrs";
1141     data.tcg_only = true,
1142     test_acpi_one(" -device amd-iommu", &data);
1143     free_test_data(&data);
1144 }
1145 
1146 static void test_acpi_piix4_tcg_numamem(void)
1147 {
1148     test_data data;
1149 
1150     memset(&data, 0, sizeof(data));
1151     data.machine = MACHINE_PC;
1152     data.variant = ".numamem";
1153     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1154                   " -numa node -numa node,memdev=ram0", &data);
1155     free_test_data(&data);
1156 }
1157 
1158 uint64_t tpm_tis_base_addr;
1159 
1160 static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
1161                               uint64_t base, enum TPMVersion tpm_version)
1162 {
1163     gchar *tmp_dir_name = g_strdup_printf("qemu-test_acpi_%s_tcg_%s.XXXXXX",
1164                                           machine, tpm_if);
1165     char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
1166     TPMTestState test;
1167     test_data data;
1168     GThread *thread;
1169     const char *suffix = tpm_version == TPM_VERSION_2_0 ? "tpm2" : "tpm12";
1170     char *args, *variant = g_strdup_printf(".%s.%s", tpm_if, suffix);
1171 
1172     tpm_tis_base_addr = base;
1173 
1174     module_call_init(MODULE_INIT_QOM);
1175 
1176     test.addr = g_new0(SocketAddress, 1);
1177     test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
1178     test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
1179     g_mutex_init(&test.data_mutex);
1180     g_cond_init(&test.data_cond);
1181     test.data_cond_signal = false;
1182     test.tpm_version = tpm_version;
1183 
1184     thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
1185     tpm_emu_test_wait_cond(&test);
1186 
1187     memset(&data, 0, sizeof(data));
1188     data.machine = machine;
1189     data.variant = variant;
1190 
1191     args = g_strdup_printf(
1192         " -chardev socket,id=chr,path=%s"
1193         " -tpmdev emulator,id=dev,chardev=chr"
1194         " -device tpm-%s,tpmdev=dev",
1195         test.addr->u.q_unix.path, tpm_if);
1196 
1197     test_acpi_one(args, &data);
1198 
1199     g_thread_join(thread);
1200     g_unlink(test.addr->u.q_unix.path);
1201     qapi_free_SocketAddress(test.addr);
1202     g_rmdir(tmp_path);
1203     g_free(variant);
1204     g_free(tmp_path);
1205     g_free(tmp_dir_name);
1206     g_free(args);
1207     free_test_data(&data);
1208 }
1209 
1210 static void test_acpi_q35_tcg_tpm2_tis(void)
1211 {
1212     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_2_0);
1213 }
1214 
1215 static void test_acpi_q35_tcg_tpm12_tis(void)
1216 {
1217     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_1_2);
1218 }
1219 
1220 static void test_acpi_tcg_dimm_pxm(const char *machine)
1221 {
1222     test_data data;
1223 
1224     memset(&data, 0, sizeof(data));
1225     data.machine = machine;
1226     data.variant = ".dimmpxm";
1227     test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
1228                   " -smp 4,sockets=4"
1229                   " -m 128M,slots=3,maxmem=1G"
1230                   " -object memory-backend-ram,id=ram0,size=32M"
1231                   " -object memory-backend-ram,id=ram1,size=32M"
1232                   " -object memory-backend-ram,id=ram2,size=32M"
1233                   " -object memory-backend-ram,id=ram3,size=32M"
1234                   " -numa node,memdev=ram0,nodeid=0"
1235                   " -numa node,memdev=ram1,nodeid=1"
1236                   " -numa node,memdev=ram2,nodeid=2"
1237                   " -numa node,memdev=ram3,nodeid=3"
1238                   " -numa cpu,node-id=0,socket-id=0"
1239                   " -numa cpu,node-id=1,socket-id=1"
1240                   " -numa cpu,node-id=2,socket-id=2"
1241                   " -numa cpu,node-id=3,socket-id=3"
1242                   " -object memory-backend-ram,id=ram4,size=128M"
1243                   " -object memory-backend-ram,id=nvm0,size=128M"
1244                   " -device pc-dimm,id=dimm0,memdev=ram4,node=1"
1245                   " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
1246                   &data);
1247     free_test_data(&data);
1248 }
1249 
1250 static void test_acpi_q35_tcg_dimm_pxm(void)
1251 {
1252     test_acpi_tcg_dimm_pxm(MACHINE_Q35);
1253 }
1254 
1255 static void test_acpi_piix4_tcg_dimm_pxm(void)
1256 {
1257     test_acpi_tcg_dimm_pxm(MACHINE_PC);
1258 }
1259 
1260 static void test_acpi_virt_tcg_memhp(void)
1261 {
1262     test_data data = {
1263         .machine = "virt",
1264         .tcg_only = true,
1265         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1266         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1267         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1268         .ram_start = 0x40000000ULL,
1269         .scan_len = 256ULL * 1024 * 1024,
1270     };
1271 
1272     data.variant = ".memhp";
1273     test_acpi_one(" -machine nvdimm=on"
1274                   " -cpu cortex-a57"
1275                   " -m 256M,slots=3,maxmem=1G"
1276                   " -object memory-backend-ram,id=ram0,size=128M"
1277                   " -object memory-backend-ram,id=ram1,size=128M"
1278                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1279                   " -numa dist,src=0,dst=1,val=21"
1280                   " -object memory-backend-ram,id=ram2,size=128M"
1281                   " -object memory-backend-ram,id=nvm0,size=128M"
1282                   " -device pc-dimm,id=dimm0,memdev=ram2,node=0"
1283                   " -device nvdimm,id=dimm1,memdev=nvm0,node=1",
1284                   &data);
1285 
1286     free_test_data(&data);
1287 
1288 }
1289 
1290 static void test_acpi_microvm_prepare(test_data *data)
1291 {
1292     memset(data, 0, sizeof(*data));
1293     data->machine = "microvm";
1294     data->required_struct_types = NULL; /* no smbios */
1295     data->required_struct_types_len = 0;
1296     data->blkdev = "virtio-blk-device";
1297 }
1298 
1299 static void test_acpi_microvm_tcg(void)
1300 {
1301     test_data data;
1302 
1303     test_acpi_microvm_prepare(&data);
1304     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off",
1305                   &data);
1306     free_test_data(&data);
1307 }
1308 
1309 static void test_acpi_microvm_usb_tcg(void)
1310 {
1311     test_data data;
1312 
1313     test_acpi_microvm_prepare(&data);
1314     data.variant = ".usb";
1315     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,usb=on,rtc=off",
1316                   &data);
1317     free_test_data(&data);
1318 }
1319 
1320 static void test_acpi_microvm_rtc_tcg(void)
1321 {
1322     test_data data;
1323 
1324     test_acpi_microvm_prepare(&data);
1325     data.variant = ".rtc";
1326     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=on",
1327                   &data);
1328     free_test_data(&data);
1329 }
1330 
1331 static void test_acpi_microvm_pcie_tcg(void)
1332 {
1333     test_data data;
1334 
1335     test_acpi_microvm_prepare(&data);
1336     data.variant = ".pcie";
1337     data.tcg_only = true; /* need constant host-phys-bits */
1338     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off,pcie=on",
1339                   &data);
1340     free_test_data(&data);
1341 }
1342 
1343 static void test_acpi_microvm_ioapic2_tcg(void)
1344 {
1345     test_data data;
1346 
1347     test_acpi_microvm_prepare(&data);
1348     data.variant = ".ioapic2";
1349     test_acpi_one(" -machine microvm,acpi=on,ioapic2=on,rtc=off",
1350                   &data);
1351     free_test_data(&data);
1352 }
1353 
1354 static void test_acpi_virt_tcg_numamem(void)
1355 {
1356     test_data data = {
1357         .machine = "virt",
1358         .tcg_only = true,
1359         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1360         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1361         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1362         .ram_start = 0x40000000ULL,
1363         .scan_len = 128ULL * 1024 * 1024,
1364     };
1365 
1366     data.variant = ".numamem";
1367     test_acpi_one(" -cpu cortex-a57"
1368                   " -object memory-backend-ram,id=ram0,size=128M"
1369                   " -numa node,memdev=ram0",
1370                   &data);
1371 
1372     free_test_data(&data);
1373 
1374 }
1375 
1376 static void test_acpi_virt_tcg_pxb(void)
1377 {
1378     test_data data = {
1379         .machine = "virt",
1380         .tcg_only = true,
1381         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1382         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1383         .ram_start = 0x40000000ULL,
1384         .scan_len = 128ULL * 1024 * 1024,
1385     };
1386     /*
1387      * While using -cdrom, the cdrom would auto plugged into pxb-pcie,
1388      * the reason is the bus of pxb-pcie is also root bus, it would lead
1389      * to the error only PCI/PCIE bridge could plug onto pxb.
1390      * Therefore,thr cdrom is defined and plugged onto the scsi controller
1391      * to solve the conflicts.
1392      */
1393     data.variant = ".pxb";
1394     test_acpi_one(" -device pcie-root-port,chassis=1,id=pci.1"
1395                   " -device virtio-scsi-pci,id=scsi0,bus=pci.1"
1396                   " -drive file="
1397                   "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2,"
1398                   "if=none,media=cdrom,id=drive-scsi0-0-0-1,readonly=on"
1399                   " -device scsi-cd,bus=scsi0.0,scsi-id=0,"
1400                   "drive=drive-scsi0-0-0-1,id=scsi0-0-0-1,bootindex=1"
1401                   " -cpu cortex-a57"
1402                   " -device pxb-pcie,bus_nr=128",
1403                   &data);
1404 
1405     free_test_data(&data);
1406 }
1407 
1408 static void test_acpi_tcg_acpi_hmat(const char *machine)
1409 {
1410     test_data data;
1411 
1412     memset(&data, 0, sizeof(data));
1413     data.machine = machine;
1414     data.variant = ".acpihmat";
1415     test_acpi_one(" -machine hmat=on"
1416                   " -smp 2,sockets=2"
1417                   " -m 128M,slots=2,maxmem=1G"
1418                   " -object memory-backend-ram,size=64M,id=m0"
1419                   " -object memory-backend-ram,size=64M,id=m1"
1420                   " -numa node,nodeid=0,memdev=m0"
1421                   " -numa node,nodeid=1,memdev=m1,initiator=0"
1422                   " -numa cpu,node-id=0,socket-id=0"
1423                   " -numa cpu,node-id=0,socket-id=1"
1424                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1425                   "data-type=access-latency,latency=1"
1426                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1427                   "data-type=access-bandwidth,bandwidth=65534M"
1428                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1429                   "data-type=access-latency,latency=65534"
1430                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1431                   "data-type=access-bandwidth,bandwidth=32767M"
1432                   " -numa hmat-cache,node-id=0,size=10K,level=1,"
1433                   "associativity=direct,policy=write-back,line=8"
1434                   " -numa hmat-cache,node-id=1,size=10K,level=1,"
1435                   "associativity=direct,policy=write-back,line=8",
1436                   &data);
1437     free_test_data(&data);
1438 }
1439 
1440 static void test_acpi_q35_tcg_acpi_hmat(void)
1441 {
1442     test_acpi_tcg_acpi_hmat(MACHINE_Q35);
1443 }
1444 
1445 static void test_acpi_piix4_tcg_acpi_hmat(void)
1446 {
1447     test_acpi_tcg_acpi_hmat(MACHINE_PC);
1448 }
1449 
1450 static void test_acpi_erst(const char *machine)
1451 {
1452     gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
1453     gchar *params;
1454     test_data data;
1455 
1456     memset(&data, 0, sizeof(data));
1457     data.machine = machine;
1458     data.variant = ".acpierst";
1459     params = g_strdup_printf(
1460         " -object memory-backend-file,id=erstnvram,"
1461             "mem-path=%s,size=0x10000,share=on"
1462         " -device acpi-erst,memdev=erstnvram", tmp_path);
1463     test_acpi_one(params, &data);
1464     free_test_data(&data);
1465     g_free(params);
1466     g_assert(g_rmdir(tmp_path) == 0);
1467     g_free(tmp_path);
1468 }
1469 
1470 static void test_acpi_piix4_acpi_erst(void)
1471 {
1472     test_acpi_erst(MACHINE_PC);
1473 }
1474 
1475 static void test_acpi_q35_acpi_erst(void)
1476 {
1477     test_acpi_erst(MACHINE_Q35);
1478 }
1479 
1480 static void test_acpi_microvm_acpi_erst(void)
1481 {
1482     gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
1483     gchar *params;
1484     test_data data;
1485 
1486     test_acpi_microvm_prepare(&data);
1487     data.variant = ".pcie";
1488     data.tcg_only = true; /* need constant host-phys-bits */
1489     params = g_strdup_printf(" -machine microvm,"
1490         "acpi=on,ioapic2=off,rtc=off,pcie=on"
1491         " -object memory-backend-file,id=erstnvram,"
1492            "mem-path=%s,size=0x10000,share=on"
1493         " -device acpi-erst,memdev=erstnvram", tmp_path);
1494     test_acpi_one(params, &data);
1495     g_free(params);
1496     g_assert(g_rmdir(tmp_path) == 0);
1497     g_free(tmp_path);
1498     free_test_data(&data);
1499 }
1500 
1501 static void test_acpi_virt_tcg(void)
1502 {
1503     test_data data = {
1504         .machine = "virt",
1505         .tcg_only = true,
1506         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1507         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1508         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1509         .ram_start = 0x40000000ULL,
1510         .scan_len = 128ULL * 1024 * 1024,
1511     };
1512 
1513     data.smbios_cpu_max_speed = 2900;
1514     data.smbios_cpu_curr_speed = 2700;
1515     test_acpi_one("-cpu cortex-a57 "
1516                   "-smbios type=4,max-speed=2900,current-speed=2700", &data);
1517     free_test_data(&data);
1518 }
1519 
1520 static void test_acpi_q35_viot(void)
1521 {
1522     test_data data = {
1523         .machine = MACHINE_Q35,
1524         .variant = ".viot",
1525     };
1526 
1527     /*
1528      * To keep things interesting, two buses bypass the IOMMU.
1529      * VIOT should only describes the other two buses.
1530      */
1531     test_acpi_one("-machine default_bus_bypass_iommu=on "
1532                   "-device virtio-iommu-pci "
1533                   "-device pxb-pcie,bus_nr=0x10,id=pcie.100,bus=pcie.0 "
1534                   "-device pxb-pcie,bus_nr=0x20,id=pcie.200,bus=pcie.0,bypass_iommu=on "
1535                   "-device pxb-pcie,bus_nr=0x30,id=pcie.300,bus=pcie.0",
1536                   &data);
1537     free_test_data(&data);
1538 }
1539 
1540 static void test_acpi_virt_viot(void)
1541 {
1542     test_data data = {
1543         .machine = "virt",
1544         .tcg_only = true,
1545         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1546         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1547         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1548         .ram_start = 0x40000000ULL,
1549         .scan_len = 128ULL * 1024 * 1024,
1550     };
1551 
1552     test_acpi_one("-cpu cortex-a57 "
1553                   "-device virtio-iommu-pci", &data);
1554     free_test_data(&data);
1555 }
1556 
1557 static void test_acpi_q35_slic(void)
1558 {
1559     test_data data = {
1560         .machine = MACHINE_Q35,
1561         .variant = ".slic",
1562     };
1563 
1564     test_acpi_one("-acpitable sig=SLIC,oem_id='CRASH ',oem_table_id='ME',"
1565                   "oem_rev=00002210,asl_compiler_id='qemu',"
1566                   "asl_compiler_rev=00000000,data=/dev/null",
1567                   &data);
1568     free_test_data(&data);
1569 }
1570 
1571 static void test_oem_fields(test_data *data)
1572 {
1573     int i;
1574 
1575     for (i = 0; i < data->tables->len; ++i) {
1576         AcpiSdtTable *sdt;
1577 
1578         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
1579         /* FACS doesn't have OEMID and OEMTABLEID fields */
1580         if (compare_signature(sdt, "FACS")) {
1581             continue;
1582         }
1583 
1584         g_assert(strncmp((char *)sdt->aml + 10, OEM_ID, 6) == 0);
1585         g_assert(strncmp((char *)sdt->aml + 16, OEM_TABLE_ID, 8) == 0);
1586     }
1587 }
1588 
1589 static void test_acpi_oem_fields_pc(void)
1590 {
1591     test_data data;
1592     char *args;
1593 
1594     memset(&data, 0, sizeof(data));
1595     data.machine = MACHINE_PC;
1596     data.required_struct_types = base_required_struct_types;
1597     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1598 
1599     args = test_acpi_create_args(&data,
1600                                  OEM_TEST_ARGS, false);
1601     data.qts = qtest_init(args);
1602     test_acpi_load_tables(&data, false);
1603     test_oem_fields(&data);
1604     qtest_quit(data.qts);
1605     free_test_data(&data);
1606     g_free(args);
1607 }
1608 
1609 static void test_acpi_oem_fields_q35(void)
1610 {
1611     test_data data;
1612     char *args;
1613 
1614     memset(&data, 0, sizeof(data));
1615     data.machine = MACHINE_Q35;
1616     data.required_struct_types = base_required_struct_types;
1617     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1618 
1619     args = test_acpi_create_args(&data,
1620                                  OEM_TEST_ARGS, false);
1621     data.qts = qtest_init(args);
1622     test_acpi_load_tables(&data, false);
1623     test_oem_fields(&data);
1624     qtest_quit(data.qts);
1625     free_test_data(&data);
1626     g_free(args);
1627 }
1628 
1629 static void test_acpi_oem_fields_microvm(void)
1630 {
1631     test_data data;
1632     char *args;
1633 
1634     test_acpi_microvm_prepare(&data);
1635 
1636     args = test_acpi_create_args(&data,
1637                                  OEM_TEST_ARGS",acpi=on", false);
1638     data.qts = qtest_init(args);
1639     test_acpi_load_tables(&data, false);
1640     test_oem_fields(&data);
1641     qtest_quit(data.qts);
1642     free_test_data(&data);
1643     g_free(args);
1644 }
1645 
1646 static void test_acpi_oem_fields_virt(void)
1647 {
1648     test_data data = {
1649         .machine = "virt",
1650         .tcg_only = true,
1651         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1652         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1653         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1654         .ram_start = 0x40000000ULL,
1655         .scan_len = 128ULL * 1024 * 1024,
1656     };
1657     char *args;
1658 
1659     args = test_acpi_create_args(&data,
1660                                  "-cpu cortex-a57 "OEM_TEST_ARGS, true);
1661     data.qts = qtest_init(args);
1662     test_acpi_load_tables(&data, true);
1663     test_oem_fields(&data);
1664     qtest_quit(data.qts);
1665     free_test_data(&data);
1666     g_free(args);
1667 }
1668 
1669 
1670 int main(int argc, char *argv[])
1671 {
1672     const char *arch = qtest_get_arch();
1673     const bool has_kvm = qtest_has_accel("kvm");
1674     const bool has_tcg = qtest_has_accel("tcg");
1675     int ret;
1676 
1677     g_test_init(&argc, &argv, NULL);
1678 
1679     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
1680         ret = boot_sector_init(disk);
1681         if (ret) {
1682             return ret;
1683         }
1684         qtest_add_func("acpi/q35/oem-fields", test_acpi_oem_fields_q35);
1685         if (tpm_model_is_available("-machine q35", "tpm-tis")) {
1686             qtest_add_func("acpi/q35/tpm2-tis", test_acpi_q35_tcg_tpm2_tis);
1687             qtest_add_func("acpi/q35/tpm12-tis", test_acpi_q35_tcg_tpm12_tis);
1688         }
1689         qtest_add_func("acpi/piix4", test_acpi_piix4_tcg);
1690         qtest_add_func("acpi/oem-fields", test_acpi_oem_fields_pc);
1691         qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge);
1692         qtest_add_func("acpi/piix4/pci-hotplug/no_root_hotplug",
1693                        test_acpi_piix4_no_root_hotplug);
1694         qtest_add_func("acpi/piix4/pci-hotplug/no_bridge_hotplug",
1695                        test_acpi_piix4_no_bridge_hotplug);
1696         qtest_add_func("acpi/piix4/pci-hotplug/off",
1697                        test_acpi_piix4_no_acpi_pci_hotplug);
1698         qtest_add_func("acpi/q35", test_acpi_q35_tcg);
1699         qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
1700         qtest_add_func("acpi/q35/multif-bridge", test_acpi_q35_multif_bridge);
1701         qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64);
1702         qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi);
1703         qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi);
1704         qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp);
1705         qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp);
1706         qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
1707         qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
1708         qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
1709         qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
1710         qtest_add_func("acpi/piix4/nosmm", test_acpi_piix4_tcg_nosmm);
1711         qtest_add_func("acpi/piix4/smm-compat",
1712                        test_acpi_piix4_tcg_smm_compat);
1713         qtest_add_func("acpi/piix4/smm-compat-nosmm",
1714                        test_acpi_piix4_tcg_smm_compat_nosmm);
1715         qtest_add_func("acpi/piix4/nohpet", test_acpi_piix4_tcg_nohpet);
1716         qtest_add_func("acpi/q35/nosmm", test_acpi_q35_tcg_nosmm);
1717         qtest_add_func("acpi/q35/smm-compat",
1718                        test_acpi_q35_tcg_smm_compat);
1719         qtest_add_func("acpi/q35/smm-compat-nosmm",
1720                        test_acpi_q35_tcg_smm_compat_nosmm);
1721         qtest_add_func("acpi/q35/nohpet", test_acpi_q35_tcg_nohpet);
1722         qtest_add_func("acpi/piix4/dimmpxm", test_acpi_piix4_tcg_dimm_pxm);
1723         qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm);
1724         qtest_add_func("acpi/piix4/acpihmat", test_acpi_piix4_tcg_acpi_hmat);
1725         qtest_add_func("acpi/q35/acpihmat", test_acpi_q35_tcg_acpi_hmat);
1726         qtest_add_func("acpi/piix4/acpierst", test_acpi_piix4_acpi_erst);
1727         qtest_add_func("acpi/q35/acpierst", test_acpi_q35_acpi_erst);
1728         qtest_add_func("acpi/microvm", test_acpi_microvm_tcg);
1729         qtest_add_func("acpi/microvm/usb", test_acpi_microvm_usb_tcg);
1730         qtest_add_func("acpi/microvm/rtc", test_acpi_microvm_rtc_tcg);
1731         qtest_add_func("acpi/microvm/ioapic2", test_acpi_microvm_ioapic2_tcg);
1732         qtest_add_func("acpi/microvm/oem-fields", test_acpi_oem_fields_microvm);
1733         if (has_tcg) {
1734             qtest_add_func("acpi/q35/ivrs", test_acpi_q35_tcg_ivrs);
1735             if (strcmp(arch, "x86_64") == 0) {
1736                 qtest_add_func("acpi/microvm/pcie", test_acpi_microvm_pcie_tcg);
1737                 qtest_add_func("acpi/microvm/acpierst", test_acpi_microvm_acpi_erst);
1738             }
1739         }
1740         if (has_kvm) {
1741             qtest_add_func("acpi/q35/kvm/xapic", test_acpi_q35_kvm_xapic);
1742             qtest_add_func("acpi/q35/kvm/dmar", test_acpi_q35_kvm_dmar);
1743         }
1744         qtest_add_func("acpi/q35/viot", test_acpi_q35_viot);
1745         qtest_add_func("acpi/q35/slic", test_acpi_q35_slic);
1746     } else if (strcmp(arch, "aarch64") == 0) {
1747         if (has_tcg) {
1748             qtest_add_func("acpi/virt", test_acpi_virt_tcg);
1749             qtest_add_func("acpi/virt/numamem", test_acpi_virt_tcg_numamem);
1750             qtest_add_func("acpi/virt/memhp", test_acpi_virt_tcg_memhp);
1751             qtest_add_func("acpi/virt/pxb", test_acpi_virt_tcg_pxb);
1752             qtest_add_func("acpi/virt/oem-fields", test_acpi_oem_fields_virt);
1753             qtest_add_func("acpi/virt/viot", test_acpi_virt_viot);
1754         }
1755     }
1756     ret = g_test_run();
1757     boot_sector_cleanup(disk);
1758     return ret;
1759 }
1760