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