VirtualBox

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

Last change on this file since 20767 was 20767, checked in by vboxsync, 15 years ago

Locking paranoia

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 67.7 KB
Line 
1/* $Id: PGMAllHandler.cpp 20767 2009-06-22 12:02:31Z 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 MMHyperFree(pVM, pCur);
275 pgmUnlock(pVM);
276 HWACCMFlushTLBOnAllVCpus(pVM);
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 MMHyperFree(pVM, pCur2);
751 pgmUnlock(pVM);
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 pgmLock(pVM);
904 /*
905 * Validate the range.
906 */
907 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
908 if (RT_LIKELY(pCur))
909 {
910 if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
911 && GCPhysPage <= pCur->Core.KeyLast))
912 {
913 Assert(!(pCur->Core.Key & PAGE_OFFSET_MASK));
914 Assert((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
915
916 AssertReturnStmt( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
917 || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL,
918 pgmUnlock(pVM), VERR_ACCESS_DENIED);
919
920 /*
921 * Change the page status.
922 */
923 PPGMPAGE pPage;
924 int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPage, &pPage);
925 AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
926 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
927 pgmUnlock(pVM);
928#ifndef IN_RC
929 HWACCMInvalidatePhysPage(pVM, GCPhysPage);
930#endif
931 return VINF_SUCCESS;
932 }
933 pgmUnlock(pVM);
934 AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
935 GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
936 return VERR_INVALID_PARAMETER;
937 }
938 pgmUnlock(pVM);
939 AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
940 return VERR_PGM_HANDLER_NOT_FOUND;
941}
942
943
944/**
945 * Replaces an MMIO page with an MMIO2 page.
946 *
947 * This is a worker for IOMMMIOMapMMIO2Page that works in a similar way to
948 * PGMHandlerPhysicalPageTempOff but for an MMIO page. Since an MMIO page has no
949 * backing, the caller must provide a replacement page. For various reasons the
950 * replacement page must be an MMIO2 page.
951 *
952 * The caller must do required page table modifications. You can get away
953 * without making any modifations since it's an MMIO page, the cost is an extra
954 * \#PF which will the resync the page.
955 *
956 * Call PGMHandlerPhysicalReset() to restore the MMIO page.
957 *
958 * The caller may still get handler callback even after this call and must be
959 * able to deal correctly with such calls. The reason for these callbacks are
960 * either that we're executing in the recompiler (which doesn't know about this
961 * arrangement) or that we've been restored from saved state (where we won't
962 * save the change).
963 *
964 * @returns VBox status code.
965 * @param pVM The VM handle
966 * @param GCPhys The start address of the access handler. This
967 * must be a fully page aligned range or we risk
968 * messing up other handlers installed for the
969 * start and end pages.
970 * @param GCPhysPage The physical address of the page to turn off
971 * access monitoring for.
972 * @param GCPhysPageRemap The physical address of the MMIO2 page that
973 * serves as backing memory.
974 *
975 * @remark May cause a page pool flush if used on a page that is already
976 * aliased.
977 *
978 * @note This trick does only work reliably if the two pages are never ever
979 * mapped in the same page table. If they are the page pool code will
980 * be confused should either of them be flushed. See the special case
981 * of zero page aliasing mentioned in #3170.
982 *
983 */
984VMMDECL(int) PGMHandlerPhysicalPageAlias(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage, RTGCPHYS GCPhysPageRemap)
985{
986/// Assert(!IOMIsLockOwner(pVM)); /* We mustn't own any other locks when calling this */
987
988 pgmLock(pVM);
989 /*
990 * Lookup and validate the range.
991 */
992 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
993 if (RT_LIKELY(pCur))
994 {
995 if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
996 && GCPhysPage <= pCur->Core.KeyLast))
997 {
998 AssertReturnStmt(pCur->enmType == PGMPHYSHANDLERTYPE_MMIO, pgmUnlock(pVM), VERR_ACCESS_DENIED);
999 AssertReturnStmt(!(pCur->Core.Key & PAGE_OFFSET_MASK), pgmUnlock(pVM), VERR_INVALID_PARAMETER);
1000 AssertReturnStmt((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK, pgmUnlock(pVM), VERR_INVALID_PARAMETER);
1001
1002 /*
1003 * Get and validate the two pages.
1004 */
1005 PPGMPAGE pPageRemap;
1006 int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPageRemap, &pPageRemap);
1007 AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
1008 AssertMsgReturnStmt(PGM_PAGE_GET_TYPE(pPageRemap) == PGMPAGETYPE_MMIO2,
1009 ("GCPhysPageRemap=%RGp %R[pgmpage]\n", GCPhysPageRemap, pPageRemap),
1010 pgmUnlock(pVM), VERR_PGM_PHYS_NOT_MMIO2);
1011
1012 PPGMPAGE pPage;
1013 rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPage, &pPage);
1014 AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
1015 if (PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO)
1016 {
1017 AssertMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO,
1018 ("GCPhysPage=%RGp %R[pgmpage]\n", GCPhysPage, pPage),
1019 VERR_PGM_PHYS_NOT_MMIO2);
1020 if (PGM_PAGE_GET_HCPHYS(pPage) == PGM_PAGE_GET_HCPHYS(pPageRemap))
1021 {
1022 pgmUnlock(pVM);
1023 return VINF_PGM_HANDLER_ALREADY_ALIASED;
1024 }
1025
1026 /*
1027 * The page is already mapped as some other page, reset it
1028 * to an MMIO/ZERO page before doing the new mapping.
1029 */
1030 Log(("PGMHandlerPhysicalPageAlias: GCPhysPage=%RGp (%R[pgmpage]; %RHp -> %RHp\n",
1031 GCPhysPage, pPage, PGM_PAGE_GET_HCPHYS(pPage), PGM_PAGE_GET_HCPHYS(pPageRemap)));
1032 pgmHandlerPhysicalResetAliasedPage(pVM, pPage, GCPhysPage);
1033 }
1034 Assert(PGM_PAGE_IS_ZERO(pPage));
1035
1036 /*
1037 * Do the actual remapping here.
1038 * This page now serves as an alias for the backing memory specified.
1039 */
1040 LogFlow(("PGMHandlerPhysicalPageAlias: %RGp (%R[pgmpage]) alias for %RGp (%R[pgmpage])\n",
1041 GCPhysPage, pPage, GCPhysPageRemap, pPageRemap ));
1042 PGM_PAGE_SET_HCPHYS(pPage, PGM_PAGE_GET_HCPHYS(pPageRemap));
1043 PGM_PAGE_SET_TYPE(pPage, PGMPAGETYPE_MMIO2_ALIAS_MMIO);
1044 PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
1045 PGM_PAGE_SET_PAGEID(pPage, PGM_PAGE_GET_PAGEID(pPageRemap));
1046 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
1047 LogFlow(("PGMHandlerPhysicalPageAlias: => %R[pgmpage]\n", pPage));
1048
1049 pgmUnlock(pVM);
1050#ifndef IN_RC
1051 HWACCMInvalidatePhysPage(pVM, GCPhysPage);
1052#endif
1053 return VINF_SUCCESS;
1054 }
1055
1056 pgmUnlock(pVM);
1057 AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
1058 GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
1059 return VERR_INVALID_PARAMETER;
1060 }
1061
1062 pgmUnlock(pVM);
1063 AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
1064 return VERR_PGM_HANDLER_NOT_FOUND;
1065}
1066
1067/**
1068 * Replaces an MMIO page with an arbitrary HC page.
1069 *
1070 * This is a worker for IOMMMIOMapMMIO2Page that works in a similar way to
1071 * PGMHandlerPhysicalPageTempOff but for an MMIO page. Since an MMIO page has no
1072 * backing, the caller must provide a replacement page. For various reasons the
1073 * replacement page must be an MMIO2 page.
1074 *
1075 * The caller must do required page table modifications. You can get away
1076 * without making any modifations since it's an MMIO page, the cost is an extra
1077 * \#PF which will the resync the page.
1078 *
1079 * Call PGMHandlerPhysicalReset() to restore the MMIO page.
1080 *
1081 * The caller may still get handler callback even after this call and must be
1082 * able to deal correctly with such calls. The reason for these callbacks are
1083 * either that we're executing in the recompiler (which doesn't know about this
1084 * arrangement) or that we've been restored from saved state (where we won't
1085 * save the change).
1086 *
1087 * @returns VBox status code.
1088 * @param pVM The VM handle
1089 * @param GCPhys The start address of the access handler. This
1090 * must be a fully page aligned range or we risk
1091 * messing up other handlers installed for the
1092 * start and end pages.
1093 * @param GCPhysPage The physical address of the page to turn off
1094 * access monitoring for.
1095 * @param HCPhysPageRemap The physical address of the HC page that
1096 * serves as backing memory.
1097 *
1098 * @remark May cause a page pool flush if used on a page that is already
1099 * aliased.
1100 */
1101VMMDECL(int) PGMHandlerPhysicalPageAliasHC(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage, RTHCPHYS HCPhysPageRemap)
1102{
1103/// Assert(!IOMIsLockOwner(pVM)); /* We mustn't own any other locks when calling this */
1104
1105 /*
1106 * Lookup and validate the range.
1107 */
1108 pgmLock(pVM);
1109 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1110 if (RT_LIKELY(pCur))
1111 {
1112 if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
1113 && GCPhysPage <= pCur->Core.KeyLast))
1114 {
1115 AssertReturnStmt(pCur->enmType == PGMPHYSHANDLERTYPE_MMIO, pgmUnlock(pVM), VERR_ACCESS_DENIED);
1116 AssertReturnStmt(!(pCur->Core.Key & PAGE_OFFSET_MASK), pgmUnlock(pVM), VERR_INVALID_PARAMETER);
1117 AssertReturnStmt((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK, pgmUnlock(pVM), VERR_INVALID_PARAMETER);
1118
1119 /*
1120 * Get and validate the pages.
1121 */
1122 PPGMPAGE pPage;
1123 int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPage, &pPage);
1124 AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
1125 if (PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO)
1126 {
1127 pgmUnlock(pVM);
1128 AssertMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO,
1129 ("GCPhysPage=%RGp %R[pgmpage]\n", GCPhysPage, pPage),
1130 VERR_PGM_PHYS_NOT_MMIO2);
1131 return VINF_PGM_HANDLER_ALREADY_ALIASED;
1132 }
1133 Assert(PGM_PAGE_IS_ZERO(pPage));
1134
1135 /*
1136 * Do the actual remapping here.
1137 * This page now serves as an alias for the backing memory specified.
1138 */
1139 LogFlow(("PGMHandlerPhysicalPageAlias: %RGp (%R[pgmpage]) alias for %RHp\n",
1140 GCPhysPage, pPage, HCPhysPageRemap));
1141 PGM_PAGE_SET_HCPHYS(pPage, HCPhysPageRemap);
1142 PGM_PAGE_SET_TYPE(pPage, PGMPAGETYPE_MMIO2_ALIAS_MMIO);
1143 PGM_PAGE_SET_STATE(pPage, PGM_PAGE_STATE_ALLOCATED);
1144 /** @todo hack alert
1145 * This needs to be done properly. Currently we get away with it as the recompiler directly calls
1146 * IOM read and write functions. Access through PGMPhysRead/Write will crash the process.
1147 */
1148 PGM_PAGE_SET_PAGEID(pPage, NIL_GMM_PAGEID);
1149 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
1150 LogFlow(("PGMHandlerPhysicalPageAliasHC: => %R[pgmpage]\n", pPage));
1151 pgmUnlock(pVM);
1152#ifndef IN_RC
1153 HWACCMInvalidatePhysPage(pVM, GCPhysPage);
1154#endif
1155 return VINF_SUCCESS;
1156 }
1157 pgmUnlock(pVM);
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 pgmUnlock(pVM);
1163
1164 AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
1165 return VERR_PGM_HANDLER_NOT_FOUND;
1166}
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 pgmLock(pVM);
1184 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1185 if (pCur)
1186 {
1187 Assert(GCPhys >= pCur->Core.Key && GCPhys <= pCur->Core.KeyLast);
1188 Assert( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
1189 || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL
1190 || pCur->enmType == PGMPHYSHANDLERTYPE_MMIO);
1191 pgmUnlock(pVM);
1192 return true;
1193 }
1194 pgmUnlock(pVM);
1195 return false;
1196}
1197
1198
1199/**
1200 * Checks if it's an disabled all access handler or write access handler at the
1201 * given address.
1202 *
1203 * @returns true if it's an all access handler, false if it's a write access
1204 * handler.
1205 * @param pVM Pointer to the shared VM structure.
1206 * @param GCPhys The address of the page with a disabled handler.
1207 *
1208 * @remarks The caller, PGMR3PhysTlbGCPhys2Ptr, must hold the PGM lock.
1209 */
1210bool pgmHandlerPhysicalIsAll(PVM pVM, RTGCPHYS GCPhys)
1211{
1212 pgmLock(pVM);
1213 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1214 if (!pCur)
1215 {
1216 pgmUnlock(pVM);
1217 AssertFailed();
1218 return true;
1219 }
1220 Assert( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
1221 || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL
1222 || pCur->enmType == PGMPHYSHANDLERTYPE_MMIO); /* sanity */
1223 /* Only whole pages can be disabled. */
1224 Assert( pCur->Core.Key <= (GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK)
1225 && pCur->Core.KeyLast >= (GCPhys | PAGE_OFFSET_MASK));
1226
1227 bool bRet = pCur->enmType != PGMPHYSHANDLERTYPE_PHYSICAL_WRITE;
1228 pgmUnlock(pVM);
1229 return bRet;
1230}
1231
1232
1233/**
1234 * Check if particular guest's VA is being monitored.
1235 *
1236 * @returns true or false
1237 * @param pVM VM handle.
1238 * @param GCPtr Virtual address.
1239 * @remarks Will acquire the PGM lock.
1240 * @threads Any.
1241 */
1242VMMDECL(bool) PGMHandlerVirtualIsRegistered(PVM pVM, RTGCPTR GCPtr)
1243{
1244 pgmLock(pVM);
1245 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGet(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, GCPtr);
1246 pgmUnlock(pVM);
1247
1248 return pCur != NULL;
1249}
1250
1251
1252/**
1253 * Search for virtual handler with matching physical address
1254 *
1255 * @returns VBox status code
1256 * @param pVM The VM handle.
1257 * @param GCPhys GC physical address to search for.
1258 * @param ppVirt Where to store the pointer to the virtual handler structure.
1259 * @param piPage Where to store the pointer to the index of the cached physical page.
1260 */
1261int pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage)
1262{
1263 STAM_PROFILE_START(&pVM->pgm.s.CTX_MID_Z(Stat,VirtHandlerSearchByPhys), a);
1264 Assert(ppVirt);
1265
1266 pgmLock(pVM);
1267 PPGMPHYS2VIRTHANDLER pCur;
1268 pCur = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, GCPhys);
1269 if (pCur)
1270 {
1271 /* found a match! */
1272 *ppVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
1273 *piPage = pCur - &(*ppVirt)->aPhysToVirt[0];
1274 pgmUnlock(pVM);
1275
1276#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1277 AssertRelease(pCur->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD);
1278#endif
1279 LogFlow(("PHYS2VIRT: found match for %RGp -> %RGv *piPage=%#x\n", GCPhys, (*ppVirt)->Core.Key, *piPage));
1280 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_MID_Z(Stat,VirtHandlerSearchByPhys), a);
1281 return VINF_SUCCESS;
1282 }
1283
1284 pgmUnlock(pVM);
1285 *ppVirt = NULL;
1286 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_MID_Z(Stat,VirtHandlerSearchByPhys), a);
1287 return VERR_PGM_HANDLER_NOT_FOUND;
1288}
1289
1290
1291/**
1292 * Deal with aliases in phys2virt.
1293 *
1294 * As pointed out by the various todos, this currently only deals with
1295 * aliases where the two ranges match 100%.
1296 *
1297 * @param pVM The VM handle.
1298 * @param pPhys2Virt The node we failed insert.
1299 */
1300static void pgmHandlerVirtualInsertAliased(PVM pVM, PPGMPHYS2VIRTHANDLER pPhys2Virt)
1301{
1302 /*
1303 * First find the node which is conflicting with us.
1304 */
1305 /** @todo Deal with partial overlapping. (Unlikly situation, so I'm too lazy to do anything about it now.) */
1306 /** @todo check if the current head node covers the ground we do. This is highly unlikely
1307 * and I'm too lazy to implement this now as it will require sorting the list and stuff like that. */
1308 PPGMPHYS2VIRTHANDLER pHead = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
1309#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1310 AssertReleaseMsg(pHead != pPhys2Virt, ("%RGp-%RGp offVirtHandler=%#RX32\n",
1311 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler));
1312#endif
1313 if (RT_UNLIKELY(!pHead || pHead->Core.KeyLast != pPhys2Virt->Core.KeyLast))
1314 {
1315 /** @todo do something clever here... */
1316 LogRel(("pgmHandlerVirtualInsertAliased: %RGp-%RGp\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
1317 pPhys2Virt->offNextAlias = 0;
1318 return;
1319 }
1320
1321 /*
1322 * Insert ourselves as the next node.
1323 */
1324 if (!(pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
1325 pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IN_TREE;
1326 else
1327 {
1328 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pHead + (pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
1329 pPhys2Virt->offNextAlias = ((intptr_t)pNext - (intptr_t)pPhys2Virt)
1330 | PGMPHYS2VIRTHANDLER_IN_TREE;
1331 }
1332 pHead->offNextAlias = ((intptr_t)pPhys2Virt - (intptr_t)pHead)
1333 | (pHead->offNextAlias & ~PGMPHYS2VIRTHANDLER_OFF_MASK);
1334 Log(("pgmHandlerVirtualInsertAliased: %RGp-%RGp offNextAlias=%#RX32\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
1335}
1336
1337
1338/**
1339 * Resets one virtual handler range.
1340 *
1341 * This is called by HandlerVirtualUpdate when it has detected some kind of
1342 * problem and have started clearing the virtual handler page states (or
1343 * when there have been registration/deregistrations). For this reason this
1344 * function will only update the page status if it's lower than desired.
1345 *
1346 * @returns 0
1347 * @param pNode Pointer to a PGMVIRTHANDLER.
1348 * @param pvUser The VM handle.
1349 */
1350DECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
1351{
1352 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
1353 PVM pVM = (PVM)pvUser;
1354
1355 Assert(PGMIsLockOwner(pVM));
1356 /*
1357 * Iterate the pages and apply the new state.
1358 */
1359 unsigned uState = pgmHandlerVirtualCalcState(pCur);
1360 PPGMRAMRANGE pRamHint = NULL;
1361 RTGCUINTPTR offPage = ((RTGCUINTPTR)pCur->Core.Key & PAGE_OFFSET_MASK);
1362 RTGCUINTPTR cbLeft = pCur->cb;
1363 for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
1364 {
1365 PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
1366 if (pPhys2Virt->Core.Key != NIL_RTGCPHYS)
1367 {
1368 /*
1369 * Update the page state wrt virtual handlers.
1370 */
1371 PPGMPAGE pPage;
1372 int rc = pgmPhysGetPageWithHintEx(&pVM->pgm.s, pPhys2Virt->Core.Key, &pPage, &pRamHint);
1373 if ( RT_SUCCESS(rc)
1374 && PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
1375 PGM_PAGE_SET_HNDL_VIRT_STATE(pPage, uState);
1376 else
1377 AssertRC(rc);
1378
1379 /*
1380 * Need to insert the page in the Phys2Virt lookup tree?
1381 */
1382 if (pPhys2Virt->Core.KeyLast == NIL_RTGCPHYS)
1383 {
1384#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1385 AssertRelease(!pPhys2Virt->offNextAlias);
1386#endif
1387 unsigned cbPhys = cbLeft;
1388 if (cbPhys > PAGE_SIZE - offPage)
1389 cbPhys = PAGE_SIZE - offPage;
1390 else
1391 Assert(iPage == pCur->cPages - 1);
1392 pPhys2Virt->Core.KeyLast = pPhys2Virt->Core.Key + cbPhys - 1; /* inclusive */
1393 pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IS_HEAD | PGMPHYS2VIRTHANDLER_IN_TREE;
1394 if (!RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, &pPhys2Virt->Core))
1395 pgmHandlerVirtualInsertAliased(pVM, pPhys2Virt);
1396#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1397 else
1398 AssertReleaseMsg(RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key) == &pPhys2Virt->Core,
1399 ("%RGp-%RGp offNextAlias=%#RX32\n",
1400 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
1401#endif
1402 Log2(("PHYS2VIRT: Insert physical range %RGp-%RGp offNextAlias=%#RX32 %s\n",
1403 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
1404 }
1405 }
1406 cbLeft -= PAGE_SIZE - offPage;
1407 offPage = 0;
1408 }
1409
1410 return 0;
1411}
1412
1413#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
1414
1415/**
1416 * Worker for pgmHandlerVirtualDumpPhysPages.
1417 *
1418 * @returns 0 (continue enumeration).
1419 * @param pNode The virtual handler node.
1420 * @param pvUser User argument, unused.
1421 */
1422static DECLCALLBACK(int) pgmHandlerVirtualDumpPhysPagesCallback(PAVLROGCPHYSNODECORE pNode, void *pvUser)
1423{
1424 PPGMPHYS2VIRTHANDLER pCur = (PPGMPHYS2VIRTHANDLER)pNode;
1425 PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
1426 Log(("PHYS2VIRT: Range %RGp-%RGp for virtual handler: %s\n", pCur->Core.Key, pCur->Core.KeyLast, pVirt->pszDesc));
1427 return 0;
1428}
1429
1430
1431/**
1432 * Assertion / logging helper for dumping all the
1433 * virtual handlers to the log.
1434 *
1435 * @param pVM Pointer to the shared VM structure.
1436 */
1437void pgmHandlerVirtualDumpPhysPages(PVM pVM)
1438{
1439 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, true /* from left */,
1440 pgmHandlerVirtualDumpPhysPagesCallback, 0);
1441}
1442
1443#endif /* VBOX_STRICT || LOG_ENABLED */
1444#ifdef VBOX_STRICT
1445
1446/**
1447 * State structure used by the PGMAssertHandlerAndFlagsInSync() function
1448 * and its AVL enumerators.
1449 */
1450typedef struct PGMAHAFIS
1451{
1452 /** The current physical address. */
1453 RTGCPHYS GCPhys;
1454 /** The state we've calculated. */
1455 unsigned uVirtStateFound;
1456 /** The state we're matching up to. */
1457 unsigned uVirtState;
1458 /** Number of errors. */
1459 unsigned cErrors;
1460 /** The VM handle. */
1461 PVM pVM;
1462} PGMAHAFIS, *PPGMAHAFIS;
1463
1464
1465#if 0 /* unused */
1466/**
1467 * Verify virtual handler by matching physical address.
1468 *
1469 * @returns 0
1470 * @param pNode Pointer to a PGMVIRTHANDLER.
1471 * @param pvUser Pointer to user parameter.
1472 */
1473static DECLCALLBACK(int) pgmHandlerVirtualVerifyOneByPhysAddr(PAVLROGCPTRNODECORE pNode, void *pvUser)
1474{
1475 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
1476 PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
1477
1478 for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
1479 {
1480 if ((pCur->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) == pState->GCPhys)
1481 {
1482 unsigned uState = pgmHandlerVirtualCalcState(pCur);
1483 if (pState->uVirtState < uState)
1484 {
1485 error
1486 }
1487
1488 if (pState->uVirtState == uState)
1489 break; //??
1490 }
1491 }
1492 return 0;
1493}
1494#endif /* unused */
1495
1496
1497/**
1498 * Verify a virtual handler (enumeration callback).
1499 *
1500 * Called by PGMAssertHandlerAndFlagsInSync to check the sanity of all
1501 * the virtual handlers, esp. that the physical addresses matches up.
1502 *
1503 * @returns 0
1504 * @param pNode Pointer to a PGMVIRTHANDLER.
1505 * @param pvUser Pointer to a PPGMAHAFIS structure.
1506 */
1507static DECLCALLBACK(int) pgmHandlerVirtualVerifyOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
1508{
1509 PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)pNode;
1510 PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
1511 PVM pVM = pState->pVM;
1512
1513 /*
1514 * Validate the type and calc state.
1515 */
1516 switch (pVirt->enmType)
1517 {
1518 case PGMVIRTHANDLERTYPE_WRITE:
1519 case PGMVIRTHANDLERTYPE_ALL:
1520 break;
1521 default:
1522 AssertMsgFailed(("unknown/wrong enmType=%d\n", pVirt->enmType));
1523 pState->cErrors++;
1524 return 0;
1525 }
1526 const unsigned uState = pgmHandlerVirtualCalcState(pVirt);
1527
1528 /*
1529 * Check key alignment.
1530 */
1531 if ( (pVirt->aPhysToVirt[0].Core.Key & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->Core.Key & PAGE_OFFSET_MASK)
1532 && pVirt->aPhysToVirt[0].Core.Key != NIL_RTGCPHYS)
1533 {
1534 AssertMsgFailed(("virt handler phys has incorrect key! %RGp %RGv %s\n",
1535 pVirt->aPhysToVirt[0].Core.Key, pVirt->Core.Key, R3STRING(pVirt->pszDesc)));
1536 pState->cErrors++;
1537 }
1538
1539 if ( (pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->Core.KeyLast & PAGE_OFFSET_MASK)
1540 && pVirt->aPhysToVirt[pVirt->cPages - 1].Core.Key != NIL_RTGCPHYS)
1541 {
1542 AssertMsgFailed(("virt handler phys has incorrect key! %RGp %RGv %s\n",
1543 pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast, pVirt->Core.KeyLast, R3STRING(pVirt->pszDesc)));
1544 pState->cErrors++;
1545 }
1546
1547 /*
1548 * Check pages for sanity and state.
1549 */
1550 RTGCUINTPTR GCPtr = (RTGCUINTPTR)pVirt->Core.Key;
1551 for (unsigned iPage = 0; iPage < pVirt->cPages; iPage++, GCPtr += PAGE_SIZE)
1552 {
1553 for (unsigned i=0;i<pVM->cCPUs;i++)
1554 {
1555 PVMCPU pVCpu = &pVM->aCpus[i];
1556
1557 RTGCPHYS GCPhysGst;
1558 uint64_t fGst;
1559 int rc = PGMGstGetPage(pVCpu, (RTGCPTR)GCPtr, &fGst, &GCPhysGst);
1560 if ( rc == VERR_PAGE_NOT_PRESENT
1561 || rc == VERR_PAGE_TABLE_NOT_PRESENT)
1562 {
1563 if (pVirt->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
1564 {
1565 AssertMsgFailed(("virt handler phys out of sync. %RGp GCPhysNew=~0 iPage=%#x %RGv %s\n",
1566 pVirt->aPhysToVirt[iPage].Core.Key, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
1567 pState->cErrors++;
1568 }
1569 continue;
1570 }
1571
1572 AssertRCReturn(rc, 0);
1573 if ((pVirt->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) != GCPhysGst)
1574 {
1575 AssertMsgFailed(("virt handler phys out of sync. %RGp GCPhysGst=%RGp iPage=%#x %RGv %s\n",
1576 pVirt->aPhysToVirt[iPage].Core.Key, GCPhysGst, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
1577 pState->cErrors++;
1578 continue;
1579 }
1580
1581 PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhysGst);
1582 if (!pPage)
1583 {
1584 AssertMsgFailed(("virt handler getting ram flags. GCPhysGst=%RGp iPage=%#x %RGv %s\n",
1585 GCPhysGst, iPage, GCPtr, R3STRING(pVirt->pszDesc)));
1586 pState->cErrors++;
1587 continue;
1588 }
1589
1590 if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
1591 {
1592 AssertMsgFailed(("virt handler state mismatch. pPage=%R[pgmpage] GCPhysGst=%RGp iPage=%#x %RGv state=%d expected>=%d %s\n",
1593 pPage, GCPhysGst, iPage, GCPtr, PGM_PAGE_GET_HNDL_VIRT_STATE(pPage), uState, R3STRING(pVirt->pszDesc)));
1594 pState->cErrors++;
1595 continue;
1596 }
1597 } /* for each VCPU */
1598 } /* for pages in virtual mapping. */
1599
1600 return 0;
1601}
1602
1603
1604/**
1605 * Asserts that the handlers+guest-page-tables == ramrange-flags and
1606 * that the physical addresses associated with virtual handlers are correct.
1607 *
1608 * @returns Number of mismatches.
1609 * @param pVM The VM handle.
1610 */
1611VMMDECL(unsigned) PGMAssertHandlerAndFlagsInSync(PVM pVM)
1612{
1613 PPGM pPGM = &pVM->pgm.s;
1614 PGMAHAFIS State;
1615 State.GCPhys = 0;
1616 State.uVirtState = 0;
1617 State.uVirtStateFound = 0;
1618 State.cErrors = 0;
1619 State.pVM = pVM;
1620
1621 Assert(PGMIsLockOwner(pVM));
1622
1623 /*
1624 * Check the RAM flags against the handlers.
1625 */
1626 for (PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges); pRam; pRam = pRam->CTX_SUFF(pNext))
1627 {
1628 const unsigned cPages = pRam->cb >> PAGE_SHIFT;
1629 for (unsigned iPage = 0; iPage < cPages; iPage++)
1630 {
1631 PGMPAGE const *pPage = &pRam->aPages[iPage];
1632 if (PGM_PAGE_HAS_ANY_HANDLERS(pPage))
1633 {
1634 State.GCPhys = pRam->GCPhys + (iPage << PAGE_SHIFT);
1635
1636 /*
1637 * Physical first - calculate the state based on the handlers
1638 * active on the page, then compare.
1639 */
1640 if (PGM_PAGE_HAS_ANY_PHYSICAL_HANDLERS(pPage))
1641 {
1642 /* the first */
1643 PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys);
1644 if (!pPhys)
1645 {
1646 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys, true);
1647 if ( pPhys
1648 && pPhys->Core.Key > (State.GCPhys + PAGE_SIZE - 1))
1649 pPhys = NULL;
1650 Assert(!pPhys || pPhys->Core.Key >= State.GCPhys);
1651 }
1652 if (pPhys)
1653 {
1654 unsigned uState = pgmHandlerPhysicalCalcState(pPhys);
1655
1656 /* more? */
1657 while (pPhys->Core.KeyLast < (State.GCPhys | PAGE_OFFSET_MASK))
1658 {
1659 PPGMPHYSHANDLER pPhys2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers,
1660 pPhys->Core.KeyLast + 1, true);
1661 if ( !pPhys2
1662 || pPhys2->Core.Key > (State.GCPhys | PAGE_OFFSET_MASK))
1663 break;
1664 unsigned uState2 = pgmHandlerPhysicalCalcState(pPhys2);
1665 uState = RT_MAX(uState, uState2);
1666 pPhys = pPhys2;
1667 }
1668
1669 /* compare.*/
1670 if ( PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != uState
1671 && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
1672 {
1673 AssertMsgFailed(("ram range vs phys handler flags mismatch. GCPhys=%RGp state=%d expected=%d %s\n",
1674 State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), uState, pPhys->pszDesc));
1675 State.cErrors++;
1676 }
1677
1678#ifdef IN_RING3
1679 /* validate that REM is handling it. */
1680 if ( !REMR3IsPageAccessHandled(pVM, State.GCPhys)
1681 /* ignore shadowed ROM for the time being. */
1682 && PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_ROM_SHADOW)
1683 {
1684 AssertMsgFailed(("ram range vs phys handler REM mismatch. GCPhys=%RGp state=%d %s\n",
1685 State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), pPhys->pszDesc));
1686 State.cErrors++;
1687 }
1688#endif
1689 }
1690 else
1691 {
1692 AssertMsgFailed(("ram range vs phys handler mismatch. no handler for GCPhys=%RGp\n", State.GCPhys));
1693 State.cErrors++;
1694 }
1695 }
1696
1697 /*
1698 * Virtual handlers.
1699 */
1700 if (PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage))
1701 {
1702 State.uVirtState = PGM_PAGE_GET_HNDL_VIRT_STATE(pPage);
1703#if 1
1704 /* locate all the matching physical ranges. */
1705 State.uVirtStateFound = PGM_PAGE_HNDL_VIRT_STATE_NONE;
1706 RTGCPHYS GCPhysKey = State.GCPhys;
1707 for (;;)
1708 {
1709 PPGMPHYS2VIRTHANDLER pPhys2Virt = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers,
1710 GCPhysKey, true /* above-or-equal */);
1711 if ( !pPhys2Virt
1712 || (pPhys2Virt->Core.Key & X86_PTE_PAE_PG_MASK) != State.GCPhys)
1713 break;
1714
1715 /* the head */
1716 GCPhysKey = pPhys2Virt->Core.KeyLast;
1717 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
1718 unsigned uState = pgmHandlerVirtualCalcState(pCur);
1719 State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
1720
1721 /* any aliases */
1722 while (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK)
1723 {
1724 pPhys2Virt = (PPGMPHYS2VIRTHANDLER)((uintptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
1725 pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
1726 uState = pgmHandlerVirtualCalcState(pCur);
1727 State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
1728 }
1729
1730 /* done? */
1731 if ((GCPhysKey & X86_PTE_PAE_PG_MASK) != State.GCPhys)
1732 break;
1733 }
1734#else
1735 /* very slow */
1736 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOneByPhysAddr, &State);
1737#endif
1738 if (State.uVirtState != State.uVirtStateFound)
1739 {
1740 AssertMsgFailed(("ram range vs virt handler flags mismatch. GCPhys=%RGp uVirtState=%#x uVirtStateFound=%#x\n",
1741 State.GCPhys, State.uVirtState, State.uVirtStateFound));
1742 State.cErrors++;
1743 }
1744 }
1745 }
1746 } /* foreach page in ram range. */
1747 } /* foreach ram range. */
1748
1749 /*
1750 * Check that the physical addresses of the virtual handlers matches up
1751 * and that they are otherwise sane.
1752 */
1753 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOne, &State);
1754
1755 /*
1756 * Do the reverse check for physical handlers.
1757 */
1758 /** @todo */
1759
1760 return State.cErrors;
1761}
1762
1763#endif /* VBOX_STRICT */
1764
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