3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 * Haiyang Zhang <haiyangz@microsoft.com>
20 * Hank Janssen <hjanssen@microsoft.com>
24 #include <linux/kernel.h>
26 #include <linux/vmalloc.h>
28 #include "include/logging.h"
29 #include "VmbusPrivate.h"
33 /* The one and only */
34 struct hv_context gHvContext
= {
35 .SynICInitialized
= false,
36 .HypercallPage
= NULL
,
37 .SignalEventParam
= NULL
,
38 .SignalEventBuffer
= NULL
,
45 HvQueryHypervisorPresence()
48 Query the cpuid for presense of windows hypervisor
52 HvQueryHypervisorPresence (
66 op
= HvCpuIdFunctionVersionAndFeatures
;
67 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
69 return (ecx
& HV_PRESENT_BIT
);
76 HvQueryHypervisorInfo()
79 Get version info of the windows hypervisor
83 HvQueryHypervisorInfo (
95 * Its assumed that this is called after confirming that Viridian
96 * is present. Query id and revision.
104 op
= HvCpuIdFunctionHvVendorAndMaxFunction
;
105 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
107 DPRINT_INFO(VMBUS
, "Vendor ID: %c%c%c%c%c%c%c%c%c%c%c%c",
110 ((ebx
>> 16) & 0xFF),
111 ((ebx
>> 24) & 0xFF),
114 ((ecx
>> 16) & 0xFF),
115 ((ecx
>> 24) & 0xFF),
118 ((edx
>> 16) & 0xFF),
119 ((edx
>> 24) & 0xFF));
126 op
= HvCpuIdFunctionHvInterface
;
127 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
129 DPRINT_INFO(VMBUS
, "Interface ID: %c%c%c%c",
132 ((eax
>> 16) & 0xFF),
133 ((eax
>> 24) & 0xFF));
135 if (maxLeaf
>= HvCpuIdFunctionMsHvVersion
) {
140 op
= HvCpuIdFunctionMsHvVersion
;
141 cpuid(op
, &eax
, &ebx
, &ecx
, &edx
);
142 DPRINT_INFO(VMBUS
, "OS Build:%d-%d.%d-%d-%d.%d",
160 Invoke the specified hypercall
172 u64 inputAddress
= (Input
)? virt_to_phys(Input
) : 0;
173 u64 outputAddress
= (Output
)? virt_to_phys(Output
) : 0;
174 volatile void* hypercallPage
= gHvContext
.HypercallPage
;
176 DPRINT_DBG(VMBUS
, "Hypercall <control %llx input phys %llx virt %p output phys %llx virt %p hypercall %p>",
184 __asm__
__volatile__ ("mov %0, %%r8" : : "r" (outputAddress
): "r8");
185 __asm__
__volatile__ ("call *%3" : "=a"(hvStatus
): "c" (Control
), "d" (inputAddress
), "m" (hypercallPage
));
187 DPRINT_DBG(VMBUS
, "Hypercall <return %llx>", hvStatus
);
193 u32 controlHi
= Control
>> 32;
194 u32 controlLo
= Control
& 0xFFFFFFFF;
197 u64 inputAddress
= (Input
) ? virt_to_phys(Input
) : 0;
198 u32 inputAddressHi
= inputAddress
>> 32;
199 u32 inputAddressLo
= inputAddress
& 0xFFFFFFFF;
200 u64 outputAddress
= (Output
) ? virt_to_phys(Output
) : 0;
201 u32 outputAddressHi
= outputAddress
>> 32;
202 u32 outputAddressLo
= outputAddress
& 0xFFFFFFFF;
203 volatile void* hypercallPage
= gHvContext
.HypercallPage
;
205 DPRINT_DBG(VMBUS
, "Hypercall <control %llx input %p output %p>",
210 __asm__
__volatile__ ("call *%8" : "=d"(hvStatusHi
), "=a"(hvStatusLo
) : "d" (controlHi
), "a" (controlLo
), "b" (inputAddressHi
), "c" (inputAddressLo
), "D"(outputAddressHi
), "S"(outputAddressLo
), "m" (hypercallPage
));
213 DPRINT_DBG(VMBUS
, "Hypercall <return %llx>", hvStatusLo
| ((u64
)hvStatusHi
<< 32));
215 return (hvStatusLo
| ((u64
)hvStatusHi
<< 32));
225 Main initialization routine. This routine must be called
226 before any other routines in here are called
233 HV_X64_MSR_HYPERCALL_CONTENTS hypercallMsr
;
234 void *virtAddr
= NULL
;
238 memset(gHvContext
.synICEventPage
, 0, sizeof(void *) * MAX_NUM_CPUS
);
239 memset(gHvContext
.synICMessagePage
, 0, sizeof(void *) * MAX_NUM_CPUS
);
241 if (!HvQueryHypervisorPresence())
243 DPRINT_ERR(VMBUS
, "No Windows hypervisor detected!!");
247 DPRINT_INFO(VMBUS
, "Windows hypervisor detected! Retrieving more info...");
249 maxLeaf
= HvQueryHypervisorInfo();
250 /* HvQueryHypervisorFeatures(maxLeaf); */
252 /* Determine if we are running on xenlinux (ie x2v shim) or native linux */
253 rdmsrl(HV_X64_MSR_GUEST_OS_ID
, gHvContext
.GuestId
);
254 if (gHvContext
.GuestId
== 0)
256 /* Write our OS info */
257 wrmsrl(HV_X64_MSR_GUEST_OS_ID
, HV_LINUX_GUEST_ID
);
259 gHvContext
.GuestId
= HV_LINUX_GUEST_ID
;
262 /* See if the hypercall page is already set */
263 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
264 if (gHvContext
.GuestId
== HV_LINUX_GUEST_ID
)
266 /* Allocate the hypercall page memory */
267 /* virtAddr = osd_PageAlloc(1); */
268 virtAddr
= osd_VirtualAllocExec(PAGE_SIZE
);
272 DPRINT_ERR(VMBUS
, "unable to allocate hypercall page!!");
276 hypercallMsr
.Enable
= 1;
277 /* hypercallMsr.GuestPhysicalAddress = virt_to_phys(virtAddr) >> PAGE_SHIFT; */
278 hypercallMsr
.GuestPhysicalAddress
= vmalloc_to_pfn(virtAddr
);
279 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
281 /* Confirm that hypercall page did get setup. */
282 hypercallMsr
.AsUINT64
= 0;
283 rdmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
284 if (!hypercallMsr
.Enable
)
286 DPRINT_ERR(VMBUS
, "unable to set hypercall page!!");
290 gHvContext
.HypercallPage
= virtAddr
;
294 DPRINT_ERR(VMBUS
, "Unknown guest id (0x%llx)!!", gHvContext
.GuestId
);
298 DPRINT_INFO(VMBUS
, "Hypercall page VA=%p, PA=0x%0llx",
299 gHvContext
.HypercallPage
,
300 (u64
)hypercallMsr
.GuestPhysicalAddress
<< PAGE_SHIFT
);
302 /* Setup the global signal event param for the signal event hypercall */
303 gHvContext
.SignalEventBuffer
= kmalloc(sizeof(struct hv_input_signal_event_buffer
), GFP_KERNEL
);
304 if (!gHvContext
.SignalEventBuffer
)
309 gHvContext
.SignalEventParam
= (struct hv_input_signal_event
*)(ALIGN_UP((unsigned long)gHvContext
.SignalEventBuffer
, HV_HYPERCALL_PARAM_ALIGN
));
310 gHvContext
.SignalEventParam
->ConnectionId
.Asu32
= 0;
311 gHvContext
.SignalEventParam
->ConnectionId
.u
.Id
= VMBUS_EVENT_CONNECTION_ID
;
312 gHvContext
.SignalEventParam
->FlagNumber
= 0;
313 gHvContext
.SignalEventParam
->RsvdZ
= 0;
315 /* DPRINT_DBG(VMBUS, "My id %llu", HvGetCurrentPartitionId()); */
324 if (hypercallMsr
.Enable
)
326 hypercallMsr
.AsUINT64
= 0;
327 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
345 Cleanup routine. This routine is called normally during driver unloading or exiting.
348 void HvCleanup (void)
350 HV_X64_MSR_HYPERCALL_CONTENTS hypercallMsr
;
354 if (gHvContext
.SignalEventBuffer
)
356 kfree(gHvContext
.SignalEventBuffer
);
357 gHvContext
.SignalEventBuffer
= NULL
;
358 gHvContext
.SignalEventParam
= NULL
;
361 if (gHvContext
.GuestId
== HV_LINUX_GUEST_ID
)
363 if (gHvContext
.HypercallPage
)
365 hypercallMsr
.AsUINT64
= 0;
366 wrmsrl(HV_X64_MSR_HYPERCALL
, hypercallMsr
.AsUINT64
);
367 vfree(gHvContext
.HypercallPage
);
368 gHvContext
.HypercallPage
= NULL
;
383 Post a message using the hypervisor message IPC. This
384 involves a hypercall.
387 HV_STATUS
HvPostMessage(
388 HV_CONNECTION_ID connectionId
,
389 HV_MESSAGE_TYPE messageType
,
394 struct alignedInput
{
396 struct hv_input_post_message msg
;
399 struct hv_input_post_message
*alignedMsg
;
403 if (payloadSize
> HV_MESSAGE_PAYLOAD_BYTE_COUNT
)
408 addr
= (unsigned long)kmalloc(sizeof(struct alignedInput
), GFP_ATOMIC
);
415 alignedMsg
= (struct hv_input_post_message
*)(ALIGN_UP(addr
, HV_HYPERCALL_PARAM_ALIGN
));
417 alignedMsg
->ConnectionId
= connectionId
;
418 alignedMsg
->MessageType
= messageType
;
419 alignedMsg
->PayloadSize
= payloadSize
;
420 memcpy((void*)alignedMsg
->Payload
, payload
, payloadSize
);
422 status
= HvDoHypercall(HvCallPostMessage
, alignedMsg
, NULL
) & 0xFFFF;
436 Signal an event on the specified connection using the hypervisor event IPC. This
437 involves a hypercall.
440 HV_STATUS
HvSignalEvent(void)
444 status
= HvDoHypercall(HvCallSignalEvent
, gHvContext
.SignalEventParam
, NULL
) & 0xFFFF;
456 Initialize the Synthethic Interrupt Controller. If it is already initialized by
457 another entity (ie x2v shim), we need to retrieve the initialized message and event pages.
458 Otherwise, we create and initialize the message and event pages.
461 int HvSynicInit (u32 irqVector
)
465 HV_SYNIC_SIEFP siefp
;
466 HV_SYNIC_SINT sharedSint
;
467 HV_SYNIC_SCONTROL sctrl
;
473 if (!gHvContext
.HypercallPage
)
479 /* Check the version */
480 rdmsrl(HV_X64_MSR_SVERSION
, version
);
482 DPRINT_INFO(VMBUS
, "SynIC version: %llx", version
);
484 /* TODO: Handle SMP */
485 if (gHvContext
.GuestId
== HV_XENLINUX_GUEST_ID
)
487 DPRINT_INFO(VMBUS
, "Skipping SIMP and SIEFP setup since it is already set.");
489 rdmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
490 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
492 DPRINT_DBG(VMBUS
, "Simp: %llx, Sifep: %llx", simp
.AsUINT64
, siefp
.AsUINT64
);
494 /* Determine if we are running on xenlinux (ie x2v shim) or native linux */
495 rdmsrl(HV_X64_MSR_GUEST_OS_ID
, guestID
);
496 if (guestID
== HV_LINUX_GUEST_ID
)
498 gHvContext
.synICMessagePage
[0] = phys_to_virt(simp
.BaseSimpGpa
<< PAGE_SHIFT
);
499 gHvContext
.synICEventPage
[0] = phys_to_virt(siefp
.BaseSiefpGpa
<< PAGE_SHIFT
);
503 DPRINT_ERR(VMBUS
, "unknown guest id!!");
506 DPRINT_DBG(VMBUS
, "MAPPED: Simp: %p, Sifep: %p", gHvContext
.synICMessagePage
[0], gHvContext
.synICEventPage
[0]);
510 gHvContext
.synICMessagePage
[0] = osd_PageAlloc(1);
511 if (gHvContext
.synICMessagePage
[0] == NULL
)
513 DPRINT_ERR(VMBUS
, "unable to allocate SYNIC message page!!");
517 gHvContext
.synICEventPage
[0] = osd_PageAlloc(1);
518 if (gHvContext
.synICEventPage
[0] == NULL
)
520 DPRINT_ERR(VMBUS
, "unable to allocate SYNIC event page!!");
524 /* Setup the Synic's message page */
525 rdmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
526 simp
.SimpEnabled
= 1;
527 simp
.BaseSimpGpa
= virt_to_phys(gHvContext
.synICMessagePage
[0]) >> PAGE_SHIFT
;
529 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SIMP msr set to: %llx", simp
.AsUINT64
);
531 wrmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
533 /* Setup the Synic's event page */
534 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
535 siefp
.SiefpEnabled
= 1;
536 siefp
.BaseSiefpGpa
= virt_to_phys(gHvContext
.synICEventPage
[0]) >> PAGE_SHIFT
;
538 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SIEFP msr set to: %llx", siefp
.AsUINT64
);
540 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
542 /* Setup the interception SINT. */
543 /* wrmsrl((HV_X64_MSR_SINT0 + HV_SYNIC_INTERCEPTION_SINT_INDEX), */
544 /* interceptionSint.AsUINT64); */
546 /* Setup the shared SINT. */
547 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
549 sharedSint
.AsUINT64
= 0;
550 sharedSint
.Vector
= irqVector
; /* HV_SHARED_SINT_IDT_VECTOR + 0x20; */
551 sharedSint
.Masked
= false;
552 sharedSint
.AutoEoi
= true;
554 DPRINT_DBG(VMBUS
, "HV_X64_MSR_SINT1 msr set to: %llx", sharedSint
.AsUINT64
);
556 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
558 /* Enable the global synic bit */
559 rdmsrl(HV_X64_MSR_SCONTROL
, sctrl
.AsUINT64
);
562 wrmsrl(HV_X64_MSR_SCONTROL
, sctrl
.AsUINT64
);
564 gHvContext
.SynICInitialized
= true;
573 if (gHvContext
.GuestId
== HV_LINUX_GUEST_ID
)
575 if (gHvContext
.synICEventPage
[0])
577 osd_PageFree(gHvContext
.synICEventPage
[0],1);
580 if (gHvContext
.synICMessagePage
[0])
582 osd_PageFree(gHvContext
.synICMessagePage
[0], 1);
598 Cleanup routine for HvSynicInit().
601 void HvSynicCleanup(void)
603 HV_SYNIC_SINT sharedSint
;
605 HV_SYNIC_SIEFP siefp
;
609 if (!gHvContext
.SynICInitialized
)
615 rdmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
617 sharedSint
.Masked
= 1;
619 /* Disable the interrupt */
620 wrmsrl(HV_X64_MSR_SINT0
+ VMBUS_MESSAGE_SINT
, sharedSint
.AsUINT64
);
623 * Disable and free the resources only if we are running as
624 * native linux since in xenlinux, we are sharing the
625 * resources with the x2v shim
627 if (gHvContext
.GuestId
== HV_LINUX_GUEST_ID
)
629 rdmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
630 simp
.SimpEnabled
= 0;
631 simp
.BaseSimpGpa
= 0;
633 wrmsrl(HV_X64_MSR_SIMP
, simp
.AsUINT64
);
635 rdmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
636 siefp
.SiefpEnabled
= 0;
637 siefp
.BaseSiefpGpa
= 0;
639 wrmsrl(HV_X64_MSR_SIEFP
, siefp
.AsUINT64
);
641 osd_PageFree(gHvContext
.synICMessagePage
[0], 1);
642 osd_PageFree(gHvContext
.synICEventPage
[0], 1);