2 * Boot order test cases.
4 * Copyright (c) 2013 Red Hat Inc.
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.
16 #include <glib/gstdio.h>
17 #include "qemu-common.h"
19 #include "qemu/compiler.h"
20 #include "hw/i386/acpi-defs.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 */
29 AcpiTableHeader header
;
30 gchar
*aml
; /* aml bytecode from guest */
33 gchar
*asl
; /* asl code generated from aml */
36 } QEMU_PACKED AcpiSdtTable
;
41 AcpiRsdpDescriptor rsdp_table
;
42 AcpiRsdtDescriptorRev1 rsdt_table
;
43 AcpiFadtDescriptorRev1 fadt_table
;
44 AcpiFacsDescriptorRev1 facs_table
;
45 uint32_t *rsdt_tables_addr
;
47 GArray
*ssdt_tables
; /* first is DSDT */
50 #define LOW(x) ((x) & 0xff)
51 #define HIGH(x) ((x) >> 8)
53 #define SIGNATURE 0xdead
54 #define SIGNATURE_OFFSET 0x10
55 #define BOOT_SECTOR_ADDRESS 0x7c00
57 #define ACPI_READ_FIELD(field, addr) \
59 switch (sizeof(field)) { \
61 field = readb(addr); \
64 field = le16_to_cpu(readw(addr)); \
67 field = le32_to_cpu(readl(addr)); \
70 field = le64_to_cpu(readq(addr)); \
75 addr += sizeof(field); \
78 #define ACPI_READ_ARRAY_PTR(arr, length, addr) \
81 for (idx = 0; idx < length; ++idx) { \
82 ACPI_READ_FIELD(arr[idx], addr); \
86 #define ACPI_READ_ARRAY(arr, addr) \
87 ACPI_READ_ARRAY_PTR(arr, sizeof(arr)/sizeof(arr[0]), addr)
89 #define ACPI_READ_TABLE_HEADER(table, addr) \
91 ACPI_READ_FIELD((table)->signature, addr); \
92 ACPI_READ_FIELD((table)->length, addr); \
93 ACPI_READ_FIELD((table)->revision, addr); \
94 ACPI_READ_FIELD((table)->checksum, addr); \
95 ACPI_READ_ARRAY((table)->oem_id, addr); \
96 ACPI_READ_ARRAY((table)->oem_table_id, addr); \
97 ACPI_READ_FIELD((table)->oem_revision, addr); \
98 ACPI_READ_ARRAY((table)->asl_compiler_id, addr); \
99 ACPI_READ_FIELD((table)->asl_compiler_revision, addr); \
102 /* Boot sector code: write SIGNATURE into memory,
104 * Q35 machine requires a minimum 0x7e000 bytes disk.
107 static uint8_t boot_sector
[0x7e000] = {
108 /* 7c00: mov $0xdead,%ax */
110 [0x01] = LOW(SIGNATURE
),
111 [0x02] = HIGH(SIGNATURE
),
112 /* 7c03: mov %ax,0x7c10 */
114 [0x04] = LOW(BOOT_SECTOR_ADDRESS
+ SIGNATURE_OFFSET
),
115 [0x05] = HIGH(BOOT_SECTOR_ADDRESS
+ SIGNATURE_OFFSET
),
120 /* 7c08: jmp 0x7c07=0x7c0a-3 */
123 /* We mov 0xdead here: set value to make debugging easier */
124 [SIGNATURE_OFFSET
] = LOW(0xface),
125 [SIGNATURE_OFFSET
+ 1] = HIGH(0xface),
126 /* End of boot sector marker */
131 static const char *disk
= "tests/acpi-test-disk.raw";
132 static const char *data_dir
= "tests/acpi-test-data";
134 static const char *iasl
= stringify(CONFIG_IASL
);
136 static const char *iasl
;
139 static void free_test_data(test_data
*data
)
144 if (data
->rsdt_tables_addr
) {
145 g_free(data
->rsdt_tables_addr
);
148 for (i
= 0; i
< data
->ssdt_tables
->len
; ++i
) {
149 temp
= &g_array_index(data
->ssdt_tables
, AcpiSdtTable
, i
);
153 if (temp
->aml_file
) {
154 if (g_strstr_len(temp
->aml_file
, -1, "aml-")) {
155 unlink(temp
->aml_file
);
157 g_free(temp
->aml_file
);
162 if (temp
->asl_file
) {
163 if (g_strstr_len(temp
->asl_file
, -1, "asl-")) {
164 unlink(temp
->asl_file
);
166 g_free(temp
->asl_file
);
170 g_array_free(data
->ssdt_tables
, false);
173 static uint8_t acpi_checksum(const uint8_t *data
, int len
)
178 for (i
= 0; i
< len
; i
++) {
185 static void test_acpi_rsdp_address(test_data
*data
)
189 /* OK, now find RSDP */
190 for (off
= 0xf0000; off
< 0x100000; off
+= 0x10) {
191 uint8_t sig
[] = "RSD PTR ";
194 for (i
= 0; i
< sizeof sig
- 1; ++i
) {
195 sig
[i
] = readb(off
+ i
);
198 if (!memcmp(sig
, "RSD PTR ", sizeof sig
)) {
203 g_assert_cmphex(off
, <, 0x100000);
204 data
->rsdp_addr
= off
;
207 static void test_acpi_rsdp_table(test_data
*data
)
209 AcpiRsdpDescriptor
*rsdp_table
= &data
->rsdp_table
;
210 uint32_t addr
= data
->rsdp_addr
;
212 ACPI_READ_FIELD(rsdp_table
->signature
, addr
);
213 g_assert_cmphex(rsdp_table
->signature
, ==, ACPI_RSDP_SIGNATURE
);
215 ACPI_READ_FIELD(rsdp_table
->checksum
, addr
);
216 ACPI_READ_ARRAY(rsdp_table
->oem_id
, addr
);
217 ACPI_READ_FIELD(rsdp_table
->revision
, addr
);
218 ACPI_READ_FIELD(rsdp_table
->rsdt_physical_address
, addr
);
219 ACPI_READ_FIELD(rsdp_table
->length
, addr
);
221 /* rsdp checksum is not for the whole table, but for the first 20 bytes */
222 g_assert(!acpi_checksum((uint8_t *)rsdp_table
, 20));
225 static void test_acpi_rsdt_table(test_data
*data
)
227 AcpiRsdtDescriptorRev1
*rsdt_table
= &data
->rsdt_table
;
228 uint32_t addr
= data
->rsdp_table
.rsdt_physical_address
;
233 /* read the header */
234 ACPI_READ_TABLE_HEADER(rsdt_table
, addr
);
235 g_assert_cmphex(rsdt_table
->signature
, ==, ACPI_RSDT_SIGNATURE
);
237 /* compute the table entries in rsdt */
238 tables_nr
= (rsdt_table
->length
- sizeof(AcpiRsdtDescriptorRev1
)) /
240 g_assert_cmpint(tables_nr
, >, 0);
242 /* get the addresses of the tables pointed by rsdt */
243 tables
= g_new0(uint32_t, tables_nr
);
244 ACPI_READ_ARRAY_PTR(tables
, tables_nr
, addr
);
246 checksum
= acpi_checksum((uint8_t *)rsdt_table
, rsdt_table
->length
) +
247 acpi_checksum((uint8_t *)tables
, tables_nr
* sizeof(uint32_t));
250 /* SSDT tables after FADT */
251 data
->rsdt_tables_addr
= tables
;
252 data
->rsdt_tables_nr
= tables_nr
;
255 static void test_acpi_fadt_table(test_data
*data
)
257 AcpiFadtDescriptorRev1
*fadt_table
= &data
->fadt_table
;
260 /* FADT table comes first */
261 addr
= data
->rsdt_tables_addr
[0];
262 ACPI_READ_TABLE_HEADER(fadt_table
, addr
);
264 ACPI_READ_FIELD(fadt_table
->firmware_ctrl
, addr
);
265 ACPI_READ_FIELD(fadt_table
->dsdt
, addr
);
266 ACPI_READ_FIELD(fadt_table
->model
, addr
);
267 ACPI_READ_FIELD(fadt_table
->reserved1
, addr
);
268 ACPI_READ_FIELD(fadt_table
->sci_int
, addr
);
269 ACPI_READ_FIELD(fadt_table
->smi_cmd
, addr
);
270 ACPI_READ_FIELD(fadt_table
->acpi_enable
, addr
);
271 ACPI_READ_FIELD(fadt_table
->acpi_disable
, addr
);
272 ACPI_READ_FIELD(fadt_table
->S4bios_req
, addr
);
273 ACPI_READ_FIELD(fadt_table
->reserved2
, addr
);
274 ACPI_READ_FIELD(fadt_table
->pm1a_evt_blk
, addr
);
275 ACPI_READ_FIELD(fadt_table
->pm1b_evt_blk
, addr
);
276 ACPI_READ_FIELD(fadt_table
->pm1a_cnt_blk
, addr
);
277 ACPI_READ_FIELD(fadt_table
->pm1b_cnt_blk
, addr
);
278 ACPI_READ_FIELD(fadt_table
->pm2_cnt_blk
, addr
);
279 ACPI_READ_FIELD(fadt_table
->pm_tmr_blk
, addr
);
280 ACPI_READ_FIELD(fadt_table
->gpe0_blk
, addr
);
281 ACPI_READ_FIELD(fadt_table
->gpe1_blk
, addr
);
282 ACPI_READ_FIELD(fadt_table
->pm1_evt_len
, addr
);
283 ACPI_READ_FIELD(fadt_table
->pm1_cnt_len
, addr
);
284 ACPI_READ_FIELD(fadt_table
->pm2_cnt_len
, addr
);
285 ACPI_READ_FIELD(fadt_table
->pm_tmr_len
, addr
);
286 ACPI_READ_FIELD(fadt_table
->gpe0_blk_len
, addr
);
287 ACPI_READ_FIELD(fadt_table
->gpe1_blk_len
, addr
);
288 ACPI_READ_FIELD(fadt_table
->gpe1_base
, addr
);
289 ACPI_READ_FIELD(fadt_table
->reserved3
, addr
);
290 ACPI_READ_FIELD(fadt_table
->plvl2_lat
, addr
);
291 ACPI_READ_FIELD(fadt_table
->plvl3_lat
, addr
);
292 ACPI_READ_FIELD(fadt_table
->flush_size
, addr
);
293 ACPI_READ_FIELD(fadt_table
->flush_stride
, addr
);
294 ACPI_READ_FIELD(fadt_table
->duty_offset
, addr
);
295 ACPI_READ_FIELD(fadt_table
->duty_width
, addr
);
296 ACPI_READ_FIELD(fadt_table
->day_alrm
, addr
);
297 ACPI_READ_FIELD(fadt_table
->mon_alrm
, addr
);
298 ACPI_READ_FIELD(fadt_table
->century
, addr
);
299 ACPI_READ_FIELD(fadt_table
->reserved4
, addr
);
300 ACPI_READ_FIELD(fadt_table
->reserved4a
, addr
);
301 ACPI_READ_FIELD(fadt_table
->reserved4b
, addr
);
302 ACPI_READ_FIELD(fadt_table
->flags
, addr
);
304 g_assert_cmphex(fadt_table
->signature
, ==, ACPI_FACP_SIGNATURE
);
305 g_assert(!acpi_checksum((uint8_t *)fadt_table
, fadt_table
->length
));
308 static void test_acpi_facs_table(test_data
*data
)
310 AcpiFacsDescriptorRev1
*facs_table
= &data
->facs_table
;
311 uint32_t addr
= data
->fadt_table
.firmware_ctrl
;
313 ACPI_READ_FIELD(facs_table
->signature
, addr
);
314 ACPI_READ_FIELD(facs_table
->length
, addr
);
315 ACPI_READ_FIELD(facs_table
->hardware_signature
, addr
);
316 ACPI_READ_FIELD(facs_table
->firmware_waking_vector
, addr
);
317 ACPI_READ_FIELD(facs_table
->global_lock
, addr
);
318 ACPI_READ_FIELD(facs_table
->flags
, addr
);
319 ACPI_READ_ARRAY(facs_table
->resverved3
, addr
);
321 g_assert_cmphex(facs_table
->signature
, ==, ACPI_FACS_SIGNATURE
);
324 static void test_dst_table(AcpiSdtTable
*sdt_table
, uint32_t addr
)
328 ACPI_READ_TABLE_HEADER(&sdt_table
->header
, addr
);
330 sdt_table
->aml_len
= sdt_table
->header
.length
- sizeof(AcpiTableHeader
);
331 sdt_table
->aml
= g_malloc0(sdt_table
->aml_len
);
332 ACPI_READ_ARRAY_PTR(sdt_table
->aml
, sdt_table
->aml_len
, addr
);
334 checksum
= acpi_checksum((uint8_t *)sdt_table
, sizeof(AcpiTableHeader
)) +
335 acpi_checksum((uint8_t *)sdt_table
->aml
, sdt_table
->aml_len
);
339 static void test_acpi_dsdt_table(test_data
*data
)
341 AcpiSdtTable dsdt_table
;
342 uint32_t addr
= data
->fadt_table
.dsdt
;
344 memset(&dsdt_table
, 0, sizeof(dsdt_table
));
345 data
->ssdt_tables
= g_array_new(false, true, sizeof(AcpiSdtTable
));
347 test_dst_table(&dsdt_table
, addr
);
348 g_assert_cmphex(dsdt_table
.header
.signature
, ==, ACPI_DSDT_SIGNATURE
);
350 /* Place DSDT first */
351 g_array_append_val(data
->ssdt_tables
, dsdt_table
);
354 static void test_acpi_ssdt_tables(test_data
*data
)
356 int ssdt_tables_nr
= data
->rsdt_tables_nr
- 1; /* fadt is first */
359 for (i
= 0; i
< ssdt_tables_nr
; i
++) {
360 AcpiSdtTable ssdt_table
;
362 memset(&ssdt_table
, 0 , sizeof(ssdt_table
));
363 uint32_t addr
= data
->rsdt_tables_addr
[i
+ 1]; /* fadt is first */
364 test_dst_table(&ssdt_table
, addr
);
365 g_array_append_val(data
->ssdt_tables
, ssdt_table
);
369 static void dump_aml_files(test_data
*data
, bool rebuild
)
372 GError
*error
= NULL
;
373 gchar
*aml_file
= NULL
;
378 for (i
= 0; i
< data
->ssdt_tables
->len
; ++i
) {
379 sdt
= &g_array_index(data
->ssdt_tables
, AcpiSdtTable
, i
);
383 aml_file
= g_strdup_printf("%s/%s/%.4s", data_dir
, data
->machine
,
384 (gchar
*)&sdt
->header
.signature
);
385 fd
= g_open(aml_file
, O_WRONLY
|O_TRUNC
|O_CREAT
,
386 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
);
388 fd
= g_file_open_tmp("aml-XXXXXX", &sdt
->aml_file
, &error
);
389 g_assert_no_error(error
);
393 ret
= qemu_write_full(fd
, sdt
, sizeof(AcpiTableHeader
));
394 g_assert(ret
== sizeof(AcpiTableHeader
));
395 ret
= qemu_write_full(fd
, sdt
->aml
, sdt
->aml_len
);
396 g_assert(ret
== sdt
->aml_len
);
406 static void load_asl(GArray
*sdts
, AcpiSdtTable
*sdt
)
409 GError
*error
= NULL
;
410 GString
*command_line
= g_string_new(iasl
);
412 gchar
*out
, *out_err
;
416 fd
= g_file_open_tmp("asl-XXXXXX.dsl", &sdt
->asl_file
, &error
);
417 g_assert_no_error(error
);
420 /* build command line */
421 g_string_append_printf(command_line
, " -p %s ", sdt
->asl_file
);
422 for (i
= 0; i
< 2; ++i
) { /* reference DSDT and SSDT */
423 temp
= &g_array_index(sdts
, AcpiSdtTable
, i
);
424 g_string_append_printf(command_line
, "-e %s ", temp
->aml_file
);
426 g_string_append_printf(command_line
, "-d %s", sdt
->aml_file
);
428 /* pass 'out' and 'out_err' in order to be redirected */
429 g_spawn_command_line_sync(command_line
->str
, &out
, &out_err
, NULL
, &error
);
430 g_assert_no_error(error
);
432 ret
= g_file_get_contents(sdt
->asl_file
, (gchar
**)&sdt
->asl
,
433 &sdt
->asl_len
, &error
);
435 g_assert_no_error(error
);
436 g_assert(sdt
->asl_len
);
440 g_string_free(command_line
, true);
443 #define COMMENT_END "*/"
444 #define DEF_BLOCK "DefinitionBlock ("
445 #define BLOCK_NAME_END ".aml"
447 static GString
*normalize_asl(gchar
*asl_code
)
449 GString
*asl
= g_string_new(asl_code
);
450 gchar
*comment
, *block_name
;
452 /* strip comments (different generation days) */
453 comment
= g_strstr_len(asl
->str
, asl
->len
, COMMENT_END
);
455 asl
= g_string_erase(asl
, 0, comment
+ sizeof(COMMENT_END
) - asl
->str
);
458 /* strip def block name (it has file path in it) */
459 if (g_str_has_prefix(asl
->str
, DEF_BLOCK
)) {
460 block_name
= g_strstr_len(asl
->str
, asl
->len
, BLOCK_NAME_END
);
461 g_assert(block_name
);
462 asl
= g_string_erase(asl
, 0,
463 block_name
+ sizeof(BLOCK_NAME_END
) - asl
->str
);
469 static GArray
*load_expected_aml(test_data
*data
)
474 GError
*error
= NULL
;
477 GArray
*exp_ssdt_tables
= g_array_new(false, true, sizeof(AcpiSdtTable
));
478 for (i
= 0; i
< data
->ssdt_tables
->len
; ++i
) {
479 AcpiSdtTable exp_sdt
;
480 sdt
= &g_array_index(data
->ssdt_tables
, AcpiSdtTable
, i
);
482 memset(&exp_sdt
, 0, sizeof(exp_sdt
));
483 exp_sdt
.header
.signature
= sdt
->header
.signature
;
485 aml_file
= g_strdup_printf("%s/%s/%.4s", data_dir
, data
->machine
,
486 (gchar
*)&exp_sdt
.header
.signature
);
487 exp_sdt
.aml_file
= aml_file
;
488 g_assert(g_file_test(aml_file
, G_FILE_TEST_EXISTS
));
489 ret
= g_file_get_contents(aml_file
, &exp_sdt
.aml
,
490 &exp_sdt
.aml_len
, &error
);
492 g_assert_no_error(error
);
493 g_assert(exp_sdt
.aml
);
494 g_assert(exp_sdt
.aml_len
);
496 g_array_append_val(exp_ssdt_tables
, exp_sdt
);
499 return exp_ssdt_tables
;
502 static void test_acpi_asl(test_data
*data
)
505 AcpiSdtTable
*sdt
, *exp_sdt
;
508 memset(&exp_data
, 0, sizeof(exp_data
));
509 exp_data
.ssdt_tables
= load_expected_aml(data
);
510 dump_aml_files(data
, false);
511 for (i
= 0; i
< data
->ssdt_tables
->len
; ++i
) {
512 GString
*asl
, *exp_asl
;
514 sdt
= &g_array_index(data
->ssdt_tables
, AcpiSdtTable
, i
);
515 exp_sdt
= &g_array_index(exp_data
.ssdt_tables
, AcpiSdtTable
, i
);
517 load_asl(data
->ssdt_tables
, sdt
);
518 asl
= normalize_asl(sdt
->asl
);
520 load_asl(exp_data
.ssdt_tables
, exp_sdt
);
521 exp_asl
= normalize_asl(exp_sdt
->asl
);
523 g_assert(!g_strcmp0(asl
->str
, exp_asl
->str
));
524 g_string_free(asl
, true);
525 g_string_free(exp_asl
, true);
528 free_test_data(&exp_data
);
531 static void test_acpi_one(const char *params
, test_data
*data
)
534 uint8_t signature_low
;
535 uint8_t signature_high
;
538 const char *device
= "";
540 if (!g_strcmp0(data
->machine
, MACHINE_Q35
)) {
541 device
= ",id=hd -device ide-hd,drive=hd";
544 args
= g_strdup_printf("-net none -display none %s -drive file=%s%s,",
545 params
? params
: "", disk
, device
);
548 /* Wait at most 1 minute */
549 #define TEST_DELAY (1 * G_USEC_PER_SEC / 10)
550 #define TEST_CYCLES MAX((60 * G_USEC_PER_SEC / TEST_DELAY), 1)
552 /* Poll until code has run and modified memory. Once it has we know BIOS
553 * initialization is done. TODO: check that IP reached the halt
556 for (i
= 0; i
< TEST_CYCLES
; ++i
) {
557 signature_low
= readb(BOOT_SECTOR_ADDRESS
+ SIGNATURE_OFFSET
);
558 signature_high
= readb(BOOT_SECTOR_ADDRESS
+ SIGNATURE_OFFSET
+ 1);
559 signature
= (signature_high
<< 8) | signature_low
;
560 if (signature
== SIGNATURE
) {
563 g_usleep(TEST_DELAY
);
565 g_assert_cmphex(signature
, ==, SIGNATURE
);
567 test_acpi_rsdp_address(data
);
568 test_acpi_rsdp_table(data
);
569 test_acpi_rsdt_table(data
);
570 test_acpi_fadt_table(data
);
571 test_acpi_facs_table(data
);
572 test_acpi_dsdt_table(data
);
573 test_acpi_ssdt_tables(data
);
576 if (getenv(ACPI_REBUILD_EXPECTED_AML
)) {
577 dump_aml_files(data
, true);
583 qtest_quit(global_qtest
);
587 static void test_acpi_tcg(void)
591 /* Supplying -machine accel argument overrides the default (qtest).
592 * This is to make guest actually run.
594 memset(&data
, 0, sizeof(data
));
595 data
.machine
= MACHINE_PC
;
596 test_acpi_one("-machine accel=tcg", &data
);
597 free_test_data(&data
);
599 memset(&data
, 0, sizeof(data
));
600 data
.machine
= MACHINE_Q35
;
601 test_acpi_one("-machine q35,accel=tcg", &data
);
602 free_test_data(&data
);
605 int main(int argc
, char *argv
[])
607 const char *arch
= qtest_get_arch();
608 FILE *f
= fopen(disk
, "w");
610 fwrite(boot_sector
, 1, sizeof boot_sector
, f
);
613 g_test_init(&argc
, &argv
, NULL
);
615 if (strcmp(arch
, "i386") == 0 || strcmp(arch
, "x86_64") == 0) {
616 qtest_add_func("acpi/tcg", test_acpi_tcg
);