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