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