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