]>
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 MA |
25 | #define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML" |
26 | ||
53333801 | 27 | /* DSDT and SSDTs format */ |
ad6423a7 | 28 | typedef struct { |
53333801 | 29 | AcpiTableHeader header; |
9e8458c0 MA |
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 | } QEMU_PACKED AcpiSdtTable; | |
53333801 MA |
37 | |
38 | typedef struct { | |
9e8458c0 | 39 | const char *machine; |
53333801 MA |
40 | uint32_t rsdp_addr; |
41 | AcpiRsdpDescriptor rsdp_table; | |
42 | AcpiRsdtDescriptorRev1 rsdt_table; | |
43 | AcpiFadtDescriptorRev1 fadt_table; | |
15650602 | 44 | AcpiFacsDescriptorRev1 facs_table; |
53333801 MA |
45 | uint32_t *rsdt_tables_addr; |
46 | int rsdt_tables_nr; | |
9e8458c0 | 47 | GArray *ssdt_tables; /* first is DSDT */ |
53333801 | 48 | } test_data; |
ad6423a7 MT |
49 | |
50 | #define LOW(x) ((x) & 0xff) | |
51 | #define HIGH(x) ((x) >> 8) | |
52 | ||
53 | #define SIGNATURE 0xdead | |
54 | #define SIGNATURE_OFFSET 0x10 | |
55 | #define BOOT_SECTOR_ADDRESS 0x7c00 | |
56 | ||
53333801 MA |
57 | #define ACPI_READ_FIELD(field, addr) \ |
58 | do { \ | |
59 | switch (sizeof(field)) { \ | |
60 | case 1: \ | |
61 | field = readb(addr); \ | |
62 | break; \ | |
63 | case 2: \ | |
084137dd | 64 | field = readw(addr); \ |
53333801 MA |
65 | break; \ |
66 | case 4: \ | |
084137dd | 67 | field = readl(addr); \ |
53333801 MA |
68 | break; \ |
69 | case 8: \ | |
084137dd | 70 | field = readq(addr); \ |
53333801 MA |
71 | break; \ |
72 | default: \ | |
73 | g_assert(false); \ | |
74 | } \ | |
75 | addr += sizeof(field); \ | |
76 | } while (0); | |
77 | ||
78 | #define ACPI_READ_ARRAY_PTR(arr, length, addr) \ | |
79 | do { \ | |
80 | int idx; \ | |
81 | for (idx = 0; idx < length; ++idx) { \ | |
82 | ACPI_READ_FIELD(arr[idx], addr); \ | |
83 | } \ | |
84 | } while (0); | |
85 | ||
86 | #define ACPI_READ_ARRAY(arr, addr) \ | |
87 | ACPI_READ_ARRAY_PTR(arr, sizeof(arr)/sizeof(arr[0]), addr) | |
88 | ||
89 | #define ACPI_READ_TABLE_HEADER(table, addr) \ | |
90 | do { \ | |
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); \ | |
100 | } while (0); | |
101 | ||
ad6423a7 MT |
102 | /* Boot sector code: write SIGNATURE into memory, |
103 | * then halt. | |
9e8458c0 MA |
104 | * Q35 machine requires a minimum 0x7e000 bytes disk. |
105 | * (bug or feature?) | |
ad6423a7 | 106 | */ |
9e8458c0 | 107 | static uint8_t boot_sector[0x7e000] = { |
ad6423a7 MT |
108 | /* 7c00: mov $0xdead,%ax */ |
109 | [0x00] = 0xb8, | |
110 | [0x01] = LOW(SIGNATURE), | |
111 | [0x02] = HIGH(SIGNATURE), | |
112 | /* 7c03: mov %ax,0x7c10 */ | |
113 | [0x03] = 0xa3, | |
114 | [0x04] = LOW(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET), | |
115 | [0x05] = HIGH(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET), | |
116 | /* 7c06: cli */ | |
117 | [0x06] = 0xfa, | |
118 | /* 7c07: hlt */ | |
119 | [0x07] = 0xf4, | |
120 | /* 7c08: jmp 0x7c07=0x7c0a-3 */ | |
121 | [0x08] = 0xeb, | |
122 | [0x09] = LOW(-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 */ | |
127 | [0x1FE] = 0x55, | |
128 | [0x1FF] = 0xAA, | |
129 | }; | |
130 | ||
131 | static const char *disk = "tests/acpi-test-disk.raw"; | |
9e8458c0 | 132 | static const char *data_dir = "tests/acpi-test-data"; |
cc8fa0e8 MA |
133 | #ifdef CONFIG_IASL |
134 | static const char *iasl = stringify(CONFIG_IASL); | |
135 | #else | |
136 | static const char *iasl; | |
137 | #endif | |
ad6423a7 | 138 | |
8ac2adf7 MA |
139 | static void free_test_data(test_data *data) |
140 | { | |
9e8458c0 | 141 | AcpiSdtTable *temp; |
8ac2adf7 MA |
142 | int i; |
143 | ||
9e8458c0 MA |
144 | if (data->rsdt_tables_addr) { |
145 | g_free(data->rsdt_tables_addr); | |
146 | } | |
147 | ||
8ac2adf7 | 148 | for (i = 0; i < data->ssdt_tables->len; ++i) { |
9e8458c0 MA |
149 | temp = &g_array_index(data->ssdt_tables, AcpiSdtTable, i); |
150 | if (temp->aml) { | |
151 | g_free(temp->aml); | |
152 | } | |
153 | if (temp->aml_file) { | |
154 | if (g_strstr_len(temp->aml_file, -1, "aml-")) { | |
155 | unlink(temp->aml_file); | |
156 | } | |
157 | g_free(temp->aml_file); | |
158 | } | |
159 | if (temp->asl) { | |
160 | g_free(temp->asl); | |
161 | } | |
162 | if (temp->asl_file) { | |
163 | if (g_strstr_len(temp->asl_file, -1, "asl-")) { | |
164 | unlink(temp->asl_file); | |
165 | } | |
166 | g_free(temp->asl_file); | |
167 | } | |
8ac2adf7 | 168 | } |
9e8458c0 | 169 | |
8ac2adf7 | 170 | g_array_free(data->ssdt_tables, false); |
8ac2adf7 MA |
171 | } |
172 | ||
53333801 MA |
173 | static uint8_t acpi_checksum(const uint8_t *data, int len) |
174 | { | |
175 | int i; | |
176 | uint8_t sum = 0; | |
177 | ||
178 | for (i = 0; i < len; i++) { | |
179 | sum += data[i]; | |
180 | } | |
181 | ||
182 | return sum; | |
183 | } | |
184 | ||
185 | static void test_acpi_rsdp_address(test_data *data) | |
186 | { | |
187 | uint32_t off; | |
188 | ||
189 | /* OK, now find RSDP */ | |
190 | for (off = 0xf0000; off < 0x100000; off += 0x10) { | |
191 | uint8_t sig[] = "RSD PTR "; | |
192 | int i; | |
193 | ||
194 | for (i = 0; i < sizeof sig - 1; ++i) { | |
195 | sig[i] = readb(off + i); | |
196 | } | |
197 | ||
198 | if (!memcmp(sig, "RSD PTR ", sizeof sig)) { | |
199 | break; | |
200 | } | |
201 | } | |
202 | ||
203 | g_assert_cmphex(off, <, 0x100000); | |
204 | data->rsdp_addr = off; | |
205 | } | |
206 | ||
207 | static void test_acpi_rsdp_table(test_data *data) | |
208 | { | |
209 | AcpiRsdpDescriptor *rsdp_table = &data->rsdp_table; | |
210 | uint32_t addr = data->rsdp_addr; | |
211 | ||
212 | ACPI_READ_FIELD(rsdp_table->signature, addr); | |
213 | g_assert_cmphex(rsdp_table->signature, ==, ACPI_RSDP_SIGNATURE); | |
214 | ||
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); | |
220 | ||
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)); | |
223 | } | |
224 | ||
225 | static void test_acpi_rsdt_table(test_data *data) | |
226 | { | |
227 | AcpiRsdtDescriptorRev1 *rsdt_table = &data->rsdt_table; | |
228 | uint32_t addr = data->rsdp_table.rsdt_physical_address; | |
229 | uint32_t *tables; | |
230 | int tables_nr; | |
231 | uint8_t checksum; | |
232 | ||
233 | /* read the header */ | |
234 | ACPI_READ_TABLE_HEADER(rsdt_table, addr); | |
235 | g_assert_cmphex(rsdt_table->signature, ==, ACPI_RSDT_SIGNATURE); | |
236 | ||
237 | /* compute the table entries in rsdt */ | |
238 | tables_nr = (rsdt_table->length - sizeof(AcpiRsdtDescriptorRev1)) / | |
239 | sizeof(uint32_t); | |
240 | g_assert_cmpint(tables_nr, >, 0); | |
241 | ||
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); | |
245 | ||
246 | checksum = acpi_checksum((uint8_t *)rsdt_table, rsdt_table->length) + | |
247 | acpi_checksum((uint8_t *)tables, tables_nr * sizeof(uint32_t)); | |
248 | g_assert(!checksum); | |
249 | ||
250 | /* SSDT tables after FADT */ | |
251 | data->rsdt_tables_addr = tables; | |
252 | data->rsdt_tables_nr = tables_nr; | |
253 | } | |
254 | ||
255 | static void test_acpi_fadt_table(test_data *data) | |
256 | { | |
257 | AcpiFadtDescriptorRev1 *fadt_table = &data->fadt_table; | |
258 | uint32_t addr; | |
259 | ||
260 | /* FADT table comes first */ | |
261 | addr = data->rsdt_tables_addr[0]; | |
262 | ACPI_READ_TABLE_HEADER(fadt_table, addr); | |
263 | ||
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); | |
303 | ||
304 | g_assert_cmphex(fadt_table->signature, ==, ACPI_FACP_SIGNATURE); | |
305 | g_assert(!acpi_checksum((uint8_t *)fadt_table, fadt_table->length)); | |
306 | } | |
307 | ||
15650602 MA |
308 | static void test_acpi_facs_table(test_data *data) |
309 | { | |
310 | AcpiFacsDescriptorRev1 *facs_table = &data->facs_table; | |
311 | uint32_t addr = data->fadt_table.firmware_ctrl; | |
312 | ||
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); | |
320 | ||
321 | g_assert_cmphex(facs_table->signature, ==, ACPI_FACS_SIGNATURE); | |
322 | } | |
323 | ||
53333801 MA |
324 | static void test_dst_table(AcpiSdtTable *sdt_table, uint32_t addr) |
325 | { | |
326 | uint8_t checksum; | |
327 | ||
328 | ACPI_READ_TABLE_HEADER(&sdt_table->header, addr); | |
329 | ||
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); | |
333 | ||
334 | checksum = acpi_checksum((uint8_t *)sdt_table, sizeof(AcpiTableHeader)) + | |
9e8458c0 | 335 | acpi_checksum((uint8_t *)sdt_table->aml, sdt_table->aml_len); |
53333801 MA |
336 | g_assert(!checksum); |
337 | } | |
338 | ||
339 | static void test_acpi_dsdt_table(test_data *data) | |
340 | { | |
9e8458c0 | 341 | AcpiSdtTable dsdt_table; |
53333801 MA |
342 | uint32_t addr = data->fadt_table.dsdt; |
343 | ||
9e8458c0 MA |
344 | memset(&dsdt_table, 0, sizeof(dsdt_table)); |
345 | data->ssdt_tables = g_array_new(false, true, sizeof(AcpiSdtTable)); | |
346 | ||
347 | test_dst_table(&dsdt_table, addr); | |
348 | g_assert_cmphex(dsdt_table.header.signature, ==, ACPI_DSDT_SIGNATURE); | |
349 | ||
350 | /* Place DSDT first */ | |
351 | g_array_append_val(data->ssdt_tables, dsdt_table); | |
53333801 MA |
352 | } |
353 | ||
354 | static void test_acpi_ssdt_tables(test_data *data) | |
355 | { | |
53333801 MA |
356 | int ssdt_tables_nr = data->rsdt_tables_nr - 1; /* fadt is first */ |
357 | int i; | |
358 | ||
53333801 | 359 | for (i = 0; i < ssdt_tables_nr; i++) { |
8ac2adf7 | 360 | AcpiSdtTable ssdt_table; |
9e8458c0 MA |
361 | |
362 | memset(&ssdt_table, 0 , sizeof(ssdt_table)); | |
53333801 | 363 | uint32_t addr = data->rsdt_tables_addr[i + 1]; /* fadt is first */ |
8ac2adf7 | 364 | test_dst_table(&ssdt_table, addr); |
9e8458c0 | 365 | g_array_append_val(data->ssdt_tables, ssdt_table); |
53333801 | 366 | } |
9e8458c0 MA |
367 | } |
368 | ||
4500bc98 | 369 | static void dump_aml_files(test_data *data, bool rebuild) |
9e8458c0 MA |
370 | { |
371 | AcpiSdtTable *sdt; | |
372 | GError *error = NULL; | |
4500bc98 | 373 | gchar *aml_file = NULL; |
9e8458c0 MA |
374 | gint fd; |
375 | ssize_t ret; | |
376 | int i; | |
377 | ||
378 | for (i = 0; i < data->ssdt_tables->len; ++i) { | |
379 | sdt = &g_array_index(data->ssdt_tables, AcpiSdtTable, i); | |
380 | g_assert(sdt->aml); | |
381 | ||
4500bc98 MA |
382 | if (rebuild) { |
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); | |
387 | } else { | |
388 | fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error); | |
389 | g_assert_no_error(error); | |
390 | } | |
391 | g_assert(fd >= 0); | |
9e8458c0 MA |
392 | |
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); | |
397 | ||
398 | close(fd); | |
4500bc98 MA |
399 | |
400 | if (aml_file) { | |
401 | g_free(aml_file); | |
402 | } | |
9e8458c0 MA |
403 | } |
404 | } | |
405 | ||
406 | static void load_asl(GArray *sdts, AcpiSdtTable *sdt) | |
407 | { | |
408 | AcpiSdtTable *temp; | |
409 | GError *error = NULL; | |
cc8fa0e8 | 410 | GString *command_line = g_string_new(iasl); |
9e8458c0 MA |
411 | gint fd; |
412 | gchar *out, *out_err; | |
413 | gboolean ret; | |
414 | int i; | |
415 | ||
416 | fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error); | |
417 | g_assert_no_error(error); | |
418 | close(fd); | |
419 | ||
420 | /* build command line */ | |
cc8fa0e8 | 421 | g_string_append_printf(command_line, " -p %s ", sdt->asl_file); |
9e8458c0 MA |
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); | |
425 | } | |
426 | g_string_append_printf(command_line, "-d %s", sdt->aml_file); | |
427 | ||
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); | |
431 | ||
432 | ret = g_file_get_contents(sdt->asl_file, (gchar **)&sdt->asl, | |
433 | &sdt->asl_len, &error); | |
434 | g_assert(ret); | |
435 | g_assert_no_error(error); | |
436 | g_assert(sdt->asl_len); | |
437 | ||
438 | g_free(out); | |
439 | g_free(out_err); | |
440 | g_string_free(command_line, true); | |
441 | } | |
442 | ||
443 | #define COMMENT_END "*/" | |
444 | #define DEF_BLOCK "DefinitionBlock (" | |
445 | #define BLOCK_NAME_END ".aml" | |
446 | ||
447 | static GString *normalize_asl(gchar *asl_code) | |
448 | { | |
449 | GString *asl = g_string_new(asl_code); | |
450 | gchar *comment, *block_name; | |
451 | ||
452 | /* strip comments (different generation days) */ | |
453 | comment = g_strstr_len(asl->str, asl->len, COMMENT_END); | |
454 | if (comment) { | |
455 | asl = g_string_erase(asl, 0, comment + sizeof(COMMENT_END) - asl->str); | |
456 | } | |
457 | ||
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); | |
464 | } | |
465 | ||
466 | return asl; | |
467 | } | |
468 | ||
469 | static GArray *load_expected_aml(test_data *data) | |
470 | { | |
471 | int i; | |
472 | AcpiSdtTable *sdt; | |
473 | gchar *aml_file; | |
474 | GError *error = NULL; | |
475 | gboolean ret; | |
476 | ||
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); | |
481 | ||
482 | memset(&exp_sdt, 0, sizeof(exp_sdt)); | |
483 | exp_sdt.header.signature = sdt->header.signature; | |
484 | ||
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); | |
491 | g_assert(ret); | |
492 | g_assert_no_error(error); | |
493 | g_assert(exp_sdt.aml); | |
494 | g_assert(exp_sdt.aml_len); | |
495 | ||
496 | g_array_append_val(exp_ssdt_tables, exp_sdt); | |
497 | } | |
498 | ||
499 | return exp_ssdt_tables; | |
500 | } | |
501 | ||
502 | static void test_acpi_asl(test_data *data) | |
503 | { | |
504 | int i; | |
505 | AcpiSdtTable *sdt, *exp_sdt; | |
506 | test_data exp_data; | |
507 | ||
508 | memset(&exp_data, 0, sizeof(exp_data)); | |
509 | exp_data.ssdt_tables = load_expected_aml(data); | |
4500bc98 | 510 | dump_aml_files(data, false); |
9e8458c0 MA |
511 | for (i = 0; i < data->ssdt_tables->len; ++i) { |
512 | GString *asl, *exp_asl; | |
513 | ||
514 | sdt = &g_array_index(data->ssdt_tables, AcpiSdtTable, i); | |
515 | exp_sdt = &g_array_index(exp_data.ssdt_tables, AcpiSdtTable, i); | |
516 | ||
517 | load_asl(data->ssdt_tables, sdt); | |
518 | asl = normalize_asl(sdt->asl); | |
519 | ||
520 | load_asl(exp_data.ssdt_tables, exp_sdt); | |
521 | exp_asl = normalize_asl(exp_sdt->asl); | |
522 | ||
523 | g_assert(!g_strcmp0(asl->str, exp_asl->str)); | |
524 | g_string_free(asl, true); | |
525 | g_string_free(exp_asl, true); | |
526 | } | |
527 | ||
528 | free_test_data(&exp_data); | |
53333801 MA |
529 | } |
530 | ||
8ac2adf7 | 531 | static void test_acpi_one(const char *params, test_data *data) |
ad6423a7 MT |
532 | { |
533 | char *args; | |
534 | uint8_t signature_low; | |
535 | uint8_t signature_high; | |
536 | uint16_t signature; | |
537 | int i; | |
9e8458c0 | 538 | const char *device = ""; |
ad6423a7 | 539 | |
9e8458c0 MA |
540 | if (!g_strcmp0(data->machine, MACHINE_Q35)) { |
541 | device = ",id=hd -device ide-hd,drive=hd"; | |
542 | } | |
543 | ||
544 | args = g_strdup_printf("-net none -display none %s -drive file=%s%s,", | |
545 | params ? params : "", disk, device); | |
ad6423a7 MT |
546 | qtest_start(args); |
547 | ||
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) | |
551 | ||
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 | |
554 | * instruction. | |
555 | */ | |
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) { | |
561 | break; | |
562 | } | |
563 | g_usleep(TEST_DELAY); | |
564 | } | |
565 | g_assert_cmphex(signature, ==, SIGNATURE); | |
566 | ||
8ac2adf7 MA |
567 | test_acpi_rsdp_address(data); |
568 | test_acpi_rsdp_table(data); | |
569 | test_acpi_rsdt_table(data); | |
570 | test_acpi_fadt_table(data); | |
15650602 | 571 | test_acpi_facs_table(data); |
8ac2adf7 MA |
572 | test_acpi_dsdt_table(data); |
573 | test_acpi_ssdt_tables(data); | |
ad6423a7 | 574 | |
cc8fa0e8 | 575 | if (iasl) { |
4500bc98 MA |
576 | if (getenv(ACPI_REBUILD_EXPECTED_AML)) { |
577 | dump_aml_files(data, true); | |
578 | } else { | |
579 | test_acpi_asl(data); | |
580 | } | |
9e8458c0 MA |
581 | } |
582 | ||
ad6423a7 MT |
583 | qtest_quit(global_qtest); |
584 | g_free(args); | |
585 | } | |
586 | ||
587 | static void test_acpi_tcg(void) | |
588 | { | |
8ac2adf7 MA |
589 | test_data data; |
590 | ||
ad6423a7 MT |
591 | /* Supplying -machine accel argument overrides the default (qtest). |
592 | * This is to make guest actually run. | |
593 | */ | |
9e8458c0 MA |
594 | memset(&data, 0, sizeof(data)); |
595 | data.machine = MACHINE_PC; | |
8ac2adf7 | 596 | test_acpi_one("-machine accel=tcg", &data); |
9e8458c0 | 597 | free_test_data(&data); |
8ac2adf7 | 598 | |
9e8458c0 MA |
599 | memset(&data, 0, sizeof(data)); |
600 | data.machine = MACHINE_Q35; | |
601 | test_acpi_one("-machine q35,accel=tcg", &data); | |
8ac2adf7 | 602 | free_test_data(&data); |
ad6423a7 MT |
603 | } |
604 | ||
605 | int main(int argc, char *argv[]) | |
606 | { | |
607 | const char *arch = qtest_get_arch(); | |
608 | FILE *f = fopen(disk, "w"); | |
5862ad0f | 609 | int ret; |
ad6423a7 MT |
610 | fwrite(boot_sector, 1, sizeof boot_sector, f); |
611 | fclose(f); | |
612 | ||
613 | g_test_init(&argc, &argv, NULL); | |
614 | ||
615 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { | |
616 | qtest_add_func("acpi/tcg", test_acpi_tcg); | |
617 | } | |
5862ad0f FZ |
618 | ret = g_test_run(); |
619 | unlink(disk); | |
620 | return ret; | |
ad6423a7 | 621 | } |