1 | /* $Id: PGMAllHandler.cpp 13062 2008-10-08 08:06:56Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PGM - Page Manager / Monitor, Access Handlers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 | #define LOG_GROUP LOG_GROUP_PGM
|
---|
27 | #include <VBox/dbgf.h>
|
---|
28 | #include <VBox/pgm.h>
|
---|
29 | #include <VBox/iom.h>
|
---|
30 | #include <VBox/mm.h>
|
---|
31 | #include <VBox/em.h>
|
---|
32 | #include <VBox/stam.h>
|
---|
33 | #include <VBox/rem.h>
|
---|
34 | #include <VBox/dbgf.h>
|
---|
35 | #include <VBox/rem.h>
|
---|
36 | #include "PGMInternal.h"
|
---|
37 | #include <VBox/vm.h>
|
---|
38 |
|
---|
39 | #include <VBox/log.h>
|
---|
40 | #include <iprt/assert.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include <VBox/param.h>
|
---|
44 | #include <VBox/err.h>
|
---|
45 | #include <VBox/selm.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Internal Functions *
|
---|
50 | *******************************************************************************/
|
---|
51 | static int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVM pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam);
|
---|
52 | static void pgmHandlerPhysicalDeregisterNotifyREM(PVM pVM, PPGMPHYSHANDLER pCur);
|
---|
53 | static void pgmHandlerPhysicalResetRamFlags(PVM pVM, PPGMPHYSHANDLER pCur);
|
---|
54 |
|
---|
55 |
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Register a access handler for a physical range.
|
---|
59 | *
|
---|
60 | * @returns VBox status code.
|
---|
61 | * @retval VINF_SUCCESS when successfully installed.
|
---|
62 | * @retval VINF_PGM_GCPHYS_ALIASED when the shadow PTs could be updated because
|
---|
63 | * the guest page aliased or/and mapped by multiple PTs. A CR3 sync has been
|
---|
64 | * flagged together with a pool clearing.
|
---|
65 | * @retval VERR_PGM_HANDLER_PHYSICAL_CONFLICT if the range conflicts with an existing
|
---|
66 | * one. A debug assertion is raised.
|
---|
67 | *
|
---|
68 | * @param pVM VM Handle.
|
---|
69 | * @param enmType Handler type. Any of the PGMPHYSHANDLERTYPE_PHYSICAL* enums.
|
---|
70 | * @param GCPhys Start physical address.
|
---|
71 | * @param GCPhysLast Last physical address. (inclusive)
|
---|
72 | * @param pfnHandlerR3 The R3 handler.
|
---|
73 | * @param pvUserR3 User argument to the R3 handler.
|
---|
74 | * @param pfnHandlerR0 The R0 handler.
|
---|
75 | * @param pvUserR0 User argument to the R0 handler.
|
---|
76 | * @param pfnHandlerGC The RC handler.
|
---|
77 | * @param pvUserRC User argument to the RC handler. This can be a value
|
---|
78 | * less that 0x10000 or a (non-null) pointer that is
|
---|
79 | * automatically relocatated.
|
---|
80 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
81 | */
|
---|
82 | VMMDECL(int) PGMHandlerPhysicalRegisterEx(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast,
|
---|
83 | R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3, RTR3PTR pvUserR3,
|
---|
84 | R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0, RTR0PTR pvUserR0,
|
---|
85 | RCPTRTYPE(PFNPGMRCPHYSHANDLER) pfnHandlerRC, RTRCPTR pvUserRC,
|
---|
86 | R3PTRTYPE(const char *) pszDesc)
|
---|
87 | {
|
---|
88 | Log(("PGMHandlerPhysicalRegisterEx: enmType=%d GCPhys=%RGp GCPhysLast=%RGp pfnHandlerR3=%RHv pvUserR3=%RHv pfnHandlerR0=%RHv pvUserR0=%RHv pfnHandlerGC=%RRv pvUserGC=%RRv pszDesc=%s\n",
|
---|
89 | enmType, GCPhys, GCPhysLast, pfnHandlerR3, pvUserR3, pfnHandlerR0, pvUserR0, pfnHandlerRC, pvUserRC, R3STRING(pszDesc)));
|
---|
90 |
|
---|
91 | /*
|
---|
92 | * Validate input.
|
---|
93 | */
|
---|
94 | if (GCPhys >= GCPhysLast)
|
---|
95 | {
|
---|
96 | AssertMsgFailed(("GCPhys >= GCPhysLast (%#x >= %#x)\n", GCPhys, GCPhysLast));
|
---|
97 | return VERR_INVALID_PARAMETER;
|
---|
98 | }
|
---|
99 | switch (enmType)
|
---|
100 | {
|
---|
101 | case PGMPHYSHANDLERTYPE_MMIO:
|
---|
102 | case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE:
|
---|
103 | case PGMPHYSHANDLERTYPE_PHYSICAL_ALL:
|
---|
104 | break;
|
---|
105 | default:
|
---|
106 | AssertMsgFailed(("Invalid input enmType=%d!\n", enmType));
|
---|
107 | return VERR_INVALID_PARAMETER;
|
---|
108 | }
|
---|
109 | if ( (RTRCUINTPTR)pvUserRC >= 0x10000
|
---|
110 | && MMHyperR3ToRC(pVM, MMHyperRCToR3(pVM, pvUserRC)) != pvUserRC)
|
---|
111 | {
|
---|
112 | AssertMsgFailed(("Not RC pointer! pvUserRC=%RRv\n", pvUserRC));
|
---|
113 | return VERR_INVALID_PARAMETER;
|
---|
114 | }
|
---|
115 | AssertReturn(pfnHandlerR3 || pfnHandlerR0 || pfnHandlerRC, VERR_INVALID_PARAMETER);
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * We require the range to be within registered ram.
|
---|
119 | * There is no apparent need to support ranges which cover more than one ram range.
|
---|
120 | */
|
---|
121 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
122 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
123 | pRam = pRam->CTX_SUFF(pNext);
|
---|
124 | if ( !pRam
|
---|
125 | || GCPhysLast < pRam->GCPhys
|
---|
126 | || GCPhys > pRam->GCPhysLast)
|
---|
127 | {
|
---|
128 | #ifdef IN_RING3
|
---|
129 | DBGFR3Info(pVM, "phys", NULL, NULL);
|
---|
130 | #endif
|
---|
131 | AssertMsgFailed(("No RAM range for %VGp-%VGp\n", GCPhys, GCPhysLast));
|
---|
132 | return VERR_PGM_HANDLER_PHYSICAL_NO_RAM_RANGE;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * Allocate and initialize the new entry.
|
---|
137 | */
|
---|
138 | PPGMPHYSHANDLER pNew;
|
---|
139 | int rc = MMHyperAlloc(pVM, sizeof(*pNew), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew);
|
---|
140 | if (VBOX_FAILURE(rc))
|
---|
141 | return rc;
|
---|
142 |
|
---|
143 | pNew->Core.Key = GCPhys;
|
---|
144 | pNew->Core.KeyLast = GCPhysLast;
|
---|
145 | pNew->enmType = enmType;
|
---|
146 | pNew->cPages = (GCPhysLast - (GCPhys & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
147 | pNew->pfnHandlerR3 = pfnHandlerR3;
|
---|
148 | pNew->pvUserR3 = pvUserR3;
|
---|
149 | pNew->pfnHandlerR0 = pfnHandlerR0;
|
---|
150 | pNew->pvUserR0 = pvUserR0;
|
---|
151 | pNew->pfnHandlerRC = pfnHandlerRC;
|
---|
152 | pNew->pvUserRC = pvUserRC;
|
---|
153 | pNew->pszDesc = pszDesc;
|
---|
154 |
|
---|
155 | pgmLock(pVM);
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * Try insert into list.
|
---|
159 | */
|
---|
160 | if (RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pNew->Core))
|
---|
161 | {
|
---|
162 | rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pNew, pRam);
|
---|
163 | if (rc == VINF_PGM_GCPHYS_ALIASED)
|
---|
164 | {
|
---|
165 | pVM->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
|
---|
166 | VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
|
---|
167 | }
|
---|
168 | pVM->pgm.s.fPhysCacheFlushPending = true;
|
---|
169 | HWACCMFlushTLB(pVM);
|
---|
170 | #ifndef IN_RING3
|
---|
171 | REMNotifyHandlerPhysicalRegister(pVM, enmType, GCPhys, GCPhysLast - GCPhys + 1, !!pfnHandlerR3);
|
---|
172 | #else
|
---|
173 | REMR3NotifyHandlerPhysicalRegister(pVM, enmType, GCPhys, GCPhysLast - GCPhys + 1, !!pfnHandlerR3);
|
---|
174 | #endif
|
---|
175 | pgmUnlock(pVM);
|
---|
176 | if (rc != VINF_SUCCESS)
|
---|
177 | Log(("PGMHandlerPhysicalRegisterEx: returns %Vrc (%VGp-%VGp)\n", rc, GCPhys, GCPhysLast));
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 |
|
---|
181 | pgmUnlock(pVM);
|
---|
182 |
|
---|
183 | #if defined(IN_RING3) && defined(VBOX_STRICT)
|
---|
184 | DBGFR3Info(pVM, "handlers", "phys nostats", NULL);
|
---|
185 | #endif
|
---|
186 | AssertMsgFailed(("Conflict! GCPhys=%VGp GCPhysLast=%VGp pszDesc=%s\n", GCPhys, GCPhysLast, pszDesc));
|
---|
187 | MMHyperFree(pVM, pNew);
|
---|
188 | return VERR_PGM_HANDLER_PHYSICAL_CONFLICT;
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Sets ram range flags and attempts updating shadow PTs.
|
---|
194 | *
|
---|
195 | * @returns VBox status code.
|
---|
196 | * @retval VINF_SUCCESS when shadow PTs was successfully updated.
|
---|
197 | * @retval VINF_PGM_GCPHYS_ALIASED when the shadow PTs could be updated because
|
---|
198 | * the guest page aliased or/and mapped by multiple PTs.
|
---|
199 | * @param pVM The VM handle.
|
---|
200 | * @param pCur The physical handler.
|
---|
201 | * @param pRam The RAM range.
|
---|
202 | */
|
---|
203 | static int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVM pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam)
|
---|
204 | {
|
---|
205 | /*
|
---|
206 | * Iterate the guest ram pages updating the flags and flushing PT entries
|
---|
207 | * mapping the page.
|
---|
208 | */
|
---|
209 | bool fFlushTLBs = false;
|
---|
210 | #if defined(PGMPOOL_WITH_GCPHYS_TRACKING) || defined(PGMPOOL_WITH_CACHE)
|
---|
211 | int rc = VINF_SUCCESS;
|
---|
212 | #else
|
---|
213 | const int rc = VINF_PGM_GCPHYS_ALIASED;
|
---|
214 | #endif
|
---|
215 | const unsigned uState = pgmHandlerPhysicalCalcState(pCur);
|
---|
216 | RTUINT cPages = pCur->cPages;
|
---|
217 | RTUINT i = (pCur->Core.Key - pRam->GCPhys) >> PAGE_SHIFT;
|
---|
218 | for (;;)
|
---|
219 | {
|
---|
220 | /* Physical chunk in dynamically allocated range not present? */
|
---|
221 | if (RT_UNLIKELY(!PGM_PAGE_GET_HCPHYS(&pRam->aPages[i])))
|
---|
222 | {
|
---|
223 | RTGCPHYS GCPhys = pRam->GCPhys + (i << PAGE_SHIFT);
|
---|
224 | #ifdef IN_RING3
|
---|
225 | int rc2 = pgmr3PhysGrowRange(pVM, GCPhys);
|
---|
226 | #else
|
---|
227 | int rc2 = CTXALLMID(VMM, CallHost)(pVM, VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
|
---|
228 | #endif
|
---|
229 | if (rc2 != VINF_SUCCESS)
|
---|
230 | return rc2;
|
---|
231 | }
|
---|
232 |
|
---|
233 | /* Only do upgrades. */
|
---|
234 | PPGMPAGE pPage = &pRam->aPages[i];
|
---|
235 | if (PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) < uState)
|
---|
236 | {
|
---|
237 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
|
---|
238 | Assert(PGM_PAGE_GET_HCPHYS(pPage));
|
---|
239 |
|
---|
240 | #ifdef PGMPOOL_WITH_GCPHYS_TRACKING
|
---|
241 | /* This code also makes ASSUMPTIONS about the cRefs and stuff. */
|
---|
242 | Assert(MM_RAM_FLAGS_IDX_SHIFT < MM_RAM_FLAGS_CREFS_SHIFT);
|
---|
243 | const uint16_t u16 = pRam->aPages[i].HCPhys >> MM_RAM_FLAGS_IDX_SHIFT; /** @todo PAGE FLAGS */
|
---|
244 | if (u16)
|
---|
245 | {
|
---|
246 | if ((u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) != MM_RAM_FLAGS_CREFS_PHYSEXT)
|
---|
247 | pgmPoolTrackFlushGCPhysPT(pVM,
|
---|
248 | pPage,
|
---|
249 | u16 & MM_RAM_FLAGS_IDX_MASK,
|
---|
250 | u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
|
---|
251 | else if (u16 != ((MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) | MM_RAM_FLAGS_IDX_OVERFLOWED))
|
---|
252 | pgmPoolTrackFlushGCPhysPTs(pVM, pPage, u16 & MM_RAM_FLAGS_IDX_MASK);
|
---|
253 | else
|
---|
254 | rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPage);
|
---|
255 | fFlushTLBs = true;
|
---|
256 | }
|
---|
257 | #elif defined(PGMPOOL_WITH_CACHE)
|
---|
258 | rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPage);
|
---|
259 | fFlushTLBs = true;
|
---|
260 | #endif
|
---|
261 | }
|
---|
262 |
|
---|
263 | /* next */
|
---|
264 | if (--cPages == 0)
|
---|
265 | break;
|
---|
266 | i++;
|
---|
267 | }
|
---|
268 |
|
---|
269 | if (fFlushTLBs && rc == VINF_SUCCESS)
|
---|
270 | {
|
---|
271 | PGM_INVL_GUEST_TLBS();
|
---|
272 | Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: flushing guest TLBs\n"));
|
---|
273 | }
|
---|
274 | else
|
---|
275 | Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: doesn't flush guest TLBs. rc=%Vrc\n", rc));
|
---|
276 | return rc;
|
---|
277 | }
|
---|
278 |
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * Register a physical page access handler.
|
---|
282 | *
|
---|
283 | * @returns VBox status code.
|
---|
284 | * @param pVM VM Handle.
|
---|
285 | * @param GCPhys Start physical address.
|
---|
286 | */
|
---|
287 | VMMDECL(int) PGMHandlerPhysicalDeregister(PVM pVM, RTGCPHYS GCPhys)
|
---|
288 | {
|
---|
289 | /*
|
---|
290 | * Find the handler.
|
---|
291 | */
|
---|
292 | pgmLock(pVM);
|
---|
293 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
294 | if (pCur)
|
---|
295 | {
|
---|
296 | LogFlow(("PGMHandlerPhysicalDeregister: Removing Range %#VGp-%#VGp %s\n",
|
---|
297 | pCur->Core.Key, pCur->Core.KeyLast, R3STRING(pCur->pszDesc)));
|
---|
298 |
|
---|
299 | /*
|
---|
300 | * Clear the page bits and notify the REM about this change.
|
---|
301 | */
|
---|
302 | HWACCMFlushTLB(pVM);
|
---|
303 | pgmHandlerPhysicalResetRamFlags(pVM, pCur);
|
---|
304 | pgmHandlerPhysicalDeregisterNotifyREM(pVM, pCur);
|
---|
305 | pgmUnlock(pVM);
|
---|
306 | MMHyperFree(pVM, pCur);
|
---|
307 | return VINF_SUCCESS;
|
---|
308 | }
|
---|
309 | pgmUnlock(pVM);
|
---|
310 |
|
---|
311 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhys));
|
---|
312 | return VERR_PGM_HANDLER_NOT_FOUND;
|
---|
313 | }
|
---|
314 |
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Shared code with modify.
|
---|
318 | */
|
---|
319 | static void pgmHandlerPhysicalDeregisterNotifyREM(PVM pVM, PPGMPHYSHANDLER pCur)
|
---|
320 | {
|
---|
321 | RTGCPHYS GCPhysStart = pCur->Core.Key;
|
---|
322 | RTGCPHYS GCPhysLast = pCur->Core.KeyLast;
|
---|
323 |
|
---|
324 | /*
|
---|
325 | * Page align the range.
|
---|
326 | *
|
---|
327 | * Since we've reset (recalculated) the physical handler state of all pages
|
---|
328 | * we can make use of the page states to figure out whether a page should be
|
---|
329 | * included in the REM notification or not.
|
---|
330 | */
|
---|
331 | if ( (pCur->Core.Key & PAGE_OFFSET_MASK)
|
---|
332 | || ((pCur->Core.KeyLast + 1) & PAGE_OFFSET_MASK))
|
---|
333 | {
|
---|
334 | Assert(pCur->enmType != PGMPHYSHANDLERTYPE_MMIO);
|
---|
335 |
|
---|
336 | if (GCPhysStart & PAGE_OFFSET_MASK)
|
---|
337 | {
|
---|
338 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhysStart);
|
---|
339 | if ( pPage
|
---|
340 | && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_NONE)
|
---|
341 | {
|
---|
342 | RTGCPHYS GCPhys = (GCPhysStart + (PAGE_SIZE - 1)) & X86_PTE_PAE_PG_MASK;
|
---|
343 | if ( GCPhys > GCPhysLast
|
---|
344 | || GCPhys < GCPhysStart)
|
---|
345 | return;
|
---|
346 | GCPhysStart = GCPhys;
|
---|
347 | }
|
---|
348 | else
|
---|
349 | GCPhysStart &= X86_PTE_PAE_PG_MASK;
|
---|
350 | Assert(!pPage || PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO); /* these are page aligned atm! */
|
---|
351 | }
|
---|
352 |
|
---|
353 | if (GCPhysLast & PAGE_OFFSET_MASK)
|
---|
354 | {
|
---|
355 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhysLast);
|
---|
356 | if ( pPage
|
---|
357 | && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_NONE)
|
---|
358 | {
|
---|
359 | RTGCPHYS GCPhys = (GCPhysLast & X86_PTE_PAE_PG_MASK) - 1;
|
---|
360 | if ( GCPhys < GCPhysStart
|
---|
361 | || GCPhys > GCPhysLast)
|
---|
362 | return;
|
---|
363 | GCPhysLast = GCPhys;
|
---|
364 | }
|
---|
365 | else
|
---|
366 | GCPhysLast |= PAGE_OFFSET_MASK;
|
---|
367 | Assert(!pPage || PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO); /* these are page aligned atm! */
|
---|
368 | }
|
---|
369 | }
|
---|
370 |
|
---|
371 | /*
|
---|
372 | * Tell REM.
|
---|
373 | */
|
---|
374 | const bool fRestoreAsRAM = pCur->pfnHandlerR3
|
---|
375 | && pCur->enmType != PGMPHYSHANDLERTYPE_MMIO; /** @todo this isn't entirely correct. */
|
---|
376 | #ifndef IN_RING3
|
---|
377 | REMNotifyHandlerPhysicalDeregister(pVM, pCur->enmType, GCPhysStart, GCPhysLast - GCPhysStart + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
|
---|
378 | #else
|
---|
379 | REMR3NotifyHandlerPhysicalDeregister(pVM, pCur->enmType, GCPhysStart, GCPhysLast - GCPhysStart + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
|
---|
380 | #endif
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * pgmHandlerPhysicalResetRamFlags helper that checks for
|
---|
386 | * other handlers on edge pages.
|
---|
387 | */
|
---|
388 | DECLINLINE(void) pgmHandlerPhysicalRecalcPageState(PPGM pPGM, RTGCPHYS GCPhys, bool fAbove, PPGMRAMRANGE *ppRamHint)
|
---|
389 | {
|
---|
390 | /*
|
---|
391 | * Look for other handlers.
|
---|
392 | */
|
---|
393 | unsigned uState = PGM_PAGE_HNDL_PHYS_STATE_NONE;
|
---|
394 | for (;;)
|
---|
395 | {
|
---|
396 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, GCPhys, fAbove);
|
---|
397 | if ( !pCur
|
---|
398 | || ((fAbove ? pCur->Core.Key : pCur->Core.KeyLast) >> PAGE_SHIFT) != (GCPhys >> PAGE_SHIFT))
|
---|
399 | break;
|
---|
400 | unsigned uThisState = pgmHandlerPhysicalCalcState(pCur);
|
---|
401 | uState = RT_MAX(uState, uThisState);
|
---|
402 |
|
---|
403 | /* next? */
|
---|
404 | RTGCPHYS GCPhysNext = fAbove
|
---|
405 | ? pCur->Core.KeyLast + 1
|
---|
406 | : pCur->Core.Key - 1;
|
---|
407 | if ((GCPhysNext >> PAGE_SHIFT) != (GCPhys >> PAGE_SHIFT))
|
---|
408 | break;
|
---|
409 | GCPhys = GCPhysNext;
|
---|
410 | }
|
---|
411 |
|
---|
412 | /*
|
---|
413 | * Update if we found something that is a higher priority
|
---|
414 | * state than the current.
|
---|
415 | */
|
---|
416 | if (uState != PGM_PAGE_HNDL_PHYS_STATE_NONE)
|
---|
417 | {
|
---|
418 | PPGMPAGE pPage;
|
---|
419 | int rc = pgmPhysGetPageWithHintEx(pPGM, GCPhys, &pPage, ppRamHint);
|
---|
420 | if ( RT_SUCCESS(rc)
|
---|
421 | && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) < uState)
|
---|
422 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
|
---|
423 | else
|
---|
424 | AssertRC(rc);
|
---|
425 | }
|
---|
426 | }
|
---|
427 |
|
---|
428 |
|
---|
429 | /**
|
---|
430 | * Resets ram range flags.
|
---|
431 | *
|
---|
432 | * @returns VBox status code.
|
---|
433 | * @retval VINF_SUCCESS when shadow PTs was successfully updated.
|
---|
434 | * @param pVM The VM handle.
|
---|
435 | * @param pCur The physical handler.
|
---|
436 | *
|
---|
437 | * @remark We don't start messing with the shadow page tables, as we've already got code
|
---|
438 | * in Trap0e which deals with out of sync handler flags (originally conceived for
|
---|
439 | * global pages).
|
---|
440 | */
|
---|
441 | static void pgmHandlerPhysicalResetRamFlags(PVM pVM, PPGMPHYSHANDLER pCur)
|
---|
442 | {
|
---|
443 | /*
|
---|
444 | * Iterate the guest ram pages updating the state.
|
---|
445 | */
|
---|
446 | RTUINT cPages = pCur->cPages;
|
---|
447 | RTGCPHYS GCPhys = pCur->Core.Key;
|
---|
448 | PPGMRAMRANGE pRamHint = NULL;
|
---|
449 | PPGM pPGM = &pVM->pgm.s;
|
---|
450 | for (;;)
|
---|
451 | {
|
---|
452 | PPGMPAGE pPage;
|
---|
453 | int rc = pgmPhysGetPageWithHintEx(pPGM, GCPhys, &pPage, &pRamHint);
|
---|
454 | if (RT_SUCCESS(rc))
|
---|
455 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_NONE);
|
---|
456 | else
|
---|
457 | AssertRC(rc);
|
---|
458 |
|
---|
459 | /* next */
|
---|
460 | if (--cPages == 0)
|
---|
461 | break;
|
---|
462 | GCPhys += PAGE_SIZE;
|
---|
463 | }
|
---|
464 |
|
---|
465 | /*
|
---|
466 | * Check for partial start and end pages.
|
---|
467 | */
|
---|
468 | if (pCur->Core.Key & PAGE_OFFSET_MASK)
|
---|
469 | pgmHandlerPhysicalRecalcPageState(pPGM, pCur->Core.Key - 1, false /* fAbove */, &pRamHint);
|
---|
470 | if ((pCur->Core.KeyLast & PAGE_OFFSET_MASK) != PAGE_SIZE - 1)
|
---|
471 | pgmHandlerPhysicalRecalcPageState(pPGM, pCur->Core.KeyLast + 1, true /* fAbove */, &pRamHint);
|
---|
472 | }
|
---|
473 |
|
---|
474 |
|
---|
475 | /**
|
---|
476 | * Modify a physical page access handler.
|
---|
477 | *
|
---|
478 | * Modification can only be done to the range it self, not the type or anything else.
|
---|
479 | *
|
---|
480 | * @returns VBox status code.
|
---|
481 | * For all return codes other than VERR_PGM_HANDLER_NOT_FOUND and VINF_SUCCESS the range is deregistered
|
---|
482 | * and a new registration must be performed!
|
---|
483 | * @param pVM VM handle.
|
---|
484 | * @param GCPhysCurrent Current location.
|
---|
485 | * @param GCPhys New location.
|
---|
486 | * @param GCPhysLast New last location.
|
---|
487 | */
|
---|
488 | VMMDECL(int) PGMHandlerPhysicalModify(PVM pVM, RTGCPHYS GCPhysCurrent, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast)
|
---|
489 | {
|
---|
490 | /*
|
---|
491 | * Remove it.
|
---|
492 | */
|
---|
493 | int rc;
|
---|
494 | pgmLock(pVM);
|
---|
495 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhysCurrent);
|
---|
496 | if (pCur)
|
---|
497 | {
|
---|
498 | /*
|
---|
499 | * Clear the ram flags. (We're gonna move or free it!)
|
---|
500 | */
|
---|
501 | pgmHandlerPhysicalResetRamFlags(pVM, pCur);
|
---|
502 | const bool fRestoreAsRAM = pCur->pfnHandlerR3
|
---|
503 | && pCur->enmType != PGMPHYSHANDLERTYPE_MMIO; /** @todo this isn't entirely correct. */
|
---|
504 |
|
---|
505 | /*
|
---|
506 | * Validate the new range, modify and reinsert.
|
---|
507 | */
|
---|
508 | if (GCPhysLast >= GCPhys)
|
---|
509 | {
|
---|
510 | /*
|
---|
511 | * We require the range to be within registered ram.
|
---|
512 | * There is no apparent need to support ranges which cover more than one ram range.
|
---|
513 | */
|
---|
514 | PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
|
---|
515 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
516 | pRam = pRam->CTX_SUFF(pNext);
|
---|
517 | if ( pRam
|
---|
518 | && GCPhys <= pRam->GCPhysLast
|
---|
519 | && GCPhysLast >= pRam->GCPhys)
|
---|
520 | {
|
---|
521 | pCur->Core.Key = GCPhys;
|
---|
522 | pCur->Core.KeyLast = GCPhysLast;
|
---|
523 | pCur->cPages = (GCPhysLast - (GCPhys & X86_PTE_PAE_PG_MASK) + 1) >> PAGE_SHIFT;
|
---|
524 |
|
---|
525 | if (RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pCur->Core))
|
---|
526 | {
|
---|
527 | /*
|
---|
528 | * Set ram flags, flush shadow PT entries and finally tell REM about this.
|
---|
529 | */
|
---|
530 | rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pCur, pRam);
|
---|
531 | if (rc == VINF_PGM_GCPHYS_ALIASED)
|
---|
532 | {
|
---|
533 | pVM->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
|
---|
534 | VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
|
---|
535 | }
|
---|
536 | pVM->pgm.s.fPhysCacheFlushPending = true;
|
---|
537 |
|
---|
538 | #ifndef IN_RING3
|
---|
539 | REMNotifyHandlerPhysicalModify(pVM, pCur->enmType, GCPhysCurrent, GCPhys,
|
---|
540 | pCur->Core.KeyLast - GCPhys + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
|
---|
541 | #else
|
---|
542 | REMR3NotifyHandlerPhysicalModify(pVM, pCur->enmType, GCPhysCurrent, GCPhys,
|
---|
543 | pCur->Core.KeyLast - GCPhys + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
|
---|
544 | #endif
|
---|
545 | HWACCMFlushTLB(pVM);
|
---|
546 | pgmUnlock(pVM);
|
---|
547 | Log(("PGMHandlerPhysicalModify: GCPhysCurrent=%VGp -> GCPhys=%VGp GCPhysLast=%VGp\n",
|
---|
548 | GCPhysCurrent, GCPhys, GCPhysLast));
|
---|
549 | return VINF_SUCCESS;
|
---|
550 | }
|
---|
551 |
|
---|
552 | AssertMsgFailed(("Conflict! GCPhys=%VGp GCPhysLast=%VGp\n", GCPhys, GCPhysLast));
|
---|
553 | rc = VERR_PGM_HANDLER_PHYSICAL_CONFLICT;
|
---|
554 | }
|
---|
555 | else
|
---|
556 | {
|
---|
557 | AssertMsgFailed(("No RAM range for %VGp-%VGp\n", GCPhys, GCPhysLast));
|
---|
558 | rc = VERR_PGM_HANDLER_PHYSICAL_NO_RAM_RANGE;
|
---|
559 | }
|
---|
560 | }
|
---|
561 | else
|
---|
562 | {
|
---|
563 | AssertMsgFailed(("Invalid range %VGp-%VGp\n", GCPhys, GCPhysLast));
|
---|
564 | rc = VERR_INVALID_PARAMETER;
|
---|
565 | }
|
---|
566 |
|
---|
567 | /*
|
---|
568 | * Invalid new location, free it.
|
---|
569 | * We've only gotta notify REM and free the memory.
|
---|
570 | */
|
---|
571 | pgmHandlerPhysicalDeregisterNotifyREM(pVM, pCur);
|
---|
572 | MMHyperFree(pVM, pCur);
|
---|
573 | }
|
---|
574 | else
|
---|
575 | {
|
---|
576 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhysCurrent));
|
---|
577 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
578 | }
|
---|
579 |
|
---|
580 | pgmUnlock(pVM);
|
---|
581 | return rc;
|
---|
582 | }
|
---|
583 |
|
---|
584 |
|
---|
585 | /**
|
---|
586 | * Changes the callbacks associated with a physical access handler.
|
---|
587 | *
|
---|
588 | * @returns VBox status code.
|
---|
589 | * @param pVM VM Handle.
|
---|
590 | * @param GCPhys Start physical address.
|
---|
591 | * @param pfnHandlerR3 The R3 handler.
|
---|
592 | * @param pvUserR3 User argument to the R3 handler.
|
---|
593 | * @param pfnHandlerR0 The R0 handler.
|
---|
594 | * @param pvUserR0 User argument to the R0 handler.
|
---|
595 | * @param pfnHandlerRC The RC handler.
|
---|
596 | * @param pvUserRC User argument to the RC handler. Values larger or
|
---|
597 | * equal to 0x10000 will be relocated automatically.
|
---|
598 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
599 | */
|
---|
600 | VMMDECL(int) PGMHandlerPhysicalChangeCallbacks(PVM pVM, RTGCPHYS GCPhys,
|
---|
601 | R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3, RTR3PTR pvUserR3,
|
---|
602 | R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0, RTR0PTR pvUserR0,
|
---|
603 | RCPTRTYPE(PFNPGMRCPHYSHANDLER) pfnHandlerRC, RTRCPTR pvUserRC,
|
---|
604 | R3PTRTYPE(const char *) pszDesc)
|
---|
605 | {
|
---|
606 | /*
|
---|
607 | * Get the handler.
|
---|
608 | */
|
---|
609 | int rc = VINF_SUCCESS;
|
---|
610 | pgmLock(pVM);
|
---|
611 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
612 | if (pCur)
|
---|
613 | {
|
---|
614 | /*
|
---|
615 | * Change callbacks.
|
---|
616 | */
|
---|
617 | pCur->pfnHandlerR3 = pfnHandlerR3;
|
---|
618 | pCur->pvUserR3 = pvUserR3;
|
---|
619 | pCur->pfnHandlerR0 = pfnHandlerR0;
|
---|
620 | pCur->pvUserR0 = pvUserR0;
|
---|
621 | pCur->pfnHandlerRC = pfnHandlerRC;
|
---|
622 | pCur->pvUserRC = pvUserRC;
|
---|
623 | pCur->pszDesc = pszDesc;
|
---|
624 | }
|
---|
625 | else
|
---|
626 | {
|
---|
627 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhys));
|
---|
628 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
629 | }
|
---|
630 |
|
---|
631 | pgmUnlock(pVM);
|
---|
632 | return rc;
|
---|
633 | }
|
---|
634 |
|
---|
635 |
|
---|
636 | /**
|
---|
637 | * Splits a physical access handler in two.
|
---|
638 | *
|
---|
639 | * @returns VBox status code.
|
---|
640 | * @param pVM VM Handle.
|
---|
641 | * @param GCPhys Start physical address of the handler.
|
---|
642 | * @param GCPhysSplit The split address.
|
---|
643 | */
|
---|
644 | VMMDECL(int) PGMHandlerPhysicalSplit(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysSplit)
|
---|
645 | {
|
---|
646 | AssertReturn(GCPhys < GCPhysSplit, VERR_INVALID_PARAMETER);
|
---|
647 |
|
---|
648 | /*
|
---|
649 | * Do the allocation without owning the lock.
|
---|
650 | */
|
---|
651 | PPGMPHYSHANDLER pNew;
|
---|
652 | int rc = MMHyperAlloc(pVM, sizeof(*pNew), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew);
|
---|
653 | if (VBOX_FAILURE(rc))
|
---|
654 | return rc;
|
---|
655 |
|
---|
656 | /*
|
---|
657 | * Get the handler.
|
---|
658 | */
|
---|
659 | pgmLock(pVM);
|
---|
660 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
661 | if (pCur)
|
---|
662 | {
|
---|
663 | if (GCPhysSplit <= pCur->Core.KeyLast)
|
---|
664 | {
|
---|
665 | /*
|
---|
666 | * Create new handler node for the 2nd half.
|
---|
667 | */
|
---|
668 | *pNew = *pCur;
|
---|
669 | pNew->Core.Key = GCPhysSplit;
|
---|
670 | pNew->cPages = (pNew->Core.KeyLast - (pNew->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
671 |
|
---|
672 | pCur->Core.KeyLast = GCPhysSplit - 1;
|
---|
673 | pCur->cPages = (pCur->Core.KeyLast - (pCur->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
674 |
|
---|
675 | if (RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pNew->Core))
|
---|
676 | {
|
---|
677 | LogFlow(("PGMHandlerPhysicalSplit: %VGp-%VGp and %VGp-%VGp\n",
|
---|
678 | pCur->Core.Key, pCur->Core.KeyLast, pNew->Core.Key, pNew->Core.KeyLast));
|
---|
679 | pgmUnlock(pVM);
|
---|
680 | return VINF_SUCCESS;
|
---|
681 | }
|
---|
682 | AssertMsgFailed(("whu?\n"));
|
---|
683 | rc = VERR_INTERNAL_ERROR;
|
---|
684 | }
|
---|
685 | else
|
---|
686 | {
|
---|
687 | AssertMsgFailed(("outside range: %VGp-%VGp split %VGp\n", pCur->Core.Key, pCur->Core.KeyLast, GCPhysSplit));
|
---|
688 | rc = VERR_INVALID_PARAMETER;
|
---|
689 | }
|
---|
690 | }
|
---|
691 | else
|
---|
692 | {
|
---|
693 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhys));
|
---|
694 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
695 | }
|
---|
696 | pgmUnlock(pVM);
|
---|
697 | MMHyperFree(pVM, pNew);
|
---|
698 | return rc;
|
---|
699 | }
|
---|
700 |
|
---|
701 |
|
---|
702 | /**
|
---|
703 | * Joins up two adjacent physical access handlers which has the same callbacks.
|
---|
704 | *
|
---|
705 | * @returns VBox status code.
|
---|
706 | * @param pVM VM Handle.
|
---|
707 | * @param GCPhys1 Start physical address of the first handler.
|
---|
708 | * @param GCPhys2 Start physical address of the second handler.
|
---|
709 | */
|
---|
710 | VMMDECL(int) PGMHandlerPhysicalJoin(PVM pVM, RTGCPHYS GCPhys1, RTGCPHYS GCPhys2)
|
---|
711 | {
|
---|
712 | /*
|
---|
713 | * Get the handlers.
|
---|
714 | */
|
---|
715 | int rc;
|
---|
716 | pgmLock(pVM);
|
---|
717 | PPGMPHYSHANDLER pCur1 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys1);
|
---|
718 | if (pCur1)
|
---|
719 | {
|
---|
720 | PPGMPHYSHANDLER pCur2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys2);
|
---|
721 | if (pCur2)
|
---|
722 | {
|
---|
723 | /*
|
---|
724 | * Make sure that they are adjacent, and that they've got the same callbacks.
|
---|
725 | */
|
---|
726 | if (pCur1->Core.KeyLast + 1 == pCur2->Core.Key)
|
---|
727 | {
|
---|
728 | if ( pCur1->pfnHandlerRC == pCur2->pfnHandlerRC
|
---|
729 | && pCur1->pfnHandlerR0 == pCur2->pfnHandlerR0
|
---|
730 | && pCur1->pfnHandlerR3 == pCur2->pfnHandlerR3)
|
---|
731 | {
|
---|
732 | PPGMPHYSHANDLER pCur3 = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys2);
|
---|
733 | if (pCur3 == pCur2)
|
---|
734 | {
|
---|
735 | pCur1->Core.KeyLast = pCur2->Core.KeyLast;
|
---|
736 | pCur1->cPages = (pCur1->Core.KeyLast - (pCur1->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
737 | LogFlow(("PGMHandlerPhysicalJoin: %VGp-%VGp %VGp-%VGp\n",
|
---|
738 | pCur1->Core.Key, pCur1->Core.KeyLast, pCur2->Core.Key, pCur2->Core.KeyLast));
|
---|
739 | pgmUnlock(pVM);
|
---|
740 | MMHyperFree(pVM, pCur2);
|
---|
741 | return VINF_SUCCESS;
|
---|
742 | }
|
---|
743 |
|
---|
744 | Assert(pCur3 == pCur2);
|
---|
745 | rc = VERR_INTERNAL_ERROR;
|
---|
746 | }
|
---|
747 | else
|
---|
748 | {
|
---|
749 | AssertMsgFailed(("mismatching handlers\n"));
|
---|
750 | rc = VERR_ACCESS_DENIED;
|
---|
751 | }
|
---|
752 | }
|
---|
753 | else
|
---|
754 | {
|
---|
755 | AssertMsgFailed(("not adjacent: %VGp-%VGp %VGp-%VGp\n",
|
---|
756 | pCur1->Core.Key, pCur1->Core.KeyLast, pCur2->Core.Key, pCur2->Core.KeyLast));
|
---|
757 | rc = VERR_INVALID_PARAMETER;
|
---|
758 | }
|
---|
759 | }
|
---|
760 | else
|
---|
761 | {
|
---|
762 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhys2));
|
---|
763 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
764 | }
|
---|
765 | }
|
---|
766 | else
|
---|
767 | {
|
---|
768 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhys1));
|
---|
769 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
770 | }
|
---|
771 | pgmUnlock(pVM);
|
---|
772 | return rc;
|
---|
773 |
|
---|
774 | }
|
---|
775 |
|
---|
776 |
|
---|
777 | /**
|
---|
778 | * Resets any modifications to individual pages in a physical
|
---|
779 | * page access handler region.
|
---|
780 | *
|
---|
781 | * This is used in pair with PGMHandlerPhysicalPageTempOff().
|
---|
782 | *
|
---|
783 | * @returns VBox status code.
|
---|
784 | * @param pVM VM Handle
|
---|
785 | * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
|
---|
786 | */
|
---|
787 | VMMDECL(int) PGMHandlerPhysicalReset(PVM pVM, RTGCPHYS GCPhys)
|
---|
788 | {
|
---|
789 | pgmLock(pVM);
|
---|
790 |
|
---|
791 | /*
|
---|
792 | * Find the handler.
|
---|
793 | */
|
---|
794 | int rc;
|
---|
795 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
796 | if (pCur)
|
---|
797 | {
|
---|
798 | /*
|
---|
799 | * Validate type.
|
---|
800 | */
|
---|
801 | switch (pCur->enmType)
|
---|
802 | {
|
---|
803 | case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE:
|
---|
804 | case PGMPHYSHANDLERTYPE_PHYSICAL_ALL:
|
---|
805 | {
|
---|
806 | /*
|
---|
807 | * Set the flags and flush shadow PT entries.
|
---|
808 | */
|
---|
809 | STAM_COUNTER_INC(&pVM->pgm.s.StatHandlePhysicalReset);
|
---|
810 | PPGMRAMRANGE pRam = pgmPhysGetRange(&pVM->pgm.s, GCPhys);
|
---|
811 | Assert(pRam);
|
---|
812 | rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pCur, pRam);
|
---|
813 | if (rc == VINF_PGM_GCPHYS_ALIASED)
|
---|
814 | {
|
---|
815 | pVM->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
|
---|
816 | VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
|
---|
817 | }
|
---|
818 | pVM->pgm.s.fPhysCacheFlushPending = true;
|
---|
819 | HWACCMFlushTLB(pVM);
|
---|
820 |
|
---|
821 | rc = VINF_SUCCESS;
|
---|
822 | break;
|
---|
823 | }
|
---|
824 |
|
---|
825 | /*
|
---|
826 | * Invalid.
|
---|
827 | */
|
---|
828 | case PGMPHYSHANDLERTYPE_MMIO:
|
---|
829 | AssertMsgFailed(("Can't reset type %d!\n", pCur->enmType));
|
---|
830 | rc = VERR_INTERNAL_ERROR;
|
---|
831 | break;
|
---|
832 |
|
---|
833 | default:
|
---|
834 | AssertMsgFailed(("Invalid type %d! Corruption!\n", pCur->enmType));
|
---|
835 | rc = VERR_INTERNAL_ERROR;
|
---|
836 | break;
|
---|
837 | }
|
---|
838 | }
|
---|
839 | else
|
---|
840 | {
|
---|
841 | AssertMsgFailed(("Didn't find MMIO Range starting at %#x\n", GCPhys));
|
---|
842 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
843 | }
|
---|
844 |
|
---|
845 | pgmUnlock(pVM);
|
---|
846 | return rc;
|
---|
847 | }
|
---|
848 |
|
---|
849 |
|
---|
850 | /**
|
---|
851 | * Temporarily turns off the access monitoring of a page within a monitored
|
---|
852 | * physical write/all page access handler region.
|
---|
853 | *
|
---|
854 | * Use this when no further \#PFs are required for that page. Be aware that
|
---|
855 | * a page directory sync might reset the flags, and turn on access monitoring
|
---|
856 | * for the page.
|
---|
857 | *
|
---|
858 | * The caller must do required page table modifications.
|
---|
859 | *
|
---|
860 | * @returns VBox status code.
|
---|
861 | * @param pVM VM Handle
|
---|
862 | * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
|
---|
863 | * This must be a fully page aligned range or we risk messing up other
|
---|
864 | * handlers installed for the start and end pages.
|
---|
865 | * @param GCPhysPage Physical address of the page to turn off access monitoring for.
|
---|
866 | */
|
---|
867 | VMMDECL(int) PGMHandlerPhysicalPageTempOff(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage)
|
---|
868 | {
|
---|
869 | /*
|
---|
870 | * Validate the range.
|
---|
871 | */
|
---|
872 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
873 | if (pCur)
|
---|
874 | {
|
---|
875 | if ( GCPhysPage >= pCur->Core.Key
|
---|
876 | && GCPhysPage <= pCur->Core.KeyLast)
|
---|
877 | {
|
---|
878 | Assert(!(pCur->Core.Key & PAGE_OFFSET_MASK));
|
---|
879 | Assert((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
|
---|
880 |
|
---|
881 | AssertReturn( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
|
---|
882 | || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL,
|
---|
883 | VERR_ACCESS_DENIED);
|
---|
884 |
|
---|
885 | /*
|
---|
886 | * Change the page status.
|
---|
887 | */
|
---|
888 | PPGMPAGE pPage;
|
---|
889 | int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPage, &pPage);
|
---|
890 | AssertRCReturn(rc, rc);
|
---|
891 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
|
---|
892 | #ifdef IN_RING0
|
---|
893 | HWACCMInvalidatePhysPage(pVM, GCPhysPage);
|
---|
894 | #endif
|
---|
895 | return VINF_SUCCESS;
|
---|
896 | }
|
---|
897 |
|
---|
898 | AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
|
---|
899 | GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
|
---|
900 | return VERR_INVALID_PARAMETER;
|
---|
901 | }
|
---|
902 |
|
---|
903 | AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
|
---|
904 | return VERR_PGM_HANDLER_NOT_FOUND;
|
---|
905 | }
|
---|
906 |
|
---|
907 |
|
---|
908 | /**
|
---|
909 | * Turns access monitoring of a page within a monitored
|
---|
910 | * physical write/all page access handler regio back on.
|
---|
911 | *
|
---|
912 | * The caller must do required page table modifications.
|
---|
913 | *
|
---|
914 | * @returns VBox status code.
|
---|
915 | * @param pVM VM Handle
|
---|
916 | * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
|
---|
917 | * This must be a fully page aligned range or we risk messing up other
|
---|
918 | * handlers installed for the start and end pages.
|
---|
919 | * @param GCPhysPage Physical address of the page to turn on access monitoring for.
|
---|
920 | */
|
---|
921 | VMMDECL(int) PGMHandlerPhysicalPageReset(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage)
|
---|
922 | {
|
---|
923 | /*
|
---|
924 | * Validate the range.
|
---|
925 | */
|
---|
926 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
927 | if (pCur)
|
---|
928 | {
|
---|
929 | if ( GCPhysPage >= pCur->Core.Key
|
---|
930 | && GCPhysPage <= pCur->Core.KeyLast)
|
---|
931 | {
|
---|
932 | Assert(!(pCur->Core.Key & PAGE_OFFSET_MASK));
|
---|
933 | Assert((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
|
---|
934 |
|
---|
935 | AssertReturn( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
|
---|
936 | || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL,
|
---|
937 | VERR_ACCESS_DENIED);
|
---|
938 |
|
---|
939 | /*
|
---|
940 | * Change the page status.
|
---|
941 | */
|
---|
942 | PPGMPAGE pPage;
|
---|
943 | int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPage, &pPage);
|
---|
944 | AssertRCReturn(rc, rc);
|
---|
945 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, pgmHandlerPhysicalCalcState(pCur));
|
---|
946 | return VINF_SUCCESS;
|
---|
947 | }
|
---|
948 |
|
---|
949 | AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
|
---|
950 | GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
|
---|
951 | return VERR_INVALID_PARAMETER;
|
---|
952 | }
|
---|
953 |
|
---|
954 | AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
|
---|
955 | return VERR_PGM_HANDLER_NOT_FOUND;
|
---|
956 | }
|
---|
957 |
|
---|
958 |
|
---|
959 | /**
|
---|
960 | * Checks if a physical range is handled
|
---|
961 | *
|
---|
962 | * @returns boolean
|
---|
963 | * @param pVM VM Handle
|
---|
964 | * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
|
---|
965 | */
|
---|
966 | VMMDECL(bool) PGMHandlerPhysicalIsRegistered(PVM pVM, RTGCPHYS GCPhys)
|
---|
967 | {
|
---|
968 | /*
|
---|
969 | * Find the handler.
|
---|
970 | */
|
---|
971 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
972 | if (pCur)
|
---|
973 | {
|
---|
974 | if ( GCPhys >= pCur->Core.Key
|
---|
975 | && GCPhys <= pCur->Core.KeyLast)
|
---|
976 | {
|
---|
977 | Assert( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
|
---|
978 | || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL
|
---|
979 | || pCur->enmType == PGMPHYSHANDLERTYPE_MMIO);
|
---|
980 | return true;
|
---|
981 | }
|
---|
982 | }
|
---|
983 |
|
---|
984 | return false;
|
---|
985 | }
|
---|
986 |
|
---|
987 |
|
---|
988 | /**
|
---|
989 | * Search for virtual handler with matching physical address
|
---|
990 | *
|
---|
991 | * @returns VBox status code
|
---|
992 | * @param pVM The VM handle.
|
---|
993 | * @param GCPhys GC physical address to search for.
|
---|
994 | * @param ppVirt Where to store the pointer to the virtual handler structure.
|
---|
995 | * @param piPage Where to store the pointer to the index of the cached physical page.
|
---|
996 | */
|
---|
997 | int pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage)
|
---|
998 | {
|
---|
999 | STAM_PROFILE_START(CTXSUFF(&pVM->pgm.s.StatVirtHandleSearchByPhys), a);
|
---|
1000 | Assert(ppVirt);
|
---|
1001 |
|
---|
1002 | PPGMPHYS2VIRTHANDLER pCur;
|
---|
1003 | pCur = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, GCPhys);
|
---|
1004 | if (pCur)
|
---|
1005 | {
|
---|
1006 | /* found a match! */
|
---|
1007 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
1008 | AssertRelease(pCur->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD);
|
---|
1009 | #endif
|
---|
1010 | *ppVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
|
---|
1011 | *piPage = pCur - &(*ppVirt)->aPhysToVirt[0];
|
---|
1012 |
|
---|
1013 | LogFlow(("PHYS2VIRT: found match for %RGp -> %RGv *piPage=%#x\n", GCPhys, (*ppVirt)->Core.Key, *piPage));
|
---|
1014 | STAM_PROFILE_STOP(CTXSUFF(&pVM->pgm.s.StatVirtHandleSearchByPhys), a);
|
---|
1015 | return VINF_SUCCESS;
|
---|
1016 | }
|
---|
1017 |
|
---|
1018 | *ppVirt = NULL;
|
---|
1019 | STAM_PROFILE_STOP(CTXSUFF(&pVM->pgm.s.StatVirtHandleSearchByPhys), a);
|
---|
1020 | return VERR_PGM_HANDLER_NOT_FOUND;
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 |
|
---|
1024 | /**
|
---|
1025 | * Deal with aliases in phys2virt.
|
---|
1026 | *
|
---|
1027 | * As pointed out by the various todos, this currently only deals with
|
---|
1028 | * aliases where the two ranges match 100%.
|
---|
1029 | *
|
---|
1030 | * @param pVM The VM handle.
|
---|
1031 | * @param pPhys2Virt The node we failed insert.
|
---|
1032 | */
|
---|
1033 | static void pgmHandlerVirtualInsertAliased(PVM pVM, PPGMPHYS2VIRTHANDLER pPhys2Virt)
|
---|
1034 | {
|
---|
1035 | /*
|
---|
1036 | * First find the node which is conflicting with us.
|
---|
1037 | */
|
---|
1038 | /** @todo Deal with partial overlapping. (Unlikly situation, so I'm too lazy to do anything about it now.) */
|
---|
1039 | /** @todo check if the current head node covers the ground we do. This is highly unlikely
|
---|
1040 | * and I'm too lazy to implement this now as it will require sorting the list and stuff like that. */
|
---|
1041 | PPGMPHYS2VIRTHANDLER pHead = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
|
---|
1042 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
1043 | AssertReleaseMsg(pHead != pPhys2Virt, ("%VGp-%VGp offVirtHandler=%#RX32\n",
|
---|
1044 | pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler));
|
---|
1045 | #endif
|
---|
1046 | if (RT_UNLIKELY(!pHead || pHead->Core.KeyLast != pPhys2Virt->Core.KeyLast))
|
---|
1047 | {
|
---|
1048 | /** @todo do something clever here... */
|
---|
1049 | LogRel(("pgmHandlerVirtualInsertAliased: %VGp-%VGp\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
|
---|
1050 | pPhys2Virt->offNextAlias = 0;
|
---|
1051 | return;
|
---|
1052 | }
|
---|
1053 |
|
---|
1054 | /*
|
---|
1055 | * Insert ourselves as the next node.
|
---|
1056 | */
|
---|
1057 | if (!(pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
|
---|
1058 | pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IN_TREE;
|
---|
1059 | else
|
---|
1060 | {
|
---|
1061 | PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pHead + (pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
|
---|
1062 | pPhys2Virt->offNextAlias = ((intptr_t)pNext - (intptr_t)pPhys2Virt)
|
---|
1063 | | PGMPHYS2VIRTHANDLER_IN_TREE;
|
---|
1064 | }
|
---|
1065 | pHead->offNextAlias = ((intptr_t)pPhys2Virt - (intptr_t)pHead)
|
---|
1066 | | (pHead->offNextAlias & ~PGMPHYS2VIRTHANDLER_OFF_MASK);
|
---|
1067 | Log(("pgmHandlerVirtualInsertAliased: %VGp-%VGp offNextAlias=%#RX32\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
|
---|
1068 | }
|
---|
1069 |
|
---|
1070 |
|
---|
1071 | /**
|
---|
1072 | * Resets one virtual handler range.
|
---|
1073 | *
|
---|
1074 | * This is called by HandlerVirtualUpdate when it has detected some kind of
|
---|
1075 | * problem and have started clearing the virtual handler page states (or
|
---|
1076 | * when there have been registration/deregistrations). For this reason this
|
---|
1077 | * function will only update the page status if it's lower than desired.
|
---|
1078 | *
|
---|
1079 | * @returns 0
|
---|
1080 | * @param pNode Pointer to a PGMVIRTHANDLER.
|
---|
1081 | * @param pvUser The VM handle.
|
---|
1082 | */
|
---|
1083 | DECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
|
---|
1084 | {
|
---|
1085 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
|
---|
1086 | PVM pVM = (PVM)pvUser;
|
---|
1087 |
|
---|
1088 | /*
|
---|
1089 | * Iterate the pages and apply the new state.
|
---|
1090 | */
|
---|
1091 | unsigned uState = pgmHandlerVirtualCalcState(pCur);
|
---|
1092 | PPGMRAMRANGE pRamHint = NULL;
|
---|
1093 | RTGCUINTPTR offPage = ((RTGCUINTPTR)pCur->Core.Key & PAGE_OFFSET_MASK);
|
---|
1094 | RTGCUINTPTR cbLeft = pCur->cb;
|
---|
1095 | for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
|
---|
1096 | {
|
---|
1097 | PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
|
---|
1098 | if (pPhys2Virt->Core.Key != NIL_RTGCPHYS)
|
---|
1099 | {
|
---|
1100 | /*
|
---|
1101 | * Update the page state wrt virtual handlers.
|
---|
1102 | */
|
---|
1103 | PPGMPAGE pPage;
|
---|
1104 | int rc = pgmPhysGetPageWithHintEx(&pVM->pgm.s, pPhys2Virt->Core.Key, &pPage, &pRamHint);
|
---|
1105 | if ( RT_SUCCESS(rc)
|
---|
1106 | && PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
|
---|
1107 | PGM_PAGE_SET_HNDL_VIRT_STATE(pPage, uState);
|
---|
1108 | else
|
---|
1109 | AssertRC(rc);
|
---|
1110 |
|
---|
1111 | /*
|
---|
1112 | * Need to insert the page in the Phys2Virt lookup tree?
|
---|
1113 | */
|
---|
1114 | if (pPhys2Virt->Core.KeyLast == NIL_RTGCPHYS)
|
---|
1115 | {
|
---|
1116 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
1117 | AssertRelease(!pPhys2Virt->offNextAlias);
|
---|
1118 | #endif
|
---|
1119 | unsigned cbPhys = cbLeft;
|
---|
1120 | if (cbPhys > PAGE_SIZE - offPage)
|
---|
1121 | cbPhys = PAGE_SIZE - offPage;
|
---|
1122 | else
|
---|
1123 | Assert(iPage == pCur->cPages - 1);
|
---|
1124 | pPhys2Virt->Core.KeyLast = pPhys2Virt->Core.Key + cbPhys - 1; /* inclusive */
|
---|
1125 | pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IS_HEAD | PGMPHYS2VIRTHANDLER_IN_TREE;
|
---|
1126 | if (!RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, &pPhys2Virt->Core))
|
---|
1127 | pgmHandlerVirtualInsertAliased(pVM, pPhys2Virt);
|
---|
1128 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
1129 | else
|
---|
1130 | AssertReleaseMsg(RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key) == &pPhys2Virt->Core,
|
---|
1131 | ("%VGp-%VGp offNextAlias=%#RX32\n",
|
---|
1132 | pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
|
---|
1133 | #endif
|
---|
1134 | Log2(("PHYS2VIRT: Insert physical range %VGp-%VGp offNextAlias=%#RX32 %s\n",
|
---|
1135 | pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
|
---|
1136 | }
|
---|
1137 | }
|
---|
1138 | cbLeft -= PAGE_SIZE - offPage;
|
---|
1139 | offPage = 0;
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | return 0;
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 |
|
---|
1146 | #if defined(VBOX_STRICT) || defined(LOG_ENABLED)
|
---|
1147 | /**
|
---|
1148 | * Worker for pgmHandlerVirtualDumpPhysPages.
|
---|
1149 | *
|
---|
1150 | * @returns 0 (continue enumeration).
|
---|
1151 | * @param pNode The virtual handler node.
|
---|
1152 | * @param pvUser User argument, unused.
|
---|
1153 | */
|
---|
1154 | static DECLCALLBACK(int) pgmHandlerVirtualDumpPhysPagesCallback(PAVLROGCPHYSNODECORE pNode, void *pvUser)
|
---|
1155 | {
|
---|
1156 | PPGMPHYS2VIRTHANDLER pCur = (PPGMPHYS2VIRTHANDLER)pNode;
|
---|
1157 | PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
|
---|
1158 | Log(("PHYS2VIRT: Range %VGp-%VGp for virtual handler: %s\n", pCur->Core.Key, pCur->Core.KeyLast, pVirt->pszDesc));
|
---|
1159 | return 0;
|
---|
1160 | }
|
---|
1161 |
|
---|
1162 |
|
---|
1163 | /**
|
---|
1164 | * Assertion / logging helper for dumping all the
|
---|
1165 | * virtual handlers to the log.
|
---|
1166 | *
|
---|
1167 | * @param pVM Pointer to the shared VM structure.
|
---|
1168 | */
|
---|
1169 | void pgmHandlerVirtualDumpPhysPages(PVM pVM)
|
---|
1170 | {
|
---|
1171 | RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, true /* from left */,
|
---|
1172 | pgmHandlerVirtualDumpPhysPagesCallback, 0);
|
---|
1173 | }
|
---|
1174 | #endif /* VBOX_STRICT || LOG_ENABLED */
|
---|
1175 |
|
---|
1176 | #ifdef VBOX_STRICT
|
---|
1177 |
|
---|
1178 | /**
|
---|
1179 | * State structure used by the PGMAssertHandlerAndFlagsInSync() function
|
---|
1180 | * and its AVL enumerators.
|
---|
1181 | */
|
---|
1182 | typedef struct PGMAHAFIS
|
---|
1183 | {
|
---|
1184 | /** The current physical address. */
|
---|
1185 | RTGCPHYS GCPhys;
|
---|
1186 | /** The state we've calculated. */
|
---|
1187 | unsigned uVirtStateFound;
|
---|
1188 | /** The state we're matching up to. */
|
---|
1189 | unsigned uVirtState;
|
---|
1190 | /** Number of errors. */
|
---|
1191 | unsigned cErrors;
|
---|
1192 | /** The VM handle. */
|
---|
1193 | PVM pVM;
|
---|
1194 | } PGMAHAFIS, *PPGMAHAFIS;
|
---|
1195 |
|
---|
1196 |
|
---|
1197 | #if 0 /* unused */
|
---|
1198 | /**
|
---|
1199 | * Verify virtual handler by matching physical address.
|
---|
1200 | *
|
---|
1201 | * @returns 0
|
---|
1202 | * @param pNode Pointer to a PGMVIRTHANDLER.
|
---|
1203 | * @param pvUser Pointer to user parameter.
|
---|
1204 | */
|
---|
1205 | static DECLCALLBACK(int) pgmHandlerVirtualVerifyOneByPhysAddr(PAVLROGCPTRNODECORE pNode, void *pvUser)
|
---|
1206 | {
|
---|
1207 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
|
---|
1208 | PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
|
---|
1209 |
|
---|
1210 | for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
|
---|
1211 | {
|
---|
1212 | if ((pCur->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) == pState->GCPhys)
|
---|
1213 | {
|
---|
1214 | unsigned uState = pgmHandlerVirtualCalcState(pCur);
|
---|
1215 | if (pState->uVirtState < uState)
|
---|
1216 | {
|
---|
1217 | error
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | if (pState->uVirtState == uState)
|
---|
1221 | break; //??
|
---|
1222 | }
|
---|
1223 | }
|
---|
1224 | return 0;
|
---|
1225 | }
|
---|
1226 | #endif /* unused */
|
---|
1227 |
|
---|
1228 |
|
---|
1229 | /**
|
---|
1230 | * Verify a virtual handler (enumeration callback).
|
---|
1231 | *
|
---|
1232 | * Called by PGMAssertHandlerAndFlagsInSync to check the sanity of all
|
---|
1233 | * the virtual handlers, esp. that the physical addresses matches up.
|
---|
1234 | *
|
---|
1235 | * @returns 0
|
---|
1236 | * @param pNode Pointer to a PGMVIRTHANDLER.
|
---|
1237 | * @param pvUser Pointer to a PPGMAHAFIS structure.
|
---|
1238 | */
|
---|
1239 | static DECLCALLBACK(int) pgmHandlerVirtualVerifyOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
|
---|
1240 | {
|
---|
1241 | PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)pNode;
|
---|
1242 | PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
|
---|
1243 | PVM pVM = pState->pVM;
|
---|
1244 |
|
---|
1245 | /*
|
---|
1246 | * Validate the type and calc state.
|
---|
1247 | */
|
---|
1248 | switch (pVirt->enmType)
|
---|
1249 | {
|
---|
1250 | case PGMVIRTHANDLERTYPE_WRITE:
|
---|
1251 | case PGMVIRTHANDLERTYPE_ALL:
|
---|
1252 | break;
|
---|
1253 | default:
|
---|
1254 | AssertMsgFailed(("unknown/wrong enmType=%d\n", pVirt->enmType));
|
---|
1255 | pState->cErrors++;
|
---|
1256 | return 0;
|
---|
1257 | }
|
---|
1258 | const unsigned uState = pgmHandlerVirtualCalcState(pVirt);
|
---|
1259 |
|
---|
1260 | /*
|
---|
1261 | * Check key alignment.
|
---|
1262 | */
|
---|
1263 | if ( (pVirt->aPhysToVirt[0].Core.Key & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->Core.Key & PAGE_OFFSET_MASK)
|
---|
1264 | && pVirt->aPhysToVirt[0].Core.Key != NIL_RTGCPHYS)
|
---|
1265 | {
|
---|
1266 | AssertMsgFailed(("virt handler phys has incorrect key! %RGp %RGv %s\n",
|
---|
1267 | pVirt->aPhysToVirt[0].Core.Key, pVirt->Core.Key, R3STRING(pVirt->pszDesc)));
|
---|
1268 | pState->cErrors++;
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 | if ( (pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->Core.KeyLast & PAGE_OFFSET_MASK)
|
---|
1272 | && pVirt->aPhysToVirt[pVirt->cPages - 1].Core.Key != NIL_RTGCPHYS)
|
---|
1273 | {
|
---|
1274 | AssertMsgFailed(("virt handler phys has incorrect key! %RGp %RGv %s\n",
|
---|
1275 | pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast, pVirt->Core.KeyLast, R3STRING(pVirt->pszDesc)));
|
---|
1276 | pState->cErrors++;
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | /*
|
---|
1280 | * Check pages for sanity and state.
|
---|
1281 | */
|
---|
1282 | RTGCUINTPTR GCPtr = (RTGCUINTPTR)pVirt->Core.Key;
|
---|
1283 | for (unsigned iPage = 0; iPage < pVirt->cPages; iPage++, GCPtr += PAGE_SIZE)
|
---|
1284 | {
|
---|
1285 | RTGCPHYS GCPhysGst;
|
---|
1286 | uint64_t fGst;
|
---|
1287 | int rc = PGMGstGetPage(pVM, (RTGCPTR)GCPtr, &fGst, &GCPhysGst);
|
---|
1288 | if ( rc == VERR_PAGE_NOT_PRESENT
|
---|
1289 | || rc == VERR_PAGE_TABLE_NOT_PRESENT)
|
---|
1290 | {
|
---|
1291 | if (pVirt->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
|
---|
1292 | {
|
---|
1293 | AssertMsgFailed(("virt handler phys out of sync. %VGp GCPhysNew=~0 iPage=%#x %VGv %s\n",
|
---|
1294 | pVirt->aPhysToVirt[iPage].Core.Key, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
|
---|
1295 | pState->cErrors++;
|
---|
1296 | }
|
---|
1297 | continue;
|
---|
1298 | }
|
---|
1299 |
|
---|
1300 | AssertRCReturn(rc, 0);
|
---|
1301 | if ((pVirt->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) != GCPhysGst)
|
---|
1302 | {
|
---|
1303 | AssertMsgFailed(("virt handler phys out of sync. %VGp GCPhysGst=%VGp iPage=%#x %VGv %s\n",
|
---|
1304 | pVirt->aPhysToVirt[iPage].Core.Key, GCPhysGst, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
|
---|
1305 | pState->cErrors++;
|
---|
1306 | continue;
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhysGst);
|
---|
1310 | if (!pPage)
|
---|
1311 | {
|
---|
1312 | AssertMsgFailed(("virt handler getting ram flags. GCPhysGst=%VGp iPage=%#x %VGv %s\n",
|
---|
1313 | GCPhysGst, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
|
---|
1314 | pState->cErrors++;
|
---|
1315 | continue;
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
|
---|
1319 | {
|
---|
1320 | AssertMsgFailed(("virt handler state mismatch. HCPhys=%VHp GCPhysGst=%VGp iPage=%#x %VGv state=%d expected>=%d %s\n",
|
---|
1321 | pPage->HCPhys, GCPhysGst, iPage, GCPtr, PGM_PAGE_GET_HNDL_VIRT_STATE(pPage), uState, R3STRING(pVirt->pszDesc)));
|
---|
1322 | pState->cErrors++;
|
---|
1323 | continue;
|
---|
1324 | }
|
---|
1325 | } /* for pages in virtual mapping. */
|
---|
1326 |
|
---|
1327 | return 0;
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 |
|
---|
1331 | /**
|
---|
1332 | * Asserts that the handlers+guest-page-tables == ramrange-flags and
|
---|
1333 | * that the physical addresses associated with virtual handlers are correct.
|
---|
1334 | *
|
---|
1335 | * @returns Number of mismatches.
|
---|
1336 | * @param pVM The VM handle.
|
---|
1337 | */
|
---|
1338 | VMMDECL(unsigned) PGMAssertHandlerAndFlagsInSync(PVM pVM)
|
---|
1339 | {
|
---|
1340 | PPGM pPGM = &pVM->pgm.s;
|
---|
1341 | PGMAHAFIS State;
|
---|
1342 | State.GCPhys = 0;
|
---|
1343 | State.uVirtState = 0;
|
---|
1344 | State.uVirtStateFound = 0;
|
---|
1345 | State.cErrors = 0;
|
---|
1346 | State.pVM = pVM;
|
---|
1347 |
|
---|
1348 | /*
|
---|
1349 | * Check the RAM flags against the handlers.
|
---|
1350 | */
|
---|
1351 | for (PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges); pRam; pRam = pRam->CTX_SUFF(pNext))
|
---|
1352 | {
|
---|
1353 | const unsigned cPages = pRam->cb >> PAGE_SHIFT;
|
---|
1354 | for (unsigned iPage = 0; iPage < cPages; iPage++)
|
---|
1355 | {
|
---|
1356 | PGMPAGE const *pPage = &pRam->aPages[iPage];
|
---|
1357 | if (PGM_PAGE_HAS_ANY_HANDLERS(pPage))
|
---|
1358 | {
|
---|
1359 | State.GCPhys = pRam->GCPhys + (iPage << PAGE_SHIFT);
|
---|
1360 |
|
---|
1361 | /*
|
---|
1362 | * Physical first - calculate the state based on the handlers
|
---|
1363 | * active on the page, then compare.
|
---|
1364 | */
|
---|
1365 | if (PGM_PAGE_HAS_ANY_PHYSICAL_HANDLERS(pPage))
|
---|
1366 | {
|
---|
1367 | /* the first */
|
---|
1368 | PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys);
|
---|
1369 | if (!pPhys)
|
---|
1370 | {
|
---|
1371 | pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys, true);
|
---|
1372 | if ( pPhys
|
---|
1373 | && pPhys->Core.Key > (State.GCPhys + PAGE_SIZE - 1))
|
---|
1374 | pPhys = NULL;
|
---|
1375 | Assert(!pPhys || pPhys->Core.Key >= State.GCPhys);
|
---|
1376 | }
|
---|
1377 | if (pPhys)
|
---|
1378 | {
|
---|
1379 | unsigned uState = pgmHandlerPhysicalCalcState(pPhys);
|
---|
1380 |
|
---|
1381 | /* more? */
|
---|
1382 | while (pPhys->Core.KeyLast < (State.GCPhys | PAGE_OFFSET_MASK))
|
---|
1383 | {
|
---|
1384 | PPGMPHYSHANDLER pPhys2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers,
|
---|
1385 | pPhys->Core.KeyLast + 1, true);
|
---|
1386 | if ( !pPhys2
|
---|
1387 | || pPhys2->Core.Key > (State.GCPhys | PAGE_OFFSET_MASK))
|
---|
1388 | break;
|
---|
1389 | unsigned uState2 = pgmHandlerPhysicalCalcState(pPhys2);
|
---|
1390 | uState = RT_MAX(uState, uState2);
|
---|
1391 | pPhys = pPhys2;
|
---|
1392 | }
|
---|
1393 |
|
---|
1394 | /* compare.*/
|
---|
1395 | if ( PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != uState
|
---|
1396 | && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
|
---|
1397 | {
|
---|
1398 | AssertMsgFailed(("ram range vs phys handler flags mismatch. GCPhys=%RGp state=%d expected=%d %s\n",
|
---|
1399 | State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), uState, pPhys->pszDesc));
|
---|
1400 | State.cErrors++;
|
---|
1401 | }
|
---|
1402 |
|
---|
1403 | #ifdef IN_RING3
|
---|
1404 | /* validate that REM is handling it. */
|
---|
1405 | if ( !REMR3IsPageAccessHandled(pVM, State.GCPhys)
|
---|
1406 | /* ignore shadowed ROM for the time being. */ /// @todo PAGE FLAGS
|
---|
1407 | && (pPage->HCPhys & (MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2)) != (MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2))
|
---|
1408 | {
|
---|
1409 | AssertMsgFailed(("ram range vs phys handler REM mismatch. GCPhys=%RGp state=%d %s\n",
|
---|
1410 | State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), pPhys->pszDesc));
|
---|
1411 | State.cErrors++;
|
---|
1412 | }
|
---|
1413 | #endif
|
---|
1414 | }
|
---|
1415 | else
|
---|
1416 | {
|
---|
1417 | AssertMsgFailed(("ram range vs phys handler mismatch. no handler for GCPhys=%RGp\n", State.GCPhys));
|
---|
1418 | State.cErrors++;
|
---|
1419 | }
|
---|
1420 | }
|
---|
1421 |
|
---|
1422 | /*
|
---|
1423 | * Virtual handlers.
|
---|
1424 | */
|
---|
1425 | if (PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage))
|
---|
1426 | {
|
---|
1427 | State.uVirtState = PGM_PAGE_GET_HNDL_VIRT_STATE(pPage);
|
---|
1428 | #if 1
|
---|
1429 | /* locate all the matching physical ranges. */
|
---|
1430 | State.uVirtStateFound = PGM_PAGE_HNDL_VIRT_STATE_NONE;
|
---|
1431 | RTGCPHYS GCPhysKey = State.GCPhys;
|
---|
1432 | for (;;)
|
---|
1433 | {
|
---|
1434 | PPGMPHYS2VIRTHANDLER pPhys2Virt = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
|
---|
1435 | GCPhysKey, true /* above-or-equal */);
|
---|
1436 | if ( !pPhys2Virt
|
---|
1437 | || (pPhys2Virt->Core.Key & X86_PTE_PAE_PG_MASK) != State.GCPhys)
|
---|
1438 | break;
|
---|
1439 |
|
---|
1440 | /* the head */
|
---|
1441 | GCPhysKey = pPhys2Virt->Core.KeyLast;
|
---|
1442 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
|
---|
1443 | unsigned uState = pgmHandlerVirtualCalcState(pCur);
|
---|
1444 | State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
|
---|
1445 |
|
---|
1446 | /* any aliases */
|
---|
1447 | while (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK)
|
---|
1448 | {
|
---|
1449 | pPhys2Virt = (PPGMPHYS2VIRTHANDLER)((uintptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
|
---|
1450 | pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
|
---|
1451 | uState = pgmHandlerVirtualCalcState(pCur);
|
---|
1452 | State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
|
---|
1453 | }
|
---|
1454 |
|
---|
1455 | /* done? */
|
---|
1456 | if ((GCPhysKey & X86_PTE_PAE_PG_MASK) != State.GCPhys)
|
---|
1457 | break;
|
---|
1458 | }
|
---|
1459 | #else
|
---|
1460 | /* very slow */
|
---|
1461 | RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOneByPhysAddr, &State);
|
---|
1462 | #endif
|
---|
1463 | if (State.uVirtState != State.uVirtStateFound)
|
---|
1464 | {
|
---|
1465 | AssertMsgFailed(("ram range vs virt handler flags mismatch. GCPhys=%RGp uVirtState=%#x uVirtStateFound=%#x\n",
|
---|
1466 | State.GCPhys, State.uVirtState, State.uVirtStateFound));
|
---|
1467 | State.cErrors++;
|
---|
1468 | }
|
---|
1469 | }
|
---|
1470 | }
|
---|
1471 | } /* foreach page in ram range. */
|
---|
1472 | } /* foreach ram range. */
|
---|
1473 |
|
---|
1474 | /*
|
---|
1475 | * Check that the physical addresses of the virtual handlers matches up
|
---|
1476 | * and that they are otherwise sane.
|
---|
1477 | */
|
---|
1478 | RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOne, &State);
|
---|
1479 |
|
---|
1480 | /*
|
---|
1481 | * Do the reverse check for physical handlers.
|
---|
1482 | */
|
---|
1483 | /** @todo */
|
---|
1484 |
|
---|
1485 | return State.cErrors;
|
---|
1486 | }
|
---|
1487 |
|
---|
1488 | #endif /* VBOX_STRICT */
|
---|
1489 |
|
---|