VirtualBox

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

Last change on this file since 9026 was 9008, checked in by vboxsync, 17 years ago

Changes for proper flushing of the TLB for physical registration changes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 55.0 KB
Line 
1/* $Id: PGMAllHandler.cpp 9008 2008-05-21 10:17:41Z 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 */
81PGMDECL(int) PGMHandlerPhysicalRegisterEx(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast,
82 R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3, RTR3PTR pvUserR3,
83 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0, RTR0PTR pvUserR0,
84 GCPTRTYPE(PFNPGMGCPHYSHANDLER) pfnHandlerGC, RTGCPTR 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=%VGv pvUserGC=%VGv pszDesc=%s\n",
88 enmType, GCPhys, GCPhysLast, pfnHandlerR3, pvUserR3, pfnHandlerR0, pvUserR0, pfnHandlerGC, pvUserGC, HCSTRING(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 */
286PGMDECL(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, HCSTRING(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 */
487PGMDECL(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 */
599PGMDECL(int) PGMHandlerPhysicalChangeCallbacks(PVM pVM, RTGCPHYS GCPhys,
600 R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3, RTR3PTR pvUserR3,
601 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0, RTR0PTR pvUserR0,
602 GCPTRTYPE(PFNPGMGCPHYSHANDLER) pfnHandlerGC, RTGCPTR 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 */
643PGMDECL(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 */
709PGMDECL(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 */
786PGMDECL(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 */
866PGMDECL(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 return VINF_SUCCESS;
892 }
893
894 AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
895 GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
896 return VERR_INVALID_PARAMETER;
897 }
898
899 AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
900 return VERR_PGM_HANDLER_NOT_FOUND;
901}
902
903
904/**
905 * Turns access monitoring of a page within a monitored
906 * physical write/all page access handler regio back on.
907 *
908 * The caller must do required page table modifications.
909 *
910 * @returns VBox status code.
911 * @param pVM VM Handle
912 * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
913 * This must be a fully page aligned range or we risk messing up other
914 * handlers installed for the start and end pages.
915 * @param GCPhysPage Physical address of the page to turn on access monitoring for.
916 */
917PGMDECL(int) PGMHandlerPhysicalPageReset(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage)
918{
919 /*
920 * Validate the range.
921 */
922 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
923 if (pCur)
924 {
925 if ( GCPhysPage >= pCur->Core.Key
926 && GCPhysPage <= pCur->Core.KeyLast)
927 {
928 Assert(!(pCur->Core.Key & PAGE_OFFSET_MASK));
929 Assert((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
930
931 AssertReturn( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
932 || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL,
933 VERR_ACCESS_DENIED);
934
935 /*
936 * Change the page status.
937 */
938 PPGMPAGE pPage;
939 int rc = pgmPhysGetPageEx(&pVM->pgm.s, GCPhysPage, &pPage);
940 AssertRCReturn(rc, rc);
941 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, pgmHandlerPhysicalCalcState(pCur));
942 return VINF_SUCCESS;
943 }
944
945 AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
946 GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
947 return VERR_INVALID_PARAMETER;
948 }
949
950 AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
951 return VERR_PGM_HANDLER_NOT_FOUND;
952}
953
954
955/**
956 * Checks if a physical range is handled
957 *
958 * @returns boolean
959 * @param pVM VM Handle
960 * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
961 */
962PGMDECL(bool) PGMHandlerPhysicalIsRegistered(PVM pVM, RTGCPHYS GCPhys)
963{
964 /*
965 * Find the handler.
966 */
967 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
968 if (pCur)
969 {
970 if ( GCPhys >= pCur->Core.Key
971 && GCPhys <= pCur->Core.KeyLast)
972 {
973 Assert( pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_WRITE
974 || pCur->enmType == PGMPHYSHANDLERTYPE_PHYSICAL_ALL
975 || pCur->enmType == PGMPHYSHANDLERTYPE_MMIO);
976 return true;
977 }
978 }
979
980 return false;
981}
982
983
984/**
985 * Search for virtual handler with matching physical address
986 *
987 * @returns VBox status code
988 * @param pVM The VM handle.
989 * @param GCPhys GC physical address to search for.
990 * @param ppVirt Where to store the pointer to the virtual handler structure.
991 * @param piPage Where to store the pointer to the index of the cached physical page.
992 */
993int pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage)
994{
995 STAM_PROFILE_START(CTXSUFF(&pVM->pgm.s.StatVirtHandleSearchByPhys), a);
996 Assert(ppVirt);
997
998 PPGMPHYS2VIRTHANDLER pCur;
999 pCur = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRangeGet(&CTXSUFF(pVM->pgm.s.pTrees)->PhysToVirtHandlers, GCPhys);
1000 if (pCur)
1001 {
1002 /* found a match! */
1003#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1004 AssertRelease(pCur->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD);
1005#endif
1006 *ppVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
1007 *piPage = pCur - &(*ppVirt)->aPhysToVirt[0];
1008
1009 LogFlow(("PHYS2VIRT: found match for %VGp -> %VGv *piPage=%#x\n", GCPhys, (*ppVirt)->GCPtr, *piPage));
1010 STAM_PROFILE_STOP(CTXSUFF(&pVM->pgm.s.StatVirtHandleSearchByPhys), a);
1011 return VINF_SUCCESS;
1012 }
1013
1014 *ppVirt = NULL;
1015 STAM_PROFILE_STOP(CTXSUFF(&pVM->pgm.s.StatVirtHandleSearchByPhys), a);
1016 return VERR_PGM_HANDLER_NOT_FOUND;
1017}
1018
1019
1020/**
1021 * Deal with aliases in phys2virt.
1022 *
1023 * As pointed out by the various todos, this currently only deals with
1024 * aliases where the two ranges match 100%.
1025 *
1026 * @param pVM The VM handle.
1027 * @param pPhys2Virt The node we failed insert.
1028 */
1029static void pgmHandlerVirtualInsertAliased(PVM pVM, PPGMPHYS2VIRTHANDLER pPhys2Virt)
1030{
1031 /*
1032 * First find the node which is conflicting with us.
1033 */
1034 /** @todo Deal with partial overlapping. (Unlikly situation, so I'm too lazy to do anything about it now.) */
1035 /** @todo check if the current head node covers the ground we do. This is highly unlikely
1036 * and I'm too lazy to implement this now as it will require sorting the list and stuff like that. */
1037 PPGMPHYS2VIRTHANDLER pHead = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
1038#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1039 AssertReleaseMsg(pHead != pPhys2Virt, ("%VGp-%VGp offVirtHandler=%#RX32\n",
1040 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler));
1041#endif
1042 if (RT_UNLIKELY(!pHead || pHead->Core.KeyLast != pPhys2Virt->Core.KeyLast))
1043 {
1044 /** @todo do something clever here... */
1045 LogRel(("pgmHandlerVirtualInsertAliased: %VGp-%VGp\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
1046 pPhys2Virt->offNextAlias = 0;
1047 return;
1048 }
1049
1050 /*
1051 * Insert ourselves as the next node.
1052 */
1053 if (!(pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
1054 pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IN_TREE;
1055 else
1056 {
1057 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pHead + (pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
1058 pPhys2Virt->offNextAlias = ((intptr_t)pNext - (intptr_t)pPhys2Virt)
1059 | PGMPHYS2VIRTHANDLER_IN_TREE;
1060 }
1061 pHead->offNextAlias = ((intptr_t)pPhys2Virt - (intptr_t)pHead)
1062 | (pHead->offNextAlias & ~PGMPHYS2VIRTHANDLER_OFF_MASK);
1063 Log(("pgmHandlerVirtualInsertAliased: %VGp-%VGp offNextAlias=%#RX32\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
1064}
1065
1066
1067/**
1068 * Resets one virtual handler range.
1069 *
1070 * This is called by HandlerVirtualUpdate when it has detected some kind of
1071 * problem and have started clearing the virtual handler page states (or
1072 * when there have been registration/deregistrations). For this reason this
1073 * function will only update the page status if it's lower than desired.
1074 *
1075 * @returns 0
1076 * @param pNode Pointer to a PGMVIRTHANDLER.
1077 * @param pvUser The VM handle.
1078 */
1079DECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
1080{
1081 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
1082 PVM pVM = (PVM)pvUser;
1083
1084 /*
1085 * Iterate the pages and apply the new state.
1086 */
1087 unsigned uState = pgmHandlerVirtualCalcState(pCur);
1088 PPGMRAMRANGE pRamHint = NULL;
1089 RTGCUINTPTR offPage = ((RTGCUINTPTR)pCur->GCPtr & PAGE_OFFSET_MASK);
1090 RTGCUINTPTR cbLeft = pCur->cb;
1091 for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
1092 {
1093 PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
1094 if (pPhys2Virt->Core.Key != NIL_RTGCPHYS)
1095 {
1096 /*
1097 * Update the page state wrt virtual handlers.
1098 */
1099 PPGMPAGE pPage;
1100 int rc = pgmPhysGetPageWithHintEx(&pVM->pgm.s, pPhys2Virt->Core.Key, &pPage, &pRamHint);
1101 if ( RT_SUCCESS(rc)
1102 && PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
1103 PGM_PAGE_SET_HNDL_VIRT_STATE(pPage, uState);
1104 else
1105 AssertRC(rc);
1106
1107 /*
1108 * Need to insert the page in the Phys2Virt lookup tree?
1109 */
1110 if (pPhys2Virt->Core.KeyLast == NIL_RTGCPHYS)
1111 {
1112#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1113 AssertRelease(!pPhys2Virt->offNextAlias);
1114#endif
1115 unsigned cbPhys = cbLeft;
1116 if (cbPhys > PAGE_SIZE - offPage)
1117 cbPhys = PAGE_SIZE - offPage;
1118 else
1119 Assert(iPage == pCur->cPages - 1);
1120 pPhys2Virt->Core.KeyLast = pPhys2Virt->Core.Key + cbPhys - 1; /* inclusive */
1121 pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IS_HEAD | PGMPHYS2VIRTHANDLER_IN_TREE;
1122 if (!RTAvlroGCPhysInsert(&pVM->pgm.s.CTXSUFF(pTrees)->PhysToVirtHandlers, &pPhys2Virt->Core))
1123 pgmHandlerVirtualInsertAliased(pVM, pPhys2Virt);
1124#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1125 else
1126 AssertReleaseMsg(RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key) == &pPhys2Virt->Core,
1127 ("%VGp-%VGp offNextAlias=%#RX32\n",
1128 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
1129#endif
1130 Log2(("PHYS2VIRT: Insert physical range %VGp-%VGp offNextAlias=%#RX32 %s\n",
1131 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
1132 }
1133 }
1134 cbLeft -= PAGE_SIZE - offPage;
1135 offPage = 0;
1136 }
1137
1138 return 0;
1139}
1140
1141
1142#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
1143/**
1144 * Worker for pgmHandlerVirtualDumpPhysPages.
1145 *
1146 * @returns 0 (continue enumeration).
1147 * @param pNode The virtual handler node.
1148 * @param pvUser User argument, unused.
1149 */
1150static DECLCALLBACK(int) pgmHandlerVirtualDumpPhysPagesCallback(PAVLROGCPHYSNODECORE pNode, void *pvUser)
1151{
1152 PPGMPHYS2VIRTHANDLER pCur = (PPGMPHYS2VIRTHANDLER)pNode;
1153 PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
1154 Log(("PHYS2VIRT: Range %VGp-%VGp for virtual handler: %s\n", pCur->Core.Key, pCur->Core.KeyLast, pVirt->pszDesc));
1155 return 0;
1156}
1157
1158
1159/**
1160 * Assertion / logging helper for dumping all the
1161 * virtual handlers to the log.
1162 *
1163 * @param pVM Pointer to the shared VM structure.
1164 */
1165void pgmHandlerVirtualDumpPhysPages(PVM pVM)
1166{
1167 RTAvlroGCPhysDoWithAll(CTXSUFF(&pVM->pgm.s.pTrees)->PhysToVirtHandlers, true /* from left */,
1168 pgmHandlerVirtualDumpPhysPagesCallback, 0);
1169}
1170#endif /* VBOX_STRICT || LOG_ENABLED */
1171
1172#ifdef VBOX_STRICT
1173
1174/**
1175 * State structure used by the PGMAssertHandlerAndFlagsInSync() function
1176 * and its AVL enumerators.
1177 */
1178typedef struct PGMAHAFIS
1179{
1180 /** The current physical address. */
1181 RTGCPHYS GCPhys;
1182 /** The state we've calculated. */
1183 unsigned uVirtStateFound;
1184 /** The state we're matching up to. */
1185 unsigned uVirtState;
1186 /** Number of errors. */
1187 unsigned cErrors;
1188 /** The VM handle. */
1189 PVM pVM;
1190} PGMAHAFIS, *PPGMAHAFIS;
1191
1192
1193#if 0 /* unused */
1194/**
1195 * Verify virtual handler by matching physical address.
1196 *
1197 * @returns 0
1198 * @param pNode Pointer to a PGMVIRTHANDLER.
1199 * @param pvUser Pointer to user parameter.
1200 */
1201static DECLCALLBACK(int) pgmHandlerVirtualVerifyOneByPhysAddr(PAVLROGCPTRNODECORE pNode, void *pvUser)
1202{
1203 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
1204 PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
1205
1206 for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
1207 {
1208 if ((pCur->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) == pState->GCPhys)
1209 {
1210 unsigned uState = pgmHandlerVirtualCalcState(pCur);
1211 if (pState->uVirtState < uState)
1212 {
1213 error
1214 }
1215
1216 if (pState->uVirtState == uState)
1217 break; //??
1218 }
1219 }
1220 return 0;
1221}
1222#endif /* unused */
1223
1224
1225/**
1226 * Verify a virtual handler (enumeration callback).
1227 *
1228 * Called by PGMAssertHandlerAndFlagsInSync to check the sanity of all
1229 * the virtual handlers, esp. that the physical addresses matches up.
1230 *
1231 * @returns 0
1232 * @param pNode Pointer to a PGMVIRTHANDLER.
1233 * @param pvUser Pointer to a PPGMAHAFIS structure.
1234 */
1235static DECLCALLBACK(int) pgmHandlerVirtualVerifyOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
1236{
1237 PPGMVIRTHANDLER pVirt = (PPGMVIRTHANDLER)pNode;
1238 PPGMAHAFIS pState = (PPGMAHAFIS)pvUser;
1239 PVM pVM = pState->pVM;
1240
1241 /*
1242 * Validate the type and calc state.
1243 */
1244 switch (pVirt->enmType)
1245 {
1246 case PGMVIRTHANDLERTYPE_WRITE:
1247 case PGMVIRTHANDLERTYPE_ALL:
1248 break;
1249 default:
1250 AssertMsgFailed(("unknown/wrong enmType=%d\n", pVirt->enmType));
1251 pState->cErrors++;
1252 return 0;
1253 }
1254 const unsigned uState = pgmHandlerVirtualCalcState(pVirt);
1255
1256 /*
1257 * Check key alignment.
1258 */
1259 if ( (pVirt->aPhysToVirt[0].Core.Key & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->GCPtr & PAGE_OFFSET_MASK)
1260 && pVirt->aPhysToVirt[0].Core.Key != NIL_RTGCPHYS)
1261 {
1262 AssertMsgFailed(("virt handler phys has incorrect key! %VGp %VGv %s\n",
1263 pVirt->aPhysToVirt[0].Core.Key, pVirt->GCPtr, HCSTRING(pVirt->pszDesc)));
1264 pState->cErrors++;
1265 }
1266
1267 if ( (pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast & PAGE_OFFSET_MASK) != ((RTGCUINTPTR)pVirt->GCPtrLast & PAGE_OFFSET_MASK)
1268 && pVirt->aPhysToVirt[pVirt->cPages - 1].Core.Key != NIL_RTGCPHYS)
1269 {
1270 AssertMsgFailed(("virt handler phys has incorrect key! %VGp %VGv %s\n",
1271 pVirt->aPhysToVirt[pVirt->cPages - 1].Core.KeyLast, pVirt->GCPtrLast, HCSTRING(pVirt->pszDesc)));
1272 pState->cErrors++;
1273 }
1274
1275 /*
1276 * Check pages for sanity and state.
1277 */
1278 RTGCUINTPTR GCPtr = (RTGCUINTPTR)pVirt->GCPtr;
1279 for (unsigned iPage = 0; iPage < pVirt->cPages; iPage++, GCPtr += PAGE_SIZE)
1280 {
1281 RTGCPHYS GCPhysGst;
1282 uint64_t fGst;
1283 int rc = PGMGstGetPage(pVM, (RTGCPTR)GCPtr, &fGst, &GCPhysGst);
1284 if ( rc == VERR_PAGE_NOT_PRESENT
1285 || rc == VERR_PAGE_TABLE_NOT_PRESENT)
1286 {
1287 if (pVirt->aPhysToVirt[iPage].Core.Key != NIL_RTGCPHYS)
1288 {
1289 AssertMsgFailed(("virt handler phys out of sync. %VGp GCPhysNew=~0 iPage=%#x %VGv %s\n",
1290 pVirt->aPhysToVirt[iPage].Core.Key, iPage, GCPtr, HCSTRING(pVirt->pszDesc)));
1291 pState->cErrors++;
1292 }
1293 continue;
1294 }
1295
1296 AssertRCReturn(rc, 0);
1297 if ((pVirt->aPhysToVirt[iPage].Core.Key & X86_PTE_PAE_PG_MASK) != GCPhysGst)
1298 {
1299 AssertMsgFailed(("virt handler phys out of sync. %VGp GCPhysGst=%VGp iPage=%#x %VGv %s\n",
1300 pVirt->aPhysToVirt[iPage].Core.Key, GCPhysGst, iPage, GCPtr, HCSTRING(pVirt->pszDesc)));
1301 pState->cErrors++;
1302 continue;
1303 }
1304
1305 PPGMPAGE pPage = pgmPhysGetPage(&pVM->pgm.s, GCPhysGst);
1306 if (!pPage)
1307 {
1308 AssertMsgFailed(("virt handler getting ram flags. GCPhysGst=%VGp iPage=%#x %VGv %s\n",
1309 GCPhysGst, iPage, GCPtr, HCSTRING(pVirt->pszDesc)));
1310 pState->cErrors++;
1311 continue;
1312 }
1313
1314 if (PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) < uState)
1315 {
1316 AssertMsgFailed(("virt handler state mismatch. HCPhys=%VHp GCPhysGst=%VGp iPage=%#x %VGv state=%d expected>=%d %s\n",
1317 pPage->HCPhys, GCPhysGst, iPage, GCPtr, PGM_PAGE_GET_HNDL_VIRT_STATE(pPage), uState, HCSTRING(pVirt->pszDesc)));
1318 pState->cErrors++;
1319 continue;
1320 }
1321 } /* for pages in virtual mapping. */
1322
1323 return 0;
1324}
1325
1326
1327/**
1328 * Asserts that the handlers+guest-page-tables == ramrange-flags and
1329 * that the physical addresses associated with virtual handlers are correct.
1330 *
1331 * @returns Number of mismatches.
1332 * @param pVM The VM handle.
1333 */
1334PGMDECL(unsigned) PGMAssertHandlerAndFlagsInSync(PVM pVM)
1335{
1336 PPGM pPGM = &pVM->pgm.s;
1337 PGMAHAFIS State;
1338 State.GCPhys = 0;
1339 State.uVirtState = 0;
1340 State.uVirtStateFound = 0;
1341 State.cErrors = 0;
1342 State.pVM = pVM;
1343
1344 /*
1345 * Check the RAM flags against the handlers.
1346 */
1347 for (PPGMRAMRANGE pRam = CTXALLSUFF(pPGM->pRamRanges); pRam; pRam = CTXALLSUFF(pRam->pNext))
1348 {
1349 const unsigned cPages = pRam->cb >> PAGE_SHIFT;
1350 for (unsigned iPage = 0; iPage < cPages; iPage++)
1351 {
1352 PGMPAGE const *pPage = &pRam->aPages[iPage];
1353 if (PGM_PAGE_HAS_ANY_HANDLERS(pPage))
1354 {
1355 State.GCPhys = pRam->GCPhys + (iPage << PAGE_SHIFT);
1356
1357 /*
1358 * Physical first - calculate the state based on the handlers
1359 * active on the page, then compare.
1360 */
1361 if (PGM_PAGE_HAS_ANY_PHYSICAL_HANDLERS(pPage))
1362 {
1363 /* the first */
1364 PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pPGM->CTXSUFF(pTrees)->PhysHandlers, State.GCPhys);
1365 if (!pPhys)
1366 {
1367 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTXSUFF(pTrees)->PhysHandlers, State.GCPhys, true);
1368 if ( pPhys
1369 && pPhys->Core.Key > (State.GCPhys + PAGE_SIZE - 1))
1370 pPhys = NULL;
1371 Assert(!pPhys || pPhys->Core.Key >= State.GCPhys);
1372 }
1373 if (pPhys)
1374 {
1375 unsigned uState = pgmHandlerPhysicalCalcState(pPhys);
1376
1377 /* more? */
1378 while (pPhys->Core.KeyLast < (State.GCPhys | PAGE_OFFSET_MASK))
1379 {
1380 PPGMPHYSHANDLER pPhys2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTXSUFF(pTrees)->PhysHandlers,
1381 pPhys->Core.KeyLast + 1, true);
1382 if ( !pPhys2
1383 || pPhys2->Core.Key > (State.GCPhys | PAGE_OFFSET_MASK))
1384 break;
1385 unsigned uState2 = pgmHandlerPhysicalCalcState(pPhys2);
1386 uState = RT_MAX(uState, uState2);
1387 pPhys = pPhys2;
1388 }
1389
1390 /* compare.*/
1391 if ( PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != uState
1392 && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
1393 {
1394 AssertMsgFailed(("ram range vs phys handler flags mismatch. GCPhys=%RGp state=%d expected=%d %s\n",
1395 State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), uState, pPhys->pszDesc));
1396 State.cErrors++;
1397 }
1398
1399#ifdef IN_RING3
1400 /* validate that REM is handling it. */
1401 if ( !REMR3IsPageAccessHandled(pVM, State.GCPhys)
1402 /* ignore shadowed ROM for the time being. */ /// @todo PAGE FLAGS
1403 && (pPage->HCPhys & (MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2)) != (MM_RAM_FLAGS_ROM | MM_RAM_FLAGS_MMIO2))
1404 {
1405 AssertMsgFailed(("ram range vs phys handler REM mismatch. GCPhys=%RGp state=%d %s\n",
1406 State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), pPhys->pszDesc));
1407 State.cErrors++;
1408 }
1409#endif
1410 }
1411 else
1412 {
1413 AssertMsgFailed(("ram range vs phys handler mismatch. no handler for GCPhys=%RGp\n", State.GCPhys));
1414 State.cErrors++;
1415 }
1416 }
1417
1418 /*
1419 * Virtual handlers.
1420 */
1421 if (PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage))
1422 {
1423 State.uVirtState = PGM_PAGE_GET_HNDL_VIRT_STATE(pPage);
1424#if 1
1425 /* locate all the matching physical ranges. */
1426 State.uVirtStateFound = PGM_PAGE_HNDL_VIRT_STATE_NONE;
1427 RTGCPHYS GCPhysKey = State.GCPhys;
1428 for (;;)
1429 {
1430 PPGMPHYS2VIRTHANDLER pPhys2Virt = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGetBestFit(&CTXSUFF(pVM->pgm.s.pTrees)->PhysToVirtHandlers,
1431 GCPhysKey, true /* above-or-equal */);
1432 if ( !pPhys2Virt
1433 || (pPhys2Virt->Core.Key & X86_PTE_PAE_PG_MASK) != State.GCPhys)
1434 break;
1435
1436 /* the head */
1437 GCPhysKey = pPhys2Virt->Core.KeyLast;
1438 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
1439 unsigned uState = pgmHandlerVirtualCalcState(pCur);
1440 State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
1441
1442 /* any aliases */
1443 while (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK)
1444 {
1445 pPhys2Virt = (PPGMPHYS2VIRTHANDLER)((uintptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
1446 pCur = (PPGMVIRTHANDLER)((uintptr_t)pPhys2Virt + pPhys2Virt->offVirtHandler);
1447 uState = pgmHandlerVirtualCalcState(pCur);
1448 State.uVirtStateFound = RT_MAX(State.uVirtStateFound, uState);
1449 }
1450
1451 /* done? */
1452 if ((GCPhysKey & X86_PTE_PAE_PG_MASK) != State.GCPhys)
1453 break;
1454 }
1455#else
1456 /* very slow */
1457 RTAvlroGCPtrDoWithAll(CTXSUFF(&pVM->pgm.s.pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOneByPhysAddr, &State);
1458#endif
1459 if (State.uVirtState != State.uVirtStateFound)
1460 {
1461 AssertMsgFailed(("ram range vs virt handler flags mismatch. GCPhys=%RGp uVirtState=%#x uVirtStateFound=%#x\n",
1462 State.GCPhys, State.uVirtState, State.uVirtStateFound));
1463 State.cErrors++;
1464 }
1465 }
1466 }
1467 } /* foreach page in ram range. */
1468 } /* foreach ram range. */
1469
1470 /*
1471 * Check that the physical addresses of the virtual handlers matches up
1472 * and that they are otherwise sane.
1473 */
1474 RTAvlroGCPtrDoWithAll(CTXSUFF(&pVM->pgm.s.pTrees)->VirtHandlers, true, pgmHandlerVirtualVerifyOne, &State);
1475
1476 /*
1477 * Do the reverse check for physical handlers.
1478 */
1479 /** @todo */
1480
1481 return State.cErrors;
1482}
1483
1484#endif /* VBOX_STRICT */
1485
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