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