xref: /freebsd/sys/x86/xen/pv.c (revision 1b5e5ff68c2bafae55a5adab42f22039bbbce59c)
1ebf5747bSPedro F. Giffuni /*-
2b61a5730SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3ebf5747bSPedro F. Giffuni  *
41a9cdd37SRoger Pau Monné  * Copyright (c) 2004 Christian Limpach.
51a9cdd37SRoger Pau Monné  * Copyright (c) 2004-2006,2008 Kip Macy
69b4e54d3SRoger Pau Monné  * Copyright (c) 2008 The NetBSD Foundation, Inc.
71a9cdd37SRoger Pau Monné  * Copyright (c) 2013 Roger Pau Monné <roger.pau@citrix.com>
81a9cdd37SRoger Pau Monné  * All rights reserved.
91a9cdd37SRoger Pau Monné  *
101a9cdd37SRoger Pau Monné  * Redistribution and use in source and binary forms, with or without
111a9cdd37SRoger Pau Monné  * modification, are permitted provided that the following conditions
121a9cdd37SRoger Pau Monné  * are met:
131a9cdd37SRoger Pau Monné  * 1. Redistributions of source code must retain the above copyright
141a9cdd37SRoger Pau Monné  *    notice, this list of conditions and the following disclaimer.
151a9cdd37SRoger Pau Monné  * 2. Redistributions in binary form must reproduce the above copyright
161a9cdd37SRoger Pau Monné  *    notice, this list of conditions and the following disclaimer in the
171a9cdd37SRoger Pau Monné  *    documentation and/or other materials provided with the distribution.
181a9cdd37SRoger Pau Monné  *
191a9cdd37SRoger Pau Monné  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
201a9cdd37SRoger Pau Monné  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
211a9cdd37SRoger Pau Monné  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
221a9cdd37SRoger Pau Monné  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
231a9cdd37SRoger Pau Monné  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
241a9cdd37SRoger Pau Monné  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
251a9cdd37SRoger Pau Monné  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
261a9cdd37SRoger Pau Monné  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
271a9cdd37SRoger Pau Monné  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
281a9cdd37SRoger Pau Monné  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
291a9cdd37SRoger Pau Monné  * SUCH DAMAGE.
301a9cdd37SRoger Pau Monné  */
311a9cdd37SRoger Pau Monné 
321a9cdd37SRoger Pau Monné #include <sys/cdefs.h>
33c98a2727SRoger Pau Monné #include "opt_ddb.h"
34721555e7SZbigniew Bodek #include "opt_kstack_pages.h"
35c98a2727SRoger Pau Monné 
361a9cdd37SRoger Pau Monné #include <sys/param.h>
371a9cdd37SRoger Pau Monné #include <sys/bus.h>
381a9cdd37SRoger Pau Monné #include <sys/kernel.h>
391a9cdd37SRoger Pau Monné #include <sys/reboot.h>
401a9cdd37SRoger Pau Monné #include <sys/systm.h>
41079f7ef8SRoger Pau Monné #include <sys/malloc.h>
42c98a2727SRoger Pau Monné #include <sys/linker.h>
431a9cdd37SRoger Pau Monné #include <sys/lock.h>
441a9cdd37SRoger Pau Monné #include <sys/rwlock.h>
45aa389b4fSRoger Pau Monné #include <sys/boot.h>
4697baeefdSRoger Pau Monné #include <sys/ctype.h>
47079f7ef8SRoger Pau Monné #include <sys/mutex.h>
48079f7ef8SRoger Pau Monné #include <sys/smp.h>
49a2495c36SRoger Pau Monné #include <sys/efi.h>
509d6ae1e3SColin Percival #include <sys/tslog.h>
511a9cdd37SRoger Pau Monné 
521a9cdd37SRoger Pau Monné #include <vm/vm.h>
531a9cdd37SRoger Pau Monné #include <vm/vm_extern.h>
541a9cdd37SRoger Pau Monné #include <vm/vm_kern.h>
551a9cdd37SRoger Pau Monné #include <vm/vm_page.h>
561a9cdd37SRoger Pau Monné #include <vm/vm_map.h>
571a9cdd37SRoger Pau Monné #include <vm/vm_object.h>
581a9cdd37SRoger Pau Monné #include <vm/vm_pager.h>
591a9cdd37SRoger Pau Monné #include <vm/vm_param.h>
601a9cdd37SRoger Pau Monné 
61b0663c33SRoger Pau Monné #include <machine/_inttypes.h>
62fae92773SJohn Baldwin #include <machine/intr_machdep.h>
63*1b5e5ff6SRoger Pau Monné #include <x86/acpica_machdep.h>
64fae92773SJohn Baldwin #include <x86/apicvar.h>
6597baeefdSRoger Pau Monné #include <x86/init.h>
661e69553eSRoger Pau Monné #include <machine/pc/bios.h>
67079f7ef8SRoger Pau Monné #include <machine/smp.h>
6844e06d15SRoger Pau Monné #include <machine/intr_machdep.h>
69a2495c36SRoger Pau Monné #include <machine/md_var.h>
70ca49b334SRoger Pau Monné #include <machine/metadata.h>
7177cb05dbSRoger Pau Monné #include <machine/cpu.h>
7297baeefdSRoger Pau Monné 
731a9cdd37SRoger Pau Monné #include <xen/xen-os.h>
74b0663c33SRoger Pau Monné #include <xen/hvm.h>
751a9cdd37SRoger Pau Monné #include <xen/hypervisor.h>
76b7df74eeSWarner Losh #include <xen/xenstore/xenstorevar.h>
77842471b3SRoger Pau Monné #include <xen/xen_pv.h>
781a9cdd37SRoger Pau Monné 
79a8ea1540SColin Percival #include <contrib/xen/arch-x86/cpuid.h>
80ad7dd514SElliott Mitchell #include <contrib/xen/arch-x86/hvm/start_info.h>
81ad7dd514SElliott Mitchell #include <contrib/xen/vcpu.h>
82079f7ef8SRoger Pau Monné 
835f05c794SRoger Pau Monné #include <dev/xen/timer/timer.h>
845f05c794SRoger Pau Monné 
85c98a2727SRoger Pau Monné #ifdef DDB
86c98a2727SRoger Pau Monné #include <ddb/ddb.h>
87c98a2727SRoger Pau Monné #endif
88c98a2727SRoger Pau Monné 
891a9cdd37SRoger Pau Monné /* Native initial function */
901a9cdd37SRoger Pau Monné extern u_int64_t hammer_time(u_int64_t, u_int64_t);
911a9cdd37SRoger Pau Monné /* Xen initial function */
92b0663c33SRoger Pau Monné uint64_t hammer_time_xen(vm_paddr_t);
931a9cdd37SRoger Pau Monné 
941e69553eSRoger Pau Monné #define MAX_E820_ENTRIES	128
951e69553eSRoger Pau Monné 
9697baeefdSRoger Pau Monné /*--------------------------- Forward Declarations ---------------------------*/
97b0663c33SRoger Pau Monné static caddr_t xen_pvh_parse_preload_data(uint64_t);
98023a025bSColin Percival static void pvh_parse_memmap(caddr_t, vm_paddr_t *, int *);
9997baeefdSRoger Pau Monné 
100079f7ef8SRoger Pau Monné /*---------------------------- Extern Declarations ---------------------------*/
101c98a2727SRoger Pau Monné /*
102c98a2727SRoger Pau Monné  * Placed by the linker at the end of the bss section, which is the last
103c98a2727SRoger Pau Monné  * section loaded by Xen before loading the symtab and strtab.
104c98a2727SRoger Pau Monné  */
105c98a2727SRoger Pau Monné extern uint32_t end;
106c98a2727SRoger Pau Monné 
10797baeefdSRoger Pau Monné /*-------------------------------- Global Data -------------------------------*/
108b0663c33SRoger Pau Monné struct init_ops xen_pvh_init_ops = {
109b0663c33SRoger Pau Monné 	.parse_preload_data		= xen_pvh_parse_preload_data,
110b0663c33SRoger Pau Monné 	.early_clock_source_init	= xen_clock_init,
111b0663c33SRoger Pau Monné 	.early_delay			= xen_delay,
112023a025bSColin Percival 	.parse_memmap			= pvh_parse_memmap,
113b0663c33SRoger Pau Monné };
114b0663c33SRoger Pau Monné 
1151e69553eSRoger Pau Monné static struct bios_smap xen_smap[MAX_E820_ENTRIES];
1161e69553eSRoger Pau Monné 
117b0663c33SRoger Pau Monné static struct hvm_start_info *start_info;
118cfa0b7b8SRoger Pau Monné 
11997baeefdSRoger Pau Monné /*-------------------------------- Xen PV init -------------------------------*/
12097baeefdSRoger Pau Monné 
121a8ea1540SColin Percival static int
isxen(void)122a8ea1540SColin Percival isxen(void)
123a8ea1540SColin Percival {
124a8ea1540SColin Percival 	static int xen = -1;
125a8ea1540SColin Percival 	uint32_t base;
126a8ea1540SColin Percival 	u_int regs[4];
127a8ea1540SColin Percival 
128a8ea1540SColin Percival 	if (xen != -1)
129a8ea1540SColin Percival 		return (xen);
130a8ea1540SColin Percival 
131a8ea1540SColin Percival 	/*
132a8ea1540SColin Percival 	 * The full code for identifying which hypervisor we're running under
133a8ea1540SColin Percival 	 * is in sys/x86/x86/identcpu.c and runs later in the boot process;
134a8ea1540SColin Percival 	 * this is sufficient to distinguish Xen PVH booting from non-Xen PVH
135a8ea1540SColin Percival 	 * and skip some very early Xen-specific code in the non-Xen case.
136a8ea1540SColin Percival 	 */
137a8ea1540SColin Percival 	xen = 0;
138a8ea1540SColin Percival 	for (base = 0x40000000; base < 0x40010000; base += 0x100) {
139a8ea1540SColin Percival 		do_cpuid(base, regs);
140a8ea1540SColin Percival 		if (regs[1] == XEN_CPUID_SIGNATURE_EBX &&
141a8ea1540SColin Percival 		    regs[2] == XEN_CPUID_SIGNATURE_ECX &&
142a8ea1540SColin Percival 		    regs[3] == XEN_CPUID_SIGNATURE_EDX) {
143a8ea1540SColin Percival 			xen = 1;
144a8ea1540SColin Percival 			break;
145a8ea1540SColin Percival 		}
146a8ea1540SColin Percival 	}
147a8ea1540SColin Percival 	return (xen);
148a8ea1540SColin Percival }
149a8ea1540SColin Percival 
150a8ea1540SColin Percival #define CRASH(...) do {					\
1518f5406c7SRoger Pau Monné 	if (isxen())					\
152a8ea1540SColin Percival 		xc_printf(__VA_ARGS__);			\
153a8ea1540SColin Percival 	halt();						\
154a8ea1540SColin Percival } while (0)
155a8ea1540SColin Percival 
156b0663c33SRoger Pau Monné uint64_t
hammer_time_xen(vm_paddr_t start_info_paddr)157b0663c33SRoger Pau Monné hammer_time_xen(vm_paddr_t start_info_paddr)
158b0663c33SRoger Pau Monné {
159b0663c33SRoger Pau Monné 	struct hvm_modlist_entry *mod;
160b0663c33SRoger Pau Monné 	uint64_t physfree;
161b0663c33SRoger Pau Monné 
162b0663c33SRoger Pau Monné 	start_info = (struct hvm_start_info *)(start_info_paddr + KERNBASE);
163b0663c33SRoger Pau Monné 	if (start_info->magic != XEN_HVM_START_MAGIC_VALUE) {
164a8ea1540SColin Percival 		CRASH("Unknown magic value in start_info struct: %#x\n",
165b0663c33SRoger Pau Monné 		    start_info->magic);
166b0663c33SRoger Pau Monné 	}
167b0663c33SRoger Pau Monné 
168b0663c33SRoger Pau Monné 	/*
16977cb05dbSRoger Pau Monné 	 * Select the higher address to use as physfree: either after
17077cb05dbSRoger Pau Monné 	 * start_info, after the kernel, after the memory map or after any of
17177cb05dbSRoger Pau Monné 	 * the modules.  We assume enough memory to be available after the
17277cb05dbSRoger Pau Monné 	 * selected address for the needs of very early memory allocations.
173b0663c33SRoger Pau Monné 	 */
17477cb05dbSRoger Pau Monné 	physfree = roundup2(start_info_paddr + sizeof(struct hvm_start_info),
17577cb05dbSRoger Pau Monné 	    PAGE_SIZE);
17677cb05dbSRoger Pau Monné 	physfree = MAX(roundup2((vm_paddr_t)_end - KERNBASE, PAGE_SIZE),
17777cb05dbSRoger Pau Monné 	    physfree);
17877cb05dbSRoger Pau Monné 
17977cb05dbSRoger Pau Monné 	if (start_info->memmap_paddr != 0)
18077cb05dbSRoger Pau Monné 		physfree = MAX(roundup2(start_info->memmap_paddr +
18177cb05dbSRoger Pau Monné 		    start_info->memmap_entries *
18277cb05dbSRoger Pau Monné 		    sizeof(struct hvm_memmap_table_entry), PAGE_SIZE),
18377cb05dbSRoger Pau Monné 		    physfree);
18477cb05dbSRoger Pau Monné 
18577cb05dbSRoger Pau Monné 	if (start_info->modlist_paddr != 0) {
18677cb05dbSRoger Pau Monné 		unsigned int i;
18777cb05dbSRoger Pau Monné 
18877cb05dbSRoger Pau Monné 		if (start_info->nr_modules == 0) {
189a8ea1540SColin Percival 			CRASH(
19077cb05dbSRoger Pau Monné 			    "ERROR: modlist_paddr != 0 but nr_modules == 0\n");
19177cb05dbSRoger Pau Monné 		}
19277cb05dbSRoger Pau Monné 		mod = (struct hvm_modlist_entry *)
19377cb05dbSRoger Pau Monné 		    (start_info->modlist_paddr + KERNBASE);
19477cb05dbSRoger Pau Monné 		for (i = 0; i < start_info->nr_modules; i++)
19577cb05dbSRoger Pau Monné 			physfree = MAX(roundup2(mod[i].paddr + mod[i].size,
19677cb05dbSRoger Pau Monné 			    PAGE_SIZE), physfree);
19777cb05dbSRoger Pau Monné 	}
198b0663c33SRoger Pau Monné 
199b0663c33SRoger Pau Monné 	/* Set the hooks for early functions that diverge from bare metal */
200b0663c33SRoger Pau Monné 	init_ops = xen_pvh_init_ops;
201b0663c33SRoger Pau Monné 	hvm_start_flags = start_info->flags;
202b0663c33SRoger Pau Monné 
203b0663c33SRoger Pau Monné 	/* Now we can jump into the native init function */
204b0663c33SRoger Pau Monné 	return (hammer_time(0, physfree));
205b0663c33SRoger Pau Monné }
206b0663c33SRoger Pau Monné 
20797baeefdSRoger Pau Monné /*-------------------------------- PV specific -------------------------------*/
208079f7ef8SRoger Pau Monné 
209c98a2727SRoger Pau Monné #ifdef DDB
210c98a2727SRoger Pau Monné /*
211c98a2727SRoger Pau Monné  * The way Xen loads the symtab is different from the native boot loader,
212c98a2727SRoger Pau Monné  * because it's tailored for NetBSD. So we have to adapt and use the same
213c98a2727SRoger Pau Monné  * method as NetBSD. Portions of the code below have been picked from NetBSD:
214c98a2727SRoger Pau Monné  * sys/kern/kern_ksyms.c CVS Revision 1.71.
215c98a2727SRoger Pau Monné  */
216c98a2727SRoger Pau Monné static void
xen_pvh_parse_symtab(void)217b0663c33SRoger Pau Monné xen_pvh_parse_symtab(void)
218c98a2727SRoger Pau Monné {
219c98a2727SRoger Pau Monné 	Elf_Ehdr *ehdr;
220c98a2727SRoger Pau Monné 	Elf_Shdr *shdr;
221c98a2727SRoger Pau Monné 	int i, j;
222c98a2727SRoger Pau Monné 
223c98a2727SRoger Pau Monné 	ehdr = (Elf_Ehdr *)(&end + 1);
224c98a2727SRoger Pau Monné 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
225c98a2727SRoger Pau Monné 	    ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
226c98a2727SRoger Pau Monné 	    ehdr->e_version > 1) {
227a8ea1540SColin Percival 		if (isxen())
228c98a2727SRoger Pau Monné 			xc_printf("Unable to load ELF symtab: invalid symbol table\n");
229c98a2727SRoger Pau Monné 		return;
230c98a2727SRoger Pau Monné 	}
231c98a2727SRoger Pau Monné 
232c98a2727SRoger Pau Monné 	shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff);
233c98a2727SRoger Pau Monné 	/* Find the symbol table and the corresponding string table. */
234c98a2727SRoger Pau Monné 	for (i = 1; i < ehdr->e_shnum; i++) {
235c98a2727SRoger Pau Monné 		if (shdr[i].sh_type != SHT_SYMTAB)
236c98a2727SRoger Pau Monné 			continue;
237c98a2727SRoger Pau Monné 		if (shdr[i].sh_offset == 0)
238c98a2727SRoger Pau Monné 			continue;
239c98a2727SRoger Pau Monné 		ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset);
240c98a2727SRoger Pau Monné 		ksymtab_size = shdr[i].sh_size;
241c98a2727SRoger Pau Monné 		j = shdr[i].sh_link;
242c98a2727SRoger Pau Monné 		if (shdr[j].sh_offset == 0)
243c98a2727SRoger Pau Monné 			continue; /* Can this happen? */
244c98a2727SRoger Pau Monné 		kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset);
245c98a2727SRoger Pau Monné 		break;
246c98a2727SRoger Pau Monné 	}
247c98a2727SRoger Pau Monné 
248a8ea1540SColin Percival 	if ((ksymtab == 0 || kstrtab == 0) && isxen())
249c98a2727SRoger Pau Monné 		xc_printf(
250c98a2727SRoger Pau Monné     "Unable to load ELF symtab: could not find symtab or strtab\n");
251c98a2727SRoger Pau Monné }
252c98a2727SRoger Pau Monné #endif
253c98a2727SRoger Pau Monné 
25497baeefdSRoger Pau Monné static caddr_t
xen_pvh_parse_preload_data(uint64_t modulep)255b0663c33SRoger Pau Monné xen_pvh_parse_preload_data(uint64_t modulep)
256b0663c33SRoger Pau Monné {
257b0663c33SRoger Pau Monné 	caddr_t kmdp;
258b0663c33SRoger Pau Monné 	vm_ooffset_t off;
259b0663c33SRoger Pau Monné 	vm_paddr_t metadata;
260b0663c33SRoger Pau Monné 	char *envp;
261b0663c33SRoger Pau Monné 
2629d6ae1e3SColin Percival 	TSENTER();
263b0663c33SRoger Pau Monné 	if (start_info->modlist_paddr != 0) {
264b0663c33SRoger Pau Monné 		struct hvm_modlist_entry *mod;
265b6d85a5fSRoger Pau Monné 		const char *cmdline;
266b0663c33SRoger Pau Monné 
267b0663c33SRoger Pau Monné 		mod = (struct hvm_modlist_entry *)
268b0663c33SRoger Pau Monné 		    (start_info->modlist_paddr + KERNBASE);
269b6d85a5fSRoger Pau Monné 		cmdline = mod[0].cmdline_paddr ?
270b6d85a5fSRoger Pau Monné 		    (const char *)(mod[0].cmdline_paddr + KERNBASE) : NULL;
271b6d85a5fSRoger Pau Monné 
272b6d85a5fSRoger Pau Monné 		if (strcmp(cmdline, "header") == 0) {
273b6d85a5fSRoger Pau Monné 			struct xen_header *header;
274b6d85a5fSRoger Pau Monné 
275b6d85a5fSRoger Pau Monné 			header = (struct xen_header *)(mod[0].paddr + KERNBASE);
276b6d85a5fSRoger Pau Monné 
277b6d85a5fSRoger Pau Monné 			if ((header->flags & XENHEADER_HAS_MODULEP_OFFSET) !=
278b6d85a5fSRoger Pau Monné 			    XENHEADER_HAS_MODULEP_OFFSET) {
279b6d85a5fSRoger Pau Monné 				xc_printf("Unable to load module metadata\n");
280b6d85a5fSRoger Pau Monné 				HYPERVISOR_shutdown(SHUTDOWN_crash);
281b6d85a5fSRoger Pau Monné 			}
282b6d85a5fSRoger Pau Monné 
283b6d85a5fSRoger Pau Monné 			preload_metadata = (caddr_t)(mod[0].paddr +
284b6d85a5fSRoger Pau Monné 			    header->modulep_offset + KERNBASE);
285b6d85a5fSRoger Pau Monné 
286b6d85a5fSRoger Pau Monné 			kmdp = preload_search_by_type("elf kernel");
287b6d85a5fSRoger Pau Monné 			if (kmdp == NULL)
288b6d85a5fSRoger Pau Monné 				kmdp = preload_search_by_type("elf64 kernel");
289b6d85a5fSRoger Pau Monné 			if (kmdp == NULL) {
290b6d85a5fSRoger Pau Monné 				xc_printf("Unable to find kernel\n");
291b6d85a5fSRoger Pau Monné 				HYPERVISOR_shutdown(SHUTDOWN_crash);
292b6d85a5fSRoger Pau Monné 			}
293b6d85a5fSRoger Pau Monné 
294b6d85a5fSRoger Pau Monné 			/*
295b6d85a5fSRoger Pau Monné 			 * Xen has relocated the metadata and the modules, so
296b6d85a5fSRoger Pau Monné 			 * we need to recalculate it's position. This is done
297b6d85a5fSRoger Pau Monné 			 * by saving the original modulep address and then
298b6d85a5fSRoger Pau Monné 			 * calculating the offset from the real modulep
299b6d85a5fSRoger Pau Monné 			 * position.
300b6d85a5fSRoger Pau Monné 			 */
301b6d85a5fSRoger Pau Monné 			metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP,
302b6d85a5fSRoger Pau Monné 			    vm_paddr_t);
303b6d85a5fSRoger Pau Monné 			off = mod[0].paddr + header->modulep_offset - metadata +
304b6d85a5fSRoger Pau Monné 			    KERNBASE;
305b6d85a5fSRoger Pau Monné 		} else {
306b0663c33SRoger Pau Monné 			preload_metadata = (caddr_t)(mod[0].paddr + KERNBASE);
307b0663c33SRoger Pau Monné 
308b0663c33SRoger Pau Monné 			kmdp = preload_search_by_type("elf kernel");
309b0663c33SRoger Pau Monné 			if (kmdp == NULL)
310b0663c33SRoger Pau Monné 				kmdp = preload_search_by_type("elf64 kernel");
311b6d85a5fSRoger Pau Monné 			if (kmdp == NULL) {
312b6d85a5fSRoger Pau Monné 				xc_printf("Unable to find kernel\n");
313b6d85a5fSRoger Pau Monné 				HYPERVISOR_shutdown(SHUTDOWN_crash);
314b6d85a5fSRoger Pau Monné 			}
315b0663c33SRoger Pau Monné 
316b0663c33SRoger Pau Monné 			metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t);
317b0663c33SRoger Pau Monné 			off = mod[0].paddr + KERNBASE - metadata;
318b6d85a5fSRoger Pau Monné 		}
319b0663c33SRoger Pau Monné 
320b0663c33SRoger Pau Monné 		preload_bootstrap_relocate(off);
321b0663c33SRoger Pau Monné 
322b0663c33SRoger Pau Monné 		boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
323b0663c33SRoger Pau Monné 		envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
324b0663c33SRoger Pau Monné 		if (envp != NULL)
325b0663c33SRoger Pau Monné 			envp += off;
326*1b5e5ff6SRoger Pau Monné 		init_static_kenv(envp, 0);
327a2495c36SRoger Pau Monné 
328a2495c36SRoger Pau Monné 		if (MD_FETCH(kmdp, MODINFOMD_EFI_MAP, void *) != NULL)
329a2495c36SRoger Pau Monné 		    strlcpy(bootmethod, "UEFI", sizeof(bootmethod));
330a2495c36SRoger Pau Monné 		else
331a2495c36SRoger Pau Monné 		    strlcpy(bootmethod, "BIOS", sizeof(bootmethod));
332b0663c33SRoger Pau Monné 	} else {
333*1b5e5ff6SRoger Pau Monné 		static char kenv_buffer[PAGE_SIZE];
334*1b5e5ff6SRoger Pau Monné 
335*1b5e5ff6SRoger Pau Monné 		/* Provide a static kenv so the command line can be parsed. */
336*1b5e5ff6SRoger Pau Monné 		init_static_kenv(kenv_buffer, sizeof(kenv_buffer));
337*1b5e5ff6SRoger Pau Monné 
338b0663c33SRoger Pau Monné 		/* Parse the extra boot information given by Xen */
339b0663c33SRoger Pau Monné 		if (start_info->cmdline_paddr != 0)
340b0663c33SRoger Pau Monné 			boot_parse_cmdline_delim(
341b0663c33SRoger Pau Monné 			    (char *)(start_info->cmdline_paddr + KERNBASE),
342c4a4011cSColin Percival 			    ", \t\n");
343b0663c33SRoger Pau Monné 		kmdp = NULL;
34413f34e21SColin Percival 		strlcpy(bootmethod, "PVH", sizeof(bootmethod));
345b0663c33SRoger Pau Monné 	}
346b0663c33SRoger Pau Monné 
347b0663c33SRoger Pau Monné 	boothowto |= boot_env_to_howto();
348b0663c33SRoger Pau Monné 
349*1b5e5ff6SRoger Pau Monné 	/*
350*1b5e5ff6SRoger Pau Monné 	 * When booted as a PVH guest FreeBSD must not use the RSDP address
351*1b5e5ff6SRoger Pau Monné 	 * hint provided by the loader because it points to the native set of
352*1b5e5ff6SRoger Pau Monné 	 * ACPI tables instead of the ones crafted by Xen.
353*1b5e5ff6SRoger Pau Monné 	 */
354*1b5e5ff6SRoger Pau Monné 	acpi_set_root(start_info->rsdp_paddr);
355b0663c33SRoger Pau Monné 
356b0663c33SRoger Pau Monné #ifdef DDB
357b0663c33SRoger Pau Monné 	xen_pvh_parse_symtab();
358c98a2727SRoger Pau Monné #endif
3599d6ae1e3SColin Percival 	TSEXIT();
360ca49b334SRoger Pau Monné 	return (kmdp);
36197baeefdSRoger Pau Monné }
3621e69553eSRoger Pau Monné 
3631e69553eSRoger Pau Monné static void
pvh_parse_memmap_start_info(caddr_t kmdp,vm_paddr_t * physmap,int * physmap_idx)364023a025bSColin Percival pvh_parse_memmap_start_info(caddr_t kmdp, vm_paddr_t *physmap,
365023a025bSColin Percival     int *physmap_idx)
366023a025bSColin Percival {
367023a025bSColin Percival 	const struct hvm_memmap_table_entry * entries;
368023a025bSColin Percival 	size_t nentries;
369023a025bSColin Percival 	size_t i;
370023a025bSColin Percival 
371023a025bSColin Percival 	/* Extract from HVM start_info. */
372023a025bSColin Percival 	entries = (struct hvm_memmap_table_entry *)(start_info->memmap_paddr + KERNBASE);
373023a025bSColin Percival 	nentries = start_info->memmap_entries;
374023a025bSColin Percival 
375023a025bSColin Percival 	/* Convert into E820 format and handle one by one. */
376023a025bSColin Percival 	for (i = 0; i < nentries; i++) {
377023a025bSColin Percival 		struct bios_smap entry;
378023a025bSColin Percival 
379023a025bSColin Percival 		entry.base = entries[i].addr;
380023a025bSColin Percival 		entry.length = entries[i].size;
381023a025bSColin Percival 
382023a025bSColin Percival 		/*
383023a025bSColin Percival 		 * Luckily for us, the XEN_HVM_MEMMAP_TYPE_* values exactly
384023a025bSColin Percival 		 * match the SMAP_TYPE_* values so we don't need to translate
385023a025bSColin Percival 		 * anything here.
386023a025bSColin Percival 		 */
387023a025bSColin Percival 		entry.type = entries[i].type;
388023a025bSColin Percival 
389023a025bSColin Percival 		bios_add_smap_entries(&entry, 1, physmap, physmap_idx);
390023a025bSColin Percival 	}
391023a025bSColin Percival }
392023a025bSColin Percival 
393023a025bSColin Percival static void
xen_pvh_parse_memmap(caddr_t kmdp,vm_paddr_t * physmap,int * physmap_idx)394b0663c33SRoger Pau Monné xen_pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx)
3951e69553eSRoger Pau Monné {
3961e69553eSRoger Pau Monné 	struct xen_memory_map memmap;
3971e69553eSRoger Pau Monné 	u_int32_t size;
3981e69553eSRoger Pau Monné 	int rc;
3991e69553eSRoger Pau Monné 
400a8ea1540SColin Percival 	/* We should only reach here if we're running under Xen. */
401a8ea1540SColin Percival 	KASSERT(isxen(), ("xen_pvh_parse_memmap reached when !Xen"));
402a8ea1540SColin Percival 
4031e69553eSRoger Pau Monné 	/* Fetch the E820 map from Xen */
4041e69553eSRoger Pau Monné 	memmap.nr_entries = MAX_E820_ENTRIES;
4051e69553eSRoger Pau Monné 	set_xen_guest_handle(memmap.buffer, xen_smap);
4061e69553eSRoger Pau Monné 	rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
407b0663c33SRoger Pau Monné 	if (rc) {
408b0663c33SRoger Pau Monné 		xc_printf("ERROR: unable to fetch Xen E820 memory map: %d\n",
409b0663c33SRoger Pau Monné 		    rc);
410b0663c33SRoger Pau Monné 		HYPERVISOR_shutdown(SHUTDOWN_crash);
411b0663c33SRoger Pau Monné 	}
412b0663c33SRoger Pau Monné 
4131e69553eSRoger Pau Monné 	size = memmap.nr_entries * sizeof(xen_smap[0]);
4141e69553eSRoger Pau Monné 
4151e69553eSRoger Pau Monné 	bios_add_smap_entries(xen_smap, size, physmap, physmap_idx);
4161e69553eSRoger Pau Monné }
417023a025bSColin Percival 
418023a025bSColin Percival static void
pvh_parse_memmap(caddr_t kmdp,vm_paddr_t * physmap,int * physmap_idx)419023a025bSColin Percival pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx)
420023a025bSColin Percival {
421023a025bSColin Percival 
422023a025bSColin Percival 	/*
423023a025bSColin Percival 	 * If version >= 1 and memmap_paddr != 0, use the memory map provided
424023a025bSColin Percival 	 * in the start_info structure; if not, we're running under legacy
425023a025bSColin Percival 	 * Xen and need to use the Xen hypercall.
426023a025bSColin Percival 	 */
427023a025bSColin Percival 	if ((start_info->version >= 1) && (start_info->memmap_paddr != 0))
428023a025bSColin Percival 		pvh_parse_memmap_start_info(kmdp, physmap, physmap_idx);
429023a025bSColin Percival 	else
430023a025bSColin Percival 		xen_pvh_parse_memmap(kmdp, physmap, physmap_idx);
431023a025bSColin Percival }
432