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