VirtualBox

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

Last change on this file since 73299 was 72493, checked in by vboxsync, 7 years ago

IEM,REM,++: Removed code related IEM_VERIFICATION_MODE and friends because it (1) adds aditional complexity and mess, (2) suffers bit rot as it's infrequently used, and (3) prevents using pVCpu->cpum.GstCtx directly.

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