GIMAllHv.cpp revision 9c01b6fe4b2d2f7184243aaf2864c06351869401
236b2935f217749893b7034e59da3e3568928acevboxsync * GIM - Guest Interface Manager, Microsoft Hyper-V, All Contexts.
236b2935f217749893b7034e59da3e3568928acevboxsync * Copyright (C) 2014 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.
eca46116c06b850c8c6be84678ba1f9dbdb3f9ddvboxsyncVMM_INT_DECL(int) GIMHvHypercall(PVMCPU pVCpu, PCPUMCTX pCtx)
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.
eca46116c06b850c8c6be84678ba1f9dbdb3f9ddvboxsyncVMM_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.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @returns VBox status code.
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.
eca46116c06b850c8c6be84678ba1f9dbdb3f9ddvboxsyncVMM_INT_DECL(int) GIMHvReadMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t *puValue)
8de3062c5ad947972fb5aa6659de0f6abd5a3f7fvboxsync /** @todo r=ramshankar: Shouldn't we add the TSC offset here? */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /* Hyper-V reports the time in 100 ns units (10 MHz). */
bc5cd42756b3f98351040bbfccc08dd9bacd103avboxsync uint64_t u64Tsc100Ns = u64TscHz / UINT64_C(10000000); /* 100 ns */
157093a77f2752732368338110cb50fa6cd7717fvboxsync /** @todo Fix this later! Get the information from DevApic. */
157093a77f2752732368338110cb50fa6cd7717fvboxsync *puValue = UINT32_C(1000000000); /* TMCLOCK_FREQ_VIRTUAL */
bc5cd42756b3f98351040bbfccc08dd9bacd103avboxsync LogRel(("GIMHvReadMsr: Unknown/invalid RdMsr %#RX32 -> #GP(0)\n", idMsr));
157093a77f2752732368338110cb50fa6cd7717fvboxsync * MSR write handler for Hyper-V.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @returns VBox status code.
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.
eca46116c06b850c8c6be84678ba1f9dbdb3f9ddvboxsyncVMM_INT_DECL(int) GIMHvWriteMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t uRawValue)
157093a77f2752732368338110cb50fa6cd7717fvboxsync /* Disable the hypercall-page if 0 is written to this MSR. */
157093a77f2752732368338110cb50fa6cd7717fvboxsync pHv->u64HypercallMsr &= ~MSR_GIM_HV_HYPERCALL_ENABLE_BIT;
157093a77f2752732368338110cb50fa6cd7717fvboxsync#endif /* !IN_RING3 */
157093a77f2752732368338110cb50fa6cd7717fvboxsync#else /* IN_RING3 */
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;
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync int rc = GIMR3HvEnableHypercallPage(pVM, GCPhysHypercallPage);
157093a77f2752732368338110cb50fa6cd7717fvboxsync#endif /* !IN_RING3 */
157093a77f2752732368338110cb50fa6cd7717fvboxsync#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;
9c01b6fe4b2d2f7184243aaf2864c06351869401vboxsync int rc = GIMR3HvEnableTscPage(pVM, GCPhysTscPage, false /* fUseThisTscSequence */, 0 /* uTscSequence */);
157093a77f2752732368338110cb50fa6cd7717fvboxsync#endif /* !IN_RING3 */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync LogRel(("GIM: HyperV: Reset initiated by MSR.\n"));
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync /* else: Ignore writes to other bits. */
2ac3892cdc8b16a0dee55e8b4510b8ecea83c95fvboxsync#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));
157093a77f2752732368338110cb50fa6cd7717fvboxsync LogRel(("GIM: Unknown/invalid WrMsr (%#x,%#x`%08x) -> #GP(0)\n", idMsr, uRawValue & UINT64_C(0xffffffff00000000),