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