]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/xen/hvm.c
MFV illumos
[FreeBSD/FreeBSD.git] / sys / x86 / xen / hvm.c
1 /*
2  * Copyright (c) 2008, 2013 Citrix Systems, Inc.
3  * Copyright (c) 2012 Spectra Logic Corporation
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/malloc.h>
35 #include <sys/proc.h>
36 #include <sys/smp.h>
37 #include <sys/systm.h>
38
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41
42 #include <dev/pci/pcivar.h>
43
44 #include <machine/cpufunc.h>
45 #include <machine/cpu.h>
46 #include <machine/smp.h>
47
48 #include <x86/apicreg.h>
49
50 #include <xen/xen-os.h>
51 #include <xen/features.h>
52 #include <xen/gnttab.h>
53 #include <xen/hypervisor.h>
54 #include <xen/hvm.h>
55 #include <xen/xen_intr.h>
56
57 #include <xen/interface/hvm/params.h>
58 #include <xen/interface/vcpu.h>
59
60 /*--------------------------- Forward Declarations ---------------------------*/
61 #ifdef SMP
62 static void xen_hvm_cpu_resume(void);
63 #endif
64 static void xen_hvm_cpu_init(void);
65
66 /*---------------------------- Extern Declarations ---------------------------*/
67 /* Variables used by mp_machdep to perform the bitmap IPI */
68 extern volatile u_int cpu_ipi_pending[MAXCPU];
69
70 /*-------------------------------- Local Types -------------------------------*/
71 enum xen_hvm_init_type {
72         XEN_HVM_INIT_COLD,
73         XEN_HVM_INIT_CANCELLED_SUSPEND,
74         XEN_HVM_INIT_RESUME
75 };
76
77 /*-------------------------------- Global Data -------------------------------*/
78 enum xen_domain_type xen_domain_type = XEN_NATIVE;
79
80 #ifdef SMP
81 struct cpu_ops xen_hvm_cpu_ops = {
82         .cpu_init       = xen_hvm_cpu_init,
83         .cpu_resume     = xen_hvm_cpu_resume
84 };
85 #endif
86
87 static MALLOC_DEFINE(M_XENHVM, "xen_hvm", "Xen HVM PV Support");
88
89 /**
90  * If non-zero, the hypervisor has been configured to use a direct
91  * IDT event callback for interrupt injection.
92  */
93 int xen_vector_callback_enabled;
94
95 /*------------------------------- Per-CPU Data -------------------------------*/
96 DPCPU_DEFINE(struct vcpu_info, vcpu_local_info);
97 DPCPU_DEFINE(struct vcpu_info *, vcpu_info);
98
99 /*------------------ Hypervisor Access Shared Memory Regions -----------------*/
100 /** Hypercall table accessed via HYPERVISOR_*_op() methods. */
101 extern char *hypercall_page;
102 shared_info_t *HYPERVISOR_shared_info;
103 start_info_t *HYPERVISOR_start_info;
104
105 #ifdef SMP
106 /* XEN diverged cpu operations */
107 static void
108 xen_hvm_cpu_resume(void)
109 {
110         u_int cpuid = PCPU_GET(cpuid);
111
112         /*
113          * Reset pending bitmap IPIs, because Xen doesn't preserve pending
114          * event channels on migration.
115          */
116         cpu_ipi_pending[cpuid] = 0;
117
118         /* register vcpu_info area */
119         xen_hvm_cpu_init();
120 }
121 #endif
122 /*---------------------- XEN Hypervisor Probe and Setup ----------------------*/
123 static uint32_t
124 xen_hvm_cpuid_base(void)
125 {
126         uint32_t base, regs[4];
127
128         for (base = 0x40000000; base < 0x40010000; base += 0x100) {
129                 do_cpuid(base, regs);
130                 if (!memcmp("XenVMMXenVMM", &regs[1], 12)
131                     && (regs[0] - base) >= 2)
132                         return (base);
133         }
134         return (0);
135 }
136
137 /*
138  * Allocate and fill in the hypcall page.
139  */
140 static int
141 xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)
142 {
143         uint32_t base, regs[4];
144         int i;
145
146         if (xen_pv_domain()) {
147                 /* hypercall page is already set in the PV case */
148                 return (0);
149         }
150
151         base = xen_hvm_cpuid_base();
152         if (base == 0)
153                 return (ENXIO);
154
155         if (init_type == XEN_HVM_INIT_COLD) {
156                 do_cpuid(base + 1, regs);
157                 printf("XEN: Hypervisor version %d.%d detected.\n",
158                     regs[0] >> 16, regs[0] & 0xffff);
159         }
160
161         /*
162          * Find the hypercall pages.
163          */
164         do_cpuid(base + 2, regs);
165
166         for (i = 0; i < regs[0]; i++)
167                 wrmsr(regs[1], vtophys(&hypercall_page + i * PAGE_SIZE) + i);
168
169         return (0);
170 }
171
172 static void
173 xen_hvm_init_shared_info_page(void)
174 {
175         struct xen_add_to_physmap xatp;
176
177         if (xen_pv_domain()) {
178                 /*
179                  * Already setup in the PV case, shared_info is passed inside
180                  * of the start_info struct at start of day.
181                  */
182                 return;
183         }
184
185         if (HYPERVISOR_shared_info == NULL) {
186                 HYPERVISOR_shared_info = malloc(PAGE_SIZE, M_XENHVM, M_NOWAIT);
187                 if (HYPERVISOR_shared_info == NULL)
188                         panic("Unable to allocate Xen shared info page");
189         }
190
191         xatp.domid = DOMID_SELF;
192         xatp.idx = 0;
193         xatp.space = XENMAPSPACE_shared_info;
194         xatp.gpfn = vtophys(HYPERVISOR_shared_info) >> PAGE_SHIFT;
195         if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
196                 panic("HYPERVISOR_memory_op failed");
197 }
198
199 /*
200  * Tell the hypervisor how to contact us for event channel callbacks.
201  */
202 void
203 xen_hvm_set_callback(device_t dev)
204 {
205         struct xen_hvm_param xhp;
206         int irq;
207
208         if (xen_vector_callback_enabled)
209                 return;
210
211         xhp.domid = DOMID_SELF;
212         xhp.index = HVM_PARAM_CALLBACK_IRQ;
213         if (xen_feature(XENFEAT_hvm_callback_vector) != 0) {
214                 int error;
215
216                 xhp.value = HVM_CALLBACK_VECTOR(IDT_EVTCHN);
217                 error = HYPERVISOR_hvm_op(HVMOP_set_param, &xhp);
218                 if (error == 0) {
219                         xen_vector_callback_enabled = 1;
220                         return;
221                 }
222                 printf("Xen HVM callback vector registration failed (%d). "
223                     "Falling back to emulated device interrupt\n", error);
224         }
225         xen_vector_callback_enabled = 0;
226         if (dev == NULL) {
227                 /*
228                  * Called from early boot or resume.
229                  * xenpci will invoke us again later.
230                  */
231                 return;
232         }
233
234         irq = pci_get_irq(dev);
235         if (irq < 16) {
236                 xhp.value = HVM_CALLBACK_GSI(irq);
237         } else {
238                 u_int slot;
239                 u_int pin;
240
241                 slot = pci_get_slot(dev);
242                 pin = pci_get_intpin(dev) - 1;
243                 xhp.value = HVM_CALLBACK_PCI_INTX(slot, pin);
244         }
245
246         if (HYPERVISOR_hvm_op(HVMOP_set_param, &xhp) != 0)
247                 panic("Can't set evtchn callback");
248 }
249
250 #define XEN_MAGIC_IOPORT 0x10
251 enum {
252         XMI_MAGIC                        = 0x49d2,
253         XMI_UNPLUG_IDE_DISKS             = 0x01,
254         XMI_UNPLUG_NICS                  = 0x02,
255         XMI_UNPLUG_IDE_EXCEPT_PRI_MASTER = 0x04
256 };
257
258 static void
259 xen_hvm_disable_emulated_devices(void)
260 {
261
262         if (xen_pv_domain()) {
263                 /*
264                  * No emulated devices in the PV case, so no need to unplug
265                  * anything.
266                  */
267                 return;
268         }
269
270         if (inw(XEN_MAGIC_IOPORT) != XMI_MAGIC)
271                 return;
272
273         if (bootverbose)
274                 printf("XEN: Disabling emulated block and network devices\n");
275         outw(XEN_MAGIC_IOPORT, XMI_UNPLUG_IDE_DISKS|XMI_UNPLUG_NICS);
276 }
277
278 static void
279 xen_hvm_init(enum xen_hvm_init_type init_type)
280 {
281         int error;
282         int i;
283
284         if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND)
285                 return;
286
287         error = xen_hvm_init_hypercall_stubs(init_type);
288
289         switch (init_type) {
290         case XEN_HVM_INIT_COLD:
291                 if (error != 0)
292                         return;
293
294                 /*
295                  * If xen_domain_type is not set at this point
296                  * it means we are inside a (PV)HVM guest, because
297                  * for PVH the guest type is set much earlier
298                  * (see hammer_time_xen).
299                  */
300                 if (!xen_domain()) {
301                         xen_domain_type = XEN_HVM_DOMAIN;
302                         vm_guest = VM_GUEST_XEN;
303                 }
304
305                 setup_xen_features();
306 #ifdef SMP
307                 cpu_ops = xen_hvm_cpu_ops;
308 #endif
309                 break;
310         case XEN_HVM_INIT_RESUME:
311                 if (error != 0)
312                         panic("Unable to init Xen hypercall stubs on resume");
313
314                 /* Clear stale vcpu_info. */
315                 CPU_FOREACH(i)
316                         DPCPU_ID_SET(i, vcpu_info, NULL);
317                 break;
318         default:
319                 panic("Unsupported HVM initialization type");
320         }
321
322         xen_vector_callback_enabled = 0;
323         xen_hvm_set_callback(NULL);
324
325         /*
326          * On (PV)HVM domains we need to request the hypervisor to
327          * fill the shared info page, for PVH guest the shared_info page
328          * is passed inside the start_info struct and is already set, so this
329          * functions are no-ops.
330          */
331         xen_hvm_init_shared_info_page();
332         xen_hvm_disable_emulated_devices();
333
334
335 void
336 xen_hvm_suspend(void)
337 {
338 }
339
340 void
341 xen_hvm_resume(bool suspend_cancelled)
342 {
343
344         xen_hvm_init(suspend_cancelled ?
345             XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME);
346
347         /* Register vcpu_info area for CPU#0. */
348         xen_hvm_cpu_init();
349 }
350  
351 static void
352 xen_hvm_sysinit(void *arg __unused)
353 {
354         xen_hvm_init(XEN_HVM_INIT_COLD);
355 }
356
357 static void
358 xen_set_vcpu_id(void)
359 {
360         struct pcpu *pc;
361         int i;
362
363         if (!xen_hvm_domain())
364                 return;
365
366         /* Set vcpu_id to acpi_id */
367         CPU_FOREACH(i) {
368                 pc = pcpu_find(i);
369                 pc->pc_vcpu_id = pc->pc_acpi_id;
370                 if (bootverbose)
371                         printf("XEN: CPU %u has VCPU ID %u\n",
372                                i, pc->pc_vcpu_id);
373         }
374 }
375
376 static void
377 xen_hvm_cpu_init(void)
378 {
379         struct vcpu_register_vcpu_info info;
380         struct vcpu_info *vcpu_info;
381         int cpu, rc;
382
383         if (!xen_domain())
384                 return;
385
386         if (DPCPU_GET(vcpu_info) != NULL) {
387                 /*
388                  * vcpu_info is already set.  We're resuming
389                  * from a failed migration and our pre-suspend
390                  * configuration is still valid.
391                  */
392                 return;
393         }
394
395         vcpu_info = DPCPU_PTR(vcpu_local_info);
396         cpu = PCPU_GET(vcpu_id);
397         info.mfn = vtophys(vcpu_info) >> PAGE_SHIFT;
398         info.offset = vtophys(vcpu_info) - trunc_page(vtophys(vcpu_info));
399
400         rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
401         if (rc != 0)
402                 DPCPU_SET(vcpu_info, &HYPERVISOR_shared_info->vcpu_info[cpu]);
403         else
404                 DPCPU_SET(vcpu_info, vcpu_info);
405 }
406
407 SYSINIT(xen_hvm_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_hvm_sysinit, NULL);
408 SYSINIT(xen_hvm_cpu_init, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_cpu_init, NULL);
409 SYSINIT(xen_set_vcpu_id, SI_SUB_CPU, SI_ORDER_ANY, xen_set_vcpu_id, NULL);