1 | /* $Id: PGMAllHandler.cpp 55966 2015-05-20 12:42:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PGM - Page Manager / Monitor, Access Handlers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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 | #include <VBox/vmm/dbgf.h>
|
---|
24 | #include <VBox/vmm/pgm.h>
|
---|
25 | #include <VBox/vmm/iom.h>
|
---|
26 | #include <VBox/vmm/mm.h>
|
---|
27 | #include <VBox/vmm/em.h>
|
---|
28 | #include <VBox/vmm/stam.h>
|
---|
29 | #ifdef VBOX_WITH_REM
|
---|
30 | # include <VBox/vmm/rem.h>
|
---|
31 | #endif
|
---|
32 | #include <VBox/vmm/dbgf.h>
|
---|
33 | #ifdef VBOX_WITH_REM
|
---|
34 | # include <VBox/vmm/rem.h>
|
---|
35 | #endif
|
---|
36 | #include "PGMInternal.h"
|
---|
37 | #include <VBox/vmm/vm.h>
|
---|
38 | #include "PGMInline.h"
|
---|
39 |
|
---|
40 | #include <VBox/log.h>
|
---|
41 | #include <iprt/assert.h>
|
---|
42 | #include <iprt/asm-amd64-x86.h>
|
---|
43 | #include <iprt/string.h>
|
---|
44 | #include <VBox/param.h>
|
---|
45 | #include <VBox/err.h>
|
---|
46 | #include <VBox/vmm/selm.h>
|
---|
47 |
|
---|
48 |
|
---|
49 | /*******************************************************************************
|
---|
50 | * Internal Functions *
|
---|
51 | *******************************************************************************/
|
---|
52 | static int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVM pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam);
|
---|
53 | static void pgmHandlerPhysicalDeregisterNotifyREM(PVM pVM, PPGMPHYSHANDLER pCur);
|
---|
54 | static void pgmHandlerPhysicalResetRamFlags(PVM pVM, PPGMPHYSHANDLER pCur);
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Internal worker for releasing a physical handler type registration reference.
|
---|
59 | *
|
---|
60 | * @returns New reference count. UINT32_MAX if invalid input (asserted).
|
---|
61 | * @param pVM Pointer to the cross context VM structure.
|
---|
62 | * @param pType Pointer to the type registration.
|
---|
63 | */
|
---|
64 | DECLINLINE(uint32_t) pgmHandlerPhysicalTypeRelease(PVM pVM, PPGMPHYSHANDLERTYPEINT pType)
|
---|
65 | {
|
---|
66 | AssertMsgReturn(pType->u32Magic == PGMPHYSHANDLERTYPEINT_MAGIC, ("%#x\n", pType->u32Magic), UINT32_MAX);
|
---|
67 | uint32_t cRefs = ASMAtomicDecU32(&pType->cRefs);
|
---|
68 | if (cRefs == 0)
|
---|
69 | {
|
---|
70 | pgmLock(pVM);
|
---|
71 | pType->u32Magic = PGMPHYSHANDLERTYPEINT_MAGIC_DEAD;
|
---|
72 | RTListOff32NodeRemove(&pType->ListNode);
|
---|
73 | pgmUnlock(pVM);
|
---|
74 | MMHyperFree(pVM, pType);
|
---|
75 | }
|
---|
76 | return cRefs;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Internal worker for retaining a physical handler type registration reference.
|
---|
82 | *
|
---|
83 | * @returns New reference count. UINT32_MAX if invalid input (asserted).
|
---|
84 | * @param pVM Pointer to the cross context VM structure.
|
---|
85 | * @param pType Pointer to the type registration.
|
---|
86 | */
|
---|
87 | DECLINLINE(uint32_t) pgmHandlerPhysicalTypeRetain(PVM pVM, PPGMPHYSHANDLERTYPEINT pType)
|
---|
88 | {
|
---|
89 | AssertMsgReturn(pType->u32Magic == PGMPHYSHANDLERTYPEINT_MAGIC, ("%#x\n", pType->u32Magic), UINT32_MAX);
|
---|
90 | uint32_t cRefs = ASMAtomicIncU32(&pType->cRefs);
|
---|
91 | Assert(cRefs < _1M && cRefs > 0);
|
---|
92 | return cRefs;
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Releases a reference to a physical handler type registration.
|
---|
98 | *
|
---|
99 | * @returns New reference count. UINT32_MAX if invalid input (asserted).
|
---|
100 | * @param pVM Pointer to the cross context VM structure.
|
---|
101 | * @param hType The type regiration handle.
|
---|
102 | */
|
---|
103 | VMMDECL(uint32_t) PGMHandlerPhysicalTypeRelease(PVM pVM, PGMPHYSHANDLERTYPE hType)
|
---|
104 | {
|
---|
105 | if (hType != NIL_PGMPHYSHANDLERTYPE)
|
---|
106 | return pgmHandlerPhysicalTypeRelease(pVM, PGMPHYSHANDLERTYPEINT_FROM_HANDLE(pVM, hType));
|
---|
107 | return 0;
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Retains a reference to a physical handler type registration.
|
---|
113 | *
|
---|
114 | * @returns New reference count. UINT32_MAX if invalid input (asserted).
|
---|
115 | * @param pVM Pointer to the cross context VM structure.
|
---|
116 | * @param hType The type regiration handle.
|
---|
117 | */
|
---|
118 | VMMDECL(uint32_t) PGMHandlerPhysicalTypeRetain(PVM pVM, PGMPHYSHANDLERTYPE hType)
|
---|
119 | {
|
---|
120 | return pgmHandlerPhysicalTypeRetain(pVM, PGMPHYSHANDLERTYPEINT_FROM_HANDLE(pVM, hType));
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Register a access handler for a physical range.
|
---|
127 | *
|
---|
128 | * @returns VBox status code.
|
---|
129 | * @retval VINF_SUCCESS when successfully installed.
|
---|
130 | * @retval VINF_PGM_GCPHYS_ALIASED when the shadow PTs could be updated because
|
---|
131 | * the guest page aliased or/and mapped by multiple PTs. A CR3 sync has been
|
---|
132 | * flagged together with a pool clearing.
|
---|
133 | * @retval VERR_PGM_HANDLER_PHYSICAL_CONFLICT if the range conflicts with an existing
|
---|
134 | * one. A debug assertion is raised.
|
---|
135 | *
|
---|
136 | * @param pVM Pointer to the VM.
|
---|
137 | * @param GCPhys Start physical address.
|
---|
138 | * @param GCPhysLast Last physical address. (inclusive)
|
---|
139 | * @param hType The handler type registration handle.
|
---|
140 | * @param pvUserR3 User argument to the R3 handler.
|
---|
141 | * @param pvUserR0 User argument to the R0 handler.
|
---|
142 | * @param pvUserRC User argument to the RC handler. This can be a value
|
---|
143 | * less that 0x10000 or a (non-null) pointer that is
|
---|
144 | * automatically relocated.
|
---|
145 | * @param pszDesc Description of this handler. If NULL, the type
|
---|
146 | * description will be used instead.
|
---|
147 | */
|
---|
148 | VMMDECL(int) PGMHandlerPhysicalRegister(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast, PGMPHYSHANDLERTYPE hType,
|
---|
149 | RTR3PTR pvUserR3, RTR0PTR pvUserR0, RTRCPTR pvUserRC, R3PTRTYPE(const char *) pszDesc)
|
---|
150 | {
|
---|
151 | PPGMPHYSHANDLERTYPEINT pType = PGMPHYSHANDLERTYPEINT_FROM_HANDLE(pVM, hType);
|
---|
152 | Log(("PGMHandlerPhysicalRegister: GCPhys=%RGp GCPhysLast=%RGp pvUserR3=%RHv pvUserR0=%RHv pvUserGC=%RRv hType=%#x (%d, %s) pszDesc=%RHv:%s\n",
|
---|
153 | GCPhys, GCPhysLast, pvUserR3, pvUserR0, pvUserRC, hType, pType->enmKind, R3STRING(pType->pszDesc), pszDesc, R3STRING(pszDesc)));
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Validate input.
|
---|
157 | */
|
---|
158 | AssertReturn(pType->u32Magic == PGMPHYSHANDLERTYPEINT_MAGIC, VERR_INVALID_HANDLE);
|
---|
159 | AssertMsgReturn(GCPhys < GCPhysLast, ("GCPhys >= GCPhysLast (%#x >= %#x)\n", GCPhys, GCPhysLast), VERR_INVALID_PARAMETER);
|
---|
160 | switch (pType->enmKind)
|
---|
161 | {
|
---|
162 | case PGMPHYSHANDLERKIND_WRITE:
|
---|
163 | break;
|
---|
164 | case PGMPHYSHANDLERKIND_MMIO:
|
---|
165 | case PGMPHYSHANDLERKIND_ALL:
|
---|
166 | /* Simplification for PGMPhysRead, PGMR0Trap0eHandlerNPMisconfig and others: Full pages. */
|
---|
167 | AssertMsgReturn(!(GCPhys & PAGE_OFFSET_MASK), ("%RGp\n", GCPhys), VERR_INVALID_PARAMETER);
|
---|
168 | AssertMsgReturn((GCPhysLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK, ("%RGp\n", GCPhysLast), VERR_INVALID_PARAMETER);
|
---|
169 | break;
|
---|
170 | default:
|
---|
171 | AssertMsgFailed(("Invalid input enmKind=%d!\n", pType->enmKind));
|
---|
172 | return VERR_INVALID_PARAMETER;
|
---|
173 | }
|
---|
174 | AssertMsgReturn( (RTRCUINTPTR)pvUserRC < 0x10000
|
---|
175 | || MMHyperR3ToRC(pVM, MMHyperRCToR3(pVM, pvUserRC)) == pvUserRC,
|
---|
176 | ("Not RC pointer! pvUserRC=%RRv\n", pvUserRC),
|
---|
177 | VERR_INVALID_PARAMETER);
|
---|
178 | AssertMsgReturn( (RTR0UINTPTR)pvUserR0 < 0x10000
|
---|
179 | || MMHyperR3ToR0(pVM, MMHyperR0ToR3(pVM, pvUserR0)) == pvUserR0,
|
---|
180 | ("Not R0 pointer! pvUserR0=%RHv\n", pvUserR0),
|
---|
181 | VERR_INVALID_PARAMETER);
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * We require the range to be within registered ram.
|
---|
185 | * There is no apparent need to support ranges which cover more than one ram range.
|
---|
186 | */
|
---|
187 | PPGMRAMRANGE pRam = pgmPhysGetRange(pVM, GCPhys);
|
---|
188 | if ( !pRam
|
---|
189 | || GCPhysLast < pRam->GCPhys
|
---|
190 | || GCPhys > pRam->GCPhysLast)
|
---|
191 | {
|
---|
192 | #ifdef IN_RING3
|
---|
193 | DBGFR3Info(pVM->pUVM, "phys", NULL, NULL);
|
---|
194 | #endif
|
---|
195 | AssertMsgFailed(("No RAM range for %RGp-%RGp\n", GCPhys, GCPhysLast));
|
---|
196 | return VERR_PGM_HANDLER_PHYSICAL_NO_RAM_RANGE;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /*
|
---|
200 | * Allocate and initialize the new entry.
|
---|
201 | */
|
---|
202 | PPGMPHYSHANDLER pNew;
|
---|
203 | int rc = MMHyperAlloc(pVM, sizeof(*pNew), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew);
|
---|
204 | if (RT_FAILURE(rc))
|
---|
205 | return rc;
|
---|
206 |
|
---|
207 | pNew->Core.Key = GCPhys;
|
---|
208 | pNew->Core.KeyLast = GCPhysLast;
|
---|
209 | pNew->cPages = (GCPhysLast - (GCPhys & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
210 | pNew->cAliasedPages = 0;
|
---|
211 | pNew->cTmpOffPages = 0;
|
---|
212 | pNew->pvUserR3 = pvUserR3;
|
---|
213 | pNew->pvUserR0 = pvUserR0;
|
---|
214 | pNew->pvUserRC = pvUserRC;
|
---|
215 | pNew->hType = hType;
|
---|
216 | pNew->pszDesc = pszDesc != NIL_RTR3PTR ? pszDesc : pType->pszDesc;
|
---|
217 | pgmHandlerPhysicalTypeRetain(pVM, pType);
|
---|
218 |
|
---|
219 | pgmLock(pVM);
|
---|
220 |
|
---|
221 | /*
|
---|
222 | * Try insert into list.
|
---|
223 | */
|
---|
224 | if (RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pNew->Core))
|
---|
225 | {
|
---|
226 | rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pNew, pRam);
|
---|
227 | if (rc == VINF_PGM_SYNC_CR3)
|
---|
228 | rc = VINF_PGM_GCPHYS_ALIASED;
|
---|
229 | pgmUnlock(pVM);
|
---|
230 | #ifdef VBOX_WITH_REM
|
---|
231 | # ifndef IN_RING3
|
---|
232 | REMNotifyHandlerPhysicalRegister(pVM, pType->enmKind, GCPhys, GCPhysLast - GCPhys + 1, !!pType->pfnHandlerR3);
|
---|
233 | # else
|
---|
234 | REMR3NotifyHandlerPhysicalRegister(pVM, pType->enmKind, GCPhys, GCPhysLast - GCPhys + 1, !!pType->pfnHandlerR3);
|
---|
235 | # endif
|
---|
236 | #endif
|
---|
237 | if (rc != VINF_SUCCESS)
|
---|
238 | Log(("PGMHandlerPhysicalRegisterEx: returns %Rrc (%RGp-%RGp)\n", rc, GCPhys, GCPhysLast));
|
---|
239 | return rc;
|
---|
240 | }
|
---|
241 |
|
---|
242 | pgmUnlock(pVM);
|
---|
243 |
|
---|
244 | #if defined(IN_RING3) && defined(VBOX_STRICT)
|
---|
245 | DBGFR3Info(pVM->pUVM, "handlers", "phys nostats", NULL);
|
---|
246 | #endif
|
---|
247 | AssertMsgFailed(("Conflict! GCPhys=%RGp GCPhysLast=%RGp pszDesc=%s/%s\n",
|
---|
248 | GCPhys, GCPhysLast, R3STRING(pszDesc), R3STRING(pType->pszDesc)));
|
---|
249 | pgmHandlerPhysicalTypeRelease(pVM, pType);
|
---|
250 | MMHyperFree(pVM, pNew);
|
---|
251 | return VERR_PGM_HANDLER_PHYSICAL_CONFLICT;
|
---|
252 | }
|
---|
253 |
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * Sets ram range flags and attempts updating shadow PTs.
|
---|
257 | *
|
---|
258 | * @returns VBox status code.
|
---|
259 | * @retval VINF_SUCCESS when shadow PTs was successfully updated.
|
---|
260 | * @retval VINF_PGM_SYNC_CR3 when the shadow PTs could be updated because
|
---|
261 | * the guest page aliased or/and mapped by multiple PTs. FFs set.
|
---|
262 | * @param pVM Pointer to the VM.
|
---|
263 | * @param pCur The physical handler.
|
---|
264 | * @param pRam The RAM range.
|
---|
265 | */
|
---|
266 | static int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVM pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam)
|
---|
267 | {
|
---|
268 | /*
|
---|
269 | * Iterate the guest ram pages updating the flags and flushing PT entries
|
---|
270 | * mapping the page.
|
---|
271 | */
|
---|
272 | bool fFlushTLBs = false;
|
---|
273 | int rc = VINF_SUCCESS;
|
---|
274 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
|
---|
275 | const unsigned uState = pCurType->uState;
|
---|
276 | uint32_t cPages = pCur->cPages;
|
---|
277 | uint32_t i = (pCur->Core.Key - pRam->GCPhys) >> PAGE_SHIFT;
|
---|
278 | for (;;)
|
---|
279 | {
|
---|
280 | PPGMPAGE pPage = &pRam->aPages[i];
|
---|
281 | AssertMsg(pCurType->enmKind != PGMPHYSHANDLERKIND_MMIO || PGM_PAGE_IS_MMIO(pPage),
|
---|
282 | ("%RGp %R[pgmpage]\n", pRam->GCPhys + (i << PAGE_SHIFT), pPage));
|
---|
283 |
|
---|
284 | /* Only do upgrades. */
|
---|
285 | if (PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) < uState)
|
---|
286 | {
|
---|
287 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
|
---|
288 |
|
---|
289 | int rc2 = pgmPoolTrackUpdateGCPhys(pVM, pRam->GCPhys + (i << PAGE_SHIFT), pPage,
|
---|
290 | false /* allow updates of PTEs (instead of flushing) */, &fFlushTLBs);
|
---|
291 | if (rc2 != VINF_SUCCESS && rc == VINF_SUCCESS)
|
---|
292 | rc = rc2;
|
---|
293 | }
|
---|
294 |
|
---|
295 | /* next */
|
---|
296 | if (--cPages == 0)
|
---|
297 | break;
|
---|
298 | i++;
|
---|
299 | }
|
---|
300 |
|
---|
301 | if (fFlushTLBs)
|
---|
302 | {
|
---|
303 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
304 | Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: flushing guest TLBs; rc=%d\n", rc));
|
---|
305 | }
|
---|
306 | else
|
---|
307 | Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: doesn't flush guest TLBs. rc=%Rrc; sync flags=%x VMCPU_FF_PGM_SYNC_CR3=%d\n", rc, VMMGetCpu(pVM)->pgm.s.fSyncFlags, VMCPU_FF_IS_SET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3)));
|
---|
308 |
|
---|
309 | return rc;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | /**
|
---|
314 | * Register a physical page access handler.
|
---|
315 | *
|
---|
316 | * @returns VBox status code.
|
---|
317 | * @param pVM Pointer to the VM.
|
---|
318 | * @param GCPhys Start physical address.
|
---|
319 | */
|
---|
320 | VMMDECL(int) PGMHandlerPhysicalDeregister(PVM pVM, RTGCPHYS GCPhys)
|
---|
321 | {
|
---|
322 | /*
|
---|
323 | * Find the handler.
|
---|
324 | */
|
---|
325 | pgmLock(pVM);
|
---|
326 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
327 | if (pCur)
|
---|
328 | {
|
---|
329 | LogFlow(("PGMHandlerPhysicalDeregister: Removing Range %RGp-%RGp %s\n", pCur->Core.Key, pCur->Core.KeyLast, R3STRING(pCur->pszDesc)));
|
---|
330 |
|
---|
331 | /*
|
---|
332 | * Clear the page bits, notify the REM about this change and clear
|
---|
333 | * the cache.
|
---|
334 | */
|
---|
335 | pgmHandlerPhysicalResetRamFlags(pVM, pCur);
|
---|
336 | pgmHandlerPhysicalDeregisterNotifyREM(pVM, pCur);
|
---|
337 | pVM->pgm.s.pLastPhysHandlerR0 = 0;
|
---|
338 | pVM->pgm.s.pLastPhysHandlerR3 = 0;
|
---|
339 | pVM->pgm.s.pLastPhysHandlerRC = 0;
|
---|
340 | PGMHandlerPhysicalTypeRelease(pVM, pCur->hType);
|
---|
341 | MMHyperFree(pVM, pCur);
|
---|
342 | pgmUnlock(pVM);
|
---|
343 | return VINF_SUCCESS;
|
---|
344 | }
|
---|
345 | pgmUnlock(pVM);
|
---|
346 |
|
---|
347 | AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys));
|
---|
348 | return VERR_PGM_HANDLER_NOT_FOUND;
|
---|
349 | }
|
---|
350 |
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Shared code with modify.
|
---|
354 | */
|
---|
355 | static void pgmHandlerPhysicalDeregisterNotifyREM(PVM pVM, PPGMPHYSHANDLER pCur)
|
---|
356 | {
|
---|
357 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
|
---|
358 | RTGCPHYS GCPhysStart = pCur->Core.Key;
|
---|
359 | RTGCPHYS GCPhysLast = pCur->Core.KeyLast;
|
---|
360 |
|
---|
361 | /*
|
---|
362 | * Page align the range.
|
---|
363 | *
|
---|
364 | * Since we've reset (recalculated) the physical handler state of all pages
|
---|
365 | * we can make use of the page states to figure out whether a page should be
|
---|
366 | * included in the REM notification or not.
|
---|
367 | */
|
---|
368 | if ( (pCur->Core.Key & PAGE_OFFSET_MASK)
|
---|
369 | || ((pCur->Core.KeyLast + 1) & PAGE_OFFSET_MASK))
|
---|
370 | {
|
---|
371 | Assert(pCurType->enmKind != PGMPHYSHANDLERKIND_MMIO);
|
---|
372 |
|
---|
373 | if (GCPhysStart & PAGE_OFFSET_MASK)
|
---|
374 | {
|
---|
375 | PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhysStart);
|
---|
376 | if ( pPage
|
---|
377 | && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_NONE)
|
---|
378 | {
|
---|
379 | RTGCPHYS GCPhys = (GCPhysStart + (PAGE_SIZE - 1)) & X86_PTE_PAE_PG_MASK;
|
---|
380 | if ( GCPhys > GCPhysLast
|
---|
381 | || GCPhys < GCPhysStart)
|
---|
382 | return;
|
---|
383 | GCPhysStart = GCPhys;
|
---|
384 | }
|
---|
385 | else
|
---|
386 | GCPhysStart &= X86_PTE_PAE_PG_MASK;
|
---|
387 | Assert(!pPage || PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO); /* these are page aligned atm! */
|
---|
388 | }
|
---|
389 |
|
---|
390 | if (GCPhysLast & PAGE_OFFSET_MASK)
|
---|
391 | {
|
---|
392 | PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhysLast);
|
---|
393 | if ( pPage
|
---|
394 | && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_NONE)
|
---|
395 | {
|
---|
396 | RTGCPHYS GCPhys = (GCPhysLast & X86_PTE_PAE_PG_MASK) - 1;
|
---|
397 | if ( GCPhys < GCPhysStart
|
---|
398 | || GCPhys > GCPhysLast)
|
---|
399 | return;
|
---|
400 | GCPhysLast = GCPhys;
|
---|
401 | }
|
---|
402 | else
|
---|
403 | GCPhysLast |= PAGE_OFFSET_MASK;
|
---|
404 | Assert(!pPage || PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO); /* these are page aligned atm! */
|
---|
405 | }
|
---|
406 | }
|
---|
407 |
|
---|
408 | /*
|
---|
409 | * Tell REM.
|
---|
410 | */
|
---|
411 | const bool fRestoreAsRAM = pCurType->pfnHandlerR3
|
---|
412 | && pCurType->enmKind != PGMPHYSHANDLERKIND_MMIO; /** @todo this isn't entirely correct. */
|
---|
413 | #ifdef VBOX_WITH_REM
|
---|
414 | # ifndef IN_RING3
|
---|
415 | REMNotifyHandlerPhysicalDeregister(pVM, pCurType->enmKind, GCPhysStart, GCPhysLast - GCPhysStart + 1,
|
---|
416 | !!pCurType->pfnHandlerR3, fRestoreAsRAM);
|
---|
417 | # else
|
---|
418 | REMR3NotifyHandlerPhysicalDeregister(pVM, pCurType->enmKind, GCPhysStart, GCPhysLast - GCPhysStart + 1,
|
---|
419 | !!pCurType->pfnHandlerR3, fRestoreAsRAM);
|
---|
420 | # endif
|
---|
421 | #endif
|
---|
422 | }
|
---|
423 |
|
---|
424 |
|
---|
425 | /**
|
---|
426 | * pgmHandlerPhysicalResetRamFlags helper that checks for other handlers on
|
---|
427 | * edge pages.
|
---|
428 | */
|
---|
429 | DECLINLINE(void) pgmHandlerPhysicalRecalcPageState(PVM pVM, RTGCPHYS GCPhys, bool fAbove, PPGMRAMRANGE *ppRamHint)
|
---|
430 | {
|
---|
431 | /*
|
---|
432 | * Look for other handlers.
|
---|
433 | */
|
---|
434 | unsigned uState = PGM_PAGE_HNDL_PHYS_STATE_NONE;
|
---|
435 | for (;;)
|
---|
436 | {
|
---|
437 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys, fAbove);
|
---|
438 | if ( !pCur
|
---|
439 | || ((fAbove ? pCur->Core.Key : pCur->Core.KeyLast) >> PAGE_SHIFT) != (GCPhys >> PAGE_SHIFT))
|
---|
440 | break;
|
---|
441 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
|
---|
442 | uState = RT_MAX(uState, pCurType->uState);
|
---|
443 |
|
---|
444 | /* next? */
|
---|
445 | RTGCPHYS GCPhysNext = fAbove
|
---|
446 | ? pCur->Core.KeyLast + 1
|
---|
447 | : pCur->Core.Key - 1;
|
---|
448 | if ((GCPhysNext >> PAGE_SHIFT) != (GCPhys >> PAGE_SHIFT))
|
---|
449 | break;
|
---|
450 | GCPhys = GCPhysNext;
|
---|
451 | }
|
---|
452 |
|
---|
453 | /*
|
---|
454 | * Update if we found something that is a higher priority
|
---|
455 | * state than the current.
|
---|
456 | */
|
---|
457 | if (uState != PGM_PAGE_HNDL_PHYS_STATE_NONE)
|
---|
458 | {
|
---|
459 | PPGMPAGE pPage;
|
---|
460 | int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, ppRamHint);
|
---|
461 | if ( RT_SUCCESS(rc)
|
---|
462 | && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) < uState)
|
---|
463 | {
|
---|
464 | /* This should normally not be necessary. */
|
---|
465 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
|
---|
466 | bool fFlushTLBs ;
|
---|
467 | rc = pgmPoolTrackUpdateGCPhys(pVM, GCPhys, pPage, false /*fFlushPTEs*/, &fFlushTLBs);
|
---|
468 | if (RT_SUCCESS(rc) && fFlushTLBs)
|
---|
469 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
470 | else
|
---|
471 | AssertRC(rc);
|
---|
472 | }
|
---|
473 | else
|
---|
474 | AssertRC(rc);
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 |
|
---|
479 | /**
|
---|
480 | * Resets an aliased page.
|
---|
481 | *
|
---|
482 | * @param pVM The VM.
|
---|
483 | * @param pPage The page.
|
---|
484 | * @param GCPhysPage The page address in case it comes in handy.
|
---|
485 | * @param fDoAccounting Whether to perform accounting. (Only set during
|
---|
486 | * reset where pgmR3PhysRamReset doesn't have the
|
---|
487 | * handler structure handy.)
|
---|
488 | */
|
---|
489 | void pgmHandlerPhysicalResetAliasedPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhysPage, bool fDoAccounting)
|
---|
490 | {
|
---|
491 | Assert( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO
|
---|
492 | || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_SPECIAL_ALIAS_MMIO);
|
---|
493 | Assert(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) == PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
|
---|
494 |
|
---|
495 | /*
|
---|
496 | * Flush any shadow page table references *first*.
|
---|
497 | */
|
---|
498 | bool fFlushTLBs = false;
|
---|
499 | int rc = pgmPoolTrackUpdateGCPhys(pVM, GCPhysPage, pPage, true /*fFlushPTEs*/, &fFlushTLBs);
|
---|
500 | AssertLogRelRCReturnVoid(rc);
|
---|
501 | # ifdef IN_RC
|
---|
502 | if (fFlushTLBs && rc != VINF_PGM_SYNC_CR3)
|
---|
503 | PGM_INVL_VCPU_TLBS(VMMGetCpu0(pVM));
|
---|
504 | # else
|
---|
505 | HMFlushTLBOnAllVCpus(pVM);
|
---|
506 | # endif
|
---|
507 |
|
---|
508 | /*
|
---|
509 | * Make it an MMIO/Zero page.
|
---|
510 | */
|
---|
511 | PGM_PAGE_SET_HCPHYS(pVM, pPage, pVM->pgm.s.HCPhysZeroPg);
|
---|
512 | PGM_PAGE_SET_TYPE(pVM, pPage, PGMPAGETYPE_MMIO);
|
---|
513 | PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_ZERO);
|
---|
514 | PGM_PAGE_SET_PAGEID(pVM, pPage, NIL_GMM_PAGEID);
|
---|
515 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_ALL);
|
---|
516 |
|
---|
517 | /* Flush its TLB entry. */
|
---|
518 | pgmPhysInvalidatePageMapTLBEntry(pVM, GCPhysPage);
|
---|
519 |
|
---|
520 | /*
|
---|
521 | * Do accounting for pgmR3PhysRamReset.
|
---|
522 | */
|
---|
523 | if (fDoAccounting)
|
---|
524 | {
|
---|
525 | PPGMPHYSHANDLER pHandler = pgmHandlerPhysicalLookup(pVM, GCPhysPage);
|
---|
526 | if (RT_LIKELY(pHandler))
|
---|
527 | {
|
---|
528 | Assert(pHandler->cAliasedPages > 0);
|
---|
529 | pHandler->cAliasedPages--;
|
---|
530 | }
|
---|
531 | else
|
---|
532 | AssertFailed();
|
---|
533 | }
|
---|
534 | }
|
---|
535 |
|
---|
536 |
|
---|
537 | /**
|
---|
538 | * Resets ram range flags.
|
---|
539 | *
|
---|
540 | * @returns VBox status code.
|
---|
541 | * @retval VINF_SUCCESS when shadow PTs was successfully updated.
|
---|
542 | * @param pVM Pointer to the VM.
|
---|
543 | * @param pCur The physical handler.
|
---|
544 | *
|
---|
545 | * @remark We don't start messing with the shadow page tables, as we've
|
---|
546 | * already got code in Trap0e which deals with out of sync handler
|
---|
547 | * flags (originally conceived for global pages).
|
---|
548 | */
|
---|
549 | static void pgmHandlerPhysicalResetRamFlags(PVM pVM, PPGMPHYSHANDLER pCur)
|
---|
550 | {
|
---|
551 | /*
|
---|
552 | * Iterate the guest ram pages updating the state.
|
---|
553 | */
|
---|
554 | RTUINT cPages = pCur->cPages;
|
---|
555 | RTGCPHYS GCPhys = pCur->Core.Key;
|
---|
556 | PPGMRAMRANGE pRamHint = NULL;
|
---|
557 | for (;;)
|
---|
558 | {
|
---|
559 | PPGMPAGE pPage;
|
---|
560 | int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
|
---|
561 | if (RT_SUCCESS(rc))
|
---|
562 | {
|
---|
563 | /* Reset aliased MMIO pages to MMIO, since this aliasing is our business.
|
---|
564 | (We don't flip MMIO to RAM though, that's PGMPhys.cpp's job.) */
|
---|
565 | if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO
|
---|
566 | || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_SPECIAL_ALIAS_MMIO)
|
---|
567 | {
|
---|
568 | Assert(pCur->cAliasedPages > 0);
|
---|
569 | pgmHandlerPhysicalResetAliasedPage(pVM, pPage, GCPhys, false /*fDoAccounting*/);
|
---|
570 | pCur->cAliasedPages--;
|
---|
571 | }
|
---|
572 | #ifdef VBOX_STRICT
|
---|
573 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
|
---|
574 | AssertMsg(pCurType->enmKind != PGMPHYSHANDLERKIND_MMIO || PGM_PAGE_IS_MMIO(pPage), ("%RGp %R[pgmpage]\n", GCPhys, pPage));
|
---|
575 | #endif
|
---|
576 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_NONE);
|
---|
577 | }
|
---|
578 | else
|
---|
579 | AssertRC(rc);
|
---|
580 |
|
---|
581 | /* next */
|
---|
582 | if (--cPages == 0)
|
---|
583 | break;
|
---|
584 | GCPhys += PAGE_SIZE;
|
---|
585 | }
|
---|
586 |
|
---|
587 | pCur->cAliasedPages = 0;
|
---|
588 | pCur->cTmpOffPages = 0;
|
---|
589 |
|
---|
590 | /*
|
---|
591 | * Check for partial start and end pages.
|
---|
592 | */
|
---|
593 | if (pCur->Core.Key & PAGE_OFFSET_MASK)
|
---|
594 | pgmHandlerPhysicalRecalcPageState(pVM, pCur->Core.Key - 1, false /* fAbove */, &pRamHint);
|
---|
595 | if ((pCur->Core.KeyLast & PAGE_OFFSET_MASK) != PAGE_OFFSET_MASK)
|
---|
596 | pgmHandlerPhysicalRecalcPageState(pVM, pCur->Core.KeyLast + 1, true /* fAbove */, &pRamHint);
|
---|
597 | }
|
---|
598 |
|
---|
599 |
|
---|
600 | /**
|
---|
601 | * Modify a physical page access handler.
|
---|
602 | *
|
---|
603 | * Modification can only be done to the range it self, not the type or anything else.
|
---|
604 | *
|
---|
605 | * @returns VBox status code.
|
---|
606 | * For all return codes other than VERR_PGM_HANDLER_NOT_FOUND and VINF_SUCCESS the range is deregistered
|
---|
607 | * and a new registration must be performed!
|
---|
608 | * @param pVM Pointer to the VM.
|
---|
609 | * @param GCPhysCurrent Current location.
|
---|
610 | * @param GCPhys New location.
|
---|
611 | * @param GCPhysLast New last location.
|
---|
612 | */
|
---|
613 | VMMDECL(int) PGMHandlerPhysicalModify(PVM pVM, RTGCPHYS GCPhysCurrent, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast)
|
---|
614 | {
|
---|
615 | /*
|
---|
616 | * Remove it.
|
---|
617 | */
|
---|
618 | int rc;
|
---|
619 | pgmLock(pVM);
|
---|
620 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhysCurrent);
|
---|
621 | if (pCur)
|
---|
622 | {
|
---|
623 | /*
|
---|
624 | * Clear the ram flags. (We're gonna move or free it!)
|
---|
625 | */
|
---|
626 | pgmHandlerPhysicalResetRamFlags(pVM, pCur);
|
---|
627 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
|
---|
628 | const bool fRestoreAsRAM = pCurType->pfnHandlerR3
|
---|
629 | && pCurType->enmKind != PGMPHYSHANDLERKIND_MMIO; /** @todo this isn't entirely correct. */
|
---|
630 |
|
---|
631 | /*
|
---|
632 | * Validate the new range, modify and reinsert.
|
---|
633 | */
|
---|
634 | if (GCPhysLast >= GCPhys)
|
---|
635 | {
|
---|
636 | /*
|
---|
637 | * We require the range to be within registered ram.
|
---|
638 | * There is no apparent need to support ranges which cover more than one ram range.
|
---|
639 | */
|
---|
640 | PPGMRAMRANGE pRam = pgmPhysGetRange(pVM, GCPhys);
|
---|
641 | if ( pRam
|
---|
642 | && GCPhys <= pRam->GCPhysLast
|
---|
643 | && GCPhysLast >= pRam->GCPhys)
|
---|
644 | {
|
---|
645 | pCur->Core.Key = GCPhys;
|
---|
646 | pCur->Core.KeyLast = GCPhysLast;
|
---|
647 | pCur->cPages = (GCPhysLast - (GCPhys & X86_PTE_PAE_PG_MASK) + 1) >> PAGE_SHIFT;
|
---|
648 |
|
---|
649 | if (RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pCur->Core))
|
---|
650 | {
|
---|
651 | RTGCPHYS cb = GCPhysLast - GCPhys + 1;
|
---|
652 | PGMPHYSHANDLERKIND enmKind = pCurType->enmKind;
|
---|
653 | bool fHasHCHandler = !!pCurType->pfnHandlerR3;
|
---|
654 |
|
---|
655 | /*
|
---|
656 | * Set ram flags, flush shadow PT entries and finally tell REM about this.
|
---|
657 | */
|
---|
658 | rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pCur, pRam);
|
---|
659 | pgmUnlock(pVM);
|
---|
660 |
|
---|
661 | #ifdef VBOX_WITH_REM
|
---|
662 | # ifndef IN_RING3
|
---|
663 | REMNotifyHandlerPhysicalModify(pVM, enmKind, GCPhysCurrent, GCPhys, cb,
|
---|
664 | fHasHCHandler, fRestoreAsRAM);
|
---|
665 | # else
|
---|
666 | REMR3NotifyHandlerPhysicalModify(pVM, enmKind, GCPhysCurrent, GCPhys, cb,
|
---|
667 | fHasHCHandler, fRestoreAsRAM);
|
---|
668 | # endif
|
---|
669 | #endif
|
---|
670 | PGM_INVL_ALL_VCPU_TLBS(pVM);
|
---|
671 | Log(("PGMHandlerPhysicalModify: GCPhysCurrent=%RGp -> GCPhys=%RGp GCPhysLast=%RGp\n",
|
---|
672 | GCPhysCurrent, GCPhys, GCPhysLast));
|
---|
673 | return VINF_SUCCESS;
|
---|
674 | }
|
---|
675 |
|
---|
676 | AssertMsgFailed(("Conflict! GCPhys=%RGp GCPhysLast=%RGp\n", GCPhys, GCPhysLast));
|
---|
677 | rc = VERR_PGM_HANDLER_PHYSICAL_CONFLICT;
|
---|
678 | }
|
---|
679 | else
|
---|
680 | {
|
---|
681 | AssertMsgFailed(("No RAM range for %RGp-%RGp\n", GCPhys, GCPhysLast));
|
---|
682 | rc = VERR_PGM_HANDLER_PHYSICAL_NO_RAM_RANGE;
|
---|
683 | }
|
---|
684 | }
|
---|
685 | else
|
---|
686 | {
|
---|
687 | AssertMsgFailed(("Invalid range %RGp-%RGp\n", GCPhys, GCPhysLast));
|
---|
688 | rc = VERR_INVALID_PARAMETER;
|
---|
689 | }
|
---|
690 |
|
---|
691 | /*
|
---|
692 | * Invalid new location, flush the cache and free it.
|
---|
693 | * We've only gotta notify REM and free the memory.
|
---|
694 | */
|
---|
695 | pgmHandlerPhysicalDeregisterNotifyREM(pVM, pCur);
|
---|
696 | pVM->pgm.s.pLastPhysHandlerR0 = 0;
|
---|
697 | pVM->pgm.s.pLastPhysHandlerR3 = 0;
|
---|
698 | pVM->pgm.s.pLastPhysHandlerRC = 0;
|
---|
699 | PGMHandlerPhysicalTypeRelease(pVM, pCur->hType);
|
---|
700 | MMHyperFree(pVM, pCur);
|
---|
701 | }
|
---|
702 | else
|
---|
703 | {
|
---|
704 | AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhysCurrent));
|
---|
705 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
706 | }
|
---|
707 |
|
---|
708 | pgmUnlock(pVM);
|
---|
709 | return rc;
|
---|
710 | }
|
---|
711 |
|
---|
712 |
|
---|
713 | /**
|
---|
714 | * Changes the user callback arguments associated with a physical access
|
---|
715 | * handler.
|
---|
716 | *
|
---|
717 | * @returns VBox status code.
|
---|
718 | * @param pVM Pointer to the VM.
|
---|
719 | * @param GCPhys Start physical address of the handler.
|
---|
720 | * @param pvUserR3 User argument to the R3 handler.
|
---|
721 | * @param pvUserR0 User argument to the R0 handler.
|
---|
722 | * @param pvUserRC User argument to the RC handler. Values larger or
|
---|
723 | * equal to 0x10000 will be relocated automatically.
|
---|
724 | */
|
---|
725 | VMMDECL(int) PGMHandlerPhysicalChangeUserArgs(PVM pVM, RTGCPHYS GCPhys, RTR3PTR pvUserR3, RTR0PTR pvUserR0, RTRCPTR pvUserRC)
|
---|
726 | {
|
---|
727 | /*
|
---|
728 | * Find the handler.
|
---|
729 | */
|
---|
730 | int rc = VINF_SUCCESS;
|
---|
731 | pgmLock(pVM);
|
---|
732 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
733 | if (pCur)
|
---|
734 | {
|
---|
735 | /*
|
---|
736 | * Change arguments.
|
---|
737 | */
|
---|
738 | pCur->pvUserR3 = pvUserR3;
|
---|
739 | pCur->pvUserR0 = pvUserR0;
|
---|
740 | pCur->pvUserRC = pvUserRC;
|
---|
741 | }
|
---|
742 | else
|
---|
743 | {
|
---|
744 | AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys));
|
---|
745 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
746 | }
|
---|
747 |
|
---|
748 | pgmUnlock(pVM);
|
---|
749 | return rc;
|
---|
750 | }
|
---|
751 |
|
---|
752 |
|
---|
753 | /**
|
---|
754 | * Splits a physical access handler in two.
|
---|
755 | *
|
---|
756 | * @returns VBox status code.
|
---|
757 | * @param pVM Pointer to the VM.
|
---|
758 | * @param GCPhys Start physical address of the handler.
|
---|
759 | * @param GCPhysSplit The split address.
|
---|
760 | */
|
---|
761 | VMMDECL(int) PGMHandlerPhysicalSplit(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysSplit)
|
---|
762 | {
|
---|
763 | AssertReturn(GCPhys < GCPhysSplit, VERR_INVALID_PARAMETER);
|
---|
764 |
|
---|
765 | /*
|
---|
766 | * Do the allocation without owning the lock.
|
---|
767 | */
|
---|
768 | PPGMPHYSHANDLER pNew;
|
---|
769 | int rc = MMHyperAlloc(pVM, sizeof(*pNew), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew);
|
---|
770 | if (RT_FAILURE(rc))
|
---|
771 | return rc;
|
---|
772 |
|
---|
773 | /*
|
---|
774 | * Get the handler.
|
---|
775 | */
|
---|
776 | pgmLock(pVM);
|
---|
777 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
778 | if (RT_LIKELY(pCur))
|
---|
779 | {
|
---|
780 | if (RT_LIKELY(GCPhysSplit <= pCur->Core.KeyLast))
|
---|
781 | {
|
---|
782 | /*
|
---|
783 | * Create new handler node for the 2nd half.
|
---|
784 | */
|
---|
785 | *pNew = *pCur;
|
---|
786 | pNew->Core.Key = GCPhysSplit;
|
---|
787 | pNew->cPages = (pNew->Core.KeyLast - (pNew->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
788 |
|
---|
789 | pCur->Core.KeyLast = GCPhysSplit - 1;
|
---|
790 | pCur->cPages = (pCur->Core.KeyLast - (pCur->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
791 |
|
---|
792 | if (RT_LIKELY(RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pNew->Core)))
|
---|
793 | {
|
---|
794 | LogFlow(("PGMHandlerPhysicalSplit: %RGp-%RGp and %RGp-%RGp\n",
|
---|
795 | pCur->Core.Key, pCur->Core.KeyLast, pNew->Core.Key, pNew->Core.KeyLast));
|
---|
796 | pgmUnlock(pVM);
|
---|
797 | return VINF_SUCCESS;
|
---|
798 | }
|
---|
799 | AssertMsgFailed(("whu?\n"));
|
---|
800 | rc = VERR_PGM_PHYS_HANDLER_IPE;
|
---|
801 | }
|
---|
802 | else
|
---|
803 | {
|
---|
804 | AssertMsgFailed(("outside range: %RGp-%RGp split %RGp\n", pCur->Core.Key, pCur->Core.KeyLast, GCPhysSplit));
|
---|
805 | rc = VERR_INVALID_PARAMETER;
|
---|
806 | }
|
---|
807 | }
|
---|
808 | else
|
---|
809 | {
|
---|
810 | AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys));
|
---|
811 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
812 | }
|
---|
813 | pgmUnlock(pVM);
|
---|
814 | MMHyperFree(pVM, pNew);
|
---|
815 | return rc;
|
---|
816 | }
|
---|
817 |
|
---|
818 |
|
---|
819 | /**
|
---|
820 | * Joins up two adjacent physical access handlers which has the same callbacks.
|
---|
821 | *
|
---|
822 | * @returns VBox status code.
|
---|
823 | * @param pVM Pointer to the VM.
|
---|
824 | * @param GCPhys1 Start physical address of the first handler.
|
---|
825 | * @param GCPhys2 Start physical address of the second handler.
|
---|
826 | */
|
---|
827 | VMMDECL(int) PGMHandlerPhysicalJoin(PVM pVM, RTGCPHYS GCPhys1, RTGCPHYS GCPhys2)
|
---|
828 | {
|
---|
829 | /*
|
---|
830 | * Get the handlers.
|
---|
831 | */
|
---|
832 | int rc;
|
---|
833 | pgmLock(pVM);
|
---|
834 | PPGMPHYSHANDLER pCur1 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys1);
|
---|
835 | if (RT_LIKELY(pCur1))
|
---|
836 | {
|
---|
837 | PPGMPHYSHANDLER pCur2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys2);
|
---|
838 | if (RT_LIKELY(pCur2))
|
---|
839 | {
|
---|
840 | /*
|
---|
841 | * Make sure that they are adjacent, and that they've got the same callbacks.
|
---|
842 | */
|
---|
843 | if (RT_LIKELY(pCur1->Core.KeyLast + 1 == pCur2->Core.Key))
|
---|
844 | {
|
---|
845 | if (RT_LIKELY(pCur1->hType == pCur2->hType))
|
---|
846 | {
|
---|
847 | PPGMPHYSHANDLER pCur3 = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys2);
|
---|
848 | if (RT_LIKELY(pCur3 == pCur2))
|
---|
849 | {
|
---|
850 | pCur1->Core.KeyLast = pCur2->Core.KeyLast;
|
---|
851 | pCur1->cPages = (pCur1->Core.KeyLast - (pCur1->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
852 | LogFlow(("PGMHandlerPhysicalJoin: %RGp-%RGp %RGp-%RGp\n",
|
---|
853 | pCur1->Core.Key, pCur1->Core.KeyLast, pCur2->Core.Key, pCur2->Core.KeyLast));
|
---|
854 | pVM->pgm.s.pLastPhysHandlerR0 = 0;
|
---|
855 | pVM->pgm.s.pLastPhysHandlerR3 = 0;
|
---|
856 | pVM->pgm.s.pLastPhysHandlerRC = 0;
|
---|
857 | PGMHandlerPhysicalTypeRelease(pVM, pCur2->hType);
|
---|
858 | MMHyperFree(pVM, pCur2);
|
---|
859 | pgmUnlock(pVM);
|
---|
860 | return VINF_SUCCESS;
|
---|
861 | }
|
---|
862 |
|
---|
863 | Assert(pCur3 == pCur2);
|
---|
864 | rc = VERR_PGM_PHYS_HANDLER_IPE;
|
---|
865 | }
|
---|
866 | else
|
---|
867 | {
|
---|
868 | AssertMsgFailed(("mismatching handlers\n"));
|
---|
869 | rc = VERR_ACCESS_DENIED;
|
---|
870 | }
|
---|
871 | }
|
---|
872 | else
|
---|
873 | {
|
---|
874 | AssertMsgFailed(("not adjacent: %RGp-%RGp %RGp-%RGp\n",
|
---|
875 | pCur1->Core.Key, pCur1->Core.KeyLast, pCur2->Core.Key, pCur2->Core.KeyLast));
|
---|
876 | rc = VERR_INVALID_PARAMETER;
|
---|
877 | }
|
---|
878 | }
|
---|
879 | else
|
---|
880 | {
|
---|
881 | AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys2));
|
---|
882 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
883 | }
|
---|
884 | }
|
---|
885 | else
|
---|
886 | {
|
---|
887 | AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys1));
|
---|
888 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
889 | }
|
---|
890 | pgmUnlock(pVM);
|
---|
891 | return rc;
|
---|
892 |
|
---|
893 | }
|
---|
894 |
|
---|
895 |
|
---|
896 | /**
|
---|
897 | * Resets any modifications to individual pages in a physical page access
|
---|
898 | * handler region.
|
---|
899 | *
|
---|
900 | * This is used in pair with PGMHandlerPhysicalPageTempOff(),
|
---|
901 | * PGMHandlerPhysicalPageAlias() or PGMHandlerPhysicalPageAliasHC().
|
---|
902 | *
|
---|
903 | * @returns VBox status code.
|
---|
904 | * @param pVM Pointer to the VM
|
---|
905 | * @param GCPhys The start address of the handler regions, i.e. what you
|
---|
906 | * passed to PGMR3HandlerPhysicalRegister(),
|
---|
907 | * PGMHandlerPhysicalRegisterEx() or
|
---|
908 | * PGMHandlerPhysicalModify().
|
---|
909 | */
|
---|
910 | VMMDECL(int) PGMHandlerPhysicalReset(PVM pVM, RTGCPHYS GCPhys)
|
---|
911 | {
|
---|
912 | LogFlow(("PGMHandlerPhysicalReset GCPhys=%RGp\n", GCPhys));
|
---|
913 | pgmLock(pVM);
|
---|
914 |
|
---|
915 | /*
|
---|
916 | * Find the handler.
|
---|
917 | */
|
---|
918 | int rc;
|
---|
919 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
920 | if (RT_LIKELY(pCur))
|
---|
921 | {
|
---|
922 | /*
|
---|
923 | * Validate kind.
|
---|
924 | */
|
---|
925 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
|
---|
926 | switch (pCurType->enmKind)
|
---|
927 | {
|
---|
928 | case PGMPHYSHANDLERKIND_WRITE:
|
---|
929 | case PGMPHYSHANDLERKIND_ALL:
|
---|
930 | case PGMPHYSHANDLERKIND_MMIO: /* NOTE: Only use when clearing MMIO ranges with aliased MMIO2 pages! */
|
---|
931 | {
|
---|
932 | STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysHandlerReset)); /**@Todo move out of switch */
|
---|
933 | PPGMRAMRANGE pRam = pgmPhysGetRange(pVM, GCPhys);
|
---|
934 | Assert(pRam);
|
---|
935 | Assert(pRam->GCPhys <= pCur->Core.Key);
|
---|
936 | Assert(pRam->GCPhysLast >= pCur->Core.KeyLast);
|
---|
937 |
|
---|
938 | if (pCurType->enmKind == PGMPHYSHANDLERKIND_MMIO)
|
---|
939 | {
|
---|
940 | /*
|
---|
941 | * Reset all the PGMPAGETYPE_MMIO2_ALIAS_MMIO pages first and that's it.
|
---|
942 | * This could probably be optimized a bit wrt to flushing, but I'm too lazy
|
---|
943 | * to do that now...
|
---|
944 | */
|
---|
945 | if (pCur->cAliasedPages)
|
---|
946 | {
|
---|
947 | PPGMPAGE pPage = &pRam->aPages[(pCur->Core.Key - pRam->GCPhys) >> PAGE_SHIFT];
|
---|
948 | uint32_t cLeft = pCur->cPages;
|
---|
949 | while (cLeft-- > 0)
|
---|
950 | {
|
---|
951 | if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO
|
---|
952 | || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_SPECIAL_ALIAS_MMIO)
|
---|
953 | {
|
---|
954 | Assert(pCur->cAliasedPages > 0);
|
---|
955 | pgmHandlerPhysicalResetAliasedPage(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)cLeft << PAGE_SHIFT),
|
---|
956 | false /*fDoAccounting*/);
|
---|
957 | --pCur->cAliasedPages;
|
---|
958 | #ifndef VBOX_STRICT
|
---|
959 | if (pCur->cAliasedPages == 0)
|
---|
960 | break;
|
---|
961 | #endif
|
---|
962 | }
|
---|
963 | Assert(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO);
|
---|
964 | pPage++;
|
---|
965 | }
|
---|
966 | Assert(pCur->cAliasedPages == 0);
|
---|
967 | }
|
---|
968 | }
|
---|
969 | else if (pCur->cTmpOffPages > 0)
|
---|
970 | {
|
---|
971 | /*
|
---|
972 | * Set the flags and flush shadow PT entries.
|
---|
973 | */
|
---|
974 | rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pCur, pRam);
|
---|
975 | }
|
---|
976 |
|
---|
977 | pCur->cAliasedPages = 0;
|
---|
978 | pCur->cTmpOffPages = 0;
|
---|
979 |
|
---|
980 | rc = VINF_SUCCESS;
|
---|
981 | break;
|
---|
982 | }
|
---|
983 |
|
---|
984 | /*
|
---|
985 | * Invalid.
|
---|
986 | */
|
---|
987 | default:
|
---|
988 | AssertMsgFailed(("Invalid type %d! Corruption!\n", pCurType->enmKind));
|
---|
989 | rc = VERR_PGM_PHYS_HANDLER_IPE;
|
---|
990 | break;
|
---|
991 | }
|
---|
992 | }
|
---|
993 | else
|
---|
994 | {
|
---|
995 | AssertMsgFailed(("Didn't find MMIO Range starting at %#x\n", GCPhys));
|
---|
996 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
997 | }
|
---|
998 |
|
---|
999 | pgmUnlock(pVM);
|
---|
1000 | return rc;
|
---|
1001 | }
|
---|
1002 |
|
---|
1003 |
|
---|
1004 | /**
|
---|
1005 | * Temporarily turns off the access monitoring of a page within a monitored
|
---|
1006 | * physical write/all page access handler region.
|
---|
1007 | *
|
---|
1008 | * Use this when no further \#PFs are required for that page. Be aware that
|
---|
1009 | * a page directory sync might reset the flags, and turn on access monitoring
|
---|
1010 | * for the page.
|
---|
1011 | *
|
---|
1012 | * The caller must do required page table modifications.
|
---|
1013 | *
|
---|
1014 | * @returns VBox status code.
|
---|
1015 | * @param pVM Pointer to the VM
|
---|
1016 | * @param GCPhys The start address of the access handler. This
|
---|
1017 | * must be a fully page aligned range or we risk
|
---|
1018 | * messing up other handlers installed for the
|
---|
1019 | * start and end pages.
|
---|
1020 | * @param GCPhysPage The physical address of the page to turn off
|
---|
1021 | * access monitoring for.
|
---|
1022 | */
|
---|
1023 | VMMDECL(int) PGMHandlerPhysicalPageTempOff(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage)
|
---|
1024 | {
|
---|
1025 | LogFlow(("PGMHandlerPhysicalPageTempOff GCPhysPage=%RGp\n", GCPhysPage));
|
---|
1026 |
|
---|
1027 | pgmLock(pVM);
|
---|
1028 | /*
|
---|
1029 | * Validate the range.
|
---|
1030 | */
|
---|
1031 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
1032 | if (RT_LIKELY(pCur))
|
---|
1033 | {
|
---|
1034 | if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
|
---|
1035 | && GCPhysPage <= pCur->Core.KeyLast))
|
---|
1036 | {
|
---|
1037 | Assert(!(pCur->Core.Key & PAGE_OFFSET_MASK));
|
---|
1038 | Assert((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
|
---|
1039 |
|
---|
1040 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
|
---|
1041 | AssertReturnStmt( pCurType->enmKind == PGMPHYSHANDLERKIND_WRITE
|
---|
1042 | || pCurType->enmKind == PGMPHYSHANDLERKIND_ALL,
|
---|
1043 | pgmUnlock(pVM), VERR_ACCESS_DENIED);
|
---|
1044 |
|
---|
1045 | /*
|
---|
1046 | * Change the page status.
|
---|
1047 | */
|
---|
1048 | PPGMPAGE pPage;
|
---|
1049 | int rc = pgmPhysGetPageEx(pVM, GCPhysPage, &pPage);
|
---|
1050 | AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
|
---|
1051 | if (PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
|
---|
1052 | {
|
---|
1053 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
|
---|
1054 | pCur->cTmpOffPages++;
|
---|
1055 | }
|
---|
1056 | pgmUnlock(pVM);
|
---|
1057 | return VINF_SUCCESS;
|
---|
1058 | }
|
---|
1059 | pgmUnlock(pVM);
|
---|
1060 | AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
|
---|
1061 | GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
|
---|
1062 | return VERR_INVALID_PARAMETER;
|
---|
1063 | }
|
---|
1064 | pgmUnlock(pVM);
|
---|
1065 | AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
|
---|
1066 | return VERR_PGM_HANDLER_NOT_FOUND;
|
---|
1067 | }
|
---|
1068 |
|
---|
1069 | #ifndef IEM_VERIFICATION_MODE_FULL
|
---|
1070 |
|
---|
1071 | /**
|
---|
1072 | * Replaces an MMIO page with an MMIO2 page.
|
---|
1073 | *
|
---|
1074 | * This is a worker for IOMMMIOMapMMIO2Page that works in a similar way to
|
---|
1075 | * PGMHandlerPhysicalPageTempOff but for an MMIO page. Since an MMIO page has no
|
---|
1076 | * backing, the caller must provide a replacement page. For various reasons the
|
---|
1077 | * replacement page must be an MMIO2 page.
|
---|
1078 | *
|
---|
1079 | * The caller must do required page table modifications. You can get away
|
---|
1080 | * without making any modifications since it's an MMIO page, the cost is an extra
|
---|
1081 | * \#PF which will the resync the page.
|
---|
1082 | *
|
---|
1083 | * Call PGMHandlerPhysicalReset() to restore the MMIO page.
|
---|
1084 | *
|
---|
1085 | * The caller may still get handler callback even after this call and must be
|
---|
1086 | * able to deal correctly with such calls. The reason for these callbacks are
|
---|
1087 | * either that we're executing in the recompiler (which doesn't know about this
|
---|
1088 | * arrangement) or that we've been restored from saved state (where we won't
|
---|
1089 | * save the change).
|
---|
1090 | *
|
---|
1091 | * @returns VBox status code.
|
---|
1092 | * @param pVM Pointer to the VM.
|
---|
1093 | * @param GCPhys The start address of the access handler. This
|
---|
1094 | * must be a fully page aligned range or we risk
|
---|
1095 | * messing up other handlers installed for the
|
---|
1096 | * start and end pages.
|
---|
1097 | * @param GCPhysPage The physical address of the page to turn off
|
---|
1098 | * access monitoring for.
|
---|
1099 | * @param GCPhysPageRemap The physical address of the MMIO2 page that
|
---|
1100 | * serves as backing memory.
|
---|
1101 | *
|
---|
1102 | * @remark May cause a page pool flush if used on a page that is already
|
---|
1103 | * aliased.
|
---|
1104 | *
|
---|
1105 | * @note This trick does only work reliably if the two pages are never ever
|
---|
1106 | * mapped in the same page table. If they are the page pool code will
|
---|
1107 | * be confused should either of them be flushed. See the special case
|
---|
1108 | * of zero page aliasing mentioned in #3170.
|
---|
1109 | *
|
---|
1110 | */
|
---|
1111 | VMMDECL(int) PGMHandlerPhysicalPageAlias(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage, RTGCPHYS GCPhysPageRemap)
|
---|
1112 | {
|
---|
1113 | /// Assert(!IOMIsLockOwner(pVM)); /* We mustn't own any other locks when calling this */
|
---|
1114 | pgmLock(pVM);
|
---|
1115 |
|
---|
1116 | /*
|
---|
1117 | * Lookup and validate the range.
|
---|
1118 | */
|
---|
1119 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
1120 | if (RT_LIKELY(pCur))
|
---|
1121 | {
|
---|
1122 | if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
|
---|
1123 | && GCPhysPage <= pCur->Core.KeyLast))
|
---|
1124 | {
|
---|
1125 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
|
---|
1126 | AssertReturnStmt(pCurType->enmKind == PGMPHYSHANDLERKIND_MMIO, pgmUnlock(pVM), VERR_ACCESS_DENIED);
|
---|
1127 | AssertReturnStmt(!(pCur->Core.Key & PAGE_OFFSET_MASK), pgmUnlock(pVM), VERR_INVALID_PARAMETER);
|
---|
1128 | AssertReturnStmt((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK, pgmUnlock(pVM), VERR_INVALID_PARAMETER);
|
---|
1129 |
|
---|
1130 | /*
|
---|
1131 | * Get and validate the two pages.
|
---|
1132 | */
|
---|
1133 | PPGMPAGE pPageRemap;
|
---|
1134 | int rc = pgmPhysGetPageEx(pVM, GCPhysPageRemap, &pPageRemap);
|
---|
1135 | AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
|
---|
1136 | AssertMsgReturnStmt(PGM_PAGE_GET_TYPE(pPageRemap) == PGMPAGETYPE_MMIO2,
|
---|
1137 | ("GCPhysPageRemap=%RGp %R[pgmpage]\n", GCPhysPageRemap, pPageRemap),
|
---|
1138 | pgmUnlock(pVM), VERR_PGM_PHYS_NOT_MMIO2);
|
---|
1139 |
|
---|
1140 | PPGMPAGE pPage;
|
---|
1141 | rc = pgmPhysGetPageEx(pVM, GCPhysPage, &pPage);
|
---|
1142 | AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
|
---|
1143 | if (PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO)
|
---|
1144 | {
|
---|
1145 | AssertMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO,
|
---|
1146 | ("GCPhysPage=%RGp %R[pgmpage]\n", GCPhysPage, pPage),
|
---|
1147 | VERR_PGM_PHYS_NOT_MMIO2);
|
---|
1148 | if (PGM_PAGE_GET_HCPHYS(pPage) == PGM_PAGE_GET_HCPHYS(pPageRemap))
|
---|
1149 | {
|
---|
1150 | pgmUnlock(pVM);
|
---|
1151 | return VINF_PGM_HANDLER_ALREADY_ALIASED;
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | /*
|
---|
1155 | * The page is already mapped as some other page, reset it
|
---|
1156 | * to an MMIO/ZERO page before doing the new mapping.
|
---|
1157 | */
|
---|
1158 | Log(("PGMHandlerPhysicalPageAlias: GCPhysPage=%RGp (%R[pgmpage]; %RHp -> %RHp\n",
|
---|
1159 | GCPhysPage, pPage, PGM_PAGE_GET_HCPHYS(pPage), PGM_PAGE_GET_HCPHYS(pPageRemap)));
|
---|
1160 | pgmHandlerPhysicalResetAliasedPage(pVM, pPage, GCPhysPage, false /*fDoAccounting*/);
|
---|
1161 | pCur->cAliasedPages--;
|
---|
1162 | }
|
---|
1163 | Assert(PGM_PAGE_IS_ZERO(pPage));
|
---|
1164 |
|
---|
1165 | /*
|
---|
1166 | * Do the actual remapping here.
|
---|
1167 | * This page now serves as an alias for the backing memory specified.
|
---|
1168 | */
|
---|
1169 | LogFlow(("PGMHandlerPhysicalPageAlias: %RGp (%R[pgmpage]) alias for %RGp (%R[pgmpage])\n",
|
---|
1170 | GCPhysPage, pPage, GCPhysPageRemap, pPageRemap ));
|
---|
1171 | PGM_PAGE_SET_HCPHYS(pVM, pPage, PGM_PAGE_GET_HCPHYS(pPageRemap));
|
---|
1172 | PGM_PAGE_SET_TYPE(pVM, pPage, PGMPAGETYPE_MMIO2_ALIAS_MMIO);
|
---|
1173 | PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
1174 | PGM_PAGE_SET_PAGEID(pVM, pPage, PGM_PAGE_GET_PAGEID(pPageRemap));
|
---|
1175 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
|
---|
1176 | pCur->cAliasedPages++;
|
---|
1177 | Assert(pCur->cAliasedPages <= pCur->cPages);
|
---|
1178 |
|
---|
1179 | /* Flush its TLB entry. */
|
---|
1180 | pgmPhysInvalidatePageMapTLBEntry(pVM, GCPhysPage);
|
---|
1181 |
|
---|
1182 | LogFlow(("PGMHandlerPhysicalPageAlias: => %R[pgmpage]\n", pPage));
|
---|
1183 | pgmUnlock(pVM);
|
---|
1184 | return VINF_SUCCESS;
|
---|
1185 | }
|
---|
1186 |
|
---|
1187 | pgmUnlock(pVM);
|
---|
1188 | AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
|
---|
1189 | GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
|
---|
1190 | return VERR_INVALID_PARAMETER;
|
---|
1191 | }
|
---|
1192 |
|
---|
1193 | pgmUnlock(pVM);
|
---|
1194 | AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
|
---|
1195 | return VERR_PGM_HANDLER_NOT_FOUND;
|
---|
1196 | }
|
---|
1197 |
|
---|
1198 |
|
---|
1199 | /**
|
---|
1200 | * Replaces an MMIO page with an arbitrary HC page in the shadow page tables.
|
---|
1201 | *
|
---|
1202 | * This differs from PGMHandlerPhysicalPageAlias in that the page doesn't need
|
---|
1203 | * to be a known MMIO2 page and that only shadow paging may access the page.
|
---|
1204 | * The latter distinction is important because the only use for this feature is
|
---|
1205 | * for mapping the special APIC access page that VT-x uses to detect APIC MMIO
|
---|
1206 | * operations, the page is shared between all guest CPUs and actually not
|
---|
1207 | * written to. At least at the moment.
|
---|
1208 | *
|
---|
1209 | * The caller must do required page table modifications. You can get away
|
---|
1210 | * without making any modifications since it's an MMIO page, the cost is an extra
|
---|
1211 | * \#PF which will the resync the page.
|
---|
1212 | *
|
---|
1213 | * Call PGMHandlerPhysicalReset() to restore the MMIO page.
|
---|
1214 | *
|
---|
1215 | *
|
---|
1216 | * @returns VBox status code.
|
---|
1217 | * @param pVM Pointer to the VM.
|
---|
1218 | * @param GCPhys The start address of the access handler. This
|
---|
1219 | * must be a fully page aligned range or we risk
|
---|
1220 | * messing up other handlers installed for the
|
---|
1221 | * start and end pages.
|
---|
1222 | * @param GCPhysPage The physical address of the page to turn off
|
---|
1223 | * access monitoring for.
|
---|
1224 | * @param HCPhysPageRemap The physical address of the HC page that
|
---|
1225 | * serves as backing memory.
|
---|
1226 | *
|
---|
1227 | * @remark May cause a page pool flush if used on a page that is already
|
---|
1228 | * aliased.
|
---|
1229 | */
|
---|
1230 | VMMDECL(int) PGMHandlerPhysicalPageAliasHC(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage, RTHCPHYS HCPhysPageRemap)
|
---|
1231 | {
|
---|
1232 | /// Assert(!IOMIsLockOwner(pVM)); /* We mustn't own any other locks when calling this */
|
---|
1233 | pgmLock(pVM);
|
---|
1234 |
|
---|
1235 | /*
|
---|
1236 | * Lookup and validate the range.
|
---|
1237 | */
|
---|
1238 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
1239 | if (RT_LIKELY(pCur))
|
---|
1240 | {
|
---|
1241 | if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
|
---|
1242 | && GCPhysPage <= pCur->Core.KeyLast))
|
---|
1243 | {
|
---|
1244 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
|
---|
1245 | AssertReturnStmt(pCurType->enmKind == PGMPHYSHANDLERKIND_MMIO, pgmUnlock(pVM), VERR_ACCESS_DENIED);
|
---|
1246 | AssertReturnStmt(!(pCur->Core.Key & PAGE_OFFSET_MASK), pgmUnlock(pVM), VERR_INVALID_PARAMETER);
|
---|
1247 | AssertReturnStmt((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK, pgmUnlock(pVM), VERR_INVALID_PARAMETER);
|
---|
1248 |
|
---|
1249 | /*
|
---|
1250 | * Get and validate the pages.
|
---|
1251 | */
|
---|
1252 | PPGMPAGE pPage;
|
---|
1253 | int rc = pgmPhysGetPageEx(pVM, GCPhysPage, &pPage);
|
---|
1254 | AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
|
---|
1255 | if (PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO)
|
---|
1256 | {
|
---|
1257 | pgmUnlock(pVM);
|
---|
1258 | AssertMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_SPECIAL_ALIAS_MMIO,
|
---|
1259 | ("GCPhysPage=%RGp %R[pgmpage]\n", GCPhysPage, pPage),
|
---|
1260 | VERR_PGM_PHYS_NOT_MMIO2);
|
---|
1261 | return VINF_PGM_HANDLER_ALREADY_ALIASED;
|
---|
1262 | }
|
---|
1263 | Assert(PGM_PAGE_IS_ZERO(pPage));
|
---|
1264 |
|
---|
1265 | /*
|
---|
1266 | * Do the actual remapping here.
|
---|
1267 | * This page now serves as an alias for the backing memory
|
---|
1268 | * specified as far as shadow paging is concerned.
|
---|
1269 | */
|
---|
1270 | LogFlow(("PGMHandlerPhysicalPageAlias: %RGp (%R[pgmpage]) alias for %RHp\n",
|
---|
1271 | GCPhysPage, pPage, HCPhysPageRemap));
|
---|
1272 | PGM_PAGE_SET_HCPHYS(pVM, pPage, HCPhysPageRemap);
|
---|
1273 | PGM_PAGE_SET_TYPE(pVM, pPage, PGMPAGETYPE_SPECIAL_ALIAS_MMIO);
|
---|
1274 | PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_ALLOCATED);
|
---|
1275 | PGM_PAGE_SET_PAGEID(pVM, pPage, NIL_GMM_PAGEID);
|
---|
1276 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
|
---|
1277 | pCur->cAliasedPages++;
|
---|
1278 | Assert(pCur->cAliasedPages <= pCur->cPages);
|
---|
1279 |
|
---|
1280 | /* Flush its TLB entry. */
|
---|
1281 | pgmPhysInvalidatePageMapTLBEntry(pVM, GCPhysPage);
|
---|
1282 |
|
---|
1283 | LogFlow(("PGMHandlerPhysicalPageAliasHC: => %R[pgmpage]\n", pPage));
|
---|
1284 | pgmUnlock(pVM);
|
---|
1285 | return VINF_SUCCESS;
|
---|
1286 | }
|
---|
1287 | pgmUnlock(pVM);
|
---|
1288 | AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
|
---|
1289 | GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
|
---|
1290 | return VERR_INVALID_PARAMETER;
|
---|
1291 | }
|
---|
1292 | pgmUnlock(pVM);
|
---|
1293 |
|
---|
1294 | AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
|
---|
1295 | return VERR_PGM_HANDLER_NOT_FOUND;
|
---|
1296 | }
|
---|
1297 |
|
---|
1298 | #endif /* !IEM_VERIFICATION_MODE_FULL */
|
---|
1299 |
|
---|
1300 | /**
|
---|
1301 | * Checks if a physical range is handled
|
---|
1302 | *
|
---|
1303 | * @returns boolean
|
---|
1304 | * @param pVM Pointer to the VM.
|
---|
1305 | * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
|
---|
1306 | * @remarks Caller must take the PGM lock...
|
---|
1307 | * @thread EMT.
|
---|
1308 | */
|
---|
1309 | VMMDECL(bool) PGMHandlerPhysicalIsRegistered(PVM pVM, RTGCPHYS GCPhys)
|
---|
1310 | {
|
---|
1311 | /*
|
---|
1312 | * Find the handler.
|
---|
1313 | */
|
---|
1314 | pgmLock(pVM);
|
---|
1315 | PPGMPHYSHANDLER pCur = pgmHandlerPhysicalLookup(pVM, GCPhys);
|
---|
1316 | if (pCur)
|
---|
1317 | {
|
---|
1318 | #ifdef VBOX_STRICT
|
---|
1319 | Assert(GCPhys >= pCur->Core.Key && GCPhys <= pCur->Core.KeyLast);
|
---|
1320 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
|
---|
1321 | Assert( pCurType->enmKind == PGMPHYSHANDLERKIND_WRITE
|
---|
1322 | || pCurType->enmKind == PGMPHYSHANDLERKIND_ALL
|
---|
1323 | || pCurType->enmKind == PGMPHYSHANDLERKIND_MMIO);
|
---|
1324 | #endif
|
---|
1325 | pgmUnlock(pVM);
|
---|
1326 | return true;
|
---|
1327 | }
|
---|
1328 | pgmUnlock(pVM);
|
---|
1329 | return false;
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 |
|
---|
1333 | /**
|
---|
1334 | * Checks if it's an disabled all access handler or write access handler at the
|
---|
1335 | * given address.
|
---|
1336 | *
|
---|
1337 | * @returns true if it's an all access handler, false if it's a write access
|
---|
1338 | * handler.
|
---|
1339 | * @param pVM Pointer to the VM.
|
---|
1340 | * @param GCPhys The address of the page with a disabled handler.
|
---|
1341 | *
|
---|
1342 | * @remarks The caller, PGMR3PhysTlbGCPhys2Ptr, must hold the PGM lock.
|
---|
1343 | */
|
---|
1344 | bool pgmHandlerPhysicalIsAll(PVM pVM, RTGCPHYS GCPhys)
|
---|
1345 | {
|
---|
1346 | pgmLock(pVM);
|
---|
1347 | PPGMPHYSHANDLER pCur = pgmHandlerPhysicalLookup(pVM, GCPhys);
|
---|
1348 | if (!pCur)
|
---|
1349 | {
|
---|
1350 | pgmUnlock(pVM);
|
---|
1351 | AssertFailed();
|
---|
1352 | return true;
|
---|
1353 | }
|
---|
1354 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
|
---|
1355 | Assert( pCurType->enmKind == PGMPHYSHANDLERKIND_WRITE
|
---|
1356 | || pCurType->enmKind == PGMPHYSHANDLERKIND_ALL
|
---|
1357 | || pCurType->enmKind == PGMPHYSHANDLERKIND_MMIO); /* sanity */
|
---|
1358 | /* Only whole pages can be disabled. */
|
---|
1359 | Assert( pCur->Core.Key <= (GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK)
|
---|
1360 | && pCur->Core.KeyLast >= (GCPhys | PAGE_OFFSET_MASK));
|
---|
1361 |
|
---|
1362 | bool bRet = pCurType->enmKind != PGMPHYSHANDLERKIND_WRITE;
|
---|
1363 | pgmUnlock(pVM);
|
---|
1364 | return bRet;
|
---|
1365 | }
|
---|
1366 |
|
---|
1367 |
|
---|
1368 | /**
|
---|
1369 | * Internal worker for releasing a virtual handler type registration reference.
|
---|
1370 | *
|
---|
1371 | * @returns New reference count. UINT32_MAX if invalid input (asserted).
|
---|
1372 | * @param pVM Pointer to the cross context VM structure.
|
---|
1373 | * @param pType Pointer to the type registration.
|
---|
1374 | */
|
---|
1375 | DECLINLINE(uint32_t) pgmHandlerVirtualTypeRelease(PVM pVM, PPGMVIRTHANDLERTYPEINT pType)
|
---|
1376 | {
|
---|
1377 | AssertMsgReturn(pType->u32Magic == PGMVIRTHANDLERTYPEINT_MAGIC, ("%#x\n", pType->u32Magic), UINT32_MAX);
|
---|
1378 | uint32_t cRefs = ASMAtomicDecU32(&pType->cRefs);
|
---|
1379 | if (cRefs == 0)
|
---|
1380 | {
|
---|
1381 | pgmLock(pVM);
|
---|
1382 | pType->u32Magic = PGMVIRTHANDLERTYPEINT_MAGIC_DEAD;
|
---|
1383 | RTListOff32NodeRemove(&pType->ListNode);
|
---|
1384 | pgmUnlock(pVM);
|
---|
1385 | MMHyperFree(pVM, pType);
|
---|
1386 | }
|
---|
1387 | return cRefs;
|
---|
1388 | }
|
---|
1389 |
|
---|
1390 |
|
---|
1391 | /**
|
---|
1392 | * Internal worker for retaining a virtual handler type registration reference.
|
---|
1393 | *
|
---|
1394 | * @returns New reference count. UINT32_MAX if invalid input (asserted).
|
---|
1395 | * @param pVM Pointer to the cross context VM structure.
|
---|
1396 | * @param pType Pointer to the type registration.
|
---|
1397 | */
|
---|
1398 | DECLINLINE(uint32_t) pgmHandlerVirtualTypeRetain(PVM pVM, PPGMVIRTHANDLERTYPEINT pType)
|
---|
1399 | {
|
---|
1400 | AssertMsgReturn(pType->u32Magic == PGMVIRTHANDLERTYPEINT_MAGIC, ("%#x\n", pType->u32Magic), UINT32_MAX);
|
---|
1401 | uint32_t cRefs = ASMAtomicIncU32(&pType->cRefs);
|
---|
1402 | Assert(cRefs < _1M && cRefs > 0);
|
---|
1403 | return cRefs;
|
---|
1404 | }
|
---|
1405 |
|
---|
1406 |
|
---|
1407 | /**
|
---|
1408 | * Releases a reference to a virtual handler type registration.
|
---|
1409 | *
|
---|
1410 | * @returns New reference count. UINT32_MAX if invalid input (asserted).
|
---|
1411 | * @param pVM Pointer to the cross context VM structure.
|
---|
1412 | * @param hType The type regiration handle.
|
---|
1413 | */
|
---|
1414 | VMM_INT_DECL(uint32_t) PGMHandlerVirtualTypeRelease(PVM pVM, PGMVIRTHANDLERTYPE hType)
|
---|
1415 | {
|
---|
1416 | if (hType != NIL_PGMVIRTHANDLERTYPE)
|
---|
1417 | return pgmHandlerVirtualTypeRelease(pVM, PGMVIRTHANDLERTYPEINT_FROM_HANDLE(pVM, hType));
|
---|
1418 | return 0;
|
---|
1419 | }
|
---|
1420 |
|
---|
1421 |
|
---|
1422 | /**
|
---|
1423 | * Retains a reference to a virtual handler type registration.
|
---|
1424 | *
|
---|
1425 | * @returns New reference count. UINT32_MAX if invalid input (asserted).
|
---|
1426 | * @param pVM Pointer to the cross context VM structure.
|
---|
1427 | * @param hType The type regiration handle.
|
---|
1428 | */
|
---|
1429 | VMM_INT_DECL(uint32_t) PGMHandlerVirtualTypeRetain(PVM pVM, PGMVIRTHANDLERTYPE hType)
|
---|
1430 | {
|
---|
1431 | return pgmHandlerVirtualTypeRetain(pVM, PGMVIRTHANDLERTYPEINT_FROM_HANDLE(pVM, hType));
|
---|
1432 | }
|
---|
1433 |
|
---|
1434 |
|
---|
1435 | /**
|
---|
1436 | * Check if particular guest's VA is being monitored.
|
---|
1437 | *
|
---|
1438 | * @returns true or false
|
---|
1439 | * @param pVM Pointer to the VM.
|
---|
1440 | * @param GCPtr Virtual address.
|
---|
1441 | * @remarks Will acquire the PGM lock.
|
---|
1442 | * @thread Any.
|
---|
1443 | */
|
---|
1444 | VMM_INT_DECL(bool) PGMHandlerVirtualIsRegistered(PVM pVM, RTGCPTR GCPtr)
|
---|
1445 | {
|
---|
1446 | pgmLock(pVM);
|
---|
1447 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGet(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, GCPtr);
|
---|
1448 | pgmUnlock(pVM);
|
---|
1449 |
|
---|
1450 | return pCur != NULL;
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 |
|
---|
1454 | /**
|
---|
1455 | * Search for virtual handler with matching physical address
|
---|
1456 | *
|
---|
1457 | * @returns Pointer to the virtual handler structure if found, otherwise NULL.
|
---|
1458 | * @param pVM Pointer to the VM.
|
---|
1459 | * @param GCPhys GC physical address to search for.
|
---|
1460 | * @param piPage Where to store the pointer to the index of the cached physical page.
|
---|
1461 | */
|
---|
1462 | PPGMVIRTHANDLER pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, unsigned *piPage)
|
---|
1463 | {
|
---|
1464 | STAM_PROFILE_START(&pVM->pgm.s.CTX_MID_Z(Stat,VirtHandlerSearchByPhys), a);
|
---|
1465 |
|
---|
1466 | pgmLock(pVM);
|
---|
1467 | PPGMPHYS2VIRTHANDLER pCur;
|
---|
1468 | pCur = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, GCPhys);
|
---|
1469 | if (pCur)
|
---|
1470 | {
|
---|
1471 | /* found a match! */
|
---|
1472 | PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
|
---|
1473 | *piPage = pCur - &pVirt->aPhysToVirt[0];
|
---|
1474 | pgmUnlock(pVM);
|
---|
1475 |
|
---|
1476 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
1477 | AssertRelease(pCur->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD);
|
---|
1478 | #endif
|
---|
1479 | LogFlow(("PHYS2VIRT: found match for %RGp -> %RGv *piPage=%#x\n", GCPhys, pVirt->Core.Key, *piPage));
|
---|
1480 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,VirtHandlerSearchByPhys), a);
|
---|
1481 | return pVirt;
|
---|
1482 | }
|
---|
1483 |
|
---|
1484 | pgmUnlock(pVM);
|
---|
1485 | STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,VirtHandlerSearchByPhys), a);
|
---|
1486 | return NULL;
|
---|
1487 | }
|
---|
1488 |
|
---|
1489 |
|
---|
1490 | /**
|
---|
1491 | * Deal with aliases in phys2virt.
|
---|
1492 | *
|
---|
1493 | * As pointed out by the various todos, this currently only deals with
|
---|
1494 | * aliases where the two ranges match 100%.
|
---|
1495 | *
|
---|
1496 | * @param pVM Pointer to the VM.
|
---|
1497 | * @param pPhys2Virt The node we failed insert.
|
---|
1498 | */
|
---|
1499 | static void pgmHandlerVirtualInsertAliased(PVM pVM, PPGMPHYS2VIRTHANDLER pPhys2Virt)
|
---|
1500 | {
|
---|
1501 | /*
|
---|
1502 | * First find the node which is conflicting with us.
|
---|
1503 | */
|
---|
1504 | /** @todo Deal with partial overlapping. (Unlikely situation, so I'm too lazy to do anything about it now.) */
|
---|
1505 | /** @todo check if the current head node covers the ground we do. This is highly unlikely
|
---|
1506 | * and I'm too lazy to implement this now as it will require sorting the list and stuff like that. */
|
---|
1507 | PPGMPHYS2VIRTHANDLER pHead = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
|
---|
1508 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
1509 | AssertReleaseMsg(pHead != pPhys2Virt, ("%RGp-%RGp offVirtHandler=%#RX32\n",
|
---|
1510 | pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler));
|
---|
1511 | #endif
|
---|
1512 | if (RT_UNLIKELY(!pHead || pHead->Core.KeyLast != pPhys2Virt->Core.KeyLast))
|
---|
1513 | {
|
---|
1514 | /** @todo do something clever here... */
|
---|
1515 | LogRel(("pgmHandlerVirtualInsertAliased: %RGp-%RGp\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
|
---|
1516 | pPhys2Virt->offNextAlias = 0;
|
---|
1517 | return;
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | /*
|
---|
1521 | * Insert ourselves as the next node.
|
---|
1522 | */
|
---|
1523 | if (!(pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
|
---|
1524 | pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IN_TREE;
|
---|
1525 | else
|
---|
1526 | {
|
---|
1527 | PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pHead + (pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
|
---|
1528 | pPhys2Virt->offNextAlias = ((intptr_t)pNext - (intptr_t)pPhys2Virt)
|
---|
1529 | | PGMPHYS2VIRTHANDLER_IN_TREE;
|
---|
1530 | }
|
---|
1531 | pHead->offNextAlias = ((intptr_t)pPhys2Virt - (intptr_t)pHead)
|
---|
1532 | | (pHead->offNextAlias & ~PGMPHYS2VIRTHANDLER_OFF_MASK);
|
---|
1533 | Log(("pgmHandlerVirtualInsertAliased: %RGp-%RGp offNextAlias=%#RX32\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
|
---|
1534 | }
|
---|
1535 |
|
---|
1536 |
|
---|
1537 | /**
|
---|
1538 | * Resets one virtual handler range.
|
---|
1539 | *
|
---|
1540 | * This is called by HandlerVirtualUpdate when it has detected some kind of
|
---|
1541 | * problem and have started clearing the virtual handler page states (or
|
---|
1542 | * when there have been registration/deregistrations). For this reason this
|
---|
1543 | * function will only update the page status if it's lower than desired.
|
---|
1544 | *
|
---|
1545 | * @returns 0
|
---|
1546 | * @param pNode Pointer to a PGMVIRTHANDLER.
|
---|
1547 | * @param pvUser Pointer to the VM.
|
---|
1548 | */
|
---|
1549 | DECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
|
---|
1550 | {
|
---|
1551 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
|
---|
1552 | PVM pVM = (PVM)pvUser;
|
---|
1553 |
|
---|
1554 | PGM_LOCK_ASSERT_OWNER(pVM);
|
---|
1555 |
|
---|
1556 | /*
|
---|
1557 | * Iterate the pages and apply the new state.
|
---|
1558 | */
|
---|
1559 | uint32_t uState = PGMVIRTANDLER_GET_TYPE(pVM, pCur)->uState;
|
---|
1560 | PPGMRAMRANGE pRamHint = NULL;
|
---|
1561 | RTGCUINTPTR offPage = ((RTGCUINTPTR)pCur->Core.Key & PAGE_OFFSET_MASK);
|
---|
1562 | RTGCUINTPTR cbLeft = pCur->cb;
|
---|
1563 | for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
|
---|
1564 | {
|
---|
1565 | PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
|
---|
1566 | if (pPhys2Virt->Core.Key != NIL_RTGCPHYS)
|
---|
1567 | {
|
---|
1568 | /*
|
---|
1569 | * Update the page state wrt virtual handlers.
|
---|
1570 | */
|
---|
1571 | PPGMPAGE pPage;
|
---|
1572 | int rc = pgmPhysGetPageWithHintEx(pVM, pPhys2Virt->Core.Key, &pPage, &pRamHint);
|
---|
1573 | if ( RT_SUCCESS(rc)
|
---|
1574 | && PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
|
---|
1575 | PGM_PAGE_SET_HNDL_VIRT_STATE(pPage, uState);
|
---|
1576 | else
|
---|
1577 | AssertRC(rc);
|
---|
1578 |
|
---|
1579 | /*
|
---|
1580 | * Need to insert the page in the Phys2Virt lookup tree?
|
---|
1581 | */
|
---|
1582 | if (pPhys2Virt->Core.KeyLast == NIL_RTGCPHYS)
|
---|
1583 | {
|
---|
1584 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
1585 | AssertRelease(!pPhys2Virt->offNextAlias);
|
---|
1586 | #endif
|
---|
1587 | unsigned cbPhys = cbLeft;
|
---|
1588 | if (cbPhys > PAGE_SIZE - offPage)
|
---|
1589 | cbPhys = PAGE_SIZE - offPage;
|
---|
1590 | else
|
---|
1591 | Assert(iPage == pCur->cPages - 1);
|
---|
1592 | pPhys2Virt->Core.KeyLast = pPhys2Virt->Core.Key + cbPhys - 1; /* inclusive */
|
---|
1593 | pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IS_HEAD | PGMPHYS2VIRTHANDLER_IN_TREE;
|
---|
1594 | if (!RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, &pPhys2Virt->Core))
|
---|
1595 | pgmHandlerVirtualInsertAliased(pVM, pPhys2Virt);
|
---|
1596 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
1597 | else
|
---|
1598 | AssertReleaseMsg(RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key) == &pPhys2Virt->Core,
|
---|
1599 | ("%RGp-%RGp offNextAlias=%#RX32\n",
|
---|
1600 | pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
|
---|
1601 | #endif
|
---|
1602 | Log2(("PHYS2VIRT: Insert physical range %RGp-%RGp offNextAlias=%#RX32 %s\n",
|
---|
1603 | pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
|
---|
1604 | }
|
---|
1605 | }
|
---|
1606 | cbLeft -= PAGE_SIZE - offPage;
|
---|
1607 | offPage = 0;
|
---|
1608 | }
|
---|
1609 |
|
---|
1610 | return 0;
|
---|
1611 | }
|
---|
1612 |
|
---|
1613 | #if defined(VBOX_STRICT) || defined(LOG_ENABLED)
|
---|
1614 |
|
---|
1615 | /**
|
---|
1616 | * Worker for pgmHandlerVirtualDumpPhysPages.
|
---|
1617 | *
|
---|
1618 | * @returns 0 (continue enumeration).
|
---|
1619 | * @param pNode The virtual handler node.
|
---|
1620 | * @param pvUser User argument, unused.
|
---|
1621 | */
|
---|
1622 | static DECLCALLBACK(int) pgmHandlerVirtualDumpPhysPagesCallback(PAVLROGCPHYSNODECORE pNode, void *pvUser)
|
---|
1623 | {
|
---|
1624 | PPGMPHYS2VIRTHANDLER pCur = (PPGMPHYS2VIRTHANDLER)pNode;
|
---|
1625 | PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
|
---|
1626 | NOREF(pvUser); NOREF(pVirt);
|
---|
1627 |
|
---|
1628 | Log(("PHYS2VIRT: Range %RGp-%RGp for virtual handler: %s\n", pCur->Core.Key, pCur->Core.KeyLast, pVirt->pszDesc));
|
---|
1629 | return 0;
|
---|
1630 | }
|
---|
1631 |
|
---|
1632 |
|
---|
1633 | /**
|
---|
1634 | * Assertion / logging helper for dumping all the
|
---|
1635 | * virtual handlers to the log.
|
---|
1636 | *
|
---|
1637 | * @param pVM Pointer to the VM.
|
---|
1638 | */
|
---|
1639 | void pgmHandlerVirtualDumpPhysPages(PVM pVM)
|
---|
1640 | {
|
---|
1641 | RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, true /* from left */,
|
---|
1642 | pgmHandlerVirtualDumpPhysPagesCallback, 0);
|
---|
1643 | }
|
---|
1644 |
|
---|
1645 | #endif /* VBOX_STRICT || LOG_ENABLED */
|
---|
1646 | #ifdef VBOX_STRICT
|
---|
1647 |
|
---|
1648 | /**
|
---|
1649 | * State structure used by the PGMAssertHandlerAndFlagsInSync() function
|
---|
1650 | * and its AVL enumerators.
|
---|
1651 | */
|
---|
1652 | typedef struct PGMAHAFIS
|
---|
1653 | {
|
---|
1654 | /** The current physical address. */
|
---|
1655 | RTGCPHYS GCPhys;
|
---|
1656 | /** The state we've calculated. */
|
---|
1657 | unsigned uVirtStateFound;
|
---|
1658 | /** The state we're matching up to. */
|
---|
1659 | unsigned uVirtState;
|
---|
1660 | /** Number of errors. */
|
---|
1661 | unsigned cErrors;
|
---|
1662 | /** Pointer to the VM. */
|
---|
1663 | PVM pVM;
|
---|
1664 | } PGMAHAFIS, *PPGMAHAFIS;
|
---|
1665 |
|
---|
1666 |
|
---|
1667 | #if 0 /* unused */
|
---|
1668 | /**
|
---|
1669 | * Verify virtual handler by matching physical address.
|
---|
1670 | *
|
---|
1671 | * @returns 0
|
---|
1672 | * @param pNode Pointer to a PGMVIRTHANDLER.
|
---|
1673 | * @param pvUser Pointer to user parameter.
|
---|
1674 | */
|
---|
1675 | static DECLCALLBACK(int) pgmHandlerVirtualVerifyOneByPhysAddr(PAVLROGCPTRNODECORE pNode, void *pvUser)
|
---|
1676 | {
|
---|
1677 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
|
---|
1678 | PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
|
---|
1679 |
|
---|
1680 | for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
|
---|
1681 | {
|
---|
1682 | if ((pCur->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) == pState->GCPhys)
|
---|
1683 | {
|
---|
1684 | unsigned uState = pgmHandlerVirtualCalcState(pCur);
|
---|
1685 | if (pState->uVirtState < uState)
|
---|
1686 | {
|
---|
1687 | error
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | if (pState->uVirtState == uState)
|
---|
1691 | break; //??
|
---|
1692 | }
|
---|
1693 | }
|
---|
1694 | return 0;
|
---|
1695 | }
|
---|
1696 | #endif /* unused */
|
---|
1697 |
|
---|
1698 |
|
---|
1699 | /**
|
---|
1700 | * Verify a virtual handler (enumeration callback).
|
---|
1701 | *
|
---|
1702 | * Called by PGMAssertHandlerAndFlagsInSync to check the sanity of all
|
---|
1703 | * the virtual handlers, esp. that the physical addresses matches up.
|
---|
1704 | *
|
---|
1705 | * @returns 0
|
---|
1706 | * @param pNode Pointer to a PGMVIRTHANDLER.
|
---|
1707 | * @param pvUser Pointer to a PPGMAHAFIS structure.
|
---|
1708 | */
|
---|
1709 | static DECLCALLBACK(int) pgmHandlerVirtualVerifyOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
|
---|
1710 | {
|
---|
1711 | PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
|
---|
1712 | PVM pVM = pState->pVM;
|
---|
1713 | PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)pNode;
|
---|
1714 | PPGMVIRTHANDLERTYPEINT pType = PGMVIRTANDLER_GET_TYPE(pVM, pVirt);
|
---|
1715 |
|
---|
1716 | /*
|
---|
1717 | * Validate the type and calc state.
|
---|
1718 | */
|
---|
1719 | switch (pType->enmKind)
|
---|
1720 | {
|
---|
1721 | case PGMVIRTHANDLERKIND_WRITE:
|
---|
1722 | case PGMVIRTHANDLERKIND_ALL:
|
---|
1723 | break;
|
---|
1724 | default:
|
---|
1725 | AssertMsgFailed(("unknown/wrong enmKind=%d\n", pType->enmKind));
|
---|
1726 | pState->cErrors++;
|
---|
1727 | return 0;
|
---|
1728 | }
|
---|
1729 | const uint32_t uState = pType->uState;
|
---|
1730 |
|
---|
1731 | /*
|
---|
1732 | * Check key alignment.
|
---|
1733 | */
|
---|
1734 | if ( (pVirt->aPhysToVirt[0].Core.Key & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->Core.Key & PAGE_OFFSET_MASK)
|
---|
1735 | && pVirt->aPhysToVirt[0].Core.Key != NIL_RTGCPHYS)
|
---|
1736 | {
|
---|
1737 | AssertMsgFailed(("virt handler phys has incorrect key! %RGp %RGv %s\n",
|
---|
1738 | pVirt->aPhysToVirt[0].Core.Key, pVirt->Core.Key, R3STRING(pVirt->pszDesc)));
|
---|
1739 | pState->cErrors++;
|
---|
1740 | }
|
---|
1741 |
|
---|
1742 | if ( (pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->Core.KeyLast & PAGE_OFFSET_MASK)
|
---|
1743 | && pVirt->aPhysToVirt[pVirt->cPages - 1].Core.Key != NIL_RTGCPHYS)
|
---|
1744 | {
|
---|
1745 | AssertMsgFailed(("virt handler phys has incorrect key! %RGp %RGv %s\n",
|
---|
1746 | pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast, pVirt->Core.KeyLast, R3STRING(pVirt->pszDesc)));
|
---|
1747 | pState->cErrors++;
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | /*
|
---|
1751 | * Check pages for sanity and state.
|
---|
1752 | */
|
---|
1753 | RTGCUINTPTR GCPtr = (RTGCUINTPTR)pVirt->Core.Key;
|
---|
1754 | for (unsigned iPage = 0; iPage < pVirt->cPages; iPage++, GCPtr += PAGE_SIZE)
|
---|
1755 | {
|
---|
1756 | for (VMCPUID i = 0; i < pVM->cCpus; i++)
|
---|
1757 | {
|
---|
1758 | PVMCPU pVCpu = &pVM->aCpus[i];
|
---|
1759 |
|
---|
1760 | RTGCPHYS GCPhysGst;
|
---|
1761 | uint64_t fGst;
|
---|
1762 | int rc = PGMGstGetPage(pVCpu, (RTGCPTR)GCPtr, &fGst, &GCPhysGst);
|
---|
1763 | if ( rc == VERR_PAGE_NOT_PRESENT
|
---|
1764 | || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
1765 | {
|
---|
1766 | if (pVirt->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
|
---|
1767 | {
|
---|
1768 | AssertMsgFailed(("virt handler phys out of sync. %RGp GCPhysNew=~0 iPage=%#x %RGv %s\n",
|
---|
1769 | pVirt->aPhysToVirt[iPage].Core.Key, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
|
---|
1770 | pState->cErrors++;
|
---|
1771 | }
|
---|
1772 | continue;
|
---|
1773 | }
|
---|
1774 |
|
---|
1775 | AssertRCReturn(rc, 0);
|
---|
1776 | if ((pVirt->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) != GCPhysGst)
|
---|
1777 | {
|
---|
1778 | AssertMsgFailed(("virt handler phys out of sync. %RGp GCPhysGst=%RGp iPage=%#x %RGv %s\n",
|
---|
1779 | pVirt->aPhysToVirt[iPage].Core.Key, GCPhysGst, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
|
---|
1780 | pState->cErrors++;
|
---|
1781 | continue;
|
---|
1782 | }
|
---|
1783 |
|
---|
1784 | PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhysGst);
|
---|
1785 | if (!pPage)
|
---|
1786 | {
|
---|
1787 | AssertMsgFailed(("virt handler getting ram flags. GCPhysGst=%RGp iPage=%#x %RGv %s\n",
|
---|
1788 | GCPhysGst, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
|
---|
1789 | pState->cErrors++;
|
---|
1790 | continue;
|
---|
1791 | }
|
---|
1792 |
|
---|
1793 | if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
|
---|
1794 | {
|
---|
1795 | AssertMsgFailed(("virt handler state mismatch. pPage=%R[pgmpage] GCPhysGst=%RGp iPage=%#x %RGv state=%d expected>=%d %s\n",
|
---|
1796 | pPage, GCPhysGst, iPage, GCPtr, PGM_PAGE_GET_HNDL_VIRT_STATE(pPage), uState, R3STRING(pVirt->pszDesc)));
|
---|
1797 | pState->cErrors++;
|
---|
1798 | continue;
|
---|
1799 | }
|
---|
1800 | } /* for each VCPU */
|
---|
1801 | } /* for pages in virtual mapping. */
|
---|
1802 |
|
---|
1803 | return 0;
|
---|
1804 | }
|
---|
1805 |
|
---|
1806 |
|
---|
1807 | /**
|
---|
1808 | * Asserts that the handlers+guest-page-tables == ramrange-flags and
|
---|
1809 | * that the physical addresses associated with virtual handlers are correct.
|
---|
1810 | *
|
---|
1811 | * @returns Number of mismatches.
|
---|
1812 | * @param pVM Pointer to the VM.
|
---|
1813 | */
|
---|
1814 | VMMDECL(unsigned) PGMAssertHandlerAndFlagsInSync(PVM pVM)
|
---|
1815 | {
|
---|
1816 | PPGM pPGM = &pVM->pgm.s;
|
---|
1817 | PGMAHAFIS State;
|
---|
1818 | State.GCPhys = 0;
|
---|
1819 | State.uVirtState = 0;
|
---|
1820 | State.uVirtStateFound = 0;
|
---|
1821 | State.cErrors = 0;
|
---|
1822 | State.pVM = pVM;
|
---|
1823 |
|
---|
1824 | PGM_LOCK_ASSERT_OWNER(pVM);
|
---|
1825 |
|
---|
1826 | /*
|
---|
1827 | * Check the RAM flags against the handlers.
|
---|
1828 | */
|
---|
1829 | for (PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRangesX); pRam; pRam = pRam->CTX_SUFF(pNext))
|
---|
1830 | {
|
---|
1831 | const uint32_t cPages = pRam->cb >> PAGE_SHIFT;
|
---|
1832 | for (uint32_t iPage = 0; iPage < cPages; iPage++)
|
---|
1833 | {
|
---|
1834 | PGMPAGE const *pPage = &pRam->aPages[iPage];
|
---|
1835 | if (PGM_PAGE_HAS_ANY_HANDLERS(pPage))
|
---|
1836 | {
|
---|
1837 | State.GCPhys = pRam->GCPhys + (iPage << PAGE_SHIFT);
|
---|
1838 |
|
---|
1839 | /*
|
---|
1840 | * Physical first - calculate the state based on the handlers
|
---|
1841 | * active on the page, then compare.
|
---|
1842 | */
|
---|
1843 | if (PGM_PAGE_HAS_ANY_PHYSICAL_HANDLERS(pPage))
|
---|
1844 | {
|
---|
1845 | /* the first */
|
---|
1846 | PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys);
|
---|
1847 | if (!pPhys)
|
---|
1848 | {
|
---|
1849 | pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys, true);
|
---|
1850 | if ( pPhys
|
---|
1851 | && pPhys->Core.Key > (State.GCPhys + PAGE_SIZE - 1))
|
---|
1852 | pPhys = NULL;
|
---|
1853 | Assert(!pPhys || pPhys->Core.Key >= State.GCPhys);
|
---|
1854 | }
|
---|
1855 | if (pPhys)
|
---|
1856 | {
|
---|
1857 | PPGMPHYSHANDLERTYPEINT pPhysType = (PPGMPHYSHANDLERTYPEINT)MMHyperHeapOffsetToPtr(pVM, pPhys->hType);
|
---|
1858 | unsigned uState = pPhysType->uState;
|
---|
1859 |
|
---|
1860 | /* more? */
|
---|
1861 | while (pPhys->Core.KeyLast < (State.GCPhys | PAGE_OFFSET_MASK))
|
---|
1862 | {
|
---|
1863 | PPGMPHYSHANDLER pPhys2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers,
|
---|
1864 | pPhys->Core.KeyLast + 1, true);
|
---|
1865 | if ( !pPhys2
|
---|
1866 | || pPhys2->Core.Key > (State.GCPhys | PAGE_OFFSET_MASK))
|
---|
1867 | break;
|
---|
1868 | PPGMPHYSHANDLERTYPEINT pPhysType2 = (PPGMPHYSHANDLERTYPEINT)MMHyperHeapOffsetToPtr(pVM, pPhys2->hType);
|
---|
1869 | uState = RT_MAX(uState, pPhysType2->uState);
|
---|
1870 | pPhys = pPhys2;
|
---|
1871 | }
|
---|
1872 |
|
---|
1873 | /* compare.*/
|
---|
1874 | if ( PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != uState
|
---|
1875 | && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
|
---|
1876 | {
|
---|
1877 | AssertMsgFailed(("ram range vs phys handler flags mismatch. GCPhys=%RGp state=%d expected=%d %s\n",
|
---|
1878 | State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), uState, pPhysType->pszDesc));
|
---|
1879 | State.cErrors++;
|
---|
1880 | }
|
---|
1881 |
|
---|
1882 | #ifdef VBOX_WITH_REM
|
---|
1883 | # ifdef IN_RING3
|
---|
1884 | /* validate that REM is handling it. */
|
---|
1885 | if ( !REMR3IsPageAccessHandled(pVM, State.GCPhys)
|
---|
1886 | /* ignore shadowed ROM for the time being. */
|
---|
1887 | && PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_ROM_SHADOW)
|
---|
1888 | {
|
---|
1889 | AssertMsgFailed(("ram range vs phys handler REM mismatch. GCPhys=%RGp state=%d %s\n",
|
---|
1890 | State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), pPhysType->pszDesc));
|
---|
1891 | State.cErrors++;
|
---|
1892 | }
|
---|
1893 | # endif
|
---|
1894 | #endif
|
---|
1895 | }
|
---|
1896 | else
|
---|
1897 | {
|
---|
1898 | AssertMsgFailed(("ram range vs phys handler mismatch. no handler for GCPhys=%RGp\n", State.GCPhys));
|
---|
1899 | State.cErrors++;
|
---|
1900 | }
|
---|
1901 | }
|
---|
1902 |
|
---|
1903 | /*
|
---|
1904 | * Virtual handlers.
|
---|
1905 | */
|
---|
1906 | if (PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage))
|
---|
1907 | {
|
---|
1908 | State.uVirtState = PGM_PAGE_GET_HNDL_VIRT_STATE(pPage);
|
---|
1909 | #if 1
|
---|
1910 | /* locate all the matching physical ranges. */
|
---|
1911 | State.uVirtStateFound = PGM_PAGE_HNDL_VIRT_STATE_NONE;
|
---|
1912 | RTGCPHYS GCPhysKey = State.GCPhys;
|
---|
1913 | for (;;)
|
---|
1914 | {
|
---|
1915 | PPGMPHYS2VIRTHANDLER pPhys2Virt = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
|
---|
1916 | GCPhysKey, true /* above-or-equal */);
|
---|
1917 | if ( !pPhys2Virt
|
---|
1918 | || (pPhys2Virt->Core.Key & X86_PTE_PAE_PG_MASK) != State.GCPhys)
|
---|
1919 | break;
|
---|
1920 |
|
---|
1921 | /* the head */
|
---|
1922 | GCPhysKey = pPhys2Virt->Core.KeyLast;
|
---|
1923 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
|
---|
1924 | unsigned uState = PGMVIRTANDLER_GET_TYPE(pVM, pCur)->uState;
|
---|
1925 | State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
|
---|
1926 |
|
---|
1927 | /* any aliases */
|
---|
1928 | while (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK)
|
---|
1929 | {
|
---|
1930 | pPhys2Virt = (PPGMPHYS2VIRTHANDLER)((uintptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
|
---|
1931 | pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
|
---|
1932 | uState = PGMVIRTANDLER_GET_TYPE(pVM, pCur)->uState;
|
---|
1933 | State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
|
---|
1934 | }
|
---|
1935 |
|
---|
1936 | /* done? */
|
---|
1937 | if ((GCPhysKey & X86_PTE_PAE_PG_MASK) != State.GCPhys)
|
---|
1938 | break;
|
---|
1939 | }
|
---|
1940 | #else
|
---|
1941 | /* very slow */
|
---|
1942 | RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOneByPhysAddr, &State);
|
---|
1943 | #endif
|
---|
1944 | if (State.uVirtState != State.uVirtStateFound)
|
---|
1945 | {
|
---|
1946 | AssertMsgFailed(("ram range vs virt handler flags mismatch. GCPhys=%RGp uVirtState=%#x uVirtStateFound=%#x\n",
|
---|
1947 | State.GCPhys, State.uVirtState, State.uVirtStateFound));
|
---|
1948 | State.cErrors++;
|
---|
1949 | }
|
---|
1950 | }
|
---|
1951 | }
|
---|
1952 | } /* foreach page in ram range. */
|
---|
1953 | } /* foreach ram range. */
|
---|
1954 |
|
---|
1955 | /*
|
---|
1956 | * Check that the physical addresses of the virtual handlers matches up
|
---|
1957 | * and that they are otherwise sane.
|
---|
1958 | */
|
---|
1959 | RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOne, &State);
|
---|
1960 |
|
---|
1961 | /*
|
---|
1962 | * Do the reverse check for physical handlers.
|
---|
1963 | */
|
---|
1964 | /** @todo */
|
---|
1965 |
|
---|
1966 | return State.cErrors;
|
---|
1967 | }
|
---|
1968 |
|
---|
1969 | #endif /* VBOX_STRICT */
|
---|
1970 |
|
---|