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 #include <sys/efi.h> 52 53 #include <vm/vm.h> 54 #include <vm/vm_extern.h> 55 #include <vm/vm_kern.h> 56 #include <vm/vm_page.h> 57 #include <vm/vm_map.h> 58 #include <vm/vm_object.h> 59 #include <vm/vm_pager.h> 60 #include <vm/vm_param.h> 61 62 #include <machine/_inttypes.h> 63 #include <machine/intr_machdep.h> 64 #include <x86/apicvar.h> 65 #include <x86/init.h> 66 #include <machine/pc/bios.h> 67 #include <machine/smp.h> 68 #include <machine/intr_machdep.h> 69 #include <machine/md_var.h> 70 #include <machine/metadata.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 <xen/interface/arch-x86/hvm/start_info.h> 79 #include <xen/interface/vcpu.h> 80 81 #include <dev/xen/timer/timer.h> 82 83 #ifdef DDB 84 #include <ddb/ddb.h> 85 #endif 86 87 /* Native initial function */ 88 extern u_int64_t hammer_time(u_int64_t, u_int64_t); 89 /* Xen initial function */ 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_pvh_parse_preload_data(uint64_t); 96 static void xen_pvh_parse_memmap(caddr_t, vm_paddr_t *, int *); 97 98 /*---------------------------- Extern Declarations ---------------------------*/ 99 /* 100 * Placed by the linker at the end of the bss section, which is the last 101 * section loaded by Xen before loading the symtab and strtab. 102 */ 103 extern uint32_t end; 104 105 /*-------------------------------- Global Data -------------------------------*/ 106 struct init_ops xen_pvh_init_ops = { 107 .parse_preload_data = xen_pvh_parse_preload_data, 108 .early_clock_source_init = xen_clock_init, 109 .early_delay = xen_delay, 110 .parse_memmap = xen_pvh_parse_memmap, 111 }; 112 113 static struct bios_smap xen_smap[MAX_E820_ENTRIES]; 114 115 static struct hvm_start_info *start_info; 116 117 /*-------------------------------- Xen PV init -------------------------------*/ 118 119 uint64_t 120 hammer_time_xen(vm_paddr_t start_info_paddr) 121 { 122 struct hvm_modlist_entry *mod; 123 struct xen_add_to_physmap xatp; 124 uint64_t physfree; 125 char *kenv; 126 int rc; 127 128 xen_domain_type = XEN_HVM_DOMAIN; 129 vm_guest = VM_GUEST_XEN; 130 131 rc = xen_hvm_init_hypercall_stubs(XEN_HVM_INIT_EARLY); 132 if (rc) { 133 xc_printf("ERROR: failed to initialize hypercall page: %d\n", 134 rc); 135 HYPERVISOR_shutdown(SHUTDOWN_crash); 136 } 137 138 start_info = (struct hvm_start_info *)(start_info_paddr + KERNBASE); 139 if (start_info->magic != XEN_HVM_START_MAGIC_VALUE) { 140 xc_printf("Unknown magic value in start_info struct: %#x\n", 141 start_info->magic); 142 HYPERVISOR_shutdown(SHUTDOWN_crash); 143 } 144 145 /* 146 * The hvm_start_into structure is always appended after loading 147 * the kernel and modules. 148 */ 149 physfree = roundup2(start_info_paddr + PAGE_SIZE, PAGE_SIZE); 150 151 xatp.domid = DOMID_SELF; 152 xatp.idx = 0; 153 xatp.space = XENMAPSPACE_shared_info; 154 xatp.gpfn = atop(physfree); 155 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) { 156 xc_printf("ERROR: failed to setup shared_info page\n"); 157 HYPERVISOR_shutdown(SHUTDOWN_crash); 158 } 159 HYPERVISOR_shared_info = (shared_info_t *)(physfree + KERNBASE); 160 physfree += PAGE_SIZE; 161 162 /* 163 * Init a static kenv using a free page. The contents will be filled 164 * from the parse_preload_data hook. 165 */ 166 kenv = (void *)(physfree + KERNBASE); 167 physfree += PAGE_SIZE; 168 bzero_early(kenv, PAGE_SIZE); 169 init_static_kenv(kenv, PAGE_SIZE); 170 171 if (start_info->modlist_paddr != 0) { 172 if (start_info->modlist_paddr >= physfree) { 173 xc_printf( 174 "ERROR: unexpected module list memory address\n"); 175 HYPERVISOR_shutdown(SHUTDOWN_crash); 176 } 177 if (start_info->nr_modules == 0) { 178 xc_printf( 179 "ERROR: modlist_paddr != 0 but nr_modules == 0\n"); 180 HYPERVISOR_shutdown(SHUTDOWN_crash); 181 } 182 mod = (struct hvm_modlist_entry *) 183 (start_info->modlist_paddr + KERNBASE); 184 if (mod[0].paddr >= physfree) { 185 xc_printf("ERROR: unexpected module memory address\n"); 186 HYPERVISOR_shutdown(SHUTDOWN_crash); 187 } 188 } 189 190 /* Set the hooks for early functions that diverge from bare metal */ 191 init_ops = xen_pvh_init_ops; 192 hvm_start_flags = start_info->flags; 193 194 /* Now we can jump into the native init function */ 195 return (hammer_time(0, physfree)); 196 } 197 198 /*-------------------------------- PV specific -------------------------------*/ 199 200 /* 201 * When booted as a PVH guest FreeBSD needs to avoid using the RSDP address 202 * hint provided by the loader because it points to the native set of ACPI 203 * tables instead of the ones crafted by Xen. The acpi.rsdp env variable is 204 * removed from kenv if present, and a new acpi.rsdp is added to kenv that 205 * points to the address of the Xen crafted RSDP. 206 */ 207 static bool reject_option(const char *option) 208 { 209 static const char *reject[] = { 210 "acpi.rsdp", 211 }; 212 unsigned int i; 213 214 for (i = 0; i < nitems(reject); i++) 215 if (strncmp(option, reject[i], strlen(reject[i])) == 0) 216 return (true); 217 218 return (false); 219 } 220 221 static void 222 xen_pvh_set_env(char *env, bool (*filter)(const char *)) 223 { 224 char *option; 225 226 if (env == NULL) 227 return; 228 229 option = env; 230 while (*option != 0) { 231 char *value; 232 233 if (filter != NULL && filter(option)) { 234 option += strlen(option) + 1; 235 continue; 236 } 237 238 value = option; 239 option = strsep(&value, "="); 240 if (kern_setenv(option, value) != 0) 241 xc_printf("unable to add kenv %s=%s\n", option, value); 242 option = value + strlen(value) + 1; 243 } 244 } 245 246 #ifdef DDB 247 /* 248 * The way Xen loads the symtab is different from the native boot loader, 249 * because it's tailored for NetBSD. So we have to adapt and use the same 250 * method as NetBSD. Portions of the code below have been picked from NetBSD: 251 * sys/kern/kern_ksyms.c CVS Revision 1.71. 252 */ 253 static void 254 xen_pvh_parse_symtab(void) 255 { 256 Elf_Ehdr *ehdr; 257 Elf_Shdr *shdr; 258 int i, j; 259 260 ehdr = (Elf_Ehdr *)(&end + 1); 261 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || 262 ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 263 ehdr->e_version > 1) { 264 xc_printf("Unable to load ELF symtab: invalid symbol table\n"); 265 return; 266 } 267 268 shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff); 269 /* Find the symbol table and the corresponding string table. */ 270 for (i = 1; i < ehdr->e_shnum; i++) { 271 if (shdr[i].sh_type != SHT_SYMTAB) 272 continue; 273 if (shdr[i].sh_offset == 0) 274 continue; 275 ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset); 276 ksymtab_size = shdr[i].sh_size; 277 j = shdr[i].sh_link; 278 if (shdr[j].sh_offset == 0) 279 continue; /* Can this happen? */ 280 kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset); 281 break; 282 } 283 284 if (ksymtab == 0 || kstrtab == 0) 285 xc_printf( 286 "Unable to load ELF symtab: could not find symtab or strtab\n"); 287 } 288 #endif 289 290 static caddr_t 291 xen_pvh_parse_preload_data(uint64_t modulep) 292 { 293 caddr_t kmdp; 294 vm_ooffset_t off; 295 vm_paddr_t metadata; 296 char *envp; 297 char acpi_rsdp[19]; 298 299 if (start_info->modlist_paddr != 0) { 300 struct hvm_modlist_entry *mod; 301 const char *cmdline; 302 303 mod = (struct hvm_modlist_entry *) 304 (start_info->modlist_paddr + KERNBASE); 305 cmdline = mod[0].cmdline_paddr ? 306 (const char *)(mod[0].cmdline_paddr + KERNBASE) : NULL; 307 308 if (strcmp(cmdline, "header") == 0) { 309 struct xen_header *header; 310 311 header = (struct xen_header *)(mod[0].paddr + KERNBASE); 312 313 if ((header->flags & XENHEADER_HAS_MODULEP_OFFSET) != 314 XENHEADER_HAS_MODULEP_OFFSET) { 315 xc_printf("Unable to load module metadata\n"); 316 HYPERVISOR_shutdown(SHUTDOWN_crash); 317 } 318 319 preload_metadata = (caddr_t)(mod[0].paddr + 320 header->modulep_offset + KERNBASE); 321 322 kmdp = preload_search_by_type("elf kernel"); 323 if (kmdp == NULL) 324 kmdp = preload_search_by_type("elf64 kernel"); 325 if (kmdp == NULL) { 326 xc_printf("Unable to find kernel\n"); 327 HYPERVISOR_shutdown(SHUTDOWN_crash); 328 } 329 330 /* 331 * Xen has relocated the metadata and the modules, so 332 * we need to recalculate it's position. This is done 333 * by saving the original modulep address and then 334 * calculating the offset from the real modulep 335 * position. 336 */ 337 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, 338 vm_paddr_t); 339 off = mod[0].paddr + header->modulep_offset - metadata + 340 KERNBASE; 341 } else { 342 preload_metadata = (caddr_t)(mod[0].paddr + KERNBASE); 343 344 kmdp = preload_search_by_type("elf kernel"); 345 if (kmdp == NULL) 346 kmdp = preload_search_by_type("elf64 kernel"); 347 if (kmdp == NULL) { 348 xc_printf("Unable to find kernel\n"); 349 HYPERVISOR_shutdown(SHUTDOWN_crash); 350 } 351 352 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t); 353 off = mod[0].paddr + KERNBASE - metadata; 354 } 355 356 preload_bootstrap_relocate(off); 357 358 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 359 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *); 360 if (envp != NULL) 361 envp += off; 362 xen_pvh_set_env(envp, reject_option); 363 364 if (MD_FETCH(kmdp, MODINFOMD_EFI_MAP, void *) != NULL) 365 strlcpy(bootmethod, "UEFI", sizeof(bootmethod)); 366 else 367 strlcpy(bootmethod, "BIOS", sizeof(bootmethod)); 368 } else { 369 /* Parse the extra boot information given by Xen */ 370 if (start_info->cmdline_paddr != 0) 371 boot_parse_cmdline_delim( 372 (char *)(start_info->cmdline_paddr + KERNBASE), 373 ","); 374 kmdp = NULL; 375 strlcpy(bootmethod, "XEN", sizeof(bootmethod)); 376 } 377 378 boothowto |= boot_env_to_howto(); 379 380 snprintf(acpi_rsdp, sizeof(acpi_rsdp), "%#" PRIx64, 381 start_info->rsdp_paddr); 382 kern_setenv("acpi.rsdp", acpi_rsdp); 383 384 #ifdef DDB 385 xen_pvh_parse_symtab(); 386 #endif 387 return (kmdp); 388 } 389 390 static void 391 xen_pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx) 392 { 393 struct xen_memory_map memmap; 394 u_int32_t size; 395 int rc; 396 397 /* Fetch the E820 map from Xen */ 398 memmap.nr_entries = MAX_E820_ENTRIES; 399 set_xen_guest_handle(memmap.buffer, xen_smap); 400 rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); 401 if (rc) { 402 xc_printf("ERROR: unable to fetch Xen E820 memory map: %d\n", 403 rc); 404 HYPERVISOR_shutdown(SHUTDOWN_crash); 405 } 406 407 size = memmap.nr_entries * sizeof(xen_smap[0]); 408 409 bios_add_smap_entries(xen_smap, size, physmap, physmap_idx); 410 } 411