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