VirtualBox

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

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

Clear HCPhys of a reset MMIO range.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 61.1 KB
Line 
1/* $Id: PGMAllHandler.cpp 16457 2009-02-02 13:05:07Z 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.215389.xyz. 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 {
822 /*
823 * Set the flags and flush shadow PT entries.
824 */
825 STAM_COUNTER_INC(&pVM->pgm.s.CTX_MID_Z(Stat,PhysHandlerReset));
826 PPGMRAMRANGE pRam = pgmPhysGetRange(&pVM->pgm.s, GCPhys);
827 Assert(pRam);
828 rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pCur, pRam);
829 if (rc == VINF_PGM_GCPHYS_ALIASED)
830 {
831 pVM->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
832 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
833 }
834 pVM->pgm.s.fPhysCacheFlushPending = true;
835 HWACCMFlushTLB(pVM);
836
837 rc = VINF_SUCCESS;
838 break;
839 }
840
841 /*
842 * Invalid.
843 */
844 case PGMPHYSHANDLERTYPE_MMIO:
845 AssertMsgFailed(("Can't reset type %d!\n", pCur->enmType));
846 rc = VERR_INTERNAL_ERROR;
847 break;
848
849 default:
850 AssertMsgFailed(("Invalid type %d! Corruption!\n", pCur->enmType));
851 rc = VERR_INTERNAL_ERROR;
852 break;
853 }
854 }
855 else
856 {
857 AssertMsgFailed(("Didn't find MMIO Range starting at %#x\n", GCPhys));
858 rc = VERR_PGM_HANDLER_NOT_FOUND;
859 }
860
861 pgmUnlock(pVM);
862 return rc;
863}
864
865
866/**
867 * Temporarily turns off the access monitoring of a page within a monitored
868 * physical write/all page access handler region.
869 *
870 * Use this when no further \#PFs are required for that page. Be aware that
871 * a page directory sync might reset the flags, and turn on access monitoring
872 * for the page.
873 *
874 * The caller must do required page table modifications.
875 *
876 * @returns VBox status code.
877 * @param pVM VM Handle
878 * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
879 * This must be a fully page aligned range or we risk messing up other
880 * handlers installed for the start and end pages.
881 * @param GCPhysPage Physical address of the page to turn off access monitoring for.
882 */
883VMMDECL(int) PGMHandlerPhysicalPageTempOff(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage)
884{
885 /*
886 * Validate the range.
887 */
888 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
889 if (RT_LIKELY(pCur))
890 {
891 if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
892 && GCPhysPage <= pCur->Core.KeyLast))
893 {
894 Assert(!(pCur->Core.Key & PAGE_OFFSET_MASK));
895 Assert((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
896
897 AssertReturn( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
898 || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL,
899 VERR_ACCESS_DENIED);
900
901 /*
902 * Change the page status.
903 */
904 PPGMPAGE pPage;
905 int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPage, &pPage);
906 AssertRCReturn(rc, rc);
907 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
908#ifndef IN_RC
909 HWACCMInvalidatePhysPage(pVM, GCPhysPage);
910#endif
911 return VINF_SUCCESS;
912 }
913
914 AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
915 GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
916 return VERR_INVALID_PARAMETER;
917 }
918
919 AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
920 return VERR_PGM_HANDLER_NOT_FOUND;
921}
922
923
924/**
925 * Temporarily turns off the access monitoring of a page within an MMIO
926 * access handler region and remaps it to another guest physical region.
927 *
928 * Use this when no further \#PFs are required for that page. Be aware that
929 * a page directory sync might reset the flags, and turn on access monitoring
930 * for the page.
931 *
932 * The caller must do required page table modifications.
933 *
934 * @returns VBox status code.
935 * @param pVM VM Handle
936 * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
937 * This must be a fully page aligned range or we risk messing up other
938 * handlers installed for the start and end pages.
939 * @param GCPhysPage Physical address of the page to turn off access monitoring for.
940 * @param GCPhysPageRemap Physical address of the page that serves as backing memory.
941 */
942VMMDECL(int) PGMHandlerPhysicalPageAlias(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage, RTGCPHYS GCPhysPageRemap)
943{
944 /*
945 * Validate the range.
946 */
947 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
948 if (RT_LIKELY(pCur))
949 {
950 if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
951 && GCPhysPage <= pCur->Core.KeyLast))
952 {
953 Assert(!(pCur->Core.Key & PAGE_OFFSET_MASK));
954 Assert((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
955
956 AssertReturn(pCur->enmType == PGMPHYSHANDLERTYPE_MMIO, VERR_ACCESS_DENIED);
957 /** @todo r=bird: This totally breaks the new PGMPAGE management. Will probably
958 * have to require that the current page is the zero page... Require
959 * GCPhysPageRemap to be a MMIO2 page might help matters because those
960 * pages aren't managed dynamically (at least not yet).
961 * VBOX_WITH_NEW_PHYS_CODE TODO! */
962
963 PPGMPAGE pPageRemap;
964 int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPageRemap, &pPageRemap);
965 AssertRCReturn(rc, rc);
966
967 /*
968 * Change the page status.
969 */
970 PPGMPAGE pPage;
971 rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPage, &pPage);
972 AssertRCReturn(rc, rc);
973
974 /* Do the actual remapping here. This page now serves as an alias for the backing memory specified. */
975 pPage->HCPhys = pPageRemap->HCPhys & MM_RAM_FLAGS_NO_REFS_MASK;
976
977 LogFlow(("PGMHandlerPhysicalPageAlias %RGp -> %RGp - %RHp\n", GCPhysPage, GCPhysPageRemap, pPageRemap->HCPhys));
978 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
979#ifndef IN_RC
980 HWACCMInvalidatePhysPage(pVM, GCPhysPage);
981#endif
982 return VINF_SUCCESS;
983 }
984
985 AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
986 GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
987 return VERR_INVALID_PARAMETER;
988 }
989
990 AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
991 return VERR_PGM_HANDLER_NOT_FOUND;
992}
993
994
995/**
996 * Turns access monitoring of a page within a monitored
997 * physical write/all page access handler regio back on.
998 *
999 * The caller must do required page table modifications.
1000 *
1001 * @returns VBox status code.
1002 * @param pVM VM Handle
1003 * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
1004 * This must be a fully page aligned range or we risk messing up other
1005 * handlers installed for the start and end pages.
1006 * @param GCPhysPage Physical address of the page to turn on access monitoring for.
1007 */
1008VMMDECL(int) PGMHandlerPhysicalPageReset(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage)
1009{
1010 /*
1011 * Validate the range.
1012 */
1013 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1014 if (RT_LIKELY(pCur))
1015 {
1016 if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
1017 && GCPhysPage <= pCur->Core.KeyLast))
1018 {
1019 Assert(!(pCur->Core.Key & PAGE_OFFSET_MASK));
1020 Assert((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
1021
1022 AssertReturn( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
1023 || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL
1024 || pCur->enmType == PGMPHYSHANDLERTYPE_MMIO,
1025 VERR_ACCESS_DENIED);
1026
1027 /*
1028 * Change the page status.
1029 */
1030 PPGMPAGE pPage;
1031 int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPage, &pPage);
1032 AssertRCReturn(rc, rc);
1033 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, pgmHandlerPhysicalCalcState(pCur));
1034
1035 /* MMIO has no backing memory; overwrite previously aliased physical address. */
1036 if (pCur->enmType == PGMPHYSHANDLERTYPE_MMIO)
1037 pPage->HCPhys = 0;
1038
1039#ifndef IN_RC
1040 HWACCMInvalidatePhysPage(pVM, GCPhysPage);
1041#endif
1042 return VINF_SUCCESS;
1043 }
1044
1045 AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
1046 GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
1047 return VERR_INVALID_PARAMETER;
1048 }
1049
1050 AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
1051 return VERR_PGM_HANDLER_NOT_FOUND;
1052}
1053
1054
1055/**
1056 * Checks if a physical range is handled
1057 *
1058 * @returns boolean
1059 * @param pVM VM Handle.
1060 * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
1061 * @remarks Caller must take the PGM lock...
1062 * @threads EMT.
1063 */
1064VMMDECL(bool) PGMHandlerPhysicalIsRegistered(PVM pVM, RTGCPHYS GCPhys)
1065{
1066 /*
1067 * Find the handler.
1068 */
1069 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1070 if (pCur)
1071 {
1072 Assert(GCPhys >= pCur->Core.Key && GCPhys <= pCur->Core.KeyLast);
1073 Assert( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
1074 || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL
1075 || pCur->enmType == PGMPHYSHANDLERTYPE_MMIO);
1076 return true;
1077 }
1078
1079 return false;
1080}
1081
1082
1083/**
1084 * Checks if it's an disabled all access handler or write access handler at the
1085 * given address.
1086 *
1087 * @returns true if it's an all access handler, false if it's a write access
1088 * handler.
1089 * @param pVM Pointer to the shared VM structure.
1090 * @param GCPhys The address of the page with a disabled handler.
1091 *
1092 * @remarks The caller, PGMR3PhysTlbGCPhys2Ptr, must hold the PGM lock.
1093 */
1094bool pgmHandlerPhysicalIsAll(PVM pVM, RTGCPHYS GCPhys)
1095{
1096 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1097 AssertReturn(pCur, true);
1098 Assert( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
1099 || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL
1100 || pCur->enmType == PGMPHYSHANDLERTYPE_MMIO); /* sanity */
1101 /* Only whole pages can be disabled. */
1102 Assert( pCur->Core.Key <= (GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK)
1103 && pCur->Core.KeyLast >= (GCPhys | PAGE_OFFSET_MASK));
1104 return pCur->enmType != PGMPHYSHANDLERTYPE_PHYSICAL_WRITE;
1105}
1106
1107
1108/**
1109 * Check if particular guest's VA is being monitored.
1110 *
1111 * @returns true or false
1112 * @param pVM VM handle.
1113 * @param GCPtr Virtual address.
1114 * @remarks Will acquire the PGM lock.
1115 * @threads Any.
1116 */
1117VMMDECL(bool) PGMHandlerVirtualIsRegistered(PVM pVM, RTGCPTR GCPtr)
1118{
1119 pgmLock(pVM);
1120 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGet(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, GCPtr);
1121 pgmUnlock(pVM);
1122
1123 return pCur != NULL;
1124}
1125
1126
1127/**
1128 * Search for virtual handler with matching physical address
1129 *
1130 * @returns VBox status code
1131 * @param pVM The VM handle.
1132 * @param GCPhys GC physical address to search for.
1133 * @param ppVirt Where to store the pointer to the virtual handler structure.
1134 * @param piPage Where to store the pointer to the index of the cached physical page.
1135 */
1136int pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage)
1137{
1138 STAM_PROFILE_START(&pVM->pgm.s.CTX_MID_Z(Stat,VirtHandlerSearchByPhys), a);
1139 Assert(ppVirt);
1140
1141 PPGMPHYS2VIRTHANDLER pCur;
1142 pCur = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, GCPhys);
1143 if (pCur)
1144 {
1145 /* found a match! */
1146#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1147 AssertRelease(pCur->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD);
1148#endif
1149 *ppVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
1150 *piPage = pCur - &(*ppVirt)->aPhysToVirt[0];
1151
1152 LogFlow(("PHYS2VIRT: found match for %RGp -> %RGv *piPage=%#x\n", GCPhys, (*ppVirt)->Core.Key, *piPage));
1153 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_MID_Z(Stat,VirtHandlerSearchByPhys), a);
1154 return VINF_SUCCESS;
1155 }
1156
1157 *ppVirt = NULL;
1158 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_MID_Z(Stat,VirtHandlerSearchByPhys), a);
1159 return VERR_PGM_HANDLER_NOT_FOUND;
1160}
1161
1162
1163/**
1164 * Deal with aliases in phys2virt.
1165 *
1166 * As pointed out by the various todos, this currently only deals with
1167 * aliases where the two ranges match 100%.
1168 *
1169 * @param pVM The VM handle.
1170 * @param pPhys2Virt The node we failed insert.
1171 */
1172static void pgmHandlerVirtualInsertAliased(PVM pVM, PPGMPHYS2VIRTHANDLER pPhys2Virt)
1173{
1174 /*
1175 * First find the node which is conflicting with us.
1176 */
1177 /** @todo Deal with partial overlapping. (Unlikly situation, so I'm too lazy to do anything about it now.) */
1178 /** @todo check if the current head node covers the ground we do. This is highly unlikely
1179 * and I'm too lazy to implement this now as it will require sorting the list and stuff like that. */
1180 PPGMPHYS2VIRTHANDLER pHead = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
1181#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1182 AssertReleaseMsg(pHead != pPhys2Virt, ("%RGp-%RGp offVirtHandler=%#RX32\n",
1183 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler));
1184#endif
1185 if (RT_UNLIKELY(!pHead || pHead->Core.KeyLast != pPhys2Virt->Core.KeyLast))
1186 {
1187 /** @todo do something clever here... */
1188 LogRel(("pgmHandlerVirtualInsertAliased: %RGp-%RGp\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
1189 pPhys2Virt->offNextAlias = 0;
1190 return;
1191 }
1192
1193 /*
1194 * Insert ourselves as the next node.
1195 */
1196 if (!(pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
1197 pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IN_TREE;
1198 else
1199 {
1200 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pHead + (pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
1201 pPhys2Virt->offNextAlias = ((intptr_t)pNext - (intptr_t)pPhys2Virt)
1202 | PGMPHYS2VIRTHANDLER_IN_TREE;
1203 }
1204 pHead->offNextAlias = ((intptr_t)pPhys2Virt - (intptr_t)pHead)
1205 | (pHead->offNextAlias & ~PGMPHYS2VIRTHANDLER_OFF_MASK);
1206 Log(("pgmHandlerVirtualInsertAliased: %RGp-%RGp offNextAlias=%#RX32\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
1207}
1208
1209
1210/**
1211 * Resets one virtual handler range.
1212 *
1213 * This is called by HandlerVirtualUpdate when it has detected some kind of
1214 * problem and have started clearing the virtual handler page states (or
1215 * when there have been registration/deregistrations). For this reason this
1216 * function will only update the page status if it's lower than desired.
1217 *
1218 * @returns 0
1219 * @param pNode Pointer to a PGMVIRTHANDLER.
1220 * @param pvUser The VM handle.
1221 */
1222DECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
1223{
1224 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
1225 PVM pVM = (PVM)pvUser;
1226
1227 /*
1228 * Iterate the pages and apply the new state.
1229 */
1230 unsigned uState = pgmHandlerVirtualCalcState(pCur);
1231 PPGMRAMRANGE pRamHint = NULL;
1232 RTGCUINTPTR offPage = ((RTGCUINTPTR)pCur->Core.Key & PAGE_OFFSET_MASK);
1233 RTGCUINTPTR cbLeft = pCur->cb;
1234 for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
1235 {
1236 PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
1237 if (pPhys2Virt->Core.Key != NIL_RTGCPHYS)
1238 {
1239 /*
1240 * Update the page state wrt virtual handlers.
1241 */
1242 PPGMPAGE pPage;
1243 int rc = pgmPhysGetPageWithHintEx(&pVM->pgm.s, pPhys2Virt->Core.Key, &pPage, &pRamHint);
1244 if ( RT_SUCCESS(rc)
1245 && PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
1246 PGM_PAGE_SET_HNDL_VIRT_STATE(pPage, uState);
1247 else
1248 AssertRC(rc);
1249
1250 /*
1251 * Need to insert the page in the Phys2Virt lookup tree?
1252 */
1253 if (pPhys2Virt->Core.KeyLast == NIL_RTGCPHYS)
1254 {
1255#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1256 AssertRelease(!pPhys2Virt->offNextAlias);
1257#endif
1258 unsigned cbPhys = cbLeft;
1259 if (cbPhys > PAGE_SIZE - offPage)
1260 cbPhys = PAGE_SIZE - offPage;
1261 else
1262 Assert(iPage == pCur->cPages - 1);
1263 pPhys2Virt->Core.KeyLast = pPhys2Virt->Core.Key + cbPhys - 1; /* inclusive */
1264 pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IS_HEAD | PGMPHYS2VIRTHANDLER_IN_TREE;
1265 if (!RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, &pPhys2Virt->Core))
1266 pgmHandlerVirtualInsertAliased(pVM, pPhys2Virt);
1267#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1268 else
1269 AssertReleaseMsg(RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key) == &pPhys2Virt->Core,
1270 ("%RGp-%RGp offNextAlias=%#RX32\n",
1271 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
1272#endif
1273 Log2(("PHYS2VIRT: Insert physical range %RGp-%RGp offNextAlias=%#RX32 %s\n",
1274 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
1275 }
1276 }
1277 cbLeft -= PAGE_SIZE - offPage;
1278 offPage = 0;
1279 }
1280
1281 return 0;
1282}
1283
1284#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
1285
1286/**
1287 * Worker for pgmHandlerVirtualDumpPhysPages.
1288 *
1289 * @returns 0 (continue enumeration).
1290 * @param pNode The virtual handler node.
1291 * @param pvUser User argument, unused.
1292 */
1293static DECLCALLBACK(int) pgmHandlerVirtualDumpPhysPagesCallback(PAVLROGCPHYSNODECORE pNode, void *pvUser)
1294{
1295 PPGMPHYS2VIRTHANDLER pCur = (PPGMPHYS2VIRTHANDLER)pNode;
1296 PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
1297 Log(("PHYS2VIRT: Range %RGp-%RGp for virtual handler: %s\n", pCur->Core.Key, pCur->Core.KeyLast, pVirt->pszDesc));
1298 return 0;
1299}
1300
1301
1302/**
1303 * Assertion / logging helper for dumping all the
1304 * virtual handlers to the log.
1305 *
1306 * @param pVM Pointer to the shared VM structure.
1307 */
1308void pgmHandlerVirtualDumpPhysPages(PVM pVM)
1309{
1310 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, true /* from left */,
1311 pgmHandlerVirtualDumpPhysPagesCallback, 0);
1312}
1313
1314#endif /* VBOX_STRICT || LOG_ENABLED */
1315#ifdef VBOX_STRICT
1316
1317/**
1318 * State structure used by the PGMAssertHandlerAndFlagsInSync() function
1319 * and its AVL enumerators.
1320 */
1321typedef struct PGMAHAFIS
1322{
1323 /** The current physical address. */
1324 RTGCPHYS GCPhys;
1325 /** The state we've calculated. */
1326 unsigned uVirtStateFound;
1327 /** The state we're matching up to. */
1328 unsigned uVirtState;
1329 /** Number of errors. */
1330 unsigned cErrors;
1331 /** The VM handle. */
1332 PVM pVM;
1333} PGMAHAFIS, *PPGMAHAFIS;
1334
1335
1336#if 0 /* unused */
1337/**
1338 * Verify virtual handler by matching physical address.
1339 *
1340 * @returns 0
1341 * @param pNode Pointer to a PGMVIRTHANDLER.
1342 * @param pvUser Pointer to user parameter.
1343 */
1344static DECLCALLBACK(int) pgmHandlerVirtualVerifyOneByPhysAddr(PAVLROGCPTRNODECORE pNode, void *pvUser)
1345{
1346 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
1347 PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
1348
1349 for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
1350 {
1351 if ((pCur->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) == pState->GCPhys)
1352 {
1353 unsigned uState = pgmHandlerVirtualCalcState(pCur);
1354 if (pState->uVirtState < uState)
1355 {
1356 error
1357 }
1358
1359 if (pState->uVirtState == uState)
1360 break; //??
1361 }
1362 }
1363 return 0;
1364}
1365#endif /* unused */
1366
1367
1368/**
1369 * Verify a virtual handler (enumeration callback).
1370 *
1371 * Called by PGMAssertHandlerAndFlagsInSync to check the sanity of all
1372 * the virtual handlers, esp. that the physical addresses matches up.
1373 *
1374 * @returns 0
1375 * @param pNode Pointer to a PGMVIRTHANDLER.
1376 * @param pvUser Pointer to a PPGMAHAFIS structure.
1377 */
1378static DECLCALLBACK(int) pgmHandlerVirtualVerifyOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
1379{
1380 PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)pNode;
1381 PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
1382 PVM pVM = pState->pVM;
1383
1384 /*
1385 * Validate the type and calc state.
1386 */
1387 switch (pVirt->enmType)
1388 {
1389 case PGMVIRTHANDLERTYPE_WRITE:
1390 case PGMVIRTHANDLERTYPE_ALL:
1391 break;
1392 default:
1393 AssertMsgFailed(("unknown/wrong enmType=%d\n", pVirt->enmType));
1394 pState->cErrors++;
1395 return 0;
1396 }
1397 const unsigned uState = pgmHandlerVirtualCalcState(pVirt);
1398
1399 /*
1400 * Check key alignment.
1401 */
1402 if ( (pVirt->aPhysToVirt[0].Core.Key & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->Core.Key & PAGE_OFFSET_MASK)
1403 && pVirt->aPhysToVirt[0].Core.Key != NIL_RTGCPHYS)
1404 {
1405 AssertMsgFailed(("virt handler phys has incorrect key! %RGp %RGv %s\n",
1406 pVirt->aPhysToVirt[0].Core.Key, pVirt->Core.Key, R3STRING(pVirt->pszDesc)));
1407 pState->cErrors++;
1408 }
1409
1410 if ( (pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->Core.KeyLast & PAGE_OFFSET_MASK)
1411 && pVirt->aPhysToVirt[pVirt->cPages - 1].Core.Key != NIL_RTGCPHYS)
1412 {
1413 AssertMsgFailed(("virt handler phys has incorrect key! %RGp %RGv %s\n",
1414 pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast, pVirt->Core.KeyLast, R3STRING(pVirt->pszDesc)));
1415 pState->cErrors++;
1416 }
1417
1418 /*
1419 * Check pages for sanity and state.
1420 */
1421 RTGCUINTPTR GCPtr = (RTGCUINTPTR)pVirt->Core.Key;
1422 for (unsigned iPage = 0; iPage < pVirt->cPages; iPage++, GCPtr += PAGE_SIZE)
1423 {
1424 RTGCPHYS GCPhysGst;
1425 uint64_t fGst;
1426 int rc = PGMGstGetPage(pVM, (RTGCPTR)GCPtr, &fGst, &GCPhysGst);
1427 if ( rc == VERR_PAGE_NOT_PRESENT
1428 || rc == VERR_PAGE_TABLE_NOT_PRESENT)
1429 {
1430 if (pVirt->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
1431 {
1432 AssertMsgFailed(("virt handler phys out of sync. %RGp GCPhysNew=~0 iPage=%#x %RGv %s\n",
1433 pVirt->aPhysToVirt[iPage].Core.Key, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
1434 pState->cErrors++;
1435 }
1436 continue;
1437 }
1438
1439 AssertRCReturn(rc, 0);
1440 if ((pVirt->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) != GCPhysGst)
1441 {
1442 AssertMsgFailed(("virt handler phys out of sync. %RGp GCPhysGst=%RGp iPage=%#x %RGv %s\n",
1443 pVirt->aPhysToVirt[iPage].Core.Key, GCPhysGst, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
1444 pState->cErrors++;
1445 continue;
1446 }
1447
1448 PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhysGst);
1449 if (!pPage)
1450 {
1451 AssertMsgFailed(("virt handler getting ram flags. GCPhysGst=%RGp iPage=%#x %RGv %s\n",
1452 GCPhysGst, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
1453 pState->cErrors++;
1454 continue;
1455 }
1456
1457 if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
1458 {
1459 AssertMsgFailed(("virt handler state mismatch. HCPhys=%RHp GCPhysGst=%RGp iPage=%#x %RGv state=%d expected>=%d %s\n",
1460 pPage->HCPhys, GCPhysGst, iPage, GCPtr, PGM_PAGE_GET_HNDL_VIRT_STATE(pPage), uState, R3STRING(pVirt->pszDesc)));
1461 pState->cErrors++;
1462 continue;
1463 }
1464 } /* for pages in virtual mapping. */
1465
1466 return 0;
1467}
1468
1469
1470/**
1471 * Asserts that the handlers+guest-page-tables == ramrange-flags and
1472 * that the physical addresses associated with virtual handlers are correct.
1473 *
1474 * @returns Number of mismatches.
1475 * @param pVM The VM handle.
1476 */
1477VMMDECL(unsigned) PGMAssertHandlerAndFlagsInSync(PVM pVM)
1478{
1479 PPGM pPGM = &pVM->pgm.s;
1480 PGMAHAFIS State;
1481 State.GCPhys = 0;
1482 State.uVirtState = 0;
1483 State.uVirtStateFound = 0;
1484 State.cErrors = 0;
1485 State.pVM = pVM;
1486
1487 /*
1488 * Check the RAM flags against the handlers.
1489 */
1490 for (PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges); pRam; pRam = pRam->CTX_SUFF(pNext))
1491 {
1492 const unsigned cPages = pRam->cb >> PAGE_SHIFT;
1493 for (unsigned iPage = 0; iPage < cPages; iPage++)
1494 {
1495 PGMPAGE const *pPage = &pRam->aPages[iPage];
1496 if (PGM_PAGE_HAS_ANY_HANDLERS(pPage))
1497 {
1498 State.GCPhys = pRam->GCPhys + (iPage << PAGE_SHIFT);
1499
1500 /*
1501 * Physical first - calculate the state based on the handlers
1502 * active on the page, then compare.
1503 */
1504 if (PGM_PAGE_HAS_ANY_PHYSICAL_HANDLERS(pPage))
1505 {
1506 /* the first */
1507 PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys);
1508 if (!pPhys)
1509 {
1510 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys, true);
1511 if ( pPhys
1512 && pPhys->Core.Key > (State.GCPhys + PAGE_SIZE - 1))
1513 pPhys = NULL;
1514 Assert(!pPhys || pPhys->Core.Key >= State.GCPhys);
1515 }
1516 if (pPhys)
1517 {
1518 unsigned uState = pgmHandlerPhysicalCalcState(pPhys);
1519
1520 /* more? */
1521 while (pPhys->Core.KeyLast < (State.GCPhys | PAGE_OFFSET_MASK))
1522 {
1523 PPGMPHYSHANDLER pPhys2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers,
1524 pPhys->Core.KeyLast + 1, true);
1525 if ( !pPhys2
1526 || pPhys2->Core.Key > (State.GCPhys | PAGE_OFFSET_MASK))
1527 break;
1528 unsigned uState2 = pgmHandlerPhysicalCalcState(pPhys2);
1529 uState = RT_MAX(uState, uState2);
1530 pPhys = pPhys2;
1531 }
1532
1533 /* compare.*/
1534 if ( PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != uState
1535 && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
1536 {
1537 AssertMsgFailed(("ram range vs phys handler flags mismatch. GCPhys=%RGp state=%d expected=%d %s\n",
1538 State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), uState, pPhys->pszDesc));
1539 State.cErrors++;
1540 }
1541
1542#ifdef IN_RING3
1543 /* validate that REM is handling it. */
1544 if ( !REMR3IsPageAccessHandled(pVM, State.GCPhys)
1545 /* ignore shadowed ROM for the time being. */ /// @todo PAGE FLAGS
1546 && (pPage->HCPhys & (MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2)) != (MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2))
1547 {
1548 AssertMsgFailed(("ram range vs phys handler REM mismatch. GCPhys=%RGp state=%d %s\n",
1549 State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), pPhys->pszDesc));
1550 State.cErrors++;
1551 }
1552#endif
1553 }
1554 else
1555 {
1556 AssertMsgFailed(("ram range vs phys handler mismatch. no handler for GCPhys=%RGp\n", State.GCPhys));
1557 State.cErrors++;
1558 }
1559 }
1560
1561 /*
1562 * Virtual handlers.
1563 */
1564 if (PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage))
1565 {
1566 State.uVirtState = PGM_PAGE_GET_HNDL_VIRT_STATE(pPage);
1567#if 1
1568 /* locate all the matching physical ranges. */
1569 State.uVirtStateFound = PGM_PAGE_HNDL_VIRT_STATE_NONE;
1570 RTGCPHYS GCPhysKey = State.GCPhys;
1571 for (;;)
1572 {
1573 PPGMPHYS2VIRTHANDLER pPhys2Virt = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
1574 GCPhysKey, true /* above-or-equal */);
1575 if ( !pPhys2Virt
1576 || (pPhys2Virt->Core.Key & X86_PTE_PAE_PG_MASK) != State.GCPhys)
1577 break;
1578
1579 /* the head */
1580 GCPhysKey = pPhys2Virt->Core.KeyLast;
1581 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
1582 unsigned uState = pgmHandlerVirtualCalcState(pCur);
1583 State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
1584
1585 /* any aliases */
1586 while (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK)
1587 {
1588 pPhys2Virt = (PPGMPHYS2VIRTHANDLER)((uintptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
1589 pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
1590 uState = pgmHandlerVirtualCalcState(pCur);
1591 State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
1592 }
1593
1594 /* done? */
1595 if ((GCPhysKey & X86_PTE_PAE_PG_MASK) != State.GCPhys)
1596 break;
1597 }
1598#else
1599 /* very slow */
1600 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOneByPhysAddr, &State);
1601#endif
1602 if (State.uVirtState != State.uVirtStateFound)
1603 {
1604 AssertMsgFailed(("ram range vs virt handler flags mismatch. GCPhys=%RGp uVirtState=%#x uVirtStateFound=%#x\n",
1605 State.GCPhys, State.uVirtState, State.uVirtStateFound));
1606 State.cErrors++;
1607 }
1608 }
1609 }
1610 } /* foreach page in ram range. */
1611 } /* foreach ram range. */
1612
1613 /*
1614 * Check that the physical addresses of the virtual handlers matches up
1615 * and that they are otherwise sane.
1616 */
1617 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOne, &State);
1618
1619 /*
1620 * Do the reverse check for physical handlers.
1621 */
1622 /** @todo */
1623
1624 return State.cErrors;
1625}
1626
1627#endif /* VBOX_STRICT */
1628
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