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