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