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