1 | /* $Id: GIMR0Hv.cpp 80274 2019-08-14 14:34:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Guest Interface Manager (GIM), Hyper-V - Host Context Ring-0.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014-2019 Oracle Corporation
|
---|
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 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define VBOX_BUGREF_9217_PART_I
|
---|
23 | #define LOG_GROUP LOG_GROUP_GIM
|
---|
24 | #include <VBox/vmm/gim.h>
|
---|
25 | #include <VBox/vmm/tm.h>
|
---|
26 | #include "GIMInternal.h"
|
---|
27 | #include "GIMHvInternal.h"
|
---|
28 | #include <VBox/vmm/vmcc.h>
|
---|
29 |
|
---|
30 | #include <VBox/err.h>
|
---|
31 |
|
---|
32 | #include <iprt/spinlock.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | #if 0
|
---|
36 | /**
|
---|
37 | * Allocates and maps one physically contiguous page. The allocated page is
|
---|
38 | * zero'd out.
|
---|
39 | *
|
---|
40 | * @returns IPRT status code.
|
---|
41 | * @param pMemObj Pointer to the ring-0 memory object.
|
---|
42 | * @param ppVirt Where to store the virtual address of the
|
---|
43 | * allocation.
|
---|
44 | * @param pPhys Where to store the physical address of the
|
---|
45 | * allocation.
|
---|
46 | */
|
---|
47 | static int gimR0HvPageAllocZ(PRTR0MEMOBJ pMemObj, PRTR0PTR ppVirt, PRTHCPHYS pHCPhys)
|
---|
48 | {
|
---|
49 | AssertPtr(pMemObj);
|
---|
50 | AssertPtr(ppVirt);
|
---|
51 | AssertPtr(pHCPhys);
|
---|
52 |
|
---|
53 | int rc = RTR0MemObjAllocCont(pMemObj, PAGE_SIZE, false /* fExecutable */);
|
---|
54 | if (RT_FAILURE(rc))
|
---|
55 | return rc;
|
---|
56 | *ppVirt = RTR0MemObjAddress(*pMemObj);
|
---|
57 | *pHCPhys = RTR0MemObjGetPagePhysAddr(*pMemObj, 0 /* iPage */);
|
---|
58 | ASMMemZero32(*ppVirt, PAGE_SIZE);
|
---|
59 | return VINF_SUCCESS;
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * Frees and unmaps an allocated physical page.
|
---|
65 | *
|
---|
66 | * @param pMemObj Pointer to the ring-0 memory object.
|
---|
67 | * @param ppVirt Where to re-initialize the virtual address of
|
---|
68 | * allocation as 0.
|
---|
69 | * @param pHCPhys Where to re-initialize the physical address of the
|
---|
70 | * allocation as 0.
|
---|
71 | */
|
---|
72 | static void gimR0HvPageFree(PRTR0MEMOBJ pMemObj, PRTR0PTR ppVirt, PRTHCPHYS pHCPhys)
|
---|
73 | {
|
---|
74 | AssertPtr(pMemObj);
|
---|
75 | AssertPtr(ppVirt);
|
---|
76 | AssertPtr(pHCPhys);
|
---|
77 | if (*pMemObj != NIL_RTR0MEMOBJ)
|
---|
78 | {
|
---|
79 | int rc = RTR0MemObjFree(*pMemObj, true /* fFreeMappings */);
|
---|
80 | AssertRC(rc);
|
---|
81 | *pMemObj = NIL_RTR0MEMOBJ;
|
---|
82 | *ppVirt = 0;
|
---|
83 | *pHCPhys = 0;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | #endif
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Updates Hyper-V's reference TSC page.
|
---|
90 | *
|
---|
91 | * @returns VBox status code.
|
---|
92 | * @param pVM The cross context VM structure.
|
---|
93 | * @param u64Offset The computed TSC offset.
|
---|
94 | * @thread EMT.
|
---|
95 | */
|
---|
96 | VMM_INT_DECL(int) gimR0HvUpdateParavirtTsc(PVMCC pVM, uint64_t u64Offset)
|
---|
97 | {
|
---|
98 | Assert(GIMIsEnabled(pVM));
|
---|
99 | bool fHvTscEnabled = MSR_GIM_HV_REF_TSC_IS_ENABLED(pVM->gim.s.u.Hv.u64TscPageMsr);
|
---|
100 | if (RT_UNLIKELY(!fHvTscEnabled))
|
---|
101 | return VERR_GIM_PVTSC_NOT_ENABLED;
|
---|
102 |
|
---|
103 | /** @todo this is buggy when large pages are used due to a PGM limitation, see
|
---|
104 | * @bugref{7532}.
|
---|
105 | *
|
---|
106 | * In any case, we do not ever update this page while the guest is
|
---|
107 | * running after setting it up (in ring-3, see gimR3HvEnableTscPage()) as
|
---|
108 | * the TSC offset is handled in the VMCS/VMCB (HM) or by trapping RDTSC
|
---|
109 | * (raw-mode). */
|
---|
110 | #if 0
|
---|
111 | PCGIMHV pcHv = &pVM->gim.s.u.Hv;
|
---|
112 | PCGIMMMIO2REGION pcRegion = &pcHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
|
---|
113 | PGIMHVREFTSC pRefTsc = (PGIMHVREFTSC)pcRegion->CTX_SUFF(pvPage);
|
---|
114 | Assert(pRefTsc);
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Hyper-V reports the reference time in 100 nanosecond units.
|
---|
118 | */
|
---|
119 | uint64_t u64Tsc100Ns = pcHv->cTscTicksPerSecond / RT_NS_10MS;
|
---|
120 | int64_t i64TscOffset = (int64_t)u64Offset / u64Tsc100Ns;
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * The TSC page can be simulatenously read by other VCPUs in the guest. The
|
---|
124 | * spinlock is only for protecting simultaneous hypervisor writes from other
|
---|
125 | * EMTs.
|
---|
126 | */
|
---|
127 | RTSpinlockAcquire(pcHv->hSpinlockR0);
|
---|
128 | if (pRefTsc->i64TscOffset != i64TscOffset)
|
---|
129 | {
|
---|
130 | if (pRefTsc->u32TscSequence < UINT32_C(0xfffffffe))
|
---|
131 | ASMAtomicIncU32(&pRefTsc->u32TscSequence);
|
---|
132 | else
|
---|
133 | ASMAtomicWriteU32(&pRefTsc->u32TscSequence, 1);
|
---|
134 | ASMAtomicWriteS64(&pRefTsc->i64TscOffset, i64TscOffset);
|
---|
135 | }
|
---|
136 | RTSpinlockRelease(pcHv->hSpinlockR0);
|
---|
137 |
|
---|
138 | Assert(pRefTsc->u32TscSequence != 0);
|
---|
139 | Assert(pRefTsc->u32TscSequence != UINT32_C(0xffffffff));
|
---|
140 | #else
|
---|
141 | NOREF(u64Offset);
|
---|
142 | #endif
|
---|
143 | return VINF_SUCCESS;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Does ring-0 per-VM GIM Hyper-V initialization.
|
---|
149 | *
|
---|
150 | * @returns VBox status code.
|
---|
151 | * @param pVM The cross context VM structure.
|
---|
152 | */
|
---|
153 | VMMR0_INT_DECL(int) gimR0HvInitVM(PVMCC pVM)
|
---|
154 | {
|
---|
155 | AssertPtr(pVM);
|
---|
156 | Assert(GIMIsEnabled(pVM));
|
---|
157 |
|
---|
158 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
159 | Assert(pHv->hSpinlockR0 == NIL_RTSPINLOCK);
|
---|
160 |
|
---|
161 | int rc = RTSpinlockCreate(&pHv->hSpinlockR0, RTSPINLOCK_FLAGS_INTERRUPT_UNSAFE, "Hyper-V");
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Does ring-0 per-VM GIM Hyper-V termination.
|
---|
168 | *
|
---|
169 | * @returns VBox status code.
|
---|
170 | * @param pVM The cross context VM structure.
|
---|
171 | */
|
---|
172 | VMMR0_INT_DECL(int) gimR0HvTermVM(PVMCC pVM)
|
---|
173 | {
|
---|
174 | AssertPtr(pVM);
|
---|
175 | Assert(GIMIsEnabled(pVM));
|
---|
176 |
|
---|
177 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
178 | RTSpinlockDestroy(pHv->hSpinlockR0);
|
---|
179 | pHv->hSpinlockR0 = NIL_RTSPINLOCK;
|
---|
180 |
|
---|
181 | return VINF_SUCCESS;
|
---|
182 | }
|
---|
183 |
|
---|