VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

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