xref: /openbmc/qemu/tests/qtest/bios-tables-test.c (revision f03359a8)
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_count(void)
1003 {
1004     test_data data = {
1005         .machine = MACHINE_Q35,
1006         .variant = ".core-count",
1007         .required_struct_types = base_required_struct_types,
1008         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1009         .smbios_core_count = 9,
1010         .smbios_core_count2 = 9,
1011     };
1012 
1013     test_acpi_one("-machine smbios-entry-point-type=64 "
1014                   "-smp 54,sockets=2,dies=3,cores=3,threads=3",
1015                   &data);
1016     free_test_data(&data);
1017 }
1018 
1019 static void test_acpi_q35_tcg_core_count2(void)
1020 {
1021     test_data data = {
1022         .machine = MACHINE_Q35,
1023         .variant = ".core-count2",
1024         .required_struct_types = base_required_struct_types,
1025         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
1026         .smbios_core_count = 0xFF,
1027         .smbios_core_count2 = 260,
1028     };
1029 
1030     test_acpi_one("-machine smbios-entry-point-type=64 "
1031                   "-smp 260,dies=2,cores=130,threads=1",
1032                   &data);
1033     free_test_data(&data);
1034 }
1035 
1036 static void test_acpi_q35_tcg_bridge(void)
1037 {
1038     test_data data = {};
1039 
1040     data.machine = MACHINE_Q35;
1041     data.variant = ".bridge";
1042     data.required_struct_types = base_required_struct_types;
1043     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1044     test_acpi_one("-device pci-bridge,chassis_nr=1,id=br1"
1045                   " -device pci-testdev,bus=pcie.0"
1046                   " -device pci-testdev,bus=br1", &data);
1047     free_test_data(&data);
1048 }
1049 
1050 static void test_acpi_q35_tcg_no_acpi_hotplug(void)
1051 {
1052     test_data data = {};
1053 
1054     data.machine = MACHINE_Q35;
1055     data.variant = ".noacpihp";
1056     data.required_struct_types = base_required_struct_types;
1057     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1058     test_acpi_one("-global ICH9-LPC.acpi-pci-hotplug-with-bridge-support=off"
1059         " -device pci-testdev,bus=pcie.0,acpi-index=101,addr=3.0"
1060         " -device pci-bridge,chassis_nr=1,id=shpcbr,addr=4.0"
1061         " -device pci-testdev,bus=shpcbr,addr=1.0,acpi-index=201"
1062         " -device pci-bridge,chassis_nr=2,shpc=off,id=noshpcbr,addr=5.0"
1063         " -device pci-testdev,bus=noshpcbr,addr=1.0,acpi-index=301"
1064         " -device pcie-root-port,id=hprp,port=0x0,chassis=1,addr=6.0"
1065         " -device pci-testdev,bus=hprp,acpi-index=401"
1066         " -device pcie-root-port,id=nohprp,port=0x0,chassis=2,hotplug=off,"
1067                                  "addr=7.0"
1068         " -device pci-testdev,bus=nohprp,acpi-index=501"
1069         " -device pcie-root-port,id=nohprpint,port=0x0,chassis=3,hotplug=off,"
1070                                  "multifunction=on,addr=8.0"
1071         " -device pci-testdev,bus=nohprpint,acpi-index=601,addr=0.1"
1072         " -device pcie-root-port,id=hprp2,port=0x0,chassis=4,bus=nohprpint,"
1073                                  "addr=0.2"
1074         " -device pci-testdev,bus=hprp2,acpi-index=602"
1075         , &data);
1076     free_test_data(&data);
1077 }
1078 
1079 static void test_acpi_q35_multif_bridge(void)
1080 {
1081     test_data data = {
1082         .machine = MACHINE_Q35,
1083         .variant = ".multi-bridge",
1084     };
1085     test_vm_prepare("-S"
1086         " -device virtio-balloon,id=balloon0,addr=0x4.0x2"
1087         " -device pcie-root-port,id=rp0,multifunction=on,"
1088                   "port=0x0,chassis=1,addr=0x2"
1089         " -device pcie-root-port,id=rp1,port=0x1,chassis=2,addr=0x3.0x1"
1090         " -device pcie-root-port,id=rp2,port=0x0,chassis=3,bus=rp1,addr=0.0"
1091         " -device pci-bridge,bus=rp2,chassis_nr=4,id=br1"
1092         " -device pcie-root-port,id=rphptgt1,port=0x0,chassis=5,addr=2.1"
1093         " -device pcie-root-port,id=rphptgt2,port=0x0,chassis=6,addr=2.2"
1094         " -device pcie-root-port,id=rphptgt3,port=0x0,chassis=7,addr=2.3"
1095         " -device pci-testdev,bus=pcie.0,addr=2.4"
1096         " -device pci-testdev,bus=pcie.0,addr=2.5,acpi-index=102"
1097         " -device pci-testdev,bus=pcie.0,addr=5.0"
1098         " -device pci-testdev,bus=pcie.0,addr=0xf.0,acpi-index=101"
1099         " -device pci-testdev,bus=rp0,addr=0.0"
1100         " -device pci-testdev,bus=br1"
1101         " -device pcie-root-port,id=rpnohp,chassis=8,addr=0xA.0,hotplug=off"
1102         " -device pcie-root-port,id=rp3,chassis=9,bus=rpnohp"
1103         , &data);
1104 
1105     /* hotplugged bridges section */
1106     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr1",
1107         "{'bus': 'br1', 'addr': '6.0', 'chassis_nr': 128 }");
1108     qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr2-multiif",
1109         "{ 'bus': 'br1', 'addr': '2.2', 'chassis_nr': 129 }");
1110     qtest_qmp_device_add(data.qts, "pcie-pci-bridge", "hpbr3",
1111         "{'bus': 'rphptgt1', 'addr': '0.0' }");
1112     qtest_qmp_device_add(data.qts, "pcie-root-port", "hprp",
1113         "{'bus': 'rphptgt2', 'addr': '0.0' }");
1114     qtest_qmp_device_add(data.qts, "pci-testdev", "hpnic",
1115         "{'bus': 'rphptgt3', 'addr': '0.0' }");
1116     qtest_qmp_send(data.qts, "{'execute':'cont' }");
1117     qtest_qmp_eventwait(data.qts, "RESUME");
1118 
1119     process_acpi_tables_noexit(&data);
1120     free_test_data(&data);
1121 
1122     /* check that reboot/reset doesn't change any ACPI tables  */
1123     qtest_qmp_send(data.qts, "{'execute':'system_reset' }");
1124     process_acpi_tables(&data);
1125     free_test_data(&data);
1126 }
1127 
1128 static void test_acpi_q35_tcg_mmio64(void)
1129 {
1130     test_data data = {
1131         .machine = MACHINE_Q35,
1132         .variant = ".mmio64",
1133         .tcg_only = true,
1134         .required_struct_types = base_required_struct_types,
1135         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types)
1136     };
1137 
1138     test_acpi_one("-m 128M,slots=1,maxmem=2G "
1139                   "-cpu Opteron_G1 "
1140                   "-object memory-backend-ram,id=ram0,size=128M "
1141                   "-numa node,memdev=ram0 "
1142                   "-device pci-testdev,membar=2G",
1143                   &data);
1144     free_test_data(&data);
1145 }
1146 
1147 static void test_acpi_piix4_tcg_cphp(void)
1148 {
1149     test_data data = {};
1150 
1151     data.machine = MACHINE_PC;
1152     data.variant = ".cphp";
1153     test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
1154                   " -object memory-backend-ram,id=ram0,size=64M"
1155                   " -object memory-backend-ram,id=ram1,size=64M"
1156                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1157                   " -numa dist,src=0,dst=1,val=21",
1158                   &data);
1159     free_test_data(&data);
1160 }
1161 
1162 static void test_acpi_q35_tcg_cphp(void)
1163 {
1164     test_data data = {};
1165 
1166     data.machine = MACHINE_Q35;
1167     data.variant = ".cphp";
1168     test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
1169                   " -object memory-backend-ram,id=ram0,size=64M"
1170                   " -object memory-backend-ram,id=ram1,size=64M"
1171                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1172                   " -numa dist,src=0,dst=1,val=21",
1173                   &data);
1174     free_test_data(&data);
1175 }
1176 
1177 static uint8_t ipmi_required_struct_types[] = {
1178     0, 1, 3, 4, 16, 17, 19, 32, 38, 127
1179 };
1180 
1181 static void test_acpi_q35_tcg_ipmi(void)
1182 {
1183     test_data data = {};
1184 
1185     data.machine = MACHINE_Q35;
1186     data.variant = ".ipmibt";
1187     data.required_struct_types = ipmi_required_struct_types;
1188     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1189     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1190                   " -device isa-ipmi-bt,bmc=bmc0",
1191                   &data);
1192     free_test_data(&data);
1193 }
1194 
1195 static void test_acpi_q35_tcg_smbus_ipmi(void)
1196 {
1197     test_data data = {};
1198 
1199     data.machine = MACHINE_Q35;
1200     data.variant = ".ipmismbus";
1201     data.required_struct_types = ipmi_required_struct_types;
1202     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1203     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1204                   " -device smbus-ipmi,bmc=bmc0",
1205                   &data);
1206     free_test_data(&data);
1207 }
1208 
1209 static void test_acpi_piix4_tcg_ipmi(void)
1210 {
1211     test_data data = {};
1212 
1213     /* Supplying -machine accel argument overrides the default (qtest).
1214      * This is to make guest actually run.
1215      */
1216     data.machine = MACHINE_PC;
1217     data.variant = ".ipmikcs";
1218     data.required_struct_types = ipmi_required_struct_types;
1219     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
1220     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
1221                   " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
1222                   &data);
1223     free_test_data(&data);
1224 }
1225 
1226 static void test_acpi_q35_tcg_memhp(void)
1227 {
1228     test_data data = {};
1229 
1230     data.machine = MACHINE_Q35;
1231     data.variant = ".memhp";
1232     test_acpi_one(" -m 128,slots=3,maxmem=1G"
1233                   " -object memory-backend-ram,id=ram0,size=64M"
1234                   " -object memory-backend-ram,id=ram1,size=64M"
1235                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1236                   " -numa dist,src=0,dst=1,val=21",
1237                   &data);
1238     free_test_data(&data);
1239 }
1240 
1241 static void test_acpi_piix4_tcg_memhp(void)
1242 {
1243     test_data data = {};
1244 
1245     data.machine = MACHINE_PC;
1246     data.variant = ".memhp";
1247     test_acpi_one(" -m 128,slots=3,maxmem=1G"
1248                   " -object memory-backend-ram,id=ram0,size=64M"
1249                   " -object memory-backend-ram,id=ram1,size=64M"
1250                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1251                   " -numa dist,src=0,dst=1,val=21",
1252                   &data);
1253     free_test_data(&data);
1254 }
1255 
1256 static void test_acpi_piix4_tcg_nosmm(void)
1257 {
1258     test_data data = {};
1259 
1260     data.machine = MACHINE_PC;
1261     data.variant = ".nosmm";
1262     test_acpi_one("-machine smm=off", &data);
1263     free_test_data(&data);
1264 }
1265 
1266 static void test_acpi_piix4_tcg_smm_compat(void)
1267 {
1268     test_data data = {};
1269 
1270     data.machine = MACHINE_PC;
1271     data.variant = ".smm-compat";
1272     test_acpi_one("-global PIIX4_PM.smm-compat=on", &data);
1273     free_test_data(&data);
1274 }
1275 
1276 static void test_acpi_piix4_tcg_smm_compat_nosmm(void)
1277 {
1278     test_data data = {};
1279 
1280     data.machine = MACHINE_PC;
1281     data.variant = ".smm-compat-nosmm";
1282     test_acpi_one("-global PIIX4_PM.smm-compat=on -machine smm=off", &data);
1283     free_test_data(&data);
1284 }
1285 
1286 static void test_acpi_piix4_tcg_nohpet(void)
1287 {
1288     test_data data = {};
1289 
1290     data.machine = MACHINE_PC;
1291     data.machine_param = ",hpet=off";
1292     data.variant = ".nohpet";
1293     test_acpi_one(NULL, &data);
1294     free_test_data(&data);
1295 }
1296 
1297 static void test_acpi_q35_tcg_numamem(void)
1298 {
1299     test_data data = {};
1300 
1301     data.machine = MACHINE_Q35;
1302     data.variant = ".numamem";
1303     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1304                   " -numa node -numa node,memdev=ram0", &data);
1305     free_test_data(&data);
1306 }
1307 
1308 static void test_acpi_q35_kvm_xapic(void)
1309 {
1310     test_data data = {};
1311 
1312     data.machine = MACHINE_Q35;
1313     data.variant = ".xapic";
1314     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1315                   " -numa node -numa node,memdev=ram0"
1316                   " -machine kernel-irqchip=on -smp 1,maxcpus=288", &data);
1317     free_test_data(&data);
1318 }
1319 
1320 static void test_acpi_q35_tcg_nosmm(void)
1321 {
1322     test_data data = {};
1323 
1324     data.machine = MACHINE_Q35;
1325     data.variant = ".nosmm";
1326     test_acpi_one("-machine smm=off", &data);
1327     free_test_data(&data);
1328 }
1329 
1330 static void test_acpi_q35_tcg_smm_compat(void)
1331 {
1332     test_data data = {};
1333 
1334     data.machine = MACHINE_Q35;
1335     data.variant = ".smm-compat";
1336     test_acpi_one("-global ICH9-LPC.smm-compat=on", &data);
1337     free_test_data(&data);
1338 }
1339 
1340 static void test_acpi_q35_tcg_smm_compat_nosmm(void)
1341 {
1342     test_data data = {};
1343 
1344     data.machine = MACHINE_Q35;
1345     data.variant = ".smm-compat-nosmm";
1346     test_acpi_one("-global ICH9-LPC.smm-compat=on -machine smm=off", &data);
1347     free_test_data(&data);
1348 }
1349 
1350 static void test_acpi_q35_tcg_nohpet(void)
1351 {
1352     test_data data = {};
1353 
1354     data.machine = MACHINE_Q35;
1355     data.machine_param = ",hpet=off";
1356     data.variant = ".nohpet";
1357     test_acpi_one(NULL, &data);
1358     free_test_data(&data);
1359 }
1360 
1361 static void test_acpi_q35_kvm_dmar(void)
1362 {
1363     test_data data = {};
1364 
1365     data.machine = MACHINE_Q35;
1366     data.variant = ".dmar";
1367     test_acpi_one("-machine kernel-irqchip=split -accel kvm"
1368                   " -device intel-iommu,intremap=on,device-iotlb=on", &data);
1369     free_test_data(&data);
1370 }
1371 
1372 static void test_acpi_q35_tcg_ivrs(void)
1373 {
1374     test_data data = {};
1375 
1376     data.machine = MACHINE_Q35;
1377     data.variant = ".ivrs";
1378     data.tcg_only = true,
1379     test_acpi_one(" -device amd-iommu", &data);
1380     free_test_data(&data);
1381 }
1382 
1383 static void test_acpi_piix4_tcg_numamem(void)
1384 {
1385     test_data data = {};
1386 
1387     data.machine = MACHINE_PC;
1388     data.variant = ".numamem";
1389     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1390                   " -numa node -numa node,memdev=ram0", &data);
1391     free_test_data(&data);
1392 }
1393 
1394 uint64_t tpm_tis_base_addr;
1395 
1396 static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
1397                               uint64_t base, enum TPMVersion tpm_version)
1398 {
1399     gchar *tmp_dir_name = g_strdup_printf("qemu-test_acpi_%s_tcg_%s.XXXXXX",
1400                                           machine, tpm_if);
1401     char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
1402     TPMTestState test;
1403     test_data data = {};
1404     GThread *thread;
1405     const char *suffix = tpm_version == TPM_VERSION_2_0 ? "tpm2" : "tpm12";
1406     char *args, *variant = g_strdup_printf(".%s.%s", tpm_if, suffix);
1407 
1408     tpm_tis_base_addr = base;
1409 
1410     module_call_init(MODULE_INIT_QOM);
1411 
1412     test.addr = g_new0(SocketAddress, 1);
1413     test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
1414     test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
1415     g_mutex_init(&test.data_mutex);
1416     g_cond_init(&test.data_cond);
1417     test.data_cond_signal = false;
1418     test.tpm_version = tpm_version;
1419 
1420     thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
1421     tpm_emu_test_wait_cond(&test);
1422 
1423     data.machine = machine;
1424     data.variant = variant;
1425 
1426     args = g_strdup_printf(
1427         " -chardev socket,id=chr,path=%s"
1428         " -tpmdev emulator,id=dev,chardev=chr"
1429         " -device tpm-%s,tpmdev=dev",
1430         test.addr->u.q_unix.path, tpm_if);
1431 
1432     test_acpi_one(args, &data);
1433 
1434     g_thread_join(thread);
1435     g_unlink(test.addr->u.q_unix.path);
1436     qapi_free_SocketAddress(test.addr);
1437     g_rmdir(tmp_path);
1438     g_free(variant);
1439     g_free(tmp_path);
1440     g_free(tmp_dir_name);
1441     g_free(args);
1442     free_test_data(&data);
1443 }
1444 
1445 static void test_acpi_q35_tcg_tpm2_tis(void)
1446 {
1447     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_2_0);
1448 }
1449 
1450 static void test_acpi_q35_tcg_tpm12_tis(void)
1451 {
1452     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_1_2);
1453 }
1454 
1455 static void test_acpi_tcg_dimm_pxm(const char *machine)
1456 {
1457     test_data data = {};
1458 
1459     data.machine = machine;
1460     data.variant = ".dimmpxm";
1461     test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
1462                   " -smp 4,sockets=4"
1463                   " -m 128M,slots=3,maxmem=1G"
1464                   " -object memory-backend-ram,id=ram0,size=32M"
1465                   " -object memory-backend-ram,id=ram1,size=32M"
1466                   " -object memory-backend-ram,id=ram2,size=32M"
1467                   " -object memory-backend-ram,id=ram3,size=32M"
1468                   " -numa node,memdev=ram0,nodeid=0"
1469                   " -numa node,memdev=ram1,nodeid=1"
1470                   " -numa node,memdev=ram2,nodeid=2"
1471                   " -numa node,memdev=ram3,nodeid=3"
1472                   " -numa cpu,node-id=0,socket-id=0"
1473                   " -numa cpu,node-id=1,socket-id=1"
1474                   " -numa cpu,node-id=2,socket-id=2"
1475                   " -numa cpu,node-id=3,socket-id=3"
1476                   " -object memory-backend-ram,id=ram4,size=128M"
1477                   " -object memory-backend-ram,id=nvm0,size=128M"
1478                   " -device pc-dimm,id=dimm0,memdev=ram4,node=1"
1479                   " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
1480                   &data);
1481     free_test_data(&data);
1482 }
1483 
1484 static void test_acpi_q35_tcg_dimm_pxm(void)
1485 {
1486     test_acpi_tcg_dimm_pxm(MACHINE_Q35);
1487 }
1488 
1489 static void test_acpi_piix4_tcg_dimm_pxm(void)
1490 {
1491     test_acpi_tcg_dimm_pxm(MACHINE_PC);
1492 }
1493 
1494 static void test_acpi_virt_tcg_memhp(void)
1495 {
1496     test_data data = {
1497         .machine = "virt",
1498         .tcg_only = true,
1499         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1500         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1501         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1502         .ram_start = 0x40000000ULL,
1503         .scan_len = 256ULL * 1024 * 1024,
1504     };
1505 
1506     data.variant = ".memhp";
1507     test_acpi_one(" -machine nvdimm=on"
1508                   " -cpu cortex-a57"
1509                   " -m 256M,slots=3,maxmem=1G"
1510                   " -object memory-backend-ram,id=ram0,size=128M"
1511                   " -object memory-backend-ram,id=ram1,size=128M"
1512                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1513                   " -numa dist,src=0,dst=1,val=21"
1514                   " -object memory-backend-ram,id=ram2,size=128M"
1515                   " -object memory-backend-ram,id=nvm0,size=128M"
1516                   " -device pc-dimm,id=dimm0,memdev=ram2,node=0"
1517                   " -device nvdimm,id=dimm1,memdev=nvm0,node=1",
1518                   &data);
1519 
1520     free_test_data(&data);
1521 
1522 }
1523 
1524 static void test_acpi_microvm_prepare(test_data *data)
1525 {
1526     data->machine = "microvm";
1527     data->required_struct_types = NULL; /* no smbios */
1528     data->required_struct_types_len = 0;
1529     data->blkdev = "virtio-blk-device";
1530 }
1531 
1532 static void test_acpi_microvm_tcg(void)
1533 {
1534     test_data data = {};
1535 
1536     test_acpi_microvm_prepare(&data);
1537     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off",
1538                   &data);
1539     free_test_data(&data);
1540 }
1541 
1542 static void test_acpi_microvm_usb_tcg(void)
1543 {
1544     test_data data = {};
1545 
1546     test_acpi_microvm_prepare(&data);
1547     data.variant = ".usb";
1548     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,usb=on,rtc=off",
1549                   &data);
1550     free_test_data(&data);
1551 }
1552 
1553 static void test_acpi_microvm_rtc_tcg(void)
1554 {
1555     test_data data = {};
1556 
1557     test_acpi_microvm_prepare(&data);
1558     data.variant = ".rtc";
1559     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=on",
1560                   &data);
1561     free_test_data(&data);
1562 }
1563 
1564 static void test_acpi_microvm_pcie_tcg(void)
1565 {
1566     test_data data = {};
1567 
1568     test_acpi_microvm_prepare(&data);
1569     data.variant = ".pcie";
1570     data.tcg_only = true; /* need constant host-phys-bits */
1571     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off,pcie=on",
1572                   &data);
1573     free_test_data(&data);
1574 }
1575 
1576 static void test_acpi_microvm_ioapic2_tcg(void)
1577 {
1578     test_data data = {};
1579 
1580     test_acpi_microvm_prepare(&data);
1581     data.variant = ".ioapic2";
1582     test_acpi_one(" -machine microvm,acpi=on,ioapic2=on,rtc=off",
1583                   &data);
1584     free_test_data(&data);
1585 }
1586 
1587 static void test_acpi_virt_tcg_numamem(void)
1588 {
1589     test_data data = {
1590         .machine = "virt",
1591         .tcg_only = true,
1592         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1593         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1594         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1595         .ram_start = 0x40000000ULL,
1596         .scan_len = 128ULL * 1024 * 1024,
1597     };
1598 
1599     data.variant = ".numamem";
1600     test_acpi_one(" -cpu cortex-a57"
1601                   " -object memory-backend-ram,id=ram0,size=128M"
1602                   " -numa node,memdev=ram0",
1603                   &data);
1604 
1605     free_test_data(&data);
1606 
1607 }
1608 
1609 static void test_acpi_virt_tcg_pxb(void)
1610 {
1611     test_data data = {
1612         .machine = "virt",
1613         .tcg_only = true,
1614         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1615         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1616         .ram_start = 0x40000000ULL,
1617         .scan_len = 128ULL * 1024 * 1024,
1618     };
1619     /*
1620      * While using -cdrom, the cdrom would auto plugged into pxb-pcie,
1621      * the reason is the bus of pxb-pcie is also root bus, it would lead
1622      * to the error only PCI/PCIE bridge could plug onto pxb.
1623      * Therefore,thr cdrom is defined and plugged onto the scsi controller
1624      * to solve the conflicts.
1625      */
1626     data.variant = ".pxb";
1627     test_acpi_one(" -device pcie-root-port,chassis=1,id=pci.1"
1628                   " -device virtio-scsi-pci,id=scsi0,bus=pci.1"
1629                   " -drive file="
1630                   "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2,"
1631                   "if=none,media=cdrom,id=drive-scsi0-0-0-1,readonly=on"
1632                   " -device scsi-cd,bus=scsi0.0,scsi-id=0,"
1633                   "drive=drive-scsi0-0-0-1,id=scsi0-0-0-1,bootindex=1"
1634                   " -cpu cortex-a57"
1635                   " -device pxb-pcie,bus_nr=128",
1636                   &data);
1637 
1638     free_test_data(&data);
1639 }
1640 
1641 static void test_acpi_tcg_acpi_hmat(const char *machine)
1642 {
1643     test_data data = {};
1644 
1645     data.machine = machine;
1646     data.variant = ".acpihmat";
1647     test_acpi_one(" -machine hmat=on"
1648                   " -smp 2,sockets=2"
1649                   " -m 128M,slots=2,maxmem=1G"
1650                   " -object memory-backend-ram,size=64M,id=m0"
1651                   " -object memory-backend-ram,size=64M,id=m1"
1652                   " -numa node,nodeid=0,memdev=m0"
1653                   " -numa node,nodeid=1,memdev=m1,initiator=0"
1654                   " -numa cpu,node-id=0,socket-id=0"
1655                   " -numa cpu,node-id=0,socket-id=1"
1656                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1657                   "data-type=access-latency,latency=1"
1658                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1659                   "data-type=access-bandwidth,bandwidth=65534M"
1660                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1661                   "data-type=access-latency,latency=65534"
1662                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1663                   "data-type=access-bandwidth,bandwidth=32767M"
1664                   " -numa hmat-cache,node-id=0,size=10K,level=1,"
1665                   "associativity=direct,policy=write-back,line=8"
1666                   " -numa hmat-cache,node-id=1,size=10K,level=1,"
1667                   "associativity=direct,policy=write-back,line=8",
1668                   &data);
1669     free_test_data(&data);
1670 }
1671 
1672 static void test_acpi_q35_tcg_acpi_hmat(void)
1673 {
1674     test_acpi_tcg_acpi_hmat(MACHINE_Q35);
1675 }
1676 
1677 static void test_acpi_piix4_tcg_acpi_hmat(void)
1678 {
1679     test_acpi_tcg_acpi_hmat(MACHINE_PC);
1680 }
1681 
1682 static void test_acpi_virt_tcg_acpi_hmat(void)
1683 {
1684     test_data data = {
1685         .machine = "virt",
1686         .tcg_only = true,
1687         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1688         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1689         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1690         .ram_start = 0x40000000ULL,
1691         .scan_len = 128ULL * 1024 * 1024,
1692     };
1693 
1694     data.variant = ".acpihmatvirt";
1695 
1696     test_acpi_one(" -machine hmat=on"
1697                   " -cpu cortex-a57"
1698                   " -smp 4,sockets=2"
1699                   " -m 384M"
1700                   " -object memory-backend-ram,size=128M,id=ram0"
1701                   " -object memory-backend-ram,size=128M,id=ram1"
1702                   " -object memory-backend-ram,size=128M,id=ram2"
1703                   " -numa node,nodeid=0,memdev=ram0"
1704                   " -numa node,nodeid=1,memdev=ram1"
1705                   " -numa node,nodeid=2,memdev=ram2"
1706                   " -numa cpu,node-id=0,socket-id=0"
1707                   " -numa cpu,node-id=0,socket-id=0"
1708                   " -numa cpu,node-id=1,socket-id=1"
1709                   " -numa cpu,node-id=1,socket-id=1"
1710                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1711                   "data-type=access-latency,latency=10"
1712                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1713                   "data-type=access-bandwidth,bandwidth=10485760"
1714                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1715                   "data-type=access-latency,latency=20"
1716                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1717                   "data-type=access-bandwidth,bandwidth=5242880"
1718                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1719                   "data-type=access-latency,latency=30"
1720                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1721                   "data-type=access-bandwidth,bandwidth=1048576"
1722                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1723                   "data-type=access-latency,latency=20"
1724                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1725                   "data-type=access-bandwidth,bandwidth=5242880"
1726                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1727                   "data-type=access-latency,latency=10"
1728                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1729                   "data-type=access-bandwidth,bandwidth=10485760"
1730                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1731                   "data-type=access-latency,latency=30"
1732                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1733                   "data-type=access-bandwidth,bandwidth=1048576",
1734                   &data);
1735 
1736     free_test_data(&data);
1737 }
1738 
1739 static void test_acpi_q35_tcg_acpi_hmat_noinitiator(void)
1740 {
1741     test_data data = {};
1742 
1743     data.machine = MACHINE_Q35;
1744     data.variant = ".acpihmat-noinitiator";
1745     test_acpi_one(" -machine hmat=on"
1746                   " -smp 4,sockets=2"
1747                   " -m 128M"
1748                   " -object memory-backend-ram,size=32M,id=ram0"
1749                   " -object memory-backend-ram,size=32M,id=ram1"
1750                   " -object memory-backend-ram,size=64M,id=ram2"
1751                   " -numa node,nodeid=0,memdev=ram0"
1752                   " -numa node,nodeid=1,memdev=ram1"
1753                   " -numa node,nodeid=2,memdev=ram2"
1754                   " -numa cpu,node-id=0,socket-id=0"
1755                   " -numa cpu,node-id=0,socket-id=0"
1756                   " -numa cpu,node-id=1,socket-id=1"
1757                   " -numa cpu,node-id=1,socket-id=1"
1758                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1759                   "data-type=access-latency,latency=10"
1760                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1761                   "data-type=access-bandwidth,bandwidth=10485760"
1762                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1763                   "data-type=access-latency,latency=20"
1764                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1765                   "data-type=access-bandwidth,bandwidth=5242880"
1766                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1767                   "data-type=access-latency,latency=30"
1768                   " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
1769                   "data-type=access-bandwidth,bandwidth=1048576"
1770                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1771                   "data-type=access-latency,latency=20"
1772                   " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
1773                   "data-type=access-bandwidth,bandwidth=5242880"
1774                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1775                   "data-type=access-latency,latency=10"
1776                   " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
1777                   "data-type=access-bandwidth,bandwidth=10485760"
1778                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1779                   "data-type=access-latency,latency=30"
1780                   " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
1781                   "data-type=access-bandwidth,bandwidth=1048576",
1782                   &data);
1783     free_test_data(&data);
1784 }
1785 
1786 #ifdef CONFIG_POSIX
1787 static void test_acpi_erst(const char *machine)
1788 {
1789     gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
1790     gchar *params;
1791     test_data data = {};
1792 
1793     data.machine = machine;
1794     data.variant = ".acpierst";
1795     params = g_strdup_printf(
1796         " -object memory-backend-file,id=erstnvram,"
1797             "mem-path=%s,size=0x10000,share=on"
1798         " -device acpi-erst,memdev=erstnvram", tmp_path);
1799     test_acpi_one(params, &data);
1800     free_test_data(&data);
1801     g_free(params);
1802     g_assert(g_rmdir(tmp_path) == 0);
1803     g_free(tmp_path);
1804 }
1805 
1806 static void test_acpi_piix4_acpi_erst(void)
1807 {
1808     test_acpi_erst(MACHINE_PC);
1809 }
1810 
1811 static void test_acpi_q35_acpi_erst(void)
1812 {
1813     test_acpi_erst(MACHINE_Q35);
1814 }
1815 
1816 static void test_acpi_microvm_acpi_erst(void)
1817 {
1818     gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
1819     gchar *params;
1820     test_data data = {};
1821 
1822     test_acpi_microvm_prepare(&data);
1823     data.variant = ".pcie";
1824     data.tcg_only = true; /* need constant host-phys-bits */
1825     params = g_strdup_printf(" -machine microvm,"
1826         "acpi=on,ioapic2=off,rtc=off,pcie=on"
1827         " -object memory-backend-file,id=erstnvram,"
1828            "mem-path=%s,size=0x10000,share=on"
1829         " -device acpi-erst,memdev=erstnvram", tmp_path);
1830     test_acpi_one(params, &data);
1831     g_free(params);
1832     g_assert(g_rmdir(tmp_path) == 0);
1833     g_free(tmp_path);
1834     free_test_data(&data);
1835 }
1836 #endif /* CONFIG_POSIX */
1837 
1838 static void test_acpi_virt_tcg(void)
1839 {
1840     test_data data = {
1841         .machine = "virt",
1842         .tcg_only = true,
1843         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1844         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1845         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1846         .ram_start = 0x40000000ULL,
1847         .scan_len = 128ULL * 1024 * 1024,
1848     };
1849 
1850     data.smbios_cpu_max_speed = 2900;
1851     data.smbios_cpu_curr_speed = 2700;
1852     test_acpi_one("-cpu cortex-a57 "
1853                   "-smbios type=4,max-speed=2900,current-speed=2700", &data);
1854     free_test_data(&data);
1855 }
1856 
1857 static void test_acpi_virt_tcg_topology(void)
1858 {
1859     test_data data = {
1860         .machine = "virt",
1861         .variant = ".topology",
1862         .tcg_only = true,
1863         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1864         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1865         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1866         .ram_start = 0x40000000ULL,
1867         .scan_len = 128ULL * 1024 * 1024,
1868     };
1869 
1870     test_acpi_one("-cpu cortex-a57 "
1871                   "-smp sockets=1,clusters=2,cores=2,threads=2", &data);
1872     free_test_data(&data);
1873 }
1874 
1875 static void test_acpi_q35_viot(void)
1876 {
1877     test_data data = {
1878         .machine = MACHINE_Q35,
1879         .variant = ".viot",
1880     };
1881 
1882     /*
1883      * To keep things interesting, two buses bypass the IOMMU.
1884      * VIOT should only describes the other two buses.
1885      */
1886     test_acpi_one("-machine default_bus_bypass_iommu=on "
1887                   "-device virtio-iommu-pci "
1888                   "-device pxb-pcie,bus_nr=0x10,id=pcie.100,bus=pcie.0 "
1889                   "-device pxb-pcie,bus_nr=0x20,id=pcie.200,bus=pcie.0,bypass_iommu=on "
1890                   "-device pxb-pcie,bus_nr=0x30,id=pcie.300,bus=pcie.0",
1891                   &data);
1892     free_test_data(&data);
1893 }
1894 
1895 #ifdef CONFIG_POSIX
1896 static void test_acpi_q35_cxl(void)
1897 {
1898     gchar *tmp_path = g_dir_make_tmp("qemu-test-cxl.XXXXXX", NULL);
1899     gchar *params;
1900 
1901     test_data data = {
1902         .machine = MACHINE_Q35,
1903         .variant = ".cxl",
1904     };
1905     /*
1906      * A complex CXL setup.
1907      */
1908     params = g_strdup_printf(" -machine cxl=on"
1909                              " -object memory-backend-file,id=cxl-mem1,mem-path=%s,size=256M"
1910                              " -object memory-backend-file,id=cxl-mem2,mem-path=%s,size=256M"
1911                              " -object memory-backend-file,id=cxl-mem3,mem-path=%s,size=256M"
1912                              " -object memory-backend-file,id=cxl-mem4,mem-path=%s,size=256M"
1913                              " -object memory-backend-file,id=lsa1,mem-path=%s,size=256M"
1914                              " -object memory-backend-file,id=lsa2,mem-path=%s,size=256M"
1915                              " -object memory-backend-file,id=lsa3,mem-path=%s,size=256M"
1916                              " -object memory-backend-file,id=lsa4,mem-path=%s,size=256M"
1917                              " -device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1"
1918                              " -device pxb-cxl,bus_nr=222,bus=pcie.0,id=cxl.2"
1919                              " -device cxl-rp,port=0,bus=cxl.1,id=rp1,chassis=0,slot=2"
1920                              " -device cxl-type3,bus=rp1,persistent-memdev=cxl-mem1,lsa=lsa1"
1921                              " -device cxl-rp,port=1,bus=cxl.1,id=rp2,chassis=0,slot=3"
1922                              " -device cxl-type3,bus=rp2,persistent-memdev=cxl-mem2,lsa=lsa2"
1923                              " -device cxl-rp,port=0,bus=cxl.2,id=rp3,chassis=0,slot=5"
1924                              " -device cxl-type3,bus=rp3,persistent-memdev=cxl-mem3,lsa=lsa3"
1925                              " -device cxl-rp,port=1,bus=cxl.2,id=rp4,chassis=0,slot=6"
1926                              " -device cxl-type3,bus=rp4,persistent-memdev=cxl-mem4,lsa=lsa4"
1927                              " -M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G,cxl-fmw.0.interleave-granularity=8k,"
1928                              "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",
1929                              tmp_path, tmp_path, tmp_path, tmp_path,
1930                              tmp_path, tmp_path, tmp_path, tmp_path);
1931     test_acpi_one(params, &data);
1932 
1933     g_free(params);
1934     g_assert(g_rmdir(tmp_path) == 0);
1935     g_free(tmp_path);
1936     free_test_data(&data);
1937 }
1938 #endif /* CONFIG_POSIX */
1939 
1940 static void test_acpi_virt_viot(void)
1941 {
1942     test_data data = {
1943         .machine = "virt",
1944         .tcg_only = true,
1945         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1946         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1947         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1948         .ram_start = 0x40000000ULL,
1949         .scan_len = 128ULL * 1024 * 1024,
1950     };
1951 
1952     test_acpi_one("-cpu cortex-a57 "
1953                   "-device virtio-iommu-pci", &data);
1954     free_test_data(&data);
1955 }
1956 
1957 #ifndef _WIN32
1958 # define DEV_NULL "/dev/null"
1959 #else
1960 # define DEV_NULL "nul"
1961 #endif
1962 
1963 static void test_acpi_q35_slic(void)
1964 {
1965     test_data data = {
1966         .machine = MACHINE_Q35,
1967         .variant = ".slic",
1968     };
1969 
1970     test_acpi_one("-acpitable sig=SLIC,oem_id=\"CRASH \",oem_table_id=ME,"
1971                   "oem_rev=00002210,asl_compiler_id=qemu,"
1972                   "asl_compiler_rev=00000000,data=" DEV_NULL,
1973                   &data);
1974     free_test_data(&data);
1975 }
1976 
1977 static void test_acpi_q35_applesmc(void)
1978 {
1979     test_data data = {
1980         .machine = MACHINE_Q35,
1981         .variant = ".applesmc",
1982     };
1983 
1984     /* supply fake 64-byte OSK to silence missing key warning */
1985     test_acpi_one("-device isa-applesmc,osk=any64characterfakeoskisenough"
1986                   "topreventinvalidkeywarningsonstderr", &data);
1987     free_test_data(&data);
1988 }
1989 
1990 static void test_acpi_q35_pvpanic_isa(void)
1991 {
1992     test_data data = {
1993         .machine = MACHINE_Q35,
1994         .variant = ".pvpanic-isa",
1995     };
1996 
1997     test_acpi_one("-device pvpanic", &data);
1998     free_test_data(&data);
1999 }
2000 
2001 static void test_oem_fields(test_data *data)
2002 {
2003     int i;
2004 
2005     for (i = 0; i < data->tables->len; ++i) {
2006         AcpiSdtTable *sdt;
2007 
2008         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
2009         /* FACS doesn't have OEMID and OEMTABLEID fields */
2010         if (compare_signature(sdt, "FACS")) {
2011             continue;
2012         }
2013 
2014         g_assert(strncmp((char *)sdt->aml + 10, OEM_ID, 6) == 0);
2015         g_assert(strncmp((char *)sdt->aml + 16, OEM_TABLE_ID, 8) == 0);
2016     }
2017 }
2018 
2019 static void test_acpi_piix4_oem_fields(void)
2020 {
2021     char *args;
2022     test_data data = {};
2023 
2024     data.machine = MACHINE_PC;
2025     data.required_struct_types = base_required_struct_types;
2026     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
2027 
2028     args = test_acpi_create_args(&data, OEM_TEST_ARGS);
2029     data.qts = qtest_init(args);
2030     test_acpi_load_tables(&data);
2031     test_oem_fields(&data);
2032     qtest_quit(data.qts);
2033     free_test_data(&data);
2034     g_free(args);
2035 }
2036 
2037 static void test_acpi_q35_oem_fields(void)
2038 {
2039     char *args;
2040     test_data data = {};
2041 
2042     data.machine = MACHINE_Q35;
2043     data.required_struct_types = base_required_struct_types;
2044     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
2045 
2046     args = test_acpi_create_args(&data, OEM_TEST_ARGS);
2047     data.qts = qtest_init(args);
2048     test_acpi_load_tables(&data);
2049     test_oem_fields(&data);
2050     qtest_quit(data.qts);
2051     free_test_data(&data);
2052     g_free(args);
2053 }
2054 
2055 static void test_acpi_microvm_oem_fields(void)
2056 {
2057     test_data data = {};
2058     char *args;
2059 
2060     test_acpi_microvm_prepare(&data);
2061 
2062     args = test_acpi_create_args(&data,
2063                                  OEM_TEST_ARGS",acpi=on");
2064     data.qts = qtest_init(args);
2065     test_acpi_load_tables(&data);
2066     test_oem_fields(&data);
2067     qtest_quit(data.qts);
2068     free_test_data(&data);
2069     g_free(args);
2070 }
2071 
2072 static void test_acpi_virt_oem_fields(void)
2073 {
2074     test_data data = {
2075         .machine = "virt",
2076         .tcg_only = true,
2077         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
2078         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
2079         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
2080         .ram_start = 0x40000000ULL,
2081         .scan_len = 128ULL * 1024 * 1024,
2082     };
2083     char *args;
2084 
2085     args = test_acpi_create_args(&data, "-cpu cortex-a57 "OEM_TEST_ARGS);
2086     data.qts = qtest_init(args);
2087     test_acpi_load_tables(&data);
2088     test_oem_fields(&data);
2089     qtest_quit(data.qts);
2090     free_test_data(&data);
2091     g_free(args);
2092 }
2093 
2094 
2095 int main(int argc, char *argv[])
2096 {
2097     const char *arch = qtest_get_arch();
2098     bool has_kvm, has_tcg;
2099     char *v_env = getenv("V");
2100     int ret;
2101 
2102     if (v_env) {
2103         verbosity_level = atoi(v_env);
2104     }
2105 
2106     g_test_init(&argc, &argv, NULL);
2107 
2108     has_kvm = qtest_has_accel("kvm");
2109     has_tcg = qtest_has_accel("tcg");
2110 
2111     if (!has_tcg && !has_kvm) {
2112         g_test_skip("No KVM or TCG accelerator available");
2113         return 0;
2114     }
2115 
2116     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
2117         ret = boot_sector_init(disk);
2118         if (ret) {
2119             return ret;
2120         }
2121         if (qtest_has_machine(MACHINE_PC)) {
2122             qtest_add_func("acpi/piix4", test_acpi_piix4_tcg);
2123             qtest_add_func("acpi/piix4/oem-fields", test_acpi_piix4_oem_fields);
2124             qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge);
2125             qtest_add_func("acpi/piix4/pci-hotplug/no_root_hotplug",
2126                            test_acpi_piix4_no_root_hotplug);
2127             qtest_add_func("acpi/piix4/pci-hotplug/no_bridge_hotplug",
2128                            test_acpi_piix4_no_bridge_hotplug);
2129             qtest_add_func("acpi/piix4/pci-hotplug/off",
2130                            test_acpi_piix4_no_acpi_pci_hotplug);
2131             qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi);
2132             qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp);
2133             qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
2134             qtest_add_func("acpi/piix4/nosmm", test_acpi_piix4_tcg_nosmm);
2135             qtest_add_func("acpi/piix4/smm-compat",
2136                            test_acpi_piix4_tcg_smm_compat);
2137             qtest_add_func("acpi/piix4/smm-compat-nosmm",
2138                            test_acpi_piix4_tcg_smm_compat_nosmm);
2139             qtest_add_func("acpi/piix4/nohpet", test_acpi_piix4_tcg_nohpet);
2140 
2141             /* i386 does not support memory hotplug */
2142             if (strcmp(arch, "i386")) {
2143                 qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
2144                 qtest_add_func("acpi/piix4/dimmpxm",
2145                                test_acpi_piix4_tcg_dimm_pxm);
2146                 qtest_add_func("acpi/piix4/acpihmat",
2147                                test_acpi_piix4_tcg_acpi_hmat);
2148             }
2149 #ifdef CONFIG_POSIX
2150             qtest_add_func("acpi/piix4/acpierst", test_acpi_piix4_acpi_erst);
2151 #endif
2152         }
2153         if (qtest_has_machine(MACHINE_Q35)) {
2154             qtest_add_func("acpi/q35", test_acpi_q35_tcg);
2155             qtest_add_func("acpi/q35/oem-fields", test_acpi_q35_oem_fields);
2156             if (tpm_model_is_available("-machine q35", "tpm-tis")) {
2157                 qtest_add_func("acpi/q35/tpm2-tis", test_acpi_q35_tcg_tpm2_tis);
2158                 qtest_add_func("acpi/q35/tpm12-tis",
2159                                test_acpi_q35_tcg_tpm12_tis);
2160             }
2161             qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
2162             qtest_add_func("acpi/q35/no-acpi-hotplug",
2163                            test_acpi_q35_tcg_no_acpi_hotplug);
2164             qtest_add_func("acpi/q35/multif-bridge",
2165                            test_acpi_q35_multif_bridge);
2166             qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi);
2167             qtest_add_func("acpi/q35/smbus/ipmi", test_acpi_q35_tcg_smbus_ipmi);
2168             qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp);
2169             qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
2170             qtest_add_func("acpi/q35/nosmm", test_acpi_q35_tcg_nosmm);
2171             qtest_add_func("acpi/q35/smm-compat",
2172                            test_acpi_q35_tcg_smm_compat);
2173             qtest_add_func("acpi/q35/smm-compat-nosmm",
2174                            test_acpi_q35_tcg_smm_compat_nosmm);
2175             qtest_add_func("acpi/q35/nohpet", test_acpi_q35_tcg_nohpet);
2176             qtest_add_func("acpi/q35/acpihmat-noinitiator",
2177                            test_acpi_q35_tcg_acpi_hmat_noinitiator);
2178 
2179             /* i386 does not support memory hotplug */
2180             if (strcmp(arch, "i386")) {
2181                 qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
2182                 qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm);
2183                 qtest_add_func("acpi/q35/acpihmat",
2184                                test_acpi_q35_tcg_acpi_hmat);
2185                 qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64);
2186             }
2187 #ifdef CONFIG_POSIX
2188             qtest_add_func("acpi/q35/acpierst", test_acpi_q35_acpi_erst);
2189 #endif
2190             qtest_add_func("acpi/q35/applesmc", test_acpi_q35_applesmc);
2191             qtest_add_func("acpi/q35/pvpanic-isa", test_acpi_q35_pvpanic_isa);
2192             if (has_tcg) {
2193                 qtest_add_func("acpi/q35/ivrs", test_acpi_q35_tcg_ivrs);
2194             }
2195             if (has_kvm) {
2196                 qtest_add_func("acpi/q35/kvm/xapic", test_acpi_q35_kvm_xapic);
2197                 qtest_add_func("acpi/q35/kvm/dmar", test_acpi_q35_kvm_dmar);
2198                 qtest_add_func("acpi/q35/type4-count",
2199                                test_acpi_q35_tcg_type4_count);
2200                 qtest_add_func("acpi/q35/core-count",
2201                                test_acpi_q35_tcg_core_count);
2202                 qtest_add_func("acpi/q35/core-count2",
2203                                test_acpi_q35_tcg_core_count2);
2204             }
2205             if (qtest_has_device("virtio-iommu-pci")) {
2206                 qtest_add_func("acpi/q35/viot", test_acpi_q35_viot);
2207             }
2208 #ifdef CONFIG_POSIX
2209             qtest_add_func("acpi/q35/cxl", test_acpi_q35_cxl);
2210 #endif
2211             qtest_add_func("acpi/q35/slic", test_acpi_q35_slic);
2212         }
2213         if (qtest_has_machine("microvm")) {
2214             qtest_add_func("acpi/microvm", test_acpi_microvm_tcg);
2215             qtest_add_func("acpi/microvm/usb", test_acpi_microvm_usb_tcg);
2216             qtest_add_func("acpi/microvm/rtc", test_acpi_microvm_rtc_tcg);
2217             qtest_add_func("acpi/microvm/ioapic2",
2218                            test_acpi_microvm_ioapic2_tcg);
2219             qtest_add_func("acpi/microvm/oem-fields",
2220                            test_acpi_microvm_oem_fields);
2221             if (has_tcg) {
2222                 if (strcmp(arch, "x86_64") == 0) {
2223                     qtest_add_func("acpi/microvm/pcie",
2224                                    test_acpi_microvm_pcie_tcg);
2225 #ifdef CONFIG_POSIX
2226                     qtest_add_func("acpi/microvm/acpierst",
2227                                    test_acpi_microvm_acpi_erst);
2228 #endif
2229                 }
2230             }
2231         }
2232     } else if (strcmp(arch, "aarch64") == 0) {
2233         if (has_tcg && qtest_has_device("virtio-blk-pci")) {
2234             qtest_add_func("acpi/virt", test_acpi_virt_tcg);
2235             qtest_add_func("acpi/virt/acpihmatvirt",
2236                             test_acpi_virt_tcg_acpi_hmat);
2237             qtest_add_func("acpi/virt/topology", test_acpi_virt_tcg_topology);
2238             qtest_add_func("acpi/virt/numamem", test_acpi_virt_tcg_numamem);
2239             qtest_add_func("acpi/virt/memhp", test_acpi_virt_tcg_memhp);
2240             qtest_add_func("acpi/virt/pxb", test_acpi_virt_tcg_pxb);
2241             qtest_add_func("acpi/virt/oem-fields", test_acpi_virt_oem_fields);
2242             if (qtest_has_device("virtio-iommu-pci")) {
2243                 qtest_add_func("acpi/virt/viot", test_acpi_virt_viot);
2244             }
2245         }
2246     }
2247     ret = g_test_run();
2248     boot_sector_cleanup(disk);
2249     return ret;
2250 }
2251