]>
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" |
a2eb5c0c | 16 | #include "hw/firmware/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 { |
3dc01874 | 27 | const char *accel; |
9e8458c0 | 28 | const char *machine; |
3a9c86df | 29 | const char *variant; |
97256e79 IM |
30 | const char *uefi_fl1; |
31 | const char *uefi_fl2; | |
32 | const char *cd; | |
33 | const uint64_t ram_start; | |
34 | const uint64_t scan_len; | |
9c041885 | 35 | uint64_t rsdp_addr; |
d6caf363 | 36 | uint8_t rsdp_table[36 /* ACPI 2.0+ RSDP size */]; |
a3a74ab9 | 37 | GArray *tables; |
eb386aac | 38 | uint32_t smbios_ep_addr; |
86299120 | 39 | struct smbios_21_entry_point smbios_ep_table; |
f4eda2d4 CM |
40 | uint8_t *required_struct_types; |
41 | int required_struct_types_len; | |
273e3d92 | 42 | QTestState *qts; |
53333801 | 43 | } test_data; |
ad6423a7 | 44 | |
3e353773 | 45 | static char disk[] = "tests/acpi-test-disk-XXXXXX"; |
438c78da | 46 | static const char *data_dir = "tests/data/acpi"; |
cc8fa0e8 MA |
47 | #ifdef CONFIG_IASL |
48 | static const char *iasl = stringify(CONFIG_IASL); | |
49 | #else | |
50 | static const char *iasl; | |
51 | #endif | |
ad6423a7 | 52 | |
b137522c IM |
53 | static bool compare_signature(const AcpiSdtTable *sdt, const char *signature) |
54 | { | |
55 | return !memcmp(sdt->aml, signature, 4); | |
56 | } | |
57 | ||
569afd84 IM |
58 | static void cleanup_table_descriptor(AcpiSdtTable *table) |
59 | { | |
60 | g_free(table->aml); | |
61 | if (table->aml_file && | |
62 | !table->tmp_files_retain && | |
63 | g_strstr_len(table->aml_file, -1, "aml-")) { | |
64 | unlink(table->aml_file); | |
65 | } | |
66 | g_free(table->aml_file); | |
67 | g_free(table->asl); | |
68 | if (table->asl_file && | |
69 | !table->tmp_files_retain) { | |
70 | unlink(table->asl_file); | |
71 | } | |
72 | g_free(table->asl_file); | |
73 | } | |
74 | ||
8ac2adf7 MA |
75 | static void free_test_data(test_data *data) |
76 | { | |
77 | int i; | |
78 | ||
a3a74ab9 | 79 | for (i = 0; i < data->tables->len; ++i) { |
569afd84 | 80 | cleanup_table_descriptor(&g_array_index(data->tables, AcpiSdtTable, i)); |
8ac2adf7 | 81 | } |
9e8458c0 | 82 | |
f11dc27b | 83 | g_array_free(data->tables, true); |
8ac2adf7 MA |
84 | } |
85 | ||
53333801 MA |
86 | static void test_acpi_rsdp_table(test_data *data) |
87 | { | |
9c041885 | 88 | uint8_t *rsdp_table = data->rsdp_table; |
53333801 | 89 | |
9c041885 | 90 | acpi_fetch_rsdp_table(data->qts, data->rsdp_addr, rsdp_table); |
d6caf363 | 91 | |
9c041885 | 92 | switch (rsdp_table[15 /* Revision offset */]) { |
d6caf363 SO |
93 | case 0: /* ACPI 1.0 RSDP */ |
94 | /* With rev 1, checksum is only for the first 20 bytes */ | |
95 | g_assert(!acpi_calc_checksum(rsdp_table, 20)); | |
96 | break; | |
97 | case 2: /* ACPI 2.0+ RSDP */ | |
98 | /* With revision 2, we have 2 checksums */ | |
99 | g_assert(!acpi_calc_checksum(rsdp_table, 20)); | |
100 | g_assert(!acpi_calc_checksum(rsdp_table, 36)); | |
101 | break; | |
102 | default: | |
103 | g_assert_not_reached(); | |
104 | } | |
53333801 MA |
105 | } |
106 | ||
f2f616ce | 107 | static void test_acpi_rxsdt_table(test_data *data) |
53333801 | 108 | { |
f2f616ce | 109 | const char *sig = "RSDT"; |
569afd84 | 110 | AcpiSdtTable rsdt = {}; |
f2f616ce IM |
111 | int entry_size = 4; |
112 | int addr_off = 16 /* RsdtAddress */; | |
acee774b | 113 | uint8_t *ent; |
569afd84 | 114 | |
f2f616ce IM |
115 | if (data->rsdp_table[15 /* Revision offset */] != 0) { |
116 | addr_off = 24 /* XsdtAddress */; | |
117 | entry_size = 8; | |
118 | sig = "XSDT"; | |
119 | } | |
120 | /* read [RX]SDT table */ | |
acee774b | 121 | acpi_fetch_table(data->qts, &rsdt.aml, &rsdt.aml_len, |
f2f616ce | 122 | &data->rsdp_table[addr_off], entry_size, sig, true); |
569afd84 IM |
123 | |
124 | /* Load all tables and add to test list directly RSDT referenced tables */ | |
f2f616ce | 125 | ACPI_FOREACH_RSDT_ENTRY(rsdt.aml, rsdt.aml_len, ent, entry_size) { |
569afd84 IM |
126 | AcpiSdtTable ssdt_table = {}; |
127 | ||
acee774b | 128 | acpi_fetch_table(data->qts, &ssdt_table.aml, &ssdt_table.aml_len, ent, |
f2f616ce | 129 | entry_size, NULL, true); |
569afd84 IM |
130 | /* Add table to ASL test tables list */ |
131 | g_array_append_val(data->tables, ssdt_table); | |
132 | } | |
133 | cleanup_table_descriptor(&rsdt); | |
53333801 MA |
134 | } |
135 | ||
db575449 | 136 | static void test_acpi_fadt_table(test_data *data) |
53333801 | 137 | { |
db575449 | 138 | /* FADT table is 1st */ |
59f9c6cc IM |
139 | AcpiSdtTable table = g_array_index(data->tables, typeof(table), 0); |
140 | uint8_t *fadt_aml = table.aml; | |
b997a04a | 141 | uint32_t fadt_len = table.aml_len; |
55089fa2 IM |
142 | uint32_t val; |
143 | int dsdt_offset = 40 /* DSDT */; | |
144 | int dsdt_entry_size = 4; | |
53333801 | 145 | |
b137522c | 146 | g_assert(compare_signature(&table, "FACP")); |
89d47c19 | 147 | |
59f9c6cc | 148 | /* Since DSDT/FACS isn't in RSDT, add them to ASL test list manually */ |
40dfd0a8 IM |
149 | memcpy(&val, fadt_aml + 112 /* Flags */, 4); |
150 | val = le32_to_cpu(val); | |
151 | if (!(val & 1UL << 20 /* HW_REDUCED_ACPI */)) { | |
152 | acpi_fetch_table(data->qts, &table.aml, &table.aml_len, | |
153 | fadt_aml + 36 /* FIRMWARE_CTRL */, 4, "FACS", false); | |
154 | g_array_append_val(data->tables, table); | |
155 | } | |
59f9c6cc | 156 | |
55089fa2 IM |
157 | memcpy(&val, fadt_aml + dsdt_offset, 4); |
158 | val = le32_to_cpu(val); | |
159 | if (!val) { | |
160 | dsdt_offset = 140 /* X_DSDT */; | |
161 | dsdt_entry_size = 8; | |
162 | } | |
acee774b | 163 | acpi_fetch_table(data->qts, &table.aml, &table.aml_len, |
55089fa2 | 164 | fadt_aml + dsdt_offset, dsdt_entry_size, "DSDT", true); |
59f9c6cc | 165 | g_array_append_val(data->tables, table); |
92146b7a | 166 | |
b997a04a IM |
167 | memset(fadt_aml + 36, 0, 4); /* sanitize FIRMWARE_CTRL ptr */ |
168 | memset(fadt_aml + 40, 0, 4); /* sanitize DSDT ptr */ | |
169 | if (fadt_aml[8 /* FADT Major Version */] >= 3) { | |
170 | memset(fadt_aml + 132, 0, 8); /* sanitize X_FIRMWARE_CTRL ptr */ | |
171 | memset(fadt_aml + 140, 0, 8); /* sanitize X_DSDT ptr */ | |
92146b7a | 172 | } |
b997a04a IM |
173 | |
174 | /* update checksum */ | |
175 | fadt_aml[9 /* Checksum */] = 0; | |
176 | fadt_aml[9 /* Checksum */] -= acpi_calc_checksum(fadt_aml, fadt_len); | |
92146b7a IM |
177 | } |
178 | ||
4500bc98 | 179 | static void dump_aml_files(test_data *data, bool rebuild) |
9e8458c0 MA |
180 | { |
181 | AcpiSdtTable *sdt; | |
182 | GError *error = NULL; | |
4500bc98 | 183 | gchar *aml_file = NULL; |
9e8458c0 MA |
184 | gint fd; |
185 | ssize_t ret; | |
186 | int i; | |
187 | ||
a3a74ab9 | 188 | for (i = 0; i < data->tables->len; ++i) { |
3a9c86df | 189 | const char *ext = data->variant ? data->variant : ""; |
a3a74ab9 | 190 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
9e8458c0 MA |
191 | g_assert(sdt->aml); |
192 | ||
4500bc98 | 193 | if (rebuild) { |
3a9c86df | 194 | aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine, |
b137522c | 195 | sdt->aml, ext); |
4500bc98 MA |
196 | fd = g_open(aml_file, O_WRONLY|O_TRUNC|O_CREAT, |
197 | S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH); | |
ba02ff90 IM |
198 | if (fd < 0) { |
199 | perror(aml_file); | |
200 | } | |
201 | g_assert(fd >= 0); | |
4500bc98 MA |
202 | } else { |
203 | fd = g_file_open_tmp("aml-XXXXXX", &sdt->aml_file, &error); | |
204 | g_assert_no_error(error); | |
205 | } | |
9e8458c0 | 206 | |
9e8458c0 MA |
207 | ret = qemu_write_full(fd, sdt->aml, sdt->aml_len); |
208 | g_assert(ret == sdt->aml_len); | |
209 | ||
210 | close(fd); | |
4500bc98 | 211 | |
ef1e1e07 | 212 | g_free(aml_file); |
9e8458c0 MA |
213 | } |
214 | } | |
215 | ||
15d914b1 | 216 | static bool load_asl(GArray *sdts, AcpiSdtTable *sdt) |
9e8458c0 MA |
217 | { |
218 | AcpiSdtTable *temp; | |
219 | GError *error = NULL; | |
cc8fa0e8 | 220 | GString *command_line = g_string_new(iasl); |
9e8458c0 MA |
221 | gint fd; |
222 | gchar *out, *out_err; | |
223 | gboolean ret; | |
224 | int i; | |
225 | ||
226 | fd = g_file_open_tmp("asl-XXXXXX.dsl", &sdt->asl_file, &error); | |
227 | g_assert_no_error(error); | |
228 | close(fd); | |
229 | ||
230 | /* build command line */ | |
cc8fa0e8 | 231 | g_string_append_printf(command_line, " -p %s ", sdt->asl_file); |
c225aa3c MT |
232 | if (compare_signature(sdt, "DSDT") || |
233 | compare_signature(sdt, "SSDT")) { | |
69d09245 MA |
234 | for (i = 0; i < sdts->len; ++i) { |
235 | temp = &g_array_index(sdts, AcpiSdtTable, i); | |
c225aa3c MT |
236 | if (compare_signature(temp, "DSDT") || |
237 | compare_signature(temp, "SSDT")) { | |
69d09245 MA |
238 | g_string_append_printf(command_line, "-e %s ", temp->aml_file); |
239 | } | |
240 | } | |
9e8458c0 MA |
241 | } |
242 | g_string_append_printf(command_line, "-d %s", sdt->aml_file); | |
243 | ||
244 | /* pass 'out' and 'out_err' in order to be redirected */ | |
15d914b1 | 245 | ret = g_spawn_command_line_sync(command_line->str, &out, &out_err, NULL, &error); |
9e8458c0 | 246 | g_assert_no_error(error); |
15d914b1 | 247 | if (ret) { |
cc80b01a | 248 | ret = g_file_get_contents(sdt->asl_file, &sdt->asl, |
15d914b1 MA |
249 | &sdt->asl_len, &error); |
250 | g_assert(ret); | |
251 | g_assert_no_error(error); | |
dac23a6c | 252 | ret = (sdt->asl_len > 0); |
15d914b1 | 253 | } |
9e8458c0 MA |
254 | |
255 | g_free(out); | |
256 | g_free(out_err); | |
257 | g_string_free(command_line, true); | |
15d914b1 MA |
258 | |
259 | return !ret; | |
9e8458c0 MA |
260 | } |
261 | ||
262 | #define COMMENT_END "*/" | |
263 | #define DEF_BLOCK "DefinitionBlock (" | |
a3973f55 | 264 | #define BLOCK_NAME_END "," |
9e8458c0 MA |
265 | |
266 | static GString *normalize_asl(gchar *asl_code) | |
267 | { | |
268 | GString *asl = g_string_new(asl_code); | |
269 | gchar *comment, *block_name; | |
270 | ||
271 | /* strip comments (different generation days) */ | |
272 | comment = g_strstr_len(asl->str, asl->len, COMMENT_END); | |
273 | if (comment) { | |
cb348985 PB |
274 | comment += strlen(COMMENT_END); |
275 | while (*comment == '\n') { | |
276 | comment++; | |
277 | } | |
278 | asl = g_string_erase(asl, 0, comment - asl->str); | |
9e8458c0 MA |
279 | } |
280 | ||
281 | /* strip def block name (it has file path in it) */ | |
282 | if (g_str_has_prefix(asl->str, DEF_BLOCK)) { | |
283 | block_name = g_strstr_len(asl->str, asl->len, BLOCK_NAME_END); | |
284 | g_assert(block_name); | |
285 | asl = g_string_erase(asl, 0, | |
286 | block_name + sizeof(BLOCK_NAME_END) - asl->str); | |
287 | } | |
288 | ||
289 | return asl; | |
290 | } | |
291 | ||
292 | static GArray *load_expected_aml(test_data *data) | |
293 | { | |
294 | int i; | |
295 | AcpiSdtTable *sdt; | |
9e8458c0 MA |
296 | GError *error = NULL; |
297 | gboolean ret; | |
acee774b | 298 | gsize aml_len; |
9e8458c0 | 299 | |
a3a74ab9 | 300 | GArray *exp_tables = g_array_new(false, true, sizeof(AcpiSdtTable)); |
fe17cca6 PMD |
301 | if (getenv("V")) { |
302 | fputc('\n', stderr); | |
303 | } | |
a3a74ab9 | 304 | for (i = 0; i < data->tables->len; ++i) { |
9e8458c0 | 305 | AcpiSdtTable exp_sdt; |
d19587db | 306 | gchar *aml_file = NULL; |
3a9c86df | 307 | const char *ext = data->variant ? data->variant : ""; |
c225aa3c | 308 | |
a3a74ab9 | 309 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
9e8458c0 MA |
310 | |
311 | memset(&exp_sdt, 0, sizeof(exp_sdt)); | |
9e8458c0 | 312 | |
3a9c86df IM |
313 | try_again: |
314 | aml_file = g_strdup_printf("%s/%s/%.4s%s", data_dir, data->machine, | |
b137522c | 315 | sdt->aml, ext); |
d19587db | 316 | if (getenv("V")) { |
fe17cca6 | 317 | fprintf(stderr, "Looking for expected file '%s'\n", aml_file); |
d19587db IM |
318 | } |
319 | if (g_file_test(aml_file, G_FILE_TEST_EXISTS)) { | |
320 | exp_sdt.aml_file = aml_file; | |
321 | } else if (*ext != '\0') { | |
e50a6121 | 322 | /* try fallback to generic (extension less) expected file */ |
3a9c86df | 323 | ext = ""; |
d19587db | 324 | g_free(aml_file); |
3a9c86df IM |
325 | goto try_again; |
326 | } | |
d19587db IM |
327 | g_assert(exp_sdt.aml_file); |
328 | if (getenv("V")) { | |
fe17cca6 | 329 | fprintf(stderr, "Using expected file '%s'\n", aml_file); |
d19587db | 330 | } |
81eb530d | 331 | ret = g_file_get_contents(aml_file, (gchar **)&exp_sdt.aml, |
acee774b IM |
332 | &aml_len, &error); |
333 | exp_sdt.aml_len = aml_len; | |
9e8458c0 MA |
334 | g_assert(ret); |
335 | g_assert_no_error(error); | |
336 | g_assert(exp_sdt.aml); | |
337 | g_assert(exp_sdt.aml_len); | |
338 | ||
a3a74ab9 | 339 | g_array_append_val(exp_tables, exp_sdt); |
9e8458c0 MA |
340 | } |
341 | ||
a3a74ab9 | 342 | return exp_tables; |
9e8458c0 MA |
343 | } |
344 | ||
ab50f223 MT |
345 | static bool test_acpi_find_diff_allowed(AcpiSdtTable *sdt) |
346 | { | |
347 | const gchar *allowed_diff_file[] = { | |
348 | #include "bios-tables-test-allowed-diff.h" | |
349 | NULL | |
350 | }; | |
351 | const gchar **f; | |
352 | ||
353 | for (f = allowed_diff_file; *f; ++f) { | |
354 | if (!g_strcmp0(sdt->aml_file, *f)) { | |
355 | return true; | |
356 | } | |
357 | } | |
358 | return false; | |
359 | } | |
360 | ||
ab20bbd2 | 361 | /* test the list of tables in @data->tables against reference tables */ |
9e8458c0 MA |
362 | static void test_acpi_asl(test_data *data) |
363 | { | |
364 | int i; | |
365 | AcpiSdtTable *sdt, *exp_sdt; | |
366 | test_data exp_data; | |
df7cafde | 367 | gboolean exp_err, err, all_tables_match = true; |
9e8458c0 MA |
368 | |
369 | memset(&exp_data, 0, sizeof(exp_data)); | |
a3a74ab9 | 370 | exp_data.tables = load_expected_aml(data); |
4500bc98 | 371 | dump_aml_files(data, false); |
a3a74ab9 | 372 | for (i = 0; i < data->tables->len; ++i) { |
9e8458c0 MA |
373 | GString *asl, *exp_asl; |
374 | ||
a3a74ab9 MA |
375 | sdt = &g_array_index(data->tables, AcpiSdtTable, i); |
376 | exp_sdt = &g_array_index(exp_data.tables, AcpiSdtTable, i); | |
9e8458c0 | 377 | |
7f36f093 MT |
378 | if (sdt->aml_len == exp_sdt->aml_len && |
379 | !memcmp(sdt->aml, exp_sdt->aml, sdt->aml_len)) { | |
380 | /* Identical table binaries: no need to disassemble. */ | |
381 | continue; | |
382 | } | |
383 | ||
384 | fprintf(stderr, | |
385 | "acpi-test: Warning! %.4s binary file mismatch. " | |
386 | "Actual [aml:%s], Expected [aml:%s].\n", | |
387 | exp_sdt->aml, sdt->aml_file, exp_sdt->aml_file); | |
388 | ||
389 | all_tables_match = all_tables_match && | |
390 | test_acpi_find_diff_allowed(exp_sdt); | |
391 | ||
7b9829bc IM |
392 | /* |
393 | * don't try to decompile if IASL isn't present, in this case user | |
394 | * will just 'get binary file mismatch' warnings and test failure | |
395 | */ | |
396 | if (!iasl) { | |
397 | continue; | |
398 | } | |
399 | ||
15d914b1 | 400 | err = load_asl(data->tables, sdt); |
9e8458c0 MA |
401 | asl = normalize_asl(sdt->asl); |
402 | ||
15d914b1 | 403 | exp_err = load_asl(exp_data.tables, exp_sdt); |
9e8458c0 MA |
404 | exp_asl = normalize_asl(exp_sdt->asl); |
405 | ||
15d914b1 MA |
406 | /* TODO: check for warnings */ |
407 | g_assert(!err || exp_err); | |
408 | ||
0651596c | 409 | if (g_strcmp0(asl->str, exp_asl->str)) { |
dac23a6c MA |
410 | if (exp_err) { |
411 | fprintf(stderr, | |
412 | "Warning! iasl couldn't parse the expected aml\n"); | |
413 | } else { | |
dac23a6c MA |
414 | sdt->tmp_files_retain = true; |
415 | exp_sdt->tmp_files_retain = true; | |
416 | fprintf(stderr, | |
417 | "acpi-test: Warning! %.4s mismatch. " | |
418 | "Actual [asl:%s, aml:%s], Expected [asl:%s, aml:%s].\n", | |
b137522c | 419 | exp_sdt->aml, sdt->asl_file, sdt->aml_file, |
dac23a6c | 420 | exp_sdt->asl_file, exp_sdt->aml_file); |
7cb08cb2 IM |
421 | if (getenv("V")) { |
422 | const char *diff_cmd = getenv("DIFF"); | |
423 | if (diff_cmd) { | |
424 | int ret G_GNUC_UNUSED; | |
425 | char *diff = g_strdup_printf("%s %s %s", diff_cmd, | |
426 | exp_sdt->asl_file, sdt->asl_file); | |
427 | ret = system(diff) ; | |
428 | g_free(diff); | |
429 | } else { | |
430 | fprintf(stderr, "acpi-test: Warning. not showing " | |
431 | "difference since no diff utility is specified. " | |
432 | "Set 'DIFF' environment variable to a preferred " | |
433 | "diff utility and run 'make V=1 check' again to " | |
434 | "see ASL difference."); | |
435 | } | |
436 | } | |
ab50f223 | 437 | } |
0651596c | 438 | } |
9e8458c0 MA |
439 | g_string_free(asl, true); |
440 | g_string_free(exp_asl, true); | |
441 | } | |
7b9829bc IM |
442 | if (!iasl && !all_tables_match) { |
443 | fprintf(stderr, "to see ASL diff between mismatched files install IASL," | |
444 | " rebuild QEMU from scratch and re-run tests with V=1" | |
445 | " environment variable set"); | |
446 | } | |
df7cafde | 447 | g_assert(all_tables_match); |
9e8458c0 MA |
448 | |
449 | free_test_data(&exp_data); | |
53333801 MA |
450 | } |
451 | ||
5efed5a1 | 452 | static bool smbios_ep_table_ok(test_data *data) |
eb386aac | 453 | { |
86299120 | 454 | struct smbios_21_entry_point *ep_table = &data->smbios_ep_table; |
eb386aac GS |
455 | uint32_t addr = data->smbios_ep_addr; |
456 | ||
3314778d | 457 | qtest_memread(data->qts, addr, ep_table, sizeof(*ep_table)); |
5efed5a1 GS |
458 | if (memcmp(ep_table->anchor_string, "_SM_", 4)) { |
459 | return false; | |
460 | } | |
5efed5a1 GS |
461 | if (memcmp(ep_table->intermediate_anchor_string, "_DMI_", 5)) { |
462 | return false; | |
463 | } | |
5efed5a1 GS |
464 | if (ep_table->structure_table_length == 0) { |
465 | return false; | |
466 | } | |
5efed5a1 GS |
467 | if (ep_table->number_of_structures == 0) { |
468 | return false; | |
469 | } | |
3248f1b4 BW |
470 | if (acpi_calc_checksum((uint8_t *)ep_table, sizeof *ep_table) || |
471 | acpi_calc_checksum((uint8_t *)ep_table + 0x10, | |
472 | sizeof *ep_table - 0x10)) { | |
5efed5a1 GS |
473 | return false; |
474 | } | |
475 | return true; | |
476 | } | |
477 | ||
478 | static void test_smbios_entry_point(test_data *data) | |
479 | { | |
480 | uint32_t off; | |
481 | ||
482 | /* find smbios entry point structure */ | |
483 | for (off = 0xf0000; off < 0x100000; off += 0x10) { | |
484 | uint8_t sig[] = "_SM_"; | |
485 | int i; | |
486 | ||
487 | for (i = 0; i < sizeof sig - 1; ++i) { | |
273e3d92 | 488 | sig[i] = qtest_readb(data->qts, off + i); |
5efed5a1 GS |
489 | } |
490 | ||
491 | if (!memcmp(sig, "_SM_", sizeof sig)) { | |
492 | /* signature match, but is this a valid entry point? */ | |
493 | data->smbios_ep_addr = off; | |
494 | if (smbios_ep_table_ok(data)) { | |
495 | break; | |
496 | } | |
497 | } | |
498 | } | |
499 | ||
500 | g_assert_cmphex(off, <, 0x100000); | |
eb386aac GS |
501 | } |
502 | ||
503 | static inline bool smbios_single_instance(uint8_t type) | |
504 | { | |
505 | switch (type) { | |
506 | case 0: | |
507 | case 1: | |
508 | case 2: | |
509 | case 3: | |
510 | case 16: | |
511 | case 32: | |
512 | case 127: | |
513 | return true; | |
514 | default: | |
515 | return false; | |
516 | } | |
517 | } | |
518 | ||
519 | static void test_smbios_structs(test_data *data) | |
520 | { | |
521 | DECLARE_BITMAP(struct_bitmap, SMBIOS_MAX_TYPE+1) = { 0 }; | |
86299120 | 522 | struct smbios_21_entry_point *ep_table = &data->smbios_ep_table; |
3831c07b | 523 | uint32_t addr = le32_to_cpu(ep_table->structure_table_address); |
eb386aac GS |
524 | int i, len, max_len = 0; |
525 | uint8_t type, prv, crt; | |
eb386aac GS |
526 | |
527 | /* walk the smbios tables */ | |
3831c07b | 528 | for (i = 0; i < le16_to_cpu(ep_table->number_of_structures); i++) { |
eb386aac GS |
529 | |
530 | /* grab type and formatted area length from struct header */ | |
273e3d92 | 531 | type = qtest_readb(data->qts, addr); |
eb386aac | 532 | g_assert_cmpuint(type, <=, SMBIOS_MAX_TYPE); |
273e3d92 | 533 | len = qtest_readb(data->qts, addr + 1); |
eb386aac GS |
534 | |
535 | /* single-instance structs must not have been encountered before */ | |
536 | if (smbios_single_instance(type)) { | |
537 | g_assert(!test_bit(type, struct_bitmap)); | |
538 | } | |
539 | set_bit(type, struct_bitmap); | |
540 | ||
541 | /* seek to end of unformatted string area of this struct ("\0\0") */ | |
542 | prv = crt = 1; | |
543 | while (prv || crt) { | |
544 | prv = crt; | |
273e3d92 | 545 | crt = qtest_readb(data->qts, addr + len); |
eb386aac GS |
546 | len++; |
547 | } | |
548 | ||
549 | /* keep track of max. struct size */ | |
550 | if (max_len < len) { | |
551 | max_len = len; | |
552 | g_assert_cmpuint(max_len, <=, ep_table->max_structure_size); | |
553 | } | |
554 | ||
555 | /* start of next structure */ | |
556 | addr += len; | |
557 | } | |
558 | ||
559 | /* total table length and max struct size must match entry point values */ | |
3831c07b TH |
560 | g_assert_cmpuint(le16_to_cpu(ep_table->structure_table_length), ==, |
561 | addr - le32_to_cpu(ep_table->structure_table_address)); | |
562 | g_assert_cmpuint(le16_to_cpu(ep_table->max_structure_size), ==, max_len); | |
eb386aac GS |
563 | |
564 | /* required struct types must all be present */ | |
f4eda2d4 CM |
565 | for (i = 0; i < data->required_struct_types_len; i++) { |
566 | g_assert(test_bit(data->required_struct_types[i], struct_bitmap)); | |
eb386aac GS |
567 | } |
568 | } | |
569 | ||
8ac2adf7 | 570 | static void test_acpi_one(const char *params, test_data *data) |
ad6423a7 MT |
571 | { |
572 | char *args; | |
97256e79 IM |
573 | bool use_uefi = data->uefi_fl1 && data->uefi_fl2; |
574 | ||
575 | if (use_uefi) { | |
576 | /* | |
577 | * TODO: convert '-drive if=pflash' to new syntax (see e33763be7cd3) | |
578 | * when arm/virt boad starts to support it. | |
579 | */ | |
580 | args = g_strdup_printf("-machine %s,accel=%s -nodefaults -nographic " | |
581 | "-drive if=pflash,format=raw,file=%s,readonly " | |
582 | "-drive if=pflash,format=raw,file=%s,snapshot=on -cdrom %s %s", | |
3dc01874 IM |
583 | data->machine, data->accel ? data->accel : "kvm:tcg", |
584 | data->uefi_fl1, data->uefi_fl2, data->cd, params ? params : ""); | |
97256e79 IM |
585 | |
586 | } else { | |
587 | /* Disable kernel irqchip to be able to override apic irq0. */ | |
588 | args = g_strdup_printf("-machine %s,accel=%s,kernel-irqchip=off " | |
589 | "-net none -display none %s " | |
590 | "-drive id=hd0,if=none,file=%s,format=raw " | |
591 | "-device ide-hd,drive=hd0 ", | |
3dc01874 IM |
592 | data->machine, data->accel ? data->accel : "kvm:tcg", |
593 | params ? params : "", disk); | |
97256e79 | 594 | } |
9e8458c0 | 595 | |
273e3d92 | 596 | data->qts = qtest_init(args); |
ad6423a7 | 597 | |
97256e79 IM |
598 | if (use_uefi) { |
599 | g_assert(data->scan_len); | |
600 | data->rsdp_addr = acpi_find_rsdp_address_uefi(data->qts, | |
601 | data->ram_start, data->scan_len); | |
602 | } else { | |
603 | boot_sector_test(data->qts); | |
604 | data->rsdp_addr = acpi_find_rsdp_address(data->qts); | |
605 | g_assert_cmphex(data->rsdp_addr, <, 0x100000); | |
606 | } | |
ad6423a7 | 607 | |
b24b9d94 | 608 | data->tables = g_array_new(false, true, sizeof(AcpiSdtTable)); |
8ac2adf7 | 609 | test_acpi_rsdp_table(data); |
f2f616ce | 610 | test_acpi_rxsdt_table(data); |
db575449 | 611 | test_acpi_fadt_table(data); |
ad6423a7 | 612 | |
ab31b337 IM |
613 | if (getenv(ACPI_REBUILD_EXPECTED_AML)) { |
614 | dump_aml_files(data, true); | |
7b9829bc | 615 | } else { |
ab31b337 | 616 | test_acpi_asl(data); |
9e8458c0 MA |
617 | } |
618 | ||
ce513b46 IM |
619 | /* |
620 | * TODO: make SMBIOS tests work with UEFI firmware, | |
621 | * Bug on uefi-test-tools to provide entry point: | |
622 | * https://bugs.launchpad.net/qemu/+bug/1821884 | |
623 | */ | |
624 | if (!use_uefi) { | |
625 | test_smbios_entry_point(data); | |
626 | test_smbios_structs(data); | |
627 | } | |
eb386aac | 628 | |
273e3d92 | 629 | qtest_quit(data->qts); |
ad6423a7 MT |
630 | g_free(args); |
631 | } | |
632 | ||
f4eda2d4 CM |
633 | static uint8_t base_required_struct_types[] = { |
634 | 0, 1, 3, 4, 16, 17, 19, 32, 127 | |
635 | }; | |
636 | ||
71f4be25 | 637 | static void test_acpi_piix4_tcg(void) |
ad6423a7 | 638 | { |
8ac2adf7 MA |
639 | test_data data; |
640 | ||
ad6423a7 MT |
641 | /* Supplying -machine accel argument overrides the default (qtest). |
642 | * This is to make guest actually run. | |
643 | */ | |
9e8458c0 MA |
644 | memset(&data, 0, sizeof(data)); |
645 | data.machine = MACHINE_PC; | |
f4eda2d4 CM |
646 | data.required_struct_types = base_required_struct_types; |
647 | data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types); | |
947b205f | 648 | test_acpi_one(NULL, &data); |
9e8458c0 | 649 | free_test_data(&data); |
71f4be25 PB |
650 | } |
651 | ||
3a9c86df IM |
652 | static void test_acpi_piix4_tcg_bridge(void) |
653 | { | |
654 | test_data data; | |
655 | ||
656 | memset(&data, 0, sizeof(data)); | |
657 | data.machine = MACHINE_PC; | |
658 | data.variant = ".bridge"; | |
f4eda2d4 CM |
659 | data.required_struct_types = base_required_struct_types; |
660 | data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types); | |
947b205f | 661 | test_acpi_one("-device pci-bridge,chassis_nr=1", &data); |
3a9c86df IM |
662 | free_test_data(&data); |
663 | } | |
664 | ||
71f4be25 PB |
665 | static void test_acpi_q35_tcg(void) |
666 | { | |
667 | test_data data; | |
8ac2adf7 | 668 | |
9e8458c0 MA |
669 | memset(&data, 0, sizeof(data)); |
670 | data.machine = MACHINE_Q35; | |
f4eda2d4 CM |
671 | data.required_struct_types = base_required_struct_types; |
672 | data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types); | |
947b205f | 673 | test_acpi_one(NULL, &data); |
8ac2adf7 | 674 | free_test_data(&data); |
ad6423a7 MT |
675 | } |
676 | ||
3a9c86df IM |
677 | static void test_acpi_q35_tcg_bridge(void) |
678 | { | |
679 | test_data data; | |
680 | ||
681 | memset(&data, 0, sizeof(data)); | |
682 | data.machine = MACHINE_Q35; | |
683 | data.variant = ".bridge"; | |
f4eda2d4 CM |
684 | data.required_struct_types = base_required_struct_types; |
685 | data.required_struct_types_len = ARRAY_SIZE(base_required_struct_types); | |
947b205f | 686 | test_acpi_one("-device pci-bridge,chassis_nr=1", |
3a9c86df IM |
687 | &data); |
688 | free_test_data(&data); | |
689 | } | |
690 | ||
0259e966 LE |
691 | static void test_acpi_q35_tcg_mmio64(void) |
692 | { | |
693 | test_data data = { | |
694 | .machine = MACHINE_Q35, | |
695 | .variant = ".mmio64", | |
696 | .required_struct_types = base_required_struct_types, | |
697 | .required_struct_types_len = ARRAY_SIZE(base_required_struct_types) | |
698 | }; | |
699 | ||
700 | test_acpi_one("-m 128M,slots=1,maxmem=2G " | |
af135030 IM |
701 | "-object memory-backend-ram,id=ram0,size=128M " |
702 | "-numa node,memdev=ram0 " | |
0259e966 LE |
703 | "-device pci-testdev,membar=2G", |
704 | &data); | |
705 | free_test_data(&data); | |
706 | } | |
707 | ||
6b9c1dd2 IM |
708 | static void test_acpi_piix4_tcg_cphp(void) |
709 | { | |
710 | test_data data; | |
711 | ||
712 | memset(&data, 0, sizeof(data)); | |
713 | data.machine = MACHINE_PC; | |
714 | data.variant = ".cphp"; | |
d6309c17 | 715 | test_acpi_one("-smp 2,cores=3,sockets=2,maxcpus=6" |
af135030 IM |
716 | " -object memory-backend-ram,id=ram0,size=64M" |
717 | " -object memory-backend-ram,id=ram1,size=64M" | |
718 | " -numa node,memdev=ram0 -numa node,memdev=ram1" | |
fda4096f | 719 | " -numa dist,src=0,dst=1,val=21", |
6b9c1dd2 IM |
720 | &data); |
721 | free_test_data(&data); | |
722 | } | |
723 | ||
724 | static void test_acpi_q35_tcg_cphp(void) | |
725 | { | |
726 | test_data data; | |
727 | ||
728 | memset(&data, 0, sizeof(data)); | |
729 | data.machine = MACHINE_Q35; | |
730 | data.variant = ".cphp"; | |
d6309c17 | 731 | test_acpi_one(" -smp 2,cores=3,sockets=2,maxcpus=6" |
af135030 IM |
732 | " -object memory-backend-ram,id=ram0,size=64M" |
733 | " -object memory-backend-ram,id=ram1,size=64M" | |
734 | " -numa node,memdev=ram0 -numa node,memdev=ram1" | |
fda4096f | 735 | " -numa dist,src=0,dst=1,val=21", |
6b9c1dd2 IM |
736 | &data); |
737 | free_test_data(&data); | |
738 | } | |
739 | ||
f4eda2d4 CM |
740 | static uint8_t ipmi_required_struct_types[] = { |
741 | 0, 1, 3, 4, 16, 17, 19, 32, 38, 127 | |
742 | }; | |
743 | ||
744 | static void test_acpi_q35_tcg_ipmi(void) | |
745 | { | |
746 | test_data data; | |
747 | ||
748 | memset(&data, 0, sizeof(data)); | |
749 | data.machine = MACHINE_Q35; | |
750 | data.variant = ".ipmibt"; | |
751 | data.required_struct_types = ipmi_required_struct_types; | |
752 | data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types); | |
947b205f | 753 | test_acpi_one("-device ipmi-bmc-sim,id=bmc0" |
f4eda2d4 CM |
754 | " -device isa-ipmi-bt,bmc=bmc0", |
755 | &data); | |
756 | free_test_data(&data); | |
757 | } | |
758 | ||
759 | static void test_acpi_piix4_tcg_ipmi(void) | |
760 | { | |
761 | test_data data; | |
762 | ||
763 | /* Supplying -machine accel argument overrides the default (qtest). | |
764 | * This is to make guest actually run. | |
765 | */ | |
766 | memset(&data, 0, sizeof(data)); | |
767 | data.machine = MACHINE_PC; | |
768 | data.variant = ".ipmikcs"; | |
769 | data.required_struct_types = ipmi_required_struct_types; | |
770 | data.required_struct_types_len = ARRAY_SIZE(ipmi_required_struct_types); | |
947b205f | 771 | test_acpi_one("-device ipmi-bmc-sim,id=bmc0" |
f4eda2d4 CM |
772 | " -device isa-ipmi-kcs,irq=0,bmc=bmc0", |
773 | &data); | |
774 | free_test_data(&data); | |
775 | } | |
776 | ||
4462fc60 IM |
777 | static void test_acpi_q35_tcg_memhp(void) |
778 | { | |
779 | test_data data; | |
780 | ||
781 | memset(&data, 0, sizeof(data)); | |
782 | data.machine = MACHINE_Q35; | |
783 | data.variant = ".memhp"; | |
fda4096f | 784 | test_acpi_one(" -m 128,slots=3,maxmem=1G" |
af135030 IM |
785 | " -object memory-backend-ram,id=ram0,size=64M" |
786 | " -object memory-backend-ram,id=ram1,size=64M" | |
787 | " -numa node,memdev=ram0 -numa node,memdev=ram1" | |
fda4096f HC |
788 | " -numa dist,src=0,dst=1,val=21", |
789 | &data); | |
4462fc60 IM |
790 | free_test_data(&data); |
791 | } | |
792 | ||
793 | static void test_acpi_piix4_tcg_memhp(void) | |
794 | { | |
795 | test_data data; | |
796 | ||
797 | memset(&data, 0, sizeof(data)); | |
798 | data.machine = MACHINE_PC; | |
799 | data.variant = ".memhp"; | |
fda4096f | 800 | test_acpi_one(" -m 128,slots=3,maxmem=1G" |
af135030 IM |
801 | " -object memory-backend-ram,id=ram0,size=64M" |
802 | " -object memory-backend-ram,id=ram1,size=64M" | |
803 | " -numa node,memdev=ram0 -numa node,memdev=ram1" | |
fda4096f HC |
804 | " -numa dist,src=0,dst=1,val=21", |
805 | &data); | |
4462fc60 IM |
806 | free_test_data(&data); |
807 | } | |
808 | ||
d82c4f82 DL |
809 | static void test_acpi_q35_tcg_numamem(void) |
810 | { | |
811 | test_data data; | |
812 | ||
813 | memset(&data, 0, sizeof(data)); | |
814 | data.machine = MACHINE_Q35; | |
815 | data.variant = ".numamem"; | |
af135030 IM |
816 | test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M" |
817 | " -numa node -numa node,memdev=ram0", &data); | |
d82c4f82 DL |
818 | free_test_data(&data); |
819 | } | |
820 | ||
821 | static void test_acpi_piix4_tcg_numamem(void) | |
822 | { | |
823 | test_data data; | |
824 | ||
825 | memset(&data, 0, sizeof(data)); | |
826 | data.machine = MACHINE_PC; | |
827 | data.variant = ".numamem"; | |
af135030 IM |
828 | test_acpi_one(" -object memory-backend-ram,id=ram0,size=128M" |
829 | " -numa node -numa node,memdev=ram0", &data); | |
d82c4f82 DL |
830 | free_test_data(&data); |
831 | } | |
832 | ||
adae91ce HZ |
833 | static void test_acpi_tcg_dimm_pxm(const char *machine) |
834 | { | |
835 | test_data data; | |
836 | ||
837 | memset(&data, 0, sizeof(data)); | |
838 | data.machine = machine; | |
839 | data.variant = ".dimmpxm"; | |
11c39b5c | 840 | test_acpi_one(" -machine nvdimm=on,nvdimm-persistence=cpu" |
adae91ce HZ |
841 | " -smp 4,sockets=4" |
842 | " -m 128M,slots=3,maxmem=1G" | |
af135030 IM |
843 | " -object memory-backend-ram,id=ram0,size=32M" |
844 | " -object memory-backend-ram,id=ram1,size=32M" | |
845 | " -object memory-backend-ram,id=ram2,size=32M" | |
846 | " -object memory-backend-ram,id=ram3,size=32M" | |
847 | " -numa node,memdev=ram0,nodeid=0" | |
848 | " -numa node,memdev=ram1,nodeid=1" | |
849 | " -numa node,memdev=ram2,nodeid=2" | |
850 | " -numa node,memdev=ram3,nodeid=3" | |
adae91ce HZ |
851 | " -numa cpu,node-id=0,socket-id=0" |
852 | " -numa cpu,node-id=1,socket-id=1" | |
853 | " -numa cpu,node-id=2,socket-id=2" | |
854 | " -numa cpu,node-id=3,socket-id=3" | |
af135030 | 855 | " -object memory-backend-ram,id=ram4,size=128M" |
adae91ce | 856 | " -object memory-backend-ram,id=nvm0,size=128M" |
af135030 | 857 | " -device pc-dimm,id=dimm0,memdev=ram4,node=1" |
adae91ce HZ |
858 | " -device nvdimm,id=dimm1,memdev=nvm0,node=2", |
859 | &data); | |
860 | free_test_data(&data); | |
861 | } | |
862 | ||
863 | static void test_acpi_q35_tcg_dimm_pxm(void) | |
864 | { | |
865 | test_acpi_tcg_dimm_pxm(MACHINE_Q35); | |
866 | } | |
867 | ||
868 | static void test_acpi_piix4_tcg_dimm_pxm(void) | |
869 | { | |
870 | test_acpi_tcg_dimm_pxm(MACHINE_PC); | |
871 | } | |
872 | ||
ab6b6a77 IM |
873 | static void test_acpi_virt_tcg(void) |
874 | { | |
875 | test_data data = { | |
876 | .machine = "virt", | |
877 | .accel = "tcg", | |
878 | .uefi_fl1 = "pc-bios/edk2-aarch64-code.fd", | |
879 | .uefi_fl2 = "pc-bios/edk2-arm-vars.fd", | |
880 | .cd = "tests/data/uefi-boot-images/bios-tables-test.aarch64.iso.qcow2", | |
881 | .ram_start = 0x40000000ULL, | |
882 | .scan_len = 128ULL * 1024 * 1024, | |
883 | }; | |
884 | ||
885 | test_acpi_one("-cpu cortex-a57", &data); | |
886 | free_test_data(&data); | |
887 | } | |
888 | ||
ad6423a7 MT |
889 | int main(int argc, char *argv[]) |
890 | { | |
891 | const char *arch = qtest_get_arch(); | |
5862ad0f | 892 | int ret; |
c39a28a4 | 893 | |
ad6423a7 MT |
894 | g_test_init(&argc, &argv, NULL); |
895 | ||
896 | if (strcmp(arch, "i386") == 0 || strcmp(arch, "x86_64") == 0) { | |
d47a5d64 IM |
897 | ret = boot_sector_init(disk); |
898 | if (ret) { | |
899 | return ret; | |
900 | } | |
901 | ||
947b205f MA |
902 | qtest_add_func("acpi/piix4", test_acpi_piix4_tcg); |
903 | qtest_add_func("acpi/piix4/bridge", test_acpi_piix4_tcg_bridge); | |
904 | qtest_add_func("acpi/q35", test_acpi_q35_tcg); | |
905 | qtest_add_func("acpi/q35/bridge", test_acpi_q35_tcg_bridge); | |
0259e966 | 906 | qtest_add_func("acpi/q35/mmio64", test_acpi_q35_tcg_mmio64); |
947b205f MA |
907 | qtest_add_func("acpi/piix4/ipmi", test_acpi_piix4_tcg_ipmi); |
908 | qtest_add_func("acpi/q35/ipmi", test_acpi_q35_tcg_ipmi); | |
909 | qtest_add_func("acpi/piix4/cpuhp", test_acpi_piix4_tcg_cphp); | |
910 | qtest_add_func("acpi/q35/cpuhp", test_acpi_q35_tcg_cphp); | |
4462fc60 IM |
911 | qtest_add_func("acpi/piix4/memhp", test_acpi_piix4_tcg_memhp); |
912 | qtest_add_func("acpi/q35/memhp", test_acpi_q35_tcg_memhp); | |
d82c4f82 DL |
913 | qtest_add_func("acpi/piix4/numamem", test_acpi_piix4_tcg_numamem); |
914 | qtest_add_func("acpi/q35/numamem", test_acpi_q35_tcg_numamem); | |
adae91ce HZ |
915 | qtest_add_func("acpi/piix4/dimmpxm", test_acpi_piix4_tcg_dimm_pxm); |
916 | qtest_add_func("acpi/q35/dimmpxm", test_acpi_q35_tcg_dimm_pxm); | |
ab6b6a77 IM |
917 | } else if (strcmp(arch, "aarch64") == 0) { |
918 | qtest_add_func("acpi/virt", test_acpi_virt_tcg); | |
ad6423a7 | 919 | } |
5862ad0f | 920 | ret = g_test_run(); |
4e082566 | 921 | boot_sector_cleanup(disk); |
5862ad0f | 922 | return ret; |
ad6423a7 | 923 | } |