ahci: map memory via device's address space instead of address_space_memory
[qemu.git] / tests / bios-tables-test.c
blob62771f760864f304e96133b08945f8b92d6a66e4
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 asl = g_string_erase(asl, 0, comment + sizeof(COMMENT_END) - asl->str);
493 /* strip def block name (it has file path in it) */
494 if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
495 block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
496 g_assert(block_name);
497 asl = g_string_erase(asl, 0,
498 block_name + sizeof(BLOCK_NAME_END) - asl->str);
501 return asl;
504 static GArray *load_expected_aml(test_data *data)
506 int i;
507 AcpiSdtTable *sdt;
508 gchar *aml_file;
509 GError *error = NULL;
510 gboolean ret;
512 GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
513 for (i = 0; i < data->tables->len; ++i) {
514 AcpiSdtTable exp_sdt;
515 uint32_t signature;
517 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
519 memset(&exp_sdt, 0, sizeof(exp_sdt));
520 exp_sdt.header.signature = sdt->header.signature;
522 signature = cpu_to_le32(sdt->header.signature);
523 aml_file = g_strdup_printf("%s/%s/%.4s", data_dir, data->machine,
524 (gchar *)&signature);
525 exp_sdt.aml_file = aml_file;
526 g_assert(g_file_test(aml_file, G_FILE_TEST_EXISTS));
527 ret = g_file_get_contents(aml_file, &exp_sdt.aml,
528 &exp_sdt.aml_len, &error);
529 g_assert(ret);
530 g_assert_no_error(error);
531 g_assert(exp_sdt.aml);
532 g_assert(exp_sdt.aml_len);
534 g_array_append_val(exp_tables, exp_sdt);
537 return exp_tables;
540 static void test_acpi_asl(test_data *data)
542 int i;
543 AcpiSdtTable *sdt, *exp_sdt;
544 test_data exp_data;
545 gboolean exp_err, err;
547 memset(&exp_data, 0, sizeof(exp_data));
548 exp_data.tables = load_expected_aml(data);
549 dump_aml_files(data, false);
550 for (i = 0; i < data->tables->len; ++i) {
551 GString *asl, *exp_asl;
553 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
554 exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
556 err = load_asl(data->tables, sdt);
557 asl = normalize_asl(sdt->asl);
559 exp_err = load_asl(exp_data.tables, exp_sdt);
560 exp_asl = normalize_asl(exp_sdt->asl);
562 /* TODO: check for warnings */
563 g_assert(!err || exp_err);
565 if (g_strcmp0(asl->str, exp_asl->str)) {
566 if (exp_err) {
567 fprintf(stderr,
568 "Warning! iasl couldn't parse the expected aml\n");
569 } else {
570 uint32_t signature = cpu_to_le32(exp_sdt->header.signature);
571 sdt->tmp_files_retain = true;
572 exp_sdt->tmp_files_retain = true;
573 fprintf(stderr,
574 "acpi-test: Warning! %.4s mismatch. "
575 "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
576 (gchar *)&signature,
577 sdt->asl_file, sdt->aml_file,
578 exp_sdt->asl_file, exp_sdt->aml_file);
581 g_string_free(asl, true);
582 g_string_free(exp_asl, true);
585 free_test_data(&exp_data);
588 static void test_smbios_ep_address(test_data *data)
590 uint32_t off;
592 /* find smbios entry point structure */
593 for (off = 0xf0000; off < 0x100000; off += 0x10) {
594 uint8_t sig[] = "_SM_";
595 int i;
597 for (i = 0; i < sizeof sig - 1; ++i) {
598 sig[i] = readb(off + i);
601 if (!memcmp(sig, "_SM_", sizeof sig)) {
602 break;
606 g_assert_cmphex(off, <, 0x100000);
607 data->smbios_ep_addr = off;
610 static void test_smbios_ep_table(test_data *data)
612 struct smbios_entry_point *ep_table = &data->smbios_ep_table;
613 uint32_t addr = data->smbios_ep_addr;
615 ACPI_READ_ARRAY(ep_table->anchor_string, addr);
616 g_assert(!memcmp(ep_table->anchor_string, "_SM_", 4));
617 ACPI_READ_FIELD(ep_table->checksum, addr);
618 ACPI_READ_FIELD(ep_table->length, addr);
619 ACPI_READ_FIELD(ep_table->smbios_major_version, addr);
620 ACPI_READ_FIELD(ep_table->smbios_minor_version, addr);
621 ACPI_READ_FIELD(ep_table->max_structure_size, addr);
622 ACPI_READ_FIELD(ep_table->entry_point_revision, addr);
623 ACPI_READ_ARRAY(ep_table->formatted_area, addr);
624 ACPI_READ_ARRAY(ep_table->intermediate_anchor_string, addr);
625 g_assert(!memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5));
626 ACPI_READ_FIELD(ep_table->intermediate_checksum, addr);
627 ACPI_READ_FIELD(ep_table->structure_table_length, addr);
628 g_assert_cmpuint(ep_table->structure_table_length, >, 0);
629 ACPI_READ_FIELD(ep_table->structure_table_address, addr);
630 ACPI_READ_FIELD(ep_table->number_of_structures, addr);
631 g_assert_cmpuint(ep_table->number_of_structures, >, 0);
632 ACPI_READ_FIELD(ep_table->smbios_bcd_revision, addr);
633 g_assert(!acpi_checksum((uint8_t *)ep_table, sizeof *ep_table));
634 g_assert(!acpi_checksum((uint8_t *)ep_table + 0x10,
635 sizeof *ep_table - 0x10));
638 static inline bool smbios_single_instance(uint8_t type)
640 switch (type) {
641 case 0:
642 case 1:
643 case 2:
644 case 3:
645 case 16:
646 case 32:
647 case 127:
648 return true;
649 default:
650 return false;
654 static void test_smbios_structs(test_data *data)
656 DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };
657 struct smbios_entry_point *ep_table = &data->smbios_ep_table;
658 uint32_t addr = ep_table->structure_table_address;
659 int i, len, max_len = 0;
660 uint8_t type, prv, crt;
661 uint8_t required_struct_types[] = {0, 1, 3, 4, 16, 17, 19, 32, 127};
663 /* walk the smbios tables */
664 for (i = 0; i < ep_table->number_of_structures; i++) {
666 /* grab type and formatted area length from struct header */
667 type = readb(addr);
668 g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
669 len = readb(addr + 1);
671 /* single-instance structs must not have been encountered before */
672 if (smbios_single_instance(type)) {
673 g_assert(!test_bit(type, struct_bitmap));
675 set_bit(type, struct_bitmap);
677 /* seek to end of unformatted string area of this struct ("\0\0") */
678 prv = crt = 1;
679 while (prv || crt) {
680 prv = crt;
681 crt = readb(addr + len);
682 len++;
685 /* keep track of max. struct size */
686 if (max_len < len) {
687 max_len = len;
688 g_assert_cmpuint(max_len, <=, ep_table->max_structure_size);
691 /* start of next structure */
692 addr += len;
695 /* total table length and max struct size must match entry point values */
696 g_assert_cmpuint(ep_table->structure_table_length, ==,
697 addr - ep_table->structure_table_address);
698 g_assert_cmpuint(ep_table->max_structure_size, ==, max_len);
700 /* required struct types must all be present */
701 for (i = 0; i < ARRAY_SIZE(required_struct_types); i++) {
702 g_assert(test_bit(required_struct_types[i], struct_bitmap));
706 static void test_acpi_one(const char *params, test_data *data)
708 char *args;
709 uint8_t signature_low;
710 uint8_t signature_high;
711 uint16_t signature;
712 int i;
713 const char *device = "";
715 if (!g_strcmp0(data->machine, MACHINE_Q35)) {
716 device = ",id=hd -device ide-hd,drive=hd";
719 args = g_strdup_printf("-net none -display none %s -drive file=%s%s,",
720 params ? params : "", disk, device);
721 qtest_start(args);
723 /* Wait at most 1 minute */
724 #define TEST_DELAY (1 * G_USEC_PER_SEC / 10)
725 #define TEST_CYCLES MAX((60 * G_USEC_PER_SEC / TEST_DELAY), 1)
727 /* Poll until code has run and modified memory. Once it has we know BIOS
728 * initialization is done. TODO: check that IP reached the halt
729 * instruction.
731 for (i = 0; i < TEST_CYCLES; ++i) {
732 signature_low = readb(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET);
733 signature_high = readb(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET + 1);
734 signature = (signature_high << 8) | signature_low;
735 if (signature == SIGNATURE) {
736 break;
738 g_usleep(TEST_DELAY);
740 g_assert_cmphex(signature, ==, SIGNATURE);
742 test_acpi_rsdp_address(data);
743 test_acpi_rsdp_table(data);
744 test_acpi_rsdt_table(data);
745 test_acpi_fadt_table(data);
746 test_acpi_facs_table(data);
747 test_acpi_dsdt_table(data);
748 test_acpi_tables(data);
750 if (iasl) {
751 if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
752 dump_aml_files(data, true);
753 } else {
754 test_acpi_asl(data);
758 test_smbios_ep_address(data);
759 test_smbios_ep_table(data);
760 test_smbios_structs(data);
762 qtest_quit(global_qtest);
763 g_free(args);
766 static void test_acpi_tcg(void)
768 test_data data;
770 /* Supplying -machine accel argument overrides the default (qtest).
771 * This is to make guest actually run.
773 memset(&data, 0, sizeof(data));
774 data.machine = MACHINE_PC;
775 test_acpi_one("-machine accel=tcg", &data);
776 free_test_data(&data);
778 memset(&data, 0, sizeof(data));
779 data.machine = MACHINE_Q35;
780 test_acpi_one("-machine q35,accel=tcg", &data);
781 free_test_data(&data);
784 int main(int argc, char *argv[])
786 const char *arch = qtest_get_arch();
787 FILE *f = fopen(disk, "w");
788 int ret;
789 fwrite(boot_sector, 1, sizeof boot_sector, f);
790 fclose(f);
792 g_test_init(&argc, &argv, NULL);
794 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
795 qtest_add_func("acpi/tcg", test_acpi_tcg);
797 ret = g_test_run();
798 unlink(disk);
799 return ret;