xref: /freebsd/sys/x86/xen/pv.c (revision f5fd950e35c962bad0aa31fdc4b4052e13207893)
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 <x86/init.h>
55 #include <machine/pc/bios.h>
56 #include <machine/smp.h>
57 
58 #include <xen/xen-os.h>
59 #include <xen/hypervisor.h>
60 
61 #include <xen/interface/vcpu.h>
62 
63 #include <dev/xen/timer/timer.h>
64 
65 /* Native initial function */
66 extern u_int64_t hammer_time(u_int64_t, u_int64_t);
67 /* Xen initial function */
68 uint64_t hammer_time_xen(start_info_t *, uint64_t);
69 
70 #define MAX_E820_ENTRIES	128
71 
72 /*--------------------------- Forward Declarations ---------------------------*/
73 static caddr_t xen_pv_parse_preload_data(u_int64_t);
74 static void xen_pv_parse_memmap(caddr_t, vm_paddr_t *, int *);
75 
76 #ifdef SMP
77 static int xen_pv_start_all_aps(void);
78 #endif
79 
80 /*---------------------------- Extern Declarations ---------------------------*/
81 #ifdef SMP
82 /* Variables used by amd64 mp_machdep to start APs */
83 extern struct mtx ap_boot_mtx;
84 extern void *bootstacks[];
85 extern char *doublefault_stack;
86 extern char *nmi_stack;
87 extern void *dpcpu;
88 extern int bootAP;
89 extern char *bootSTK;
90 #endif
91 
92 /*-------------------------------- Global Data -------------------------------*/
93 /* Xen init_ops implementation. */
94 struct init_ops xen_init_ops = {
95 	.parse_preload_data =	xen_pv_parse_preload_data,
96 	.early_clock_source_init =	xen_clock_init,
97 	.early_delay =			xen_delay,
98 	.parse_memmap =			xen_pv_parse_memmap,
99 #ifdef SMP
100 	.start_all_aps =		xen_pv_start_all_aps,
101 #endif
102 };
103 
104 static struct bios_smap xen_smap[MAX_E820_ENTRIES];
105 
106 /*-------------------------------- Xen PV init -------------------------------*/
107 /*
108  * First function called by the Xen PVH boot sequence.
109  *
110  * Set some Xen global variables and prepare the environment so it is
111  * as similar as possible to what native FreeBSD init function expects.
112  */
113 uint64_t
114 hammer_time_xen(start_info_t *si, uint64_t xenstack)
115 {
116 	uint64_t physfree;
117 	uint64_t *PT4 = (u_int64_t *)xenstack;
118 	uint64_t *PT3 = (u_int64_t *)(xenstack + PAGE_SIZE);
119 	uint64_t *PT2 = (u_int64_t *)(xenstack + 2 * PAGE_SIZE);
120 	int i;
121 
122 	xen_domain_type = XEN_PV_DOMAIN;
123 	vm_guest = VM_GUEST_XEN;
124 
125 	if ((si == NULL) || (xenstack == 0)) {
126 		xc_printf("ERROR: invalid start_info or xen stack, halting\n");
127 		HYPERVISOR_shutdown(SHUTDOWN_crash);
128 	}
129 
130 	xc_printf("FreeBSD PVH running on %s\n", si->magic);
131 
132 	/* We use 3 pages of xen stack for the boot pagetables */
133 	physfree = xenstack + 3 * PAGE_SIZE - KERNBASE;
134 
135 	/* Setup Xen global variables */
136 	HYPERVISOR_start_info = si;
137 	HYPERVISOR_shared_info =
138 	    (shared_info_t *)(si->shared_info + KERNBASE);
139 
140 	/*
141 	 * Setup some misc global variables for Xen devices
142 	 *
143 	 * XXX: Devices that need these specific variables should
144 	 *      be rewritten to fetch this info by themselves from the
145 	 *      start_info page.
146 	 */
147 	xen_store = (struct xenstore_domain_interface *)
148 	    (ptoa(si->store_mfn) + KERNBASE);
149 	console_page = (char *)(ptoa(si->console.domU.mfn) + KERNBASE);
150 
151 	/*
152 	 * Use the stack Xen gives us to build the page tables
153 	 * as native FreeBSD expects to find them (created
154 	 * by the boot trampoline).
155 	 */
156 	for (i = 0; i < (PAGE_SIZE / sizeof(uint64_t)); i++) {
157 		/* Each slot of the level 4 pages points to the same level 3 page */
158 		PT4[i] = ((uint64_t)&PT3[0]) - KERNBASE;
159 		PT4[i] |= PG_V | PG_RW | PG_U;
160 
161 		/* Each slot of the level 3 pages points to the same level 2 page */
162 		PT3[i] = ((uint64_t)&PT2[0]) - KERNBASE;
163 		PT3[i] |= PG_V | PG_RW | PG_U;
164 
165 		/* The level 2 page slots are mapped with 2MB pages for 1GB. */
166 		PT2[i] = i * (2 * 1024 * 1024);
167 		PT2[i] |= PG_V | PG_RW | PG_PS | PG_U;
168 	}
169 	load_cr3(((uint64_t)&PT4[0]) - KERNBASE);
170 
171 	/* Set the hooks for early functions that diverge from bare metal */
172 	init_ops = xen_init_ops;
173 
174 	/* Now we can jump into the native init function */
175 	return (hammer_time(0, physfree));
176 }
177 
178 /*-------------------------------- PV specific -------------------------------*/
179 #ifdef SMP
180 static bool
181 start_xen_ap(int cpu)
182 {
183 	struct vcpu_guest_context *ctxt;
184 	int ms, cpus = mp_naps;
185 	const size_t stacksize = KSTACK_PAGES * PAGE_SIZE;
186 
187 	/* allocate and set up an idle stack data page */
188 	bootstacks[cpu] =
189 	    (void *)kmem_malloc(kernel_arena, stacksize, M_WAITOK | M_ZERO);
190 	doublefault_stack =
191 	    (char *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO);
192 	nmi_stack =
193 	    (char *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO);
194 	dpcpu =
195 	    (void *)kmem_malloc(kernel_arena, DPCPU_SIZE, M_WAITOK | M_ZERO);
196 
197 	bootSTK = (char *)bootstacks[cpu] + KSTACK_PAGES * PAGE_SIZE - 8;
198 	bootAP = cpu;
199 
200 	ctxt = malloc(sizeof(*ctxt), M_TEMP, M_WAITOK | M_ZERO);
201 	if (ctxt == NULL)
202 		panic("unable to allocate memory");
203 
204 	ctxt->flags = VGCF_IN_KERNEL;
205 	ctxt->user_regs.rip = (unsigned long) init_secondary;
206 	ctxt->user_regs.rsp = (unsigned long) bootSTK;
207 
208 	/* Set the AP to use the same page tables */
209 	ctxt->ctrlreg[3] = KPML4phys;
210 
211 	if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
212 		panic("unable to initialize AP#%d", cpu);
213 
214 	free(ctxt, M_TEMP);
215 
216 	/* Launch the vCPU */
217 	if (HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
218 		panic("unable to start AP#%d", cpu);
219 
220 	/* Wait up to 5 seconds for it to start. */
221 	for (ms = 0; ms < 5000; ms++) {
222 		if (mp_naps > cpus)
223 			return (true);
224 		DELAY(1000);
225 	}
226 
227 	return (false);
228 }
229 
230 static int
231 xen_pv_start_all_aps(void)
232 {
233 	int cpu;
234 
235 	mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
236 
237 	for (cpu = 1; cpu < mp_ncpus; cpu++) {
238 
239 		/* attempt to start the Application Processor */
240 		if (!start_xen_ap(cpu))
241 			panic("AP #%d failed to start!", cpu);
242 
243 		CPU_SET(cpu, &all_cpus);	/* record AP in CPU map */
244 	}
245 
246 	return (mp_naps);
247 }
248 #endif /* SMP */
249 
250 /*
251  * Functions to convert the "extra" parameters passed by Xen
252  * into FreeBSD boot options.
253  */
254 static void
255 xen_pv_set_env(void)
256 {
257 	char *cmd_line_next, *cmd_line;
258 	size_t env_size;
259 
260 	cmd_line = HYPERVISOR_start_info->cmd_line;
261 	env_size = sizeof(HYPERVISOR_start_info->cmd_line);
262 
263 	/* Skip leading spaces */
264 	for (; isspace(*cmd_line) && (env_size != 0); cmd_line++)
265 		env_size--;
266 
267 	/* Replace ',' with '\0' */
268 	for (cmd_line_next = cmd_line; strsep(&cmd_line_next, ",") != NULL;)
269 		;
270 
271 	init_static_kenv(cmd_line, env_size);
272 }
273 
274 static void
275 xen_pv_set_boothowto(void)
276 {
277 	int i;
278 
279 	/* get equivalents from the environment */
280 	for (i = 0; howto_names[i].ev != NULL; i++) {
281 		if (getenv(howto_names[i].ev) != NULL)
282 			boothowto |= howto_names[i].mask;
283 	}
284 }
285 
286 static caddr_t
287 xen_pv_parse_preload_data(u_int64_t modulep)
288 {
289 	/* Parse the extra boot information given by Xen */
290 	xen_pv_set_env();
291 	xen_pv_set_boothowto();
292 
293 	return (NULL);
294 }
295 
296 static void
297 xen_pv_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx)
298 {
299 	struct xen_memory_map memmap;
300 	u_int32_t size;
301 	int rc;
302 
303 	/* Fetch the E820 map from Xen */
304 	memmap.nr_entries = MAX_E820_ENTRIES;
305 	set_xen_guest_handle(memmap.buffer, xen_smap);
306 	rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
307 	if (rc)
308 		panic("unable to fetch Xen E820 memory map");
309 	size = memmap.nr_entries * sizeof(xen_smap[0]);
310 
311 	bios_add_smap_entries(xen_smap, size, physmap, physmap_idx);
312 }
313