]> git.proxmox.com Git - mirror_qemu.git/blob - tests/bios-tables-test.c
tests: acpi: report names of expected files in verbose mode
[mirror_qemu.git] / tests / bios-tables-test.c
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 #include "qemu/osdep.h"
14 #include <glib.h>
15 #include <glib/gstdio.h>
16 #include "qemu-common.h"
17 #include "libqtest.h"
18 #include "hw/acpi/acpi-defs.h"
19 #include "hw/smbios/smbios.h"
20 #include "qemu/bitmap.h"
21 #include "boot-sector.h"
22
23 #define MACHINE_PC "pc"
24 #define MACHINE_Q35 "q35"
25
26 #define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML"
27
28 /* DSDT and SSDTs format */
29 typedef struct {
30 AcpiTableHeader header;
31 gchar *aml; /* aml bytecode from guest */
32 gsize aml_len;
33 gchar *aml_file;
34 gchar *asl; /* asl code generated from aml */
35 gsize asl_len;
36 gchar *asl_file;
37 bool tmp_files_retain; /* do not delete the temp asl/aml */
38 } QEMU_PACKED AcpiSdtTable;
39
40 typedef struct {
41 const char *machine;
42 const char *variant;
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_21_entry_point smbios_ep_table;
53 } test_data;
54
55 #define ACPI_READ_FIELD(field, addr) \
56 do { \
57 switch (sizeof(field)) { \
58 case 1: \
59 field = readb(addr); \
60 break; \
61 case 2: \
62 field = readw(addr); \
63 break; \
64 case 4: \
65 field = readl(addr); \
66 break; \
67 case 8: \
68 field = readq(addr); \
69 break; \
70 default: \
71 g_assert(false); \
72 } \
73 addr += sizeof(field); \
74 } while (0);
75
76 #define ACPI_READ_ARRAY_PTR(arr, length, addr) \
77 do { \
78 int idx; \
79 for (idx = 0; idx < length; ++idx) { \
80 ACPI_READ_FIELD(arr[idx], addr); \
81 } \
82 } while (0);
83
84 #define ACPI_READ_ARRAY(arr, addr) \
85 ACPI_READ_ARRAY_PTR(arr, sizeof(arr)/sizeof(arr[0]), addr)
86
87 #define ACPI_READ_TABLE_HEADER(table, addr) \
88 do { \
89 ACPI_READ_FIELD((table)->signature, addr); \
90 ACPI_READ_FIELD((table)->length, addr); \
91 ACPI_READ_FIELD((table)->revision, addr); \
92 ACPI_READ_FIELD((table)->checksum, addr); \
93 ACPI_READ_ARRAY((table)->oem_id, addr); \
94 ACPI_READ_ARRAY((table)->oem_table_id, addr); \
95 ACPI_READ_FIELD((table)->oem_revision, addr); \
96 ACPI_READ_ARRAY((table)->asl_compiler_id, addr); \
97 ACPI_READ_FIELD((table)->asl_compiler_revision, addr); \
98 } while (0);
99
100 #define ACPI_ASSERT_CMP(actual, expected) do { \
101 uint32_t ACPI_ASSERT_CMP_le = cpu_to_le32(actual); \
102 char ACPI_ASSERT_CMP_str[5] = {}; \
103 memcpy(ACPI_ASSERT_CMP_str, &ACPI_ASSERT_CMP_le, 4); \
104 g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
105 } while (0)
106
107 #define ACPI_ASSERT_CMP64(actual, expected) do { \
108 uint64_t ACPI_ASSERT_CMP_le = cpu_to_le64(actual); \
109 char ACPI_ASSERT_CMP_str[9] = {}; \
110 memcpy(ACPI_ASSERT_CMP_str, &ACPI_ASSERT_CMP_le, 8); \
111 g_assert_cmpstr(ACPI_ASSERT_CMP_str, ==, expected); \
112 } while (0)
113
114 static const char *disk = "tests/acpi-test-disk.raw";
115 static const char *data_dir = "tests/acpi-test-data";
116 #ifdef CONFIG_IASL
117 static const char *iasl = stringify(CONFIG_IASL);
118 #else
119 static const char *iasl;
120 #endif
121
122 static void free_test_data(test_data *data)
123 {
124 AcpiSdtTable *temp;
125 int i;
126
127 g_free(data->rsdt_tables_addr);
128
129 for (i = 0; i < data->tables->len; ++i) {
130 temp = &g_array_index(data->tables, AcpiSdtTable, i);
131 g_free(temp->aml);
132 if (temp->aml_file &&
133 !temp->tmp_files_retain &&
134 g_strstr_len(temp->aml_file, -1, "aml-")) {
135 unlink(temp->aml_file);
136 }
137 g_free(temp->aml_file);
138 g_free(temp->asl);
139 if (temp->asl_file &&
140 !temp->tmp_files_retain) {
141 unlink(temp->asl_file);
142 }
143 g_free(temp->asl_file);
144 }
145
146 g_array_free(data->tables, false);
147 }
148
149 static uint8_t acpi_checksum(const uint8_t *data, int len)
150 {
151 int i;
152 uint8_t sum = 0;
153
154 for (i = 0; i < len; i++) {
155 sum += data[i];
156 }
157
158 return sum;
159 }
160
161 static void test_acpi_rsdp_address(test_data *data)
162 {
163 uint32_t off;
164
165 /* OK, now find RSDP */
166 for (off = 0xf0000; off < 0x100000; off += 0x10) {
167 uint8_t sig[] = "RSD PTR ";
168 int i;
169
170 for (i = 0; i < sizeof sig - 1; ++i) {
171 sig[i] = readb(off + i);
172 }
173
174 if (!memcmp(sig, "RSD PTR ", sizeof sig)) {
175 break;
176 }
177 }
178
179 g_assert_cmphex(off, <, 0x100000);
180 data->rsdp_addr = off;
181 }
182
183 static void test_acpi_rsdp_table(test_data *data)
184 {
185 AcpiRsdpDescriptor *rsdp_table = &data->rsdp_table;
186 uint32_t addr = data->rsdp_addr;
187
188 ACPI_READ_FIELD(rsdp_table->signature, addr);
189 ACPI_ASSERT_CMP64(rsdp_table->signature, "RSD PTR ");
190
191 ACPI_READ_FIELD(rsdp_table->checksum, addr);
192 ACPI_READ_ARRAY(rsdp_table->oem_id, addr);
193 ACPI_READ_FIELD(rsdp_table->revision, addr);
194 ACPI_READ_FIELD(rsdp_table->rsdt_physical_address, addr);
195 ACPI_READ_FIELD(rsdp_table->length, addr);
196
197 /* rsdp checksum is not for the whole table, but for the first 20 bytes */
198 g_assert(!acpi_checksum((uint8_t *)rsdp_table, 20));
199 }
200
201 static void test_acpi_rsdt_table(test_data *data)
202 {
203 AcpiRsdtDescriptorRev1 *rsdt_table = &data->rsdt_table;
204 uint32_t addr = data->rsdp_table.rsdt_physical_address;
205 uint32_t *tables;
206 int tables_nr;
207 uint8_t checksum;
208
209 /* read the header */
210 ACPI_READ_TABLE_HEADER(rsdt_table, addr);
211 ACPI_ASSERT_CMP(rsdt_table->signature, "RSDT");
212
213 /* compute the table entries in rsdt */
214 tables_nr = (rsdt_table->length - sizeof(AcpiRsdtDescriptorRev1)) /
215 sizeof(uint32_t);
216 g_assert_cmpint(tables_nr, >, 0);
217
218 /* get the addresses of the tables pointed by rsdt */
219 tables = g_new0(uint32_t, tables_nr);
220 ACPI_READ_ARRAY_PTR(tables, tables_nr, addr);
221
222 checksum = acpi_checksum((uint8_t *)rsdt_table, rsdt_table->length) +
223 acpi_checksum((uint8_t *)tables, tables_nr * sizeof(uint32_t));
224 g_assert(!checksum);
225
226 /* SSDT tables after FADT */
227 data->rsdt_tables_addr = tables;
228 data->rsdt_tables_nr = tables_nr;
229 }
230
231 static void test_acpi_fadt_table(test_data *data)
232 {
233 AcpiFadtDescriptorRev1 *fadt_table = &data->fadt_table;
234 uint32_t addr;
235
236 /* FADT table comes first */
237 addr = data->rsdt_tables_addr[0];
238 ACPI_READ_TABLE_HEADER(fadt_table, addr);
239
240 ACPI_READ_FIELD(fadt_table->firmware_ctrl, addr);
241 ACPI_READ_FIELD(fadt_table->dsdt, addr);
242 ACPI_READ_FIELD(fadt_table->model, addr);
243 ACPI_READ_FIELD(fadt_table->reserved1, addr);
244 ACPI_READ_FIELD(fadt_table->sci_int, addr);
245 ACPI_READ_FIELD(fadt_table->smi_cmd, addr);
246 ACPI_READ_FIELD(fadt_table->acpi_enable, addr);
247 ACPI_READ_FIELD(fadt_table->acpi_disable, addr);
248 ACPI_READ_FIELD(fadt_table->S4bios_req, addr);
249 ACPI_READ_FIELD(fadt_table->reserved2, addr);
250 ACPI_READ_FIELD(fadt_table->pm1a_evt_blk, addr);
251 ACPI_READ_FIELD(fadt_table->pm1b_evt_blk, addr);
252 ACPI_READ_FIELD(fadt_table->pm1a_cnt_blk, addr);
253 ACPI_READ_FIELD(fadt_table->pm1b_cnt_blk, addr);
254 ACPI_READ_FIELD(fadt_table->pm2_cnt_blk, addr);
255 ACPI_READ_FIELD(fadt_table->pm_tmr_blk, addr);
256 ACPI_READ_FIELD(fadt_table->gpe0_blk, addr);
257 ACPI_READ_FIELD(fadt_table->gpe1_blk, addr);
258 ACPI_READ_FIELD(fadt_table->pm1_evt_len, addr);
259 ACPI_READ_FIELD(fadt_table->pm1_cnt_len, addr);
260 ACPI_READ_FIELD(fadt_table->pm2_cnt_len, addr);
261 ACPI_READ_FIELD(fadt_table->pm_tmr_len, addr);
262 ACPI_READ_FIELD(fadt_table->gpe0_blk_len, addr);
263 ACPI_READ_FIELD(fadt_table->gpe1_blk_len, addr);
264 ACPI_READ_FIELD(fadt_table->gpe1_base, addr);
265 ACPI_READ_FIELD(fadt_table->reserved3, addr);
266 ACPI_READ_FIELD(fadt_table->plvl2_lat, addr);
267 ACPI_READ_FIELD(fadt_table->plvl3_lat, addr);
268 ACPI_READ_FIELD(fadt_table->flush_size, addr);
269 ACPI_READ_FIELD(fadt_table->flush_stride, addr);
270 ACPI_READ_FIELD(fadt_table->duty_offset, addr);
271 ACPI_READ_FIELD(fadt_table->duty_width, addr);
272 ACPI_READ_FIELD(fadt_table->day_alrm, addr);
273 ACPI_READ_FIELD(fadt_table->mon_alrm, addr);
274 ACPI_READ_FIELD(fadt_table->century, addr);
275 ACPI_READ_FIELD(fadt_table->reserved4, addr);
276 ACPI_READ_FIELD(fadt_table->reserved4a, addr);
277 ACPI_READ_FIELD(fadt_table->reserved4b, addr);
278 ACPI_READ_FIELD(fadt_table->flags, addr);
279
280 ACPI_ASSERT_CMP(fadt_table->signature, "FACP");
281 g_assert(!acpi_checksum((uint8_t *)fadt_table, fadt_table->length));
282 }
283
284 static void test_acpi_facs_table(test_data *data)
285 {
286 AcpiFacsDescriptorRev1 *facs_table = &data->facs_table;
287 uint32_t addr = data->fadt_table.firmware_ctrl;
288
289 ACPI_READ_FIELD(facs_table->signature, addr);
290 ACPI_READ_FIELD(facs_table->length, addr);
291 ACPI_READ_FIELD(facs_table->hardware_signature, addr);
292 ACPI_READ_FIELD(facs_table->firmware_waking_vector, addr);
293 ACPI_READ_FIELD(facs_table->global_lock, addr);
294 ACPI_READ_FIELD(facs_table->flags, addr);
295 ACPI_READ_ARRAY(facs_table->resverved3, addr);
296
297 ACPI_ASSERT_CMP(facs_table->signature, "FACS");
298 }
299
300 static void test_dst_table(AcpiSdtTable *sdt_table, uint32_t addr)
301 {
302 uint8_t checksum;
303
304 ACPI_READ_TABLE_HEADER(&sdt_table->header, addr);
305
306 sdt_table->aml_len = sdt_table->header.length - sizeof(AcpiTableHeader);
307 sdt_table->aml = g_malloc0(sdt_table->aml_len);
308 ACPI_READ_ARRAY_PTR(sdt_table->aml, sdt_table->aml_len, addr);
309
310 checksum = acpi_checksum((uint8_t *)sdt_table, sizeof(AcpiTableHeader)) +
311 acpi_checksum((uint8_t *)sdt_table->aml, sdt_table->aml_len);
312 g_assert(!checksum);
313 }
314
315 static void test_acpi_dsdt_table(test_data *data)
316 {
317 AcpiSdtTable dsdt_table;
318 uint32_t addr = data->fadt_table.dsdt;
319
320 memset(&dsdt_table, 0, sizeof(dsdt_table));
321 data->tables = g_array_new(false, true, sizeof(AcpiSdtTable));
322
323 test_dst_table(&dsdt_table, addr);
324 ACPI_ASSERT_CMP(dsdt_table.header.signature, "DSDT");
325
326 /* Place DSDT first */
327 g_array_append_val(data->tables, dsdt_table);
328 }
329
330 static void test_acpi_tables(test_data *data)
331 {
332 int tables_nr = data->rsdt_tables_nr - 1; /* fadt is first */
333 int i;
334
335 for (i = 0; i < tables_nr; i++) {
336 AcpiSdtTable ssdt_table;
337
338 memset(&ssdt_table, 0 , sizeof(ssdt_table));
339 uint32_t addr = data->rsdt_tables_addr[i + 1]; /* fadt is first */
340 test_dst_table(&ssdt_table, addr);
341 g_array_append_val(data->tables, ssdt_table);
342 }
343 }
344
345 static void dump_aml_files(test_data *data, bool rebuild)
346 {
347 AcpiSdtTable *sdt;
348 GError *error = NULL;
349 gchar *aml_file = NULL;
350 gint fd;
351 ssize_t ret;
352 int i;
353
354 for (i = 0; i < data->tables->len; ++i) {
355 const char *ext = data->variant ? data->variant : "";
356 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
357 g_assert(sdt->aml);
358
359 if (rebuild) {
360 uint32_t signature = cpu_to_le32(sdt->header.signature);
361 aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
362 (gchar *)&signature, ext);
363 fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT,
364 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH);
365 } else {
366 fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error);
367 g_assert_no_error(error);
368 }
369 g_assert(fd >= 0);
370
371 ret = qemu_write_full(fd, sdt, sizeof(AcpiTableHeader));
372 g_assert(ret == sizeof(AcpiTableHeader));
373 ret = qemu_write_full(fd, sdt->aml, sdt->aml_len);
374 g_assert(ret == sdt->aml_len);
375
376 close(fd);
377
378 g_free(aml_file);
379 }
380 }
381
382 static bool compare_signature(AcpiSdtTable *sdt, const char *signature)
383 {
384 return !memcmp(&sdt->header.signature, signature, 4);
385 }
386
387 static bool load_asl(GArray *sdts, AcpiSdtTable *sdt)
388 {
389 AcpiSdtTable *temp;
390 GError *error = NULL;
391 GString *command_line = g_string_new(iasl);
392 gint fd;
393 gchar *out, *out_err;
394 gboolean ret;
395 int i;
396
397 fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error);
398 g_assert_no_error(error);
399 close(fd);
400
401 /* build command line */
402 g_string_append_printf(command_line, " -p %s ", sdt->asl_file);
403 if (compare_signature(sdt, "DSDT") ||
404 compare_signature(sdt, "SSDT")) {
405 for (i = 0; i < sdts->len; ++i) {
406 temp = &g_array_index(sdts, AcpiSdtTable, i);
407 if (compare_signature(temp, "DSDT") ||
408 compare_signature(temp, "SSDT")) {
409 g_string_append_printf(command_line, "-e %s ", temp->aml_file);
410 }
411 }
412 }
413 g_string_append_printf(command_line, "-d %s", sdt->aml_file);
414
415 /* pass 'out' and 'out_err' in order to be redirected */
416 ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error);
417 g_assert_no_error(error);
418 if (ret) {
419 ret = g_file_get_contents(sdt->asl_file, (gchar **)&sdt->asl,
420 &sdt->asl_len, &error);
421 g_assert(ret);
422 g_assert_no_error(error);
423 ret = (sdt->asl_len > 0);
424 }
425
426 g_free(out);
427 g_free(out_err);
428 g_string_free(command_line, true);
429
430 return !ret;
431 }
432
433 #define COMMENT_END "*/"
434 #define DEF_BLOCK "DefinitionBlock ("
435 #define BLOCK_NAME_END ","
436
437 static GString *normalize_asl(gchar *asl_code)
438 {
439 GString *asl = g_string_new(asl_code);
440 gchar *comment, *block_name;
441
442 /* strip comments (different generation days) */
443 comment = g_strstr_len(asl->str, asl->len, COMMENT_END);
444 if (comment) {
445 comment += strlen(COMMENT_END);
446 while (*comment == '\n') {
447 comment++;
448 }
449 asl = g_string_erase(asl, 0, comment - asl->str);
450 }
451
452 /* strip def block name (it has file path in it) */
453 if (g_str_has_prefix(asl->str, DEF_BLOCK)) {
454 block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END);
455 g_assert(block_name);
456 asl = g_string_erase(asl, 0,
457 block_name + sizeof(BLOCK_NAME_END) - asl->str);
458 }
459
460 return asl;
461 }
462
463 static GArray *load_expected_aml(test_data *data)
464 {
465 int i;
466 AcpiSdtTable *sdt;
467 GError *error = NULL;
468 gboolean ret;
469
470 GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable));
471 for (i = 0; i < data->tables->len; ++i) {
472 AcpiSdtTable exp_sdt;
473 uint32_t signature;
474 gchar *aml_file = NULL;
475 const char *ext = data->variant ? data->variant : "";
476
477 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
478
479 memset(&exp_sdt, 0, sizeof(exp_sdt));
480 exp_sdt.header.signature = sdt->header.signature;
481
482 signature = cpu_to_le32(sdt->header.signature);
483
484 try_again:
485 aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine,
486 (gchar *)&signature, ext);
487 if (getenv("V")) {
488 fprintf(stderr, "\nLooking for expected file '%s'\n", aml_file);
489 }
490 if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) {
491 exp_sdt.aml_file = aml_file;
492 } else if (*ext != '\0') {
493 /* try fallback to generic (extention less) expected file */
494 ext = "";
495 g_free(aml_file);
496 goto try_again;
497 }
498 g_assert(exp_sdt.aml_file);
499 if (getenv("V")) {
500 fprintf(stderr, "\nUsing expected file '%s'\n", aml_file);
501 }
502 ret = g_file_get_contents(aml_file, &exp_sdt.aml,
503 &exp_sdt.aml_len, &error);
504 g_assert(ret);
505 g_assert_no_error(error);
506 g_assert(exp_sdt.aml);
507 g_assert(exp_sdt.aml_len);
508
509 g_array_append_val(exp_tables, exp_sdt);
510 }
511
512 return exp_tables;
513 }
514
515 static void test_acpi_asl(test_data *data)
516 {
517 int i;
518 AcpiSdtTable *sdt, *exp_sdt;
519 test_data exp_data;
520 gboolean exp_err, err;
521
522 memset(&exp_data, 0, sizeof(exp_data));
523 exp_data.tables = load_expected_aml(data);
524 dump_aml_files(data, false);
525 for (i = 0; i < data->tables->len; ++i) {
526 GString *asl, *exp_asl;
527
528 sdt = &g_array_index(data->tables, AcpiSdtTable, i);
529 exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i);
530
531 err = load_asl(data->tables, sdt);
532 asl = normalize_asl(sdt->asl);
533
534 exp_err = load_asl(exp_data.tables, exp_sdt);
535 exp_asl = normalize_asl(exp_sdt->asl);
536
537 /* TODO: check for warnings */
538 g_assert(!err || exp_err);
539
540 if (g_strcmp0(asl->str, exp_asl->str)) {
541 if (exp_err) {
542 fprintf(stderr,
543 "Warning! iasl couldn't parse the expected aml\n");
544 } else {
545 uint32_t signature = cpu_to_le32(exp_sdt->header.signature);
546 sdt->tmp_files_retain = true;
547 exp_sdt->tmp_files_retain = true;
548 fprintf(stderr,
549 "acpi-test: Warning! %.4s mismatch. "
550 "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n",
551 (gchar *)&signature,
552 sdt->asl_file, sdt->aml_file,
553 exp_sdt->asl_file, exp_sdt->aml_file);
554 if (getenv("V")) {
555 const char *diff_cmd = getenv("DIFF");
556 if (diff_cmd) {
557 int ret G_GNUC_UNUSED;
558 char *diff = g_strdup_printf("%s %s %s", diff_cmd,
559 exp_sdt->asl_file, sdt->asl_file);
560 ret = system(diff) ;
561 g_free(diff);
562 } else {
563 fprintf(stderr, "acpi-test: Warning. not showing "
564 "difference since no diff utility is specified. "
565 "Set 'DIFF' environment variable to a preferred "
566 "diff utility and run 'make V=1 check' again to "
567 "see ASL difference.");
568 }
569 }
570 }
571 }
572 g_string_free(asl, true);
573 g_string_free(exp_asl, true);
574 }
575
576 free_test_data(&exp_data);
577 }
578
579 static bool smbios_ep_table_ok(test_data *data)
580 {
581 struct smbios_21_entry_point *ep_table = &data->smbios_ep_table;
582 uint32_t addr = data->smbios_ep_addr;
583
584 ACPI_READ_ARRAY(ep_table->anchor_string, addr);
585 if (memcmp(ep_table->anchor_string, "_SM_", 4)) {
586 return false;
587 }
588 ACPI_READ_FIELD(ep_table->checksum, addr);
589 ACPI_READ_FIELD(ep_table->length, addr);
590 ACPI_READ_FIELD(ep_table->smbios_major_version, addr);
591 ACPI_READ_FIELD(ep_table->smbios_minor_version, addr);
592 ACPI_READ_FIELD(ep_table->max_structure_size, addr);
593 ACPI_READ_FIELD(ep_table->entry_point_revision, addr);
594 ACPI_READ_ARRAY(ep_table->formatted_area, addr);
595 ACPI_READ_ARRAY(ep_table->intermediate_anchor_string, addr);
596 if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) {
597 return false;
598 }
599 ACPI_READ_FIELD(ep_table->intermediate_checksum, addr);
600 ACPI_READ_FIELD(ep_table->structure_table_length, addr);
601 if (ep_table->structure_table_length == 0) {
602 return false;
603 }
604 ACPI_READ_FIELD(ep_table->structure_table_address, addr);
605 ACPI_READ_FIELD(ep_table->number_of_structures, addr);
606 if (ep_table->number_of_structures == 0) {
607 return false;
608 }
609 ACPI_READ_FIELD(ep_table->smbios_bcd_revision, addr);
610 if (acpi_checksum((uint8_t *)ep_table, sizeof *ep_table) ||
611 acpi_checksum((uint8_t *)ep_table + 0x10, sizeof *ep_table - 0x10)) {
612 return false;
613 }
614 return true;
615 }
616
617 static void test_smbios_entry_point(test_data *data)
618 {
619 uint32_t off;
620
621 /* find smbios entry point structure */
622 for (off = 0xf0000; off < 0x100000; off += 0x10) {
623 uint8_t sig[] = "_SM_";
624 int i;
625
626 for (i = 0; i < sizeof sig - 1; ++i) {
627 sig[i] = readb(off + i);
628 }
629
630 if (!memcmp(sig, "_SM_", sizeof sig)) {
631 /* signature match, but is this a valid entry point? */
632 data->smbios_ep_addr = off;
633 if (smbios_ep_table_ok(data)) {
634 break;
635 }
636 }
637 }
638
639 g_assert_cmphex(off, <, 0x100000);
640 }
641
642 static inline bool smbios_single_instance(uint8_t type)
643 {
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;
655 }
656 }
657
658 static void test_smbios_structs(test_data *data)
659 {
660 DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 };
661 struct smbios_21_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};
666
667 /* walk the smbios tables */
668 for (i = 0; i < ep_table->number_of_structures; i++) {
669
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);
674
675 /* single-instance structs must not have been encountered before */
676 if (smbios_single_instance(type)) {
677 g_assert(!test_bit(type, struct_bitmap));
678 }
679 set_bit(type, struct_bitmap);
680
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++;
687 }
688
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);
693 }
694
695 /* start of next structure */
696 addr += len;
697 }
698
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);
703
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));
707 }
708 }
709
710 static void test_acpi_one(const char *params, test_data *data)
711 {
712 char *args;
713
714 args = g_strdup_printf("-net none -display none %s "
715 "-drive id=hd0,if=none,file=%s,format=raw "
716 "-device ide-hd,drive=hd0 ",
717 params ? params : "", disk);
718
719 qtest_start(args);
720
721 boot_sector_test();
722
723 test_acpi_rsdp_address(data);
724 test_acpi_rsdp_table(data);
725 test_acpi_rsdt_table(data);
726 test_acpi_fadt_table(data);
727 test_acpi_facs_table(data);
728 test_acpi_dsdt_table(data);
729 test_acpi_tables(data);
730
731 if (iasl) {
732 if (getenv(ACPI_REBUILD_EXPECTED_AML)) {
733 dump_aml_files(data, true);
734 } else {
735 test_acpi_asl(data);
736 }
737 }
738
739 test_smbios_entry_point(data);
740 test_smbios_structs(data);
741
742 qtest_quit(global_qtest);
743 g_free(args);
744 }
745
746 static void test_acpi_piix4_tcg(void)
747 {
748 test_data data;
749
750 /* Supplying -machine accel argument overrides the default (qtest).
751 * This is to make guest actually run.
752 */
753 memset(&data, 0, sizeof(data));
754 data.machine = MACHINE_PC;
755 test_acpi_one("-machine accel=tcg", &data);
756 free_test_data(&data);
757 }
758
759 static void test_acpi_piix4_tcg_bridge(void)
760 {
761 test_data data;
762
763 memset(&data, 0, sizeof(data));
764 data.machine = MACHINE_PC;
765 data.variant = ".bridge";
766 test_acpi_one("-machine accel=tcg -device pci-bridge,chassis_nr=1", &data);
767 free_test_data(&data);
768 }
769
770 static void test_acpi_q35_tcg(void)
771 {
772 test_data data;
773
774 memset(&data, 0, sizeof(data));
775 data.machine = MACHINE_Q35;
776 test_acpi_one("-machine q35,accel=tcg", &data);
777 free_test_data(&data);
778 }
779
780 static void test_acpi_q35_tcg_bridge(void)
781 {
782 test_data data;
783
784 memset(&data, 0, sizeof(data));
785 data.machine = MACHINE_Q35;
786 data.variant = ".bridge";
787 test_acpi_one("-machine q35,accel=tcg -device pci-bridge,chassis_nr=1",
788 &data);
789 free_test_data(&data);
790 }
791
792 int main(int argc, char *argv[])
793 {
794 const char *arch = qtest_get_arch();
795 int ret;
796
797 ret = boot_sector_init(disk);
798 if(ret)
799 return ret;
800
801 g_test_init(&argc, &argv, NULL);
802
803 if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) {
804 qtest_add_func("acpi/piix4/tcg", test_acpi_piix4_tcg);
805 qtest_add_func("acpi/piix4/tcg/bridge", test_acpi_piix4_tcg_bridge);
806 qtest_add_func("acpi/q35/tcg", test_acpi_q35_tcg);
807 qtest_add_func("acpi/q35/tcg/bridge", test_acpi_q35_tcg_bridge);
808 }
809 ret = g_test_run();
810 boot_sector_cleanup(disk);
811 return ret;
812 }