xref: /openbmc/qemu/tests/qtest/bios-tables-test.c (revision ef422a14)
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 "qemu-common.h"
61 #include "hw/firmware/smbios.h"
62 #include "qemu/bitmap.h"
63 #include "acpi-utils.h"
64 #include "boot-sector.h"
65 #include "tpm-emu.h"
66 #include "hw/acpi/tpm.h"
67 #include "qemu/cutils.h"
68 
69 #define MACHINE_PC "pc"
70 #define MACHINE_Q35 "q35"
71 
72 #define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"
73 
74 #define OEM_ID             "TEST"
75 #define OEM_TABLE_ID       "OEM"
76 #define OEM_TEST_ARGS      "-machine x-oem-id="OEM_ID",x-oem-table-id="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     uint32_t smbios_ep_addr;
92     struct smbios_21_entry_point smbios_ep_table;
93     uint16_t smbios_cpu_max_speed;
94     uint16_t smbios_cpu_curr_speed;
95     uint8_t *required_struct_types;
96     int required_struct_types_len;
97     QTestState *qts;
98 } test_data;
99 
100 static char disk[] = "tests/acpi-test-disk-XXXXXX";
101 static const char *data_dir = "tests/data/acpi";
102 #ifdef CONFIG_IASL
103 static const char *iasl = CONFIG_IASL;
104 #else
105 static const char *iasl;
106 #endif
107 
108 static bool compare_signature(const AcpiSdtTable *sdt, const char *signature)
109 {
110    return !memcmp(sdt->aml, signature, 4);
111 }
112 
113 static void cleanup_table_descriptor(AcpiSdtTable *table)
114 {
115     g_free(table->aml);
116     if (table->aml_file &&
117         !table->tmp_files_retain &&
118         g_strstr_len(table->aml_file, -1, "aml-")) {
119         unlink(table->aml_file);
120     }
121     g_free(table->aml_file);
122     g_free(table->asl);
123     if (table->asl_file &&
124         !table->tmp_files_retain) {
125         unlink(table->asl_file);
126     }
127     g_free(table->asl_file);
128 }
129 
130 static void free_test_data(test_data *data)
131 {
132     int i;
133 
134     if (!data->tables) {
135         return;
136     }
137     for (i = 0; i < data->tables->len; ++i) {
138         cleanup_table_descriptor(&g_array_index(data->tables, AcpiSdtTable, i));
139     }
140 
141     g_array_free(data->tables, true);
142 }
143 
144 static void test_acpi_rsdp_table(test_data *data)
145 {
146     uint8_t *rsdp_table = data->rsdp_table;
147 
148     acpi_fetch_rsdp_table(data->qts, data->rsdp_addr, rsdp_table);
149 
150     switch (rsdp_table[15 /* Revision offset */]) {
151     case 0: /* ACPI 1.0 RSDP */
152         /* With rev 1, checksum is only for the first 20 bytes */
153         g_assert(!acpi_calc_checksum(rsdp_table,  20));
154         break;
155     case 2: /* ACPI 2.0+ RSDP */
156         /* With revision 2, we have 2 checksums */
157         g_assert(!acpi_calc_checksum(rsdp_table, 20));
158         g_assert(!acpi_calc_checksum(rsdp_table, 36));
159         break;
160     default:
161         g_assert_not_reached();
162     }
163 }
164 
165 static void test_acpi_rxsdt_table(test_data *data)
166 {
167     const char *sig = "RSDT";
168     AcpiSdtTable rsdt = {};
169     int entry_size = 4;
170     int addr_off = 16 /* RsdtAddress */;
171     uint8_t *ent;
172 
173     if (data->rsdp_table[15 /* Revision offset */] != 0) {
174         addr_off = 24 /* XsdtAddress */;
175         entry_size = 8;
176         sig = "XSDT";
177     }
178     /* read [RX]SDT table */
179     acpi_fetch_table(data->qts, &rsdt.aml, &rsdt.aml_len,
180                      &data->rsdp_table[addr_off], entry_size, sig, true);
181 
182     /* Load all tables and add to test list directly RSDT referenced tables */
183     ACPI_FOREACH_RSDT_ENTRY(rsdt.aml, rsdt.aml_len, ent, entry_size) {
184         AcpiSdtTable ssdt_table = {};
185 
186         acpi_fetch_table(data->qts, &ssdt_table.aml, &ssdt_table.aml_len, ent,
187                          entry_size, NULL, true);
188         /* Add table to ASL test tables list */
189         g_array_append_val(data->tables, ssdt_table);
190     }
191     cleanup_table_descriptor(&rsdt);
192 }
193 
194 static void test_acpi_fadt_table(test_data *data)
195 {
196     /* FADT table is 1st */
197     AcpiSdtTable table = g_array_index(data->tables, typeof(table), 0);
198     uint8_t *fadt_aml = table.aml;
199     uint32_t fadt_len = table.aml_len;
200     uint32_t val;
201     int dsdt_offset = 40 /* DSDT */;
202     int dsdt_entry_size = 4;
203 
204     g_assert(compare_signature(&table, "FACP"));
205 
206     /* Since DSDT/FACS isn't in RSDT, add them to ASL test list manually */
207     memcpy(&val, fadt_aml + 112 /* Flags */, 4);
208     val = le32_to_cpu(val);
209     if (!(val & 1UL << 20 /* HW_REDUCED_ACPI */)) {
210         acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
211                          fadt_aml + 36 /* FIRMWARE_CTRL */, 4, "FACS", false);
212         g_array_append_val(data->tables, table);
213     }
214 
215     memcpy(&val, fadt_aml + dsdt_offset, 4);
216     val = le32_to_cpu(val);
217     if (!val) {
218         dsdt_offset = 140 /* X_DSDT */;
219         dsdt_entry_size = 8;
220     }
221     acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
222                      fadt_aml + dsdt_offset, dsdt_entry_size, "DSDT", true);
223     g_array_append_val(data->tables, table);
224 
225     memset(fadt_aml + 36, 0, 4); /* sanitize FIRMWARE_CTRL ptr */
226     memset(fadt_aml + 40, 0, 4); /* sanitize DSDT ptr */
227     if (fadt_aml[8 /* FADT Major Version */] >= 3) {
228         memset(fadt_aml + 132, 0, 8); /* sanitize X_FIRMWARE_CTRL ptr */
229         memset(fadt_aml + 140, 0, 8); /* sanitize X_DSDT ptr */
230     }
231 
232     /* update checksum */
233     fadt_aml[9 /* Checksum */] = 0;
234     fadt_aml[9 /* Checksum */] -= acpi_calc_checksum(fadt_aml, fadt_len);
235 }
236 
237 static void dump_aml_files(test_data *data, bool rebuild)
238 {
239     AcpiSdtTable *sdt;
240     GError *error = NULL;
241     gchar *aml_file = NULL;
242     gint fd;
243     ssize_t ret;
244     int i;
245 
246     for (i = 0; i < data->tables->len; ++i) {
247         const char *ext = data->variant ? data->variant : "";
248         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
249         g_assert(sdt->aml);
250 
251         if (rebuild) {
252             aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
253                                        sdt->aml, ext);
254             fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
255                         S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
256             if (fd < 0) {
257                 perror(aml_file);
258             }
259             g_assert(fd >= 0);
260         } else {
261             fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
262             g_assert_no_error(error);
263         }
264 
265         ret = qemu_write_full(fd, sdt->aml, sdt->aml_len);
266         g_assert(ret == sdt->aml_len);
267 
268         close(fd);
269 
270         g_free(aml_file);
271     }
272 }
273 
274 static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
275 {
276     AcpiSdtTable *temp;
277     GError *error = NULL;
278     GString *command_line = g_string_new(iasl);
279     gint fd;
280     gchar *out, *out_err;
281     gboolean ret;
282     int i;
283 
284     fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
285     g_assert_no_error(error);
286     close(fd);
287 
288     /* build command line */
289     g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
290     if (compare_signature(sdt, "DSDT") ||
291         compare_signature(sdt, "SSDT")) {
292         for (i = 0; i < sdts->len; ++i) {
293             temp = &g_array_index(sdts, AcpiSdtTable, i);
294             if (compare_signature(temp, "DSDT") ||
295                 compare_signature(temp, "SSDT")) {
296                 g_string_append_printf(command_line, "-e %s ", temp->aml_file);
297             }
298         }
299     }
300     g_string_append_printf(command_line, "-d %s", sdt->aml_file);
301 
302     /* pass 'out' and 'out_err' in order to be redirected */
303     ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
304     g_assert_no_error(error);
305     if (ret) {
306         ret = g_file_get_contents(sdt->asl_file, &sdt->asl,
307                                   &sdt->asl_len, &error);
308         g_assert(ret);
309         g_assert_no_error(error);
310         ret = (sdt->asl_len > 0);
311     }
312 
313     g_free(out);
314     g_free(out_err);
315     g_string_free(command_line, true);
316 
317     return !ret;
318 }
319 
320 #define COMMENT_END "*/"
321 #define DEF_BLOCK "DefinitionBlock ("
322 #define BLOCK_NAME_END ","
323 
324 static GString *normalize_asl(gchar *asl_code)
325 {
326     GString *asl = g_string_new(asl_code);
327     gchar *comment, *block_name;
328 
329     /* strip comments (different generation days) */
330     comment = g_strstr_len(asl->str, asl->len, COMMENT_END);
331     if (comment) {
332         comment += strlen(COMMENT_END);
333         while (*comment == '\n') {
334             comment++;
335         }
336         asl = g_string_erase(asl, 0, comment - asl->str);
337     }
338 
339     /* strip def block name (it has file path in it) */
340     if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
341         block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
342         g_assert(block_name);
343         asl = g_string_erase(asl, 0,
344                              block_name + sizeof(BLOCK_NAME_END) - asl->str);
345     }
346 
347     return asl;
348 }
349 
350 static GArray *load_expected_aml(test_data *data)
351 {
352     int i;
353     AcpiSdtTable *sdt;
354     GError *error = NULL;
355     gboolean ret;
356     gsize aml_len;
357 
358     GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
359     if (getenv("V")) {
360         fputc('\n', stderr);
361     }
362     for (i = 0; i < data->tables->len; ++i) {
363         AcpiSdtTable exp_sdt;
364         gchar *aml_file = NULL;
365         const char *ext = data->variant ? data->variant : "";
366 
367         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
368 
369         memset(&exp_sdt, 0, sizeof(exp_sdt));
370 
371 try_again:
372         aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
373                                    sdt->aml, ext);
374         if (getenv("V")) {
375             fprintf(stderr, "Looking for expected file '%s'\n", aml_file);
376         }
377         if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) {
378             exp_sdt.aml_file = aml_file;
379         } else if (*ext != '\0') {
380             /* try fallback to generic (extension less) expected file */
381             ext = "";
382             g_free(aml_file);
383             goto try_again;
384         }
385         g_assert(exp_sdt.aml_file);
386         if (getenv("V")) {
387             fprintf(stderr, "Using expected file '%s'\n", aml_file);
388         }
389         ret = g_file_get_contents(aml_file, (gchar **)&exp_sdt.aml,
390                                   &aml_len, &error);
391         exp_sdt.aml_len = aml_len;
392         g_assert(ret);
393         g_assert_no_error(error);
394         g_assert(exp_sdt.aml);
395         if (!exp_sdt.aml_len) {
396             fprintf(stderr, "Warning! zero length expected file '%s'\n",
397                     aml_file);
398         }
399 
400         g_array_append_val(exp_tables, exp_sdt);
401     }
402 
403     return exp_tables;
404 }
405 
406 static bool test_acpi_find_diff_allowed(AcpiSdtTable *sdt)
407 {
408     const gchar *allowed_diff_file[] = {
409 #include "bios-tables-test-allowed-diff.h"
410         NULL
411     };
412     const gchar **f;
413 
414     for (f = allowed_diff_file; *f; ++f) {
415         if (!g_strcmp0(sdt->aml_file, *f)) {
416             return true;
417         }
418     }
419     return false;
420 }
421 
422 /* test the list of tables in @data->tables against reference tables */
423 static void test_acpi_asl(test_data *data)
424 {
425     int i;
426     AcpiSdtTable *sdt, *exp_sdt;
427     test_data exp_data;
428     gboolean exp_err, err, all_tables_match = true;
429 
430     memset(&exp_data, 0, sizeof(exp_data));
431     exp_data.tables = load_expected_aml(data);
432     dump_aml_files(data, false);
433     for (i = 0; i < data->tables->len; ++i) {
434         GString *asl, *exp_asl;
435 
436         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
437         exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
438 
439         if (sdt->aml_len == exp_sdt->aml_len &&
440             !memcmp(sdt->aml, exp_sdt->aml, sdt->aml_len)) {
441             /* Identical table binaries: no need to disassemble. */
442             continue;
443         }
444 
445         fprintf(stderr,
446                 "acpi-test: Warning! %.4s binary file mismatch. "
447                 "Actual [aml:%s], Expected [aml:%s].\n"
448                 "See source file tests/qtest/bios-tables-test.c "
449                 "for instructions on how to update expected files.\n",
450                 exp_sdt->aml, sdt->aml_file, exp_sdt->aml_file);
451 
452         all_tables_match = all_tables_match &&
453             test_acpi_find_diff_allowed(exp_sdt);
454 
455         /*
456          *  don't try to decompile if IASL isn't present, in this case user
457          * will just 'get binary file mismatch' warnings and test failure
458          */
459         if (!iasl) {
460             continue;
461         }
462 
463         err = load_asl(data->tables, sdt);
464         asl = normalize_asl(sdt->asl);
465 
466         exp_err = load_asl(exp_data.tables, exp_sdt);
467         exp_asl = normalize_asl(exp_sdt->asl);
468 
469         /* TODO: check for warnings */
470         g_assert(!err || exp_err);
471 
472         if (g_strcmp0(asl->str, exp_asl->str)) {
473             sdt->tmp_files_retain = true;
474             if (exp_err) {
475                 fprintf(stderr,
476                         "Warning! iasl couldn't parse the expected aml\n");
477             } else {
478                 exp_sdt->tmp_files_retain = true;
479                 fprintf(stderr,
480                         "acpi-test: Warning! %.4s mismatch. "
481                         "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
482                         exp_sdt->aml, sdt->asl_file, sdt->aml_file,
483                         exp_sdt->asl_file, exp_sdt->aml_file);
484                 fflush(stderr);
485                 if (getenv("V")) {
486                     const char *diff_env = getenv("DIFF");
487                     const char *diff_cmd = diff_env ? diff_env : "diff -U 16";
488                     char *diff = g_strdup_printf("%s %s %s", diff_cmd,
489                                                  exp_sdt->asl_file, sdt->asl_file);
490                     int out = dup(STDOUT_FILENO);
491                     int ret G_GNUC_UNUSED;
492                     int dupret;
493 
494                     g_assert(out >= 0);
495                     dupret = dup2(STDERR_FILENO, STDOUT_FILENO);
496                     g_assert(dupret >= 0);
497                     ret = system(diff) ;
498                     dupret = dup2(out, STDOUT_FILENO);
499                     g_assert(dupret >= 0);
500                     close(out);
501                     g_free(diff);
502                 }
503             }
504         }
505         g_string_free(asl, true);
506         g_string_free(exp_asl, true);
507     }
508     if (!iasl && !all_tables_match) {
509         fprintf(stderr, "to see ASL diff between mismatched files install IASL,"
510                 " rebuild QEMU from scratch and re-run tests with V=1"
511                 " environment variable set");
512     }
513     g_assert(all_tables_match);
514 
515     free_test_data(&exp_data);
516 }
517 
518 static bool smbios_ep_table_ok(test_data *data)
519 {
520     struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
521     uint32_t addr = data->smbios_ep_addr;
522 
523     qtest_memread(data->qts, addr, ep_table, sizeof(*ep_table));
524     if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
525         return false;
526     }
527     if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
528         return false;
529     }
530     if (ep_table->structure_table_length == 0) {
531         return false;
532     }
533     if (ep_table->number_of_structures == 0) {
534         return false;
535     }
536     if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
537         acpi_calc_checksum((uint8_t *)ep_table + 0x10,
538                            sizeof *ep_table - 0x10)) {
539         return false;
540     }
541     return true;
542 }
543 
544 static void test_smbios_entry_point(test_data *data)
545 {
546     uint32_t off;
547 
548     /* find smbios entry point structure */
549     for (off = 0xf0000; off < 0x100000; off += 0x10) {
550         uint8_t sig[] = "_SM_";
551         int i;
552 
553         for (i = 0; i < sizeof sig - 1; ++i) {
554             sig[i] = qtest_readb(data->qts, off + i);
555         }
556 
557         if (!memcmp(sig, "_SM_", sizeof sig)) {
558             /* signature match, but is this a valid entry point? */
559             data->smbios_ep_addr = off;
560             if (smbios_ep_table_ok(data)) {
561                 break;
562             }
563         }
564     }
565 
566     g_assert_cmphex(off, <, 0x100000);
567 }
568 
569 static inline bool smbios_single_instance(uint8_t type)
570 {
571     switch (type) {
572     case 0:
573     case 1:
574     case 2:
575     case 3:
576     case 16:
577     case 32:
578     case 127:
579         return true;
580     default:
581         return false;
582     }
583 }
584 
585 static bool smbios_cpu_test(test_data *data, uint32_t addr)
586 {
587     uint16_t expect_speed[2];
588     uint16_t real;
589     int offset[2];
590     int i;
591 
592     /* Check CPU speed for backward compatibility */
593     offset[0] = offsetof(struct smbios_type_4, max_speed);
594     offset[1] = offsetof(struct smbios_type_4, current_speed);
595     expect_speed[0] = data->smbios_cpu_max_speed ? : 2000;
596     expect_speed[1] = data->smbios_cpu_curr_speed ? : 2000;
597 
598     for (i = 0; i < 2; i++) {
599         real = qtest_readw(data->qts, addr + offset[i]);
600         if (real != expect_speed[i]) {
601             fprintf(stderr, "Unexpected SMBIOS CPU speed: real %u expect %u\n",
602                     real, expect_speed[i]);
603             return false;
604         }
605     }
606 
607     return true;
608 }
609 
610 static void test_smbios_structs(test_data *data)
611 {
612     DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };
613     struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
614     uint32_t addr = le32_to_cpu(ep_table->structure_table_address);
615     int i, len, max_len = 0;
616     uint8_t type, prv, crt;
617 
618     /* walk the smbios tables */
619     for (i = 0; i < le16_to_cpu(ep_table->number_of_structures); i++) {
620 
621         /* grab type and formatted area length from struct header */
622         type = qtest_readb(data->qts, addr);
623         g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
624         len = qtest_readb(data->qts, addr + 1);
625 
626         /* single-instance structs must not have been encountered before */
627         if (smbios_single_instance(type)) {
628             g_assert(!test_bit(type, struct_bitmap));
629         }
630         set_bit(type, struct_bitmap);
631 
632         if (type == 4) {
633             g_assert(smbios_cpu_test(data, addr));
634         }
635 
636         /* seek to end of unformatted string area of this struct ("\0\0") */
637         prv = crt = 1;
638         while (prv || crt) {
639             prv = crt;
640             crt = qtest_readb(data->qts, addr + len);
641             len++;
642         }
643 
644         /* keep track of max. struct size */
645         if (max_len < len) {
646             max_len = len;
647             g_assert_cmpuint(max_len, <=, ep_table->max_structure_size);
648         }
649 
650         /* start of next structure */
651         addr += len;
652     }
653 
654     /* total table length and max struct size must match entry point values */
655     g_assert_cmpuint(le16_to_cpu(ep_table->structure_table_length), ==,
656                      addr - le32_to_cpu(ep_table->structure_table_address));
657     g_assert_cmpuint(le16_to_cpu(ep_table->max_structure_size), ==, max_len);
658 
659     /* required struct types must all be present */
660     for (i = 0; i < data->required_struct_types_len; i++) {
661         g_assert(test_bit(data->required_struct_types[i], struct_bitmap));
662     }
663 }
664 
665 static void test_acpi_load_tables(test_data *data, bool use_uefi)
666 {
667     if (use_uefi) {
668         g_assert(data->scan_len);
669         data->rsdp_addr = acpi_find_rsdp_address_uefi(data->qts,
670             data->ram_start, data->scan_len);
671     } else {
672         boot_sector_test(data->qts);
673         data->rsdp_addr = acpi_find_rsdp_address(data->qts);
674         g_assert_cmphex(data->rsdp_addr, <, 0x100000);
675     }
676 
677     data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
678     test_acpi_rsdp_table(data);
679     test_acpi_rxsdt_table(data);
680     test_acpi_fadt_table(data);
681 }
682 
683 static char *test_acpi_create_args(test_data *data, const char *params,
684                                    bool use_uefi)
685 {
686     char *args;
687 
688     if (use_uefi) {
689         /*
690          * TODO: convert '-drive if=pflash' to new syntax (see e33763be7cd3)
691          * when arm/virt boad starts to support it.
692          */
693         if (data->cd) {
694             args = g_strdup_printf("-machine %s %s -accel tcg "
695                 "-nodefaults -nographic "
696                 "-drive if=pflash,format=raw,file=%s,readonly=on "
697                 "-drive if=pflash,format=raw,file=%s,snapshot=on -cdrom %s %s",
698                 data->machine, data->tcg_only ? "" : "-accel kvm",
699                 data->uefi_fl1, data->uefi_fl2, data->cd, params ? params : "");
700         } else {
701             args = g_strdup_printf("-machine %s %s -accel tcg "
702                 "-nodefaults -nographic "
703                 "-drive if=pflash,format=raw,file=%s,readonly=on "
704                 "-drive if=pflash,format=raw,file=%s,snapshot=on %s",
705                 data->machine, data->tcg_only ? "" : "-accel kvm",
706                 data->uefi_fl1, data->uefi_fl2, params ? params : "");
707         }
708     } else {
709         args = g_strdup_printf("-machine %s %s -accel tcg "
710             "-net none -display none %s "
711             "-drive id=hd0,if=none,file=%s,format=raw "
712             "-device %s,drive=hd0 ",
713              data->machine, data->tcg_only ? "" : "-accel kvm",
714              params ? params : "", disk,
715              data->blkdev ?: "ide-hd");
716     }
717     return args;
718 }
719 
720 static void test_acpi_one(const char *params, test_data *data)
721 {
722     char *args;
723     bool use_uefi = data->uefi_fl1 && data->uefi_fl2;
724 
725 #ifndef CONFIG_TCG
726     if (data->tcg_only) {
727         g_test_skip("TCG disabled, skipping ACPI tcg_only test");
728         return;
729     }
730 #endif /* CONFIG_TCG */
731 
732     args = test_acpi_create_args(data, params, use_uefi);
733     data->qts = qtest_init(args);
734     test_acpi_load_tables(data, use_uefi);
735 
736     if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
737         dump_aml_files(data, true);
738     } else {
739         test_acpi_asl(data);
740     }
741 
742     /*
743      * TODO: make SMBIOS tests work with UEFI firmware,
744      * Bug on uefi-test-tools to provide entry point:
745      * https://bugs.launchpad.net/qemu/+bug/1821884
746      */
747     if (!use_uefi) {
748         test_smbios_entry_point(data);
749         test_smbios_structs(data);
750     }
751 
752     qtest_quit(data->qts);
753     g_free(args);
754 }
755 
756 static uint8_t base_required_struct_types[] = {
757     0, 1, 3, 4, 16, 17, 19, 32, 127
758 };
759 
760 static void test_acpi_piix4_tcg(void)
761 {
762     test_data data;
763 
764     /* Supplying -machine accel argument overrides the default (qtest).
765      * This is to make guest actually run.
766      */
767     memset(&data, 0, sizeof(data));
768     data.machine = MACHINE_PC;
769     data.required_struct_types = base_required_struct_types;
770     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
771     test_acpi_one(NULL, &data);
772     free_test_data(&data);
773 }
774 
775 static void test_acpi_piix4_tcg_bridge(void)
776 {
777     test_data data;
778 
779     memset(&data, 0, sizeof(data));
780     data.machine = MACHINE_PC;
781     data.variant = ".bridge";
782     data.required_struct_types = base_required_struct_types;
783     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
784     test_acpi_one("-device pci-bridge,chassis_nr=1", &data);
785     free_test_data(&data);
786 }
787 
788 static void test_acpi_piix4_no_root_hotplug(void)
789 {
790     test_data data;
791 
792     memset(&data, 0, sizeof(data));
793     data.machine = MACHINE_PC;
794     data.variant = ".roothp";
795     data.required_struct_types = base_required_struct_types;
796     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
797     test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
798                   "-device pci-bridge,chassis_nr=1", &data);
799     free_test_data(&data);
800 }
801 
802 static void test_acpi_piix4_no_bridge_hotplug(void)
803 {
804     test_data data;
805 
806     memset(&data, 0, sizeof(data));
807     data.machine = MACHINE_PC;
808     data.variant = ".hpbridge";
809     data.required_struct_types = base_required_struct_types;
810     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
811     test_acpi_one("-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
812                   "-device pci-bridge,chassis_nr=1", &data);
813     free_test_data(&data);
814 }
815 
816 static void test_acpi_piix4_no_acpi_pci_hotplug(void)
817 {
818     test_data data;
819 
820     memset(&data, 0, sizeof(data));
821     data.machine = MACHINE_PC;
822     data.variant = ".hpbrroot";
823     data.required_struct_types = base_required_struct_types;
824     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
825     test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
826                   "-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
827                   "-device pci-bridge,chassis_nr=1", &data);
828     free_test_data(&data);
829 }
830 
831 static void test_acpi_q35_tcg(void)
832 {
833     test_data data;
834 
835     memset(&data, 0, sizeof(data));
836     data.machine = MACHINE_Q35;
837     data.required_struct_types = base_required_struct_types;
838     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
839     test_acpi_one(NULL, &data);
840     free_test_data(&data);
841 
842     data.smbios_cpu_max_speed = 3000;
843     data.smbios_cpu_curr_speed = 2600;
844     test_acpi_one("-smbios type=4,max-speed=3000,current-speed=2600", &data);
845     free_test_data(&data);
846 }
847 
848 static void test_acpi_q35_tcg_bridge(void)
849 {
850     test_data data;
851 
852     memset(&data, 0, sizeof(data));
853     data.machine = MACHINE_Q35;
854     data.variant = ".bridge";
855     data.required_struct_types = base_required_struct_types;
856     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
857     test_acpi_one("-device pci-bridge,chassis_nr=1",
858                   &data);
859     free_test_data(&data);
860 }
861 
862 static void test_acpi_q35_tcg_mmio64(void)
863 {
864     test_data data = {
865         .machine = MACHINE_Q35,
866         .variant = ".mmio64",
867         .required_struct_types = base_required_struct_types,
868         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types)
869     };
870 
871     test_acpi_one("-m 128M,slots=1,maxmem=2G "
872                   "-object memory-backend-ram,id=ram0,size=128M "
873                   "-numa node,memdev=ram0 "
874                   "-device pci-testdev,membar=2G",
875                   &data);
876     free_test_data(&data);
877 }
878 
879 static void test_acpi_piix4_tcg_cphp(void)
880 {
881     test_data data;
882 
883     memset(&data, 0, sizeof(data));
884     data.machine = MACHINE_PC;
885     data.variant = ".cphp";
886     test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
887                   " -object memory-backend-ram,id=ram0,size=64M"
888                   " -object memory-backend-ram,id=ram1,size=64M"
889                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
890                   " -numa dist,src=0,dst=1,val=21",
891                   &data);
892     free_test_data(&data);
893 }
894 
895 static void test_acpi_q35_tcg_cphp(void)
896 {
897     test_data data;
898 
899     memset(&data, 0, sizeof(data));
900     data.machine = MACHINE_Q35;
901     data.variant = ".cphp";
902     test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
903                   " -object memory-backend-ram,id=ram0,size=64M"
904                   " -object memory-backend-ram,id=ram1,size=64M"
905                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
906                   " -numa dist,src=0,dst=1,val=21",
907                   &data);
908     free_test_data(&data);
909 }
910 
911 static uint8_t ipmi_required_struct_types[] = {
912     0, 1, 3, 4, 16, 17, 19, 32, 38, 127
913 };
914 
915 static void test_acpi_q35_tcg_ipmi(void)
916 {
917     test_data data;
918 
919     memset(&data, 0, sizeof(data));
920     data.machine = MACHINE_Q35;
921     data.variant = ".ipmibt";
922     data.required_struct_types = ipmi_required_struct_types;
923     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
924     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
925                   " -device isa-ipmi-bt,bmc=bmc0",
926                   &data);
927     free_test_data(&data);
928 }
929 
930 static void test_acpi_piix4_tcg_ipmi(void)
931 {
932     test_data data;
933 
934     /* Supplying -machine accel argument overrides the default (qtest).
935      * This is to make guest actually run.
936      */
937     memset(&data, 0, sizeof(data));
938     data.machine = MACHINE_PC;
939     data.variant = ".ipmikcs";
940     data.required_struct_types = ipmi_required_struct_types;
941     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
942     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
943                   " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
944                   &data);
945     free_test_data(&data);
946 }
947 
948 static void test_acpi_q35_tcg_memhp(void)
949 {
950     test_data data;
951 
952     memset(&data, 0, sizeof(data));
953     data.machine = MACHINE_Q35;
954     data.variant = ".memhp";
955     test_acpi_one(" -m 128,slots=3,maxmem=1G"
956                   " -object memory-backend-ram,id=ram0,size=64M"
957                   " -object memory-backend-ram,id=ram1,size=64M"
958                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
959                   " -numa dist,src=0,dst=1,val=21",
960                   &data);
961     free_test_data(&data);
962 }
963 
964 static void test_acpi_piix4_tcg_memhp(void)
965 {
966     test_data data;
967 
968     memset(&data, 0, sizeof(data));
969     data.machine = MACHINE_PC;
970     data.variant = ".memhp";
971     test_acpi_one(" -m 128,slots=3,maxmem=1G"
972                   " -object memory-backend-ram,id=ram0,size=64M"
973                   " -object memory-backend-ram,id=ram1,size=64M"
974                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
975                   " -numa dist,src=0,dst=1,val=21",
976                   &data);
977     free_test_data(&data);
978 }
979 
980 static void test_acpi_piix4_tcg_nosmm(void)
981 {
982     test_data data;
983 
984     memset(&data, 0, sizeof(data));
985     data.machine = MACHINE_PC;
986     data.variant = ".nosmm";
987     test_acpi_one("-machine smm=off", &data);
988     free_test_data(&data);
989 }
990 
991 static void test_acpi_piix4_tcg_smm_compat(void)
992 {
993     test_data data;
994 
995     memset(&data, 0, sizeof(data));
996     data.machine = MACHINE_PC;
997     data.variant = ".smm-compat";
998     test_acpi_one("-global PIIX4_PM.smm-compat=on", &data);
999     free_test_data(&data);
1000 }
1001 
1002 static void test_acpi_piix4_tcg_smm_compat_nosmm(void)
1003 {
1004     test_data data;
1005 
1006     memset(&data, 0, sizeof(data));
1007     data.machine = MACHINE_PC;
1008     data.variant = ".smm-compat-nosmm";
1009     test_acpi_one("-global PIIX4_PM.smm-compat=on -machine smm=off", &data);
1010     free_test_data(&data);
1011 }
1012 
1013 static void test_acpi_piix4_tcg_nohpet(void)
1014 {
1015     test_data data;
1016 
1017     memset(&data, 0, sizeof(data));
1018     data.machine = MACHINE_PC;
1019     data.variant = ".nohpet";
1020     test_acpi_one("-no-hpet", &data);
1021     free_test_data(&data);
1022 }
1023 
1024 static void test_acpi_q35_tcg_numamem(void)
1025 {
1026     test_data data;
1027 
1028     memset(&data, 0, sizeof(data));
1029     data.machine = MACHINE_Q35;
1030     data.variant = ".numamem";
1031     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1032                   " -numa node -numa node,memdev=ram0", &data);
1033     free_test_data(&data);
1034 }
1035 
1036 static void test_acpi_q35_kvm_xapic(void)
1037 {
1038     test_data data;
1039 
1040     memset(&data, 0, sizeof(data));
1041     data.machine = MACHINE_Q35;
1042     data.variant = ".xapic";
1043     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1044                   " -numa node -numa node,memdev=ram0"
1045                   " -machine kernel-irqchip=on -smp 1,maxcpus=288", &data);
1046     free_test_data(&data);
1047 }
1048 
1049 static void test_acpi_q35_tcg_nosmm(void)
1050 {
1051     test_data data;
1052 
1053     memset(&data, 0, sizeof(data));
1054     data.machine = MACHINE_Q35;
1055     data.variant = ".nosmm";
1056     test_acpi_one("-machine smm=off", &data);
1057     free_test_data(&data);
1058 }
1059 
1060 static void test_acpi_q35_tcg_smm_compat(void)
1061 {
1062     test_data data;
1063 
1064     memset(&data, 0, sizeof(data));
1065     data.machine = MACHINE_Q35;
1066     data.variant = ".smm-compat";
1067     test_acpi_one("-global ICH9-LPC.smm-compat=on", &data);
1068     free_test_data(&data);
1069 }
1070 
1071 static void test_acpi_q35_tcg_smm_compat_nosmm(void)
1072 {
1073     test_data data;
1074 
1075     memset(&data, 0, sizeof(data));
1076     data.machine = MACHINE_Q35;
1077     data.variant = ".smm-compat-nosmm";
1078     test_acpi_one("-global ICH9-LPC.smm-compat=on -machine smm=off", &data);
1079     free_test_data(&data);
1080 }
1081 
1082 static void test_acpi_q35_tcg_nohpet(void)
1083 {
1084     test_data data;
1085 
1086     memset(&data, 0, sizeof(data));
1087     data.machine = MACHINE_Q35;
1088     data.variant = ".nohpet";
1089     test_acpi_one("-no-hpet", &data);
1090     free_test_data(&data);
1091 }
1092 
1093 static void test_acpi_q35_kvm_dmar(void)
1094 {
1095     test_data data;
1096 
1097     memset(&data, 0, sizeof(data));
1098     data.machine = MACHINE_Q35;
1099     data.variant = ".dmar";
1100     test_acpi_one("-machine kernel-irqchip=split -accel kvm"
1101                   " -device intel-iommu,intremap=on,device-iotlb=on", &data);
1102     free_test_data(&data);
1103 }
1104 
1105 static void test_acpi_q35_tcg_ivrs(void)
1106 {
1107     test_data data;
1108 
1109     memset(&data, 0, sizeof(data));
1110     data.machine = MACHINE_Q35;
1111     data.variant = ".ivrs";
1112     data.tcg_only = true,
1113     test_acpi_one(" -device amd-iommu", &data);
1114     free_test_data(&data);
1115 }
1116 
1117 static void test_acpi_piix4_tcg_numamem(void)
1118 {
1119     test_data data;
1120 
1121     memset(&data, 0, sizeof(data));
1122     data.machine = MACHINE_PC;
1123     data.variant = ".numamem";
1124     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
1125                   " -numa node -numa node,memdev=ram0", &data);
1126     free_test_data(&data);
1127 }
1128 
1129 uint64_t tpm_tis_base_addr;
1130 
1131 static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
1132                               uint64_t base, enum TPMVersion tpm_version)
1133 {
1134     gchar *tmp_dir_name = g_strdup_printf("qemu-test_acpi_%s_tcg_%s.XXXXXX",
1135                                           machine, tpm_if);
1136     char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
1137     TPMTestState test;
1138     test_data data;
1139     GThread *thread;
1140     const char *suffix = tpm_version == TPM_VERSION_2_0 ? "tpm2" : "tpm12";
1141     char *args, *variant = g_strdup_printf(".%s.%s", tpm_if, suffix);
1142 
1143     tpm_tis_base_addr = base;
1144 
1145     module_call_init(MODULE_INIT_QOM);
1146 
1147     test.addr = g_new0(SocketAddress, 1);
1148     test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
1149     test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
1150     g_mutex_init(&test.data_mutex);
1151     g_cond_init(&test.data_cond);
1152     test.data_cond_signal = false;
1153     test.tpm_version = tpm_version;
1154 
1155     thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
1156     tpm_emu_test_wait_cond(&test);
1157 
1158     memset(&data, 0, sizeof(data));
1159     data.machine = machine;
1160     data.variant = variant;
1161 
1162     args = g_strdup_printf(
1163         " -chardev socket,id=chr,path=%s"
1164         " -tpmdev emulator,id=dev,chardev=chr"
1165         " -device tpm-%s,tpmdev=dev",
1166         test.addr->u.q_unix.path, tpm_if);
1167 
1168     test_acpi_one(args, &data);
1169 
1170     g_thread_join(thread);
1171     g_unlink(test.addr->u.q_unix.path);
1172     qapi_free_SocketAddress(test.addr);
1173     g_rmdir(tmp_path);
1174     g_free(variant);
1175     g_free(tmp_path);
1176     g_free(tmp_dir_name);
1177     g_free(args);
1178     free_test_data(&data);
1179 }
1180 
1181 static void test_acpi_q35_tcg_tpm2_tis(void)
1182 {
1183     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_2_0);
1184 }
1185 
1186 static void test_acpi_q35_tcg_tpm12_tis(void)
1187 {
1188     test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_1_2);
1189 }
1190 
1191 static void test_acpi_tcg_dimm_pxm(const char *machine)
1192 {
1193     test_data data;
1194 
1195     memset(&data, 0, sizeof(data));
1196     data.machine = machine;
1197     data.variant = ".dimmpxm";
1198     test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
1199                   " -smp 4,sockets=4"
1200                   " -m 128M,slots=3,maxmem=1G"
1201                   " -object memory-backend-ram,id=ram0,size=32M"
1202                   " -object memory-backend-ram,id=ram1,size=32M"
1203                   " -object memory-backend-ram,id=ram2,size=32M"
1204                   " -object memory-backend-ram,id=ram3,size=32M"
1205                   " -numa node,memdev=ram0,nodeid=0"
1206                   " -numa node,memdev=ram1,nodeid=1"
1207                   " -numa node,memdev=ram2,nodeid=2"
1208                   " -numa node,memdev=ram3,nodeid=3"
1209                   " -numa cpu,node-id=0,socket-id=0"
1210                   " -numa cpu,node-id=1,socket-id=1"
1211                   " -numa cpu,node-id=2,socket-id=2"
1212                   " -numa cpu,node-id=3,socket-id=3"
1213                   " -object memory-backend-ram,id=ram4,size=128M"
1214                   " -object memory-backend-ram,id=nvm0,size=128M"
1215                   " -device pc-dimm,id=dimm0,memdev=ram4,node=1"
1216                   " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
1217                   &data);
1218     free_test_data(&data);
1219 }
1220 
1221 static void test_acpi_q35_tcg_dimm_pxm(void)
1222 {
1223     test_acpi_tcg_dimm_pxm(MACHINE_Q35);
1224 }
1225 
1226 static void test_acpi_piix4_tcg_dimm_pxm(void)
1227 {
1228     test_acpi_tcg_dimm_pxm(MACHINE_PC);
1229 }
1230 
1231 static void test_acpi_virt_tcg_memhp(void)
1232 {
1233     test_data data = {
1234         .machine = "virt",
1235         .tcg_only = true,
1236         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1237         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1238         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1239         .ram_start = 0x40000000ULL,
1240         .scan_len = 256ULL * 1024 * 1024,
1241     };
1242 
1243     data.variant = ".memhp";
1244     test_acpi_one(" -machine nvdimm=on"
1245                   " -cpu cortex-a57"
1246                   " -m 256M,slots=3,maxmem=1G"
1247                   " -object memory-backend-ram,id=ram0,size=128M"
1248                   " -object memory-backend-ram,id=ram1,size=128M"
1249                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
1250                   " -numa dist,src=0,dst=1,val=21"
1251                   " -object memory-backend-ram,id=ram2,size=128M"
1252                   " -object memory-backend-ram,id=nvm0,size=128M"
1253                   " -device pc-dimm,id=dimm0,memdev=ram2,node=0"
1254                   " -device nvdimm,id=dimm1,memdev=nvm0,node=1",
1255                   &data);
1256 
1257     free_test_data(&data);
1258 
1259 }
1260 
1261 static void test_acpi_microvm_prepare(test_data *data)
1262 {
1263     memset(data, 0, sizeof(*data));
1264     data->machine = "microvm";
1265     data->required_struct_types = NULL; /* no smbios */
1266     data->required_struct_types_len = 0;
1267     data->blkdev = "virtio-blk-device";
1268 }
1269 
1270 static void test_acpi_microvm_tcg(void)
1271 {
1272     test_data data;
1273 
1274     test_acpi_microvm_prepare(&data);
1275     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off",
1276                   &data);
1277     free_test_data(&data);
1278 }
1279 
1280 static void test_acpi_microvm_usb_tcg(void)
1281 {
1282     test_data data;
1283 
1284     test_acpi_microvm_prepare(&data);
1285     data.variant = ".usb";
1286     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,usb=on,rtc=off",
1287                   &data);
1288     free_test_data(&data);
1289 }
1290 
1291 static void test_acpi_microvm_rtc_tcg(void)
1292 {
1293     test_data data;
1294 
1295     test_acpi_microvm_prepare(&data);
1296     data.variant = ".rtc";
1297     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=on",
1298                   &data);
1299     free_test_data(&data);
1300 }
1301 
1302 static void test_acpi_microvm_pcie_tcg(void)
1303 {
1304     test_data data;
1305 
1306     test_acpi_microvm_prepare(&data);
1307     data.variant = ".pcie";
1308     data.tcg_only = true; /* need constant host-phys-bits */
1309     test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off,pcie=on",
1310                   &data);
1311     free_test_data(&data);
1312 }
1313 
1314 static void test_acpi_microvm_ioapic2_tcg(void)
1315 {
1316     test_data data;
1317 
1318     test_acpi_microvm_prepare(&data);
1319     data.variant = ".ioapic2";
1320     test_acpi_one(" -machine microvm,acpi=on,ioapic2=on,rtc=off",
1321                   &data);
1322     free_test_data(&data);
1323 }
1324 
1325 static void test_acpi_virt_tcg_numamem(void)
1326 {
1327     test_data data = {
1328         .machine = "virt",
1329         .tcg_only = true,
1330         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1331         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1332         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1333         .ram_start = 0x40000000ULL,
1334         .scan_len = 128ULL * 1024 * 1024,
1335     };
1336 
1337     data.variant = ".numamem";
1338     test_acpi_one(" -cpu cortex-a57"
1339                   " -object memory-backend-ram,id=ram0,size=128M"
1340                   " -numa node,memdev=ram0",
1341                   &data);
1342 
1343     free_test_data(&data);
1344 
1345 }
1346 
1347 static void test_acpi_virt_tcg_pxb(void)
1348 {
1349     test_data data = {
1350         .machine = "virt",
1351         .tcg_only = true,
1352         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1353         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1354         .ram_start = 0x40000000ULL,
1355         .scan_len = 128ULL * 1024 * 1024,
1356     };
1357     /*
1358      * While using -cdrom, the cdrom would auto plugged into pxb-pcie,
1359      * the reason is the bus of pxb-pcie is also root bus, it would lead
1360      * to the error only PCI/PCIE bridge could plug onto pxb.
1361      * Therefore,thr cdrom is defined and plugged onto the scsi controller
1362      * to solve the conflicts.
1363      */
1364     data.variant = ".pxb";
1365     test_acpi_one(" -device pcie-root-port,chassis=1,id=pci.1"
1366                   " -device virtio-scsi-pci,id=scsi0,bus=pci.1"
1367                   " -drive file="
1368                   "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2,"
1369                   "if=none,media=cdrom,id=drive-scsi0-0-0-1,readonly=on"
1370                   " -device scsi-cd,bus=scsi0.0,scsi-id=0,"
1371                   "drive=drive-scsi0-0-0-1,id=scsi0-0-0-1,bootindex=1"
1372                   " -cpu cortex-a57"
1373                   " -device pxb-pcie,bus_nr=128",
1374                   &data);
1375 
1376     free_test_data(&data);
1377 }
1378 
1379 static void test_acpi_tcg_acpi_hmat(const char *machine)
1380 {
1381     test_data data;
1382 
1383     memset(&data, 0, sizeof(data));
1384     data.machine = machine;
1385     data.variant = ".acpihmat";
1386     test_acpi_one(" -machine hmat=on"
1387                   " -smp 2,sockets=2"
1388                   " -m 128M,slots=2,maxmem=1G"
1389                   " -object memory-backend-ram,size=64M,id=m0"
1390                   " -object memory-backend-ram,size=64M,id=m1"
1391                   " -numa node,nodeid=0,memdev=m0"
1392                   " -numa node,nodeid=1,memdev=m1,initiator=0"
1393                   " -numa cpu,node-id=0,socket-id=0"
1394                   " -numa cpu,node-id=0,socket-id=1"
1395                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1396                   "data-type=access-latency,latency=1"
1397                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1398                   "data-type=access-bandwidth,bandwidth=65534M"
1399                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1400                   "data-type=access-latency,latency=65534"
1401                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1402                   "data-type=access-bandwidth,bandwidth=32767M"
1403                   " -numa hmat-cache,node-id=0,size=10K,level=1,"
1404                   "associativity=direct,policy=write-back,line=8"
1405                   " -numa hmat-cache,node-id=1,size=10K,level=1,"
1406                   "associativity=direct,policy=write-back,line=8",
1407                   &data);
1408     free_test_data(&data);
1409 }
1410 
1411 static void test_acpi_q35_tcg_acpi_hmat(void)
1412 {
1413     test_acpi_tcg_acpi_hmat(MACHINE_Q35);
1414 }
1415 
1416 static void test_acpi_piix4_tcg_acpi_hmat(void)
1417 {
1418     test_acpi_tcg_acpi_hmat(MACHINE_PC);
1419 }
1420 
1421 static void test_acpi_virt_tcg(void)
1422 {
1423     test_data data = {
1424         .machine = "virt",
1425         .tcg_only = true,
1426         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1427         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1428         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1429         .ram_start = 0x40000000ULL,
1430         .scan_len = 128ULL * 1024 * 1024,
1431     };
1432 
1433     test_acpi_one("-cpu cortex-a57", &data);
1434     free_test_data(&data);
1435 
1436     data.smbios_cpu_max_speed = 2900;
1437     data.smbios_cpu_curr_speed = 2700;
1438     test_acpi_one("-cpu cortex-a57 "
1439                   "-smbios type=4,max-speed=2900,current-speed=2700", &data);
1440     free_test_data(&data);
1441 }
1442 
1443 static void test_oem_fields(test_data *data)
1444 {
1445     int i;
1446     char oem_id[6];
1447     char oem_table_id[8];
1448 
1449     strpadcpy(oem_id, sizeof oem_id, OEM_ID, ' ');
1450     strpadcpy(oem_table_id, sizeof oem_table_id, OEM_TABLE_ID, ' ');
1451     for (i = 0; i < data->tables->len; ++i) {
1452         AcpiSdtTable *sdt;
1453 
1454         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
1455         /* FACS doesn't have OEMID and OEMTABLEID fields */
1456         if (compare_signature(sdt, "FACS")) {
1457             continue;
1458         }
1459 
1460         g_assert(memcmp(sdt->aml + 10, oem_id, 6) == 0);
1461         g_assert(memcmp(sdt->aml + 16, oem_table_id, 8) == 0);
1462     }
1463 }
1464 
1465 static void test_acpi_oem_fields_pc(void)
1466 {
1467     test_data data;
1468     char *args;
1469 
1470     memset(&data, 0, sizeof(data));
1471     data.machine = MACHINE_PC;
1472     data.required_struct_types = base_required_struct_types;
1473     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1474 
1475     args = test_acpi_create_args(&data,
1476                                  OEM_TEST_ARGS, false);
1477     data.qts = qtest_init(args);
1478     test_acpi_load_tables(&data, false);
1479     test_oem_fields(&data);
1480     qtest_quit(data.qts);
1481     free_test_data(&data);
1482     g_free(args);
1483 }
1484 
1485 static void test_acpi_oem_fields_q35(void)
1486 {
1487     test_data data;
1488     char *args;
1489 
1490     memset(&data, 0, sizeof(data));
1491     data.machine = MACHINE_Q35;
1492     data.required_struct_types = base_required_struct_types;
1493     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
1494 
1495     args = test_acpi_create_args(&data,
1496                                  OEM_TEST_ARGS, false);
1497     data.qts = qtest_init(args);
1498     test_acpi_load_tables(&data, false);
1499     test_oem_fields(&data);
1500     qtest_quit(data.qts);
1501     free_test_data(&data);
1502     g_free(args);
1503 }
1504 
1505 static void test_acpi_oem_fields_microvm(void)
1506 {
1507     test_data data;
1508     char *args;
1509 
1510     test_acpi_microvm_prepare(&data);
1511 
1512     args = test_acpi_create_args(&data,
1513                                  OEM_TEST_ARGS",acpi=on", false);
1514     data.qts = qtest_init(args);
1515     test_acpi_load_tables(&data, false);
1516     test_oem_fields(&data);
1517     qtest_quit(data.qts);
1518     free_test_data(&data);
1519     g_free(args);
1520 }
1521 
1522 static void test_acpi_oem_fields_virt(void)
1523 {
1524     test_data data = {
1525         .machine = "virt",
1526         .tcg_only = true,
1527         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1528         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1529         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1530         .ram_start = 0x40000000ULL,
1531         .scan_len = 128ULL * 1024 * 1024,
1532     };
1533     char *args;
1534 
1535     args = test_acpi_create_args(&data,
1536                                  "-cpu cortex-a57 "OEM_TEST_ARGS, true);
1537     data.qts = qtest_init(args);
1538     test_acpi_load_tables(&data, true);
1539     test_oem_fields(&data);
1540     qtest_quit(data.qts);
1541     free_test_data(&data);
1542     g_free(args);
1543 }
1544 
1545 
1546 int main(int argc, char *argv[])
1547 {
1548     const char *arch = qtest_get_arch();
1549     const bool has_kvm = qtest_has_accel("kvm");
1550     int ret;
1551 
1552     g_test_init(&argc, &argv, NULL);
1553 
1554     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
1555         ret = boot_sector_init(disk);
1556         if (ret) {
1557             return ret;
1558         }
1559         qtest_add_func("acpi/q35/oem-fields", test_acpi_oem_fields_q35);
1560         if (tpm_model_is_available("-machine q35", "tpm-tis")) {
1561             qtest_add_func("acpi/q35/tpm2-tis", test_acpi_q35_tcg_tpm2_tis);
1562             qtest_add_func("acpi/q35/tpm12-tis", test_acpi_q35_tcg_tpm12_tis);
1563         }
1564         qtest_add_func("acpi/piix4", test_acpi_piix4_tcg);
1565         qtest_add_func("acpi/oem-fields", test_acpi_oem_fields_pc);
1566         qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge);
1567         qtest_add_func("acpi/piix4/pci-hotplug/no_root_hotplug",
1568                        test_acpi_piix4_no_root_hotplug);
1569         qtest_add_func("acpi/piix4/pci-hotplug/no_bridge_hotplug",
1570                        test_acpi_piix4_no_bridge_hotplug);
1571         qtest_add_func("acpi/piix4/pci-hotplug/off",
1572                        test_acpi_piix4_no_acpi_pci_hotplug);
1573         qtest_add_func("acpi/q35", test_acpi_q35_tcg);
1574         qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
1575         qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64);
1576         qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi);
1577         qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi);
1578         qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp);
1579         qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp);
1580         qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
1581         qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
1582         qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
1583         qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
1584         qtest_add_func("acpi/piix4/nosmm", test_acpi_piix4_tcg_nosmm);
1585         qtest_add_func("acpi/piix4/smm-compat",
1586                        test_acpi_piix4_tcg_smm_compat);
1587         qtest_add_func("acpi/piix4/smm-compat-nosmm",
1588                        test_acpi_piix4_tcg_smm_compat_nosmm);
1589         qtest_add_func("acpi/piix4/nohpet", test_acpi_piix4_tcg_nohpet);
1590         qtest_add_func("acpi/q35/nosmm", test_acpi_q35_tcg_nosmm);
1591         qtest_add_func("acpi/q35/smm-compat",
1592                        test_acpi_q35_tcg_smm_compat);
1593         qtest_add_func("acpi/q35/smm-compat-nosmm",
1594                        test_acpi_q35_tcg_smm_compat_nosmm);
1595         qtest_add_func("acpi/q35/nohpet", test_acpi_q35_tcg_nohpet);
1596         qtest_add_func("acpi/q35/ivrs", test_acpi_q35_tcg_ivrs);
1597         qtest_add_func("acpi/piix4/dimmpxm", test_acpi_piix4_tcg_dimm_pxm);
1598         qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm);
1599         qtest_add_func("acpi/piix4/acpihmat", test_acpi_piix4_tcg_acpi_hmat);
1600         qtest_add_func("acpi/q35/acpihmat", test_acpi_q35_tcg_acpi_hmat);
1601         qtest_add_func("acpi/microvm", test_acpi_microvm_tcg);
1602         qtest_add_func("acpi/microvm/usb", test_acpi_microvm_usb_tcg);
1603         qtest_add_func("acpi/microvm/rtc", test_acpi_microvm_rtc_tcg);
1604         qtest_add_func("acpi/microvm/ioapic2", test_acpi_microvm_ioapic2_tcg);
1605         qtest_add_func("acpi/microvm/oem-fields", test_acpi_oem_fields_microvm);
1606         if (strcmp(arch, "x86_64") == 0) {
1607             qtest_add_func("acpi/microvm/pcie", test_acpi_microvm_pcie_tcg);
1608         }
1609         if (has_kvm) {
1610             qtest_add_func("acpi/q35/kvm/xapic", test_acpi_q35_kvm_xapic);
1611             qtest_add_func("acpi/q35/kvm/dmar", test_acpi_q35_kvm_dmar);
1612         }
1613     } else if (strcmp(arch, "aarch64") == 0) {
1614         qtest_add_func("acpi/virt", test_acpi_virt_tcg);
1615         qtest_add_func("acpi/virt/numamem", test_acpi_virt_tcg_numamem);
1616         qtest_add_func("acpi/virt/memhp", test_acpi_virt_tcg_memhp);
1617         qtest_add_func("acpi/virt/pxb", test_acpi_virt_tcg_pxb);
1618         qtest_add_func("acpi/virt/oem-fields", test_acpi_oem_fields_virt);
1619     }
1620     ret = g_test_run();
1621     boot_sector_cleanup(disk);
1622     return ret;
1623 }
1624