VirtualBox

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

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

PGM,IOM: Implemented MMIO2 aliases for MMIO pages. This involved some forgotten changes to the MMIO implementation too - changing the page type to MMIO, freeing the backing RAM page.

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

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