1 /*- SPDX-License-Identifier: BSD-2-Clause-FreeBSD 2 * Copyright (c) 2009-2012,2016-2017, 2022 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 /** 30 * Implements low-level interactions with Hyper-V/Azure 31 */ 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/kernel.h> 38 #include <sys/malloc.h> 39 #include <sys/timetc.h> 40 41 #include <vm/vm.h> 42 #include <vm/pmap.h> 43 #include <vm/vm_extern.h> 44 #include <vm/vm_kern.h> 45 46 #include <dev/hyperv/include/hyperv.h> 47 #include <dev/hyperv/include/hyperv_busdma.h> 48 #include <dev/hyperv/vmbus/hyperv_var.h> 49 #include <dev/hyperv/vmbus/x86/hyperv_machdep.h> 50 #include <dev/hyperv/vmbus/x86/hyperv_reg.h> 51 52 #define HYPERV_FREEBSD_BUILD 0ULL 53 #define HYPERV_FREEBSD_VERSION ((uint64_t)__FreeBSD_version) 54 #define HYPERV_FREEBSD_OSID 0ULL 55 56 void hyperv_init_tc(void); 57 int hypercall_page_setup(vm_paddr_t); 58 void hypercall_disable(void); 59 bool hyperv_identify_features(void); 60 61 u_int hyperv_ver_major; 62 u_int hyperv_features; 63 u_int hyperv_recommends; 64 65 hyperv_tc64_t hyperv_tc64; 66 67 static u_int hyperv_pm_features; 68 static u_int hyperv_features3; 69 static u_int hyperv_get_timecount(struct timecounter *); 70 71 static struct timecounter hyperv_timecounter = { 72 .tc_get_timecount = hyperv_get_timecount, 73 .tc_poll_pps = NULL, 74 .tc_counter_mask = 0xffffffff, 75 .tc_frequency = HYPERV_TIMER_FREQ, 76 .tc_name = "Hyper-V", 77 .tc_quality = 2000, 78 .tc_flags = 0, 79 .tc_priv = NULL 80 }; 81 82 static u_int 83 hyperv_get_timecount(struct timecounter *tc __unused) 84 { 85 return rdmsr(MSR_HV_TIME_REF_COUNT); 86 } 87 88 static uint64_t 89 hyperv_tc64_rdmsr(void) 90 { 91 92 return (rdmsr(MSR_HV_TIME_REF_COUNT)); 93 } 94 95 void 96 hyperv_init_tc(void) 97 { 98 if (hyperv_features & CPUID_HV_MSR_TIME_REFCNT) { 99 /* 100 * Register Hyper-V timecounter. This should be done as early 101 * as possible to let DELAY() work, since the 8254 PIT is not 102 * reliably emulated or even available. 103 */ 104 tc_init(&hyperv_timecounter); 105 106 /* 107 * Install 64 bits timecounter method for other modules 108 * to use. 109 */ 110 hyperv_tc64 = hyperv_tc64_rdmsr; 111 } 112 } 113 114 int 115 hypercall_page_setup(vm_paddr_t paddr) 116 { 117 uint64_t hc, hc_orig; 118 hc_orig = rdmsr(MSR_HV_HYPERCALL); 119 120 /* 121 * Setup the Hypercall page. 122 * 123 * NOTE: 'reserved' bits MUST be preserved. 124 */ 125 hc = ((paddr >> PAGE_SHIFT) << MSR_HV_HYPERCALL_PGSHIFT) | 126 (hc_orig & MSR_HV_HYPERCALL_RSVD_MASK) | MSR_HV_HYPERCALL_ENABLE; 127 wrmsr(MSR_HV_HYPERCALL, hc); 128 129 /* 130 * Confirm that Hypercall page did get setup. 131 */ 132 hc = rdmsr(MSR_HV_HYPERCALL); 133 if ((hc & MSR_HV_HYPERCALL_ENABLE) == 0) { 134 printf("hyperv: Hypercall setup failed\n"); 135 /* Can't perform any Hyper-V specific actions */ 136 vm_guest = VM_GUEST_VM; 137 return (-1); 138 } 139 return (0); 140 } 141 142 void 143 hypercall_disable(void) 144 { 145 uint64_t hc; 146 /* Disable Hypercall */ 147 hc = rdmsr(MSR_HV_HYPERCALL); 148 wrmsr(MSR_HV_HYPERCALL, (hc & MSR_HV_HYPERCALL_RSVD_MASK)); 149 } 150 151 bool 152 hyperv_identify_features(void) 153 { 154 u_int regs[4]; 155 unsigned int maxleaf; 156 157 if (vm_guest != VM_GUEST_HV) 158 return (false); 159 160 do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs); 161 maxleaf = regs[0]; 162 if (maxleaf < CPUID_LEAF_HV_LIMITS) 163 return (false); 164 165 do_cpuid(CPUID_LEAF_HV_INTERFACE, regs); 166 if (regs[0] != CPUID_HV_IFACE_HYPERV) 167 return (false); 168 169 do_cpuid(CPUID_LEAF_HV_FEATURES, regs); 170 if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0) { 171 /* 172 * Hyper-V w/o Hypercall is impossible; someone 173 * is faking Hyper-V. 174 */ 175 return (false); 176 } 177 hyperv_features = regs[0]; 178 hyperv_pm_features = regs[2]; 179 hyperv_features3 = regs[3]; 180 do_cpuid(CPUID_LEAF_HV_IDENTITY, regs); 181 hyperv_ver_major = regs[1] >> 16; 182 printf("Hyper-V Version: %d.%d.%d [SP%d]\n", hyperv_ver_major, 183 regs[1] & 0xffff, regs[0], regs[2]); 184 185 printf(" Features=0x%b\n", hyperv_features, 186 "\020" 187 "\001VPRUNTIME" /* MSR_HV_VP_RUNTIME */ 188 "\002TMREFCNT" /* MSR_HV_TIME_REF_COUNT */ 189 "\003SYNIC" /* MSRs for SynIC */ 190 "\004SYNTM" /* MSRs for SynTimer */ 191 "\005APIC" /* MSR_HV_{EOI,ICR,TPR} */ 192 "\006HYPERCALL" /* MSR_HV_{GUEST_OS_ID,HYPERCALL} */ 193 "\007VPINDEX" /* MSR_HV_VP_INDEX */ 194 "\010RESET" /* MSR_HV_RESET */ 195 "\011STATS" /* MSR_HV_STATS_ */ 196 "\012REFTSC" /* MSR_HV_REFERENCE_TSC */ 197 "\013IDLE" /* MSR_HV_GUEST_IDLE */ 198 "\014TMFREQ" /* MSR_HV_{TSC,APIC}_FREQUENCY */ 199 "\015DEBUG"); /* MSR_HV_SYNTH_DEBUG_ */ 200 printf(" PM Features=0x%b [C%u]\n", 201 (hyperv_pm_features & ~CPUPM_HV_CSTATE_MASK), 202 "\020" 203 "\005C3HPET", /* HPET is required for C3 state */ 204 CPUPM_HV_CSTATE(hyperv_pm_features)); 205 printf(" Features3=0x%b\n", hyperv_features3, 206 "\020" 207 "\001MWAIT" /* MWAIT */ 208 "\002DEBUG" /* guest debug support */ 209 "\003PERFMON" /* performance monitor */ 210 "\004PCPUDPE" /* physical CPU dynamic partition event */ 211 "\005XMMHC" /* hypercall input through XMM regs */ 212 "\006IDLE" /* guest idle support */ 213 "\007SLEEP" /* hypervisor sleep support */ 214 "\010NUMA" /* NUMA distance query support */ 215 "\011TMFREQ" /* timer frequency query (TSC, LAPIC) */ 216 "\012SYNCMC" /* inject synthetic machine checks */ 217 "\013CRASH" /* MSRs for guest crash */ 218 "\014DEBUGMSR" /* MSRs for guest debug */ 219 "\015NPIEP" /* NPIEP */ 220 "\016HVDIS"); /* disabling hypervisor */ 221 222 do_cpuid(CPUID_LEAF_HV_RECOMMENDS, regs); 223 hyperv_recommends = regs[0]; 224 if (bootverbose) 225 printf(" Recommends: %08x %08x\n", regs[0], regs[1]); 226 227 do_cpuid(CPUID_LEAF_HV_LIMITS, regs); 228 if (bootverbose) { 229 printf(" Limits: Vcpu:%d Lcpu:%d Int:%d\n", regs[0], regs[1], 230 regs[2]); 231 } 232 233 if (maxleaf >= CPUID_LEAF_HV_HWFEATURES) { 234 do_cpuid(CPUID_LEAF_HV_HWFEATURES, regs); 235 if (bootverbose) { 236 printf(" HW Features: %08x, AMD: %08x\n", regs[0], 237 regs[3]); 238 } 239 } 240 return (true); 241 } 242