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