VirtualBox

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

Last change on this file since 64117 was 64117, checked in by vboxsync, 8 years ago

PGM: build fix

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