GIMAllHv.cpp revision eca46116c06b850c8c6be84678ba1f9dbdb3f9dd
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 * Updates Hyper-V's reference TSC page.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @returns VBox status code.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param pVM Pointer to the VM.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @param u64Offset The computed TSC offset.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * @thread EMT(pVCpu)
eca46116c06b850c8c6be84678ba1f9dbdb3f9ddvboxsyncVMM_INT_DECL(int) GIMHvUpdateParavirtTsc(PVM pVM, uint64_t u64Offset)
157093a77f2752732368338110cb50fa6cd7717fvboxsync bool fHvTscEnabled = MSR_GIM_HV_REF_TSC_IS_ENABLED(pVM->gim.s.u.Hv.u64TscPageMsr);
157093a77f2752732368338110cb50fa6cd7717fvboxsync PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
157093a77f2752732368338110cb50fa6cd7717fvboxsync PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pRegion->CTX_SUFF(pvPage);
157093a77f2752732368338110cb50fa6cd7717fvboxsync /** @todo Protect this with a spinlock! */
157093a77f2752732368338110cb50fa6cd7717fvboxsync pRefTsc->u64TscScale = UINT64_C(0x1000000000000000);
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)
bc5cd42756b3f98351040bbfccc08dd9bacd103avboxsync /* Hyper-V reports the time in 100ns units. */
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 GIMR3Mmio2Unmap(pVM, &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX]);
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);
157093a77f2752732368338110cb50fa6cd7717fvboxsync PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Is the guest disabling the hypercall-page? Allow it regardless of the Guest-OS Id Msr.
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Map the hypercall-page.
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTGCPHYS GCPhysHypercallPage = MSR_GIM_HV_HYPERCALL_GUEST_PFN(uRawValue) << PAGE_SHIFT;
157093a77f2752732368338110cb50fa6cd7717fvboxsync int rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysHypercallPage, "Hyper-V Hypercall-page");
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Patch the hypercall-page.
157093a77f2752732368338110cb50fa6cd7717fvboxsync rc = HMPatchHypercall(pVM, pRegion->pvPageR3, PAGE_SIZE, &cbWritten);
157093a77f2752732368338110cb50fa6cd7717fvboxsync uint8_t *pbLast = (uint8_t *)pRegion->pvPageR3 + cbWritten;
157093a77f2752732368338110cb50fa6cd7717fvboxsync LogRelFunc(("Enabled hypercalls at %#RGp\n", GCPhysHypercallPage));
157093a77f2752732368338110cb50fa6cd7717fvboxsync LogRelFunc(("%.*Rhxd\n", cbWritten + 1, (uint8_t *)pRegion->pvPageR3));
157093a77f2752732368338110cb50fa6cd7717fvboxsync LogFunc(("MSR_GIM_HV_HYPERCALL: HMPatchHypercall failed. rc=%Rrc cbWritten=%u\n", rc, cbWritten));
157093a77f2752732368338110cb50fa6cd7717fvboxsync /** @todo Handle raw-mode hypercall page patching. */
157093a77f2752732368338110cb50fa6cd7717fvboxsync LogRelFunc(("MSR_GIM_HV_HYPERCALL: raw-mode not yet implemented!\n"));
157093a77f2752732368338110cb50fa6cd7717fvboxsync LogFunc(("MSR_GIM_HV_HYPERCALL: GIMR3Mmio2Map failed. rc=%Rrc -> #GP(0)\n", rc));
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);
157093a77f2752732368338110cb50fa6cd7717fvboxsync PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Is the guest disabling the TSC-page?
157093a77f2752732368338110cb50fa6cd7717fvboxsync bool fEnable = RT_BOOL(uRawValue & MSR_GIM_HV_REF_TSC_ENABLE_BIT);
157093a77f2752732368338110cb50fa6cd7717fvboxsync * Map the TSC-page.
157093a77f2752732368338110cb50fa6cd7717fvboxsync RTGCPHYS GCPhysTscPage = MSR_GIM_HV_REF_TSC_GUEST_PFN(uRawValue) << PAGE_SHIFT;
157093a77f2752732368338110cb50fa6cd7717fvboxsync int rc = GIMR3Mmio2Map(pVM, pRegion, GCPhysTscPage, "Hyper-V TSC-page");
157093a77f2752732368338110cb50fa6cd7717fvboxsync Log4Func(("MSR_GIM_HV_REF_TSC: Enabled Hyper-V TSC page at %#RGp\n", GCPhysTscPage));
157093a77f2752732368338110cb50fa6cd7717fvboxsync LogFunc(("MSR_GIM_HV_REF_TSC: GIMR3Mmio2Map failed. rc=%Rrc -> #GP(0)\n", rc));
157093a77f2752732368338110cb50fa6cd7717fvboxsync#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),