]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/xen/pv.c
Merge libc++ trunk r321414 to contrib/libc++.
[FreeBSD/FreeBSD.git] / sys / x86 / xen / pv.c
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
52 #include <vm/vm.h>
53 #include <vm/vm_extern.h>
54 #include <vm/vm_kern.h>
55 #include <vm/vm_page.h>
56 #include <vm/vm_map.h>
57 #include <vm/vm_object.h>
58 #include <vm/vm_pager.h>
59 #include <vm/vm_param.h>
60
61 #include <machine/intr_machdep.h>
62 #include <x86/apicvar.h>
63 #include <x86/init.h>
64 #include <machine/pc/bios.h>
65 #include <machine/smp.h>
66 #include <machine/intr_machdep.h>
67 #include <machine/metadata.h>
68
69 #include <xen/xen-os.h>
70 #include <xen/hypervisor.h>
71 #include <xen/xenstore/xenstorevar.h>
72 #include <xen/xen_pv.h>
73 #include <xen/xen_msi.h>
74
75 #include <xen/interface/vcpu.h>
76
77 #include <dev/xen/timer/timer.h>
78
79 #ifdef DDB
80 #include <ddb/ddb.h>
81 #endif
82
83 /* Native initial function */
84 extern u_int64_t hammer_time(u_int64_t, u_int64_t);
85 /* Xen initial function */
86 uint64_t hammer_time_xen(start_info_t *, uint64_t);
87
88 #define MAX_E820_ENTRIES        128
89
90 /*--------------------------- Forward Declarations ---------------------------*/
91 static caddr_t xen_pv_parse_preload_data(u_int64_t);
92 static void xen_pv_parse_memmap(caddr_t, vm_paddr_t *, int *);
93
94 #ifdef SMP
95 static int xen_pv_start_all_aps(void);
96 #endif
97
98 /*---------------------------- Extern Declarations ---------------------------*/
99 #ifdef SMP
100 /* Variables used by amd64 mp_machdep to start APs */
101 extern char *doublefault_stack;
102 extern char *nmi_stack;
103 #endif
104
105 /*
106  * Placed by the linker at the end of the bss section, which is the last
107  * section loaded by Xen before loading the symtab and strtab.
108  */
109 extern uint32_t end;
110
111 /*-------------------------------- Global Data -------------------------------*/
112 /* Xen init_ops implementation. */
113 struct init_ops xen_init_ops = {
114         .parse_preload_data             = xen_pv_parse_preload_data,
115         .early_clock_source_init        = xen_clock_init,
116         .early_delay                    = xen_delay,
117         .parse_memmap                   = xen_pv_parse_memmap,
118 #ifdef SMP
119         .start_all_aps                  = xen_pv_start_all_aps,
120 #endif
121         .msi_init =                     xen_msi_init,
122 };
123
124 static struct bios_smap xen_smap[MAX_E820_ENTRIES];
125
126 /*-------------------------------- Xen PV init -------------------------------*/
127 /*
128  * First function called by the Xen PVH boot sequence.
129  *
130  * Set some Xen global variables and prepare the environment so it is
131  * as similar as possible to what native FreeBSD init function expects.
132  */
133 uint64_t
134 hammer_time_xen(start_info_t *si, uint64_t xenstack)
135 {
136         uint64_t physfree;
137         uint64_t *PT4 = (u_int64_t *)xenstack;
138         uint64_t *PT3 = (u_int64_t *)(xenstack + PAGE_SIZE);
139         uint64_t *PT2 = (u_int64_t *)(xenstack + 2 * PAGE_SIZE);
140         int i;
141
142         xen_domain_type = XEN_PV_DOMAIN;
143         vm_guest = VM_GUEST_XEN;
144
145         if ((si == NULL) || (xenstack == 0)) {
146                 xc_printf("ERROR: invalid start_info or xen stack, halting\n");
147                 HYPERVISOR_shutdown(SHUTDOWN_crash);
148         }
149
150         xc_printf("FreeBSD PVH running on %s\n", si->magic);
151
152         /* We use 3 pages of xen stack for the boot pagetables */
153         physfree = xenstack + 3 * PAGE_SIZE - KERNBASE;
154
155         /* Setup Xen global variables */
156         HYPERVISOR_start_info = si;
157         HYPERVISOR_shared_info =
158             (shared_info_t *)(si->shared_info + KERNBASE);
159
160         /*
161          * Setup some misc global variables for Xen devices
162          *
163          * XXX: Devices that need these specific variables should
164          *      be rewritten to fetch this info by themselves from the
165          *      start_info page.
166          */
167         xen_store = (struct xenstore_domain_interface *)
168             (ptoa(si->store_mfn) + KERNBASE);
169         console_page = (char *)(ptoa(si->console.domU.mfn) + KERNBASE);
170
171         /*
172          * Use the stack Xen gives us to build the page tables
173          * as native FreeBSD expects to find them (created
174          * by the boot trampoline).
175          */
176         for (i = 0; i < (PAGE_SIZE / sizeof(uint64_t)); i++) {
177                 /*
178                  * Each slot of the level 4 pages points
179                  * to the same level 3 page
180                  */
181                 PT4[i] = ((uint64_t)&PT3[0]) - KERNBASE;
182                 PT4[i] |= PG_V | PG_RW | PG_U;
183
184                 /*
185                  * Each slot of the level 3 pages points
186                  * to the same level 2 page
187                  */
188                 PT3[i] = ((uint64_t)&PT2[0]) - KERNBASE;
189                 PT3[i] |= PG_V | PG_RW | PG_U;
190
191                 /*
192                  * The level 2 page slots are mapped with
193                  * 2MB pages for 1GB.
194                  */
195                 PT2[i] = i * (2 * 1024 * 1024);
196                 PT2[i] |= PG_V | PG_RW | PG_PS | PG_U;
197         }
198         load_cr3(((uint64_t)&PT4[0]) - KERNBASE);
199
200         /* Set the hooks for early functions that diverge from bare metal */
201         init_ops = xen_init_ops;
202         apic_ops = xen_apic_ops;
203
204         /* Now we can jump into the native init function */
205         return (hammer_time(0, physfree));
206 }
207
208 /*-------------------------------- PV specific -------------------------------*/
209 #ifdef SMP
210 static bool
211 start_xen_ap(int cpu)
212 {
213         struct vcpu_guest_context *ctxt;
214         int ms, cpus = mp_naps;
215         const size_t stacksize = kstack_pages * PAGE_SIZE;
216
217         /* allocate and set up an idle stack data page */
218         bootstacks[cpu] =
219             (void *)kmem_malloc(kernel_arena, stacksize, M_WAITOK | M_ZERO);
220         doublefault_stack =
221             (char *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO);
222         nmi_stack =
223             (char *)kmem_malloc(kernel_arena, PAGE_SIZE, M_WAITOK | M_ZERO);
224         dpcpu =
225             (void *)kmem_malloc(kernel_arena, DPCPU_SIZE, M_WAITOK | M_ZERO);
226
227         bootSTK = (char *)bootstacks[cpu] + kstack_pages * PAGE_SIZE - 8;
228         bootAP = cpu;
229
230         ctxt = malloc(sizeof(*ctxt), M_TEMP, M_WAITOK | M_ZERO);
231
232         ctxt->flags = VGCF_IN_KERNEL;
233         ctxt->user_regs.rip = (unsigned long) init_secondary;
234         ctxt->user_regs.rsp = (unsigned long) bootSTK;
235
236         /* Set the AP to use the same page tables */
237         ctxt->ctrlreg[3] = KPML4phys;
238
239         if (HYPERVISOR_vcpu_op(VCPUOP_initialise, cpu, ctxt))
240                 panic("unable to initialize AP#%d", cpu);
241
242         free(ctxt, M_TEMP);
243
244         /* Launch the vCPU */
245         if (HYPERVISOR_vcpu_op(VCPUOP_up, cpu, NULL))
246                 panic("unable to start AP#%d", cpu);
247
248         /* Wait up to 5 seconds for it to start. */
249         for (ms = 0; ms < 5000; ms++) {
250                 if (mp_naps > cpus)
251                         return (true);
252                 DELAY(1000);
253         }
254
255         return (false);
256 }
257
258 static int
259 xen_pv_start_all_aps(void)
260 {
261         int cpu;
262
263         mtx_init(&ap_boot_mtx, "ap boot", NULL, MTX_SPIN);
264
265         for (cpu = 1; cpu < mp_ncpus; cpu++) {
266
267                 /* attempt to start the Application Processor */
268                 if (!start_xen_ap(cpu))
269                         panic("AP #%d failed to start!", cpu);
270
271                 CPU_SET(cpu, &all_cpus);        /* record AP in CPU map */
272         }
273
274         return (mp_naps);
275 }
276 #endif /* SMP */
277
278 /*
279  * Functions to convert the "extra" parameters passed by Xen
280  * into FreeBSD boot options.
281  */
282 static void
283 xen_pv_set_env(void)
284 {
285         char *cmd_line_next, *cmd_line;
286         size_t env_size;
287
288         cmd_line = HYPERVISOR_start_info->cmd_line;
289         env_size = sizeof(HYPERVISOR_start_info->cmd_line);
290
291         /* Skip leading spaces */
292         for (; isspace(*cmd_line) && (env_size != 0); cmd_line++)
293                 env_size--;
294
295         /* Replace ',' with '\0' */
296         for (cmd_line_next = cmd_line; strsep(&cmd_line_next, ",") != NULL;)
297                 ;
298
299         init_static_kenv(cmd_line, 0);
300 }
301
302 static void
303 xen_pv_set_boothowto(void)
304 {
305         int i;
306         char *env;
307
308         /* get equivalents from the environment */
309         for (i = 0; howto_names[i].ev != NULL; i++) {
310                 if ((env = kern_getenv(howto_names[i].ev)) != NULL) {
311                         boothowto |= howto_names[i].mask;
312                         freeenv(env);
313                 }
314         }
315 }
316
317 #ifdef DDB
318 /*
319  * The way Xen loads the symtab is different from the native boot loader,
320  * because it's tailored for NetBSD. So we have to adapt and use the same
321  * method as NetBSD. Portions of the code below have been picked from NetBSD:
322  * sys/kern/kern_ksyms.c CVS Revision 1.71.
323  */
324 static void
325 xen_pv_parse_symtab(void)
326 {
327         Elf_Ehdr *ehdr;
328         Elf_Shdr *shdr;
329         vm_offset_t sym_end;
330         uint32_t size;
331         int i, j;
332
333         size = end;
334         sym_end = HYPERVISOR_start_info->mod_start != 0 ?
335             HYPERVISOR_start_info->mod_start :
336             HYPERVISOR_start_info->mfn_list;
337
338         /*
339          * Make sure the size is right headed, sym_end is just a
340          * high boundary, but at least allows us to fail earlier.
341          */
342         if ((vm_offset_t)&end + size > sym_end) {
343                 xc_printf("Unable to load ELF symtab: size mismatch\n");
344                 return;
345         }
346
347         ehdr = (Elf_Ehdr *)(&end + 1);
348         if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG) ||
349             ehdr->e_ident[EI_CLASS] != ELF_TARG_CLASS ||
350             ehdr->e_version > 1) {
351                 xc_printf("Unable to load ELF symtab: invalid symbol table\n");
352                 return;
353         }
354
355         shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff);
356         /* Find the symbol table and the corresponding string table. */
357         for (i = 1; i < ehdr->e_shnum; i++) {
358                 if (shdr[i].sh_type != SHT_SYMTAB)
359                         continue;
360                 if (shdr[i].sh_offset == 0)
361                         continue;
362                 ksymtab = (uintptr_t)((uint8_t *)ehdr + shdr[i].sh_offset);
363                 ksymtab_size = shdr[i].sh_size;
364                 j = shdr[i].sh_link;
365                 if (shdr[j].sh_offset == 0)
366                         continue; /* Can this happen? */
367                 kstrtab = (uintptr_t)((uint8_t *)ehdr + shdr[j].sh_offset);
368                 break;
369         }
370
371         if (ksymtab == 0 || kstrtab == 0) {
372                 xc_printf(
373     "Unable to load ELF symtab: could not find symtab or strtab\n");
374                 return;
375         }
376 }
377 #endif
378
379 static caddr_t
380 xen_pv_parse_preload_data(u_int64_t modulep)
381 {
382         caddr_t          kmdp;
383         vm_ooffset_t     off;
384         vm_paddr_t       metadata;
385         char             *envp;
386
387         if (HYPERVISOR_start_info->mod_start != 0) {
388                 preload_metadata = (caddr_t)(HYPERVISOR_start_info->mod_start);
389
390                 kmdp = preload_search_by_type("elf kernel");
391                 if (kmdp == NULL)
392                         kmdp = preload_search_by_type("elf64 kernel");
393                 KASSERT(kmdp != NULL, ("unable to find kernel"));
394
395                 /*
396                  * Xen has relocated the metadata and the modules,
397                  * so we need to recalculate it's position. This is
398                  * done by saving the original modulep address and
399                  * then calculating the offset with mod_start,
400                  * which contains the relocated modulep address.
401                  */
402                 metadata = MD_FETCH(kmdp, MODINFOMD_MODULEP, vm_paddr_t);
403                 off = HYPERVISOR_start_info->mod_start - metadata;
404
405                 preload_bootstrap_relocate(off);
406
407                 boothowto = MD_FETCH(kmdp, MODINFOMD_HOWTO, int);
408                 envp = MD_FETCH(kmdp, MODINFOMD_ENVP, char *);
409                 if (envp != NULL)
410                         envp += off;
411                 init_static_kenv(envp, 0);
412         } else {
413                 /* Parse the extra boot information given by Xen */
414                 xen_pv_set_env();
415                 xen_pv_set_boothowto();
416                 kmdp = NULL;
417         }
418
419 #ifdef DDB
420         xen_pv_parse_symtab();
421 #endif
422         return (kmdp);
423 }
424
425 static void
426 xen_pv_parse_memmap(caddr_t kmdp, vm_paddr_t *physmap, int *physmap_idx)
427 {
428         struct xen_memory_map memmap;
429         u_int32_t size;
430         int rc;
431
432         /* Fetch the E820 map from Xen */
433         memmap.nr_entries = MAX_E820_ENTRIES;
434         set_xen_guest_handle(memmap.buffer, xen_smap);
435         rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap);
436         if (rc)
437                 panic("unable to fetch Xen E820 memory map");
438         size = memmap.nr_entries * sizeof(xen_smap[0]);
439
440         bios_add_smap_entries(xen_smap, size, physmap, physmap_idx);
441 }