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