VirtualBox

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

Last change on this file since 47719 was 47719, checked in by vboxsync, 11 years ago

No MMIO2 aliasing when doing full IEM verification runs.

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