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