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