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