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