1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 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 #include "opt_ddb.h" 34 #include "opt_kstack_pages.h" 35 36 #include <sys/param.h> 37 #include <sys/bus.h> 38 #include <sys/kernel.h> 39 #include <sys/reboot.h> 40 #include <sys/systm.h> 41 #include <sys/malloc.h> 42 #include <sys/linker.h> 43 #include <sys/lock.h> 44 #include <sys/rwlock.h> 45 #include <sys/boot.h> 46 #include <sys/ctype.h> 47 #include <sys/mutex.h> 48 #include <sys/smp.h> 49 #include <sys/efi.h> 50 #include <sys/tslog.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/md_var.h> 69 #include <machine/metadata.h> 70 #include <machine/cpu.h> 71 72 #include <xen/xen-os.h> 73 #include <xen/hvm.h> 74 #include <xen/hypervisor.h> 75 #include <xen/xenstore/xenstorevar.h> 76 #include <xen/xen_pv.h> 77 78 #include <contrib/xen/arch-x86/cpuid.h> 79 #include <contrib/xen/arch-x86/hvm/start_info.h> 80 #include <contrib/xen/vcpu.h> 81 82 #include <dev/xen/timer/timer.h> 83 84 #ifdef DDB 85 #include <ddb/ddb.h> 86 #endif 87 88 /* Native initial function */ 89 extern u_int64_t hammer_time(u_int64_t, u_int64_t); 90 /* Xen initial function */ 91 uint64_t hammer_time_xen(vm_paddr_t); 92 93 #define MAX_E820_ENTRIES 128 94 95 /*--------------------------- Forward Declarations ---------------------------*/ 96 static caddr_t xen_pvh_parse_preload_data(uint64_t); 97 static void pvh_parse_memmap(caddr_t, vm_paddr_t *, int *); 98 99 /*---------------------------- Extern Declarations ---------------------------*/ 100 /* 101 * Placed by the linker at the end of the bss section, which is the last 102 * section loaded by Xen before loading the symtab and strtab. 103 */ 104 extern uint32_t end; 105 106 /*-------------------------------- Global Data -------------------------------*/ 107 struct init_ops xen_pvh_init_ops = { 108 .parse_preload_data = xen_pvh_parse_preload_data, 109 .early_clock_source_init = xen_clock_init, 110 .early_delay = xen_delay, 111 .parse_memmap = pvh_parse_memmap, 112 }; 113 114 static struct bios_smap xen_smap[MAX_E820_ENTRIES]; 115 116 static struct hvm_start_info *start_info; 117 118 /*-------------------------------- Xen PV init -------------------------------*/ 119 120 static int 121 isxen(void) 122 { 123 static int xen = -1; 124 uint32_t base; 125 u_int regs[4]; 126 127 if (xen != -1) 128 return (xen); 129 130 /* 131 * The full code for identifying which hypervisor we're running under 132 * is in sys/x86/x86/identcpu.c and runs later in the boot process; 133 * this is sufficient to distinguish Xen PVH booting from non-Xen PVH 134 * and skip some very early Xen-specific code in the non-Xen case. 135 */ 136 xen = 0; 137 for (base = 0x40000000; base < 0x40010000; base += 0x100) { 138 do_cpuid(base, regs); 139 if (regs[1] == XEN_CPUID_SIGNATURE_EBX && 140 regs[2] == XEN_CPUID_SIGNATURE_ECX && 141 regs[3] == XEN_CPUID_SIGNATURE_EDX) { 142 xen = 1; 143 break; 144 } 145 } 146 return (xen); 147 } 148 149 #define CRASH(...) do { \ 150 if (isxen()) { \ 151 xc_printf(__VA_ARGS__); \ 152 HYPERVISOR_shutdown(SHUTDOWN_crash); \ 153 } else { \ 154 halt(); \ 155 } \ 156 } while (0) 157 158 uint64_t 159 hammer_time_xen(vm_paddr_t start_info_paddr) 160 { 161 struct hvm_modlist_entry *mod; 162 struct xen_add_to_physmap xatp; 163 uint64_t physfree; 164 char *kenv; 165 int rc; 166 167 if (isxen()) { 168 vm_guest = VM_GUEST_XEN; 169 rc = xen_hvm_init_hypercall_stubs(XEN_HVM_INIT_EARLY); 170 if (rc) { 171 xc_printf("ERROR: failed to initialize hypercall page: %d\n", 172 rc); 173 HYPERVISOR_shutdown(SHUTDOWN_crash); 174 } 175 } 176 177 start_info = (struct hvm_start_info *)(start_info_paddr + KERNBASE); 178 if (start_info->magic != XEN_HVM_START_MAGIC_VALUE) { 179 CRASH("Unknown magic value in start_info struct: %#x\n", 180 start_info->magic); 181 } 182 183 /* 184 * Select the higher address to use as physfree: either after 185 * start_info, after the kernel, after the memory map or after any of 186 * the modules. We assume enough memory to be available after the 187 * selected address for the needs of very early memory allocations. 188 */ 189 physfree = roundup2(start_info_paddr + sizeof(struct hvm_start_info), 190 PAGE_SIZE); 191 physfree = MAX(roundup2((vm_paddr_t)_end - KERNBASE, PAGE_SIZE), 192 physfree); 193 194 if (start_info->memmap_paddr != 0) 195 physfree = MAX(roundup2(start_info->memmap_paddr + 196 start_info->memmap_entries * 197 sizeof(struct hvm_memmap_table_entry), PAGE_SIZE), 198 physfree); 199 200 if (start_info->modlist_paddr != 0) { 201 unsigned int i; 202 203 if (start_info->nr_modules == 0) { 204 CRASH( 205 "ERROR: modlist_paddr != 0 but nr_modules == 0\n"); 206 } 207 mod = (struct hvm_modlist_entry *) 208 (start_info->modlist_paddr + KERNBASE); 209 for (i = 0; i < start_info->nr_modules; i++) 210 physfree = MAX(roundup2(mod[i].paddr + mod[i].size, 211 PAGE_SIZE), physfree); 212 } 213 214 if (isxen()) { 215 xatp.domid = DOMID_SELF; 216 xatp.idx = 0; 217 xatp.space = XENMAPSPACE_shared_info; 218 xatp.gpfn = atop(physfree); 219 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) { 220 xc_printf("ERROR: failed to setup shared_info page\n"); 221 HYPERVISOR_shutdown(SHUTDOWN_crash); 222 } 223 HYPERVISOR_shared_info = (shared_info_t *)(physfree + KERNBASE); 224 physfree += PAGE_SIZE; 225 } 226 227 /* 228 * Init a static kenv using a free page. The contents will be filled 229 * from the parse_preload_data hook. 230 */ 231 kenv = (void *)(physfree + KERNBASE); 232 physfree += PAGE_SIZE; 233 bzero_early(kenv, PAGE_SIZE); 234 init_static_kenv(kenv, PAGE_SIZE); 235 236 /* Set the hooks for early functions that diverge from bare metal */ 237 init_ops = xen_pvh_init_ops; 238 hvm_start_flags = start_info->flags; 239 240 /* Now we can jump into the native init function */ 241 return (hammer_time(0, physfree)); 242 } 243 244 /*-------------------------------- PV specific -------------------------------*/ 245 246 /* 247 * When booted as a PVH guest FreeBSD needs to avoid using the RSDP address 248 * hint provided by the loader because it points to the native set of ACPI 249 * tables instead of the ones crafted by Xen. The acpi.rsdp env variable is 250 * removed from kenv if present, and a new acpi.rsdp is added to kenv that 251 * points to the address of the Xen crafted RSDP. 252 */ 253 static bool reject_option(const char *option) 254 { 255 static const char *reject[] = { 256 "acpi.rsdp", 257 }; 258 unsigned int i; 259 260 for (i = 0; i < nitems(reject); i++) 261 if (strncmp(option, reject[i], strlen(reject[i])) == 0) 262 return (true); 263 264 return (false); 265 } 266 267 static void 268 xen_pvh_set_env(char *env, bool (*filter)(const char *)) 269 { 270 char *option; 271 272 if (env == NULL) 273 return; 274 275 option = env; 276 while (*option != 0) { 277 char *value; 278 279 if (filter != NULL && filter(option)) { 280 option += strlen(option) + 1; 281 continue; 282 } 283 284 value = option; 285 option = strsep(&value, "="); 286 if (kern_setenv(option, value) != 0 && isxen()) 287 xc_printf("unable to add kenv %s=%s\n", option, value); 288 option = value + strlen(value) + 1; 289 } 290 } 291 292 #ifdef DDB 293 /* 294 * The way Xen loads the symtab is different from the native boot loader, 295 * because it's tailored for NetBSD. So we have to adapt and use the same 296 * method as NetBSD. Portions of the code below have been picked from NetBSD: 297 * sys/kern/kern_ksyms.c CVS Revision 1.71. 298 */ 299 static void 300 xen_pvh_parse_symtab(void) 301 { 302 Elf_Ehdr *ehdr; 303 Elf_Shdr *shdr; 304 int i, j; 305 306 ehdr = (Elf_Ehdr *)(&end + 1); 307 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || 308 ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 309 ehdr->e_version > 1) { 310 if (isxen()) 311 xc_printf("Unable to load ELF symtab: invalid symbol table\n"); 312 return; 313 } 314 315 shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff); 316 /* Find the symbol table and the corresponding string table. */ 317 for (i = 1; i < ehdr->e_shnum; i++) { 318 if (shdr[i].sh_type != SHT_SYMTAB) 319 continue; 320 if (shdr[i].sh_offset == 0) 321 continue; 322 ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset); 323 ksymtab_size = shdr[i].sh_size; 324 j = shdr[i].sh_link; 325 if (shdr[j].sh_offset == 0) 326 continue; /* Can this happen? */ 327 kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset); 328 break; 329 } 330 331 if ((ksymtab == 0 || kstrtab == 0) && isxen()) 332 xc_printf( 333 "Unable to load ELF symtab: could not find symtab or strtab\n"); 334 } 335 #endif 336 337 static void 338 fixup_console(caddr_t kmdp) 339 { 340 struct xen_platform_op op = { 341 .cmd = XENPF_get_dom0_console, 342 }; 343 xenpf_dom0_console_t *console = &op.u.dom0_console; 344 union { 345 struct efi_fb efi; 346 struct vbe_fb vbe; 347 } *fb = NULL; 348 int size; 349 350 size = HYPERVISOR_platform_op(&op); 351 if (size < 0) { 352 xc_printf("Failed to get dom0 video console info: %d\n", size); 353 return; 354 } 355 356 switch (console->video_type) { 357 case XEN_VGATYPE_VESA_LFB: 358 fb = (__typeof__ (fb))preload_search_info(kmdp, 359 MODINFO_METADATA | MODINFOMD_VBE_FB); 360 361 if (fb == NULL) { 362 xc_printf("No VBE FB in kernel metadata\n"); 363 return; 364 } 365 366 _Static_assert(offsetof(struct vbe_fb, fb_bpp) == 367 offsetof(struct efi_fb, fb_mask_reserved) + 368 sizeof(fb->efi.fb_mask_reserved), 369 "Bad structure overlay\n"); 370 fb->vbe.fb_bpp = console->u.vesa_lfb.bits_per_pixel; 371 /* FALLTHROUGH */ 372 case XEN_VGATYPE_EFI_LFB: 373 if (fb == NULL) { 374 fb = (__typeof__ (fb))preload_search_info(kmdp, 375 MODINFO_METADATA | MODINFOMD_EFI_FB); 376 if (fb == NULL) { 377 xc_printf("No EFI FB in kernel metadata\n"); 378 return; 379 } 380 } 381 382 fb->efi.fb_addr = console->u.vesa_lfb.lfb_base; 383 if (size > 384 offsetof(xenpf_dom0_console_t, u.vesa_lfb.ext_lfb_base)) 385 fb->efi.fb_addr |= 386 (uint64_t)console->u.vesa_lfb.ext_lfb_base << 32; 387 fb->efi.fb_size = console->u.vesa_lfb.lfb_size << 16; 388 fb->efi.fb_height = console->u.vesa_lfb.height; 389 fb->efi.fb_width = console->u.vesa_lfb.width; 390 fb->efi.fb_stride = (console->u.vesa_lfb.bytes_per_line << 3) / 391 console->u.vesa_lfb.bits_per_pixel; 392 #define FBMASK(c) \ 393 ((~0u << console->u.vesa_lfb.c ## _pos) & \ 394 (~0u >> (32 - console->u.vesa_lfb.c ## _pos - \ 395 console->u.vesa_lfb.c ## _size))) 396 fb->efi.fb_mask_red = FBMASK(red); 397 fb->efi.fb_mask_green = FBMASK(green); 398 fb->efi.fb_mask_blue = FBMASK(blue); 399 fb->efi.fb_mask_reserved = FBMASK(rsvd); 400 #undef FBMASK 401 break; 402 403 default: 404 xc_printf("Video console type unsupported\n"); 405 return; 406 } 407 } 408 409 static caddr_t 410 xen_pvh_parse_preload_data(uint64_t modulep) 411 { 412 caddr_t kmdp; 413 vm_ooffset_t off; 414 vm_paddr_t metadata; 415 char *envp; 416 char acpi_rsdp[19]; 417 418 TSENTER(); 419 if (start_info->modlist_paddr != 0) { 420 struct hvm_modlist_entry *mod; 421 const char *cmdline; 422 423 mod = (struct hvm_modlist_entry *) 424 (start_info->modlist_paddr + KERNBASE); 425 cmdline = mod[0].cmdline_paddr ? 426 (const char *)(mod[0].cmdline_paddr + KERNBASE) : NULL; 427 428 if (strcmp(cmdline, "header") == 0) { 429 struct xen_header *header; 430 431 header = (struct xen_header *)(mod[0].paddr + KERNBASE); 432 433 if ((header->flags & XENHEADER_HAS_MODULEP_OFFSET) != 434 XENHEADER_HAS_MODULEP_OFFSET) { 435 xc_printf("Unable to load module metadata\n"); 436 HYPERVISOR_shutdown(SHUTDOWN_crash); 437 } 438 439 preload_metadata = (caddr_t)(mod[0].paddr + 440 header->modulep_offset + KERNBASE); 441 442 kmdp = preload_search_by_type("elf kernel"); 443 if (kmdp == NULL) 444 kmdp = preload_search_by_type("elf64 kernel"); 445 if (kmdp == NULL) { 446 xc_printf("Unable to find kernel\n"); 447 HYPERVISOR_shutdown(SHUTDOWN_crash); 448 } 449 450 /* 451 * Xen has relocated the metadata and the modules, so 452 * we need to recalculate it's position. This is done 453 * by saving the original modulep address and then 454 * calculating the offset from the real modulep 455 * position. 456 */ 457 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, 458 vm_paddr_t); 459 off = mod[0].paddr + header->modulep_offset - metadata + 460 KERNBASE; 461 } else { 462 preload_metadata = (caddr_t)(mod[0].paddr + KERNBASE); 463 464 kmdp = preload_search_by_type("elf kernel"); 465 if (kmdp == NULL) 466 kmdp = preload_search_by_type("elf64 kernel"); 467 if (kmdp == NULL) { 468 xc_printf("Unable to find kernel\n"); 469 HYPERVISOR_shutdown(SHUTDOWN_crash); 470 } 471 472 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t); 473 off = mod[0].paddr + KERNBASE - metadata; 474 } 475 476 preload_bootstrap_relocate(off); 477 478 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 479 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *); 480 if (envp != NULL) 481 envp += off; 482 xen_pvh_set_env(envp, reject_option); 483 484 if (MD_FETCH(kmdp, MODINFOMD_EFI_MAP, void *) != NULL) 485 strlcpy(bootmethod, "UEFI", sizeof(bootmethod)); 486 else 487 strlcpy(bootmethod, "BIOS", sizeof(bootmethod)); 488 489 fixup_console(kmdp); 490 } else { 491 /* Parse the extra boot information given by Xen */ 492 if (start_info->cmdline_paddr != 0) 493 boot_parse_cmdline_delim( 494 (char *)(start_info->cmdline_paddr + KERNBASE), 495 ", \t\n"); 496 kmdp = NULL; 497 strlcpy(bootmethod, "PVH", sizeof(bootmethod)); 498 } 499 500 boothowto |= boot_env_to_howto(); 501 502 snprintf(acpi_rsdp, sizeof(acpi_rsdp), "%#" PRIx64, 503 start_info->rsdp_paddr); 504 kern_setenv("acpi.rsdp", acpi_rsdp); 505 506 #ifdef DDB 507 xen_pvh_parse_symtab(); 508 #endif 509 TSEXIT(); 510 return (kmdp); 511 } 512 513 static void 514 pvh_parse_memmap_start_info(caddr_t kmdp, vm_paddr_t *physmap, 515 int *physmap_idx) 516 { 517 const struct hvm_memmap_table_entry * entries; 518 size_t nentries; 519 size_t i; 520 521 /* Extract from HVM start_info. */ 522 entries = (struct hvm_memmap_table_entry *)(start_info->memmap_paddr + KERNBASE); 523 nentries = start_info->memmap_entries; 524 525 /* Convert into E820 format and handle one by one. */ 526 for (i = 0; i < nentries; i++) { 527 struct bios_smap entry; 528 529 entry.base = entries[i].addr; 530 entry.length = entries[i].size; 531 532 /* 533 * Luckily for us, the XEN_HVM_MEMMAP_TYPE_* values exactly 534 * match the SMAP_TYPE_* values so we don't need to translate 535 * anything here. 536 */ 537 entry.type = entries[i].type; 538 539 bios_add_smap_entries(&entry, 1, physmap, physmap_idx); 540 } 541 } 542 543 static void 544 xen_pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx) 545 { 546 struct xen_memory_map memmap; 547 u_int32_t size; 548 int rc; 549 550 /* We should only reach here if we're running under Xen. */ 551 KASSERT(isxen(), ("xen_pvh_parse_memmap reached when !Xen")); 552 553 /* Fetch the E820 map from Xen */ 554 memmap.nr_entries = MAX_E820_ENTRIES; 555 set_xen_guest_handle(memmap.buffer, xen_smap); 556 rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); 557 if (rc) { 558 xc_printf("ERROR: unable to fetch Xen E820 memory map: %d\n", 559 rc); 560 HYPERVISOR_shutdown(SHUTDOWN_crash); 561 } 562 563 size = memmap.nr_entries * sizeof(xen_smap[0]); 564 565 bios_add_smap_entries(xen_smap, size, physmap, physmap_idx); 566 } 567 568 static void 569 pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx) 570 { 571 572 /* 573 * If version >= 1 and memmap_paddr != 0, use the memory map provided 574 * in the start_info structure; if not, we're running under legacy 575 * Xen and need to use the Xen hypercall. 576 */ 577 if ((start_info->version >= 1) && (start_info->memmap_paddr != 0)) 578 pvh_parse_memmap_start_info(kmdp, physmap, physmap_idx); 579 else 580 xen_pvh_parse_memmap(kmdp, physmap, physmap_idx); 581 } 582