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