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