VirtualBox

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

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

Removed assertion

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