2 * Copyright (c) 2009-2012,2016 Microsoft Corp.
3 * Copyright (c) 2012 NetApp Inc.
4 * Copyright (c) 2012 Citrix Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
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.
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.
30 * Implements low-level interactions with Hypver-V/Azure
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/timetc.h>
40 #include <dev/hyperv/include/hyperv.h>
41 #include <dev/hyperv/include/hyperv_busdma.h>
42 #include <dev/hyperv/vmbus/hyperv_machdep.h>
43 #include <dev/hyperv/vmbus/hyperv_reg.h>
44 #include <dev/hyperv/vmbus/hyperv_var.h>
46 #define HYPERV_FREEBSD_BUILD 0ULL
47 #define HYPERV_FREEBSD_VERSION ((uint64_t)__FreeBSD_version)
48 #define HYPERV_FREEBSD_OSID 0ULL
50 #define MSR_HV_GUESTID_BUILD_FREEBSD \
51 (HYPERV_FREEBSD_BUILD & MSR_HV_GUESTID_BUILD_MASK)
52 #define MSR_HV_GUESTID_VERSION_FREEBSD \
53 ((HYPERV_FREEBSD_VERSION << MSR_HV_GUESTID_VERSION_SHIFT) & \
54 MSR_HV_GUESTID_VERSION_MASK)
55 #define MSR_HV_GUESTID_OSID_FREEBSD \
56 ((HYPERV_FREEBSD_OSID << MSR_HV_GUESTID_OSID_SHIFT) & \
57 MSR_HV_GUESTID_OSID_MASK)
59 #define MSR_HV_GUESTID_FREEBSD \
60 (MSR_HV_GUESTID_BUILD_FREEBSD | \
61 MSR_HV_GUESTID_VERSION_FREEBSD | \
62 MSR_HV_GUESTID_OSID_FREEBSD | \
63 MSR_HV_GUESTID_OSTYPE_FREEBSD)
65 struct hypercall_ctx {
67 struct hyperv_dma hc_dma;
70 static u_int hyperv_get_timecount(struct timecounter *);
71 static bool hyperv_identify(void);
72 static void hypercall_memfree(void);
74 u_int hyperv_features;
75 u_int hyperv_recommends;
77 static u_int hyperv_pm_features;
78 static u_int hyperv_features3;
80 static struct timecounter hyperv_timecounter = {
81 .tc_get_timecount = hyperv_get_timecount,
83 .tc_counter_mask = 0xffffffff,
84 .tc_frequency = HYPERV_TIMER_FREQ,
91 static struct hypercall_ctx hypercall_context;
94 hyperv_get_timecount(struct timecounter *tc __unused)
96 return rdmsr(MSR_HV_TIME_REF_COUNT);
100 hypercall_post_message(bus_addr_t msg_paddr)
102 return hypercall_md(hypercall_context.hc_addr,
103 HYPERCALL_POST_MESSAGE, msg_paddr, 0);
107 hypercall_signal_event(bus_addr_t monprm_paddr)
109 return hypercall_md(hypercall_context.hc_addr,
110 HYPERCALL_SIGNAL_EVENT, monprm_paddr, 0);
114 hyperv_guid2str(const struct hyperv_guid *guid, char *buf, size_t sz)
116 const uint8_t *d = guid->hv_guid;
118 return snprintf(buf, sz, "%02x%02x%02x%02x-"
119 "%02x%02x-%02x%02x-%02x%02x-"
120 "%02x%02x%02x%02x%02x%02x",
121 d[3], d[2], d[1], d[0],
122 d[5], d[4], d[7], d[6], d[8], d[9],
123 d[10], d[11], d[12], d[13], d[14], d[15]);
127 hyperv_identify(void)
130 unsigned int maxleaf;
132 if (vm_guest != VM_GUEST_HV)
135 do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs);
137 if (maxleaf < CPUID_LEAF_HV_LIMITS)
140 do_cpuid(CPUID_LEAF_HV_INTERFACE, regs);
141 if (regs[0] != CPUID_HV_IFACE_HYPERV)
144 do_cpuid(CPUID_LEAF_HV_FEATURES, regs);
145 if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0) {
147 * Hyper-V w/o Hypercall is impossible; someone
152 hyperv_features = regs[0];
153 hyperv_pm_features = regs[2];
154 hyperv_features3 = regs[3];
156 do_cpuid(CPUID_LEAF_HV_IDENTITY, regs);
157 printf("Hyper-V Version: %d.%d.%d [SP%d]\n",
158 regs[1] >> 16, regs[1] & 0xffff, regs[0], regs[2]);
160 printf(" Features=0x%b\n", hyperv_features,
162 "\001VPRUNTIME" /* MSR_HV_VP_RUNTIME */
163 "\002TMREFCNT" /* MSR_HV_TIME_REF_COUNT */
164 "\003SYNIC" /* MSRs for SynIC */
165 "\004SYNTM" /* MSRs for SynTimer */
166 "\005APIC" /* MSR_HV_{EOI,ICR,TPR} */
167 "\006HYPERCALL" /* MSR_HV_{GUEST_OS_ID,HYPERCALL} */
168 "\007VPINDEX" /* MSR_HV_VP_INDEX */
169 "\010RESET" /* MSR_HV_RESET */
170 "\011STATS" /* MSR_HV_STATS_ */
171 "\012REFTSC" /* MSR_HV_REFERENCE_TSC */
172 "\013IDLE" /* MSR_HV_GUEST_IDLE */
173 "\014TMFREQ" /* MSR_HV_{TSC,APIC}_FREQUENCY */
174 "\015DEBUG"); /* MSR_HV_SYNTH_DEBUG_ */
175 printf(" PM Features=0x%b [C%u]\n",
176 (hyperv_pm_features & ~CPUPM_HV_CSTATE_MASK),
178 "\005C3HPET", /* HPET is required for C3 state */
179 CPUPM_HV_CSTATE(hyperv_pm_features));
180 printf(" Features3=0x%b\n", hyperv_features3,
182 "\001MWAIT" /* MWAIT */
183 "\002DEBUG" /* guest debug support */
184 "\003PERFMON" /* performance monitor */
185 "\004PCPUDPE" /* physical CPU dynamic partition event */
186 "\005XMMHC" /* hypercall input through XMM regs */
187 "\006IDLE" /* guest idle support */
188 "\007SLEEP" /* hypervisor sleep support */
189 "\010NUMA" /* NUMA distance query support */
190 "\011TMFREQ" /* timer frequency query (TSC, LAPIC) */
191 "\012SYNCMC" /* inject synthetic machine checks */
192 "\013CRASH" /* MSRs for guest crash */
193 "\014DEBUGMSR" /* MSRs for guest debug */
194 "\015NPIEP" /* NPIEP */
195 "\016HVDIS"); /* disabling hypervisor */
197 do_cpuid(CPUID_LEAF_HV_RECOMMENDS, regs);
198 hyperv_recommends = regs[0];
200 printf(" Recommends: %08x %08x\n", regs[0], regs[1]);
202 do_cpuid(CPUID_LEAF_HV_LIMITS, regs);
204 printf(" Limits: Vcpu:%d Lcpu:%d Int:%d\n",
205 regs[0], regs[1], regs[2]);
208 if (maxleaf >= CPUID_LEAF_HV_HWFEATURES) {
209 do_cpuid(CPUID_LEAF_HV_HWFEATURES, regs);
211 printf(" HW Features: %08x, AMD: %08x\n",
220 hyperv_init(void *dummy __unused)
222 if (!hyperv_identify()) {
223 /* Not Hyper-V; reset guest id to the generic one. */
224 if (vm_guest == VM_GUEST_HV)
225 vm_guest = VM_GUEST_VM;
230 wrmsr(MSR_HV_GUEST_OS_ID, MSR_HV_GUESTID_FREEBSD);
232 if (hyperv_features & CPUID_HV_MSR_TIME_REFCNT) {
233 /* Register Hyper-V timecounter */
234 tc_init(&hyperv_timecounter);
237 SYSINIT(hyperv_initialize, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, hyperv_init,
241 hypercall_memfree(void)
243 hyperv_dmamem_free(&hypercall_context.hc_dma,
244 hypercall_context.hc_addr);
245 hypercall_context.hc_addr = NULL;
249 hypercall_create(void *arg __unused)
251 uint64_t hc, hc_orig;
253 if (vm_guest != VM_GUEST_HV)
256 hypercall_context.hc_addr = hyperv_dmamem_alloc(NULL, PAGE_SIZE, 0,
257 PAGE_SIZE, &hypercall_context.hc_dma, BUS_DMA_WAITOK);
258 if (hypercall_context.hc_addr == NULL) {
259 printf("hyperv: Hypercall page allocation failed\n");
260 /* Can't perform any Hyper-V specific actions */
261 vm_guest = VM_GUEST_VM;
265 /* Get the 'reserved' bits, which requires preservation. */
266 hc_orig = rdmsr(MSR_HV_HYPERCALL);
269 * Setup the Hypercall page.
271 * NOTE: 'reserved' bits MUST be preserved.
273 hc = ((hypercall_context.hc_dma.hv_paddr >> PAGE_SHIFT) <<
274 MSR_HV_HYPERCALL_PGSHIFT) |
275 (hc_orig & MSR_HV_HYPERCALL_RSVD_MASK) |
276 MSR_HV_HYPERCALL_ENABLE;
277 wrmsr(MSR_HV_HYPERCALL, hc);
280 * Confirm that Hypercall page did get setup.
282 hc = rdmsr(MSR_HV_HYPERCALL);
283 if ((hc & MSR_HV_HYPERCALL_ENABLE) == 0) {
284 printf("hyperv: Hypercall setup failed\n");
286 /* Can't perform any Hyper-V specific actions */
287 vm_guest = VM_GUEST_VM;
291 printf("hyperv: Hypercall created\n");
293 SYSINIT(hypercall_ctor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_create, NULL);
296 hypercall_destroy(void *arg __unused)
300 if (hypercall_context.hc_addr == NULL)
303 /* Disable Hypercall */
304 hc = rdmsr(MSR_HV_HYPERCALL);
305 wrmsr(MSR_HV_HYPERCALL, (hc & MSR_HV_HYPERCALL_RSVD_MASK));
309 printf("hyperv: Hypercall destroyed\n");
311 SYSUNINIT(hypercall_dtor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_destroy,