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