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