1 | /* $Id: PGMR0.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PGM - Page Manager and Monitor, Ring-0.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2022 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 LOG_GROUP LOG_GROUP_PGM
|
---|
23 | #define VBOX_WITHOUT_PAGING_BIT_FIELDS /* 64-bit bitfields are just asking for trouble. See @bugref{9841} and others. */
|
---|
24 | #include <VBox/rawpci.h>
|
---|
25 | #include <VBox/vmm/pgm.h>
|
---|
26 | #include <VBox/vmm/gmm.h>
|
---|
27 | #include "PGMInternal.h"
|
---|
28 | #include <VBox/vmm/pdmdev.h>
|
---|
29 | #include <VBox/vmm/vmcc.h>
|
---|
30 | #include <VBox/vmm/gvm.h>
|
---|
31 | #include "PGMInline.h"
|
---|
32 | #include <VBox/log.h>
|
---|
33 | #include <VBox/err.h>
|
---|
34 | #include <iprt/assert.h>
|
---|
35 | #include <iprt/mem.h>
|
---|
36 | #include <iprt/memobj.h>
|
---|
37 | #include <iprt/time.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*
|
---|
41 | * Instantiate the ring-0 header/code templates.
|
---|
42 | */
|
---|
43 | /** @todo r=bird: Gotta love this nested paging hacking we're still carrying with us... (Split PGM_TYPE_NESTED.) */
|
---|
44 | #define PGM_BTH_NAME(name) PGM_BTH_NAME_32BIT_PROT(name)
|
---|
45 | #include "PGMR0Bth.h"
|
---|
46 | #undef PGM_BTH_NAME
|
---|
47 |
|
---|
48 | #define PGM_BTH_NAME(name) PGM_BTH_NAME_PAE_PROT(name)
|
---|
49 | #include "PGMR0Bth.h"
|
---|
50 | #undef PGM_BTH_NAME
|
---|
51 |
|
---|
52 | #define PGM_BTH_NAME(name) PGM_BTH_NAME_AMD64_PROT(name)
|
---|
53 | #include "PGMR0Bth.h"
|
---|
54 | #undef PGM_BTH_NAME
|
---|
55 |
|
---|
56 | #define PGM_BTH_NAME(name) PGM_BTH_NAME_EPT_PROT(name)
|
---|
57 | #include "PGMR0Bth.h"
|
---|
58 | #undef PGM_BTH_NAME
|
---|
59 |
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Initializes the per-VM data for the PGM.
|
---|
63 | *
|
---|
64 | * This is called from under the GVMM lock, so it should only initialize the
|
---|
65 | * data so PGMR0CleanupVM and others will work smoothly.
|
---|
66 | *
|
---|
67 | * @returns VBox status code.
|
---|
68 | * @param pGVM Pointer to the global VM structure.
|
---|
69 | */
|
---|
70 | VMMR0_INT_DECL(int) PGMR0InitPerVMData(PGVM pGVM)
|
---|
71 | {
|
---|
72 | AssertCompile(sizeof(pGVM->pgm.s) <= sizeof(pGVM->pgm.padding));
|
---|
73 | AssertCompile(sizeof(pGVM->pgmr0.s) <= sizeof(pGVM->pgmr0.padding));
|
---|
74 |
|
---|
75 | AssertCompile(RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMemObjs) == RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMapObjs));
|
---|
76 | for (uint32_t i = 0; i < RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMemObjs); i++)
|
---|
77 | {
|
---|
78 | pGVM->pgmr0.s.ahPoolMemObjs[i] = NIL_RTR0MEMOBJ;
|
---|
79 | pGVM->pgmr0.s.ahPoolMapObjs[i] = NIL_RTR0MEMOBJ;
|
---|
80 | }
|
---|
81 | return RTCritSectInit(&pGVM->pgmr0.s.PoolGrowCritSect);
|
---|
82 | }
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Initalize the per-VM PGM for ring-0.
|
---|
87 | *
|
---|
88 | * @returns VBox status code.
|
---|
89 | * @param pGVM Pointer to the global VM structure.
|
---|
90 | */
|
---|
91 | VMMR0_INT_DECL(int) PGMR0InitVM(PGVM pGVM)
|
---|
92 | {
|
---|
93 | RT_NOREF(pGVM);
|
---|
94 | /* Was used for DynMap init */
|
---|
95 | return VINF_SUCCESS;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Cleans up any loose ends before the GVM structure is destroyed.
|
---|
101 | */
|
---|
102 | VMMR0_INT_DECL(void) PGMR0CleanupVM(PGVM pGVM)
|
---|
103 | {
|
---|
104 | for (uint32_t i = 0; i < RT_ELEMENTS(pGVM->pgmr0.s.ahPoolMemObjs); i++)
|
---|
105 | {
|
---|
106 | if (pGVM->pgmr0.s.ahPoolMapObjs[i] != NIL_RTR0MEMOBJ)
|
---|
107 | {
|
---|
108 | int rc = RTR0MemObjFree(pGVM->pgmr0.s.ahPoolMapObjs[i], true /*fFreeMappings*/);
|
---|
109 | AssertRC(rc);
|
---|
110 | pGVM->pgmr0.s.ahPoolMapObjs[i] = NIL_RTR0MEMOBJ;
|
---|
111 | }
|
---|
112 |
|
---|
113 | if (pGVM->pgmr0.s.ahPoolMemObjs[i] != NIL_RTR0MEMOBJ)
|
---|
114 | {
|
---|
115 | int rc = RTR0MemObjFree(pGVM->pgmr0.s.ahPoolMemObjs[i], true /*fFreeMappings*/);
|
---|
116 | AssertRC(rc);
|
---|
117 | pGVM->pgmr0.s.ahPoolMemObjs[i] = NIL_RTR0MEMOBJ;
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 | if (RTCritSectIsInitialized(&pGVM->pgmr0.s.PoolGrowCritSect))
|
---|
122 | RTCritSectDelete(&pGVM->pgmr0.s.PoolGrowCritSect);
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * Worker function for PGMR3PhysAllocateHandyPages and pgmPhysEnsureHandyPage.
|
---|
128 | *
|
---|
129 | * @returns The following VBox status codes.
|
---|
130 | * @retval VINF_SUCCESS on success. FF cleared.
|
---|
131 | * @retval VINF_EM_NO_MEMORY if we're out of memory. The FF is set in this case.
|
---|
132 | *
|
---|
133 | * @param pGVM The global (ring-0) VM structure.
|
---|
134 | * @param idCpu The ID of the calling EMT.
|
---|
135 | * @param fRing3 Set if the caller is ring-3. Determins whether to
|
---|
136 | * return VINF_EM_NO_MEMORY or not.
|
---|
137 | *
|
---|
138 | * @thread EMT(idCpu)
|
---|
139 | *
|
---|
140 | * @remarks Must be called from within the PGM critical section. The caller
|
---|
141 | * must clear the new pages.
|
---|
142 | */
|
---|
143 | int pgmR0PhysAllocateHandyPages(PGVM pGVM, VMCPUID idCpu, bool fRing3)
|
---|
144 | {
|
---|
145 | /*
|
---|
146 | * Validate inputs.
|
---|
147 | */
|
---|
148 | AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID); /* caller already checked this, but just to be sure. */
|
---|
149 | Assert(pGVM->aCpus[idCpu].hEMT == RTThreadNativeSelf());
|
---|
150 | PGM_LOCK_ASSERT_OWNER_EX(pGVM, &pGVM->aCpus[idCpu]);
|
---|
151 |
|
---|
152 | /*
|
---|
153 | * Check for error injection.
|
---|
154 | */
|
---|
155 | if (RT_LIKELY(!pGVM->pgm.s.fErrInjHandyPages))
|
---|
156 | { /* likely */ }
|
---|
157 | else
|
---|
158 | return VERR_NO_MEMORY;
|
---|
159 |
|
---|
160 | /*
|
---|
161 | * Try allocate a full set of handy pages.
|
---|
162 | */
|
---|
163 | uint32_t const iFirst = pGVM->pgm.s.cHandyPages;
|
---|
164 | AssertMsgReturn(iFirst <= RT_ELEMENTS(pGVM->pgm.s.aHandyPages), ("%#x\n", iFirst), VERR_PGM_HANDY_PAGE_IPE);
|
---|
165 |
|
---|
166 | uint32_t const cPages = RT_ELEMENTS(pGVM->pgm.s.aHandyPages) - iFirst;
|
---|
167 | if (!cPages)
|
---|
168 | return VINF_SUCCESS;
|
---|
169 |
|
---|
170 | int rc = GMMR0AllocateHandyPages(pGVM, idCpu, cPages, cPages, &pGVM->pgm.s.aHandyPages[iFirst]);
|
---|
171 | if (RT_SUCCESS(rc))
|
---|
172 | {
|
---|
173 | uint32_t const cHandyPages = RT_ELEMENTS(pGVM->pgm.s.aHandyPages); /** @todo allow allocating less... */
|
---|
174 | pGVM->pgm.s.cHandyPages = cHandyPages;
|
---|
175 | VM_FF_CLEAR(pGVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
176 | VM_FF_CLEAR(pGVM, VM_FF_PGM_NO_MEMORY);
|
---|
177 |
|
---|
178 | #ifdef VBOX_STRICT
|
---|
179 | for (uint32_t i = 0; i < cHandyPages; i++)
|
---|
180 | {
|
---|
181 | Assert(pGVM->pgm.s.aHandyPages[i].idPage != NIL_GMM_PAGEID);
|
---|
182 | Assert(pGVM->pgm.s.aHandyPages[i].idPage <= GMM_PAGEID_LAST);
|
---|
183 | Assert(pGVM->pgm.s.aHandyPages[i].idSharedPage == NIL_GMM_PAGEID);
|
---|
184 | Assert(pGVM->pgm.s.aHandyPages[i].HCPhysGCPhys != NIL_GMMPAGEDESC_PHYS);
|
---|
185 | Assert(!(pGVM->pgm.s.aHandyPages[i].HCPhysGCPhys & ~X86_PTE_PAE_PG_MASK));
|
---|
186 | }
|
---|
187 | #endif
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * Clear the pages.
|
---|
191 | */
|
---|
192 | for (uint32_t iPage = iFirst; iPage < cHandyPages; iPage++)
|
---|
193 | {
|
---|
194 | PGMMPAGEDESC pPage = &pGVM->pgm.s.aHandyPages[iPage];
|
---|
195 | if (!pPage->fZeroed)
|
---|
196 | {
|
---|
197 | void *pv = NULL;
|
---|
198 | #ifdef VBOX_WITH_LINEAR_HOST_PHYS_MEM
|
---|
199 | rc = SUPR0HCPhysToVirt(pPage->HCPhysGCPhys, &pv);
|
---|
200 | #else
|
---|
201 | rc = GMMR0PageIdToVirt(pGVM, pPage->idPage, &pv);
|
---|
202 | #endif
|
---|
203 | AssertMsgRCReturn(rc, ("idPage=%#x HCPhys=%RHp rc=%Rrc\n", pPage->idPage, pPage->HCPhysGCPhys, rc), rc);
|
---|
204 |
|
---|
205 | ASMMemZeroPage(pv);
|
---|
206 | pPage->fZeroed = true;
|
---|
207 | }
|
---|
208 | #ifdef VBOX_STRICT
|
---|
209 | else
|
---|
210 | {
|
---|
211 | void *pv = NULL;
|
---|
212 | # ifdef VBOX_WITH_LINEAR_HOST_PHYS_MEM
|
---|
213 | rc = SUPR0HCPhysToVirt(pPage->HCPhysGCPhys, &pv);
|
---|
214 | # else
|
---|
215 | rc = GMMR0PageIdToVirt(pGVM, pPage->idPage, &pv);
|
---|
216 | # endif
|
---|
217 | AssertMsgRCReturn(rc, ("idPage=%#x HCPhys=%RHp rc=%Rrc\n", pPage->idPage, pPage->HCPhysGCPhys, rc), rc);
|
---|
218 | AssertReturn(ASMMemIsZeroPage(pv), VERR_PGM_HANDY_PAGE_IPE);
|
---|
219 | }
|
---|
220 | #endif
|
---|
221 | Log3(("PGMR0PhysAllocateHandyPages: idPage=%#x HCPhys=%RGp\n", pPage->idPage, pPage->HCPhysGCPhys));
|
---|
222 | }
|
---|
223 | }
|
---|
224 | else
|
---|
225 | {
|
---|
226 | /*
|
---|
227 | * We should never get here unless there is a genuine shortage of
|
---|
228 | * memory (or some internal error). Flag the error so the VM can be
|
---|
229 | * suspended ASAP and the user informed. If we're totally out of
|
---|
230 | * handy pages we will return failure.
|
---|
231 | */
|
---|
232 | /* Report the failure. */
|
---|
233 | LogRel(("PGM: Failed to procure handy pages; rc=%Rrc cHandyPages=%#x\n"
|
---|
234 | " cAllPages=%#x cPrivatePages=%#x cSharedPages=%#x cZeroPages=%#x\n",
|
---|
235 | rc, pGVM->pgm.s.cHandyPages,
|
---|
236 | pGVM->pgm.s.cAllPages, pGVM->pgm.s.cPrivatePages, pGVM->pgm.s.cSharedPages, pGVM->pgm.s.cZeroPages));
|
---|
237 |
|
---|
238 | GMMMEMSTATSREQ Stats = { { SUPVMMR0REQHDR_MAGIC, sizeof(Stats) }, 0, 0, 0, 0, 0 };
|
---|
239 | if (RT_SUCCESS(GMMR0QueryMemoryStatsReq(pGVM, idCpu, &Stats)))
|
---|
240 | LogRel(("GMM: Statistics:\n"
|
---|
241 | " Allocated pages: %RX64\n"
|
---|
242 | " Free pages: %RX64\n"
|
---|
243 | " Shared pages: %RX64\n"
|
---|
244 | " Maximum pages: %RX64\n"
|
---|
245 | " Ballooned pages: %RX64\n",
|
---|
246 | Stats.cAllocPages, Stats.cFreePages, Stats.cSharedPages, Stats.cMaxPages, Stats.cBalloonedPages));
|
---|
247 |
|
---|
248 | if ( rc != VERR_NO_MEMORY
|
---|
249 | && rc != VERR_NO_PHYS_MEMORY
|
---|
250 | && rc != VERR_LOCK_FAILED)
|
---|
251 | for (uint32_t iPage = 0; iPage < RT_ELEMENTS(pGVM->pgm.s.aHandyPages); iPage++)
|
---|
252 | LogRel(("PGM: aHandyPages[#%#04x] = {.HCPhysGCPhys=%RHp, .idPage=%#08x, .idSharedPage=%#08x}\n",
|
---|
253 | iPage, pGVM->pgm.s.aHandyPages[iPage].HCPhysGCPhys, pGVM->pgm.s.aHandyPages[iPage].idPage,
|
---|
254 | pGVM->pgm.s.aHandyPages[iPage].idSharedPage));
|
---|
255 |
|
---|
256 | /* Set the FFs and adjust rc. */
|
---|
257 | VM_FF_SET(pGVM, VM_FF_PGM_NEED_HANDY_PAGES);
|
---|
258 | VM_FF_SET(pGVM, VM_FF_PGM_NO_MEMORY);
|
---|
259 | if (!fRing3)
|
---|
260 | if ( rc == VERR_NO_MEMORY
|
---|
261 | || rc == VERR_NO_PHYS_MEMORY
|
---|
262 | || rc == VERR_LOCK_FAILED
|
---|
263 | || rc == VERR_MAP_FAILED)
|
---|
264 | rc = VINF_EM_NO_MEMORY;
|
---|
265 | }
|
---|
266 |
|
---|
267 | LogFlow(("PGMR0PhysAllocateHandyPages: cPages=%d rc=%Rrc\n", cPages, rc));
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Worker function for PGMR3PhysAllocateHandyPages / VMMR0_DO_PGM_ALLOCATE_HANDY_PAGES.
|
---|
274 | *
|
---|
275 | * @returns The following VBox status codes.
|
---|
276 | * @retval VINF_SUCCESS on success. FF cleared.
|
---|
277 | * @retval VINF_EM_NO_MEMORY if we're out of memory. The FF is set in this case.
|
---|
278 | *
|
---|
279 | * @param pGVM The global (ring-0) VM structure.
|
---|
280 | * @param idCpu The ID of the calling EMT.
|
---|
281 | *
|
---|
282 | * @thread EMT(idCpu)
|
---|
283 | *
|
---|
284 | * @remarks Must be called from within the PGM critical section. The caller
|
---|
285 | * must clear the new pages.
|
---|
286 | */
|
---|
287 | VMMR0_INT_DECL(int) PGMR0PhysAllocateHandyPages(PGVM pGVM, VMCPUID idCpu)
|
---|
288 | {
|
---|
289 | /*
|
---|
290 | * Validate inputs.
|
---|
291 | */
|
---|
292 | AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID); /* caller already checked this, but just to be sure. */
|
---|
293 | AssertReturn(pGVM->aCpus[idCpu].hEMT == RTThreadNativeSelf(), VERR_NOT_OWNER);
|
---|
294 |
|
---|
295 | /*
|
---|
296 | * Enter the PGM lock and call the worker.
|
---|
297 | */
|
---|
298 | int rc = PGM_LOCK(pGVM);
|
---|
299 | if (RT_SUCCESS(rc))
|
---|
300 | {
|
---|
301 | rc = pgmR0PhysAllocateHandyPages(pGVM, idCpu, true /*fRing3*/);
|
---|
302 | PGM_UNLOCK(pGVM);
|
---|
303 | }
|
---|
304 | return rc;
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 | /**
|
---|
309 | * Flushes any changes pending in the handy page array.
|
---|
310 | *
|
---|
311 | * It is very important that this gets done when page sharing is enabled.
|
---|
312 | *
|
---|
313 | * @returns The following VBox status codes.
|
---|
314 | * @retval VINF_SUCCESS on success. FF cleared.
|
---|
315 | *
|
---|
316 | * @param pGVM The global (ring-0) VM structure.
|
---|
317 | * @param idCpu The ID of the calling EMT.
|
---|
318 | *
|
---|
319 | * @thread EMT(idCpu)
|
---|
320 | *
|
---|
321 | * @remarks Must be called from within the PGM critical section.
|
---|
322 | */
|
---|
323 | VMMR0_INT_DECL(int) PGMR0PhysFlushHandyPages(PGVM pGVM, VMCPUID idCpu)
|
---|
324 | {
|
---|
325 | /*
|
---|
326 | * Validate inputs.
|
---|
327 | */
|
---|
328 | AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID); /* caller already checked this, but just to be sure. */
|
---|
329 | AssertReturn(pGVM->aCpus[idCpu].hEMT == RTThreadNativeSelf(), VERR_NOT_OWNER);
|
---|
330 | PGM_LOCK_ASSERT_OWNER_EX(pGVM, &pGVM->aCpus[idCpu]);
|
---|
331 |
|
---|
332 | /*
|
---|
333 | * Try allocate a full set of handy pages.
|
---|
334 | */
|
---|
335 | uint32_t iFirst = pGVM->pgm.s.cHandyPages;
|
---|
336 | AssertReturn(iFirst <= RT_ELEMENTS(pGVM->pgm.s.aHandyPages), VERR_PGM_HANDY_PAGE_IPE);
|
---|
337 | uint32_t cPages = RT_ELEMENTS(pGVM->pgm.s.aHandyPages) - iFirst;
|
---|
338 | if (!cPages)
|
---|
339 | return VINF_SUCCESS;
|
---|
340 | int rc = GMMR0AllocateHandyPages(pGVM, idCpu, cPages, 0, &pGVM->pgm.s.aHandyPages[iFirst]);
|
---|
341 |
|
---|
342 | LogFlow(("PGMR0PhysFlushHandyPages: cPages=%d rc=%Rrc\n", cPages, rc));
|
---|
343 | return rc;
|
---|
344 | }
|
---|
345 |
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Allocate a large page at @a GCPhys.
|
---|
349 | *
|
---|
350 | * @returns The following VBox status codes.
|
---|
351 | * @retval VINF_SUCCESS on success.
|
---|
352 | * @retval VINF_EM_NO_MEMORY if we're out of memory.
|
---|
353 | *
|
---|
354 | * @param pGVM The global (ring-0) VM structure.
|
---|
355 | * @param idCpu The ID of the calling EMT.
|
---|
356 | * @param GCPhys The guest physical address of the page.
|
---|
357 | *
|
---|
358 | * @thread EMT(idCpu)
|
---|
359 | *
|
---|
360 | * @remarks Must be called from within the PGM critical section. The caller
|
---|
361 | * must clear the new pages.
|
---|
362 | */
|
---|
363 | int pgmR0PhysAllocateLargePage(PGVM pGVM, VMCPUID idCpu, RTGCPHYS GCPhys)
|
---|
364 | {
|
---|
365 | STAM_PROFILE_START(&pGVM->pgm.s.Stats.StatLargePageAlloc2, a);
|
---|
366 | PGM_LOCK_ASSERT_OWNER_EX(pGVM, &pGVM->aCpus[idCpu]);
|
---|
367 |
|
---|
368 | /*
|
---|
369 | * Allocate a large page.
|
---|
370 | */
|
---|
371 | RTHCPHYS HCPhys = NIL_GMMPAGEDESC_PHYS;
|
---|
372 | uint32_t idPage = NIL_GMM_PAGEID;
|
---|
373 |
|
---|
374 | if (true) /** @todo pre-allocate 2-3 pages on the allocation thread. */
|
---|
375 | {
|
---|
376 | uint64_t const nsAllocStart = RTTimeNanoTS();
|
---|
377 | if (nsAllocStart < pGVM->pgm.s.nsLargePageRetry)
|
---|
378 | {
|
---|
379 | LogFlowFunc(("returns VERR_TRY_AGAIN - %RU64 ns left of hold off period\n", pGVM->pgm.s.nsLargePageRetry - nsAllocStart));
|
---|
380 | return VERR_TRY_AGAIN;
|
---|
381 | }
|
---|
382 |
|
---|
383 | int const rc = GMMR0AllocateLargePage(pGVM, idCpu, _2M, &idPage, &HCPhys);
|
---|
384 |
|
---|
385 | uint64_t const nsAllocEnd = RTTimeNanoTS();
|
---|
386 | uint64_t const cNsElapsed = nsAllocEnd - nsAllocStart;
|
---|
387 | STAM_REL_PROFILE_ADD_PERIOD(&pGVM->pgm.s.StatLargePageAlloc, cNsElapsed);
|
---|
388 | if (cNsElapsed < RT_NS_100MS)
|
---|
389 | pGVM->pgm.s.cLargePageLongAllocRepeats = 0;
|
---|
390 | else
|
---|
391 | {
|
---|
392 | /* If a large page allocation takes more than 100ms back off for a
|
---|
393 | while so the host OS can reshuffle memory and make some more large
|
---|
394 | pages available. However if it took over a second, just disable it. */
|
---|
395 | STAM_REL_COUNTER_INC(&pGVM->pgm.s.StatLargePageOverflow);
|
---|
396 | pGVM->pgm.s.cLargePageLongAllocRepeats++;
|
---|
397 | if (cNsElapsed > RT_NS_1SEC)
|
---|
398 | {
|
---|
399 | LogRel(("PGMR0PhysAllocateLargePage: Disabling large pages after %'RU64 ns allocation time.\n", cNsElapsed));
|
---|
400 | PGMSetLargePageUsage(pGVM, false);
|
---|
401 | }
|
---|
402 | else
|
---|
403 | {
|
---|
404 | Log(("PGMR0PhysAllocateLargePage: Suspending large page allocations for %u sec after %'RU64 ns allocation time.\n",
|
---|
405 | 30 * pGVM->pgm.s.cLargePageLongAllocRepeats, cNsElapsed));
|
---|
406 | pGVM->pgm.s.nsLargePageRetry = nsAllocEnd + RT_NS_30SEC * pGVM->pgm.s.cLargePageLongAllocRepeats;
|
---|
407 | }
|
---|
408 | }
|
---|
409 |
|
---|
410 | if (RT_FAILURE(rc))
|
---|
411 | {
|
---|
412 | Log(("PGMR0PhysAllocateLargePage: Failed: %Rrc\n", rc));
|
---|
413 | STAM_REL_COUNTER_INC(&pGVM->pgm.s.StatLargePageAllocFailed);
|
---|
414 | if (rc == VERR_NOT_SUPPORTED)
|
---|
415 | {
|
---|
416 | LogRel(("PGM: Disabling large pages because of VERR_NOT_SUPPORTED status.\n"));
|
---|
417 | PGMSetLargePageUsage(pGVM, false);
|
---|
418 | }
|
---|
419 | return rc;
|
---|
420 | }
|
---|
421 | }
|
---|
422 |
|
---|
423 | STAM_PROFILE_STOP_START(&pGVM->pgm.s.Stats.StatLargePageAlloc2, &pGVM->pgm.s.Stats.StatLargePageSetup, a);
|
---|
424 |
|
---|
425 | /*
|
---|
426 | * Enter the pages into PGM.
|
---|
427 | */
|
---|
428 | bool fFlushTLBs = false;
|
---|
429 | VBOXSTRICTRC rc = VINF_SUCCESS;
|
---|
430 | unsigned cLeft = _2M / PAGE_SIZE;
|
---|
431 | while (cLeft-- > 0)
|
---|
432 | {
|
---|
433 | PPGMPAGE const pPage = pgmPhysGetPage(pGVM, GCPhys);
|
---|
434 | AssertReturn(pPage && PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM && PGM_PAGE_IS_ZERO(pPage), VERR_PGM_UNEXPECTED_PAGE_STATE);
|
---|
435 |
|
---|
436 | /* Make sure there are no zero mappings. */
|
---|
437 | uint16_t const u16Tracking = PGM_PAGE_GET_TRACKING(pPage);
|
---|
438 | if (u16Tracking == 0)
|
---|
439 | Assert(PGM_PAGE_GET_PTE_INDEX(pPage) == 0);
|
---|
440 | else
|
---|
441 | {
|
---|
442 | STAM_REL_COUNTER_INC(&pGVM->pgm.s.StatLargePageZeroEvict);
|
---|
443 | VBOXSTRICTRC rc3 = pgmPoolTrackUpdateGCPhys(pGVM, GCPhys, pPage, true /*fFlushPTEs*/, &fFlushTLBs);
|
---|
444 | Log(("PGMR0PhysAllocateLargePage: GCPhys=%RGp: tracking=%#x rc3=%Rrc\n", GCPhys, u16Tracking, VBOXSTRICTRC_VAL(rc3)));
|
---|
445 | if (rc3 != VINF_SUCCESS && rc == VINF_SUCCESS)
|
---|
446 | rc = rc3; /** @todo not perfect... */
|
---|
447 | PGM_PAGE_SET_PTE_INDEX(pGVM, pPage, 0);
|
---|
448 | PGM_PAGE_SET_TRACKING(pGVM, pPage, 0);
|
---|
449 | }
|
---|
450 |
|
---|
451 | /* Setup the new page. */
|
---|
452 | PGM_PAGE_SET_HCPHYS(pGVM, pPage, HCPhys);
|
---|
453 | PGM_PAGE_SET_STATE(pGVM, pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
454 | PGM_PAGE_SET_PDE_TYPE(pGVM, pPage, PGM_PAGE_PDE_TYPE_PDE);
|
---|
455 | PGM_PAGE_SET_PAGEID(pGVM, pPage, idPage);
|
---|
456 | Log3(("PGMR0PhysAllocateLargePage: GCPhys=%RGp: idPage=%#x HCPhys=%RGp (old tracking=%#x)\n",
|
---|
457 | GCPhys, idPage, HCPhys, u16Tracking));
|
---|
458 |
|
---|
459 | /* advance */
|
---|
460 | idPage++;
|
---|
461 | HCPhys += PAGE_SIZE;
|
---|
462 | GCPhys += PAGE_SIZE;
|
---|
463 | }
|
---|
464 |
|
---|
465 | STAM_COUNTER_ADD(&pGVM->pgm.s.Stats.StatRZPageReplaceZero, _2M / PAGE_SIZE);
|
---|
466 | pGVM->pgm.s.cZeroPages -= _2M / PAGE_SIZE;
|
---|
467 | pGVM->pgm.s.cPrivatePages += _2M / PAGE_SIZE;
|
---|
468 |
|
---|
469 | /*
|
---|
470 | * Flush all TLBs.
|
---|
471 | */
|
---|
472 | if (!fFlushTLBs)
|
---|
473 | { /* likely as we shouldn't normally map zero pages */ }
|
---|
474 | else
|
---|
475 | {
|
---|
476 | STAM_REL_COUNTER_INC(&pGVM->pgm.s.StatLargePageTlbFlush);
|
---|
477 | PGM_INVL_ALL_VCPU_TLBS(pGVM);
|
---|
478 | }
|
---|
479 | /** @todo this is a little expensive (~3000 ticks) since we'll have to
|
---|
480 | * invalidate everything. Add a version to the TLB? */
|
---|
481 | pgmPhysInvalidatePageMapTLB(pGVM);
|
---|
482 |
|
---|
483 | STAM_PROFILE_STOP(&pGVM->pgm.s.Stats.StatLargePageSetup, a);
|
---|
484 | #if 0 /** @todo returning info statuses here might not be a great idea... */
|
---|
485 | LogFlow(("PGMR0PhysAllocateLargePage: returns %Rrc\n", VBOXSTRICTRC_VAL(rc) ));
|
---|
486 | return VBOXSTRICTRC_TODO(rc);
|
---|
487 | #else
|
---|
488 | LogFlow(("PGMR0PhysAllocateLargePage: returns VINF_SUCCESS (rc=%Rrc)\n", VBOXSTRICTRC_VAL(rc) ));
|
---|
489 | return VINF_SUCCESS;
|
---|
490 | #endif
|
---|
491 | }
|
---|
492 |
|
---|
493 |
|
---|
494 | /**
|
---|
495 | * Allocate a large page at @a GCPhys.
|
---|
496 | *
|
---|
497 | * @returns The following VBox status codes.
|
---|
498 | * @retval VINF_SUCCESS on success.
|
---|
499 | * @retval VINF_EM_NO_MEMORY if we're out of memory.
|
---|
500 | *
|
---|
501 | * @param pGVM The global (ring-0) VM structure.
|
---|
502 | * @param idCpu The ID of the calling EMT.
|
---|
503 | * @param GCPhys The guest physical address of the page.
|
---|
504 | *
|
---|
505 | * @thread EMT(idCpu)
|
---|
506 | *
|
---|
507 | * @remarks Must be called from within the PGM critical section. The caller
|
---|
508 | * must clear the new pages.
|
---|
509 | */
|
---|
510 | VMMR0_INT_DECL(int) PGMR0PhysAllocateLargePage(PGVM pGVM, VMCPUID idCpu, RTGCPHYS GCPhys)
|
---|
511 | {
|
---|
512 | /*
|
---|
513 | * Validate inputs.
|
---|
514 | */
|
---|
515 | AssertReturn(idCpu < pGVM->cCpus, VERR_INVALID_CPU_ID);
|
---|
516 | AssertReturn(pGVM->aCpus[idCpu].hEMT == RTThreadNativeSelf(), VERR_NOT_OWNER);
|
---|
517 |
|
---|
518 | int rc = PGM_LOCK(pGVM);
|
---|
519 | AssertRCReturn(rc, rc);
|
---|
520 |
|
---|
521 | /* The caller might have done this already, but since we're ring-3 callable we
|
---|
522 | need to make sure everything is fine before starting the allocation here. */
|
---|
523 | for (unsigned i = 0; i < _2M / PAGE_SIZE; i++)
|
---|
524 | {
|
---|
525 | PPGMPAGE pPage;
|
---|
526 | rc = pgmPhysGetPageEx(pGVM, GCPhys + i * PAGE_SIZE, &pPage);
|
---|
527 | AssertRCReturnStmt(rc, PGM_UNLOCK(pGVM), rc);
|
---|
528 | AssertReturnStmt(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM, PGM_UNLOCK(pGVM), VERR_PGM_PHYS_NOT_RAM);
|
---|
529 | AssertReturnStmt(PGM_PAGE_IS_ZERO(pPage), PGM_UNLOCK(pGVM), VERR_PGM_UNEXPECTED_PAGE_STATE);
|
---|
530 | }
|
---|
531 |
|
---|
532 | /*
|
---|
533 | * Call common code.
|
---|
534 | */
|
---|
535 | rc = pgmR0PhysAllocateLargePage(pGVM, idCpu, GCPhys);
|
---|
536 |
|
---|
537 | PGM_UNLOCK(pGVM);
|
---|
538 | return rc;
|
---|
539 | }
|
---|
540 |
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * Locate a MMIO2 range.
|
---|
544 | *
|
---|
545 | * @returns Pointer to the MMIO2 range.
|
---|
546 | * @param pGVM The global (ring-0) VM structure.
|
---|
547 | * @param pDevIns The device instance owning the region.
|
---|
548 | * @param hMmio2 Handle to look up.
|
---|
549 | */
|
---|
550 | DECLINLINE(PPGMREGMMIO2RANGE) pgmR0PhysMmio2Find(PGVM pGVM, PPDMDEVINS pDevIns, PGMMMIO2HANDLE hMmio2)
|
---|
551 | {
|
---|
552 | /*
|
---|
553 | * We use the lookup table here as list walking is tedious in ring-0 when using
|
---|
554 | * ring-3 pointers and this probably will require some kind of refactoring anyway.
|
---|
555 | */
|
---|
556 | if (hMmio2 <= RT_ELEMENTS(pGVM->pgm.s.apMmio2RangesR0) && hMmio2 != 0)
|
---|
557 | {
|
---|
558 | PPGMREGMMIO2RANGE pCur = pGVM->pgm.s.apMmio2RangesR0[hMmio2 - 1];
|
---|
559 | if (pCur && pCur->pDevInsR3 == pDevIns->pDevInsForR3)
|
---|
560 | {
|
---|
561 | Assert(pCur->idMmio2 == hMmio2);
|
---|
562 | return pCur;
|
---|
563 | }
|
---|
564 | Assert(!pCur);
|
---|
565 | }
|
---|
566 | return NULL;
|
---|
567 | }
|
---|
568 |
|
---|
569 |
|
---|
570 | /**
|
---|
571 | * Worker for PDMDEVHLPR0::pfnMmio2SetUpContext.
|
---|
572 | *
|
---|
573 | * @returns VBox status code.
|
---|
574 | * @param pGVM The global (ring-0) VM structure.
|
---|
575 | * @param pDevIns The device instance.
|
---|
576 | * @param hMmio2 The MMIO2 region to map into ring-0 address space.
|
---|
577 | * @param offSub The offset into the region.
|
---|
578 | * @param cbSub The size of the mapping, zero meaning all the rest.
|
---|
579 | * @param ppvMapping Where to return the ring-0 mapping address.
|
---|
580 | */
|
---|
581 | VMMR0_INT_DECL(int) PGMR0PhysMMIO2MapKernel(PGVM pGVM, PPDMDEVINS pDevIns, PGMMMIO2HANDLE hMmio2,
|
---|
582 | size_t offSub, size_t cbSub, void **ppvMapping)
|
---|
583 | {
|
---|
584 | AssertReturn(!(offSub & PAGE_OFFSET_MASK), VERR_UNSUPPORTED_ALIGNMENT);
|
---|
585 | AssertReturn(!(cbSub & PAGE_OFFSET_MASK), VERR_UNSUPPORTED_ALIGNMENT);
|
---|
586 |
|
---|
587 | /*
|
---|
588 | * Translate hRegion into a range pointer.
|
---|
589 | */
|
---|
590 | PPGMREGMMIO2RANGE pFirstRegMmio = pgmR0PhysMmio2Find(pGVM, pDevIns, hMmio2);
|
---|
591 | AssertReturn(pFirstRegMmio, VERR_NOT_FOUND);
|
---|
592 | #ifndef VBOX_WITH_LINEAR_HOST_PHYS_MEM
|
---|
593 | uint8_t * const pvR0 = (uint8_t *)pFirstRegMmio->pvR0;
|
---|
594 | #else
|
---|
595 | RTR3PTR const pvR3 = pFirstRegMmio->pvR3;
|
---|
596 | #endif
|
---|
597 | RTGCPHYS const cbReal = pFirstRegMmio->cbReal;
|
---|
598 | pFirstRegMmio = NULL;
|
---|
599 | ASMCompilerBarrier();
|
---|
600 |
|
---|
601 | AssertReturn(offSub < cbReal, VERR_OUT_OF_RANGE);
|
---|
602 | if (cbSub == 0)
|
---|
603 | cbSub = cbReal - offSub;
|
---|
604 | else
|
---|
605 | AssertReturn(cbSub < cbReal && cbSub + offSub <= cbReal, VERR_OUT_OF_RANGE);
|
---|
606 |
|
---|
607 | /*
|
---|
608 | * Do the mapping.
|
---|
609 | */
|
---|
610 | #ifndef VBOX_WITH_LINEAR_HOST_PHYS_MEM
|
---|
611 | AssertPtr(pvR0);
|
---|
612 | *ppvMapping = pvR0 + offSub;
|
---|
613 | return VINF_SUCCESS;
|
---|
614 | #else
|
---|
615 | return SUPR0PageMapKernel(pGVM->pSession, pvR3, (uint32_t)offSub, (uint32_t)cbSub, 0 /*fFlags*/, ppvMapping);
|
---|
616 | #endif
|
---|
617 | }
|
---|
618 |
|
---|
619 |
|
---|
620 | #ifdef VBOX_WITH_PCI_PASSTHROUGH
|
---|
621 | /* Interface sketch. The interface belongs to a global PCI pass-through
|
---|
622 | manager. It shall use the global VM handle, not the user VM handle to
|
---|
623 | store the per-VM info (domain) since that is all ring-0 stuff, thus
|
---|
624 | passing pGVM here. I've tentitively prefixed the functions 'GPciRawR0',
|
---|
625 | we can discuss the PciRaw code re-organtization when I'm back from
|
---|
626 | vacation.
|
---|
627 |
|
---|
628 | I've implemented the initial IOMMU set up below. For things to work
|
---|
629 | reliably, we will probably need add a whole bunch of checks and
|
---|
630 | GPciRawR0GuestPageUpdate call to the PGM code. For the present,
|
---|
631 | assuming nested paging (enforced) and prealloc (enforced), no
|
---|
632 | ballooning (check missing), page sharing (check missing) or live
|
---|
633 | migration (check missing), it might work fine. At least if some
|
---|
634 | VM power-off hook is present and can tear down the IOMMU page tables. */
|
---|
635 |
|
---|
636 | /**
|
---|
637 | * Tells the global PCI pass-through manager that we are about to set up the
|
---|
638 | * guest page to host page mappings for the specfied VM.
|
---|
639 | *
|
---|
640 | * @returns VBox status code.
|
---|
641 | *
|
---|
642 | * @param pGVM The ring-0 VM structure.
|
---|
643 | */
|
---|
644 | VMMR0_INT_DECL(int) GPciRawR0GuestPageBeginAssignments(PGVM pGVM)
|
---|
645 | {
|
---|
646 | NOREF(pGVM);
|
---|
647 | return VINF_SUCCESS;
|
---|
648 | }
|
---|
649 |
|
---|
650 |
|
---|
651 | /**
|
---|
652 | * Assigns a host page mapping for a guest page.
|
---|
653 | *
|
---|
654 | * This is only used when setting up the mappings, i.e. between
|
---|
655 | * GPciRawR0GuestPageBeginAssignments and GPciRawR0GuestPageEndAssignments.
|
---|
656 | *
|
---|
657 | * @returns VBox status code.
|
---|
658 | * @param pGVM The ring-0 VM structure.
|
---|
659 | * @param GCPhys The address of the guest page (page aligned).
|
---|
660 | * @param HCPhys The address of the host page (page aligned).
|
---|
661 | */
|
---|
662 | VMMR0_INT_DECL(int) GPciRawR0GuestPageAssign(PGVM pGVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys)
|
---|
663 | {
|
---|
664 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INTERNAL_ERROR_3);
|
---|
665 | AssertReturn(!(HCPhys & PAGE_OFFSET_MASK), VERR_INTERNAL_ERROR_3);
|
---|
666 |
|
---|
667 | if (pGVM->rawpci.s.pfnContigMemInfo)
|
---|
668 | /** @todo what do we do on failure? */
|
---|
669 | pGVM->rawpci.s.pfnContigMemInfo(&pGVM->rawpci.s, HCPhys, GCPhys, PAGE_SIZE, PCIRAW_MEMINFO_MAP);
|
---|
670 |
|
---|
671 | return VINF_SUCCESS;
|
---|
672 | }
|
---|
673 |
|
---|
674 |
|
---|
675 | /**
|
---|
676 | * Indicates that the specified guest page doesn't exists but doesn't have host
|
---|
677 | * page mapping we trust PCI pass-through with.
|
---|
678 | *
|
---|
679 | * This is only used when setting up the mappings, i.e. between
|
---|
680 | * GPciRawR0GuestPageBeginAssignments and GPciRawR0GuestPageEndAssignments.
|
---|
681 | *
|
---|
682 | * @returns VBox status code.
|
---|
683 | * @param pGVM The ring-0 VM structure.
|
---|
684 | * @param GCPhys The address of the guest page (page aligned).
|
---|
685 | * @param HCPhys The address of the host page (page aligned).
|
---|
686 | */
|
---|
687 | VMMR0_INT_DECL(int) GPciRawR0GuestPageUnassign(PGVM pGVM, RTGCPHYS GCPhys)
|
---|
688 | {
|
---|
689 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INTERNAL_ERROR_3);
|
---|
690 |
|
---|
691 | if (pGVM->rawpci.s.pfnContigMemInfo)
|
---|
692 | /** @todo what do we do on failure? */
|
---|
693 | pGVM->rawpci.s.pfnContigMemInfo(&pGVM->rawpci.s, 0, GCPhys, PAGE_SIZE, PCIRAW_MEMINFO_UNMAP);
|
---|
694 |
|
---|
695 | return VINF_SUCCESS;
|
---|
696 | }
|
---|
697 |
|
---|
698 |
|
---|
699 | /**
|
---|
700 | * Tells the global PCI pass-through manager that we have completed setting up
|
---|
701 | * the guest page to host page mappings for the specfied VM.
|
---|
702 | *
|
---|
703 | * This complements GPciRawR0GuestPageBeginAssignments and will be called even
|
---|
704 | * if some page assignment failed.
|
---|
705 | *
|
---|
706 | * @returns VBox status code.
|
---|
707 | *
|
---|
708 | * @param pGVM The ring-0 VM structure.
|
---|
709 | */
|
---|
710 | VMMR0_INT_DECL(int) GPciRawR0GuestPageEndAssignments(PGVM pGVM)
|
---|
711 | {
|
---|
712 | NOREF(pGVM);
|
---|
713 | return VINF_SUCCESS;
|
---|
714 | }
|
---|
715 |
|
---|
716 |
|
---|
717 | /**
|
---|
718 | * Tells the global PCI pass-through manager that a guest page mapping has
|
---|
719 | * changed after the initial setup.
|
---|
720 | *
|
---|
721 | * @returns VBox status code.
|
---|
722 | * @param pGVM The ring-0 VM structure.
|
---|
723 | * @param GCPhys The address of the guest page (page aligned).
|
---|
724 | * @param HCPhys The new host page address or NIL_RTHCPHYS if
|
---|
725 | * now unassigned.
|
---|
726 | */
|
---|
727 | VMMR0_INT_DECL(int) GPciRawR0GuestPageUpdate(PGVM pGVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys)
|
---|
728 | {
|
---|
729 | AssertReturn(!(GCPhys & PAGE_OFFSET_MASK), VERR_INTERNAL_ERROR_4);
|
---|
730 | AssertReturn(!(HCPhys & PAGE_OFFSET_MASK) || HCPhys == NIL_RTHCPHYS, VERR_INTERNAL_ERROR_4);
|
---|
731 | NOREF(pGVM);
|
---|
732 | return VINF_SUCCESS;
|
---|
733 | }
|
---|
734 |
|
---|
735 | #endif /* VBOX_WITH_PCI_PASSTHROUGH */
|
---|
736 |
|
---|
737 |
|
---|
738 | /**
|
---|
739 | * Sets up the IOMMU when raw PCI device is enabled.
|
---|
740 | *
|
---|
741 | * @note This is a hack that will probably be remodelled and refined later!
|
---|
742 | *
|
---|
743 | * @returns VBox status code.
|
---|
744 | *
|
---|
745 | * @param pGVM The global (ring-0) VM structure.
|
---|
746 | */
|
---|
747 | VMMR0_INT_DECL(int) PGMR0PhysSetupIoMmu(PGVM pGVM)
|
---|
748 | {
|
---|
749 | int rc = GVMMR0ValidateGVM(pGVM);
|
---|
750 | if (RT_FAILURE(rc))
|
---|
751 | return rc;
|
---|
752 |
|
---|
753 | #ifdef VBOX_WITH_PCI_PASSTHROUGH
|
---|
754 | if (pGVM->pgm.s.fPciPassthrough)
|
---|
755 | {
|
---|
756 | /*
|
---|
757 | * The Simplistic Approach - Enumerate all the pages and call tell the
|
---|
758 | * IOMMU about each of them.
|
---|
759 | */
|
---|
760 | PGM_LOCK_VOID(pGVM);
|
---|
761 | rc = GPciRawR0GuestPageBeginAssignments(pGVM);
|
---|
762 | if (RT_SUCCESS(rc))
|
---|
763 | {
|
---|
764 | for (PPGMRAMRANGE pRam = pGVM->pgm.s.pRamRangesXR0; RT_SUCCESS(rc) && pRam; pRam = pRam->pNextR0)
|
---|
765 | {
|
---|
766 | PPGMPAGE pPage = &pRam->aPages[0];
|
---|
767 | RTGCPHYS GCPhys = pRam->GCPhys;
|
---|
768 | uint32_t cLeft = pRam->cb >> PAGE_SHIFT;
|
---|
769 | while (cLeft-- > 0)
|
---|
770 | {
|
---|
771 | /* Only expose pages that are 100% safe for now. */
|
---|
772 | if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_RAM
|
---|
773 | && PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED
|
---|
774 | && !PGM_PAGE_HAS_ANY_HANDLERS(pPage))
|
---|
775 | rc = GPciRawR0GuestPageAssign(pGVM, GCPhys, PGM_PAGE_GET_HCPHYS(pPage));
|
---|
776 | else
|
---|
777 | rc = GPciRawR0GuestPageUnassign(pGVM, GCPhys);
|
---|
778 |
|
---|
779 | /* next */
|
---|
780 | pPage++;
|
---|
781 | GCPhys += PAGE_SIZE;
|
---|
782 | }
|
---|
783 | }
|
---|
784 |
|
---|
785 | int rc2 = GPciRawR0GuestPageEndAssignments(pGVM);
|
---|
786 | if (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
787 | rc = rc2;
|
---|
788 | }
|
---|
789 | PGM_UNLOCK(pGVM);
|
---|
790 | }
|
---|
791 | else
|
---|
792 | #endif
|
---|
793 | rc = VERR_NOT_SUPPORTED;
|
---|
794 | return rc;
|
---|
795 | }
|
---|
796 |
|
---|
797 |
|
---|
798 | /**
|
---|
799 | * \#PF Handler for nested paging.
|
---|
800 | *
|
---|
801 | * @returns VBox status code (appropriate for trap handling and GC return).
|
---|
802 | * @param pGVM The global (ring-0) VM structure.
|
---|
803 | * @param pGVCpu The global (ring-0) CPU structure of the calling
|
---|
804 | * EMT.
|
---|
805 | * @param enmShwPagingMode Paging mode for the nested page tables.
|
---|
806 | * @param uErr The trap error code.
|
---|
807 | * @param pRegFrame Trap register frame.
|
---|
808 | * @param GCPhysFault The fault address.
|
---|
809 | */
|
---|
810 | VMMR0DECL(int) PGMR0Trap0eHandlerNestedPaging(PGVM pGVM, PGVMCPU pGVCpu, PGMMODE enmShwPagingMode, RTGCUINT uErr,
|
---|
811 | PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault)
|
---|
812 | {
|
---|
813 | int rc;
|
---|
814 |
|
---|
815 | LogFlow(("PGMTrap0eHandler: uErr=%RGx GCPhysFault=%RGp eip=%RGv\n", uErr, GCPhysFault, (RTGCPTR)pRegFrame->rip));
|
---|
816 | STAM_PROFILE_START(&pGVCpu->pgm.s.StatRZTrap0e, a);
|
---|
817 | STAM_STATS({ pGVCpu->pgmr0.s.pStatTrap0eAttributionR0 = NULL; } );
|
---|
818 |
|
---|
819 | /* AMD uses the host's paging mode; Intel has a single mode (EPT). */
|
---|
820 | AssertMsg( enmShwPagingMode == PGMMODE_32_BIT || enmShwPagingMode == PGMMODE_PAE || enmShwPagingMode == PGMMODE_PAE_NX
|
---|
821 | || enmShwPagingMode == PGMMODE_AMD64 || enmShwPagingMode == PGMMODE_AMD64_NX || enmShwPagingMode == PGMMODE_EPT,
|
---|
822 | ("enmShwPagingMode=%d\n", enmShwPagingMode));
|
---|
823 |
|
---|
824 | /* Reserved shouldn't end up here. */
|
---|
825 | Assert(!(uErr & X86_TRAP_PF_RSVD));
|
---|
826 |
|
---|
827 | #ifdef VBOX_WITH_STATISTICS
|
---|
828 | /*
|
---|
829 | * Error code stats.
|
---|
830 | */
|
---|
831 | if (uErr & X86_TRAP_PF_US)
|
---|
832 | {
|
---|
833 | if (!(uErr & X86_TRAP_PF_P))
|
---|
834 | {
|
---|
835 | if (uErr & X86_TRAP_PF_RW)
|
---|
836 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatRZTrap0eUSNotPresentWrite);
|
---|
837 | else
|
---|
838 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatRZTrap0eUSNotPresentRead);
|
---|
839 | }
|
---|
840 | else if (uErr & X86_TRAP_PF_RW)
|
---|
841 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatRZTrap0eUSWrite);
|
---|
842 | else if (uErr & X86_TRAP_PF_RSVD)
|
---|
843 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatRZTrap0eUSReserved);
|
---|
844 | else if (uErr & X86_TRAP_PF_ID)
|
---|
845 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatRZTrap0eUSNXE);
|
---|
846 | else
|
---|
847 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatRZTrap0eUSRead);
|
---|
848 | }
|
---|
849 | else
|
---|
850 | { /* Supervisor */
|
---|
851 | if (!(uErr & X86_TRAP_PF_P))
|
---|
852 | {
|
---|
853 | if (uErr & X86_TRAP_PF_RW)
|
---|
854 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatRZTrap0eSVNotPresentWrite);
|
---|
855 | else
|
---|
856 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatRZTrap0eSVNotPresentRead);
|
---|
857 | }
|
---|
858 | else if (uErr & X86_TRAP_PF_RW)
|
---|
859 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatRZTrap0eSVWrite);
|
---|
860 | else if (uErr & X86_TRAP_PF_ID)
|
---|
861 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatRZTrap0eSNXE);
|
---|
862 | else if (uErr & X86_TRAP_PF_RSVD)
|
---|
863 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatRZTrap0eSVReserved);
|
---|
864 | }
|
---|
865 | #endif
|
---|
866 |
|
---|
867 | /*
|
---|
868 | * Call the worker.
|
---|
869 | *
|
---|
870 | * Note! We pretend the guest is in protected mode without paging, so we
|
---|
871 | * can use existing code to build the nested page tables.
|
---|
872 | */
|
---|
873 | /** @todo r=bird: Gotta love this nested paging hacking we're still carrying with us... (Split PGM_TYPE_NESTED.) */
|
---|
874 | bool fLockTaken = false;
|
---|
875 | switch (enmShwPagingMode)
|
---|
876 | {
|
---|
877 | case PGMMODE_32_BIT:
|
---|
878 | rc = PGM_BTH_NAME_32BIT_PROT(Trap0eHandler)(pGVCpu, uErr, pRegFrame, GCPhysFault, &fLockTaken);
|
---|
879 | break;
|
---|
880 | case PGMMODE_PAE:
|
---|
881 | case PGMMODE_PAE_NX:
|
---|
882 | rc = PGM_BTH_NAME_PAE_PROT(Trap0eHandler)(pGVCpu, uErr, pRegFrame, GCPhysFault, &fLockTaken);
|
---|
883 | break;
|
---|
884 | case PGMMODE_AMD64:
|
---|
885 | case PGMMODE_AMD64_NX:
|
---|
886 | rc = PGM_BTH_NAME_AMD64_PROT(Trap0eHandler)(pGVCpu, uErr, pRegFrame, GCPhysFault, &fLockTaken);
|
---|
887 | break;
|
---|
888 | case PGMMODE_EPT:
|
---|
889 | rc = PGM_BTH_NAME_EPT_PROT(Trap0eHandler)(pGVCpu, uErr, pRegFrame, GCPhysFault, &fLockTaken);
|
---|
890 | break;
|
---|
891 | default:
|
---|
892 | AssertFailed();
|
---|
893 | rc = VERR_INVALID_PARAMETER;
|
---|
894 | break;
|
---|
895 | }
|
---|
896 | if (fLockTaken)
|
---|
897 | {
|
---|
898 | PGM_LOCK_ASSERT_OWNER(pGVM);
|
---|
899 | PGM_UNLOCK(pGVM);
|
---|
900 | }
|
---|
901 |
|
---|
902 | if (rc == VINF_PGM_SYNCPAGE_MODIFIED_PDE)
|
---|
903 | rc = VINF_SUCCESS;
|
---|
904 | /*
|
---|
905 | * Handle the case where we cannot interpret the instruction because we cannot get the guest physical address
|
---|
906 | * via its page tables, see @bugref{6043}.
|
---|
907 | */
|
---|
908 | else if ( rc == VERR_PAGE_NOT_PRESENT /* SMP only ; disassembly might fail. */
|
---|
909 | || rc == VERR_PAGE_TABLE_NOT_PRESENT /* seen with UNI & SMP */
|
---|
910 | || rc == VERR_PAGE_DIRECTORY_PTR_NOT_PRESENT /* seen with SMP */
|
---|
911 | || rc == VERR_PAGE_MAP_LEVEL4_NOT_PRESENT) /* precaution */
|
---|
912 | {
|
---|
913 | Log(("WARNING: Unexpected VERR_PAGE_TABLE_NOT_PRESENT (%d) for page fault at %RGp error code %x (rip=%RGv)\n", rc, GCPhysFault, uErr, pRegFrame->rip));
|
---|
914 | /* Some kind of inconsistency in the SMP case; it's safe to just execute the instruction again; not sure about
|
---|
915 | single VCPU VMs though. */
|
---|
916 | rc = VINF_SUCCESS;
|
---|
917 | }
|
---|
918 |
|
---|
919 | STAM_STATS({ if (!pGVCpu->pgmr0.s.pStatTrap0eAttributionR0)
|
---|
920 | pGVCpu->pgmr0.s.pStatTrap0eAttributionR0 = &pGVCpu->pgm.s.Stats.StatRZTrap0eTime2Misc; });
|
---|
921 | STAM_PROFILE_STOP_EX(&pGVCpu->pgm.s.Stats.StatRZTrap0e, pGVCpu->pgmr0.s.pStatTrap0eAttributionR0, a);
|
---|
922 | return rc;
|
---|
923 | }
|
---|
924 |
|
---|
925 |
|
---|
926 | /**
|
---|
927 | * \#PF Handler for deliberate nested paging misconfiguration (/reserved bit)
|
---|
928 | * employed for MMIO pages.
|
---|
929 | *
|
---|
930 | * @returns VBox status code (appropriate for trap handling and GC return).
|
---|
931 | * @param pGVM The global (ring-0) VM structure.
|
---|
932 | * @param pGVCpu The global (ring-0) CPU structure of the calling
|
---|
933 | * EMT.
|
---|
934 | * @param enmShwPagingMode Paging mode for the nested page tables.
|
---|
935 | * @param pRegFrame Trap register frame.
|
---|
936 | * @param GCPhysFault The fault address.
|
---|
937 | * @param uErr The error code, UINT32_MAX if not available
|
---|
938 | * (VT-x).
|
---|
939 | */
|
---|
940 | VMMR0DECL(VBOXSTRICTRC) PGMR0Trap0eHandlerNPMisconfig(PGVM pGVM, PGVMCPU pGVCpu, PGMMODE enmShwPagingMode,
|
---|
941 | PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, uint32_t uErr)
|
---|
942 | {
|
---|
943 | #ifdef PGM_WITH_MMIO_OPTIMIZATIONS
|
---|
944 | STAM_PROFILE_START(&pGVCpu->CTX_SUFF(pStats)->StatR0NpMiscfg, a);
|
---|
945 | VBOXSTRICTRC rc;
|
---|
946 |
|
---|
947 | /*
|
---|
948 | * Try lookup the all access physical handler for the address.
|
---|
949 | */
|
---|
950 | PGM_LOCK_VOID(pGVM);
|
---|
951 | PPGMPHYSHANDLER pHandler = pgmHandlerPhysicalLookup(pGVM, GCPhysFault);
|
---|
952 | PPGMPHYSHANDLERTYPEINT pHandlerType = RT_LIKELY(pHandler) ? PGMPHYSHANDLER_GET_TYPE(pGVM, pHandler) : NULL;
|
---|
953 | if (RT_LIKELY(pHandler && pHandlerType->enmKind != PGMPHYSHANDLERKIND_WRITE))
|
---|
954 | {
|
---|
955 | /*
|
---|
956 | * If the handle has aliases page or pages that have been temporarily
|
---|
957 | * disabled, we'll have to take a detour to make sure we resync them
|
---|
958 | * to avoid lots of unnecessary exits.
|
---|
959 | */
|
---|
960 | PPGMPAGE pPage;
|
---|
961 | if ( ( pHandler->cAliasedPages
|
---|
962 | || pHandler->cTmpOffPages)
|
---|
963 | && ( (pPage = pgmPhysGetPage(pGVM, GCPhysFault)) == NULL
|
---|
964 | || PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) == PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
|
---|
965 | )
|
---|
966 | {
|
---|
967 | Log(("PGMR0Trap0eHandlerNPMisconfig: Resyncing aliases / tmp-off page at %RGp (uErr=%#x) %R[pgmpage]\n", GCPhysFault, uErr, pPage));
|
---|
968 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatR0NpMiscfgSyncPage);
|
---|
969 | rc = pgmShwSyncNestedPageLocked(pGVCpu, GCPhysFault, 1 /*cPages*/, enmShwPagingMode);
|
---|
970 | PGM_UNLOCK(pGVM);
|
---|
971 | }
|
---|
972 | else
|
---|
973 | {
|
---|
974 | if (pHandlerType->CTX_SUFF(pfnPfHandler))
|
---|
975 | {
|
---|
976 | void *pvUser = pHandler->CTX_SUFF(pvUser);
|
---|
977 | STAM_PROFILE_START(&pHandler->Stat, h);
|
---|
978 | PGM_UNLOCK(pGVM);
|
---|
979 |
|
---|
980 | Log6(("PGMR0Trap0eHandlerNPMisconfig: calling %p(,%#x,,%RGp,%p)\n", pHandlerType->CTX_SUFF(pfnPfHandler), uErr, GCPhysFault, pvUser));
|
---|
981 | rc = pHandlerType->CTX_SUFF(pfnPfHandler)(pGVM, pGVCpu, uErr == UINT32_MAX ? RTGCPTR_MAX : uErr, pRegFrame,
|
---|
982 | GCPhysFault, GCPhysFault, pvUser);
|
---|
983 |
|
---|
984 | #ifdef VBOX_WITH_STATISTICS
|
---|
985 | PGM_LOCK_VOID(pGVM);
|
---|
986 | pHandler = pgmHandlerPhysicalLookup(pGVM, GCPhysFault);
|
---|
987 | if (pHandler)
|
---|
988 | STAM_PROFILE_STOP(&pHandler->Stat, h);
|
---|
989 | PGM_UNLOCK(pGVM);
|
---|
990 | #endif
|
---|
991 | }
|
---|
992 | else
|
---|
993 | {
|
---|
994 | PGM_UNLOCK(pGVM);
|
---|
995 | Log(("PGMR0Trap0eHandlerNPMisconfig: %RGp (uErr=%#x) -> R3\n", GCPhysFault, uErr));
|
---|
996 | rc = VINF_EM_RAW_EMULATE_INSTR;
|
---|
997 | }
|
---|
998 | }
|
---|
999 | }
|
---|
1000 | else
|
---|
1001 | {
|
---|
1002 | /*
|
---|
1003 | * Must be out of sync, so do a SyncPage and restart the instruction.
|
---|
1004 | *
|
---|
1005 | * ASSUMES that ALL handlers are page aligned and covers whole pages
|
---|
1006 | * (assumption asserted in PGMHandlerPhysicalRegisterEx).
|
---|
1007 | */
|
---|
1008 | Log(("PGMR0Trap0eHandlerNPMisconfig: Out of sync page at %RGp (uErr=%#x)\n", GCPhysFault, uErr));
|
---|
1009 | STAM_COUNTER_INC(&pGVCpu->pgm.s.Stats.StatR0NpMiscfgSyncPage);
|
---|
1010 | rc = pgmShwSyncNestedPageLocked(pGVCpu, GCPhysFault, 1 /*cPages*/, enmShwPagingMode);
|
---|
1011 | PGM_UNLOCK(pGVM);
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | STAM_PROFILE_STOP(&pGVCpu->pgm.s.Stats.StatR0NpMiscfg, a);
|
---|
1015 | return rc;
|
---|
1016 |
|
---|
1017 | #else
|
---|
1018 | AssertLogRelFailed();
|
---|
1019 | return VERR_PGM_NOT_USED_IN_MODE;
|
---|
1020 | #endif
|
---|
1021 | }
|
---|
1022 |
|
---|