1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-NetBSD 3 * 4 * Copyright (c) 2004 Christian Limpach. 5 * Copyright (c) 2004-2006,2008 Kip Macy 6 * Copyright (c) 2008 The NetBSD Foundation, Inc. 7 * Copyright (c) 2013 Roger Pau Monné <roger.pau@citrix.com> 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include "opt_ddb.h" 36 #include "opt_kstack_pages.h" 37 38 #include <sys/param.h> 39 #include <sys/bus.h> 40 #include <sys/kernel.h> 41 #include <sys/reboot.h> 42 #include <sys/systm.h> 43 #include <sys/malloc.h> 44 #include <sys/linker.h> 45 #include <sys/lock.h> 46 #include <sys/rwlock.h> 47 #include <sys/boot.h> 48 #include <sys/ctype.h> 49 #include <sys/mutex.h> 50 #include <sys/smp.h> 51 52 #include <vm/vm.h> 53 #include <vm/vm_extern.h> 54 #include <vm/vm_kern.h> 55 #include <vm/vm_page.h> 56 #include <vm/vm_map.h> 57 #include <vm/vm_object.h> 58 #include <vm/vm_pager.h> 59 #include <vm/vm_param.h> 60 61 #include <machine/_inttypes.h> 62 #include <machine/intr_machdep.h> 63 #include <x86/apicvar.h> 64 #include <x86/init.h> 65 #include <machine/pc/bios.h> 66 #include <machine/smp.h> 67 #include <machine/intr_machdep.h> 68 #include <machine/metadata.h> 69 70 #include <xen/xen-os.h> 71 #include <xen/hvm.h> 72 #include <xen/hypervisor.h> 73 #include <xen/xenstore/xenstorevar.h> 74 #include <xen/xen_pv.h> 75 #include <xen/xen_msi.h> 76 77 #include <xen/interface/arch-x86/hvm/start_info.h> 78 #include <xen/interface/vcpu.h> 79 80 #include <dev/xen/timer/timer.h> 81 82 #ifdef DDB 83 #include <ddb/ddb.h> 84 #endif 85 86 /* Native initial function */ 87 extern u_int64_t hammer_time(u_int64_t, u_int64_t); 88 /* Xen initial function */ 89 uint64_t hammer_time_xen_legacy(start_info_t *, uint64_t); 90 uint64_t hammer_time_xen(vm_paddr_t); 91 92 #define MAX_E820_ENTRIES 128 93 94 /*--------------------------- Forward Declarations ---------------------------*/ 95 static caddr_t xen_legacy_pvh_parse_preload_data(uint64_t); 96 static caddr_t xen_pvh_parse_preload_data(uint64_t); 97 static void xen_pvh_parse_memmap(caddr_t, vm_paddr_t *, int *); 98 99 #ifdef SMP 100 static int xen_pv_start_all_aps(void); 101 #endif 102 103 /*---------------------------- Extern Declarations ---------------------------*/ 104 #ifdef SMP 105 /* Variables used by amd64 mp_machdep to start APs */ 106 extern char *doublefault_stack; 107 extern char *mce_stack; 108 extern char *nmi_stack; 109 extern char *dbg_stack; 110 #endif 111 112 /* 113 * Placed by the linker at the end of the bss section, which is the last 114 * section loaded by Xen before loading the symtab and strtab. 115 */ 116 extern uint32_t end; 117 118 /*-------------------------------- Global Data -------------------------------*/ 119 /* Xen init_ops implementation. */ 120 struct init_ops xen_legacy_init_ops = { 121 .parse_preload_data = xen_legacy_pvh_parse_preload_data, 122 .early_clock_source_init = xen_clock_init, 123 .early_delay = xen_delay, 124 .parse_memmap = xen_pvh_parse_memmap, 125 #ifdef SMP 126 .start_all_aps = xen_pv_start_all_aps, 127 #endif 128 .msi_init = xen_msi_init, 129 }; 130 131 struct init_ops xen_pvh_init_ops = { 132 .parse_preload_data = xen_pvh_parse_preload_data, 133 .early_clock_source_init = xen_clock_init, 134 .early_delay = xen_delay, 135 .parse_memmap = xen_pvh_parse_memmap, 136 #ifdef SMP 137 .mp_bootaddress = mp_bootaddress, 138 .start_all_aps = native_start_all_aps, 139 #endif 140 .msi_init = msi_init, 141 }; 142 143 static struct bios_smap xen_smap[MAX_E820_ENTRIES]; 144 145 static start_info_t *legacy_start_info; 146 static struct hvm_start_info *start_info; 147 148 /*----------------------- Legacy PVH start_info accessors --------------------*/ 149 static vm_paddr_t 150 legacy_get_xenstore_mfn(void) 151 { 152 153 return (legacy_start_info->store_mfn); 154 } 155 156 static evtchn_port_t 157 legacy_get_xenstore_evtchn(void) 158 { 159 160 return (legacy_start_info->store_evtchn); 161 } 162 163 static vm_paddr_t 164 legacy_get_console_mfn(void) 165 { 166 167 return (legacy_start_info->console.domU.mfn); 168 } 169 170 static evtchn_port_t 171 legacy_get_console_evtchn(void) 172 { 173 174 return (legacy_start_info->console.domU.evtchn); 175 } 176 177 static uint32_t 178 legacy_get_start_flags(void) 179 { 180 181 return (legacy_start_info->flags); 182 } 183 184 struct hypervisor_info legacy_info = { 185 .get_xenstore_mfn = legacy_get_xenstore_mfn, 186 .get_xenstore_evtchn = legacy_get_xenstore_evtchn, 187 .get_console_mfn = legacy_get_console_mfn, 188 .get_console_evtchn = legacy_get_console_evtchn, 189 .get_start_flags = legacy_get_start_flags, 190 }; 191 192 /*-------------------------------- Xen PV init -------------------------------*/ 193 /* 194 * First function called by the Xen legacy PVH boot sequence. 195 * 196 * Set some Xen global variables and prepare the environment so it is 197 * as similar as possible to what native FreeBSD init function expects. 198 */ 199 uint64_t 200 hammer_time_xen_legacy(start_info_t *si, uint64_t xenstack) 201 { 202 uint64_t physfree; 203 uint64_t *PT4 = (u_int64_t *)xenstack; 204 uint64_t *PT3 = (u_int64_t *)(xenstack + PAGE_SIZE); 205 uint64_t *PT2 = (u_int64_t *)(xenstack + 2 * PAGE_SIZE); 206 int i; 207 208 xen_domain_type = XEN_PV_DOMAIN; 209 vm_guest = VM_GUEST_XEN; 210 211 if ((si == NULL) || (xenstack == 0)) { 212 xc_printf("ERROR: invalid start_info or xen stack, halting\n"); 213 HYPERVISOR_shutdown(SHUTDOWN_crash); 214 } 215 216 xc_printf("FreeBSD PVH running on %s\n", si->magic); 217 218 /* We use 3 pages of xen stack for the boot pagetables */ 219 physfree = xenstack + 3 * PAGE_SIZE - KERNBASE; 220 221 /* Setup Xen global variables */ 222 legacy_start_info = si; 223 HYPERVISOR_shared_info = 224 (shared_info_t *)(si->shared_info + KERNBASE); 225 226 /* 227 * Use the stack Xen gives us to build the page tables 228 * as native FreeBSD expects to find them (created 229 * by the boot trampoline). 230 */ 231 for (i = 0; i < (PAGE_SIZE / sizeof(uint64_t)); i++) { 232 /* 233 * Each slot of the level 4 pages points 234 * to the same level 3 page 235 */ 236 PT4[i] = ((uint64_t)&PT3[0]) - KERNBASE; 237 PT4[i] |= PG_V | PG_RW | PG_U; 238 239 /* 240 * Each slot of the level 3 pages points 241 * to the same level 2 page 242 */ 243 PT3[i] = ((uint64_t)&PT2[0]) - KERNBASE; 244 PT3[i] |= PG_V | PG_RW | PG_U; 245 246 /* 247 * The level 2 page slots are mapped with 248 * 2MB pages for 1GB. 249 */ 250 PT2[i] = i * (2 * 1024 * 1024); 251 PT2[i] |= PG_V | PG_RW | PG_PS | PG_U; 252 } 253 load_cr3(((uint64_t)&PT4[0]) - KERNBASE); 254 255 /* Set the hooks for early functions that diverge from bare metal */ 256 init_ops = xen_legacy_init_ops; 257 apic_ops = xen_apic_ops; 258 hypervisor_info = legacy_info; 259 260 /* Now we can jump into the native init function */ 261 return (hammer_time(0, physfree)); 262 } 263 264 uint64_t 265 hammer_time_xen(vm_paddr_t start_info_paddr) 266 { 267 struct hvm_modlist_entry *mod; 268 struct xen_add_to_physmap xatp; 269 uint64_t physfree; 270 char *kenv; 271 int rc; 272 273 xen_domain_type = XEN_HVM_DOMAIN; 274 vm_guest = VM_GUEST_XEN; 275 276 rc = xen_hvm_init_hypercall_stubs(XEN_HVM_INIT_EARLY); 277 if (rc) { 278 xc_printf("ERROR: failed to initialize hypercall page: %d\n", 279 rc); 280 HYPERVISOR_shutdown(SHUTDOWN_crash); 281 } 282 283 start_info = (struct hvm_start_info *)(start_info_paddr + KERNBASE); 284 if (start_info->magic != XEN_HVM_START_MAGIC_VALUE) { 285 xc_printf("Unknown magic value in start_info struct: %#x\n", 286 start_info->magic); 287 HYPERVISOR_shutdown(SHUTDOWN_crash); 288 } 289 290 /* 291 * The hvm_start_into structure is always appended after loading 292 * the kernel and modules. 293 */ 294 physfree = roundup2(start_info_paddr + PAGE_SIZE, PAGE_SIZE); 295 296 xatp.domid = DOMID_SELF; 297 xatp.idx = 0; 298 xatp.space = XENMAPSPACE_shared_info; 299 xatp.gpfn = atop(physfree); 300 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) { 301 xc_printf("ERROR: failed to setup shared_info page\n"); 302 HYPERVISOR_shutdown(SHUTDOWN_crash); 303 } 304 HYPERVISOR_shared_info = (shared_info_t *)(physfree + KERNBASE); 305 physfree += PAGE_SIZE; 306 307 /* 308 * Init a static kenv using a free page. The contents will be filled 309 * from the parse_preload_data hook. 310 */ 311 kenv = (void *)(physfree + KERNBASE); 312 physfree += PAGE_SIZE; 313 bzero(kenv, PAGE_SIZE); 314 init_static_kenv(kenv, PAGE_SIZE); 315 316 if (start_info->modlist_paddr != 0) { 317 if (start_info->modlist_paddr >= physfree) { 318 xc_printf( 319 "ERROR: unexpected module list memory address\n"); 320 HYPERVISOR_shutdown(SHUTDOWN_crash); 321 } 322 if (start_info->nr_modules == 0) { 323 xc_printf( 324 "ERROR: modlist_paddr != 0 but nr_modules == 0\n"); 325 HYPERVISOR_shutdown(SHUTDOWN_crash); 326 } 327 mod = (struct hvm_modlist_entry *) 328 (vm_paddr_t)start_info->modlist_paddr + KERNBASE; 329 if (mod[0].paddr >= physfree) { 330 xc_printf("ERROR: unexpected module memory address\n"); 331 HYPERVISOR_shutdown(SHUTDOWN_crash); 332 } 333 } 334 335 /* Set the hooks for early functions that diverge from bare metal */ 336 init_ops = xen_pvh_init_ops; 337 hvm_start_flags = start_info->flags; 338 339 /* Now we can jump into the native init function */ 340 return (hammer_time(0, physfree)); 341 } 342 343 /*-------------------------------- PV specific -------------------------------*/ 344 #ifdef SMP 345 static bool 346 start_xen_ap(int cpu) 347 { 348 struct vcpu_guest_context *ctxt; 349 int ms, cpus = mp_naps; 350 const size_t stacksize = kstack_pages * PAGE_SIZE; 351 352 /* allocate and set up an idle stack data page */ 353 bootstacks[cpu] = 354 (void *)kmem_malloc(kernel_arena, stacksize, M_WAITOK | M_ZERO); 355 doublefault_stack = 356 (char *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO); 357 mce_stack = 358 (char *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO); 359 nmi_stack = 360 (char *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO); 361 dbg_stack = 362 (void *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO); 363 dpcpu = 364 (void *)kmem_malloc(kernel_arena, DPCPU_SIZE, M_WAITOK | M_ZERO); 365 366 bootSTK = (char *)bootstacks[cpu] + kstack_pages * PAGE_SIZE - 8; 367 bootAP = cpu; 368 369 ctxt = malloc(sizeof(*ctxt), M_TEMP, M_WAITOK | M_ZERO); 370 371 ctxt->flags = VGCF_IN_KERNEL; 372 ctxt->user_regs.rip = (unsigned long) init_secondary; 373 ctxt->user_regs.rsp = (unsigned long) bootSTK; 374 375 /* Set the AP to use the same page tables */ 376 ctxt->ctrlreg[3] = KPML4phys; 377 378 if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt)) 379 panic("unable to initialize AP#%d", cpu); 380 381 free(ctxt, M_TEMP); 382 383 /* Launch the vCPU */ 384 if (HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL)) 385 panic("unable to start AP#%d", cpu); 386 387 /* Wait up to 5 seconds for it to start. */ 388 for (ms = 0; ms < 5000; ms++) { 389 if (mp_naps > cpus) 390 return (true); 391 DELAY(1000); 392 } 393 394 return (false); 395 } 396 397 static int 398 xen_pv_start_all_aps(void) 399 { 400 int cpu; 401 402 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN); 403 404 for (cpu = 1; cpu < mp_ncpus; cpu++) { 405 406 /* attempt to start the Application Processor */ 407 if (!start_xen_ap(cpu)) 408 panic("AP #%d failed to start!", cpu); 409 410 CPU_SET(cpu, &all_cpus); /* record AP in CPU map */ 411 } 412 413 return (mp_naps); 414 } 415 #endif /* SMP */ 416 417 /* 418 * When booted as a PVH guest FreeBSD needs to avoid using the RSDP address 419 * hint provided by the loader because it points to the native set of ACPI 420 * tables instead of the ones crafted by Xen. The acpi.rsdp env variable is 421 * removed from kenv if present, and a new acpi.rsdp is added to kenv that 422 * points to the address of the Xen crafted RSDP. 423 */ 424 static bool reject_option(const char *option) 425 { 426 static const char *reject[] = { 427 "acpi.rsdp", 428 }; 429 unsigned int i; 430 431 for (i = 0; i < nitems(reject); i++) 432 if (strncmp(option, reject[i], strlen(reject[i])) == 0) 433 return (true); 434 435 return (false); 436 } 437 438 static void 439 xen_pvh_set_env(char *env, bool (*filter)(const char *)) 440 { 441 char *option; 442 443 if (env == NULL) 444 return; 445 446 option = env; 447 while (*option != 0) { 448 char *value; 449 450 if (filter != NULL && filter(option)) { 451 option += strlen(option) + 1; 452 continue; 453 } 454 455 value = option; 456 option = strsep(&value, "="); 457 if (kern_setenv(option, value) != 0) 458 xc_printf("unable to add kenv %s=%s\n", option, value); 459 option = value + strlen(value) + 1; 460 } 461 } 462 463 #ifdef DDB 464 /* 465 * The way Xen loads the symtab is different from the native boot loader, 466 * because it's tailored for NetBSD. So we have to adapt and use the same 467 * method as NetBSD. Portions of the code below have been picked from NetBSD: 468 * sys/kern/kern_ksyms.c CVS Revision 1.71. 469 */ 470 static void 471 xen_pvh_parse_symtab(void) 472 { 473 Elf_Ehdr *ehdr; 474 Elf_Shdr *shdr; 475 uint32_t size; 476 int i, j; 477 478 size = end; 479 480 ehdr = (Elf_Ehdr *)(&end + 1); 481 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || 482 ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 483 ehdr->e_version > 1) { 484 xc_printf("Unable to load ELF symtab: invalid symbol table\n"); 485 return; 486 } 487 488 shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff); 489 /* Find the symbol table and the corresponding string table. */ 490 for (i = 1; i < ehdr->e_shnum; i++) { 491 if (shdr[i].sh_type != SHT_SYMTAB) 492 continue; 493 if (shdr[i].sh_offset == 0) 494 continue; 495 ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset); 496 ksymtab_size = shdr[i].sh_size; 497 j = shdr[i].sh_link; 498 if (shdr[j].sh_offset == 0) 499 continue; /* Can this happen? */ 500 kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset); 501 break; 502 } 503 504 if (ksymtab == 0 || kstrtab == 0) 505 xc_printf( 506 "Unable to load ELF symtab: could not find symtab or strtab\n"); 507 } 508 #endif 509 510 static caddr_t 511 xen_legacy_pvh_parse_preload_data(uint64_t modulep) 512 { 513 caddr_t kmdp; 514 vm_ooffset_t off; 515 vm_paddr_t metadata; 516 char *envp; 517 518 if (legacy_start_info->mod_start != 0) { 519 preload_metadata = (caddr_t)legacy_start_info->mod_start; 520 521 kmdp = preload_search_by_type("elf kernel"); 522 if (kmdp == NULL) 523 kmdp = preload_search_by_type("elf64 kernel"); 524 KASSERT(kmdp != NULL, ("unable to find kernel")); 525 526 /* 527 * Xen has relocated the metadata and the modules, 528 * so we need to recalculate it's position. This is 529 * done by saving the original modulep address and 530 * then calculating the offset with mod_start, 531 * which contains the relocated modulep address. 532 */ 533 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t); 534 off = legacy_start_info->mod_start - metadata; 535 536 preload_bootstrap_relocate(off); 537 538 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 539 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *); 540 if (envp != NULL) 541 envp += off; 542 xen_pvh_set_env(envp, NULL); 543 } else { 544 /* Parse the extra boot information given by Xen */ 545 boot_parse_cmdline_delim(legacy_start_info->cmd_line, ","); 546 kmdp = NULL; 547 } 548 549 boothowto |= boot_env_to_howto(); 550 551 #ifdef DDB 552 xen_pvh_parse_symtab(); 553 #endif 554 return (kmdp); 555 } 556 557 static caddr_t 558 xen_pvh_parse_preload_data(uint64_t modulep) 559 { 560 caddr_t kmdp; 561 vm_ooffset_t off; 562 vm_paddr_t metadata; 563 char *envp; 564 char acpi_rsdp[19]; 565 566 if (start_info->modlist_paddr != 0) { 567 struct hvm_modlist_entry *mod; 568 569 mod = (struct hvm_modlist_entry *) 570 (start_info->modlist_paddr + KERNBASE); 571 preload_metadata = (caddr_t)(mod[0].paddr + KERNBASE); 572 573 kmdp = preload_search_by_type("elf kernel"); 574 if (kmdp == NULL) 575 kmdp = preload_search_by_type("elf64 kernel"); 576 KASSERT(kmdp != NULL, ("unable to find kernel")); 577 578 /* 579 * Xen has relocated the metadata and the modules, 580 * so we need to recalculate it's position. This is 581 * done by saving the original modulep address and 582 * then calculating the offset with mod_start, 583 * which contains the relocated modulep address. 584 */ 585 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t); 586 off = mod[0].paddr + KERNBASE - metadata; 587 588 preload_bootstrap_relocate(off); 589 590 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 591 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *); 592 if (envp != NULL) 593 envp += off; 594 xen_pvh_set_env(envp, reject_option); 595 } else { 596 /* Parse the extra boot information given by Xen */ 597 if (start_info->cmdline_paddr != 0) 598 boot_parse_cmdline_delim( 599 (char *)(start_info->cmdline_paddr + KERNBASE), 600 ","); 601 kmdp = NULL; 602 } 603 604 boothowto |= boot_env_to_howto(); 605 606 snprintf(acpi_rsdp, sizeof(acpi_rsdp), "%#" PRIx64, 607 start_info->rsdp_paddr); 608 kern_setenv("acpi.rsdp", acpi_rsdp); 609 610 #ifdef DDB 611 xen_pvh_parse_symtab(); 612 #endif 613 return (kmdp); 614 } 615 616 static void 617 xen_pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx) 618 { 619 struct xen_memory_map memmap; 620 u_int32_t size; 621 int rc; 622 623 /* Fetch the E820 map from Xen */ 624 memmap.nr_entries = MAX_E820_ENTRIES; 625 set_xen_guest_handle(memmap.buffer, xen_smap); 626 rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); 627 if (rc) { 628 xc_printf("ERROR: unable to fetch Xen E820 memory map: %d\n", 629 rc); 630 HYPERVISOR_shutdown(SHUTDOWN_crash); 631 } 632 633 size = memmap.nr_entries * sizeof(xen_smap[0]); 634 635 bios_add_smap_entries(xen_smap, size, physmap, physmap_idx); 636 } 637