1ebf5747bSPedro F. Giffuni /*- 2*b61a5730SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3ebf5747bSPedro F. Giffuni * 41a9cdd37SRoger Pau Monné * Copyright (c) 2004 Christian Limpach. 51a9cdd37SRoger Pau Monné * Copyright (c) 2004-2006,2008 Kip Macy 69b4e54d3SRoger Pau Monné * Copyright (c) 2008 The NetBSD Foundation, Inc. 71a9cdd37SRoger Pau Monné * Copyright (c) 2013 Roger Pau Monné <roger.pau@citrix.com> 81a9cdd37SRoger Pau Monné * All rights reserved. 91a9cdd37SRoger Pau Monné * 101a9cdd37SRoger Pau Monné * Redistribution and use in source and binary forms, with or without 111a9cdd37SRoger Pau Monné * modification, are permitted provided that the following conditions 121a9cdd37SRoger Pau Monné * are met: 131a9cdd37SRoger Pau Monné * 1. Redistributions of source code must retain the above copyright 141a9cdd37SRoger Pau Monné * notice, this list of conditions and the following disclaimer. 151a9cdd37SRoger Pau Monné * 2. Redistributions in binary form must reproduce the above copyright 161a9cdd37SRoger Pau Monné * notice, this list of conditions and the following disclaimer in the 171a9cdd37SRoger Pau Monné * documentation and/or other materials provided with the distribution. 181a9cdd37SRoger Pau Monné * 191a9cdd37SRoger Pau Monné * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND 201a9cdd37SRoger Pau Monné * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 211a9cdd37SRoger Pau Monné * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 221a9cdd37SRoger Pau Monné * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 231a9cdd37SRoger Pau Monné * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 241a9cdd37SRoger Pau Monné * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 251a9cdd37SRoger Pau Monné * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 261a9cdd37SRoger Pau Monné * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 271a9cdd37SRoger Pau Monné * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 281a9cdd37SRoger Pau Monné * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 291a9cdd37SRoger Pau Monné * SUCH DAMAGE. 301a9cdd37SRoger Pau Monné */ 311a9cdd37SRoger Pau Monné 321a9cdd37SRoger Pau Monné #include <sys/cdefs.h> 331a9cdd37SRoger Pau Monné __FBSDID("$FreeBSD$"); 341a9cdd37SRoger Pau Monné 35c98a2727SRoger Pau Monné #include "opt_ddb.h" 36721555e7SZbigniew Bodek #include "opt_kstack_pages.h" 37c98a2727SRoger Pau Monné 381a9cdd37SRoger Pau Monné #include <sys/param.h> 391a9cdd37SRoger Pau Monné #include <sys/bus.h> 401a9cdd37SRoger Pau Monné #include <sys/kernel.h> 411a9cdd37SRoger Pau Monné #include <sys/reboot.h> 421a9cdd37SRoger Pau Monné #include <sys/systm.h> 43079f7ef8SRoger Pau Monné #include <sys/malloc.h> 44c98a2727SRoger Pau Monné #include <sys/linker.h> 451a9cdd37SRoger Pau Monné #include <sys/lock.h> 461a9cdd37SRoger Pau Monné #include <sys/rwlock.h> 47aa389b4fSRoger Pau Monné #include <sys/boot.h> 4897baeefdSRoger Pau Monné #include <sys/ctype.h> 49079f7ef8SRoger Pau Monné #include <sys/mutex.h> 50079f7ef8SRoger Pau Monné #include <sys/smp.h> 51a2495c36SRoger Pau Monné #include <sys/efi.h> 521a9cdd37SRoger Pau Monné 531a9cdd37SRoger Pau Monné #include <vm/vm.h> 541a9cdd37SRoger Pau Monné #include <vm/vm_extern.h> 551a9cdd37SRoger Pau Monné #include <vm/vm_kern.h> 561a9cdd37SRoger Pau Monné #include <vm/vm_page.h> 571a9cdd37SRoger Pau Monné #include <vm/vm_map.h> 581a9cdd37SRoger Pau Monné #include <vm/vm_object.h> 591a9cdd37SRoger Pau Monné #include <vm/vm_pager.h> 601a9cdd37SRoger Pau Monné #include <vm/vm_param.h> 611a9cdd37SRoger Pau Monné 62b0663c33SRoger Pau Monné #include <machine/_inttypes.h> 63fae92773SJohn Baldwin #include <machine/intr_machdep.h> 64fae92773SJohn Baldwin #include <x86/apicvar.h> 6597baeefdSRoger Pau Monné #include <x86/init.h> 661e69553eSRoger Pau Monné #include <machine/pc/bios.h> 67079f7ef8SRoger Pau Monné #include <machine/smp.h> 6844e06d15SRoger Pau Monné #include <machine/intr_machdep.h> 69a2495c36SRoger Pau Monné #include <machine/md_var.h> 70ca49b334SRoger Pau Monné #include <machine/metadata.h> 7177cb05dbSRoger Pau Monné #include <machine/cpu.h> 7297baeefdSRoger Pau Monné 731a9cdd37SRoger Pau Monné #include <xen/xen-os.h> 74b0663c33SRoger Pau Monné #include <xen/hvm.h> 751a9cdd37SRoger Pau Monné #include <xen/hypervisor.h> 76b7df74eeSWarner Losh #include <xen/xenstore/xenstorevar.h> 77842471b3SRoger Pau Monné #include <xen/xen_pv.h> 781a9cdd37SRoger Pau Monné 79a8ea1540SColin Percival #include <contrib/xen/arch-x86/cpuid.h> 80ad7dd514SElliott Mitchell #include <contrib/xen/arch-x86/hvm/start_info.h> 81ad7dd514SElliott Mitchell #include <contrib/xen/vcpu.h> 82079f7ef8SRoger Pau Monné 835f05c794SRoger Pau Monné #include <dev/xen/timer/timer.h> 845f05c794SRoger Pau Monné 85c98a2727SRoger Pau Monné #ifdef DDB 86c98a2727SRoger Pau Monné #include <ddb/ddb.h> 87c98a2727SRoger Pau Monné #endif 88c98a2727SRoger Pau Monné 891a9cdd37SRoger Pau Monné /* Native initial function */ 901a9cdd37SRoger Pau Monné extern u_int64_t hammer_time(u_int64_t, u_int64_t); 911a9cdd37SRoger Pau Monné /* Xen initial function */ 92b0663c33SRoger Pau Monné uint64_t hammer_time_xen(vm_paddr_t); 931a9cdd37SRoger Pau Monné 941e69553eSRoger Pau Monné #define MAX_E820_ENTRIES 128 951e69553eSRoger Pau Monné 9697baeefdSRoger Pau Monné /*--------------------------- Forward Declarations ---------------------------*/ 97b0663c33SRoger Pau Monné static caddr_t xen_pvh_parse_preload_data(uint64_t); 98023a025bSColin Percival static void pvh_parse_memmap(caddr_t, vm_paddr_t *, int *); 9997baeefdSRoger Pau Monné 100079f7ef8SRoger Pau Monné /*---------------------------- Extern Declarations ---------------------------*/ 101c98a2727SRoger Pau Monné /* 102c98a2727SRoger Pau Monné * Placed by the linker at the end of the bss section, which is the last 103c98a2727SRoger Pau Monné * section loaded by Xen before loading the symtab and strtab. 104c98a2727SRoger Pau Monné */ 105c98a2727SRoger Pau Monné extern uint32_t end; 106c98a2727SRoger Pau Monné 10797baeefdSRoger Pau Monné /*-------------------------------- Global Data -------------------------------*/ 108b0663c33SRoger Pau Monné struct init_ops xen_pvh_init_ops = { 109b0663c33SRoger Pau Monné .parse_preload_data = xen_pvh_parse_preload_data, 110b0663c33SRoger Pau Monné .early_clock_source_init = xen_clock_init, 111b0663c33SRoger Pau Monné .early_delay = xen_delay, 112023a025bSColin Percival .parse_memmap = pvh_parse_memmap, 113b0663c33SRoger Pau Monné }; 114b0663c33SRoger Pau Monné 1151e69553eSRoger Pau Monné static struct bios_smap xen_smap[MAX_E820_ENTRIES]; 1161e69553eSRoger Pau Monné 117b0663c33SRoger Pau Monné static struct hvm_start_info *start_info; 118cfa0b7b8SRoger Pau Monné 11997baeefdSRoger Pau Monné /*-------------------------------- Xen PV init -------------------------------*/ 12097baeefdSRoger Pau Monné 121a8ea1540SColin Percival static int 122a8ea1540SColin Percival isxen(void) 123a8ea1540SColin Percival { 124a8ea1540SColin Percival static int xen = -1; 125a8ea1540SColin Percival uint32_t base; 126a8ea1540SColin Percival u_int regs[4]; 127a8ea1540SColin Percival 128a8ea1540SColin Percival if (xen != -1) 129a8ea1540SColin Percival return (xen); 130a8ea1540SColin Percival 131a8ea1540SColin Percival /* 132a8ea1540SColin Percival * The full code for identifying which hypervisor we're running under 133a8ea1540SColin Percival * is in sys/x86/x86/identcpu.c and runs later in the boot process; 134a8ea1540SColin Percival * this is sufficient to distinguish Xen PVH booting from non-Xen PVH 135a8ea1540SColin Percival * and skip some very early Xen-specific code in the non-Xen case. 136a8ea1540SColin Percival */ 137a8ea1540SColin Percival xen = 0; 138a8ea1540SColin Percival for (base = 0x40000000; base < 0x40010000; base += 0x100) { 139a8ea1540SColin Percival do_cpuid(base, regs); 140a8ea1540SColin Percival if (regs[1] == XEN_CPUID_SIGNATURE_EBX && 141a8ea1540SColin Percival regs[2] == XEN_CPUID_SIGNATURE_ECX && 142a8ea1540SColin Percival regs[3] == XEN_CPUID_SIGNATURE_EDX) { 143a8ea1540SColin Percival xen = 1; 144a8ea1540SColin Percival break; 145a8ea1540SColin Percival } 146a8ea1540SColin Percival } 147a8ea1540SColin Percival return (xen); 148a8ea1540SColin Percival } 149a8ea1540SColin Percival 150a8ea1540SColin Percival #define CRASH(...) do { \ 151a8ea1540SColin Percival if (isxen()) { \ 152a8ea1540SColin Percival xc_printf(__VA_ARGS__); \ 153a8ea1540SColin Percival HYPERVISOR_shutdown(SHUTDOWN_crash); \ 154a8ea1540SColin Percival } else { \ 155a8ea1540SColin Percival halt(); \ 156a8ea1540SColin Percival } \ 157a8ea1540SColin Percival } while (0) 158a8ea1540SColin Percival 159b0663c33SRoger Pau Monné uint64_t 160b0663c33SRoger Pau Monné hammer_time_xen(vm_paddr_t start_info_paddr) 161b0663c33SRoger Pau Monné { 162b0663c33SRoger Pau Monné struct hvm_modlist_entry *mod; 163b0663c33SRoger Pau Monné struct xen_add_to_physmap xatp; 164b0663c33SRoger Pau Monné uint64_t physfree; 165b0663c33SRoger Pau Monné char *kenv; 166b0663c33SRoger Pau Monné int rc; 167b0663c33SRoger Pau Monné 168a8ea1540SColin Percival if (isxen()) { 169b0663c33SRoger Pau Monné xen_domain_type = XEN_HVM_DOMAIN; 170b0663c33SRoger Pau Monné vm_guest = VM_GUEST_XEN; 171b0663c33SRoger Pau Monné rc = xen_hvm_init_hypercall_stubs(XEN_HVM_INIT_EARLY); 172b0663c33SRoger Pau Monné if (rc) { 173b0663c33SRoger Pau Monné xc_printf("ERROR: failed to initialize hypercall page: %d\n", 174b0663c33SRoger Pau Monné rc); 175b0663c33SRoger Pau Monné HYPERVISOR_shutdown(SHUTDOWN_crash); 176b0663c33SRoger Pau Monné } 177a8ea1540SColin Percival } 178b0663c33SRoger Pau Monné 179b0663c33SRoger Pau Monné start_info = (struct hvm_start_info *)(start_info_paddr + KERNBASE); 180b0663c33SRoger Pau Monné if (start_info->magic != XEN_HVM_START_MAGIC_VALUE) { 181a8ea1540SColin Percival CRASH("Unknown magic value in start_info struct: %#x\n", 182b0663c33SRoger Pau Monné start_info->magic); 183b0663c33SRoger Pau Monné } 184b0663c33SRoger Pau Monné 185b0663c33SRoger Pau Monné /* 18677cb05dbSRoger Pau Monné * Select the higher address to use as physfree: either after 18777cb05dbSRoger Pau Monné * start_info, after the kernel, after the memory map or after any of 18877cb05dbSRoger Pau Monné * the modules. We assume enough memory to be available after the 18977cb05dbSRoger Pau Monné * selected address for the needs of very early memory allocations. 190b0663c33SRoger Pau Monné */ 19177cb05dbSRoger Pau Monné physfree = roundup2(start_info_paddr + sizeof(struct hvm_start_info), 19277cb05dbSRoger Pau Monné PAGE_SIZE); 19377cb05dbSRoger Pau Monné physfree = MAX(roundup2((vm_paddr_t)_end - KERNBASE, PAGE_SIZE), 19477cb05dbSRoger Pau Monné physfree); 19577cb05dbSRoger Pau Monné 19677cb05dbSRoger Pau Monné if (start_info->memmap_paddr != 0) 19777cb05dbSRoger Pau Monné physfree = MAX(roundup2(start_info->memmap_paddr + 19877cb05dbSRoger Pau Monné start_info->memmap_entries * 19977cb05dbSRoger Pau Monné sizeof(struct hvm_memmap_table_entry), PAGE_SIZE), 20077cb05dbSRoger Pau Monné physfree); 20177cb05dbSRoger Pau Monné 20277cb05dbSRoger Pau Monné if (start_info->modlist_paddr != 0) { 20377cb05dbSRoger Pau Monné unsigned int i; 20477cb05dbSRoger Pau Monné 20577cb05dbSRoger Pau Monné if (start_info->nr_modules == 0) { 206a8ea1540SColin Percival CRASH( 20777cb05dbSRoger Pau Monné "ERROR: modlist_paddr != 0 but nr_modules == 0\n"); 20877cb05dbSRoger Pau Monné } 20977cb05dbSRoger Pau Monné mod = (struct hvm_modlist_entry *) 21077cb05dbSRoger Pau Monné (start_info->modlist_paddr + KERNBASE); 21177cb05dbSRoger Pau Monné for (i = 0; i < start_info->nr_modules; i++) 21277cb05dbSRoger Pau Monné physfree = MAX(roundup2(mod[i].paddr + mod[i].size, 21377cb05dbSRoger Pau Monné PAGE_SIZE), physfree); 21477cb05dbSRoger Pau Monné } 215b0663c33SRoger Pau Monné 216a8ea1540SColin Percival if (isxen()) { 217b0663c33SRoger Pau Monné xatp.domid = DOMID_SELF; 218b0663c33SRoger Pau Monné xatp.idx = 0; 219b0663c33SRoger Pau Monné xatp.space = XENMAPSPACE_shared_info; 220b0663c33SRoger Pau Monné xatp.gpfn = atop(physfree); 221b0663c33SRoger Pau Monné if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) { 222b0663c33SRoger Pau Monné xc_printf("ERROR: failed to setup shared_info page\n"); 223b0663c33SRoger Pau Monné HYPERVISOR_shutdown(SHUTDOWN_crash); 224b0663c33SRoger Pau Monné } 225b0663c33SRoger Pau Monné HYPERVISOR_shared_info = (shared_info_t *)(physfree + KERNBASE); 226b0663c33SRoger Pau Monné physfree += PAGE_SIZE; 227a8ea1540SColin Percival } 228b0663c33SRoger Pau Monné 229b0663c33SRoger Pau Monné /* 230b0663c33SRoger Pau Monné * Init a static kenv using a free page. The contents will be filled 231b0663c33SRoger Pau Monné * from the parse_preload_data hook. 232b0663c33SRoger Pau Monné */ 233b0663c33SRoger Pau Monné kenv = (void *)(physfree + KERNBASE); 234b0663c33SRoger Pau Monné physfree += PAGE_SIZE; 2353f102f58SMateusz Guzik bzero_early(kenv, PAGE_SIZE); 236b0663c33SRoger Pau Monné init_static_kenv(kenv, PAGE_SIZE); 237b0663c33SRoger Pau Monné 238b0663c33SRoger Pau Monné /* Set the hooks for early functions that diverge from bare metal */ 239b0663c33SRoger Pau Monné init_ops = xen_pvh_init_ops; 240b0663c33SRoger Pau Monné hvm_start_flags = start_info->flags; 241b0663c33SRoger Pau Monné 242b0663c33SRoger Pau Monné /* Now we can jump into the native init function */ 243b0663c33SRoger Pau Monné return (hammer_time(0, physfree)); 244b0663c33SRoger Pau Monné } 245b0663c33SRoger Pau Monné 24697baeefdSRoger Pau Monné /*-------------------------------- PV specific -------------------------------*/ 247079f7ef8SRoger Pau Monné 24897baeefdSRoger Pau Monné /* 249b0663c33SRoger Pau Monné * When booted as a PVH guest FreeBSD needs to avoid using the RSDP address 250b0663c33SRoger Pau Monné * hint provided by the loader because it points to the native set of ACPI 251b0663c33SRoger Pau Monné * tables instead of the ones crafted by Xen. The acpi.rsdp env variable is 252b0663c33SRoger Pau Monné * removed from kenv if present, and a new acpi.rsdp is added to kenv that 253b0663c33SRoger Pau Monné * points to the address of the Xen crafted RSDP. 25497baeefdSRoger Pau Monné */ 255b0663c33SRoger Pau Monné static bool reject_option(const char *option) 25697baeefdSRoger Pau Monné { 257b0663c33SRoger Pau Monné static const char *reject[] = { 258b0663c33SRoger Pau Monné "acpi.rsdp", 259b0663c33SRoger Pau Monné }; 260b0663c33SRoger Pau Monné unsigned int i; 26197baeefdSRoger Pau Monné 262b0663c33SRoger Pau Monné for (i = 0; i < nitems(reject); i++) 263b0663c33SRoger Pau Monné if (strncmp(option, reject[i], strlen(reject[i])) == 0) 264b0663c33SRoger Pau Monné return (true); 26597baeefdSRoger Pau Monné 266b0663c33SRoger Pau Monné return (false); 267b0663c33SRoger Pau Monné } 26897baeefdSRoger Pau Monné 269b0663c33SRoger Pau Monné static void 270b0663c33SRoger Pau Monné xen_pvh_set_env(char *env, bool (*filter)(const char *)) 271b0663c33SRoger Pau Monné { 272b0663c33SRoger Pau Monné char *option; 27397baeefdSRoger Pau Monné 274b0663c33SRoger Pau Monné if (env == NULL) 275b0663c33SRoger Pau Monné return; 276b0663c33SRoger Pau Monné 277b0663c33SRoger Pau Monné option = env; 278b0663c33SRoger Pau Monné while (*option != 0) { 279b0663c33SRoger Pau Monné char *value; 280b0663c33SRoger Pau Monné 281b0663c33SRoger Pau Monné if (filter != NULL && filter(option)) { 282b0663c33SRoger Pau Monné option += strlen(option) + 1; 283b0663c33SRoger Pau Monné continue; 284b0663c33SRoger Pau Monné } 285b0663c33SRoger Pau Monné 286b0663c33SRoger Pau Monné value = option; 287b0663c33SRoger Pau Monné option = strsep(&value, "="); 288a8ea1540SColin Percival if (kern_setenv(option, value) != 0 && isxen()) 289b0663c33SRoger Pau Monné xc_printf("unable to add kenv %s=%s\n", option, value); 290b0663c33SRoger Pau Monné option = value + strlen(value) + 1; 291b0663c33SRoger Pau Monné } 29297baeefdSRoger Pau Monné } 29397baeefdSRoger Pau Monné 294c98a2727SRoger Pau Monné #ifdef DDB 295c98a2727SRoger Pau Monné /* 296c98a2727SRoger Pau Monné * The way Xen loads the symtab is different from the native boot loader, 297c98a2727SRoger Pau Monné * because it's tailored for NetBSD. So we have to adapt and use the same 298c98a2727SRoger Pau Monné * method as NetBSD. Portions of the code below have been picked from NetBSD: 299c98a2727SRoger Pau Monné * sys/kern/kern_ksyms.c CVS Revision 1.71. 300c98a2727SRoger Pau Monné */ 301c98a2727SRoger Pau Monné static void 302b0663c33SRoger Pau Monné xen_pvh_parse_symtab(void) 303c98a2727SRoger Pau Monné { 304c98a2727SRoger Pau Monné Elf_Ehdr *ehdr; 305c98a2727SRoger Pau Monné Elf_Shdr *shdr; 306c98a2727SRoger Pau Monné int i, j; 307c98a2727SRoger Pau Monné 308c98a2727SRoger Pau Monné ehdr = (Elf_Ehdr *)(&end + 1); 309c98a2727SRoger Pau Monné if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || 310c98a2727SRoger Pau Monné ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 311c98a2727SRoger Pau Monné ehdr->e_version > 1) { 312a8ea1540SColin Percival if (isxen()) 313c98a2727SRoger Pau Monné xc_printf("Unable to load ELF symtab: invalid symbol table\n"); 314c98a2727SRoger Pau Monné return; 315c98a2727SRoger Pau Monné } 316c98a2727SRoger Pau Monné 317c98a2727SRoger Pau Monné shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff); 318c98a2727SRoger Pau Monné /* Find the symbol table and the corresponding string table. */ 319c98a2727SRoger Pau Monné for (i = 1; i < ehdr->e_shnum; i++) { 320c98a2727SRoger Pau Monné if (shdr[i].sh_type != SHT_SYMTAB) 321c98a2727SRoger Pau Monné continue; 322c98a2727SRoger Pau Monné if (shdr[i].sh_offset == 0) 323c98a2727SRoger Pau Monné continue; 324c98a2727SRoger Pau Monné ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset); 325c98a2727SRoger Pau Monné ksymtab_size = shdr[i].sh_size; 326c98a2727SRoger Pau Monné j = shdr[i].sh_link; 327c98a2727SRoger Pau Monné if (shdr[j].sh_offset == 0) 328c98a2727SRoger Pau Monné continue; /* Can this happen? */ 329c98a2727SRoger Pau Monné kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset); 330c98a2727SRoger Pau Monné break; 331c98a2727SRoger Pau Monné } 332c98a2727SRoger Pau Monné 333a8ea1540SColin Percival if ((ksymtab == 0 || kstrtab == 0) && isxen()) 334c98a2727SRoger Pau Monné xc_printf( 335c98a2727SRoger Pau Monné "Unable to load ELF symtab: could not find symtab or strtab\n"); 336c98a2727SRoger Pau Monné } 337c98a2727SRoger Pau Monné #endif 338c98a2727SRoger Pau Monné 3396f80738bSRoger Pau Monné static void 3406f80738bSRoger Pau Monné fixup_console(caddr_t kmdp) 3416f80738bSRoger Pau Monné { 3426f80738bSRoger Pau Monné struct xen_platform_op op = { 3436f80738bSRoger Pau Monné .cmd = XENPF_get_dom0_console, 3446f80738bSRoger Pau Monné }; 3456f80738bSRoger Pau Monné xenpf_dom0_console_t *console = &op.u.dom0_console; 3466f80738bSRoger Pau Monné union { 3476f80738bSRoger Pau Monné struct efi_fb efi; 3486f80738bSRoger Pau Monné struct vbe_fb vbe; 3496f80738bSRoger Pau Monné } *fb = NULL; 350a78e46a7SRoger Pau Monné int size; 3516f80738bSRoger Pau Monné 352a78e46a7SRoger Pau Monné size = HYPERVISOR_platform_op(&op); 353a78e46a7SRoger Pau Monné if (size < 0) { 354a78e46a7SRoger Pau Monné xc_printf("Failed to get dom0 video console info: %d\n", size); 3556f80738bSRoger Pau Monné return; 3566f80738bSRoger Pau Monné } 3576f80738bSRoger Pau Monné 3586f80738bSRoger Pau Monné switch (console->video_type) { 3596f80738bSRoger Pau Monné case XEN_VGATYPE_VESA_LFB: 3606f80738bSRoger Pau Monné fb = (__typeof__ (fb))preload_search_info(kmdp, 3616f80738bSRoger Pau Monné MODINFO_METADATA | MODINFOMD_VBE_FB); 3626f80738bSRoger Pau Monné 3636f80738bSRoger Pau Monné if (fb == NULL) { 3646f80738bSRoger Pau Monné xc_printf("No VBE FB in kernel metadata\n"); 3656f80738bSRoger Pau Monné return; 3666f80738bSRoger Pau Monné } 3676f80738bSRoger Pau Monné 3686f80738bSRoger Pau Monné _Static_assert(offsetof(struct vbe_fb, fb_bpp) == 3696f80738bSRoger Pau Monné offsetof(struct efi_fb, fb_mask_reserved) + 3706f80738bSRoger Pau Monné sizeof(fb->efi.fb_mask_reserved), 3716f80738bSRoger Pau Monné "Bad structure overlay\n"); 3726f80738bSRoger Pau Monné fb->vbe.fb_bpp = console->u.vesa_lfb.bits_per_pixel; 3736f80738bSRoger Pau Monné /* FALLTHROUGH */ 3746f80738bSRoger Pau Monné case XEN_VGATYPE_EFI_LFB: 3756f80738bSRoger Pau Monné if (fb == NULL) { 3766f80738bSRoger Pau Monné fb = (__typeof__ (fb))preload_search_info(kmdp, 3776f80738bSRoger Pau Monné MODINFO_METADATA | MODINFOMD_EFI_FB); 3786f80738bSRoger Pau Monné if (fb == NULL) { 3796f80738bSRoger Pau Monné xc_printf("No EFI FB in kernel metadata\n"); 3806f80738bSRoger Pau Monné return; 3816f80738bSRoger Pau Monné } 3826f80738bSRoger Pau Monné } 3836f80738bSRoger Pau Monné 384a78e46a7SRoger Pau Monné fb->efi.fb_addr = console->u.vesa_lfb.lfb_base; 385a78e46a7SRoger Pau Monné if (size > 386a78e46a7SRoger Pau Monné offsetof(xenpf_dom0_console_t, u.vesa_lfb.ext_lfb_base)) 387a78e46a7SRoger Pau Monné fb->efi.fb_addr |= 388a78e46a7SRoger Pau Monné (uint64_t)console->u.vesa_lfb.ext_lfb_base << 32; 3896f80738bSRoger Pau Monné fb->efi.fb_size = console->u.vesa_lfb.lfb_size << 16; 3906f80738bSRoger Pau Monné fb->efi.fb_height = console->u.vesa_lfb.height; 3916f80738bSRoger Pau Monné fb->efi.fb_width = console->u.vesa_lfb.width; 3926f80738bSRoger Pau Monné fb->efi.fb_stride = (console->u.vesa_lfb.bytes_per_line << 3) / 3936f80738bSRoger Pau Monné console->u.vesa_lfb.bits_per_pixel; 3946f80738bSRoger Pau Monné #define FBMASK(c) \ 3956f80738bSRoger Pau Monné ((~0u << console->u.vesa_lfb.c ## _pos) & \ 3966f80738bSRoger Pau Monné (~0u >> (32 - console->u.vesa_lfb.c ## _pos - \ 3976f80738bSRoger Pau Monné console->u.vesa_lfb.c ## _size))) 3986f80738bSRoger Pau Monné fb->efi.fb_mask_red = FBMASK(red); 3996f80738bSRoger Pau Monné fb->efi.fb_mask_green = FBMASK(green); 4006f80738bSRoger Pau Monné fb->efi.fb_mask_blue = FBMASK(blue); 4016f80738bSRoger Pau Monné fb->efi.fb_mask_reserved = FBMASK(rsvd); 4026f80738bSRoger Pau Monné #undef FBMASK 4036f80738bSRoger Pau Monné break; 4046f80738bSRoger Pau Monné 4056f80738bSRoger Pau Monné default: 4066f80738bSRoger Pau Monné xc_printf("Video console type unsupported\n"); 4076f80738bSRoger Pau Monné return; 4086f80738bSRoger Pau Monné } 4096f80738bSRoger Pau Monné } 4106f80738bSRoger Pau Monné 41197baeefdSRoger Pau Monné static caddr_t 412b0663c33SRoger Pau Monné xen_pvh_parse_preload_data(uint64_t modulep) 413b0663c33SRoger Pau Monné { 414b0663c33SRoger Pau Monné caddr_t kmdp; 415b0663c33SRoger Pau Monné vm_ooffset_t off; 416b0663c33SRoger Pau Monné vm_paddr_t metadata; 417b0663c33SRoger Pau Monné char *envp; 418b0663c33SRoger Pau Monné char acpi_rsdp[19]; 419b0663c33SRoger Pau Monné 420b0663c33SRoger Pau Monné if (start_info->modlist_paddr != 0) { 421b0663c33SRoger Pau Monné struct hvm_modlist_entry *mod; 422b6d85a5fSRoger Pau Monné const char *cmdline; 423b0663c33SRoger Pau Monné 424b0663c33SRoger Pau Monné mod = (struct hvm_modlist_entry *) 425b0663c33SRoger Pau Monné (start_info->modlist_paddr + KERNBASE); 426b6d85a5fSRoger Pau Monné cmdline = mod[0].cmdline_paddr ? 427b6d85a5fSRoger Pau Monné (const char *)(mod[0].cmdline_paddr + KERNBASE) : NULL; 428b6d85a5fSRoger Pau Monné 429b6d85a5fSRoger Pau Monné if (strcmp(cmdline, "header") == 0) { 430b6d85a5fSRoger Pau Monné struct xen_header *header; 431b6d85a5fSRoger Pau Monné 432b6d85a5fSRoger Pau Monné header = (struct xen_header *)(mod[0].paddr + KERNBASE); 433b6d85a5fSRoger Pau Monné 434b6d85a5fSRoger Pau Monné if ((header->flags & XENHEADER_HAS_MODULEP_OFFSET) != 435b6d85a5fSRoger Pau Monné XENHEADER_HAS_MODULEP_OFFSET) { 436b6d85a5fSRoger Pau Monné xc_printf("Unable to load module metadata\n"); 437b6d85a5fSRoger Pau Monné HYPERVISOR_shutdown(SHUTDOWN_crash); 438b6d85a5fSRoger Pau Monné } 439b6d85a5fSRoger Pau Monné 440b6d85a5fSRoger Pau Monné preload_metadata = (caddr_t)(mod[0].paddr + 441b6d85a5fSRoger Pau Monné header->modulep_offset + KERNBASE); 442b6d85a5fSRoger Pau Monné 443b6d85a5fSRoger Pau Monné kmdp = preload_search_by_type("elf kernel"); 444b6d85a5fSRoger Pau Monné if (kmdp == NULL) 445b6d85a5fSRoger Pau Monné kmdp = preload_search_by_type("elf64 kernel"); 446b6d85a5fSRoger Pau Monné if (kmdp == NULL) { 447b6d85a5fSRoger Pau Monné xc_printf("Unable to find kernel\n"); 448b6d85a5fSRoger Pau Monné HYPERVISOR_shutdown(SHUTDOWN_crash); 449b6d85a5fSRoger Pau Monné } 450b6d85a5fSRoger Pau Monné 451b6d85a5fSRoger Pau Monné /* 452b6d85a5fSRoger Pau Monné * Xen has relocated the metadata and the modules, so 453b6d85a5fSRoger Pau Monné * we need to recalculate it's position. This is done 454b6d85a5fSRoger Pau Monné * by saving the original modulep address and then 455b6d85a5fSRoger Pau Monné * calculating the offset from the real modulep 456b6d85a5fSRoger Pau Monné * position. 457b6d85a5fSRoger Pau Monné */ 458b6d85a5fSRoger Pau Monné metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, 459b6d85a5fSRoger Pau Monné vm_paddr_t); 460b6d85a5fSRoger Pau Monné off = mod[0].paddr + header->modulep_offset - metadata + 461b6d85a5fSRoger Pau Monné KERNBASE; 462b6d85a5fSRoger Pau Monné } else { 463b0663c33SRoger Pau Monné preload_metadata = (caddr_t)(mod[0].paddr + KERNBASE); 464b0663c33SRoger Pau Monné 465b0663c33SRoger Pau Monné kmdp = preload_search_by_type("elf kernel"); 466b0663c33SRoger Pau Monné if (kmdp == NULL) 467b0663c33SRoger Pau Monné kmdp = preload_search_by_type("elf64 kernel"); 468b6d85a5fSRoger Pau Monné if (kmdp == NULL) { 469b6d85a5fSRoger Pau Monné xc_printf("Unable to find kernel\n"); 470b6d85a5fSRoger Pau Monné HYPERVISOR_shutdown(SHUTDOWN_crash); 471b6d85a5fSRoger Pau Monné } 472b0663c33SRoger Pau Monné 473b0663c33SRoger Pau Monné metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t); 474b0663c33SRoger Pau Monné off = mod[0].paddr + KERNBASE - metadata; 475b6d85a5fSRoger Pau Monné } 476b0663c33SRoger Pau Monné 477b0663c33SRoger Pau Monné preload_bootstrap_relocate(off); 478b0663c33SRoger Pau Monné 479b0663c33SRoger Pau Monné boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 480b0663c33SRoger Pau Monné envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *); 481b0663c33SRoger Pau Monné if (envp != NULL) 482b0663c33SRoger Pau Monné envp += off; 483b0663c33SRoger Pau Monné xen_pvh_set_env(envp, reject_option); 484a2495c36SRoger Pau Monné 485a2495c36SRoger Pau Monné if (MD_FETCH(kmdp, MODINFOMD_EFI_MAP, void *) != NULL) 486a2495c36SRoger Pau Monné strlcpy(bootmethod, "UEFI", sizeof(bootmethod)); 487a2495c36SRoger Pau Monné else 488a2495c36SRoger Pau Monné strlcpy(bootmethod, "BIOS", sizeof(bootmethod)); 4896f80738bSRoger Pau Monné 4906f80738bSRoger Pau Monné fixup_console(kmdp); 491b0663c33SRoger Pau Monné } else { 492b0663c33SRoger Pau Monné /* Parse the extra boot information given by Xen */ 493b0663c33SRoger Pau Monné if (start_info->cmdline_paddr != 0) 494b0663c33SRoger Pau Monné boot_parse_cmdline_delim( 495b0663c33SRoger Pau Monné (char *)(start_info->cmdline_paddr + KERNBASE), 496c4a4011cSColin Percival ", \t\n"); 497b0663c33SRoger Pau Monné kmdp = NULL; 49813f34e21SColin Percival strlcpy(bootmethod, "PVH", sizeof(bootmethod)); 499b0663c33SRoger Pau Monné } 500b0663c33SRoger Pau Monné 501b0663c33SRoger Pau Monné boothowto |= boot_env_to_howto(); 502b0663c33SRoger Pau Monné 503b0663c33SRoger Pau Monné snprintf(acpi_rsdp, sizeof(acpi_rsdp), "%#" PRIx64, 504b0663c33SRoger Pau Monné start_info->rsdp_paddr); 505b0663c33SRoger Pau Monné kern_setenv("acpi.rsdp", acpi_rsdp); 506b0663c33SRoger Pau Monné 507b0663c33SRoger Pau Monné #ifdef DDB 508b0663c33SRoger Pau Monné xen_pvh_parse_symtab(); 509c98a2727SRoger Pau Monné #endif 510ca49b334SRoger Pau Monné return (kmdp); 51197baeefdSRoger Pau Monné } 5121e69553eSRoger Pau Monné 5131e69553eSRoger Pau Monné static void 514023a025bSColin Percival pvh_parse_memmap_start_info(caddr_t kmdp, vm_paddr_t *physmap, 515023a025bSColin Percival int *physmap_idx) 516023a025bSColin Percival { 517023a025bSColin Percival const struct hvm_memmap_table_entry * entries; 518023a025bSColin Percival size_t nentries; 519023a025bSColin Percival size_t i; 520023a025bSColin Percival 521023a025bSColin Percival /* Extract from HVM start_info. */ 522023a025bSColin Percival entries = (struct hvm_memmap_table_entry *)(start_info->memmap_paddr + KERNBASE); 523023a025bSColin Percival nentries = start_info->memmap_entries; 524023a025bSColin Percival 525023a025bSColin Percival /* Convert into E820 format and handle one by one. */ 526023a025bSColin Percival for (i = 0; i < nentries; i++) { 527023a025bSColin Percival struct bios_smap entry; 528023a025bSColin Percival 529023a025bSColin Percival entry.base = entries[i].addr; 530023a025bSColin Percival entry.length = entries[i].size; 531023a025bSColin Percival 532023a025bSColin Percival /* 533023a025bSColin Percival * Luckily for us, the XEN_HVM_MEMMAP_TYPE_* values exactly 534023a025bSColin Percival * match the SMAP_TYPE_* values so we don't need to translate 535023a025bSColin Percival * anything here. 536023a025bSColin Percival */ 537023a025bSColin Percival entry.type = entries[i].type; 538023a025bSColin Percival 539023a025bSColin Percival bios_add_smap_entries(&entry, 1, physmap, physmap_idx); 540023a025bSColin Percival } 541023a025bSColin Percival } 542023a025bSColin Percival 543023a025bSColin Percival static void 544b0663c33SRoger Pau Monné xen_pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx) 5451e69553eSRoger Pau Monné { 5461e69553eSRoger Pau Monné struct xen_memory_map memmap; 5471e69553eSRoger Pau Monné u_int32_t size; 5481e69553eSRoger Pau Monné int rc; 5491e69553eSRoger Pau Monné 550a8ea1540SColin Percival /* We should only reach here if we're running under Xen. */ 551a8ea1540SColin Percival KASSERT(isxen(), ("xen_pvh_parse_memmap reached when !Xen")); 552a8ea1540SColin Percival 5531e69553eSRoger Pau Monné /* Fetch the E820 map from Xen */ 5541e69553eSRoger Pau Monné memmap.nr_entries = MAX_E820_ENTRIES; 5551e69553eSRoger Pau Monné set_xen_guest_handle(memmap.buffer, xen_smap); 5561e69553eSRoger Pau Monné rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); 557b0663c33SRoger Pau Monné if (rc) { 558b0663c33SRoger Pau Monné xc_printf("ERROR: unable to fetch Xen E820 memory map: %d\n", 559b0663c33SRoger Pau Monné rc); 560b0663c33SRoger Pau Monné HYPERVISOR_shutdown(SHUTDOWN_crash); 561b0663c33SRoger Pau Monné } 562b0663c33SRoger Pau Monné 5631e69553eSRoger Pau Monné size = memmap.nr_entries * sizeof(xen_smap[0]); 5641e69553eSRoger Pau Monné 5651e69553eSRoger Pau Monné bios_add_smap_entries(xen_smap, size, physmap, physmap_idx); 5661e69553eSRoger Pau Monné } 567023a025bSColin Percival 568023a025bSColin Percival static void 569023a025bSColin Percival pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx) 570023a025bSColin Percival { 571023a025bSColin Percival 572023a025bSColin Percival /* 573023a025bSColin Percival * If version >= 1 and memmap_paddr != 0, use the memory map provided 574023a025bSColin Percival * in the start_info structure; if not, we're running under legacy 575023a025bSColin Percival * Xen and need to use the Xen hypercall. 576023a025bSColin Percival */ 577023a025bSColin Percival if ((start_info->version >= 1) && (start_info->memmap_paddr != 0)) 578023a025bSColin Percival pvh_parse_memmap_start_info(kmdp, physmap, physmap_idx); 579023a025bSColin Percival else 580023a025bSColin Percival xen_pvh_parse_memmap(kmdp, physmap, physmap_idx); 581023a025bSColin Percival } 582