2 * Copyright (c) 2009-2012 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/malloc.h>
39 #include <sys/timetc.h>
40 #include <machine/bus.h>
41 #include <machine/md_var.h>
43 #include <vm/vm_param.h>
47 #include "hv_vmbus_priv.h"
49 #define HV_NANOSECONDS_PER_SEC 1000000000L
51 #define HYPERV_INTERFACE 0x31237648 /* HV#1 */
53 static u_int hv_get_timecount(struct timecounter *tc);
55 u_int hyperv_features;
56 u_int hyperv_recommends;
58 static u_int hyperv_pm_features;
59 static u_int hyperv_features3;
64 hv_vmbus_context hv_vmbus_g_context = {
65 .syn_ic_initialized = FALSE,
66 .hypercall_page = NULL,
69 static struct timecounter hv_timecounter = {
70 hv_get_timecount, 0, ~0u, HV_NANOSECONDS_PER_SEC/100, "Hyper-V", HV_NANOSECONDS_PER_SEC/100
74 hv_get_timecount(struct timecounter *tc)
76 u_int now = rdmsr(HV_X64_MSR_TIME_REF_COUNT);
81 * @brief Invoke the specified hypercall
84 hv_vmbus_do_hypercall(uint64_t control, void* input, void* output)
87 uint64_t hv_status = 0;
88 uint64_t input_address = (input) ? hv_get_phys_addr(input) : 0;
89 uint64_t output_address = (output) ? hv_get_phys_addr(output) : 0;
90 volatile void* hypercall_page = hv_vmbus_g_context.hypercall_page;
92 __asm__ __volatile__ ("mov %0, %%r8" : : "r" (output_address): "r8");
93 __asm__ __volatile__ ("call *%3" : "=a"(hv_status):
94 "c" (control), "d" (input_address),
95 "m" (hypercall_page));
98 uint32_t control_high = control >> 32;
99 uint32_t control_low = control & 0xFFFFFFFF;
100 uint32_t hv_status_high = 1;
101 uint32_t hv_status_low = 1;
102 uint64_t input_address = (input) ? hv_get_phys_addr(input) : 0;
103 uint32_t input_address_high = input_address >> 32;
104 uint32_t input_address_low = input_address & 0xFFFFFFFF;
105 uint64_t output_address = (output) ? hv_get_phys_addr(output) : 0;
106 uint32_t output_address_high = output_address >> 32;
107 uint32_t output_address_low = output_address & 0xFFFFFFFF;
108 volatile void* hypercall_page = hv_vmbus_g_context.hypercall_page;
110 __asm__ __volatile__ ("call *%8" : "=d"(hv_status_high),
111 "=a"(hv_status_low) : "d" (control_high),
112 "a" (control_low), "b" (input_address_high),
113 "c" (input_address_low),
114 "D"(output_address_high),
115 "S"(output_address_low), "m" (hypercall_page));
116 return (hv_status_low | ((uint64_t)hv_status_high << 32));
117 #endif /* __x86_64__ */
121 * @brief Main initialization routine.
123 * This routine must be called
124 * before any other routines in here are called
129 hv_vmbus_x64_msr_hypercall_contents hypercall_msr;
130 void* virt_addr = NULL;
133 hv_vmbus_g_context.syn_ic_event_page,
135 sizeof(hv_vmbus_handle) * MAXCPU);
138 hv_vmbus_g_context.syn_ic_msg_page,
140 sizeof(hv_vmbus_handle) * MAXCPU);
142 if (vm_guest != VM_GUEST_HV)
148 uint64_t os_guest_info = HV_FREEBSD_GUEST_ID;
149 wrmsr(HV_X64_MSR_GUEST_OS_ID, os_guest_info);
150 hv_vmbus_g_context.guest_id = os_guest_info;
153 * See if the hypercall page is already set
155 hypercall_msr.as_uint64_t = rdmsr(HV_X64_MSR_HYPERCALL);
156 virt_addr = malloc(PAGE_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
158 hypercall_msr.u.enable = 1;
159 hypercall_msr.u.guest_physical_address =
160 (hv_get_phys_addr(virt_addr) >> PAGE_SHIFT);
161 wrmsr(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64_t);
164 * Confirm that hypercall page did get set up
166 hypercall_msr.as_uint64_t = 0;
167 hypercall_msr.as_uint64_t = rdmsr(HV_X64_MSR_HYPERCALL);
169 if (!hypercall_msr.u.enable)
172 hv_vmbus_g_context.hypercall_page = virt_addr;
179 if (virt_addr != NULL) {
180 if (hypercall_msr.u.enable) {
181 hypercall_msr.as_uint64_t = 0;
182 wrmsr(HV_X64_MSR_HYPERCALL,
183 hypercall_msr.as_uint64_t);
186 free(virt_addr, M_DEVBUF);
192 * @brief Cleanup routine, called normally during driver unloading or exiting
195 hv_vmbus_cleanup(void)
197 hv_vmbus_x64_msr_hypercall_contents hypercall_msr;
199 if (hv_vmbus_g_context.guest_id == HV_FREEBSD_GUEST_ID) {
200 if (hv_vmbus_g_context.hypercall_page != NULL) {
201 hypercall_msr.as_uint64_t = 0;
202 wrmsr(HV_X64_MSR_HYPERCALL,
203 hypercall_msr.as_uint64_t);
204 free(hv_vmbus_g_context.hypercall_page, M_DEVBUF);
205 hv_vmbus_g_context.hypercall_page = NULL;
211 * @brief Post a message using the hypervisor message IPC.
212 * (This involves a hypercall.)
215 hv_vmbus_post_msg_via_msg_ipc(
216 hv_vmbus_connection_id connection_id,
217 hv_vmbus_msg_type message_type,
221 struct alignedinput {
223 hv_vmbus_input_post_message msg;
226 hv_vmbus_input_post_message* aligned_msg;
227 hv_vmbus_status status;
230 if (payload_size > HV_MESSAGE_PAYLOAD_BYTE_COUNT)
233 addr = (size_t) malloc(sizeof(struct alignedinput), M_DEVBUF,
236 ("Error VMBUS: malloc failed to allocate message buffer!"));
240 aligned_msg = (hv_vmbus_input_post_message*)
241 (HV_ALIGN_UP(addr, HV_HYPERCALL_PARAM_ALIGN));
243 aligned_msg->connection_id = connection_id;
244 aligned_msg->message_type = message_type;
245 aligned_msg->payload_size = payload_size;
246 memcpy((void*) aligned_msg->payload, payload, payload_size);
248 status = hv_vmbus_do_hypercall(
249 HV_CALL_POST_MESSAGE, aligned_msg, 0) & 0xFFFF;
251 free((void *) addr, M_DEVBUF);
256 * @brief Signal an event on the specified connection using the hypervisor
257 * event IPC. (This involves a hypercall.)
260 hv_vmbus_signal_event(void *con_id)
262 hv_vmbus_status status;
264 status = hv_vmbus_do_hypercall(
265 HV_CALL_SIGNAL_EVENT,
273 * @brief hv_vmbus_synic_init
276 hv_vmbus_synic_init(void *arg)
280 uint64_t hv_vcpu_index;
281 hv_vmbus_synic_simp simp;
282 hv_vmbus_synic_siefp siefp;
283 hv_vmbus_synic_scontrol sctrl;
284 hv_vmbus_synic_sint shared_sint;
286 hv_setup_args* setup_args = (hv_setup_args *)arg;
288 cpu = PCPU_GET(cpuid);
290 if (hv_vmbus_g_context.hypercall_page == NULL)
294 * TODO: Check the version
296 version = rdmsr(HV_X64_MSR_SVERSION);
298 hv_vmbus_g_context.syn_ic_msg_page[cpu] =
299 setup_args->page_buffers[2 * cpu];
300 hv_vmbus_g_context.syn_ic_event_page[cpu] =
301 setup_args->page_buffers[2 * cpu + 1];
304 * Setup the Synic's message page
307 simp.as_uint64_t = rdmsr(HV_X64_MSR_SIMP);
308 simp.u.simp_enabled = 1;
309 simp.u.base_simp_gpa = ((hv_get_phys_addr(
310 hv_vmbus_g_context.syn_ic_msg_page[cpu])) >> PAGE_SHIFT);
312 wrmsr(HV_X64_MSR_SIMP, simp.as_uint64_t);
315 * Setup the Synic's event page
317 siefp.as_uint64_t = rdmsr(HV_X64_MSR_SIEFP);
318 siefp.u.siefp_enabled = 1;
319 siefp.u.base_siefp_gpa = ((hv_get_phys_addr(
320 hv_vmbus_g_context.syn_ic_event_page[cpu])) >> PAGE_SHIFT);
322 wrmsr(HV_X64_MSR_SIEFP, siefp.as_uint64_t);
324 /*HV_SHARED_SINT_IDT_VECTOR + 0x20; */
325 shared_sint.as_uint64_t = 0;
326 shared_sint.u.vector = setup_args->vector;
327 shared_sint.u.masked = FALSE;
328 shared_sint.u.auto_eoi = TRUE;
330 wrmsr(HV_X64_MSR_SINT0 + HV_VMBUS_MESSAGE_SINT,
331 shared_sint.as_uint64_t);
333 wrmsr(HV_X64_MSR_SINT0 + HV_VMBUS_TIMER_SINT,
334 shared_sint.as_uint64_t);
336 /* Enable the global synic bit */
337 sctrl.as_uint64_t = rdmsr(HV_X64_MSR_SCONTROL);
340 wrmsr(HV_X64_MSR_SCONTROL, sctrl.as_uint64_t);
342 hv_vmbus_g_context.syn_ic_initialized = TRUE;
345 * Set up the cpuid mapping from Hyper-V to FreeBSD.
346 * The array is indexed using FreeBSD cpuid.
348 hv_vcpu_index = rdmsr(HV_X64_MSR_VP_INDEX);
349 hv_vmbus_g_context.hv_vcpu_index[cpu] = (uint32_t)hv_vcpu_index;
355 * @brief Cleanup routine for hv_vmbus_synic_init()
357 void hv_vmbus_synic_cleanup(void *arg)
359 hv_vmbus_synic_sint shared_sint;
360 hv_vmbus_synic_simp simp;
361 hv_vmbus_synic_siefp siefp;
363 if (!hv_vmbus_g_context.syn_ic_initialized)
366 shared_sint.as_uint64_t = rdmsr(
367 HV_X64_MSR_SINT0 + HV_VMBUS_MESSAGE_SINT);
369 shared_sint.u.masked = 1;
372 * Disable the interrupt 0
375 HV_X64_MSR_SINT0 + HV_VMBUS_MESSAGE_SINT,
376 shared_sint.as_uint64_t);
378 shared_sint.as_uint64_t = rdmsr(
379 HV_X64_MSR_SINT0 + HV_VMBUS_TIMER_SINT);
381 shared_sint.u.masked = 1;
384 * Disable the interrupt 1
387 HV_X64_MSR_SINT0 + HV_VMBUS_TIMER_SINT,
388 shared_sint.as_uint64_t);
389 simp.as_uint64_t = rdmsr(HV_X64_MSR_SIMP);
390 simp.u.simp_enabled = 0;
391 simp.u.base_simp_gpa = 0;
393 wrmsr(HV_X64_MSR_SIMP, simp.as_uint64_t);
395 siefp.as_uint64_t = rdmsr(HV_X64_MSR_SIEFP);
396 siefp.u.siefp_enabled = 0;
397 siefp.u.base_siefp_gpa = 0;
399 wrmsr(HV_X64_MSR_SIEFP, siefp.as_uint64_t);
403 hyperv_identify(void)
406 unsigned int maxLeaf;
409 if (vm_guest != VM_GUEST_HV)
412 op = HV_CPU_ID_FUNCTION_HV_VENDOR_AND_MAX_FUNCTION;
415 if (maxLeaf < HV_CPU_ID_FUNCTION_MS_HV_IMPLEMENTATION_LIMITS)
418 op = HV_CPU_ID_FUNCTION_HV_INTERFACE;
420 if (regs[0] != HYPERV_INTERFACE)
423 op = HV_CPU_ID_FUNCTION_MS_HV_FEATURES;
425 if ((regs[0] & HV_FEATURE_MSR_HYPERCALL) == 0) {
427 * Hyper-V w/o Hypercall is impossible; someone
432 hyperv_features = regs[0];
433 hyperv_pm_features = regs[2];
434 hyperv_features3 = regs[3];
436 op = HV_CPU_ID_FUNCTION_MS_HV_VERSION;
438 printf("Hyper-V Version: %d.%d.%d [SP%d]\n",
439 regs[1] >> 16, regs[1] & 0xffff, regs[0], regs[2]);
441 printf(" Features=0x%b\n", hyperv_features,
443 "\001VPRUNTIME" /* MSR_VP_RUNTIME */
444 "\002TMREFCNT" /* MSR_TIME_REF_COUNT */
445 "\003SYNIC" /* MSRs for SynIC */
446 "\004SYNTM" /* MSRs for SynTimer */
447 "\005APIC" /* MSR_{EOI,ICR,TPR} */
448 "\006HYPERCALL" /* MSR_{GUEST_OS_ID,HYPERCALL} */
449 "\007VPINDEX" /* MSR_VP_INDEX */
450 "\010RESET" /* MSR_RESET */
451 "\011STATS" /* MSR_STATS_ */
452 "\012REFTSC" /* MSR_REFERENCE_TSC */
453 "\013IDLE" /* MSR_GUEST_IDLE */
454 "\014TMFREQ" /* MSR_{TSC,APIC}_FREQUENCY */
455 "\015DEBUG"); /* MSR_SYNTH_DEBUG_ */
456 printf(" PM Features=max C%u, 0x%b\n",
457 HV_PM_FEATURE_CSTATE(hyperv_pm_features),
458 (hyperv_pm_features & ~HV_PM_FEATURE_CSTATE_MASK),
460 "\005C3HPET"); /* HPET is required for C3 state */
461 printf(" Features3=0x%b\n", hyperv_features3,
463 "\001MWAIT" /* MWAIT */
464 "\002DEBUG" /* guest debug support */
465 "\003PERFMON" /* performance monitor */
466 "\004PCPUDPE" /* physical CPU dynamic partition event */
467 "\005XMMHC" /* hypercall input through XMM regs */
468 "\006IDLE" /* guest idle support */
469 "\007SLEEP" /* hypervisor sleep support */
470 "\010NUMA" /* NUMA distance query support */
471 "\011TMFREQ" /* timer frequency query (TSC, LAPIC) */
472 "\012SYNCMC" /* inject synthetic machine checks */
473 "\013CRASH" /* MSRs for guest crash */
474 "\014DEBUGMSR" /* MSRs for guest debug */
475 "\015NPIEP" /* NPIEP */
476 "\016HVDIS"); /* disabling hypervisor */
478 op = HV_CPU_ID_FUNCTION_MS_HV_ENLIGHTENMENT_INFORMATION;
480 hyperv_recommends = regs[0];
482 printf(" Recommends: %08x %08x\n", regs[0], regs[1]);
484 op = HV_CPU_ID_FUNCTION_MS_HV_IMPLEMENTATION_LIMITS;
487 printf(" Limits: Vcpu:%d Lcpu:%d Int:%d\n",
488 regs[0], regs[1], regs[2]);
491 if (maxLeaf >= HV_CPU_ID_FUNCTION_MS_HV_HARDWARE_FEATURE) {
492 op = HV_CPU_ID_FUNCTION_MS_HV_HARDWARE_FEATURE;
495 printf(" HW Features: %08x AMD: %08x\n",
504 hyperv_init(void *dummy __unused)
506 if (!hyperv_identify())
509 if (hyperv_features & HV_FEATURE_MSR_TIME_REFCNT) {
510 /* Register virtual timecount */
511 tc_init(&hv_timecounter);
514 SYSINIT(hyperv_initialize, SI_SUB_HYPERVISOR, SI_ORDER_FIRST, hyperv_init,