trace: split out trace events for qom/ directory
[qemu/ar7.git] / tests / bios-tables-test.c
blob16d11aa85448a4102401ecbbb7e040fe0b11715c
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 "qemu/osdep.h"
14 #include <glib/gstdio.h>
15 #include "qemu-common.h"
16 #include "libqtest.h"
17 #include "hw/acpi/acpi-defs.h"
18 #include "hw/smbios/smbios.h"
19 #include "qemu/bitmap.h"
20 #include "boot-sector.h"
22 #define MACHINE_PC "pc"
23 #define MACHINE_Q35 "q35"
25 #define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"
27 /* DSDT and SSDTs format */
28 typedef struct {
29 AcpiTableHeader header;
30 gchar *aml; /* aml bytecode from guest */
31 gsize aml_len;
32 gchar *aml_file;
33 gchar *asl; /* asl code generated from aml */
34 gsize asl_len;
35 gchar *asl_file;
36 bool tmp_files_retain; /* do not delete the temp asl/aml */
37 } QEMU_PACKED AcpiSdtTable;
39 typedef struct {
40 const char *machine;
41 const char *variant;
42 uint32_t rsdp_addr;
43 AcpiRsdpDescriptor rsdp_table;
44 AcpiRsdtDescriptorRev1 rsdt_table;
45 AcpiFadtDescriptorRev1 fadt_table;
46 AcpiFacsDescriptorRev1 facs_table;
47 uint32_t *rsdt_tables_addr;
48 int rsdt_tables_nr;
49 GArray *tables;
50 uint32_t smbios_ep_addr;
51 struct smbios_21_entry_point smbios_ep_table;
52 } test_data;
54 #define ACPI_READ_FIELD(field, addr) \
55 do { \
56 switch (sizeof(field)) { \
57 case 1: \
58 field = readb(addr); \
59 break; \
60 case 2: \
61 field = readw(addr); \
62 break; \
63 case 4: \
64 field = readl(addr); \
65 break; \
66 case 8: \
67 field = readq(addr); \
68 break; \
69 default: \
70 g_assert(false); \
71 } \
72 addr += sizeof(field); \
73 } while (0);
75 #define ACPI_READ_ARRAY_PTR(arr, length, addr) \
76 do { \
77 int idx; \
78 for (idx = 0; idx < length; ++idx) { \
79 ACPI_READ_FIELD(arr[idx], addr); \
80 } \
81 } while (0);
83 #define ACPI_READ_ARRAY(arr, addr) \
84 ACPI_READ_ARRAY_PTR(arr, sizeof(arr)/sizeof(arr[0]), addr)
86 #define ACPI_READ_TABLE_HEADER(table, addr) \
87 do { \
88 ACPI_READ_FIELD((table)->signature, addr); \
89 ACPI_READ_FIELD((table)->length, addr); \
90 ACPI_READ_FIELD((table)->revision, addr); \
91 ACPI_READ_FIELD((table)->checksum, addr); \
92 ACPI_READ_ARRAY((table)->oem_id, addr); \
93 ACPI_READ_ARRAY((table)->oem_table_id, addr); \
94 ACPI_READ_FIELD((table)->oem_revision, addr); \
95 ACPI_READ_ARRAY((table)->asl_compiler_id, addr); \
96 ACPI_READ_FIELD((table)->asl_compiler_revision, addr); \
97 } while (0);
99 #define ACPI_ASSERT_CMP(actual, expected) do { \
100 uint32_t ACPI_ASSERT_CMP_le = cpu_to_le32(actual); \
101 char ACPI_ASSERT_CMP_str[5] = {}; \
102 memcpy(ACPI_ASSERT_CMP_str, &ACPI_ASSERT_CMP_le, 4); \
103 g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
104 } while (0)
106 #define ACPI_ASSERT_CMP64(actual, expected) do { \
107 uint64_t ACPI_ASSERT_CMP_le = cpu_to_le64(actual); \
108 char ACPI_ASSERT_CMP_str[9] = {}; \
109 memcpy(ACPI_ASSERT_CMP_str, &ACPI_ASSERT_CMP_le, 8); \
110 g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
111 } while (0)
113 static const char *disk = "tests/acpi-test-disk.raw";
114 static const char *data_dir = "tests/acpi-test-data";
115 #ifdef CONFIG_IASL
116 static const char *iasl = stringify(CONFIG_IASL);
117 #else
118 static const char *iasl;
119 #endif
121 static void free_test_data(test_data *data)
123 AcpiSdtTable *temp;
124 int i;
126 g_free(data->rsdt_tables_addr);
128 for (i = 0; i < data->tables->len; ++i) {
129 temp = &g_array_index(data->tables, AcpiSdtTable, i);
130 g_free(temp->aml);
131 if (temp->aml_file &&
132 !temp->tmp_files_retain &&
133 g_strstr_len(temp->aml_file, -1, "aml-")) {
134 unlink(temp->aml_file);
136 g_free(temp->aml_file);
137 g_free(temp->asl);
138 if (temp->asl_file &&
139 !temp->tmp_files_retain) {
140 unlink(temp->asl_file);
142 g_free(temp->asl_file);
145 g_array_free(data->tables, false);
148 static uint8_t acpi_checksum(const uint8_t *data, int len)
150 int i;
151 uint8_t sum = 0;
153 for (i = 0; i < len; i++) {
154 sum += data[i];
157 return sum;
160 static void test_acpi_rsdp_address(test_data *data)
162 uint32_t off;
164 /* OK, now find RSDP */
165 for (off = 0xf0000; off < 0x100000; off += 0x10) {
166 uint8_t sig[] = "RSD PTR ";
167 int i;
169 for (i = 0; i < sizeof sig - 1; ++i) {
170 sig[i] = readb(off + i);
173 if (!memcmp(sig, "RSD PTR ", sizeof sig)) {
174 break;
178 g_assert_cmphex(off, <, 0x100000);
179 data->rsdp_addr = off;
182 static void test_acpi_rsdp_table(test_data *data)
184 AcpiRsdpDescriptor *rsdp_table = &data->rsdp_table;
185 uint32_t addr = data->rsdp_addr;
187 ACPI_READ_FIELD(rsdp_table->signature, addr);
188 ACPI_ASSERT_CMP64(rsdp_table->signature, "RSD PTR ");
190 ACPI_READ_FIELD(rsdp_table->checksum, addr);
191 ACPI_READ_ARRAY(rsdp_table->oem_id, addr);
192 ACPI_READ_FIELD(rsdp_table->revision, addr);
193 ACPI_READ_FIELD(rsdp_table->rsdt_physical_address, addr);
194 ACPI_READ_FIELD(rsdp_table->length, addr);
196 /* rsdp checksum is not for the whole table, but for the first 20 bytes */
197 g_assert(!acpi_checksum((uint8_t *)rsdp_table, 20));
200 static void test_acpi_rsdt_table(test_data *data)
202 AcpiRsdtDescriptorRev1 *rsdt_table = &data->rsdt_table;
203 uint32_t addr = data->rsdp_table.rsdt_physical_address;
204 uint32_t *tables;
205 int tables_nr;
206 uint8_t checksum;
208 /* read the header */
209 ACPI_READ_TABLE_HEADER(rsdt_table, addr);
210 ACPI_ASSERT_CMP(rsdt_table->signature, "RSDT");
212 /* compute the table entries in rsdt */
213 tables_nr = (rsdt_table->length - sizeof(AcpiRsdtDescriptorRev1)) /
214 sizeof(uint32_t);
215 g_assert_cmpint(tables_nr, >, 0);
217 /* get the addresses of the tables pointed by rsdt */
218 tables = g_new0(uint32_t, tables_nr);
219 ACPI_READ_ARRAY_PTR(tables, tables_nr, addr);
221 checksum = acpi_checksum((uint8_t *)rsdt_table, rsdt_table->length) +
222 acpi_checksum((uint8_t *)tables, tables_nr * sizeof(uint32_t));
223 g_assert(!checksum);
225 /* SSDT tables after FADT */
226 data->rsdt_tables_addr = tables;
227 data->rsdt_tables_nr = tables_nr;
230 static void test_acpi_fadt_table(test_data *data)
232 AcpiFadtDescriptorRev1 *fadt_table = &data->fadt_table;
233 uint32_t addr;
235 /* FADT table comes first */
236 addr = data->rsdt_tables_addr[0];
237 ACPI_READ_TABLE_HEADER(fadt_table, addr);
239 ACPI_READ_FIELD(fadt_table->firmware_ctrl, addr);
240 ACPI_READ_FIELD(fadt_table->dsdt, addr);
241 ACPI_READ_FIELD(fadt_table->model, addr);
242 ACPI_READ_FIELD(fadt_table->reserved1, addr);
243 ACPI_READ_FIELD(fadt_table->sci_int, addr);
244 ACPI_READ_FIELD(fadt_table->smi_cmd, addr);
245 ACPI_READ_FIELD(fadt_table->acpi_enable, addr);
246 ACPI_READ_FIELD(fadt_table->acpi_disable, addr);
247 ACPI_READ_FIELD(fadt_table->S4bios_req, addr);
248 ACPI_READ_FIELD(fadt_table->reserved2, addr);
249 ACPI_READ_FIELD(fadt_table->pm1a_evt_blk, addr);
250 ACPI_READ_FIELD(fadt_table->pm1b_evt_blk, addr);
251 ACPI_READ_FIELD(fadt_table->pm1a_cnt_blk, addr);
252 ACPI_READ_FIELD(fadt_table->pm1b_cnt_blk, addr);
253 ACPI_READ_FIELD(fadt_table->pm2_cnt_blk, addr);
254 ACPI_READ_FIELD(fadt_table->pm_tmr_blk, addr);
255 ACPI_READ_FIELD(fadt_table->gpe0_blk, addr);
256 ACPI_READ_FIELD(fadt_table->gpe1_blk, addr);
257 ACPI_READ_FIELD(fadt_table->pm1_evt_len, addr);
258 ACPI_READ_FIELD(fadt_table->pm1_cnt_len, addr);
259 ACPI_READ_FIELD(fadt_table->pm2_cnt_len, addr);
260 ACPI_READ_FIELD(fadt_table->pm_tmr_len, addr);
261 ACPI_READ_FIELD(fadt_table->gpe0_blk_len, addr);
262 ACPI_READ_FIELD(fadt_table->gpe1_blk_len, addr);
263 ACPI_READ_FIELD(fadt_table->gpe1_base, addr);
264 ACPI_READ_FIELD(fadt_table->reserved3, addr);
265 ACPI_READ_FIELD(fadt_table->plvl2_lat, addr);
266 ACPI_READ_FIELD(fadt_table->plvl3_lat, addr);
267 ACPI_READ_FIELD(fadt_table->flush_size, addr);
268 ACPI_READ_FIELD(fadt_table->flush_stride, addr);
269 ACPI_READ_FIELD(fadt_table->duty_offset, addr);
270 ACPI_READ_FIELD(fadt_table->duty_width, addr);
271 ACPI_READ_FIELD(fadt_table->day_alrm, addr);
272 ACPI_READ_FIELD(fadt_table->mon_alrm, addr);
273 ACPI_READ_FIELD(fadt_table->century, addr);
274 ACPI_READ_FIELD(fadt_table->reserved4, addr);
275 ACPI_READ_FIELD(fadt_table->reserved4a, addr);
276 ACPI_READ_FIELD(fadt_table->reserved4b, addr);
277 ACPI_READ_FIELD(fadt_table->flags, addr);
279 ACPI_ASSERT_CMP(fadt_table->signature, "FACP");
280 g_assert(!acpi_checksum((uint8_t *)fadt_table, fadt_table->length));
283 static void test_acpi_facs_table(test_data *data)
285 AcpiFacsDescriptorRev1 *facs_table = &data->facs_table;
286 uint32_t addr = data->fadt_table.firmware_ctrl;
288 ACPI_READ_FIELD(facs_table->signature, addr);
289 ACPI_READ_FIELD(facs_table->length, addr);
290 ACPI_READ_FIELD(facs_table->hardware_signature, addr);
291 ACPI_READ_FIELD(facs_table->firmware_waking_vector, addr);
292 ACPI_READ_FIELD(facs_table->global_lock, addr);
293 ACPI_READ_FIELD(facs_table->flags, addr);
294 ACPI_READ_ARRAY(facs_table->resverved3, addr);
296 ACPI_ASSERT_CMP(facs_table->signature, "FACS");
299 static void test_dst_table(AcpiSdtTable *sdt_table, uint32_t addr)
301 uint8_t checksum;
303 ACPI_READ_TABLE_HEADER(&sdt_table->header, addr);
305 sdt_table->aml_len = sdt_table->header.length - sizeof(AcpiTableHeader);
306 sdt_table->aml = g_malloc0(sdt_table->aml_len);
307 ACPI_READ_ARRAY_PTR(sdt_table->aml, sdt_table->aml_len, addr);
309 checksum = acpi_checksum((uint8_t *)sdt_table, sizeof(AcpiTableHeader)) +
310 acpi_checksum((uint8_t *)sdt_table->aml, sdt_table->aml_len);
311 g_assert(!checksum);
314 static void test_acpi_dsdt_table(test_data *data)
316 AcpiSdtTable dsdt_table;
317 uint32_t addr = data->fadt_table.dsdt;
319 memset(&dsdt_table, 0, sizeof(dsdt_table));
320 data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
322 test_dst_table(&dsdt_table, addr);
323 ACPI_ASSERT_CMP(dsdt_table.header.signature, "DSDT");
325 /* Place DSDT first */
326 g_array_append_val(data->tables, dsdt_table);
329 static void test_acpi_tables(test_data *data)
331 int tables_nr = data->rsdt_tables_nr - 1; /* fadt is first */
332 int i;
334 for (i = 0; i < tables_nr; i++) {
335 AcpiSdtTable ssdt_table;
337 memset(&ssdt_table, 0 , sizeof(ssdt_table));
338 uint32_t addr = data->rsdt_tables_addr[i + 1]; /* fadt is first */
339 test_dst_table(&ssdt_table, addr);
340 g_array_append_val(data->tables, ssdt_table);
344 static void dump_aml_files(test_data *data, bool rebuild)
346 AcpiSdtTable *sdt;
347 GError *error = NULL;
348 gchar *aml_file = NULL;
349 gint fd;
350 ssize_t ret;
351 int i;
353 for (i = 0; i < data->tables->len; ++i) {
354 const char *ext = data->variant ? data->variant : "";
355 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
356 g_assert(sdt->aml);
358 if (rebuild) {
359 uint32_t signature = cpu_to_le32(sdt->header.signature);
360 aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
361 (gchar *)&signature, ext);
362 fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
363 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
364 } else {
365 fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
366 g_assert_no_error(error);
368 g_assert(fd >= 0);
370 ret = qemu_write_full(fd, sdt, sizeof(AcpiTableHeader));
371 g_assert(ret == sizeof(AcpiTableHeader));
372 ret = qemu_write_full(fd, sdt->aml, sdt->aml_len);
373 g_assert(ret == sdt->aml_len);
375 close(fd);
377 g_free(aml_file);
381 static bool compare_signature(AcpiSdtTable *sdt, const char *signature)
383 return !memcmp(&sdt->header.signature, signature, 4);
386 static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
388 AcpiSdtTable *temp;
389 GError *error = NULL;
390 GString *command_line = g_string_new(iasl);
391 gint fd;
392 gchar *out, *out_err;
393 gboolean ret;
394 int i;
396 fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
397 g_assert_no_error(error);
398 close(fd);
400 /* build command line */
401 g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
402 if (compare_signature(sdt, "DSDT") ||
403 compare_signature(sdt, "SSDT")) {
404 for (i = 0; i < sdts->len; ++i) {
405 temp = &g_array_index(sdts, AcpiSdtTable, i);
406 if (compare_signature(temp, "DSDT") ||
407 compare_signature(temp, "SSDT")) {
408 g_string_append_printf(command_line, "-e %s ", temp->aml_file);
412 g_string_append_printf(command_line, "-d %s", sdt->aml_file);
414 /* pass 'out' and 'out_err' in order to be redirected */
415 ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
416 g_assert_no_error(error);
417 if (ret) {
418 ret = g_file_get_contents(sdt->asl_file, (gchar **)&sdt->asl,
419 &sdt->asl_len, &error);
420 g_assert(ret);
421 g_assert_no_error(error);
422 ret = (sdt->asl_len > 0);
425 g_free(out);
426 g_free(out_err);
427 g_string_free(command_line, true);
429 return !ret;
432 #define COMMENT_END "*/"
433 #define DEF_BLOCK "DefinitionBlock ("
434 #define BLOCK_NAME_END ","
436 static GString *normalize_asl(gchar *asl_code)
438 GString *asl = g_string_new(asl_code);
439 gchar *comment, *block_name;
441 /* strip comments (different generation days) */
442 comment = g_strstr_len(asl->str, asl->len, COMMENT_END);
443 if (comment) {
444 comment += strlen(COMMENT_END);
445 while (*comment == '\n') {
446 comment++;
448 asl = g_string_erase(asl, 0, comment - asl->str);
451 /* strip def block name (it has file path in it) */
452 if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
453 block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
454 g_assert(block_name);
455 asl = g_string_erase(asl, 0,
456 block_name + sizeof(BLOCK_NAME_END) - asl->str);
459 return asl;
462 static GArray *load_expected_aml(test_data *data)
464 int i;
465 AcpiSdtTable *sdt;
466 GError *error = NULL;
467 gboolean ret;
469 GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
470 for (i = 0; i < data->tables->len; ++i) {
471 AcpiSdtTable exp_sdt;
472 uint32_t signature;
473 gchar *aml_file = NULL;
474 const char *ext = data->variant ? data->variant : "";
476 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
478 memset(&exp_sdt, 0, sizeof(exp_sdt));
479 exp_sdt.header.signature = sdt->header.signature;
481 signature = cpu_to_le32(sdt->header.signature);
483 try_again:
484 aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
485 (gchar *)&signature, ext);
486 if (getenv("V")) {
487 fprintf(stderr, "\nLooking for expected file '%s'\n", aml_file);
489 if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) {
490 exp_sdt.aml_file = aml_file;
491 } else if (*ext != '\0') {
492 /* try fallback to generic (extention less) expected file */
493 ext = "";
494 g_free(aml_file);
495 goto try_again;
497 g_assert(exp_sdt.aml_file);
498 if (getenv("V")) {
499 fprintf(stderr, "\nUsing expected file '%s'\n", aml_file);
501 ret = g_file_get_contents(aml_file, &exp_sdt.aml,
502 &exp_sdt.aml_len, &error);
503 g_assert(ret);
504 g_assert_no_error(error);
505 g_assert(exp_sdt.aml);
506 g_assert(exp_sdt.aml_len);
508 g_array_append_val(exp_tables, exp_sdt);
511 return exp_tables;
514 static void test_acpi_asl(test_data *data)
516 int i;
517 AcpiSdtTable *sdt, *exp_sdt;
518 test_data exp_data;
519 gboolean exp_err, err;
521 memset(&exp_data, 0, sizeof(exp_data));
522 exp_data.tables = load_expected_aml(data);
523 dump_aml_files(data, false);
524 for (i = 0; i < data->tables->len; ++i) {
525 GString *asl, *exp_asl;
527 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
528 exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
530 err = load_asl(data->tables, sdt);
531 asl = normalize_asl(sdt->asl);
533 exp_err = load_asl(exp_data.tables, exp_sdt);
534 exp_asl = normalize_asl(exp_sdt->asl);
536 /* TODO: check for warnings */
537 g_assert(!err || exp_err);
539 if (g_strcmp0(asl->str, exp_asl->str)) {
540 if (exp_err) {
541 fprintf(stderr,
542 "Warning! iasl couldn't parse the expected aml\n");
543 } else {
544 uint32_t signature = cpu_to_le32(exp_sdt->header.signature);
545 sdt->tmp_files_retain = true;
546 exp_sdt->tmp_files_retain = true;
547 fprintf(stderr,
548 "acpi-test: Warning! %.4s mismatch. "
549 "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
550 (gchar *)&signature,
551 sdt->asl_file, sdt->aml_file,
552 exp_sdt->asl_file, exp_sdt->aml_file);
553 if (getenv("V")) {
554 const char *diff_cmd = getenv("DIFF");
555 if (diff_cmd) {
556 int ret G_GNUC_UNUSED;
557 char *diff = g_strdup_printf("%s %s %s", diff_cmd,
558 exp_sdt->asl_file, sdt->asl_file);
559 ret = system(diff) ;
560 g_free(diff);
561 } else {
562 fprintf(stderr, "acpi-test: Warning. not showing "
563 "difference since no diff utility is specified. "
564 "Set 'DIFF' environment variable to a preferred "
565 "diff utility and run 'make V=1 check' again to "
566 "see ASL difference.");
571 g_string_free(asl, true);
572 g_string_free(exp_asl, true);
575 free_test_data(&exp_data);
578 static bool smbios_ep_table_ok(test_data *data)
580 struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
581 uint32_t addr = data->smbios_ep_addr;
583 ACPI_READ_ARRAY(ep_table->anchor_string, addr);
584 if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
585 return false;
587 ACPI_READ_FIELD(ep_table->checksum, addr);
588 ACPI_READ_FIELD(ep_table->length, addr);
589 ACPI_READ_FIELD(ep_table->smbios_major_version, addr);
590 ACPI_READ_FIELD(ep_table->smbios_minor_version, addr);
591 ACPI_READ_FIELD(ep_table->max_structure_size, addr);
592 ACPI_READ_FIELD(ep_table->entry_point_revision, addr);
593 ACPI_READ_ARRAY(ep_table->formatted_area, addr);
594 ACPI_READ_ARRAY(ep_table->intermediate_anchor_string, addr);
595 if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
596 return false;
598 ACPI_READ_FIELD(ep_table->intermediate_checksum, addr);
599 ACPI_READ_FIELD(ep_table->structure_table_length, addr);
600 if (ep_table->structure_table_length == 0) {
601 return false;
603 ACPI_READ_FIELD(ep_table->structure_table_address, addr);
604 ACPI_READ_FIELD(ep_table->number_of_structures, addr);
605 if (ep_table->number_of_structures == 0) {
606 return false;
608 ACPI_READ_FIELD(ep_table->smbios_bcd_revision, addr);
609 if (acpi_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
610 acpi_checksum((uint8_t *)ep_table + 0x10, sizeof *ep_table - 0x10)) {
611 return false;
613 return true;
616 static void test_smbios_entry_point(test_data *data)
618 uint32_t off;
620 /* find smbios entry point structure */
621 for (off = 0xf0000; off < 0x100000; off += 0x10) {
622 uint8_t sig[] = "_SM_";
623 int i;
625 for (i = 0; i < sizeof sig - 1; ++i) {
626 sig[i] = readb(off + i);
629 if (!memcmp(sig, "_SM_", sizeof sig)) {
630 /* signature match, but is this a valid entry point? */
631 data->smbios_ep_addr = off;
632 if (smbios_ep_table_ok(data)) {
633 break;
638 g_assert_cmphex(off, <, 0x100000);
641 static inline bool smbios_single_instance(uint8_t type)
643 switch (type) {
644 case 0:
645 case 1:
646 case 2:
647 case 3:
648 case 16:
649 case 32:
650 case 127:
651 return true;
652 default:
653 return false;
657 static void test_smbios_structs(test_data *data)
659 DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };
660 struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
661 uint32_t addr = ep_table->structure_table_address;
662 int i, len, max_len = 0;
663 uint8_t type, prv, crt;
664 uint8_t required_struct_types[] = {0, 1, 3, 4, 16, 17, 19, 32, 127};
666 /* walk the smbios tables */
667 for (i = 0; i < ep_table->number_of_structures; i++) {
669 /* grab type and formatted area length from struct header */
670 type = readb(addr);
671 g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE);
672 len = readb(addr + 1);
674 /* single-instance structs must not have been encountered before */
675 if (smbios_single_instance(type)) {
676 g_assert(!test_bit(type, struct_bitmap));
678 set_bit(type, struct_bitmap);
680 /* seek to end of unformatted string area of this struct ("\0\0") */
681 prv = crt = 1;
682 while (prv || crt) {
683 prv = crt;
684 crt = readb(addr + len);
685 len++;
688 /* keep track of max. struct size */
689 if (max_len < len) {
690 max_len = len;
691 g_assert_cmpuint(max_len, <=, ep_table->max_structure_size);
694 /* start of next structure */
695 addr += len;
698 /* total table length and max struct size must match entry point values */
699 g_assert_cmpuint(ep_table->structure_table_length, ==,
700 addr - ep_table->structure_table_address);
701 g_assert_cmpuint(ep_table->max_structure_size, ==, max_len);
703 /* required struct types must all be present */
704 for (i = 0; i < ARRAY_SIZE(required_struct_types); i++) {
705 g_assert(test_bit(required_struct_types[i], struct_bitmap));
709 static void test_acpi_one(const char *params, test_data *data)
711 char *args;
713 args = g_strdup_printf("-net none -display none %s "
714 "-drive id=hd0,if=none,file=%s,format=raw "
715 "-device ide-hd,drive=hd0 ",
716 params ? params : "", disk);
718 qtest_start(args);
720 boot_sector_test();
722 test_acpi_rsdp_address(data);
723 test_acpi_rsdp_table(data);
724 test_acpi_rsdt_table(data);
725 test_acpi_fadt_table(data);
726 test_acpi_facs_table(data);
727 test_acpi_dsdt_table(data);
728 test_acpi_tables(data);
730 if (iasl) {
731 if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
732 dump_aml_files(data, true);
733 } else {
734 test_acpi_asl(data);
738 test_smbios_entry_point(data);
739 test_smbios_structs(data);
741 qtest_quit(global_qtest);
742 g_free(args);
745 static void test_acpi_piix4_tcg(void)
747 test_data data;
749 /* Supplying -machine accel argument overrides the default (qtest).
750 * This is to make guest actually run.
752 memset(&data, 0, sizeof(data));
753 data.machine = MACHINE_PC;
754 test_acpi_one("-machine accel=tcg", &data);
755 free_test_data(&data);
758 static void test_acpi_piix4_tcg_bridge(void)
760 test_data data;
762 memset(&data, 0, sizeof(data));
763 data.machine = MACHINE_PC;
764 data.variant = ".bridge";
765 test_acpi_one("-machine accel=tcg -device pci-bridge,chassis_nr=1", &data);
766 free_test_data(&data);
769 static void test_acpi_q35_tcg(void)
771 test_data data;
773 memset(&data, 0, sizeof(data));
774 data.machine = MACHINE_Q35;
775 test_acpi_one("-machine q35,accel=tcg", &data);
776 free_test_data(&data);
779 static void test_acpi_q35_tcg_bridge(void)
781 test_data data;
783 memset(&data, 0, sizeof(data));
784 data.machine = MACHINE_Q35;
785 data.variant = ".bridge";
786 test_acpi_one("-machine q35,accel=tcg -device pci-bridge,chassis_nr=1",
787 &data);
788 free_test_data(&data);
791 int main(int argc, char *argv[])
793 const char *arch = qtest_get_arch();
794 int ret;
796 ret = boot_sector_init(disk);
797 if(ret)
798 return ret;
800 g_test_init(&argc, &argv, NULL);
802 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
803 qtest_add_func("acpi/piix4/tcg", test_acpi_piix4_tcg);
804 qtest_add_func("acpi/piix4/tcg/bridge", test_acpi_piix4_tcg_bridge);
805 qtest_add_func("acpi/q35/tcg", test_acpi_q35_tcg);
806 qtest_add_func("acpi/q35/tcg/bridge", test_acpi_q35_tcg_bridge);
808 ret = g_test_run();
809 boot_sector_cleanup(disk);
810 return ret;