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