/* $Id: GIMAllKvm.cpp 58123 2015-10-08 18:09:45Z vboxsync $ */ /** @file * GIM - Guest Interface Manager, KVM, All Contexts. */ /* * Copyright (C) 2015 Oracle Corporation * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; * you can redistribute it and/or modify it under the terms of the GNU * General Public License (GPL) as published by the Free Software * Foundation, in version 2 as it comes in the "COPYING" file of the * VirtualBox OSE distribution. VirtualBox OSE is distributed in the * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind. */ /********************************************************************************************************************************* * Header Files * *********************************************************************************************************************************/ #define LOG_GROUP LOG_GROUP_GIM #include "GIMKvmInternal.h" #include "GIMInternal.h" #include #include #include #include #include #include #include #include #include #include #include #include /** * Handles the KVM hypercall. * * @returns VBox status code. * @param pVCpu The cross context virtual CPU structure. * @param pCtx Pointer to the guest-CPU context. * * @thread EMT. */ VMM_INT_DECL(int) gimKvmHypercall(PVMCPU pVCpu, PCPUMCTX pCtx) { /* * Get the hypercall operation and arguments. */ bool const fIs64BitMode = CPUMIsGuestIn64BitCodeEx(pCtx); uint64_t uHyperOp = pCtx->rax; uint64_t uHyperArg0 = pCtx->rbx; uint64_t uHyperArg1 = pCtx->rcx; uint64_t uHyperArg2 = pCtx->rdi; uint64_t uHyperArg3 = pCtx->rsi; uint64_t uHyperRet = KVM_HYPERCALL_RET_ENOSYS; uint64_t uAndMask = UINT64_C(0xffffffffffffffff); if (!fIs64BitMode) { uAndMask = UINT64_C(0xffffffff); uHyperOp &= UINT64_C(0xffffffff); uHyperArg0 &= UINT64_C(0xffffffff); uHyperArg1 &= UINT64_C(0xffffffff); uHyperArg2 &= UINT64_C(0xffffffff); uHyperArg3 &= UINT64_C(0xffffffff); uHyperRet &= UINT64_C(0xffffffff); } /* * Verify that guest ring-0 is the one making the hypercall. */ uint32_t uCpl = CPUMGetGuestCPL(pVCpu); if (uCpl) { pCtx->rax = KVM_HYPERCALL_RET_EPERM & uAndMask; return VINF_SUCCESS; } /* * Do the work. */ switch (uHyperOp) { case KVM_HYPERCALL_OP_KICK_CPU: { PVM pVM = pVCpu->CTX_SUFF(pVM); if (uHyperArg1 < pVM->cCpus) { PVMCPU pVCpuTarget = &pVM->aCpus[uHyperArg1]; /** ASSUMES pVCpu index == ApicId of the VCPU. */ VMCPU_FF_SET(pVCpuTarget, VMCPU_FF_UNHALT); #ifdef IN_RING0 /* * We might be here with preemption disabled or enabled (i.e. depending on thread-context hooks * being used), so don't try obtaining the GVMMR0 used lock here. See @bugref{7270#c148}. */ GVMMR0SchedWakeUpEx(pVM, pVCpuTarget->idCpu, false /* fTakeUsedLock */); #elif defined(IN_RING3) int rc2 = SUPR3CallVMMR0(pVM->pVMR0, pVCpuTarget->idCpu, VMMR0_DO_GVMM_SCHED_WAKE_UP, NULL); AssertRC(rc2); #elif defined(IN_RC) /* Nothing to do for raw-mode, shouldn't really be used by raw-mode guests anyway. */ Assert(pVM->cCpus == 1); #endif uHyperRet = KVM_HYPERCALL_RET_SUCCESS; } break; } case KVM_HYPERCALL_OP_VAPIC_POLL_IRQ: uHyperRet = KVM_HYPERCALL_RET_SUCCESS; break; default: break; } /* * Place the result in rax/eax. */ pCtx->rax = uHyperRet & uAndMask; return VINF_SUCCESS; } /** * Returns whether the guest has configured and enabled the use of KVM's * hypercall interface. * * @returns true if hypercalls are enabled, false otherwise. * @param pVCpu The cross context virtual CPU structure. */ VMM_INT_DECL(bool) gimKvmAreHypercallsEnabled(PVMCPU pVCpu) { NOREF(pVCpu); /* KVM paravirt interface doesn't have hypercall control bits (like Hyper-V does) that guests can control, i.e. hypercalls are always enabled. */ return true; } /** * Returns whether the guest has configured and enabled the use of KVM's * paravirtualized TSC. * * @returns true if paravirt. TSC is enabled, false otherwise. * @param pVM The cross context VM structure. */ VMM_INT_DECL(bool) gimKvmIsParavirtTscEnabled(PVM pVM) { uint32_t cCpus = pVM->cCpus; for (uint32_t i = 0; i < cCpus; i++) { PVMCPU pVCpu = &pVM->aCpus[i]; PGIMKVMCPU pGimKvmCpu = &pVCpu->gim.s.u.KvmCpu; if (MSR_GIM_KVM_SYSTEM_TIME_IS_ENABLED(pGimKvmCpu->u64SystemTimeMsr)) return true; } return false; } /** * MSR read handler for KVM. * * @returns Strict VBox status code like CPUMQueryGuestMsr(). * @retval VINF_CPUM_R3_MSR_READ * @retval VERR_CPUM_RAISE_GP_0 * * @param pVCpu The cross context virtual CPU structure. * @param idMsr The MSR being read. * @param pRange The range this MSR belongs to. * @param puValue Where to store the MSR value read. */ VMM_INT_DECL(VBOXSTRICTRC) gimKvmReadMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t *puValue) { NOREF(pRange); PVM pVM = pVCpu->CTX_SUFF(pVM); PGIMKVM pKvm = &pVM->gim.s.u.Kvm; PGIMKVMCPU pKvmCpu = &pVCpu->gim.s.u.KvmCpu; switch (idMsr) { case MSR_GIM_KVM_SYSTEM_TIME: case MSR_GIM_KVM_SYSTEM_TIME_OLD: *puValue = pKvmCpu->u64SystemTimeMsr; return VINF_SUCCESS; case MSR_GIM_KVM_WALL_CLOCK: case MSR_GIM_KVM_WALL_CLOCK_OLD: *puValue = pKvm->u64WallClockMsr; return VINF_SUCCESS; default: { #ifdef IN_RING3 static uint32_t s_cTimes = 0; if (s_cTimes++ < 20) LogRel(("GIM: KVM: Unknown/invalid RdMsr (%#x) -> #GP(0)\n", idMsr)); #endif LogFunc(("Unknown/invalid RdMsr (%#RX32) -> #GP(0)\n", idMsr)); break; } } return VERR_CPUM_RAISE_GP_0; } /** * MSR write handler for KVM. * * @returns Strict VBox status code like CPUMSetGuestMsr(). * @retval VINF_CPUM_R3_MSR_WRITE * @retval VERR_CPUM_RAISE_GP_0 * * @param pVCpu The cross context virtual CPU structure. * @param idMsr The MSR being written. * @param pRange The range this MSR belongs to. * @param uRawValue The raw value with the ignored bits not masked. */ VMM_INT_DECL(VBOXSTRICTRC) gimKvmWriteMsr(PVMCPU pVCpu, uint32_t idMsr, PCCPUMMSRRANGE pRange, uint64_t uRawValue) { NOREF(pRange); PVM pVM = pVCpu->CTX_SUFF(pVM); PGIMKVMCPU pKvmCpu = &pVCpu->gim.s.u.KvmCpu; switch (idMsr) { case MSR_GIM_KVM_SYSTEM_TIME: case MSR_GIM_KVM_SYSTEM_TIME_OLD: { bool fEnable = RT_BOOL(uRawValue & MSR_GIM_KVM_SYSTEM_TIME_ENABLE_BIT); #ifdef IN_RING0 NOREF(fEnable); NOREF(pKvmCpu); gimR0KvmUpdateSystemTime(pVM, pVCpu); return VINF_CPUM_R3_MSR_WRITE; #elif defined(IN_RC) Assert(pVM->cCpus == 1); if (fEnable) { RTCCUINTREG fEFlags = ASMIntDisableFlags(); pKvmCpu->uTsc = TMCpuTickGetNoCheck(pVCpu) | UINT64_C(1); pKvmCpu->uVirtNanoTS = TMVirtualGetNoCheck(pVM) | UINT64_C(1); ASMSetFlags(fEFlags); } return VINF_CPUM_R3_MSR_WRITE; #else /* IN_RING3 */ if (!fEnable) { gimR3KvmDisableSystemTime(pVM); pKvmCpu->u64SystemTimeMsr = uRawValue; return VINF_SUCCESS; } /* Is the system-time struct. already enabled? If so, get flags that need preserving. */ uint8_t fFlags = 0; GIMKVMSYSTEMTIME SystemTime; RT_ZERO(SystemTime); if ( MSR_GIM_KVM_SYSTEM_TIME_IS_ENABLED(pKvmCpu->u64SystemTimeMsr) && MSR_GIM_KVM_SYSTEM_TIME_GUEST_GPA(uRawValue) == pKvmCpu->GCPhysSystemTime) { int rc2 = PGMPhysSimpleReadGCPhys(pVM, &SystemTime, pKvmCpu->GCPhysSystemTime, sizeof(GIMKVMSYSTEMTIME)); if (RT_SUCCESS(rc2)) pKvmCpu->fSystemTimeFlags = (SystemTime.fFlags & GIM_KVM_SYSTEM_TIME_FLAGS_GUEST_PAUSED); } /* Enable and populate the system-time struct. */ pKvmCpu->u64SystemTimeMsr = uRawValue; pKvmCpu->GCPhysSystemTime = MSR_GIM_KVM_SYSTEM_TIME_GUEST_GPA(uRawValue); pKvmCpu->u32SystemTimeVersion += 2; int rc = gimR3KvmEnableSystemTime(pVM, pVCpu); if (RT_FAILURE(rc)) { pKvmCpu->u64SystemTimeMsr = 0; return VERR_CPUM_RAISE_GP_0; } return VINF_SUCCESS; #endif } case MSR_GIM_KVM_WALL_CLOCK: case MSR_GIM_KVM_WALL_CLOCK_OLD: { #ifndef IN_RING3 return VINF_CPUM_R3_MSR_WRITE; #else /* Enable the wall-clock struct. */ RTGCPHYS GCPhysWallClock = MSR_GIM_KVM_WALL_CLOCK_GUEST_GPA(uRawValue); if (RT_LIKELY(RT_ALIGN_64(GCPhysWallClock, 4) == GCPhysWallClock)) { int rc = gimR3KvmEnableWallClock(pVM, GCPhysWallClock); if (RT_SUCCESS(rc)) { PGIMKVM pKvm = &pVM->gim.s.u.Kvm; pKvm->u64WallClockMsr = uRawValue; return VINF_SUCCESS; } } return VERR_CPUM_RAISE_GP_0; #endif /* IN_RING3 */ } default: { #ifdef IN_RING3 static uint32_t s_cTimes = 0; if (s_cTimes++ < 20) LogRel(("GIM: KVM: Unknown/invalid WrMsr (%#x,%#x`%08x) -> #GP(0)\n", idMsr, uRawValue & UINT64_C(0xffffffff00000000), uRawValue & UINT64_C(0xffffffff))); #endif LogFunc(("Unknown/invalid WrMsr (%#RX32,%#RX64) -> #GP(0)\n", idMsr, uRawValue)); break; } } return VERR_CPUM_RAISE_GP_0; } /** * Whether we need to trap \#UD exceptions in the guest. * * On AMD-V we need to trap them because paravirtualized Linux/KVM guests use * the Intel VMCALL instruction to make hypercalls and we need to trap and * optionally patch them to the AMD-V VMMCALL instruction and handle the * hypercall. * * I guess this was done so that guest teleporation between an AMD and an Intel * machine would working without any changes at the time of teleporation. * However, this also means we -always- need to intercept \#UD exceptions on one * of the two CPU models (Intel or AMD). Hyper-V solves this problem more * elegantly by letting the hypervisor supply an opaque hypercall page. * * For raw-mode VMs, this function will always return true. See gimR3KvmInit(). * * @param pVCpu The cross context virtual CPU structure. */ VMM_INT_DECL(bool) gimKvmShouldTrapXcptUD(PVMCPU pVCpu) { PVM pVM = pVCpu->CTX_SUFF(pVM); return pVM->gim.s.u.Kvm.fTrapXcptUD; } /** * Exception handler for \#UD. * * @param pVCpu The cross context virtual CPU structure. * @param pCtx Pointer to the guest-CPU context. * @param pDis Pointer to the disassembled instruction state at RIP. * Optional, can be NULL. * * @thread EMT. */ VMM_INT_DECL(int) gimKvmXcptUD(PVMCPU pVCpu, PCPUMCTX pCtx, PDISCPUSTATE pDis) { /* * If we didn't ask for #UD to be trapped, bail. */ PVM pVM = pVCpu->CTX_SUFF(pVM); PGIMKVM pKvm = &pVM->gim.s.u.Kvm; if (RT_UNLIKELY(!pVM->gim.s.u.Kvm.fTrapXcptUD)) return VERR_GIM_OPERATION_FAILED; int rc = VINF_SUCCESS; if (!pDis) { /* * Disassemble the instruction at RIP to figure out if it's the Intel VMCALL instruction * or the AMD VMMCALL instruction and if so, handle it as a hypercall. */ DISCPUSTATE Dis; rc = EMInterpretDisasCurrent(pVM, pVCpu, &Dis, NULL /* pcbInstr */); pDis = &Dis; } if (RT_SUCCESS(rc)) { /* * Patch the instruction to so we don't have to spend time disassembling it each time. * Makes sense only for HM as with raw-mode we will be getting a #UD regardless. */ if ( pDis->pCurInstr->uOpcode == OP_VMCALL || pDis->pCurInstr->uOpcode == OP_VMMCALL) { /* * Make sure guest ring-0 is the one making the hypercall. */ if (CPUMGetGuestCPL(pVCpu)) return VERR_GIM_HYPERCALL_ACCESS_DENIED; if ( pDis->pCurInstr->uOpcode != pKvm->uOpCodeNative && HMIsEnabled(pVM)) { uint8_t abHypercall[3]; size_t cbWritten = 0; rc = VMMPatchHypercall(pVM, &abHypercall, sizeof(abHypercall), &cbWritten); AssertRC(rc); Assert(sizeof(abHypercall) == pDis->cbInstr); Assert(sizeof(abHypercall) == cbWritten); rc = PGMPhysSimpleWriteGCPtr(pVCpu, pCtx->rip, &abHypercall, sizeof(abHypercall)); } /* * Update RIP and perform the hypercall. * * For HM, we can simply resume guest execution without performing the hypercall now and * do it on the next VMCALL/VMMCALL exit handler on the patched instruction. * * For raw-mode we need to do this now anyway. So we do it here regardless with an added * advantage is that it saves one world-switch for the HM case. */ if (RT_SUCCESS(rc)) { pCtx->rip += pDis->cbInstr; rc = gimKvmHypercall(pVCpu, pCtx); } } else rc = VERR_GIM_OPERATION_FAILED; } return rc; }