]>
Commit | Line | Data |
---|---|---|
ad6423a7 MT |
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 <string.h> | |
14 | #include <stdio.h> | |
15 | #include <glib.h> | |
4500bc98 | 16 | #include <glib/gstdio.h> |
53333801 | 17 | #include "qemu-common.h" |
ad6423a7 | 18 | #include "libqtest.h" |
53333801 MA |
19 | #include "qemu/compiler.h" |
20 | #include "hw/i386/acpi-defs.h" | |
ad6423a7 | 21 | |
9e8458c0 MA |
22 | #define MACHINE_PC "pc" |
23 | #define MACHINE_Q35 "q35" | |
24 | ||
4500bc98 | 25 | #define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML" |
69d09245 | 26 | #define ACPI_SSDT_SIGNATURE 0x54445353 /* SSDT */ |
4500bc98 | 27 | |
53333801 | 28 | /* DSDT and SSDTs format */ |
ad6423a7 | 29 | typedef struct { |
53333801 | 30 | AcpiTableHeader header; |
9e8458c0 MA |
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; | |
262f6f51 | 37 | bool tmp_files_retain; /* do not delete the temp asl/aml */ |
9e8458c0 | 38 | } QEMU_PACKED AcpiSdtTable; |
53333801 MA |
39 | |
40 | typedef struct { | |
9e8458c0 | 41 | const char *machine; |
53333801 MA |
42 | uint32_t rsdp_addr; |
43 | AcpiRsdpDescriptor rsdp_table; | |
44 | AcpiRsdtDescriptorRev1 rsdt_table; | |
45 | AcpiFadtDescriptorRev1 fadt_table; | |
15650602 | 46 | AcpiFacsDescriptorRev1 facs_table; |
53333801 MA |
47 | uint32_t *rsdt_tables_addr; |
48 | int rsdt_tables_nr; | |
a3a74ab9 | 49 | GArray *tables; |
53333801 | 50 | } test_data; |
ad6423a7 MT |
51 | |
52 | #define LOW(x) ((x) & 0xff) | |
53 | #define HIGH(x) ((x) >> 8) | |
54 | ||
55 | #define SIGNATURE 0xdead | |
56 | #define SIGNATURE_OFFSET 0x10 | |
57 | #define BOOT_SECTOR_ADDRESS 0x7c00 | |
58 | ||
53333801 MA |
59 | #define ACPI_READ_FIELD(field, addr) \ |
60 | do { \ | |
61 | switch (sizeof(field)) { \ | |
62 | case 1: \ | |
63 | field = readb(addr); \ | |
64 | break; \ | |
65 | case 2: \ | |
084137dd | 66 | field = readw(addr); \ |
53333801 MA |
67 | break; \ |
68 | case 4: \ | |
084137dd | 69 | field = readl(addr); \ |
53333801 MA |
70 | break; \ |
71 | case 8: \ | |
084137dd | 72 | field = readq(addr); \ |
53333801 MA |
73 | break; \ |
74 | default: \ | |
75 | g_assert(false); \ | |
76 | } \ | |
77 | addr += sizeof(field); \ | |
78 | } while (0); | |
79 | ||
80 | #define ACPI_READ_ARRAY_PTR(arr, length, addr) \ | |
81 | do { \ | |
82 | int idx; \ | |
83 | for (idx = 0; idx < length; ++idx) { \ | |
84 | ACPI_READ_FIELD(arr[idx], addr); \ | |
85 | } \ | |
86 | } while (0); | |
87 | ||
88 | #define ACPI_READ_ARRAY(arr, addr) \ | |
89 | ACPI_READ_ARRAY_PTR(arr, sizeof(arr)/sizeof(arr[0]), addr) | |
90 | ||
91 | #define ACPI_READ_TABLE_HEADER(table, addr) \ | |
92 | do { \ | |
93 | ACPI_READ_FIELD((table)->signature, addr); \ | |
94 | ACPI_READ_FIELD((table)->length, addr); \ | |
95 | ACPI_READ_FIELD((table)->revision, addr); \ | |
96 | ACPI_READ_FIELD((table)->checksum, addr); \ | |
97 | ACPI_READ_ARRAY((table)->oem_id, addr); \ | |
98 | ACPI_READ_ARRAY((table)->oem_table_id, addr); \ | |
99 | ACPI_READ_FIELD((table)->oem_revision, addr); \ | |
100 | ACPI_READ_ARRAY((table)->asl_compiler_id, addr); \ | |
101 | ACPI_READ_FIELD((table)->asl_compiler_revision, addr); \ | |
102 | } while (0); | |
103 | ||
ad6423a7 MT |
104 | /* Boot sector code: write SIGNATURE into memory, |
105 | * then halt. | |
9e8458c0 MA |
106 | * Q35 machine requires a minimum 0x7e000 bytes disk. |
107 | * (bug or feature?) | |
ad6423a7 | 108 | */ |
9e8458c0 | 109 | static uint8_t boot_sector[0x7e000] = { |
ad6423a7 MT |
110 | /* 7c00: mov $0xdead,%ax */ |
111 | [0x00] = 0xb8, | |
112 | [0x01] = LOW(SIGNATURE), | |
113 | [0x02] = HIGH(SIGNATURE), | |
114 | /* 7c03: mov %ax,0x7c10 */ | |
115 | [0x03] = 0xa3, | |
116 | [0x04] = LOW(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET), | |
117 | [0x05] = HIGH(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET), | |
118 | /* 7c06: cli */ | |
119 | [0x06] = 0xfa, | |
120 | /* 7c07: hlt */ | |
121 | [0x07] = 0xf4, | |
122 | /* 7c08: jmp 0x7c07=0x7c0a-3 */ | |
123 | [0x08] = 0xeb, | |
124 | [0x09] = LOW(-3), | |
125 | /* We mov 0xdead here: set value to make debugging easier */ | |
126 | [SIGNATURE_OFFSET] = LOW(0xface), | |
127 | [SIGNATURE_OFFSET + 1] = HIGH(0xface), | |
128 | /* End of boot sector marker */ | |
129 | [0x1FE] = 0x55, | |
130 | [0x1FF] = 0xAA, | |
131 | }; | |
132 | ||
133 | static const char *disk = "tests/acpi-test-disk.raw"; | |
9e8458c0 | 134 | static const char *data_dir = "tests/acpi-test-data"; |
cc8fa0e8 MA |
135 | #ifdef CONFIG_IASL |
136 | static const char *iasl = stringify(CONFIG_IASL); | |
137 | #else | |
138 | static const char *iasl; | |
139 | #endif | |
ad6423a7 | 140 | |
8ac2adf7 MA |
141 | static void free_test_data(test_data *data) |
142 | { | |
9e8458c0 | 143 | AcpiSdtTable *temp; |
8ac2adf7 MA |
144 | int i; |
145 | ||
9e8458c0 MA |
146 | if (data->rsdt_tables_addr) { |
147 | g_free(data->rsdt_tables_addr); | |
148 | } | |
149 | ||
a3a74ab9 MA |
150 | for (i = 0; i < data->tables->len; ++i) { |
151 | temp = &g_array_index(data->tables, AcpiSdtTable, i); | |
9e8458c0 MA |
152 | if (temp->aml) { |
153 | g_free(temp->aml); | |
154 | } | |
155 | if (temp->aml_file) { | |
262f6f51 MA |
156 | if (!temp->tmp_files_retain && |
157 | g_strstr_len(temp->aml_file, -1, "aml-")) { | |
9e8458c0 MA |
158 | unlink(temp->aml_file); |
159 | } | |
160 | g_free(temp->aml_file); | |
161 | } | |
162 | if (temp->asl) { | |
163 | g_free(temp->asl); | |
164 | } | |
165 | if (temp->asl_file) { | |
262f6f51 | 166 | if (!temp->tmp_files_retain) { |
9e8458c0 MA |
167 | unlink(temp->asl_file); |
168 | } | |
169 | g_free(temp->asl_file); | |
170 | } | |
8ac2adf7 | 171 | } |
9e8458c0 | 172 | |
a3a74ab9 | 173 | g_array_free(data->tables, false); |
8ac2adf7 MA |
174 | } |
175 | ||
53333801 MA |
176 | static uint8_t acpi_checksum(const uint8_t *data, int len) |
177 | { | |
178 | int i; | |
179 | uint8_t sum = 0; | |
180 | ||
181 | for (i = 0; i < len; i++) { | |
182 | sum += data[i]; | |
183 | } | |
184 | ||
185 | return sum; | |
186 | } | |
187 | ||
188 | static void test_acpi_rsdp_address(test_data *data) | |
189 | { | |
190 | uint32_t off; | |
191 | ||
192 | /* OK, now find RSDP */ | |
193 | for (off = 0xf0000; off < 0x100000; off += 0x10) { | |
194 | uint8_t sig[] = "RSD PTR "; | |
195 | int i; | |
196 | ||
197 | for (i = 0; i < sizeof sig - 1; ++i) { | |
198 | sig[i] = readb(off + i); | |
199 | } | |
200 | ||
201 | if (!memcmp(sig, "RSD PTR ", sizeof sig)) { | |
202 | break; | |
203 | } | |
204 | } | |
205 | ||
206 | g_assert_cmphex(off, <, 0x100000); | |
207 | data->rsdp_addr = off; | |
208 | } | |
209 | ||
210 | static void test_acpi_rsdp_table(test_data *data) | |
211 | { | |
212 | AcpiRsdpDescriptor *rsdp_table = &data->rsdp_table; | |
213 | uint32_t addr = data->rsdp_addr; | |
214 | ||
215 | ACPI_READ_FIELD(rsdp_table->signature, addr); | |
216 | g_assert_cmphex(rsdp_table->signature, ==, ACPI_RSDP_SIGNATURE); | |
217 | ||
218 | ACPI_READ_FIELD(rsdp_table->checksum, addr); | |
219 | ACPI_READ_ARRAY(rsdp_table->oem_id, addr); | |
220 | ACPI_READ_FIELD(rsdp_table->revision, addr); | |
221 | ACPI_READ_FIELD(rsdp_table->rsdt_physical_address, addr); | |
222 | ACPI_READ_FIELD(rsdp_table->length, addr); | |
223 | ||
224 | /* rsdp checksum is not for the whole table, but for the first 20 bytes */ | |
225 | g_assert(!acpi_checksum((uint8_t *)rsdp_table, 20)); | |
226 | } | |
227 | ||
228 | static void test_acpi_rsdt_table(test_data *data) | |
229 | { | |
230 | AcpiRsdtDescriptorRev1 *rsdt_table = &data->rsdt_table; | |
231 | uint32_t addr = data->rsdp_table.rsdt_physical_address; | |
232 | uint32_t *tables; | |
233 | int tables_nr; | |
234 | uint8_t checksum; | |
235 | ||
236 | /* read the header */ | |
237 | ACPI_READ_TABLE_HEADER(rsdt_table, addr); | |
238 | g_assert_cmphex(rsdt_table->signature, ==, ACPI_RSDT_SIGNATURE); | |
239 | ||
240 | /* compute the table entries in rsdt */ | |
241 | tables_nr = (rsdt_table->length - sizeof(AcpiRsdtDescriptorRev1)) / | |
242 | sizeof(uint32_t); | |
243 | g_assert_cmpint(tables_nr, >, 0); | |
244 | ||
245 | /* get the addresses of the tables pointed by rsdt */ | |
246 | tables = g_new0(uint32_t, tables_nr); | |
247 | ACPI_READ_ARRAY_PTR(tables, tables_nr, addr); | |
248 | ||
249 | checksum = acpi_checksum((uint8_t *)rsdt_table, rsdt_table->length) + | |
250 | acpi_checksum((uint8_t *)tables, tables_nr * sizeof(uint32_t)); | |
251 | g_assert(!checksum); | |
252 | ||
253 | /* SSDT tables after FADT */ | |
254 | data->rsdt_tables_addr = tables; | |
255 | data->rsdt_tables_nr = tables_nr; | |
256 | } | |
257 | ||
258 | static void test_acpi_fadt_table(test_data *data) | |
259 | { | |
260 | AcpiFadtDescriptorRev1 *fadt_table = &data->fadt_table; | |
261 | uint32_t addr; | |
262 | ||
263 | /* FADT table comes first */ | |
264 | addr = data->rsdt_tables_addr[0]; | |
265 | ACPI_READ_TABLE_HEADER(fadt_table, addr); | |
266 | ||
267 | ACPI_READ_FIELD(fadt_table->firmware_ctrl, addr); | |
268 | ACPI_READ_FIELD(fadt_table->dsdt, addr); | |
269 | ACPI_READ_FIELD(fadt_table->model, addr); | |
270 | ACPI_READ_FIELD(fadt_table->reserved1, addr); | |
271 | ACPI_READ_FIELD(fadt_table->sci_int, addr); | |
272 | ACPI_READ_FIELD(fadt_table->smi_cmd, addr); | |
273 | ACPI_READ_FIELD(fadt_table->acpi_enable, addr); | |
274 | ACPI_READ_FIELD(fadt_table->acpi_disable, addr); | |
275 | ACPI_READ_FIELD(fadt_table->S4bios_req, addr); | |
276 | ACPI_READ_FIELD(fadt_table->reserved2, addr); | |
277 | ACPI_READ_FIELD(fadt_table->pm1a_evt_blk, addr); | |
278 | ACPI_READ_FIELD(fadt_table->pm1b_evt_blk, addr); | |
279 | ACPI_READ_FIELD(fadt_table->pm1a_cnt_blk, addr); | |
280 | ACPI_READ_FIELD(fadt_table->pm1b_cnt_blk, addr); | |
281 | ACPI_READ_FIELD(fadt_table->pm2_cnt_blk, addr); | |
282 | ACPI_READ_FIELD(fadt_table->pm_tmr_blk, addr); | |
283 | ACPI_READ_FIELD(fadt_table->gpe0_blk, addr); | |
284 | ACPI_READ_FIELD(fadt_table->gpe1_blk, addr); | |
285 | ACPI_READ_FIELD(fadt_table->pm1_evt_len, addr); | |
286 | ACPI_READ_FIELD(fadt_table->pm1_cnt_len, addr); | |
287 | ACPI_READ_FIELD(fadt_table->pm2_cnt_len, addr); | |
288 | ACPI_READ_FIELD(fadt_table->pm_tmr_len, addr); | |
289 | ACPI_READ_FIELD(fadt_table->gpe0_blk_len, addr); | |
290 | ACPI_READ_FIELD(fadt_table->gpe1_blk_len, addr); | |
291 | ACPI_READ_FIELD(fadt_table->gpe1_base, addr); | |
292 | ACPI_READ_FIELD(fadt_table->reserved3, addr); | |
293 | ACPI_READ_FIELD(fadt_table->plvl2_lat, addr); | |
294 | ACPI_READ_FIELD(fadt_table->plvl3_lat, addr); | |
295 | ACPI_READ_FIELD(fadt_table->flush_size, addr); | |
296 | ACPI_READ_FIELD(fadt_table->flush_stride, addr); | |
297 | ACPI_READ_FIELD(fadt_table->duty_offset, addr); | |
298 | ACPI_READ_FIELD(fadt_table->duty_width, addr); | |
299 | ACPI_READ_FIELD(fadt_table->day_alrm, addr); | |
300 | ACPI_READ_FIELD(fadt_table->mon_alrm, addr); | |
301 | ACPI_READ_FIELD(fadt_table->century, addr); | |
302 | ACPI_READ_FIELD(fadt_table->reserved4, addr); | |
303 | ACPI_READ_FIELD(fadt_table->reserved4a, addr); | |
304 | ACPI_READ_FIELD(fadt_table->reserved4b, addr); | |
305 | ACPI_READ_FIELD(fadt_table->flags, addr); | |
306 | ||
307 | g_assert_cmphex(fadt_table->signature, ==, ACPI_FACP_SIGNATURE); | |
308 | g_assert(!acpi_checksum((uint8_t *)fadt_table, fadt_table->length)); | |
309 | } | |
310 | ||
15650602 MA |
311 | static void test_acpi_facs_table(test_data *data) |
312 | { | |
313 | AcpiFacsDescriptorRev1 *facs_table = &data->facs_table; | |
314 | uint32_t addr = data->fadt_table.firmware_ctrl; | |
315 | ||
316 | ACPI_READ_FIELD(facs_table->signature, addr); | |
317 | ACPI_READ_FIELD(facs_table->length, addr); | |
318 | ACPI_READ_FIELD(facs_table->hardware_signature, addr); | |
319 | ACPI_READ_FIELD(facs_table->firmware_waking_vector, addr); | |
320 | ACPI_READ_FIELD(facs_table->global_lock, addr); | |
321 | ACPI_READ_FIELD(facs_table->flags, addr); | |
322 | ACPI_READ_ARRAY(facs_table->resverved3, addr); | |
323 | ||
324 | g_assert_cmphex(facs_table->signature, ==, ACPI_FACS_SIGNATURE); | |
325 | } | |
326 | ||
53333801 MA |
327 | static void test_dst_table(AcpiSdtTable *sdt_table, uint32_t addr) |
328 | { | |
329 | uint8_t checksum; | |
330 | ||
331 | ACPI_READ_TABLE_HEADER(&sdt_table->header, addr); | |
332 | ||
333 | sdt_table->aml_len = sdt_table->header.length - sizeof(AcpiTableHeader); | |
334 | sdt_table->aml = g_malloc0(sdt_table->aml_len); | |
335 | ACPI_READ_ARRAY_PTR(sdt_table->aml, sdt_table->aml_len, addr); | |
336 | ||
337 | checksum = acpi_checksum((uint8_t *)sdt_table, sizeof(AcpiTableHeader)) + | |
9e8458c0 | 338 | acpi_checksum((uint8_t *)sdt_table->aml, sdt_table->aml_len); |
53333801 MA |
339 | g_assert(!checksum); |
340 | } | |
341 | ||
342 | static void test_acpi_dsdt_table(test_data *data) | |
343 | { | |
9e8458c0 | 344 | AcpiSdtTable dsdt_table; |
53333801 MA |
345 | uint32_t addr = data->fadt_table.dsdt; |
346 | ||
9e8458c0 | 347 | memset(&dsdt_table, 0, sizeof(dsdt_table)); |
a3a74ab9 | 348 | data->tables = g_array_new(false, true, sizeof(AcpiSdtTable)); |
9e8458c0 MA |
349 | |
350 | test_dst_table(&dsdt_table, addr); | |
351 | g_assert_cmphex(dsdt_table.header.signature, ==, ACPI_DSDT_SIGNATURE); | |
352 | ||
353 | /* Place DSDT first */ | |
a3a74ab9 | 354 | g_array_append_val(data->tables, dsdt_table); |
53333801 MA |
355 | } |
356 | ||
a3a74ab9 | 357 | static void test_acpi_tables(test_data *data) |
53333801 | 358 | { |
a3a74ab9 | 359 | int tables_nr = data->rsdt_tables_nr - 1; /* fadt is first */ |
53333801 MA |
360 | int i; |
361 | ||
a3a74ab9 | 362 | for (i = 0; i < tables_nr; i++) { |
8ac2adf7 | 363 | AcpiSdtTable ssdt_table; |
9e8458c0 MA |
364 | |
365 | memset(&ssdt_table, 0 , sizeof(ssdt_table)); | |
53333801 | 366 | uint32_t addr = data->rsdt_tables_addr[i + 1]; /* fadt is first */ |
8ac2adf7 | 367 | test_dst_table(&ssdt_table, addr); |
a3a74ab9 | 368 | g_array_append_val(data->tables, ssdt_table); |
53333801 | 369 | } |
9e8458c0 MA |
370 | } |
371 | ||
4500bc98 | 372 | static void dump_aml_files(test_data *data, bool rebuild) |
9e8458c0 MA |
373 | { |
374 | AcpiSdtTable *sdt; | |
375 | GError *error = NULL; | |
4500bc98 | 376 | gchar *aml_file = NULL; |
9e8458c0 MA |
377 | gint fd; |
378 | ssize_t ret; | |
379 | int i; | |
380 | ||
a3a74ab9 MA |
381 | for (i = 0; i < data->tables->len; ++i) { |
382 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); | |
9e8458c0 MA |
383 | g_assert(sdt->aml); |
384 | ||
4500bc98 MA |
385 | if (rebuild) { |
386 | aml_file = g_strdup_printf("%s/%s/%.4s", data_dir, data->machine, | |
387 | (gchar *)&sdt->header.signature); | |
388 | fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT, | |
389 | S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH); | |
390 | } else { | |
391 | fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error); | |
392 | g_assert_no_error(error); | |
393 | } | |
394 | g_assert(fd >= 0); | |
9e8458c0 MA |
395 | |
396 | ret = qemu_write_full(fd, sdt, sizeof(AcpiTableHeader)); | |
397 | g_assert(ret == sizeof(AcpiTableHeader)); | |
398 | ret = qemu_write_full(fd, sdt->aml, sdt->aml_len); | |
399 | g_assert(ret == sdt->aml_len); | |
400 | ||
401 | close(fd); | |
4500bc98 MA |
402 | |
403 | if (aml_file) { | |
404 | g_free(aml_file); | |
405 | } | |
9e8458c0 MA |
406 | } |
407 | } | |
408 | ||
69d09245 MA |
409 | static bool compare_signature(AcpiSdtTable *sdt, uint32_t signature) |
410 | { | |
411 | return sdt->header.signature == signature; | |
412 | } | |
413 | ||
9e8458c0 MA |
414 | static void load_asl(GArray *sdts, AcpiSdtTable *sdt) |
415 | { | |
416 | AcpiSdtTable *temp; | |
417 | GError *error = NULL; | |
cc8fa0e8 | 418 | GString *command_line = g_string_new(iasl); |
9e8458c0 MA |
419 | gint fd; |
420 | gchar *out, *out_err; | |
421 | gboolean ret; | |
422 | int i; | |
423 | ||
424 | fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error); | |
425 | g_assert_no_error(error); | |
426 | close(fd); | |
427 | ||
428 | /* build command line */ | |
cc8fa0e8 | 429 | g_string_append_printf(command_line, " -p %s ", sdt->asl_file); |
69d09245 MA |
430 | if (compare_signature(sdt, ACPI_DSDT_SIGNATURE) || |
431 | compare_signature(sdt, ACPI_SSDT_SIGNATURE)) { | |
432 | for (i = 0; i < sdts->len; ++i) { | |
433 | temp = &g_array_index(sdts, AcpiSdtTable, i); | |
434 | if (compare_signature(temp, ACPI_DSDT_SIGNATURE) || | |
435 | compare_signature(temp, ACPI_SSDT_SIGNATURE)) { | |
436 | g_string_append_printf(command_line, "-e %s ", temp->aml_file); | |
437 | } | |
438 | } | |
9e8458c0 MA |
439 | } |
440 | g_string_append_printf(command_line, "-d %s", sdt->aml_file); | |
441 | ||
442 | /* pass 'out' and 'out_err' in order to be redirected */ | |
443 | g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error); | |
444 | g_assert_no_error(error); | |
445 | ||
446 | ret = g_file_get_contents(sdt->asl_file, (gchar **)&sdt->asl, | |
447 | &sdt->asl_len, &error); | |
448 | g_assert(ret); | |
449 | g_assert_no_error(error); | |
450 | g_assert(sdt->asl_len); | |
451 | ||
452 | g_free(out); | |
453 | g_free(out_err); | |
454 | g_string_free(command_line, true); | |
455 | } | |
456 | ||
457 | #define COMMENT_END "*/" | |
458 | #define DEF_BLOCK "DefinitionBlock (" | |
459 | #define BLOCK_NAME_END ".aml" | |
460 | ||
461 | static GString *normalize_asl(gchar *asl_code) | |
462 | { | |
463 | GString *asl = g_string_new(asl_code); | |
464 | gchar *comment, *block_name; | |
465 | ||
466 | /* strip comments (different generation days) */ | |
467 | comment = g_strstr_len(asl->str, asl->len, COMMENT_END); | |
468 | if (comment) { | |
469 | asl = g_string_erase(asl, 0, comment + sizeof(COMMENT_END) - asl->str); | |
470 | } | |
471 | ||
472 | /* strip def block name (it has file path in it) */ | |
473 | if (g_str_has_prefix(asl->str, DEF_BLOCK)) { | |
474 | block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END); | |
475 | g_assert(block_name); | |
476 | asl = g_string_erase(asl, 0, | |
477 | block_name + sizeof(BLOCK_NAME_END) - asl->str); | |
478 | } | |
479 | ||
480 | return asl; | |
481 | } | |
482 | ||
483 | static GArray *load_expected_aml(test_data *data) | |
484 | { | |
485 | int i; | |
486 | AcpiSdtTable *sdt; | |
487 | gchar *aml_file; | |
488 | GError *error = NULL; | |
489 | gboolean ret; | |
490 | ||
a3a74ab9 MA |
491 | GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable)); |
492 | for (i = 0; i < data->tables->len; ++i) { | |
9e8458c0 | 493 | AcpiSdtTable exp_sdt; |
a3a74ab9 | 494 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
9e8458c0 MA |
495 | |
496 | memset(&exp_sdt, 0, sizeof(exp_sdt)); | |
497 | exp_sdt.header.signature = sdt->header.signature; | |
498 | ||
499 | aml_file = g_strdup_printf("%s/%s/%.4s", data_dir, data->machine, | |
500 | (gchar *)&exp_sdt.header.signature); | |
501 | exp_sdt.aml_file = aml_file; | |
502 | g_assert(g_file_test(aml_file, G_FILE_TEST_EXISTS)); | |
503 | ret = g_file_get_contents(aml_file, &exp_sdt.aml, | |
504 | &exp_sdt.aml_len, &error); | |
505 | g_assert(ret); | |
506 | g_assert_no_error(error); | |
507 | g_assert(exp_sdt.aml); | |
508 | g_assert(exp_sdt.aml_len); | |
509 | ||
a3a74ab9 | 510 | g_array_append_val(exp_tables, exp_sdt); |
9e8458c0 MA |
511 | } |
512 | ||
a3a74ab9 | 513 | return exp_tables; |
9e8458c0 MA |
514 | } |
515 | ||
516 | static void test_acpi_asl(test_data *data) | |
517 | { | |
518 | int i; | |
519 | AcpiSdtTable *sdt, *exp_sdt; | |
520 | test_data exp_data; | |
521 | ||
522 | memset(&exp_data, 0, sizeof(exp_data)); | |
a3a74ab9 | 523 | exp_data.tables = load_expected_aml(data); |
4500bc98 | 524 | dump_aml_files(data, false); |
a3a74ab9 | 525 | for (i = 0; i < data->tables->len; ++i) { |
9e8458c0 MA |
526 | GString *asl, *exp_asl; |
527 | ||
a3a74ab9 MA |
528 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
529 | exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i); | |
9e8458c0 | 530 | |
a3a74ab9 | 531 | load_asl(data->tables, sdt); |
9e8458c0 MA |
532 | asl = normalize_asl(sdt->asl); |
533 | ||
a3a74ab9 | 534 | load_asl(exp_data.tables, exp_sdt); |
9e8458c0 MA |
535 | exp_asl = normalize_asl(exp_sdt->asl); |
536 | ||
0651596c | 537 | if (g_strcmp0(asl->str, exp_asl->str)) { |
262f6f51 MA |
538 | sdt->tmp_files_retain = true; |
539 | exp_sdt->tmp_files_retain = true; | |
0651596c MA |
540 | fprintf(stderr, |
541 | "acpi-test: Warning! %.4s mismatch. " | |
262f6f51 | 542 | "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n", |
0651596c | 543 | (gchar *)&exp_sdt->header.signature, |
262f6f51 MA |
544 | sdt->asl_file, sdt->aml_file, |
545 | exp_sdt->asl_file, exp_sdt->aml_file); | |
0651596c | 546 | } |
9e8458c0 MA |
547 | g_string_free(asl, true); |
548 | g_string_free(exp_asl, true); | |
549 | } | |
550 | ||
551 | free_test_data(&exp_data); | |
53333801 MA |
552 | } |
553 | ||
8ac2adf7 | 554 | static void test_acpi_one(const char *params, test_data *data) |
ad6423a7 MT |
555 | { |
556 | char *args; | |
557 | uint8_t signature_low; | |
558 | uint8_t signature_high; | |
559 | uint16_t signature; | |
560 | int i; | |
9e8458c0 | 561 | const char *device = ""; |
ad6423a7 | 562 | |
9e8458c0 MA |
563 | if (!g_strcmp0(data->machine, MACHINE_Q35)) { |
564 | device = ",id=hd -device ide-hd,drive=hd"; | |
565 | } | |
566 | ||
567 | args = g_strdup_printf("-net none -display none %s -drive file=%s%s,", | |
568 | params ? params : "", disk, device); | |
ad6423a7 MT |
569 | qtest_start(args); |
570 | ||
571 | /* Wait at most 1 minute */ | |
572 | #define TEST_DELAY (1 * G_USEC_PER_SEC / 10) | |
573 | #define TEST_CYCLES MAX((60 * G_USEC_PER_SEC / TEST_DELAY), 1) | |
574 | ||
575 | /* Poll until code has run and modified memory. Once it has we know BIOS | |
576 | * initialization is done. TODO: check that IP reached the halt | |
577 | * instruction. | |
578 | */ | |
579 | for (i = 0; i < TEST_CYCLES; ++i) { | |
580 | signature_low = readb(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET); | |
581 | signature_high = readb(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET + 1); | |
582 | signature = (signature_high << 8) | signature_low; | |
583 | if (signature == SIGNATURE) { | |
584 | break; | |
585 | } | |
586 | g_usleep(TEST_DELAY); | |
587 | } | |
588 | g_assert_cmphex(signature, ==, SIGNATURE); | |
589 | ||
8ac2adf7 MA |
590 | test_acpi_rsdp_address(data); |
591 | test_acpi_rsdp_table(data); | |
592 | test_acpi_rsdt_table(data); | |
593 | test_acpi_fadt_table(data); | |
15650602 | 594 | test_acpi_facs_table(data); |
8ac2adf7 | 595 | test_acpi_dsdt_table(data); |
a3a74ab9 | 596 | test_acpi_tables(data); |
ad6423a7 | 597 | |
cc8fa0e8 | 598 | if (iasl) { |
4500bc98 MA |
599 | if (getenv(ACPI_REBUILD_EXPECTED_AML)) { |
600 | dump_aml_files(data, true); | |
601 | } else { | |
602 | test_acpi_asl(data); | |
603 | } | |
9e8458c0 MA |
604 | } |
605 | ||
ad6423a7 MT |
606 | qtest_quit(global_qtest); |
607 | g_free(args); | |
608 | } | |
609 | ||
610 | static void test_acpi_tcg(void) | |
611 | { | |
8ac2adf7 MA |
612 | test_data data; |
613 | ||
ad6423a7 MT |
614 | /* Supplying -machine accel argument overrides the default (qtest). |
615 | * This is to make guest actually run. | |
616 | */ | |
9e8458c0 MA |
617 | memset(&data, 0, sizeof(data)); |
618 | data.machine = MACHINE_PC; | |
8ac2adf7 | 619 | test_acpi_one("-machine accel=tcg", &data); |
9e8458c0 | 620 | free_test_data(&data); |
8ac2adf7 | 621 | |
9e8458c0 MA |
622 | memset(&data, 0, sizeof(data)); |
623 | data.machine = MACHINE_Q35; | |
624 | test_acpi_one("-machine q35,accel=tcg", &data); | |
8ac2adf7 | 625 | free_test_data(&data); |
ad6423a7 MT |
626 | } |
627 | ||
628 | int main(int argc, char *argv[]) | |
629 | { | |
630 | const char *arch = qtest_get_arch(); | |
631 | FILE *f = fopen(disk, "w"); | |
5862ad0f | 632 | int ret; |
ad6423a7 MT |
633 | fwrite(boot_sector, 1, sizeof boot_sector, f); |
634 | fclose(f); | |
635 | ||
636 | g_test_init(&argc, &argv, NULL); | |
637 | ||
638 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { | |
639 | qtest_add_func("acpi/tcg", test_acpi_tcg); | |
640 | } | |
5862ad0f FZ |
641 | ret = g_test_run(); |
642 | unlink(disk); | |
643 | return ret; | |
ad6423a7 | 644 | } |