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