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