VirtualBox

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

Last change on this file since 14257 was 14082, checked in by vboxsync, 16 years ago

Must mask off the reference count from the HC physical address

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