2 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
4 * Copyright (c) 2004 Christian Limpach.
5 * Copyright (c) 2004-2006,2008 Kip Macy
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Copyright (c) 2013 Roger Pau Monné <roger.pau@citrix.com>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
36 #include "opt_kstack_pages.h"
38 #include <sys/param.h>
40 #include <sys/kernel.h>
41 #include <sys/reboot.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/linker.h>
46 #include <sys/rwlock.h>
48 #include <sys/ctype.h>
49 #include <sys/mutex.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_pager.h>
59 #include <vm/vm_param.h>
61 #include <machine/_inttypes.h>
62 #include <machine/intr_machdep.h>
63 #include <x86/apicvar.h>
65 #include <machine/pc/bios.h>
66 #include <machine/smp.h>
67 #include <machine/intr_machdep.h>
68 #include <machine/metadata.h>
70 #include <xen/xen-os.h>
72 #include <xen/hypervisor.h>
73 #include <xen/xenstore/xenstorevar.h>
74 #include <xen/xen_pv.h>
75 #include <xen/xen_msi.h>
77 #include <xen/interface/arch-x86/hvm/start_info.h>
78 #include <xen/interface/vcpu.h>
80 #include <dev/xen/timer/timer.h>
86 /* Native initial function */
87 extern u_int64_t hammer_time(u_int64_t, u_int64_t);
88 /* Xen initial function */
89 uint64_t hammer_time_xen_legacy(start_info_t *, uint64_t);
90 uint64_t hammer_time_xen(vm_paddr_t);
92 #define MAX_E820_ENTRIES 128
94 /*--------------------------- Forward Declarations ---------------------------*/
95 static caddr_t xen_legacy_pvh_parse_preload_data(uint64_t);
96 static caddr_t xen_pvh_parse_preload_data(uint64_t);
97 static void xen_pvh_parse_memmap(caddr_t, vm_paddr_t *, int *);
100 static int xen_pv_start_all_aps(void);
103 /*---------------------------- Extern Declarations ---------------------------*/
105 /* Variables used by amd64 mp_machdep to start APs */
106 extern char *doublefault_stack;
107 extern char *mce_stack;
108 extern char *nmi_stack;
109 extern char *dbg_stack;
113 * Placed by the linker at the end of the bss section, which is the last
114 * section loaded by Xen before loading the symtab and strtab.
118 /*-------------------------------- Global Data -------------------------------*/
119 /* Xen init_ops implementation. */
120 struct init_ops xen_legacy_init_ops = {
121 .parse_preload_data = xen_legacy_pvh_parse_preload_data,
122 .early_clock_source_init = xen_clock_init,
123 .early_delay = xen_delay,
124 .parse_memmap = xen_pvh_parse_memmap,
126 .start_all_aps = xen_pv_start_all_aps,
128 .msi_init = xen_msi_init,
131 struct init_ops xen_pvh_init_ops = {
132 .parse_preload_data = xen_pvh_parse_preload_data,
133 .early_clock_source_init = xen_clock_init,
134 .early_delay = xen_delay,
135 .parse_memmap = xen_pvh_parse_memmap,
137 .mp_bootaddress = mp_bootaddress,
138 .start_all_aps = native_start_all_aps,
140 .msi_init = msi_init,
143 static struct bios_smap xen_smap[MAX_E820_ENTRIES];
145 static start_info_t *legacy_start_info;
146 static struct hvm_start_info *start_info;
148 /*----------------------- Legacy PVH start_info accessors --------------------*/
150 legacy_get_xenstore_mfn(void)
153 return (legacy_start_info->store_mfn);
157 legacy_get_xenstore_evtchn(void)
160 return (legacy_start_info->store_evtchn);
164 legacy_get_console_mfn(void)
167 return (legacy_start_info->console.domU.mfn);
171 legacy_get_console_evtchn(void)
174 return (legacy_start_info->console.domU.evtchn);
178 legacy_get_start_flags(void)
181 return (legacy_start_info->flags);
184 struct hypervisor_info legacy_info = {
185 .get_xenstore_mfn = legacy_get_xenstore_mfn,
186 .get_xenstore_evtchn = legacy_get_xenstore_evtchn,
187 .get_console_mfn = legacy_get_console_mfn,
188 .get_console_evtchn = legacy_get_console_evtchn,
189 .get_start_flags = legacy_get_start_flags,
192 /*-------------------------------- Xen PV init -------------------------------*/
194 * First function called by the Xen legacy PVH boot sequence.
196 * Set some Xen global variables and prepare the environment so it is
197 * as similar as possible to what native FreeBSD init function expects.
200 hammer_time_xen_legacy(start_info_t *si, uint64_t xenstack)
203 uint64_t *PT4 = (u_int64_t *)xenstack;
204 uint64_t *PT3 = (u_int64_t *)(xenstack + PAGE_SIZE);
205 uint64_t *PT2 = (u_int64_t *)(xenstack + 2 * PAGE_SIZE);
209 xen_domain_type = XEN_PV_DOMAIN;
210 vm_guest = VM_GUEST_XEN;
212 if ((si == NULL) || (xenstack == 0)) {
213 xc_printf("ERROR: invalid start_info or xen stack, halting\n");
214 HYPERVISOR_shutdown(SHUTDOWN_crash);
217 xc_printf("FreeBSD PVH running on %s\n", si->magic);
219 /* We use 3 pages of xen stack for the boot pagetables */
220 physfree = xenstack + 3 * PAGE_SIZE - KERNBASE;
222 /* Setup Xen global variables */
223 legacy_start_info = si;
224 HYPERVISOR_shared_info =
225 (shared_info_t *)(si->shared_info + KERNBASE);
228 * Use the stack Xen gives us to build the page tables
229 * as native FreeBSD expects to find them (created
230 * by the boot trampoline).
232 for (i = 0; i < (PAGE_SIZE / sizeof(uint64_t)); i++) {
234 * Each slot of the level 4 pages points
235 * to the same level 3 page
237 PT4[i] = ((uint64_t)&PT3[0]) - KERNBASE;
238 PT4[i] |= PG_V | PG_RW | PG_U;
241 * Each slot of the level 3 pages points
242 * to the same level 2 page
244 PT3[i] = ((uint64_t)&PT2[0]) - KERNBASE;
245 PT3[i] |= PG_V | PG_RW | PG_U;
248 * The level 2 page slots are mapped with
251 PT2[i] = i * (2 * 1024 * 1024);
252 PT2[i] |= PG_V | PG_RW | PG_PS | PG_U;
254 load_cr3(((uint64_t)&PT4[0]) - KERNBASE);
257 * Init an empty static kenv using a free page. The contents will be
258 * filled from the parse_preload_data hook.
260 kenv = (void *)(physfree + KERNBASE);
261 physfree += PAGE_SIZE;
262 bzero_early(kenv, PAGE_SIZE);
263 init_static_kenv(kenv, PAGE_SIZE);
265 /* Set the hooks for early functions that diverge from bare metal */
266 init_ops = xen_legacy_init_ops;
267 apic_ops = xen_apic_ops;
268 hypervisor_info = legacy_info;
270 /* Now we can jump into the native init function */
271 return (hammer_time(0, physfree));
275 hammer_time_xen(vm_paddr_t start_info_paddr)
277 struct hvm_modlist_entry *mod;
278 struct xen_add_to_physmap xatp;
283 xen_domain_type = XEN_HVM_DOMAIN;
284 vm_guest = VM_GUEST_XEN;
286 rc = xen_hvm_init_hypercall_stubs(XEN_HVM_INIT_EARLY);
288 xc_printf("ERROR: failed to initialize hypercall page: %d\n",
290 HYPERVISOR_shutdown(SHUTDOWN_crash);
293 start_info = (struct hvm_start_info *)(start_info_paddr + KERNBASE);
294 if (start_info->magic != XEN_HVM_START_MAGIC_VALUE) {
295 xc_printf("Unknown magic value in start_info struct: %#x\n",
297 HYPERVISOR_shutdown(SHUTDOWN_crash);
301 * The hvm_start_into structure is always appended after loading
302 * the kernel and modules.
304 physfree = roundup2(start_info_paddr + PAGE_SIZE, PAGE_SIZE);
306 xatp.domid = DOMID_SELF;
308 xatp.space = XENMAPSPACE_shared_info;
309 xatp.gpfn = atop(physfree);
310 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) {
311 xc_printf("ERROR: failed to setup shared_info page\n");
312 HYPERVISOR_shutdown(SHUTDOWN_crash);
314 HYPERVISOR_shared_info = (shared_info_t *)(physfree + KERNBASE);
315 physfree += PAGE_SIZE;
318 * Init a static kenv using a free page. The contents will be filled
319 * from the parse_preload_data hook.
321 kenv = (void *)(physfree + KERNBASE);
322 physfree += PAGE_SIZE;
323 bzero_early(kenv, PAGE_SIZE);
324 init_static_kenv(kenv, PAGE_SIZE);
326 if (start_info->modlist_paddr != 0) {
327 if (start_info->modlist_paddr >= physfree) {
329 "ERROR: unexpected module list memory address\n");
330 HYPERVISOR_shutdown(SHUTDOWN_crash);
332 if (start_info->nr_modules == 0) {
334 "ERROR: modlist_paddr != 0 but nr_modules == 0\n");
335 HYPERVISOR_shutdown(SHUTDOWN_crash);
337 mod = (struct hvm_modlist_entry *)
338 (vm_paddr_t)start_info->modlist_paddr + KERNBASE;
339 if (mod[0].paddr >= physfree) {
340 xc_printf("ERROR: unexpected module memory address\n");
341 HYPERVISOR_shutdown(SHUTDOWN_crash);
345 /* Set the hooks for early functions that diverge from bare metal */
346 init_ops = xen_pvh_init_ops;
347 hvm_start_flags = start_info->flags;
349 /* Now we can jump into the native init function */
350 return (hammer_time(0, physfree));
353 /*-------------------------------- PV specific -------------------------------*/
356 start_xen_ap(int cpu)
358 struct vcpu_guest_context *ctxt;
359 int ms, cpus = mp_naps;
360 const size_t stacksize = kstack_pages * PAGE_SIZE;
362 /* allocate and set up an idle stack data page */
363 bootstacks[cpu] = (void *)kmem_malloc(stacksize, M_WAITOK | M_ZERO);
364 doublefault_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
365 mce_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
366 nmi_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
367 dbg_stack = (void *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
368 dpcpu = (void *)kmem_malloc(DPCPU_SIZE, M_WAITOK | M_ZERO);
370 bootSTK = (char *)bootstacks[cpu] + kstack_pages * PAGE_SIZE - 8;
373 ctxt = malloc(sizeof(*ctxt), M_TEMP, M_WAITOK | M_ZERO);
375 ctxt->flags = VGCF_IN_KERNEL;
376 ctxt->user_regs.rip = (unsigned long) init_secondary;
377 ctxt->user_regs.rsp = (unsigned long) bootSTK;
379 /* Set the AP to use the same page tables */
380 ctxt->ctrlreg[3] = KPML4phys;
382 if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
383 panic("unable to initialize AP#%d", cpu);
387 /* Launch the vCPU */
388 if (HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
389 panic("unable to start AP#%d", cpu);
391 /* Wait up to 5 seconds for it to start. */
392 for (ms = 0; ms < 5000; ms++) {
402 xen_pv_start_all_aps(void)
406 mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
408 for (cpu = 1; cpu < mp_ncpus; cpu++) {
410 /* attempt to start the Application Processor */
411 if (!start_xen_ap(cpu))
412 panic("AP #%d failed to start!", cpu);
414 CPU_SET(cpu, &all_cpus); /* record AP in CPU map */
422 * When booted as a PVH guest FreeBSD needs to avoid using the RSDP address
423 * hint provided by the loader because it points to the native set of ACPI
424 * tables instead of the ones crafted by Xen. The acpi.rsdp env variable is
425 * removed from kenv if present, and a new acpi.rsdp is added to kenv that
426 * points to the address of the Xen crafted RSDP.
428 static bool reject_option(const char *option)
430 static const char *reject[] = {
435 for (i = 0; i < nitems(reject); i++)
436 if (strncmp(option, reject[i], strlen(reject[i])) == 0)
443 xen_pvh_set_env(char *env, bool (*filter)(const char *))
451 while (*option != 0) {
454 if (filter != NULL && filter(option)) {
455 option += strlen(option) + 1;
460 option = strsep(&value, "=");
461 if (kern_setenv(option, value) != 0)
462 xc_printf("unable to add kenv %s=%s\n", option, value);
463 option = value + strlen(value) + 1;
469 * The way Xen loads the symtab is different from the native boot loader,
470 * because it's tailored for NetBSD. So we have to adapt and use the same
471 * method as NetBSD. Portions of the code below have been picked from NetBSD:
472 * sys/kern/kern_ksyms.c CVS Revision 1.71.
475 xen_pvh_parse_symtab(void)
484 ehdr = (Elf_Ehdr *)(&end + 1);
485 if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
486 ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
487 ehdr->e_version > 1) {
488 xc_printf("Unable to load ELF symtab: invalid symbol table\n");
492 shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff);
493 /* Find the symbol table and the corresponding string table. */
494 for (i = 1; i < ehdr->e_shnum; i++) {
495 if (shdr[i].sh_type != SHT_SYMTAB)
497 if (shdr[i].sh_offset == 0)
499 ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset);
500 ksymtab_size = shdr[i].sh_size;
502 if (shdr[j].sh_offset == 0)
503 continue; /* Can this happen? */
504 kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset);
508 if (ksymtab == 0 || kstrtab == 0)
510 "Unable to load ELF symtab: could not find symtab or strtab\n");
515 xen_legacy_pvh_parse_preload_data(uint64_t modulep)
522 if (legacy_start_info->mod_start != 0) {
523 preload_metadata = (caddr_t)legacy_start_info->mod_start;
525 kmdp = preload_search_by_type("elf kernel");
527 kmdp = preload_search_by_type("elf64 kernel");
528 KASSERT(kmdp != NULL, ("unable to find kernel"));
531 * Xen has relocated the metadata and the modules,
532 * so we need to recalculate it's position. This is
533 * done by saving the original modulep address and
534 * then calculating the offset with mod_start,
535 * which contains the relocated modulep address.
537 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t);
538 off = legacy_start_info->mod_start - metadata;
540 preload_bootstrap_relocate(off);
542 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
543 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
546 xen_pvh_set_env(envp, NULL);
548 /* Parse the extra boot information given by Xen */
549 boot_parse_cmdline_delim(legacy_start_info->cmd_line, ",");
553 boothowto |= boot_env_to_howto();
556 xen_pvh_parse_symtab();
562 xen_pvh_parse_preload_data(uint64_t modulep)
570 if (start_info->modlist_paddr != 0) {
571 struct hvm_modlist_entry *mod;
573 mod = (struct hvm_modlist_entry *)
574 (start_info->modlist_paddr + KERNBASE);
575 preload_metadata = (caddr_t)(mod[0].paddr + KERNBASE);
577 kmdp = preload_search_by_type("elf kernel");
579 kmdp = preload_search_by_type("elf64 kernel");
580 KASSERT(kmdp != NULL, ("unable to find kernel"));
583 * Xen has relocated the metadata and the modules,
584 * so we need to recalculate it's position. This is
585 * done by saving the original modulep address and
586 * then calculating the offset with mod_start,
587 * which contains the relocated modulep address.
589 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t);
590 off = mod[0].paddr + KERNBASE - metadata;
592 preload_bootstrap_relocate(off);
594 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
595 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
598 xen_pvh_set_env(envp, reject_option);
600 /* Parse the extra boot information given by Xen */
601 if (start_info->cmdline_paddr != 0)
602 boot_parse_cmdline_delim(
603 (char *)(start_info->cmdline_paddr + KERNBASE),
608 boothowto |= boot_env_to_howto();
610 snprintf(acpi_rsdp, sizeof(acpi_rsdp), "%#" PRIx64,
611 start_info->rsdp_paddr);
612 kern_setenv("acpi.rsdp", acpi_rsdp);
615 xen_pvh_parse_symtab();
621 xen_pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx)
623 struct xen_memory_map memmap;
627 /* Fetch the E820 map from Xen */
628 memmap.nr_entries = MAX_E820_ENTRIES;
629 set_xen_guest_handle(memmap.buffer, xen_smap);
630 rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
632 xc_printf("ERROR: unable to fetch Xen E820 memory map: %d\n",
634 HYPERVISOR_shutdown(SHUTDOWN_crash);
637 size = memmap.nr_entries * sizeof(xen_smap[0]);
639 bios_add_smap_entries(xen_smap, size, physmap, physmap_idx);