]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/acpi.c
Add support ps/2 scancodes for NumLock, ScrollLock and numerical keypad
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / acpi.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 /*
32  * bhyve ACPI table generator.
33  *
34  * Create the minimal set of ACPI tables required to boot FreeBSD (and
35  * hopefully other o/s's) by writing out ASL template files for each of
36  * the tables and the compiling them to AML with the Intel iasl compiler.
37  * The AML files are then read into guest memory.
38  *
39  *  The tables are placed in the guest's ROM area just below 1MB physical,
40  * above the MPTable.
41  *
42  *  Layout
43  *  ------
44  *   RSDP  ->   0xf2400    (36 bytes fixed)
45  *     RSDT  ->   0xf2440    (36 bytes + 4*7 table addrs, 4 used)
46  *     XSDT  ->   0xf2480    (36 bytes + 8*7 table addrs, 4 used)
47  *       MADT  ->   0xf2500  (depends on #CPUs)
48  *       FADT  ->   0xf2600  (268 bytes)
49  *       HPET  ->   0xf2740  (56 bytes)
50  *       MCFG  ->   0xf2780  (60 bytes)
51  *         FACS  ->   0xf27C0 (64 bytes)
52  *         DSDT  ->   0xf2800 (variable - can go up to 0x100000)
53  */
54
55 #include <sys/cdefs.h>
56 __FBSDID("$FreeBSD$");
57
58 #include <sys/param.h>
59 #include <sys/errno.h>
60 #include <sys/stat.h>
61
62 #include <paths.h>
63 #include <stdarg.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <unistd.h>
68
69 #include <machine/vmm.h>
70 #include <vmmapi.h>
71
72 #include "bhyverun.h"
73 #include "acpi.h"
74 #include "pci_emul.h"
75
76 /*
77  * Define the base address of the ACPI tables, and the offsets to
78  * the individual tables
79  */
80 #define BHYVE_ACPI_BASE         0xf2400
81 #define RSDT_OFFSET             0x040
82 #define XSDT_OFFSET             0x080
83 #define MADT_OFFSET             0x100
84 #define FADT_OFFSET             0x200
85 #define HPET_OFFSET             0x340
86 #define MCFG_OFFSET             0x380
87 #define FACS_OFFSET             0x3C0
88 #define DSDT_OFFSET             0x400
89
90 #define BHYVE_ASL_TEMPLATE      "bhyve.XXXXXXX"
91 #define BHYVE_ASL_SUFFIX        ".aml"
92 #define BHYVE_ASL_COMPILER      "/usr/sbin/iasl"
93
94 static int basl_keep_temps;
95 static int basl_verbose_iasl;
96 static int basl_ncpu;
97 static uint32_t basl_acpi_base = BHYVE_ACPI_BASE;
98 static uint32_t hpet_capabilities;
99
100 /*
101  * Contains the full pathname of the template to be passed
102  * to mkstemp/mktemps(3)
103  */
104 static char basl_template[MAXPATHLEN];
105 static char basl_stemplate[MAXPATHLEN];
106
107 /*
108  * State for dsdt_line(), dsdt_indent(), and dsdt_unindent().
109  */
110 static FILE *dsdt_fp;
111 static int dsdt_indent_level;
112 static int dsdt_error;
113
114 struct basl_fio {
115         int     fd;
116         FILE    *fp;
117         char    f_name[MAXPATHLEN];
118 };
119
120 #define EFPRINTF(...) \
121         if (fprintf(__VA_ARGS__) < 0) goto err_exit;
122
123 #define EFFLUSH(x) \
124         if (fflush(x) != 0) goto err_exit;
125
126 static int
127 basl_fwrite_rsdp(FILE *fp)
128 {
129         EFPRINTF(fp, "/*\n");
130         EFPRINTF(fp, " * bhyve RSDP template\n");
131         EFPRINTF(fp, " */\n");
132         EFPRINTF(fp, "[0008]\t\tSignature : \"RSD PTR \"\n");
133         EFPRINTF(fp, "[0001]\t\tChecksum : 43\n");
134         EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
135         EFPRINTF(fp, "[0001]\t\tRevision : 02\n");
136         EFPRINTF(fp, "[0004]\t\tRSDT Address : %08X\n",
137             basl_acpi_base + RSDT_OFFSET);
138         EFPRINTF(fp, "[0004]\t\tLength : 00000024\n");
139         EFPRINTF(fp, "[0008]\t\tXSDT Address : 00000000%08X\n",
140             basl_acpi_base + XSDT_OFFSET);
141         EFPRINTF(fp, "[0001]\t\tExtended Checksum : 00\n");
142         EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
143
144         EFFLUSH(fp);
145
146         return (0);
147
148 err_exit:
149         return (errno);
150 }
151
152 static int
153 basl_fwrite_rsdt(FILE *fp)
154 {
155         EFPRINTF(fp, "/*\n");
156         EFPRINTF(fp, " * bhyve RSDT template\n");
157         EFPRINTF(fp, " */\n");
158         EFPRINTF(fp, "[0004]\t\tSignature : \"RSDT\"\n");
159         EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
160         EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
161         EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
162         EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
163         EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVRSDT  \"\n");
164         EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
165         /* iasl will fill in the compiler ID/revision fields */
166         EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
167         EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
168         EFPRINTF(fp, "\n");
169
170         /* Add in pointers to the MADT, FADT and HPET */
171         EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : %08X\n",
172             basl_acpi_base + MADT_OFFSET);
173         EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : %08X\n",
174             basl_acpi_base + FADT_OFFSET);
175         EFPRINTF(fp, "[0004]\t\tACPI Table Address 2 : %08X\n",
176             basl_acpi_base + HPET_OFFSET);
177         EFPRINTF(fp, "[0004]\t\tACPI Table Address 3 : %08X\n",
178             basl_acpi_base + MCFG_OFFSET);
179
180         EFFLUSH(fp);
181
182         return (0);
183
184 err_exit:
185         return (errno);
186 }
187
188 static int
189 basl_fwrite_xsdt(FILE *fp)
190 {
191         EFPRINTF(fp, "/*\n");
192         EFPRINTF(fp, " * bhyve XSDT template\n");
193         EFPRINTF(fp, " */\n");
194         EFPRINTF(fp, "[0004]\t\tSignature : \"XSDT\"\n");
195         EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
196         EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
197         EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
198         EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
199         EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVXSDT  \"\n");
200         EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
201         /* iasl will fill in the compiler ID/revision fields */
202         EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
203         EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
204         EFPRINTF(fp, "\n");
205
206         /* Add in pointers to the MADT, FADT and HPET */
207         EFPRINTF(fp, "[0004]\t\tACPI Table Address 0 : 00000000%08X\n",
208             basl_acpi_base + MADT_OFFSET);
209         EFPRINTF(fp, "[0004]\t\tACPI Table Address 1 : 00000000%08X\n",
210             basl_acpi_base + FADT_OFFSET);
211         EFPRINTF(fp, "[0004]\t\tACPI Table Address 2 : 00000000%08X\n",
212             basl_acpi_base + HPET_OFFSET);
213         EFPRINTF(fp, "[0004]\t\tACPI Table Address 3 : 00000000%08X\n",
214             basl_acpi_base + MCFG_OFFSET);
215
216         EFFLUSH(fp);
217
218         return (0);
219
220 err_exit:
221         return (errno);
222 }
223
224 static int
225 basl_fwrite_madt(FILE *fp)
226 {
227         int i;
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 : %02X\n", SCI_INT);
287         EFPRINTF(fp, "[0004]\t\tInterrupt : %08X\n", SCI_INT);
288         EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0000\n");
289         EFPRINTF(fp, "\t\t\tPolarity : 3\n");
290         EFPRINTF(fp, "\t\t\tTrigger Mode : 3\n");
291         EFPRINTF(fp, "\n");
292
293         /* Local APIC NMI is connected to LINT 1 on all CPUs */
294         EFPRINTF(fp, "[0001]\t\tSubtable Type : 04\n");
295         EFPRINTF(fp, "[0001]\t\tLength : 06\n");
296         EFPRINTF(fp, "[0001]\t\tProcessorId : FF\n");
297         EFPRINTF(fp, "[0002]\t\tFlags (decoded below) : 0005\n");
298         EFPRINTF(fp, "\t\t\tPolarity : 1\n");
299         EFPRINTF(fp, "\t\t\tTrigger Mode : 1\n");
300         EFPRINTF(fp, "[0001]\t\tInterrupt : 01\n");
301         EFPRINTF(fp, "\n");
302
303         EFFLUSH(fp);
304
305         return (0);
306
307 err_exit:
308         return (errno);
309 }
310
311 static int
312 basl_fwrite_fadt(FILE *fp)
313 {
314         EFPRINTF(fp, "/*\n");
315         EFPRINTF(fp, " * bhyve FADT template\n");
316         EFPRINTF(fp, " */\n");
317         EFPRINTF(fp, "[0004]\t\tSignature : \"FACP\"\n");
318         EFPRINTF(fp, "[0004]\t\tTable Length : 0000010C\n");
319         EFPRINTF(fp, "[0001]\t\tRevision : 05\n");
320         EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
321         EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
322         EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVFACP  \"\n");
323         EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
324         /* iasl will fill in the compiler ID/revision fields */
325         EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
326         EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
327         EFPRINTF(fp, "\n");
328
329         EFPRINTF(fp, "[0004]\t\tFACS Address : %08X\n",
330             basl_acpi_base + FACS_OFFSET);
331         EFPRINTF(fp, "[0004]\t\tDSDT Address : %08X\n",
332             basl_acpi_base + DSDT_OFFSET);
333         EFPRINTF(fp, "[0001]\t\tModel : 01\n");
334         EFPRINTF(fp, "[0001]\t\tPM Profile : 00 [Unspecified]\n");
335         EFPRINTF(fp, "[0002]\t\tSCI Interrupt : %04X\n",
336             SCI_INT);
337         EFPRINTF(fp, "[0004]\t\tSMI Command Port : %08X\n",
338             SMI_CMD);
339         EFPRINTF(fp, "[0001]\t\tACPI Enable Value : %02X\n",
340             BHYVE_ACPI_ENABLE);
341         EFPRINTF(fp, "[0001]\t\tACPI Disable Value : %02X\n",
342             BHYVE_ACPI_DISABLE);
343         EFPRINTF(fp, "[0001]\t\tS4BIOS Command : 00\n");
344         EFPRINTF(fp, "[0001]\t\tP-State Control : 00\n");
345         EFPRINTF(fp, "[0004]\t\tPM1A Event Block Address : %08X\n",
346             PM1A_EVT_ADDR);
347         EFPRINTF(fp, "[0004]\t\tPM1B Event Block Address : 00000000\n");
348         EFPRINTF(fp, "[0004]\t\tPM1A Control Block Address : %08X\n",
349             PM1A_CNT_ADDR);
350         EFPRINTF(fp, "[0004]\t\tPM1B Control Block Address : 00000000\n");
351         EFPRINTF(fp, "[0004]\t\tPM2 Control Block Address : 00000000\n");
352         EFPRINTF(fp, "[0004]\t\tPM Timer Block Address : %08X\n",
353             IO_PMTMR);
354         EFPRINTF(fp, "[0004]\t\tGPE0 Block Address : 00000000\n");
355         EFPRINTF(fp, "[0004]\t\tGPE1 Block Address : 00000000\n");
356         EFPRINTF(fp, "[0001]\t\tPM1 Event Block Length : 04\n");
357         EFPRINTF(fp, "[0001]\t\tPM1 Control Block Length : 02\n");
358         EFPRINTF(fp, "[0001]\t\tPM2 Control Block Length : 00\n");
359         EFPRINTF(fp, "[0001]\t\tPM Timer Block Length : 04\n");
360         EFPRINTF(fp, "[0001]\t\tGPE0 Block Length : 00\n");
361         EFPRINTF(fp, "[0001]\t\tGPE1 Block Length : 00\n");
362         EFPRINTF(fp, "[0001]\t\tGPE1 Base Offset : 00\n");
363         EFPRINTF(fp, "[0001]\t\t_CST Support : 00\n");
364         EFPRINTF(fp, "[0002]\t\tC2 Latency : 0000\n");
365         EFPRINTF(fp, "[0002]\t\tC3 Latency : 0000\n");
366         EFPRINTF(fp, "[0002]\t\tCPU Cache Size : 0000\n");
367         EFPRINTF(fp, "[0002]\t\tCache Flush Stride : 0000\n");
368         EFPRINTF(fp, "[0001]\t\tDuty Cycle Offset : 00\n");
369         EFPRINTF(fp, "[0001]\t\tDuty Cycle Width : 00\n");
370         EFPRINTF(fp, "[0001]\t\tRTC Day Alarm Index : 00\n");
371         EFPRINTF(fp, "[0001]\t\tRTC Month Alarm Index : 00\n");
372         EFPRINTF(fp, "[0001]\t\tRTC Century Index : 32\n");
373         EFPRINTF(fp, "[0002]\t\tBoot Flags (decoded below) : 0000\n");
374         EFPRINTF(fp, "\t\t\tLegacy Devices Supported (V2) : 0\n");
375         EFPRINTF(fp, "\t\t\t8042 Present on ports 60/64 (V2) : 0\n");
376         EFPRINTF(fp, "\t\t\tVGA Not Present (V4) : 1\n");
377         EFPRINTF(fp, "\t\t\tMSI Not Supported (V4) : 0\n");
378         EFPRINTF(fp, "\t\t\tPCIe ASPM Not Supported (V4) : 1\n");
379         EFPRINTF(fp, "\t\t\tCMOS RTC Not Present (V5) : 0\n");
380         EFPRINTF(fp, "[0001]\t\tReserved : 00\n");
381         EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
382         EFPRINTF(fp, "\t\t\tWBINVD instruction is operational (V1) : 1\n");
383         EFPRINTF(fp, "\t\t\tWBINVD flushes all caches (V1) : 0\n");
384         EFPRINTF(fp, "\t\t\tAll CPUs support C1 (V1) : 1\n");
385         EFPRINTF(fp, "\t\t\tC2 works on MP system (V1) : 0\n");
386         EFPRINTF(fp, "\t\t\tControl Method Power Button (V1) : 0\n");
387         EFPRINTF(fp, "\t\t\tControl Method Sleep Button (V1) : 1\n");
388         EFPRINTF(fp, "\t\t\tRTC wake not in fixed reg space (V1) : 0\n");
389         EFPRINTF(fp, "\t\t\tRTC can wake system from S4 (V1) : 0\n");
390         EFPRINTF(fp, "\t\t\t32-bit PM Timer (V1) : 1\n");
391         EFPRINTF(fp, "\t\t\tDocking Supported (V1) : 0\n");
392         EFPRINTF(fp, "\t\t\tReset Register Supported (V2) : 1\n");
393         EFPRINTF(fp, "\t\t\tSealed Case (V3) : 0\n");
394         EFPRINTF(fp, "\t\t\tHeadless - No Video (V3) : 1\n");
395         EFPRINTF(fp, "\t\t\tUse native instr after SLP_TYPx (V3) : 0\n");
396         EFPRINTF(fp, "\t\t\tPCIEXP_WAK Bits Supported (V4) : 0\n");
397         EFPRINTF(fp, "\t\t\tUse Platform Timer (V4) : 0\n");
398         EFPRINTF(fp, "\t\t\tRTC_STS valid on S4 wake (V4) : 0\n");
399         EFPRINTF(fp, "\t\t\tRemote Power-on capable (V4) : 0\n");
400         EFPRINTF(fp, "\t\t\tUse APIC Cluster Model (V4) : 0\n");
401         EFPRINTF(fp, "\t\t\tUse APIC Physical Destination Mode (V4) : 1\n");
402         EFPRINTF(fp, "\t\t\tHardware Reduced (V5) : 0\n");
403         EFPRINTF(fp, "\t\t\tLow Power S0 Idle (V5) : 0\n");
404         EFPRINTF(fp, "\n");
405
406         EFPRINTF(fp,
407             "[0012]\t\tReset Register : [Generic Address Structure]\n");
408         EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
409         EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
410         EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
411         EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
412         EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000CF9\n");
413         EFPRINTF(fp, "\n");
414
415         EFPRINTF(fp, "[0001]\t\tValue to cause reset : 06\n");
416         EFPRINTF(fp, "[0002]\t\tARM Flags (decoded below): 0000\n");
417         EFPRINTF(fp, "\t\t\tPSCI Compliant : 0\n");
418         EFPRINTF(fp, "\t\t\tMust use HVC for PSCI : 0\n");
419         EFPRINTF(fp, "[0001]\t\tFADT Minor Revision : 01\n");
420         EFPRINTF(fp, "[0008]\t\tFACS Address : 00000000%08X\n",
421             basl_acpi_base + FACS_OFFSET);
422         EFPRINTF(fp, "[0008]\t\tDSDT Address : 00000000%08X\n",
423             basl_acpi_base + DSDT_OFFSET);
424         EFPRINTF(fp,
425             "[0012]\t\tPM1A Event Block : [Generic Address Structure]\n");
426         EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
427         EFPRINTF(fp, "[0001]\t\tBit Width : 20\n");
428         EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
429         EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
430         EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
431             PM1A_EVT_ADDR);
432         EFPRINTF(fp, "\n");
433         
434         EFPRINTF(fp,
435             "[0012]\t\tPM1B Event Block : [Generic Address Structure]\n");
436         EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
437         EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
438         EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
439         EFPRINTF(fp,
440             "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
441         EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
442         EFPRINTF(fp, "\n");
443
444         EFPRINTF(fp,
445             "[0012]\t\tPM1A Control Block : [Generic Address Structure]\n");
446         EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
447         EFPRINTF(fp, "[0001]\t\tBit Width : 10\n");
448         EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
449         EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 02 [Word Access:16]\n");
450         EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
451             PM1A_CNT_ADDR);
452         EFPRINTF(fp, "\n");
453
454         EFPRINTF(fp,
455             "[0012]\t\tPM1B Control 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\tPM2 Control Block : [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,
470             "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
471         EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
472         EFPRINTF(fp, "\n");
473
474         /* Valid for bhyve */
475         EFPRINTF(fp,
476             "[0012]\t\tPM Timer Block : [Generic Address Structure]\n");
477         EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
478         EFPRINTF(fp, "[0001]\t\tBit Width : 20\n");
479         EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
480         EFPRINTF(fp,
481             "[0001]\t\tEncoded Access Width : 03 [DWord Access:32]\n");
482         EFPRINTF(fp, "[0008]\t\tAddress : 00000000%08X\n",
483             IO_PMTMR);
484         EFPRINTF(fp, "\n");
485
486         EFPRINTF(fp, "[0012]\t\tGPE0 Block : [Generic Address Structure]\n");
487         EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
488         EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
489         EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
490         EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
491         EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
492         EFPRINTF(fp, "\n");
493
494         EFPRINTF(fp, "[0012]\t\tGPE1 Block : [Generic Address Structure]\n");
495         EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
496         EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
497         EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
498         EFPRINTF(fp,
499             "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
500         EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
501         EFPRINTF(fp, "\n");
502
503         EFPRINTF(fp,
504            "[0012]\t\tSleep Control Register : [Generic Address Structure]\n");
505         EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
506         EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
507         EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
508         EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
509         EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
510         EFPRINTF(fp, "\n");
511
512         EFPRINTF(fp,
513             "[0012]\t\tSleep Status Register : [Generic Address Structure]\n");
514         EFPRINTF(fp, "[0001]\t\tSpace ID : 01 [SystemIO]\n");
515         EFPRINTF(fp, "[0001]\t\tBit Width : 08\n");
516         EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
517         EFPRINTF(fp, "[0001]\t\tEncoded Access Width : 01 [Byte Access:8]\n");
518         EFPRINTF(fp, "[0008]\t\tAddress : 0000000000000000\n");
519
520         EFFLUSH(fp);
521
522         return (0);
523
524 err_exit:
525         return (errno);
526 }
527
528 static int
529 basl_fwrite_hpet(FILE *fp)
530 {
531         EFPRINTF(fp, "/*\n");
532         EFPRINTF(fp, " * bhyve HPET template\n");
533         EFPRINTF(fp, " */\n");
534         EFPRINTF(fp, "[0004]\t\tSignature : \"HPET\"\n");
535         EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
536         EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
537         EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
538         EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
539         EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVHPET  \"\n");
540         EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
541
542         /* iasl will fill in the compiler ID/revision fields */
543         EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
544         EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
545         EFPRINTF(fp, "\n");
546
547         EFPRINTF(fp, "[0004]\t\tTimer Block ID : %08X\n", hpet_capabilities);
548         EFPRINTF(fp,
549             "[0012]\t\tTimer Block Register : [Generic Address Structure]\n");
550         EFPRINTF(fp, "[0001]\t\tSpace ID : 00 [SystemMemory]\n");
551         EFPRINTF(fp, "[0001]\t\tBit Width : 00\n");
552         EFPRINTF(fp, "[0001]\t\tBit Offset : 00\n");
553         EFPRINTF(fp,
554                  "[0001]\t\tEncoded Access Width : 00 [Undefined/Legacy]\n");
555         EFPRINTF(fp, "[0008]\t\tAddress : 00000000FED00000\n");
556         EFPRINTF(fp, "\n");
557
558         EFPRINTF(fp, "[0001]\t\tHPET Number : 00\n");
559         EFPRINTF(fp, "[0002]\t\tMinimum Clock Ticks : 0000\n");
560         EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000001\n");
561         EFPRINTF(fp, "\t\t\t4K Page Protect : 1\n");
562         EFPRINTF(fp, "\t\t\t64K Page Protect : 0\n");
563         EFPRINTF(fp, "\n");
564
565         EFFLUSH(fp);
566
567         return (0);
568
569 err_exit:
570         return (errno);
571 }
572
573 static int
574 basl_fwrite_mcfg(FILE *fp)
575 {
576         EFPRINTF(fp, "/*\n");
577         EFPRINTF(fp, " * bhyve MCFG template\n");
578         EFPRINTF(fp, " */\n");
579         EFPRINTF(fp, "[0004]\t\tSignature : \"MCFG\"\n");
580         EFPRINTF(fp, "[0004]\t\tTable Length : 00000000\n");
581         EFPRINTF(fp, "[0001]\t\tRevision : 01\n");
582         EFPRINTF(fp, "[0001]\t\tChecksum : 00\n");
583         EFPRINTF(fp, "[0006]\t\tOem ID : \"BHYVE \"\n");
584         EFPRINTF(fp, "[0008]\t\tOem Table ID : \"BVMCFG  \"\n");
585         EFPRINTF(fp, "[0004]\t\tOem Revision : 00000001\n");
586
587         /* iasl will fill in the compiler ID/revision fields */
588         EFPRINTF(fp, "[0004]\t\tAsl Compiler ID : \"xxxx\"\n");
589         EFPRINTF(fp, "[0004]\t\tAsl Compiler Revision : 00000000\n");
590         EFPRINTF(fp, "[0008]\t\tReserved : 0\n");
591         EFPRINTF(fp, "\n");
592
593         EFPRINTF(fp, "[0008]\t\tBase Address : %016lX\n", pci_ecfg_base());
594         EFPRINTF(fp, "[0002]\t\tSegment Group: 0000\n");
595         EFPRINTF(fp, "[0001]\t\tStart Bus: 00\n");
596         EFPRINTF(fp, "[0001]\t\tEnd Bus: FF\n");
597         EFPRINTF(fp, "[0004]\t\tReserved : 0\n");
598         EFFLUSH(fp);
599         return (0);
600 err_exit:
601         return (errno);
602 }
603
604 static int
605 basl_fwrite_facs(FILE *fp)
606 {
607         EFPRINTF(fp, "/*\n");
608         EFPRINTF(fp, " * bhyve FACS template\n");
609         EFPRINTF(fp, " */\n");
610         EFPRINTF(fp, "[0004]\t\tSignature : \"FACS\"\n");
611         EFPRINTF(fp, "[0004]\t\tLength : 00000040\n");
612         EFPRINTF(fp, "[0004]\t\tHardware Signature : 00000000\n");
613         EFPRINTF(fp, "[0004]\t\t32 Firmware Waking Vector : 00000000\n");
614         EFPRINTF(fp, "[0004]\t\tGlobal Lock : 00000000\n");
615         EFPRINTF(fp, "[0004]\t\tFlags (decoded below) : 00000000\n");
616         EFPRINTF(fp, "\t\t\tS4BIOS Support Present : 0\n");
617         EFPRINTF(fp, "\t\t\t64-bit Wake Supported (V2) : 0\n");
618         EFPRINTF(fp,
619             "[0008]\t\t64 Firmware Waking Vector : 0000000000000000\n");
620         EFPRINTF(fp, "[0001]\t\tVersion : 02\n");
621         EFPRINTF(fp, "[0003]\t\tReserved : 000000\n");
622         EFPRINTF(fp, "[0004]\t\tOspmFlags (decoded below) : 00000000\n");
623         EFPRINTF(fp, "\t\t\t64-bit Wake Env Required (V2) : 0\n");
624
625         EFFLUSH(fp);
626
627         return (0);
628         
629 err_exit:
630         return (errno);
631 }
632
633 /*
634  * Helper routines for writing to the DSDT from other modules.
635  */
636 void
637 dsdt_line(const char *fmt, ...)
638 {
639         va_list ap;
640
641         if (dsdt_error != 0)
642                 return;
643
644         if (strcmp(fmt, "") != 0) {
645                 if (dsdt_indent_level != 0)
646                         EFPRINTF(dsdt_fp, "%*c", dsdt_indent_level * 2, ' ');
647                 va_start(ap, fmt);
648                 if (vfprintf(dsdt_fp, fmt, ap) < 0) {
649                         va_end(ap);
650                         goto err_exit;
651                 }
652                 va_end(ap);
653         }
654         EFPRINTF(dsdt_fp, "\n");
655         return;
656
657 err_exit:
658         dsdt_error = errno;
659 }
660
661 void
662 dsdt_indent(int levels)
663 {
664
665         dsdt_indent_level += levels;
666         assert(dsdt_indent_level >= 0);
667 }
668
669 void
670 dsdt_unindent(int levels)
671 {
672
673         assert(dsdt_indent_level >= levels);
674         dsdt_indent_level -= levels;
675 }
676
677 void
678 dsdt_fixed_ioport(uint16_t iobase, uint16_t length)
679 {
680
681         dsdt_line("IO (Decode16,");
682         dsdt_line("  0x%04X,             // Range Minimum", iobase);
683         dsdt_line("  0x%04X,             // Range Maximum", iobase);
684         dsdt_line("  0x01,               // Alignment");
685         dsdt_line("  0x%02X,               // Length", length);
686         dsdt_line("  )");
687 }
688
689 void
690 dsdt_fixed_irq(uint8_t irq)
691 {
692
693         dsdt_line("IRQNoFlags ()");
694         dsdt_line("  {%d}", irq);
695 }
696
697 void
698 dsdt_fixed_mem32(uint32_t base, uint32_t length)
699 {
700
701         dsdt_line("Memory32Fixed (ReadWrite,");
702         dsdt_line("  0x%08X,         // Address Base", base);
703         dsdt_line("  0x%08X,         // Address Length", length);
704         dsdt_line("  )");
705 }
706
707 static int
708 basl_fwrite_dsdt(FILE *fp)
709 {
710         dsdt_fp = fp;
711         dsdt_error = 0;
712         dsdt_indent_level = 0;
713
714         dsdt_line("/*");
715         dsdt_line(" * bhyve DSDT template");
716         dsdt_line(" */");
717         dsdt_line("DefinitionBlock (\"bhyve_dsdt.aml\", \"DSDT\", 2,"
718                  "\"BHYVE \", \"BVDSDT  \", 0x00000001)");
719         dsdt_line("{");
720         dsdt_line("  Name (_S5, Package ()");
721         dsdt_line("  {");
722         dsdt_line("      0x05,");
723         dsdt_line("      Zero,");
724         dsdt_line("  })");
725
726         pci_write_dsdt();
727
728         dsdt_line("");
729         dsdt_line("  Scope (_SB.PC00)");
730         dsdt_line("  {");
731         dsdt_line("    Device (HPET)");
732         dsdt_line("    {");
733         dsdt_line("      Name (_HID, EISAID(\"PNP0103\"))");
734         dsdt_line("      Name (_UID, 0)");
735         dsdt_line("      Name (_CRS, ResourceTemplate ()");
736         dsdt_line("      {");
737         dsdt_indent(4);
738         dsdt_fixed_mem32(0xFED00000, 0x400);
739         dsdt_unindent(4);
740         dsdt_line("      })");
741         dsdt_line("    }");
742         dsdt_line("  }");
743         dsdt_line("}");
744
745         if (dsdt_error != 0)
746                 return (dsdt_error);
747
748         EFFLUSH(fp);
749
750         return (0);
751
752 err_exit:
753         return (errno);
754 }
755
756 static int
757 basl_open(struct basl_fio *bf, int suffix)
758 {
759         int err;
760
761         err = 0;
762
763         if (suffix) {
764                 strlcpy(bf->f_name, basl_stemplate, MAXPATHLEN);
765                 bf->fd = mkstemps(bf->f_name, strlen(BHYVE_ASL_SUFFIX));
766         } else {
767                 strlcpy(bf->f_name, basl_template, MAXPATHLEN);
768                 bf->fd = mkstemp(bf->f_name);
769         }
770
771         if (bf->fd > 0) {
772                 bf->fp = fdopen(bf->fd, "w+");
773                 if (bf->fp == NULL) {
774                         unlink(bf->f_name);
775                         close(bf->fd);
776                 }
777         } else {
778                 err = 1;
779         }
780
781         return (err);
782 }
783
784 static void
785 basl_close(struct basl_fio *bf)
786 {
787
788         if (!basl_keep_temps)
789                 unlink(bf->f_name);
790         fclose(bf->fp);
791 }
792
793 static int
794 basl_start(struct basl_fio *in, struct basl_fio *out)
795 {
796         int err;
797
798         err = basl_open(in, 0);
799         if (!err) {
800                 err = basl_open(out, 1);
801                 if (err) {
802                         basl_close(in);
803                 }
804         }
805
806         return (err);
807 }
808
809 static void
810 basl_end(struct basl_fio *in, struct basl_fio *out)
811 {
812
813         basl_close(in);
814         basl_close(out);
815 }
816
817 static int
818 basl_load(struct vmctx *ctx, int fd, uint64_t off)
819 {
820         struct stat sb;
821         void *gaddr;
822
823         if (fstat(fd, &sb) < 0)
824                 return (errno);
825                 
826         gaddr = paddr_guest2host(ctx, basl_acpi_base + off, sb.st_size);
827         if (gaddr == NULL)
828                 return (EFAULT);
829
830         if (read(fd, gaddr, sb.st_size) < 0)
831                 return (errno);
832
833         return (0);
834 }
835
836 static int
837 basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *), uint64_t offset)
838 {
839         struct basl_fio io[2];
840         static char iaslbuf[3*MAXPATHLEN + 10];
841         char *fmt;
842         int err;
843
844         err = basl_start(&io[0], &io[1]);
845         if (!err) {
846                 err = (*fwrite_section)(io[0].fp);
847
848                 if (!err) {
849                         /*
850                          * iasl sends the results of the compilation to
851                          * stdout. Shut this down by using the shell to
852                          * redirect stdout to /dev/null, unless the user
853                          * has requested verbose output for debugging
854                          * purposes
855                          */
856                         fmt = basl_verbose_iasl ?
857                                 "%s -p %s %s" :
858                                 "/bin/sh -c \"%s -p %s %s\" 1> /dev/null";
859                                 
860                         snprintf(iaslbuf, sizeof(iaslbuf),
861                                  fmt,
862                                  BHYVE_ASL_COMPILER,
863                                  io[1].f_name, io[0].f_name);
864                         err = system(iaslbuf);
865
866                         if (!err) {
867                                 /*
868                                  * Copy the aml output file into guest
869                                  * memory at the specified location
870                                  */
871                                 err = basl_load(ctx, io[1].fd, offset);
872                         }
873                 }
874                 basl_end(&io[0], &io[1]);
875         }
876
877         return (err);
878 }
879
880 static int
881 basl_make_templates(void)
882 {
883         const char *tmpdir;
884         int err;
885         int len;
886
887         err = 0;
888
889         /*
890          * 
891          */
892         if ((tmpdir = getenv("BHYVE_TMPDIR")) == NULL || *tmpdir == '\0' ||
893             (tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') {
894                 tmpdir = _PATH_TMP;
895         }
896
897         len = strlen(tmpdir);
898
899         if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) {
900                 strcpy(basl_template, tmpdir);
901                 while (len > 0 && basl_template[len - 1] == '/')
902                         len--;
903                 basl_template[len] = '/';
904                 strcpy(&basl_template[len + 1], BHYVE_ASL_TEMPLATE);
905         } else
906                 err = E2BIG;
907
908         if (!err) {
909                 /*
910                  * len has been intialized (and maybe adjusted) above
911                  */
912                 if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1 +
913                      sizeof(BHYVE_ASL_SUFFIX)) < MAXPATHLEN) {
914                         strcpy(basl_stemplate, tmpdir);
915                         basl_stemplate[len] = '/';
916                         strcpy(&basl_stemplate[len + 1], BHYVE_ASL_TEMPLATE);
917                         len = strlen(basl_stemplate);
918                         strcpy(&basl_stemplate[len], BHYVE_ASL_SUFFIX);
919                 } else
920                         err = E2BIG;
921         }
922
923         return (err);
924 }
925
926 static struct {
927         int     (*wsect)(FILE *fp);
928         uint64_t  offset;
929 } basl_ftables[] =
930 {
931         { basl_fwrite_rsdp, 0},
932         { basl_fwrite_rsdt, RSDT_OFFSET },
933         { basl_fwrite_xsdt, XSDT_OFFSET },
934         { basl_fwrite_madt, MADT_OFFSET },
935         { basl_fwrite_fadt, FADT_OFFSET },
936         { basl_fwrite_hpet, HPET_OFFSET },
937         { basl_fwrite_mcfg, MCFG_OFFSET },
938         { basl_fwrite_facs, FACS_OFFSET },
939         { basl_fwrite_dsdt, DSDT_OFFSET },
940         { NULL }
941 };
942
943 int
944 acpi_build(struct vmctx *ctx, int ncpu)
945 {
946         int err;
947         int i;
948
949         basl_ncpu = ncpu;
950
951         err = vm_get_hpet_capabilities(ctx, &hpet_capabilities);
952         if (err != 0)
953                 return (err);
954
955         /*
956          * For debug, allow the user to have iasl compiler output sent
957          * to stdout rather than /dev/null
958          */
959         if (getenv("BHYVE_ACPI_VERBOSE_IASL"))
960                 basl_verbose_iasl = 1;
961
962         /*
963          * Allow the user to keep the generated ASL files for debugging
964          * instead of deleting them following use
965          */
966         if (getenv("BHYVE_ACPI_KEEPTMPS"))
967                 basl_keep_temps = 1;
968
969         i = 0;
970         err = basl_make_templates();
971
972         /*
973          * Run through all the ASL files, compiling them and
974          * copying them into guest memory
975          */
976         while (!err && basl_ftables[i].wsect != NULL) {
977                 err = basl_compile(ctx, basl_ftables[i].wsect,
978                                    basl_ftables[i].offset);
979                 i++;
980         }
981
982         return (err);
983 }