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 <contrib/xen/arch-x86/cpuid.h> 60 #include <contrib/xen/hvm/params.h> 61 #include <contrib/xen/vcpu.h> 62 63 /*--------------------------- Forward Declarations ---------------------------*/ 64 static void xen_hvm_cpu_init(void); 65 66 /*-------------------------------- Global Data -------------------------------*/ 67 #ifdef SMP 68 struct cpu_ops xen_hvm_cpu_ops = { 69 .cpu_init = xen_hvm_cpu_init, 70 .cpu_resume = xen_hvm_cpu_init 71 }; 72 #endif 73 74 static MALLOC_DEFINE(M_XENHVM, "xen_hvm", "Xen HVM PV Support"); 75 76 /** 77 * If non-zero, the hypervisor has been configured to use a direct 78 * IDT event callback for interrupt injection. 79 */ 80 int xen_vector_callback_enabled; 81 82 /** 83 * Signal whether the vector injected for the event channel upcall requires to 84 * be EOI'ed on the local APIC. 85 */ 86 bool xen_evtchn_needs_ack; 87 88 /*------------------------------- Per-CPU Data -------------------------------*/ 89 DPCPU_DECLARE(struct vcpu_info *, vcpu_info); 90 91 /*------------------------------ Sysctl tunables -----------------------------*/ 92 int xen_disable_pv_disks = 0; 93 int xen_disable_pv_nics = 0; 94 TUNABLE_INT("hw.xen.disable_pv_disks", &xen_disable_pv_disks); 95 TUNABLE_INT("hw.xen.disable_pv_nics", &xen_disable_pv_nics); 96 97 /*---------------------- XEN Hypervisor Probe and Setup ----------------------*/ 98 99 uint32_t xen_cpuid_base; 100 101 static uint32_t 102 xen_hvm_cpuid_base(void) 103 { 104 uint32_t base, regs[4]; 105 106 for (base = 0x40000000; base < 0x40010000; base += 0x100) { 107 do_cpuid(base, regs); 108 if (!memcmp("XenVMMXenVMM", ®s[1], 12) 109 && (regs[0] - base) >= 2) 110 return (base); 111 } 112 return (0); 113 } 114 115 static void 116 hypervisor_quirks(unsigned int major, unsigned int minor) 117 { 118 #ifdef SMP 119 if (((major < 4) || (major == 4 && minor <= 5)) && 120 msix_disable_migration == -1) { 121 /* 122 * Xen hypervisors prior to 4.6.0 do not properly 123 * handle updates to enabled MSI-X table entries, 124 * so disable MSI-X interrupt migration in that 125 * case. 126 */ 127 if (bootverbose) 128 printf( 129 "Disabling MSI-X interrupt migration due to Xen hypervisor bug.\n" 130 "Set machdep.msix_disable_migration=0 to forcefully enable it.\n"); 131 msix_disable_migration = 1; 132 } 133 #endif 134 } 135 136 static void 137 hypervisor_version(void) 138 { 139 uint32_t regs[4]; 140 int major, minor; 141 142 do_cpuid(xen_cpuid_base + 1, regs); 143 144 major = regs[0] >> 16; 145 minor = regs[0] & 0xffff; 146 printf("XEN: Hypervisor version %d.%d detected.\n", major, minor); 147 148 hypervisor_quirks(major, minor); 149 } 150 151 /* 152 * Allocate and fill in the hypcall page. 153 */ 154 int 155 xen_hvm_init_hypercall_stubs(enum xen_hvm_init_type init_type) 156 { 157 uint32_t regs[4]; 158 159 /* Legacy PVH will get here without the cpuid leaf being set. */ 160 if (xen_cpuid_base == 0) 161 xen_cpuid_base = xen_hvm_cpuid_base(); 162 if (xen_cpuid_base == 0) 163 return (ENXIO); 164 165 if (xen_domain() && init_type == XEN_HVM_INIT_LATE) { 166 /* 167 * If the domain type is already set we can assume that the 168 * hypercall page has been populated too, so just print the 169 * version (and apply any quirks) and exit. 170 */ 171 hypervisor_version(); 172 return 0; 173 } 174 175 if (init_type == XEN_HVM_INIT_LATE) 176 hypervisor_version(); 177 178 /* 179 * Find the hypercall pages. 180 */ 181 do_cpuid(xen_cpuid_base + 2, regs); 182 if (regs[0] != 1) 183 return (EINVAL); 184 185 wrmsr(regs[1], (init_type == XEN_HVM_INIT_EARLY) 186 ? (vm_paddr_t)((uintptr_t)&hypercall_page - KERNBASE) 187 : vtophys(&hypercall_page)); 188 189 return (0); 190 } 191 192 static void 193 xen_hvm_init_shared_info_page(void) 194 { 195 struct xen_add_to_physmap xatp; 196 197 if (xen_pv_domain()) { 198 /* 199 * Already setup in the PV case, shared_info is passed inside 200 * of the start_info struct at start of day. 201 */ 202 return; 203 } 204 205 if (HYPERVISOR_shared_info == NULL) { 206 HYPERVISOR_shared_info = malloc(PAGE_SIZE, M_XENHVM, M_NOWAIT); 207 if (HYPERVISOR_shared_info == NULL) 208 panic("Unable to allocate Xen shared info page"); 209 } 210 211 xatp.domid = DOMID_SELF; 212 xatp.idx = 0; 213 xatp.space = XENMAPSPACE_shared_info; 214 xatp.gpfn = vtophys(HYPERVISOR_shared_info) >> PAGE_SHIFT; 215 if (HYPERVISOR_memory_op(XENMEM_add_to_physmap, &xatp)) 216 panic("HYPERVISOR_memory_op failed"); 217 } 218 219 static int 220 set_percpu_callback(unsigned int vcpu) 221 { 222 struct xen_hvm_evtchn_upcall_vector vec; 223 int error; 224 225 vec.vcpu = vcpu; 226 vec.vector = IDT_EVTCHN; 227 error = HYPERVISOR_hvm_op(HVMOP_set_evtchn_upcall_vector, &vec); 228 229 return (error != 0 ? xen_translate_error(error) : 0); 230 } 231 232 /* 233 * Tell the hypervisor how to contact us for event channel callbacks. 234 */ 235 void 236 xen_hvm_set_callback(device_t dev) 237 { 238 struct xen_hvm_param xhp; 239 int irq; 240 241 if (xen_vector_callback_enabled) 242 return; 243 244 xhp.domid = DOMID_SELF; 245 xhp.index = HVM_PARAM_CALLBACK_IRQ; 246 if (xen_feature(XENFEAT_hvm_callback_vector) != 0) { 247 int error; 248 249 error = set_percpu_callback(0); 250 if (error == 0) { 251 xen_evtchn_needs_ack = true; 252 /* Trick toolstack to think we are enlightened */ 253 xhp.value = 1; 254 } else 255 xhp.value = HVM_CALLBACK_VECTOR(IDT_EVTCHN); 256 error = HYPERVISOR_hvm_op(HVMOP_set_param, &xhp); 257 if (error == 0) { 258 xen_vector_callback_enabled = 1; 259 return; 260 } else if (xen_evtchn_needs_ack) 261 panic("Unable to setup fake HVM param: %d", error); 262 263 printf("Xen HVM callback vector registration failed (%d). " 264 "Falling back to emulated device interrupt\n", error); 265 } 266 xen_vector_callback_enabled = 0; 267 if (dev == NULL) { 268 /* 269 * Called from early boot or resume. 270 * xenpci will invoke us again later. 271 */ 272 return; 273 } 274 275 irq = pci_get_irq(dev); 276 if (irq < 16) { 277 xhp.value = HVM_CALLBACK_GSI(irq); 278 } else { 279 u_int slot; 280 u_int pin; 281 282 slot = pci_get_slot(dev); 283 pin = pci_get_intpin(dev) - 1; 284 xhp.value = HVM_CALLBACK_PCI_INTX(slot, pin); 285 } 286 287 if (HYPERVISOR_hvm_op(HVMOP_set_param, &xhp) != 0) 288 panic("Can't set evtchn callback"); 289 } 290 291 #define XEN_MAGIC_IOPORT 0x10 292 enum { 293 XMI_MAGIC = 0x49d2, 294 XMI_UNPLUG_IDE_DISKS = 0x01, 295 XMI_UNPLUG_NICS = 0x02, 296 XMI_UNPLUG_IDE_EXCEPT_PRI_MASTER = 0x04 297 }; 298 299 static void 300 xen_hvm_disable_emulated_devices(void) 301 { 302 u_short disable_devs = 0; 303 304 if (xen_pv_domain()) { 305 /* 306 * No emulated devices in the PV case, so no need to unplug 307 * anything. 308 */ 309 if (xen_disable_pv_disks != 0 || xen_disable_pv_nics != 0) 310 printf("PV devices cannot be disabled in PV guests\n"); 311 return; 312 } 313 314 if (inw(XEN_MAGIC_IOPORT) != XMI_MAGIC) 315 return; 316 317 if (xen_disable_pv_disks == 0) { 318 if (bootverbose) 319 printf("XEN: disabling emulated disks\n"); 320 disable_devs |= XMI_UNPLUG_IDE_DISKS; 321 } 322 if (xen_disable_pv_nics == 0) { 323 if (bootverbose) 324 printf("XEN: disabling emulated nics\n"); 325 disable_devs |= XMI_UNPLUG_NICS; 326 } 327 328 if (disable_devs != 0) 329 outw(XEN_MAGIC_IOPORT, disable_devs); 330 } 331 332 static void 333 xen_hvm_init(enum xen_hvm_init_type init_type) 334 { 335 int error; 336 int i; 337 338 if (init_type == XEN_HVM_INIT_CANCELLED_SUSPEND) 339 return; 340 341 error = xen_hvm_init_hypercall_stubs(init_type); 342 343 switch (init_type) { 344 case XEN_HVM_INIT_LATE: 345 if (error != 0) 346 return; 347 348 /* 349 * If xen_domain_type is not set at this point 350 * it means we are inside a (PV)HVM guest, because 351 * for PVH the guest type is set much earlier 352 * (see hammer_time_xen). 353 */ 354 if (!xen_domain()) { 355 xen_domain_type = XEN_HVM_DOMAIN; 356 vm_guest = VM_GUEST_XEN; 357 } 358 359 setup_xen_features(); 360 #ifdef SMP 361 cpu_ops = xen_hvm_cpu_ops; 362 #endif 363 break; 364 case XEN_HVM_INIT_RESUME: 365 if (error != 0) 366 panic("Unable to init Xen hypercall stubs on resume"); 367 368 /* Clear stale vcpu_info. */ 369 CPU_FOREACH(i) 370 DPCPU_ID_SET(i, vcpu_info, NULL); 371 break; 372 default: 373 panic("Unsupported HVM initialization type"); 374 } 375 376 xen_vector_callback_enabled = 0; 377 xen_evtchn_needs_ack = false; 378 xen_hvm_set_callback(NULL); 379 380 /* 381 * On (PV)HVM domains we need to request the hypervisor to 382 * fill the shared info page, for PVH guest the shared_info page 383 * is passed inside the start_info struct and is already set, so this 384 * functions are no-ops. 385 */ 386 xen_hvm_init_shared_info_page(); 387 xen_hvm_disable_emulated_devices(); 388 } 389 390 void 391 xen_hvm_suspend(void) 392 { 393 } 394 395 void 396 xen_hvm_resume(bool suspend_cancelled) 397 { 398 399 xen_hvm_init(suspend_cancelled ? 400 XEN_HVM_INIT_CANCELLED_SUSPEND : XEN_HVM_INIT_RESUME); 401 402 /* Register vcpu_info area for CPU#0. */ 403 xen_hvm_cpu_init(); 404 } 405 406 static void 407 xen_hvm_sysinit(void *arg __unused) 408 { 409 xen_hvm_init(XEN_HVM_INIT_LATE); 410 } 411 SYSINIT(xen_hvm_init, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, xen_hvm_sysinit, NULL); 412 413 static void 414 xen_hvm_cpu_init(void) 415 { 416 uint32_t regs[4]; 417 int rc; 418 419 if (!xen_domain()) 420 return; 421 422 if (DPCPU_GET(vcpu_info) != NULL) { 423 /* 424 * vcpu_info is already set. We're resuming 425 * from a failed migration and our pre-suspend 426 * configuration is still valid. 427 */ 428 return; 429 } 430 431 /* 432 * Set vCPU ID. If available fetch the ID from CPUID, if not just use 433 * the ACPI ID. 434 */ 435 KASSERT(xen_cpuid_base != 0, ("Invalid base Xen CPUID leaf")); 436 cpuid_count(xen_cpuid_base + 4, 0, regs); 437 KASSERT((regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) || 438 !xen_pv_domain(), 439 ("Xen PV domain without vcpu_id in cpuid")); 440 PCPU_SET(vcpu_id, (regs[0] & XEN_HVM_CPUID_VCPU_ID_PRESENT) ? 441 regs[1] : PCPU_GET(acpi_id)); 442 443 if (xen_evtchn_needs_ack && !IS_BSP()) { 444 /* 445 * Setup the per-vpcu event channel upcall vector. This is only 446 * required when using the new HVMOP_set_evtchn_upcall_vector 447 * hypercall, which allows using a different vector for each 448 * vCPU. Note that FreeBSD uses the same vector for all vCPUs 449 * because it's not dynamically allocated. 450 */ 451 rc = set_percpu_callback(PCPU_GET(vcpu_id)); 452 if (rc != 0) 453 panic("Event channel upcall vector setup failed: %d", 454 rc); 455 } 456 457 xen_setup_vcpu_info(); 458 } 459 SYSINIT(xen_hvm_cpu_init, SI_SUB_INTR, SI_ORDER_FIRST, xen_hvm_cpu_init, NULL); 460 461 bool 462 xen_has_iommu_maps(void) 463 { 464 uint32_t regs[4]; 465 466 KASSERT(xen_cpuid_base != 0, ("Invalid base Xen CPUID leaf")); 467 cpuid_count(xen_cpuid_base + 4, 0, regs); 468 469 return (regs[0] & XEN_HVM_CPUID_IOMMU_MAPPINGS); 470 } 471