xref: /freebsd/usr.sbin/bhyve/acpi.c (revision 3494f7c0)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2012 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * bhyve ACPI table generator.
31  *
32  * Create the minimal set of ACPI tables required to boot FreeBSD (and
33  * hopefully other o/s's).
34  *
35  * The tables are placed in the guest's ROM area just below 1MB physical,
36  * above the MPTable.
37  */
38 
39 #include <sys/param.h>
40 #include <sys/endian.h>
41 #include <sys/errno.h>
42 #include <sys/stat.h>
43 
44 #include <err.h>
45 #include <paths.h>
46 #include <stdarg.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52 
53 #include <machine/vmm.h>
54 #include <vmmapi.h>
55 
56 #include "bhyverun.h"
57 #include "acpi.h"
58 #include "basl.h"
59 #include "pci_emul.h"
60 #include "vmgenc.h"
61 
62 #define	BHYVE_ASL_TEMPLATE	"bhyve.XXXXXXX"
63 #define BHYVE_ASL_SUFFIX	".aml"
64 #define BHYVE_ASL_COMPILER	"/usr/sbin/iasl"
65 
66 #define BHYVE_ADDRESS_IOAPIC 	0xFEC00000
67 #define BHYVE_ADDRESS_HPET 	0xFED00000
68 #define BHYVE_ADDRESS_LAPIC 	0xFEE00000
69 
70 static int basl_keep_temps;
71 static int basl_verbose_iasl;
72 static int basl_ncpu;
73 
74 /*
75  * Contains the full pathname of the template to be passed
76  * to mkstemp/mktemps(3)
77  */
78 static char basl_template[MAXPATHLEN];
79 static char basl_stemplate[MAXPATHLEN];
80 
81 /*
82  * State for dsdt_line(), dsdt_indent(), and dsdt_unindent().
83  */
84 static FILE *dsdt_fp;
85 static int dsdt_indent_level;
86 static int dsdt_error;
87 
88 struct basl_fio {
89 	int	fd;
90 	FILE	*fp;
91 	char	f_name[MAXPATHLEN];
92 };
93 
94 #define EFPRINTF(...) \
95 	if (fprintf(__VA_ARGS__) < 0) goto err_exit
96 
97 #define EFFLUSH(x) \
98 	if (fflush(x) != 0) goto err_exit
99 
100 /*
101  * A list for additional ACPI devices like a TPM.
102  */
103 struct acpi_device_list_entry {
104 	SLIST_ENTRY(acpi_device_list_entry) chain;
105 	const struct acpi_device *dev;
106 };
107 static SLIST_HEAD(acpi_device_list,
108     acpi_device_list_entry) acpi_devices = SLIST_HEAD_INITIALIZER(acpi_devices);
109 
110 int
111 acpi_tables_add_device(const struct acpi_device *const dev)
112 {
113 	struct acpi_device_list_entry *const entry = calloc(1, sizeof(*entry));
114 	if (entry == NULL) {
115 		return (ENOMEM);
116 	}
117 
118 	entry->dev = dev;
119 	SLIST_INSERT_HEAD(&acpi_devices, entry, chain);
120 
121 	return (0);
122 }
123 
124 /*
125  * Helper routines for writing to the DSDT from other modules.
126  */
127 void
128 dsdt_line(const char *fmt, ...)
129 {
130 	va_list ap;
131 
132 	if (dsdt_error != 0)
133 		return;
134 
135 	if (strcmp(fmt, "") != 0) {
136 		if (dsdt_indent_level != 0)
137 			EFPRINTF(dsdt_fp, "%*c", dsdt_indent_level * 2, ' ');
138 		va_start(ap, fmt);
139 		if (vfprintf(dsdt_fp, fmt, ap) < 0) {
140 			va_end(ap);
141 			goto err_exit;
142 		}
143 		va_end(ap);
144 	}
145 	EFPRINTF(dsdt_fp, "\n");
146 	return;
147 
148 err_exit:
149 	dsdt_error = errno;
150 }
151 
152 void
153 dsdt_indent(int levels)
154 {
155 
156 	dsdt_indent_level += levels;
157 	assert(dsdt_indent_level >= 0);
158 }
159 
160 void
161 dsdt_unindent(int levels)
162 {
163 
164 	assert(dsdt_indent_level >= levels);
165 	dsdt_indent_level -= levels;
166 }
167 
168 void
169 dsdt_fixed_ioport(uint16_t iobase, uint16_t length)
170 {
171 
172 	dsdt_line("IO (Decode16,");
173 	dsdt_line("  0x%04X,             // Range Minimum", iobase);
174 	dsdt_line("  0x%04X,             // Range Maximum", iobase);
175 	dsdt_line("  0x01,               // Alignment");
176 	dsdt_line("  0x%02X,               // Length", length);
177 	dsdt_line("  )");
178 }
179 
180 void
181 dsdt_fixed_irq(uint8_t irq)
182 {
183 
184 	dsdt_line("IRQNoFlags ()");
185 	dsdt_line("  {%d}", irq);
186 }
187 
188 void
189 dsdt_fixed_mem32(uint32_t base, uint32_t length)
190 {
191 
192 	dsdt_line("Memory32Fixed (ReadWrite,");
193 	dsdt_line("  0x%08X,         // Address Base", base);
194 	dsdt_line("  0x%08X,         // Address Length", length);
195 	dsdt_line("  )");
196 }
197 
198 static int
199 basl_fwrite_dsdt(FILE *fp)
200 {
201 	dsdt_fp = fp;
202 	dsdt_error = 0;
203 	dsdt_indent_level = 0;
204 
205 	dsdt_line("/*");
206 	dsdt_line(" * bhyve DSDT template");
207 	dsdt_line(" */");
208 	dsdt_line("DefinitionBlock (\"bhyve_dsdt.aml\", \"DSDT\", 2,"
209 		 "\"BHYVE \", \"BVDSDT  \", 0x00000001)");
210 	dsdt_line("{");
211 	dsdt_line("  Name (_S5, Package ()");
212 	dsdt_line("  {");
213 	dsdt_line("      0x05,");
214 	dsdt_line("      Zero,");
215 	dsdt_line("  })");
216 
217 	pci_write_dsdt();
218 
219 #ifdef __amd64__
220 	dsdt_line("");
221 	dsdt_line("  Scope (_SB.PC00)");
222 	dsdt_line("  {");
223 	dsdt_line("    Device (HPET)");
224 	dsdt_line("    {");
225 	dsdt_line("      Name (_HID, EISAID(\"PNP0103\"))");
226 	dsdt_line("      Name (_UID, 0)");
227 	dsdt_line("      Name (_CRS, ResourceTemplate ()");
228 	dsdt_line("      {");
229 	dsdt_indent(4);
230 	dsdt_fixed_mem32(0xFED00000, 0x400);
231 	dsdt_unindent(4);
232 	dsdt_line("      })");
233 	dsdt_line("    }");
234 	dsdt_line("  }");
235 #endif
236 
237 	vmgenc_write_dsdt();
238 
239 	const struct acpi_device_list_entry *entry;
240 	SLIST_FOREACH(entry, &acpi_devices, chain) {
241 		BASL_EXEC(acpi_device_write_dsdt(entry->dev));
242 	}
243 
244 	dsdt_line("}");
245 
246 	if (dsdt_error != 0)
247 		return (dsdt_error);
248 
249 	EFFLUSH(fp);
250 
251 	return (0);
252 
253 err_exit:
254 	return (errno);
255 }
256 
257 static int
258 basl_open(struct basl_fio *bf, int suffix)
259 {
260 	int err;
261 
262 	err = 0;
263 
264 	if (suffix) {
265 		strlcpy(bf->f_name, basl_stemplate, MAXPATHLEN);
266 		bf->fd = mkstemps(bf->f_name, strlen(BHYVE_ASL_SUFFIX));
267 	} else {
268 		strlcpy(bf->f_name, basl_template, MAXPATHLEN);
269 		bf->fd = mkstemp(bf->f_name);
270 	}
271 
272 	if (bf->fd > 0) {
273 		bf->fp = fdopen(bf->fd, "w+");
274 		if (bf->fp == NULL) {
275 			unlink(bf->f_name);
276 			close(bf->fd);
277 		}
278 	} else {
279 		err = 1;
280 	}
281 
282 	return (err);
283 }
284 
285 static void
286 basl_close(struct basl_fio *bf)
287 {
288 
289 	if (!basl_keep_temps)
290 		unlink(bf->f_name);
291 	fclose(bf->fp);
292 }
293 
294 static int
295 basl_start(struct basl_fio *in, struct basl_fio *out)
296 {
297 	int err;
298 
299 	err = basl_open(in, 0);
300 	if (!err) {
301 		err = basl_open(out, 1);
302 		if (err) {
303 			basl_close(in);
304 		}
305 	}
306 
307 	return (err);
308 }
309 
310 static void
311 basl_end(struct basl_fio *in, struct basl_fio *out)
312 {
313 
314 	basl_close(in);
315 	basl_close(out);
316 }
317 
318 static int
319 basl_load(struct vmctx *ctx, int fd)
320 {
321 	struct stat sb;
322 	void *addr;
323 
324 	if (fstat(fd, &sb) < 0)
325 		return (errno);
326 
327 	addr = calloc(1, sb.st_size);
328 	if (addr == NULL)
329 		return (EFAULT);
330 
331 	if (read(fd, addr, sb.st_size) < 0)
332 		return (errno);
333 
334 	struct basl_table *table;
335 
336 	uint8_t name[ACPI_NAMESEG_SIZE + 1] = { 0 };
337 	memcpy(name, addr, sizeof(name) - 1 /* last char is '\0' */);
338 	BASL_EXEC(basl_table_create(&table, ctx, name, BASL_TABLE_ALIGNMENT));
339 	BASL_EXEC(basl_table_append_bytes(table, addr, sb.st_size));
340 
341 	return (0);
342 }
343 
344 static int
345 basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *))
346 {
347 	struct basl_fio io[2];
348 	static char iaslbuf[3*MAXPATHLEN + 10];
349 	const char *fmt;
350 	int err;
351 
352 	err = basl_start(&io[0], &io[1]);
353 	if (!err) {
354 		err = (*fwrite_section)(io[0].fp);
355 
356 		if (!err) {
357 			/*
358 			 * iasl sends the results of the compilation to
359 			 * stdout. Shut this down by using the shell to
360 			 * redirect stdout to /dev/null, unless the user
361 			 * has requested verbose output for debugging
362 			 * purposes
363 			 */
364 			fmt = basl_verbose_iasl ?
365 				"%s -p %s %s" :
366 				"/bin/sh -c \"%s -p %s %s\" 1> /dev/null";
367 
368 			snprintf(iaslbuf, sizeof(iaslbuf),
369 				 fmt,
370 				 BHYVE_ASL_COMPILER,
371 				 io[1].f_name, io[0].f_name);
372 			err = system(iaslbuf);
373 
374 			if (!err) {
375 				/*
376 				 * Copy the aml output file into guest
377 				 * memory at the specified location
378 				 */
379 				err = basl_load(ctx, io[1].fd);
380 			}
381 		}
382 		basl_end(&io[0], &io[1]);
383 	}
384 
385 	return (err);
386 }
387 
388 static int
389 basl_make_templates(void)
390 {
391 	const char *tmpdir;
392 	int err;
393 	int len;
394 
395 	err = 0;
396 
397 	/*
398 	 *
399 	 */
400 	if ((tmpdir = getenv("BHYVE_TMPDIR")) == NULL || *tmpdir == '\0' ||
401 	    (tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') {
402 		tmpdir = _PATH_TMP;
403 	}
404 
405 	len = strlen(tmpdir);
406 
407 	if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) {
408 		strcpy(basl_template, tmpdir);
409 		while (len > 0 && basl_template[len - 1] == '/')
410 			len--;
411 		basl_template[len] = '/';
412 		strcpy(&basl_template[len + 1], BHYVE_ASL_TEMPLATE);
413 	} else
414 		err = E2BIG;
415 
416 	if (!err) {
417 		/*
418 		 * len has been initialized (and maybe adjusted) above
419 		 */
420 		if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1 +
421 		     sizeof(BHYVE_ASL_SUFFIX)) < MAXPATHLEN) {
422 			strcpy(basl_stemplate, tmpdir);
423 			basl_stemplate[len] = '/';
424 			strcpy(&basl_stemplate[len + 1], BHYVE_ASL_TEMPLATE);
425 			len = strlen(basl_stemplate);
426 			strcpy(&basl_stemplate[len], BHYVE_ASL_SUFFIX);
427 		} else
428 			err = E2BIG;
429 	}
430 
431 	return (err);
432 }
433 
434 static int
435 build_dsdt(struct vmctx *const ctx)
436 {
437 	BASL_EXEC(basl_compile(ctx, basl_fwrite_dsdt));
438 
439 	return (0);
440 }
441 
442 static int
443 build_facs(struct vmctx *const ctx)
444 {
445 	ACPI_TABLE_FACS facs;
446 	struct basl_table *table;
447 
448 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_FACS,
449 	    BASL_TABLE_ALIGNMENT_FACS));
450 
451 	memset(&facs, 0, sizeof(facs));
452 	memcpy(facs.Signature, ACPI_SIG_FACS, ACPI_NAMESEG_SIZE);
453 	facs.Length = sizeof(facs);
454 	facs.Version = htole32(2);
455 	BASL_EXEC(basl_table_append_bytes(table, &facs, sizeof(facs)));
456 
457 	return (0);
458 }
459 
460 static int
461 build_fadt(struct vmctx *const ctx)
462 {
463 	ACPI_TABLE_FADT fadt;
464 	struct basl_table *table;
465 
466 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_FADT,
467 	    BASL_TABLE_ALIGNMENT));
468 
469 	memset(&fadt, 0, sizeof(fadt));
470 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_FADT, 5, 1));
471 	fadt.Facs = htole32(0); /* patched by basl */
472 	fadt.Dsdt = htole32(0); /* patched by basl */
473 	fadt.SciInterrupt = htole16(SCI_INT);
474 	fadt.SmiCommand = htole32(SMI_CMD);
475 	fadt.AcpiEnable = BHYVE_ACPI_ENABLE;
476 	fadt.AcpiDisable = BHYVE_ACPI_DISABLE;
477 	fadt.Pm1aEventBlock = htole32(PM1A_EVT_ADDR);
478 	fadt.Pm1aControlBlock = htole32(PM1A_CNT_ADDR);
479 	fadt.PmTimerBlock = htole32(IO_PMTMR);
480 	fadt.Gpe0Block = htole32(IO_GPE0_BLK);
481 	fadt.Pm1EventLength = 4;
482 	fadt.Pm1ControlLength = 2;
483 	fadt.PmTimerLength = 4;
484 	fadt.Gpe0BlockLength = IO_GPE0_LEN;
485 	fadt.Century = 0x32;
486 	fadt.BootFlags = htole16(ACPI_FADT_NO_VGA | ACPI_FADT_NO_ASPM);
487 	fadt.Flags = htole32(ACPI_FADT_WBINVD | ACPI_FADT_C1_SUPPORTED |
488 	    ACPI_FADT_SLEEP_BUTTON | ACPI_FADT_32BIT_TIMER |
489 	    ACPI_FADT_RESET_REGISTER | ACPI_FADT_HEADLESS |
490 	    ACPI_FADT_APIC_PHYSICAL);
491 	basl_fill_gas(&fadt.ResetRegister, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
492 	    ACPI_GAS_ACCESS_WIDTH_BYTE, 0xCF9);
493 	fadt.ResetValue = 6;
494 	fadt.MinorRevision = 1;
495 	fadt.XFacs = htole64(0); /* patched by basl */
496 	fadt.XDsdt = htole64(0); /* patched by basl */
497 	basl_fill_gas(&fadt.XPm1aEventBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0x20, 0,
498 	    ACPI_GAS_ACCESS_WIDTH_WORD, PM1A_EVT_ADDR);
499 	basl_fill_gas(&fadt.XPm1bEventBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0, 0,
500 	    ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
501 	basl_fill_gas(&fadt.XPm1aControlBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0x10,
502 	    0, ACPI_GAS_ACCESS_WIDTH_WORD, PM1A_CNT_ADDR);
503 	basl_fill_gas(&fadt.XPm1bControlBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0, 0,
504 	    ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
505 	basl_fill_gas(&fadt.XPm2ControlBlock, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
506 	    ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
507 	basl_fill_gas(&fadt.XPmTimerBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0x20, 0,
508 	    ACPI_GAS_ACCESS_WIDTH_DWORD, IO_PMTMR);
509 	basl_fill_gas(&fadt.XGpe0Block, ACPI_ADR_SPACE_SYSTEM_IO,
510 	    IO_GPE0_LEN * 8, 0, ACPI_GAS_ACCESS_WIDTH_BYTE, IO_GPE0_BLK);
511 	basl_fill_gas(&fadt.XGpe1Block, ACPI_ADR_SPACE_SYSTEM_IO, 0, 0,
512 	    ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
513 	basl_fill_gas(&fadt.SleepControl, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
514 	    ACPI_GAS_ACCESS_WIDTH_BYTE, 0);
515 	basl_fill_gas(&fadt.SleepStatus, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
516 	    ACPI_GAS_ACCESS_WIDTH_BYTE, 0);
517 	BASL_EXEC(basl_table_append_content(table, &fadt, sizeof(fadt)));
518 
519 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_FACS,
520 	    offsetof(ACPI_TABLE_FADT, Facs), sizeof(fadt.Facs)));
521 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_DSDT,
522 	    offsetof(ACPI_TABLE_FADT, Dsdt), sizeof(fadt.Dsdt)));
523 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_FACS,
524 	    offsetof(ACPI_TABLE_FADT, XFacs), sizeof(fadt.XFacs)));
525 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_DSDT,
526 	    offsetof(ACPI_TABLE_FADT, XDsdt), sizeof(fadt.XDsdt)));
527 
528 	BASL_EXEC(basl_table_register_to_rsdt(table));
529 
530 	return (0);
531 }
532 
533 #ifdef __amd64__
534 static int
535 build_hpet(struct vmctx *const ctx)
536 {
537 	ACPI_TABLE_HPET hpet;
538 	struct basl_table *table;
539 	uint32_t hpet_capabilities;
540 	int err;
541 
542 	err = vm_get_hpet_capabilities(ctx, &hpet_capabilities);
543 	if (err != 0)
544 		return (err);
545 
546 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_HPET,
547 	    BASL_TABLE_ALIGNMENT));
548 
549 	memset(&hpet, 0, sizeof(hpet));
550 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_HPET, 1, 1));
551 	hpet.Id = htole32(hpet_capabilities);
552 	basl_fill_gas(&hpet.Address, ACPI_ADR_SPACE_SYSTEM_MEMORY, 0, 0,
553 	    ACPI_GAS_ACCESS_WIDTH_LEGACY, BHYVE_ADDRESS_HPET);
554 	hpet.Flags = ACPI_HPET_PAGE_PROTECT4;
555 	BASL_EXEC(basl_table_append_content(table, &hpet, sizeof(hpet)));
556 
557 	BASL_EXEC(basl_table_register_to_rsdt(table));
558 
559 	return (0);
560 }
561 #endif
562 
563 static int
564 build_madt(struct vmctx *const ctx)
565 {
566 	ACPI_TABLE_MADT madt;
567 	ACPI_MADT_LOCAL_APIC madt_lapic;
568 	ACPI_MADT_IO_APIC madt_ioapic;
569 	ACPI_MADT_INTERRUPT_OVERRIDE madt_irq_override;
570 	ACPI_MADT_LOCAL_APIC_NMI madt_lapic_nmi;
571 	struct basl_table *table;
572 
573 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_MADT,
574 	    BASL_TABLE_ALIGNMENT));
575 
576 	memset(&madt, 0, sizeof(madt));
577 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_MADT, 1, 1));
578 	madt.Address = htole32(BHYVE_ADDRESS_LAPIC);
579 	madt.Flags = htole32(ACPI_MADT_PCAT_COMPAT);
580 	BASL_EXEC(basl_table_append_content(table, &madt, sizeof(madt)));
581 
582 	/* Local APIC for each CPU */
583 	for (int i = 0; i < basl_ncpu; ++i) {
584 		memset(&madt_lapic, 0, sizeof(madt_lapic));
585 		madt_lapic.Header.Type = ACPI_MADT_TYPE_LOCAL_APIC;
586 		madt_lapic.Header.Length = sizeof(madt_lapic);
587 		madt_lapic.ProcessorId = i;
588 		madt_lapic.Id = i;
589 		madt_lapic.LapicFlags = htole32(ACPI_MADT_ENABLED);
590 		BASL_EXEC(basl_table_append_bytes(table, &madt_lapic,
591 		    sizeof(madt_lapic)));
592 	}
593 
594 	/* I/O APIC */
595 	memset(&madt_ioapic, 0, sizeof(madt_ioapic));
596 	madt_ioapic.Header.Type = ACPI_MADT_TYPE_IO_APIC;
597 	madt_ioapic.Header.Length = sizeof(madt_ioapic);
598 	madt_ioapic.Address = htole32(BHYVE_ADDRESS_IOAPIC);
599 	BASL_EXEC(
600 	    basl_table_append_bytes(table, &madt_ioapic, sizeof(madt_ioapic)));
601 
602 	/* Legacy IRQ0 is connected to pin 2 of the I/O APIC */
603 	memset(&madt_irq_override, 0, sizeof(madt_irq_override));
604 	madt_irq_override.Header.Type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE;
605 	madt_irq_override.Header.Length = sizeof(madt_irq_override);
606 	madt_irq_override.GlobalIrq = htole32(2);
607 	madt_irq_override.IntiFlags = htole16(
608 	    ACPI_MADT_POLARITY_ACTIVE_HIGH | ACPI_MADT_TRIGGER_EDGE);
609 	BASL_EXEC(basl_table_append_bytes(table, &madt_irq_override,
610 	    sizeof(madt_irq_override)));
611 
612 	memset(&madt_irq_override, 0, sizeof(madt_irq_override));
613 	madt_irq_override.Header.Type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE;
614 	madt_irq_override.Header.Length = sizeof(madt_irq_override);
615 	madt_irq_override.SourceIrq = SCI_INT;
616 	madt_irq_override.GlobalIrq = htole32(SCI_INT);
617 	madt_irq_override.IntiFlags = htole16(
618 	    ACPI_MADT_POLARITY_ACTIVE_LOW | ACPI_MADT_TRIGGER_LEVEL);
619 	BASL_EXEC(basl_table_append_bytes(table, &madt_irq_override,
620 	    sizeof(madt_irq_override)));
621 
622 	/* Local APIC NMI is conntected to LINT 1 on all CPUs */
623 	memset(&madt_lapic_nmi, 0, sizeof(madt_lapic_nmi));
624 	madt_lapic_nmi.Header.Type = ACPI_MADT_TYPE_LOCAL_APIC_NMI;
625 	madt_lapic_nmi.Header.Length = sizeof(madt_lapic_nmi);
626 	madt_lapic_nmi.ProcessorId = 0xFF;
627 	madt_lapic_nmi.IntiFlags = htole16(
628 	    ACPI_MADT_POLARITY_ACTIVE_HIGH | ACPI_MADT_TRIGGER_EDGE);
629 	madt_lapic_nmi.Lint = 1;
630 	BASL_EXEC(basl_table_append_bytes(table, &madt_lapic_nmi,
631 	    sizeof(madt_lapic_nmi)));
632 
633 	BASL_EXEC(basl_table_register_to_rsdt(table));
634 
635 	return (0);
636 }
637 
638 static int
639 build_mcfg(struct vmctx *const ctx)
640 {
641 	ACPI_TABLE_MCFG mcfg;
642 	ACPI_MCFG_ALLOCATION mcfg_allocation;
643 	struct basl_table *table;
644 
645 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_MCFG,
646 	    BASL_TABLE_ALIGNMENT));
647 
648 	memset(&mcfg, 0, sizeof(mcfg));
649 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_MCFG, 1, 1));
650 	BASL_EXEC(basl_table_append_content(table, &mcfg, sizeof(mcfg)));
651 
652 	memset(&mcfg_allocation, 0, sizeof(mcfg_allocation));
653 	mcfg_allocation.Address = htole64(pci_ecfg_base());
654 	mcfg_allocation.EndBusNumber = 0xFF;
655 	BASL_EXEC(basl_table_append_bytes(table, &mcfg_allocation,
656 	    sizeof(mcfg_allocation)));
657 
658 	BASL_EXEC(basl_table_register_to_rsdt(table));
659 
660 	return (0);
661 }
662 
663 static int
664 build_rsdp(struct vmctx *const ctx)
665 {
666 	ACPI_TABLE_RSDP rsdp;
667 	struct basl_table *table;
668 
669 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_RSDP_NAME,
670 	    BASL_TABLE_ALIGNMENT));
671 
672 	memset(&rsdp, 0, sizeof(rsdp));
673 	memcpy(rsdp.Signature, ACPI_SIG_RSDP, 8);
674 	rsdp.Checksum = 0; /* patched by basl */
675 	memcpy(rsdp.OemId, "BHYVE ", ACPI_OEM_ID_SIZE);
676 	rsdp.Revision = 2;
677 	rsdp.RsdtPhysicalAddress = htole32(0); /* patched by basl */
678 	rsdp.Length = htole32(0);	       /* patched by basl */
679 	rsdp.XsdtPhysicalAddress = htole64(0); /* patched by basl */
680 	rsdp.ExtendedChecksum = 0;	       /* patched by basl */
681 	BASL_EXEC(basl_table_append_bytes(table, &rsdp, sizeof(rsdp)));
682 
683 	BASL_EXEC(basl_table_add_checksum(table,
684 	    offsetof(ACPI_TABLE_RSDP, Checksum), 0, 20));
685 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_RSDT,
686 	    offsetof(ACPI_TABLE_RSDP, RsdtPhysicalAddress),
687 	    sizeof(rsdp.RsdtPhysicalAddress)));
688 	BASL_EXEC(basl_table_add_length(table,
689 	    offsetof(ACPI_TABLE_RSDP, Length), sizeof(rsdp.Length)));
690 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_XSDT,
691 	    offsetof(ACPI_TABLE_RSDP, XsdtPhysicalAddress),
692 	    sizeof(rsdp.XsdtPhysicalAddress)));
693 	BASL_EXEC(basl_table_add_checksum(table,
694 	    offsetof(ACPI_TABLE_RSDP, ExtendedChecksum), 0,
695 	    BASL_TABLE_CHECKSUM_LEN_FULL_TABLE));
696 
697 	return (0);
698 }
699 
700 static int
701 build_spcr(struct vmctx *const ctx)
702 {
703 	ACPI_TABLE_SPCR spcr;
704 	struct basl_table *table;
705 
706 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_SPCR,
707 	    BASL_TABLE_ALIGNMENT));
708 
709 	memset(&spcr, 0, sizeof(spcr));
710 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_SPCR, 1, 1));
711 	spcr.InterfaceType = ACPI_DBG2_16550_COMPATIBLE;
712 	basl_fill_gas(&spcr.SerialPort, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
713 	    ACPI_GAS_ACCESS_WIDTH_LEGACY, 0x3F8);
714 	spcr.InterruptType = ACPI_SPCR_INTERRUPT_TYPE_8259;
715 	spcr.PcInterrupt = 4;
716 	spcr.BaudRate = ACPI_SPCR_BAUD_RATE_115200;
717 	spcr.Parity = ACPI_SPCR_PARITY_NO_PARITY;
718 	spcr.StopBits = ACPI_SPCR_STOP_BITS_1;
719 	spcr.FlowControl = 3; /* RTS/CTS | DCD */
720 	spcr.TerminalType = ACPI_SPCR_TERMINAL_TYPE_VT_UTF8;
721 	BASL_EXEC(basl_table_append_content(table, &spcr, sizeof(spcr)));
722 
723 	BASL_EXEC(basl_table_register_to_rsdt(table));
724 
725 	return (0);
726 }
727 
728 int
729 acpi_build(struct vmctx *ctx, int ncpu)
730 {
731 	basl_ncpu = ncpu;
732 
733 	/*
734 	 * For debug, allow the user to have iasl compiler output sent
735 	 * to stdout rather than /dev/null
736 	 */
737 	if (getenv("BHYVE_ACPI_VERBOSE_IASL"))
738 		basl_verbose_iasl = 1;
739 
740 	/*
741 	 * Allow the user to keep the generated ASL files for debugging
742 	 * instead of deleting them following use
743 	 */
744 	if (getenv("BHYVE_ACPI_KEEPTMPS"))
745 		basl_keep_temps = 1;
746 
747 	BASL_EXEC(basl_init(ctx));
748 
749 	BASL_EXEC(basl_make_templates());
750 
751 	/*
752 	 * Generate ACPI tables and copy them into guest memory.
753 	 *
754 	 * According to UEFI Specification v6.3 chapter 5.1 the FADT should be
755 	 * the first table pointed to by XSDT. For that reason, build it as the
756 	 * first table after XSDT.
757 	 */
758 	BASL_EXEC(build_rsdp(ctx));
759 	BASL_EXEC(build_fadt(ctx));
760 	BASL_EXEC(build_madt(ctx));
761 #ifdef __amd64__
762 	BASL_EXEC(build_hpet(ctx));
763 #endif
764 	BASL_EXEC(build_mcfg(ctx));
765 	BASL_EXEC(build_facs(ctx));
766 	BASL_EXEC(build_spcr(ctx));
767 
768 	/* Build ACPI device-specific tables such as a TPM2 table. */
769 	const struct acpi_device_list_entry *entry;
770 	SLIST_FOREACH(entry, &acpi_devices, chain) {
771 		BASL_EXEC(acpi_device_build_table(entry->dev));
772 	}
773 
774 	BASL_EXEC(build_dsdt(ctx));
775 
776 	BASL_EXEC(basl_finish());
777 
778 	return (0);
779 }
780