VirtualBox

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

Last change on this file since 17332 was 17316, checked in by vboxsync, 16 years ago

PGM: Use %R[pgmpage] instead of logging PGMPAGE::HCPhys.

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

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