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