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 char *kenv; 208 209 xen_domain_type = XEN_PV_DOMAIN; 210 vm_guest = VM_GUEST_XEN; 211 212 if ((si == NULL) || (xenstack == 0)) { 213 xc_printf("ERROR: invalid start_info or xen stack, halting\n"); 214 HYPERVISOR_shutdown(SHUTDOWN_crash); 215 } 216 217 xc_printf("FreeBSD PVH running on %s\n", si->magic); 218 219 /* We use 3 pages of xen stack for the boot pagetables */ 220 physfree = xenstack + 3 * PAGE_SIZE - KERNBASE; 221 222 /* Setup Xen global variables */ 223 legacy_start_info = si; 224 HYPERVISOR_shared_info = 225 (shared_info_t *)(si->shared_info + KERNBASE); 226 227 /* 228 * Use the stack Xen gives us to build the page tables 229 * as native FreeBSD expects to find them (created 230 * by the boot trampoline). 231 */ 232 for (i = 0; i < (PAGE_SIZE / sizeof(uint64_t)); i++) { 233 /* 234 * Each slot of the level 4 pages points 235 * to the same level 3 page 236 */ 237 PT4[i] = ((uint64_t)&PT3[0]) - KERNBASE; 238 PT4[i] |= PG_V | PG_RW | PG_U; 239 240 /* 241 * Each slot of the level 3 pages points 242 * to the same level 2 page 243 */ 244 PT3[i] = ((uint64_t)&PT2[0]) - KERNBASE; 245 PT3[i] |= PG_V | PG_RW | PG_U; 246 247 /* 248 * The level 2 page slots are mapped with 249 * 2MB pages for 1GB. 250 */ 251 PT2[i] = i * (2 * 1024 * 1024); 252 PT2[i] |= PG_V | PG_RW | PG_PS | PG_U; 253 } 254 load_cr3(((uint64_t)&PT4[0]) - KERNBASE); 255 256 /* 257 * Init an empty static kenv using a free page. The contents will be 258 * filled from the parse_preload_data hook. 259 */ 260 kenv = (void *)(physfree + KERNBASE); 261 physfree += PAGE_SIZE; 262 bzero_early(kenv, PAGE_SIZE); 263 init_static_kenv(kenv, PAGE_SIZE); 264 265 /* Set the hooks for early functions that diverge from bare metal */ 266 init_ops = xen_legacy_init_ops; 267 apic_ops = xen_apic_ops; 268 hypervisor_info = legacy_info; 269 270 /* Now we can jump into the native init function */ 271 return (hammer_time(0, physfree)); 272 } 273 274 uint64_t 275 hammer_time_xen(vm_paddr_t start_info_paddr) 276 { 277 struct hvm_modlist_entry *mod; 278 struct xen_add_to_physmap xatp; 279 uint64_t physfree; 280 char *kenv; 281 int rc; 282 283 xen_domain_type = XEN_HVM_DOMAIN; 284 vm_guest = VM_GUEST_XEN; 285 286 rc = xen_hvm_init_hypercall_stubs(XEN_HVM_INIT_EARLY); 287 if (rc) { 288 xc_printf("ERROR: failed to initialize hypercall page: %d\n", 289 rc); 290 HYPERVISOR_shutdown(SHUTDOWN_crash); 291 } 292 293 start_info = (struct hvm_start_info *)(start_info_paddr + KERNBASE); 294 if (start_info->magic != XEN_HVM_START_MAGIC_VALUE) { 295 xc_printf("Unknown magic value in start_info struct: %#x\n", 296 start_info->magic); 297 HYPERVISOR_shutdown(SHUTDOWN_crash); 298 } 299 300 /* 301 * The hvm_start_into structure is always appended after loading 302 * the kernel and modules. 303 */ 304 physfree = roundup2(start_info_paddr + PAGE_SIZE, PAGE_SIZE); 305 306 xatp.domid = DOMID_SELF; 307 xatp.idx = 0; 308 xatp.space = XENMAPSPACE_shared_info; 309 xatp.gpfn = atop(physfree); 310 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) { 311 xc_printf("ERROR: failed to setup shared_info page\n"); 312 HYPERVISOR_shutdown(SHUTDOWN_crash); 313 } 314 HYPERVISOR_shared_info = (shared_info_t *)(physfree + KERNBASE); 315 physfree += PAGE_SIZE; 316 317 /* 318 * Init a static kenv using a free page. The contents will be filled 319 * from the parse_preload_data hook. 320 */ 321 kenv = (void *)(physfree + KERNBASE); 322 physfree += PAGE_SIZE; 323 bzero_early(kenv, PAGE_SIZE); 324 init_static_kenv(kenv, PAGE_SIZE); 325 326 if (start_info->modlist_paddr != 0) { 327 if (start_info->modlist_paddr >= physfree) { 328 xc_printf( 329 "ERROR: unexpected module list memory address\n"); 330 HYPERVISOR_shutdown(SHUTDOWN_crash); 331 } 332 if (start_info->nr_modules == 0) { 333 xc_printf( 334 "ERROR: modlist_paddr != 0 but nr_modules == 0\n"); 335 HYPERVISOR_shutdown(SHUTDOWN_crash); 336 } 337 mod = (struct hvm_modlist_entry *) 338 (vm_paddr_t)start_info->modlist_paddr + KERNBASE; 339 if (mod[0].paddr >= physfree) { 340 xc_printf("ERROR: unexpected module memory address\n"); 341 HYPERVISOR_shutdown(SHUTDOWN_crash); 342 } 343 } 344 345 /* Set the hooks for early functions that diverge from bare metal */ 346 init_ops = xen_pvh_init_ops; 347 hvm_start_flags = start_info->flags; 348 349 /* Now we can jump into the native init function */ 350 return (hammer_time(0, physfree)); 351 } 352 353 /*-------------------------------- PV specific -------------------------------*/ 354 #ifdef SMP 355 static bool 356 start_xen_ap(int cpu) 357 { 358 struct vcpu_guest_context *ctxt; 359 int ms, cpus = mp_naps; 360 const size_t stacksize = kstack_pages * PAGE_SIZE; 361 362 /* allocate and set up an idle stack data page */ 363 bootstacks[cpu] = (void *)kmem_malloc(stacksize, M_WAITOK | M_ZERO); 364 doublefault_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO); 365 mce_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO); 366 nmi_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO); 367 dbg_stack = (void *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO); 368 dpcpu = (void *)kmem_malloc(DPCPU_SIZE, M_WAITOK | M_ZERO); 369 370 bootSTK = (char *)bootstacks[cpu] + kstack_pages * PAGE_SIZE - 8; 371 bootAP = cpu; 372 373 ctxt = malloc(sizeof(*ctxt), M_TEMP, M_WAITOK | M_ZERO); 374 375 ctxt->flags = VGCF_IN_KERNEL; 376 ctxt->user_regs.rip = (unsigned long) init_secondary; 377 ctxt->user_regs.rsp = (unsigned long) bootSTK; 378 379 /* Set the AP to use the same page tables */ 380 ctxt->ctrlreg[3] = KPML4phys; 381 382 if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt)) 383 panic("unable to initialize AP#%d", cpu); 384 385 free(ctxt, M_TEMP); 386 387 /* Launch the vCPU */ 388 if (HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL)) 389 panic("unable to start AP#%d", cpu); 390 391 /* Wait up to 5 seconds for it to start. */ 392 for (ms = 0; ms < 5000; ms++) { 393 if (mp_naps > cpus) 394 return (true); 395 DELAY(1000); 396 } 397 398 return (false); 399 } 400 401 static int 402 xen_pv_start_all_aps(void) 403 { 404 int cpu; 405 406 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN); 407 408 for (cpu = 1; cpu < mp_ncpus; cpu++) { 409 410 /* attempt to start the Application Processor */ 411 if (!start_xen_ap(cpu)) 412 panic("AP #%d failed to start!", cpu); 413 414 CPU_SET(cpu, &all_cpus); /* record AP in CPU map */ 415 } 416 417 return (mp_naps); 418 } 419 #endif /* SMP */ 420 421 /* 422 * When booted as a PVH guest FreeBSD needs to avoid using the RSDP address 423 * hint provided by the loader because it points to the native set of ACPI 424 * tables instead of the ones crafted by Xen. The acpi.rsdp env variable is 425 * removed from kenv if present, and a new acpi.rsdp is added to kenv that 426 * points to the address of the Xen crafted RSDP. 427 */ 428 static bool reject_option(const char *option) 429 { 430 static const char *reject[] = { 431 "acpi.rsdp", 432 }; 433 unsigned int i; 434 435 for (i = 0; i < nitems(reject); i++) 436 if (strncmp(option, reject[i], strlen(reject[i])) == 0) 437 return (true); 438 439 return (false); 440 } 441 442 static void 443 xen_pvh_set_env(char *env, bool (*filter)(const char *)) 444 { 445 char *option; 446 447 if (env == NULL) 448 return; 449 450 option = env; 451 while (*option != 0) { 452 char *value; 453 454 if (filter != NULL && filter(option)) { 455 option += strlen(option) + 1; 456 continue; 457 } 458 459 value = option; 460 option = strsep(&value, "="); 461 if (kern_setenv(option, value) != 0) 462 xc_printf("unable to add kenv %s=%s\n", option, value); 463 option = value + strlen(value) + 1; 464 } 465 } 466 467 #ifdef DDB 468 /* 469 * The way Xen loads the symtab is different from the native boot loader, 470 * because it's tailored for NetBSD. So we have to adapt and use the same 471 * method as NetBSD. Portions of the code below have been picked from NetBSD: 472 * sys/kern/kern_ksyms.c CVS Revision 1.71. 473 */ 474 static void 475 xen_pvh_parse_symtab(void) 476 { 477 Elf_Ehdr *ehdr; 478 Elf_Shdr *shdr; 479 uint32_t size; 480 int i, j; 481 482 size = end; 483 484 ehdr = (Elf_Ehdr *)(&end + 1); 485 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || 486 ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 487 ehdr->e_version > 1) { 488 xc_printf("Unable to load ELF symtab: invalid symbol table\n"); 489 return; 490 } 491 492 shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff); 493 /* Find the symbol table and the corresponding string table. */ 494 for (i = 1; i < ehdr->e_shnum; i++) { 495 if (shdr[i].sh_type != SHT_SYMTAB) 496 continue; 497 if (shdr[i].sh_offset == 0) 498 continue; 499 ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset); 500 ksymtab_size = shdr[i].sh_size; 501 j = shdr[i].sh_link; 502 if (shdr[j].sh_offset == 0) 503 continue; /* Can this happen? */ 504 kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset); 505 break; 506 } 507 508 if (ksymtab == 0 || kstrtab == 0) 509 xc_printf( 510 "Unable to load ELF symtab: could not find symtab or strtab\n"); 511 } 512 #endif 513 514 static caddr_t 515 xen_legacy_pvh_parse_preload_data(uint64_t modulep) 516 { 517 caddr_t kmdp; 518 vm_ooffset_t off; 519 vm_paddr_t metadata; 520 char *envp; 521 522 if (legacy_start_info->mod_start != 0) { 523 preload_metadata = (caddr_t)legacy_start_info->mod_start; 524 525 kmdp = preload_search_by_type("elf kernel"); 526 if (kmdp == NULL) 527 kmdp = preload_search_by_type("elf64 kernel"); 528 KASSERT(kmdp != NULL, ("unable to find kernel")); 529 530 /* 531 * Xen has relocated the metadata and the modules, 532 * so we need to recalculate it's position. This is 533 * done by saving the original modulep address and 534 * then calculating the offset with mod_start, 535 * which contains the relocated modulep address. 536 */ 537 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t); 538 off = legacy_start_info->mod_start - metadata; 539 540 preload_bootstrap_relocate(off); 541 542 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 543 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *); 544 if (envp != NULL) 545 envp += off; 546 xen_pvh_set_env(envp, NULL); 547 } else { 548 /* Parse the extra boot information given by Xen */ 549 boot_parse_cmdline_delim(legacy_start_info->cmd_line, ","); 550 kmdp = NULL; 551 } 552 553 boothowto |= boot_env_to_howto(); 554 555 #ifdef DDB 556 xen_pvh_parse_symtab(); 557 #endif 558 return (kmdp); 559 } 560 561 static caddr_t 562 xen_pvh_parse_preload_data(uint64_t modulep) 563 { 564 caddr_t kmdp; 565 vm_ooffset_t off; 566 vm_paddr_t metadata; 567 char *envp; 568 char acpi_rsdp[19]; 569 570 if (start_info->modlist_paddr != 0) { 571 struct hvm_modlist_entry *mod; 572 573 mod = (struct hvm_modlist_entry *) 574 (start_info->modlist_paddr + KERNBASE); 575 preload_metadata = (caddr_t)(mod[0].paddr + KERNBASE); 576 577 kmdp = preload_search_by_type("elf kernel"); 578 if (kmdp == NULL) 579 kmdp = preload_search_by_type("elf64 kernel"); 580 KASSERT(kmdp != NULL, ("unable to find kernel")); 581 582 /* 583 * Xen has relocated the metadata and the modules, 584 * so we need to recalculate it's position. This is 585 * done by saving the original modulep address and 586 * then calculating the offset with mod_start, 587 * which contains the relocated modulep address. 588 */ 589 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t); 590 off = mod[0].paddr + KERNBASE - metadata; 591 592 preload_bootstrap_relocate(off); 593 594 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 595 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *); 596 if (envp != NULL) 597 envp += off; 598 xen_pvh_set_env(envp, reject_option); 599 } else { 600 /* Parse the extra boot information given by Xen */ 601 if (start_info->cmdline_paddr != 0) 602 boot_parse_cmdline_delim( 603 (char *)(start_info->cmdline_paddr + KERNBASE), 604 ","); 605 kmdp = NULL; 606 } 607 608 boothowto |= boot_env_to_howto(); 609 610 snprintf(acpi_rsdp, sizeof(acpi_rsdp), "%#" PRIx64, 611 start_info->rsdp_paddr); 612 kern_setenv("acpi.rsdp", acpi_rsdp); 613 614 #ifdef DDB 615 xen_pvh_parse_symtab(); 616 #endif 617 return (kmdp); 618 } 619 620 static void 621 xen_pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx) 622 { 623 struct xen_memory_map memmap; 624 u_int32_t size; 625 int rc; 626 627 /* Fetch the E820 map from Xen */ 628 memmap.nr_entries = MAX_E820_ENTRIES; 629 set_xen_guest_handle(memmap.buffer, xen_smap); 630 rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); 631 if (rc) { 632 xc_printf("ERROR: unable to fetch Xen E820 memory map: %d\n", 633 rc); 634 HYPERVISOR_shutdown(SHUTDOWN_crash); 635 } 636 637 size = memmap.nr_entries * sizeof(xen_smap[0]); 638 639 bios_add_smap_entries(xen_smap, size, physmap, physmap_idx); 640 } 641