acpi.c revision 244167
1230557Sjimharris/*-
2230557Sjimharris * Copyright (c) 2012 NetApp, Inc.
3230557Sjimharris * All rights reserved.
4230557Sjimharris *
5230557Sjimharris * Redistribution and use in source and binary forms, with or without
6230557Sjimharris * modification, are permitted provided that the following conditions
7230557Sjimharris * are met:
8230557Sjimharris * 1. Redistributions of source code must retain the above copyright
9230557Sjimharris *    notice, this list of conditions and the following disclaimer.
10230557Sjimharris * 2. Redistributions in binary form must reproduce the above copyright
11230557Sjimharris *    notice, this list of conditions and the following disclaimer in the
12230557Sjimharris *    documentation and/or other materials provided with the distribution.
13230557Sjimharris *
14230557Sjimharris * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
15230557Sjimharris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16230557Sjimharris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17230557Sjimharris * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
18230557Sjimharris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19230557Sjimharris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20230557Sjimharris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21230557Sjimharris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22230557Sjimharris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23230557Sjimharris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24230557Sjimharris * SUCH DAMAGE.
25230557Sjimharris *
26230557Sjimharris * $FreeBSD: projects/bhyve/usr.sbin/bhyve/acpi.c 244167 2012-12-13 01:58:11Z grehan $
27230557Sjimharris */
28230557Sjimharris
29230557Sjimharris/*
30230557Sjimharris * bhyve ACPI table generator.
31230557Sjimharris *
32230557Sjimharris * Create the minimal set of ACPI tables required to boot FreeBSD (and
33230557Sjimharris * hopefully other o/s's) by writing out ASL template files for each of
34230557Sjimharris * the tables and the compiling them to AML with the Intel iasl compiler.
35230557Sjimharris * The AML files are then read into guest memory.
36230557Sjimharris *
37230557Sjimharris *  The tables are placed in the guest's ROM area just below 1MB physical,
38230557Sjimharris * above the MPTable.
39230557Sjimharris *
40230557Sjimharris *  Layout
41230557Sjimharris *  ------
42230557Sjimharris *   RSDP  ->   0xf0400    (36 bytes fixed)
43230557Sjimharris *     RSDT  ->   0xf0440    (36 bytes + 4*N table addrs, 2 used)
44230557Sjimharris *     XSDT  ->   0xf0480    (36 bytes + 8*N table addrs, 2 used)
45230557Sjimharris *       MADT  ->   0xf0500  (depends on #CPUs)
46230557Sjimharris *       FADT  ->   0xf0600  (268 bytes)
47230557Sjimharris *         FACS  ->   0xf0780 (64 bytes)
48230557Sjimharris *         DSDT  ->   0xf0800 (variable - can go up to 0x100000)
49230557Sjimharris */
50230557Sjimharris
51230557Sjimharris#include <sys/cdefs.h>
52230557Sjimharris__FBSDID("$FreeBSD: projects/bhyve/usr.sbin/bhyve/acpi.c 244167 2012-12-13 01:58:11Z grehan $");
53230557Sjimharris
54230557Sjimharris#include <sys/param.h>
55230557Sjimharris#include <sys/errno.h>
56230557Sjimharris#include <sys/stat.h>
57230557Sjimharris
58230557Sjimharris#include <paths.h>
59230557Sjimharris#include <stdio.h>
60230557Sjimharris#include <stdlib.h>
61230557Sjimharris#include <string.h>
62230557Sjimharris#include <unistd.h>
63230557Sjimharris
64230557Sjimharris#include "bhyverun.h"
65230557Sjimharris#include "acpi.h"
66230557Sjimharris
67230557Sjimharris/*
68230557Sjimharris * Define the base address of the ACPI tables, and the offsets to
69230557Sjimharris * the individual tables
70230557Sjimharris */
71230557Sjimharris#define BHYVE_ACPI_BASE		0xf0400
72230557Sjimharris#define RSDT_OFFSET		0x040
73230557Sjimharris#define XSDT_OFFSET		0x080
74230557Sjimharris#define MADT_OFFSET		0x100
75230557Sjimharris#define FADT_OFFSET		0x200
76230557Sjimharris#define FACS_OFFSET		0x380
77230557Sjimharris#define DSDT_OFFSET		0x400
78230557Sjimharris
79230557Sjimharris#define	BHYVE_ASL_TEMPLATE	"bhyve.XXXXXXX"
80230557Sjimharris#define BHYVE_ASL_SUFFIX	".aml"
81230557Sjimharris#define BHYVE_ASL_COMPILER	"/usr/sbin/iasl"
82230557Sjimharris
83230557Sjimharris#define BHYVE_PM_TIMER_ADDR	0x408
84230557Sjimharris
85230557Sjimharrisstatic int basl_keep_temps;
86230557Sjimharrisstatic int basl_verbose_iasl;
87230557Sjimharrisstatic int basl_ncpu;
88230557Sjimharrisstatic uint32_t basl_acpi_base = BHYVE_ACPI_BASE;
89230557Sjimharris
90230557Sjimharris/*
91230557Sjimharris * Contains the full pathname of the template to be passed
92230557Sjimharris * to mkstemp/mktemps(3)
93230557Sjimharris */
94230557Sjimharrisstatic char basl_template[MAXPATHLEN];
95230557Sjimharrisstatic char basl_stemplate[MAXPATHLEN];
96230557Sjimharris
97230557Sjimharrisstruct basl_fio {
98230557Sjimharris	int	fd;
99230557Sjimharris	FILE	*fp;
100230557Sjimharris	char	f_name[MAXPATHLEN];
101230557Sjimharris};
102230557Sjimharris
103230557Sjimharris#define EFPRINTF(...) \
104230557Sjimharris	err = fprintf(__VA_ARGS__); if (err < 0) goto err_exit;
105230557Sjimharris
106230557Sjimharris#define EFFLUSH(x) \
107230557Sjimharris	err = fflush(x); if (err != 0) goto err_exit;
108230557Sjimharris
109230557Sjimharrisstatic int
110230557Sjimharrisbasl_fwrite_rsdp(FILE *fp)
111230557Sjimharris{
112230557Sjimharris	int err;
113230557Sjimharris
114230557Sjimharris	err = 0;
115230557Sjimharris
116230557Sjimharris	EFPRINTF(fp, "/*\n");
117230557Sjimharris	EFPRINTF(fp, " * bhyve RSDP template\n");
118230557Sjimharris	EFPRINTF(fp, " */\n");
119230557Sjimharris	EFPRINTF(fp, "[0008]\t\tSignature : \"RSD PTR \"\n");
120230557Sjimharris	EFPRINTF(fp, "[0001]\t\tChecksum : 43\n");
121230557Sjimharris	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
122230557Sjimharris	EFPRINTF(fp, "[0001]\t\tRevision : 02\n");
123230557Sjimharris	EFPRINTF(fp, "[0004]\t\tRSDT Address : %08X\n",
124230557Sjimharris	    basl_acpi_base + RSDT_OFFSET);
125230557Sjimharris	EFPRINTF(fp, "[0004]\t\tLength : 00000024\n");
126230557Sjimharris	EFPRINTF(fp, "[0008]\t\tXSDT Address : 00000000%08X\n",
127230557Sjimharris	    basl_acpi_base + XSDT_OFFSET);
128230557Sjimharris	EFPRINTF(fp, "[0001]\t\tExtended Checksum : 00\n");
129230557Sjimharris	EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
130230557Sjimharris
131230557Sjimharris	EFFLUSH(fp);
132230557Sjimharris
133230557Sjimharris	return (0);
134230557Sjimharris
135230557Sjimharriserr_exit:
136230557Sjimharris	return (errno);
137230557Sjimharris}
138230557Sjimharris
139230557Sjimharrisstatic int
140230557Sjimharrisbasl_fwrite_rsdt(FILE *fp)
141230557Sjimharris{
142230557Sjimharris	int err;
143230557Sjimharris
144230557Sjimharris	err = 0;
145230557Sjimharris
146230557Sjimharris	EFPRINTF(fp, "/*\n");
147230557Sjimharris	EFPRINTF(fp, " * bhyve RSDT template\n");
148230557Sjimharris	EFPRINTF(fp, " */\n");
149230557Sjimharris	EFPRINTF(fp, "[0004]\t\tSignature : \"RSDT\"\n");
150230557Sjimharris	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
151230557Sjimharris	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
152230557Sjimharris	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
153230557Sjimharris	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
154230557Sjimharris	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVRSDT  \"\n");
155230557Sjimharris	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
156230557Sjimharris	/* iasl will fill in the compiler ID/revision fields */
157230557Sjimharris	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
158230557Sjimharris	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
159230557Sjimharris	EFPRINTF(fp, "\n");
160230557Sjimharris
161230557Sjimharris	/* Add in pointers to the MADT and FADT */
162230557Sjimharris	EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : %08X\n",
163230557Sjimharris	    basl_acpi_base + MADT_OFFSET);
164230557Sjimharris	EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : %08X\n",
165230557Sjimharris	    basl_acpi_base + FADT_OFFSET);
166230557Sjimharris
167230557Sjimharris	EFFLUSH(fp);
168230557Sjimharris
169230557Sjimharris	return (0);
170230557Sjimharris
171230557Sjimharriserr_exit:
172230557Sjimharris	return (errno);
173230557Sjimharris}
174230557Sjimharris
175230557Sjimharrisstatic int
176230557Sjimharrisbasl_fwrite_xsdt(FILE *fp)
177230557Sjimharris{
178230557Sjimharris	int err;
179230557Sjimharris
180230557Sjimharris	err = 0;
181230557Sjimharris
182230557Sjimharris	EFPRINTF(fp, "/*\n");
183230557Sjimharris	EFPRINTF(fp, " * bhyve XSDT template\n");
184230557Sjimharris	EFPRINTF(fp, " */\n");
185230557Sjimharris	EFPRINTF(fp, "[0004]\t\tSignature : \"XSDT\"\n");
186230557Sjimharris	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
187230557Sjimharris	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
188230557Sjimharris	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
189230557Sjimharris	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
190230557Sjimharris	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVXSDT  \"\n");
191230557Sjimharris	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
192230557Sjimharris	/* iasl will fill in the compiler ID/revision fields */
193230557Sjimharris	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
194230557Sjimharris	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
195230557Sjimharris	EFPRINTF(fp, "\n");
196230557Sjimharris
197230557Sjimharris	/* Add in pointers to the MADT and FADT */
198230557Sjimharris	EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : 00000000%08X\n",
199230557Sjimharris	    basl_acpi_base + MADT_OFFSET);
200230557Sjimharris	EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : 00000000%08X\n",
201230557Sjimharris	    basl_acpi_base + FADT_OFFSET);
202230557Sjimharris
203230557Sjimharris	EFFLUSH(fp);
204230557Sjimharris
205230557Sjimharris	return (0);
206230557Sjimharris
207230557Sjimharriserr_exit:
208230557Sjimharris	return (errno);
209230557Sjimharris}
210230557Sjimharris
211230557Sjimharrisstatic int
212230557Sjimharrisbasl_fwrite_madt(FILE *fp)
213230557Sjimharris{
214230557Sjimharris	int err;
215230557Sjimharris	int i;
216230557Sjimharris
217230557Sjimharris	err = 0;
218230557Sjimharris
219230557Sjimharris	EFPRINTF(fp, "/*\n");
220230557Sjimharris	EFPRINTF(fp, " * bhyve MADT template\n");
221230557Sjimharris	EFPRINTF(fp, " */\n");
222230557Sjimharris	EFPRINTF(fp, "[0004]\t\tSignature : \"APIC\"\n");
223230557Sjimharris	EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
224230557Sjimharris	EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
225230557Sjimharris	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
226230557Sjimharris	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
227230557Sjimharris	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVMADT  \"\n");
228230557Sjimharris	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
229230557Sjimharris
230230557Sjimharris	/* iasl will fill in the compiler ID/revision fields */
231230557Sjimharris	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
232230557Sjimharris	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
233230557Sjimharris	EFPRINTF(fp, "\n");
234230557Sjimharris
235230557Sjimharris	EFPRINTF(fp, "[0004]\t\tLocal Apic Address : FEE00000\n");
236230557Sjimharris	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
237230557Sjimharris	EFPRINTF(fp, "\t\t\tPC-AT Compatibility : 1\n");
238230557Sjimharris	EFPRINTF(fp, "\n");
239230557Sjimharris
240230557Sjimharris	/* Add a Processor Local APIC entry for each CPU */
241230557Sjimharris	for (i = 0; i < basl_ncpu; i++) {
242230557Sjimharris		EFPRINTF(fp, "[0001]\t\tSubtable Type : 00\n");
243230557Sjimharris		EFPRINTF(fp, "[0001]\t\tLength : 08\n");
244230557Sjimharris		EFPRINTF(fp, "[0001]\t\tProcessor ID : %02d\n", i);
245230557Sjimharris		EFPRINTF(fp, "[0001]\t\tLocal Apic ID : %02d\n", i);
246230557Sjimharris		EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
247230557Sjimharris		EFPRINTF(fp, "\t\t\tProcessor Enabled : 1\n");
248		EFPRINTF(fp, "\n");
249	}
250
251	/* Always a single IOAPIC entry, with ID ncpu+1 */
252	EFPRINTF(fp, "[0001]\t\tSubtable Type : 01\n");
253	EFPRINTF(fp, "[0001]\t\tLength : 0C\n");
254	EFPRINTF(fp, "[0001]\t\tI/O Apic ID : %02d\n", basl_ncpu);
255	EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
256	EFPRINTF(fp, "[0004]\t\tAddress : fec00000\n");
257	EFPRINTF(fp, "[0004]\t\tInterrupt : 00000000\n");
258	EFPRINTF(fp, "\n");
259
260	/* Override the 8259 chained vector. XXX maybe not needed */
261	EFPRINTF(fp, "[0001]\t\tSubtable Type : 02\n");
262	EFPRINTF(fp, "[0001]\t\tLength : 0A\n");
263	EFPRINTF(fp, "[0001]\t\tBus : 00\n");
264	EFPRINTF(fp, "[0001]\t\tSource : 09\n");
265	EFPRINTF(fp, "[0004]\t\tInterrupt : 00000009\n");
266	EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0000\n");
267	EFPRINTF(fp, "\t\t\tPolarity : 0\n");
268	EFPRINTF(fp, "\t\t\tTrigger Mode : 0\n");
269	EFPRINTF(fp, "\n");
270
271	EFFLUSH(fp);
272
273	return (0);
274
275err_exit:
276	return (errno);
277}
278
279static int
280basl_fwrite_fadt(FILE *fp)
281{
282	int err;
283
284	err = 0;
285
286	EFPRINTF(fp, "/*\n");
287	EFPRINTF(fp, " * bhyve FADT template\n");
288	EFPRINTF(fp, " */\n");
289	EFPRINTF(fp, "[0004]\t\tSignature : \"FACP\"\n");
290	EFPRINTF(fp, "[0004]\t\tTable Length : 0000010C\n");
291	EFPRINTF(fp, "[0001]\t\tRevision : 05\n");
292	EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
293	EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
294	EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVFACP  \"\n");
295	EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
296	/* iasl will fill in the compiler ID/revision fields */
297	EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
298	EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
299	EFPRINTF(fp, "\n");
300
301	EFPRINTF(fp, "[0004]\t\tFACS Address : %08X\n",
302	    basl_acpi_base + FACS_OFFSET);
303	EFPRINTF(fp, "[0004]\t\tDSDT Address : %08X\n",
304	    basl_acpi_base + DSDT_OFFSET);
305	EFPRINTF(fp, "[0001]\t\tModel : 00\n");
306	EFPRINTF(fp, "[0001]\t\tPM Profile : 00 [Unspecified]\n");
307	EFPRINTF(fp, "[0002]\t\tSCI Interrupt : 0009\n");
308	EFPRINTF(fp, "[0004]\t\tSMI Command Port : 00000000\n");
309	EFPRINTF(fp, "[0001]\t\tACPI Enable Value : 00\n");
310	EFPRINTF(fp, "[0001]\t\tACPI Disable Value : 00\n");
311	EFPRINTF(fp, "[0001]\t\tS4BIOS Command : 00\n");
312	EFPRINTF(fp, "[0001]\t\tP-State Control : 00\n");
313	EFPRINTF(fp, "[0004]\t\tPM1A Event Block Address : 00000000\n");
314	EFPRINTF(fp, "[0004]\t\tPM1B Event Block Address : 00000000\n");
315	EFPRINTF(fp, "[0004]\t\tPM1A Control Block Address : 00000000\n");
316	EFPRINTF(fp, "[0004]\t\tPM1B Control Block Address : 00000000\n");
317	EFPRINTF(fp, "[0004]\t\tPM2 Control Block Address : 00000000\n");
318	EFPRINTF(fp, "[0004]\t\tPM Timer Block Address : %08X\n",
319		 BHYVE_PM_TIMER_ADDR);
320	EFPRINTF(fp, "[0004]\t\tGPE0 Block Address : 00000000\n");
321	EFPRINTF(fp, "[0004]\t\tGPE1 Block Address : 00000000\n");
322	EFPRINTF(fp, "[0001]\t\tPM1 Event Block Length : 04\n");
323	EFPRINTF(fp, "[0001]\t\tPM1 Control Block Length : 02\n");
324	EFPRINTF(fp, "[0001]\t\tPM2 Control Block Length : 00\n");
325	EFPRINTF(fp, "[0001]\t\tPM Timer Block Length : 04\n");
326	EFPRINTF(fp, "[0001]\t\tGPE0 Block Length : 00\n");
327	EFPRINTF(fp, "[0001]\t\tGPE1 Block Length : 00\n");
328	EFPRINTF(fp, "[0001]\t\tGPE1 Base Offset : 00\n");
329	EFPRINTF(fp, "[0001]\t\t_CST Support : 00\n");
330	EFPRINTF(fp, "[0002]\t\tC2 Latency : 0000\n");
331	EFPRINTF(fp, "[0002]\t\tC3 Latency : 0000\n");
332	EFPRINTF(fp, "[0002]\t\tCPU Cache Size : 0000\n");
333	EFPRINTF(fp, "[0002]\t\tCache Flush Stride : 0000\n");
334	EFPRINTF(fp, "[0001]\t\tDuty Cycle Offset : 00\n");
335	EFPRINTF(fp, "[0001]\t\tDuty Cycle Width : 00\n");
336	EFPRINTF(fp, "[0001]\t\tRTC Day Alarm Index : 00\n");
337	EFPRINTF(fp, "[0001]\t\tRTC Month Alarm Index : 00\n");
338	EFPRINTF(fp, "[0001]\t\tRTC Century Index : 00\n");
339	EFPRINTF(fp, "[0002]\t\tBoot Flags (decoded below) : 0000\n");
340	EFPRINTF(fp, "\t\t\tLegacy Devices Supported (V2) : 0\n");
341	EFPRINTF(fp, "\t\t\t8042 Present on ports 60/64 (V2) : 0\n");
342	EFPRINTF(fp, "\t\t\tVGA Not Present (V4) : 1\n");
343	EFPRINTF(fp, "\t\t\tMSI Not Supported (V4) : 0\n");
344	EFPRINTF(fp, "\t\t\tPCIe ASPM Not Supported (V4) : 1\n");
345	EFPRINTF(fp, "\t\t\tCMOS RTC Not Present (V5) : 0\n");
346	EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
347	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
348	EFPRINTF(fp, "\t\t\tWBINVD instruction is operational (V1) : 1\n");
349	EFPRINTF(fp, "\t\t\tWBINVD flushes all caches (V1) : 0\n");
350	EFPRINTF(fp, "\t\t\tAll CPUs support C1 (V1) : 0\n");
351	EFPRINTF(fp, "\t\t\tC2 works on MP system (V1) : 0\n");
352	EFPRINTF(fp, "\t\t\tControl Method Power Button (V1) : 1\n");
353	EFPRINTF(fp, "\t\t\tControl Method Sleep Button (V1) : 1\n");
354	EFPRINTF(fp, "\t\t\tRTC wake not in fixed reg space (V1) : 0\n");
355	EFPRINTF(fp, "\t\t\tRTC can wake system from S4 (V1) : 0\n");
356	EFPRINTF(fp, "\t\t\t32-bit PM Timer (V1) : 1\n");
357	EFPRINTF(fp, "\t\t\tDocking Supported (V1) : 0\n");
358	EFPRINTF(fp, "\t\t\tReset Register Supported (V2) : 0\n");
359	EFPRINTF(fp, "\t\t\tSealed Case (V3) : 0\n");
360	EFPRINTF(fp, "\t\t\tHeadless - No Video (V3) : 1\n");
361	EFPRINTF(fp, "\t\t\tUse native instr after SLP_TYPx (V3) : 0\n");
362	EFPRINTF(fp, "\t\t\tPCIEXP_WAK Bits Supported (V4) : 0\n");
363	EFPRINTF(fp, "\t\t\tUse Platform Timer (V4) : 0\n");
364	EFPRINTF(fp, "\t\t\tRTC_STS valid on S4 wake (V4) : 0\n");
365	EFPRINTF(fp, "\t\t\tRemote Power-on capable (V4) : 0\n");
366	EFPRINTF(fp, "\t\t\tUse APIC Cluster Model (V4) : 0\n");
367	EFPRINTF(fp, "\t\t\tUse APIC Physical Destination Mode (V4) : 1\n");
368	EFPRINTF(fp, "\t\t\tHardware Reduced (V5) : 0\n");
369	EFPRINTF(fp, "\t\t\tLow Power S0 Idle (V5) : 0\n");
370	EFPRINTF(fp, "\n");
371
372	EFPRINTF(fp,
373	    "[0012]\t\tReset Register : [Generic Address Structure]\n");
374	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
375	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
376	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
377	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
378	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000001\n");
379	EFPRINTF(fp, "\n");
380
381	EFPRINTF(fp, "[0001]\t\tValue to cause reset : 00\n");
382	EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
383	EFPRINTF(fp, "[0008]\t\tFACS Address : 00000000%08X\n",
384	    basl_acpi_base + FACS_OFFSET);
385	EFPRINTF(fp, "[0008]\t\tDSDT Address : 00000000%08X\n",
386	    basl_acpi_base + DSDT_OFFSET);
387	EFPRINTF(fp,
388	    "[0012]\t\tPM1A Event Block : [Generic Address Structure]\n");
389	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
390	EFPRINTF(fp, "[0001]\t\tBit Width : 20\n");
391	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
392	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
393	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000001\n");
394	EFPRINTF(fp, "\n");
395
396	EFPRINTF(fp,
397	    "[0012]\t\tPM1B Event Block : [Generic Address Structure]\n");
398	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
399	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
400	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
401	EFPRINTF(fp,
402	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
403	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
404	EFPRINTF(fp, "\n");
405
406	EFPRINTF(fp,
407	    "[0012]\t\tPM1A Control Block : [Generic Address Structure]\n");
408	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
409	EFPRINTF(fp, "[0001]\t\tBit Width : 10\n");
410	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
411	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
412	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000001\n");
413	EFPRINTF(fp, "\n");
414
415	EFPRINTF(fp,
416	    "[0012]\t\tPM1B Control Block : [Generic Address Structure]\n");
417	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
418	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
419	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
420	EFPRINTF(fp,
421	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
422	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
423	EFPRINTF(fp, "\n");
424
425	EFPRINTF(fp,
426	    "[0012]\t\tPM2 Control Block : [Generic Address Structure]\n");
427	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
428	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
429	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
430	EFPRINTF(fp,
431	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
432	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
433	EFPRINTF(fp, "\n");
434
435	/* Valid for bhyve */
436	EFPRINTF(fp,
437	    "[0012]\t\tPM Timer Block : [Generic Address Structure]\n");
438	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
439	EFPRINTF(fp, "[0001]\t\tBit Width : 32\n");
440	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
441	EFPRINTF(fp,
442	    "[0001]\t\tEncoded Access Width : 03 [DWord Access:32]\n");
443	EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
444	    BHYVE_PM_TIMER_ADDR);
445	EFPRINTF(fp, "\n");
446
447	EFPRINTF(fp, "[0012]\t\tGPE0 Block : [Generic Address Structure]\n");
448	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
449	EFPRINTF(fp, "[0001]\t\tBit Width : 80\n");
450	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
451	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
452	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
453	EFPRINTF(fp, "\n");
454
455	EFPRINTF(fp, "[0012]\t\tGPE1 Block : [Generic Address Structure]\n");
456	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
457	EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
458	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
459	EFPRINTF(fp,
460	    "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
461	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
462	EFPRINTF(fp, "\n");
463
464	EFPRINTF(fp,
465	   "[0012]\t\tSleep Control Register : [Generic Address Structure]\n");
466	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
467	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
468	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
469	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
470	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
471	EFPRINTF(fp, "\n");
472
473	EFPRINTF(fp,
474	    "[0012]\t\tSleep Status Register : [Generic Address Structure]\n");
475	EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
476	EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
477	EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
478	EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
479	EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
480
481	EFFLUSH(fp);
482
483	return (0);
484
485err_exit:
486	return (errno);
487}
488
489static int
490basl_fwrite_facs(FILE *fp)
491{
492	int err;
493
494	err = 0;
495
496	EFPRINTF(fp, "/*\n");
497	EFPRINTF(fp, " * bhyve FACS template\n");
498	EFPRINTF(fp, " */\n");
499	EFPRINTF(fp, "[0004]\t\tSignature : \"FACS\"\n");
500	EFPRINTF(fp, "[0004]\t\tLength : 00000040\n");
501	EFPRINTF(fp, "[0004]\t\tHardware Signature : 00000000\n");
502	EFPRINTF(fp, "[0004]\t\t32 Firmware Waking Vector : 00000000\n");
503	EFPRINTF(fp, "[0004]\t\tGlobal Lock : 00000000\n");
504	EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
505	EFPRINTF(fp, "\t\t\tS4BIOS Support Present : 0\n");
506	EFPRINTF(fp, "\t\t\t64-bit Wake Supported (V2) : 0\n");
507	EFPRINTF(fp,
508	    "[0008]\t\t64 Firmware Waking Vector : 0000000000000000\n");
509	EFPRINTF(fp, "[0001]\t\tVersion : 02\n");
510	EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
511	EFPRINTF(fp, "[0004]\t\tOspmFlags (decoded below) : 00000000\n");
512	EFPRINTF(fp, "\t\t\t64-bit Wake Env Required (V2) : 0\n");
513
514	EFFLUSH(fp);
515
516	return (0);
517
518err_exit:
519	return (errno);
520}
521
522static int
523basl_fwrite_dsdt(FILE *fp)
524{
525	int err;
526
527	err = 0;
528
529	EFPRINTF(fp, "/*\n");
530	EFPRINTF(fp, " * bhyve DSDT template\n");
531	EFPRINTF(fp, " */\n");
532	EFPRINTF(fp, "DefinitionBlock (\"bhyve_dsdt.aml\", \"DSDT\", 2,"
533		 "\"BHYVE \", \"BVDSDT  \", 0x00000001)\n");
534	EFPRINTF(fp, "{\n");
535	EFPRINTF(fp, "  Scope (_SB)\n");
536	EFPRINTF(fp, "  {\n");
537	EFPRINTF(fp, "    Device (PCI0)\n");
538	EFPRINTF(fp, "    {\n");
539	EFPRINTF(fp, "      Name (_HID, EisaId (\"PNP0A03\"))\n");
540	EFPRINTF(fp, "      Name (_ADR, Zero)\n");
541	EFPRINTF(fp, "      Name (_UID, One)\n");
542	EFPRINTF(fp, "      Name (_CRS, ResourceTemplate ()\n");
543	EFPRINTF(fp, "      {\n");
544	EFPRINTF(fp, "        WordBusNumber (ResourceProducer, MinFixed,"
545		 "MaxFixed, PosDecode,\n");
546	EFPRINTF(fp, "            0x0000,             // Granularity\n");
547	EFPRINTF(fp, "            0x0000,             // Range Minimum\n");
548	EFPRINTF(fp, "            0x00FF,             // Range Maximum\n");
549	EFPRINTF(fp, "            0x0000,             // Transl Offset\n");
550	EFPRINTF(fp, "            0x0100,             // Length\n");
551	EFPRINTF(fp, "            ,, )\n");
552	EFPRINTF(fp, "         IO (Decode16,\n");
553	EFPRINTF(fp, "            0x0CF8,             // Range Minimum\n");
554	EFPRINTF(fp, "            0x0CF8,             // Range Maximum\n");
555	EFPRINTF(fp, "            0x01,               // Alignment\n");
556	EFPRINTF(fp, "            0x08,               // Length\n");
557	EFPRINTF(fp, "            )\n");
558	EFPRINTF(fp, "         WordIO (ResourceProducer, MinFixed, MaxFixed,"
559		 "PosDecode, EntireRange,\n");
560	EFPRINTF(fp, "            0x0000,             // Granularity\n");
561	EFPRINTF(fp, "            0x0000,             // Range Minimum\n");
562	EFPRINTF(fp, "            0x0CF7,             // Range Maximum\n");
563	EFPRINTF(fp, "            0x0000,             // Transl Offset\n");
564	EFPRINTF(fp, "            0x0CF8,             // Length\n");
565	EFPRINTF(fp, "            ,, , TypeStatic)\n");
566	EFPRINTF(fp, "         WordIO (ResourceProducer, MinFixed, MaxFixed,"
567		 "PosDecode, EntireRange,\n");
568	EFPRINTF(fp, "            0x0000,             // Granularity\n");
569	EFPRINTF(fp, "            0x0D00,             // Range Minimum\n");
570	EFPRINTF(fp, "            0xFFFF,             // Range Maximum\n");
571	EFPRINTF(fp, "            0x0000,             // Transl Offset\n");
572	EFPRINTF(fp, "            0xF300,             // Length\n");
573	EFPRINTF(fp, "             ,, , TypeStatic)\n");
574	EFPRINTF(fp, "          })\n");
575	EFPRINTF(fp, "     }\n");
576	EFPRINTF(fp, "  }\n");
577	EFPRINTF(fp, "\n");
578	EFPRINTF(fp, "  Scope (_SB.PCI0)\n");
579	EFPRINTF(fp, "  {\n");
580	EFPRINTF(fp, "    Device (ISA)\n");
581	EFPRINTF(fp, "    {\n");
582	EFPRINTF(fp, "      Name (_ADR, 0x00010000)\n");
583	EFPRINTF(fp, "      OperationRegion (P40C, PCI_Config, 0x60, 0x04)\n");
584	EFPRINTF(fp, "    }\n");
585	EFPRINTF(fp, "  }\n");
586	EFPRINTF(fp, "\n");
587	EFPRINTF(fp, "  Scope (_SB.PCI0.ISA)\n");
588        EFPRINTF(fp, "  {\n");
589	EFPRINTF(fp, "    Device (RTC)\n");
590        EFPRINTF(fp, "    {\n");
591	EFPRINTF(fp, "      Name (_HID, EisaId (\"PNP0B00\"))\n");
592	EFPRINTF(fp, "      Name (_CRS, ResourceTemplate ()\n");
593	EFPRINTF(fp, "      {\n");
594	EFPRINTF(fp, "        IO (Decode16,\n");
595	EFPRINTF(fp, "            0x0070,             // Range Minimum\n");
596	EFPRINTF(fp, "            0x0070,             // Range Maximum\n");
597	EFPRINTF(fp, "            0x10,               // Alignment\n");
598	EFPRINTF(fp, "            0x02,               // Length\n");
599	EFPRINTF(fp, "            )\n");
600	EFPRINTF(fp, "        IRQNoFlags ()\n");
601	EFPRINTF(fp, "            {8}\n");
602	EFPRINTF(fp, "        IO (Decode16,\n");
603	EFPRINTF(fp, "            0x0072,             // Range Minimum\n");
604	EFPRINTF(fp, "            0x0072,             // Range Maximum\n");
605	EFPRINTF(fp, "            0x02,               // Alignment\n");
606	EFPRINTF(fp, "            0x06,               // Length\n");
607	EFPRINTF(fp, "            )\n");
608	EFPRINTF(fp, "      })\n");
609        EFPRINTF(fp, "    }\n");
610	EFPRINTF(fp, "  }\n");
611	EFPRINTF(fp, "}\n");
612
613	EFFLUSH(fp);
614
615	return (0);
616
617err_exit:
618	return (errno);
619}
620
621static int
622basl_open(struct basl_fio *bf, int suffix)
623{
624	int err;
625
626	err = 0;
627
628	if (suffix) {
629		strncpy(bf->f_name, basl_stemplate, MAXPATHLEN);
630		bf->fd = mkstemps(bf->f_name, strlen(BHYVE_ASL_SUFFIX));
631	} else {
632		strncpy(bf->f_name, basl_template, MAXPATHLEN);
633		bf->fd = mkstemp(bf->f_name);
634	}
635
636	if (bf->fd > 0) {
637		bf->fp = fdopen(bf->fd, "w+");
638		if (bf->fp == NULL) {
639			unlink(bf->f_name);
640			close(bf->fd);
641		}
642	} else {
643		err = 1;
644	}
645
646	return (err);
647}
648
649static void
650basl_close(struct basl_fio *bf)
651{
652
653	if (!basl_keep_temps)
654		unlink(bf->f_name);
655	fclose(bf->fp);
656}
657
658static int
659basl_start(struct basl_fio *in, struct basl_fio *out)
660{
661	int err;
662
663	err = basl_open(in, 0);
664	if (!err) {
665		err = basl_open(out, 1);
666		if (err) {
667			basl_close(in);
668		}
669	}
670
671	return (err);
672}
673
674static void
675basl_end(struct basl_fio *in, struct basl_fio *out)
676{
677
678	basl_close(in);
679	basl_close(out);
680}
681
682static int
683basl_load(int fd, uint64_t off)
684{
685        struct stat sb;
686	int err;
687
688	err = 0;
689
690	if (fstat(fd, &sb) < 0 ||
691	    read(fd, paddr_guest2host(basl_acpi_base + off), sb.st_size) < 0)
692			err = errno;
693
694	return (err);
695}
696
697static int
698basl_compile(int (*fwrite_section)(FILE *fp), uint64_t offset)
699{
700	struct basl_fio io[2];
701	static char iaslbuf[3*MAXPATHLEN + 10];
702	char *fmt;
703	int err;
704
705	err = basl_start(&io[0], &io[1]);
706	if (!err) {
707		err = (*fwrite_section)(io[0].fp);
708
709		if (!err) {
710			/*
711			 * iasl sends the results of the compilation to
712			 * stdout. Shut this down by using the shell to
713			 * redirect stdout to /dev/null, unless the user
714			 * has requested verbose output for debugging
715			 * purposes
716			 */
717			fmt = basl_verbose_iasl ?
718				"%s -p %s %s" :
719				"/bin/sh -c \"%s -p %s %s\" 1> /dev/null";
720
721			snprintf(iaslbuf, sizeof(iaslbuf),
722				 fmt,
723				 BHYVE_ASL_COMPILER,
724				 io[1].f_name, io[0].f_name);
725			err = system(iaslbuf);
726
727			if (!err) {
728				/*
729				 * Copy the aml output file into guest
730				 * memory at the specified location
731				 */
732				err = basl_load(io[1].fd, offset);
733			}
734		}
735		basl_end(&io[0], &io[1]);
736	}
737
738	return (err);
739}
740
741static int
742basl_make_templates(void)
743{
744	const char *tmpdir;
745	int err;
746	int len;
747
748	err = 0;
749
750	/*
751	 *
752	 */
753	if ((tmpdir = getenv("BHYVE_TMPDIR")) == NULL || *tmpdir == '\0' ||
754	    (tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') {
755		tmpdir = _PATH_TMP;
756	}
757
758	len = strlen(tmpdir);
759
760	if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) {
761		strcpy(basl_template, tmpdir);
762		while (len > 0 && basl_template[len - 1] == '/')
763			len--;
764		basl_template[len] = '/';
765		strcpy(&basl_template[len + 1], BHYVE_ASL_TEMPLATE);
766	} else
767		err = E2BIG;
768
769	if (!err) {
770		/*
771		 * len has been intialized (and maybe adjusted) above
772		 */
773		if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1 +
774		     sizeof(BHYVE_ASL_SUFFIX)) < MAXPATHLEN) {
775			strcpy(basl_stemplate, tmpdir);
776			basl_stemplate[len] = '/';
777			strcpy(&basl_stemplate[len + 1], BHYVE_ASL_TEMPLATE);
778			len = strlen(basl_stemplate);
779			strcpy(&basl_stemplate[len], BHYVE_ASL_SUFFIX);
780		} else
781			err = E2BIG;
782	}
783
784	return (err);
785}
786
787static struct {
788	int	(*wsect)(FILE *fp);
789	uint64_t  offset;
790} basl_ftables[] =
791{
792	{ basl_fwrite_rsdp, 0},
793	{ basl_fwrite_rsdt, RSDT_OFFSET },
794	{ basl_fwrite_xsdt, XSDT_OFFSET },
795	{ basl_fwrite_madt, MADT_OFFSET },
796	{ basl_fwrite_fadt, FADT_OFFSET },
797	{ basl_fwrite_facs, FACS_OFFSET },
798	{ basl_fwrite_dsdt, DSDT_OFFSET },
799	{ NULL }
800};
801
802int
803acpi_build(struct vmctx *ctx, int ncpu, int ioapic)
804{
805	int err;
806	int i;
807
808	err = 0;
809	basl_ncpu = ncpu;
810
811	if (!ioapic) {
812		fprintf(stderr, "ACPI tables require an ioapic\n");
813		return (EINVAL);
814	}
815
816	/*
817	 * For debug, allow the user to have iasl compiler output sent
818	 * to stdout rather than /dev/null
819	 */
820	if (getenv("BHYVE_ACPI_VERBOSE_IASL"))
821		basl_verbose_iasl = 1;
822
823	/*
824	 * Allow the user to keep the generated ASL files for debugging
825	 * instead of deleting them following use
826	 */
827	if (getenv("BHYVE_ACPI_KEEPTMPS"))
828		basl_keep_temps = 1;
829
830	i = 0;
831	err = basl_make_templates();
832
833	/*
834	 * Run through all the ASL files, compiling them and
835	 * copying them into guest memory
836	 */
837	while (!err && basl_ftables[i].wsect != NULL) {
838		err = basl_compile(basl_ftables[i].wsect,
839				   basl_ftables[i].offset);
840		i++;
841	}
842
843	return (err);
844}
845