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