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