2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2008, 2013 Citrix Systems, Inc.
5 * Copyright (c) 2012 Spectra Logic Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
39 #include <sys/systm.h>
43 #include <vm/vm_param.h>
45 #include <dev/pci/pcivar.h>
47 #include <machine/cpufunc.h>
48 #include <machine/cpu.h>
49 #include <machine/smp.h>
51 #include <x86/apicreg.h>
53 #include <xen/xen-os.h>
54 #include <xen/error.h>
55 #include <xen/features.h>
56 #include <xen/gnttab.h>
57 #include <xen/hypervisor.h>
59 #include <xen/xen_intr.h>
61 #include <xen/interface/arch-x86/cpuid.h>
62 #include <xen/interface/hvm/params.h>
63 #include <xen/interface/vcpu.h>
65 /*--------------------------- Forward Declarations ---------------------------*/
66 static void xen_hvm_cpu_init(void);
68 /*-------------------------------- Global Data -------------------------------*/
69 enum xen_domain_type xen_domain_type = XEN_NATIVE;
72 struct cpu_ops xen_hvm_cpu_ops = {
73 .cpu_init = xen_hvm_cpu_init,
74 .cpu_resume = xen_hvm_cpu_init
78 static MALLOC_DEFINE(M_XENHVM, "xen_hvm", "Xen HVM PV Support");
81 * If non-zero, the hypervisor has been configured to use a direct
82 * IDT event callback for interrupt injection.
84 int xen_vector_callback_enabled;
87 * Start info flags. ATM this only used to store the initial domain flag for
88 * PVHv2, and it's always empty for HVM guests.
90 uint32_t hvm_start_flags;
93 * Signal whether the vector injected for the event channel upcall requires to
94 * be EOI'ed on the local APIC.
96 bool xen_evtchn_needs_ack;
98 /*------------------------------- Per-CPU Data -------------------------------*/
99 DPCPU_DEFINE(struct vcpu_info, vcpu_local_info);
100 DPCPU_DEFINE(struct vcpu_info *, vcpu_info);
102 /*------------------ Hypervisor Access Shared Memory Regions -----------------*/
103 shared_info_t *HYPERVISOR_shared_info;
105 /*------------------------------ Sysctl tunables -----------------------------*/
106 int xen_disable_pv_disks = 0;
107 int xen_disable_pv_nics = 0;
108 TUNABLE_INT("hw.xen.disable_pv_disks", &xen_disable_pv_disks);
109 TUNABLE_INT("hw.xen.disable_pv_nics", &xen_disable_pv_nics);
111 /*---------------------- XEN Hypervisor Probe and Setup ----------------------*/
113 static uint32_t cpuid_base;
116 xen_hvm_cpuid_base(void)
118 uint32_t base, regs[4];
120 for (base = 0x40000000; base < 0x40010000; base += 0x100) {
121 do_cpuid(base, regs);
122 if (!memcmp("XenVMMXenVMM", ®s[1], 12)
123 && (regs[0] - base) >= 2)
130 hypervisor_quirks(unsigned int major, unsigned int minor)
133 if (((major < 4) || (major == 4 && minor <= 5)) &&
134 msix_disable_migration == -1) {
136 * Xen hypervisors prior to 4.6.0 do not properly
137 * handle updates to enabled MSI-X table entries,
138 * so disable MSI-X interrupt migration in that
143 "Disabling MSI-X interrupt migration due to Xen hypervisor bug.\n"
144 "Set machdep.msix_disable_migration=0 to forcefully enable it.\n");
145 msix_disable_migration = 1;
151 hypervisor_version(void)
156 do_cpuid(cpuid_base + 1, regs);
158 major = regs[0] >> 16;
159 minor = regs[0] & 0xffff;
160 printf("XEN: Hypervisor version %d.%d detected.\n", major, minor);
162 hypervisor_quirks(major, minor);
166 * Allocate and fill in the hypcall page.
169 xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type)
173 /* Legacy PVH will get here without the cpuid leaf being set. */
175 cpuid_base = xen_hvm_cpuid_base();
179 if (xen_domain() && init_type == XEN_HVM_INIT_LATE) {
181 * If the domain type is already set we can assume that the
182 * hypercall page has been populated too, so just print the
183 * version (and apply any quirks) and exit.
185 hypervisor_version();
189 if (init_type == XEN_HVM_INIT_LATE)
190 hypervisor_version();
193 * Find the hypercall pages.
195 do_cpuid(cpuid_base + 2, regs);
199 wrmsr(regs[1], (init_type == XEN_HVM_INIT_EARLY)
200 ? ((vm_paddr_t)&hypercall_page - KERNBASE)
201 : vtophys(&hypercall_page));
207 xen_hvm_init_shared_info_page(void)
209 struct xen_add_to_physmap xatp;
211 if (xen_pv_domain()) {
213 * Already setup in the PV case, shared_info is passed inside
214 * of the start_info struct at start of day.
219 if (HYPERVISOR_shared_info == NULL) {
220 HYPERVISOR_shared_info = malloc(PAGE_SIZE, M_XENHVM, M_NOWAIT);
221 if (HYPERVISOR_shared_info == NULL)
222 panic("Unable to allocate Xen shared info page");
225 xatp.domid = DOMID_SELF;
227 xatp.space = XENMAPSPACE_shared_info;
228 xatp.gpfn = vtophys(HYPERVISOR_shared_info) >> PAGE_SHIFT;
229 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp))
230 panic("HYPERVISOR_memory_op failed");
234 set_percpu_callback(unsigned int vcpu)
236 struct xen_hvm_evtchn_upcall_vector vec;
240 vec.vector = IDT_EVTCHN;
241 error = HYPERVISOR_hvm_op(HVMOP_set_evtchn_upcall_vector, &vec);
243 return (error != 0 ? xen_translate_error(error) : 0);
247 * Tell the hypervisor how to contact us for event channel callbacks.
250 xen_hvm_set_callback(device_t dev)
252 struct xen_hvm_param xhp;
255 if (xen_vector_callback_enabled)
258 xhp.domid = DOMID_SELF;
259 xhp.index = HVM_PARAM_CALLBACK_IRQ;
260 if (xen_feature(XENFEAT_hvm_callback_vector) != 0) {
263 error = set_percpu_callback(0);
265 xen_evtchn_needs_ack = true;
266 /* Trick toolstack to think we are enlightened */
269 xhp.value = HVM_CALLBACK_VECTOR(IDT_EVTCHN);
270 error = HYPERVISOR_hvm_op(HVMOP_set_param, &xhp);
272 xen_vector_callback_enabled = 1;
274 } else if (xen_evtchn_needs_ack)
275 panic("Unable to setup fake HVM param: %d", error);
277 printf("Xen HVM callback vector registration failed (%d). "
278 "Falling back to emulated device interrupt\n", error);
280 xen_vector_callback_enabled = 0;
283 * Called from early boot or resume.
284 * xenpci will invoke us again later.
289 irq = pci_get_irq(dev);
291 xhp.value = HVM_CALLBACK_GSI(irq);
296 slot = pci_get_slot(dev);
297 pin = pci_get_intpin(dev) - 1;
298 xhp.value = HVM_CALLBACK_PCI_INTX(slot, pin);
301 if (HYPERVISOR_hvm_op(HVMOP_set_param, &xhp) != 0)
302 panic("Can't set evtchn callback");
305 #define XEN_MAGIC_IOPORT 0x10
308 XMI_UNPLUG_IDE_DISKS = 0x01,
309 XMI_UNPLUG_NICS = 0x02,
310 XMI_UNPLUG_IDE_EXCEPT_PRI_MASTER = 0x04
314 xen_hvm_disable_emulated_devices(void)
316 u_short disable_devs = 0;
318 if (xen_pv_domain()) {
320 * No emulated devices in the PV case, so no need to unplug
323 if (xen_disable_pv_disks != 0 || xen_disable_pv_nics != 0)
324 printf("PV devices cannot be disabled in PV guests\n");
328 if (inw(XEN_MAGIC_IOPORT) != XMI_MAGIC)
331 if (xen_disable_pv_disks == 0) {
333 printf("XEN: disabling emulated disks\n");
334 disable_devs |= XMI_UNPLUG_IDE_DISKS;
336 if (xen_disable_pv_nics == 0) {
338 printf("XEN: disabling emulated nics\n");
339 disable_devs |= XMI_UNPLUG_NICS;
342 if (disable_devs != 0)
343 outw(XEN_MAGIC_IOPORT, disable_devs);
347 xen_hvm_init(enum xen_hvm_init_type init_type)
352 if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND)
355 error = xen_hvm_init_hypercall_stubs(init_type);
358 case XEN_HVM_INIT_LATE:
363 * If xen_domain_type is not set at this point
364 * it means we are inside a (PV)HVM guest, because
365 * for PVH the guest type is set much earlier
366 * (see hammer_time_xen).
369 xen_domain_type = XEN_HVM_DOMAIN;
370 vm_guest = VM_GUEST_XEN;
373 setup_xen_features();
375 cpu_ops = xen_hvm_cpu_ops;
378 case XEN_HVM_INIT_RESUME:
380 panic("Unable to init Xen hypercall stubs on resume");
382 /* Clear stale vcpu_info. */
384 DPCPU_ID_SET(i, vcpu_info, NULL);
387 panic("Unsupported HVM initialization type");
390 xen_vector_callback_enabled = 0;
391 xen_evtchn_needs_ack = false;
392 xen_hvm_set_callback(NULL);
395 * On (PV)HVM domains we need to request the hypervisor to
396 * fill the shared info page, for PVH guest the shared_info page
397 * is passed inside the start_info struct and is already set, so this
398 * functions are no-ops.
400 xen_hvm_init_shared_info_page();
401 xen_hvm_disable_emulated_devices();
405 xen_hvm_suspend(void)
410 xen_hvm_resume(bool suspend_cancelled)
413 xen_hvm_init(suspend_cancelled ?
414 XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME);
416 /* Register vcpu_info area for CPU#0. */
421 xen_hvm_sysinit(void *arg __unused)
423 xen_hvm_init(XEN_HVM_INIT_LATE);
425 SYSINIT(xen_hvm_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_hvm_sysinit, NULL);
428 xen_hvm_cpu_init(void)
430 struct vcpu_register_vcpu_info info;
431 struct vcpu_info *vcpu_info;
438 if (DPCPU_GET(vcpu_info) != NULL) {
440 * vcpu_info is already set. We're resuming
441 * from a failed migration and our pre-suspend
442 * configuration is still valid.
448 * Set vCPU ID. If available fetch the ID from CPUID, if not just use
451 KASSERT(cpuid_base != 0, ("Invalid base Xen CPUID leaf"));
452 cpuid_count(cpuid_base + 4, 0, regs);
453 KASSERT((regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) ||
455 ("Xen PV domain without vcpu_id in cpuid"));
456 PCPU_SET(vcpu_id, (regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) ?
457 regs[1] : PCPU_GET(acpi_id));
459 if (xen_evtchn_needs_ack && !IS_BSP()) {
461 * Setup the per-vpcu event channel upcall vector. This is only
462 * required when using the new HVMOP_set_evtchn_upcall_vector
463 * hypercall, which allows using a different vector for each
464 * vCPU. Note that FreeBSD uses the same vector for all vCPUs
465 * because it's not dynamically allocated.
467 rc = set_percpu_callback(PCPU_GET(vcpu_id));
469 panic("Event channel upcall vector setup failed: %d",
476 * NB: the vCPU info for vCPUs < 32 can be fetched from the shared info
477 * page, but in order to make sure the mapping code is correct always
478 * attempt to map the vCPU info at a custom place.
480 vcpu_info = DPCPU_PTR(vcpu_local_info);
481 cpu = PCPU_GET(vcpu_id);
482 info.mfn = vtophys(vcpu_info) >> PAGE_SHIFT;
483 info.offset = vtophys(vcpu_info) - trunc_page(vtophys(vcpu_info));
485 rc = HYPERVISOR_vcpu_op(VCPUOP_register_vcpu_info, cpu, &info);
487 DPCPU_SET(vcpu_info, &HYPERVISOR_shared_info->vcpu_info[cpu]);
489 DPCPU_SET(vcpu_info, vcpu_info);
491 SYSINIT(xen_hvm_cpu_init, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_cpu_init, NULL);
493 /* HVM/PVH start_info accessors */
495 hvm_get_xenstore_mfn(void)
498 return (hvm_get_parameter(HVM_PARAM_STORE_PFN));
502 hvm_get_xenstore_evtchn(void)
505 return (hvm_get_parameter(HVM_PARAM_STORE_EVTCHN));
509 hvm_get_console_mfn(void)
512 return (hvm_get_parameter(HVM_PARAM_CONSOLE_PFN));
516 hvm_get_console_evtchn(void)
519 return (hvm_get_parameter(HVM_PARAM_CONSOLE_EVTCHN));
523 hvm_get_start_flags(void)
526 return (hvm_start_flags);
529 struct hypervisor_info hypervisor_info = {
530 .get_xenstore_mfn = hvm_get_xenstore_mfn,
531 .get_xenstore_evtchn = hvm_get_xenstore_evtchn,
532 .get_console_mfn = hvm_get_console_mfn,
533 .get_console_evtchn = hvm_get_console_evtchn,
534 .get_start_flags = hvm_get_start_flags,