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