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 halt(); \ 153 } while (0) 154 155 uint64_t 156 hammer_time_xen(vm_paddr_t start_info_paddr) 157 { 158 struct hvm_modlist_entry *mod; 159 uint64_t physfree; 160 char *kenv; 161 162 start_info = (struct hvm_start_info *)(start_info_paddr + KERNBASE); 163 if (start_info->magic != XEN_HVM_START_MAGIC_VALUE) { 164 CRASH("Unknown magic value in start_info struct: %#x\n", 165 start_info->magic); 166 } 167 168 /* 169 * Select the higher address to use as physfree: either after 170 * start_info, after the kernel, after the memory map or after any of 171 * the modules. We assume enough memory to be available after the 172 * selected address for the needs of very early memory allocations. 173 */ 174 physfree = roundup2(start_info_paddr + sizeof(struct hvm_start_info), 175 PAGE_SIZE); 176 physfree = MAX(roundup2((vm_paddr_t)_end - KERNBASE, PAGE_SIZE), 177 physfree); 178 179 if (start_info->memmap_paddr != 0) 180 physfree = MAX(roundup2(start_info->memmap_paddr + 181 start_info->memmap_entries * 182 sizeof(struct hvm_memmap_table_entry), PAGE_SIZE), 183 physfree); 184 185 if (start_info->modlist_paddr != 0) { 186 unsigned int i; 187 188 if (start_info->nr_modules == 0) { 189 CRASH( 190 "ERROR: modlist_paddr != 0 but nr_modules == 0\n"); 191 } 192 mod = (struct hvm_modlist_entry *) 193 (start_info->modlist_paddr + KERNBASE); 194 for (i = 0; i < start_info->nr_modules; i++) 195 physfree = MAX(roundup2(mod[i].paddr + mod[i].size, 196 PAGE_SIZE), physfree); 197 } 198 199 /* 200 * Init a static kenv using a free page. The contents will be filled 201 * from the parse_preload_data hook. 202 */ 203 kenv = (void *)(physfree + KERNBASE); 204 physfree += PAGE_SIZE; 205 bzero_early(kenv, PAGE_SIZE); 206 init_static_kenv(kenv, PAGE_SIZE); 207 208 /* Set the hooks for early functions that diverge from bare metal */ 209 init_ops = xen_pvh_init_ops; 210 hvm_start_flags = start_info->flags; 211 212 /* Now we can jump into the native init function */ 213 return (hammer_time(0, physfree)); 214 } 215 216 /*-------------------------------- PV specific -------------------------------*/ 217 218 /* 219 * When booted as a PVH guest FreeBSD needs to avoid using the RSDP address 220 * hint provided by the loader because it points to the native set of ACPI 221 * tables instead of the ones crafted by Xen. The acpi.rsdp env variable is 222 * removed from kenv if present, and a new acpi.rsdp is added to kenv that 223 * points to the address of the Xen crafted RSDP. 224 */ 225 static bool reject_option(const char *option) 226 { 227 static const char *reject[] = { 228 "acpi.rsdp", 229 }; 230 unsigned int i; 231 232 for (i = 0; i < nitems(reject); i++) 233 if (strncmp(option, reject[i], strlen(reject[i])) == 0) 234 return (true); 235 236 return (false); 237 } 238 239 static void 240 xen_pvh_set_env(char *env, bool (*filter)(const char *)) 241 { 242 char *option; 243 244 if (env == NULL) 245 return; 246 247 option = env; 248 while (*option != 0) { 249 char *value; 250 251 if (filter != NULL && filter(option)) { 252 option += strlen(option) + 1; 253 continue; 254 } 255 256 value = option; 257 option = strsep(&value, "="); 258 if (kern_setenv(option, value) != 0 && isxen()) 259 xc_printf("unable to add kenv %s=%s\n", option, value); 260 option = value + strlen(value) + 1; 261 } 262 } 263 264 #ifdef DDB 265 /* 266 * The way Xen loads the symtab is different from the native boot loader, 267 * because it's tailored for NetBSD. So we have to adapt and use the same 268 * method as NetBSD. Portions of the code below have been picked from NetBSD: 269 * sys/kern/kern_ksyms.c CVS Revision 1.71. 270 */ 271 static void 272 xen_pvh_parse_symtab(void) 273 { 274 Elf_Ehdr *ehdr; 275 Elf_Shdr *shdr; 276 int i, j; 277 278 ehdr = (Elf_Ehdr *)(&end + 1); 279 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || 280 ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 281 ehdr->e_version > 1) { 282 if (isxen()) 283 xc_printf("Unable to load ELF symtab: invalid symbol table\n"); 284 return; 285 } 286 287 shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff); 288 /* Find the symbol table and the corresponding string table. */ 289 for (i = 1; i < ehdr->e_shnum; i++) { 290 if (shdr[i].sh_type != SHT_SYMTAB) 291 continue; 292 if (shdr[i].sh_offset == 0) 293 continue; 294 ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset); 295 ksymtab_size = shdr[i].sh_size; 296 j = shdr[i].sh_link; 297 if (shdr[j].sh_offset == 0) 298 continue; /* Can this happen? */ 299 kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset); 300 break; 301 } 302 303 if ((ksymtab == 0 || kstrtab == 0) && isxen()) 304 xc_printf( 305 "Unable to load ELF symtab: could not find symtab or strtab\n"); 306 } 307 #endif 308 309 static caddr_t 310 xen_pvh_parse_preload_data(uint64_t modulep) 311 { 312 caddr_t kmdp; 313 vm_ooffset_t off; 314 vm_paddr_t metadata; 315 char *envp; 316 char acpi_rsdp[19]; 317 318 TSENTER(); 319 if (start_info->modlist_paddr != 0) { 320 struct hvm_modlist_entry *mod; 321 const char *cmdline; 322 323 mod = (struct hvm_modlist_entry *) 324 (start_info->modlist_paddr + KERNBASE); 325 cmdline = mod[0].cmdline_paddr ? 326 (const char *)(mod[0].cmdline_paddr + KERNBASE) : NULL; 327 328 if (strcmp(cmdline, "header") == 0) { 329 struct xen_header *header; 330 331 header = (struct xen_header *)(mod[0].paddr + KERNBASE); 332 333 if ((header->flags & XENHEADER_HAS_MODULEP_OFFSET) != 334 XENHEADER_HAS_MODULEP_OFFSET) { 335 xc_printf("Unable to load module metadata\n"); 336 HYPERVISOR_shutdown(SHUTDOWN_crash); 337 } 338 339 preload_metadata = (caddr_t)(mod[0].paddr + 340 header->modulep_offset + KERNBASE); 341 342 kmdp = preload_search_by_type("elf kernel"); 343 if (kmdp == NULL) 344 kmdp = preload_search_by_type("elf64 kernel"); 345 if (kmdp == NULL) { 346 xc_printf("Unable to find kernel\n"); 347 HYPERVISOR_shutdown(SHUTDOWN_crash); 348 } 349 350 /* 351 * Xen has relocated the metadata and the modules, so 352 * we need to recalculate it's position. This is done 353 * by saving the original modulep address and then 354 * calculating the offset from the real modulep 355 * position. 356 */ 357 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, 358 vm_paddr_t); 359 off = mod[0].paddr + header->modulep_offset - metadata + 360 KERNBASE; 361 } else { 362 preload_metadata = (caddr_t)(mod[0].paddr + KERNBASE); 363 364 kmdp = preload_search_by_type("elf kernel"); 365 if (kmdp == NULL) 366 kmdp = preload_search_by_type("elf64 kernel"); 367 if (kmdp == NULL) { 368 xc_printf("Unable to find kernel\n"); 369 HYPERVISOR_shutdown(SHUTDOWN_crash); 370 } 371 372 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t); 373 off = mod[0].paddr + KERNBASE - metadata; 374 } 375 376 preload_bootstrap_relocate(off); 377 378 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 379 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *); 380 if (envp != NULL) 381 envp += off; 382 xen_pvh_set_env(envp, reject_option); 383 384 if (MD_FETCH(kmdp, MODINFOMD_EFI_MAP, void *) != NULL) 385 strlcpy(bootmethod, "UEFI", sizeof(bootmethod)); 386 else 387 strlcpy(bootmethod, "BIOS", sizeof(bootmethod)); 388 } else { 389 /* Parse the extra boot information given by Xen */ 390 if (start_info->cmdline_paddr != 0) 391 boot_parse_cmdline_delim( 392 (char *)(start_info->cmdline_paddr + KERNBASE), 393 ", \t\n"); 394 kmdp = NULL; 395 strlcpy(bootmethod, "PVH", sizeof(bootmethod)); 396 } 397 398 boothowto |= boot_env_to_howto(); 399 400 snprintf(acpi_rsdp, sizeof(acpi_rsdp), "%#" PRIx64, 401 start_info->rsdp_paddr); 402 kern_setenv("acpi.rsdp", acpi_rsdp); 403 404 #ifdef DDB 405 xen_pvh_parse_symtab(); 406 #endif 407 TSEXIT(); 408 return (kmdp); 409 } 410 411 static void 412 pvh_parse_memmap_start_info(caddr_t kmdp, vm_paddr_t *physmap, 413 int *physmap_idx) 414 { 415 const struct hvm_memmap_table_entry * entries; 416 size_t nentries; 417 size_t i; 418 419 /* Extract from HVM start_info. */ 420 entries = (struct hvm_memmap_table_entry *)(start_info->memmap_paddr + KERNBASE); 421 nentries = start_info->memmap_entries; 422 423 /* Convert into E820 format and handle one by one. */ 424 for (i = 0; i < nentries; i++) { 425 struct bios_smap entry; 426 427 entry.base = entries[i].addr; 428 entry.length = entries[i].size; 429 430 /* 431 * Luckily for us, the XEN_HVM_MEMMAP_TYPE_* values exactly 432 * match the SMAP_TYPE_* values so we don't need to translate 433 * anything here. 434 */ 435 entry.type = entries[i].type; 436 437 bios_add_smap_entries(&entry, 1, physmap, physmap_idx); 438 } 439 } 440 441 static void 442 xen_pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx) 443 { 444 struct xen_memory_map memmap; 445 u_int32_t size; 446 int rc; 447 448 /* We should only reach here if we're running under Xen. */ 449 KASSERT(isxen(), ("xen_pvh_parse_memmap reached when !Xen")); 450 451 /* Fetch the E820 map from Xen */ 452 memmap.nr_entries = MAX_E820_ENTRIES; 453 set_xen_guest_handle(memmap.buffer, xen_smap); 454 rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); 455 if (rc) { 456 xc_printf("ERROR: unable to fetch Xen E820 memory map: %d\n", 457 rc); 458 HYPERVISOR_shutdown(SHUTDOWN_crash); 459 } 460 461 size = memmap.nr_entries * sizeof(xen_smap[0]); 462 463 bios_add_smap_entries(xen_smap, size, physmap, physmap_idx); 464 } 465 466 static void 467 pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx) 468 { 469 470 /* 471 * If version >= 1 and memmap_paddr != 0, use the memory map provided 472 * in the start_info structure; if not, we're running under legacy 473 * Xen and need to use the Xen hypercall. 474 */ 475 if ((start_info->version >= 1) && (start_info->memmap_paddr != 0)) 476 pvh_parse_memmap_start_info(kmdp, physmap, physmap_idx); 477 else 478 xen_pvh_parse_memmap(kmdp, physmap, physmap_idx); 479 } 480