xref: /freebsd/sys/x86/xen/pv.c (revision a0409676120c1e558d0ade943019934e0f15118d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
3  *
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>
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #include "opt_ddb.h"
36 #include "opt_kstack_pages.h"
37 
38 #include <sys/param.h>
39 #include <sys/bus.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>
45 #include <sys/lock.h>
46 #include <sys/rwlock.h>
47 #include <sys/boot.h>
48 #include <sys/ctype.h>
49 #include <sys/mutex.h>
50 #include <sys/smp.h>
51 #include <sys/efi.h>
52 
53 #include <vm/vm.h>
54 #include <vm/vm_extern.h>
55 #include <vm/vm_kern.h>
56 #include <vm/vm_page.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_object.h>
59 #include <vm/vm_pager.h>
60 #include <vm/vm_param.h>
61 
62 #include <machine/_inttypes.h>
63 #include <machine/intr_machdep.h>
64 #include <x86/apicvar.h>
65 #include <x86/init.h>
66 #include <machine/pc/bios.h>
67 #include <machine/smp.h>
68 #include <machine/intr_machdep.h>
69 #include <machine/md_var.h>
70 #include <machine/metadata.h>
71 
72 #include <xen/xen-os.h>
73 #include <xen/hvm.h>
74 #include <xen/hypervisor.h>
75 #include <xen/xenstore/xenstorevar.h>
76 #include <xen/xen_pv.h>
77 #include <xen/xen_msi.h>
78 
79 #include <xen/interface/arch-x86/hvm/start_info.h>
80 #include <xen/interface/vcpu.h>
81 
82 #include <dev/xen/timer/timer.h>
83 
84 #ifdef DDB
85 #include <ddb/ddb.h>
86 #endif
87 
88 /* Native initial function */
89 extern u_int64_t hammer_time(u_int64_t, u_int64_t);
90 /* Xen initial function */
91 uint64_t hammer_time_xen_legacy(start_info_t *, uint64_t);
92 uint64_t hammer_time_xen(vm_paddr_t);
93 
94 #define MAX_E820_ENTRIES	128
95 
96 /*--------------------------- Forward Declarations ---------------------------*/
97 static caddr_t xen_legacy_pvh_parse_preload_data(uint64_t);
98 static caddr_t xen_pvh_parse_preload_data(uint64_t);
99 static void xen_pvh_parse_memmap(caddr_t, vm_paddr_t *, int *);
100 
101 #ifdef SMP
102 static int xen_pv_start_all_aps(void);
103 #endif
104 
105 /*---------------------------- Extern Declarations ---------------------------*/
106 #ifdef SMP
107 /* Variables used by amd64 mp_machdep to start APs */
108 extern char *doublefault_stack;
109 extern char *mce_stack;
110 extern char *nmi_stack;
111 extern char *dbg_stack;
112 #endif
113 
114 /*
115  * Placed by the linker at the end of the bss section, which is the last
116  * section loaded by Xen before loading the symtab and strtab.
117  */
118 extern uint32_t end;
119 
120 /*-------------------------------- Global Data -------------------------------*/
121 /* Xen init_ops implementation. */
122 struct init_ops xen_legacy_init_ops = {
123 	.parse_preload_data		= xen_legacy_pvh_parse_preload_data,
124 	.early_clock_source_init	= xen_clock_init,
125 	.early_delay			= xen_delay,
126 	.parse_memmap			= xen_pvh_parse_memmap,
127 #ifdef SMP
128 	.start_all_aps			= xen_pv_start_all_aps,
129 #endif
130 	.msi_init			= xen_msi_init,
131 };
132 
133 struct init_ops xen_pvh_init_ops = {
134 	.parse_preload_data		= xen_pvh_parse_preload_data,
135 	.early_clock_source_init	= xen_clock_init,
136 	.early_delay			= xen_delay,
137 	.parse_memmap			= xen_pvh_parse_memmap,
138 #ifdef SMP
139 	.mp_bootaddress			= mp_bootaddress,
140 	.start_all_aps			= native_start_all_aps,
141 #endif
142 	.msi_init			= msi_init,
143 };
144 
145 static struct bios_smap xen_smap[MAX_E820_ENTRIES];
146 
147 static start_info_t *legacy_start_info;
148 static struct hvm_start_info *start_info;
149 
150 /*----------------------- Legacy PVH start_info accessors --------------------*/
151 static vm_paddr_t
152 legacy_get_xenstore_mfn(void)
153 {
154 
155 	return (legacy_start_info->store_mfn);
156 }
157 
158 static evtchn_port_t
159 legacy_get_xenstore_evtchn(void)
160 {
161 
162 	return (legacy_start_info->store_evtchn);
163 }
164 
165 static vm_paddr_t
166 legacy_get_console_mfn(void)
167 {
168 
169 	return (legacy_start_info->console.domU.mfn);
170 }
171 
172 static evtchn_port_t
173 legacy_get_console_evtchn(void)
174 {
175 
176 	return (legacy_start_info->console.domU.evtchn);
177 }
178 
179 static uint32_t
180 legacy_get_start_flags(void)
181 {
182 
183 	return (legacy_start_info->flags);
184 }
185 
186 struct hypervisor_info legacy_info = {
187 	.get_xenstore_mfn		= legacy_get_xenstore_mfn,
188 	.get_xenstore_evtchn		= legacy_get_xenstore_evtchn,
189 	.get_console_mfn		= legacy_get_console_mfn,
190 	.get_console_evtchn		= legacy_get_console_evtchn,
191 	.get_start_flags		= legacy_get_start_flags,
192 };
193 
194 /*-------------------------------- Xen PV init -------------------------------*/
195 /*
196  * First function called by the Xen legacy PVH boot sequence.
197  *
198  * Set some Xen global variables and prepare the environment so it is
199  * as similar as possible to what native FreeBSD init function expects.
200  */
201 uint64_t
202 hammer_time_xen_legacy(start_info_t *si, uint64_t xenstack)
203 {
204 	uint64_t physfree;
205 	uint64_t *PT4 = (u_int64_t *)xenstack;
206 	uint64_t *PT3 = (u_int64_t *)(xenstack + PAGE_SIZE);
207 	uint64_t *PT2 = (u_int64_t *)(xenstack + 2 * PAGE_SIZE);
208 	int i;
209 	char *kenv;
210 
211 	xen_domain_type = XEN_PV_DOMAIN;
212 	vm_guest = VM_GUEST_XEN;
213 
214 	if ((si == NULL) || (xenstack == 0)) {
215 		xc_printf("ERROR: invalid start_info or xen stack, halting\n");
216 		HYPERVISOR_shutdown(SHUTDOWN_crash);
217 	}
218 
219 	xc_printf("FreeBSD PVH running on %s\n", si->magic);
220 
221 	/* We use 3 pages of xen stack for the boot pagetables */
222 	physfree = xenstack + 3 * PAGE_SIZE - KERNBASE;
223 
224 	/* Setup Xen global variables */
225 	legacy_start_info = si;
226 	HYPERVISOR_shared_info =
227 	    (shared_info_t *)(si->shared_info + KERNBASE);
228 
229 	/*
230 	 * Use the stack Xen gives us to build the page tables
231 	 * as native FreeBSD expects to find them (created
232 	 * by the boot trampoline).
233 	 */
234 	for (i = 0; i < (PAGE_SIZE / sizeof(uint64_t)); i++) {
235 		/*
236 		 * Each slot of the level 4 pages points
237 		 * to the same level 3 page
238 		 */
239 		PT4[i] = ((uint64_t)&PT3[0]) - KERNBASE;
240 		PT4[i] |= PG_V | PG_RW | PG_U;
241 
242 		/*
243 		 * Each slot of the level 3 pages points
244 		 * to the same level 2 page
245 		 */
246 		PT3[i] = ((uint64_t)&PT2[0]) - KERNBASE;
247 		PT3[i] |= PG_V | PG_RW | PG_U;
248 
249 		/*
250 		 * The level 2 page slots are mapped with
251 		 * 2MB pages for 1GB.
252 		 */
253 		PT2[i] = i * (2 * 1024 * 1024);
254 		PT2[i] |= PG_V | PG_RW | PG_PS | PG_U;
255 	}
256 	load_cr3(((uint64_t)&PT4[0]) - KERNBASE);
257 
258 	/*
259 	 * Init an empty static kenv using a free page. The contents will be
260 	 * filled from the parse_preload_data hook.
261 	 */
262 	kenv = (void *)(physfree + KERNBASE);
263 	physfree += PAGE_SIZE;
264 	bzero_early(kenv, PAGE_SIZE);
265 	init_static_kenv(kenv, PAGE_SIZE);
266 
267 	/* Set the hooks for early functions that diverge from bare metal */
268 	init_ops = xen_legacy_init_ops;
269 	apic_ops = xen_apic_ops;
270 	hypervisor_info = legacy_info;
271 
272 	/* Now we can jump into the native init function */
273 	return (hammer_time(0, physfree));
274 }
275 
276 uint64_t
277 hammer_time_xen(vm_paddr_t start_info_paddr)
278 {
279 	struct hvm_modlist_entry *mod;
280 	struct xen_add_to_physmap xatp;
281 	uint64_t physfree;
282 	char *kenv;
283 	int rc;
284 
285 	xen_domain_type = XEN_HVM_DOMAIN;
286 	vm_guest = VM_GUEST_XEN;
287 
288 	rc = xen_hvm_init_hypercall_stubs(XEN_HVM_INIT_EARLY);
289 	if (rc) {
290 		xc_printf("ERROR: failed to initialize hypercall page: %d\n",
291 		    rc);
292 		HYPERVISOR_shutdown(SHUTDOWN_crash);
293 	}
294 
295 	start_info = (struct hvm_start_info *)(start_info_paddr + KERNBASE);
296 	if (start_info->magic != XEN_HVM_START_MAGIC_VALUE) {
297 		xc_printf("Unknown magic value in start_info struct: %#x\n",
298 		    start_info->magic);
299 		HYPERVISOR_shutdown(SHUTDOWN_crash);
300 	}
301 
302 	/*
303 	 * The hvm_start_into structure is always appended after loading
304 	 * the kernel and modules.
305 	 */
306 	physfree = roundup2(start_info_paddr + PAGE_SIZE, PAGE_SIZE);
307 
308 	xatp.domid = DOMID_SELF;
309 	xatp.idx = 0;
310 	xatp.space = XENMAPSPACE_shared_info;
311 	xatp.gpfn = atop(physfree);
312 	if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) {
313 		xc_printf("ERROR: failed to setup shared_info page\n");
314 		HYPERVISOR_shutdown(SHUTDOWN_crash);
315 	}
316 	HYPERVISOR_shared_info = (shared_info_t *)(physfree + KERNBASE);
317 	physfree += PAGE_SIZE;
318 
319 	/*
320 	 * Init a static kenv using a free page. The contents will be filled
321 	 * from the parse_preload_data hook.
322 	 */
323 	kenv = (void *)(physfree + KERNBASE);
324 	physfree += PAGE_SIZE;
325 	bzero_early(kenv, PAGE_SIZE);
326 	init_static_kenv(kenv, PAGE_SIZE);
327 
328 	if (start_info->modlist_paddr != 0) {
329 		if (start_info->modlist_paddr >= physfree) {
330 			xc_printf(
331 			    "ERROR: unexpected module list memory address\n");
332 			HYPERVISOR_shutdown(SHUTDOWN_crash);
333 		}
334 		if (start_info->nr_modules == 0) {
335 			xc_printf(
336 			    "ERROR: modlist_paddr != 0 but nr_modules == 0\n");
337 			HYPERVISOR_shutdown(SHUTDOWN_crash);
338 		}
339 		mod = (struct hvm_modlist_entry *)
340 		    (vm_paddr_t)start_info->modlist_paddr + KERNBASE;
341 		if (mod[0].paddr >= physfree) {
342 			xc_printf("ERROR: unexpected module memory address\n");
343 			HYPERVISOR_shutdown(SHUTDOWN_crash);
344 		}
345 	}
346 
347 	/* Set the hooks for early functions that diverge from bare metal */
348 	init_ops = xen_pvh_init_ops;
349 	hvm_start_flags = start_info->flags;
350 
351 	/* Now we can jump into the native init function */
352 	return (hammer_time(0, physfree));
353 }
354 
355 /*-------------------------------- PV specific -------------------------------*/
356 #ifdef SMP
357 static bool
358 start_xen_ap(int cpu)
359 {
360 	struct vcpu_guest_context *ctxt;
361 	int ms, cpus = mp_naps;
362 	const size_t stacksize = kstack_pages * PAGE_SIZE;
363 
364 	/* allocate and set up an idle stack data page */
365 	bootstacks[cpu] = (void *)kmem_malloc(stacksize, M_WAITOK | M_ZERO);
366 	doublefault_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
367 	mce_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
368 	nmi_stack = (char *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
369 	dbg_stack = (void *)kmem_malloc(PAGE_SIZE, M_WAITOK | M_ZERO);
370 	dpcpu = (void *)kmem_malloc(DPCPU_SIZE, M_WAITOK | M_ZERO);
371 
372 	bootSTK = (char *)bootstacks[cpu] + kstack_pages * PAGE_SIZE - 8;
373 	bootAP = cpu;
374 
375 	ctxt = malloc(sizeof(*ctxt), M_TEMP, M_WAITOK | M_ZERO);
376 
377 	ctxt->flags = VGCF_IN_KERNEL;
378 	ctxt->user_regs.rip = (unsigned long) init_secondary;
379 	ctxt->user_regs.rsp = (unsigned long) bootSTK;
380 
381 	/* Set the AP to use the same page tables */
382 	ctxt->ctrlreg[3] = KPML4phys;
383 
384 	if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
385 		panic("unable to initialize AP#%d", cpu);
386 
387 	free(ctxt, M_TEMP);
388 
389 	/* Launch the vCPU */
390 	if (HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
391 		panic("unable to start AP#%d", cpu);
392 
393 	/* Wait up to 5 seconds for it to start. */
394 	for (ms = 0; ms < 5000; ms++) {
395 		if (mp_naps > cpus)
396 			return (true);
397 		DELAY(1000);
398 	}
399 
400 	return (false);
401 }
402 
403 static int
404 xen_pv_start_all_aps(void)
405 {
406 	int cpu;
407 
408 	mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
409 
410 	for (cpu = 1; cpu < mp_ncpus; cpu++) {
411 		/* attempt to start the Application Processor */
412 		if (!start_xen_ap(cpu))
413 			panic("AP #%d failed to start!", cpu);
414 
415 		CPU_SET(cpu, &all_cpus);	/* record AP in CPU map */
416 	}
417 
418 	return (mp_naps);
419 }
420 #endif /* SMP */
421 
422 /*
423  * When booted as a PVH guest FreeBSD needs to avoid using the RSDP address
424  * hint provided by the loader because it points to the native set of ACPI
425  * tables instead of the ones crafted by Xen. The acpi.rsdp env variable is
426  * removed from kenv if present, and a new acpi.rsdp is added to kenv that
427  * points to the address of the Xen crafted RSDP.
428  */
429 static bool reject_option(const char *option)
430 {
431 	static const char *reject[] = {
432 		"acpi.rsdp",
433 	};
434 	unsigned int i;
435 
436 	for (i = 0; i < nitems(reject); i++)
437 		if (strncmp(option, reject[i], strlen(reject[i])) == 0)
438 			return (true);
439 
440 	return (false);
441 }
442 
443 static void
444 xen_pvh_set_env(char *env, bool (*filter)(const char *))
445 {
446 	char *option;
447 
448 	if (env == NULL)
449 		return;
450 
451 	option = env;
452 	while (*option != 0) {
453 		char *value;
454 
455 		if (filter != NULL && filter(option)) {
456 			option += strlen(option) + 1;
457 			continue;
458 		}
459 
460 		value = option;
461 		option = strsep(&value, "=");
462 		if (kern_setenv(option, value) != 0)
463 			xc_printf("unable to add kenv %s=%s\n", option, value);
464 		option = value + strlen(value) + 1;
465 	}
466 }
467 
468 #ifdef DDB
469 /*
470  * The way Xen loads the symtab is different from the native boot loader,
471  * because it's tailored for NetBSD. So we have to adapt and use the same
472  * method as NetBSD. Portions of the code below have been picked from NetBSD:
473  * sys/kern/kern_ksyms.c CVS Revision 1.71.
474  */
475 static void
476 xen_pvh_parse_symtab(void)
477 {
478 	Elf_Ehdr *ehdr;
479 	Elf_Shdr *shdr;
480 	uint32_t size;
481 	int i, j;
482 
483 	size = end;
484 
485 	ehdr = (Elf_Ehdr *)(&end + 1);
486 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
487 	    ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
488 	    ehdr->e_version > 1) {
489 		xc_printf("Unable to load ELF symtab: invalid symbol table\n");
490 		return;
491 	}
492 
493 	shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff);
494 	/* Find the symbol table and the corresponding string table. */
495 	for (i = 1; i < ehdr->e_shnum; i++) {
496 		if (shdr[i].sh_type != SHT_SYMTAB)
497 			continue;
498 		if (shdr[i].sh_offset == 0)
499 			continue;
500 		ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset);
501 		ksymtab_size = shdr[i].sh_size;
502 		j = shdr[i].sh_link;
503 		if (shdr[j].sh_offset == 0)
504 			continue; /* Can this happen? */
505 		kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset);
506 		break;
507 	}
508 
509 	if (ksymtab == 0 || kstrtab == 0)
510 		xc_printf(
511     "Unable to load ELF symtab: could not find symtab or strtab\n");
512 }
513 #endif
514 
515 static caddr_t
516 xen_legacy_pvh_parse_preload_data(uint64_t modulep)
517 {
518 	caddr_t		 kmdp;
519 	vm_ooffset_t	 off;
520 	vm_paddr_t	 metadata;
521 	char             *envp;
522 
523 	if (legacy_start_info->mod_start != 0) {
524 		preload_metadata = (caddr_t)legacy_start_info->mod_start;
525 
526 		kmdp = preload_search_by_type("elf kernel");
527 		if (kmdp == NULL)
528 			kmdp = preload_search_by_type("elf64 kernel");
529 		KASSERT(kmdp != NULL, ("unable to find kernel"));
530 
531 		/*
532 		 * Xen has relocated the metadata and the modules,
533 		 * so we need to recalculate it's position. This is
534 		 * done by saving the original modulep address and
535 		 * then calculating the offset with mod_start,
536 		 * which contains the relocated modulep address.
537 		 */
538 		metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t);
539 		off = legacy_start_info->mod_start - metadata;
540 
541 		preload_bootstrap_relocate(off);
542 
543 		boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
544 		envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
545 		if (envp != NULL)
546 			envp += off;
547 		xen_pvh_set_env(envp, NULL);
548 	} else {
549 		/* Parse the extra boot information given by Xen */
550 		boot_parse_cmdline_delim(legacy_start_info->cmd_line, ",");
551 		kmdp = NULL;
552 	}
553 
554 	boothowto |= boot_env_to_howto();
555 
556 #ifdef DDB
557 	xen_pvh_parse_symtab();
558 #endif
559 	return (kmdp);
560 }
561 
562 static caddr_t
563 xen_pvh_parse_preload_data(uint64_t modulep)
564 {
565 	caddr_t kmdp;
566 	vm_ooffset_t off;
567 	vm_paddr_t metadata;
568 	char *envp;
569 	char acpi_rsdp[19];
570 
571 	if (start_info->modlist_paddr != 0) {
572 		struct hvm_modlist_entry *mod;
573 		const char *cmdline;
574 
575 		mod = (struct hvm_modlist_entry *)
576 		    (start_info->modlist_paddr + KERNBASE);
577 		cmdline = mod[0].cmdline_paddr ?
578 		    (const char *)(mod[0].cmdline_paddr + KERNBASE) : NULL;
579 
580 		if (strcmp(cmdline, "header") == 0) {
581 			struct xen_header *header;
582 
583 			header = (struct xen_header *)(mod[0].paddr + KERNBASE);
584 
585 			if ((header->flags & XENHEADER_HAS_MODULEP_OFFSET) !=
586 			    XENHEADER_HAS_MODULEP_OFFSET) {
587 				xc_printf("Unable to load module metadata\n");
588 				HYPERVISOR_shutdown(SHUTDOWN_crash);
589 			}
590 
591 			preload_metadata = (caddr_t)(mod[0].paddr +
592 			    header->modulep_offset + KERNBASE);
593 
594 			kmdp = preload_search_by_type("elf kernel");
595 			if (kmdp == NULL)
596 				kmdp = preload_search_by_type("elf64 kernel");
597 			if (kmdp == NULL) {
598 				xc_printf("Unable to find kernel\n");
599 				HYPERVISOR_shutdown(SHUTDOWN_crash);
600 			}
601 
602 			/*
603 			 * Xen has relocated the metadata and the modules, so
604 			 * we need to recalculate it's position. This is done
605 			 * by saving the original modulep address and then
606 			 * calculating the offset from the real modulep
607 			 * position.
608 			 */
609 			metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP,
610 			    vm_paddr_t);
611 			off = mod[0].paddr + header->modulep_offset - metadata +
612 			    KERNBASE;
613 		} else {
614 			preload_metadata = (caddr_t)(mod[0].paddr + KERNBASE);
615 
616 			kmdp = preload_search_by_type("elf kernel");
617 			if (kmdp == NULL)
618 				kmdp = preload_search_by_type("elf64 kernel");
619 			if (kmdp == NULL) {
620 				xc_printf("Unable to find kernel\n");
621 				HYPERVISOR_shutdown(SHUTDOWN_crash);
622 			}
623 
624 			metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t);
625 			off = mod[0].paddr + KERNBASE - metadata;
626 		}
627 
628 		preload_bootstrap_relocate(off);
629 
630 		boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
631 		envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
632 		if (envp != NULL)
633 			envp += off;
634 		xen_pvh_set_env(envp, reject_option);
635 
636 		if (MD_FETCH(kmdp, MODINFOMD_EFI_MAP, void *) != NULL)
637 		    strlcpy(bootmethod, "UEFI", sizeof(bootmethod));
638 		else
639 		    strlcpy(bootmethod, "BIOS", sizeof(bootmethod));
640 	} else {
641 		/* Parse the extra boot information given by Xen */
642 		if (start_info->cmdline_paddr != 0)
643 			boot_parse_cmdline_delim(
644 			    (char *)(start_info->cmdline_paddr + KERNBASE),
645 			    ",");
646 		kmdp = NULL;
647 		strlcpy(bootmethod, "XEN", sizeof(bootmethod));
648 	}
649 
650 	boothowto |= boot_env_to_howto();
651 
652 	snprintf(acpi_rsdp, sizeof(acpi_rsdp), "%#" PRIx64,
653 	    start_info->rsdp_paddr);
654 	kern_setenv("acpi.rsdp", acpi_rsdp);
655 
656 #ifdef DDB
657 	xen_pvh_parse_symtab();
658 #endif
659 	return (kmdp);
660 }
661 
662 static void
663 xen_pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx)
664 {
665 	struct xen_memory_map memmap;
666 	u_int32_t size;
667 	int rc;
668 
669 	/* Fetch the E820 map from Xen */
670 	memmap.nr_entries = MAX_E820_ENTRIES;
671 	set_xen_guest_handle(memmap.buffer, xen_smap);
672 	rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
673 	if (rc) {
674 		xc_printf("ERROR: unable to fetch Xen E820 memory map: %d\n",
675 		    rc);
676 		HYPERVISOR_shutdown(SHUTDOWN_crash);
677 	}
678 
679 	size = memmap.nr_entries * sizeof(xen_smap[0]);
680 
681 	bios_add_smap_entries(xen_smap, size, physmap, physmap_idx);
682 }
683