VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PGMAllHandler.cpp@ 17462

Last change on this file since 17462 was 17432, checked in by vboxsync, 16 years ago

PGM,GMM: Filling in missing bits and fixing some bugs.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette