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