]>
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 | ||
681c28a3 | 13 | #include "qemu/osdep.h" |
4500bc98 | 14 | #include <glib/gstdio.h> |
53333801 | 15 | #include "qemu-common.h" |
60d8f328 | 16 | #include "hw/smbios/smbios.h" |
eb386aac | 17 | #include "qemu/bitmap.h" |
3248f1b4 | 18 | #include "acpi-utils.h" |
4e082566 | 19 | #include "boot-sector.h" |
ad6423a7 | 20 | |
9e8458c0 MA |
21 | #define MACHINE_PC "pc" |
22 | #define MACHINE_Q35 "q35" | |
23 | ||
4500bc98 MA |
24 | #define ACPI_REBUILD_EXPECTED_AML "TEST_ACPI_REBUILD_AML" |
25 | ||
53333801 | 26 | typedef struct { |
9e8458c0 | 27 | const char *machine; |
3a9c86df | 28 | const char *variant; |
53333801 MA |
29 | uint32_t rsdp_addr; |
30 | AcpiRsdpDescriptor rsdp_table; | |
31 | AcpiRsdtDescriptorRev1 rsdt_table; | |
77af8a2b | 32 | AcpiFadtDescriptorRev3 fadt_table; |
15650602 | 33 | AcpiFacsDescriptorRev1 facs_table; |
53333801 MA |
34 | uint32_t *rsdt_tables_addr; |
35 | int rsdt_tables_nr; | |
a3a74ab9 | 36 | GArray *tables; |
eb386aac | 37 | uint32_t smbios_ep_addr; |
86299120 | 38 | struct smbios_21_entry_point smbios_ep_table; |
f4eda2d4 CM |
39 | uint8_t *required_struct_types; |
40 | int required_struct_types_len; | |
53333801 | 41 | } test_data; |
ad6423a7 | 42 | |
3e353773 | 43 | static char disk[] = "tests/acpi-test-disk-XXXXXX"; |
9e8458c0 | 44 | static const char *data_dir = "tests/acpi-test-data"; |
cc8fa0e8 MA |
45 | #ifdef CONFIG_IASL |
46 | static const char *iasl = stringify(CONFIG_IASL); | |
47 | #else | |
48 | static const char *iasl; | |
49 | #endif | |
ad6423a7 | 50 | |
8ac2adf7 MA |
51 | static void free_test_data(test_data *data) |
52 | { | |
9e8458c0 | 53 | AcpiSdtTable *temp; |
8ac2adf7 MA |
54 | int i; |
55 | ||
ef1e1e07 | 56 | g_free(data->rsdt_tables_addr); |
9e8458c0 | 57 | |
a3a74ab9 MA |
58 | for (i = 0; i < data->tables->len; ++i) { |
59 | temp = &g_array_index(data->tables, AcpiSdtTable, i); | |
ef1e1e07 DB |
60 | g_free(temp->aml); |
61 | if (temp->aml_file && | |
62 | !temp->tmp_files_retain && | |
63 | g_strstr_len(temp->aml_file, -1, "aml-")) { | |
64 | unlink(temp->aml_file); | |
9e8458c0 | 65 | } |
ef1e1e07 DB |
66 | g_free(temp->aml_file); |
67 | g_free(temp->asl); | |
68 | if (temp->asl_file && | |
69 | !temp->tmp_files_retain) { | |
70 | unlink(temp->asl_file); | |
9e8458c0 | 71 | } |
ef1e1e07 | 72 | g_free(temp->asl_file); |
8ac2adf7 | 73 | } |
9e8458c0 | 74 | |
f11dc27b | 75 | g_array_free(data->tables, true); |
8ac2adf7 MA |
76 | } |
77 | ||
53333801 MA |
78 | static void test_acpi_rsdp_address(test_data *data) |
79 | { | |
3248f1b4 | 80 | uint32_t off = acpi_find_rsdp_address(); |
53333801 MA |
81 | g_assert_cmphex(off, <, 0x100000); |
82 | data->rsdp_addr = off; | |
83 | } | |
84 | ||
85 | static void test_acpi_rsdp_table(test_data *data) | |
86 | { | |
87 | AcpiRsdpDescriptor *rsdp_table = &data->rsdp_table; | |
88 | uint32_t addr = data->rsdp_addr; | |
89 | ||
3248f1b4 | 90 | acpi_parse_rsdp_table(addr, rsdp_table); |
53333801 MA |
91 | |
92 | /* rsdp checksum is not for the whole table, but for the first 20 bytes */ | |
3248f1b4 | 93 | g_assert(!acpi_calc_checksum((uint8_t *)rsdp_table, 20)); |
53333801 MA |
94 | } |
95 | ||
96 | static void test_acpi_rsdt_table(test_data *data) | |
97 | { | |
98 | AcpiRsdtDescriptorRev1 *rsdt_table = &data->rsdt_table; | |
3831c07b | 99 | uint32_t addr = le32_to_cpu(data->rsdp_table.rsdt_physical_address); |
53333801 MA |
100 | uint32_t *tables; |
101 | int tables_nr; | |
102 | uint8_t checksum; | |
3831c07b | 103 | uint32_t rsdt_table_length; |
53333801 MA |
104 | |
105 | /* read the header */ | |
106 | ACPI_READ_TABLE_HEADER(rsdt_table, addr); | |
c225aa3c | 107 | ACPI_ASSERT_CMP(rsdt_table->signature, "RSDT"); |
53333801 | 108 | |
3831c07b TH |
109 | rsdt_table_length = le32_to_cpu(rsdt_table->length); |
110 | ||
53333801 | 111 | /* compute the table entries in rsdt */ |
3831c07b | 112 | tables_nr = (rsdt_table_length - sizeof(AcpiRsdtDescriptorRev1)) / |
53333801 | 113 | sizeof(uint32_t); |
91c38e08 | 114 | g_assert(tables_nr > 0); |
53333801 MA |
115 | |
116 | /* get the addresses of the tables pointed by rsdt */ | |
117 | tables = g_new0(uint32_t, tables_nr); | |
118 | ACPI_READ_ARRAY_PTR(tables, tables_nr, addr); | |
119 | ||
3831c07b | 120 | checksum = acpi_calc_checksum((uint8_t *)rsdt_table, rsdt_table_length) + |
3248f1b4 BW |
121 | acpi_calc_checksum((uint8_t *)tables, |
122 | tables_nr * sizeof(uint32_t)); | |
53333801 MA |
123 | g_assert(!checksum); |
124 | ||
125 | /* SSDT tables after FADT */ | |
126 | data->rsdt_tables_addr = tables; | |
127 | data->rsdt_tables_nr = tables_nr; | |
128 | } | |
129 | ||
130 | static void test_acpi_fadt_table(test_data *data) | |
131 | { | |
77af8a2b | 132 | AcpiFadtDescriptorRev3 *fadt_table = &data->fadt_table; |
53333801 MA |
133 | uint32_t addr; |
134 | ||
135 | /* FADT table comes first */ | |
3831c07b | 136 | addr = le32_to_cpu(data->rsdt_tables_addr[0]); |
53333801 MA |
137 | ACPI_READ_TABLE_HEADER(fadt_table, addr); |
138 | ||
139 | ACPI_READ_FIELD(fadt_table->firmware_ctrl, addr); | |
140 | ACPI_READ_FIELD(fadt_table->dsdt, addr); | |
141 | ACPI_READ_FIELD(fadt_table->model, addr); | |
142 | ACPI_READ_FIELD(fadt_table->reserved1, addr); | |
143 | ACPI_READ_FIELD(fadt_table->sci_int, addr); | |
144 | ACPI_READ_FIELD(fadt_table->smi_cmd, addr); | |
145 | ACPI_READ_FIELD(fadt_table->acpi_enable, addr); | |
146 | ACPI_READ_FIELD(fadt_table->acpi_disable, addr); | |
147 | ACPI_READ_FIELD(fadt_table->S4bios_req, addr); | |
148 | ACPI_READ_FIELD(fadt_table->reserved2, addr); | |
149 | ACPI_READ_FIELD(fadt_table->pm1a_evt_blk, addr); | |
150 | ACPI_READ_FIELD(fadt_table->pm1b_evt_blk, addr); | |
151 | ACPI_READ_FIELD(fadt_table->pm1a_cnt_blk, addr); | |
152 | ACPI_READ_FIELD(fadt_table->pm1b_cnt_blk, addr); | |
153 | ACPI_READ_FIELD(fadt_table->pm2_cnt_blk, addr); | |
154 | ACPI_READ_FIELD(fadt_table->pm_tmr_blk, addr); | |
155 | ACPI_READ_FIELD(fadt_table->gpe0_blk, addr); | |
156 | ACPI_READ_FIELD(fadt_table->gpe1_blk, addr); | |
157 | ACPI_READ_FIELD(fadt_table->pm1_evt_len, addr); | |
158 | ACPI_READ_FIELD(fadt_table->pm1_cnt_len, addr); | |
159 | ACPI_READ_FIELD(fadt_table->pm2_cnt_len, addr); | |
160 | ACPI_READ_FIELD(fadt_table->pm_tmr_len, addr); | |
161 | ACPI_READ_FIELD(fadt_table->gpe0_blk_len, addr); | |
162 | ACPI_READ_FIELD(fadt_table->gpe1_blk_len, addr); | |
163 | ACPI_READ_FIELD(fadt_table->gpe1_base, addr); | |
164 | ACPI_READ_FIELD(fadt_table->reserved3, addr); | |
165 | ACPI_READ_FIELD(fadt_table->plvl2_lat, addr); | |
166 | ACPI_READ_FIELD(fadt_table->plvl3_lat, addr); | |
167 | ACPI_READ_FIELD(fadt_table->flush_size, addr); | |
168 | ACPI_READ_FIELD(fadt_table->flush_stride, addr); | |
169 | ACPI_READ_FIELD(fadt_table->duty_offset, addr); | |
170 | ACPI_READ_FIELD(fadt_table->duty_width, addr); | |
171 | ACPI_READ_FIELD(fadt_table->day_alrm, addr); | |
172 | ACPI_READ_FIELD(fadt_table->mon_alrm, addr); | |
173 | ACPI_READ_FIELD(fadt_table->century, addr); | |
77af8a2b PDJ |
174 | ACPI_READ_FIELD(fadt_table->boot_flags, addr); |
175 | ACPI_READ_FIELD(fadt_table->reserved, addr); | |
53333801 | 176 | ACPI_READ_FIELD(fadt_table->flags, addr); |
77af8a2b PDJ |
177 | ACPI_READ_GENERIC_ADDRESS(fadt_table->reset_register, addr); |
178 | ACPI_READ_FIELD(fadt_table->reset_value, addr); | |
179 | ACPI_READ_FIELD(fadt_table->arm_boot_flags, addr); | |
180 | ACPI_READ_FIELD(fadt_table->minor_revision, addr); | |
5ee85347 AB |
181 | ACPI_READ_FIELD(fadt_table->x_facs, addr); |
182 | ACPI_READ_FIELD(fadt_table->x_dsdt, addr); | |
77af8a2b PDJ |
183 | ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1a_event_block, addr); |
184 | ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1b_event_block, addr); | |
185 | ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1a_control_block, addr); | |
186 | ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm1b_control_block, addr); | |
187 | ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm2_control_block, addr); | |
188 | ACPI_READ_GENERIC_ADDRESS(fadt_table->xpm_timer_block, addr); | |
189 | ACPI_READ_GENERIC_ADDRESS(fadt_table->xgpe0_block, addr); | |
190 | ACPI_READ_GENERIC_ADDRESS(fadt_table->xgpe1_block, addr); | |
53333801 | 191 | |
c225aa3c | 192 | ACPI_ASSERT_CMP(fadt_table->signature, "FACP"); |
3831c07b TH |
193 | g_assert(!acpi_calc_checksum((uint8_t *)fadt_table, |
194 | le32_to_cpu(fadt_table->length))); | |
53333801 MA |
195 | } |
196 | ||
15650602 MA |
197 | static void test_acpi_facs_table(test_data *data) |
198 | { | |
199 | AcpiFacsDescriptorRev1 *facs_table = &data->facs_table; | |
3831c07b | 200 | uint32_t addr = le32_to_cpu(data->fadt_table.firmware_ctrl); |
15650602 MA |
201 | |
202 | ACPI_READ_FIELD(facs_table->signature, addr); | |
203 | ACPI_READ_FIELD(facs_table->length, addr); | |
204 | ACPI_READ_FIELD(facs_table->hardware_signature, addr); | |
205 | ACPI_READ_FIELD(facs_table->firmware_waking_vector, addr); | |
206 | ACPI_READ_FIELD(facs_table->global_lock, addr); | |
207 | ACPI_READ_FIELD(facs_table->flags, addr); | |
208 | ACPI_READ_ARRAY(facs_table->resverved3, addr); | |
209 | ||
c225aa3c | 210 | ACPI_ASSERT_CMP(facs_table->signature, "FACS"); |
15650602 MA |
211 | } |
212 | ||
03010579 IM |
213 | /** fetch_table |
214 | * load ACPI table at @addr into table descriptor @sdt_table | |
215 | * and check that header checksum matches actual one. | |
216 | */ | |
217 | static void fetch_table(AcpiSdtTable *sdt_table, uint32_t addr) | |
53333801 MA |
218 | { |
219 | uint8_t checksum; | |
220 | ||
dabc7f21 | 221 | memset(sdt_table, 0, sizeof(*sdt_table)); |
53333801 MA |
222 | ACPI_READ_TABLE_HEADER(&sdt_table->header, addr); |
223 | ||
3831c07b TH |
224 | sdt_table->aml_len = le32_to_cpu(sdt_table->header.length) |
225 | - sizeof(AcpiTableHeader); | |
53333801 MA |
226 | sdt_table->aml = g_malloc0(sdt_table->aml_len); |
227 | ACPI_READ_ARRAY_PTR(sdt_table->aml, sdt_table->aml_len, addr); | |
228 | ||
3248f1b4 BW |
229 | checksum = acpi_calc_checksum((uint8_t *)sdt_table, |
230 | sizeof(AcpiTableHeader)) + | |
231 | acpi_calc_checksum((uint8_t *)sdt_table->aml, | |
232 | sdt_table->aml_len); | |
53333801 MA |
233 | g_assert(!checksum); |
234 | } | |
235 | ||
236 | static void test_acpi_dsdt_table(test_data *data) | |
237 | { | |
9e8458c0 | 238 | AcpiSdtTable dsdt_table; |
3831c07b | 239 | uint32_t addr = le32_to_cpu(data->fadt_table.dsdt); |
53333801 | 240 | |
03010579 | 241 | fetch_table(&dsdt_table, addr); |
c225aa3c | 242 | ACPI_ASSERT_CMP(dsdt_table.header.signature, "DSDT"); |
9e8458c0 | 243 | |
b24b9d94 | 244 | /* Since DSDT isn't in RSDT, add DSDT to ASL test tables list manually */ |
a3a74ab9 | 245 | g_array_append_val(data->tables, dsdt_table); |
53333801 MA |
246 | } |
247 | ||
03010579 IM |
248 | /* Load all tables and add to test list directly RSDT referenced tables */ |
249 | static void fetch_rsdt_referenced_tables(test_data *data) | |
53333801 | 250 | { |
a3a74ab9 | 251 | int tables_nr = data->rsdt_tables_nr - 1; /* fadt is first */ |
53333801 MA |
252 | int i; |
253 | ||
a3a74ab9 | 254 | for (i = 0; i < tables_nr; i++) { |
8ac2adf7 | 255 | AcpiSdtTable ssdt_table; |
3831c07b | 256 | uint32_t addr; |
9e8458c0 | 257 | |
3831c07b | 258 | addr = le32_to_cpu(data->rsdt_tables_addr[i + 1]); /* fadt is first */ |
03010579 | 259 | fetch_table(&ssdt_table, addr); |
ab20bbd2 IM |
260 | |
261 | /* Add table to ASL test tables list */ | |
a3a74ab9 | 262 | g_array_append_val(data->tables, ssdt_table); |
53333801 | 263 | } |
9e8458c0 MA |
264 | } |
265 | ||
4500bc98 | 266 | static void dump_aml_files(test_data *data, bool rebuild) |
9e8458c0 MA |
267 | { |
268 | AcpiSdtTable *sdt; | |
269 | GError *error = NULL; | |
4500bc98 | 270 | gchar *aml_file = NULL; |
9e8458c0 MA |
271 | gint fd; |
272 | ssize_t ret; | |
273 | int i; | |
274 | ||
a3a74ab9 | 275 | for (i = 0; i < data->tables->len; ++i) { |
3a9c86df | 276 | const char *ext = data->variant ? data->variant : ""; |
a3a74ab9 | 277 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
9e8458c0 MA |
278 | g_assert(sdt->aml); |
279 | ||
4500bc98 | 280 | if (rebuild) { |
3a9c86df | 281 | aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine, |
3831c07b | 282 | (gchar *)&sdt->header.signature, ext); |
4500bc98 MA |
283 | fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT, |
284 | S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH); | |
285 | } else { | |
286 | fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error); | |
287 | g_assert_no_error(error); | |
288 | } | |
289 | g_assert(fd >= 0); | |
9e8458c0 MA |
290 | |
291 | ret = qemu_write_full(fd, sdt, sizeof(AcpiTableHeader)); | |
292 | g_assert(ret == sizeof(AcpiTableHeader)); | |
293 | ret = qemu_write_full(fd, sdt->aml, sdt->aml_len); | |
294 | g_assert(ret == sdt->aml_len); | |
295 | ||
296 | close(fd); | |
4500bc98 | 297 | |
ef1e1e07 | 298 | g_free(aml_file); |
9e8458c0 MA |
299 | } |
300 | } | |
301 | ||
c225aa3c | 302 | static bool compare_signature(AcpiSdtTable *sdt, const char *signature) |
69d09245 | 303 | { |
c225aa3c | 304 | return !memcmp(&sdt->header.signature, signature, 4); |
69d09245 MA |
305 | } |
306 | ||
15d914b1 | 307 | static bool load_asl(GArray *sdts, AcpiSdtTable *sdt) |
9e8458c0 MA |
308 | { |
309 | AcpiSdtTable *temp; | |
310 | GError *error = NULL; | |
cc8fa0e8 | 311 | GString *command_line = g_string_new(iasl); |
9e8458c0 MA |
312 | gint fd; |
313 | gchar *out, *out_err; | |
314 | gboolean ret; | |
315 | int i; | |
316 | ||
317 | fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error); | |
318 | g_assert_no_error(error); | |
319 | close(fd); | |
320 | ||
321 | /* build command line */ | |
cc8fa0e8 | 322 | g_string_append_printf(command_line, " -p %s ", sdt->asl_file); |
c225aa3c MT |
323 | if (compare_signature(sdt, "DSDT") || |
324 | compare_signature(sdt, "SSDT")) { | |
69d09245 MA |
325 | for (i = 0; i < sdts->len; ++i) { |
326 | temp = &g_array_index(sdts, AcpiSdtTable, i); | |
c225aa3c MT |
327 | if (compare_signature(temp, "DSDT") || |
328 | compare_signature(temp, "SSDT")) { | |
69d09245 MA |
329 | g_string_append_printf(command_line, "-e %s ", temp->aml_file); |
330 | } | |
331 | } | |
9e8458c0 MA |
332 | } |
333 | g_string_append_printf(command_line, "-d %s", sdt->aml_file); | |
334 | ||
335 | /* pass 'out' and 'out_err' in order to be redirected */ | |
15d914b1 | 336 | ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error); |
9e8458c0 | 337 | g_assert_no_error(error); |
15d914b1 MA |
338 | if (ret) { |
339 | ret = g_file_get_contents(sdt->asl_file, (gchar **)&sdt->asl, | |
340 | &sdt->asl_len, &error); | |
341 | g_assert(ret); | |
342 | g_assert_no_error(error); | |
dac23a6c | 343 | ret = (sdt->asl_len > 0); |
15d914b1 | 344 | } |
9e8458c0 MA |
345 | |
346 | g_free(out); | |
347 | g_free(out_err); | |
348 | g_string_free(command_line, true); | |
15d914b1 MA |
349 | |
350 | return !ret; | |
9e8458c0 MA |
351 | } |
352 | ||
353 | #define COMMENT_END "*/" | |
354 | #define DEF_BLOCK "DefinitionBlock (" | |
a3973f55 | 355 | #define BLOCK_NAME_END "," |
9e8458c0 MA |
356 | |
357 | static GString *normalize_asl(gchar *asl_code) | |
358 | { | |
359 | GString *asl = g_string_new(asl_code); | |
360 | gchar *comment, *block_name; | |
361 | ||
362 | /* strip comments (different generation days) */ | |
363 | comment = g_strstr_len(asl->str, asl->len, COMMENT_END); | |
364 | if (comment) { | |
cb348985 PB |
365 | comment += strlen(COMMENT_END); |
366 | while (*comment == '\n') { | |
367 | comment++; | |
368 | } | |
369 | asl = g_string_erase(asl, 0, comment - asl->str); | |
9e8458c0 MA |
370 | } |
371 | ||
372 | /* strip def block name (it has file path in it) */ | |
373 | if (g_str_has_prefix(asl->str, DEF_BLOCK)) { | |
374 | block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END); | |
375 | g_assert(block_name); | |
376 | asl = g_string_erase(asl, 0, | |
377 | block_name + sizeof(BLOCK_NAME_END) - asl->str); | |
378 | } | |
379 | ||
380 | return asl; | |
381 | } | |
382 | ||
383 | static GArray *load_expected_aml(test_data *data) | |
384 | { | |
385 | int i; | |
386 | AcpiSdtTable *sdt; | |
9e8458c0 MA |
387 | GError *error = NULL; |
388 | gboolean ret; | |
389 | ||
a3a74ab9 MA |
390 | GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable)); |
391 | for (i = 0; i < data->tables->len; ++i) { | |
9e8458c0 | 392 | AcpiSdtTable exp_sdt; |
d19587db | 393 | gchar *aml_file = NULL; |
3a9c86df | 394 | const char *ext = data->variant ? data->variant : ""; |
c225aa3c | 395 | |
a3a74ab9 | 396 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
9e8458c0 MA |
397 | |
398 | memset(&exp_sdt, 0, sizeof(exp_sdt)); | |
399 | exp_sdt.header.signature = sdt->header.signature; | |
400 | ||
3a9c86df IM |
401 | try_again: |
402 | aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine, | |
3831c07b | 403 | (gchar *)&sdt->header.signature, ext); |
d19587db IM |
404 | if (getenv("V")) { |
405 | fprintf(stderr, "\nLooking for expected file '%s'\n", aml_file); | |
406 | } | |
407 | if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) { | |
408 | exp_sdt.aml_file = aml_file; | |
409 | } else if (*ext != '\0') { | |
410 | /* try fallback to generic (extention less) expected file */ | |
3a9c86df | 411 | ext = ""; |
d19587db | 412 | g_free(aml_file); |
3a9c86df IM |
413 | goto try_again; |
414 | } | |
d19587db IM |
415 | g_assert(exp_sdt.aml_file); |
416 | if (getenv("V")) { | |
417 | fprintf(stderr, "\nUsing expected file '%s'\n", aml_file); | |
418 | } | |
9e8458c0 MA |
419 | ret = g_file_get_contents(aml_file, &exp_sdt.aml, |
420 | &exp_sdt.aml_len, &error); | |
421 | g_assert(ret); | |
422 | g_assert_no_error(error); | |
423 | g_assert(exp_sdt.aml); | |
424 | g_assert(exp_sdt.aml_len); | |
425 | ||
a3a74ab9 | 426 | g_array_append_val(exp_tables, exp_sdt); |
9e8458c0 MA |
427 | } |
428 | ||
a3a74ab9 | 429 | return exp_tables; |
9e8458c0 MA |
430 | } |
431 | ||
ab20bbd2 | 432 | /* test the list of tables in @data->tables against reference tables */ |
9e8458c0 MA |
433 | static void test_acpi_asl(test_data *data) |
434 | { | |
435 | int i; | |
436 | AcpiSdtTable *sdt, *exp_sdt; | |
437 | test_data exp_data; | |
15d914b1 | 438 | gboolean exp_err, err; |
9e8458c0 MA |
439 | |
440 | memset(&exp_data, 0, sizeof(exp_data)); | |
a3a74ab9 | 441 | exp_data.tables = load_expected_aml(data); |
4500bc98 | 442 | dump_aml_files(data, false); |
a3a74ab9 | 443 | for (i = 0; i < data->tables->len; ++i) { |
9e8458c0 MA |
444 | GString *asl, *exp_asl; |
445 | ||
a3a74ab9 MA |
446 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
447 | exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i); | |
9e8458c0 | 448 | |
15d914b1 | 449 | err = load_asl(data->tables, sdt); |
9e8458c0 MA |
450 | asl = normalize_asl(sdt->asl); |
451 | ||
15d914b1 | 452 | exp_err = load_asl(exp_data.tables, exp_sdt); |
9e8458c0 MA |
453 | exp_asl = normalize_asl(exp_sdt->asl); |
454 | ||
15d914b1 MA |
455 | /* TODO: check for warnings */ |
456 | g_assert(!err || exp_err); | |
457 | ||
0651596c | 458 | if (g_strcmp0(asl->str, exp_asl->str)) { |
dac23a6c MA |
459 | if (exp_err) { |
460 | fprintf(stderr, | |
461 | "Warning! iasl couldn't parse the expected aml\n"); | |
462 | } else { | |
463 | uint32_t signature = cpu_to_le32(exp_sdt->header.signature); | |
464 | sdt->tmp_files_retain = true; | |
465 | exp_sdt->tmp_files_retain = true; | |
466 | fprintf(stderr, | |
467 | "acpi-test: Warning! %.4s mismatch. " | |
468 | "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n", | |
469 | (gchar *)&signature, | |
470 | sdt->asl_file, sdt->aml_file, | |
471 | exp_sdt->asl_file, exp_sdt->aml_file); | |
7cb08cb2 IM |
472 | if (getenv("V")) { |
473 | const char *diff_cmd = getenv("DIFF"); | |
474 | if (diff_cmd) { | |
475 | int ret G_GNUC_UNUSED; | |
476 | char *diff = g_strdup_printf("%s %s %s", diff_cmd, | |
477 | exp_sdt->asl_file, sdt->asl_file); | |
478 | ret = system(diff) ; | |
479 | g_free(diff); | |
480 | } else { | |
481 | fprintf(stderr, "acpi-test: Warning. not showing " | |
482 | "difference since no diff utility is specified. " | |
483 | "Set 'DIFF' environment variable to a preferred " | |
484 | "diff utility and run 'make V=1 check' again to " | |
485 | "see ASL difference."); | |
486 | } | |
487 | } | |
dac23a6c | 488 | } |
0651596c | 489 | } |
9e8458c0 MA |
490 | g_string_free(asl, true); |
491 | g_string_free(exp_asl, true); | |
492 | } | |
493 | ||
494 | free_test_data(&exp_data); | |
53333801 MA |
495 | } |
496 | ||
5efed5a1 | 497 | static bool smbios_ep_table_ok(test_data *data) |
eb386aac | 498 | { |
86299120 | 499 | struct smbios_21_entry_point *ep_table = &data->smbios_ep_table; |
eb386aac GS |
500 | uint32_t addr = data->smbios_ep_addr; |
501 | ||
502 | ACPI_READ_ARRAY(ep_table->anchor_string, addr); | |
5efed5a1 GS |
503 | if (memcmp(ep_table->anchor_string, "_SM_", 4)) { |
504 | return false; | |
505 | } | |
eb386aac GS |
506 | ACPI_READ_FIELD(ep_table->checksum, addr); |
507 | ACPI_READ_FIELD(ep_table->length, addr); | |
508 | ACPI_READ_FIELD(ep_table->smbios_major_version, addr); | |
509 | ACPI_READ_FIELD(ep_table->smbios_minor_version, addr); | |
510 | ACPI_READ_FIELD(ep_table->max_structure_size, addr); | |
511 | ACPI_READ_FIELD(ep_table->entry_point_revision, addr); | |
512 | ACPI_READ_ARRAY(ep_table->formatted_area, addr); | |
513 | ACPI_READ_ARRAY(ep_table->intermediate_anchor_string, addr); | |
5efed5a1 GS |
514 | if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) { |
515 | return false; | |
516 | } | |
eb386aac GS |
517 | ACPI_READ_FIELD(ep_table->intermediate_checksum, addr); |
518 | ACPI_READ_FIELD(ep_table->structure_table_length, addr); | |
5efed5a1 GS |
519 | if (ep_table->structure_table_length == 0) { |
520 | return false; | |
521 | } | |
eb386aac GS |
522 | ACPI_READ_FIELD(ep_table->structure_table_address, addr); |
523 | ACPI_READ_FIELD(ep_table->number_of_structures, addr); | |
5efed5a1 GS |
524 | if (ep_table->number_of_structures == 0) { |
525 | return false; | |
526 | } | |
eb386aac | 527 | ACPI_READ_FIELD(ep_table->smbios_bcd_revision, addr); |
3248f1b4 BW |
528 | if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) || |
529 | acpi_calc_checksum((uint8_t *)ep_table + 0x10, | |
530 | sizeof *ep_table - 0x10)) { | |
5efed5a1 GS |
531 | return false; |
532 | } | |
533 | return true; | |
534 | } | |
535 | ||
536 | static void test_smbios_entry_point(test_data *data) | |
537 | { | |
538 | uint32_t off; | |
539 | ||
540 | /* find smbios entry point structure */ | |
541 | for (off = 0xf0000; off < 0x100000; off += 0x10) { | |
542 | uint8_t sig[] = "_SM_"; | |
543 | int i; | |
544 | ||
545 | for (i = 0; i < sizeof sig - 1; ++i) { | |
546 | sig[i] = readb(off + i); | |
547 | } | |
548 | ||
549 | if (!memcmp(sig, "_SM_", sizeof sig)) { | |
550 | /* signature match, but is this a valid entry point? */ | |
551 | data->smbios_ep_addr = off; | |
552 | if (smbios_ep_table_ok(data)) { | |
553 | break; | |
554 | } | |
555 | } | |
556 | } | |
557 | ||
558 | g_assert_cmphex(off, <, 0x100000); | |
eb386aac GS |
559 | } |
560 | ||
561 | static inline bool smbios_single_instance(uint8_t type) | |
562 | { | |
563 | switch (type) { | |
564 | case 0: | |
565 | case 1: | |
566 | case 2: | |
567 | case 3: | |
568 | case 16: | |
569 | case 32: | |
570 | case 127: | |
571 | return true; | |
572 | default: | |
573 | return false; | |
574 | } | |
575 | } | |
576 | ||
577 | static void test_smbios_structs(test_data *data) | |
578 | { | |
579 | DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 }; | |
86299120 | 580 | struct smbios_21_entry_point *ep_table = &data->smbios_ep_table; |
3831c07b | 581 | uint32_t addr = le32_to_cpu(ep_table->structure_table_address); |
eb386aac GS |
582 | int i, len, max_len = 0; |
583 | uint8_t type, prv, crt; | |
eb386aac GS |
584 | |
585 | /* walk the smbios tables */ | |
3831c07b | 586 | for (i = 0; i < le16_to_cpu(ep_table->number_of_structures); i++) { |
eb386aac GS |
587 | |
588 | /* grab type and formatted area length from struct header */ | |
589 | type = readb(addr); | |
590 | g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE); | |
591 | len = readb(addr + 1); | |
592 | ||
593 | /* single-instance structs must not have been encountered before */ | |
594 | if (smbios_single_instance(type)) { | |
595 | g_assert(!test_bit(type, struct_bitmap)); | |
596 | } | |
597 | set_bit(type, struct_bitmap); | |
598 | ||
599 | /* seek to end of unformatted string area of this struct ("\0\0") */ | |
600 | prv = crt = 1; | |
601 | while (prv || crt) { | |
602 | prv = crt; | |
603 | crt = readb(addr + len); | |
604 | len++; | |
605 | } | |
606 | ||
607 | /* keep track of max. struct size */ | |
608 | if (max_len < len) { | |
609 | max_len = len; | |
610 | g_assert_cmpuint(max_len, <=, ep_table->max_structure_size); | |
611 | } | |
612 | ||
613 | /* start of next structure */ | |
614 | addr += len; | |
615 | } | |
616 | ||
617 | /* total table length and max struct size must match entry point values */ | |
3831c07b TH |
618 | g_assert_cmpuint(le16_to_cpu(ep_table->structure_table_length), ==, |
619 | addr - le32_to_cpu(ep_table->structure_table_address)); | |
620 | g_assert_cmpuint(le16_to_cpu(ep_table->max_structure_size), ==, max_len); | |
eb386aac GS |
621 | |
622 | /* required struct types must all be present */ | |
f4eda2d4 CM |
623 | for (i = 0; i < data->required_struct_types_len; i++) { |
624 | g_assert(test_bit(data->required_struct_types[i], struct_bitmap)); | |
eb386aac GS |
625 | } |
626 | } | |
627 | ||
8ac2adf7 | 628 | static void test_acpi_one(const char *params, test_data *data) |
ad6423a7 MT |
629 | { |
630 | char *args; | |
ad6423a7 | 631 | |
947b205f MA |
632 | /* Disable kernel irqchip to be able to override apic irq0. */ |
633 | args = g_strdup_printf("-machine %s,accel=%s,kernel-irqchip=off " | |
634 | "-net none -display none %s " | |
b8e665e4 | 635 | "-drive id=hd0,if=none,file=%s,format=raw " |
6b9e03a4 | 636 | "-device ide-hd,drive=hd0 ", |
947b205f | 637 | data->machine, "kvm:tcg", |
6b9e03a4 | 638 | params ? params : "", disk); |
9e8458c0 | 639 | |
ad6423a7 MT |
640 | qtest_start(args); |
641 | ||
4e082566 | 642 | boot_sector_test(); |
ad6423a7 | 643 | |
b24b9d94 | 644 | data->tables = g_array_new(false, true, sizeof(AcpiSdtTable)); |
8ac2adf7 MA |
645 | test_acpi_rsdp_address(data); |
646 | test_acpi_rsdp_table(data); | |
647 | test_acpi_rsdt_table(data); | |
648 | test_acpi_fadt_table(data); | |
15650602 | 649 | test_acpi_facs_table(data); |
8ac2adf7 | 650 | test_acpi_dsdt_table(data); |
03010579 | 651 | fetch_rsdt_referenced_tables(data); |
ad6423a7 | 652 | |
cc8fa0e8 | 653 | if (iasl) { |
4500bc98 MA |
654 | if (getenv(ACPI_REBUILD_EXPECTED_AML)) { |
655 | dump_aml_files(data, true); | |
656 | } else { | |
657 | test_acpi_asl(data); | |
658 | } | |
9e8458c0 MA |
659 | } |
660 | ||
5efed5a1 | 661 | test_smbios_entry_point(data); |
eb386aac GS |
662 | test_smbios_structs(data); |
663 | ||
ad6423a7 MT |
664 | qtest_quit(global_qtest); |
665 | g_free(args); | |
666 | } | |
667 | ||
f4eda2d4 CM |
668 | static uint8_t base_required_struct_types[] = { |
669 | 0, 1, 3, 4, 16, 17, 19, 32, 127 | |
670 | }; | |
671 | ||
71f4be25 | 672 | static void test_acpi_piix4_tcg(void) |
ad6423a7 | 673 | { |
8ac2adf7 MA |
674 | test_data data; |
675 | ||
ad6423a7 MT |
676 | /* Supplying -machine accel argument overrides the default (qtest). |
677 | * This is to make guest actually run. | |
678 | */ | |
9e8458c0 MA |
679 | memset(&data, 0, sizeof(data)); |
680 | data.machine = MACHINE_PC; | |
f4eda2d4 CM |
681 | data.required_struct_types = base_required_struct_types; |
682 | data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types); | |
947b205f | 683 | test_acpi_one(NULL, &data); |
9e8458c0 | 684 | free_test_data(&data); |
71f4be25 PB |
685 | } |
686 | ||
3a9c86df IM |
687 | static void test_acpi_piix4_tcg_bridge(void) |
688 | { | |
689 | test_data data; | |
690 | ||
691 | memset(&data, 0, sizeof(data)); | |
692 | data.machine = MACHINE_PC; | |
693 | data.variant = ".bridge"; | |
f4eda2d4 CM |
694 | data.required_struct_types = base_required_struct_types; |
695 | data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types); | |
947b205f | 696 | test_acpi_one("-device pci-bridge,chassis_nr=1", &data); |
3a9c86df IM |
697 | free_test_data(&data); |
698 | } | |
699 | ||
71f4be25 PB |
700 | static void test_acpi_q35_tcg(void) |
701 | { | |
702 | test_data data; | |
8ac2adf7 | 703 | |
9e8458c0 MA |
704 | memset(&data, 0, sizeof(data)); |
705 | data.machine = MACHINE_Q35; | |
f4eda2d4 CM |
706 | data.required_struct_types = base_required_struct_types; |
707 | data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types); | |
947b205f | 708 | test_acpi_one(NULL, &data); |
8ac2adf7 | 709 | free_test_data(&data); |
ad6423a7 MT |
710 | } |
711 | ||
3a9c86df IM |
712 | static void test_acpi_q35_tcg_bridge(void) |
713 | { | |
714 | test_data data; | |
715 | ||
716 | memset(&data, 0, sizeof(data)); | |
717 | data.machine = MACHINE_Q35; | |
718 | data.variant = ".bridge"; | |
f4eda2d4 CM |
719 | data.required_struct_types = base_required_struct_types; |
720 | data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types); | |
947b205f | 721 | test_acpi_one("-device pci-bridge,chassis_nr=1", |
3a9c86df IM |
722 | &data); |
723 | free_test_data(&data); | |
724 | } | |
725 | ||
6b9c1dd2 IM |
726 | static void test_acpi_piix4_tcg_cphp(void) |
727 | { | |
728 | test_data data; | |
729 | ||
730 | memset(&data, 0, sizeof(data)); | |
731 | data.machine = MACHINE_PC; | |
732 | data.variant = ".cphp"; | |
d6309c17 | 733 | test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6" |
fda4096f HC |
734 | " -numa node -numa node" |
735 | " -numa dist,src=0,dst=1,val=21", | |
6b9c1dd2 IM |
736 | &data); |
737 | free_test_data(&data); | |
738 | } | |
739 | ||
740 | static void test_acpi_q35_tcg_cphp(void) | |
741 | { | |
742 | test_data data; | |
743 | ||
744 | memset(&data, 0, sizeof(data)); | |
745 | data.machine = MACHINE_Q35; | |
746 | data.variant = ".cphp"; | |
d6309c17 | 747 | test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6" |
fda4096f HC |
748 | " -numa node -numa node" |
749 | " -numa dist,src=0,dst=1,val=21", | |
6b9c1dd2 IM |
750 | &data); |
751 | free_test_data(&data); | |
752 | } | |
753 | ||
f4eda2d4 CM |
754 | static uint8_t ipmi_required_struct_types[] = { |
755 | 0, 1, 3, 4, 16, 17, 19, 32, 38, 127 | |
756 | }; | |
757 | ||
758 | static void test_acpi_q35_tcg_ipmi(void) | |
759 | { | |
760 | test_data data; | |
761 | ||
762 | memset(&data, 0, sizeof(data)); | |
763 | data.machine = MACHINE_Q35; | |
764 | data.variant = ".ipmibt"; | |
765 | data.required_struct_types = ipmi_required_struct_types; | |
766 | data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types); | |
947b205f | 767 | test_acpi_one("-device ipmi-bmc-sim,id=bmc0" |
f4eda2d4 CM |
768 | " -device isa-ipmi-bt,bmc=bmc0", |
769 | &data); | |
770 | free_test_data(&data); | |
771 | } | |
772 | ||
773 | static void test_acpi_piix4_tcg_ipmi(void) | |
774 | { | |
775 | test_data data; | |
776 | ||
777 | /* Supplying -machine accel argument overrides the default (qtest). | |
778 | * This is to make guest actually run. | |
779 | */ | |
780 | memset(&data, 0, sizeof(data)); | |
781 | data.machine = MACHINE_PC; | |
782 | data.variant = ".ipmikcs"; | |
783 | data.required_struct_types = ipmi_required_struct_types; | |
784 | data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types); | |
947b205f | 785 | test_acpi_one("-device ipmi-bmc-sim,id=bmc0" |
f4eda2d4 CM |
786 | " -device isa-ipmi-kcs,irq=0,bmc=bmc0", |
787 | &data); | |
788 | free_test_data(&data); | |
789 | } | |
790 | ||
4462fc60 IM |
791 | static void test_acpi_q35_tcg_memhp(void) |
792 | { | |
793 | test_data data; | |
794 | ||
795 | memset(&data, 0, sizeof(data)); | |
796 | data.machine = MACHINE_Q35; | |
797 | data.variant = ".memhp"; | |
fda4096f HC |
798 | test_acpi_one(" -m 128,slots=3,maxmem=1G" |
799 | " -numa node -numa node" | |
800 | " -numa dist,src=0,dst=1,val=21", | |
801 | &data); | |
4462fc60 IM |
802 | free_test_data(&data); |
803 | } | |
804 | ||
805 | static void test_acpi_piix4_tcg_memhp(void) | |
806 | { | |
807 | test_data data; | |
808 | ||
809 | memset(&data, 0, sizeof(data)); | |
810 | data.machine = MACHINE_PC; | |
811 | data.variant = ".memhp"; | |
fda4096f HC |
812 | test_acpi_one(" -m 128,slots=3,maxmem=1G" |
813 | " -numa node -numa node" | |
814 | " -numa dist,src=0,dst=1,val=21", | |
815 | &data); | |
4462fc60 IM |
816 | free_test_data(&data); |
817 | } | |
818 | ||
d82c4f82 DL |
819 | static void test_acpi_q35_tcg_numamem(void) |
820 | { | |
821 | test_data data; | |
822 | ||
823 | memset(&data, 0, sizeof(data)); | |
824 | data.machine = MACHINE_Q35; | |
825 | data.variant = ".numamem"; | |
826 | test_acpi_one(" -numa node -numa node,mem=128", &data); | |
827 | free_test_data(&data); | |
828 | } | |
829 | ||
830 | static void test_acpi_piix4_tcg_numamem(void) | |
831 | { | |
832 | test_data data; | |
833 | ||
834 | memset(&data, 0, sizeof(data)); | |
835 | data.machine = MACHINE_PC; | |
836 | data.variant = ".numamem"; | |
837 | test_acpi_one(" -numa node -numa node,mem=128", &data); | |
838 | free_test_data(&data); | |
839 | } | |
840 | ||
ad6423a7 MT |
841 | int main(int argc, char *argv[]) |
842 | { | |
843 | const char *arch = qtest_get_arch(); | |
5862ad0f | 844 | int ret; |
c39a28a4 | 845 | |
4e082566 VK |
846 | ret = boot_sector_init(disk); |
847 | if(ret) | |
848 | return ret; | |
ad6423a7 MT |
849 | |
850 | g_test_init(&argc, &argv, NULL); | |
851 | ||
852 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { | |
947b205f MA |
853 | qtest_add_func("acpi/piix4", test_acpi_piix4_tcg); |
854 | qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge); | |
855 | qtest_add_func("acpi/q35", test_acpi_q35_tcg); | |
856 | qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge); | |
857 | qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi); | |
858 | qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi); | |
859 | qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp); | |
860 | qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp); | |
4462fc60 IM |
861 | qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp); |
862 | qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp); | |
d82c4f82 DL |
863 | qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem); |
864 | qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem); | |
ad6423a7 | 865 | } |
5862ad0f | 866 | ret = g_test_run(); |
4e082566 | 867 | boot_sector_cleanup(disk); |
5862ad0f | 868 | return ret; |
ad6423a7 | 869 | } |