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