2 * Copyright (c) 2009-2012,2016-2017 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 Hyper-V/Azure
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/systm.h>
39 #include <sys/timetc.h>
42 #include <vm/vm_extern.h>
43 #include <vm/vm_kern.h>
46 #include <dev/hyperv/include/hyperv.h>
47 #include <dev/hyperv/include/hyperv_busdma.h>
48 #include <dev/hyperv/vmbus/hyperv_machdep.h>
49 #include <dev/hyperv/vmbus/hyperv_reg.h>
50 #include <dev/hyperv/vmbus/hyperv_var.h>
52 #define HYPERV_FREEBSD_BUILD 0ULL
53 #define HYPERV_FREEBSD_VERSION ((uint64_t)__FreeBSD_version)
54 #define HYPERV_FREEBSD_OSID 0ULL
56 #define MSR_HV_GUESTID_BUILD_FREEBSD \
57 (HYPERV_FREEBSD_BUILD & MSR_HV_GUESTID_BUILD_MASK)
58 #define MSR_HV_GUESTID_VERSION_FREEBSD \
59 ((HYPERV_FREEBSD_VERSION << MSR_HV_GUESTID_VERSION_SHIFT) & \
60 MSR_HV_GUESTID_VERSION_MASK)
61 #define MSR_HV_GUESTID_OSID_FREEBSD \
62 ((HYPERV_FREEBSD_OSID << MSR_HV_GUESTID_OSID_SHIFT) & \
63 MSR_HV_GUESTID_OSID_MASK)
65 #define MSR_HV_GUESTID_FREEBSD \
66 (MSR_HV_GUESTID_BUILD_FREEBSD | \
67 MSR_HV_GUESTID_VERSION_FREEBSD | \
68 MSR_HV_GUESTID_OSID_FREEBSD | \
69 MSR_HV_GUESTID_OSTYPE_FREEBSD)
71 struct hypercall_ctx {
76 static u_int hyperv_get_timecount(struct timecounter *);
77 static bool hyperv_identify(void);
78 static void hypercall_memfree(void);
80 u_int hyperv_ver_major;
82 u_int hyperv_features;
83 u_int hyperv_recommends;
85 static u_int hyperv_pm_features;
86 static u_int hyperv_features3;
88 hyperv_tc64_t hyperv_tc64;
90 static struct timecounter hyperv_timecounter = {
91 .tc_get_timecount = hyperv_get_timecount,
93 .tc_counter_mask = 0xffffffff,
94 .tc_frequency = HYPERV_TIMER_FREQ,
101 static struct hypercall_ctx hypercall_context;
104 hyperv_get_timecount(struct timecounter *tc __unused)
106 return rdmsr(MSR_HV_TIME_REF_COUNT);
110 hyperv_tc64_rdmsr(void)
113 return (rdmsr(MSR_HV_TIME_REF_COUNT));
117 hypercall_post_message(bus_addr_t msg_paddr)
119 return hypercall_md(hypercall_context.hc_addr,
120 HYPERCALL_POST_MESSAGE, msg_paddr, 0);
124 hypercall_signal_event(bus_addr_t monprm_paddr)
126 return hypercall_md(hypercall_context.hc_addr,
127 HYPERCALL_SIGNAL_EVENT, monprm_paddr, 0);
131 hyperv_guid2str(const struct hyperv_guid *guid, char *buf, size_t sz)
133 const uint8_t *d = guid->hv_guid;
135 return snprintf(buf, sz, "%02x%02x%02x%02x-"
136 "%02x%02x-%02x%02x-%02x%02x-"
137 "%02x%02x%02x%02x%02x%02x",
138 d[3], d[2], d[1], d[0],
139 d[5], d[4], d[7], d[6], d[8], d[9],
140 d[10], d[11], d[12], d[13], d[14], d[15]);
144 hyperv_identify(void)
147 unsigned int maxleaf;
149 if (vm_guest != VM_GUEST_HV)
152 do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs);
154 if (maxleaf < CPUID_LEAF_HV_LIMITS)
157 do_cpuid(CPUID_LEAF_HV_INTERFACE, regs);
158 if (regs[0] != CPUID_HV_IFACE_HYPERV)
161 do_cpuid(CPUID_LEAF_HV_FEATURES, regs);
162 if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0) {
164 * Hyper-V w/o Hypercall is impossible; someone
169 hyperv_features = regs[0];
170 hyperv_pm_features = regs[2];
171 hyperv_features3 = regs[3];
173 do_cpuid(CPUID_LEAF_HV_IDENTITY, regs);
174 hyperv_ver_major = regs[1] >> 16;
175 printf("Hyper-V Version: %d.%d.%d [SP%d]\n",
176 hyperv_ver_major, regs[1] & 0xffff, regs[0], regs[2]);
178 printf(" Features=0x%b\n", hyperv_features,
180 "\001VPRUNTIME" /* MSR_HV_VP_RUNTIME */
181 "\002TMREFCNT" /* MSR_HV_TIME_REF_COUNT */
182 "\003SYNIC" /* MSRs for SynIC */
183 "\004SYNTM" /* MSRs for SynTimer */
184 "\005APIC" /* MSR_HV_{EOI,ICR,TPR} */
185 "\006HYPERCALL" /* MSR_HV_{GUEST_OS_ID,HYPERCALL} */
186 "\007VPINDEX" /* MSR_HV_VP_INDEX */
187 "\010RESET" /* MSR_HV_RESET */
188 "\011STATS" /* MSR_HV_STATS_ */
189 "\012REFTSC" /* MSR_HV_REFERENCE_TSC */
190 "\013IDLE" /* MSR_HV_GUEST_IDLE */
191 "\014TMFREQ" /* MSR_HV_{TSC,APIC}_FREQUENCY */
192 "\015DEBUG"); /* MSR_HV_SYNTH_DEBUG_ */
193 printf(" PM Features=0x%b [C%u]\n",
194 (hyperv_pm_features & ~CPUPM_HV_CSTATE_MASK),
196 "\005C3HPET", /* HPET is required for C3 state */
197 CPUPM_HV_CSTATE(hyperv_pm_features));
198 printf(" Features3=0x%b\n", hyperv_features3,
200 "\001MWAIT" /* MWAIT */
201 "\002DEBUG" /* guest debug support */
202 "\003PERFMON" /* performance monitor */
203 "\004PCPUDPE" /* physical CPU dynamic partition event */
204 "\005XMMHC" /* hypercall input through XMM regs */
205 "\006IDLE" /* guest idle support */
206 "\007SLEEP" /* hypervisor sleep support */
207 "\010NUMA" /* NUMA distance query support */
208 "\011TMFREQ" /* timer frequency query (TSC, LAPIC) */
209 "\012SYNCMC" /* inject synthetic machine checks */
210 "\013CRASH" /* MSRs for guest crash */
211 "\014DEBUGMSR" /* MSRs for guest debug */
212 "\015NPIEP" /* NPIEP */
213 "\016HVDIS"); /* disabling hypervisor */
215 do_cpuid(CPUID_LEAF_HV_RECOMMENDS, regs);
216 hyperv_recommends = regs[0];
218 printf(" Recommends: %08x %08x\n", regs[0], regs[1]);
220 do_cpuid(CPUID_LEAF_HV_LIMITS, regs);
222 printf(" Limits: Vcpu:%d Lcpu:%d Int:%d\n",
223 regs[0], regs[1], regs[2]);
226 if (maxleaf >= CPUID_LEAF_HV_HWFEATURES) {
227 do_cpuid(CPUID_LEAF_HV_HWFEATURES, regs);
229 printf(" HW Features: %08x, AMD: %08x\n",
238 hyperv_init(void *dummy __unused)
240 if (!hyperv_identify()) {
241 /* Not Hyper-V; reset guest id to the generic one. */
242 if (vm_guest == VM_GUEST_HV)
243 vm_guest = VM_GUEST_VM;
248 wrmsr(MSR_HV_GUEST_OS_ID, MSR_HV_GUESTID_FREEBSD);
250 if (hyperv_features & CPUID_HV_MSR_TIME_REFCNT) {
251 /* Register Hyper-V timecounter */
252 tc_init(&hyperv_timecounter);
255 * Install 64 bits timecounter method for other modules
258 hyperv_tc64 = hyperv_tc64_rdmsr;
261 SYSINIT(hyperv_initialize, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, hyperv_init,
265 hypercall_memfree(void)
267 kmem_free((vm_offset_t)hypercall_context.hc_addr, PAGE_SIZE);
268 hypercall_context.hc_addr = NULL;
272 hypercall_create(void *arg __unused)
274 uint64_t hc, hc_orig;
276 if (vm_guest != VM_GUEST_HV)
281 * - busdma(9), i.e. hyperv_dmamem APIs, can _not_ be used due to
283 * - Assume kmem_malloc() returns properly aligned memory.
285 hypercall_context.hc_addr = (void *)kmem_malloc(PAGE_SIZE, M_EXEC |
287 hypercall_context.hc_paddr = vtophys(hypercall_context.hc_addr);
289 /* Get the 'reserved' bits, which requires preservation. */
290 hc_orig = rdmsr(MSR_HV_HYPERCALL);
293 * Setup the Hypercall page.
295 * NOTE: 'reserved' bits MUST be preserved.
297 hc = ((hypercall_context.hc_paddr >> PAGE_SHIFT) <<
298 MSR_HV_HYPERCALL_PGSHIFT) |
299 (hc_orig & MSR_HV_HYPERCALL_RSVD_MASK) |
300 MSR_HV_HYPERCALL_ENABLE;
301 wrmsr(MSR_HV_HYPERCALL, hc);
304 * Confirm that Hypercall page did get setup.
306 hc = rdmsr(MSR_HV_HYPERCALL);
307 if ((hc & MSR_HV_HYPERCALL_ENABLE) == 0) {
308 printf("hyperv: Hypercall setup failed\n");
310 /* Can't perform any Hyper-V specific actions */
311 vm_guest = VM_GUEST_VM;
315 printf("hyperv: Hypercall created\n");
317 SYSINIT(hypercall_ctor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_create, NULL);
320 hypercall_destroy(void *arg __unused)
324 if (hypercall_context.hc_addr == NULL)
327 /* Disable Hypercall */
328 hc = rdmsr(MSR_HV_HYPERCALL);
329 wrmsr(MSR_HV_HYPERCALL, (hc & MSR_HV_HYPERCALL_RSVD_MASK));
333 printf("hyperv: Hypercall destroyed\n");
335 SYSUNINIT(hypercall_dtor, SI_SUB_DRIVERS, SI_ORDER_FIRST, hypercall_destroy,