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