236b2935f217749893b7034e59da3e3568928acevboxsync * GIM - Guest Interface Manager, Microsoft Hyper-V, All Contexts.
f84ab9e4599e758ec1f36479f871b3f5b7f271f2vboxsync * Copyright (C) 2014-2015 Oracle Corporation
236b2935f217749893b7034e59da3e3568928acevboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
236b2935f217749893b7034e59da3e3568928acevboxsync * available from http://www.virtualbox.org. This file is free software;
236b2935f217749893b7034e59da3e3568928acevboxsync * you can redistribute it and/or modify it under the terms of the GNU
236b2935f217749893b7034e59da3e3568928acevboxsync * General Public License (GPL) as published by the Free Software
236b2935f217749893b7034e59da3e3568928acevboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
236b2935f217749893b7034e59da3e3568928acevboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
236b2935f217749893b7034e59da3e3568928acevboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
236b2935f217749893b7034e59da3e3568928acevboxsync/*******************************************************************************
236b2935f217749893b7034e59da3e3568928acevboxsync* Header Files *
236b2935f217749893b7034e59da3e3568928acevboxsync*******************************************************************************/
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Handles the Hyper-V hypercall.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @returns VBox status code.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param pVCpu Pointer to the VMCPU.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param pCtx Pointer to the guest-CPU context.
f84ab9e4599e758ec1f36479f871b3f5b7f271f2vboxsyncVMM_INT_DECL(int) gimHvHypercall(PVMCPU pVCpu, PCPUMCTX pCtx)
ce9f428fc2f581b4d8968d870967e259d9924d8dvboxsync if (!MSR_GIM_HV_HYPERCALL_IS_ENABLED(pVM->gim.s.u.Hv.u64HypercallMsr))
ce9f428fc2f581b4d8968d870967e259d9924d8dvboxsync /** @todo Handle hypercalls. Fail for now */
ce9f428fc2f581b4d8968d870967e259d9924d8dvboxsync * Returns whether the guest has configured and enabled the use of Hyper-V's
ce9f428fc2f581b4d8968d870967e259d9924d8dvboxsync * hypercall interface.
ce9f428fc2f581b4d8968d870967e259d9924d8dvboxsync * @returns true if hypercalls are enabled, false otherwise.
ce9f428fc2f581b4d8968d870967e259d9924d8dvboxsync * @param pVCpu Pointer to the VMCPU.
f84ab9e4599e758ec1f36479f871b3f5b7f271f2vboxsyncVMM_INT_DECL(bool) gimHvAreHypercallsEnabled(PVMCPU pVCpu)
ce9f428fc2f581b4d8968d870967e259d9924d8dvboxsync return MSR_GIM_HV_HYPERCALL_IS_ENABLED(pVCpu->CTX_SUFF(pVM)->gim.s.u.Hv.u64HypercallMsr);
eca46116c06b850c8c6be84678ba1f9dbdb3f9ddvboxsync * Returns whether the guest has configured and enabled the use of Hyper-V's
eca46116c06b850c8c6be84678ba1f9dbdb3f9ddvboxsync * paravirtualized TSC.
eca46116c06b850c8c6be84678ba1f9dbdb3f9ddvboxsync * @returns true if paravirt. TSC is enabled, false otherwise.
eca46116c06b850c8c6be84678ba1f9dbdb3f9ddvboxsync * @param pVM Pointer to the VM.
f84ab9e4599e758ec1f36479f871b3f5b7f271f2vboxsyncVMM_INT_DECL(bool) gimHvIsParavirtTscEnabled(PVM pVM)
eca46116c06b850c8c6be84678ba1f9dbdb3f9ddvboxsync return MSR_GIM_HV_REF_TSC_IS_ENABLED(pVM->gim.s.u.Hv.u64TscPageMsr);
157093a77f2752732368338110cb50fa6cd7717fvboxsync * MSR read handler for Hyper-V.
8d1b88d15f8863c8c7312f9b0738587e5d3ff6f1vboxsync * @returns Strict VBox status code like CPUMQueryGuestMsr().
ad4f6ac2063d9b48efd9c3193442136a8c7c890avboxsync * @retval VINF_CPUM_R3_MSR_READ
ad4f6ac2063d9b48efd9c3193442136a8c7c890avboxsync * @retval VERR_CPUM_RAISE_GP_0
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param pVCpu Pointer to the VMCPU.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param idMsr The MSR being read.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param pRange The range this MSR belongs to.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param puValue Where to store the MSR value read.
f84ab9e4599e758ec1f36479f871b3f5b7f271f2vboxsyncVMM_INT_DECL(VBOXSTRICTRC) gimHvReadMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t *puValue)
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /* Hyper-V reports the time in 100 ns units (10 MHz). */
bc5cd42756b3f98351040bbfccc08dd9bacd103avboxsync uint64_t u64Tsc100Ns = u64TscHz / UINT64_C(10000000); /* 100 ns */
773940a4d3b16fc24fa111b73075a9fdcabbfc78vboxsync LogRel(("GIM: HyperV: Unknown/invalid RdMsr (%#x) -> #GP(0)\n", idMsr));
773940a4d3b16fc24fa111b73075a9fdcabbfc78vboxsync LogFunc(("Unknown/invalid RdMsr (%#RX32) -> #GP(0)\n", idMsr));
157093a77f2752732368338110cb50fa6cd7717fvboxsync * MSR write handler for Hyper-V.
8d1b88d15f8863c8c7312f9b0738587e5d3ff6f1vboxsync * @returns Strict VBox status code like CPUMSetGuestMsr().
ad4f6ac2063d9b48efd9c3193442136a8c7c890avboxsync * @retval VINF_CPUM_R3_MSR_WRITE
ad4f6ac2063d9b48efd9c3193442136a8c7c890avboxsync * @retval VERR_CPUM_RAISE_GP_0
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param pVCpu Pointer to the VMCPU.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param idMsr The MSR being written.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param pRange The range this MSR belongs to.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param uRawValue The raw value with the ignored bits not masked.
f84ab9e4599e758ec1f36479f871b3f5b7f271f2vboxsyncVMM_INT_DECL(VBOXSTRICTRC) gimHvWriteMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t uRawValue)
773940a4d3b16fc24fa111b73075a9fdcabbfc78vboxsync PDMApicWriteMSR(pVM, pVCpu->idCpu, 0x80, uRawValue);
773940a4d3b16fc24fa111b73075a9fdcabbfc78vboxsync PDMApicWriteMSR(pVM, pVCpu->idCpu, 0x0B, uRawValue);
773940a4d3b16fc24fa111b73075a9fdcabbfc78vboxsync PDMApicWriteMSR(pVM, pVCpu->idCpu, 0x30, uRawValue);
157093a77f2752732368338110cb50fa6cd7717fvboxsync /* Disable the hypercall-page if 0 is written to this MSR. */
157093a77f2752732368338110cb50fa6cd7717fvboxsync pHv->u64HypercallMsr &= ~MSR_GIM_HV_HYPERCALL_ENABLE_BIT;
ad4f6ac2063d9b48efd9c3193442136a8c7c890avboxsync#endif /* IN_RING3 */
ad4f6ac2063d9b48efd9c3193442136a8c7c890avboxsync#else /* IN_RING3 */
46fa8016c74a7f3a46a16278103fda26c749e6f1vboxsync * For now ignore writes to the hypercall MSR (i.e. keeps it disabled).
46fa8016c74a7f3a46a16278103fda26c749e6f1vboxsync * This is required to boot FreeBSD 10.1 (with Hyper-V enabled ofc),
46fa8016c74a7f3a46a16278103fda26c749e6f1vboxsync * see @bugref{7270} comment #116.
157093a77f2752732368338110cb50fa6cd7717fvboxsync /* First, update all but the hypercall enable bit. */
157093a77f2752732368338110cb50fa6cd7717fvboxsync pHv->u64HypercallMsr = (uRawValue & ~MSR_GIM_HV_HYPERCALL_ENABLE_BIT);
157093a77f2752732368338110cb50fa6cd7717fvboxsync /* Hypercalls can only be enabled when the guest has set the Guest-OS Id Msr. */
157093a77f2752732368338110cb50fa6cd7717fvboxsync bool fEnable = RT_BOOL(uRawValue & MSR_GIM_HV_HYPERCALL_ENABLE_BIT);
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /* Is the guest disabling the hypercall-page? Allow it regardless of the Guest-OS Id Msr. */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /* Enable the hypercall-page. */
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTGCPHYS GCPhysHypercallPage = MSR_GIM_HV_HYPERCALL_GUEST_PFN(uRawValue) << PAGE_SHIFT;
f84ab9e4599e758ec1f36479f871b3f5b7f271f2vboxsync int rc = gimR3HvEnableHypercallPage(pVM, GCPhysHypercallPage);
ad4f6ac2063d9b48efd9c3193442136a8c7c890avboxsync#endif /* IN_RING3 */
ad4f6ac2063d9b48efd9c3193442136a8c7c890avboxsync#else /* IN_RING3 */
157093a77f2752732368338110cb50fa6cd7717fvboxsync /* First, update all but the TSC-page enable bit. */
157093a77f2752732368338110cb50fa6cd7717fvboxsync pHv->u64TscPageMsr = (uRawValue & ~MSR_GIM_HV_REF_TSC_ENABLE_BIT);
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /* Is the guest disabling the TSC-page? */
157093a77f2752732368338110cb50fa6cd7717fvboxsync bool fEnable = RT_BOOL(uRawValue & MSR_GIM_HV_REF_TSC_ENABLE_BIT);
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /* Enable the TSC-page. */
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTGCPHYS GCPhysTscPage = MSR_GIM_HV_REF_TSC_GUEST_PFN(uRawValue) << PAGE_SHIFT;
f84ab9e4599e758ec1f36479f871b3f5b7f271f2vboxsync int rc = gimR3HvEnableTscPage(pVM, GCPhysTscPage, false /* fUseThisTscSequence */, 0 /* uTscSequence */);
ad4f6ac2063d9b48efd9c3193442136a8c7c890avboxsync#endif /* IN_RING3 */
773940a4d3b16fc24fa111b73075a9fdcabbfc78vboxsync LogRel(("GIM: HyperV: Reset initiated through MSR.\n"));
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /* else: Ignore writes to other bits. */
ad4f6ac2063d9b48efd9c3193442136a8c7c890avboxsync#endif /* IN_RING3 */
157093a77f2752732368338110cb50fa6cd7717fvboxsync case MSR_GIM_HV_TIME_REF_COUNT: /* Read-only MSRs. */
157093a77f2752732368338110cb50fa6cd7717fvboxsync LogFunc(("WrMsr on read-only MSR %#RX32 -> #GP(0)\n", idMsr));
0bc35f54322c5f9b2d43b064f839a8cf8c99a234vboxsync LogRel(("GIM: HyperV: Unknown/invalid WrMsr (%#x,%#x`%08x) -> #GP(0)\n", idMsr,
0bc35f54322c5f9b2d43b064f839a8cf8c99a234vboxsync uRawValue & UINT64_C(0xffffffff00000000), uRawValue & UINT64_C(0xffffffff)));