xref: /illumos-gate/usr/src/cmd/bhyve/acpi.c (revision 32640292)
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/cdefs.h>
40 
41 #include <sys/param.h>
42 #include <sys/endian.h>
43 #include <sys/errno.h>
44 #include <sys/stat.h>
45 
46 #include <err.h>
47 #include <paths.h>
48 #include <stdarg.h>
49 #include <stddef.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54 
55 #include <machine/vmm.h>
56 #include <vmmapi.h>
57 
58 #include "bhyverun.h"
59 #include "acpi.h"
60 #include "basl.h"
61 #include "pci_emul.h"
62 #include "vmgenc.h"
63 
64 #define	BHYVE_ASL_TEMPLATE	"bhyve.XXXXXXX"
65 #define BHYVE_ASL_SUFFIX	".aml"
66 #define BHYVE_ASL_COMPILER	"/usr/sbin/iasl"
67 
68 #define BHYVE_ADDRESS_IOAPIC 	0xFEC00000
69 #define BHYVE_ADDRESS_HPET 	0xFED00000
70 #define BHYVE_ADDRESS_LAPIC 	0xFEE00000
71 
72 static int basl_keep_temps;
73 static int basl_verbose_iasl;
74 static int basl_ncpu;
75 static uint32_t hpet_capabilities;
76 
77 /*
78  * Contains the full pathname of the template to be passed
79  * to mkstemp/mktemps(3)
80  */
81 static char basl_template[MAXPATHLEN];
82 static char basl_stemplate[MAXPATHLEN];
83 
84 /*
85  * State for dsdt_line(), dsdt_indent(), and dsdt_unindent().
86  */
87 static FILE *dsdt_fp;
88 static int dsdt_indent_level;
89 static int dsdt_error;
90 
91 struct basl_fio {
92 	int	fd;
93 	FILE	*fp;
94 	char	f_name[MAXPATHLEN];
95 };
96 
97 #define EFPRINTF(...) \
98 	if (fprintf(__VA_ARGS__) < 0) goto err_exit
99 
100 #define EFFLUSH(x) \
101 	if (fflush(x) != 0) goto err_exit
102 
103 /*
104  * A list for additional ACPI devices like a TPM.
105  */
106 struct acpi_device_list_entry {
107 	SLIST_ENTRY(acpi_device_list_entry) chain;
108 	const struct acpi_device *dev;
109 };
110 static SLIST_HEAD(acpi_device_list,
111     acpi_device_list_entry) acpi_devices = SLIST_HEAD_INITIALIZER(acpi_devices);
112 
113 int
acpi_tables_add_device(const struct acpi_device * const dev)114 acpi_tables_add_device(const struct acpi_device *const dev)
115 {
116 	struct acpi_device_list_entry *const entry = calloc(1, sizeof(*entry));
117 	if (entry == NULL) {
118 		return (ENOMEM);
119 	}
120 
121 	entry->dev = dev;
122 	SLIST_INSERT_HEAD(&acpi_devices, entry, chain);
123 
124 	return (0);
125 }
126 
127 /*
128  * Helper routines for writing to the DSDT from other modules.
129  */
130 void
dsdt_line(const char * fmt,...)131 dsdt_line(const char *fmt, ...)
132 {
133 	va_list ap;
134 
135 	if (dsdt_error != 0)
136 		return;
137 
138 	if (strcmp(fmt, "") != 0) {
139 		if (dsdt_indent_level != 0)
140 			EFPRINTF(dsdt_fp, "%*c", dsdt_indent_level * 2, ' ');
141 		va_start(ap, fmt);
142 		if (vfprintf(dsdt_fp, fmt, ap) < 0) {
143 			va_end(ap);
144 			goto err_exit;
145 		}
146 		va_end(ap);
147 	}
148 	EFPRINTF(dsdt_fp, "\n");
149 	return;
150 
151 err_exit:
152 	dsdt_error = errno;
153 }
154 
155 void
dsdt_indent(int levels)156 dsdt_indent(int levels)
157 {
158 
159 	dsdt_indent_level += levels;
160 	assert(dsdt_indent_level >= 0);
161 }
162 
163 void
dsdt_unindent(int levels)164 dsdt_unindent(int levels)
165 {
166 
167 	assert(dsdt_indent_level >= levels);
168 	dsdt_indent_level -= levels;
169 }
170 
171 void
dsdt_fixed_ioport(uint16_t iobase,uint16_t length)172 dsdt_fixed_ioport(uint16_t iobase, uint16_t length)
173 {
174 
175 	dsdt_line("IO (Decode16,");
176 	dsdt_line("  0x%04X,             // Range Minimum", iobase);
177 	dsdt_line("  0x%04X,             // Range Maximum", iobase);
178 	dsdt_line("  0x01,               // Alignment");
179 	dsdt_line("  0x%02X,               // Length", length);
180 	dsdt_line("  )");
181 }
182 
183 void
dsdt_fixed_irq(uint8_t irq)184 dsdt_fixed_irq(uint8_t irq)
185 {
186 
187 	dsdt_line("IRQNoFlags ()");
188 	dsdt_line("  {%d}", irq);
189 }
190 
191 void
dsdt_fixed_mem32(uint32_t base,uint32_t length)192 dsdt_fixed_mem32(uint32_t base, uint32_t length)
193 {
194 
195 	dsdt_line("Memory32Fixed (ReadWrite,");
196 	dsdt_line("  0x%08X,         // Address Base", base);
197 	dsdt_line("  0x%08X,         // Address Length", length);
198 	dsdt_line("  )");
199 }
200 
201 static int
basl_fwrite_dsdt(FILE * fp)202 basl_fwrite_dsdt(FILE *fp)
203 {
204 	dsdt_fp = fp;
205 	dsdt_error = 0;
206 	dsdt_indent_level = 0;
207 
208 	dsdt_line("/*");
209 	dsdt_line(" * bhyve DSDT template");
210 	dsdt_line(" */");
211 	dsdt_line("DefinitionBlock (\"bhyve_dsdt.aml\", \"DSDT\", 2,"
212 		 "\"BHYVE \", \"BVDSDT  \", 0x00000001)");
213 	dsdt_line("{");
214 	dsdt_line("  Name (_S5, Package ()");
215 	dsdt_line("  {");
216 	dsdt_line("      0x05,");
217 	dsdt_line("      Zero,");
218 	dsdt_line("  })");
219 
220 	pci_write_dsdt();
221 
222 	dsdt_line("");
223 	dsdt_line("  Scope (_SB.PC00)");
224 	dsdt_line("  {");
225 	dsdt_line("    Device (HPET)");
226 	dsdt_line("    {");
227 	dsdt_line("      Name (_HID, EISAID(\"PNP0103\"))");
228 	dsdt_line("      Name (_UID, 0)");
229 	dsdt_line("      Name (_CRS, ResourceTemplate ()");
230 	dsdt_line("      {");
231 	dsdt_indent(4);
232 	dsdt_fixed_mem32(0xFED00000, 0x400);
233 	dsdt_unindent(4);
234 	dsdt_line("      })");
235 	dsdt_line("    }");
236 	dsdt_line("  }");
237 
238 	vmgenc_write_dsdt();
239 
240 	const struct acpi_device_list_entry *entry;
241 	SLIST_FOREACH(entry, &acpi_devices, chain) {
242 		BASL_EXEC(acpi_device_write_dsdt(entry->dev));
243 	}
244 
245 	dsdt_line("}");
246 
247 	if (dsdt_error != 0)
248 		return (dsdt_error);
249 
250 	EFFLUSH(fp);
251 
252 	return (0);
253 
254 err_exit:
255 	return (errno);
256 }
257 
258 static int
basl_open(struct basl_fio * bf,int suffix)259 basl_open(struct basl_fio *bf, int suffix)
260 {
261 	int err;
262 
263 	err = 0;
264 
265 	if (suffix) {
266 		strlcpy(bf->f_name, basl_stemplate, MAXPATHLEN);
267 		bf->fd = mkstemps(bf->f_name, strlen(BHYVE_ASL_SUFFIX));
268 	} else {
269 		strlcpy(bf->f_name, basl_template, MAXPATHLEN);
270 		bf->fd = mkstemp(bf->f_name);
271 	}
272 
273 	if (bf->fd > 0) {
274 		bf->fp = fdopen(bf->fd, "w+");
275 		if (bf->fp == NULL) {
276 			unlink(bf->f_name);
277 			close(bf->fd);
278 		}
279 	} else {
280 		err = 1;
281 	}
282 
283 	return (err);
284 }
285 
286 static void
basl_close(struct basl_fio * bf)287 basl_close(struct basl_fio *bf)
288 {
289 
290 	if (!basl_keep_temps)
291 		unlink(bf->f_name);
292 	fclose(bf->fp);
293 }
294 
295 static int
basl_start(struct basl_fio * in,struct basl_fio * out)296 basl_start(struct basl_fio *in, struct basl_fio *out)
297 {
298 	int err;
299 
300 	err = basl_open(in, 0);
301 	if (!err) {
302 		err = basl_open(out, 1);
303 		if (err) {
304 			basl_close(in);
305 		}
306 	}
307 
308 	return (err);
309 }
310 
311 static void
basl_end(struct basl_fio * in,struct basl_fio * out)312 basl_end(struct basl_fio *in, struct basl_fio *out)
313 {
314 
315 	basl_close(in);
316 	basl_close(out);
317 }
318 
319 static int
basl_load(struct vmctx * ctx,int fd)320 basl_load(struct vmctx *ctx, int fd)
321 {
322 	struct stat sb;
323 	void *addr;
324 
325 	if (fstat(fd, &sb) < 0)
326 		return (errno);
327 
328 	addr = calloc(1, sb.st_size);
329 	if (addr == NULL)
330 		return (EFAULT);
331 
332 	if (read(fd, addr, sb.st_size) < 0)
333 		return (errno);
334 
335 	struct basl_table *table;
336 
337 	uint8_t name[ACPI_NAMESEG_SIZE + 1] = { 0 };
338 	memcpy(name, addr, sizeof(name) - 1 /* last char is '\0' */);
339 	BASL_EXEC(basl_table_create(&table, ctx, name, BASL_TABLE_ALIGNMENT));
340 	BASL_EXEC(basl_table_append_bytes(table, addr, sb.st_size));
341 
342 	return (0);
343 }
344 
345 static int
basl_compile(struct vmctx * ctx,int (* fwrite_section)(FILE *))346 basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *))
347 {
348 	struct basl_fio io[2];
349 	static char iaslbuf[3*MAXPATHLEN + 10];
350 	const char *fmt;
351 	int err;
352 
353 	err = basl_start(&io[0], &io[1]);
354 	if (!err) {
355 		err = (*fwrite_section)(io[0].fp);
356 
357 		if (!err) {
358 			/*
359 			 * iasl sends the results of the compilation to
360 			 * stdout. Shut this down by using the shell to
361 			 * redirect stdout to /dev/null, unless the user
362 			 * has requested verbose output for debugging
363 			 * purposes
364 			 */
365 			fmt = basl_verbose_iasl ?
366 				"%s -p %s %s" :
367 				"/bin/sh -c \"%s -p %s %s\" 1> /dev/null";
368 
369 			snprintf(iaslbuf, sizeof(iaslbuf),
370 				 fmt,
371 				 BHYVE_ASL_COMPILER,
372 				 io[1].f_name, io[0].f_name);
373 			err = system(iaslbuf);
374 
375 			if (!err) {
376 				/*
377 				 * Copy the aml output file into guest
378 				 * memory at the specified location
379 				 */
380 				err = basl_load(ctx, io[1].fd);
381 			}
382 		}
383 		basl_end(&io[0], &io[1]);
384 	}
385 
386 	return (err);
387 }
388 
389 static int
basl_make_templates(void)390 basl_make_templates(void)
391 {
392 	const char *tmpdir;
393 	int err;
394 	int len;
395 
396 	err = 0;
397 
398 	/*
399 	 *
400 	 */
401 	if ((tmpdir = getenv("BHYVE_TMPDIR")) == NULL || *tmpdir == '\0' ||
402 	    (tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') {
403 		tmpdir = _PATH_TMP;
404 	}
405 
406 	len = strlen(tmpdir);
407 
408 	if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) {
409 		strcpy(basl_template, tmpdir);
410 		while (len > 0 && basl_template[len - 1] == '/')
411 			len--;
412 		basl_template[len] = '/';
413 		strcpy(&basl_template[len + 1], BHYVE_ASL_TEMPLATE);
414 	} else
415 		err = E2BIG;
416 
417 	if (!err) {
418 		/*
419 		 * len has been initialized (and maybe adjusted) above
420 		 */
421 		if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1 +
422 		     sizeof(BHYVE_ASL_SUFFIX)) < MAXPATHLEN) {
423 			strcpy(basl_stemplate, tmpdir);
424 			basl_stemplate[len] = '/';
425 			strcpy(&basl_stemplate[len + 1], BHYVE_ASL_TEMPLATE);
426 			len = strlen(basl_stemplate);
427 			strcpy(&basl_stemplate[len], BHYVE_ASL_SUFFIX);
428 		} else
429 			err = E2BIG;
430 	}
431 
432 	return (err);
433 }
434 
435 static int
build_dsdt(struct vmctx * const ctx)436 build_dsdt(struct vmctx *const ctx)
437 {
438 	BASL_EXEC(basl_compile(ctx, basl_fwrite_dsdt));
439 
440 	return (0);
441 }
442 
443 static int
build_facs(struct vmctx * const ctx)444 build_facs(struct vmctx *const ctx)
445 {
446 	ACPI_TABLE_FACS facs;
447 	struct basl_table *table;
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
build_fadt(struct vmctx * const ctx)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 static int
build_hpet(struct vmctx * const ctx)534 build_hpet(struct vmctx *const ctx)
535 {
536 	ACPI_TABLE_HPET hpet;
537 	struct basl_table *table;
538 
539 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_HPET,
540 	    BASL_TABLE_ALIGNMENT));
541 
542 	memset(&hpet, 0, sizeof(hpet));
543 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_HPET, 1, 1));
544 	hpet.Id = htole32(hpet_capabilities);
545 	basl_fill_gas(&hpet.Address, ACPI_ADR_SPACE_SYSTEM_MEMORY, 0, 0,
546 	    ACPI_GAS_ACCESS_WIDTH_LEGACY, BHYVE_ADDRESS_HPET);
547 	hpet.Flags = ACPI_HPET_PAGE_PROTECT4;
548 	BASL_EXEC(basl_table_append_content(table, &hpet, sizeof(hpet)));
549 
550 	BASL_EXEC(basl_table_register_to_rsdt(table));
551 
552 	return (0);
553 }
554 
555 static int
build_madt(struct vmctx * const ctx)556 build_madt(struct vmctx *const ctx)
557 {
558 	ACPI_TABLE_MADT madt;
559 	ACPI_MADT_LOCAL_APIC madt_lapic;
560 	ACPI_MADT_IO_APIC madt_ioapic;
561 	ACPI_MADT_INTERRUPT_OVERRIDE madt_irq_override;
562 	ACPI_MADT_LOCAL_APIC_NMI madt_lapic_nmi;
563 	struct basl_table *table;
564 
565 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_MADT,
566 	    BASL_TABLE_ALIGNMENT));
567 
568 	memset(&madt, 0, sizeof(madt));
569 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_MADT, 1, 1));
570 	madt.Address = htole32(BHYVE_ADDRESS_LAPIC);
571 	madt.Flags = htole32(ACPI_MADT_PCAT_COMPAT);
572 	BASL_EXEC(basl_table_append_content(table, &madt, sizeof(madt)));
573 
574 	/* Local APIC for each CPU */
575 	for (int i = 0; i < basl_ncpu; ++i) {
576 		memset(&madt_lapic, 0, sizeof(madt_lapic));
577 		madt_lapic.Header.Type = ACPI_MADT_TYPE_LOCAL_APIC;
578 		madt_lapic.Header.Length = sizeof(madt_lapic);
579 		madt_lapic.ProcessorId = i;
580 		madt_lapic.Id = i;
581 		madt_lapic.LapicFlags = htole32(ACPI_MADT_ENABLED);
582 		BASL_EXEC(basl_table_append_bytes(table, &madt_lapic,
583 		    sizeof(madt_lapic)));
584 	}
585 
586 	/* I/O APIC */
587 	memset(&madt_ioapic, 0, sizeof(madt_ioapic));
588 	madt_ioapic.Header.Type = ACPI_MADT_TYPE_IO_APIC;
589 	madt_ioapic.Header.Length = sizeof(madt_ioapic);
590 	madt_ioapic.Address = htole32(BHYVE_ADDRESS_IOAPIC);
591 	BASL_EXEC(
592 	    basl_table_append_bytes(table, &madt_ioapic, sizeof(madt_ioapic)));
593 
594 	/* Legacy IRQ0 is connected to pin 2 of the I/O APIC */
595 	memset(&madt_irq_override, 0, sizeof(madt_irq_override));
596 	madt_irq_override.Header.Type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE;
597 	madt_irq_override.Header.Length = sizeof(madt_irq_override);
598 	madt_irq_override.GlobalIrq = htole32(2);
599 	madt_irq_override.IntiFlags = htole16(
600 	    ACPI_MADT_POLARITY_ACTIVE_HIGH | ACPI_MADT_TRIGGER_EDGE);
601 	BASL_EXEC(basl_table_append_bytes(table, &madt_irq_override,
602 	    sizeof(madt_irq_override)));
603 
604 	memset(&madt_irq_override, 0, sizeof(madt_irq_override));
605 	madt_irq_override.Header.Type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE;
606 	madt_irq_override.Header.Length = sizeof(madt_irq_override);
607 	madt_irq_override.SourceIrq = SCI_INT;
608 	madt_irq_override.GlobalIrq = htole32(SCI_INT);
609 	madt_irq_override.IntiFlags = htole16(
610 	    ACPI_MADT_POLARITY_ACTIVE_LOW | ACPI_MADT_TRIGGER_LEVEL);
611 	BASL_EXEC(basl_table_append_bytes(table, &madt_irq_override,
612 	    sizeof(madt_irq_override)));
613 
614 	/* Local APIC NMI is conntected to LINT 1 on all CPUs */
615 	memset(&madt_lapic_nmi, 0, sizeof(madt_lapic_nmi));
616 	madt_lapic_nmi.Header.Type = ACPI_MADT_TYPE_LOCAL_APIC_NMI;
617 	madt_lapic_nmi.Header.Length = sizeof(madt_lapic_nmi);
618 	madt_lapic_nmi.ProcessorId = 0xFF;
619 	madt_lapic_nmi.IntiFlags = htole16(
620 	    ACPI_MADT_POLARITY_ACTIVE_HIGH | ACPI_MADT_TRIGGER_EDGE);
621 	madt_lapic_nmi.Lint = 1;
622 	BASL_EXEC(basl_table_append_bytes(table, &madt_lapic_nmi,
623 	    sizeof(madt_lapic_nmi)));
624 
625 	BASL_EXEC(basl_table_register_to_rsdt(table));
626 
627 	return (0);
628 }
629 
630 static int
build_mcfg(struct vmctx * const ctx)631 build_mcfg(struct vmctx *const ctx)
632 {
633 	ACPI_TABLE_MCFG mcfg;
634 	ACPI_MCFG_ALLOCATION mcfg_allocation;
635 	struct basl_table *table;
636 
637 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_MCFG,
638 	    BASL_TABLE_ALIGNMENT));
639 
640 	memset(&mcfg, 0, sizeof(mcfg));
641 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_MCFG, 1, 1));
642 	BASL_EXEC(basl_table_append_content(table, &mcfg, sizeof(mcfg)));
643 
644 	memset(&mcfg_allocation, 0, sizeof(mcfg_allocation));
645 	mcfg_allocation.Address = htole64(pci_ecfg_base());
646 	mcfg_allocation.EndBusNumber = 0xFF;
647 	BASL_EXEC(basl_table_append_bytes(table, &mcfg_allocation,
648 	    sizeof(mcfg_allocation)));
649 
650 	BASL_EXEC(basl_table_register_to_rsdt(table));
651 
652 	return (0);
653 }
654 
655 static int
build_rsdp(struct vmctx * const ctx)656 build_rsdp(struct vmctx *const ctx)
657 {
658 	ACPI_TABLE_RSDP rsdp;
659 	struct basl_table *table;
660 
661 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_RSDP_NAME,
662 	    BASL_TABLE_ALIGNMENT));
663 
664 	memset(&rsdp, 0, sizeof(rsdp));
665 	memcpy(rsdp.Signature, ACPI_SIG_RSDP, 8);
666 	rsdp.Checksum = 0; /* patched by basl */
667 	memcpy(rsdp.OemId, "BHYVE ", ACPI_OEM_ID_SIZE);
668 	rsdp.Revision = 2;
669 	rsdp.RsdtPhysicalAddress = htole32(0); /* patched by basl */
670 	rsdp.Length = htole32(0);	       /* patched by basl */
671 	rsdp.XsdtPhysicalAddress = htole64(0); /* patched by basl */
672 	rsdp.ExtendedChecksum = 0;	       /* patched by basl */
673 	BASL_EXEC(basl_table_append_bytes(table, &rsdp, sizeof(rsdp)));
674 
675 	BASL_EXEC(basl_table_add_checksum(table,
676 	    offsetof(ACPI_TABLE_RSDP, Checksum), 0, 20));
677 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_RSDT,
678 	    offsetof(ACPI_TABLE_RSDP, RsdtPhysicalAddress),
679 	    sizeof(rsdp.RsdtPhysicalAddress)));
680 	BASL_EXEC(basl_table_add_length(table,
681 	    offsetof(ACPI_TABLE_RSDP, Length), sizeof(rsdp.Length)));
682 	BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_XSDT,
683 	    offsetof(ACPI_TABLE_RSDP, XsdtPhysicalAddress),
684 	    sizeof(rsdp.XsdtPhysicalAddress)));
685 	BASL_EXEC(basl_table_add_checksum(table,
686 	    offsetof(ACPI_TABLE_RSDP, ExtendedChecksum), 0,
687 	    BASL_TABLE_CHECKSUM_LEN_FULL_TABLE));
688 
689 	return (0);
690 }
691 
692 static int
build_spcr(struct vmctx * const ctx)693 build_spcr(struct vmctx *const ctx)
694 {
695 	ACPI_TABLE_SPCR spcr;
696 	struct basl_table *table;
697 
698 	BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_SPCR,
699 	    BASL_TABLE_ALIGNMENT));
700 
701 	memset(&spcr, 0, sizeof(spcr));
702 	BASL_EXEC(basl_table_append_header(table, ACPI_SIG_SPCR, 1, 1));
703 	spcr.InterfaceType = ACPI_DBG2_16550_COMPATIBLE;
704 	basl_fill_gas(&spcr.SerialPort, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
705 	    ACPI_GAS_ACCESS_WIDTH_LEGACY, 0x3F8);
706 	spcr.InterruptType = ACPI_SPCR_INTERRUPT_TYPE_8259;
707 	spcr.PcInterrupt = 4;
708 	spcr.BaudRate = ACPI_SPCR_BAUD_RATE_115200;
709 	spcr.Parity = ACPI_SPCR_PARITY_NO_PARITY;
710 	spcr.StopBits = ACPI_SPCR_STOP_BITS_1;
711 	spcr.FlowControl = 3; /* RTS/CTS | DCD */
712 	spcr.TerminalType = ACPI_SPCR_TERMINAL_TYPE_VT_UTF8;
713 	BASL_EXEC(basl_table_append_content(table, &spcr, sizeof(spcr)));
714 
715 	BASL_EXEC(basl_table_register_to_rsdt(table));
716 
717 	return (0);
718 }
719 
720 int
acpi_build(struct vmctx * ctx,int ncpu)721 acpi_build(struct vmctx *ctx, int ncpu)
722 {
723 	int err;
724 
725 	basl_ncpu = ncpu;
726 
727 	err = vm_get_hpet_capabilities(ctx, &hpet_capabilities);
728 	if (err != 0)
729 		return (err);
730 
731 	/*
732 	 * For debug, allow the user to have iasl compiler output sent
733 	 * to stdout rather than /dev/null
734 	 */
735 	if (getenv("BHYVE_ACPI_VERBOSE_IASL"))
736 		basl_verbose_iasl = 1;
737 
738 	/*
739 	 * Allow the user to keep the generated ASL files for debugging
740 	 * instead of deleting them following use
741 	 */
742 	if (getenv("BHYVE_ACPI_KEEPTMPS"))
743 		basl_keep_temps = 1;
744 
745 	BASL_EXEC(basl_init(ctx));
746 
747 	BASL_EXEC(basl_make_templates());
748 
749 	/*
750 	 * Generate ACPI tables and copy them into guest memory.
751 	 *
752 	 * According to UEFI Specification v6.3 chapter 5.1 the FADT should be
753 	 * the first table pointed to by XSDT. For that reason, build it as the
754 	 * first table after XSDT.
755 	 */
756 	BASL_EXEC(build_rsdp(ctx));
757 	BASL_EXEC(build_fadt(ctx));
758 	BASL_EXEC(build_madt(ctx));
759 	BASL_EXEC(build_hpet(ctx));
760 	BASL_EXEC(build_mcfg(ctx));
761 	BASL_EXEC(build_facs(ctx));
762 	BASL_EXEC(build_spcr(ctx));
763 
764 	/* Build ACPI device-specific tables such as a TPM2 table. */
765 	const struct acpi_device_list_entry *entry;
766 	SLIST_FOREACH(entry, &acpi_devices, chain) {
767 		BASL_EXEC(acpi_device_build_table(entry->dev));
768 	}
769 
770 	BASL_EXEC(build_dsdt(ctx));
771 
772 	BASL_EXEC(basl_finish());
773 
774 	return (0);
775 }
776