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