[56791] | 1 | /* $Id: GIMR0Kvm.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * Guest Interface Manager (GIM), KVM - Host Context Ring-0.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[76553] | 7 | * Copyright (C) 2015-2019 Oracle Corporation
|
---|
[56791] | 8 | *
|
---|
| 9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
| 10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
| 11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
| 12 | * General Public License (GPL) as published by the Free Software
|
---|
| 13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
| 14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
| 15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
| 16 | */
|
---|
| 17 |
|
---|
[57358] | 18 |
|
---|
| 19 | /*********************************************************************************************************************************
|
---|
| 20 | * Header Files *
|
---|
| 21 | *********************************************************************************************************************************/
|
---|
[56791] | 22 | #define LOG_GROUP LOG_GROUP_GIM
|
---|
[61632] | 23 | #include <VBox/vmm/gim.h>
|
---|
| 24 | #include <VBox/vmm/tm.h>
|
---|
[56791] | 25 | #include "GIMInternal.h"
|
---|
| 26 | #include "GIMKvmInternal.h"
|
---|
[61632] | 27 | #include <VBox/vmm/vm.h>
|
---|
[56791] | 28 |
|
---|
| 29 | #include <VBox/err.h>
|
---|
| 30 |
|
---|
[56813] | 31 | #include <iprt/spinlock.h>
|
---|
[56791] | 32 |
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Updates KVM's system time information globally for all VCPUs.
|
---|
| 36 | *
|
---|
| 37 | * @returns VBox status code.
|
---|
[58122] | 38 | * @param pVM The cross context VM structure.
|
---|
[58126] | 39 | * @param pVCpu The cross context virtual CPU structure.
|
---|
[56791] | 40 | * @thread EMT.
|
---|
[56898] | 41 | * @remarks Can be called with preemption disabled!
|
---|
[56791] | 42 | */
|
---|
| 43 | VMM_INT_DECL(int) gimR0KvmUpdateSystemTime(PVM pVM, PVMCPU pVCpu)
|
---|
| 44 | {
|
---|
| 45 | /*
|
---|
| 46 | * Validate.
|
---|
| 47 | */
|
---|
| 48 | Assert(GIMIsEnabled(pVM));
|
---|
| 49 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
[56813] | 50 | AssertReturn(pKvm->hSpinlockR0 != NIL_RTSPINLOCK, VERR_GIM_IPE_3);
|
---|
[56791] | 51 |
|
---|
| 52 | /*
|
---|
| 53 | * Record the TSC and virtual NanoTS pairs.
|
---|
| 54 | */
|
---|
| 55 | uint64_t uTsc;
|
---|
| 56 | uint64_t uVirtNanoTS;
|
---|
| 57 | RTCCUINTREG fEFlags = ASMIntDisableFlags();
|
---|
| 58 | uTsc = TMCpuTickGetNoCheck(pVCpu) | UINT64_C(1);
|
---|
| 59 | uVirtNanoTS = TMVirtualGetNoCheck(pVM) | UINT64_C(1);
|
---|
| 60 | ASMSetFlags(fEFlags);
|
---|
| 61 |
|
---|
| 62 | /*
|
---|
| 63 | * Update VCPUs with this information. The first VCPU's values
|
---|
| 64 | * will be applied to the remaining.
|
---|
| 65 | */
|
---|
[56813] | 66 | RTSpinlockAcquire(pKvm->hSpinlockR0);
|
---|
[56791] | 67 | for (uint32_t i = 0; i < pVM->cCpus; i++)
|
---|
| 68 | {
|
---|
[56813] | 69 | PGIMKVMCPU pKvmCpu = &pVM->aCpus[i].gim.s.u.KvmCpu;
|
---|
[56791] | 70 | if ( !pKvmCpu->uTsc
|
---|
| 71 | && !pKvmCpu->uVirtNanoTS)
|
---|
| 72 | {
|
---|
| 73 | pKvmCpu->uTsc = uTsc;
|
---|
| 74 | pKvmCpu->uVirtNanoTS = uVirtNanoTS;
|
---|
| 75 | }
|
---|
| 76 | }
|
---|
[56813] | 77 | RTSpinlockRelease(pKvm->hSpinlockR0);
|
---|
[56791] | 78 |
|
---|
| 79 | return VINF_SUCCESS;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 |
|
---|
| 83 | /**
|
---|
| 84 | * Does ring-0 per-VM GIM KVM initialization.
|
---|
| 85 | *
|
---|
| 86 | * @returns VBox status code.
|
---|
[58122] | 87 | * @param pVM The cross context VM structure.
|
---|
[56791] | 88 | */
|
---|
| 89 | VMMR0_INT_DECL(int) gimR0KvmInitVM(PVM pVM)
|
---|
| 90 | {
|
---|
| 91 | AssertPtr(pVM);
|
---|
| 92 | Assert(GIMIsEnabled(pVM));
|
---|
| 93 |
|
---|
| 94 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
[56813] | 95 | Assert(pKvm->hSpinlockR0 == NIL_RTSPINLOCK);
|
---|
[56791] | 96 |
|
---|
[56813] | 97 | int rc = RTSpinlockCreate(&pKvm->hSpinlockR0, RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, "KVM");
|
---|
[56791] | 98 | return rc;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | /**
|
---|
| 103 | * Does ring-0 per-VM GIM KVM termination.
|
---|
| 104 | *
|
---|
| 105 | * @returns VBox status code.
|
---|
[58122] | 106 | * @param pVM The cross context VM structure.
|
---|
[56791] | 107 | */
|
---|
| 108 | VMMR0_INT_DECL(int) gimR0KvmTermVM(PVM pVM)
|
---|
| 109 | {
|
---|
| 110 | AssertPtr(pVM);
|
---|
| 111 | Assert(GIMIsEnabled(pVM));
|
---|
| 112 |
|
---|
| 113 | PGIMKVM pKvm = &pVM->gim.s.u.Kvm;
|
---|
[56813] | 114 | RTSpinlockDestroy(pKvm->hSpinlockR0);
|
---|
| 115 | pKvm->hSpinlockR0 = NIL_RTSPINLOCK;
|
---|
[56791] | 116 |
|
---|
| 117 | return VINF_SUCCESS;
|
---|
| 118 | }
|
---|
| 119 |
|
---|