1 | /* $Id: GIMHv.cpp 51560 2014-06-06 05:17:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * GIM - Guest Interface Manager, Hyper-V implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2014 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 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | #define LOG_GROUP LOG_GROUP_GIM
|
---|
22 | #include "GIMInternal.h"
|
---|
23 |
|
---|
24 | #include <iprt/assert.h>
|
---|
25 | #include <iprt/err.h>
|
---|
26 | #include <iprt/string.h>
|
---|
27 | #include <iprt/mem.h>
|
---|
28 |
|
---|
29 | #include <VBox/vmm/cpum.h>
|
---|
30 | #include <VBox/vmm/vm.h>
|
---|
31 | #include <VBox/vmm/hm.h>
|
---|
32 | #include <VBox/vmm/pdmapi.h>
|
---|
33 | #include <VBox/version.h>
|
---|
34 |
|
---|
35 | /*******************************************************************************
|
---|
36 | * Defined Constants And Macros *
|
---|
37 | *******************************************************************************/
|
---|
38 | //#define GIMHV_HYPERCALL "GIMHvHypercall"
|
---|
39 | #ifdef VBOX_WITH_STATISTICS
|
---|
40 | # define GIMHV_MSRRANGE(a_uFirst, a_uLast, a_szName) \
|
---|
41 | { (a_uFirst), (a_uLast), kCpumMsrRdFn_Gim, kCpumMsrWrFn_Gim, 0, 0, 0, 0, 0, a_szName, { 0 }, { 0 }, { 0 }, { 0 } }
|
---|
42 | #else
|
---|
43 | # define GIMHV_MSRRANGE(a_uFirst, a_uLast, a_szName) \
|
---|
44 | { (a_uFirst), (a_uLast), kCpumMsrRdFn_Gim, kCpumMsrWrFn_Gim, 0, 0, 0, 0, 0, a_szName }
|
---|
45 | #endif
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Array of MSR ranges supported by Hyper-V.
|
---|
49 | */
|
---|
50 | static CPUMMSRRANGE const g_aMsrRanges_HyperV[] =
|
---|
51 | {
|
---|
52 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE0_START, MSR_GIM_HV_RANGE0_END, "Hyper-V range 0"),
|
---|
53 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE1_START, MSR_GIM_HV_RANGE1_END, "Hyper-V range 1"),
|
---|
54 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE2_START, MSR_GIM_HV_RANGE2_END, "Hyper-V range 2"),
|
---|
55 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE3_START, MSR_GIM_HV_RANGE3_END, "Hyper-V range 3"),
|
---|
56 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE4_START, MSR_GIM_HV_RANGE4_END, "Hyper-V range 4"),
|
---|
57 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE5_START, MSR_GIM_HV_RANGE5_END, "Hyper-V range 5"),
|
---|
58 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE6_START, MSR_GIM_HV_RANGE6_END, "Hyper-V range 6"),
|
---|
59 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE7_START, MSR_GIM_HV_RANGE7_END, "Hyper-V range 7"),
|
---|
60 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE8_START, MSR_GIM_HV_RANGE8_END, "Hyper-V range 8"),
|
---|
61 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE9_START, MSR_GIM_HV_RANGE9_END, "Hyper-V range 9"),
|
---|
62 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE10_START, MSR_GIM_HV_RANGE10_END, "Hyper-V range 10"),
|
---|
63 | GIMHV_MSRRANGE(MSR_GIM_HV_RANGE11_START, MSR_GIM_HV_RANGE11_END, "Hyper-V range 11")
|
---|
64 | };
|
---|
65 | #undef GIMHV_MSR
|
---|
66 |
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Initializes the Hyper-V GIM provider.
|
---|
70 | *
|
---|
71 | * @returns VBox status code.
|
---|
72 | * @param pVM Pointer to the VM.
|
---|
73 | * @param uVersion The interface version this VM should use.
|
---|
74 | */
|
---|
75 | VMMR3_INT_DECL(int) GIMR3HvInit(PVM pVM)
|
---|
76 | {
|
---|
77 | AssertReturn(pVM, VERR_INVALID_PARAMETER);
|
---|
78 | AssertReturn(pVM->gim.s.enmProviderId == GIMPROVIDERID_HYPERV, VERR_INTERNAL_ERROR_5);
|
---|
79 |
|
---|
80 | int rc;
|
---|
81 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Determine interface capabilities based on the version.
|
---|
85 | */
|
---|
86 | if (!pVM->gim.s.u32Version)
|
---|
87 | {
|
---|
88 | pHv->uBaseFeat = 0
|
---|
89 | //| GIM_HV_BASE_FEAT_VP_RUNTIME_MSR
|
---|
90 | | GIM_HV_BASE_FEAT_PART_TIME_REF_COUNT_MSR
|
---|
91 | //| GIM_HV_BASE_FEAT_BASIC_SYNTH_IC
|
---|
92 | //| GIM_HV_BASE_FEAT_SYNTH_TIMER_MSRS
|
---|
93 | //| GIM_HV_BASE_FEAT_APIC_ACCESS_MSRS
|
---|
94 | | GIM_HV_BASE_FEAT_HYPERCALL_MSRS
|
---|
95 | | GIM_HV_BASE_FEAT_VP_ID_MSR
|
---|
96 | //| GIM_HV_BASE_FEAT_VIRT_SYS_RESET_MSR
|
---|
97 | //| GIM_HV_BASE_FEAT_STAT_PAGES_MSR
|
---|
98 | | GIM_HV_BASE_FEAT_PART_REF_TSC_MSR
|
---|
99 | //| GIM_HV_BASE_FEAT_GUEST_IDLE_STATE_MSR
|
---|
100 | | GIM_HV_BASE_FEAT_TIMER_FREQ_MSRS
|
---|
101 | //| GIM_HV_BASE_FEAT_DEBUG_MSRS
|
---|
102 | ;
|
---|
103 |
|
---|
104 | pHv->uMiscFeat = GIM_HV_MISC_FEAT_TIMER_FREQ;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /*
|
---|
108 | * Populate the required fields in MMIO2 region records for registering.
|
---|
109 | */
|
---|
110 | AssertCompile(GIM_HV_PAGE_SIZE == PAGE_SIZE);
|
---|
111 | PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[GIM_HV_HYPERCALL_PAGE_REGION_IDX];
|
---|
112 | pRegion->iRegion = GIM_HV_HYPERCALL_PAGE_REGION_IDX;
|
---|
113 | pRegion->cbRegion = PAGE_SIZE;
|
---|
114 | pRegion->GCPhysPage = NIL_RTGCPHYS;
|
---|
115 | RTStrCopy(pRegion->szDescription, sizeof(pRegion->szDescription), "Hypercall Page");
|
---|
116 | Assert(!pRegion->fRCMapping);
|
---|
117 | Assert(!pRegion->fMapped);
|
---|
118 |
|
---|
119 | pRegion = &pHv->aMmio2Regions[GIM_HV_REF_TSC_PAGE_REGION_IDX];
|
---|
120 | pRegion->iRegion = GIM_HV_REF_TSC_PAGE_REGION_IDX;
|
---|
121 | pRegion->cbRegion = PAGE_SIZE;
|
---|
122 | pRegion->GCPhysPage = NIL_RTGCPHYS;
|
---|
123 | RTStrCopy(pRegion->szDescription, sizeof(pRegion->szDescription), "TSC Page");
|
---|
124 |
|
---|
125 | /*
|
---|
126 | * Make sure the CPU ID bit are in accordance to the Hyper-V
|
---|
127 | * requirement and other paranoia checks.
|
---|
128 | * See "Requirements for implementing the Microsoft hypervisor interface" spec.
|
---|
129 | */
|
---|
130 | Assert(!(pHv->uPartFlags & ( GIM_HV_PART_FLAGS_CREATE_PART
|
---|
131 | | GIM_HV_PART_FLAGS_ACCESS_MEMORY_POOL
|
---|
132 | | GIM_HV_PART_FLAGS_ACCESS_PART_ID
|
---|
133 | | GIM_HV_PART_FLAGS_ADJUST_MSG_BUFFERS
|
---|
134 | | GIM_HV_PART_FLAGS_CREATE_PORT
|
---|
135 | | GIM_HV_PART_FLAGS_ACCESS_STATS
|
---|
136 | | GIM_HV_PART_FLAGS_CPU_MGMT
|
---|
137 | | GIM_HV_PART_FLAGS_CPU_PROFILER)));
|
---|
138 | Assert((pHv->uBaseFeat & (GIM_HV_BASE_FEAT_HYPERCALL_MSRS | GIM_HV_BASE_FEAT_VP_ID_MSR))
|
---|
139 | == (GIM_HV_BASE_FEAT_HYPERCALL_MSRS | GIM_HV_BASE_FEAT_VP_ID_MSR));
|
---|
140 | for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
|
---|
141 | {
|
---|
142 | PCGIMMMIO2REGION pcCur = &pHv->aMmio2Regions[i];
|
---|
143 | Assert(!pcCur->fRCMapping);
|
---|
144 | Assert(!pcCur->fMapped);
|
---|
145 | Assert(pcCur->GCPhysPage == NIL_RTGCPHYS);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /*
|
---|
149 | * Expose HVP (Hypervisor Present) bit to the guest.
|
---|
150 | */
|
---|
151 | CPUMSetGuestCpuIdFeature(pVM, CPUMCPUIDFEATURE_HVP);
|
---|
152 |
|
---|
153 | /*
|
---|
154 | * Modify the standard hypervisor leaves for Hyper-V.
|
---|
155 | */
|
---|
156 | CPUMCPUIDLEAF HyperLeaf;
|
---|
157 | RT_ZERO(HyperLeaf);
|
---|
158 | HyperLeaf.uLeaf = UINT32_C(0x40000000);
|
---|
159 | HyperLeaf.uEax = UINT32_C(0x40000005); /* Minimum value for Hyper-V */
|
---|
160 | HyperLeaf.uEbx = 0x7263694D; /* 'Micr' */
|
---|
161 | HyperLeaf.uEcx = 0x666F736F; /* 'osof' */
|
---|
162 | HyperLeaf.uEdx = 0x76482074; /* 't Hv' */
|
---|
163 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
164 | AssertLogRelRCReturn(rc, rc);
|
---|
165 |
|
---|
166 | HyperLeaf.uLeaf = UINT32_C(0x40000001);
|
---|
167 | HyperLeaf.uEax = 0x31237648; /* 'Hv#1' */
|
---|
168 | HyperLeaf.uEbx = 0; /* Reserved */
|
---|
169 | HyperLeaf.uEcx = 0; /* Reserved */
|
---|
170 | HyperLeaf.uEdx = 0; /* Reserved */
|
---|
171 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
172 | AssertLogRelRCReturn(rc, rc);
|
---|
173 |
|
---|
174 | /*
|
---|
175 | * Add Hyper-V specific leaves.
|
---|
176 | */
|
---|
177 | HyperLeaf.uLeaf = UINT32_C(0x40000002); /* MBZ until MSR_GIM_HV_GUEST_OS_ID is set by the guest. */
|
---|
178 | HyperLeaf.uEax = 0;
|
---|
179 | HyperLeaf.uEbx = 0;
|
---|
180 | HyperLeaf.uEcx = 0;
|
---|
181 | HyperLeaf.uEdx = 0;
|
---|
182 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
183 | AssertLogRelRCReturn(rc, rc);
|
---|
184 |
|
---|
185 | HyperLeaf.uLeaf = UINT32_C(0x40000003);
|
---|
186 | HyperLeaf.uEax = pHv->uBaseFeat;
|
---|
187 | HyperLeaf.uEbx = pHv->uPartFlags;
|
---|
188 | HyperLeaf.uEcx = pHv->uPowMgmtFeat;
|
---|
189 | HyperLeaf.uEdx = pHv->uMiscFeat;
|
---|
190 | rc = CPUMR3CpuIdInsert(pVM, &HyperLeaf);
|
---|
191 | AssertLogRelRCReturn(rc, rc);
|
---|
192 |
|
---|
193 | /*
|
---|
194 | * Insert all MSR ranges of Hyper-V.
|
---|
195 | */
|
---|
196 | for (unsigned i = 0; i < RT_ELEMENTS(g_aMsrRanges_HyperV); i++)
|
---|
197 | {
|
---|
198 | rc = CPUMR3MsrRangesInsert(pVM, &g_aMsrRanges_HyperV[i]);
|
---|
199 | AssertLogRelRCReturn(rc, rc);
|
---|
200 | }
|
---|
201 |
|
---|
202 | return VINF_SUCCESS;
|
---|
203 | }
|
---|
204 |
|
---|
205 |
|
---|
206 | #if 0
|
---|
207 | VMMR3_INT_DECL(int) GIMR3HvInitFinalize(PVM pVM)
|
---|
208 | {
|
---|
209 | pVM->gim.s.pfnHypercallR3 = &GIMHvHypercall;
|
---|
210 | if (!HMIsEnabled(pVM))
|
---|
211 | {
|
---|
212 | rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
|
---|
213 | AssertRCReturn(rc, rc);
|
---|
214 | }
|
---|
215 | rc = PDMR3LdrGetSymbolR0(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallR0);
|
---|
216 | AssertRCReturn(rc, rc);
|
---|
217 | }
|
---|
218 | #endif
|
---|
219 |
|
---|
220 |
|
---|
221 | VMMR3_INT_DECL(void) GIMR3HvRelocate(PVM pVM, RTGCINTPTR offDelta)
|
---|
222 | {
|
---|
223 | #if 0
|
---|
224 | int rc = PDMR3LdrGetSymbolRC(pVM, NULL /* pszModule */, GIMHV_HYPERCALL, &pVM->gim.s.pfnHypercallRC);
|
---|
225 | AssertFatalRC(rc);
|
---|
226 | #endif
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * The VM is being reset. This resets Hyper-V provider MSRs and unmaps whatever
|
---|
232 | * Hyper-V regions that the guest may have mapped.
|
---|
233 | *
|
---|
234 | * @param pVM Pointer to the VM.
|
---|
235 | */
|
---|
236 | VMMR3_INT_DECL(void) GIMR3HvReset(PVM pVM)
|
---|
237 | {
|
---|
238 | /*
|
---|
239 | * Unmap MMIO2 pages that the guest may have setup.
|
---|
240 | */
|
---|
241 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
242 | for (unsigned i = 0; i < RT_ELEMENTS(pHv->aMmio2Regions); i++)
|
---|
243 | {
|
---|
244 | PGIMMMIO2REGION pRegion = &pHv->aMmio2Regions[i];
|
---|
245 | GIMR3Mmio2Unmap(pVM, pRegion);
|
---|
246 | }
|
---|
247 |
|
---|
248 | /*
|
---|
249 | * Reset MSRs.
|
---|
250 | */
|
---|
251 | pHv->u64GuestOsIdMsr = 0;
|
---|
252 | pHv->u64HypercallMsr = 0;
|
---|
253 | pHv->u64TscPageMsr = 0;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * Returns a pointer to the MMIO2 regions supported by Hyper-V.
|
---|
259 | *
|
---|
260 | * @returns Pointer to an array of MMIO2 regions.
|
---|
261 | * @param pVM Pointer to the VM.
|
---|
262 | * @param pcRegions Where to store the number of regions in the array.
|
---|
263 | */
|
---|
264 | VMMR3_INT_DECL(PGIMMMIO2REGION) GIMR3HvGetMmio2Regions(PVM pVM, uint32_t *pcRegions)
|
---|
265 | {
|
---|
266 | Assert(GIMIsEnabled(pVM));
|
---|
267 | PGIMHV pHv = &pVM->gim.s.u.Hv;
|
---|
268 |
|
---|
269 | *pcRegions = RT_ELEMENTS(pHv->aMmio2Regions);
|
---|
270 | Assert(*pcRegions <= UINT8_MAX); /* See PGMR3PhysMMIO2Register(). */
|
---|
271 | return pHv->aMmio2Regions;
|
---|
272 | }
|
---|
273 |
|
---|