Commit | Line | Data |
---|---|---|
ad6423a7 MT |
1 | /* |
2 | * Boot order test cases. | |
3 | * | |
4 | * Copyright (c) 2013 Red Hat Inc. | |
5 | * | |
6 | * Authors: | |
7 | * Michael S. Tsirkin <mst@redhat.com>, | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
10 | * See the COPYING file in the top-level directory. | |
11 | */ | |
12 | ||
13 | #include <string.h> | |
14 | #include <stdio.h> | |
15 | #include <glib.h> | |
4500bc98 | 16 | #include <glib/gstdio.h> |
53333801 | 17 | #include "qemu-common.h" |
ad6423a7 | 18 | #include "libqtest.h" |
53333801 MA |
19 | #include "qemu/compiler.h" |
20 | #include "hw/i386/acpi-defs.h" | |
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; |
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 GS |
52 | uint32_t smbios_ep_addr; |
53 | struct smbios_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 | ||
9e8458c0 MA |
164 | if (data->rsdt_tables_addr) { |
165 | g_free(data->rsdt_tables_addr); | |
166 | } | |
167 | ||
a3a74ab9 MA |
168 | for (i = 0; i < data->tables->len; ++i) { |
169 | temp = &g_array_index(data->tables, AcpiSdtTable, i); | |
9e8458c0 MA |
170 | if (temp->aml) { |
171 | g_free(temp->aml); | |
172 | } | |
173 | if (temp->aml_file) { | |
262f6f51 MA |
174 | if (!temp->tmp_files_retain && |
175 | g_strstr_len(temp->aml_file, -1, "aml-")) { | |
9e8458c0 MA |
176 | unlink(temp->aml_file); |
177 | } | |
178 | g_free(temp->aml_file); | |
179 | } | |
180 | if (temp->asl) { | |
181 | g_free(temp->asl); | |
182 | } | |
183 | if (temp->asl_file) { | |
262f6f51 | 184 | if (!temp->tmp_files_retain) { |
9e8458c0 MA |
185 | unlink(temp->asl_file); |
186 | } | |
187 | g_free(temp->asl_file); | |
188 | } | |
8ac2adf7 | 189 | } |
9e8458c0 | 190 | |
a3a74ab9 | 191 | g_array_free(data->tables, false); |
8ac2adf7 MA |
192 | } |
193 | ||
53333801 MA |
194 | static uint8_t acpi_checksum(const uint8_t *data, int len) |
195 | { | |
196 | int i; | |
197 | uint8_t sum = 0; | |
198 | ||
199 | for (i = 0; i < len; i++) { | |
200 | sum += data[i]; | |
201 | } | |
202 | ||
203 | return sum; | |
204 | } | |
205 | ||
206 | static void test_acpi_rsdp_address(test_data *data) | |
207 | { | |
208 | uint32_t off; | |
209 | ||
210 | /* OK, now find RSDP */ | |
211 | for (off = 0xf0000; off < 0x100000; off += 0x10) { | |
212 | uint8_t sig[] = "RSD PTR "; | |
213 | int i; | |
214 | ||
215 | for (i = 0; i < sizeof sig - 1; ++i) { | |
216 | sig[i] = readb(off + i); | |
217 | } | |
218 | ||
219 | if (!memcmp(sig, "RSD PTR ", sizeof sig)) { | |
220 | break; | |
221 | } | |
222 | } | |
223 | ||
224 | g_assert_cmphex(off, <, 0x100000); | |
225 | data->rsdp_addr = off; | |
226 | } | |
227 | ||
228 | static void test_acpi_rsdp_table(test_data *data) | |
229 | { | |
230 | AcpiRsdpDescriptor *rsdp_table = &data->rsdp_table; | |
231 | uint32_t addr = data->rsdp_addr; | |
232 | ||
233 | ACPI_READ_FIELD(rsdp_table->signature, addr); | |
c225aa3c | 234 | ACPI_ASSERT_CMP64(rsdp_table->signature, "RSD PTR "); |
53333801 MA |
235 | |
236 | ACPI_READ_FIELD(rsdp_table->checksum, addr); | |
237 | ACPI_READ_ARRAY(rsdp_table->oem_id, addr); | |
238 | ACPI_READ_FIELD(rsdp_table->revision, addr); | |
239 | ACPI_READ_FIELD(rsdp_table->rsdt_physical_address, addr); | |
240 | ACPI_READ_FIELD(rsdp_table->length, addr); | |
241 | ||
242 | /* rsdp checksum is not for the whole table, but for the first 20 bytes */ | |
243 | g_assert(!acpi_checksum((uint8_t *)rsdp_table, 20)); | |
244 | } | |
245 | ||
246 | static void test_acpi_rsdt_table(test_data *data) | |
247 | { | |
248 | AcpiRsdtDescriptorRev1 *rsdt_table = &data->rsdt_table; | |
249 | uint32_t addr = data->rsdp_table.rsdt_physical_address; | |
250 | uint32_t *tables; | |
251 | int tables_nr; | |
252 | uint8_t checksum; | |
253 | ||
254 | /* read the header */ | |
255 | ACPI_READ_TABLE_HEADER(rsdt_table, addr); | |
c225aa3c | 256 | ACPI_ASSERT_CMP(rsdt_table->signature, "RSDT"); |
53333801 MA |
257 | |
258 | /* compute the table entries in rsdt */ | |
259 | tables_nr = (rsdt_table->length - sizeof(AcpiRsdtDescriptorRev1)) / | |
260 | sizeof(uint32_t); | |
261 | g_assert_cmpint(tables_nr, >, 0); | |
262 | ||
263 | /* get the addresses of the tables pointed by rsdt */ | |
264 | tables = g_new0(uint32_t, tables_nr); | |
265 | ACPI_READ_ARRAY_PTR(tables, tables_nr, addr); | |
266 | ||
267 | checksum = acpi_checksum((uint8_t *)rsdt_table, rsdt_table->length) + | |
268 | acpi_checksum((uint8_t *)tables, tables_nr * sizeof(uint32_t)); | |
269 | g_assert(!checksum); | |
270 | ||
271 | /* SSDT tables after FADT */ | |
272 | data->rsdt_tables_addr = tables; | |
273 | data->rsdt_tables_nr = tables_nr; | |
274 | } | |
275 | ||
276 | static void test_acpi_fadt_table(test_data *data) | |
277 | { | |
278 | AcpiFadtDescriptorRev1 *fadt_table = &data->fadt_table; | |
279 | uint32_t addr; | |
280 | ||
281 | /* FADT table comes first */ | |
282 | addr = data->rsdt_tables_addr[0]; | |
283 | ACPI_READ_TABLE_HEADER(fadt_table, addr); | |
284 | ||
285 | ACPI_READ_FIELD(fadt_table->firmware_ctrl, addr); | |
286 | ACPI_READ_FIELD(fadt_table->dsdt, addr); | |
287 | ACPI_READ_FIELD(fadt_table->model, addr); | |
288 | ACPI_READ_FIELD(fadt_table->reserved1, addr); | |
289 | ACPI_READ_FIELD(fadt_table->sci_int, addr); | |
290 | ACPI_READ_FIELD(fadt_table->smi_cmd, addr); | |
291 | ACPI_READ_FIELD(fadt_table->acpi_enable, addr); | |
292 | ACPI_READ_FIELD(fadt_table->acpi_disable, addr); | |
293 | ACPI_READ_FIELD(fadt_table->S4bios_req, addr); | |
294 | ACPI_READ_FIELD(fadt_table->reserved2, addr); | |
295 | ACPI_READ_FIELD(fadt_table->pm1a_evt_blk, addr); | |
296 | ACPI_READ_FIELD(fadt_table->pm1b_evt_blk, addr); | |
297 | ACPI_READ_FIELD(fadt_table->pm1a_cnt_blk, addr); | |
298 | ACPI_READ_FIELD(fadt_table->pm1b_cnt_blk, addr); | |
299 | ACPI_READ_FIELD(fadt_table->pm2_cnt_blk, addr); | |
300 | ACPI_READ_FIELD(fadt_table->pm_tmr_blk, addr); | |
301 | ACPI_READ_FIELD(fadt_table->gpe0_blk, addr); | |
302 | ACPI_READ_FIELD(fadt_table->gpe1_blk, addr); | |
303 | ACPI_READ_FIELD(fadt_table->pm1_evt_len, addr); | |
304 | ACPI_READ_FIELD(fadt_table->pm1_cnt_len, addr); | |
305 | ACPI_READ_FIELD(fadt_table->pm2_cnt_len, addr); | |
306 | ACPI_READ_FIELD(fadt_table->pm_tmr_len, addr); | |
307 | ACPI_READ_FIELD(fadt_table->gpe0_blk_len, addr); | |
308 | ACPI_READ_FIELD(fadt_table->gpe1_blk_len, addr); | |
309 | ACPI_READ_FIELD(fadt_table->gpe1_base, addr); | |
310 | ACPI_READ_FIELD(fadt_table->reserved3, addr); | |
311 | ACPI_READ_FIELD(fadt_table->plvl2_lat, addr); | |
312 | ACPI_READ_FIELD(fadt_table->plvl3_lat, addr); | |
313 | ACPI_READ_FIELD(fadt_table->flush_size, addr); | |
314 | ACPI_READ_FIELD(fadt_table->flush_stride, addr); | |
315 | ACPI_READ_FIELD(fadt_table->duty_offset, addr); | |
316 | ACPI_READ_FIELD(fadt_table->duty_width, addr); | |
317 | ACPI_READ_FIELD(fadt_table->day_alrm, addr); | |
318 | ACPI_READ_FIELD(fadt_table->mon_alrm, addr); | |
319 | ACPI_READ_FIELD(fadt_table->century, addr); | |
320 | ACPI_READ_FIELD(fadt_table->reserved4, addr); | |
321 | ACPI_READ_FIELD(fadt_table->reserved4a, addr); | |
322 | ACPI_READ_FIELD(fadt_table->reserved4b, addr); | |
323 | ACPI_READ_FIELD(fadt_table->flags, addr); | |
324 | ||
c225aa3c | 325 | ACPI_ASSERT_CMP(fadt_table->signature, "FACP"); |
53333801 MA |
326 | g_assert(!acpi_checksum((uint8_t *)fadt_table, fadt_table->length)); |
327 | } | |
328 | ||
15650602 MA |
329 | static void test_acpi_facs_table(test_data *data) |
330 | { | |
331 | AcpiFacsDescriptorRev1 *facs_table = &data->facs_table; | |
332 | uint32_t addr = data->fadt_table.firmware_ctrl; | |
333 | ||
334 | ACPI_READ_FIELD(facs_table->signature, addr); | |
335 | ACPI_READ_FIELD(facs_table->length, addr); | |
336 | ACPI_READ_FIELD(facs_table->hardware_signature, addr); | |
337 | ACPI_READ_FIELD(facs_table->firmware_waking_vector, addr); | |
338 | ACPI_READ_FIELD(facs_table->global_lock, addr); | |
339 | ACPI_READ_FIELD(facs_table->flags, addr); | |
340 | ACPI_READ_ARRAY(facs_table->resverved3, addr); | |
341 | ||
c225aa3c | 342 | ACPI_ASSERT_CMP(facs_table->signature, "FACS"); |
15650602 MA |
343 | } |
344 | ||
53333801 MA |
345 | static void test_dst_table(AcpiSdtTable *sdt_table, uint32_t addr) |
346 | { | |
347 | uint8_t checksum; | |
348 | ||
349 | ACPI_READ_TABLE_HEADER(&sdt_table->header, addr); | |
350 | ||
351 | sdt_table->aml_len = sdt_table->header.length - sizeof(AcpiTableHeader); | |
352 | sdt_table->aml = g_malloc0(sdt_table->aml_len); | |
353 | ACPI_READ_ARRAY_PTR(sdt_table->aml, sdt_table->aml_len, addr); | |
354 | ||
355 | checksum = acpi_checksum((uint8_t *)sdt_table, sizeof(AcpiTableHeader)) + | |
9e8458c0 | 356 | acpi_checksum((uint8_t *)sdt_table->aml, sdt_table->aml_len); |
53333801 MA |
357 | g_assert(!checksum); |
358 | } | |
359 | ||
360 | static void test_acpi_dsdt_table(test_data *data) | |
361 | { | |
9e8458c0 | 362 | AcpiSdtTable dsdt_table; |
53333801 MA |
363 | uint32_t addr = data->fadt_table.dsdt; |
364 | ||
9e8458c0 | 365 | memset(&dsdt_table, 0, sizeof(dsdt_table)); |
a3a74ab9 | 366 | data->tables = g_array_new(false, true, sizeof(AcpiSdtTable)); |
9e8458c0 MA |
367 | |
368 | test_dst_table(&dsdt_table, addr); | |
c225aa3c | 369 | ACPI_ASSERT_CMP(dsdt_table.header.signature, "DSDT"); |
9e8458c0 MA |
370 | |
371 | /* Place DSDT first */ | |
a3a74ab9 | 372 | g_array_append_val(data->tables, dsdt_table); |
53333801 MA |
373 | } |
374 | ||
a3a74ab9 | 375 | static void test_acpi_tables(test_data *data) |
53333801 | 376 | { |
a3a74ab9 | 377 | int tables_nr = data->rsdt_tables_nr - 1; /* fadt is first */ |
53333801 MA |
378 | int i; |
379 | ||
a3a74ab9 | 380 | for (i = 0; i < tables_nr; i++) { |
8ac2adf7 | 381 | AcpiSdtTable ssdt_table; |
9e8458c0 MA |
382 | |
383 | memset(&ssdt_table, 0 , sizeof(ssdt_table)); | |
53333801 | 384 | uint32_t addr = data->rsdt_tables_addr[i + 1]; /* fadt is first */ |
8ac2adf7 | 385 | test_dst_table(&ssdt_table, addr); |
a3a74ab9 | 386 | g_array_append_val(data->tables, ssdt_table); |
53333801 | 387 | } |
9e8458c0 MA |
388 | } |
389 | ||
4500bc98 | 390 | static void dump_aml_files(test_data *data, bool rebuild) |
9e8458c0 MA |
391 | { |
392 | AcpiSdtTable *sdt; | |
393 | GError *error = NULL; | |
4500bc98 | 394 | gchar *aml_file = NULL; |
9e8458c0 MA |
395 | gint fd; |
396 | ssize_t ret; | |
397 | int i; | |
398 | ||
a3a74ab9 | 399 | for (i = 0; i < data->tables->len; ++i) { |
3a9c86df | 400 | const char *ext = data->variant ? data->variant : ""; |
a3a74ab9 | 401 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
9e8458c0 MA |
402 | g_assert(sdt->aml); |
403 | ||
4500bc98 | 404 | if (rebuild) { |
c225aa3c | 405 | uint32_t signature = cpu_to_le32(sdt->header.signature); |
3a9c86df IM |
406 | aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine, |
407 | (gchar *)&signature, ext); | |
4500bc98 MA |
408 | fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT, |
409 | S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH); | |
410 | } else { | |
411 | fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error); | |
412 | g_assert_no_error(error); | |
413 | } | |
414 | g_assert(fd >= 0); | |
9e8458c0 MA |
415 | |
416 | ret = qemu_write_full(fd, sdt, sizeof(AcpiTableHeader)); | |
417 | g_assert(ret == sizeof(AcpiTableHeader)); | |
418 | ret = qemu_write_full(fd, sdt->aml, sdt->aml_len); | |
419 | g_assert(ret == sdt->aml_len); | |
420 | ||
421 | close(fd); | |
4500bc98 MA |
422 | |
423 | if (aml_file) { | |
424 | g_free(aml_file); | |
425 | } | |
9e8458c0 MA |
426 | } |
427 | } | |
428 | ||
c225aa3c | 429 | static bool compare_signature(AcpiSdtTable *sdt, const char *signature) |
69d09245 | 430 | { |
c225aa3c | 431 | return !memcmp(&sdt->header.signature, signature, 4); |
69d09245 MA |
432 | } |
433 | ||
15d914b1 | 434 | static bool load_asl(GArray *sdts, AcpiSdtTable *sdt) |
9e8458c0 MA |
435 | { |
436 | AcpiSdtTable *temp; | |
437 | GError *error = NULL; | |
cc8fa0e8 | 438 | GString *command_line = g_string_new(iasl); |
9e8458c0 MA |
439 | gint fd; |
440 | gchar *out, *out_err; | |
441 | gboolean ret; | |
442 | int i; | |
443 | ||
444 | fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error); | |
445 | g_assert_no_error(error); | |
446 | close(fd); | |
447 | ||
448 | /* build command line */ | |
cc8fa0e8 | 449 | g_string_append_printf(command_line, " -p %s ", sdt->asl_file); |
c225aa3c MT |
450 | if (compare_signature(sdt, "DSDT") || |
451 | compare_signature(sdt, "SSDT")) { | |
69d09245 MA |
452 | for (i = 0; i < sdts->len; ++i) { |
453 | temp = &g_array_index(sdts, AcpiSdtTable, i); | |
c225aa3c MT |
454 | if (compare_signature(temp, "DSDT") || |
455 | compare_signature(temp, "SSDT")) { | |
69d09245 MA |
456 | g_string_append_printf(command_line, "-e %s ", temp->aml_file); |
457 | } | |
458 | } | |
9e8458c0 MA |
459 | } |
460 | g_string_append_printf(command_line, "-d %s", sdt->aml_file); | |
461 | ||
462 | /* pass 'out' and 'out_err' in order to be redirected */ | |
15d914b1 | 463 | ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error); |
9e8458c0 | 464 | g_assert_no_error(error); |
15d914b1 MA |
465 | if (ret) { |
466 | ret = g_file_get_contents(sdt->asl_file, (gchar **)&sdt->asl, | |
467 | &sdt->asl_len, &error); | |
468 | g_assert(ret); | |
469 | g_assert_no_error(error); | |
dac23a6c | 470 | ret = (sdt->asl_len > 0); |
15d914b1 | 471 | } |
9e8458c0 MA |
472 | |
473 | g_free(out); | |
474 | g_free(out_err); | |
475 | g_string_free(command_line, true); | |
15d914b1 MA |
476 | |
477 | return !ret; | |
9e8458c0 MA |
478 | } |
479 | ||
480 | #define COMMENT_END "*/" | |
481 | #define DEF_BLOCK "DefinitionBlock (" | |
482 | #define BLOCK_NAME_END ".aml" | |
483 | ||
484 | static GString *normalize_asl(gchar *asl_code) | |
485 | { | |
486 | GString *asl = g_string_new(asl_code); | |
487 | gchar *comment, *block_name; | |
488 | ||
489 | /* strip comments (different generation days) */ | |
490 | comment = g_strstr_len(asl->str, asl->len, COMMENT_END); | |
491 | if (comment) { | |
cb348985 PB |
492 | comment += strlen(COMMENT_END); |
493 | while (*comment == '\n') { | |
494 | comment++; | |
495 | } | |
496 | asl = g_string_erase(asl, 0, comment - asl->str); | |
9e8458c0 MA |
497 | } |
498 | ||
499 | /* strip def block name (it has file path in it) */ | |
500 | if (g_str_has_prefix(asl->str, DEF_BLOCK)) { | |
501 | block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END); | |
502 | g_assert(block_name); | |
503 | asl = g_string_erase(asl, 0, | |
504 | block_name + sizeof(BLOCK_NAME_END) - asl->str); | |
505 | } | |
506 | ||
507 | return asl; | |
508 | } | |
509 | ||
510 | static GArray *load_expected_aml(test_data *data) | |
511 | { | |
512 | int i; | |
513 | AcpiSdtTable *sdt; | |
3a9c86df | 514 | gchar *aml_file = NULL; |
9e8458c0 MA |
515 | GError *error = NULL; |
516 | gboolean ret; | |
517 | ||
a3a74ab9 MA |
518 | GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable)); |
519 | for (i = 0; i < data->tables->len; ++i) { | |
9e8458c0 | 520 | AcpiSdtTable exp_sdt; |
c225aa3c | 521 | uint32_t signature; |
3a9c86df | 522 | const char *ext = data->variant ? data->variant : ""; |
c225aa3c | 523 | |
a3a74ab9 | 524 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
9e8458c0 MA |
525 | |
526 | memset(&exp_sdt, 0, sizeof(exp_sdt)); | |
527 | exp_sdt.header.signature = sdt->header.signature; | |
528 | ||
c225aa3c | 529 | signature = cpu_to_le32(sdt->header.signature); |
3a9c86df IM |
530 | |
531 | try_again: | |
532 | aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine, | |
533 | (gchar *)&signature, ext); | |
534 | if (data->variant && !g_file_test(aml_file, G_FILE_TEST_EXISTS)) { | |
535 | g_free(aml_file); | |
536 | ext = ""; | |
537 | goto try_again; | |
538 | } | |
9e8458c0 MA |
539 | exp_sdt.aml_file = aml_file; |
540 | g_assert(g_file_test(aml_file, G_FILE_TEST_EXISTS)); | |
541 | ret = g_file_get_contents(aml_file, &exp_sdt.aml, | |
542 | &exp_sdt.aml_len, &error); | |
543 | g_assert(ret); | |
544 | g_assert_no_error(error); | |
545 | g_assert(exp_sdt.aml); | |
546 | g_assert(exp_sdt.aml_len); | |
547 | ||
a3a74ab9 | 548 | g_array_append_val(exp_tables, exp_sdt); |
9e8458c0 MA |
549 | } |
550 | ||
a3a74ab9 | 551 | return exp_tables; |
9e8458c0 MA |
552 | } |
553 | ||
554 | static void test_acpi_asl(test_data *data) | |
555 | { | |
556 | int i; | |
557 | AcpiSdtTable *sdt, *exp_sdt; | |
558 | test_data exp_data; | |
15d914b1 | 559 | gboolean exp_err, err; |
9e8458c0 MA |
560 | |
561 | memset(&exp_data, 0, sizeof(exp_data)); | |
a3a74ab9 | 562 | exp_data.tables = load_expected_aml(data); |
4500bc98 | 563 | dump_aml_files(data, false); |
a3a74ab9 | 564 | for (i = 0; i < data->tables->len; ++i) { |
9e8458c0 MA |
565 | GString *asl, *exp_asl; |
566 | ||
a3a74ab9 MA |
567 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
568 | exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i); | |
9e8458c0 | 569 | |
15d914b1 | 570 | err = load_asl(data->tables, sdt); |
9e8458c0 MA |
571 | asl = normalize_asl(sdt->asl); |
572 | ||
15d914b1 | 573 | exp_err = load_asl(exp_data.tables, exp_sdt); |
9e8458c0 MA |
574 | exp_asl = normalize_asl(exp_sdt->asl); |
575 | ||
15d914b1 MA |
576 | /* TODO: check for warnings */ |
577 | g_assert(!err || exp_err); | |
578 | ||
0651596c | 579 | if (g_strcmp0(asl->str, exp_asl->str)) { |
dac23a6c MA |
580 | if (exp_err) { |
581 | fprintf(stderr, | |
582 | "Warning! iasl couldn't parse the expected aml\n"); | |
583 | } else { | |
584 | uint32_t signature = cpu_to_le32(exp_sdt->header.signature); | |
585 | sdt->tmp_files_retain = true; | |
586 | exp_sdt->tmp_files_retain = true; | |
587 | fprintf(stderr, | |
588 | "acpi-test: Warning! %.4s mismatch. " | |
589 | "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n", | |
590 | (gchar *)&signature, | |
591 | sdt->asl_file, sdt->aml_file, | |
592 | exp_sdt->asl_file, exp_sdt->aml_file); | |
593 | } | |
0651596c | 594 | } |
9e8458c0 MA |
595 | g_string_free(asl, true); |
596 | g_string_free(exp_asl, true); | |
597 | } | |
598 | ||
599 | free_test_data(&exp_data); | |
53333801 MA |
600 | } |
601 | ||
eb386aac GS |
602 | static void test_smbios_ep_address(test_data *data) |
603 | { | |
604 | uint32_t off; | |
605 | ||
606 | /* find smbios entry point structure */ | |
607 | for (off = 0xf0000; off < 0x100000; off += 0x10) { | |
608 | uint8_t sig[] = "_SM_"; | |
609 | int i; | |
610 | ||
611 | for (i = 0; i < sizeof sig - 1; ++i) { | |
612 | sig[i] = readb(off + i); | |
613 | } | |
614 | ||
615 | if (!memcmp(sig, "_SM_", sizeof sig)) { | |
616 | break; | |
617 | } | |
618 | } | |
619 | ||
620 | g_assert_cmphex(off, <, 0x100000); | |
621 | data->smbios_ep_addr = off; | |
622 | } | |
623 | ||
624 | static void test_smbios_ep_table(test_data *data) | |
625 | { | |
626 | struct smbios_entry_point *ep_table = &data->smbios_ep_table; | |
627 | uint32_t addr = data->smbios_ep_addr; | |
628 | ||
629 | ACPI_READ_ARRAY(ep_table->anchor_string, addr); | |
630 | g_assert(!memcmp(ep_table->anchor_string, "_SM_", 4)); | |
631 | ACPI_READ_FIELD(ep_table->checksum, addr); | |
632 | ACPI_READ_FIELD(ep_table->length, addr); | |
633 | ACPI_READ_FIELD(ep_table->smbios_major_version, addr); | |
634 | ACPI_READ_FIELD(ep_table->smbios_minor_version, addr); | |
635 | ACPI_READ_FIELD(ep_table->max_structure_size, addr); | |
636 | ACPI_READ_FIELD(ep_table->entry_point_revision, addr); | |
637 | ACPI_READ_ARRAY(ep_table->formatted_area, addr); | |
638 | ACPI_READ_ARRAY(ep_table->intermediate_anchor_string, addr); | |
639 | g_assert(!memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)); | |
640 | ACPI_READ_FIELD(ep_table->intermediate_checksum, addr); | |
641 | ACPI_READ_FIELD(ep_table->structure_table_length, addr); | |
642 | g_assert_cmpuint(ep_table->structure_table_length, >, 0); | |
643 | ACPI_READ_FIELD(ep_table->structure_table_address, addr); | |
644 | ACPI_READ_FIELD(ep_table->number_of_structures, addr); | |
645 | g_assert_cmpuint(ep_table->number_of_structures, >, 0); | |
646 | ACPI_READ_FIELD(ep_table->smbios_bcd_revision, addr); | |
647 | g_assert(!acpi_checksum((uint8_t *)ep_table, sizeof *ep_table)); | |
648 | g_assert(!acpi_checksum((uint8_t *)ep_table + 0x10, | |
649 | sizeof *ep_table - 0x10)); | |
650 | } | |
651 | ||
652 | static inline bool smbios_single_instance(uint8_t type) | |
653 | { | |
654 | switch (type) { | |
655 | case 0: | |
656 | case 1: | |
657 | case 2: | |
658 | case 3: | |
659 | case 16: | |
660 | case 32: | |
661 | case 127: | |
662 | return true; | |
663 | default: | |
664 | return false; | |
665 | } | |
666 | } | |
667 | ||
668 | static void test_smbios_structs(test_data *data) | |
669 | { | |
670 | DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 }; | |
671 | struct smbios_entry_point *ep_table = &data->smbios_ep_table; | |
672 | uint32_t addr = ep_table->structure_table_address; | |
673 | int i, len, max_len = 0; | |
674 | uint8_t type, prv, crt; | |
675 | uint8_t required_struct_types[] = {0, 1, 3, 4, 16, 17, 19, 32, 127}; | |
676 | ||
677 | /* walk the smbios tables */ | |
678 | for (i = 0; i < ep_table->number_of_structures; i++) { | |
679 | ||
680 | /* grab type and formatted area length from struct header */ | |
681 | type = readb(addr); | |
682 | g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE); | |
683 | len = readb(addr + 1); | |
684 | ||
685 | /* single-instance structs must not have been encountered before */ | |
686 | if (smbios_single_instance(type)) { | |
687 | g_assert(!test_bit(type, struct_bitmap)); | |
688 | } | |
689 | set_bit(type, struct_bitmap); | |
690 | ||
691 | /* seek to end of unformatted string area of this struct ("\0\0") */ | |
692 | prv = crt = 1; | |
693 | while (prv || crt) { | |
694 | prv = crt; | |
695 | crt = readb(addr + len); | |
696 | len++; | |
697 | } | |
698 | ||
699 | /* keep track of max. struct size */ | |
700 | if (max_len < len) { | |
701 | max_len = len; | |
702 | g_assert_cmpuint(max_len, <=, ep_table->max_structure_size); | |
703 | } | |
704 | ||
705 | /* start of next structure */ | |
706 | addr += len; | |
707 | } | |
708 | ||
709 | /* total table length and max struct size must match entry point values */ | |
710 | g_assert_cmpuint(ep_table->structure_table_length, ==, | |
711 | addr - ep_table->structure_table_address); | |
712 | g_assert_cmpuint(ep_table->max_structure_size, ==, max_len); | |
713 | ||
714 | /* required struct types must all be present */ | |
715 | for (i = 0; i < ARRAY_SIZE(required_struct_types); i++) { | |
716 | g_assert(test_bit(required_struct_types[i], struct_bitmap)); | |
717 | } | |
718 | } | |
719 | ||
8ac2adf7 | 720 | static void test_acpi_one(const char *params, test_data *data) |
ad6423a7 MT |
721 | { |
722 | char *args; | |
723 | uint8_t signature_low; | |
724 | uint8_t signature_high; | |
725 | uint16_t signature; | |
726 | int i; | |
ad6423a7 | 727 | |
6b9e03a4 | 728 | args = g_strdup_printf("-net none -display none %s " |
b8e665e4 | 729 | "-drive id=hd0,if=none,file=%s,format=raw " |
6b9e03a4 JS |
730 | "-device ide-hd,drive=hd0 ", |
731 | params ? params : "", disk); | |
9e8458c0 | 732 | |
ad6423a7 MT |
733 | qtest_start(args); |
734 | ||
735 | /* Wait at most 1 minute */ | |
736 | #define TEST_DELAY (1 * G_USEC_PER_SEC / 10) | |
737 | #define TEST_CYCLES MAX((60 * G_USEC_PER_SEC / TEST_DELAY), 1) | |
738 | ||
739 | /* Poll until code has run and modified memory. Once it has we know BIOS | |
740 | * initialization is done. TODO: check that IP reached the halt | |
741 | * instruction. | |
742 | */ | |
743 | for (i = 0; i < TEST_CYCLES; ++i) { | |
744 | signature_low = readb(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET); | |
745 | signature_high = readb(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET + 1); | |
746 | signature = (signature_high << 8) | signature_low; | |
747 | if (signature == SIGNATURE) { | |
748 | break; | |
749 | } | |
750 | g_usleep(TEST_DELAY); | |
751 | } | |
752 | g_assert_cmphex(signature, ==, SIGNATURE); | |
753 | ||
8ac2adf7 MA |
754 | test_acpi_rsdp_address(data); |
755 | test_acpi_rsdp_table(data); | |
756 | test_acpi_rsdt_table(data); | |
757 | test_acpi_fadt_table(data); | |
15650602 | 758 | test_acpi_facs_table(data); |
8ac2adf7 | 759 | test_acpi_dsdt_table(data); |
a3a74ab9 | 760 | test_acpi_tables(data); |
ad6423a7 | 761 | |
cc8fa0e8 | 762 | if (iasl) { |
4500bc98 MA |
763 | if (getenv(ACPI_REBUILD_EXPECTED_AML)) { |
764 | dump_aml_files(data, true); | |
765 | } else { | |
766 | test_acpi_asl(data); | |
767 | } | |
9e8458c0 MA |
768 | } |
769 | ||
eb386aac GS |
770 | test_smbios_ep_address(data); |
771 | test_smbios_ep_table(data); | |
772 | test_smbios_structs(data); | |
773 | ||
ad6423a7 MT |
774 | qtest_quit(global_qtest); |
775 | g_free(args); | |
776 | } | |
777 | ||
71f4be25 | 778 | static void test_acpi_piix4_tcg(void) |
ad6423a7 | 779 | { |
8ac2adf7 MA |
780 | test_data data; |
781 | ||
ad6423a7 MT |
782 | /* Supplying -machine accel argument overrides the default (qtest). |
783 | * This is to make guest actually run. | |
784 | */ | |
9e8458c0 MA |
785 | memset(&data, 0, sizeof(data)); |
786 | data.machine = MACHINE_PC; | |
8ac2adf7 | 787 | test_acpi_one("-machine accel=tcg", &data); |
9e8458c0 | 788 | free_test_data(&data); |
71f4be25 PB |
789 | } |
790 | ||
3a9c86df IM |
791 | static void test_acpi_piix4_tcg_bridge(void) |
792 | { | |
793 | test_data data; | |
794 | ||
795 | memset(&data, 0, sizeof(data)); | |
796 | data.machine = MACHINE_PC; | |
797 | data.variant = ".bridge"; | |
798 | test_acpi_one("-machine accel=tcg -device pci-bridge,chassis_nr=1", &data); | |
799 | free_test_data(&data); | |
800 | } | |
801 | ||
71f4be25 PB |
802 | static void test_acpi_q35_tcg(void) |
803 | { | |
804 | test_data data; | |
8ac2adf7 | 805 | |
9e8458c0 MA |
806 | memset(&data, 0, sizeof(data)); |
807 | data.machine = MACHINE_Q35; | |
808 | test_acpi_one("-machine q35,accel=tcg", &data); | |
8ac2adf7 | 809 | free_test_data(&data); |
ad6423a7 MT |
810 | } |
811 | ||
3a9c86df IM |
812 | static void test_acpi_q35_tcg_bridge(void) |
813 | { | |
814 | test_data data; | |
815 | ||
816 | memset(&data, 0, sizeof(data)); | |
817 | data.machine = MACHINE_Q35; | |
818 | data.variant = ".bridge"; | |
819 | test_acpi_one("-machine q35,accel=tcg -device pci-bridge,chassis_nr=1", | |
820 | &data); | |
821 | free_test_data(&data); | |
822 | } | |
823 | ||
ad6423a7 MT |
824 | int main(int argc, char *argv[]) |
825 | { | |
826 | const char *arch = qtest_get_arch(); | |
827 | FILE *f = fopen(disk, "w"); | |
5862ad0f | 828 | int ret; |
c39a28a4 HZ |
829 | |
830 | if (!f) { | |
831 | fprintf(stderr, "Couldn't open \"%s\": %s", disk, strerror(errno)); | |
832 | return 1; | |
833 | } | |
ad6423a7 MT |
834 | fwrite(boot_sector, 1, sizeof boot_sector, f); |
835 | fclose(f); | |
836 | ||
837 | g_test_init(&argc, &argv, NULL); | |
838 | ||
839 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { | |
71f4be25 | 840 | qtest_add_func("acpi/piix4/tcg", test_acpi_piix4_tcg); |
3a9c86df | 841 | qtest_add_func("acpi/piix4/tcg/bridge", test_acpi_piix4_tcg_bridge); |
71f4be25 | 842 | qtest_add_func("acpi/q35/tcg", test_acpi_q35_tcg); |
3a9c86df | 843 | qtest_add_func("acpi/q35/tcg/bridge", test_acpi_q35_tcg_bridge); |
ad6423a7 | 844 | } |
5862ad0f FZ |
845 | ret = g_test_run(); |
846 | unlink(disk); | |
847 | return ret; | |
ad6423a7 | 848 | } |