1 /* 2 * Copyright (c) 2004 Christian Limpach. 3 * Copyright (c) 2004-2006,2008 Kip Macy 4 * Copyright (c) 2008 The NetBSD Foundation, Inc. 5 * Copyright (c) 2013 Roger Pau Monné <roger.pau@citrix.com> 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 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 50 #include <vm/vm.h> 51 #include <vm/vm_extern.h> 52 #include <vm/vm_kern.h> 53 #include <vm/vm_page.h> 54 #include <vm/vm_map.h> 55 #include <vm/vm_object.h> 56 #include <vm/vm_pager.h> 57 #include <vm/vm_param.h> 58 59 #include <machine/intr_machdep.h> 60 #include <x86/apicvar.h> 61 #include <x86/init.h> 62 #include <machine/pc/bios.h> 63 #include <machine/smp.h> 64 #include <machine/intr_machdep.h> 65 #include <machine/metadata.h> 66 67 #include <xen/xen-os.h> 68 #include <xen/hypervisor.h> 69 #include <xen/xenstore/xenstorevar.h> 70 #include <xen/xen_pv.h> 71 #include <xen/xen_msi.h> 72 73 #include <xen/interface/vcpu.h> 74 75 #include <dev/xen/timer/timer.h> 76 77 #ifdef DDB 78 #include <ddb/ddb.h> 79 #endif 80 81 /* Native initial function */ 82 extern u_int64_t hammer_time(u_int64_t, u_int64_t); 83 /* Xen initial function */ 84 uint64_t hammer_time_xen(start_info_t *, uint64_t); 85 86 #define MAX_E820_ENTRIES 128 87 88 /*--------------------------- Forward Declarations ---------------------------*/ 89 static caddr_t xen_pv_parse_preload_data(u_int64_t); 90 static void xen_pv_parse_memmap(caddr_t, vm_paddr_t *, int *); 91 92 #ifdef SMP 93 static int xen_pv_start_all_aps(void); 94 #endif 95 96 /*---------------------------- Extern Declarations ---------------------------*/ 97 #ifdef SMP 98 /* Variables used by amd64 mp_machdep to start APs */ 99 extern struct mtx ap_boot_mtx; 100 extern void *bootstacks[]; 101 extern char *doublefault_stack; 102 extern char *nmi_stack; 103 extern void *dpcpu; 104 extern int bootAP; 105 extern char *bootSTK; 106 #endif 107 108 /* 109 * Placed by the linker at the end of the bss section, which is the last 110 * section loaded by Xen before loading the symtab and strtab. 111 */ 112 extern uint32_t end; 113 114 /*-------------------------------- Global Data -------------------------------*/ 115 /* Xen init_ops implementation. */ 116 struct init_ops xen_init_ops = { 117 .parse_preload_data = xen_pv_parse_preload_data, 118 .early_clock_source_init = xen_clock_init, 119 .early_delay = xen_delay, 120 .parse_memmap = xen_pv_parse_memmap, 121 #ifdef SMP 122 .start_all_aps = xen_pv_start_all_aps, 123 #endif 124 .msi_init = xen_msi_init, 125 }; 126 127 static struct bios_smap xen_smap[MAX_E820_ENTRIES]; 128 129 /*-------------------------------- Xen PV init -------------------------------*/ 130 /* 131 * First function called by the Xen PVH boot sequence. 132 * 133 * Set some Xen global variables and prepare the environment so it is 134 * as similar as possible to what native FreeBSD init function expects. 135 */ 136 uint64_t 137 hammer_time_xen(start_info_t *si, uint64_t xenstack) 138 { 139 uint64_t physfree; 140 uint64_t *PT4 = (u_int64_t *)xenstack; 141 uint64_t *PT3 = (u_int64_t *)(xenstack + PAGE_SIZE); 142 uint64_t *PT2 = (u_int64_t *)(xenstack + 2 * PAGE_SIZE); 143 int i; 144 145 xen_domain_type = XEN_PV_DOMAIN; 146 vm_guest = VM_GUEST_XEN; 147 148 if ((si == NULL) || (xenstack == 0)) { 149 xc_printf("ERROR: invalid start_info or xen stack, halting\n"); 150 HYPERVISOR_shutdown(SHUTDOWN_crash); 151 } 152 153 xc_printf("FreeBSD PVH running on %s\n", si->magic); 154 155 /* We use 3 pages of xen stack for the boot pagetables */ 156 physfree = xenstack + 3 * PAGE_SIZE - KERNBASE; 157 158 /* Setup Xen global variables */ 159 HYPERVISOR_start_info = si; 160 HYPERVISOR_shared_info = 161 (shared_info_t *)(si->shared_info + KERNBASE); 162 163 /* 164 * Setup some misc global variables for Xen devices 165 * 166 * XXX: Devices that need these specific variables should 167 * be rewritten to fetch this info by themselves from the 168 * start_info page. 169 */ 170 xen_store = (struct xenstore_domain_interface *) 171 (ptoa(si->store_mfn) + KERNBASE); 172 console_page = (char *)(ptoa(si->console.domU.mfn) + KERNBASE); 173 174 /* 175 * Use the stack Xen gives us to build the page tables 176 * as native FreeBSD expects to find them (created 177 * by the boot trampoline). 178 */ 179 for (i = 0; i < (PAGE_SIZE / sizeof(uint64_t)); i++) { 180 /* 181 * Each slot of the level 4 pages points 182 * to the same level 3 page 183 */ 184 PT4[i] = ((uint64_t)&PT3[0]) - KERNBASE; 185 PT4[i] |= PG_V | PG_RW | PG_U; 186 187 /* 188 * Each slot of the level 3 pages points 189 * to the same level 2 page 190 */ 191 PT3[i] = ((uint64_t)&PT2[0]) - KERNBASE; 192 PT3[i] |= PG_V | PG_RW | PG_U; 193 194 /* 195 * The level 2 page slots are mapped with 196 * 2MB pages for 1GB. 197 */ 198 PT2[i] = i * (2 * 1024 * 1024); 199 PT2[i] |= PG_V | PG_RW | PG_PS | PG_U; 200 } 201 load_cr3(((uint64_t)&PT4[0]) - KERNBASE); 202 203 /* Set the hooks for early functions that diverge from bare metal */ 204 init_ops = xen_init_ops; 205 apic_ops = xen_apic_ops; 206 207 /* Now we can jump into the native init function */ 208 return (hammer_time(0, physfree)); 209 } 210 211 /*-------------------------------- PV specific -------------------------------*/ 212 #ifdef SMP 213 static bool 214 start_xen_ap(int cpu) 215 { 216 struct vcpu_guest_context *ctxt; 217 int ms, cpus = mp_naps; 218 const size_t stacksize = KSTACK_PAGES * PAGE_SIZE; 219 220 /* allocate and set up an idle stack data page */ 221 bootstacks[cpu] = 222 (void *)kmem_malloc(kernel_arena, stacksize, M_WAITOK | M_ZERO); 223 doublefault_stack = 224 (char *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO); 225 nmi_stack = 226 (char *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO); 227 dpcpu = 228 (void *)kmem_malloc(kernel_arena, DPCPU_SIZE, M_WAITOK | M_ZERO); 229 230 bootSTK = (char *)bootstacks[cpu] + KSTACK_PAGES * PAGE_SIZE - 8; 231 bootAP = cpu; 232 233 ctxt = malloc(sizeof(*ctxt), M_TEMP, M_WAITOK | M_ZERO); 234 if (ctxt == NULL) 235 panic("unable to allocate memory"); 236 237 ctxt->flags = VGCF_IN_KERNEL; 238 ctxt->user_regs.rip = (unsigned long) init_secondary; 239 ctxt->user_regs.rsp = (unsigned long) bootSTK; 240 241 /* Set the AP to use the same page tables */ 242 ctxt->ctrlreg[3] = KPML4phys; 243 244 if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt)) 245 panic("unable to initialize AP#%d", cpu); 246 247 free(ctxt, M_TEMP); 248 249 /* Launch the vCPU */ 250 if (HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL)) 251 panic("unable to start AP#%d", cpu); 252 253 /* Wait up to 5 seconds for it to start. */ 254 for (ms = 0; ms < 5000; ms++) { 255 if (mp_naps > cpus) 256 return (true); 257 DELAY(1000); 258 } 259 260 return (false); 261 } 262 263 static int 264 xen_pv_start_all_aps(void) 265 { 266 int cpu; 267 268 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN); 269 270 for (cpu = 1; cpu < mp_ncpus; cpu++) { 271 272 /* attempt to start the Application Processor */ 273 if (!start_xen_ap(cpu)) 274 panic("AP #%d failed to start!", cpu); 275 276 CPU_SET(cpu, &all_cpus); /* record AP in CPU map */ 277 } 278 279 return (mp_naps); 280 } 281 #endif /* SMP */ 282 283 /* 284 * Functions to convert the "extra" parameters passed by Xen 285 * into FreeBSD boot options. 286 */ 287 static void 288 xen_pv_set_env(void) 289 { 290 char *cmd_line_next, *cmd_line; 291 size_t env_size; 292 293 cmd_line = HYPERVISOR_start_info->cmd_line; 294 env_size = sizeof(HYPERVISOR_start_info->cmd_line); 295 296 /* Skip leading spaces */ 297 for (; isspace(*cmd_line) && (env_size != 0); cmd_line++) 298 env_size--; 299 300 /* Replace ',' with '\0' */ 301 for (cmd_line_next = cmd_line; strsep(&cmd_line_next, ",") != NULL;) 302 ; 303 304 init_static_kenv(cmd_line, env_size); 305 } 306 307 static void 308 xen_pv_set_boothowto(void) 309 { 310 int i; 311 char *env; 312 313 /* get equivalents from the environment */ 314 for (i = 0; howto_names[i].ev != NULL; i++) { 315 if ((env = kern_getenv(howto_names[i].ev)) != NULL) { 316 boothowto |= howto_names[i].mask; 317 freeenv(env); 318 } 319 } 320 } 321 322 #ifdef DDB 323 /* 324 * The way Xen loads the symtab is different from the native boot loader, 325 * because it's tailored for NetBSD. So we have to adapt and use the same 326 * method as NetBSD. Portions of the code below have been picked from NetBSD: 327 * sys/kern/kern_ksyms.c CVS Revision 1.71. 328 */ 329 static void 330 xen_pv_parse_symtab(void) 331 { 332 Elf_Ehdr *ehdr; 333 Elf_Shdr *shdr; 334 vm_offset_t sym_end; 335 uint32_t size; 336 int i, j; 337 338 size = end; 339 sym_end = HYPERVISOR_start_info->mod_start != 0 ? 340 HYPERVISOR_start_info->mod_start : 341 HYPERVISOR_start_info->mfn_list; 342 343 /* 344 * Make sure the size is right headed, sym_end is just a 345 * high boundary, but at least allows us to fail earlier. 346 */ 347 if ((vm_offset_t)&end + size > sym_end) { 348 xc_printf("Unable to load ELF symtab: size mismatch\n"); 349 return; 350 } 351 352 ehdr = (Elf_Ehdr *)(&end + 1); 353 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) || 354 ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS || 355 ehdr->e_version > 1) { 356 xc_printf("Unable to load ELF symtab: invalid symbol table\n"); 357 return; 358 } 359 360 shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff); 361 /* Find the symbol table and the corresponding string table. */ 362 for (i = 1; i < ehdr->e_shnum; i++) { 363 if (shdr[i].sh_type != SHT_SYMTAB) 364 continue; 365 if (shdr[i].sh_offset == 0) 366 continue; 367 ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset); 368 ksymtab_size = shdr[i].sh_size; 369 j = shdr[i].sh_link; 370 if (shdr[j].sh_offset == 0) 371 continue; /* Can this happen? */ 372 kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset); 373 break; 374 } 375 376 if (ksymtab == 0 || kstrtab == 0) { 377 xc_printf( 378 "Unable to load ELF symtab: could not find symtab or strtab\n"); 379 return; 380 } 381 } 382 #endif 383 384 static caddr_t 385 xen_pv_parse_preload_data(u_int64_t modulep) 386 { 387 caddr_t kmdp; 388 vm_ooffset_t off; 389 vm_paddr_t metadata; 390 391 if (HYPERVISOR_start_info->mod_start != 0) { 392 preload_metadata = (caddr_t)(HYPERVISOR_start_info->mod_start); 393 394 kmdp = preload_search_by_type("elf kernel"); 395 if (kmdp == NULL) 396 kmdp = preload_search_by_type("elf64 kernel"); 397 KASSERT(kmdp != NULL, ("unable to find kernel")); 398 399 /* 400 * Xen has relocated the metadata and the modules, 401 * so we need to recalculate it's position. This is 402 * done by saving the original modulep address and 403 * then calculating the offset with mod_start, 404 * which contains the relocated modulep address. 405 */ 406 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t); 407 off = HYPERVISOR_start_info->mod_start - metadata; 408 409 preload_bootstrap_relocate(off); 410 411 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int); 412 kern_envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *); 413 kern_envp += off; 414 } else { 415 /* Parse the extra boot information given by Xen */ 416 xen_pv_set_env(); 417 xen_pv_set_boothowto(); 418 kmdp = NULL; 419 } 420 421 #ifdef DDB 422 xen_pv_parse_symtab(); 423 #endif 424 return (kmdp); 425 } 426 427 static void 428 xen_pv_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx) 429 { 430 struct xen_memory_map memmap; 431 u_int32_t size; 432 int rc; 433 434 /* Fetch the E820 map from Xen */ 435 memmap.nr_entries = MAX_E820_ENTRIES; 436 set_xen_guest_handle(memmap.buffer, xen_smap); 437 rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); 438 if (rc) 439 panic("unable to fetch Xen E820 memory map"); 440 size = memmap.nr_entries * sizeof(xen_smap[0]); 441 442 bios_add_smap_entries(xen_smap, size, physmap, physmap_idx); 443 } 444