xref: /freebsd/sys/x86/xen/pv.c (revision 19261079)
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 
78 #include <xen/interface/arch-x86/hvm/start_info.h>
79 #include <xen/interface/vcpu.h>
80 
81 #include <dev/xen/timer/timer.h>
82 
83 #ifdef DDB
84 #include <ddb/ddb.h>
85 #endif
86 
87 /* Native initial function */
88 extern u_int64_t hammer_time(u_int64_t, u_int64_t);
89 /* Xen initial function */
90 uint64_t hammer_time_xen(vm_paddr_t);
91 
92 #define MAX_E820_ENTRIES	128
93 
94 /*--------------------------- Forward Declarations ---------------------------*/
95 static caddr_t xen_pvh_parse_preload_data(uint64_t);
96 static void xen_pvh_parse_memmap(caddr_t, vm_paddr_t *, int *);
97 
98 /*---------------------------- Extern Declarations ---------------------------*/
99 /*
100  * Placed by the linker at the end of the bss section, which is the last
101  * section loaded by Xen before loading the symtab and strtab.
102  */
103 extern uint32_t end;
104 
105 /*-------------------------------- Global Data -------------------------------*/
106 struct init_ops xen_pvh_init_ops = {
107 	.parse_preload_data		= xen_pvh_parse_preload_data,
108 	.early_clock_source_init	= xen_clock_init,
109 	.early_delay			= xen_delay,
110 	.parse_memmap			= xen_pvh_parse_memmap,
111 };
112 
113 static struct bios_smap xen_smap[MAX_E820_ENTRIES];
114 
115 static struct hvm_start_info *start_info;
116 
117 /*-------------------------------- Xen PV init -------------------------------*/
118 
119 uint64_t
120 hammer_time_xen(vm_paddr_t start_info_paddr)
121 {
122 	struct hvm_modlist_entry *mod;
123 	struct xen_add_to_physmap xatp;
124 	uint64_t physfree;
125 	char *kenv;
126 	int rc;
127 
128 	xen_domain_type = XEN_HVM_DOMAIN;
129 	vm_guest = VM_GUEST_XEN;
130 
131 	rc = xen_hvm_init_hypercall_stubs(XEN_HVM_INIT_EARLY);
132 	if (rc) {
133 		xc_printf("ERROR: failed to initialize hypercall page: %d\n",
134 		    rc);
135 		HYPERVISOR_shutdown(SHUTDOWN_crash);
136 	}
137 
138 	start_info = (struct hvm_start_info *)(start_info_paddr + KERNBASE);
139 	if (start_info->magic != XEN_HVM_START_MAGIC_VALUE) {
140 		xc_printf("Unknown magic value in start_info struct: %#x\n",
141 		    start_info->magic);
142 		HYPERVISOR_shutdown(SHUTDOWN_crash);
143 	}
144 
145 	/*
146 	 * The hvm_start_into structure is always appended after loading
147 	 * the kernel and modules.
148 	 */
149 	physfree = roundup2(start_info_paddr + PAGE_SIZE, PAGE_SIZE);
150 
151 	xatp.domid = DOMID_SELF;
152 	xatp.idx = 0;
153 	xatp.space = XENMAPSPACE_shared_info;
154 	xatp.gpfn = atop(physfree);
155 	if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) {
156 		xc_printf("ERROR: failed to setup shared_info page\n");
157 		HYPERVISOR_shutdown(SHUTDOWN_crash);
158 	}
159 	HYPERVISOR_shared_info = (shared_info_t *)(physfree + KERNBASE);
160 	physfree += PAGE_SIZE;
161 
162 	/*
163 	 * Init a static kenv using a free page. The contents will be filled
164 	 * from the parse_preload_data hook.
165 	 */
166 	kenv = (void *)(physfree + KERNBASE);
167 	physfree += PAGE_SIZE;
168 	bzero_early(kenv, PAGE_SIZE);
169 	init_static_kenv(kenv, PAGE_SIZE);
170 
171 	if (start_info->modlist_paddr != 0) {
172 		if (start_info->modlist_paddr >= physfree) {
173 			xc_printf(
174 			    "ERROR: unexpected module list memory address\n");
175 			HYPERVISOR_shutdown(SHUTDOWN_crash);
176 		}
177 		if (start_info->nr_modules == 0) {
178 			xc_printf(
179 			    "ERROR: modlist_paddr != 0 but nr_modules == 0\n");
180 			HYPERVISOR_shutdown(SHUTDOWN_crash);
181 		}
182 		mod = (struct hvm_modlist_entry *)
183 		    (start_info->modlist_paddr + KERNBASE);
184 		if (mod[0].paddr >= physfree) {
185 			xc_printf("ERROR: unexpected module memory address\n");
186 			HYPERVISOR_shutdown(SHUTDOWN_crash);
187 		}
188 	}
189 
190 	/* Set the hooks for early functions that diverge from bare metal */
191 	init_ops = xen_pvh_init_ops;
192 	hvm_start_flags = start_info->flags;
193 
194 	/* Now we can jump into the native init function */
195 	return (hammer_time(0, physfree));
196 }
197 
198 /*-------------------------------- PV specific -------------------------------*/
199 
200 /*
201  * When booted as a PVH guest FreeBSD needs to avoid using the RSDP address
202  * hint provided by the loader because it points to the native set of ACPI
203  * tables instead of the ones crafted by Xen. The acpi.rsdp env variable is
204  * removed from kenv if present, and a new acpi.rsdp is added to kenv that
205  * points to the address of the Xen crafted RSDP.
206  */
207 static bool reject_option(const char *option)
208 {
209 	static const char *reject[] = {
210 		"acpi.rsdp",
211 	};
212 	unsigned int i;
213 
214 	for (i = 0; i < nitems(reject); i++)
215 		if (strncmp(option, reject[i], strlen(reject[i])) == 0)
216 			return (true);
217 
218 	return (false);
219 }
220 
221 static void
222 xen_pvh_set_env(char *env, bool (*filter)(const char *))
223 {
224 	char *option;
225 
226 	if (env == NULL)
227 		return;
228 
229 	option = env;
230 	while (*option != 0) {
231 		char *value;
232 
233 		if (filter != NULL && filter(option)) {
234 			option += strlen(option) + 1;
235 			continue;
236 		}
237 
238 		value = option;
239 		option = strsep(&value, "=");
240 		if (kern_setenv(option, value) != 0)
241 			xc_printf("unable to add kenv %s=%s\n", option, value);
242 		option = value + strlen(value) + 1;
243 	}
244 }
245 
246 #ifdef DDB
247 /*
248  * The way Xen loads the symtab is different from the native boot loader,
249  * because it's tailored for NetBSD. So we have to adapt and use the same
250  * method as NetBSD. Portions of the code below have been picked from NetBSD:
251  * sys/kern/kern_ksyms.c CVS Revision 1.71.
252  */
253 static void
254 xen_pvh_parse_symtab(void)
255 {
256 	Elf_Ehdr *ehdr;
257 	Elf_Shdr *shdr;
258 	uint32_t size;
259 	int i, j;
260 
261 	size = end;
262 
263 	ehdr = (Elf_Ehdr *)(&end + 1);
264 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
265 	    ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
266 	    ehdr->e_version > 1) {
267 		xc_printf("Unable to load ELF symtab: invalid symbol table\n");
268 		return;
269 	}
270 
271 	shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff);
272 	/* Find the symbol table and the corresponding string table. */
273 	for (i = 1; i < ehdr->e_shnum; i++) {
274 		if (shdr[i].sh_type != SHT_SYMTAB)
275 			continue;
276 		if (shdr[i].sh_offset == 0)
277 			continue;
278 		ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset);
279 		ksymtab_size = shdr[i].sh_size;
280 		j = shdr[i].sh_link;
281 		if (shdr[j].sh_offset == 0)
282 			continue; /* Can this happen? */
283 		kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset);
284 		break;
285 	}
286 
287 	if (ksymtab == 0 || kstrtab == 0)
288 		xc_printf(
289     "Unable to load ELF symtab: could not find symtab or strtab\n");
290 }
291 #endif
292 
293 static caddr_t
294 xen_pvh_parse_preload_data(uint64_t modulep)
295 {
296 	caddr_t kmdp;
297 	vm_ooffset_t off;
298 	vm_paddr_t metadata;
299 	char *envp;
300 	char acpi_rsdp[19];
301 
302 	if (start_info->modlist_paddr != 0) {
303 		struct hvm_modlist_entry *mod;
304 		const char *cmdline;
305 
306 		mod = (struct hvm_modlist_entry *)
307 		    (start_info->modlist_paddr + KERNBASE);
308 		cmdline = mod[0].cmdline_paddr ?
309 		    (const char *)(mod[0].cmdline_paddr + KERNBASE) : NULL;
310 
311 		if (strcmp(cmdline, "header") == 0) {
312 			struct xen_header *header;
313 
314 			header = (struct xen_header *)(mod[0].paddr + KERNBASE);
315 
316 			if ((header->flags & XENHEADER_HAS_MODULEP_OFFSET) !=
317 			    XENHEADER_HAS_MODULEP_OFFSET) {
318 				xc_printf("Unable to load module metadata\n");
319 				HYPERVISOR_shutdown(SHUTDOWN_crash);
320 			}
321 
322 			preload_metadata = (caddr_t)(mod[0].paddr +
323 			    header->modulep_offset + KERNBASE);
324 
325 			kmdp = preload_search_by_type("elf kernel");
326 			if (kmdp == NULL)
327 				kmdp = preload_search_by_type("elf64 kernel");
328 			if (kmdp == NULL) {
329 				xc_printf("Unable to find kernel\n");
330 				HYPERVISOR_shutdown(SHUTDOWN_crash);
331 			}
332 
333 			/*
334 			 * Xen has relocated the metadata and the modules, so
335 			 * we need to recalculate it's position. This is done
336 			 * by saving the original modulep address and then
337 			 * calculating the offset from the real modulep
338 			 * position.
339 			 */
340 			metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP,
341 			    vm_paddr_t);
342 			off = mod[0].paddr + header->modulep_offset - metadata +
343 			    KERNBASE;
344 		} else {
345 			preload_metadata = (caddr_t)(mod[0].paddr + KERNBASE);
346 
347 			kmdp = preload_search_by_type("elf kernel");
348 			if (kmdp == NULL)
349 				kmdp = preload_search_by_type("elf64 kernel");
350 			if (kmdp == NULL) {
351 				xc_printf("Unable to find kernel\n");
352 				HYPERVISOR_shutdown(SHUTDOWN_crash);
353 			}
354 
355 			metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t);
356 			off = mod[0].paddr + KERNBASE - metadata;
357 		}
358 
359 		preload_bootstrap_relocate(off);
360 
361 		boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
362 		envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
363 		if (envp != NULL)
364 			envp += off;
365 		xen_pvh_set_env(envp, reject_option);
366 
367 		if (MD_FETCH(kmdp, MODINFOMD_EFI_MAP, void *) != NULL)
368 		    strlcpy(bootmethod, "UEFI", sizeof(bootmethod));
369 		else
370 		    strlcpy(bootmethod, "BIOS", sizeof(bootmethod));
371 	} else {
372 		/* Parse the extra boot information given by Xen */
373 		if (start_info->cmdline_paddr != 0)
374 			boot_parse_cmdline_delim(
375 			    (char *)(start_info->cmdline_paddr + KERNBASE),
376 			    ",");
377 		kmdp = NULL;
378 		strlcpy(bootmethod, "XEN", sizeof(bootmethod));
379 	}
380 
381 	boothowto |= boot_env_to_howto();
382 
383 	snprintf(acpi_rsdp, sizeof(acpi_rsdp), "%#" PRIx64,
384 	    start_info->rsdp_paddr);
385 	kern_setenv("acpi.rsdp", acpi_rsdp);
386 
387 #ifdef DDB
388 	xen_pvh_parse_symtab();
389 #endif
390 	return (kmdp);
391 }
392 
393 static void
394 xen_pvh_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx)
395 {
396 	struct xen_memory_map memmap;
397 	u_int32_t size;
398 	int rc;
399 
400 	/* Fetch the E820 map from Xen */
401 	memmap.nr_entries = MAX_E820_ENTRIES;
402 	set_xen_guest_handle(memmap.buffer, xen_smap);
403 	rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
404 	if (rc) {
405 		xc_printf("ERROR: unable to fetch Xen E820 memory map: %d\n",
406 		    rc);
407 		HYPERVISOR_shutdown(SHUTDOWN_crash);
408 	}
409 
410 	size = memmap.nr_entries * sizeof(xen_smap[0]);
411 
412 	bios_add_smap_entries(xen_smap, size, physmap, physmap_idx);
413 }
414