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