]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/xen/hvm.c
Merge clang 3.5.0 release from ^/vendor/clang/dist, resolve conflicts,
[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 shared_info_t *HYPERVISOR_shared_info;
101 start_info_t *HYPERVISOR_start_info;
102
103 #ifdef SMP
104 /* XEN diverged cpu operations */
105 static void
106 xen_hvm_cpu_resume(void)
107 {
108         u_int cpuid = PCPU_GET(cpuid);
109
110         /*
111          * Reset pending bitmap IPIs, because Xen doesn't preserve pending
112          * event channels on migration.
113          */
114         cpu_ipi_pending[cpuid] = 0;
115
116         /* register vcpu_info area */
117         xen_hvm_cpu_init();
118 }
119 #endif
120 /*---------------------- XEN Hypervisor Probe and Setup ----------------------*/
121 static uint32_t
122 xen_hvm_cpuid_base(void)
123 {
124         uint32_t base, regs[4];
125
126         for (base = 0x40000000; base < 0x40010000; base += 0x100) {
127                 do_cpuid(base, regs);
128                 if (!memcmp("XenVMMXenVMM", &regs[1], 12)
129                     && (regs[0] - base) >= 2)
130                         return (base);
131         }
132         return (0);
133 }
134
135 /*
136  * Allocate and fill in the hypcall page.
137  */
138 static int
139 xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)
140 {
141         uint32_t base, regs[4];
142         int i;
143
144         if (xen_pv_domain()) {
145                 /* hypercall page is already set in the PV case */
146                 return (0);
147         }
148
149         base = xen_hvm_cpuid_base();
150         if (base == 0)
151                 return (ENXIO);
152
153         if (init_type == XEN_HVM_INIT_COLD) {
154                 do_cpuid(base + 1, regs);
155                 printf("XEN: Hypervisor version %d.%d detected.\n",
156                     regs[0] >> 16, regs[0] & 0xffff);
157         }
158
159         /*
160          * Find the hypercall pages.
161          */
162         do_cpuid(base + 2, regs);
163
164         for (i = 0; i < regs[0]; i++)
165                 wrmsr(regs[1], vtophys(&hypercall_page + i * PAGE_SIZE) + i);
166
167         return (0);
168 }
169
170 static void
171 xen_hvm_init_shared_info_page(void)
172 {
173         struct xen_add_to_physmap xatp;
174
175         if (xen_pv_domain()) {
176                 /*
177                  * Already setup in the PV case, shared_info is passed inside
178                  * of the start_info struct at start of day.
179                  */
180                 return;
181         }
182
183         if (HYPERVISOR_shared_info == NULL) {
184                 HYPERVISOR_shared_info = malloc(PAGE_SIZE, M_XENHVM, M_NOWAIT);
185                 if (HYPERVISOR_shared_info == NULL)
186                         panic("Unable to allocate Xen shared info page");
187         }
188
189         xatp.domid = DOMID_SELF;
190         xatp.idx = 0;
191         xatp.space = XENMAPSPACE_shared_info;
192         xatp.gpfn = vtophys(HYPERVISOR_shared_info) >> PAGE_SHIFT;
193         if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
194                 panic("HYPERVISOR_memory_op failed");
195 }
196
197 /*
198  * Tell the hypervisor how to contact us for event channel callbacks.
199  */
200 void
201 xen_hvm_set_callback(device_t dev)
202 {
203         struct xen_hvm_param xhp;
204         int irq;
205
206         if (xen_vector_callback_enabled)
207                 return;
208
209         xhp.domid = DOMID_SELF;
210         xhp.index = HVM_PARAM_CALLBACK_IRQ;
211         if (xen_feature(XENFEAT_hvm_callback_vector) != 0) {
212                 int error;
213
214                 xhp.value = HVM_CALLBACK_VECTOR(IDT_EVTCHN);
215                 error = HYPERVISOR_hvm_op(HVMOP_set_param, &xhp);
216                 if (error == 0) {
217                         xen_vector_callback_enabled = 1;
218                         return;
219                 }
220                 printf("Xen HVM callback vector registration failed (%d). "
221                     "Falling back to emulated device interrupt\n", error);
222         }
223         xen_vector_callback_enabled = 0;
224         if (dev == NULL) {
225                 /*
226                  * Called from early boot or resume.
227                  * xenpci will invoke us again later.
228                  */
229                 return;
230         }
231
232         irq = pci_get_irq(dev);
233         if (irq < 16) {
234                 xhp.value = HVM_CALLBACK_GSI(irq);
235         } else {
236                 u_int slot;
237                 u_int pin;
238
239                 slot = pci_get_slot(dev);
240                 pin = pci_get_intpin(dev) - 1;
241                 xhp.value = HVM_CALLBACK_PCI_INTX(slot, pin);
242         }
243
244         if (HYPERVISOR_hvm_op(HVMOP_set_param, &xhp) != 0)
245                 panic("Can't set evtchn callback");
246 }
247
248 #define XEN_MAGIC_IOPORT 0x10
249 enum {
250         XMI_MAGIC                        = 0x49d2,
251         XMI_UNPLUG_IDE_DISKS             = 0x01,
252         XMI_UNPLUG_NICS                  = 0x02,
253         XMI_UNPLUG_IDE_EXCEPT_PRI_MASTER = 0x04
254 };
255
256 static void
257 xen_hvm_disable_emulated_devices(void)
258 {
259
260         if (xen_pv_domain()) {
261                 /*
262                  * No emulated devices in the PV case, so no need to unplug
263                  * anything.
264                  */
265                 return;
266         }
267
268         if (inw(XEN_MAGIC_IOPORT) != XMI_MAGIC)
269                 return;
270
271         if (bootverbose)
272                 printf("XEN: Disabling emulated block and network devices\n");
273         outw(XEN_MAGIC_IOPORT, XMI_UNPLUG_IDE_DISKS|XMI_UNPLUG_NICS);
274 }
275
276 static void
277 xen_hvm_init(enum xen_hvm_init_type init_type)
278 {
279         int error;
280         int i;
281
282         if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND)
283                 return;
284
285         error = xen_hvm_init_hypercall_stubs(init_type);
286
287         switch (init_type) {
288         case XEN_HVM_INIT_COLD:
289                 if (error != 0)
290                         return;
291
292                 /*
293                  * If xen_domain_type is not set at this point
294                  * it means we are inside a (PV)HVM guest, because
295                  * for PVH the guest type is set much earlier
296                  * (see hammer_time_xen).
297                  */
298                 if (!xen_domain()) {
299                         xen_domain_type = XEN_HVM_DOMAIN;
300                         vm_guest = VM_GUEST_XEN;
301                 }
302
303                 setup_xen_features();
304 #ifdef SMP
305                 cpu_ops = xen_hvm_cpu_ops;
306 #endif
307                 break;
308         case XEN_HVM_INIT_RESUME:
309                 if (error != 0)
310                         panic("Unable to init Xen hypercall stubs on resume");
311
312                 /* Clear stale vcpu_info. */
313                 CPU_FOREACH(i)
314                         DPCPU_ID_SET(i, vcpu_info, NULL);
315                 break;
316         default:
317                 panic("Unsupported HVM initialization type");
318         }
319
320         xen_vector_callback_enabled = 0;
321         xen_hvm_set_callback(NULL);
322
323         /*
324          * On (PV)HVM domains we need to request the hypervisor to
325          * fill the shared info page, for PVH guest the shared_info page
326          * is passed inside the start_info struct and is already set, so this
327          * functions are no-ops.
328          */
329         xen_hvm_init_shared_info_page();
330         xen_hvm_disable_emulated_devices();
331
332
333 void
334 xen_hvm_suspend(void)
335 {
336 }
337
338 void
339 xen_hvm_resume(bool suspend_cancelled)
340 {
341
342         xen_hvm_init(suspend_cancelled ?
343             XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME);
344
345         /* Register vcpu_info area for CPU#0. */
346         xen_hvm_cpu_init();
347 }
348  
349 static void
350 xen_hvm_sysinit(void *arg __unused)
351 {
352         xen_hvm_init(XEN_HVM_INIT_COLD);
353 }
354
355 static void
356 xen_set_vcpu_id(void)
357 {
358         struct pcpu *pc;
359         int i;
360
361         if (!xen_hvm_domain())
362                 return;
363
364         /* Set vcpu_id to acpi_id */
365         CPU_FOREACH(i) {
366                 pc = pcpu_find(i);
367                 pc->pc_vcpu_id = pc->pc_acpi_id;
368                 if (bootverbose)
369                         printf("XEN: CPU %u has VCPU ID %u\n",
370                                i, pc->pc_vcpu_id);
371         }
372 }
373
374 static void
375 xen_hvm_cpu_init(void)
376 {
377         struct vcpu_register_vcpu_info info;
378         struct vcpu_info *vcpu_info;
379         int cpu, rc;
380
381         if (!xen_domain())
382                 return;
383
384         if (DPCPU_GET(vcpu_info) != NULL) {
385                 /*
386                  * vcpu_info is already set.  We're resuming
387                  * from a failed migration and our pre-suspend
388                  * configuration is still valid.
389                  */
390                 return;
391         }
392
393         vcpu_info = DPCPU_PTR(vcpu_local_info);
394         cpu = PCPU_GET(vcpu_id);
395         info.mfn = vtophys(vcpu_info) >> PAGE_SHIFT;
396         info.offset = vtophys(vcpu_info) - trunc_page(vtophys(vcpu_info));
397
398         rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
399         if (rc != 0)
400                 DPCPU_SET(vcpu_info, &HYPERVISOR_shared_info->vcpu_info[cpu]);
401         else
402                 DPCPU_SET(vcpu_info, vcpu_info);
403 }
404
405 SYSINIT(xen_hvm_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_hvm_sysinit, NULL);
406 SYSINIT(xen_hvm_cpu_init, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_cpu_init, NULL);
407 SYSINIT(xen_set_vcpu_id, SI_SUB_CPU, SI_ORDER_ANY, xen_set_vcpu_id, NULL);