VirtualBox

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

Last change on this file since 13384 was 13232, checked in by vboxsync, 16 years ago

#1865: changed some validation into pure assertions or assert-return. added some todos, update a comment or five and adjusted lots of whitespace.

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