xref: /freebsd/sys/x86/xen/pv.c (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
1 /*
2  * Copyright (c) 2004 Christian Limpach.
3  * Copyright (c) 2004-2006,2008 Kip Macy
4  * Copyright (c) 2013 Roger Pau Monné <roger.pau@citrix.com>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/reboot.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/lock.h>
39 #include <sys/rwlock.h>
40 #include <sys/boot.h>
41 #include <sys/ctype.h>
42 #include <sys/mutex.h>
43 #include <sys/smp.h>
44 
45 #include <vm/vm.h>
46 #include <vm/vm_extern.h>
47 #include <vm/vm_kern.h>
48 #include <vm/vm_page.h>
49 #include <vm/vm_map.h>
50 #include <vm/vm_object.h>
51 #include <vm/vm_pager.h>
52 #include <vm/vm_param.h>
53 
54 #include <machine/intr_machdep.h>
55 #include <x86/apicvar.h>
56 #include <x86/init.h>
57 #include <machine/pc/bios.h>
58 #include <machine/smp.h>
59 
60 #include <xen/xen-os.h>
61 #include <xen/hypervisor.h>
62 #include <xen/xenstore/xenstorevar.h>
63 #include <xen/xen_pv.h>
64 
65 #include <xen/interface/vcpu.h>
66 
67 #include <dev/xen/timer/timer.h>
68 
69 /* Native initial function */
70 extern u_int64_t hammer_time(u_int64_t, u_int64_t);
71 /* Xen initial function */
72 uint64_t hammer_time_xen(start_info_t *, uint64_t);
73 
74 #define MAX_E820_ENTRIES	128
75 
76 /*--------------------------- Forward Declarations ---------------------------*/
77 static caddr_t xen_pv_parse_preload_data(u_int64_t);
78 static void xen_pv_parse_memmap(caddr_t, vm_paddr_t *, int *);
79 
80 #ifdef SMP
81 static int xen_pv_start_all_aps(void);
82 #endif
83 
84 /*---------------------------- Extern Declarations ---------------------------*/
85 #ifdef SMP
86 /* Variables used by amd64 mp_machdep to start APs */
87 extern struct mtx ap_boot_mtx;
88 extern void *bootstacks[];
89 extern char *doublefault_stack;
90 extern char *nmi_stack;
91 extern void *dpcpu;
92 extern int bootAP;
93 extern char *bootSTK;
94 #endif
95 
96 /*-------------------------------- Global Data -------------------------------*/
97 /* Xen init_ops implementation. */
98 struct init_ops xen_init_ops = {
99 	.parse_preload_data		= xen_pv_parse_preload_data,
100 	.early_clock_source_init	= xen_clock_init,
101 	.early_delay			= xen_delay,
102 	.parse_memmap			= xen_pv_parse_memmap,
103 #ifdef SMP
104 	.start_all_aps			= xen_pv_start_all_aps,
105 #endif
106 };
107 
108 static struct bios_smap xen_smap[MAX_E820_ENTRIES];
109 
110 /*-------------------------------- Xen PV init -------------------------------*/
111 /*
112  * First function called by the Xen PVH boot sequence.
113  *
114  * Set some Xen global variables and prepare the environment so it is
115  * as similar as possible to what native FreeBSD init function expects.
116  */
117 uint64_t
118 hammer_time_xen(start_info_t *si, uint64_t xenstack)
119 {
120 	uint64_t physfree;
121 	uint64_t *PT4 = (u_int64_t *)xenstack;
122 	uint64_t *PT3 = (u_int64_t *)(xenstack + PAGE_SIZE);
123 	uint64_t *PT2 = (u_int64_t *)(xenstack + 2 * PAGE_SIZE);
124 	int i;
125 
126 	xen_domain_type = XEN_PV_DOMAIN;
127 	vm_guest = VM_GUEST_XEN;
128 
129 	if ((si == NULL) || (xenstack == 0)) {
130 		xc_printf("ERROR: invalid start_info or xen stack, halting\n");
131 		HYPERVISOR_shutdown(SHUTDOWN_crash);
132 	}
133 
134 	xc_printf("FreeBSD PVH running on %s\n", si->magic);
135 
136 	/* We use 3 pages of xen stack for the boot pagetables */
137 	physfree = xenstack + 3 * PAGE_SIZE - KERNBASE;
138 
139 	/* Setup Xen global variables */
140 	HYPERVISOR_start_info = si;
141 	HYPERVISOR_shared_info =
142 	    (shared_info_t *)(si->shared_info + KERNBASE);
143 
144 	/*
145 	 * Setup some misc global variables for Xen devices
146 	 *
147 	 * XXX: Devices that need these specific variables should
148 	 *      be rewritten to fetch this info by themselves from the
149 	 *      start_info page.
150 	 */
151 	xen_store = (struct xenstore_domain_interface *)
152 	    (ptoa(si->store_mfn) + KERNBASE);
153 	console_page = (char *)(ptoa(si->console.domU.mfn) + KERNBASE);
154 
155 	/*
156 	 * Use the stack Xen gives us to build the page tables
157 	 * as native FreeBSD expects to find them (created
158 	 * by the boot trampoline).
159 	 */
160 	for (i = 0; i < (PAGE_SIZE / sizeof(uint64_t)); i++) {
161 		/*
162 		 * Each slot of the level 4 pages points
163 		 * to the same level 3 page
164 		 */
165 		PT4[i] = ((uint64_t)&PT3[0]) - KERNBASE;
166 		PT4[i] |= PG_V | PG_RW | PG_U;
167 
168 		/*
169 		 * Each slot of the level 3 pages points
170 		 * to the same level 2 page
171 		 */
172 		PT3[i] = ((uint64_t)&PT2[0]) - KERNBASE;
173 		PT3[i] |= PG_V | PG_RW | PG_U;
174 
175 		/*
176 		 * The level 2 page slots are mapped with
177 		 * 2MB pages for 1GB.
178 		 */
179 		PT2[i] = i * (2 * 1024 * 1024);
180 		PT2[i] |= PG_V | PG_RW | PG_PS | PG_U;
181 	}
182 	load_cr3(((uint64_t)&PT4[0]) - KERNBASE);
183 
184 	/* Set the hooks for early functions that diverge from bare metal */
185 	init_ops = xen_init_ops;
186 	apic_ops = xen_apic_ops;
187 
188 	/* Now we can jump into the native init function */
189 	return (hammer_time(0, physfree));
190 }
191 
192 /*-------------------------------- PV specific -------------------------------*/
193 #ifdef SMP
194 static bool
195 start_xen_ap(int cpu)
196 {
197 	struct vcpu_guest_context *ctxt;
198 	int ms, cpus = mp_naps;
199 	const size_t stacksize = KSTACK_PAGES * PAGE_SIZE;
200 
201 	/* allocate and set up an idle stack data page */
202 	bootstacks[cpu] =
203 	    (void *)kmem_malloc(kernel_arena, stacksize, M_WAITOK | M_ZERO);
204 	doublefault_stack =
205 	    (char *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO);
206 	nmi_stack =
207 	    (char *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO);
208 	dpcpu =
209 	    (void *)kmem_malloc(kernel_arena, DPCPU_SIZE, M_WAITOK | M_ZERO);
210 
211 	bootSTK = (char *)bootstacks[cpu] + KSTACK_PAGES * PAGE_SIZE - 8;
212 	bootAP = cpu;
213 
214 	ctxt = malloc(sizeof(*ctxt), M_TEMP, M_WAITOK | M_ZERO);
215 	if (ctxt == NULL)
216 		panic("unable to allocate memory");
217 
218 	ctxt->flags = VGCF_IN_KERNEL;
219 	ctxt->user_regs.rip = (unsigned long) init_secondary;
220 	ctxt->user_regs.rsp = (unsigned long) bootSTK;
221 
222 	/* Set the AP to use the same page tables */
223 	ctxt->ctrlreg[3] = KPML4phys;
224 
225 	if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
226 		panic("unable to initialize AP#%d", cpu);
227 
228 	free(ctxt, M_TEMP);
229 
230 	/* Launch the vCPU */
231 	if (HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
232 		panic("unable to start AP#%d", cpu);
233 
234 	/* Wait up to 5 seconds for it to start. */
235 	for (ms = 0; ms < 5000; ms++) {
236 		if (mp_naps > cpus)
237 			return (true);
238 		DELAY(1000);
239 	}
240 
241 	return (false);
242 }
243 
244 static int
245 xen_pv_start_all_aps(void)
246 {
247 	int cpu;
248 
249 	mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
250 
251 	for (cpu = 1; cpu < mp_ncpus; cpu++) {
252 
253 		/* attempt to start the Application Processor */
254 		if (!start_xen_ap(cpu))
255 			panic("AP #%d failed to start!", cpu);
256 
257 		CPU_SET(cpu, &all_cpus);	/* record AP in CPU map */
258 	}
259 
260 	return (mp_naps);
261 }
262 #endif /* SMP */
263 
264 /*
265  * Functions to convert the "extra" parameters passed by Xen
266  * into FreeBSD boot options.
267  */
268 static void
269 xen_pv_set_env(void)
270 {
271 	char *cmd_line_next, *cmd_line;
272 	size_t env_size;
273 
274 	cmd_line = HYPERVISOR_start_info->cmd_line;
275 	env_size = sizeof(HYPERVISOR_start_info->cmd_line);
276 
277 	/* Skip leading spaces */
278 	for (; isspace(*cmd_line) && (env_size != 0); cmd_line++)
279 		env_size--;
280 
281 	/* Replace ',' with '\0' */
282 	for (cmd_line_next = cmd_line; strsep(&cmd_line_next, ",") != NULL;)
283 		;
284 
285 	init_static_kenv(cmd_line, env_size);
286 }
287 
288 static void
289 xen_pv_set_boothowto(void)
290 {
291 	int i;
292 
293 	/* get equivalents from the environment */
294 	for (i = 0; howto_names[i].ev != NULL; i++) {
295 		if (getenv(howto_names[i].ev) != NULL)
296 			boothowto |= howto_names[i].mask;
297 	}
298 }
299 
300 static caddr_t
301 xen_pv_parse_preload_data(u_int64_t modulep)
302 {
303 	/* Parse the extra boot information given by Xen */
304 	xen_pv_set_env();
305 	xen_pv_set_boothowto();
306 
307 	return (NULL);
308 }
309 
310 static void
311 xen_pv_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx)
312 {
313 	struct xen_memory_map memmap;
314 	u_int32_t size;
315 	int rc;
316 
317 	/* Fetch the E820 map from Xen */
318 	memmap.nr_entries = MAX_E820_ENTRIES;
319 	set_xen_guest_handle(memmap.buffer, xen_smap);
320 	rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
321 	if (rc)
322 		panic("unable to fetch Xen E820 memory map");
323 	size = memmap.nr_entries * sizeof(xen_smap[0]);
324 
325 	bios_add_smap_entries(xen_smap, size, physmap, physmap_idx);
326 }
327