xref: /qemu/tests/qtest/bios-tables-test.c (revision c0d19126f39000181a007371b9200fd2e2b0dcc8)
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)
752 {
753     if (data->uefi_fl1 && data->uefi_fl2) { /* 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 {
771     char *args;
772 
773     if (data->uefi_fl1 && data->uefi_fl2) { /* use UEFI */
774         /*
775          * TODO: convert '-drive if=pflash' to new syntax (see e33763be7cd3)
776          * when arm/virt boad starts to support it.
777          */
778         if (data->cd) {
779             args = g_strdup_printf("-machine %s%s %s -accel tcg "
780                 "-nodefaults -nographic "
781                 "-drive if=pflash,format=raw,file=%s,readonly=on "
782                 "-drive if=pflash,format=raw,file=%s,snapshot=on -cdrom %s %s",
783                 data->machine, data->machine_param ?: "",
784                 data->tcg_only ? "" : "-accel kvm",
785                 data->uefi_fl1, data->uefi_fl2, data->cd, params ? params : "");
786         } else {
787             args = g_strdup_printf("-machine %s%s %s -accel tcg "
788                 "-nodefaults -nographic "
789                 "-drive if=pflash,format=raw,file=%s,readonly=on "
790                 "-drive if=pflash,format=raw,file=%s,snapshot=on %s",
791                 data->machine, data->machine_param ?: "",
792                 data->tcg_only ? "" : "-accel kvm",
793                 data->uefi_fl1, data->uefi_fl2, params ? params : "");
794         }
795     } else {
796         args = g_strdup_printf("-machine %s%s %s -accel tcg "
797             "-net none %s "
798             "-drive id=hd0,if=none,file=%s,format=raw "
799             "-device %s,drive=hd0 ",
800              data->machine, data->machine_param ?: "",
801              data->tcg_only ? "" : "-accel kvm",
802              params ? params : "", disk,
803              data->blkdev ?: "ide-hd");
804     }
805     return args;
806 }
807 
808 static void test_vm_prepare(const char *params, test_data *data)
809 {
810     char *args = test_acpi_create_args(data, params);
811     data->qts = qtest_init(args);
812     g_free(args);
813 }
814 
815 static void process_acpi_tables_noexit(test_data *data)
816 {
817     test_acpi_load_tables(data);
818 
819     if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
820         dump_aml_files(data, true);
821     } else {
822         test_acpi_asl(data);
823     }
824 
825     /*
826      * TODO: make SMBIOS tests work with UEFI firmware,
827      * Bug on uefi-test-tools to provide entry point:
828      * https://bugs.launchpad.net/qemu/+bug/1821884
829      */
830     if (!(data->uefi_fl1 && data->uefi_fl2)) {
831         SmbiosEntryPointType ep_type = test_smbios_entry_point(data);
832         test_smbios_structs(data, ep_type);
833     }
834 }
835 
836 static void process_acpi_tables(test_data *data)
837 {
838     process_acpi_tables_noexit(data);
839     qtest_quit(data->qts);
840 }
841 
842 static void test_acpi_one(const char *params, test_data *data)
843 {
844     test_vm_prepare(params, data);
845     process_acpi_tables(data);
846 }
847 
848 static uint8_t base_required_struct_types[] = {
849     0, 1, 3, 4, 16, 17, 19, 32, 127
850 };
851 
852 static void test_acpi_piix4_tcg(void)
853 {
854     test_data data;
855 
856     /* Supplying -machine accel argument overrides the default (qtest).
857      * This is to make guest actually run.
858      */
859     memset(&data, 0, sizeof(data));
860     data.machine = MACHINE_PC;
861     data.required_struct_types = base_required_struct_types;
862     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
863     test_acpi_one(NULL, &data);
864     free_test_data(&data);
865 }
866 
867 static void test_acpi_piix4_tcg_bridge(void)
868 {
869     test_data data;
870 
871     memset(&data, 0, sizeof(data));
872     data.machine = MACHINE_PC;
873     data.variant = ".bridge";
874     data.required_struct_types = base_required_struct_types;
875     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
876     test_vm_prepare("-S"
877         " -device pci-bridge,chassis_nr=1"
878         " -device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2", &data);
879 
880     /* hotplugged bridges section */
881     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr",
882         "{'bus': 'pci.1', 'addr': '2.0', 'chassis_nr': 3 }");
883     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr_multifunc",
884         "{'bus': 'pci.1', 'addr': '0xf.1', 'chassis_nr': 4 }");
885     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbrhost",
886         "{'bus': 'pci.0', 'addr': '4.0', 'chassis_nr': 5 }");
887     qtest_qmp_send(data.qts, "{'execute':'cont' }");
888     qtest_qmp_eventwait(data.qts, "RESUME");
889 
890     process_acpi_tables_noexit(&data);
891     free_test_data(&data);
892 
893     /* check that reboot/reset doesn't change any ACPI tables  */
894     qtest_qmp_send(data.qts, "{'execute':'system_reset' }");
895     process_acpi_tables(&data);
896     free_test_data(&data);
897 }
898 
899 static void test_acpi_piix4_no_root_hotplug(void)
900 {
901     test_data data;
902 
903     memset(&data, 0, sizeof(data));
904     data.machine = MACHINE_PC;
905     data.variant = ".roothp";
906     data.required_struct_types = base_required_struct_types;
907     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
908     test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
909                   "-device pci-bridge,chassis_nr=1 "
910                   "-device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2 ", &data);
911     free_test_data(&data);
912 }
913 
914 static void test_acpi_piix4_no_bridge_hotplug(void)
915 {
916     test_data data;
917 
918     memset(&data, 0, sizeof(data));
919     data.machine = MACHINE_PC;
920     data.variant = ".hpbridge";
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("-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
924                   "-device pci-bridge,chassis_nr=1 "
925                   "-device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2 ", &data);
926     free_test_data(&data);
927 }
928 
929 static void test_acpi_piix4_no_acpi_pci_hotplug(void)
930 {
931     test_data data;
932 
933     memset(&data, 0, sizeof(data));
934     data.machine = MACHINE_PC;
935     data.variant = ".hpbrroot";
936     data.required_struct_types = base_required_struct_types;
937     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
938     test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
939                   "-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
940                   "-device pci-bridge,chassis_nr=1", &data);
941     free_test_data(&data);
942 }
943 
944 static void test_acpi_q35_tcg(void)
945 {
946     test_data data;
947 
948     memset(&data, 0, sizeof(data));
949     data.machine = MACHINE_Q35;
950     data.required_struct_types = base_required_struct_types;
951     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
952     test_acpi_one(NULL, &data);
953     free_test_data(&data);
954 
955     data.smbios_cpu_max_speed = 3000;
956     data.smbios_cpu_curr_speed = 2600;
957     test_acpi_one("-smbios type=4,max-speed=3000,current-speed=2600", &data);
958     free_test_data(&data);
959 }
960 
961 static void test_acpi_q35_tcg_core_count2(void)
962 {
963     test_data data = {
964         .machine = MACHINE_Q35,
965         .variant = ".core-count2",
966         .required_struct_types = base_required_struct_types,
967         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
968         .smbios_core_count = 0xFF,
969         .smbios_core_count2 = 275,
970     };
971 
972     test_acpi_one("-machine smbios-entry-point-type=64 -smp 275", &data);
973     free_test_data(&data);
974 }
975 
976 static void test_acpi_q35_tcg_bridge(void)
977 {
978     test_data data;
979 
980     memset(&data, 0, sizeof(data));
981     data.machine = MACHINE_Q35;
982     data.variant = ".bridge";
983     data.required_struct_types = base_required_struct_types;
984     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
985     test_acpi_one("-device pci-bridge,chassis_nr=1", &data);
986     free_test_data(&data);
987 }
988 
989 static void test_acpi_q35_multif_bridge(void)
990 {
991     test_data data = {
992         .machine = MACHINE_Q35,
993         .variant = ".multi-bridge",
994     };
995     test_vm_prepare("-S"
996         " -device virtio-balloon,id=balloon0,addr=0x4.0x2"
997         " -device pcie-root-port,id=rp0,multifunction=on,"
998                   "port=0x0,chassis=1,addr=0x2"
999         " -device pcie-root-port,id=rp1,port=0x1,chassis=2,addr=0x3.0x1"
1000         " -device pcie-root-port,id=rp2,port=0x0,chassis=3,bus=rp1,addr=0.0"
1001         " -device pci-bridge,bus=rp2,chassis_nr=4,id=br1"
1002         " -device pcie-root-port,id=rphptgt1,port=0x0,chassis=5,addr=2.1"
1003         " -device pcie-root-port,id=rphptgt2,port=0x0,chassis=6,addr=2.2"
1004         " -device pcie-root-port,id=rphptgt3,port=0x0,chassis=7,addr=2.3",
1005         &data);
1006 
1007     /* hotplugged bridges section */
1008     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr1",
1009         "{'bus': 'br1', 'addr': '6.0', 'chassis_nr': 128 }");
1010     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr2-multiif",
1011         "{ 'bus': 'br1', 'addr': '2.2', 'chassis_nr': 129 }");
1012     qtest_qmp_device_add(data.qts, "pcie-pci-bridge", "hpbr3",
1013         "{'bus': 'rp0', 'addr': '0.0' }");
1014     qtest_qmp_send(data.qts, "{'execute':'cont' }");
1015     qtest_qmp_eventwait(data.qts, "RESUME");
1016 
1017     process_acpi_tables_noexit(&data);
1018     free_test_data(&data);
1019 
1020     /* check that reboot/reset doesn't change any ACPI tables  */
1021     qtest_qmp_send(data.qts, "{'execute':'system_reset' }");
1022     process_acpi_tables(&data);
1023     free_test_data(&data);
1024 }
1025 
1026 static void test_acpi_q35_tcg_mmio64(void)
1027 {
1028     test_data data = {
1029         .machine = MACHINE_Q35,
1030         .variant = ".mmio64",
1031         .required_struct_types = base_required_struct_types,
1032         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types)
1033     };
1034 
1035     test_acpi_one("-m 128M,slots=1,maxmem=2G "
1036                   "-object memory-backend-ram,id=ram0,size=128M "
1037                   "-numa node,memdev=ram0 "
1038                   "-device pci-testdev,membar=2G",
1039                   &data);
1040     free_test_data(&data);
1041 }
1042 
1043 static void test_acpi_piix4_tcg_cphp(void)
1044 {
1045     test_data data;
1046 
1047     memset(&data, 0, sizeof(data));
1048     data.machine = MACHINE_PC;
1049     data.variant = ".cphp";
1050     test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
1051                   " -object memory-backend-ram,id=ram0,size=64M"
1052                   " -object memory-backend-ram,id=ram1,size=64M"
1053                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1054                   " -numa dist,src=0,dst=1,val=21",
1055                   &data);
1056     free_test_data(&data);
1057 }
1058 
1059 static void test_acpi_q35_tcg_cphp(void)
1060 {
1061     test_data data;
1062 
1063     memset(&data, 0, sizeof(data));
1064     data.machine = MACHINE_Q35;
1065     data.variant = ".cphp";
1066     test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
1067                   " -object memory-backend-ram,id=ram0,size=64M"
1068                   " -object memory-backend-ram,id=ram1,size=64M"
1069                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1070                   " -numa dist,src=0,dst=1,val=21",
1071                   &data);
1072     free_test_data(&data);
1073 }
1074 
1075 static uint8_t ipmi_required_struct_types[] = {
1076     0, 1, 3, 4, 16, 17, 19, 32, 38, 127
1077 };
1078 
1079 static void test_acpi_q35_tcg_ipmi(void)
1080 {
1081     test_data data;
1082 
1083     memset(&data, 0, sizeof(data));
1084     data.machine = MACHINE_Q35;
1085     data.variant = ".ipmibt";
1086     data.required_struct_types = ipmi_required_struct_types;
1087     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1088     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1089                   " -device isa-ipmi-bt,bmc=bmc0",
1090                   &data);
1091     free_test_data(&data);
1092 }
1093 
1094 static void test_acpi_q35_tcg_smbus_ipmi(void)
1095 {
1096     test_data data;
1097 
1098     memset(&data, 0, sizeof(data));
1099     data.machine = MACHINE_Q35;
1100     data.variant = ".ipmismbus";
1101     data.required_struct_types = ipmi_required_struct_types;
1102     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1103     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1104                   " -device smbus-ipmi,bmc=bmc0",
1105                   &data);
1106     free_test_data(&data);
1107 }
1108 
1109 static void test_acpi_piix4_tcg_ipmi(void)
1110 {
1111     test_data data;
1112 
1113     /* Supplying -machine accel argument overrides the default (qtest).
1114      * This is to make guest actually run.
1115      */
1116     memset(&data, 0, sizeof(data));
1117     data.machine = MACHINE_PC;
1118     data.variant = ".ipmikcs";
1119     data.required_struct_types = ipmi_required_struct_types;
1120     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1121     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1122                   " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
1123                   &data);
1124     free_test_data(&data);
1125 }
1126 
1127 static void test_acpi_q35_tcg_memhp(void)
1128 {
1129     test_data data;
1130 
1131     memset(&data, 0, sizeof(data));
1132     data.machine = MACHINE_Q35;
1133     data.variant = ".memhp";
1134     test_acpi_one(" -m 128,slots=3,maxmem=1G"
1135                   " -object memory-backend-ram,id=ram0,size=64M"
1136                   " -object memory-backend-ram,id=ram1,size=64M"
1137                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1138                   " -numa dist,src=0,dst=1,val=21",
1139                   &data);
1140     free_test_data(&data);
1141 }
1142 
1143 static void test_acpi_piix4_tcg_memhp(void)
1144 {
1145     test_data data;
1146 
1147     memset(&data, 0, sizeof(data));
1148     data.machine = MACHINE_PC;
1149     data.variant = ".memhp";
1150     test_acpi_one(" -m 128,slots=3,maxmem=1G"
1151                   " -object memory-backend-ram,id=ram0,size=64M"
1152                   " -object memory-backend-ram,id=ram1,size=64M"
1153                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1154                   " -numa dist,src=0,dst=1,val=21",
1155                   &data);
1156     free_test_data(&data);
1157 }
1158 
1159 static void test_acpi_piix4_tcg_nosmm(void)
1160 {
1161     test_data data;
1162 
1163     memset(&data, 0, sizeof(data));
1164     data.machine = MACHINE_PC;
1165     data.variant = ".nosmm";
1166     test_acpi_one("-machine smm=off", &data);
1167     free_test_data(&data);
1168 }
1169 
1170 static void test_acpi_piix4_tcg_smm_compat(void)
1171 {
1172     test_data data;
1173 
1174     memset(&data, 0, sizeof(data));
1175     data.machine = MACHINE_PC;
1176     data.variant = ".smm-compat";
1177     test_acpi_one("-global PIIX4_PM.smm-compat=on", &data);
1178     free_test_data(&data);
1179 }
1180 
1181 static void test_acpi_piix4_tcg_smm_compat_nosmm(void)
1182 {
1183     test_data data;
1184 
1185     memset(&data, 0, sizeof(data));
1186     data.machine = MACHINE_PC;
1187     data.variant = ".smm-compat-nosmm";
1188     test_acpi_one("-global PIIX4_PM.smm-compat=on -machine smm=off", &data);
1189     free_test_data(&data);
1190 }
1191 
1192 static void test_acpi_piix4_tcg_nohpet(void)
1193 {
1194     test_data data;
1195 
1196     memset(&data, 0, sizeof(data));
1197     data.machine = MACHINE_PC;
1198     data.machine_param = ",hpet=off";
1199     data.variant = ".nohpet";
1200     test_acpi_one(NULL, &data);
1201     free_test_data(&data);
1202 }
1203 
1204 static void test_acpi_q35_tcg_numamem(void)
1205 {
1206     test_data data;
1207 
1208     memset(&data, 0, sizeof(data));
1209     data.machine = MACHINE_Q35;
1210     data.variant = ".numamem";
1211     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1212                   " -numa node -numa node,memdev=ram0", &data);
1213     free_test_data(&data);
1214 }
1215 
1216 static void test_acpi_q35_kvm_xapic(void)
1217 {
1218     test_data data;
1219 
1220     memset(&data, 0, sizeof(data));
1221     data.machine = MACHINE_Q35;
1222     data.variant = ".xapic";
1223     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1224                   " -numa node -numa node,memdev=ram0"
1225                   " -machine kernel-irqchip=on -smp 1,maxcpus=288", &data);
1226     free_test_data(&data);
1227 }
1228 
1229 static void test_acpi_q35_tcg_nosmm(void)
1230 {
1231     test_data data;
1232 
1233     memset(&data, 0, sizeof(data));
1234     data.machine = MACHINE_Q35;
1235     data.variant = ".nosmm";
1236     test_acpi_one("-machine smm=off", &data);
1237     free_test_data(&data);
1238 }
1239 
1240 static void test_acpi_q35_tcg_smm_compat(void)
1241 {
1242     test_data data;
1243 
1244     memset(&data, 0, sizeof(data));
1245     data.machine = MACHINE_Q35;
1246     data.variant = ".smm-compat";
1247     test_acpi_one("-global ICH9-LPC.smm-compat=on", &data);
1248     free_test_data(&data);
1249 }
1250 
1251 static void test_acpi_q35_tcg_smm_compat_nosmm(void)
1252 {
1253     test_data data;
1254 
1255     memset(&data, 0, sizeof(data));
1256     data.machine = MACHINE_Q35;
1257     data.variant = ".smm-compat-nosmm";
1258     test_acpi_one("-global ICH9-LPC.smm-compat=on -machine smm=off", &data);
1259     free_test_data(&data);
1260 }
1261 
1262 static void test_acpi_q35_tcg_nohpet(void)
1263 {
1264     test_data data;
1265 
1266     memset(&data, 0, sizeof(data));
1267     data.machine = MACHINE_Q35;
1268     data.machine_param = ",hpet=off";
1269     data.variant = ".nohpet";
1270     test_acpi_one(NULL, &data);
1271     free_test_data(&data);
1272 }
1273 
1274 static void test_acpi_q35_kvm_dmar(void)
1275 {
1276     test_data data;
1277 
1278     memset(&data, 0, sizeof(data));
1279     data.machine = MACHINE_Q35;
1280     data.variant = ".dmar";
1281     test_acpi_one("-machine kernel-irqchip=split -accel kvm"
1282                   " -device intel-iommu,intremap=on,device-iotlb=on", &data);
1283     free_test_data(&data);
1284 }
1285 
1286 static void test_acpi_q35_tcg_ivrs(void)
1287 {
1288     test_data data;
1289 
1290     memset(&data, 0, sizeof(data));
1291     data.machine = MACHINE_Q35;
1292     data.variant = ".ivrs";
1293     data.tcg_only = true,
1294     test_acpi_one(" -device amd-iommu", &data);
1295     free_test_data(&data);
1296 }
1297 
1298 static void test_acpi_piix4_tcg_numamem(void)
1299 {
1300     test_data data;
1301 
1302     memset(&data, 0, sizeof(data));
1303     data.machine = MACHINE_PC;
1304     data.variant = ".numamem";
1305     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1306                   " -numa node -numa node,memdev=ram0", &data);
1307     free_test_data(&data);
1308 }
1309 
1310 uint64_t tpm_tis_base_addr;
1311 
1312 static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
1313                               uint64_t base, enum TPMVersion tpm_version)
1314 {
1315     gchar *tmp_dir_name = g_strdup_printf("qemu-test_acpi_%s_tcg_%s.XXXXXX",
1316                                           machine, tpm_if);
1317     char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
1318     TPMTestState test;
1319     test_data data;
1320     GThread *thread;
1321     const char *suffix = tpm_version == TPM_VERSION_2_0 ? "tpm2" : "tpm12";
1322     char *args, *variant = g_strdup_printf(".%s.%s", tpm_if, suffix);
1323 
1324     tpm_tis_base_addr = base;
1325 
1326     module_call_init(MODULE_INIT_QOM);
1327 
1328     test.addr = g_new0(SocketAddress, 1);
1329     test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
1330     test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
1331     g_mutex_init(&test.data_mutex);
1332     g_cond_init(&test.data_cond);
1333     test.data_cond_signal = false;
1334     test.tpm_version = tpm_version;
1335 
1336     thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
1337     tpm_emu_test_wait_cond(&test);
1338 
1339     memset(&data, 0, sizeof(data));
1340     data.machine = machine;
1341     data.variant = variant;
1342 
1343     args = g_strdup_printf(
1344         " -chardev socket,id=chr,path=%s"
1345         " -tpmdev emulator,id=dev,chardev=chr"
1346         " -device tpm-%s,tpmdev=dev",
1347         test.addr->u.q_unix.path, tpm_if);
1348 
1349     test_acpi_one(args, &data);
1350 
1351     g_thread_join(thread);
1352     g_unlink(test.addr->u.q_unix.path);
1353     qapi_free_SocketAddress(test.addr);
1354     g_rmdir(tmp_path);
1355     g_free(variant);
1356     g_free(tmp_path);
1357     g_free(tmp_dir_name);
1358     g_free(args);
1359     free_test_data(&data);
1360 }
1361 
1362 static void test_acpi_q35_tcg_tpm2_tis(void)
1363 {
1364     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_2_0);
1365 }
1366 
1367 static void test_acpi_q35_tcg_tpm12_tis(void)
1368 {
1369     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_1_2);
1370 }
1371 
1372 static void test_acpi_tcg_dimm_pxm(const char *machine)
1373 {
1374     test_data data;
1375 
1376     memset(&data, 0, sizeof(data));
1377     data.machine = machine;
1378     data.variant = ".dimmpxm";
1379     test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
1380                   " -smp 4,sockets=4"
1381                   " -m 128M,slots=3,maxmem=1G"
1382                   " -object memory-backend-ram,id=ram0,size=32M"
1383                   " -object memory-backend-ram,id=ram1,size=32M"
1384                   " -object memory-backend-ram,id=ram2,size=32M"
1385                   " -object memory-backend-ram,id=ram3,size=32M"
1386                   " -numa node,memdev=ram0,nodeid=0"
1387                   " -numa node,memdev=ram1,nodeid=1"
1388                   " -numa node,memdev=ram2,nodeid=2"
1389                   " -numa node,memdev=ram3,nodeid=3"
1390                   " -numa cpu,node-id=0,socket-id=0"
1391                   " -numa cpu,node-id=1,socket-id=1"
1392                   " -numa cpu,node-id=2,socket-id=2"
1393                   " -numa cpu,node-id=3,socket-id=3"
1394                   " -object memory-backend-ram,id=ram4,size=128M"
1395                   " -object memory-backend-ram,id=nvm0,size=128M"
1396                   " -device pc-dimm,id=dimm0,memdev=ram4,node=1"
1397                   " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
1398                   &data);
1399     free_test_data(&data);
1400 }
1401 
1402 static void test_acpi_q35_tcg_dimm_pxm(void)
1403 {
1404     test_acpi_tcg_dimm_pxm(MACHINE_Q35);
1405 }
1406 
1407 static void test_acpi_piix4_tcg_dimm_pxm(void)
1408 {
1409     test_acpi_tcg_dimm_pxm(MACHINE_PC);
1410 }
1411 
1412 static void test_acpi_virt_tcg_memhp(void)
1413 {
1414     test_data data = {
1415         .machine = "virt",
1416         .tcg_only = true,
1417         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1418         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1419         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1420         .ram_start = 0x40000000ULL,
1421         .scan_len = 256ULL * 1024 * 1024,
1422     };
1423 
1424     data.variant = ".memhp";
1425     test_acpi_one(" -machine nvdimm=on"
1426                   " -cpu cortex-a57"
1427                   " -m 256M,slots=3,maxmem=1G"
1428                   " -object memory-backend-ram,id=ram0,size=128M"
1429                   " -object memory-backend-ram,id=ram1,size=128M"
1430                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1431                   " -numa dist,src=0,dst=1,val=21"
1432                   " -object memory-backend-ram,id=ram2,size=128M"
1433                   " -object memory-backend-ram,id=nvm0,size=128M"
1434                   " -device pc-dimm,id=dimm0,memdev=ram2,node=0"
1435                   " -device nvdimm,id=dimm1,memdev=nvm0,node=1",
1436                   &data);
1437 
1438     free_test_data(&data);
1439 
1440 }
1441 
1442 static void test_acpi_microvm_prepare(test_data *data)
1443 {
1444     memset(data, 0, sizeof(*data));
1445     data->machine = "microvm";
1446     data->required_struct_types = NULL; /* no smbios */
1447     data->required_struct_types_len = 0;
1448     data->blkdev = "virtio-blk-device";
1449 }
1450 
1451 static void test_acpi_microvm_tcg(void)
1452 {
1453     test_data data;
1454 
1455     test_acpi_microvm_prepare(&data);
1456     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off",
1457                   &data);
1458     free_test_data(&data);
1459 }
1460 
1461 static void test_acpi_microvm_usb_tcg(void)
1462 {
1463     test_data data;
1464 
1465     test_acpi_microvm_prepare(&data);
1466     data.variant = ".usb";
1467     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,usb=on,rtc=off",
1468                   &data);
1469     free_test_data(&data);
1470 }
1471 
1472 static void test_acpi_microvm_rtc_tcg(void)
1473 {
1474     test_data data;
1475 
1476     test_acpi_microvm_prepare(&data);
1477     data.variant = ".rtc";
1478     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=on",
1479                   &data);
1480     free_test_data(&data);
1481 }
1482 
1483 static void test_acpi_microvm_pcie_tcg(void)
1484 {
1485     test_data data;
1486 
1487     test_acpi_microvm_prepare(&data);
1488     data.variant = ".pcie";
1489     data.tcg_only = true; /* need constant host-phys-bits */
1490     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off,pcie=on",
1491                   &data);
1492     free_test_data(&data);
1493 }
1494 
1495 static void test_acpi_microvm_ioapic2_tcg(void)
1496 {
1497     test_data data;
1498 
1499     test_acpi_microvm_prepare(&data);
1500     data.variant = ".ioapic2";
1501     test_acpi_one(" -machine microvm,acpi=on,ioapic2=on,rtc=off",
1502                   &data);
1503     free_test_data(&data);
1504 }
1505 
1506 static void test_acpi_virt_tcg_numamem(void)
1507 {
1508     test_data data = {
1509         .machine = "virt",
1510         .tcg_only = true,
1511         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1512         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1513         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1514         .ram_start = 0x40000000ULL,
1515         .scan_len = 128ULL * 1024 * 1024,
1516     };
1517 
1518     data.variant = ".numamem";
1519     test_acpi_one(" -cpu cortex-a57"
1520                   " -object memory-backend-ram,id=ram0,size=128M"
1521                   " -numa node,memdev=ram0",
1522                   &data);
1523 
1524     free_test_data(&data);
1525 
1526 }
1527 
1528 static void test_acpi_virt_tcg_pxb(void)
1529 {
1530     test_data data = {
1531         .machine = "virt",
1532         .tcg_only = true,
1533         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1534         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1535         .ram_start = 0x40000000ULL,
1536         .scan_len = 128ULL * 1024 * 1024,
1537     };
1538     /*
1539      * While using -cdrom, the cdrom would auto plugged into pxb-pcie,
1540      * the reason is the bus of pxb-pcie is also root bus, it would lead
1541      * to the error only PCI/PCIE bridge could plug onto pxb.
1542      * Therefore,thr cdrom is defined and plugged onto the scsi controller
1543      * to solve the conflicts.
1544      */
1545     data.variant = ".pxb";
1546     test_acpi_one(" -device pcie-root-port,chassis=1,id=pci.1"
1547                   " -device virtio-scsi-pci,id=scsi0,bus=pci.1"
1548                   " -drive file="
1549                   "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2,"
1550                   "if=none,media=cdrom,id=drive-scsi0-0-0-1,readonly=on"
1551                   " -device scsi-cd,bus=scsi0.0,scsi-id=0,"
1552                   "drive=drive-scsi0-0-0-1,id=scsi0-0-0-1,bootindex=1"
1553                   " -cpu cortex-a57"
1554                   " -device pxb-pcie,bus_nr=128",
1555                   &data);
1556 
1557     free_test_data(&data);
1558 }
1559 
1560 static void test_acpi_tcg_acpi_hmat(const char *machine)
1561 {
1562     test_data data;
1563 
1564     memset(&data, 0, sizeof(data));
1565     data.machine = machine;
1566     data.variant = ".acpihmat";
1567     test_acpi_one(" -machine hmat=on"
1568                   " -smp 2,sockets=2"
1569                   " -m 128M,slots=2,maxmem=1G"
1570                   " -object memory-backend-ram,size=64M,id=m0"
1571                   " -object memory-backend-ram,size=64M,id=m1"
1572                   " -numa node,nodeid=0,memdev=m0"
1573                   " -numa node,nodeid=1,memdev=m1,initiator=0"
1574                   " -numa cpu,node-id=0,socket-id=0"
1575                   " -numa cpu,node-id=0,socket-id=1"
1576                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1577                   "data-type=access-latency,latency=1"
1578                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1579                   "data-type=access-bandwidth,bandwidth=65534M"
1580                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1581                   "data-type=access-latency,latency=65534"
1582                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1583                   "data-type=access-bandwidth,bandwidth=32767M"
1584                   " -numa hmat-cache,node-id=0,size=10K,level=1,"
1585                   "associativity=direct,policy=write-back,line=8"
1586                   " -numa hmat-cache,node-id=1,size=10K,level=1,"
1587                   "associativity=direct,policy=write-back,line=8",
1588                   &data);
1589     free_test_data(&data);
1590 }
1591 
1592 static void test_acpi_q35_tcg_acpi_hmat(void)
1593 {
1594     test_acpi_tcg_acpi_hmat(MACHINE_Q35);
1595 }
1596 
1597 static void test_acpi_piix4_tcg_acpi_hmat(void)
1598 {
1599     test_acpi_tcg_acpi_hmat(MACHINE_PC);
1600 }
1601 
1602 static void test_acpi_virt_tcg_acpi_hmat(void)
1603 {
1604     test_data data = {
1605         .machine = "virt",
1606         .tcg_only = true,
1607         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1608         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1609         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1610         .ram_start = 0x40000000ULL,
1611         .scan_len = 128ULL * 1024 * 1024,
1612     };
1613 
1614     data.variant = ".acpihmatvirt";
1615 
1616     test_acpi_one(" -machine hmat=on"
1617                   " -cpu cortex-a57"
1618                   " -smp 4,sockets=2"
1619                   " -m 256M"
1620                   " -object memory-backend-ram,size=64M,id=ram0"
1621                   " -object memory-backend-ram,size=64M,id=ram1"
1622                   " -object memory-backend-ram,size=128M,id=ram2"
1623                   " -numa node,nodeid=0,memdev=ram0"
1624                   " -numa node,nodeid=1,memdev=ram1"
1625                   " -numa node,nodeid=2,memdev=ram2"
1626                   " -numa cpu,node-id=0,socket-id=0"
1627                   " -numa cpu,node-id=0,socket-id=0"
1628                   " -numa cpu,node-id=1,socket-id=1"
1629                   " -numa cpu,node-id=1,socket-id=1"
1630                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1631                   "data-type=access-latency,latency=10"
1632                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1633                   "data-type=access-bandwidth,bandwidth=10485760"
1634                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1635                   "data-type=access-latency,latency=20"
1636                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1637                   "data-type=access-bandwidth,bandwidth=5242880"
1638                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1639                   "data-type=access-latency,latency=30"
1640                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1641                   "data-type=access-bandwidth,bandwidth=1048576"
1642                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1643                   "data-type=access-latency,latency=20"
1644                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1645                   "data-type=access-bandwidth,bandwidth=5242880"
1646                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1647                   "data-type=access-latency,latency=10"
1648                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1649                   "data-type=access-bandwidth,bandwidth=10485760"
1650                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1651                   "data-type=access-latency,latency=30"
1652                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1653                   "data-type=access-bandwidth,bandwidth=1048576",
1654                   &data);
1655 
1656     free_test_data(&data);
1657 }
1658 
1659 static void test_acpi_q35_tcg_acpi_hmat_noinitiator(void)
1660 {
1661     test_data data;
1662 
1663     memset(&data, 0, sizeof(data));
1664     data.machine = MACHINE_Q35;
1665     data.variant = ".acpihmat-noinitiator";
1666     test_acpi_one(" -machine hmat=on"
1667                   " -smp 4,sockets=2"
1668                   " -m 128M"
1669                   " -object memory-backend-ram,size=32M,id=ram0"
1670                   " -object memory-backend-ram,size=32M,id=ram1"
1671                   " -object memory-backend-ram,size=64M,id=ram2"
1672                   " -numa node,nodeid=0,memdev=ram0"
1673                   " -numa node,nodeid=1,memdev=ram1"
1674                   " -numa node,nodeid=2,memdev=ram2"
1675                   " -numa cpu,node-id=0,socket-id=0"
1676                   " -numa cpu,node-id=0,socket-id=0"
1677                   " -numa cpu,node-id=1,socket-id=1"
1678                   " -numa cpu,node-id=1,socket-id=1"
1679                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1680                   "data-type=access-latency,latency=10"
1681                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1682                   "data-type=access-bandwidth,bandwidth=10485760"
1683                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1684                   "data-type=access-latency,latency=20"
1685                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1686                   "data-type=access-bandwidth,bandwidth=5242880"
1687                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1688                   "data-type=access-latency,latency=30"
1689                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1690                   "data-type=access-bandwidth,bandwidth=1048576"
1691                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1692                   "data-type=access-latency,latency=20"
1693                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1694                   "data-type=access-bandwidth,bandwidth=5242880"
1695                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1696                   "data-type=access-latency,latency=10"
1697                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1698                   "data-type=access-bandwidth,bandwidth=10485760"
1699                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1700                   "data-type=access-latency,latency=30"
1701                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1702                   "data-type=access-bandwidth,bandwidth=1048576",
1703                   &data);
1704     free_test_data(&data);
1705 }
1706 
1707 #ifdef CONFIG_POSIX
1708 static void test_acpi_erst(const char *machine)
1709 {
1710     gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
1711     gchar *params;
1712     test_data data;
1713 
1714     memset(&data, 0, sizeof(data));
1715     data.machine = machine;
1716     data.variant = ".acpierst";
1717     params = g_strdup_printf(
1718         " -object memory-backend-file,id=erstnvram,"
1719             "mem-path=%s,size=0x10000,share=on"
1720         " -device acpi-erst,memdev=erstnvram", tmp_path);
1721     test_acpi_one(params, &data);
1722     free_test_data(&data);
1723     g_free(params);
1724     g_assert(g_rmdir(tmp_path) == 0);
1725     g_free(tmp_path);
1726 }
1727 
1728 static void test_acpi_piix4_acpi_erst(void)
1729 {
1730     test_acpi_erst(MACHINE_PC);
1731 }
1732 
1733 static void test_acpi_q35_acpi_erst(void)
1734 {
1735     test_acpi_erst(MACHINE_Q35);
1736 }
1737 
1738 static void test_acpi_microvm_acpi_erst(void)
1739 {
1740     gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
1741     gchar *params;
1742     test_data data;
1743 
1744     test_acpi_microvm_prepare(&data);
1745     data.variant = ".pcie";
1746     data.tcg_only = true; /* need constant host-phys-bits */
1747     params = g_strdup_printf(" -machine microvm,"
1748         "acpi=on,ioapic2=off,rtc=off,pcie=on"
1749         " -object memory-backend-file,id=erstnvram,"
1750            "mem-path=%s,size=0x10000,share=on"
1751         " -device acpi-erst,memdev=erstnvram", tmp_path);
1752     test_acpi_one(params, &data);
1753     g_free(params);
1754     g_assert(g_rmdir(tmp_path) == 0);
1755     g_free(tmp_path);
1756     free_test_data(&data);
1757 }
1758 #endif /* CONFIG_POSIX */
1759 
1760 static void test_acpi_virt_tcg(void)
1761 {
1762     test_data data = {
1763         .machine = "virt",
1764         .tcg_only = true,
1765         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1766         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1767         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1768         .ram_start = 0x40000000ULL,
1769         .scan_len = 128ULL * 1024 * 1024,
1770     };
1771 
1772     data.smbios_cpu_max_speed = 2900;
1773     data.smbios_cpu_curr_speed = 2700;
1774     test_acpi_one("-cpu cortex-a57 "
1775                   "-smbios type=4,max-speed=2900,current-speed=2700", &data);
1776     free_test_data(&data);
1777 }
1778 
1779 static void test_acpi_virt_tcg_topology(void)
1780 {
1781     test_data data = {
1782         .machine = "virt",
1783         .variant = ".topology",
1784         .tcg_only = true,
1785         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1786         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1787         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1788         .ram_start = 0x40000000ULL,
1789         .scan_len = 128ULL * 1024 * 1024,
1790     };
1791 
1792     test_acpi_one("-cpu cortex-a57 "
1793                   "-smp sockets=1,clusters=2,cores=2,threads=2", &data);
1794     free_test_data(&data);
1795 }
1796 
1797 static void test_acpi_q35_viot(void)
1798 {
1799     test_data data = {
1800         .machine = MACHINE_Q35,
1801         .variant = ".viot",
1802     };
1803 
1804     /*
1805      * To keep things interesting, two buses bypass the IOMMU.
1806      * VIOT should only describes the other two buses.
1807      */
1808     test_acpi_one("-machine default_bus_bypass_iommu=on "
1809                   "-device virtio-iommu-pci "
1810                   "-device pxb-pcie,bus_nr=0x10,id=pcie.100,bus=pcie.0 "
1811                   "-device pxb-pcie,bus_nr=0x20,id=pcie.200,bus=pcie.0,bypass_iommu=on "
1812                   "-device pxb-pcie,bus_nr=0x30,id=pcie.300,bus=pcie.0",
1813                   &data);
1814     free_test_data(&data);
1815 }
1816 
1817 #ifdef CONFIG_POSIX
1818 static void test_acpi_q35_cxl(void)
1819 {
1820     gchar *tmp_path = g_dir_make_tmp("qemu-test-cxl.XXXXXX", NULL);
1821     gchar *params;
1822 
1823     test_data data = {
1824         .machine = MACHINE_Q35,
1825         .variant = ".cxl",
1826     };
1827     /*
1828      * A complex CXL setup.
1829      */
1830     params = g_strdup_printf(" -machine cxl=on"
1831                              " -object memory-backend-file,id=cxl-mem1,mem-path=%s,size=256M"
1832                              " -object memory-backend-file,id=cxl-mem2,mem-path=%s,size=256M"
1833                              " -object memory-backend-file,id=cxl-mem3,mem-path=%s,size=256M"
1834                              " -object memory-backend-file,id=cxl-mem4,mem-path=%s,size=256M"
1835                              " -object memory-backend-file,id=lsa1,mem-path=%s,size=256M"
1836                              " -object memory-backend-file,id=lsa2,mem-path=%s,size=256M"
1837                              " -object memory-backend-file,id=lsa3,mem-path=%s,size=256M"
1838                              " -object memory-backend-file,id=lsa4,mem-path=%s,size=256M"
1839                              " -device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1"
1840                              " -device pxb-cxl,bus_nr=222,bus=pcie.0,id=cxl.2"
1841                              " -device cxl-rp,port=0,bus=cxl.1,id=rp1,chassis=0,slot=2"
1842                              " -device cxl-type3,bus=rp1,memdev=cxl-mem1,lsa=lsa1"
1843                              " -device cxl-rp,port=1,bus=cxl.1,id=rp2,chassis=0,slot=3"
1844                              " -device cxl-type3,bus=rp2,memdev=cxl-mem2,lsa=lsa2"
1845                              " -device cxl-rp,port=0,bus=cxl.2,id=rp3,chassis=0,slot=5"
1846                              " -device cxl-type3,bus=rp3,memdev=cxl-mem3,lsa=lsa3"
1847                              " -device cxl-rp,port=1,bus=cxl.2,id=rp4,chassis=0,slot=6"
1848                              " -device cxl-type3,bus=rp4,memdev=cxl-mem4,lsa=lsa4"
1849                              " -M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G,cxl-fmw.0.interleave-granularity=8k,"
1850                              "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",
1851                              tmp_path, tmp_path, tmp_path, tmp_path,
1852                              tmp_path, tmp_path, tmp_path, tmp_path);
1853     test_acpi_one(params, &data);
1854 
1855     g_free(params);
1856     g_assert(g_rmdir(tmp_path) == 0);
1857     g_free(tmp_path);
1858     free_test_data(&data);
1859 }
1860 #endif /* CONFIG_POSIX */
1861 
1862 static void test_acpi_virt_viot(void)
1863 {
1864     test_data data = {
1865         .machine = "virt",
1866         .tcg_only = true,
1867         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1868         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1869         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1870         .ram_start = 0x40000000ULL,
1871         .scan_len = 128ULL * 1024 * 1024,
1872     };
1873 
1874     test_acpi_one("-cpu cortex-a57 "
1875                   "-device virtio-iommu-pci", &data);
1876     free_test_data(&data);
1877 }
1878 
1879 #ifndef _WIN32
1880 # define DEV_NULL "/dev/null"
1881 #else
1882 # define DEV_NULL "nul"
1883 #endif
1884 
1885 static void test_acpi_q35_slic(void)
1886 {
1887     test_data data = {
1888         .machine = MACHINE_Q35,
1889         .variant = ".slic",
1890     };
1891 
1892     test_acpi_one("-acpitable sig=SLIC,oem_id=\"CRASH \",oem_table_id=ME,"
1893                   "oem_rev=00002210,asl_compiler_id=qemu,"
1894                   "asl_compiler_rev=00000000,data=" DEV_NULL,
1895                   &data);
1896     free_test_data(&data);
1897 }
1898 
1899 static void test_acpi_q35_applesmc(void)
1900 {
1901     test_data data = {
1902         .machine = MACHINE_Q35,
1903         .variant = ".applesmc",
1904     };
1905 
1906     /* supply fake 64-byte OSK to silence missing key warning */
1907     test_acpi_one("-device isa-applesmc,osk=any64characterfakeoskisenough"
1908                   "topreventinvalidkeywarningsonstderr", &data);
1909     free_test_data(&data);
1910 }
1911 
1912 static void test_acpi_q35_pvpanic_isa(void)
1913 {
1914     test_data data = {
1915         .machine = MACHINE_Q35,
1916         .variant = ".pvpanic-isa",
1917     };
1918 
1919     test_acpi_one("-device pvpanic", &data);
1920     free_test_data(&data);
1921 }
1922 
1923 static void test_oem_fields(test_data *data)
1924 {
1925     int i;
1926 
1927     for (i = 0; i < data->tables->len; ++i) {
1928         AcpiSdtTable *sdt;
1929 
1930         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
1931         /* FACS doesn't have OEMID and OEMTABLEID fields */
1932         if (compare_signature(sdt, "FACS")) {
1933             continue;
1934         }
1935 
1936         g_assert(strncmp((char *)sdt->aml + 10, OEM_ID, 6) == 0);
1937         g_assert(strncmp((char *)sdt->aml + 16, OEM_TABLE_ID, 8) == 0);
1938     }
1939 }
1940 
1941 static void test_acpi_piix4_oem_fields(void)
1942 {
1943     test_data data;
1944     char *args;
1945 
1946     memset(&data, 0, sizeof(data));
1947     data.machine = MACHINE_PC;
1948     data.required_struct_types = base_required_struct_types;
1949     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1950 
1951     args = test_acpi_create_args(&data, OEM_TEST_ARGS);
1952     data.qts = qtest_init(args);
1953     test_acpi_load_tables(&data);
1954     test_oem_fields(&data);
1955     qtest_quit(data.qts);
1956     free_test_data(&data);
1957     g_free(args);
1958 }
1959 
1960 static void test_acpi_q35_oem_fields(void)
1961 {
1962     test_data data;
1963     char *args;
1964 
1965     memset(&data, 0, sizeof(data));
1966     data.machine = MACHINE_Q35;
1967     data.required_struct_types = base_required_struct_types;
1968     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1969 
1970     args = test_acpi_create_args(&data, OEM_TEST_ARGS);
1971     data.qts = qtest_init(args);
1972     test_acpi_load_tables(&data);
1973     test_oem_fields(&data);
1974     qtest_quit(data.qts);
1975     free_test_data(&data);
1976     g_free(args);
1977 }
1978 
1979 static void test_acpi_microvm_oem_fields(void)
1980 {
1981     test_data data;
1982     char *args;
1983 
1984     test_acpi_microvm_prepare(&data);
1985 
1986     args = test_acpi_create_args(&data,
1987                                  OEM_TEST_ARGS",acpi=on");
1988     data.qts = qtest_init(args);
1989     test_acpi_load_tables(&data);
1990     test_oem_fields(&data);
1991     qtest_quit(data.qts);
1992     free_test_data(&data);
1993     g_free(args);
1994 }
1995 
1996 static void test_acpi_virt_oem_fields(void)
1997 {
1998     test_data data = {
1999         .machine = "virt",
2000         .tcg_only = true,
2001         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
2002         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
2003         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
2004         .ram_start = 0x40000000ULL,
2005         .scan_len = 128ULL * 1024 * 1024,
2006     };
2007     char *args;
2008 
2009     args = test_acpi_create_args(&data, "-cpu cortex-a57 "OEM_TEST_ARGS);
2010     data.qts = qtest_init(args);
2011     test_acpi_load_tables(&data);
2012     test_oem_fields(&data);
2013     qtest_quit(data.qts);
2014     free_test_data(&data);
2015     g_free(args);
2016 }
2017 
2018 
2019 int main(int argc, char *argv[])
2020 {
2021     const char *arch = qtest_get_arch();
2022     const bool has_kvm = qtest_has_accel("kvm");
2023     const bool has_tcg = qtest_has_accel("tcg");
2024     int ret;
2025 
2026     g_test_init(&argc, &argv, NULL);
2027 
2028     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
2029         ret = boot_sector_init(disk);
2030         if (ret) {
2031             return ret;
2032         }
2033         if (qtest_has_machine(MACHINE_PC)) {
2034             qtest_add_func("acpi/piix4", test_acpi_piix4_tcg);
2035             qtest_add_func("acpi/piix4/oem-fields", test_acpi_piix4_oem_fields);
2036             qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge);
2037             qtest_add_func("acpi/piix4/pci-hotplug/no_root_hotplug",
2038                            test_acpi_piix4_no_root_hotplug);
2039             qtest_add_func("acpi/piix4/pci-hotplug/no_bridge_hotplug",
2040                            test_acpi_piix4_no_bridge_hotplug);
2041             qtest_add_func("acpi/piix4/pci-hotplug/off",
2042                            test_acpi_piix4_no_acpi_pci_hotplug);
2043             qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi);
2044             qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp);
2045             qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
2046             qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
2047             qtest_add_func("acpi/piix4/nosmm", test_acpi_piix4_tcg_nosmm);
2048             qtest_add_func("acpi/piix4/smm-compat",
2049                            test_acpi_piix4_tcg_smm_compat);
2050             qtest_add_func("acpi/piix4/smm-compat-nosmm",
2051                            test_acpi_piix4_tcg_smm_compat_nosmm);
2052             qtest_add_func("acpi/piix4/nohpet", test_acpi_piix4_tcg_nohpet);
2053             qtest_add_func("acpi/piix4/dimmpxm", test_acpi_piix4_tcg_dimm_pxm);
2054             qtest_add_func("acpi/piix4/acpihmat",
2055                            test_acpi_piix4_tcg_acpi_hmat);
2056 #ifdef CONFIG_POSIX
2057             qtest_add_func("acpi/piix4/acpierst", test_acpi_piix4_acpi_erst);
2058 #endif
2059         }
2060         if (qtest_has_machine(MACHINE_Q35)) {
2061             qtest_add_func("acpi/q35", test_acpi_q35_tcg);
2062             qtest_add_func("acpi/q35/oem-fields", test_acpi_q35_oem_fields);
2063             if (tpm_model_is_available("-machine q35", "tpm-tis")) {
2064                 qtest_add_func("acpi/q35/tpm2-tis", test_acpi_q35_tcg_tpm2_tis);
2065                 qtest_add_func("acpi/q35/tpm12-tis",
2066                                test_acpi_q35_tcg_tpm12_tis);
2067             }
2068             qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
2069             qtest_add_func("acpi/q35/multif-bridge",
2070                            test_acpi_q35_multif_bridge);
2071             qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64);
2072             qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi);
2073             qtest_add_func("acpi/q35/smbus/ipmi", test_acpi_q35_tcg_smbus_ipmi);
2074             qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp);
2075             qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
2076             qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
2077             qtest_add_func("acpi/q35/nosmm", test_acpi_q35_tcg_nosmm);
2078             qtest_add_func("acpi/q35/smm-compat",
2079                            test_acpi_q35_tcg_smm_compat);
2080             qtest_add_func("acpi/q35/smm-compat-nosmm",
2081                            test_acpi_q35_tcg_smm_compat_nosmm);
2082             qtest_add_func("acpi/q35/nohpet", test_acpi_q35_tcg_nohpet);
2083             qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm);
2084             qtest_add_func("acpi/q35/acpihmat", test_acpi_q35_tcg_acpi_hmat);
2085             qtest_add_func("acpi/q35/acpihmat-noinitiator",
2086                            test_acpi_q35_tcg_acpi_hmat_noinitiator);
2087 #ifdef CONFIG_POSIX
2088             qtest_add_func("acpi/q35/acpierst", test_acpi_q35_acpi_erst);
2089 #endif
2090             qtest_add_func("acpi/q35/applesmc", test_acpi_q35_applesmc);
2091             qtest_add_func("acpi/q35/pvpanic-isa", test_acpi_q35_pvpanic_isa);
2092             if (has_tcg) {
2093                 qtest_add_func("acpi/q35/ivrs", test_acpi_q35_tcg_ivrs);
2094             }
2095             if (has_kvm) {
2096                 qtest_add_func("acpi/q35/kvm/xapic", test_acpi_q35_kvm_xapic);
2097                 qtest_add_func("acpi/q35/kvm/dmar", test_acpi_q35_kvm_dmar);
2098                 qtest_add_func("acpi/q35/core-count2",
2099                                test_acpi_q35_tcg_core_count2);
2100             }
2101             qtest_add_func("acpi/q35/viot", test_acpi_q35_viot);
2102 #ifdef CONFIG_POSIX
2103             qtest_add_func("acpi/q35/cxl", test_acpi_q35_cxl);
2104 #endif
2105             qtest_add_func("acpi/q35/slic", test_acpi_q35_slic);
2106         }
2107         if (qtest_has_machine("microvm")) {
2108             qtest_add_func("acpi/microvm", test_acpi_microvm_tcg);
2109             qtest_add_func("acpi/microvm/usb", test_acpi_microvm_usb_tcg);
2110             qtest_add_func("acpi/microvm/rtc", test_acpi_microvm_rtc_tcg);
2111             qtest_add_func("acpi/microvm/ioapic2",
2112                            test_acpi_microvm_ioapic2_tcg);
2113             qtest_add_func("acpi/microvm/oem-fields",
2114                            test_acpi_microvm_oem_fields);
2115             if (has_tcg) {
2116                 if (strcmp(arch, "x86_64") == 0) {
2117                     qtest_add_func("acpi/microvm/pcie",
2118                                    test_acpi_microvm_pcie_tcg);
2119 #ifdef CONFIG_POSIX
2120                     qtest_add_func("acpi/microvm/acpierst",
2121                                    test_acpi_microvm_acpi_erst);
2122 #endif
2123                 }
2124             }
2125         }
2126     } else if (strcmp(arch, "aarch64") == 0) {
2127         if (has_tcg) {
2128             qtest_add_func("acpi/virt", test_acpi_virt_tcg);
2129             qtest_add_func("acpi/virt/acpihmatvirt",
2130                             test_acpi_virt_tcg_acpi_hmat);
2131             qtest_add_func("acpi/virt/topology", test_acpi_virt_tcg_topology);
2132             qtest_add_func("acpi/virt/numamem", test_acpi_virt_tcg_numamem);
2133             qtest_add_func("acpi/virt/memhp", test_acpi_virt_tcg_memhp);
2134             qtest_add_func("acpi/virt/pxb", test_acpi_virt_tcg_pxb);
2135             qtest_add_func("acpi/virt/oem-fields", test_acpi_virt_oem_fields);
2136             qtest_add_func("acpi/virt/viot", test_acpi_virt_viot);
2137         }
2138     }
2139     ret = g_test_run();
2140     boot_sector_cleanup(disk);
2141     return ret;
2142 }
2143