1 /*- 2 * Copyright (c) 2009-2012,2016 Microsoft Corp. 3 * Copyright (c) 2012 NetApp Inc. 4 * Copyright (c) 2012 Citrix Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * 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 ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/param.h> 30 #include <sys/kernel.h> 31 #include <sys/systimer.h> 32 #include <sys/systm.h> 33 34 #include <machine/cpufunc.h> 35 36 #include <dev/virtual/hyperv/hyperv_busdma.h> 37 #include <dev/virtual/hyperv/hyperv_machdep.h> 38 #include <dev/virtual/hyperv/hyperv_reg.h> 39 #include <dev/virtual/hyperv/hyperv_var.h> 40 41 #define HYPERV_DRAGONFLY_BUILD 0ULL 42 #define HYPERV_DRAGONFLY_VERSION ((uint64_t)__DragonFly_version) 43 #define HYPERV_DRAGONFLY_OSID 0ULL 44 45 #define MSR_HV_GUESTID_BUILD_DRAGONFLY \ 46 (HYPERV_DRAGONFLY_BUILD & MSR_HV_GUESTID_BUILD_MASK) 47 #define MSR_HV_GUESTID_VERSION_DRAGONFLY \ 48 ((HYPERV_DRAGONFLY_VERSION << MSR_HV_GUESTID_VERSION_SHIFT) & \ 49 MSR_HV_GUESTID_VERSION_MASK) 50 #define MSR_HV_GUESTID_OSID_DRAGONFLY \ 51 ((HYPERV_DRAGONFLY_OSID << MSR_HV_GUESTID_OSID_SHIFT) & \ 52 MSR_HV_GUESTID_OSID_MASK) 53 54 #define MSR_HV_GUESTID_DRAGONFLY \ 55 (MSR_HV_GUESTID_BUILD_DRAGONFLY | \ 56 MSR_HV_GUESTID_VERSION_DRAGONFLY | \ 57 MSR_HV_GUESTID_OSID_DRAGONFLY | \ 58 MSR_HV_GUESTID_OSTYPE_FREEBSD) 59 60 struct hypercall_ctx { 61 void *hc_addr; 62 struct hyperv_dma hc_dma; 63 }; 64 65 static void hyperv_cputimer_construct(struct cputimer *, 66 sysclock_t); 67 static sysclock_t hyperv_cputimer_count(void); 68 static boolean_t hyperv_identify(void); 69 static int hypercall_create(void); 70 static void hypercall_destroy(void); 71 static void hypercall_memfree(void); 72 static uint64_t hyperv_tc64_rdmsr(void); 73 74 u_int hyperv_features; 75 static u_int hyperv_recommends; 76 77 static u_int hyperv_pm_features; 78 static u_int hyperv_features3; 79 80 hyperv_tc64_t hyperv_tc64; 81 82 static struct cputimer hyperv_cputimer = { 83 .next = SLIST_ENTRY_INITIALIZER, 84 .name = "Hyper-V", 85 .pri = CPUTIMER_PRI_VMM, 86 .type = CPUTIMER_VMM, 87 .count = hyperv_cputimer_count, 88 .fromhz = cputimer_default_fromhz, 89 .fromus = cputimer_default_fromus, 90 .construct = hyperv_cputimer_construct, 91 .destruct = cputimer_default_destruct, 92 .freq = HYPERV_TIMER_FREQ 93 }; 94 95 static struct hypercall_ctx hypercall_context; 96 97 uint64_t 98 hypercall_post_message(bus_addr_t msg_paddr) 99 { 100 return hypercall_md(hypercall_context.hc_addr, 101 HYPERCALL_POST_MESSAGE, msg_paddr, 0); 102 } 103 104 static void 105 hyperv_cputimer_construct(struct cputimer *timer, sysclock_t oldclock) 106 { 107 timer->base = 0; 108 timer->base = oldclock - timer->count(); 109 } 110 111 static sysclock_t 112 hyperv_cputimer_count(void) 113 { 114 uint64_t val; 115 116 val = rdmsr(MSR_HV_TIME_REF_COUNT); 117 return (val + hyperv_cputimer.base); 118 } 119 120 static void 121 hypercall_memfree(void) 122 { 123 hyperv_dmamem_free(&hypercall_context.hc_dma, 124 hypercall_context.hc_addr); 125 hypercall_context.hc_addr = NULL; 126 } 127 128 static int 129 hypercall_create(void) 130 { 131 uint64_t hc, hc_orig; 132 133 hypercall_context.hc_addr = hyperv_dmamem_alloc(NULL, PAGE_SIZE, 0, 134 PAGE_SIZE, &hypercall_context.hc_dma, BUS_DMA_WAITOK); 135 if (hypercall_context.hc_addr == NULL) { 136 kprintf("hyperv: Hypercall page allocation failed\n"); 137 return ENOMEM; 138 } 139 140 /* Get the 'reserved' bits, which requires preservation. */ 141 hc_orig = rdmsr(MSR_HV_HYPERCALL); 142 143 /* 144 * Setup the Hypercall page. 145 * 146 * NOTE: 'reserved' bits MUST be preserved. 147 */ 148 hc = ((hypercall_context.hc_dma.hv_paddr >> PAGE_SHIFT) << 149 MSR_HV_HYPERCALL_PGSHIFT) | 150 (hc_orig & MSR_HV_HYPERCALL_RSVD_MASK) | 151 MSR_HV_HYPERCALL_ENABLE; 152 wrmsr(MSR_HV_HYPERCALL, hc); 153 154 /* 155 * Confirm that Hypercall page did get setup. 156 */ 157 hc = rdmsr(MSR_HV_HYPERCALL); 158 if ((hc & MSR_HV_HYPERCALL_ENABLE) == 0) { 159 kprintf("hyperv: Hypercall setup failed\n"); 160 hypercall_memfree(); 161 return EIO; 162 } 163 if (bootverbose) 164 kprintf("hyperv: Hypercall created\n"); 165 166 return 0; 167 } 168 169 static void 170 hypercall_destroy(void) 171 { 172 uint64_t hc; 173 174 if (hypercall_context.hc_addr == NULL) 175 return; 176 177 /* Disable Hypercall */ 178 hc = rdmsr(MSR_HV_HYPERCALL); 179 wrmsr(MSR_HV_HYPERCALL, (hc & MSR_HV_HYPERCALL_RSVD_MASK)); 180 hypercall_memfree(); 181 182 if (bootverbose) 183 kprintf("hyperv: Hypercall destroyed\n"); 184 } 185 186 static uint64_t 187 hyperv_tc64_rdmsr(void) 188 { 189 190 return (rdmsr(MSR_HV_TIME_REF_COUNT)); 191 } 192 193 static boolean_t 194 hyperv_identify(void) 195 { 196 u_int regs[4]; 197 unsigned int maxleaf; 198 199 if (vmm_guest != VMM_GUEST_HYPERV) 200 return (FALSE); 201 202 do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs); 203 maxleaf = regs[0]; 204 if (maxleaf < CPUID_LEAF_HV_LIMITS) 205 return (FALSE); 206 207 do_cpuid(CPUID_LEAF_HV_INTERFACE, regs); 208 if (regs[0] != CPUID_HV_IFACE_HYPERV) 209 return (FALSE); 210 211 do_cpuid(CPUID_LEAF_HV_FEATURES, regs); 212 if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0) { 213 /* 214 * Hyper-V w/o Hypercall is impossible; someone 215 * is faking Hyper-V. 216 */ 217 return (FALSE); 218 } 219 hyperv_features = regs[0]; 220 hyperv_pm_features = regs[2]; 221 hyperv_features3 = regs[3]; 222 223 do_cpuid(CPUID_LEAF_HV_IDENTITY, regs); 224 kprintf("Hyper-V Version: %d.%d.%d [SP%d]\n", 225 regs[1] >> 16, regs[1] & 0xffff, regs[0], regs[2]); 226 227 kprintf(" Features=0x%b\n", hyperv_features, 228 "\020" 229 "\001VPRUNTIME" /* MSR_HV_VP_RUNTIME */ 230 "\002TMREFCNT" /* MSR_HV_TIME_REF_COUNT */ 231 "\003SYNIC" /* MSRs for SynIC */ 232 "\004SYNTM" /* MSRs for SynTimer */ 233 "\005APIC" /* MSR_HV_{EOI,ICR,TPR} */ 234 "\006HYPERCALL" /* MSR_HV_{GUEST_OS_ID,HYPERCALL} */ 235 "\007VPINDEX" /* MSR_HV_VP_INDEX */ 236 "\010RESET" /* MSR_HV_RESET */ 237 "\011STATS" /* MSR_HV_STATS_ */ 238 "\012REFTSC" /* MSR_HV_REFERENCE_TSC */ 239 "\013IDLE" /* MSR_HV_GUEST_IDLE */ 240 "\014TMFREQ" /* MSR_HV_{TSC,APIC}_FREQUENCY */ 241 "\015DEBUG"); /* MSR_HV_SYNTH_DEBUG_ */ 242 kprintf(" PM Features=0x%b [C%u]\n", 243 (hyperv_pm_features & ~CPUPM_HV_CSTATE_MASK), 244 "\020" 245 "\005C3HPET", /* HPET is required for C3 state */ 246 CPUPM_HV_CSTATE(hyperv_pm_features)); 247 kprintf(" Features3=0x%b\n", hyperv_features3, 248 "\020" 249 "\001MWAIT" /* MWAIT */ 250 "\002DEBUG" /* guest debug support */ 251 "\003PERFMON" /* performance monitor */ 252 "\004PCPUDPE" /* physical CPU dynamic partition event */ 253 "\005XMMHC" /* hypercall input through XMM regs */ 254 "\006IDLE" /* guest idle support */ 255 "\007SLEEP" /* hypervisor sleep support */ 256 "\010NUMA" /* NUMA distance query support */ 257 "\011TMFREQ" /* timer frequency query (TSC, LAPIC) */ 258 "\012SYNCMC" /* inject synthetic machine checks */ 259 "\013CRASH" /* MSRs for guest crash */ 260 "\014DEBUGMSR" /* MSRs for guest debug */ 261 "\015NPIEP" /* NPIEP */ 262 "\016HVDIS"); /* disabling hypervisor */ 263 264 do_cpuid(CPUID_LEAF_HV_RECOMMENDS, regs); 265 hyperv_recommends = regs[0]; 266 if (bootverbose) 267 kprintf(" Recommends: %08x %08x\n", regs[0], regs[1]); 268 269 do_cpuid(CPUID_LEAF_HV_LIMITS, regs); 270 if (bootverbose) { 271 kprintf(" Limits: Vcpu:%d Lcpu:%d Int:%d\n", 272 regs[0], regs[1], regs[2]); 273 } 274 275 if (maxleaf >= CPUID_LEAF_HV_HWFEATURES) { 276 do_cpuid(CPUID_LEAF_HV_HWFEATURES, regs); 277 if (bootverbose) { 278 kprintf(" HW Features: %08x, AMD: %08x\n", 279 regs[0], regs[3]); 280 } 281 } 282 283 return (TRUE); 284 } 285 286 static void 287 hyperv_init(void *dummy __unused) 288 { 289 int error; 290 291 if (!hyperv_identify()) { 292 /* Not Hyper-V; reset guest id to the generic one. */ 293 if (vmm_guest == VMM_GUEST_HYPERV) 294 vmm_guest = VMM_GUEST_UNKNOWN; 295 return; 296 } 297 298 /* Set guest id */ 299 wrmsr(MSR_HV_GUEST_OS_ID, MSR_HV_GUESTID_DRAGONFLY); 300 301 if (hyperv_features & CPUID_HV_MSR_TIME_REFCNT) { 302 /* Register Hyper-V systimer */ 303 cputimer_register(&hyperv_cputimer); 304 cputimer_select(&hyperv_cputimer, 0); 305 hyperv_tc64 = hyperv_tc64_rdmsr; 306 } 307 308 error = hypercall_create(); 309 if (error) { 310 /* Can't perform any Hyper-V specific actions */ 311 vmm_guest = VMM_GUEST_UNKNOWN; 312 } 313 314 /* Machine dependent initialization. */ 315 hyperv_md_init(); 316 } 317 SYSINIT(hyperv_initialize, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, 318 hyperv_init, NULL); 319 320 static void 321 hyperv_uninit(void *dummy __unused) 322 { 323 /* Machine dependent uninitialization. */ 324 hyperv_md_uninit(); 325 326 if (hyperv_features & CPUID_HV_MSR_TIME_REFCNT) { 327 /* Deregister Hyper-V systimer */ 328 cputimer_deregister(&hyperv_cputimer); 329 } 330 hypercall_destroy(); 331 } 332 SYSUNINIT(hyperv_uninitialize, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST, 333 hyperv_uninit, NULL); 334