1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58#include "qemu/osdep.h"
59#include <glib/gstdio.h>
60#include "qemu-common.h"
61#include "hw/firmware/smbios.h"
62#include "qemu/bitmap.h"
63#include "acpi-utils.h"
64#include "boot-sector.h"
65#include "tpm-emu.h"
66#include "hw/acpi/tpm.h"
67
68
69#define MACHINE_PC "pc"
70#define MACHINE_Q35 "q35"
71
72#define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"
73
74typedef struct {
75 bool tcg_only;
76 const char *machine;
77 const char *variant;
78 const char *uefi_fl1;
79 const char *uefi_fl2;
80 const char *blkdev;
81 const char *cd;
82 const uint64_t ram_start;
83 const uint64_t scan_len;
84 uint64_t rsdp_addr;
85 uint8_t rsdp_table[36 ];
86 GArray *tables;
87 uint32_t smbios_ep_addr;
88 struct smbios_21_entry_point smbios_ep_table;
89 uint16_t smbios_cpu_max_speed;
90 uint16_t smbios_cpu_curr_speed;
91 uint8_t *required_struct_types;
92 int required_struct_types_len;
93 QTestState *qts;
94} test_data;
95
96static char disk[] = "tests/acpi-test-disk-XXXXXX";
97static const char *data_dir = "tests/data/acpi";
98#ifdef CONFIG_IASL
99static const char *iasl = CONFIG_IASL;
100#else
101static const char *iasl;
102#endif
103
104static bool compare_signature(const AcpiSdtTable *sdt, const char *signature)
105{
106 return !memcmp(sdt->aml, signature, 4);
107}
108
109static void cleanup_table_descriptor(AcpiSdtTable *table)
110{
111 g_free(table->aml);
112 if (table->aml_file &&
113 !table->tmp_files_retain &&
114 g_strstr_len(table->aml_file, -1, "aml-")) {
115 unlink(table->aml_file);
116 }
117 g_free(table->aml_file);
118 g_free(table->asl);
119 if (table->asl_file &&
120 !table->tmp_files_retain) {
121 unlink(table->asl_file);
122 }
123 g_free(table->asl_file);
124}
125
126static void free_test_data(test_data *data)
127{
128 int i;
129
130 if (!data->tables) {
131 return;
132 }
133 for (i = 0; i < data->tables->len; ++i) {
134 cleanup_table_descriptor(&g_array_index(data->tables, AcpiSdtTable, i));
135 }
136
137 g_array_free(data->tables, true);
138}
139
140static void test_acpi_rsdp_table(test_data *data)
141{
142 uint8_t *rsdp_table = data->rsdp_table;
143
144 acpi_fetch_rsdp_table(data->qts, data->rsdp_addr, rsdp_table);
145
146 switch (rsdp_table[15 ]) {
147 case 0:
148
149 g_assert(!acpi_calc_checksum(rsdp_table, 20));
150 break;
151 case 2:
152
153 g_assert(!acpi_calc_checksum(rsdp_table, 20));
154 g_assert(!acpi_calc_checksum(rsdp_table, 36));
155 break;
156 default:
157 g_assert_not_reached();
158 }
159}
160
161static void test_acpi_rxsdt_table(test_data *data)
162{
163 const char *sig = "RSDT";
164 AcpiSdtTable rsdt = {};
165 int entry_size = 4;
166 int addr_off = 16 ;
167 uint8_t *ent;
168
169 if (data->rsdp_table[15 ] != 0) {
170 addr_off = 24 ;
171 entry_size = 8;
172 sig = "XSDT";
173 }
174
175 acpi_fetch_table(data->qts, &rsdt.aml, &rsdt.aml_len,
176 &data->rsdp_table[addr_off], entry_size, sig, true);
177
178
179 ACPI_FOREACH_RSDT_ENTRY(rsdt.aml, rsdt.aml_len, ent, entry_size) {
180 AcpiSdtTable ssdt_table = {};
181
182 acpi_fetch_table(data->qts, &ssdt_table.aml, &ssdt_table.aml_len, ent,
183 entry_size, NULL, true);
184
185 g_array_append_val(data->tables, ssdt_table);
186 }
187 cleanup_table_descriptor(&rsdt);
188}
189
190static void test_acpi_fadt_table(test_data *data)
191{
192
193 AcpiSdtTable table = g_array_index(data->tables, typeof(table), 0);
194 uint8_t *fadt_aml = table.aml;
195 uint32_t fadt_len = table.aml_len;
196 uint32_t val;
197 int dsdt_offset = 40 ;
198 int dsdt_entry_size = 4;
199
200 g_assert(compare_signature(&table, "FACP"));
201
202
203 memcpy(&val, fadt_aml + 112 , 4);
204 val = le32_to_cpu(val);
205 if (!(val & 1UL << 20 )) {
206 acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
207 fadt_aml + 36 , 4, "FACS", false);
208 g_array_append_val(data->tables, table);
209 }
210
211 memcpy(&val, fadt_aml + dsdt_offset, 4);
212 val = le32_to_cpu(val);
213 if (!val) {
214 dsdt_offset = 140 ;
215 dsdt_entry_size = 8;
216 }
217 acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
218 fadt_aml + dsdt_offset, dsdt_entry_size, "DSDT", true);
219 g_array_append_val(data->tables, table);
220
221 memset(fadt_aml + 36, 0, 4);
222 memset(fadt_aml + 40, 0, 4);
223 if (fadt_aml[8 ] >= 3) {
224 memset(fadt_aml + 132, 0, 8);
225 memset(fadt_aml + 140, 0, 8);
226 }
227
228
229 fadt_aml[9 ] = 0;
230 fadt_aml[9 ] -= acpi_calc_checksum(fadt_aml, fadt_len);
231}
232
233static void dump_aml_files(test_data *data, bool rebuild)
234{
235 AcpiSdtTable *sdt;
236 GError *error = NULL;
237 gchar *aml_file = NULL;
238 gint fd;
239 ssize_t ret;
240 int i;
241
242 for (i = 0; i < data->tables->len; ++i) {
243 const char *ext = data->variant ? data->variant : "";
244 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
245 g_assert(sdt->aml);
246
247 if (rebuild) {
248 aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
249 sdt->aml, ext);
250 fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
251 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
252 if (fd < 0) {
253 perror(aml_file);
254 }
255 g_assert(fd >= 0);
256 } else {
257 fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
258 g_assert_no_error(error);
259 }
260
261 ret = qemu_write_full(fd, sdt->aml, sdt->aml_len);
262 g_assert(ret == sdt->aml_len);
263
264 close(fd);
265
266 g_free(aml_file);
267 }
268}
269
270static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
271{
272 AcpiSdtTable *temp;
273 GError *error = NULL;
274 GString *command_line = g_string_new(iasl);
275 gint fd;
276 gchar *out, *out_err;
277 gboolean ret;
278 int i;
279
280 fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
281 g_assert_no_error(error);
282 close(fd);
283
284
285 g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
286 if (compare_signature(sdt, "DSDT") ||
287 compare_signature(sdt, "SSDT")) {
288 for (i = 0; i < sdts->len; ++i) {
289 temp = &g_array_index(sdts, AcpiSdtTable, i);
290 if (compare_signature(temp, "DSDT") ||
291 compare_signature(temp, "SSDT")) {
292 g_string_append_printf(command_line, "-e %s ", temp->aml_file);
293 }
294 }
295 }
296 g_string_append_printf(command_line, "-d %s", sdt->aml_file);
297
298
299 ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
300 g_assert_no_error(error);
301 if (ret) {
302 ret = g_file_get_contents(sdt->asl_file, &sdt->asl,
303 &sdt->asl_len, &error);
304 g_assert(ret);
305 g_assert_no_error(error);
306 ret = (sdt->asl_len > 0);
307 }
308
309 g_free(out);
310 g_free(out_err);
311 g_string_free(command_line, true);
312
313 return !ret;
314}
315
316#define COMMENT_END "*/"
317#define DEF_BLOCK "DefinitionBlock ("
318#define BLOCK_NAME_END ","
319
320static GString *normalize_asl(gchar *asl_code)
321{
322 GString *asl = g_string_new(asl_code);
323 gchar *comment, *block_name;
324
325
326 comment = g_strstr_len(asl->str, asl->len, COMMENT_END);
327 if (comment) {
328 comment += strlen(COMMENT_END);
329 while (*comment == '\n') {
330 comment++;
331 }
332 asl = g_string_erase(asl, 0, comment - asl->str);
333 }
334
335
336 if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
337 block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
338 g_assert(block_name);
339 asl = g_string_erase(asl, 0,
340 block_name + sizeof(BLOCK_NAME_END) - asl->str);
341 }
342
343 return asl;
344}
345
346static GArray *load_expected_aml(test_data *data)
347{
348 int i;
349 AcpiSdtTable *sdt;
350 GError *error = NULL;
351 gboolean ret;
352 gsize aml_len;
353
354 GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
355 if (getenv("V")) {
356 fputc('\n', stderr);
357 }
358 for (i = 0; i < data->tables->len; ++i) {
359 AcpiSdtTable exp_sdt;
360 gchar *aml_file = NULL;
361 const char *ext = data->variant ? data->variant : "";
362
363 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
364
365 memset(&exp_sdt, 0, sizeof(exp_sdt));
366
367try_again:
368 aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
369 sdt->aml, ext);
370 if (getenv("V")) {
371 fprintf(stderr, "Looking for expected file '%s'\n", aml_file);
372 }
373 if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) {
374 exp_sdt.aml_file = aml_file;
375 } else if (*ext != '\0') {
376
377 ext = "";
378 g_free(aml_file);
379 goto try_again;
380 }
381 g_assert(exp_sdt.aml_file);
382 if (getenv("V")) {
383 fprintf(stderr, "Using expected file '%s'\n", aml_file);
384 }
385 ret = g_file_get_contents(aml_file, (gchar **)&exp_sdt.aml,
386 &aml_len, &error);
387 exp_sdt.aml_len = aml_len;
388 g_assert(ret);
389 g_assert_no_error(error);
390 g_assert(exp_sdt.aml);
391 if (!exp_sdt.aml_len) {
392 fprintf(stderr, "Warning! zero length expected file '%s'\n",
393 aml_file);
394 }
395
396 g_array_append_val(exp_tables, exp_sdt);
397 }
398
399 return exp_tables;
400}
401
402static bool test_acpi_find_diff_allowed(AcpiSdtTable *sdt)
403{
404 const gchar *allowed_diff_file[] = {
405#include "bios-tables-test-allowed-diff.h"
406 NULL
407 };
408 const gchar **f;
409
410 for (f = allowed_diff_file; *f; ++f) {
411 if (!g_strcmp0(sdt->aml_file, *f)) {
412 return true;
413 }
414 }
415 return false;
416}
417
418
419static void test_acpi_asl(test_data *data)
420{
421 int i;
422 AcpiSdtTable *sdt, *exp_sdt;
423 test_data exp_data;
424 gboolean exp_err, err, all_tables_match = true;
425
426 memset(&exp_data, 0, sizeof(exp_data));
427 exp_data.tables = load_expected_aml(data);
428 dump_aml_files(data, false);
429 for (i = 0; i < data->tables->len; ++i) {
430 GString *asl, *exp_asl;
431
432 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
433 exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
434
435 if (sdt->aml_len == exp_sdt->aml_len &&
436 !memcmp(sdt->aml, exp_sdt->aml, sdt->aml_len)) {
437
438 continue;
439 }
440
441 fprintf(stderr,
442 "acpi-test: Warning! %.4s binary file mismatch. "
443 "Actual [aml:%s], Expected [aml:%s].\n"
444 "See source file tests/qtest/bios-tables-test.c "
445 "for instructions on how to update expected files.\n",
446 exp_sdt->aml, sdt->aml_file, exp_sdt->aml_file);
447
448 all_tables_match = all_tables_match &&
449 test_acpi_find_diff_allowed(exp_sdt);
450
451
452
453
454
455 if (!iasl) {
456 continue;
457 }
458
459 err = load_asl(data->tables, sdt);
460 asl = normalize_asl(sdt->asl);
461
462 exp_err = load_asl(exp_data.tables, exp_sdt);
463 exp_asl = normalize_asl(exp_sdt->asl);
464
465
466 g_assert(!err || exp_err);
467
468 if (g_strcmp0(asl->str, exp_asl->str)) {
469 sdt->tmp_files_retain = true;
470 if (exp_err) {
471 fprintf(stderr,
472 "Warning! iasl couldn't parse the expected aml\n");
473 } else {
474 exp_sdt->tmp_files_retain = true;
475 fprintf(stderr,
476 "acpi-test: Warning! %.4s mismatch. "
477 "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
478 exp_sdt->aml, sdt->asl_file, sdt->aml_file,
479 exp_sdt->asl_file, exp_sdt->aml_file);
480 fflush(stderr);
481 if (getenv("V")) {
482 const char *diff_env = getenv("DIFF");
483 const char *diff_cmd = diff_env ? diff_env : "diff -U 16";
484 char *diff = g_strdup_printf("%s %s %s", diff_cmd,
485 exp_sdt->asl_file, sdt->asl_file);
486 int out = dup(STDOUT_FILENO);
487 int ret G_GNUC_UNUSED;
488
489 dup2(STDERR_FILENO, STDOUT_FILENO);
490 ret = system(diff) ;
491 dup2(out, STDOUT_FILENO);
492 close(out);
493 g_free(diff);
494 }
495 }
496 }
497 g_string_free(asl, true);
498 g_string_free(exp_asl, true);
499 }
500 if (!iasl && !all_tables_match) {
501 fprintf(stderr, "to see ASL diff between mismatched files install IASL,"
502 " rebuild QEMU from scratch and re-run tests with V=1"
503 " environment variable set");
504 }
505 g_assert(all_tables_match);
506
507 free_test_data(&exp_data);
508}
509
510static bool smbios_ep_table_ok(test_data *data)
511{
512 struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
513 uint32_t addr = data->smbios_ep_addr;
514
515 qtest_memread(data->qts, addr, ep_table, sizeof(*ep_table));
516 if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
517 return false;
518 }
519 if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
520 return false;
521 }
522 if (ep_table->structure_table_length == 0) {
523 return false;
524 }
525 if (ep_table->number_of_structures == 0) {
526 return false;
527 }
528 if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
529 acpi_calc_checksum((uint8_t *)ep_table + 0x10,
530 sizeof *ep_table - 0x10)) {
531 return false;
532 }
533 return true;
534}
535
536static void test_smbios_entry_point(test_data *data)
537{
538 uint32_t off;
539
540
541 for (off = 0xf0000; off < 0x100000; off += 0x10) {
542 uint8_t sig[] = "_SM_";
543 int i;
544
545 for (i = 0; i < sizeof sig - 1; ++i) {
546 sig[i] = qtest_readb(data->qts, off + i);
547 }
548
549 if (!memcmp(sig, "_SM_", sizeof sig)) {
550
551 data->smbios_ep_addr = off;
552 if (smbios_ep_table_ok(data)) {
553 break;
554 }
555 }
556 }
557
558 g_assert_cmphex(off, <, 0x100000);
559}
560
561static inline bool smbios_single_instance(uint8_t type)
562{
563 switch (type) {
564 case 0:
565 case 1:
566 case 2:
567 case 3:
568 case 16:
569 case 32:
570 case 127:
571 return true;
572 default:
573 return false;
574 }
575}
576
577static bool smbios_cpu_test(test_data *data, uint32_t addr)
578{
579 uint16_t expect_speed[2];
580 uint16_t real;
581 int offset[2];
582 int i;
583
584
585 offset[0] = offsetof(struct smbios_type_4, max_speed);
586 offset[1] = offsetof(struct smbios_type_4, current_speed);
587 expect_speed[0] = data->smbios_cpu_max_speed ? : 2000;
588 expect_speed[1] = data->smbios_cpu_curr_speed ? : 2000;
589
590 for (i = 0; i < 2; i++) {
591 real = qtest_readw(data->qts, addr + offset[i]);
592 if (real != expect_speed[i]) {
593 fprintf(stderr, "Unexpected SMBIOS CPU speed: real %u expect %u\n",
594 real, expect_speed[i]);
595 return false;
596 }
597 }
598
599 return true;
600}
601
602static void test_smbios_structs(test_data *data)
603{
604 DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };
605 struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
606 uint32_t addr = le32_to_cpu(ep_table->structure_table_address);
607 int i, len, max_len = 0;
608 uint8_t type, prv, crt;
609
610
611 for (i = 0; i < le16_to_cpu(ep_table->number_of_structures); i++) {
612
613
614 type = qtest_readb(data->qts, addr);
615 g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
616 len = qtest_readb(data->qts, addr + 1);
617
618
619 if (smbios_single_instance(type)) {
620 g_assert(!test_bit(type, struct_bitmap));
621 }
622 set_bit(type, struct_bitmap);
623
624 if (type == 4) {
625 g_assert(smbios_cpu_test(data, addr));
626 }
627
628
629 prv = crt = 1;
630 while (prv || crt) {
631 prv = crt;
632 crt = qtest_readb(data->qts, addr + len);
633 len++;
634 }
635
636
637 if (max_len < len) {
638 max_len = len;
639 g_assert_cmpuint(max_len, <=, ep_table->max_structure_size);
640 }
641
642
643 addr += len;
644 }
645
646
647 g_assert_cmpuint(le16_to_cpu(ep_table->structure_table_length), ==,
648 addr - le32_to_cpu(ep_table->structure_table_address));
649 g_assert_cmpuint(le16_to_cpu(ep_table->max_structure_size), ==, max_len);
650
651
652 for (i = 0; i < data->required_struct_types_len; i++) {
653 g_assert(test_bit(data->required_struct_types[i], struct_bitmap));
654 }
655}
656
657static void test_acpi_one(const char *params, test_data *data)
658{
659 char *args;
660 bool use_uefi = data->uefi_fl1 && data->uefi_fl2;
661
662#ifndef CONFIG_TCG
663 if (data->tcg_only) {
664 g_test_skip("TCG disabled, skipping ACPI tcg_only test");
665 return;
666 }
667#endif
668
669 if (use_uefi) {
670
671
672
673
674 args = g_strdup_printf("-machine %s %s -accel tcg -nodefaults -nographic "
675 "-drive if=pflash,format=raw,file=%s,readonly "
676 "-drive if=pflash,format=raw,file=%s,snapshot=on -cdrom %s %s",
677 data->machine, data->tcg_only ? "" : "-accel kvm",
678 data->uefi_fl1, data->uefi_fl2, data->cd, params ? params : "");
679
680 } else {
681 args = g_strdup_printf("-machine %s %s -accel tcg "
682 "-net none -display none %s "
683 "-drive id=hd0,if=none,file=%s,format=raw "
684 "-device %s,drive=hd0 ",
685 data->machine, data->tcg_only ? "" : "-accel kvm",
686 params ? params : "", disk,
687 data->blkdev ?: "ide-hd");
688 }
689
690 data->qts = qtest_init(args);
691
692 if (use_uefi) {
693 g_assert(data->scan_len);
694 data->rsdp_addr = acpi_find_rsdp_address_uefi(data->qts,
695 data->ram_start, data->scan_len);
696 } else {
697 boot_sector_test(data->qts);
698 data->rsdp_addr = acpi_find_rsdp_address(data->qts);
699 g_assert_cmphex(data->rsdp_addr, <, 0x100000);
700 }
701
702 data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
703 test_acpi_rsdp_table(data);
704 test_acpi_rxsdt_table(data);
705 test_acpi_fadt_table(data);
706
707 if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
708 dump_aml_files(data, true);
709 } else {
710 test_acpi_asl(data);
711 }
712
713
714
715
716
717
718 if (!use_uefi) {
719 test_smbios_entry_point(data);
720 test_smbios_structs(data);
721 }
722
723 qtest_quit(data->qts);
724 g_free(args);
725}
726
727static uint8_t base_required_struct_types[] = {
728 0, 1, 3, 4, 16, 17, 19, 32, 127
729};
730
731static void test_acpi_piix4_tcg(void)
732{
733 test_data data;
734
735
736
737
738 memset(&data, 0, sizeof(data));
739 data.machine = MACHINE_PC;
740 data.required_struct_types = base_required_struct_types;
741 data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
742 test_acpi_one(NULL, &data);
743 free_test_data(&data);
744}
745
746static void test_acpi_piix4_tcg_bridge(void)
747{
748 test_data data;
749
750 memset(&data, 0, sizeof(data));
751 data.machine = MACHINE_PC;
752 data.variant = ".bridge";
753 data.required_struct_types = base_required_struct_types;
754 data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
755 test_acpi_one("-device pci-bridge,chassis_nr=1", &data);
756 free_test_data(&data);
757}
758
759static void test_acpi_piix4_no_root_hotplug(void)
760{
761 test_data data;
762
763 memset(&data, 0, sizeof(data));
764 data.machine = MACHINE_PC;
765 data.variant = ".roothp";
766 data.required_struct_types = base_required_struct_types;
767 data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
768 test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
769 "-device pci-bridge,chassis_nr=1", &data);
770 free_test_data(&data);
771}
772
773static void test_acpi_piix4_no_bridge_hotplug(void)
774{
775 test_data data;
776
777 memset(&data, 0, sizeof(data));
778 data.machine = MACHINE_PC;
779 data.variant = ".hpbridge";
780 data.required_struct_types = base_required_struct_types;
781 data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
782 test_acpi_one("-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
783 "-device pci-bridge,chassis_nr=1", &data);
784 free_test_data(&data);
785}
786
787static void test_acpi_piix4_no_acpi_pci_hotplug(void)
788{
789 test_data data;
790
791 memset(&data, 0, sizeof(data));
792 data.machine = MACHINE_PC;
793 data.variant = ".hpbrroot";
794 data.required_struct_types = base_required_struct_types;
795 data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
796 test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
797 "-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
798 "-device pci-bridge,chassis_nr=1", &data);
799 free_test_data(&data);
800}
801
802static void test_acpi_q35_tcg(void)
803{
804 test_data data;
805
806 memset(&data, 0, sizeof(data));
807 data.machine = MACHINE_Q35;
808 data.required_struct_types = base_required_struct_types;
809 data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
810 test_acpi_one(NULL, &data);
811 free_test_data(&data);
812
813 data.smbios_cpu_max_speed = 3000;
814 data.smbios_cpu_curr_speed = 2600;
815 test_acpi_one("-smbios type=4,max-speed=3000,current-speed=2600", &data);
816 free_test_data(&data);
817}
818
819static void test_acpi_q35_tcg_bridge(void)
820{
821 test_data data;
822
823 memset(&data, 0, sizeof(data));
824 data.machine = MACHINE_Q35;
825 data.variant = ".bridge";
826 data.required_struct_types = base_required_struct_types;
827 data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
828 test_acpi_one("-device pci-bridge,chassis_nr=1",
829 &data);
830 free_test_data(&data);
831}
832
833static void test_acpi_q35_tcg_mmio64(void)
834{
835 test_data data = {
836 .machine = MACHINE_Q35,
837 .variant = ".mmio64",
838 .required_struct_types = base_required_struct_types,
839 .required_struct_types_len = ARRAY_SIZE(base_required_struct_types)
840 };
841
842 test_acpi_one("-m 128M,slots=1,maxmem=2G "
843 "-object memory-backend-ram,id=ram0,size=128M "
844 "-numa node,memdev=ram0 "
845 "-device pci-testdev,membar=2G",
846 &data);
847 free_test_data(&data);
848}
849
850static void test_acpi_piix4_tcg_cphp(void)
851{
852 test_data data;
853
854 memset(&data, 0, sizeof(data));
855 data.machine = MACHINE_PC;
856 data.variant = ".cphp";
857 test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
858 " -object memory-backend-ram,id=ram0,size=64M"
859 " -object memory-backend-ram,id=ram1,size=64M"
860 " -numa node,memdev=ram0 -numa node,memdev=ram1"
861 " -numa dist,src=0,dst=1,val=21",
862 &data);
863 free_test_data(&data);
864}
865
866static void test_acpi_q35_tcg_cphp(void)
867{
868 test_data data;
869
870 memset(&data, 0, sizeof(data));
871 data.machine = MACHINE_Q35;
872 data.variant = ".cphp";
873 test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
874 " -object memory-backend-ram,id=ram0,size=64M"
875 " -object memory-backend-ram,id=ram1,size=64M"
876 " -numa node,memdev=ram0 -numa node,memdev=ram1"
877 " -numa dist,src=0,dst=1,val=21",
878 &data);
879 free_test_data(&data);
880}
881
882static uint8_t ipmi_required_struct_types[] = {
883 0, 1, 3, 4, 16, 17, 19, 32, 38, 127
884};
885
886static void test_acpi_q35_tcg_ipmi(void)
887{
888 test_data data;
889
890 memset(&data, 0, sizeof(data));
891 data.machine = MACHINE_Q35;
892 data.variant = ".ipmibt";
893 data.required_struct_types = ipmi_required_struct_types;
894 data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
895 test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
896 " -device isa-ipmi-bt,bmc=bmc0",
897 &data);
898 free_test_data(&data);
899}
900
901static void test_acpi_piix4_tcg_ipmi(void)
902{
903 test_data data;
904
905
906
907
908 memset(&data, 0, sizeof(data));
909 data.machine = MACHINE_PC;
910 data.variant = ".ipmikcs";
911 data.required_struct_types = ipmi_required_struct_types;
912 data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
913 test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
914 " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
915 &data);
916 free_test_data(&data);
917}
918
919static void test_acpi_q35_tcg_memhp(void)
920{
921 test_data data;
922
923 memset(&data, 0, sizeof(data));
924 data.machine = MACHINE_Q35;
925 data.variant = ".memhp";
926 test_acpi_one(" -m 128,slots=3,maxmem=1G"
927 " -object memory-backend-ram,id=ram0,size=64M"
928 " -object memory-backend-ram,id=ram1,size=64M"
929 " -numa node,memdev=ram0 -numa node,memdev=ram1"
930 " -numa dist,src=0,dst=1,val=21",
931 &data);
932 free_test_data(&data);
933}
934
935static void test_acpi_piix4_tcg_memhp(void)
936{
937 test_data data;
938
939 memset(&data, 0, sizeof(data));
940 data.machine = MACHINE_PC;
941 data.variant = ".memhp";
942 test_acpi_one(" -m 128,slots=3,maxmem=1G"
943 " -object memory-backend-ram,id=ram0,size=64M"
944 " -object memory-backend-ram,id=ram1,size=64M"
945 " -numa node,memdev=ram0 -numa node,memdev=ram1"
946 " -numa dist,src=0,dst=1,val=21",
947 &data);
948 free_test_data(&data);
949}
950
951static void test_acpi_q35_tcg_numamem(void)
952{
953 test_data data;
954
955 memset(&data, 0, sizeof(data));
956 data.machine = MACHINE_Q35;
957 data.variant = ".numamem";
958 test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
959 " -numa node -numa node,memdev=ram0", &data);
960 free_test_data(&data);
961}
962
963static void test_acpi_piix4_tcg_numamem(void)
964{
965 test_data data;
966
967 memset(&data, 0, sizeof(data));
968 data.machine = MACHINE_PC;
969 data.variant = ".numamem";
970 test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
971 " -numa node -numa node,memdev=ram0", &data);
972 free_test_data(&data);
973}
974
975uint64_t tpm_tis_base_addr;
976
977static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
978 uint64_t base)
979{
980#ifdef CONFIG_TPM
981 gchar *tmp_dir_name = g_strdup_printf("qemu-test_acpi_%s_tcg_%s.XXXXXX",
982 machine, tpm_if);
983 char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
984 TestState test;
985 test_data data;
986 GThread *thread;
987 char *args, *variant = g_strdup_printf(".%s", tpm_if);
988
989 tpm_tis_base_addr = base;
990
991 module_call_init(MODULE_INIT_QOM);
992
993 test.addr = g_new0(SocketAddress, 1);
994 test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
995 test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
996 g_mutex_init(&test.data_mutex);
997 g_cond_init(&test.data_cond);
998 test.data_cond_signal = false;
999
1000 thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
1001 tpm_emu_test_wait_cond(&test);
1002
1003 memset(&data, 0, sizeof(data));
1004 data.machine = machine;
1005 data.variant = variant;
1006
1007 args = g_strdup_printf(
1008 " -chardev socket,id=chr,path=%s"
1009 " -tpmdev emulator,id=dev,chardev=chr"
1010 " -device tpm-%s,tpmdev=dev",
1011 test.addr->u.q_unix.path, tpm_if);
1012
1013 test_acpi_one(args, &data);
1014
1015 g_thread_join(thread);
1016 g_unlink(test.addr->u.q_unix.path);
1017 qapi_free_SocketAddress(test.addr);
1018 g_rmdir(tmp_path);
1019 g_free(variant);
1020 g_free(tmp_path);
1021 g_free(tmp_dir_name);
1022 g_free(args);
1023 free_test_data(&data);
1024#else
1025 g_test_skip("TPM disabled");
1026#endif
1027}
1028
1029static void test_acpi_q35_tcg_tpm_tis(void)
1030{
1031 test_acpi_tcg_tpm("q35", "tis", 0xFED40000);
1032}
1033
1034static void test_acpi_tcg_dimm_pxm(const char *machine)
1035{
1036 test_data data;
1037
1038 memset(&data, 0, sizeof(data));
1039 data.machine = machine;
1040 data.variant = ".dimmpxm";
1041 test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
1042 " -smp 4,sockets=4"
1043 " -m 128M,slots=3,maxmem=1G"
1044 " -object memory-backend-ram,id=ram0,size=32M"
1045 " -object memory-backend-ram,id=ram1,size=32M"
1046 " -object memory-backend-ram,id=ram2,size=32M"
1047 " -object memory-backend-ram,id=ram3,size=32M"
1048 " -numa node,memdev=ram0,nodeid=0"
1049 " -numa node,memdev=ram1,nodeid=1"
1050 " -numa node,memdev=ram2,nodeid=2"
1051 " -numa node,memdev=ram3,nodeid=3"
1052 " -numa cpu,node-id=0,socket-id=0"
1053 " -numa cpu,node-id=1,socket-id=1"
1054 " -numa cpu,node-id=2,socket-id=2"
1055 " -numa cpu,node-id=3,socket-id=3"
1056 " -object memory-backend-ram,id=ram4,size=128M"
1057 " -object memory-backend-ram,id=nvm0,size=128M"
1058 " -device pc-dimm,id=dimm0,memdev=ram4,node=1"
1059 " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
1060 &data);
1061 free_test_data(&data);
1062}
1063
1064static void test_acpi_q35_tcg_dimm_pxm(void)
1065{
1066 test_acpi_tcg_dimm_pxm(MACHINE_Q35);
1067}
1068
1069static void test_acpi_piix4_tcg_dimm_pxm(void)
1070{
1071 test_acpi_tcg_dimm_pxm(MACHINE_PC);
1072}
1073
1074static void test_acpi_virt_tcg_memhp(void)
1075{
1076 test_data data = {
1077 .machine = "virt",
1078 .tcg_only = true,
1079 .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1080 .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1081 .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1082 .ram_start = 0x40000000ULL,
1083 .scan_len = 256ULL * 1024 * 1024,
1084 };
1085
1086 data.variant = ".memhp";
1087 test_acpi_one(" -machine nvdimm=on"
1088 " -cpu cortex-a57"
1089 " -m 256M,slots=3,maxmem=1G"
1090 " -object memory-backend-ram,id=ram0,size=128M"
1091 " -object memory-backend-ram,id=ram1,size=128M"
1092 " -numa node,memdev=ram0 -numa node,memdev=ram1"
1093 " -numa dist,src=0,dst=1,val=21"
1094 " -object memory-backend-ram,id=ram2,size=128M"
1095 " -object memory-backend-ram,id=nvm0,size=128M"
1096 " -device pc-dimm,id=dimm0,memdev=ram2,node=0"
1097 " -device nvdimm,id=dimm1,memdev=nvm0,node=1",
1098 &data);
1099
1100 free_test_data(&data);
1101
1102}
1103
1104static void test_acpi_microvm_prepare(test_data *data)
1105{
1106 memset(data, 0, sizeof(*data));
1107 data->machine = "microvm";
1108 data->required_struct_types = NULL;
1109 data->required_struct_types_len = 0;
1110 data->blkdev = "virtio-blk-device";
1111}
1112
1113static void test_acpi_microvm_tcg(void)
1114{
1115 test_data data;
1116
1117 test_acpi_microvm_prepare(&data);
1118 test_acpi_one(" -machine microvm,acpi=on,rtc=off",
1119 &data);
1120 free_test_data(&data);
1121}
1122
1123static void test_acpi_microvm_usb_tcg(void)
1124{
1125 test_data data;
1126
1127 test_acpi_microvm_prepare(&data);
1128 data.variant = ".usb";
1129 test_acpi_one(" -machine microvm,acpi=on,usb=on,rtc=off",
1130 &data);
1131 free_test_data(&data);
1132}
1133
1134static void test_acpi_microvm_rtc_tcg(void)
1135{
1136 test_data data;
1137
1138 test_acpi_microvm_prepare(&data);
1139 data.variant = ".rtc";
1140 test_acpi_one(" -machine microvm,acpi=on,rtc=on",
1141 &data);
1142 free_test_data(&data);
1143}
1144
1145static void test_acpi_microvm_pcie_tcg(void)
1146{
1147 test_data data;
1148
1149 test_acpi_microvm_prepare(&data);
1150 data.variant = ".pcie";
1151 data.tcg_only = true;
1152 test_acpi_one(" -machine microvm,acpi=on,rtc=off,pcie=on",
1153 &data);
1154 free_test_data(&data);
1155}
1156
1157static void test_acpi_virt_tcg_numamem(void)
1158{
1159 test_data data = {
1160 .machine = "virt",
1161 .tcg_only = true,
1162 .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1163 .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1164 .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1165 .ram_start = 0x40000000ULL,
1166 .scan_len = 128ULL * 1024 * 1024,
1167 };
1168
1169 data.variant = ".numamem";
1170 test_acpi_one(" -cpu cortex-a57"
1171 " -object memory-backend-ram,id=ram0,size=128M"
1172 " -numa node,memdev=ram0",
1173 &data);
1174
1175 free_test_data(&data);
1176
1177}
1178
1179static void test_acpi_tcg_acpi_hmat(const char *machine)
1180{
1181 test_data data;
1182
1183 memset(&data, 0, sizeof(data));
1184 data.machine = machine;
1185 data.variant = ".acpihmat";
1186 test_acpi_one(" -machine hmat=on"
1187 " -smp 2,sockets=2"
1188 " -m 128M,slots=2,maxmem=1G"
1189 " -object memory-backend-ram,size=64M,id=m0"
1190 " -object memory-backend-ram,size=64M,id=m1"
1191 " -numa node,nodeid=0,memdev=m0"
1192 " -numa node,nodeid=1,memdev=m1,initiator=0"
1193 " -numa cpu,node-id=0,socket-id=0"
1194 " -numa cpu,node-id=0,socket-id=1"
1195 " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1196 "data-type=access-latency,latency=1"
1197 " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
1198 "data-type=access-bandwidth,bandwidth=65534M"
1199 " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1200 "data-type=access-latency,latency=65534"
1201 " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
1202 "data-type=access-bandwidth,bandwidth=32767M"
1203 " -numa hmat-cache,node-id=0,size=10K,level=1,"
1204 "associativity=direct,policy=write-back,line=8"
1205 " -numa hmat-cache,node-id=1,size=10K,level=1,"
1206 "associativity=direct,policy=write-back,line=8",
1207 &data);
1208 free_test_data(&data);
1209}
1210
1211static void test_acpi_q35_tcg_acpi_hmat(void)
1212{
1213 test_acpi_tcg_acpi_hmat(MACHINE_Q35);
1214}
1215
1216static void test_acpi_piix4_tcg_acpi_hmat(void)
1217{
1218 test_acpi_tcg_acpi_hmat(MACHINE_PC);
1219}
1220
1221static void test_acpi_virt_tcg(void)
1222{
1223 test_data data = {
1224 .machine = "virt",
1225 .tcg_only = true,
1226 .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
1227 .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
1228 .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
1229 .ram_start = 0x40000000ULL,
1230 .scan_len = 128ULL * 1024 * 1024,
1231 };
1232
1233 test_acpi_one("-cpu cortex-a57", &data);
1234 free_test_data(&data);
1235
1236 data.smbios_cpu_max_speed = 2900;
1237 data.smbios_cpu_curr_speed = 2700;
1238 test_acpi_one("-cpu cortex-a57 "
1239 "-smbios type=4,max-speed=2900,current-speed=2700", &data);
1240 free_test_data(&data);
1241}
1242
1243int main(int argc, char *argv[])
1244{
1245 const char *arch = qtest_get_arch();
1246 int ret;
1247
1248 g_test_init(&argc, &argv, NULL);
1249
1250 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
1251 ret = boot_sector_init(disk);
1252 if (ret) {
1253 return ret;
1254 }
1255
1256 qtest_add_func("acpi/q35/tpm-tis", test_acpi_q35_tcg_tpm_tis);
1257 qtest_add_func("acpi/piix4", test_acpi_piix4_tcg);
1258 qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge);
1259 qtest_add_func("acpi/piix4/pci-hotplug/no_root_hotplug",
1260 test_acpi_piix4_no_root_hotplug);
1261 qtest_add_func("acpi/piix4/pci-hotplug/no_bridge_hotplug",
1262 test_acpi_piix4_no_bridge_hotplug);
1263 qtest_add_func("acpi/piix4/pci-hotplug/off",
1264 test_acpi_piix4_no_acpi_pci_hotplug);
1265 qtest_add_func("acpi/q35", test_acpi_q35_tcg);
1266 qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
1267 qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64);
1268 qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi);
1269 qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi);
1270 qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp);
1271 qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp);
1272 qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
1273 qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
1274 qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
1275 qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
1276 qtest_add_func("acpi/piix4/dimmpxm", test_acpi_piix4_tcg_dimm_pxm);
1277 qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm);
1278 qtest_add_func("acpi/piix4/acpihmat", test_acpi_piix4_tcg_acpi_hmat);
1279 qtest_add_func("acpi/q35/acpihmat", test_acpi_q35_tcg_acpi_hmat);
1280 qtest_add_func("acpi/microvm", test_acpi_microvm_tcg);
1281 qtest_add_func("acpi/microvm/usb", test_acpi_microvm_usb_tcg);
1282 qtest_add_func("acpi/microvm/rtc", test_acpi_microvm_rtc_tcg);
1283 if (strcmp(arch, "x86_64") == 0) {
1284 qtest_add_func("acpi/microvm/pcie", test_acpi_microvm_pcie_tcg);
1285 }
1286 } else if (strcmp(arch, "aarch64") == 0) {
1287 qtest_add_func("acpi/virt", test_acpi_virt_tcg);
1288 qtest_add_func("acpi/virt/numamem", test_acpi_virt_tcg_numamem);
1289 qtest_add_func("acpi/virt/memhp", test_acpi_virt_tcg_memhp);
1290 }
1291 ret = g_test_run();
1292 boot_sector_cleanup(disk);
1293 return ret;
1294}
1295