]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/acpi.c
sqlite3: Vendor import of sqlite3 3.42.0
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / acpi.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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).
36  *
37  * The tables are placed in the guest's ROM area just below 1MB physical,
38  * above the MPTable.
39  */
40
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43
44 #include <sys/param.h>
45 #include <sys/endian.h>
46 #include <sys/errno.h>
47 #include <sys/stat.h>
48
49 #include <err.h>
50 #include <paths.h>
51 #include <stdarg.h>
52 #include <stddef.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #include <machine/vmm.h>
59 #include <vmmapi.h>
60
61 #include "bhyverun.h"
62 #include "acpi.h"
63 #include "basl.h"
64 #include "pci_emul.h"
65 #include "vmgenc.h"
66
67 #define BHYVE_ASL_TEMPLATE      "bhyve.XXXXXXX"
68 #define BHYVE_ASL_SUFFIX        ".aml"
69 #define BHYVE_ASL_COMPILER      "/usr/sbin/iasl"
70
71 #define BHYVE_ADDRESS_IOAPIC    0xFEC00000
72 #define BHYVE_ADDRESS_HPET      0xFED00000
73 #define BHYVE_ADDRESS_LAPIC     0xFEE00000
74
75 static int basl_keep_temps;
76 static int basl_verbose_iasl;
77 static int basl_ncpu;
78 static uint32_t hpet_capabilities;
79
80 /*
81  * Contains the full pathname of the template to be passed
82  * to mkstemp/mktemps(3)
83  */
84 static char basl_template[MAXPATHLEN];
85 static char basl_stemplate[MAXPATHLEN];
86
87 /*
88  * State for dsdt_line(), dsdt_indent(), and dsdt_unindent().
89  */
90 static FILE *dsdt_fp;
91 static int dsdt_indent_level;
92 static int dsdt_error;
93
94 static struct basl_table *rsdt;
95 static struct basl_table *xsdt;
96
97 struct basl_fio {
98         int     fd;
99         FILE    *fp;
100         char    f_name[MAXPATHLEN];
101 };
102
103 #define EFPRINTF(...) \
104         if (fprintf(__VA_ARGS__) < 0) goto err_exit
105
106 #define EFFLUSH(x) \
107         if (fflush(x) != 0) goto err_exit
108
109 /*
110  * A list for additional ACPI devices like a TPM.
111  */
112 struct acpi_device_list_entry {
113         SLIST_ENTRY(acpi_device_list_entry) chain;
114         const struct acpi_device *dev;
115 };
116 static SLIST_HEAD(acpi_device_list,
117     acpi_device_list_entry) acpi_devices = SLIST_HEAD_INITIALIZER(acpi_devices);
118
119 int
120 acpi_tables_add_device(const struct acpi_device *const dev)
121 {
122         struct acpi_device_list_entry *const entry = calloc(1, sizeof(*entry));
123         if (entry == NULL) {
124                 return (ENOMEM);
125         }
126
127         entry->dev = dev;
128         SLIST_INSERT_HEAD(&acpi_devices, entry, chain);
129
130         return (0);
131 }
132
133 /*
134  * Helper routines for writing to the DSDT from other modules.
135  */
136 void
137 dsdt_line(const char *fmt, ...)
138 {
139         va_list ap;
140
141         if (dsdt_error != 0)
142                 return;
143
144         if (strcmp(fmt, "") != 0) {
145                 if (dsdt_indent_level != 0)
146                         EFPRINTF(dsdt_fp, "%*c", dsdt_indent_level * 2, ' ');
147                 va_start(ap, fmt);
148                 if (vfprintf(dsdt_fp, fmt, ap) < 0) {
149                         va_end(ap);
150                         goto err_exit;
151                 }
152                 va_end(ap);
153         }
154         EFPRINTF(dsdt_fp, "\n");
155         return;
156
157 err_exit:
158         dsdt_error = errno;
159 }
160
161 void
162 dsdt_indent(int levels)
163 {
164
165         dsdt_indent_level += levels;
166         assert(dsdt_indent_level >= 0);
167 }
168
169 void
170 dsdt_unindent(int levels)
171 {
172
173         assert(dsdt_indent_level >= levels);
174         dsdt_indent_level -= levels;
175 }
176
177 void
178 dsdt_fixed_ioport(uint16_t iobase, uint16_t length)
179 {
180
181         dsdt_line("IO (Decode16,");
182         dsdt_line("  0x%04X,             // Range Minimum", iobase);
183         dsdt_line("  0x%04X,             // Range Maximum", iobase);
184         dsdt_line("  0x01,               // Alignment");
185         dsdt_line("  0x%02X,               // Length", length);
186         dsdt_line("  )");
187 }
188
189 void
190 dsdt_fixed_irq(uint8_t irq)
191 {
192
193         dsdt_line("IRQNoFlags ()");
194         dsdt_line("  {%d}", irq);
195 }
196
197 void
198 dsdt_fixed_mem32(uint32_t base, uint32_t length)
199 {
200
201         dsdt_line("Memory32Fixed (ReadWrite,");
202         dsdt_line("  0x%08X,         // Address Base", base);
203         dsdt_line("  0x%08X,         // Address Length", length);
204         dsdt_line("  )");
205 }
206
207 static int
208 basl_fwrite_dsdt(FILE *fp)
209 {
210         dsdt_fp = fp;
211         dsdt_error = 0;
212         dsdt_indent_level = 0;
213
214         dsdt_line("/*");
215         dsdt_line(" * bhyve DSDT template");
216         dsdt_line(" */");
217         dsdt_line("DefinitionBlock (\"bhyve_dsdt.aml\", \"DSDT\", 2,"
218                  "\"BHYVE \", \"BVDSDT  \", 0x00000001)");
219         dsdt_line("{");
220         dsdt_line("  Name (_S5, Package ()");
221         dsdt_line("  {");
222         dsdt_line("      0x05,");
223         dsdt_line("      Zero,");
224         dsdt_line("  })");
225
226         pci_write_dsdt();
227
228         dsdt_line("");
229         dsdt_line("  Scope (_SB.PC00)");
230         dsdt_line("  {");
231         dsdt_line("    Device (HPET)");
232         dsdt_line("    {");
233         dsdt_line("      Name (_HID, EISAID(\"PNP0103\"))");
234         dsdt_line("      Name (_UID, 0)");
235         dsdt_line("      Name (_CRS, ResourceTemplate ()");
236         dsdt_line("      {");
237         dsdt_indent(4);
238         dsdt_fixed_mem32(0xFED00000, 0x400);
239         dsdt_unindent(4);
240         dsdt_line("      })");
241         dsdt_line("    }");
242         dsdt_line("  }");
243
244         vmgenc_write_dsdt();
245
246         const struct acpi_device_list_entry *entry;
247         SLIST_FOREACH(entry, &acpi_devices, chain) {
248                 BASL_EXEC(acpi_device_write_dsdt(entry->dev));
249         }
250
251         dsdt_line("}");
252
253         if (dsdt_error != 0)
254                 return (dsdt_error);
255
256         EFFLUSH(fp);
257
258         return (0);
259
260 err_exit:
261         return (errno);
262 }
263
264 static int
265 basl_open(struct basl_fio *bf, int suffix)
266 {
267         int err;
268
269         err = 0;
270
271         if (suffix) {
272                 strlcpy(bf->f_name, basl_stemplate, MAXPATHLEN);
273                 bf->fd = mkstemps(bf->f_name, strlen(BHYVE_ASL_SUFFIX));
274         } else {
275                 strlcpy(bf->f_name, basl_template, MAXPATHLEN);
276                 bf->fd = mkstemp(bf->f_name);
277         }
278
279         if (bf->fd > 0) {
280                 bf->fp = fdopen(bf->fd, "w+");
281                 if (bf->fp == NULL) {
282                         unlink(bf->f_name);
283                         close(bf->fd);
284                 }
285         } else {
286                 err = 1;
287         }
288
289         return (err);
290 }
291
292 static void
293 basl_close(struct basl_fio *bf)
294 {
295
296         if (!basl_keep_temps)
297                 unlink(bf->f_name);
298         fclose(bf->fp);
299 }
300
301 static int
302 basl_start(struct basl_fio *in, struct basl_fio *out)
303 {
304         int err;
305
306         err = basl_open(in, 0);
307         if (!err) {
308                 err = basl_open(out, 1);
309                 if (err) {
310                         basl_close(in);
311                 }
312         }
313
314         return (err);
315 }
316
317 static void
318 basl_end(struct basl_fio *in, struct basl_fio *out)
319 {
320
321         basl_close(in);
322         basl_close(out);
323 }
324
325 static int
326 basl_load(struct vmctx *ctx, int fd)
327 {
328         struct stat sb;
329         void *addr;
330
331         if (fstat(fd, &sb) < 0)
332                 return (errno);
333
334         addr = calloc(1, sb.st_size);
335         if (addr == NULL)
336                 return (EFAULT);
337
338         if (read(fd, addr, sb.st_size) < 0)
339                 return (errno);
340
341         struct basl_table *table;
342
343         uint8_t name[ACPI_NAMESEG_SIZE + 1] = { 0 };
344         memcpy(name, addr, sizeof(name) - 1 /* last char is '\0' */);
345         BASL_EXEC(basl_table_create(&table, ctx, name, BASL_TABLE_ALIGNMENT));
346         BASL_EXEC(basl_table_append_bytes(table, addr, sb.st_size));
347
348         return (0);
349 }
350
351 static int
352 basl_compile(struct vmctx *ctx, int (*fwrite_section)(FILE *))
353 {
354         struct basl_fio io[2];
355         static char iaslbuf[3*MAXPATHLEN + 10];
356         const char *fmt;
357         int err;
358
359         err = basl_start(&io[0], &io[1]);
360         if (!err) {
361                 err = (*fwrite_section)(io[0].fp);
362
363                 if (!err) {
364                         /*
365                          * iasl sends the results of the compilation to
366                          * stdout. Shut this down by using the shell to
367                          * redirect stdout to /dev/null, unless the user
368                          * has requested verbose output for debugging
369                          * purposes
370                          */
371                         fmt = basl_verbose_iasl ?
372                                 "%s -p %s %s" :
373                                 "/bin/sh -c \"%s -p %s %s\" 1> /dev/null";
374
375                         snprintf(iaslbuf, sizeof(iaslbuf),
376                                  fmt,
377                                  BHYVE_ASL_COMPILER,
378                                  io[1].f_name, io[0].f_name);
379                         err = system(iaslbuf);
380
381                         if (!err) {
382                                 /*
383                                  * Copy the aml output file into guest
384                                  * memory at the specified location
385                                  */
386                                 err = basl_load(ctx, io[1].fd);
387                         }
388                 }
389                 basl_end(&io[0], &io[1]);
390         }
391
392         return (err);
393 }
394
395 static int
396 basl_make_templates(void)
397 {
398         const char *tmpdir;
399         int err;
400         int len;
401
402         err = 0;
403
404         /*
405          *
406          */
407         if ((tmpdir = getenv("BHYVE_TMPDIR")) == NULL || *tmpdir == '\0' ||
408             (tmpdir = getenv("TMPDIR")) == NULL || *tmpdir == '\0') {
409                 tmpdir = _PATH_TMP;
410         }
411
412         len = strlen(tmpdir);
413
414         if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1) < MAXPATHLEN) {
415                 strcpy(basl_template, tmpdir);
416                 while (len > 0 && basl_template[len - 1] == '/')
417                         len--;
418                 basl_template[len] = '/';
419                 strcpy(&basl_template[len + 1], BHYVE_ASL_TEMPLATE);
420         } else
421                 err = E2BIG;
422
423         if (!err) {
424                 /*
425                  * len has been intialized (and maybe adjusted) above
426                  */
427                 if ((len + sizeof(BHYVE_ASL_TEMPLATE) + 1 +
428                      sizeof(BHYVE_ASL_SUFFIX)) < MAXPATHLEN) {
429                         strcpy(basl_stemplate, tmpdir);
430                         basl_stemplate[len] = '/';
431                         strcpy(&basl_stemplate[len + 1], BHYVE_ASL_TEMPLATE);
432                         len = strlen(basl_stemplate);
433                         strcpy(&basl_stemplate[len], BHYVE_ASL_SUFFIX);
434                 } else
435                         err = E2BIG;
436         }
437
438         return (err);
439 }
440
441 static int
442 build_dsdt(struct vmctx *const ctx)
443 {
444         BASL_EXEC(basl_compile(ctx, basl_fwrite_dsdt));
445
446         return (0);
447 }
448
449 static int
450 build_facs(struct vmctx *const ctx)
451 {
452         ACPI_TABLE_FACS facs;
453         struct basl_table *table;
454
455         BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_FACS,
456             BASL_TABLE_ALIGNMENT_FACS));
457
458         memset(&facs, 0, sizeof(facs));
459         memcpy(facs.Signature, ACPI_SIG_FACS, ACPI_NAMESEG_SIZE);
460         facs.Length = sizeof(facs);
461         facs.Version = htole32(2);
462         BASL_EXEC(basl_table_append_bytes(table, &facs, sizeof(facs)));
463
464         return (0);
465 }
466
467 static int
468 build_fadt(struct vmctx *const ctx)
469 {
470         ACPI_TABLE_FADT fadt;
471         struct basl_table *table;
472
473         BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_FADT,
474             BASL_TABLE_ALIGNMENT));
475
476         memset(&fadt, 0, sizeof(fadt));
477         BASL_EXEC(basl_table_append_header(table, ACPI_SIG_FADT, 5, 1));
478         fadt.Facs = htole32(0); /* patched by basl */
479         fadt.Dsdt = htole32(0); /* patched by basl */
480         fadt.SciInterrupt = htole16(SCI_INT);
481         fadt.SmiCommand = htole32(SMI_CMD);
482         fadt.AcpiEnable = BHYVE_ACPI_ENABLE;
483         fadt.AcpiDisable = BHYVE_ACPI_DISABLE;
484         fadt.Pm1aEventBlock = htole32(PM1A_EVT_ADDR);
485         fadt.Pm1aControlBlock = htole32(PM1A_CNT_ADDR);
486         fadt.PmTimerBlock = htole32(IO_PMTMR);
487         fadt.Gpe0Block = htole32(IO_GPE0_BLK);
488         fadt.Pm1EventLength = 4;
489         fadt.Pm1ControlLength = 2;
490         fadt.PmTimerLength = 4;
491         fadt.Gpe0BlockLength = IO_GPE0_LEN;
492         fadt.Century = 0x32;
493         fadt.BootFlags = htole16(ACPI_FADT_NO_VGA | ACPI_FADT_NO_ASPM);
494         fadt.Flags = htole32(ACPI_FADT_WBINVD | ACPI_FADT_C1_SUPPORTED |
495             ACPI_FADT_SLEEP_BUTTON | ACPI_FADT_32BIT_TIMER |
496             ACPI_FADT_RESET_REGISTER | ACPI_FADT_HEADLESS |
497             ACPI_FADT_APIC_PHYSICAL);
498         basl_fill_gas(&fadt.ResetRegister, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
499             ACPI_GAS_ACCESS_WIDTH_BYTE, 0xCF9);
500         fadt.ResetValue = 6;
501         fadt.MinorRevision = 1;
502         fadt.XFacs = htole64(0); /* patched by basl */
503         fadt.XDsdt = htole64(0); /* patched by basl */
504         basl_fill_gas(&fadt.XPm1aEventBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0x20, 0,
505             ACPI_GAS_ACCESS_WIDTH_WORD, PM1A_EVT_ADDR);
506         basl_fill_gas(&fadt.XPm1bEventBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0, 0,
507             ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
508         basl_fill_gas(&fadt.XPm1aControlBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0x10,
509             0, ACPI_GAS_ACCESS_WIDTH_WORD, PM1A_CNT_ADDR);
510         basl_fill_gas(&fadt.XPm1bControlBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0, 0,
511             ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
512         basl_fill_gas(&fadt.XPm2ControlBlock, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
513             ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
514         basl_fill_gas(&fadt.XPmTimerBlock, ACPI_ADR_SPACE_SYSTEM_IO, 0x20, 0,
515             ACPI_GAS_ACCESS_WIDTH_DWORD, IO_PMTMR);
516         basl_fill_gas(&fadt.XGpe0Block, ACPI_ADR_SPACE_SYSTEM_IO,
517             IO_GPE0_LEN * 8, 0, ACPI_GAS_ACCESS_WIDTH_BYTE, IO_GPE0_BLK);
518         basl_fill_gas(&fadt.XGpe1Block, ACPI_ADR_SPACE_SYSTEM_IO, 0, 0,
519             ACPI_GAS_ACCESS_WIDTH_UNDEFINED, 0);
520         basl_fill_gas(&fadt.SleepControl, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
521             ACPI_GAS_ACCESS_WIDTH_BYTE, 0);
522         basl_fill_gas(&fadt.SleepStatus, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
523             ACPI_GAS_ACCESS_WIDTH_BYTE, 0);
524         BASL_EXEC(basl_table_append_content(table, &fadt, sizeof(fadt)));
525
526         BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_FACS,
527             offsetof(ACPI_TABLE_FADT, Facs), sizeof(fadt.Facs)));
528         BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_DSDT,
529             offsetof(ACPI_TABLE_FADT, Dsdt), sizeof(fadt.Dsdt)));
530         BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_FACS,
531             offsetof(ACPI_TABLE_FADT, XFacs), sizeof(fadt.XFacs)));
532         BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_DSDT,
533             offsetof(ACPI_TABLE_FADT, XDsdt), sizeof(fadt.XDsdt)));
534
535         BASL_EXEC(basl_table_append_pointer(rsdt, ACPI_SIG_FADT,
536             ACPI_RSDT_ENTRY_SIZE));
537         BASL_EXEC(basl_table_append_pointer(xsdt, ACPI_SIG_FADT,
538             ACPI_XSDT_ENTRY_SIZE));
539
540         return (0);
541 }
542
543 static int
544 build_hpet(struct vmctx *const ctx)
545 {
546         ACPI_TABLE_HPET hpet;
547         struct basl_table *table;
548
549         BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_HPET,
550             BASL_TABLE_ALIGNMENT));
551
552         memset(&hpet, 0, sizeof(hpet));
553         BASL_EXEC(basl_table_append_header(table, ACPI_SIG_HPET, 1, 1));
554         hpet.Id = htole32(hpet_capabilities);
555         basl_fill_gas(&hpet.Address, ACPI_ADR_SPACE_SYSTEM_MEMORY, 0, 0,
556             ACPI_GAS_ACCESS_WIDTH_LEGACY, BHYVE_ADDRESS_HPET);
557         hpet.Flags = ACPI_HPET_PAGE_PROTECT4;
558         BASL_EXEC(basl_table_append_content(table, &hpet, sizeof(hpet)));
559
560         BASL_EXEC(basl_table_append_pointer(rsdt, ACPI_SIG_HPET,
561             ACPI_RSDT_ENTRY_SIZE));
562         BASL_EXEC(basl_table_append_pointer(xsdt, ACPI_SIG_HPET,
563             ACPI_XSDT_ENTRY_SIZE));
564
565         return (0);
566 }
567
568 static int
569 build_madt(struct vmctx *const ctx)
570 {
571         ACPI_TABLE_MADT madt;
572         ACPI_MADT_LOCAL_APIC madt_lapic;
573         ACPI_MADT_IO_APIC madt_ioapic;
574         ACPI_MADT_INTERRUPT_OVERRIDE madt_irq_override;
575         ACPI_MADT_LOCAL_APIC_NMI madt_lapic_nmi;
576         struct basl_table *table;
577
578         BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_MADT,
579             BASL_TABLE_ALIGNMENT));
580
581         memset(&madt, 0, sizeof(madt));
582         BASL_EXEC(basl_table_append_header(table, ACPI_SIG_MADT, 1, 1));
583         madt.Address = htole32(BHYVE_ADDRESS_LAPIC);
584         madt.Flags = htole32(ACPI_MADT_PCAT_COMPAT);
585         BASL_EXEC(basl_table_append_content(table, &madt, sizeof(madt)));
586
587         /* Local APIC for each CPU */
588         for (int i = 0; i < basl_ncpu; ++i) {
589                 memset(&madt_lapic, 0, sizeof(madt_lapic));
590                 madt_lapic.Header.Type = ACPI_MADT_TYPE_LOCAL_APIC;
591                 madt_lapic.Header.Length = sizeof(madt_lapic);
592                 madt_lapic.ProcessorId = i;
593                 madt_lapic.Id = i;
594                 madt_lapic.LapicFlags = htole32(ACPI_MADT_ENABLED);
595                 BASL_EXEC(basl_table_append_bytes(table, &madt_lapic,
596                     sizeof(madt_lapic)));
597         }
598
599         /* I/O APIC */
600         memset(&madt_ioapic, 0, sizeof(madt_ioapic));
601         madt_ioapic.Header.Type = ACPI_MADT_TYPE_IO_APIC;
602         madt_ioapic.Header.Length = sizeof(madt_ioapic);
603         madt_ioapic.Address = htole32(BHYVE_ADDRESS_IOAPIC);
604         BASL_EXEC(
605             basl_table_append_bytes(table, &madt_ioapic, sizeof(madt_ioapic)));
606
607         /* Legacy IRQ0 is connected to pin 2 of the I/O APIC */
608         memset(&madt_irq_override, 0, sizeof(madt_irq_override));
609         madt_irq_override.Header.Type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE;
610         madt_irq_override.Header.Length = sizeof(madt_irq_override);
611         madt_irq_override.GlobalIrq = htole32(2);
612         madt_irq_override.IntiFlags = htole16(
613             ACPI_MADT_POLARITY_ACTIVE_HIGH | ACPI_MADT_TRIGGER_EDGE);
614         BASL_EXEC(basl_table_append_bytes(table, &madt_irq_override,
615             sizeof(madt_irq_override)));
616
617         memset(&madt_irq_override, 0, sizeof(madt_irq_override));
618         madt_irq_override.Header.Type = ACPI_MADT_TYPE_INTERRUPT_OVERRIDE;
619         madt_irq_override.Header.Length = sizeof(madt_irq_override);
620         madt_irq_override.SourceIrq = SCI_INT;
621         madt_irq_override.GlobalIrq = htole32(SCI_INT);
622         madt_irq_override.IntiFlags = htole16(
623             ACPI_MADT_POLARITY_ACTIVE_LOW | ACPI_MADT_TRIGGER_LEVEL);
624         BASL_EXEC(basl_table_append_bytes(table, &madt_irq_override,
625             sizeof(madt_irq_override)));
626
627         /* Local APIC NMI is conntected to LINT 1 on all CPUs */
628         memset(&madt_lapic_nmi, 0, sizeof(madt_lapic_nmi));
629         madt_lapic_nmi.Header.Type = ACPI_MADT_TYPE_LOCAL_APIC_NMI;
630         madt_lapic_nmi.Header.Length = sizeof(madt_lapic_nmi);
631         madt_lapic_nmi.ProcessorId = 0xFF;
632         madt_lapic_nmi.IntiFlags = htole16(
633             ACPI_MADT_POLARITY_ACTIVE_HIGH | ACPI_MADT_TRIGGER_EDGE);
634         madt_lapic_nmi.Lint = 1;
635         BASL_EXEC(basl_table_append_bytes(table, &madt_lapic_nmi,
636             sizeof(madt_lapic_nmi)));
637
638         BASL_EXEC(basl_table_append_pointer(rsdt, ACPI_SIG_MADT,
639             ACPI_RSDT_ENTRY_SIZE));
640         BASL_EXEC(basl_table_append_pointer(xsdt, ACPI_SIG_MADT,
641             ACPI_XSDT_ENTRY_SIZE));
642
643         return (0);
644 }
645
646 static int
647 build_mcfg(struct vmctx *const ctx)
648 {
649         ACPI_TABLE_MCFG mcfg;
650         ACPI_MCFG_ALLOCATION mcfg_allocation;
651         struct basl_table *table;
652
653         BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_MCFG,
654             BASL_TABLE_ALIGNMENT));
655
656         memset(&mcfg, 0, sizeof(mcfg));
657         BASL_EXEC(basl_table_append_header(table, ACPI_SIG_MCFG, 1, 1));
658         BASL_EXEC(basl_table_append_content(table, &mcfg, sizeof(mcfg)));
659
660         memset(&mcfg_allocation, 0, sizeof(mcfg_allocation));
661         mcfg_allocation.Address = htole64(pci_ecfg_base());
662         mcfg_allocation.EndBusNumber = 0xFF;
663         BASL_EXEC(basl_table_append_bytes(table, &mcfg_allocation,
664             sizeof(mcfg_allocation)));
665
666         BASL_EXEC(basl_table_append_pointer(rsdt, ACPI_SIG_MCFG,
667             ACPI_RSDT_ENTRY_SIZE));
668         BASL_EXEC(basl_table_append_pointer(xsdt, ACPI_SIG_MCFG,
669             ACPI_XSDT_ENTRY_SIZE));
670
671         return (0);
672 }
673
674 static int
675 build_rsdp(struct vmctx *const ctx)
676 {
677         ACPI_TABLE_RSDP rsdp;
678         struct basl_table *table;
679
680         BASL_EXEC(basl_table_create(&table, ctx, ACPI_RSDP_NAME,
681             BASL_TABLE_ALIGNMENT));
682
683         memset(&rsdp, 0, sizeof(rsdp));
684         memcpy(rsdp.Signature, ACPI_SIG_RSDP, 8);
685         rsdp.Checksum = 0; /* patched by basl */
686         memcpy(rsdp.OemId, "BHYVE ", ACPI_OEM_ID_SIZE);
687         rsdp.Revision = 2;
688         rsdp.RsdtPhysicalAddress = htole32(0); /* patched by basl */
689         rsdp.Length = htole32(0);              /* patched by basl */
690         rsdp.XsdtPhysicalAddress = htole64(0); /* patched by basl */
691         rsdp.ExtendedChecksum = 0;             /* patched by basl */
692         BASL_EXEC(basl_table_append_bytes(table, &rsdp, sizeof(rsdp)));
693
694         BASL_EXEC(basl_table_add_checksum(table,
695             offsetof(ACPI_TABLE_RSDP, Checksum), 0, 20));
696         BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_RSDT,
697             offsetof(ACPI_TABLE_RSDP, RsdtPhysicalAddress),
698             sizeof(rsdp.RsdtPhysicalAddress)));
699         BASL_EXEC(basl_table_add_length(table,
700             offsetof(ACPI_TABLE_RSDP, Length), sizeof(rsdp.Length)));
701         BASL_EXEC(basl_table_add_pointer(table, ACPI_SIG_XSDT,
702             offsetof(ACPI_TABLE_RSDP, XsdtPhysicalAddress),
703             sizeof(rsdp.XsdtPhysicalAddress)));
704         BASL_EXEC(basl_table_add_checksum(table,
705             offsetof(ACPI_TABLE_RSDP, ExtendedChecksum), 0,
706             BASL_TABLE_CHECKSUM_LEN_FULL_TABLE));
707
708         return (0);
709 }
710
711 static int
712 build_rsdt(struct vmctx *const ctx)
713 {
714         BASL_EXEC(
715             basl_table_create(&rsdt, ctx, ACPI_SIG_RSDT, BASL_TABLE_ALIGNMENT));
716
717         /* Header */
718         BASL_EXEC(basl_table_append_header(rsdt, ACPI_SIG_RSDT, 1, 1));
719         /* Pointers (added by other build_XXX funcs) */
720
721         return (0);
722 }
723
724 static int
725 build_spcr(struct vmctx *const ctx)
726 {
727         ACPI_TABLE_SPCR spcr;
728         struct basl_table *table;
729
730         BASL_EXEC(basl_table_create(&table, ctx, ACPI_SIG_SPCR,
731             BASL_TABLE_ALIGNMENT));
732
733         memset(&spcr, 0, sizeof(spcr));
734         BASL_EXEC(basl_table_append_header(table, ACPI_SIG_SPCR, 1, 1));
735         spcr.InterfaceType = ACPI_DBG2_16550_COMPATIBLE;
736         basl_fill_gas(&spcr.SerialPort, ACPI_ADR_SPACE_SYSTEM_IO, 8, 0,
737             ACPI_GAS_ACCESS_WIDTH_LEGACY, 0x3F8);
738         spcr.InterruptType = ACPI_SPCR_INTERRUPT_TYPE_8259;
739         spcr.PcInterrupt = 4;
740         spcr.BaudRate = ACPI_SPCR_BAUD_RATE_115200;
741         spcr.Parity = ACPI_SPCR_PARITY_NO_PARITY;
742         spcr.StopBits = ACPI_SPCR_STOP_BITS_1;
743         spcr.FlowControl = 3; /* RTS/CTS | DCD */
744         spcr.TerminalType = ACPI_SPCR_TERMINAL_TYPE_VT_UTF8;
745         BASL_EXEC(basl_table_append_content(table, &spcr, sizeof(spcr)));
746
747         BASL_EXEC(basl_table_append_pointer(rsdt, ACPI_SIG_SPCR,
748             ACPI_RSDT_ENTRY_SIZE));
749         BASL_EXEC(basl_table_append_pointer(xsdt, ACPI_SIG_SPCR,
750             ACPI_XSDT_ENTRY_SIZE));
751
752         return (0);
753 }
754
755 static int
756 build_xsdt(struct vmctx *const ctx)
757 {
758         BASL_EXEC(
759             basl_table_create(&xsdt, ctx, ACPI_SIG_XSDT, BASL_TABLE_ALIGNMENT));
760
761         /* Header */
762         BASL_EXEC(basl_table_append_header(xsdt, ACPI_SIG_XSDT, 1, 1));
763         /* Pointers (added by other build_XXX funcs) */
764
765         return (0);
766 }
767
768 int
769 acpi_build(struct vmctx *ctx, int ncpu)
770 {
771         int err;
772
773         basl_ncpu = ncpu;
774
775         err = vm_get_hpet_capabilities(ctx, &hpet_capabilities);
776         if (err != 0)
777                 return (err);
778
779         /*
780          * For debug, allow the user to have iasl compiler output sent
781          * to stdout rather than /dev/null
782          */
783         if (getenv("BHYVE_ACPI_VERBOSE_IASL"))
784                 basl_verbose_iasl = 1;
785
786         /*
787          * Allow the user to keep the generated ASL files for debugging
788          * instead of deleting them following use
789          */
790         if (getenv("BHYVE_ACPI_KEEPTMPS"))
791                 basl_keep_temps = 1;
792
793         BASL_EXEC(basl_init());
794
795         BASL_EXEC(basl_make_templates());
796
797         /*
798          * Generate ACPI tables and copy them into guest memory.
799          *
800          * According to UEFI Specification v6.3 chapter 5.1 the FADT should be
801          * the first table pointed to by XSDT. For that reason, build it as the
802          * first table after XSDT.
803          */
804         BASL_EXEC(build_rsdp(ctx));
805         BASL_EXEC(build_rsdt(ctx));
806         BASL_EXEC(build_xsdt(ctx));
807         BASL_EXEC(build_fadt(ctx));
808         BASL_EXEC(build_madt(ctx));
809         BASL_EXEC(build_hpet(ctx));
810         BASL_EXEC(build_mcfg(ctx));
811         BASL_EXEC(build_facs(ctx));
812         BASL_EXEC(build_spcr(ctx));
813
814         /* Build ACPI device-specific tables such as a TPM2 table. */
815         const struct acpi_device_list_entry *entry;
816         SLIST_FOREACH(entry, &acpi_devices, chain) {
817                 BASL_EXEC(acpi_device_build_table(entry->dev));
818         }
819
820         BASL_EXEC(build_dsdt(ctx));
821
822         BASL_EXEC(basl_finish());
823
824         return (0);
825 }