2
0

bios-tables-test.c 31 KB

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