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