VirtualBox

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

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

VMM + VBox/cdefs.h: consolidated all the XYZ*DECLS of the VMM into VMM*DECL. Removed dead DECL and IN_XYZ* macros.

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