xref: /openbmc/qemu/tests/qtest/bios-tables-test.c (revision c27c1cc3)
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:
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  * 7. Before sending patches to the list (Contributor)
42  *    or before doing a pull request (Maintainer), make sure
43  *    tests/qtest/bios-tables-test-allowed-diff.h is empty - this will ensure
44  *    following changes to ACPI tables will be noticed.
45  *
46  * The resulting patchset/pull request then looks like this:
47  * - patch 1: list changed files in tests/qtest/bios-tables-test-allowed-diff.h.
48  * - patches 2 - n: real changes, may contain multiple patches.
49  * - patch n + 1: update golden master binaries and empty
50  *   tests/qtest/bios-tables-test-allowed-diff.h
51  */
52 
53 #include "qemu/osdep.h"
54 #include <glib/gstdio.h>
55 #include "qemu-common.h"
56 #include "hw/firmware/smbios.h"
57 #include "qemu/bitmap.h"
58 #include "acpi-utils.h"
59 #include "boot-sector.h"
60 #include "tpm-emu.h"
61 #include "hw/acpi/tpm.h"
62 
63 
64 #define MACHINE_PC "pc"
65 #define MACHINE_Q35 "q35"
66 
67 #define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"
68 
69 typedef struct {
70     bool tcg_only;
71     const char *machine;
72     const char *variant;
73     const char *uefi_fl1;
74     const char *uefi_fl2;
75     const char *cd;
76     const uint64_t ram_start;
77     const uint64_t scan_len;
78     uint64_t rsdp_addr;
79     uint8_t rsdp_table[36 /* ACPI 2.0+ RSDP size */];
80     GArray *tables;
81     uint32_t smbios_ep_addr;
82     struct smbios_21_entry_point smbios_ep_table;
83     uint8_t *required_struct_types;
84     int required_struct_types_len;
85     QTestState *qts;
86 } test_data;
87 
88 static char disk[] = "tests/acpi-test-disk-XXXXXX";
89 static const char *data_dir = "tests/data/acpi";
90 #ifdef CONFIG_IASL
91 static const char *iasl = CONFIG_IASL;
92 #else
93 static const char *iasl;
94 #endif
95 
96 static bool compare_signature(const AcpiSdtTable *sdt, const char *signature)
97 {
98    return !memcmp(sdt->aml, signature, 4);
99 }
100 
101 static void cleanup_table_descriptor(AcpiSdtTable *table)
102 {
103     g_free(table->aml);
104     if (table->aml_file &&
105         !table->tmp_files_retain &&
106         g_strstr_len(table->aml_file, -1, "aml-")) {
107         unlink(table->aml_file);
108     }
109     g_free(table->aml_file);
110     g_free(table->asl);
111     if (table->asl_file &&
112         !table->tmp_files_retain) {
113         unlink(table->asl_file);
114     }
115     g_free(table->asl_file);
116 }
117 
118 static void free_test_data(test_data *data)
119 {
120     int i;
121 
122     for (i = 0; i < data->tables->len; ++i) {
123         cleanup_table_descriptor(&g_array_index(data->tables, AcpiSdtTable, i));
124     }
125 
126     g_array_free(data->tables, true);
127 }
128 
129 static void test_acpi_rsdp_table(test_data *data)
130 {
131     uint8_t *rsdp_table = data->rsdp_table;
132 
133     acpi_fetch_rsdp_table(data->qts, data->rsdp_addr, rsdp_table);
134 
135     switch (rsdp_table[15 /* Revision offset */]) {
136     case 0: /* ACPI 1.0 RSDP */
137         /* With rev 1, checksum is only for the first 20 bytes */
138         g_assert(!acpi_calc_checksum(rsdp_table,  20));
139         break;
140     case 2: /* ACPI 2.0+ RSDP */
141         /* With revision 2, we have 2 checksums */
142         g_assert(!acpi_calc_checksum(rsdp_table, 20));
143         g_assert(!acpi_calc_checksum(rsdp_table, 36));
144         break;
145     default:
146         g_assert_not_reached();
147     }
148 }
149 
150 static void test_acpi_rxsdt_table(test_data *data)
151 {
152     const char *sig = "RSDT";
153     AcpiSdtTable rsdt = {};
154     int entry_size = 4;
155     int addr_off = 16 /* RsdtAddress */;
156     uint8_t *ent;
157 
158     if (data->rsdp_table[15 /* Revision offset */] != 0) {
159         addr_off = 24 /* XsdtAddress */;
160         entry_size = 8;
161         sig = "XSDT";
162     }
163     /* read [RX]SDT table */
164     acpi_fetch_table(data->qts, &rsdt.aml, &rsdt.aml_len,
165                      &data->rsdp_table[addr_off], entry_size, sig, true);
166 
167     /* Load all tables and add to test list directly RSDT referenced tables */
168     ACPI_FOREACH_RSDT_ENTRY(rsdt.aml, rsdt.aml_len, ent, entry_size) {
169         AcpiSdtTable ssdt_table = {};
170 
171         acpi_fetch_table(data->qts, &ssdt_table.aml, &ssdt_table.aml_len, ent,
172                          entry_size, NULL, true);
173         /* Add table to ASL test tables list */
174         g_array_append_val(data->tables, ssdt_table);
175     }
176     cleanup_table_descriptor(&rsdt);
177 }
178 
179 static void test_acpi_fadt_table(test_data *data)
180 {
181     /* FADT table is 1st */
182     AcpiSdtTable table = g_array_index(data->tables, typeof(table), 0);
183     uint8_t *fadt_aml = table.aml;
184     uint32_t fadt_len = table.aml_len;
185     uint32_t val;
186     int dsdt_offset = 40 /* DSDT */;
187     int dsdt_entry_size = 4;
188 
189     g_assert(compare_signature(&table, "FACP"));
190 
191     /* Since DSDT/FACS isn't in RSDT, add them to ASL test list manually */
192     memcpy(&val, fadt_aml + 112 /* Flags */, 4);
193     val = le32_to_cpu(val);
194     if (!(val & 1UL << 20 /* HW_REDUCED_ACPI */)) {
195         acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
196                          fadt_aml + 36 /* FIRMWARE_CTRL */, 4, "FACS", false);
197         g_array_append_val(data->tables, table);
198     }
199 
200     memcpy(&val, fadt_aml + dsdt_offset, 4);
201     val = le32_to_cpu(val);
202     if (!val) {
203         dsdt_offset = 140 /* X_DSDT */;
204         dsdt_entry_size = 8;
205     }
206     acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
207                      fadt_aml + dsdt_offset, dsdt_entry_size, "DSDT", true);
208     g_array_append_val(data->tables, table);
209 
210     memset(fadt_aml + 36, 0, 4); /* sanitize FIRMWARE_CTRL ptr */
211     memset(fadt_aml + 40, 0, 4); /* sanitize DSDT ptr */
212     if (fadt_aml[8 /* FADT Major Version */] >= 3) {
213         memset(fadt_aml + 132, 0, 8); /* sanitize X_FIRMWARE_CTRL ptr */
214         memset(fadt_aml + 140, 0, 8); /* sanitize X_DSDT ptr */
215     }
216 
217     /* update checksum */
218     fadt_aml[9 /* Checksum */] = 0;
219     fadt_aml[9 /* Checksum */] -= acpi_calc_checksum(fadt_aml, fadt_len);
220 }
221 
222 static void dump_aml_files(test_data *data, bool rebuild)
223 {
224     AcpiSdtTable *sdt;
225     GError *error = NULL;
226     gchar *aml_file = NULL;
227     gint fd;
228     ssize_t ret;
229     int i;
230 
231     for (i = 0; i < data->tables->len; ++i) {
232         const char *ext = data->variant ? data->variant : "";
233         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
234         g_assert(sdt->aml);
235 
236         if (rebuild) {
237             aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
238                                        sdt->aml, ext);
239             fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
240                         S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
241             if (fd < 0) {
242                 perror(aml_file);
243             }
244             g_assert(fd >= 0);
245         } else {
246             fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
247             g_assert_no_error(error);
248         }
249 
250         ret = qemu_write_full(fd, sdt->aml, sdt->aml_len);
251         g_assert(ret == sdt->aml_len);
252 
253         close(fd);
254 
255         g_free(aml_file);
256     }
257 }
258 
259 static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
260 {
261     AcpiSdtTable *temp;
262     GError *error = NULL;
263     GString *command_line = g_string_new(iasl);
264     gint fd;
265     gchar *out, *out_err;
266     gboolean ret;
267     int i;
268 
269     fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
270     g_assert_no_error(error);
271     close(fd);
272 
273     /* build command line */
274     g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
275     if (compare_signature(sdt, "DSDT") ||
276         compare_signature(sdt, "SSDT")) {
277         for (i = 0; i < sdts->len; ++i) {
278             temp = &g_array_index(sdts, AcpiSdtTable, i);
279             if (compare_signature(temp, "DSDT") ||
280                 compare_signature(temp, "SSDT")) {
281                 g_string_append_printf(command_line, "-e %s ", temp->aml_file);
282             }
283         }
284     }
285     g_string_append_printf(command_line, "-d %s", sdt->aml_file);
286 
287     /* pass 'out' and 'out_err' in order to be redirected */
288     ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
289     g_assert_no_error(error);
290     if (ret) {
291         ret = g_file_get_contents(sdt->asl_file, &sdt->asl,
292                                   &sdt->asl_len, &error);
293         g_assert(ret);
294         g_assert_no_error(error);
295         ret = (sdt->asl_len > 0);
296     }
297 
298     g_free(out);
299     g_free(out_err);
300     g_string_free(command_line, true);
301 
302     return !ret;
303 }
304 
305 #define COMMENT_END "*/"
306 #define DEF_BLOCK "DefinitionBlock ("
307 #define BLOCK_NAME_END ","
308 
309 static GString *normalize_asl(gchar *asl_code)
310 {
311     GString *asl = g_string_new(asl_code);
312     gchar *comment, *block_name;
313 
314     /* strip comments (different generation days) */
315     comment = g_strstr_len(asl->str, asl->len, COMMENT_END);
316     if (comment) {
317         comment += strlen(COMMENT_END);
318         while (*comment == '\n') {
319             comment++;
320         }
321         asl = g_string_erase(asl, 0, comment - asl->str);
322     }
323 
324     /* strip def block name (it has file path in it) */
325     if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
326         block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
327         g_assert(block_name);
328         asl = g_string_erase(asl, 0,
329                              block_name + sizeof(BLOCK_NAME_END) - asl->str);
330     }
331 
332     return asl;
333 }
334 
335 static GArray *load_expected_aml(test_data *data)
336 {
337     int i;
338     AcpiSdtTable *sdt;
339     GError *error = NULL;
340     gboolean ret;
341     gsize aml_len;
342 
343     GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
344     if (getenv("V")) {
345         fputc('\n', stderr);
346     }
347     for (i = 0; i < data->tables->len; ++i) {
348         AcpiSdtTable exp_sdt;
349         gchar *aml_file = NULL;
350         const char *ext = data->variant ? data->variant : "";
351 
352         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
353 
354         memset(&exp_sdt, 0, sizeof(exp_sdt));
355 
356 try_again:
357         aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
358                                    sdt->aml, ext);
359         if (getenv("V")) {
360             fprintf(stderr, "Looking for expected file '%s'\n", aml_file);
361         }
362         if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) {
363             exp_sdt.aml_file = aml_file;
364         } else if (*ext != '\0') {
365             /* try fallback to generic (extension less) expected file */
366             ext = "";
367             g_free(aml_file);
368             goto try_again;
369         }
370         g_assert(exp_sdt.aml_file);
371         if (getenv("V")) {
372             fprintf(stderr, "Using expected file '%s'\n", aml_file);
373         }
374         ret = g_file_get_contents(aml_file, (gchar **)&exp_sdt.aml,
375                                   &aml_len, &error);
376         exp_sdt.aml_len = aml_len;
377         g_assert(ret);
378         g_assert_no_error(error);
379         g_assert(exp_sdt.aml);
380         if (!exp_sdt.aml_len) {
381             fprintf(stderr, "Warning! zero length expected file '%s'\n",
382                     aml_file);
383         }
384 
385         g_array_append_val(exp_tables, exp_sdt);
386     }
387 
388     return exp_tables;
389 }
390 
391 static bool test_acpi_find_diff_allowed(AcpiSdtTable *sdt)
392 {
393     const gchar *allowed_diff_file[] = {
394 #include "bios-tables-test-allowed-diff.h"
395         NULL
396     };
397     const gchar **f;
398 
399     for (f = allowed_diff_file; *f; ++f) {
400         if (!g_strcmp0(sdt->aml_file, *f)) {
401             return true;
402         }
403     }
404     return false;
405 }
406 
407 /* test the list of tables in @data->tables against reference tables */
408 static void test_acpi_asl(test_data *data)
409 {
410     int i;
411     AcpiSdtTable *sdt, *exp_sdt;
412     test_data exp_data;
413     gboolean exp_err, err, all_tables_match = true;
414 
415     memset(&exp_data, 0, sizeof(exp_data));
416     exp_data.tables = load_expected_aml(data);
417     dump_aml_files(data, false);
418     for (i = 0; i < data->tables->len; ++i) {
419         GString *asl, *exp_asl;
420 
421         sdt = &g_array_index(data->tables, AcpiSdtTable, i);
422         exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
423 
424         if (sdt->aml_len == exp_sdt->aml_len &&
425             !memcmp(sdt->aml, exp_sdt->aml, sdt->aml_len)) {
426             /* Identical table binaries: no need to disassemble. */
427             continue;
428         }
429 
430         fprintf(stderr,
431                 "acpi-test: Warning! %.4s binary file mismatch. "
432                 "Actual [aml:%s], Expected [aml:%s].\n"
433                 "See source file tests/qtest/bios-tables-test.c "
434                 "for instructions on how to update expected files.\n",
435                 exp_sdt->aml, sdt->aml_file, exp_sdt->aml_file);
436 
437         all_tables_match = all_tables_match &&
438             test_acpi_find_diff_allowed(exp_sdt);
439 
440         /*
441          *  don't try to decompile if IASL isn't present, in this case user
442          * will just 'get binary file mismatch' warnings and test failure
443          */
444         if (!iasl) {
445             continue;
446         }
447 
448         err = load_asl(data->tables, sdt);
449         asl = normalize_asl(sdt->asl);
450 
451         exp_err = load_asl(exp_data.tables, exp_sdt);
452         exp_asl = normalize_asl(exp_sdt->asl);
453 
454         /* TODO: check for warnings */
455         g_assert(!err || exp_err);
456 
457         if (g_strcmp0(asl->str, exp_asl->str)) {
458             sdt->tmp_files_retain = true;
459             if (exp_err) {
460                 fprintf(stderr,
461                         "Warning! iasl couldn't parse the expected aml\n");
462             } else {
463                 exp_sdt->tmp_files_retain = true;
464                 fprintf(stderr,
465                         "acpi-test: Warning! %.4s mismatch. "
466                         "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
467                         exp_sdt->aml, sdt->asl_file, sdt->aml_file,
468                         exp_sdt->asl_file, exp_sdt->aml_file);
469                 fflush(stderr);
470                 if (getenv("V")) {
471                     const char *diff_env = getenv("DIFF");
472                     const char *diff_cmd = diff_env ? diff_env : "diff -U 16";
473                     char *diff = g_strdup_printf("%s %s %s", diff_cmd,
474                                                  exp_sdt->asl_file, sdt->asl_file);
475                     int out = dup(STDOUT_FILENO);
476                     int ret G_GNUC_UNUSED;
477 
478                     dup2(STDERR_FILENO, STDOUT_FILENO);
479                     ret = system(diff) ;
480                     dup2(out, STDOUT_FILENO);
481                     close(out);
482                     g_free(diff);
483                 }
484             }
485         }
486         g_string_free(asl, true);
487         g_string_free(exp_asl, true);
488     }
489     if (!iasl && !all_tables_match) {
490         fprintf(stderr, "to see ASL diff between mismatched files install IASL,"
491                 " rebuild QEMU from scratch and re-run tests with V=1"
492                 " environment variable set");
493     }
494     g_assert(all_tables_match);
495 
496     free_test_data(&exp_data);
497 }
498 
499 static bool smbios_ep_table_ok(test_data *data)
500 {
501     struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
502     uint32_t addr = data->smbios_ep_addr;
503 
504     qtest_memread(data->qts, addr, ep_table, sizeof(*ep_table));
505     if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
506         return false;
507     }
508     if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
509         return false;
510     }
511     if (ep_table->structure_table_length == 0) {
512         return false;
513     }
514     if (ep_table->number_of_structures == 0) {
515         return false;
516     }
517     if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
518         acpi_calc_checksum((uint8_t *)ep_table + 0x10,
519                            sizeof *ep_table - 0x10)) {
520         return false;
521     }
522     return true;
523 }
524 
525 static void test_smbios_entry_point(test_data *data)
526 {
527     uint32_t off;
528 
529     /* find smbios entry point structure */
530     for (off = 0xf0000; off < 0x100000; off += 0x10) {
531         uint8_t sig[] = "_SM_";
532         int i;
533 
534         for (i = 0; i < sizeof sig - 1; ++i) {
535             sig[i] = qtest_readb(data->qts, off + i);
536         }
537 
538         if (!memcmp(sig, "_SM_", sizeof sig)) {
539             /* signature match, but is this a valid entry point? */
540             data->smbios_ep_addr = off;
541             if (smbios_ep_table_ok(data)) {
542                 break;
543             }
544         }
545     }
546 
547     g_assert_cmphex(off, <, 0x100000);
548 }
549 
550 static inline bool smbios_single_instance(uint8_t type)
551 {
552     switch (type) {
553     case 0:
554     case 1:
555     case 2:
556     case 3:
557     case 16:
558     case 32:
559     case 127:
560         return true;
561     default:
562         return false;
563     }
564 }
565 
566 static void test_smbios_structs(test_data *data)
567 {
568     DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };
569     struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
570     uint32_t addr = le32_to_cpu(ep_table->structure_table_address);
571     int i, len, max_len = 0;
572     uint8_t type, prv, crt;
573 
574     /* walk the smbios tables */
575     for (i = 0; i < le16_to_cpu(ep_table->number_of_structures); i++) {
576 
577         /* grab type and formatted area length from struct header */
578         type = qtest_readb(data->qts, addr);
579         g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
580         len = qtest_readb(data->qts, addr + 1);
581 
582         /* single-instance structs must not have been encountered before */
583         if (smbios_single_instance(type)) {
584             g_assert(!test_bit(type, struct_bitmap));
585         }
586         set_bit(type, struct_bitmap);
587 
588         /* seek to end of unformatted string area of this struct ("\0\0") */
589         prv = crt = 1;
590         while (prv || crt) {
591             prv = crt;
592             crt = qtest_readb(data->qts, addr + len);
593             len++;
594         }
595 
596         /* keep track of max. struct size */
597         if (max_len < len) {
598             max_len = len;
599             g_assert_cmpuint(max_len, <=, ep_table->max_structure_size);
600         }
601 
602         /* start of next structure */
603         addr += len;
604     }
605 
606     /* total table length and max struct size must match entry point values */
607     g_assert_cmpuint(le16_to_cpu(ep_table->structure_table_length), ==,
608                      addr - le32_to_cpu(ep_table->structure_table_address));
609     g_assert_cmpuint(le16_to_cpu(ep_table->max_structure_size), ==, max_len);
610 
611     /* required struct types must all be present */
612     for (i = 0; i < data->required_struct_types_len; i++) {
613         g_assert(test_bit(data->required_struct_types[i], struct_bitmap));
614     }
615 }
616 
617 static void test_acpi_one(const char *params, test_data *data)
618 {
619     char *args;
620     bool use_uefi = data->uefi_fl1 && data->uefi_fl2;
621 
622     if (use_uefi) {
623         /*
624          * TODO: convert '-drive if=pflash' to new syntax (see e33763be7cd3)
625          * when arm/virt boad starts to support it.
626          */
627         args = g_strdup_printf("-machine %s %s -accel tcg -nodefaults -nographic "
628             "-drive if=pflash,format=raw,file=%s,readonly "
629             "-drive if=pflash,format=raw,file=%s,snapshot=on -cdrom %s %s",
630             data->machine, data->tcg_only ? "" : "-accel kvm",
631             data->uefi_fl1, data->uefi_fl2, data->cd, params ? params : "");
632 
633     } else {
634         /* Disable kernel irqchip to be able to override apic irq0. */
635         args = g_strdup_printf("-machine %s,kernel-irqchip=off %s -accel tcg "
636             "-net none -display none %s "
637             "-drive id=hd0,if=none,file=%s,format=raw "
638             "-device ide-hd,drive=hd0 ",
639              data->machine, data->tcg_only ? "" : "-accel kvm",
640              params ? params : "", disk);
641     }
642 
643     data->qts = qtest_init(args);
644 
645     if (use_uefi) {
646         g_assert(data->scan_len);
647         data->rsdp_addr = acpi_find_rsdp_address_uefi(data->qts,
648             data->ram_start, data->scan_len);
649     } else {
650         boot_sector_test(data->qts);
651         data->rsdp_addr = acpi_find_rsdp_address(data->qts);
652         g_assert_cmphex(data->rsdp_addr, <, 0x100000);
653     }
654 
655     data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
656     test_acpi_rsdp_table(data);
657     test_acpi_rxsdt_table(data);
658     test_acpi_fadt_table(data);
659 
660     if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
661         dump_aml_files(data, true);
662     } else {
663         test_acpi_asl(data);
664     }
665 
666     /*
667      * TODO: make SMBIOS tests work with UEFI firmware,
668      * Bug on uefi-test-tools to provide entry point:
669      * https://bugs.launchpad.net/qemu/+bug/1821884
670      */
671     if (!use_uefi) {
672         test_smbios_entry_point(data);
673         test_smbios_structs(data);
674     }
675 
676     qtest_quit(data->qts);
677     g_free(args);
678 }
679 
680 static uint8_t base_required_struct_types[] = {
681     0, 1, 3, 4, 16, 17, 19, 32, 127
682 };
683 
684 static void test_acpi_piix4_tcg(void)
685 {
686     test_data data;
687 
688     /* Supplying -machine accel argument overrides the default (qtest).
689      * This is to make guest actually run.
690      */
691     memset(&data, 0, sizeof(data));
692     data.machine = MACHINE_PC;
693     data.required_struct_types = base_required_struct_types;
694     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
695     test_acpi_one(NULL, &data);
696     free_test_data(&data);
697 }
698 
699 static void test_acpi_piix4_tcg_bridge(void)
700 {
701     test_data data;
702 
703     memset(&data, 0, sizeof(data));
704     data.machine = MACHINE_PC;
705     data.variant = ".bridge";
706     data.required_struct_types = base_required_struct_types;
707     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
708     test_acpi_one("-device pci-bridge,chassis_nr=1", &data);
709     free_test_data(&data);
710 }
711 
712 static void test_acpi_q35_tcg(void)
713 {
714     test_data data;
715 
716     memset(&data, 0, sizeof(data));
717     data.machine = MACHINE_Q35;
718     data.required_struct_types = base_required_struct_types;
719     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
720     test_acpi_one(NULL, &data);
721     free_test_data(&data);
722 }
723 
724 static void test_acpi_q35_tcg_bridge(void)
725 {
726     test_data data;
727 
728     memset(&data, 0, sizeof(data));
729     data.machine = MACHINE_Q35;
730     data.variant = ".bridge";
731     data.required_struct_types = base_required_struct_types;
732     data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
733     test_acpi_one("-device pci-bridge,chassis_nr=1",
734                   &data);
735     free_test_data(&data);
736 }
737 
738 static void test_acpi_q35_tcg_mmio64(void)
739 {
740     test_data data = {
741         .machine = MACHINE_Q35,
742         .variant = ".mmio64",
743         .required_struct_types = base_required_struct_types,
744         .required_struct_types_len = ARRAY_SIZE(base_required_struct_types)
745     };
746 
747     test_acpi_one("-m 128M,slots=1,maxmem=2G "
748                   "-object memory-backend-ram,id=ram0,size=128M "
749                   "-numa node,memdev=ram0 "
750                   "-device pci-testdev,membar=2G",
751                   &data);
752     free_test_data(&data);
753 }
754 
755 static void test_acpi_piix4_tcg_cphp(void)
756 {
757     test_data data;
758 
759     memset(&data, 0, sizeof(data));
760     data.machine = MACHINE_PC;
761     data.variant = ".cphp";
762     test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
763                   " -object memory-backend-ram,id=ram0,size=64M"
764                   " -object memory-backend-ram,id=ram1,size=64M"
765                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
766                   " -numa dist,src=0,dst=1,val=21",
767                   &data);
768     free_test_data(&data);
769 }
770 
771 static void test_acpi_q35_tcg_cphp(void)
772 {
773     test_data data;
774 
775     memset(&data, 0, sizeof(data));
776     data.machine = MACHINE_Q35;
777     data.variant = ".cphp";
778     test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
779                   " -object memory-backend-ram,id=ram0,size=64M"
780                   " -object memory-backend-ram,id=ram1,size=64M"
781                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
782                   " -numa dist,src=0,dst=1,val=21",
783                   &data);
784     free_test_data(&data);
785 }
786 
787 static uint8_t ipmi_required_struct_types[] = {
788     0, 1, 3, 4, 16, 17, 19, 32, 38, 127
789 };
790 
791 static void test_acpi_q35_tcg_ipmi(void)
792 {
793     test_data data;
794 
795     memset(&data, 0, sizeof(data));
796     data.machine = MACHINE_Q35;
797     data.variant = ".ipmibt";
798     data.required_struct_types = ipmi_required_struct_types;
799     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
800     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
801                   " -device isa-ipmi-bt,bmc=bmc0",
802                   &data);
803     free_test_data(&data);
804 }
805 
806 static void test_acpi_piix4_tcg_ipmi(void)
807 {
808     test_data data;
809 
810     /* Supplying -machine accel argument overrides the default (qtest).
811      * This is to make guest actually run.
812      */
813     memset(&data, 0, sizeof(data));
814     data.machine = MACHINE_PC;
815     data.variant = ".ipmikcs";
816     data.required_struct_types = ipmi_required_struct_types;
817     data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
818     test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
819                   " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
820                   &data);
821     free_test_data(&data);
822 }
823 
824 static void test_acpi_q35_tcg_memhp(void)
825 {
826     test_data data;
827 
828     memset(&data, 0, sizeof(data));
829     data.machine = MACHINE_Q35;
830     data.variant = ".memhp";
831     test_acpi_one(" -m 128,slots=3,maxmem=1G"
832                   " -object memory-backend-ram,id=ram0,size=64M"
833                   " -object memory-backend-ram,id=ram1,size=64M"
834                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
835                   " -numa dist,src=0,dst=1,val=21",
836                   &data);
837     free_test_data(&data);
838 }
839 
840 static void test_acpi_piix4_tcg_memhp(void)
841 {
842     test_data data;
843 
844     memset(&data, 0, sizeof(data));
845     data.machine = MACHINE_PC;
846     data.variant = ".memhp";
847     test_acpi_one(" -m 128,slots=3,maxmem=1G"
848                   " -object memory-backend-ram,id=ram0,size=64M"
849                   " -object memory-backend-ram,id=ram1,size=64M"
850                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
851                   " -numa dist,src=0,dst=1,val=21",
852                   &data);
853     free_test_data(&data);
854 }
855 
856 static void test_acpi_q35_tcg_numamem(void)
857 {
858     test_data data;
859 
860     memset(&data, 0, sizeof(data));
861     data.machine = MACHINE_Q35;
862     data.variant = ".numamem";
863     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
864                   " -numa node -numa node,memdev=ram0", &data);
865     free_test_data(&data);
866 }
867 
868 static void test_acpi_piix4_tcg_numamem(void)
869 {
870     test_data data;
871 
872     memset(&data, 0, sizeof(data));
873     data.machine = MACHINE_PC;
874     data.variant = ".numamem";
875     test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
876                   " -numa node -numa node,memdev=ram0", &data);
877     free_test_data(&data);
878 }
879 
880 uint64_t tpm_tis_base_addr;
881 
882 static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
883                               uint64_t base)
884 {
885 #ifdef CONFIG_TPM
886     gchar *tmp_dir_name = g_strdup_printf("qemu-test_acpi_%s_tcg_%s.XXXXXX",
887                                           machine, tpm_if);
888     char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
889     TestState test;
890     test_data data;
891     GThread *thread;
892     char *args, *variant = g_strdup_printf(".%s", tpm_if);
893 
894     tpm_tis_base_addr = base;
895 
896     module_call_init(MODULE_INIT_QOM);
897 
898     test.addr = g_new0(SocketAddress, 1);
899     test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
900     test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
901     g_mutex_init(&test.data_mutex);
902     g_cond_init(&test.data_cond);
903     test.data_cond_signal = false;
904 
905     thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
906     tpm_emu_test_wait_cond(&test);
907 
908     memset(&data, 0, sizeof(data));
909     data.machine = machine;
910     data.variant = variant;
911 
912     args = g_strdup_printf(
913         " -chardev socket,id=chr,path=%s"
914         " -tpmdev emulator,id=dev,chardev=chr"
915         " -device tpm-%s,tpmdev=dev",
916         test.addr->u.q_unix.path, tpm_if);
917 
918     test_acpi_one(args, &data);
919 
920     g_thread_join(thread);
921     g_unlink(test.addr->u.q_unix.path);
922     qapi_free_SocketAddress(test.addr);
923     g_rmdir(tmp_path);
924     g_free(variant);
925     g_free(tmp_path);
926     g_free(tmp_dir_name);
927     g_free(args);
928     free_test_data(&data);
929 #else
930     g_test_skip("TPM disabled");
931 #endif
932 }
933 
934 static void test_acpi_q35_tcg_tpm_tis(void)
935 {
936     test_acpi_tcg_tpm("q35", "tis", 0xFED40000);
937 }
938 
939 static void test_acpi_tcg_dimm_pxm(const char *machine)
940 {
941     test_data data;
942 
943     memset(&data, 0, sizeof(data));
944     data.machine = machine;
945     data.variant = ".dimmpxm";
946     test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
947                   " -smp 4,sockets=4"
948                   " -m 128M,slots=3,maxmem=1G"
949                   " -object memory-backend-ram,id=ram0,size=32M"
950                   " -object memory-backend-ram,id=ram1,size=32M"
951                   " -object memory-backend-ram,id=ram2,size=32M"
952                   " -object memory-backend-ram,id=ram3,size=32M"
953                   " -numa node,memdev=ram0,nodeid=0"
954                   " -numa node,memdev=ram1,nodeid=1"
955                   " -numa node,memdev=ram2,nodeid=2"
956                   " -numa node,memdev=ram3,nodeid=3"
957                   " -numa cpu,node-id=0,socket-id=0"
958                   " -numa cpu,node-id=1,socket-id=1"
959                   " -numa cpu,node-id=2,socket-id=2"
960                   " -numa cpu,node-id=3,socket-id=3"
961                   " -object memory-backend-ram,id=ram4,size=128M"
962                   " -object memory-backend-ram,id=nvm0,size=128M"
963                   " -device pc-dimm,id=dimm0,memdev=ram4,node=1"
964                   " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
965                   &data);
966     free_test_data(&data);
967 }
968 
969 static void test_acpi_q35_tcg_dimm_pxm(void)
970 {
971     test_acpi_tcg_dimm_pxm(MACHINE_Q35);
972 }
973 
974 static void test_acpi_piix4_tcg_dimm_pxm(void)
975 {
976     test_acpi_tcg_dimm_pxm(MACHINE_PC);
977 }
978 
979 static void test_acpi_virt_tcg_memhp(void)
980 {
981     test_data data = {
982         .machine = "virt",
983         .tcg_only = true,
984         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
985         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
986         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
987         .ram_start = 0x40000000ULL,
988         .scan_len = 256ULL * 1024 * 1024,
989     };
990 
991     data.variant = ".memhp";
992     test_acpi_one(" -machine nvdimm=on"
993                   " -cpu cortex-a57"
994                   " -m 256M,slots=3,maxmem=1G"
995                   " -object memory-backend-ram,id=ram0,size=128M"
996                   " -object memory-backend-ram,id=ram1,size=128M"
997                   " -numa node,memdev=ram0 -numa node,memdev=ram1"
998                   " -numa dist,src=0,dst=1,val=21"
999                   " -object memory-backend-ram,id=ram2,size=128M"
1000                   " -object memory-backend-ram,id=nvm0,size=128M"
1001                   " -device pc-dimm,id=dimm0,memdev=ram2,node=0"
1002                   " -device nvdimm,id=dimm1,memdev=nvm0,node=1",
1003                   &data);
1004 
1005     free_test_data(&data);
1006 
1007 }
1008 
1009 static void test_acpi_virt_tcg_numamem(void)
1010 {
1011     test_data data = {
1012         .machine = "virt",
1013         .tcg_only = true,
1014         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1015         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1016         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1017         .ram_start = 0x40000000ULL,
1018         .scan_len = 128ULL * 1024 * 1024,
1019     };
1020 
1021     data.variant = ".numamem";
1022     test_acpi_one(" -cpu cortex-a57"
1023                   " -object memory-backend-ram,id=ram0,size=128M"
1024                   " -numa node,memdev=ram0",
1025                   &data);
1026 
1027     free_test_data(&data);
1028 
1029 }
1030 
1031 static void test_acpi_tcg_acpi_hmat(const char *machine)
1032 {
1033     test_data data;
1034 
1035     memset(&data, 0, sizeof(data));
1036     data.machine = machine;
1037     data.variant = ".acpihmat";
1038     test_acpi_one(" -machine hmat=on"
1039                   " -smp 2,sockets=2"
1040                   " -m 128M,slots=2,maxmem=1G"
1041                   " -object memory-backend-ram,size=64M,id=m0"
1042                   " -object memory-backend-ram,size=64M,id=m1"
1043                   " -numa node,nodeid=0,memdev=m0"
1044                   " -numa node,nodeid=1,memdev=m1,initiator=0"
1045                   " -numa cpu,node-id=0,socket-id=0"
1046                   " -numa cpu,node-id=0,socket-id=1"
1047                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1048                   "data-type=access-latency,latency=1"
1049                   " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1050                   "data-type=access-bandwidth,bandwidth=65534M"
1051                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1052                   "data-type=access-latency,latency=65534"
1053                   " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1054                   "data-type=access-bandwidth,bandwidth=32767M"
1055                   " -numa hmat-cache,node-id=0,size=10K,level=1,"
1056                   "associativity=direct,policy=write-back,line=8"
1057                   " -numa hmat-cache,node-id=1,size=10K,level=1,"
1058                   "associativity=direct,policy=write-back,line=8",
1059                   &data);
1060     free_test_data(&data);
1061 }
1062 
1063 static void test_acpi_q35_tcg_acpi_hmat(void)
1064 {
1065     test_acpi_tcg_acpi_hmat(MACHINE_Q35);
1066 }
1067 
1068 static void test_acpi_piix4_tcg_acpi_hmat(void)
1069 {
1070     test_acpi_tcg_acpi_hmat(MACHINE_PC);
1071 }
1072 
1073 static void test_acpi_virt_tcg(void)
1074 {
1075     test_data data = {
1076         .machine = "virt",
1077         .tcg_only = true,
1078         .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1079         .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1080         .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1081         .ram_start = 0x40000000ULL,
1082         .scan_len = 128ULL * 1024 * 1024,
1083     };
1084 
1085     test_acpi_one("-cpu cortex-a57", &data);
1086     free_test_data(&data);
1087 }
1088 
1089 int main(int argc, char *argv[])
1090 {
1091     const char *arch = qtest_get_arch();
1092     int ret;
1093 
1094     g_test_init(&argc, &argv, NULL);
1095 
1096     if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
1097         ret = boot_sector_init(disk);
1098         if (ret) {
1099             return ret;
1100         }
1101 
1102         qtest_add_func("acpi/q35/tpm-tis", test_acpi_q35_tcg_tpm_tis);
1103         qtest_add_func("acpi/piix4", test_acpi_piix4_tcg);
1104         qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge);
1105         qtest_add_func("acpi/q35", test_acpi_q35_tcg);
1106         qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
1107         qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64);
1108         qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi);
1109         qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi);
1110         qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp);
1111         qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp);
1112         qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
1113         qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
1114         qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
1115         qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
1116         qtest_add_func("acpi/piix4/dimmpxm", test_acpi_piix4_tcg_dimm_pxm);
1117         qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm);
1118         qtest_add_func("acpi/piix4/acpihmat", test_acpi_piix4_tcg_acpi_hmat);
1119         qtest_add_func("acpi/q35/acpihmat", test_acpi_q35_tcg_acpi_hmat);
1120     } else if (strcmp(arch, "aarch64") == 0) {
1121         qtest_add_func("acpi/virt", test_acpi_virt_tcg);
1122         qtest_add_func("acpi/virt/numamem", test_acpi_virt_tcg_numamem);
1123         qtest_add_func("acpi/virt/memhp", test_acpi_virt_tcg_memhp);
1124     }
1125     ret = g_test_run();
1126     boot_sector_cleanup(disk);
1127     return ret;
1128 }
1129