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