aboutsummaryrefslogtreecommitdiff
path: root/tests/qtest/bios-tables-test.c
blob: 159e4edb8f2993a9a14e0f86c542d233540e8ca2 (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
/*
 * Boot order test cases.
 *
 * Copyright (c) 2013 Red Hat Inc.
 *
 * Authors:
 *  Michael S. Tsirkin <mst@redhat.com>,
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

/*
 * How to add or update the tests or commit changes that affect ACPI tables:
 * Contributor:
 * 1. add empty files for new tables, if any, under tests/data/acpi
 * 2. list any changed files in tests/qtest/bios-tables-test-allowed-diff.h
 * 3. commit the above *before* making changes that affect the tables
 *
 * Contributor or ACPI Maintainer (steps 4-7 need to be redone to resolve conflicts
 * in binary commit created in step 6):
 *
 * After 1-3 above tests will pass but ignore differences with the expected files.
 * You will also notice that tests/qtest/bios-tables-test-allowed-diff.h lists
 * a bunch of files. This is your hint that you need to do the below:
 * 4. Run
 *      make check V=2
 * this will produce a bunch of warnings about differences
 * beween actual and expected ACPI tables. If you have IASL installed,
 * they will also be disassembled so you can look at the disassembled
 * output. If not - disassemble them yourself in any way you like.
 * Look at the differences - make sure they make sense and match what the
 * changes you are merging are supposed to do.
 * Save the changes, preferably in form of ASL diff for the commit log in
 * step 6.
 *
 * 5. From build directory, run:
 *      $(SRC_PATH)/tests/data/acpi/rebuild-expected-aml.sh
 * 6. Now commit any changes to the expected binary, include diff from step 4
 *    in commit log.
 *    Expected binary updates needs to be a separate patch from the code that
 *    introduces changes to ACPI tables. It lets the maintainer drop
 *    and regenerate binary updates in case of merge conflicts. Further, a code
 *    change is easily reviewable but a binary blob is not (without doing a
 *    disassembly).
 * 7. Before sending patches to the list (Contributor)
 *    or before doing a pull request (Maintainer), make sure
 *    tests/qtest/bios-tables-test-allowed-diff.h is empty - this will ensure
 *    following changes to ACPI tables will be noticed.
 *
 * The resulting patchset/pull request then looks like this:
 * - patch 1: list changed files in tests/qtest/bios-tables-test-allowed-diff.h.
 * - patches 2 - n: real changes, may contain multiple patches.
 * - patch n + 1: update golden master binaries and empty
 *   tests/qtest/bios-tables-test-allowed-diff.h
 */

#include "qemu/osdep.h"
#include <glib/gstdio.h>
#include "hw/firmware/smbios.h"
#include "qemu/bitmap.h"
#include "acpi-utils.h"
#include "boot-sector.h"
#include "tpm-emu.h"
#include "hw/acpi/tpm.h"
#include "qemu/cutils.h"

#define MACHINE_PC "pc"
#define MACHINE_Q35 "q35"

#define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"

#define OEM_ID             "TEST"
#define OEM_TABLE_ID       "OEM"
#define OEM_TEST_ARGS      "-machine x-oem-id=" OEM_ID ",x-oem-table-id=" \
                           OEM_TABLE_ID

typedef struct {
    bool tcg_only;
    const char *machine;
    const char *machine_param;
    const char *variant;
    const char *uefi_fl1;
    const char *uefi_fl2;
    const char *blkdev;
    const char *cd;
    const uint64_t ram_start;
    const uint64_t scan_len;
    uint64_t rsdp_addr;
    uint8_t rsdp_table[36 /* ACPI 2.0+ RSDP size */];
    GArray *tables;
    uint64_t smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE__MAX];
    SmbiosEntryPoint smbios_ep_table;
    uint16_t smbios_cpu_max_speed;
    uint16_t smbios_cpu_curr_speed;
    uint8_t smbios_core_count;
    uint16_t smbios_core_count2;
    uint8_t *required_struct_types;
    int required_struct_types_len;
    QTestState *qts;
} test_data;

static char disk[] = "tests/acpi-test-disk-XXXXXX";
static const char *data_dir = "tests/data/acpi";
#ifdef CONFIG_IASL
static const char *iasl = CONFIG_IASL;
#else
static const char *iasl;
#endif

static int verbosity_level;

static bool compare_signature(const AcpiSdtTable *sdt, const char *signature)
{
   return !memcmp(sdt->aml, signature, 4);
}

static void cleanup_table_descriptor(AcpiSdtTable *table)
{
    g_free(table->aml);
    if (table->aml_file &&
        !table->tmp_files_retain &&
        g_strstr_len(table->aml_file, -1, "aml-")) {
        unlink(table->aml_file);
    }
    g_free(table->aml_file);
    g_free(table->asl);
    if (table->asl_file &&
        !table->tmp_files_retain) {
        unlink(table->asl_file);
    }
    g_free(table->asl_file);
}

static void free_test_data(test_data *data)
{
    int i;

    if (!data->tables) {
        return;
    }
    for (i = 0; i < data->tables->len; ++i) {
        cleanup_table_descriptor(&g_array_index(data->tables, AcpiSdtTable, i));
    }

    g_array_free(data->tables, true);
}

static void test_acpi_rsdp_table(test_data *data)
{
    uint8_t *rsdp_table = data->rsdp_table;

    acpi_fetch_rsdp_table(data->qts, data->rsdp_addr, rsdp_table);

    switch (rsdp_table[15 /* Revision offset */]) {
    case 0: /* ACPI 1.0 RSDP */
        /* With rev 1, checksum is only for the first 20 bytes */
        g_assert(!acpi_calc_checksum(rsdp_table,  20));
        break;
    case 2: /* ACPI 2.0+ RSDP */
        /* With revision 2, we have 2 checksums */
        g_assert(!acpi_calc_checksum(rsdp_table, 20));
        g_assert(!acpi_calc_checksum(rsdp_table, 36));
        break;
    default:
        g_assert_not_reached();
    }
}

static void test_acpi_rxsdt_table(test_data *data)
{
    const char *sig = "RSDT";
    AcpiSdtTable rsdt = {};
    int entry_size = 4;
    int addr_off = 16 /* RsdtAddress */;
    uint8_t *ent;

    if (data->rsdp_table[15 /* Revision offset */] != 0) {
        addr_off = 24 /* XsdtAddress */;
        entry_size = 8;
        sig = "XSDT";
    }
    /* read [RX]SDT table */
    acpi_fetch_table(data->qts, &rsdt.aml, &rsdt.aml_len,
                     &data->rsdp_table[addr_off], entry_size, sig, true);

    /* Load all tables and add to test list directly RSDT referenced tables */
    ACPI_FOREACH_RSDT_ENTRY(rsdt.aml, rsdt.aml_len, ent, entry_size) {
        AcpiSdtTable ssdt_table = {};

        acpi_fetch_table(data->qts, &ssdt_table.aml, &ssdt_table.aml_len, ent,
                         entry_size, NULL, true);
        /* Add table to ASL test tables list */
        g_array_append_val(data->tables, ssdt_table);
    }
    cleanup_table_descriptor(&rsdt);
}

static void test_acpi_fadt_table(test_data *data)
{
    /* FADT table is 1st */
    AcpiSdtTable table = g_array_index(data->tables, typeof(table), 0);
    uint8_t *fadt_aml = table.aml;
    uint32_t fadt_len = table.aml_len;
    uint32_t val;
    int dsdt_offset = 40 /* DSDT */;
    int dsdt_entry_size = 4;

    g_assert(compare_signature(&table, "FACP"));

    /* Since DSDT/FACS isn't in RSDT, add them to ASL test list manually */
    memcpy(&val, fadt_aml + 112 /* Flags */, 4);
    val = le32_to_cpu(val);
    if (!(val & 1UL << 20 /* HW_REDUCED_ACPI */)) {
        acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
                         fadt_aml + 36 /* FIRMWARE_CTRL */, 4, "FACS", false);
        g_array_append_val(data->tables, table);
    }

    memcpy(&val, fadt_aml + dsdt_offset, 4);
    val = le32_to_cpu(val);
    if (!val) {
        dsdt_offset = 140 /* X_DSDT */;
        dsdt_entry_size = 8;
    }
    acpi_fetch_table(data->qts, &table.aml, &table.aml_len,
                     fadt_aml + dsdt_offset, dsdt_entry_size, "DSDT", true);
    g_array_append_val(data->tables, table);

    memset(fadt_aml + 36, 0, 4); /* sanitize FIRMWARE_CTRL ptr */
    memset(fadt_aml + 40, 0, 4); /* sanitize DSDT ptr */
    if (fadt_aml[8 /* FADT Major Version */] >= 3) {
        memset(fadt_aml + 132, 0, 8); /* sanitize X_FIRMWARE_CTRL ptr */
        memset(fadt_aml + 140, 0, 8); /* sanitize X_DSDT ptr */
    }

    /* update checksum */
    fadt_aml[9 /* Checksum */] = 0;
    fadt_aml[9 /* Checksum */] -= acpi_calc_checksum(fadt_aml, fadt_len);
}

static void dump_aml_files(test_data *data, bool rebuild)
{
    AcpiSdtTable *sdt;
    GError *error = NULL;
    gchar *aml_file = NULL;
    gint fd;
    ssize_t ret;
    int i;

    for (i = 0; i < data->tables->len; ++i) {
        const char *ext = data->variant ? data->variant : "";
        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
        g_assert(sdt->aml);

        if (rebuild) {
            aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
                                       sdt->aml, ext);
            fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
                        S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
            if (fd < 0) {
                perror(aml_file);
            }
            g_assert(fd >= 0);
        } else {
            fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
            g_assert_no_error(error);
        }

        ret = qemu_write_full(fd, sdt->aml, sdt->aml_len);
        g_assert(ret == sdt->aml_len);

        close(fd);

        g_free(aml_file);
    }
}

static bool create_tmp_asl(AcpiSdtTable *sdt)
{
    GError *error = NULL;
    gint fd;

    fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
    g_assert_no_error(error);
    close(fd);

    return false;
}

static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
{
    AcpiSdtTable *temp;
    GError *error = NULL;
    GString *command_line = g_string_new(iasl);
    gchar *out, *out_err;
    gboolean ret;
    int i;

    create_tmp_asl(sdt);

    /* build command line */
    g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
    if (compare_signature(sdt, "DSDT") ||
        compare_signature(sdt, "SSDT")) {
        for (i = 0; i < sdts->len; ++i) {
            temp = &g_array_index(sdts, AcpiSdtTable, i);
            if (compare_signature(temp, "DSDT") ||
                compare_signature(temp, "SSDT")) {
                g_string_append_printf(command_line, "-e %s ", temp->aml_file);
            }
        }
    }
    g_string_append_printf(command_line, "-d %s", sdt->aml_file);

    /* pass 'out' and 'out_err' in order to be redirected */
    ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
    g_assert_no_error(error);
    if (ret) {
        ret = g_file_get_contents(sdt->asl_file, &sdt->asl,
                                  &sdt->asl_len, &error);
        g_assert(ret);
        g_assert_no_error(error);
        ret = (sdt->asl_len > 0);
    }

    g_free(out);
    g_free(out_err);
    g_string_free(command_line, true);

    return !ret;
}

#define COMMENT_END "*/"
#define DEF_BLOCK "DefinitionBlock ("
#define BLOCK_NAME_END ","

static GString *normalize_asl(gchar *asl_code)
{
    GString *asl = g_string_new(asl_code);
    gchar *comment, *block_name;

    /* strip comments (different generation days) */
    comment = g_strstr_len(asl->str, asl->len, COMMENT_END);
    if (comment) {
        comment += strlen(COMMENT_END);
        while (*comment == '\n') {
            comment++;
        }
        asl = g_string_erase(asl, 0, comment - asl->str);
    }

    /* strip def block name (it has file path in it) */
    if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
        block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
        g_assert(block_name);
        asl = g_string_erase(asl, 0,
                             block_name + sizeof(BLOCK_NAME_END) - asl->str);
    }

    return asl;
}

static GArray *load_expected_aml(test_data *data)
{
    int i;
    AcpiSdtTable *sdt;
    GError *error = NULL;
    gboolean ret;
    gsize aml_len;

    GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
    if (verbosity_level >= 2) {
        fputc('\n', stderr);
    }
    for (i = 0; i < data->tables->len; ++i) {
        AcpiSdtTable exp_sdt;
        gchar *aml_file = NULL;
        const char *ext = data->variant ? data->variant : "";

        sdt = &g_array_index(data->tables, AcpiSdtTable, i);

        memset(&exp_sdt, 0, sizeof(exp_sdt));

try_again:
        aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
                                   sdt->aml, ext);
        if (verbosity_level >= 2) {
            fprintf(stderr, "Looking for expected file '%s'\n", aml_file);
        }
        if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) {
            exp_sdt.aml_file = aml_file;
        } else if (*ext != '\0') {
            /* try fallback to generic (extension less) expected file */
            ext = "";
            g_free(aml_file);
            goto try_again;
        }
        g_assert(exp_sdt.aml_file);
        if (verbosity_level >= 2) {
            fprintf(stderr, "Using expected file '%s'\n", aml_file);
        }
        ret = g_file_get_contents(aml_file, (gchar **)&exp_sdt.aml,
                                  &aml_len, &error);
        exp_sdt.aml_len = aml_len;
        g_assert(ret);
        g_assert_no_error(error);
        g_assert(exp_sdt.aml);
        if (!exp_sdt.aml_len) {
            fprintf(stderr, "Warning! zero length expected file '%s'\n",
                    aml_file);
        }

        g_array_append_val(exp_tables, exp_sdt);
    }

    return exp_tables;
}

static bool test_acpi_find_diff_allowed(AcpiSdtTable *sdt)
{
    const gchar *allowed_diff_file[] = {
#include "bios-tables-test-allowed-diff.h"
        NULL
    };
    const gchar **f;

    for (f = allowed_diff_file; *f; ++f) {
        if (!g_strcmp0(sdt->aml_file, *f)) {
            return true;
        }
    }
    return false;
}

/* test the list of tables in @data->tables against reference tables */
static void test_acpi_asl(test_data *data)
{
    int i;
    AcpiSdtTable *sdt, *exp_sdt;
    test_data exp_data = {};
    gboolean exp_err, err, all_tables_match = true;

    exp_data.tables = load_expected_aml(data);
    dump_aml_files(data, false);
    for (i = 0; i < data->tables->len; ++i) {
        GString *asl, *exp_asl;

        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
        exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);

        if (sdt->aml_len == exp_sdt->aml_len &&
            !memcmp(sdt->aml, exp_sdt->aml, sdt->aml_len)) {
            /* Identical table binaries: no need to disassemble. */
            continue;
        }

        fprintf(stderr,
                "acpi-test: Warning! %.4s binary file mismatch. "
                "Actual [aml:%s], Expected [aml:%s].\n"
                "See source file tests/qtest/bios-tables-test.c "
                "for instructions on how to update expected files.\n",
                exp_sdt->aml, sdt->aml_file, exp_sdt->aml_file);

        all_tables_match = all_tables_match &&
            test_acpi_find_diff_allowed(exp_sdt);

        /*
         *  don't try to decompile if IASL isn't present, in this case user
         * will just 'get binary file mismatch' warnings and test failure
         */
        if (!iasl) {
            continue;
        }

        err = load_asl(data->tables, sdt);
        asl = normalize_asl(sdt->asl);

        /*
         * If expected file is empty - it's likely that it was a stub just
         * created for step 1 above: we do want to decompile the actual one.
         */
        if (exp_sdt->aml_len) {
            exp_err = load_asl(exp_data.tables, exp_sdt);
            exp_asl = normalize_asl(exp_sdt->asl);
        } else {
            exp_err = create_tmp_asl(exp_sdt);
            exp_asl = g_string_new("");
        }

        /* TODO: check for warnings */
        g_assert(!err || exp_err || !exp_sdt->aml_len);

        if (g_strcmp0(asl->str, exp_asl->str)) {
            sdt->tmp_files_retain = true;
            if (exp_err) {
                fprintf(stderr,
                        "Warning! iasl couldn't parse the expected aml\n");
            } else {
                exp_sdt->tmp_files_retain = true;
                fprintf(stderr,
                        "acpi-test: Warning! %.4s mismatch. "
                        "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
                        exp_sdt->aml, sdt->asl_file, sdt->aml_file,
                        exp_sdt->asl_file, exp_sdt->aml_file);
                fflush(stderr);
                if (verbosity_level >= 1) {
                    const char *diff_env = getenv("DIFF");
                    const char *diff_cmd = diff_env ? diff_env : "diff -U 16";
                    char *diff = g_strdup_printf("%s %s %s", diff_cmd,
                                                 exp_sdt->asl_file, sdt->asl_file);
                    int out = dup(STDOUT_FILENO);
                    int ret G_GNUC_UNUSED;
                    int dupret;

                    g_assert(out >= 0);
                    dupret = dup2(STDERR_FILENO, STDOUT_FILENO);
                    g_assert(dupret >= 0);
                    ret = system(diff) ;
                    dupret = dup2(out, STDOUT_FILENO);
                    g_assert(dupret >= 0);
                    close(out);
                    g_free(diff);
                }
            }
        }
        g_string_free(asl, true);
        g_string_free(exp_asl, true);
    }
    if (!iasl && !all_tables_match) {
        fprintf(stderr, "to see ASL diff between mismatched files install IASL,"
                " rebuild QEMU from scratch and re-run tests with V=1"
                " environment variable set");
    }
    g_assert(all_tables_match);

    free_test_data(&exp_data);
}

static bool smbios_ep2_table_ok(test_data *data, uint32_t addr)
{
    struct smbios_21_entry_point *ep_table = &data->smbios_ep_table.ep21;

    qtest_memread(data->qts, addr, ep_table, sizeof(*ep_table));
    if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
        return false;
    }
    if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
        return false;
    }
    if (ep_table->structure_table_length == 0) {
        return false;
    }
    if (ep_table->number_of_structures == 0) {
        return false;
    }
    if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
        acpi_calc_checksum((uint8_t *)ep_table + 0x10,
                           sizeof *ep_table - 0x10)) {
        return false;
    }
    return true;
}

static bool smbios_ep3_table_ok(test_data *data, uint64_t addr)
{
    struct smbios_30_entry_point *ep_table = &data->smbios_ep_table.ep30;

    qtest_memread(data->qts, addr, ep_table, sizeof(*ep_table));
    if (memcmp(ep_table->anchor_string, "_SM3_", 5)) {
        return false;
    }

    if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table)) {
        return false;
    }

    return true;
}

static SmbiosEntryPointType test_smbios_entry_point(test_data *data)
{
    uint32_t off;

    /* find smbios entry point structure */
    for (off = 0xf0000; off < 0x100000; off += 0x10) {
        uint8_t sig[] = "_SM_", sig3[] = "_SM3_";
        int i;

        for (i = 0; i < sizeof sig - 1; ++i) {
            sig[i] = qtest_readb(data->qts, off + i);
        }

        if (!memcmp(sig, "_SM_", sizeof sig)) {
            /* signature match, but is this a valid entry point? */
            if (smbios_ep2_table_ok(data, off)) {
                data->smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE_32] = off;
            }
        }

        for (i = 0; i < sizeof sig3 - 1; ++i) {
            sig3[i] = qtest_readb(data->qts, off + i);
        }

        if (!memcmp(sig3, "_SM3_", sizeof sig3)) {
            if (smbios_ep3_table_ok(data, off)) {
                data->smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE_64] = off;
                /* found 64-bit entry point, no need to look for 32-bit one */
                break;
            }
        }
    }

    /* found at least one entry point */
    g_assert_true(data->smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE_32] ||
                  data->smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE_64]);

    return data->smbios_ep_addr[SMBIOS_ENTRY_POINT_TYPE_64] ?
           SMBIOS_ENTRY_POINT_TYPE_64 : SMBIOS_ENTRY_POINT_TYPE_32;
}

static inline bool smbios_single_instance(uint8_t type)
{
    switch (type) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 16:
    case 32:
    case 127:
        return true;
    default:
        return false;
    }
}

static void smbios_cpu_test(test_data *data, uint32_t addr,
                            SmbiosEntryPointType ep_type)
{
    uint8_t core_count, expected_core_count = data->smbios_core_count;
    uint16_t speed, expected_speed[2];
    uint16_t core_count2, expected_core_count2 = data->smbios_core_count2;
    int offset[2];
    int i;

    /* Check CPU speed for backward compatibility */
    offset[0] = offsetof(struct smbios_type_4, max_speed);
    offset[1] = offsetof(struct smbios_type_4, current_speed);
    expected_speed[0] = data->smbios_cpu_max_speed ? : 2000;
    expected_speed[1] = data->smbios_cpu_curr_speed ? : 2000;

    for (i = 0; i < 2; i++) {
        speed = qtest_readw(data->qts, addr + offset[i]);
        g_assert_cmpuint(speed, ==, expected_speed[i]);
    }

    core_count = qtest_readb(data->qts,
                    addr + offsetof(struct smbios_type_4, core_count));

    if (expected_core_count) {
        g_assert_cmpuint(core_count, ==, expected_core_count);
    }

    if (ep_type == SMBIOS_ENTRY_POINT_TYPE_64) {
        core_count2 = qtest_readw(data->qts,
                          addr + offsetof(struct smbios_type_4, core_count2));

        /* Core Count has reached its limit, checking Core Count 2 */
        if (expected_core_count == 0xFF && expected_core_count2) {
            g_assert_cmpuint(core_count2, ==, expected_core_count2);
        }
    }
}

static void test_smbios_structs(test_data *data, SmbiosEntryPointType ep_type)
{
    DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };

    SmbiosEntryPoint *ep_table = &data->smbios_ep_table;
    int i = 0, len, max_len = 0;
    uint8_t type, prv, crt;
    uint64_t addr;

    if (ep_type == SMBIOS_ENTRY_POINT_TYPE_32) {
        addr = le32_to_cpu(ep_table->ep21.structure_table_address);
    } else {
        addr = le64_to_cpu(ep_table->ep30.structure_table_address);
    }

    /* walk the smbios tables */
    do {

        /* grab type and formatted area length from struct header */
        type = qtest_readb(data->qts, addr);
        g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
        len = qtest_readb(data->qts, addr + 1);

        /* single-instance structs must not have been encountered before */
        if (smbios_single_instance(type)) {
            g_assert(!test_bit(type, struct_bitmap));
        }
        set_bit(type, struct_bitmap);

        if (type == 4) {
            smbios_cpu_test(data, addr, ep_type);
        }

        /* seek to end of unformatted string area of this struct ("\0\0") */
        prv = crt = 1;
        while (prv || crt) {
            prv = crt;
            crt = qtest_readb(data->qts, addr + len);
            len++;
        }

        /* keep track of max. struct size */
        if (ep_type == SMBIOS_ENTRY_POINT_TYPE_32 && max_len < len) {
            max_len = len;
            g_assert_cmpuint(max_len, <=, ep_table->ep21.max_structure_size);
        }

        /* start of next structure */
        addr += len;

    /*
     * Until all structures have been scanned (ep21)
     * or an EOF structure is found (ep30)
     */
    } while (ep_type == SMBIOS_ENTRY_POINT_TYPE_32 ?
                ++i < le16_to_cpu(ep_table->ep21.number_of_structures) :
                type != 127);

    if (ep_type == SMBIOS_ENTRY_POINT_TYPE_32) {
        /*
         * Total table length and max struct size
         * must match entry point values
         */
        g_assert_cmpuint(le16_to_cpu(ep_table->ep21.structure_table_length), ==,
            addr - le32_to_cpu(ep_table->ep21.structure_table_address));

        g_assert_cmpuint(le16_to_cpu(ep_table->ep21.max_structure_size), ==,
            max_len);
    }

    /* required struct types must all be present */
    for (i = 0; i < data->required_struct_types_len; i++) {
        g_assert(test_bit(data->required_struct_types[i], struct_bitmap));
    }
}

static void test_acpi_load_tables(test_data *data)
{
    if (data->uefi_fl1 && data->uefi_fl2) { /* use UEFI */
        g_assert(data->scan_len);
        data->rsdp_addr = acpi_find_rsdp_address_uefi(data->qts,
            data->ram_start, data->scan_len);
    } else {
        boot_sector_test(data->qts);
        data->rsdp_addr = acpi_find_rsdp_address(data->qts);
        g_assert_cmphex(data->rsdp_addr, <, 0x100000);
    }

    data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
    test_acpi_rsdp_table(data);
    test_acpi_rxsdt_table(data);
    test_acpi_fadt_table(data);
}

static char *test_acpi_create_args(test_data *data, const char *params)
{
    char *args;

    if (data->uefi_fl1 && data->uefi_fl2) { /* use UEFI */
        /*
         * TODO: convert '-drive if=pflash' to new syntax (see e33763be7cd3)
         * when arm/virt boad starts to support it.
         */
        if (data->cd) {
            args = g_strdup_printf("-machine %s%s %s -accel tcg "
                "-nodefaults -nographic "
                "-drive if=pflash,format=raw,file=%s,readonly=on "
                "-drive if=pflash,format=raw,file=%s,snapshot=on -cdrom %s %s",
                data->machine, data->machine_param ?: "",
                data->tcg_only ? "" : "-accel kvm",
                data->uefi_fl1, data->uefi_fl2, data->cd, params ? params : "");
        } else {
            args = g_strdup_printf("-machine %s%s %s -accel tcg "
                "-nodefaults -nographic "
                "-drive if=pflash,format=raw,file=%s,readonly=on "
                "-drive if=pflash,format=raw,file=%s,snapshot=on %s",
                data->machine, data->machine_param ?: "",
                data->tcg_only ? "" : "-accel kvm",
                data->uefi_fl1, data->uefi_fl2, params ? params : "");
        }
    } else {
        args = g_strdup_printf("-machine %s%s %s -accel tcg "
            "-net none %s "
            "-drive id=hd0,if=none,file=%s,format=raw "
            "-device %s,drive=hd0 ",
             data->machine, data->machine_param ?: "",
             data->tcg_only ? "" : "-accel kvm",
             params ? params : "", disk,
             data->blkdev ?: "ide-hd");
    }
    return args;
}

static void test_vm_prepare(const char *params, test_data *data)
{
    char *args = test_acpi_create_args(data, params);
    data->qts = qtest_init(args);
    g_free(args);
}

static void process_acpi_tables_noexit(test_data *data)
{
    test_acpi_load_tables(data);

    if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
        dump_aml_files(data, true);
    } else {
        test_acpi_asl(data);
    }

    /*
     * TODO: make SMBIOS tests work with UEFI firmware,
     * Bug on uefi-test-tools to provide entry point:
     * https://bugs.launchpad.net/qemu/+bug/1821884
     */
    if (!(data->uefi_fl1 && data->uefi_fl2)) {
        SmbiosEntryPointType ep_type = test_smbios_entry_point(data);
        test_smbios_structs(data, ep_type);
    }
}

static void process_acpi_tables(test_data *data)
{
    process_acpi_tables_noexit(data);
    qtest_quit(data->qts);
}

static void test_acpi_one(const char *params, test_data *data)
{
    test_vm_prepare(params, data);
    process_acpi_tables(data);
}

static uint8_t base_required_struct_types[] = {
    0, 1, 3, 4, 16, 17, 19, 32, 127
};

static void test_acpi_piix4_tcg(void)
{
    test_data data = {};

    /* Supplying -machine accel argument overrides the default (qtest).
     * This is to make guest actually run.
     */
    data.machine = MACHINE_PC;
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
    test_acpi_one(NULL, &data);
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_bridge(void)
{
    test_data data = {};

    data.machine = MACHINE_PC;
    data.variant = ".bridge";
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
    test_vm_prepare("-S"
        " -device pci-bridge,chassis_nr=1"
        " -device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2"
        " -device pci-testdev,bus=pci.0,addr=5.0"
        " -device pci-testdev,bus=pci.1", &data);

    /* hotplugged bridges section */
    qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr",
        "{'bus': 'pci.1', 'addr': '2.0', 'chassis_nr': 3 }");
    qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr_multifunc",
        "{'bus': 'pci.1', 'addr': '0xf.1', 'chassis_nr': 4 }");
    qtest_qmp_device_add(data.qts, "pci-bridge", "hpbrhost",
        "{'bus': 'pci.0', 'addr': '4.0', 'chassis_nr': 5 }");
    qtest_qmp_device_add(data.qts, "pci-testdev", "d1", "{'bus': 'pci.0' }");
    qtest_qmp_device_add(data.qts, "pci-testdev", "d2", "{'bus': 'pci.1' }");
    qtest_qmp_device_add(data.qts, "pci-testdev", "d3", "{'bus': 'hpbr', "
                                   "'addr': '1.0' }");
    qtest_qmp_send(data.qts, "{'execute':'cont' }");
    qtest_qmp_eventwait(data.qts, "RESUME");

    process_acpi_tables_noexit(&data);
    free_test_data(&data);

    /* check that reboot/reset doesn't change any ACPI tables  */
    qtest_qmp_send(data.qts, "{'execute':'system_reset' }");
    process_acpi_tables(&data);
    free_test_data(&data);
}

static void test_acpi_piix4_no_root_hotplug(void)
{
    test_data data = {};

    data.machine = MACHINE_PC;
    data.variant = ".roothp";
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
    test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
                  "-device pci-bridge,chassis_nr=1 "
                  "-device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2 "
                  "-device pci-testdev,bus=pci.0 "
                  "-device pci-testdev,bus=pci.1", &data);
    free_test_data(&data);
}

static void test_acpi_piix4_no_bridge_hotplug(void)
{
    test_data data = {};

    data.machine = MACHINE_PC;
    data.variant = ".hpbridge";
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
    test_acpi_one("-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
                  "-device pci-bridge,chassis_nr=1 "
                  "-device pci-bridge,bus=pci.1,addr=1.0,chassis_nr=2 "
                  "-device pci-testdev,bus=pci.0 "
                  "-device pci-testdev,bus=pci.1,addr=2.0", &data);
    free_test_data(&data);
}

static void test_acpi_piix4_no_acpi_pci_hotplug(void)
{
    test_data data = {};

    data.machine = MACHINE_PC;
    data.variant = ".hpbrroot";
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
    test_acpi_one("-global PIIX4_PM.acpi-root-pci-hotplug=off "
                  "-global PIIX4_PM.acpi-pci-hotplug-with-bridge-support=off "
                  "-device pci-bridge,chassis_nr=1,addr=4.0 "
                  "-device pci-testdev,bus=pci.0,addr=5.0 "
                  "-device pci-testdev,bus=pci.0,addr=6.0,acpi-index=101 "
                  "-device pci-testdev,bus=pci.1,addr=1.0 "
                  "-device pci-testdev,bus=pci.1,addr=2.0,acpi-index=201 "
                  "-device pci-bridge,id=nhpbr,chassis_nr=2,shpc=off,addr=7.0 "
                  "-device pci-testdev,bus=nhpbr,addr=1.0,acpi-index=301 "
                  , &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
    test_acpi_one(NULL, &data);
    free_test_data(&data);

    data.smbios_cpu_max_speed = 3000;
    data.smbios_cpu_curr_speed = 2600;
    test_acpi_one("-smbios type=4,max-speed=3000,current-speed=2600", &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_core_count2(void)
{
    test_data data = {
        .machine = MACHINE_Q35,
        .variant = ".core-count2",
        .required_struct_types = base_required_struct_types,
        .required_struct_types_len = ARRAY_SIZE(base_required_struct_types),
        .smbios_core_count = 0xFF,
        .smbios_core_count2 = 275,
    };

    test_acpi_one("-machine smbios-entry-point-type=64 -smp 275", &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_bridge(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".bridge";
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
    test_acpi_one("-device pci-bridge,chassis_nr=1,id=br1"
                  " -device pci-testdev,bus=pcie.0"
                  " -device pci-testdev,bus=br1", &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_no_acpi_hotplug(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".noacpihp";
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);
    test_acpi_one("-global ICH9-LPC.acpi-pci-hotplug-with-bridge-support=off"
        " -device pci-testdev,bus=pcie.0,acpi-index=101,addr=3.0"
        " -device pci-bridge,chassis_nr=1,id=shpcbr,addr=4.0"
        " -device pci-testdev,bus=shpcbr,addr=1.0,acpi-index=201"
        " -device pci-bridge,chassis_nr=2,shpc=off,id=noshpcbr,addr=5.0"
        " -device pci-testdev,bus=noshpcbr,addr=1.0,acpi-index=301"
        " -device pcie-root-port,id=hprp,port=0x0,chassis=1,addr=6.0"
        " -device pci-testdev,bus=hprp,acpi-index=401"
        " -device pcie-root-port,id=nohprp,port=0x0,chassis=2,hotplug=off,"
                                 "addr=7.0"
        " -device pci-testdev,bus=nohprp,acpi-index=501"
        " -device pcie-root-port,id=nohprpint,port=0x0,chassis=3,hotplug=off,"
                                 "multifunction=on,addr=8.0"
        " -device pci-testdev,bus=nohprpint,acpi-index=601,addr=8.1"
        " -device pcie-root-port,id=hprp2,port=0x0,chassis=4,bus=nohprpint,"
                                 "addr=9.0"
        " -device pci-testdev,bus=hprp2,acpi-index=602"
        , &data);
    free_test_data(&data);
}

static void test_acpi_q35_multif_bridge(void)
{
    test_data data = {
        .machine = MACHINE_Q35,
        .variant = ".multi-bridge",
    };
    test_vm_prepare("-S"
        " -device virtio-balloon,id=balloon0,addr=0x4.0x2"
        " -device pcie-root-port,id=rp0,multifunction=on,"
                  "port=0x0,chassis=1,addr=0x2"
        " -device pcie-root-port,id=rp1,port=0x1,chassis=2,addr=0x3.0x1"
        " -device pcie-root-port,id=rp2,port=0x0,chassis=3,bus=rp1,addr=0.0"
        " -device pci-bridge,bus=rp2,chassis_nr=4,id=br1"
        " -device pcie-root-port,id=rphptgt1,port=0x0,chassis=5,addr=2.1"
        " -device pcie-root-port,id=rphptgt2,port=0x0,chassis=6,addr=2.2"
        " -device pcie-root-port,id=rphptgt3,port=0x0,chassis=7,addr=2.3"
        " -device pci-testdev,bus=pcie.0,addr=2.4"
        " -device pci-testdev,bus=pcie.0,addr=2.5,acpi-index=102"
        " -device pci-testdev,bus=pcie.0,addr=5.0"
        " -device pci-testdev,bus=pcie.0,addr=0xf.0,acpi-index=101"
        " -device pci-testdev,bus=rp0,addr=0.0"
        " -device pci-testdev,bus=br1"
        " -device pcie-root-port,id=rpnohp,chassis=8,addr=0xA.0,hotplug=off"
        " -device pcie-root-port,id=rp3,chassis=9,bus=rpnohp"
        , &data);

    /* hotplugged bridges section */
    qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr1",
        "{'bus': 'br1', 'addr': '6.0', 'chassis_nr': 128 }");
    qtest_qmp_device_add(data.qts, "pci-bridge", "hpbr2-multiif",
        "{ 'bus': 'br1', 'addr': '2.2', 'chassis_nr': 129 }");
    qtest_qmp_device_add(data.qts, "pcie-pci-bridge", "hpbr3",
        "{'bus': 'rphptgt1', 'addr': '0.0' }");
    qtest_qmp_device_add(data.qts, "pcie-root-port", "hprp",
        "{'bus': 'rphptgt2', 'addr': '0.0' }");
    qtest_qmp_device_add(data.qts, "pci-testdev", "hpnic",
        "{'bus': 'rphptgt3', 'addr': '0.0' }");
    qtest_qmp_send(data.qts, "{'execute':'cont' }");
    qtest_qmp_eventwait(data.qts, "RESUME");

    process_acpi_tables_noexit(&data);
    free_test_data(&data);

    /* check that reboot/reset doesn't change any ACPI tables  */
    qtest_qmp_send(data.qts, "{'execute':'system_reset' }");
    process_acpi_tables(&data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_mmio64(void)
{
    test_data data = {
        .machine = MACHINE_Q35,
        .variant = ".mmio64",
        .required_struct_types = base_required_struct_types,
        .required_struct_types_len = ARRAY_SIZE(base_required_struct_types)
    };

    test_acpi_one("-m 128M,slots=1,maxmem=2G "
                  "-object memory-backend-ram,id=ram0,size=128M "
                  "-numa node,memdev=ram0 "
                  "-device pci-testdev,membar=2G",
                  &data);
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_cphp(void)
{
    test_data data = {};

    data.machine = MACHINE_PC;
    data.variant = ".cphp";
    test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6"
                  " -object memory-backend-ram,id=ram0,size=64M"
                  " -object memory-backend-ram,id=ram1,size=64M"
                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
                  " -numa dist,src=0,dst=1,val=21",
                  &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_cphp(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".cphp";
    test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6"
                  " -object memory-backend-ram,id=ram0,size=64M"
                  " -object memory-backend-ram,id=ram1,size=64M"
                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
                  " -numa dist,src=0,dst=1,val=21",
                  &data);
    free_test_data(&data);
}

static uint8_t ipmi_required_struct_types[] = {
    0, 1, 3, 4, 16, 17, 19, 32, 38, 127
};

static void test_acpi_q35_tcg_ipmi(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".ipmibt";
    data.required_struct_types = ipmi_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
                  " -device isa-ipmi-bt,bmc=bmc0",
                  &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_smbus_ipmi(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".ipmismbus";
    data.required_struct_types = ipmi_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
                  " -device smbus-ipmi,bmc=bmc0",
                  &data);
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_ipmi(void)
{
    test_data data = {};

    /* Supplying -machine accel argument overrides the default (qtest).
     * This is to make guest actually run.
     */
    data.machine = MACHINE_PC;
    data.variant = ".ipmikcs";
    data.required_struct_types = ipmi_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types);
    test_acpi_one("-device ipmi-bmc-sim,id=bmc0"
                  " -device isa-ipmi-kcs,irq=0,bmc=bmc0",
                  &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_memhp(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".memhp";
    test_acpi_one(" -m 128,slots=3,maxmem=1G"
                  " -object memory-backend-ram,id=ram0,size=64M"
                  " -object memory-backend-ram,id=ram1,size=64M"
                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
                  " -numa dist,src=0,dst=1,val=21",
                  &data);
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_memhp(void)
{
    test_data data = {};

    data.machine = MACHINE_PC;
    data.variant = ".memhp";
    test_acpi_one(" -m 128,slots=3,maxmem=1G"
                  " -object memory-backend-ram,id=ram0,size=64M"
                  " -object memory-backend-ram,id=ram1,size=64M"
                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
                  " -numa dist,src=0,dst=1,val=21",
                  &data);
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_nosmm(void)
{
    test_data data = {};

    data.machine = MACHINE_PC;
    data.variant = ".nosmm";
    test_acpi_one("-machine smm=off", &data);
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_smm_compat(void)
{
    test_data data = {};

    data.machine = MACHINE_PC;
    data.variant = ".smm-compat";
    test_acpi_one("-global PIIX4_PM.smm-compat=on", &data);
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_smm_compat_nosmm(void)
{
    test_data data = {};

    data.machine = MACHINE_PC;
    data.variant = ".smm-compat-nosmm";
    test_acpi_one("-global PIIX4_PM.smm-compat=on -machine smm=off", &data);
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_nohpet(void)
{
    test_data data = {};

    data.machine = MACHINE_PC;
    data.machine_param = ",hpet=off";
    data.variant = ".nohpet";
    test_acpi_one(NULL, &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_numamem(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".numamem";
    test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
                  " -numa node -numa node,memdev=ram0", &data);
    free_test_data(&data);
}

static void test_acpi_q35_kvm_xapic(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".xapic";
    test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
                  " -numa node -numa node,memdev=ram0"
                  " -machine kernel-irqchip=on -smp 1,maxcpus=288", &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_nosmm(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".nosmm";
    test_acpi_one("-machine smm=off", &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_smm_compat(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".smm-compat";
    test_acpi_one("-global ICH9-LPC.smm-compat=on", &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_smm_compat_nosmm(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".smm-compat-nosmm";
    test_acpi_one("-global ICH9-LPC.smm-compat=on -machine smm=off", &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_nohpet(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.machine_param = ",hpet=off";
    data.variant = ".nohpet";
    test_acpi_one(NULL, &data);
    free_test_data(&data);
}

static void test_acpi_q35_kvm_dmar(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".dmar";
    test_acpi_one("-machine kernel-irqchip=split -accel kvm"
                  " -device intel-iommu,intremap=on,device-iotlb=on", &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_ivrs(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".ivrs";
    data.tcg_only = true,
    test_acpi_one(" -device amd-iommu", &data);
    free_test_data(&data);
}

static void test_acpi_piix4_tcg_numamem(void)
{
    test_data data = {};

    data.machine = MACHINE_PC;
    data.variant = ".numamem";
    test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M"
                  " -numa node -numa node,memdev=ram0", &data);
    free_test_data(&data);
}

uint64_t tpm_tis_base_addr;

static void test_acpi_tcg_tpm(const char *machine, const char *tpm_if,
                              uint64_t base, enum TPMVersion tpm_version)
{
    gchar *tmp_dir_name = g_strdup_printf("qemu-test_acpi_%s_tcg_%s.XXXXXX",
                                          machine, tpm_if);
    char *tmp_path = g_dir_make_tmp(tmp_dir_name, NULL);
    TPMTestState test;
    test_data data = {};
    GThread *thread;
    const char *suffix = tpm_version == TPM_VERSION_2_0 ? "tpm2" : "tpm12";
    char *args, *variant = g_strdup_printf(".%s.%s", tpm_if, suffix);

    tpm_tis_base_addr = base;

    module_call_init(MODULE_INIT_QOM);

    test.addr = g_new0(SocketAddress, 1);
    test.addr->type = SOCKET_ADDRESS_TYPE_UNIX;
    test.addr->u.q_unix.path = g_build_filename(tmp_path, "sock", NULL);
    g_mutex_init(&test.data_mutex);
    g_cond_init(&test.data_cond);
    test.data_cond_signal = false;
    test.tpm_version = tpm_version;

    thread = g_thread_new(NULL, tpm_emu_ctrl_thread, &test);
    tpm_emu_test_wait_cond(&test);

    data.machine = machine;
    data.variant = variant;

    args = g_strdup_printf(
        " -chardev socket,id=chr,path=%s"
        " -tpmdev emulator,id=dev,chardev=chr"
        " -device tpm-%s,tpmdev=dev",
        test.addr->u.q_unix.path, tpm_if);

    test_acpi_one(args, &data);

    g_thread_join(thread);
    g_unlink(test.addr->u.q_unix.path);
    qapi_free_SocketAddress(test.addr);
    g_rmdir(tmp_path);
    g_free(variant);
    g_free(tmp_path);
    g_free(tmp_dir_name);
    g_free(args);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_tpm2_tis(void)
{
    test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_2_0);
}

static void test_acpi_q35_tcg_tpm12_tis(void)
{
    test_acpi_tcg_tpm("q35", "tis", 0xFED40000, TPM_VERSION_1_2);
}

static void test_acpi_tcg_dimm_pxm(const char *machine)
{
    test_data data = {};

    data.machine = machine;
    data.variant = ".dimmpxm";
    test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu"
                  " -smp 4,sockets=4"
                  " -m 128M,slots=3,maxmem=1G"
                  " -object memory-backend-ram,id=ram0,size=32M"
                  " -object memory-backend-ram,id=ram1,size=32M"
                  " -object memory-backend-ram,id=ram2,size=32M"
                  " -object memory-backend-ram,id=ram3,size=32M"
                  " -numa node,memdev=ram0,nodeid=0"
                  " -numa node,memdev=ram1,nodeid=1"
                  " -numa node,memdev=ram2,nodeid=2"
                  " -numa node,memdev=ram3,nodeid=3"
                  " -numa cpu,node-id=0,socket-id=0"
                  " -numa cpu,node-id=1,socket-id=1"
                  " -numa cpu,node-id=2,socket-id=2"
                  " -numa cpu,node-id=3,socket-id=3"
                  " -object memory-backend-ram,id=ram4,size=128M"
                  " -object memory-backend-ram,id=nvm0,size=128M"
                  " -device pc-dimm,id=dimm0,memdev=ram4,node=1"
                  " -device nvdimm,id=dimm1,memdev=nvm0,node=2",
                  &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_dimm_pxm(void)
{
    test_acpi_tcg_dimm_pxm(MACHINE_Q35);
}

static void test_acpi_piix4_tcg_dimm_pxm(void)
{
    test_acpi_tcg_dimm_pxm(MACHINE_PC);
}

static void test_acpi_virt_tcg_memhp(void)
{
    test_data data = {
        .machine = "virt",
        .tcg_only = true,
        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
        .ram_start = 0x40000000ULL,
        .scan_len = 256ULL * 1024 * 1024,
    };

    data.variant = ".memhp";
    test_acpi_one(" -machine nvdimm=on"
                  " -cpu cortex-a57"
                  " -m 256M,slots=3,maxmem=1G"
                  " -object memory-backend-ram,id=ram0,size=128M"
                  " -object memory-backend-ram,id=ram1,size=128M"
                  " -numa node,memdev=ram0 -numa node,memdev=ram1"
                  " -numa dist,src=0,dst=1,val=21"
                  " -object memory-backend-ram,id=ram2,size=128M"
                  " -object memory-backend-ram,id=nvm0,size=128M"
                  " -device pc-dimm,id=dimm0,memdev=ram2,node=0"
                  " -device nvdimm,id=dimm1,memdev=nvm0,node=1",
                  &data);

    free_test_data(&data);

}

static void test_acpi_microvm_prepare(test_data *data)
{
    data->machine = "microvm";
    data->required_struct_types = NULL; /* no smbios */
    data->required_struct_types_len = 0;
    data->blkdev = "virtio-blk-device";
}

static void test_acpi_microvm_tcg(void)
{
    test_data data = {};

    test_acpi_microvm_prepare(&data);
    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off",
                  &data);
    free_test_data(&data);
}

static void test_acpi_microvm_usb_tcg(void)
{
    test_data data = {};

    test_acpi_microvm_prepare(&data);
    data.variant = ".usb";
    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,usb=on,rtc=off",
                  &data);
    free_test_data(&data);
}

static void test_acpi_microvm_rtc_tcg(void)
{
    test_data data = {};

    test_acpi_microvm_prepare(&data);
    data.variant = ".rtc";
    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=on",
                  &data);
    free_test_data(&data);
}

static void test_acpi_microvm_pcie_tcg(void)
{
    test_data data = {};

    test_acpi_microvm_prepare(&data);
    data.variant = ".pcie";
    data.tcg_only = true; /* need constant host-phys-bits */
    test_acpi_one(" -machine microvm,acpi=on,ioapic2=off,rtc=off,pcie=on",
                  &data);
    free_test_data(&data);
}

static void test_acpi_microvm_ioapic2_tcg(void)
{
    test_data data = {};

    test_acpi_microvm_prepare(&data);
    data.variant = ".ioapic2";
    test_acpi_one(" -machine microvm,acpi=on,ioapic2=on,rtc=off",
                  &data);
    free_test_data(&data);
}

static void test_acpi_virt_tcg_numamem(void)
{
    test_data data = {
        .machine = "virt",
        .tcg_only = true,
        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
        .ram_start = 0x40000000ULL,
        .scan_len = 128ULL * 1024 * 1024,
    };

    data.variant = ".numamem";
    test_acpi_one(" -cpu cortex-a57"
                  " -object memory-backend-ram,id=ram0,size=128M"
                  " -numa node,memdev=ram0",
                  &data);

    free_test_data(&data);

}

static void test_acpi_virt_tcg_pxb(void)
{
    test_data data = {
        .machine = "virt",
        .tcg_only = true,
        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
        .ram_start = 0x40000000ULL,
        .scan_len = 128ULL * 1024 * 1024,
    };
    /*
     * While using -cdrom, the cdrom would auto plugged into pxb-pcie,
     * the reason is the bus of pxb-pcie is also root bus, it would lead
     * to the error only PCI/PCIE bridge could plug onto pxb.
     * Therefore,thr cdrom is defined and plugged onto the scsi controller
     * to solve the conflicts.
     */
    data.variant = ".pxb";
    test_acpi_one(" -device pcie-root-port,chassis=1,id=pci.1"
                  " -device virtio-scsi-pci,id=scsi0,bus=pci.1"
                  " -drive file="
                  "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2,"
                  "if=none,media=cdrom,id=drive-scsi0-0-0-1,readonly=on"
                  " -device scsi-cd,bus=scsi0.0,scsi-id=0,"
                  "drive=drive-scsi0-0-0-1,id=scsi0-0-0-1,bootindex=1"
                  " -cpu cortex-a57"
                  " -device pxb-pcie,bus_nr=128",
                  &data);

    free_test_data(&data);
}

static void test_acpi_tcg_acpi_hmat(const char *machine)
{
    test_data data = {};

    data.machine = machine;
    data.variant = ".acpihmat";
    test_acpi_one(" -machine hmat=on"
                  " -smp 2,sockets=2"
                  " -m 128M,slots=2,maxmem=1G"
                  " -object memory-backend-ram,size=64M,id=m0"
                  " -object memory-backend-ram,size=64M,id=m1"
                  " -numa node,nodeid=0,memdev=m0"
                  " -numa node,nodeid=1,memdev=m1,initiator=0"
                  " -numa cpu,node-id=0,socket-id=0"
                  " -numa cpu,node-id=0,socket-id=1"
                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
                  "data-type=access-latency,latency=1"
                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=65534M"
                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
                  "data-type=access-latency,latency=65534"
                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=32767M"
                  " -numa hmat-cache,node-id=0,size=10K,level=1,"
                  "associativity=direct,policy=write-back,line=8"
                  " -numa hmat-cache,node-id=1,size=10K,level=1,"
                  "associativity=direct,policy=write-back,line=8",
                  &data);
    free_test_data(&data);
}

static void test_acpi_q35_tcg_acpi_hmat(void)
{
    test_acpi_tcg_acpi_hmat(MACHINE_Q35);
}

static void test_acpi_piix4_tcg_acpi_hmat(void)
{
    test_acpi_tcg_acpi_hmat(MACHINE_PC);
}

static void test_acpi_virt_tcg_acpi_hmat(void)
{
    test_data data = {
        .machine = "virt",
        .tcg_only = true,
        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
        .ram_start = 0x40000000ULL,
        .scan_len = 128ULL * 1024 * 1024,
    };

    data.variant = ".acpihmatvirt";

    test_acpi_one(" -machine hmat=on"
                  " -cpu cortex-a57"
                  " -smp 4,sockets=2"
                  " -m 384M"
                  " -object memory-backend-ram,size=128M,id=ram0"
                  " -object memory-backend-ram,size=128M,id=ram1"
                  " -object memory-backend-ram,size=128M,id=ram2"
                  " -numa node,nodeid=0,memdev=ram0"
                  " -numa node,nodeid=1,memdev=ram1"
                  " -numa node,nodeid=2,memdev=ram2"
                  " -numa cpu,node-id=0,socket-id=0"
                  " -numa cpu,node-id=0,socket-id=0"
                  " -numa cpu,node-id=1,socket-id=1"
                  " -numa cpu,node-id=1,socket-id=1"
                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
                  "data-type=access-latency,latency=10"
                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=10485760"
                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
                  "data-type=access-latency,latency=20"
                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=5242880"
                  " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
                  "data-type=access-latency,latency=30"
                  " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=1048576"
                  " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
                  "data-type=access-latency,latency=20"
                  " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=5242880"
                  " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
                  "data-type=access-latency,latency=10"
                  " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=10485760"
                  " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
                  "data-type=access-latency,latency=30"
                  " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=1048576",
                  &data);

    free_test_data(&data);
}

static void test_acpi_q35_tcg_acpi_hmat_noinitiator(void)
{
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.variant = ".acpihmat-noinitiator";
    test_acpi_one(" -machine hmat=on"
                  " -smp 4,sockets=2"
                  " -m 128M"
                  " -object memory-backend-ram,size=32M,id=ram0"
                  " -object memory-backend-ram,size=32M,id=ram1"
                  " -object memory-backend-ram,size=64M,id=ram2"
                  " -numa node,nodeid=0,memdev=ram0"
                  " -numa node,nodeid=1,memdev=ram1"
                  " -numa node,nodeid=2,memdev=ram2"
                  " -numa cpu,node-id=0,socket-id=0"
                  " -numa cpu,node-id=0,socket-id=0"
                  " -numa cpu,node-id=1,socket-id=1"
                  " -numa cpu,node-id=1,socket-id=1"
                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
                  "data-type=access-latency,latency=10"
                  " -numa hmat-lb,initiator=0,target=0,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=10485760"
                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
                  "data-type=access-latency,latency=20"
                  " -numa hmat-lb,initiator=0,target=1,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=5242880"
                  " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
                  "data-type=access-latency,latency=30"
                  " -numa hmat-lb,initiator=0,target=2,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=1048576"
                  " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
                  "data-type=access-latency,latency=20"
                  " -numa hmat-lb,initiator=1,target=0,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=5242880"
                  " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
                  "data-type=access-latency,latency=10"
                  " -numa hmat-lb,initiator=1,target=1,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=10485760"
                  " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
                  "data-type=access-latency,latency=30"
                  " -numa hmat-lb,initiator=1,target=2,hierarchy=memory,"
                  "data-type=access-bandwidth,bandwidth=1048576",
                  &data);
    free_test_data(&data);
}

#ifdef CONFIG_POSIX
static void test_acpi_erst(const char *machine)
{
    gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
    gchar *params;
    test_data data = {};

    data.machine = machine;
    data.variant = ".acpierst";
    params = g_strdup_printf(
        " -object memory-backend-file,id=erstnvram,"
            "mem-path=%s,size=0x10000,share=on"
        " -device acpi-erst,memdev=erstnvram", tmp_path);
    test_acpi_one(params, &data);
    free_test_data(&data);
    g_free(params);
    g_assert(g_rmdir(tmp_path) == 0);
    g_free(tmp_path);
}

static void test_acpi_piix4_acpi_erst(void)
{
    test_acpi_erst(MACHINE_PC);
}

static void test_acpi_q35_acpi_erst(void)
{
    test_acpi_erst(MACHINE_Q35);
}

static void test_acpi_microvm_acpi_erst(void)
{
    gchar *tmp_path = g_dir_make_tmp("qemu-test-erst.XXXXXX", NULL);
    gchar *params;
    test_data data = {};

    test_acpi_microvm_prepare(&data);
    data.variant = ".pcie";
    data.tcg_only = true; /* need constant host-phys-bits */
    params = g_strdup_printf(" -machine microvm,"
        "acpi=on,ioapic2=off,rtc=off,pcie=on"
        " -object memory-backend-file,id=erstnvram,"
           "mem-path=%s,size=0x10000,share=on"
        " -device acpi-erst,memdev=erstnvram", tmp_path);
    test_acpi_one(params, &data);
    g_free(params);
    g_assert(g_rmdir(tmp_path) == 0);
    g_free(tmp_path);
    free_test_data(&data);
}
#endif /* CONFIG_POSIX */

static void test_acpi_virt_tcg(void)
{
    test_data data = {
        .machine = "virt",
        .tcg_only = true,
        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
        .ram_start = 0x40000000ULL,
        .scan_len = 128ULL * 1024 * 1024,
    };

    data.smbios_cpu_max_speed = 2900;
    data.smbios_cpu_curr_speed = 2700;
    test_acpi_one("-cpu cortex-a57 "
                  "-smbios type=4,max-speed=2900,current-speed=2700", &data);
    free_test_data(&data);
}

static void test_acpi_virt_tcg_topology(void)
{
    test_data data = {
        .machine = "virt",
        .variant = ".topology",
        .tcg_only = true,
        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
        .ram_start = 0x40000000ULL,
        .scan_len = 128ULL * 1024 * 1024,
    };

    test_acpi_one("-cpu cortex-a57 "
                  "-smp sockets=1,clusters=2,cores=2,threads=2", &data);
    free_test_data(&data);
}

static void test_acpi_q35_viot(void)
{
    test_data data = {
        .machine = MACHINE_Q35,
        .variant = ".viot",
    };

    /*
     * To keep things interesting, two buses bypass the IOMMU.
     * VIOT should only describes the other two buses.
     */
    test_acpi_one("-machine default_bus_bypass_iommu=on "
                  "-device virtio-iommu-pci "
                  "-device pxb-pcie,bus_nr=0x10,id=pcie.100,bus=pcie.0 "
                  "-device pxb-pcie,bus_nr=0x20,id=pcie.200,bus=pcie.0,bypass_iommu=on "
                  "-device pxb-pcie,bus_nr=0x30,id=pcie.300,bus=pcie.0",
                  &data);
    free_test_data(&data);
}

#ifdef CONFIG_POSIX
static void test_acpi_q35_cxl(void)
{
    gchar *tmp_path = g_dir_make_tmp("qemu-test-cxl.XXXXXX", NULL);
    gchar *params;

    test_data data = {
        .machine = MACHINE_Q35,
        .variant = ".cxl",
    };
    /*
     * A complex CXL setup.
     */
    params = g_strdup_printf(" -machine cxl=on"
                             " -object memory-backend-file,id=cxl-mem1,mem-path=%s,size=256M"
                             " -object memory-backend-file,id=cxl-mem2,mem-path=%s,size=256M"
                             " -object memory-backend-file,id=cxl-mem3,mem-path=%s,size=256M"
                             " -object memory-backend-file,id=cxl-mem4,mem-path=%s,size=256M"
                             " -object memory-backend-file,id=lsa1,mem-path=%s,size=256M"
                             " -object memory-backend-file,id=lsa2,mem-path=%s,size=256M"
                             " -object memory-backend-file,id=lsa3,mem-path=%s,size=256M"
                             " -object memory-backend-file,id=lsa4,mem-path=%s,size=256M"
                             " -device pxb-cxl,bus_nr=12,bus=pcie.0,id=cxl.1"
                             " -device pxb-cxl,bus_nr=222,bus=pcie.0,id=cxl.2"
                             " -device cxl-rp,port=0,bus=cxl.1,id=rp1,chassis=0,slot=2"
                             " -device cxl-type3,bus=rp1,persistent-memdev=cxl-mem1,lsa=lsa1"
                             " -device cxl-rp,port=1,bus=cxl.1,id=rp2,chassis=0,slot=3"
                             " -device cxl-type3,bus=rp2,persistent-memdev=cxl-mem2,lsa=lsa2"
                             " -device cxl-rp,port=0,bus=cxl.2,id=rp3,chassis=0,slot=5"
                             " -device cxl-type3,bus=rp3,persistent-memdev=cxl-mem3,lsa=lsa3"
                             " -device cxl-rp,port=1,bus=cxl.2,id=rp4,chassis=0,slot=6"
                             " -device cxl-type3,bus=rp4,persistent-memdev=cxl-mem4,lsa=lsa4"
                             " -M cxl-fmw.0.targets.0=cxl.1,cxl-fmw.0.size=4G,cxl-fmw.0.interleave-granularity=8k,"
                             "cxl-fmw.1.targets.0=cxl.1,cxl-fmw.1.targets.1=cxl.2,cxl-fmw.1.size=4G,cxl-fmw.1.interleave-granularity=8k",
                             tmp_path, tmp_path, tmp_path, tmp_path,
                             tmp_path, tmp_path, tmp_path, tmp_path);
    test_acpi_one(params, &data);

    g_free(params);
    g_assert(g_rmdir(tmp_path) == 0);
    g_free(tmp_path);
    free_test_data(&data);
}
#endif /* CONFIG_POSIX */

static void test_acpi_virt_viot(void)
{
    test_data data = {
        .machine = "virt",
        .tcg_only = true,
        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
        .ram_start = 0x40000000ULL,
        .scan_len = 128ULL * 1024 * 1024,
    };

    test_acpi_one("-cpu cortex-a57 "
                  "-device virtio-iommu-pci", &data);
    free_test_data(&data);
}

#ifndef _WIN32
# define DEV_NULL "/dev/null"
#else
# define DEV_NULL "nul"
#endif

static void test_acpi_q35_slic(void)
{
    test_data data = {
        .machine = MACHINE_Q35,
        .variant = ".slic",
    };

    test_acpi_one("-acpitable sig=SLIC,oem_id=\"CRASH \",oem_table_id=ME,"
                  "oem_rev=00002210,asl_compiler_id=qemu,"
                  "asl_compiler_rev=00000000,data=" DEV_NULL,
                  &data);
    free_test_data(&data);
}

static void test_acpi_q35_applesmc(void)
{
    test_data data = {
        .machine = MACHINE_Q35,
        .variant = ".applesmc",
    };

    /* supply fake 64-byte OSK to silence missing key warning */
    test_acpi_one("-device isa-applesmc,osk=any64characterfakeoskisenough"
                  "topreventinvalidkeywarningsonstderr", &data);
    free_test_data(&data);
}

static void test_acpi_q35_pvpanic_isa(void)
{
    test_data data = {
        .machine = MACHINE_Q35,
        .variant = ".pvpanic-isa",
    };

    test_acpi_one("-device pvpanic", &data);
    free_test_data(&data);
}

static void test_oem_fields(test_data *data)
{
    int i;

    for (i = 0; i < data->tables->len; ++i) {
        AcpiSdtTable *sdt;

        sdt = &g_array_index(data->tables, AcpiSdtTable, i);
        /* FACS doesn't have OEMID and OEMTABLEID fields */
        if (compare_signature(sdt, "FACS")) {
            continue;
        }

        g_assert(strncmp((char *)sdt->aml + 10, OEM_ID, 6) == 0);
        g_assert(strncmp((char *)sdt->aml + 16, OEM_TABLE_ID, 8) == 0);
    }
}

static void test_acpi_piix4_oem_fields(void)
{
    char *args;
    test_data data = {};

    data.machine = MACHINE_PC;
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);

    args = test_acpi_create_args(&data, OEM_TEST_ARGS);
    data.qts = qtest_init(args);
    test_acpi_load_tables(&data);
    test_oem_fields(&data);
    qtest_quit(data.qts);
    free_test_data(&data);
    g_free(args);
}

static void test_acpi_q35_oem_fields(void)
{
    char *args;
    test_data data = {};

    data.machine = MACHINE_Q35;
    data.required_struct_types = base_required_struct_types;
    data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types);

    args = test_acpi_create_args(&data, OEM_TEST_ARGS);
    data.qts = qtest_init(args);
    test_acpi_load_tables(&data);
    test_oem_fields(&data);
    qtest_quit(data.qts);
    free_test_data(&data);
    g_free(args);
}

static void test_acpi_microvm_oem_fields(void)
{
    test_data data = {};
    char *args;

    test_acpi_microvm_prepare(&data);

    args = test_acpi_create_args(&data,
                                 OEM_TEST_ARGS",acpi=on");
    data.qts = qtest_init(args);
    test_acpi_load_tables(&data);
    test_oem_fields(&data);
    qtest_quit(data.qts);
    free_test_data(&data);
    g_free(args);
}

static void test_acpi_virt_oem_fields(void)
{
    test_data data = {
        .machine = "virt",
        .tcg_only = true,
        .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd",
        .uefi_fl2 = "pc-bios/edk2-arm-vars.fd",
        .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2",
        .ram_start = 0x40000000ULL,
        .scan_len = 128ULL * 1024 * 1024,
    };
    char *args;

    args = test_acpi_create_args(&data, "-cpu cortex-a57 "OEM_TEST_ARGS);
    data.qts = qtest_init(args);
    test_acpi_load_tables(&data);
    test_oem_fields(&data);
    qtest_quit(data.qts);
    free_test_data(&data);
    g_free(args);
}


int main(int argc, char *argv[])
{
    const char *arch = qtest_get_arch();
    bool has_kvm, has_tcg;
    char *v_env = getenv("V");
    int ret;

    if (v_env) {
        verbosity_level = atoi(v_env);
    }

    g_test_init(&argc, &argv, NULL);

    has_kvm = qtest_has_accel("kvm");
    has_tcg = qtest_has_accel("tcg");

    if (!has_tcg && !has_kvm) {
        g_test_skip("No KVM or TCG accelerator available");
        return 0;
    }

    if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
        ret = boot_sector_init(disk);
        if (ret) {
            return ret;
        }
        if (qtest_has_machine(MACHINE_PC)) {
            qtest_add_func("acpi/piix4", test_acpi_piix4_tcg);
            qtest_add_func("acpi/piix4/oem-fields", test_acpi_piix4_oem_fields);
            qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge);
            qtest_add_func("acpi/piix4/pci-hotplug/no_root_hotplug",
                           test_acpi_piix4_no_root_hotplug);
            qtest_add_func("acpi/piix4/pci-hotplug/no_bridge_hotplug",
                           test_acpi_piix4_no_bridge_hotplug);
            qtest_add_func("acpi/piix4/pci-hotplug/off",
                           test_acpi_piix4_no_acpi_pci_hotplug);
            qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi);
            qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp);
            qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp);
            qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem);
            qtest_add_func("acpi/piix4/nosmm", test_acpi_piix4_tcg_nosmm);
            qtest_add_func("acpi/piix4/smm-compat",
                           test_acpi_piix4_tcg_smm_compat);
            qtest_add_func("acpi/piix4/smm-compat-nosmm",
                           test_acpi_piix4_tcg_smm_compat_nosmm);
            qtest_add_func("acpi/piix4/nohpet", test_acpi_piix4_tcg_nohpet);
            qtest_add_func("acpi/piix4/dimmpxm", test_acpi_piix4_tcg_dimm_pxm);
            qtest_add_func("acpi/piix4/acpihmat",
                           test_acpi_piix4_tcg_acpi_hmat);
#ifdef CONFIG_POSIX
            qtest_add_func("acpi/piix4/acpierst", test_acpi_piix4_acpi_erst);
#endif
        }
        if (qtest_has_machine(MACHINE_Q35)) {
            qtest_add_func("acpi/q35", test_acpi_q35_tcg);
            qtest_add_func("acpi/q35/oem-fields", test_acpi_q35_oem_fields);
            if (tpm_model_is_available("-machine q35", "tpm-tis")) {
                qtest_add_func("acpi/q35/tpm2-tis", test_acpi_q35_tcg_tpm2_tis);
                qtest_add_func("acpi/q35/tpm12-tis",
                               test_acpi_q35_tcg_tpm12_tis);
            }
            qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge);
            qtest_add_func("acpi/q35/no-acpi-hotplug",
                           test_acpi_q35_tcg_no_acpi_hotplug);
            qtest_add_func("acpi/q35/multif-bridge",
                           test_acpi_q35_multif_bridge);
            qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64);
            qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi);
            qtest_add_func("acpi/q35/smbus/ipmi", test_acpi_q35_tcg_smbus_ipmi);
            qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp);
            qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp);
            qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem);
            qtest_add_func("acpi/q35/nosmm", test_acpi_q35_tcg_nosmm);
            qtest_add_func("acpi/q35/smm-compat",
                           test_acpi_q35_tcg_smm_compat);
            qtest_add_func("acpi/q35/smm-compat-nosmm",
                           test_acpi_q35_tcg_smm_compat_nosmm);
            qtest_add_func("acpi/q35/nohpet", test_acpi_q35_tcg_nohpet);
            qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm);
            qtest_add_func("acpi/q35/acpihmat", test_acpi_q35_tcg_acpi_hmat);
            qtest_add_func("acpi/q35/acpihmat-noinitiator",
                           test_acpi_q35_tcg_acpi_hmat_noinitiator);
#ifdef CONFIG_POSIX
            qtest_add_func("acpi/q35/acpierst", test_acpi_q35_acpi_erst);
#endif
            qtest_add_func("acpi/q35/applesmc", test_acpi_q35_applesmc);
            qtest_add_func("acpi/q35/pvpanic-isa", test_acpi_q35_pvpanic_isa);
            if (has_tcg) {
                qtest_add_func("acpi/q35/ivrs", test_acpi_q35_tcg_ivrs);
            }
            if (has_kvm) {
                qtest_add_func("acpi/q35/kvm/xapic", test_acpi_q35_kvm_xapic);
                qtest_add_func("acpi/q35/kvm/dmar", test_acpi_q35_kvm_dmar);
                qtest_add_func("acpi/q35/core-count2",
                               test_acpi_q35_tcg_core_count2);
            }
            qtest_add_func("acpi/q35/viot", test_acpi_q35_viot);
#ifdef CONFIG_POSIX
            qtest_add_func("acpi/q35/cxl", test_acpi_q35_cxl);
#endif
            qtest_add_func("acpi/q35/slic", test_acpi_q35_slic);
        }
        if (qtest_has_machine("microvm")) {
            qtest_add_func("acpi/microvm", test_acpi_microvm_tcg);
            qtest_add_func("acpi/microvm/usb", test_acpi_microvm_usb_tcg);
            qtest_add_func("acpi/microvm/rtc", test_acpi_microvm_rtc_tcg);
            qtest_add_func("acpi/microvm/ioapic2",
                           test_acpi_microvm_ioapic2_tcg);
            qtest_add_func("acpi/microvm/oem-fields",
                           test_acpi_microvm_oem_fields);
            if (has_tcg) {
                if (strcmp(arch, "x86_64") == 0) {
                    qtest_add_func("acpi/microvm/pcie",
                                   test_acpi_microvm_pcie_tcg);
#ifdef CONFIG_POSIX
                    qtest_add_func("acpi/microvm/acpierst",
                                   test_acpi_microvm_acpi_erst);
#endif
                }
            }
        }
    } else if (strcmp(arch, "aarch64") == 0) {
        if (has_tcg) {
            qtest_add_func("acpi/virt", test_acpi_virt_tcg);
            qtest_add_func("acpi/virt/acpihmatvirt",
                            test_acpi_virt_tcg_acpi_hmat);
            qtest_add_func("acpi/virt/topology", test_acpi_virt_tcg_topology);
            qtest_add_func("acpi/virt/numamem", test_acpi_virt_tcg_numamem);
            qtest_add_func("acpi/virt/memhp", test_acpi_virt_tcg_memhp);
            qtest_add_func("acpi/virt/pxb", test_acpi_virt_tcg_pxb);
            qtest_add_func("acpi/virt/oem-fields", test_acpi_virt_oem_fields);
            qtest_add_func("acpi/virt/viot", test_acpi_virt_viot);
        }
    }
    ret = g_test_run();
    boot_sector_cleanup(disk);
    return ret;
}