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