xref: /freebsd/usr.sbin/bhyve/acpi.c (revision dbd5678d)
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) by writing out ASL template files for each of
36  * the tables and the compiling them to AML with the Intel iasl compiler.
37  * The AML files are then read into guest memory.
38  *
39  *  The tables are placed in the guest's ROM area just below 1MB physical,
40  * above the MPTable.
41  *
42  *  Layout (No longer correct at FADT and beyond due to properly
43  *  calculating the size of the MADT to allow for changes to
44  *  VM_MAXCPU above 21 which overflows this layout.)
45  *  ------
46  *   RSDP  ->   0xf2400    (36 bytes fixed)
47  *     RSDT  ->   0xf2440    (36 bytes + 4*7 table addrs, 4 used)
48  *     XSDT  ->   0xf2480    (36 bytes + 8*7 table addrs, 4 used)
49  *       MADT  ->   0xf2500  (depends on #CPUs)
50  *       FADT  ->   0xf2600  (268 bytes)
51  *       HPET  ->   0xf2740  (56 bytes)
52  *       MCFG  ->   0xf2780  (60 bytes)
53  *         FACS  ->   0xf27C0 (64 bytes)
54  *         DSDT  ->   0xf2800 (variable - can go up to 0x100000)
55  */
56 
57 #include <sys/cdefs.h>
58 __FBSDID("$FreeBSD$");
59 
60 #include <sys/param.h>
61 #include <sys/errno.h>
62 #include <sys/stat.h>
63 
64 #include <err.h>
65 #include <paths.h>
66 #include <stdarg.h>
67 #include <stdio.h>
68 #include <stdlib.h>
69 #include <string.h>
70 #include <unistd.h>
71 
72 #include <machine/vmm.h>
73 #include <vmmapi.h>
74 
75 #include "bhyverun.h"
76 #include "acpi.h"
77 #include "basl.h"
78 #include "pci_emul.h"
79 #include "vmgenc.h"
80 
81 /*
82  * Define the base address of the ACPI tables, the sizes of some tables,
83  * and the offsets to the individual tables,
84  */
85 #define RSDT_OFFSET		0x040
86 #define XSDT_OFFSET		0x080
87 #define MADT_OFFSET		0x100
88 /*
89  * The MADT consists of:
90  *	44		Fixed Header
91  *	8 * maxcpu	Processor Local APIC entries
92  *	12		I/O APIC entry
93  *	2 * 10		Interrupt Source Override entries
94  *	6		Local APIC NMI entry
95  */
96 #define	MADT_SIZE		roundup2((44 + basl_ncpu*8 + 12 + 2*10 + 6), 0x100)
97 #define	FADT_OFFSET		(MADT_OFFSET + MADT_SIZE)
98 #define	FADT_SIZE		0x140
99 #define	HPET_OFFSET		(FADT_OFFSET + FADT_SIZE)
100 #define	HPET_SIZE		0x40
101 #define	MCFG_OFFSET		(HPET_OFFSET + HPET_SIZE)
102 #define	MCFG_SIZE		0x40
103 #define	FACS_OFFSET		(MCFG_OFFSET + MCFG_SIZE)
104 #define	FACS_SIZE		0x40
105 #define	DSDT_OFFSET		(FACS_OFFSET + FACS_SIZE)
106 
107 #define	BHYVE_ASL_TEMPLATE	"bhyve.XXXXXXX"
108 #define BHYVE_ASL_SUFFIX	".aml"
109 #define BHYVE_ASL_COMPILER	"/usr/sbin/iasl"
110 
111 static int basl_keep_temps;
112 static int basl_verbose_iasl;
113 static int basl_ncpu;
114 static uint32_t basl_acpi_base = BHYVE_ACPI_BASE;
115 static uint32_t hpet_capabilities;
116 
117 /*
118  * Contains the full pathname of the template to be passed
119  * to mkstemp/mktemps(3)
120  */
121 static char basl_template[MAXPATHLEN];
122 static char basl_stemplate[MAXPATHLEN];
123 
124 /*
125  * State for dsdt_line(), dsdt_indent(), and dsdt_unindent().
126  */
127 static FILE *dsdt_fp;
128 static int dsdt_indent_level;
129 static int dsdt_error;
130 
131 struct basl_fio {
132 	int	fd;
133 	FILE	*fp;
134 	char	f_name[MAXPATHLEN];
135 };
136 
137 #define EFPRINTF(...) \
138 	if (fprintf(__VA_ARGS__) < 0) goto err_exit;
139 
140 #define EFFLUSH(x) \
141 	if (fflush(x) != 0) goto err_exit;
142 
143 static int
144 basl_fwrite_rsdp(FILE *fp)
145 {
146 	EFPRINTF(fp, "/*\n");
147 	EFPRINTF(fp, " * bhyve RSDP template\n");
148 	EFPRINTF(fp, " */\n");
149 	EFPRINTF(fp, "[0008]\t\tSignature : \"RSD PTR \"\n");
150 	EFPRINTF(fp, "[0001]\t\tChecksum : 43\n");
151 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
152 	EFPRINTF(fp, "[0001]\t\tRevision : 02\n");
153 	EFPRINTF(fp, "[0004]\t\tRSDT Address : %08X\n",
154 	    basl_acpi_base + RSDT_OFFSET);
155 	EFPRINTF(fp, "[0004]\t\tLength : 00000024\n");
156 	EFPRINTF(fp, "[0008]\t\tXSDT Address : 00000000%08X\n",
157 	    basl_acpi_base + XSDT_OFFSET);
158 	EFPRINTF(fp, "[0001]\t\tExtended Checksum : 00\n");
159 	EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
160 
161 	EFFLUSH(fp);
162 
163 	return (0);
164 
165 err_exit:
166 	return (errno);
167 }
168 
169 static int
170 basl_fwrite_rsdt(FILE *fp)
171 {
172 	EFPRINTF(fp, "/*\n");
173 	EFPRINTF(fp, " * bhyve RSDT template\n");
174 	EFPRINTF(fp, " */\n");
175 	EFPRINTF(fp, "[0004]\t\tSignature : \"RSDT\"\n");
176 	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
177 	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
178 	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
179 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
180 	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVRSDT  \"\n");
181 	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
182 	/* iasl will fill in the compiler ID/revision fields */
183 	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
184 	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
185 	EFPRINTF(fp, "\n");
186 
187 	/* Add in pointers to the MADT, FADT and HPET */
188 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : %08X\n",
189 	    basl_acpi_base + MADT_OFFSET);
190 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : %08X\n",
191 	    basl_acpi_base + FADT_OFFSET);
192 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 2 : %08X\n",
193 	    basl_acpi_base + HPET_OFFSET);
194 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 3 : %08X\n",
195 	    basl_acpi_base + MCFG_OFFSET);
196 
197 	EFFLUSH(fp);
198 
199 	return (0);
200 
201 err_exit:
202 	return (errno);
203 }
204 
205 static int
206 basl_fwrite_xsdt(FILE *fp)
207 {
208 	EFPRINTF(fp, "/*\n");
209 	EFPRINTF(fp, " * bhyve XSDT template\n");
210 	EFPRINTF(fp, " */\n");
211 	EFPRINTF(fp, "[0004]\t\tSignature : \"XSDT\"\n");
212 	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
213 	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
214 	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
215 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
216 	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVXSDT  \"\n");
217 	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
218 	/* iasl will fill in the compiler ID/revision fields */
219 	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
220 	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
221 	EFPRINTF(fp, "\n");
222 
223 	/* Add in pointers to the MADT, FADT and HPET */
224 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : 00000000%08X\n",
225 	    basl_acpi_base + MADT_OFFSET);
226 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : 00000000%08X\n",
227 	    basl_acpi_base + FADT_OFFSET);
228 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 2 : 00000000%08X\n",
229 	    basl_acpi_base + HPET_OFFSET);
230 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 3 : 00000000%08X\n",
231 	    basl_acpi_base + MCFG_OFFSET);
232 
233 	EFFLUSH(fp);
234 
235 	return (0);
236 
237 err_exit:
238 	return (errno);
239 }
240 
241 static int
242 basl_fwrite_madt(FILE *fp)
243 {
244 	int i;
245 
246 	EFPRINTF(fp, "/*\n");
247 	EFPRINTF(fp, " * bhyve MADT template\n");
248 	EFPRINTF(fp, " */\n");
249 	EFPRINTF(fp, "[0004]\t\tSignature : \"APIC\"\n");
250 	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
251 	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
252 	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
253 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
254 	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVMADT  \"\n");
255 	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
256 
257 	/* iasl will fill in the compiler ID/revision fields */
258 	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
259 	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
260 	EFPRINTF(fp, "\n");
261 
262 	EFPRINTF(fp, "[0004]\t\tLocal Apic Address : FEE00000\n");
263 	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
264 	EFPRINTF(fp, "\t\t\tPC-AT Compatibility : 1\n");
265 	EFPRINTF(fp, "\n");
266 
267 	/* Add a Processor Local APIC entry for each CPU */
268 	for (i = 0; i < basl_ncpu; i++) {
269 		EFPRINTF(fp, "[0001]\t\tSubtable Type : 00\n");
270 		EFPRINTF(fp, "[0001]\t\tLength : 08\n");
271 		/* iasl expects hex values for the proc and apic id's */
272 		EFPRINTF(fp, "[0001]\t\tProcessor ID : %02x\n", i);
273 		EFPRINTF(fp, "[0001]\t\tLocal Apic ID : %02x\n", i);
274 		EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
275 		EFPRINTF(fp, "\t\t\tProcessor Enabled : 1\n");
276 		EFPRINTF(fp, "\t\t\tRuntime Online Capable : 0\n");
277 		EFPRINTF(fp, "\n");
278 	}
279 
280 	/* Always a single IOAPIC entry, with ID 0 */
281 	EFPRINTF(fp, "[0001]\t\tSubtable Type : 01\n");
282 	EFPRINTF(fp, "[0001]\t\tLength : 0C\n");
283 	/* iasl expects a hex value for the i/o apic id */
284 	EFPRINTF(fp, "[0001]\t\tI/O Apic ID : %02x\n", 0);
285 	EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
286 	EFPRINTF(fp, "[0004]\t\tAddress : fec00000\n");
287 	EFPRINTF(fp, "[0004]\t\tInterrupt : 00000000\n");
288 	EFPRINTF(fp, "\n");
289 
290 	/* Legacy IRQ0 is connected to pin 2 of the IOAPIC */
291 	EFPRINTF(fp, "[0001]\t\tSubtable Type : 02\n");
292 	EFPRINTF(fp, "[0001]\t\tLength : 0A\n");
293 	EFPRINTF(fp, "[0001]\t\tBus : 00\n");
294 	EFPRINTF(fp, "[0001]\t\tSource : 00\n");
295 	EFPRINTF(fp, "[0004]\t\tInterrupt : 00000002\n");
296 	EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0005\n");
297 	EFPRINTF(fp, "\t\t\tPolarity : 1\n");
298 	EFPRINTF(fp, "\t\t\tTrigger Mode : 1\n");
299 	EFPRINTF(fp, "\n");
300 
301 	EFPRINTF(fp, "[0001]\t\tSubtable Type : 02\n");
302 	EFPRINTF(fp, "[0001]\t\tLength : 0A\n");
303 	EFPRINTF(fp, "[0001]\t\tBus : 00\n");
304 	EFPRINTF(fp, "[0001]\t\tSource : %02X\n", SCI_INT);
305 	EFPRINTF(fp, "[0004]\t\tInterrupt : %08X\n", SCI_INT);
306 	EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0000\n");
307 	EFPRINTF(fp, "\t\t\tPolarity : 3\n");
308 	EFPRINTF(fp, "\t\t\tTrigger Mode : 3\n");
309 	EFPRINTF(fp, "\n");
310 
311 	/* Local APIC NMI is connected to LINT 1 on all CPUs */
312 	EFPRINTF(fp, "[0001]\t\tSubtable Type : 04\n");
313 	EFPRINTF(fp, "[0001]\t\tLength : 06\n");
314 	EFPRINTF(fp, "[0001]\t\tProcessor ID : FF\n");
315 	EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0005\n");
316 	EFPRINTF(fp, "\t\t\tPolarity : 1\n");
317 	EFPRINTF(fp, "\t\t\tTrigger Mode : 1\n");
318 	EFPRINTF(fp, "[0001]\t\tInterrupt Input LINT : 01\n");
319 	EFPRINTF(fp, "\n");
320 
321 	EFFLUSH(fp);
322 
323 	return (0);
324 
325 err_exit:
326 	return (errno);
327 }
328 
329 static int
330 basl_fwrite_fadt(FILE *fp)
331 {
332 	EFPRINTF(fp, "/*\n");
333 	EFPRINTF(fp, " * bhyve FADT template\n");
334 	EFPRINTF(fp, " */\n");
335 	EFPRINTF(fp, "[0004]\t\tSignature : \"FACP\"\n");
336 	EFPRINTF(fp, "[0004]\t\tTable Length : 0000010C\n");
337 	EFPRINTF(fp, "[0001]\t\tRevision : 05\n");
338 	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
339 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
340 	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVFACP  \"\n");
341 	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
342 	/* iasl will fill in the compiler ID/revision fields */
343 	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
344 	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
345 	EFPRINTF(fp, "\n");
346 
347 	EFPRINTF(fp, "[0004]\t\tFACS Address : %08X\n",
348 	    basl_acpi_base + FACS_OFFSET);
349 	EFPRINTF(fp, "[0004]\t\tDSDT Address : %08X\n",
350 	    basl_acpi_base + DSDT_OFFSET);
351 	EFPRINTF(fp, "[0001]\t\tModel : 01\n");
352 	EFPRINTF(fp, "[0001]\t\tPM Profile : 00 [Unspecified]\n");
353 	EFPRINTF(fp, "[0002]\t\tSCI Interrupt : %04X\n",
354 	    SCI_INT);
355 	EFPRINTF(fp, "[0004]\t\tSMI Command Port : %08X\n",
356 	    SMI_CMD);
357 	EFPRINTF(fp, "[0001]\t\tACPI Enable Value : %02X\n",
358 	    BHYVE_ACPI_ENABLE);
359 	EFPRINTF(fp, "[0001]\t\tACPI Disable Value : %02X\n",
360 	    BHYVE_ACPI_DISABLE);
361 	EFPRINTF(fp, "[0001]\t\tS4BIOS Command : 00\n");
362 	EFPRINTF(fp, "[0001]\t\tP-State Control : 00\n");
363 	EFPRINTF(fp, "[0004]\t\tPM1A Event Block Address : %08X\n",
364 	    PM1A_EVT_ADDR);
365 	EFPRINTF(fp, "[0004]\t\tPM1B Event Block Address : 00000000\n");
366 	EFPRINTF(fp, "[0004]\t\tPM1A Control Block Address : %08X\n",
367 	    PM1A_CNT_ADDR);
368 	EFPRINTF(fp, "[0004]\t\tPM1B Control Block Address : 00000000\n");
369 	EFPRINTF(fp, "[0004]\t\tPM2 Control Block Address : 00000000\n");
370 	EFPRINTF(fp, "[0004]\t\tPM Timer Block Address : %08X\n",
371 	    IO_PMTMR);
372 	EFPRINTF(fp, "[0004]\t\tGPE0 Block Address : %08X\n", IO_GPE0_BLK);
373 	EFPRINTF(fp, "[0004]\t\tGPE1 Block Address : 00000000\n");
374 	EFPRINTF(fp, "[0001]\t\tPM1 Event Block Length : 04\n");
375 	EFPRINTF(fp, "[0001]\t\tPM1 Control Block Length : 02\n");
376 	EFPRINTF(fp, "[0001]\t\tPM2 Control Block Length : 00\n");
377 	EFPRINTF(fp, "[0001]\t\tPM Timer Block Length : 04\n");
378 	EFPRINTF(fp, "[0001]\t\tGPE0 Block Length : %02x\n", IO_GPE0_LEN);
379 	EFPRINTF(fp, "[0001]\t\tGPE1 Block Length : 00\n");
380 	EFPRINTF(fp, "[0001]\t\tGPE1 Base Offset : 00\n");
381 	EFPRINTF(fp, "[0001]\t\t_CST Support : 00\n");
382 	EFPRINTF(fp, "[0002]\t\tC2 Latency : 0000\n");
383 	EFPRINTF(fp, "[0002]\t\tC3 Latency : 0000\n");
384 	EFPRINTF(fp, "[0002]\t\tCPU Cache Size : 0000\n");
385 	EFPRINTF(fp, "[0002]\t\tCache Flush Stride : 0000\n");
386 	EFPRINTF(fp, "[0001]\t\tDuty Cycle Offset : 00\n");
387 	EFPRINTF(fp, "[0001]\t\tDuty Cycle Width : 00\n");
388 	EFPRINTF(fp, "[0001]\t\tRTC Day Alarm Index : 00\n");
389 	EFPRINTF(fp, "[0001]\t\tRTC Month Alarm Index : 00\n");
390 	EFPRINTF(fp, "[0001]\t\tRTC Century Index : 32\n");
391 	EFPRINTF(fp, "[0002]\t\tBoot Flags (decoded below) : 0000\n");
392 	EFPRINTF(fp, "\t\t\tLegacy Devices Supported (V2) : 0\n");
393 	EFPRINTF(fp, "\t\t\t8042 Present on ports 60/64 (V2) : 0\n");
394 	EFPRINTF(fp, "\t\t\tVGA Not Present (V4) : 1\n");
395 	EFPRINTF(fp, "\t\t\tMSI Not Supported (V4) : 0\n");
396 	EFPRINTF(fp, "\t\t\tPCIe ASPM Not Supported (V4) : 1\n");
397 	EFPRINTF(fp, "\t\t\tCMOS RTC Not Present (V5) : 0\n");
398 	EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
399 	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
400 	EFPRINTF(fp, "\t\t\tWBINVD instruction is operational (V1) : 1\n");
401 	EFPRINTF(fp, "\t\t\tWBINVD flushes all caches (V1) : 0\n");
402 	EFPRINTF(fp, "\t\t\tAll CPUs support C1 (V1) : 1\n");
403 	EFPRINTF(fp, "\t\t\tC2 works on MP system (V1) : 0\n");
404 	EFPRINTF(fp, "\t\t\tControl Method Power Button (V1) : 0\n");
405 	EFPRINTF(fp, "\t\t\tControl Method Sleep Button (V1) : 1\n");
406 	EFPRINTF(fp, "\t\t\tRTC wake not in fixed reg space (V1) : 0\n");
407 	EFPRINTF(fp, "\t\t\tRTC can wake system from S4 (V1) : 0\n");
408 	EFPRINTF(fp, "\t\t\t32-bit PM Timer (V1) : 1\n");
409 	EFPRINTF(fp, "\t\t\tDocking Supported (V1) : 0\n");
410 	EFPRINTF(fp, "\t\t\tReset Register Supported (V2) : 1\n");
411 	EFPRINTF(fp, "\t\t\tSealed Case (V3) : 0\n");
412 	EFPRINTF(fp, "\t\t\tHeadless - No Video (V3) : 1\n");
413 	EFPRINTF(fp, "\t\t\tUse native instr after SLP_TYPx (V3) : 0\n");
414 	EFPRINTF(fp, "\t\t\tPCIEXP_WAK Bits Supported (V4) : 0\n");
415 	EFPRINTF(fp, "\t\t\tUse Platform Timer (V4) : 0\n");
416 	EFPRINTF(fp, "\t\t\tRTC_STS valid on S4 wake (V4) : 0\n");
417 	EFPRINTF(fp, "\t\t\tRemote Power-on capable (V4) : 0\n");
418 	EFPRINTF(fp, "\t\t\tUse APIC Cluster Model (V4) : 0\n");
419 	EFPRINTF(fp, "\t\t\tUse APIC Physical Destination Mode (V4) : 1\n");
420 	EFPRINTF(fp, "\t\t\tHardware Reduced (V5) : 0\n");
421 	EFPRINTF(fp, "\t\t\tLow Power S0 Idle (V5) : 0\n");
422 	EFPRINTF(fp, "\n");
423 
424 	EFPRINTF(fp,
425 	    "[0012]\t\tReset Register : [Generic Address Structure]\n");
426 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
427 	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
428 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
429 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
430 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000CF9\n");
431 	EFPRINTF(fp, "\n");
432 
433 	EFPRINTF(fp, "[0001]\t\tValue to cause reset : 06\n");
434 	EFPRINTF(fp, "[0002]\t\tARM Flags (decoded below): 0000\n");
435 	EFPRINTF(fp, "\t\t\tPSCI Compliant : 0\n");
436 	EFPRINTF(fp, "\t\t\tMust use HVC for PSCI : 0\n");
437 	EFPRINTF(fp, "[0001]\t\tFADT Minor Revision : 01\n");
438 	EFPRINTF(fp, "[0008]\t\tFACS Address : 00000000%08X\n",
439 	    basl_acpi_base + FACS_OFFSET);
440 	EFPRINTF(fp, "[0008]\t\tDSDT Address : 00000000%08X\n",
441 	    basl_acpi_base + DSDT_OFFSET);
442 	EFPRINTF(fp,
443 	    "[0012]\t\tPM1A Event Block : [Generic Address Structure]\n");
444 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
445 	EFPRINTF(fp, "[0001]\t\tBit Width : 20\n");
446 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
447 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
448 	EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
449 	    PM1A_EVT_ADDR);
450 	EFPRINTF(fp, "\n");
451 
452 	EFPRINTF(fp,
453 	    "[0012]\t\tPM1B Event Block : [Generic Address Structure]\n");
454 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
455 	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
456 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
457 	EFPRINTF(fp,
458 	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
459 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
460 	EFPRINTF(fp, "\n");
461 
462 	EFPRINTF(fp,
463 	    "[0012]\t\tPM1A Control Block : [Generic Address Structure]\n");
464 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
465 	EFPRINTF(fp, "[0001]\t\tBit Width : 10\n");
466 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
467 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
468 	EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
469 	    PM1A_CNT_ADDR);
470 	EFPRINTF(fp, "\n");
471 
472 	EFPRINTF(fp,
473 	    "[0012]\t\tPM1B Control Block : [Generic Address Structure]\n");
474 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
475 	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
476 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
477 	EFPRINTF(fp,
478 	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
479 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
480 	EFPRINTF(fp, "\n");
481 
482 	EFPRINTF(fp,
483 	    "[0012]\t\tPM2 Control Block : [Generic Address Structure]\n");
484 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
485 	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
486 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
487 	EFPRINTF(fp,
488 	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
489 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
490 	EFPRINTF(fp, "\n");
491 
492 	/* Valid for bhyve */
493 	EFPRINTF(fp,
494 	    "[0012]\t\tPM Timer Block : [Generic Address Structure]\n");
495 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
496 	EFPRINTF(fp, "[0001]\t\tBit Width : 20\n");
497 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
498 	EFPRINTF(fp,
499 	    "[0001]\t\tEncoded Access Width : 03 [DWord Access:32]\n");
500 	EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
501 	    IO_PMTMR);
502 	EFPRINTF(fp, "\n");
503 
504 	EFPRINTF(fp, "[0012]\t\tGPE0 Block : [Generic Address Structure]\n");
505 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
506 	EFPRINTF(fp, "[0001]\t\tBit Width : %02x\n", IO_GPE0_LEN * 8);
507 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
508 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
509 	EFPRINTF(fp, "[0008]\t\tAddress : %016X\n", IO_GPE0_BLK);
510 	EFPRINTF(fp, "\n");
511 
512 	EFPRINTF(fp, "[0012]\t\tGPE1 Block : [Generic Address Structure]\n");
513 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
514 	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
515 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
516 	EFPRINTF(fp,
517 	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
518 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
519 	EFPRINTF(fp, "\n");
520 
521 	EFPRINTF(fp,
522 	   "[0012]\t\tSleep Control Register : [Generic Address Structure]\n");
523 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
524 	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
525 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
526 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
527 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
528 	EFPRINTF(fp, "\n");
529 
530 	EFPRINTF(fp,
531 	    "[0012]\t\tSleep Status Register : [Generic Address Structure]\n");
532 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
533 	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
534 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
535 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
536 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
537 
538 	EFFLUSH(fp);
539 
540 	return (0);
541 
542 err_exit:
543 	return (errno);
544 }
545 
546 static int
547 basl_fwrite_hpet(FILE *fp)
548 {
549 	EFPRINTF(fp, "/*\n");
550 	EFPRINTF(fp, " * bhyve HPET template\n");
551 	EFPRINTF(fp, " */\n");
552 	EFPRINTF(fp, "[0004]\t\tSignature : \"HPET\"\n");
553 	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
554 	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
555 	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
556 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
557 	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVHPET  \"\n");
558 	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
559 
560 	/* iasl will fill in the compiler ID/revision fields */
561 	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
562 	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
563 	EFPRINTF(fp, "\n");
564 
565 	EFPRINTF(fp, "[0004]\t\tHardware Block ID : %08X\n", hpet_capabilities);
566 	EFPRINTF(fp,
567 	    "[0012]\t\tTimer Block Register : [Generic Address Structure]\n");
568 	EFPRINTF(fp, "[0001]\t\tSpace ID : 00 [SystemMemory]\n");
569 	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
570 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
571 	EFPRINTF(fp,
572 		 "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
573 	EFPRINTF(fp, "[0008]\t\tAddress : 00000000FED00000\n");
574 	EFPRINTF(fp, "\n");
575 
576 	EFPRINTF(fp, "[0001]\t\tSequence Number : 00\n");
577 	EFPRINTF(fp, "[0002]\t\tMinimum Clock Ticks : 0000\n");
578 	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
579 	EFPRINTF(fp, "\t\t\t4K Page Protect : 1\n");
580 	EFPRINTF(fp, "\t\t\t64K Page Protect : 0\n");
581 	EFPRINTF(fp, "\n");
582 
583 	EFFLUSH(fp);
584 
585 	return (0);
586 
587 err_exit:
588 	return (errno);
589 }
590 
591 static int
592 basl_fwrite_mcfg(FILE *fp)
593 {
594 	EFPRINTF(fp, "/*\n");
595 	EFPRINTF(fp, " * bhyve MCFG template\n");
596 	EFPRINTF(fp, " */\n");
597 	EFPRINTF(fp, "[0004]\t\tSignature : \"MCFG\"\n");
598 	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
599 	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
600 	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
601 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
602 	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVMCFG  \"\n");
603 	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
604 
605 	/* iasl will fill in the compiler ID/revision fields */
606 	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
607 	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
608 	EFPRINTF(fp, "[0008]\t\tReserved : 0\n");
609 	EFPRINTF(fp, "\n");
610 
611 	EFPRINTF(fp, "[0008]\t\tBase Address : %016lX\n", pci_ecfg_base());
612 	EFPRINTF(fp, "[0002]\t\tSegment Group Number : 0000\n");
613 	EFPRINTF(fp, "[0001]\t\tStart Bus Number : 00\n");
614 	EFPRINTF(fp, "[0001]\t\tEnd Bus Number : FF\n");
615 	EFPRINTF(fp, "[0004]\t\tReserved : 0\n");
616 	EFFLUSH(fp);
617 	return (0);
618 err_exit:
619 	return (errno);
620 }
621 
622 static int
623 basl_fwrite_facs(FILE *fp)
624 {
625 	EFPRINTF(fp, "/*\n");
626 	EFPRINTF(fp, " * bhyve FACS template\n");
627 	EFPRINTF(fp, " */\n");
628 	EFPRINTF(fp, "[0004]\t\tSignature : \"FACS\"\n");
629 	EFPRINTF(fp, "[0004]\t\tLength : 00000040\n");
630 	EFPRINTF(fp, "[0004]\t\tHardware Signature : 00000000\n");
631 	EFPRINTF(fp, "[0004]\t\t32 Firmware Waking Vector : 00000000\n");
632 	EFPRINTF(fp, "[0004]\t\tGlobal Lock : 00000000\n");
633 	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
634 	EFPRINTF(fp, "\t\t\tS4BIOS Support Present : 0\n");
635 	EFPRINTF(fp, "\t\t\t64-bit Wake Supported (V2) : 0\n");
636 	EFPRINTF(fp,
637 	    "[0008]\t\t64 Firmware Waking Vector : 0000000000000000\n");
638 	EFPRINTF(fp, "[0001]\t\tVersion : 02\n");
639 	EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
640 	EFPRINTF(fp, "[0004]\t\tOspmFlags (decoded below) : 00000000\n");
641 	EFPRINTF(fp, "\t\t\t64-bit Wake Env Required (V2) : 0\n");
642 
643 	EFFLUSH(fp);
644 
645 	return (0);
646 
647 err_exit:
648 	return (errno);
649 }
650 
651 /*
652  * Helper routines for writing to the DSDT from other modules.
653  */
654 void
655 dsdt_line(const char *fmt, ...)
656 {
657 	va_list ap;
658 
659 	if (dsdt_error != 0)
660 		return;
661 
662 	if (strcmp(fmt, "") != 0) {
663 		if (dsdt_indent_level != 0)
664 			EFPRINTF(dsdt_fp, "%*c", dsdt_indent_level * 2, ' ');
665 		va_start(ap, fmt);
666 		if (vfprintf(dsdt_fp, fmt, ap) < 0) {
667 			va_end(ap);
668 			goto err_exit;
669 		}
670 		va_end(ap);
671 	}
672 	EFPRINTF(dsdt_fp, "\n");
673 	return;
674 
675 err_exit:
676 	dsdt_error = errno;
677 }
678 
679 void
680 dsdt_indent(int levels)
681 {
682 
683 	dsdt_indent_level += levels;
684 	assert(dsdt_indent_level >= 0);
685 }
686 
687 void
688 dsdt_unindent(int levels)
689 {
690 
691 	assert(dsdt_indent_level >= levels);
692 	dsdt_indent_level -= levels;
693 }
694 
695 void
696 dsdt_fixed_ioport(uint16_t iobase, uint16_t length)
697 {
698 
699 	dsdt_line("IO (Decode16,");
700 	dsdt_line("  0x%04X,             // Range Minimum", iobase);
701 	dsdt_line("  0x%04X,             // Range Maximum", iobase);
702 	dsdt_line("  0x01,               // Alignment");
703 	dsdt_line("  0x%02X,               // Length", length);
704 	dsdt_line("  )");
705 }
706 
707 void
708 dsdt_fixed_irq(uint8_t irq)
709 {
710 
711 	dsdt_line("IRQNoFlags ()");
712 	dsdt_line("  {%d}", irq);
713 }
714 
715 void
716 dsdt_fixed_mem32(uint32_t base, uint32_t length)
717 {
718 
719 	dsdt_line("Memory32Fixed (ReadWrite,");
720 	dsdt_line("  0x%08X,         // Address Base", base);
721 	dsdt_line("  0x%08X,         // Address Length", length);
722 	dsdt_line("  )");
723 }
724 
725 static int
726 basl_fwrite_dsdt(FILE *fp)
727 {
728 	dsdt_fp = fp;
729 	dsdt_error = 0;
730 	dsdt_indent_level = 0;
731 
732 	dsdt_line("/*");
733 	dsdt_line(" * bhyve DSDT template");
734 	dsdt_line(" */");
735 	dsdt_line("DefinitionBlock (\"bhyve_dsdt.aml\", \"DSDT\", 2,"
736 		 "\"BHYVE \", \"BVDSDT  \", 0x00000001)");
737 	dsdt_line("{");
738 	dsdt_line("  Name (_S5, Package ()");
739 	dsdt_line("  {");
740 	dsdt_line("      0x05,");
741 	dsdt_line("      Zero,");
742 	dsdt_line("  })");
743 
744 	pci_write_dsdt();
745 
746 	dsdt_line("");
747 	dsdt_line("  Scope (_SB.PC00)");
748 	dsdt_line("  {");
749 	dsdt_line("    Device (HPET)");
750 	dsdt_line("    {");
751 	dsdt_line("      Name (_HID, EISAID(\"PNP0103\"))");
752 	dsdt_line("      Name (_UID, 0)");
753 	dsdt_line("      Name (_CRS, ResourceTemplate ()");
754 	dsdt_line("      {");
755 	dsdt_indent(4);
756 	dsdt_fixed_mem32(0xFED00000, 0x400);
757 	dsdt_unindent(4);
758 	dsdt_line("      })");
759 	dsdt_line("    }");
760 	dsdt_line("  }");
761 
762 	vmgenc_write_dsdt();
763 
764 	dsdt_line("}");
765 
766 	if (dsdt_error != 0)
767 		return (dsdt_error);
768 
769 	EFFLUSH(fp);
770 
771 	return (0);
772 
773 err_exit:
774 	return (errno);
775 }
776 
777 static int
778 basl_open(struct basl_fio *bf, int suffix)
779 {
780 	int err;
781 
782 	err = 0;
783 
784 	if (suffix) {
785 		strlcpy(bf->f_name, basl_stemplate, MAXPATHLEN);
786 		bf->fd = mkstemps(bf->f_name, strlen(BHYVE_ASL_SUFFIX));
787 	} else {
788 		strlcpy(bf->f_name, basl_template, MAXPATHLEN);
789 		bf->fd = mkstemp(bf->f_name);
790 	}
791 
792 	if (bf->fd > 0) {
793 		bf->fp = fdopen(bf->fd, "w+");
794 		if (bf->fp == NULL) {
795 			unlink(bf->f_name);
796 			close(bf->fd);
797 		}
798 	} else {
799 		err = 1;
800 	}
801 
802 	return (err);
803 }
804 
805 static void
806 basl_close(struct basl_fio *bf)
807 {
808 
809 	if (!basl_keep_temps)
810 		unlink(bf->f_name);
811 	fclose(bf->fp);
812 }
813 
814 static int
815 basl_start(struct basl_fio *in, struct basl_fio *out)
816 {
817 	int err;
818 
819 	err = basl_open(in, 0);
820 	if (!err) {
821 		err = basl_open(out, 1);
822 		if (err) {
823 			basl_close(in);
824 		}
825 	}
826 
827 	return (err);
828 }
829 
830 static void
831 basl_end(struct basl_fio *in, struct basl_fio *out)
832 {
833 
834 	basl_close(in);
835 	basl_close(out);
836 }
837 
838 static int
839 basl_load(struct vmctx *ctx, int fd, uint64_t off)
840 {
841 	struct stat sb;
842 	void *addr;
843 
844 	if (fstat(fd, &sb) < 0)
845 		return (errno);
846 
847 	addr = calloc(1, sb.st_size);
848 	if (addr == NULL)
849 		return (EFAULT);
850 
851 	if (read(fd, addr, sb.st_size) < 0)
852 		return (errno);
853 
854 	struct basl_table *table;
855 
856 	uint8_t name[ACPI_NAMESEG_SIZE + 1] = { 0 };
857 	memcpy(name, addr, sizeof(name) - 1 /* last char is '\0' */);
858 	BASL_EXEC(
859 	    basl_table_create(&table, ctx, name, BASL_TABLE_ALIGNMENT, off));
860 	BASL_EXEC(basl_table_append_bytes(table, addr, sb.st_size));
861 
862 	return (0);
863 }
864 
865 static int
866 basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *), uint64_t offset)
867 {
868 	struct basl_fio io[2];
869 	static char iaslbuf[3*MAXPATHLEN + 10];
870 	const char *fmt;
871 	int err;
872 
873 	err = basl_start(&io[0], &io[1]);
874 	if (!err) {
875 		err = (*fwrite_section)(io[0].fp);
876 
877 		if (!err) {
878 			/*
879 			 * iasl sends the results of the compilation to
880 			 * stdout. Shut this down by using the shell to
881 			 * redirect stdout to /dev/null, unless the user
882 			 * has requested verbose output for debugging
883 			 * purposes
884 			 */
885 			fmt = basl_verbose_iasl ?
886 				"%s -p %s %s" :
887 				"/bin/sh -c \"%s -p %s %s\" 1> /dev/null";
888 
889 			snprintf(iaslbuf, sizeof(iaslbuf),
890 				 fmt,
891 				 BHYVE_ASL_COMPILER,
892 				 io[1].f_name, io[0].f_name);
893 			err = system(iaslbuf);
894 
895 			if (!err) {
896 				/*
897 				 * Copy the aml output file into guest
898 				 * memory at the specified location
899 				 */
900 				err = basl_load(ctx, io[1].fd, offset);
901 			}
902 		}
903 		basl_end(&io[0], &io[1]);
904 	}
905 
906 	return (err);
907 }
908 
909 static int
910 basl_make_templates(void)
911 {
912 	const char *tmpdir;
913 	int err;
914 	int len;
915 
916 	err = 0;
917 
918 	/*
919 	 *
920 	 */
921 	if ((tmpdir = getenv("BHYVE_TMPDIR")) == NULL || *tmpdir == '\0' ||
922 	    (tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') {
923 		tmpdir = _PATH_TMP;
924 	}
925 
926 	len = strlen(tmpdir);
927 
928 	if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) {
929 		strcpy(basl_template, tmpdir);
930 		while (len > 0 && basl_template[len - 1] == '/')
931 			len--;
932 		basl_template[len] = '/';
933 		strcpy(&basl_template[len + 1], BHYVE_ASL_TEMPLATE);
934 	} else
935 		err = E2BIG;
936 
937 	if (!err) {
938 		/*
939 		 * len has been intialized (and maybe adjusted) above
940 		 */
941 		if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1 +
942 		     sizeof(BHYVE_ASL_SUFFIX)) < MAXPATHLEN) {
943 			strcpy(basl_stemplate, tmpdir);
944 			basl_stemplate[len] = '/';
945 			strcpy(&basl_stemplate[len + 1], BHYVE_ASL_TEMPLATE);
946 			len = strlen(basl_stemplate);
947 			strcpy(&basl_stemplate[len], BHYVE_ASL_SUFFIX);
948 		} else
949 			err = E2BIG;
950 	}
951 
952 	return (err);
953 }
954 
955 static int
956 build_dsdt(struct vmctx *const ctx)
957 {
958 	BASL_EXEC(basl_compile(ctx, basl_fwrite_dsdt, DSDT_OFFSET));
959 
960 	return (0);
961 }
962 
963 int
964 acpi_build(struct vmctx *ctx, int ncpu)
965 {
966 	int err;
967 
968 	basl_ncpu = ncpu;
969 
970 	err = vm_get_hpet_capabilities(ctx, &hpet_capabilities);
971 	if (err != 0)
972 		return (err);
973 
974 	/*
975 	 * For debug, allow the user to have iasl compiler output sent
976 	 * to stdout rather than /dev/null
977 	 */
978 	if (getenv("BHYVE_ACPI_VERBOSE_IASL"))
979 		basl_verbose_iasl = 1;
980 
981 	/*
982 	 * Allow the user to keep the generated ASL files for debugging
983 	 * instead of deleting them following use
984 	 */
985 	if (getenv("BHYVE_ACPI_KEEPTMPS"))
986 		basl_keep_temps = 1;
987 
988 	BASL_EXEC(basl_init());
989 
990 	BASL_EXEC(basl_make_templates());
991 
992 	/*
993 	 * Run through all the ASL files, compiling them and
994 	 * copying them into guest memory
995 	 */
996 	BASL_EXEC(basl_compile(ctx, basl_fwrite_rsdp, 0));
997 	BASL_EXEC(basl_compile(ctx, basl_fwrite_rsdt, RSDT_OFFSET));
998 	BASL_EXEC(basl_compile(ctx, basl_fwrite_xsdt, XSDT_OFFSET));
999 	BASL_EXEC(basl_compile(ctx, basl_fwrite_madt, MADT_OFFSET));
1000 	BASL_EXEC(basl_compile(ctx, basl_fwrite_fadt, FADT_OFFSET));
1001 	BASL_EXEC(basl_compile(ctx, basl_fwrite_hpet, HPET_OFFSET));
1002 	BASL_EXEC(basl_compile(ctx, basl_fwrite_mcfg, MCFG_OFFSET));
1003 	BASL_EXEC(basl_compile(ctx, basl_fwrite_facs, FACS_OFFSET));
1004 	BASL_EXEC(build_dsdt(ctx));
1005 
1006 	BASL_EXEC(basl_finish());
1007 
1008 	return (0);
1009 }
1010