kvm_stat: Add kvm_exit reasons for aarch64
[qemu.git] / tests / bios-tables-test.c
blob4d0fa848b1440cb5e77b3fbe69c3620419e9fede
1 /*
2 * Boot order test cases.
4 * Copyright (c) 2013 Red Hat Inc.
6 * Authors:
7 * Michael S. Tsirkin <mst@redhat.com>,
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.
13 #include <string.h>
14 #include <stdio.h>
15 #include <glib.h>
16 #include <glib/gstdio.h>
17 #include "qemu-common.h"
18 #include "libqtest.h"
19 #include "qemu/compiler.h"
20 #include "hw/i386/acpi-defs.h"
21 #include "hw/i386/smbios.h"
22 #include "qemu/bitmap.h"
24 #define MACHINE_PC "pc"
25 #define MACHINE_Q35 "q35"
27 #define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"
29 /* DSDT and SSDTs format */
30 typedef struct {
31 AcpiTableHeader header;
32 gchar *aml; /* aml bytecode from guest */
33 gsize aml_len;
34 gchar *aml_file;
35 gchar *asl; /* asl code generated from aml */
36 gsize asl_len;
37 gchar *asl_file;
38 bool tmp_files_retain; /* do not delete the temp asl/aml */
39 } QEMU_PACKED AcpiSdtTable;
41 typedef struct {
42 const char *machine;
43 uint32_t rsdp_addr;
44 AcpiRsdpDescriptor rsdp_table;
45 AcpiRsdtDescriptorRev1 rsdt_table;
46 AcpiFadtDescriptorRev1 fadt_table;
47 AcpiFacsDescriptorRev1 facs_table;
48 uint32_t *rsdt_tables_addr;
49 int rsdt_tables_nr;
50 GArray *tables;
51 uint32_t smbios_ep_addr;
52 struct smbios_entry_point smbios_ep_table;
53 } test_data;
55 #define LOW(x) ((x) & 0xff)
56 #define HIGH(x) ((x) >> 8)
58 #define SIGNATURE 0xdead
59 #define SIGNATURE_OFFSET 0x10
60 #define BOOT_SECTOR_ADDRESS 0x7c00
62 #define ACPI_READ_FIELD(field, addr) \
63 do { \
64 switch (sizeof(field)) { \
65 case 1: \
66 field = readb(addr); \
67 break; \
68 case 2: \
69 field = readw(addr); \
70 break; \
71 case 4: \
72 field = readl(addr); \
73 break; \
74 case 8: \
75 field = readq(addr); \
76 break; \
77 default: \
78 g_assert(false); \
79 } \
80 addr += sizeof(field); \
81 } while (0);
83 #define ACPI_READ_ARRAY_PTR(arr, length, addr) \
84 do { \
85 int idx; \
86 for (idx = 0; idx < length; ++idx) { \
87 ACPI_READ_FIELD(arr[idx], addr); \
88 } \
89 } while (0);
91 #define ACPI_READ_ARRAY(arr, addr) \
92 ACPI_READ_ARRAY_PTR(arr, sizeof(arr)/sizeof(arr[0]), addr)
94 #define ACPI_READ_TABLE_HEADER(table, addr) \
95 do { \
96 ACPI_READ_FIELD((table)->signature, addr); \
97 ACPI_READ_FIELD((table)->length, addr); \
98 ACPI_READ_FIELD((table)->revision, addr); \
99 ACPI_READ_FIELD((table)->checksum, addr); \
100 ACPI_READ_ARRAY((table)->oem_id, addr); \
101 ACPI_READ_ARRAY((table)->oem_table_id, addr); \
102 ACPI_READ_FIELD((table)->oem_revision, addr); \
103 ACPI_READ_ARRAY((table)->asl_compiler_id, addr); \
104 ACPI_READ_FIELD((table)->asl_compiler_revision, addr); \
105 } while (0);
107 #define ACPI_ASSERT_CMP(actual, expected) do { \
108 uint32_t ACPI_ASSERT_CMP_le = cpu_to_le32(actual); \
109 char ACPI_ASSERT_CMP_str[5] = {}; \
110 memcpy(ACPI_ASSERT_CMP_str, &ACPI_ASSERT_CMP_le, 4); \
111 g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
112 } while (0)
114 #define ACPI_ASSERT_CMP64(actual, expected) do { \
115 uint64_t ACPI_ASSERT_CMP_le = cpu_to_le64(actual); \
116 char ACPI_ASSERT_CMP_str[9] = {}; \
117 memcpy(ACPI_ASSERT_CMP_str, &ACPI_ASSERT_CMP_le, 8); \
118 g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
119 } while (0)
121 /* Boot sector code: write SIGNATURE into memory,
122 * then halt.
123 * Q35 machine requires a minimum 0x7e000 bytes disk.
124 * (bug or feature?)
126 static uint8_t boot_sector[0x7e000] = {
127 /* 7c00: mov $0xdead,%ax */
128 [0x00] = 0xb8,
129 [0x01] = LOW(SIGNATURE),
130 [0x02] = HIGH(SIGNATURE),
131 /* 7c03: mov %ax,0x7c10 */
132 [0x03] = 0xa3,
133 [0x04] = LOW(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET),
134 [0x05] = HIGH(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET),
135 /* 7c06: cli */
136 [0x06] = 0xfa,
137 /* 7c07: hlt */
138 [0x07] = 0xf4,
139 /* 7c08: jmp 0x7c07=0x7c0a-3 */
140 [0x08] = 0xeb,
141 [0x09] = LOW(-3),
142 /* We mov 0xdead here: set value to make debugging easier */
143 [SIGNATURE_OFFSET] = LOW(0xface),
144 [SIGNATURE_OFFSET + 1] = HIGH(0xface),
145 /* End of boot sector marker */
146 [0x1FE] = 0x55,
147 [0x1FF] = 0xAA,
150 static const char *disk = "tests/acpi-test-disk.raw";
151 static const char *data_dir = "tests/acpi-test-data";
152 #ifdef CONFIG_IASL
153 static const char *iasl = stringify(CONFIG_IASL);
154 #else
155 static const char *iasl;
156 #endif
158 static void free_test_data(test_data *data)
160 AcpiSdtTable *temp;
161 int i;
163 if (data->rsdt_tables_addr) {
164 g_free(data->rsdt_tables_addr);
167 for (i = 0; i < data->tables->len; ++i) {
168 temp = &g_array_index(data->tables, AcpiSdtTable, i);
169 if (temp->aml) {
170 g_free(temp->aml);
172 if (temp->aml_file) {
173 if (!temp->tmp_files_retain &&
174 g_strstr_len(temp->aml_file, -1, "aml-")) {
175 unlink(temp->aml_file);
177 g_free(temp->aml_file);
179 if (temp->asl) {
180 g_free(temp->asl);
182 if (temp->asl_file) {
183 if (!temp->tmp_files_retain) {
184 unlink(temp->asl_file);
186 g_free(temp->asl_file);
190 g_array_free(data->tables, false);
193 static uint8_t acpi_checksum(const uint8_t *data, int len)
195 int i;
196 uint8_t sum = 0;
198 for (i = 0; i < len; i++) {
199 sum += data[i];
202 return sum;
205 static void test_acpi_rsdp_address(test_data *data)
207 uint32_t off;
209 /* OK, now find RSDP */
210 for (off = 0xf0000; off < 0x100000; off += 0x10) {
211 uint8_t sig[] = "RSD PTR ";
212 int i;
214 for (i = 0; i < sizeof sig - 1; ++i) {
215 sig[i] = readb(off + i);
218 if (!memcmp(sig, "RSD PTR ", sizeof sig)) {
219 break;
223 g_assert_cmphex(off, <, 0x100000);
224 data->rsdp_addr = off;
227 static void test_acpi_rsdp_table(test_data *data)
229 AcpiRsdpDescriptor *rsdp_table = &data->rsdp_table;
230 uint32_t addr = data->rsdp_addr;
232 ACPI_READ_FIELD(rsdp_table->signature, addr);
233 ACPI_ASSERT_CMP64(rsdp_table->signature, "RSD PTR ");
235 ACPI_READ_FIELD(rsdp_table->checksum, addr);
236 ACPI_READ_ARRAY(rsdp_table->oem_id, addr);
237 ACPI_READ_FIELD(rsdp_table->revision, addr);
238 ACPI_READ_FIELD(rsdp_table->rsdt_physical_address, addr);
239 ACPI_READ_FIELD(rsdp_table->length, addr);
241 /* rsdp checksum is not for the whole table, but for the first 20 bytes */
242 g_assert(!acpi_checksum((uint8_t *)rsdp_table, 20));
245 static void test_acpi_rsdt_table(test_data *data)
247 AcpiRsdtDescriptorRev1 *rsdt_table = &data->rsdt_table;
248 uint32_t addr = data->rsdp_table.rsdt_physical_address;
249 uint32_t *tables;
250 int tables_nr;
251 uint8_t checksum;
253 /* read the header */
254 ACPI_READ_TABLE_HEADER(rsdt_table, addr);
255 ACPI_ASSERT_CMP(rsdt_table->signature, "RSDT");
257 /* compute the table entries in rsdt */
258 tables_nr = (rsdt_table->length - sizeof(AcpiRsdtDescriptorRev1)) /
259 sizeof(uint32_t);
260 g_assert_cmpint(tables_nr, >, 0);
262 /* get the addresses of the tables pointed by rsdt */
263 tables = g_new0(uint32_t, tables_nr);
264 ACPI_READ_ARRAY_PTR(tables, tables_nr, addr);
266 checksum = acpi_checksum((uint8_t *)rsdt_table, rsdt_table->length) +
267 acpi_checksum((uint8_t *)tables, tables_nr * sizeof(uint32_t));
268 g_assert(!checksum);
270 /* SSDT tables after FADT */
271 data->rsdt_tables_addr = tables;
272 data->rsdt_tables_nr = tables_nr;
275 static void test_acpi_fadt_table(test_data *data)
277 AcpiFadtDescriptorRev1 *fadt_table = &data->fadt_table;
278 uint32_t addr;
280 /* FADT table comes first */
281 addr = data->rsdt_tables_addr[0];
282 ACPI_READ_TABLE_HEADER(fadt_table, addr);
284 ACPI_READ_FIELD(fadt_table->firmware_ctrl, addr);
285 ACPI_READ_FIELD(fadt_table->dsdt, addr);
286 ACPI_READ_FIELD(fadt_table->model, addr);
287 ACPI_READ_FIELD(fadt_table->reserved1, addr);
288 ACPI_READ_FIELD(fadt_table->sci_int, addr);
289 ACPI_READ_FIELD(fadt_table->smi_cmd, addr);
290 ACPI_READ_FIELD(fadt_table->acpi_enable, addr);
291 ACPI_READ_FIELD(fadt_table->acpi_disable, addr);
292 ACPI_READ_FIELD(fadt_table->S4bios_req, addr);
293 ACPI_READ_FIELD(fadt_table->reserved2, addr);
294 ACPI_READ_FIELD(fadt_table->pm1a_evt_blk, addr);
295 ACPI_READ_FIELD(fadt_table->pm1b_evt_blk, addr);
296 ACPI_READ_FIELD(fadt_table->pm1a_cnt_blk, addr);
297 ACPI_READ_FIELD(fadt_table->pm1b_cnt_blk, addr);
298 ACPI_READ_FIELD(fadt_table->pm2_cnt_blk, addr);
299 ACPI_READ_FIELD(fadt_table->pm_tmr_blk, addr);
300 ACPI_READ_FIELD(fadt_table->gpe0_blk, addr);
301 ACPI_READ_FIELD(fadt_table->gpe1_blk, addr);
302 ACPI_READ_FIELD(fadt_table->pm1_evt_len, addr);
303 ACPI_READ_FIELD(fadt_table->pm1_cnt_len, addr);
304 ACPI_READ_FIELD(fadt_table->pm2_cnt_len, addr);
305 ACPI_READ_FIELD(fadt_table->pm_tmr_len, addr);
306 ACPI_READ_FIELD(fadt_table->gpe0_blk_len, addr);
307 ACPI_READ_FIELD(fadt_table->gpe1_blk_len, addr);
308 ACPI_READ_FIELD(fadt_table->gpe1_base, addr);
309 ACPI_READ_FIELD(fadt_table->reserved3, addr);
310 ACPI_READ_FIELD(fadt_table->plvl2_lat, addr);
311 ACPI_READ_FIELD(fadt_table->plvl3_lat, addr);
312 ACPI_READ_FIELD(fadt_table->flush_size, addr);
313 ACPI_READ_FIELD(fadt_table->flush_stride, addr);
314 ACPI_READ_FIELD(fadt_table->duty_offset, addr);
315 ACPI_READ_FIELD(fadt_table->duty_width, addr);
316 ACPI_READ_FIELD(fadt_table->day_alrm, addr);
317 ACPI_READ_FIELD(fadt_table->mon_alrm, addr);
318 ACPI_READ_FIELD(fadt_table->century, addr);
319 ACPI_READ_FIELD(fadt_table->reserved4, addr);
320 ACPI_READ_FIELD(fadt_table->reserved4a, addr);
321 ACPI_READ_FIELD(fadt_table->reserved4b, addr);
322 ACPI_READ_FIELD(fadt_table->flags, addr);
324 ACPI_ASSERT_CMP(fadt_table->signature, "FACP");
325 g_assert(!acpi_checksum((uint8_t *)fadt_table, fadt_table->length));
328 static void test_acpi_facs_table(test_data *data)
330 AcpiFacsDescriptorRev1 *facs_table = &data->facs_table;
331 uint32_t addr = data->fadt_table.firmware_ctrl;
333 ACPI_READ_FIELD(facs_table->signature, addr);
334 ACPI_READ_FIELD(facs_table->length, addr);
335 ACPI_READ_FIELD(facs_table->hardware_signature, addr);
336 ACPI_READ_FIELD(facs_table->firmware_waking_vector, addr);
337 ACPI_READ_FIELD(facs_table->global_lock, addr);
338 ACPI_READ_FIELD(facs_table->flags, addr);
339 ACPI_READ_ARRAY(facs_table->resverved3, addr);
341 ACPI_ASSERT_CMP(facs_table->signature, "FACS");
344 static void test_dst_table(AcpiSdtTable *sdt_table, uint32_t addr)
346 uint8_t checksum;
348 ACPI_READ_TABLE_HEADER(&sdt_table->header, addr);
350 sdt_table->aml_len = sdt_table->header.length - sizeof(AcpiTableHeader);
351 sdt_table->aml = g_malloc0(sdt_table->aml_len);
352 ACPI_READ_ARRAY_PTR(sdt_table->aml, sdt_table->aml_len, addr);
354 checksum = acpi_checksum((uint8_t *)sdt_table, sizeof(AcpiTableHeader)) +
355 acpi_checksum((uint8_t *)sdt_table->aml, sdt_table->aml_len);
356 g_assert(!checksum);
359 static void test_acpi_dsdt_table(test_data *data)
361 AcpiSdtTable dsdt_table;
362 uint32_t addr = data->fadt_table.dsdt;
364 memset(&dsdt_table, 0, sizeof(dsdt_table));
365 data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
367 test_dst_table(&dsdt_table, addr);
368 ACPI_ASSERT_CMP(dsdt_table.header.signature, "DSDT");
370 /* Place DSDT first */
371 g_array_append_val(data->tables, dsdt_table);
374 static void test_acpi_tables(test_data *data)
376 int tables_nr = data->rsdt_tables_nr - 1; /* fadt is first */
377 int i;
379 for (i = 0; i < tables_nr; i++) {
380 AcpiSdtTable ssdt_table;
382 memset(&ssdt_table, 0 , sizeof(ssdt_table));
383 uint32_t addr = data->rsdt_tables_addr[i + 1]; /* fadt is first */
384 test_dst_table(&ssdt_table, addr);
385 g_array_append_val(data->tables, ssdt_table);
389 static void dump_aml_files(test_data *data, bool rebuild)
391 AcpiSdtTable *sdt;
392 GError *error = NULL;
393 gchar *aml_file = NULL;
394 gint fd;
395 ssize_t ret;
396 int i;
398 for (i = 0; i < data->tables->len; ++i) {
399 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
400 g_assert(sdt->aml);
402 if (rebuild) {
403 uint32_t signature = cpu_to_le32(sdt->header.signature);
404 aml_file = g_strdup_printf("%s/%s/%.4s", data_dir, data->machine,
405 (gchar *)&signature);
406 fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
407 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
408 } else {
409 fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
410 g_assert_no_error(error);
412 g_assert(fd >= 0);
414 ret = qemu_write_full(fd, sdt, sizeof(AcpiTableHeader));
415 g_assert(ret == sizeof(AcpiTableHeader));
416 ret = qemu_write_full(fd, sdt->aml, sdt->aml_len);
417 g_assert(ret == sdt->aml_len);
419 close(fd);
421 if (aml_file) {
422 g_free(aml_file);
427 static bool compare_signature(AcpiSdtTable *sdt, const char *signature)
429 return !memcmp(&sdt->header.signature, signature, 4);
432 static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
434 AcpiSdtTable *temp;
435 GError *error = NULL;
436 GString *command_line = g_string_new(iasl);
437 gint fd;
438 gchar *out, *out_err;
439 gboolean ret;
440 int i;
442 fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
443 g_assert_no_error(error);
444 close(fd);
446 /* build command line */
447 g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
448 if (compare_signature(sdt, "DSDT") ||
449 compare_signature(sdt, "SSDT")) {
450 for (i = 0; i < sdts->len; ++i) {
451 temp = &g_array_index(sdts, AcpiSdtTable, i);
452 if (compare_signature(temp, "DSDT") ||
453 compare_signature(temp, "SSDT")) {
454 g_string_append_printf(command_line, "-e %s ", temp->aml_file);
458 g_string_append_printf(command_line, "-d %s", sdt->aml_file);
460 /* pass 'out' and 'out_err' in order to be redirected */
461 ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
462 g_assert_no_error(error);
463 if (ret) {
464 ret = g_file_get_contents(sdt->asl_file, (gchar **)&sdt->asl,
465 &sdt->asl_len, &error);
466 g_assert(ret);
467 g_assert_no_error(error);
468 ret = (sdt->asl_len > 0);
471 g_free(out);
472 g_free(out_err);
473 g_string_free(command_line, true);
475 return !ret;
478 #define COMMENT_END "*/"
479 #define DEF_BLOCK "DefinitionBlock ("
480 #define BLOCK_NAME_END ".aml"
482 static GString *normalize_asl(gchar *asl_code)
484 GString *asl = g_string_new(asl_code);
485 gchar *comment, *block_name;
487 /* strip comments (different generation days) */
488 comment = g_strstr_len(asl->str, asl->len, COMMENT_END);
489 if (comment) {
490 comment += strlen(COMMENT_END);
491 while (*comment == '\n') {
492 comment++;
494 asl = g_string_erase(asl, 0, comment - asl->str);
497 /* strip def block name (it has file path in it) */
498 if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
499 block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
500 g_assert(block_name);
501 asl = g_string_erase(asl, 0,
502 block_name + sizeof(BLOCK_NAME_END) - asl->str);
505 return asl;
508 static GArray *load_expected_aml(test_data *data)
510 int i;
511 AcpiSdtTable *sdt;
512 gchar *aml_file;
513 GError *error = NULL;
514 gboolean ret;
516 GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
517 for (i = 0; i < data->tables->len; ++i) {
518 AcpiSdtTable exp_sdt;
519 uint32_t signature;
521 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
523 memset(&exp_sdt, 0, sizeof(exp_sdt));
524 exp_sdt.header.signature = sdt->header.signature;
526 signature = cpu_to_le32(sdt->header.signature);
527 aml_file = g_strdup_printf("%s/%s/%.4s", data_dir, data->machine,
528 (gchar *)&signature);
529 exp_sdt.aml_file = aml_file;
530 g_assert(g_file_test(aml_file, G_FILE_TEST_EXISTS));
531 ret = g_file_get_contents(aml_file, &exp_sdt.aml,
532 &exp_sdt.aml_len, &error);
533 g_assert(ret);
534 g_assert_no_error(error);
535 g_assert(exp_sdt.aml);
536 g_assert(exp_sdt.aml_len);
538 g_array_append_val(exp_tables, exp_sdt);
541 return exp_tables;
544 static void test_acpi_asl(test_data *data)
546 int i;
547 AcpiSdtTable *sdt, *exp_sdt;
548 test_data exp_data;
549 gboolean exp_err, err;
551 memset(&exp_data, 0, sizeof(exp_data));
552 exp_data.tables = load_expected_aml(data);
553 dump_aml_files(data, false);
554 for (i = 0; i < data->tables->len; ++i) {
555 GString *asl, *exp_asl;
557 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
558 exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
560 err = load_asl(data->tables, sdt);
561 asl = normalize_asl(sdt->asl);
563 exp_err = load_asl(exp_data.tables, exp_sdt);
564 exp_asl = normalize_asl(exp_sdt->asl);
566 /* TODO: check for warnings */
567 g_assert(!err || exp_err);
569 if (g_strcmp0(asl->str, exp_asl->str)) {
570 if (exp_err) {
571 fprintf(stderr,
572 "Warning! iasl couldn't parse the expected aml\n");
573 } else {
574 uint32_t signature = cpu_to_le32(exp_sdt->header.signature);
575 sdt->tmp_files_retain = true;
576 exp_sdt->tmp_files_retain = true;
577 fprintf(stderr,
578 "acpi-test: Warning! %.4s mismatch. "
579 "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
580 (gchar *)&signature,
581 sdt->asl_file, sdt->aml_file,
582 exp_sdt->asl_file, exp_sdt->aml_file);
585 g_string_free(asl, true);
586 g_string_free(exp_asl, true);
589 free_test_data(&exp_data);
592 static void test_smbios_ep_address(test_data *data)
594 uint32_t off;
596 /* find smbios entry point structure */
597 for (off = 0xf0000; off < 0x100000; off += 0x10) {
598 uint8_t sig[] = "_SM_";
599 int i;
601 for (i = 0; i < sizeof sig - 1; ++i) {
602 sig[i] = readb(off + i);
605 if (!memcmp(sig, "_SM_", sizeof sig)) {
606 break;
610 g_assert_cmphex(off, <, 0x100000);
611 data->smbios_ep_addr = off;
614 static void test_smbios_ep_table(test_data *data)
616 struct smbios_entry_point *ep_table = &data->smbios_ep_table;
617 uint32_t addr = data->smbios_ep_addr;
619 ACPI_READ_ARRAY(ep_table->anchor_string, addr);
620 g_assert(!memcmp(ep_table->anchor_string, "_SM_", 4));
621 ACPI_READ_FIELD(ep_table->checksum, addr);
622 ACPI_READ_FIELD(ep_table->length, addr);
623 ACPI_READ_FIELD(ep_table->smbios_major_version, addr);
624 ACPI_READ_FIELD(ep_table->smbios_minor_version, addr);
625 ACPI_READ_FIELD(ep_table->max_structure_size, addr);
626 ACPI_READ_FIELD(ep_table->entry_point_revision, addr);
627 ACPI_READ_ARRAY(ep_table->formatted_area, addr);
628 ACPI_READ_ARRAY(ep_table->intermediate_anchor_string, addr);
629 g_assert(!memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5));
630 ACPI_READ_FIELD(ep_table->intermediate_checksum, addr);
631 ACPI_READ_FIELD(ep_table->structure_table_length, addr);
632 g_assert_cmpuint(ep_table->structure_table_length, >, 0);
633 ACPI_READ_FIELD(ep_table->structure_table_address, addr);
634 ACPI_READ_FIELD(ep_table->number_of_structures, addr);
635 g_assert_cmpuint(ep_table->number_of_structures, >, 0);
636 ACPI_READ_FIELD(ep_table->smbios_bcd_revision, addr);
637 g_assert(!acpi_checksum((uint8_t *)ep_table, sizeof *ep_table));
638 g_assert(!acpi_checksum((uint8_t *)ep_table + 0x10,
639 sizeof *ep_table - 0x10));
642 static inline bool smbios_single_instance(uint8_t type)
644 switch (type) {
645 case 0:
646 case 1:
647 case 2:
648 case 3:
649 case 16:
650 case 32:
651 case 127:
652 return true;
653 default:
654 return false;
658 static void test_smbios_structs(test_data *data)
660 DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };
661 struct smbios_entry_point *ep_table = &data->smbios_ep_table;
662 uint32_t addr = ep_table->structure_table_address;
663 int i, len, max_len = 0;
664 uint8_t type, prv, crt;
665 uint8_t required_struct_types[] = {0, 1, 3, 4, 16, 17, 19, 32, 127};
667 /* walk the smbios tables */
668 for (i = 0; i < ep_table->number_of_structures; i++) {
670 /* grab type and formatted area length from struct header */
671 type = readb(addr);
672 g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
673 len = readb(addr + 1);
675 /* single-instance structs must not have been encountered before */
676 if (smbios_single_instance(type)) {
677 g_assert(!test_bit(type, struct_bitmap));
679 set_bit(type, struct_bitmap);
681 /* seek to end of unformatted string area of this struct ("\0\0") */
682 prv = crt = 1;
683 while (prv || crt) {
684 prv = crt;
685 crt = readb(addr + len);
686 len++;
689 /* keep track of max. struct size */
690 if (max_len < len) {
691 max_len = len;
692 g_assert_cmpuint(max_len, <=, ep_table->max_structure_size);
695 /* start of next structure */
696 addr += len;
699 /* total table length and max struct size must match entry point values */
700 g_assert_cmpuint(ep_table->structure_table_length, ==,
701 addr - ep_table->structure_table_address);
702 g_assert_cmpuint(ep_table->max_structure_size, ==, max_len);
704 /* required struct types must all be present */
705 for (i = 0; i < ARRAY_SIZE(required_struct_types); i++) {
706 g_assert(test_bit(required_struct_types[i], struct_bitmap));
710 static void test_acpi_one(const char *params, test_data *data)
712 char *args;
713 uint8_t signature_low;
714 uint8_t signature_high;
715 uint16_t signature;
716 int i;
718 args = g_strdup_printf("-net none -display none %s "
719 "-drive id=hd0,if=none,file=%s,format=raw "
720 "-device ide-hd,drive=hd0 ",
721 params ? params : "", disk);
723 qtest_start(args);
725 /* Wait at most 1 minute */
726 #define TEST_DELAY (1 * G_USEC_PER_SEC / 10)
727 #define TEST_CYCLES MAX((60 * G_USEC_PER_SEC / TEST_DELAY), 1)
729 /* Poll until code has run and modified memory. Once it has we know BIOS
730 * initialization is done. TODO: check that IP reached the halt
731 * instruction.
733 for (i = 0; i < TEST_CYCLES; ++i) {
734 signature_low = readb(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET);
735 signature_high = readb(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET + 1);
736 signature = (signature_high << 8) | signature_low;
737 if (signature == SIGNATURE) {
738 break;
740 g_usleep(TEST_DELAY);
742 g_assert_cmphex(signature, ==, SIGNATURE);
744 test_acpi_rsdp_address(data);
745 test_acpi_rsdp_table(data);
746 test_acpi_rsdt_table(data);
747 test_acpi_fadt_table(data);
748 test_acpi_facs_table(data);
749 test_acpi_dsdt_table(data);
750 test_acpi_tables(data);
752 if (iasl) {
753 if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
754 dump_aml_files(data, true);
755 } else {
756 test_acpi_asl(data);
760 test_smbios_ep_address(data);
761 test_smbios_ep_table(data);
762 test_smbios_structs(data);
764 qtest_quit(global_qtest);
765 g_free(args);
768 static void test_acpi_piix4_tcg(void)
770 test_data data;
772 /* Supplying -machine accel argument overrides the default (qtest).
773 * This is to make guest actually run.
775 memset(&data, 0, sizeof(data));
776 data.machine = MACHINE_PC;
777 test_acpi_one("-machine accel=tcg", &data);
778 free_test_data(&data);
781 static void test_acpi_q35_tcg(void)
783 test_data data;
785 memset(&data, 0, sizeof(data));
786 data.machine = MACHINE_Q35;
787 test_acpi_one("-machine q35,accel=tcg", &data);
788 free_test_data(&data);
791 int main(int argc, char *argv[])
793 const char *arch = qtest_get_arch();
794 FILE *f = fopen(disk, "w");
795 int ret;
797 if (!f) {
798 fprintf(stderr, "Couldn't open \"%s\": %s", disk, strerror(errno));
799 return 1;
801 fwrite(boot_sector, 1, sizeof boot_sector, f);
802 fclose(f);
804 g_test_init(&argc, &argv, NULL);
806 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
807 qtest_add_func("acpi/piix4/tcg", test_acpi_piix4_tcg);
808 qtest_add_func("acpi/q35/tcg", test_acpi_q35_tcg);
810 ret = g_test_run();
811 unlink(disk);
812 return ret;