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