VirtualBox

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

Last change on this file since 16527 was 16465, checked in by vboxsync, 16 years ago

Got rid of the aliased hack that directly manipulates shadow page table entries; use the right way by going through the PGM pool.

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