1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008, 2013 Citrix Systems, Inc.
5 * Copyright (c) 2012 Spectra Logic Corporation
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
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 #include <vm/vm_param.h>
42
43 #include <dev/pci/pcivar.h>
44
45 #include <machine/cpufunc.h>
46 #include <machine/cpu.h>
47 #include <machine/smp.h>
48
49 #include <x86/apicreg.h>
50
51 #include <xen/xen-os.h>
52 #include <xen/error.h>
53 #include <xen/features.h>
54 #include <xen/gnttab.h>
55 #include <xen/hypervisor.h>
56 #include <xen/hvm.h>
57 #include <xen/xen_intr.h>
58
59 #include <xen/interface/arch-x86/cpuid.h>
60 #include <xen/interface/hvm/params.h>
61 #include <xen/interface/vcpu.h>
62
63 /*--------------------------- Forward Declarations ---------------------------*/
64 static void xen_hvm_cpu_init(void);
65
66 /*-------------------------------- Global Data -------------------------------*/
67 enum xen_domain_type xen_domain_type = XEN_NATIVE;
68
69 #ifdef SMP
70 struct cpu_ops xen_hvm_cpu_ops = {
71 .cpu_init = xen_hvm_cpu_init,
72 .cpu_resume = xen_hvm_cpu_init
73 };
74 #endif
75
76 static MALLOC_DEFINE(M_XENHVM, "xen_hvm", "Xen HVM PV Support");
77
78 /**
79 * If non-zero, the hypervisor has been configured to use a direct
80 * IDT event callback for interrupt injection.
81 */
82 int xen_vector_callback_enabled;
83
84 /**
85 * Start info flags. ATM this only used to store the initial domain flag for
86 * PVHv2, and it's always empty for HVM guests.
87 */
88 uint32_t hvm_start_flags;
89
90 /**
91 * Signal whether the vector injected for the event channel upcall requires to
92 * be EOI'ed on the local APIC.
93 */
94 bool xen_evtchn_needs_ack;
95
96 /*------------------------------- Per-CPU Data -------------------------------*/
97 DPCPU_DEFINE(struct vcpu_info, vcpu_local_info);
98 DPCPU_DEFINE(struct vcpu_info *, vcpu_info);
99
100 /*------------------ Hypervisor Access Shared Memory Regions -----------------*/
101 shared_info_t *HYPERVISOR_shared_info;
102
103 /*------------------------------ Sysctl tunables -----------------------------*/
104 int xen_disable_pv_disks = 0;
105 int xen_disable_pv_nics = 0;
106 TUNABLE_INT("hw.xen.disable_pv_disks", &xen_disable_pv_disks);
107 TUNABLE_INT("hw.xen.disable_pv_nics", &xen_disable_pv_nics);
108
109 /*---------------------- XEN Hypervisor Probe and Setup ----------------------*/
110
111 static uint32_t cpuid_base;
112
113 static uint32_t
xen_hvm_cpuid_base(void)114 xen_hvm_cpuid_base(void)
115 {
116 uint32_t base, regs[4];
117
118 for (base = 0x40000000; base < 0x40010000; base += 0x100) {
119 do_cpuid(base, regs);
120 if (!memcmp("XenVMMXenVMM", ®s[1], 12)
121 && (regs[0] - base) >= 2)
122 return (base);
123 }
124 return (0);
125 }
126
127 static void
hypervisor_quirks(unsigned int major,unsigned int minor)128 hypervisor_quirks(unsigned int major, unsigned int minor)
129 {
130 #ifdef SMP
131 if (((major < 4) || (major == 4 && minor <= 5)) &&
132 msix_disable_migration == -1) {
133 /*
134 * Xen hypervisors prior to 4.6.0 do not properly
135 * handle updates to enabled MSI-X table entries,
136 * so disable MSI-X interrupt migration in that
137 * case.
138 */
139 if (bootverbose)
140 printf(
141 "Disabling MSI-X interrupt migration due to Xen hypervisor bug.\n"
142 "Set machdep.msix_disable_migration=0 to forcefully enable it.\n");
143 msix_disable_migration = 1;
144 }
145 #endif
146 }
147
148 static void
hypervisor_version(void)149 hypervisor_version(void)
150 {
151 uint32_t regs[4];
152 int major, minor;
153
154 do_cpuid(cpuid_base + 1, regs);
155
156 major = regs[0] >> 16;
157 minor = regs[0] & 0xffff;
158 printf("XEN: Hypervisor version %d.%d detected.\n", major, minor);
159
160 hypervisor_quirks(major, minor);
161 }
162
163 /*
164 * Allocate and fill in the hypcall page.
165 */
166 int
xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)167 xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)
168 {
169 uint32_t regs[4];
170
171 /* Legacy PVH will get here without the cpuid leaf being set. */
172 if (cpuid_base == 0)
173 cpuid_base = xen_hvm_cpuid_base();
174 if (cpuid_base == 0)
175 return (ENXIO);
176
177 if (xen_domain() && init_type == XEN_HVM_INIT_LATE) {
178 /*
179 * If the domain type is already set we can assume that the
180 * hypercall page has been populated too, so just print the
181 * version (and apply any quirks) and exit.
182 */
183 hypervisor_version();
184 return 0;
185 }
186
187 if (init_type == XEN_HVM_INIT_LATE)
188 hypervisor_version();
189
190 /*
191 * Find the hypercall pages.
192 */
193 do_cpuid(cpuid_base + 2, regs);
194 if (regs[0] != 1)
195 return (EINVAL);
196
197 wrmsr(regs[1], (init_type == XEN_HVM_INIT_EARLY)
198 ? (vm_paddr_t)((uintptr_t)&hypercall_page - KERNBASE)
199 : vtophys(&hypercall_page));
200
201 return (0);
202 }
203
204 static void
xen_hvm_init_shared_info_page(void)205 xen_hvm_init_shared_info_page(void)
206 {
207 struct xen_add_to_physmap xatp;
208
209 if (xen_pv_domain()) {
210 /*
211 * Already setup in the PV case, shared_info is passed inside
212 * of the start_info struct at start of day.
213 */
214 return;
215 }
216
217 if (HYPERVISOR_shared_info == NULL) {
218 HYPERVISOR_shared_info = malloc(PAGE_SIZE, M_XENHVM, M_NOWAIT);
219 if (HYPERVISOR_shared_info == NULL)
220 panic("Unable to allocate Xen shared info page");
221 }
222
223 xatp.domid = DOMID_SELF;
224 xatp.idx = 0;
225 xatp.space = XENMAPSPACE_shared_info;
226 xatp.gpfn = vtophys(HYPERVISOR_shared_info) >> PAGE_SHIFT;
227 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
228 panic("HYPERVISOR_memory_op failed");
229 }
230
231 static int
set_percpu_callback(unsigned int vcpu)232 set_percpu_callback(unsigned int vcpu)
233 {
234 struct xen_hvm_evtchn_upcall_vector vec;
235 int error;
236
237 vec.vcpu = vcpu;
238 vec.vector = IDT_EVTCHN;
239 error = HYPERVISOR_hvm_op(HVMOP_set_evtchn_upcall_vector, &vec);
240
241 return (error != 0 ? xen_translate_error(error) : 0);
242 }
243
244 /*
245 * Tell the hypervisor how to contact us for event channel callbacks.
246 */
247 void
xen_hvm_set_callback(device_t dev)248 xen_hvm_set_callback(device_t dev)
249 {
250 struct xen_hvm_param xhp;
251 int irq;
252
253 if (xen_vector_callback_enabled)
254 return;
255
256 xhp.domid = DOMID_SELF;
257 xhp.index = HVM_PARAM_CALLBACK_IRQ;
258 if (xen_feature(XENFEAT_hvm_callback_vector) != 0) {
259 int error;
260
261 error = set_percpu_callback(0);
262 if (error == 0) {
263 xen_evtchn_needs_ack = true;
264 /* Trick toolstack to think we are enlightened */
265 xhp.value = 1;
266 } else
267 xhp.value = HVM_CALLBACK_VECTOR(IDT_EVTCHN);
268 error = HYPERVISOR_hvm_op(HVMOP_set_param, &xhp);
269 if (error == 0) {
270 xen_vector_callback_enabled = 1;
271 return;
272 } else if (xen_evtchn_needs_ack)
273 panic("Unable to setup fake HVM param: %d", error);
274
275 printf("Xen HVM callback vector registration failed (%d). "
276 "Falling back to emulated device interrupt\n", error);
277 }
278 xen_vector_callback_enabled = 0;
279 if (dev == NULL) {
280 /*
281 * Called from early boot or resume.
282 * xenpci will invoke us again later.
283 */
284 return;
285 }
286
287 irq = pci_get_irq(dev);
288 if (irq < 16) {
289 xhp.value = HVM_CALLBACK_GSI(irq);
290 } else {
291 u_int slot;
292 u_int pin;
293
294 slot = pci_get_slot(dev);
295 pin = pci_get_intpin(dev) - 1;
296 xhp.value = HVM_CALLBACK_PCI_INTX(slot, pin);
297 }
298
299 if (HYPERVISOR_hvm_op(HVMOP_set_param, &xhp) != 0)
300 panic("Can't set evtchn callback");
301 }
302
303 #define XEN_MAGIC_IOPORT 0x10
304 enum {
305 XMI_MAGIC = 0x49d2,
306 XMI_UNPLUG_IDE_DISKS = 0x01,
307 XMI_UNPLUG_NICS = 0x02,
308 XMI_UNPLUG_IDE_EXCEPT_PRI_MASTER = 0x04
309 };
310
311 static void
xen_hvm_disable_emulated_devices(void)312 xen_hvm_disable_emulated_devices(void)
313 {
314 u_short disable_devs = 0;
315
316 if (xen_pv_domain()) {
317 /*
318 * No emulated devices in the PV case, so no need to unplug
319 * anything.
320 */
321 if (xen_disable_pv_disks != 0 || xen_disable_pv_nics != 0)
322 printf("PV devices cannot be disabled in PV guests\n");
323 return;
324 }
325
326 if (inw(XEN_MAGIC_IOPORT) != XMI_MAGIC)
327 return;
328
329 if (xen_disable_pv_disks == 0) {
330 if (bootverbose)
331 printf("XEN: disabling emulated disks\n");
332 disable_devs |= XMI_UNPLUG_IDE_DISKS;
333 }
334 if (xen_disable_pv_nics == 0) {
335 if (bootverbose)
336 printf("XEN: disabling emulated nics\n");
337 disable_devs |= XMI_UNPLUG_NICS;
338 }
339
340 if (disable_devs != 0)
341 outw(XEN_MAGIC_IOPORT, disable_devs);
342 }
343
344 static void
xen_hvm_init(enum xen_hvm_init_type init_type)345 xen_hvm_init(enum xen_hvm_init_type init_type)
346 {
347 int error;
348 int i;
349
350 if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND)
351 return;
352
353 error = xen_hvm_init_hypercall_stubs(init_type);
354
355 switch (init_type) {
356 case XEN_HVM_INIT_LATE:
357 if (error != 0)
358 return;
359
360 /*
361 * If xen_domain_type is not set at this point
362 * it means we are inside a (PV)HVM guest, because
363 * for PVH the guest type is set much earlier
364 * (see hammer_time_xen).
365 */
366 if (!xen_domain()) {
367 xen_domain_type = XEN_HVM_DOMAIN;
368 vm_guest = VM_GUEST_XEN;
369 }
370
371 setup_xen_features();
372 #ifdef SMP
373 cpu_ops = xen_hvm_cpu_ops;
374 #endif
375 break;
376 case XEN_HVM_INIT_RESUME:
377 if (error != 0)
378 panic("Unable to init Xen hypercall stubs on resume");
379
380 /* Clear stale vcpu_info. */
381 CPU_FOREACH(i)
382 DPCPU_ID_SET(i, vcpu_info, NULL);
383 break;
384 default:
385 panic("Unsupported HVM initialization type");
386 }
387
388 xen_vector_callback_enabled = 0;
389 xen_evtchn_needs_ack = false;
390 xen_hvm_set_callback(NULL);
391
392 /*
393 * On (PV)HVM domains we need to request the hypervisor to
394 * fill the shared info page, for PVH guest the shared_info page
395 * is passed inside the start_info struct and is already set, so this
396 * functions are no-ops.
397 */
398 xen_hvm_init_shared_info_page();
399 xen_hvm_disable_emulated_devices();
400 }
401
402 void
xen_hvm_suspend(void)403 xen_hvm_suspend(void)
404 {
405 }
406
407 void
xen_hvm_resume(bool suspend_cancelled)408 xen_hvm_resume(bool suspend_cancelled)
409 {
410
411 xen_hvm_init(suspend_cancelled ?
412 XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME);
413
414 /* Register vcpu_info area for CPU#0. */
415 xen_hvm_cpu_init();
416 }
417
418 static void
xen_hvm_sysinit(void * arg __unused)419 xen_hvm_sysinit(void *arg __unused)
420 {
421 xen_hvm_init(XEN_HVM_INIT_LATE);
422 }
423 SYSINIT(xen_hvm_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_hvm_sysinit, NULL);
424
425 static void
xen_hvm_cpu_init(void)426 xen_hvm_cpu_init(void)
427 {
428 struct vcpu_register_vcpu_info info;
429 struct vcpu_info *vcpu_info;
430 uint32_t regs[4];
431 int cpu, rc;
432
433 if (!xen_domain())
434 return;
435
436 if (DPCPU_GET(vcpu_info) != NULL) {
437 /*
438 * vcpu_info is already set. We're resuming
439 * from a failed migration and our pre-suspend
440 * configuration is still valid.
441 */
442 return;
443 }
444
445 /*
446 * Set vCPU ID. If available fetch the ID from CPUID, if not just use
447 * the ACPI ID.
448 */
449 KASSERT(cpuid_base != 0, ("Invalid base Xen CPUID leaf"));
450 cpuid_count(cpuid_base + 4, 0, regs);
451 KASSERT((regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) ||
452 !xen_pv_domain(),
453 ("Xen PV domain without vcpu_id in cpuid"));
454 PCPU_SET(vcpu_id, (regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) ?
455 regs[1] : PCPU_GET(acpi_id));
456
457 if (xen_evtchn_needs_ack && !IS_BSP()) {
458 /*
459 * Setup the per-vpcu event channel upcall vector. This is only
460 * required when using the new HVMOP_set_evtchn_upcall_vector
461 * hypercall, which allows using a different vector for each
462 * vCPU. Note that FreeBSD uses the same vector for all vCPUs
463 * because it's not dynamically allocated.
464 */
465 rc = set_percpu_callback(PCPU_GET(vcpu_id));
466 if (rc != 0)
467 panic("Event channel upcall vector setup failed: %d",
468 rc);
469 }
470
471 /*
472 * Set the vCPU info.
473 *
474 * NB: the vCPU info for vCPUs < 32 can be fetched from the shared info
475 * page, but in order to make sure the mapping code is correct always
476 * attempt to map the vCPU info at a custom place.
477 */
478 vcpu_info = DPCPU_PTR(vcpu_local_info);
479 cpu = PCPU_GET(vcpu_id);
480 info.mfn = vtophys(vcpu_info) >> PAGE_SHIFT;
481 info.offset = vtophys(vcpu_info) - trunc_page(vtophys(vcpu_info));
482
483 rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
484 if (rc != 0)
485 DPCPU_SET(vcpu_info, &HYPERVISOR_shared_info->vcpu_info[cpu]);
486 else
487 DPCPU_SET(vcpu_info, vcpu_info);
488 }
489 SYSINIT(xen_hvm_cpu_init, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_cpu_init, NULL);
490
491 /* HVM/PVH start_info accessors */
492 static vm_paddr_t
hvm_get_xenstore_mfn(void)493 hvm_get_xenstore_mfn(void)
494 {
495
496 return (hvm_get_parameter(HVM_PARAM_STORE_PFN));
497 }
498
499 static evtchn_port_t
hvm_get_xenstore_evtchn(void)500 hvm_get_xenstore_evtchn(void)
501 {
502
503 return (hvm_get_parameter(HVM_PARAM_STORE_EVTCHN));
504 }
505
506 static vm_paddr_t
hvm_get_console_mfn(void)507 hvm_get_console_mfn(void)
508 {
509
510 return (hvm_get_parameter(HVM_PARAM_CONSOLE_PFN));
511 }
512
513 static evtchn_port_t
hvm_get_console_evtchn(void)514 hvm_get_console_evtchn(void)
515 {
516
517 return (hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN));
518 }
519
520 static uint32_t
hvm_get_start_flags(void)521 hvm_get_start_flags(void)
522 {
523
524 return (hvm_start_flags);
525 }
526
527 struct hypervisor_info hypervisor_info = {
528 .get_xenstore_mfn = hvm_get_xenstore_mfn,
529 .get_xenstore_evtchn = hvm_get_xenstore_evtchn,
530 .get_console_mfn = hvm_get_console_mfn,
531 .get_console_evtchn = hvm_get_console_evtchn,
532 .get_start_flags = hvm_get_start_flags,
533 };
534