VirtualBox

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

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

PGM,MM: Made VBOX_WITH_NEW_PHYS_CODE compile and link.

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