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