xref: /freebsd/usr.sbin/bhyve/acpi.c (revision 09d325677d53a12c79a43664ff29871e92247629)
1 /*-
2  * Copyright (c) 2012 NetApp, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
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) by writing out ASL template files for each of
34  * the tables and the compiling them to AML with the Intel iasl compiler.
35  * The AML files are then read into guest memory.
36  *
37  *  The tables are placed in the guest's ROM area just below 1MB physical,
38  * above the MPTable.
39  *
40  *  Layout
41  *  ------
42  *   RSDP  ->   0xf0400    (36 bytes fixed)
43  *     RSDT  ->   0xf0440    (36 bytes + 4*N table addrs, 2 used)
44  *     XSDT  ->   0xf0480    (36 bytes + 8*N table addrs, 2 used)
45  *       MADT  ->   0xf0500  (depends on #CPUs)
46  *       FADT  ->   0xf0600  (268 bytes)
47  *         FACS  ->   0xf0780 (64 bytes)
48  *         DSDT  ->   0xf0800 (variable - can go up to 0x100000)
49  */
50 
51 #include <sys/cdefs.h>
52 __FBSDID("$FreeBSD$");
53 
54 #include <sys/param.h>
55 #include <sys/errno.h>
56 #include <sys/stat.h>
57 
58 #include <paths.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 #include "bhyverun.h"
65 #include "acpi.h"
66 
67 /*
68  * Define the base address of the ACPI tables, and the offsets to
69  * the individual tables
70  */
71 #define BHYVE_ACPI_BASE		0xf0400
72 #define RSDT_OFFSET		0x040
73 #define XSDT_OFFSET		0x080
74 #define MADT_OFFSET		0x100
75 #define FADT_OFFSET		0x200
76 #define FACS_OFFSET		0x380
77 #define DSDT_OFFSET		0x400
78 
79 #define	BHYVE_ASL_TEMPLATE	"bhyve.XXXXXXX"
80 #define BHYVE_ASL_SUFFIX	".aml"
81 #define BHYVE_ASL_COMPILER	"/usr/sbin/iasl"
82 
83 #define BHYVE_PM_TIMER_ADDR	0x408
84 
85 static int basl_keep_temps;
86 static int basl_verbose_iasl;
87 static int basl_ncpu;
88 static uint32_t basl_acpi_base = BHYVE_ACPI_BASE;
89 
90 /*
91  * Contains the full pathname of the template to be passed
92  * to mkstemp/mktemps(3)
93  */
94 static char basl_template[MAXPATHLEN];
95 static char basl_stemplate[MAXPATHLEN];
96 
97 struct basl_fio {
98 	int	fd;
99 	FILE	*fp;
100 	char	f_name[MAXPATHLEN];
101 };
102 
103 #define EFPRINTF(...) \
104 	err = fprintf(__VA_ARGS__); if (err < 0) goto err_exit;
105 
106 #define EFFLUSH(x) \
107 	err = fflush(x); if (err != 0) goto err_exit;
108 
109 static int
110 basl_fwrite_rsdp(FILE *fp)
111 {
112 	int err;
113 
114 	err = 0;
115 
116 	EFPRINTF(fp, "/*\n");
117 	EFPRINTF(fp, " * bhyve RSDP template\n");
118 	EFPRINTF(fp, " */\n");
119 	EFPRINTF(fp, "[0008]\t\tSignature : \"RSD PTR \"\n");
120 	EFPRINTF(fp, "[0001]\t\tChecksum : 43\n");
121 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
122 	EFPRINTF(fp, "[0001]\t\tRevision : 02\n");
123 	EFPRINTF(fp, "[0004]\t\tRSDT Address : %08X\n",
124 	    basl_acpi_base + RSDT_OFFSET);
125 	EFPRINTF(fp, "[0004]\t\tLength : 00000024\n");
126 	EFPRINTF(fp, "[0008]\t\tXSDT Address : 00000000%08X\n",
127 	    basl_acpi_base + XSDT_OFFSET);
128 	EFPRINTF(fp, "[0001]\t\tExtended Checksum : 00\n");
129 	EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
130 
131 	EFFLUSH(fp);
132 
133 	return (0);
134 
135 err_exit:
136 	return (errno);
137 }
138 
139 static int
140 basl_fwrite_rsdt(FILE *fp)
141 {
142 	int err;
143 
144 	err = 0;
145 
146 	EFPRINTF(fp, "/*\n");
147 	EFPRINTF(fp, " * bhyve RSDT template\n");
148 	EFPRINTF(fp, " */\n");
149 	EFPRINTF(fp, "[0004]\t\tSignature : \"RSDT\"\n");
150 	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
151 	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
152 	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
153 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
154 	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVRSDT  \"\n");
155 	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
156 	/* iasl will fill in the compiler ID/revision fields */
157 	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
158 	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
159 	EFPRINTF(fp, "\n");
160 
161 	/* Add in pointers to the MADT and FADT */
162 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : %08X\n",
163 	    basl_acpi_base + MADT_OFFSET);
164 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : %08X\n",
165 	    basl_acpi_base + FADT_OFFSET);
166 
167 	EFFLUSH(fp);
168 
169 	return (0);
170 
171 err_exit:
172 	return (errno);
173 }
174 
175 static int
176 basl_fwrite_xsdt(FILE *fp)
177 {
178 	int err;
179 
180 	err = 0;
181 
182 	EFPRINTF(fp, "/*\n");
183 	EFPRINTF(fp, " * bhyve XSDT template\n");
184 	EFPRINTF(fp, " */\n");
185 	EFPRINTF(fp, "[0004]\t\tSignature : \"XSDT\"\n");
186 	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
187 	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
188 	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
189 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
190 	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVXSDT  \"\n");
191 	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
192 	/* iasl will fill in the compiler ID/revision fields */
193 	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
194 	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
195 	EFPRINTF(fp, "\n");
196 
197 	/* Add in pointers to the MADT and FADT */
198 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : 00000000%08X\n",
199 	    basl_acpi_base + MADT_OFFSET);
200 	EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : 00000000%08X\n",
201 	    basl_acpi_base + FADT_OFFSET);
202 
203 	EFFLUSH(fp);
204 
205 	return (0);
206 
207 err_exit:
208 	return (errno);
209 }
210 
211 static int
212 basl_fwrite_madt(FILE *fp)
213 {
214 	int err;
215 	int i;
216 
217 	err = 0;
218 
219 	EFPRINTF(fp, "/*\n");
220 	EFPRINTF(fp, " * bhyve MADT template\n");
221 	EFPRINTF(fp, " */\n");
222 	EFPRINTF(fp, "[0004]\t\tSignature : \"APIC\"\n");
223 	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
224 	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
225 	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
226 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
227 	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVMADT  \"\n");
228 	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
229 
230 	/* iasl will fill in the compiler ID/revision fields */
231 	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
232 	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
233 	EFPRINTF(fp, "\n");
234 
235 	EFPRINTF(fp, "[0004]\t\tLocal Apic Address : FEE00000\n");
236 	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
237 	EFPRINTF(fp, "\t\t\tPC-AT Compatibility : 1\n");
238 	EFPRINTF(fp, "\n");
239 
240 	/* Add a Processor Local APIC entry for each CPU */
241 	for (i = 0; i < basl_ncpu; i++) {
242 		EFPRINTF(fp, "[0001]\t\tSubtable Type : 00\n");
243 		EFPRINTF(fp, "[0001]\t\tLength : 08\n");
244 		/* iasl expects hex values for the proc and apic id's */
245 		EFPRINTF(fp, "[0001]\t\tProcessor ID : %02x\n", i);
246 		EFPRINTF(fp, "[0001]\t\tLocal Apic ID : %02x\n", i);
247 		EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
248 		EFPRINTF(fp, "\t\t\tProcessor Enabled : 1\n");
249 		EFPRINTF(fp, "\n");
250 	}
251 
252 	/* Always a single IOAPIC entry, with ID ncpu+1 */
253 	EFPRINTF(fp, "[0001]\t\tSubtable Type : 01\n");
254 	EFPRINTF(fp, "[0001]\t\tLength : 0C\n");
255 	/* iasl expects a hex value for the i/o apic id */
256 	EFPRINTF(fp, "[0001]\t\tI/O Apic ID : %02x\n", basl_ncpu);
257 	EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
258 	EFPRINTF(fp, "[0004]\t\tAddress : fec00000\n");
259 	EFPRINTF(fp, "[0004]\t\tInterrupt : 00000000\n");
260 	EFPRINTF(fp, "\n");
261 
262 	/* Override the 8259 chained vector. XXX maybe not needed */
263 	EFPRINTF(fp, "[0001]\t\tSubtable Type : 02\n");
264 	EFPRINTF(fp, "[0001]\t\tLength : 0A\n");
265 	EFPRINTF(fp, "[0001]\t\tBus : 00\n");
266 	EFPRINTF(fp, "[0001]\t\tSource : 09\n");
267 	EFPRINTF(fp, "[0004]\t\tInterrupt : 00000009\n");
268 	EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0000\n");
269 	EFPRINTF(fp, "\t\t\tPolarity : 0\n");
270 	EFPRINTF(fp, "\t\t\tTrigger Mode : 0\n");
271 	EFPRINTF(fp, "\n");
272 
273 	EFFLUSH(fp);
274 
275 	return (0);
276 
277 err_exit:
278 	return (errno);
279 }
280 
281 static int
282 basl_fwrite_fadt(FILE *fp)
283 {
284 	int err;
285 
286 	err = 0;
287 
288 	EFPRINTF(fp, "/*\n");
289 	EFPRINTF(fp, " * bhyve FADT template\n");
290 	EFPRINTF(fp, " */\n");
291 	EFPRINTF(fp, "[0004]\t\tSignature : \"FACP\"\n");
292 	EFPRINTF(fp, "[0004]\t\tTable Length : 0000010C\n");
293 	EFPRINTF(fp, "[0001]\t\tRevision : 05\n");
294 	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
295 	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
296 	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVFACP  \"\n");
297 	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
298 	/* iasl will fill in the compiler ID/revision fields */
299 	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
300 	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
301 	EFPRINTF(fp, "\n");
302 
303 	EFPRINTF(fp, "[0004]\t\tFACS Address : %08X\n",
304 	    basl_acpi_base + FACS_OFFSET);
305 	EFPRINTF(fp, "[0004]\t\tDSDT Address : %08X\n",
306 	    basl_acpi_base + DSDT_OFFSET);
307 	EFPRINTF(fp, "[0001]\t\tModel : 00\n");
308 	EFPRINTF(fp, "[0001]\t\tPM Profile : 00 [Unspecified]\n");
309 	EFPRINTF(fp, "[0002]\t\tSCI Interrupt : 0009\n");
310 	EFPRINTF(fp, "[0004]\t\tSMI Command Port : 00000000\n");
311 	EFPRINTF(fp, "[0001]\t\tACPI Enable Value : 00\n");
312 	EFPRINTF(fp, "[0001]\t\tACPI Disable Value : 00\n");
313 	EFPRINTF(fp, "[0001]\t\tS4BIOS Command : 00\n");
314 	EFPRINTF(fp, "[0001]\t\tP-State Control : 00\n");
315 	EFPRINTF(fp, "[0004]\t\tPM1A Event Block Address : 00000000\n");
316 	EFPRINTF(fp, "[0004]\t\tPM1B Event Block Address : 00000000\n");
317 	EFPRINTF(fp, "[0004]\t\tPM1A Control Block Address : 00000000\n");
318 	EFPRINTF(fp, "[0004]\t\tPM1B Control Block Address : 00000000\n");
319 	EFPRINTF(fp, "[0004]\t\tPM2 Control Block Address : 00000000\n");
320 	EFPRINTF(fp, "[0004]\t\tPM Timer Block Address : %08X\n",
321 		 BHYVE_PM_TIMER_ADDR);
322 	EFPRINTF(fp, "[0004]\t\tGPE0 Block Address : 00000000\n");
323 	EFPRINTF(fp, "[0004]\t\tGPE1 Block Address : 00000000\n");
324 	EFPRINTF(fp, "[0001]\t\tPM1 Event Block Length : 04\n");
325 	EFPRINTF(fp, "[0001]\t\tPM1 Control Block Length : 02\n");
326 	EFPRINTF(fp, "[0001]\t\tPM2 Control Block Length : 00\n");
327 	EFPRINTF(fp, "[0001]\t\tPM Timer Block Length : 04\n");
328 	EFPRINTF(fp, "[0001]\t\tGPE0 Block Length : 00\n");
329 	EFPRINTF(fp, "[0001]\t\tGPE1 Block Length : 00\n");
330 	EFPRINTF(fp, "[0001]\t\tGPE1 Base Offset : 00\n");
331 	EFPRINTF(fp, "[0001]\t\t_CST Support : 00\n");
332 	EFPRINTF(fp, "[0002]\t\tC2 Latency : 0000\n");
333 	EFPRINTF(fp, "[0002]\t\tC3 Latency : 0000\n");
334 	EFPRINTF(fp, "[0002]\t\tCPU Cache Size : 0000\n");
335 	EFPRINTF(fp, "[0002]\t\tCache Flush Stride : 0000\n");
336 	EFPRINTF(fp, "[0001]\t\tDuty Cycle Offset : 00\n");
337 	EFPRINTF(fp, "[0001]\t\tDuty Cycle Width : 00\n");
338 	EFPRINTF(fp, "[0001]\t\tRTC Day Alarm Index : 00\n");
339 	EFPRINTF(fp, "[0001]\t\tRTC Month Alarm Index : 00\n");
340 	EFPRINTF(fp, "[0001]\t\tRTC Century Index : 00\n");
341 	EFPRINTF(fp, "[0002]\t\tBoot Flags (decoded below) : 0000\n");
342 	EFPRINTF(fp, "\t\t\tLegacy Devices Supported (V2) : 0\n");
343 	EFPRINTF(fp, "\t\t\t8042 Present on ports 60/64 (V2) : 0\n");
344 	EFPRINTF(fp, "\t\t\tVGA Not Present (V4) : 1\n");
345 	EFPRINTF(fp, "\t\t\tMSI Not Supported (V4) : 0\n");
346 	EFPRINTF(fp, "\t\t\tPCIe ASPM Not Supported (V4) : 1\n");
347 	EFPRINTF(fp, "\t\t\tCMOS RTC Not Present (V5) : 0\n");
348 	EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
349 	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
350 	EFPRINTF(fp, "\t\t\tWBINVD instruction is operational (V1) : 1\n");
351 	EFPRINTF(fp, "\t\t\tWBINVD flushes all caches (V1) : 0\n");
352 	EFPRINTF(fp, "\t\t\tAll CPUs support C1 (V1) : 0\n");
353 	EFPRINTF(fp, "\t\t\tC2 works on MP system (V1) : 0\n");
354 	EFPRINTF(fp, "\t\t\tControl Method Power Button (V1) : 1\n");
355 	EFPRINTF(fp, "\t\t\tControl Method Sleep Button (V1) : 1\n");
356 	EFPRINTF(fp, "\t\t\tRTC wake not in fixed reg space (V1) : 0\n");
357 	EFPRINTF(fp, "\t\t\tRTC can wake system from S4 (V1) : 0\n");
358 	EFPRINTF(fp, "\t\t\t32-bit PM Timer (V1) : 1\n");
359 	EFPRINTF(fp, "\t\t\tDocking Supported (V1) : 0\n");
360 	EFPRINTF(fp, "\t\t\tReset Register Supported (V2) : 0\n");
361 	EFPRINTF(fp, "\t\t\tSealed Case (V3) : 0\n");
362 	EFPRINTF(fp, "\t\t\tHeadless - No Video (V3) : 1\n");
363 	EFPRINTF(fp, "\t\t\tUse native instr after SLP_TYPx (V3) : 0\n");
364 	EFPRINTF(fp, "\t\t\tPCIEXP_WAK Bits Supported (V4) : 0\n");
365 	EFPRINTF(fp, "\t\t\tUse Platform Timer (V4) : 0\n");
366 	EFPRINTF(fp, "\t\t\tRTC_STS valid on S4 wake (V4) : 0\n");
367 	EFPRINTF(fp, "\t\t\tRemote Power-on capable (V4) : 0\n");
368 	EFPRINTF(fp, "\t\t\tUse APIC Cluster Model (V4) : 0\n");
369 	EFPRINTF(fp, "\t\t\tUse APIC Physical Destination Mode (V4) : 1\n");
370 	EFPRINTF(fp, "\t\t\tHardware Reduced (V5) : 0\n");
371 	EFPRINTF(fp, "\t\t\tLow Power S0 Idle (V5) : 0\n");
372 	EFPRINTF(fp, "\n");
373 
374 	EFPRINTF(fp,
375 	    "[0012]\t\tReset Register : [Generic Address Structure]\n");
376 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
377 	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
378 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
379 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
380 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000001\n");
381 	EFPRINTF(fp, "\n");
382 
383 	EFPRINTF(fp, "[0001]\t\tValue to cause reset : 00\n");
384 	EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
385 	EFPRINTF(fp, "[0008]\t\tFACS Address : 00000000%08X\n",
386 	    basl_acpi_base + FACS_OFFSET);
387 	EFPRINTF(fp, "[0008]\t\tDSDT Address : 00000000%08X\n",
388 	    basl_acpi_base + DSDT_OFFSET);
389 	EFPRINTF(fp,
390 	    "[0012]\t\tPM1A Event Block : [Generic Address Structure]\n");
391 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
392 	EFPRINTF(fp, "[0001]\t\tBit Width : 20\n");
393 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
394 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
395 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000001\n");
396 	EFPRINTF(fp, "\n");
397 
398 	EFPRINTF(fp,
399 	    "[0012]\t\tPM1B Event Block : [Generic Address Structure]\n");
400 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
401 	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
402 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
403 	EFPRINTF(fp,
404 	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
405 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
406 	EFPRINTF(fp, "\n");
407 
408 	EFPRINTF(fp,
409 	    "[0012]\t\tPM1A Control Block : [Generic Address Structure]\n");
410 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
411 	EFPRINTF(fp, "[0001]\t\tBit Width : 10\n");
412 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
413 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
414 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000001\n");
415 	EFPRINTF(fp, "\n");
416 
417 	EFPRINTF(fp,
418 	    "[0012]\t\tPM1B Control Block : [Generic Address Structure]\n");
419 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
420 	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
421 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
422 	EFPRINTF(fp,
423 	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
424 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
425 	EFPRINTF(fp, "\n");
426 
427 	EFPRINTF(fp,
428 	    "[0012]\t\tPM2 Control Block : [Generic Address Structure]\n");
429 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
430 	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
431 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
432 	EFPRINTF(fp,
433 	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
434 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
435 	EFPRINTF(fp, "\n");
436 
437 	/* Valid for bhyve */
438 	EFPRINTF(fp,
439 	    "[0012]\t\tPM Timer Block : [Generic Address Structure]\n");
440 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
441 	EFPRINTF(fp, "[0001]\t\tBit Width : 32\n");
442 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
443 	EFPRINTF(fp,
444 	    "[0001]\t\tEncoded Access Width : 03 [DWord Access:32]\n");
445 	EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
446 	    BHYVE_PM_TIMER_ADDR);
447 	EFPRINTF(fp, "\n");
448 
449 	EFPRINTF(fp, "[0012]\t\tGPE0 Block : [Generic Address Structure]\n");
450 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
451 	EFPRINTF(fp, "[0001]\t\tBit Width : 80\n");
452 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
453 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
454 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
455 	EFPRINTF(fp, "\n");
456 
457 	EFPRINTF(fp, "[0012]\t\tGPE1 Block : [Generic Address Structure]\n");
458 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
459 	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
460 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
461 	EFPRINTF(fp,
462 	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
463 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
464 	EFPRINTF(fp, "\n");
465 
466 	EFPRINTF(fp,
467 	   "[0012]\t\tSleep Control Register : [Generic Address Structure]\n");
468 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
469 	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
470 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
471 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
472 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
473 	EFPRINTF(fp, "\n");
474 
475 	EFPRINTF(fp,
476 	    "[0012]\t\tSleep Status Register : [Generic Address Structure]\n");
477 	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
478 	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
479 	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
480 	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
481 	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
482 
483 	EFFLUSH(fp);
484 
485 	return (0);
486 
487 err_exit:
488 	return (errno);
489 }
490 
491 static int
492 basl_fwrite_facs(FILE *fp)
493 {
494 	int err;
495 
496 	err = 0;
497 
498 	EFPRINTF(fp, "/*\n");
499 	EFPRINTF(fp, " * bhyve FACS template\n");
500 	EFPRINTF(fp, " */\n");
501 	EFPRINTF(fp, "[0004]\t\tSignature : \"FACS\"\n");
502 	EFPRINTF(fp, "[0004]\t\tLength : 00000040\n");
503 	EFPRINTF(fp, "[0004]\t\tHardware Signature : 00000000\n");
504 	EFPRINTF(fp, "[0004]\t\t32 Firmware Waking Vector : 00000000\n");
505 	EFPRINTF(fp, "[0004]\t\tGlobal Lock : 00000000\n");
506 	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
507 	EFPRINTF(fp, "\t\t\tS4BIOS Support Present : 0\n");
508 	EFPRINTF(fp, "\t\t\t64-bit Wake Supported (V2) : 0\n");
509 	EFPRINTF(fp,
510 	    "[0008]\t\t64 Firmware Waking Vector : 0000000000000000\n");
511 	EFPRINTF(fp, "[0001]\t\tVersion : 02\n");
512 	EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
513 	EFPRINTF(fp, "[0004]\t\tOspmFlags (decoded below) : 00000000\n");
514 	EFPRINTF(fp, "\t\t\t64-bit Wake Env Required (V2) : 0\n");
515 
516 	EFFLUSH(fp);
517 
518 	return (0);
519 
520 err_exit:
521 	return (errno);
522 }
523 
524 static int
525 basl_fwrite_dsdt(FILE *fp)
526 {
527 	int err;
528 
529 	err = 0;
530 
531 	EFPRINTF(fp, "/*\n");
532 	EFPRINTF(fp, " * bhyve DSDT template\n");
533 	EFPRINTF(fp, " */\n");
534 	EFPRINTF(fp, "DefinitionBlock (\"bhyve_dsdt.aml\", \"DSDT\", 2,"
535 		 "\"BHYVE \", \"BVDSDT  \", 0x00000001)\n");
536 	EFPRINTF(fp, "{\n");
537 	EFPRINTF(fp, "  Scope (_SB)\n");
538 	EFPRINTF(fp, "  {\n");
539 	EFPRINTF(fp, "    Device (PCI0)\n");
540 	EFPRINTF(fp, "    {\n");
541 	EFPRINTF(fp, "      Name (_HID, EisaId (\"PNP0A03\"))\n");
542 	EFPRINTF(fp, "      Name (_ADR, Zero)\n");
543 	EFPRINTF(fp, "      Name (_UID, One)\n");
544 	EFPRINTF(fp, "      Name (_CRS, ResourceTemplate ()\n");
545 	EFPRINTF(fp, "      {\n");
546 	EFPRINTF(fp, "        WordBusNumber (ResourceProducer, MinFixed,"
547 		 "MaxFixed, PosDecode,\n");
548 	EFPRINTF(fp, "            0x0000,             // Granularity\n");
549 	EFPRINTF(fp, "            0x0000,             // Range Minimum\n");
550 	EFPRINTF(fp, "            0x00FF,             // Range Maximum\n");
551 	EFPRINTF(fp, "            0x0000,             // Transl Offset\n");
552 	EFPRINTF(fp, "            0x0100,             // Length\n");
553 	EFPRINTF(fp, "            ,, )\n");
554 	EFPRINTF(fp, "         IO (Decode16,\n");
555 	EFPRINTF(fp, "            0x0CF8,             // Range Minimum\n");
556 	EFPRINTF(fp, "            0x0CF8,             // Range Maximum\n");
557 	EFPRINTF(fp, "            0x01,               // Alignment\n");
558 	EFPRINTF(fp, "            0x08,               // Length\n");
559 	EFPRINTF(fp, "            )\n");
560 	EFPRINTF(fp, "         WordIO (ResourceProducer, MinFixed, MaxFixed,"
561 		 "PosDecode, EntireRange,\n");
562 	EFPRINTF(fp, "            0x0000,             // Granularity\n");
563 	EFPRINTF(fp, "            0x0000,             // Range Minimum\n");
564 	EFPRINTF(fp, "            0x0CF7,             // Range Maximum\n");
565 	EFPRINTF(fp, "            0x0000,             // Transl Offset\n");
566 	EFPRINTF(fp, "            0x0CF8,             // Length\n");
567 	EFPRINTF(fp, "            ,, , TypeStatic)\n");
568 	EFPRINTF(fp, "         WordIO (ResourceProducer, MinFixed, MaxFixed,"
569 		 "PosDecode, EntireRange,\n");
570 	EFPRINTF(fp, "            0x0000,             // Granularity\n");
571 	EFPRINTF(fp, "            0x0D00,             // Range Minimum\n");
572 	EFPRINTF(fp, "            0xFFFF,             // Range Maximum\n");
573 	EFPRINTF(fp, "            0x0000,             // Transl Offset\n");
574 	EFPRINTF(fp, "            0xF300,             // Length\n");
575 	EFPRINTF(fp, "             ,, , TypeStatic)\n");
576 	EFPRINTF(fp, "          })\n");
577 	EFPRINTF(fp, "     }\n");
578 	EFPRINTF(fp, "  }\n");
579 	EFPRINTF(fp, "\n");
580 	EFPRINTF(fp, "  Scope (_SB.PCI0)\n");
581 	EFPRINTF(fp, "  {\n");
582 	EFPRINTF(fp, "    Device (ISA)\n");
583 	EFPRINTF(fp, "    {\n");
584 	EFPRINTF(fp, "      Name (_ADR, 0x00010000)\n");
585 	EFPRINTF(fp, "      OperationRegion (P40C, PCI_Config, 0x60, 0x04)\n");
586 	EFPRINTF(fp, "    }\n");
587 	EFPRINTF(fp, "  }\n");
588 	EFPRINTF(fp, "\n");
589 	EFPRINTF(fp, "  Scope (_SB.PCI0.ISA)\n");
590         EFPRINTF(fp, "  {\n");
591 	EFPRINTF(fp, "    Device (RTC)\n");
592         EFPRINTF(fp, "    {\n");
593 	EFPRINTF(fp, "      Name (_HID, EisaId (\"PNP0B00\"))\n");
594 	EFPRINTF(fp, "      Name (_CRS, ResourceTemplate ()\n");
595 	EFPRINTF(fp, "      {\n");
596 	EFPRINTF(fp, "        IO (Decode16,\n");
597 	EFPRINTF(fp, "            0x0070,             // Range Minimum\n");
598 	EFPRINTF(fp, "            0x0070,             // Range Maximum\n");
599 	EFPRINTF(fp, "            0x10,               // Alignment\n");
600 	EFPRINTF(fp, "            0x02,               // Length\n");
601 	EFPRINTF(fp, "            )\n");
602 	EFPRINTF(fp, "        IRQNoFlags ()\n");
603 	EFPRINTF(fp, "            {8}\n");
604 	EFPRINTF(fp, "        IO (Decode16,\n");
605 	EFPRINTF(fp, "            0x0072,             // Range Minimum\n");
606 	EFPRINTF(fp, "            0x0072,             // Range Maximum\n");
607 	EFPRINTF(fp, "            0x02,               // Alignment\n");
608 	EFPRINTF(fp, "            0x06,               // Length\n");
609 	EFPRINTF(fp, "            )\n");
610 	EFPRINTF(fp, "      })\n");
611         EFPRINTF(fp, "    }\n");
612 	EFPRINTF(fp, "  }\n");
613 	EFPRINTF(fp, "}\n");
614 
615 	EFFLUSH(fp);
616 
617 	return (0);
618 
619 err_exit:
620 	return (errno);
621 }
622 
623 static int
624 basl_open(struct basl_fio *bf, int suffix)
625 {
626 	int err;
627 
628 	err = 0;
629 
630 	if (suffix) {
631 		strncpy(bf->f_name, basl_stemplate, MAXPATHLEN);
632 		bf->fd = mkstemps(bf->f_name, strlen(BHYVE_ASL_SUFFIX));
633 	} else {
634 		strncpy(bf->f_name, basl_template, MAXPATHLEN);
635 		bf->fd = mkstemp(bf->f_name);
636 	}
637 
638 	if (bf->fd > 0) {
639 		bf->fp = fdopen(bf->fd, "w+");
640 		if (bf->fp == NULL) {
641 			unlink(bf->f_name);
642 			close(bf->fd);
643 		}
644 	} else {
645 		err = 1;
646 	}
647 
648 	return (err);
649 }
650 
651 static void
652 basl_close(struct basl_fio *bf)
653 {
654 
655 	if (!basl_keep_temps)
656 		unlink(bf->f_name);
657 	fclose(bf->fp);
658 }
659 
660 static int
661 basl_start(struct basl_fio *in, struct basl_fio *out)
662 {
663 	int err;
664 
665 	err = basl_open(in, 0);
666 	if (!err) {
667 		err = basl_open(out, 1);
668 		if (err) {
669 			basl_close(in);
670 		}
671 	}
672 
673 	return (err);
674 }
675 
676 static void
677 basl_end(struct basl_fio *in, struct basl_fio *out)
678 {
679 
680 	basl_close(in);
681 	basl_close(out);
682 }
683 
684 static int
685 basl_load(struct vmctx *ctx, int fd, uint64_t off)
686 {
687 	struct stat sb;
688 	void *gaddr;
689 
690 	if (fstat(fd, &sb) < 0)
691 		return (errno);
692 
693 	gaddr = paddr_guest2host(ctx, basl_acpi_base + off, sb.st_size);
694 	if (gaddr == NULL)
695 		return (EFAULT);
696 
697 	if (read(fd, gaddr, sb.st_size) < 0)
698 		return (errno);
699 
700 	return (0);
701 }
702 
703 static int
704 basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *), uint64_t offset)
705 {
706 	struct basl_fio io[2];
707 	static char iaslbuf[3*MAXPATHLEN + 10];
708 	char *fmt;
709 	int err;
710 
711 	err = basl_start(&io[0], &io[1]);
712 	if (!err) {
713 		err = (*fwrite_section)(io[0].fp);
714 
715 		if (!err) {
716 			/*
717 			 * iasl sends the results of the compilation to
718 			 * stdout. Shut this down by using the shell to
719 			 * redirect stdout to /dev/null, unless the user
720 			 * has requested verbose output for debugging
721 			 * purposes
722 			 */
723 			fmt = basl_verbose_iasl ?
724 				"%s -p %s %s" :
725 				"/bin/sh -c \"%s -p %s %s\" 1> /dev/null";
726 
727 			snprintf(iaslbuf, sizeof(iaslbuf),
728 				 fmt,
729 				 BHYVE_ASL_COMPILER,
730 				 io[1].f_name, io[0].f_name);
731 			err = system(iaslbuf);
732 
733 			if (!err) {
734 				/*
735 				 * Copy the aml output file into guest
736 				 * memory at the specified location
737 				 */
738 				err = basl_load(ctx, io[1].fd, offset);
739 			}
740 		}
741 		basl_end(&io[0], &io[1]);
742 	}
743 
744 	return (err);
745 }
746 
747 static int
748 basl_make_templates(void)
749 {
750 	const char *tmpdir;
751 	int err;
752 	int len;
753 
754 	err = 0;
755 
756 	/*
757 	 *
758 	 */
759 	if ((tmpdir = getenv("BHYVE_TMPDIR")) == NULL || *tmpdir == '\0' ||
760 	    (tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') {
761 		tmpdir = _PATH_TMP;
762 	}
763 
764 	len = strlen(tmpdir);
765 
766 	if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) {
767 		strcpy(basl_template, tmpdir);
768 		while (len > 0 && basl_template[len - 1] == '/')
769 			len--;
770 		basl_template[len] = '/';
771 		strcpy(&basl_template[len + 1], BHYVE_ASL_TEMPLATE);
772 	} else
773 		err = E2BIG;
774 
775 	if (!err) {
776 		/*
777 		 * len has been intialized (and maybe adjusted) above
778 		 */
779 		if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1 +
780 		     sizeof(BHYVE_ASL_SUFFIX)) < MAXPATHLEN) {
781 			strcpy(basl_stemplate, tmpdir);
782 			basl_stemplate[len] = '/';
783 			strcpy(&basl_stemplate[len + 1], BHYVE_ASL_TEMPLATE);
784 			len = strlen(basl_stemplate);
785 			strcpy(&basl_stemplate[len], BHYVE_ASL_SUFFIX);
786 		} else
787 			err = E2BIG;
788 	}
789 
790 	return (err);
791 }
792 
793 static struct {
794 	int	(*wsect)(FILE *fp);
795 	uint64_t  offset;
796 } basl_ftables[] =
797 {
798 	{ basl_fwrite_rsdp, 0},
799 	{ basl_fwrite_rsdt, RSDT_OFFSET },
800 	{ basl_fwrite_xsdt, XSDT_OFFSET },
801 	{ basl_fwrite_madt, MADT_OFFSET },
802 	{ basl_fwrite_fadt, FADT_OFFSET },
803 	{ basl_fwrite_facs, FACS_OFFSET },
804 	{ basl_fwrite_dsdt, DSDT_OFFSET },
805 	{ NULL }
806 };
807 
808 int
809 acpi_build(struct vmctx *ctx, int ncpu)
810 {
811 	int err;
812 	int i;
813 
814 	err = 0;
815 	basl_ncpu = ncpu;
816 
817 	/*
818 	 * For debug, allow the user to have iasl compiler output sent
819 	 * to stdout rather than /dev/null
820 	 */
821 	if (getenv("BHYVE_ACPI_VERBOSE_IASL"))
822 		basl_verbose_iasl = 1;
823 
824 	/*
825 	 * Allow the user to keep the generated ASL files for debugging
826 	 * instead of deleting them following use
827 	 */
828 	if (getenv("BHYVE_ACPI_KEEPTMPS"))
829 		basl_keep_temps = 1;
830 
831 	i = 0;
832 	err = basl_make_templates();
833 
834 	/*
835 	 * Run through all the ASL files, compiling them and
836 	 * copying them into guest memory
837 	 */
838 	while (!err && basl_ftables[i].wsect != NULL) {
839 		err = basl_compile(ctx, basl_ftables[i].wsect,
840 				   basl_ftables[i].offset);
841 		i++;
842 	}
843 
844 	return (err);
845 }
846