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.
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systimer.h>
32 #include <sys/systm.h>
34 #include <machine/cpufunc.h>
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>
41 #define HYPERV_DRAGONFLY_BUILD 0ULL
42 #define HYPERV_DRAGONFLY_VERSION ((uint64_t)__DragonFly_version)
43 #define HYPERV_DRAGONFLY_OSID 0ULL
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)
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)
60 struct hypercall_ctx {
62 struct hyperv_dma hc_dma;
65 static void hyperv_cputimer_construct(struct cputimer *,
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);
74 u_int hyperv_features;
75 static u_int hyperv_recommends;
77 static u_int hyperv_pm_features;
78 static u_int hyperv_features3;
80 hyperv_tc64_t hyperv_tc64;
82 static struct cputimer hyperv_cputimer = {
83 .next = SLIST_ENTRY_INITIALIZER,
85 .pri = CPUTIMER_PRI_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
95 static struct cpucounter hyperv_cpucounter = {
96 .freq = HYPERV_TIMER_FREQ,
97 .count = hyperv_tc64_rdmsr,
98 .flags = CPUCOUNTER_FLAG_MPSYNC,
99 .prio = CPUCOUNTER_PRIO_VMM,
100 .type = CPUCOUNTER_VMM
103 static struct hypercall_ctx hypercall_context;
106 hypercall_post_message(bus_addr_t msg_paddr)
108 return hypercall_md(hypercall_context.hc_addr,
109 HYPERCALL_POST_MESSAGE, msg_paddr, 0);
113 hyperv_cputimer_construct(struct cputimer *timer, sysclock_t oldclock)
116 timer->base = oldclock - timer->count();
120 hyperv_cputimer_count(void)
124 val = rdmsr(MSR_HV_TIME_REF_COUNT);
125 return (val + hyperv_cputimer.base);
129 hypercall_memfree(void)
131 hyperv_dmamem_free(&hypercall_context.hc_dma,
132 hypercall_context.hc_addr);
133 hypercall_context.hc_addr = NULL;
137 hypercall_create(void)
139 uint64_t hc, hc_orig;
141 hypercall_context.hc_addr = hyperv_dmamem_alloc(NULL, PAGE_SIZE, 0,
142 PAGE_SIZE, &hypercall_context.hc_dma, BUS_DMA_WAITOK);
143 if (hypercall_context.hc_addr == NULL) {
144 kprintf("hyperv: Hypercall page allocation failed\n");
148 /* Get the 'reserved' bits, which requires preservation. */
149 hc_orig = rdmsr(MSR_HV_HYPERCALL);
152 * Setup the Hypercall page.
154 * NOTE: 'reserved' bits MUST be preserved.
156 hc = ((hypercall_context.hc_dma.hv_paddr >> PAGE_SHIFT) <<
157 MSR_HV_HYPERCALL_PGSHIFT) |
158 (hc_orig & MSR_HV_HYPERCALL_RSVD_MASK) |
159 MSR_HV_HYPERCALL_ENABLE;
160 wrmsr(MSR_HV_HYPERCALL, hc);
163 * Confirm that Hypercall page did get setup.
165 hc = rdmsr(MSR_HV_HYPERCALL);
166 if ((hc & MSR_HV_HYPERCALL_ENABLE) == 0) {
167 kprintf("hyperv: Hypercall setup failed\n");
172 kprintf("hyperv: Hypercall created\n");
178 hypercall_destroy(void)
182 if (hypercall_context.hc_addr == NULL)
185 /* Disable Hypercall */
186 hc = rdmsr(MSR_HV_HYPERCALL);
187 wrmsr(MSR_HV_HYPERCALL, (hc & MSR_HV_HYPERCALL_RSVD_MASK));
191 kprintf("hyperv: Hypercall destroyed\n");
195 hyperv_tc64_rdmsr(void)
198 return (rdmsr(MSR_HV_TIME_REF_COUNT));
202 hyperv_identify(void)
205 unsigned int maxleaf;
207 if (vmm_guest != VMM_GUEST_HYPERV)
210 do_cpuid(CPUID_LEAF_HV_MAXLEAF, regs);
212 if (maxleaf < CPUID_LEAF_HV_LIMITS)
215 do_cpuid(CPUID_LEAF_HV_INTERFACE, regs);
216 if (regs[0] != CPUID_HV_IFACE_HYPERV)
219 do_cpuid(CPUID_LEAF_HV_FEATURES, regs);
220 if ((regs[0] & CPUID_HV_MSR_HYPERCALL) == 0) {
222 * Hyper-V w/o Hypercall is impossible; someone
227 hyperv_features = regs[0];
228 hyperv_pm_features = regs[2];
229 hyperv_features3 = regs[3];
231 do_cpuid(CPUID_LEAF_HV_IDENTITY, regs);
232 kprintf("Hyper-V Version: %d.%d.%d [SP%d]\n",
233 regs[1] >> 16, regs[1] & 0xffff, regs[0], regs[2]);
235 kprintf(" Features=0x%pb%i\n",
237 "\001VPRUNTIME" /* MSR_HV_VP_RUNTIME */
238 "\002TMREFCNT" /* MSR_HV_TIME_REF_COUNT */
239 "\003SYNIC" /* MSRs for SynIC */
240 "\004SYNTM" /* MSRs for SynTimer */
241 "\005APIC" /* MSR_HV_{EOI,ICR,TPR} */
242 "\006HYPERCALL" /* MSR_HV_{GUEST_OS_ID,HYPERCALL} */
243 "\007VPINDEX" /* MSR_HV_VP_INDEX */
244 "\010RESET" /* MSR_HV_RESET */
245 "\011STATS" /* MSR_HV_STATS_ */
246 "\012REFTSC" /* MSR_HV_REFERENCE_TSC */
247 "\013IDLE" /* MSR_HV_GUEST_IDLE */
248 "\014TMFREQ" /* MSR_HV_{TSC,APIC}_FREQUENCY */
249 "\015DEBUG", /* MSR_HV_SYNTH_DEBUG_ */
251 kprintf(" PM Features=0x%pb%i [C%u]\n",
253 "\005C3HPET", /* HPET is required for C3 state */
254 (hyperv_pm_features & ~CPUPM_HV_CSTATE_MASK),
255 CPUPM_HV_CSTATE(hyperv_pm_features));
256 kprintf(" Features3=0x%pb%i\n",
258 "\001MWAIT" /* MWAIT */
259 "\002DEBUG" /* guest debug support */
260 "\003PERFMON" /* performance monitor */
261 "\004PCPUDPE" /* physical CPU dynamic partition event */
262 "\005XMMHC" /* hypercall input through XMM regs */
263 "\006IDLE" /* guest idle support */
264 "\007SLEEP" /* hypervisor sleep support */
265 "\010NUMA" /* NUMA distance query support */
266 "\011TMFREQ" /* timer frequency query (TSC, LAPIC) */
267 "\012SYNCMC" /* inject synthetic machine checks */
268 "\013CRASH" /* MSRs for guest crash */
269 "\014DEBUGMSR" /* MSRs for guest debug */
270 "\015NPIEP" /* NPIEP */
271 "\016HVDIS", /* disabling hypervisor */
274 do_cpuid(CPUID_LEAF_HV_RECOMMENDS, regs);
275 hyperv_recommends = regs[0];
277 kprintf(" Recommends: %08x %08x\n", regs[0], regs[1]);
279 do_cpuid(CPUID_LEAF_HV_LIMITS, regs);
281 kprintf(" Limits: Vcpu:%d Lcpu:%d Int:%d\n",
282 regs[0], regs[1], regs[2]);
285 if (maxleaf >= CPUID_LEAF_HV_HWFEATURES) {
286 do_cpuid(CPUID_LEAF_HV_HWFEATURES, regs);
288 kprintf(" HW Features: %08x, AMD: %08x\n",
297 hyperv_init(void *dummy __unused)
301 if (!hyperv_identify()) {
302 /* Not Hyper-V; reset guest id to the generic one. */
303 if (vmm_guest == VMM_GUEST_HYPERV)
304 vmm_guest = VMM_GUEST_UNKNOWN;
309 wrmsr(MSR_HV_GUEST_OS_ID, MSR_HV_GUESTID_DRAGONFLY);
311 if (hyperv_features & CPUID_HV_MSR_TIME_REFCNT) {
312 /* Register Hyper-V cputimers */
313 cputimer_register(&hyperv_cputimer);
314 cputimer_select(&hyperv_cputimer, 0);
315 cpucounter_register(&hyperv_cpucounter);
316 hyperv_tc64 = hyperv_tc64_rdmsr;
319 error = hypercall_create();
321 /* Can't perform any Hyper-V specific actions */
322 vmm_guest = VMM_GUEST_UNKNOWN;
325 /* Machine dependent initialization. */
328 SYSINIT(hyperv_initialize, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
332 hyperv_uninit(void *dummy __unused)
334 /* Machine dependent uninitialization. */
337 if (hyperv_features & CPUID_HV_MSR_TIME_REFCNT) {
338 /* Deregister Hyper-V systimer */
339 cputimer_deregister(&hyperv_cputimer);
343 SYSUNINIT(hyperv_uninitialize, SI_SUB_PRE_DRIVERS, SI_ORDER_FIRST,
344 hyperv_uninit, NULL);