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