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