VirtualBox

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

最後變更 在這個檔案從80268是 80268,由 vboxsync 提交於 6 年 前

VMM: Refactoring VMMAll/* to use VMCC & VMMCPUCC. bugref:9217

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 68.5 KB
 
1/* $Id: PGMAllHandler.cpp 80268 2019-08-14 11:25:13Z vboxsync $ */
2/** @file
3 * PGM - Page Manager / Monitor, Access Handlers.
4 */
5
6/*
7 * Copyright (C) 2006-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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 VBOX_BUGREF_9217_PART_I
23#define LOG_GROUP LOG_GROUP_PGM
24#include <VBox/vmm/dbgf.h>
25#include <VBox/vmm/pgm.h>
26#include <VBox/vmm/iom.h>
27#include <VBox/vmm/mm.h>
28#include <VBox/vmm/em.h>
29#include <VBox/vmm/nem.h>
30#include <VBox/vmm/stam.h>
31#ifdef VBOX_WITH_REM
32# include <VBox/vmm/rem.h>
33#endif
34#include <VBox/vmm/dbgf.h>
35#include "PGMInternal.h"
36#include <VBox/vmm/vmcc.h>
37#include "PGMInline.h"
38
39#include <VBox/log.h>
40#include <iprt/assert.h>
41#include <iprt/asm-amd64-x86.h>
42#include <iprt/string.h>
43#include <VBox/param.h>
44#include <VBox/err.h>
45#include <VBox/vmm/selm.h>
46
47
48/*********************************************************************************************************************************
49* Internal Functions *
50*********************************************************************************************************************************/
51static int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVMCC pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam);
52static void pgmHandlerPhysicalDeregisterNotifyREMAndNEM(PVMCC pVM, PPGMPHYSHANDLER pCur, int fRestoreRAM);
53static void pgmHandlerPhysicalResetRamFlags(PVMCC pVM, PPGMPHYSHANDLER pCur);
54
55
56/**
57 * Internal worker for releasing a physical handler type registration reference.
58 *
59 * @returns New reference count. UINT32_MAX if invalid input (asserted).
60 * @param pVM The cross context VM structure.
61 * @param pType Pointer to the type registration.
62 */
63DECLINLINE(uint32_t) pgmHandlerPhysicalTypeRelease(PVM pVM, PPGMPHYSHANDLERTYPEINT pType)
64{
65 AssertMsgReturn(pType->u32Magic == PGMPHYSHANDLERTYPEINT_MAGIC, ("%#x\n", pType->u32Magic), UINT32_MAX);
66 uint32_t cRefs = ASMAtomicDecU32(&pType->cRefs);
67 if (cRefs == 0)
68 {
69 pgmLock(pVM);
70 pType->u32Magic = PGMPHYSHANDLERTYPEINT_MAGIC_DEAD;
71 RTListOff32NodeRemove(&pType->ListNode);
72 pgmUnlock(pVM);
73 MMHyperFree(pVM, pType);
74 }
75 return cRefs;
76}
77
78
79/**
80 * Internal worker for retaining a physical handler type registration reference.
81 *
82 * @returns New reference count. UINT32_MAX if invalid input (asserted).
83 * @param pVM The cross context VM structure.
84 * @param pType Pointer to the type registration.
85 */
86DECLINLINE(uint32_t) pgmHandlerPhysicalTypeRetain(PVM pVM, PPGMPHYSHANDLERTYPEINT pType)
87{
88 NOREF(pVM);
89 AssertMsgReturn(pType->u32Magic == PGMPHYSHANDLERTYPEINT_MAGIC, ("%#x\n", pType->u32Magic), UINT32_MAX);
90 uint32_t cRefs = ASMAtomicIncU32(&pType->cRefs);
91 Assert(cRefs < _1M && cRefs > 0);
92 return cRefs;
93}
94
95
96/**
97 * Releases a reference to a physical handler type registration.
98 *
99 * @returns New reference count. UINT32_MAX if invalid input (asserted).
100 * @param pVM The cross context VM structure.
101 * @param hType The type regiration handle.
102 */
103VMMDECL(uint32_t) PGMHandlerPhysicalTypeRelease(PVM pVM, PGMPHYSHANDLERTYPE hType)
104{
105 if (hType != NIL_PGMPHYSHANDLERTYPE)
106 return pgmHandlerPhysicalTypeRelease(pVM, PGMPHYSHANDLERTYPEINT_FROM_HANDLE(pVM, hType));
107 return 0;
108}
109
110
111/**
112 * Retains a reference to a physical handler type registration.
113 *
114 * @returns New reference count. UINT32_MAX if invalid input (asserted).
115 * @param pVM The cross context VM structure.
116 * @param hType The type regiration handle.
117 */
118VMMDECL(uint32_t) PGMHandlerPhysicalTypeRetain(PVM pVM, PGMPHYSHANDLERTYPE hType)
119{
120 return pgmHandlerPhysicalTypeRetain(pVM, PGMPHYSHANDLERTYPEINT_FROM_HANDLE(pVM, hType));
121}
122
123
124/**
125 * Creates a physical access handler.
126 *
127 * @returns VBox status code.
128 * @retval VINF_SUCCESS when successfully installed.
129 * @retval VINF_PGM_GCPHYS_ALIASED when the shadow PTs could be updated because
130 * the guest page aliased or/and mapped by multiple PTs. A CR3 sync has been
131 * flagged together with a pool clearing.
132 * @retval VERR_PGM_HANDLER_PHYSICAL_CONFLICT if the range conflicts with an existing
133 * one. A debug assertion is raised.
134 *
135 * @param pVM The cross context VM structure.
136 * @param hType The handler type registration handle.
137 * @param pvUserR3 User argument to the R3 handler.
138 * @param pvUserR0 User argument to the R0 handler.
139 * @param pvUserRC User argument to the RC handler. This can be a value
140 * less that 0x10000 or a (non-null) pointer that is
141 * automatically relocated.
142 * @param pszDesc Description of this handler. If NULL, the type
143 * description will be used instead.
144 * @param ppPhysHandler Where to return the access handler structure on
145 * success.
146 */
147int pgmHandlerPhysicalExCreate(PVM pVM, PGMPHYSHANDLERTYPE hType, RTR3PTR pvUserR3, RTR0PTR pvUserR0, RTRCPTR pvUserRC,
148 R3PTRTYPE(const char *) pszDesc, PPGMPHYSHANDLER *ppPhysHandler)
149{
150 PPGMPHYSHANDLERTYPEINT pType = PGMPHYSHANDLERTYPEINT_FROM_HANDLE(pVM, hType);
151 Log(("pgmHandlerPhysicalExCreate: pvUserR3=%RHv pvUserR0=%RHv pvUserGC=%RRv hType=%#x (%d, %s) pszDesc=%RHv:%s\n",
152 pvUserR3, pvUserR0, pvUserRC, hType, pType->enmKind, R3STRING(pType->pszDesc), pszDesc, R3STRING(pszDesc)));
153
154 /*
155 * Validate input.
156 */
157 AssertPtr(ppPhysHandler);
158 AssertReturn(pType->u32Magic == PGMPHYSHANDLERTYPEINT_MAGIC, VERR_INVALID_HANDLE);
159 AssertMsgReturn( (RTRCUINTPTR)pvUserRC < 0x10000
160 || MMHyperR3ToRC(pVM, MMHyperRCToR3(pVM, pvUserRC)) == pvUserRC,
161 ("Not RC pointer! pvUserRC=%RRv\n", pvUserRC),
162 VERR_INVALID_PARAMETER);
163 AssertMsgReturn( (RTR0UINTPTR)pvUserR0 < 0x10000
164 || MMHyperR3ToR0(pVM, MMHyperR0ToR3(pVM, pvUserR0)) == pvUserR0,
165 ("Not R0 pointer! pvUserR0=%RHv\n", pvUserR0),
166 VERR_INVALID_PARAMETER);
167
168 /*
169 * Allocate and initialize the new entry.
170 */
171 PPGMPHYSHANDLER pNew;
172 int rc = MMHyperAlloc(pVM, sizeof(*pNew), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew);
173 if (RT_SUCCESS(rc))
174 {
175 pNew->Core.Key = NIL_RTGCPHYS;
176 pNew->Core.KeyLast = NIL_RTGCPHYS;
177 pNew->cPages = 0;
178 pNew->cAliasedPages = 0;
179 pNew->cTmpOffPages = 0;
180 pNew->pvUserR3 = pvUserR3;
181 pNew->pvUserR0 = pvUserR0;
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 NIL_RTR0PTR,
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(PVMCC 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(PVMCC 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(PVMCC 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 /* Tell NEM about the protection update. */
402 if (VM_IS_NEM_ENABLED(pVM))
403 {
404 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
405 PGMPAGETYPE enmType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
406 NEMHCNotifyPhysPageProtChanged(pVM, GCPhysPage, PGM_PAGE_GET_HCPHYS(pPage),
407 pgmPhysPageCalcNemProtection(pPage, enmType), enmType, &u2State);
408 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
409 }
410 }
411
412 /* next */
413 if (--cPages == 0)
414 break;
415 i++;
416 }
417
418 if (fFlushTLBs)
419 {
420 PGM_INVL_ALL_VCPU_TLBS(pVM);
421 Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: flushing guest TLBs; rc=%d\n", rc));
422 }
423 else
424 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)));
425
426 return rc;
427}
428
429
430/**
431 * Deregister a physical page access handler.
432 *
433 * @returns VBox status code.
434 * @param pVM The cross context VM structure.
435 * @param pPhysHandler The handler to deregister (but not free).
436 * @param fRestoreAsRAM How this will likely be restored, if we know (true,
437 * false, or if we don't know -1).
438 */
439int pgmHandlerPhysicalExDeregister(PVMCC pVM, PPGMPHYSHANDLER pPhysHandler, int fRestoreAsRAM)
440{
441 LogFlow(("pgmHandlerPhysicalExDeregister: Removing Range %RGp-%RGp %s fRestoreAsRAM=%d\n",
442 pPhysHandler->Core.Key, pPhysHandler->Core.KeyLast, R3STRING(pPhysHandler->pszDesc), fRestoreAsRAM));
443 AssertReturn(pPhysHandler->Core.Key != NIL_RTGCPHYS, VERR_PGM_HANDLER_NOT_FOUND);
444
445 /*
446 * Remove the handler from the tree.
447 */
448 pgmLock(pVM);
449 PPGMPHYSHANDLER pRemoved = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers,
450 pPhysHandler->Core.Key);
451 if (pRemoved == pPhysHandler)
452 {
453 /*
454 * Clear the page bits, notify the REM about this change and clear
455 * the cache.
456 */
457 pgmHandlerPhysicalResetRamFlags(pVM, pPhysHandler);
458 pgmHandlerPhysicalDeregisterNotifyREMAndNEM(pVM, pPhysHandler, fRestoreAsRAM);
459 pVM->pgm.s.pLastPhysHandlerR0 = 0;
460 pVM->pgm.s.pLastPhysHandlerR3 = 0;
461
462 pPhysHandler->Core.Key = NIL_RTGCPHYS;
463 pPhysHandler->Core.KeyLast = NIL_RTGCPHYS;
464
465 pgmUnlock(pVM);
466
467 return VINF_SUCCESS;
468 }
469
470 /*
471 * Both of the failure conditions here are considered internal processing
472 * errors because they can only be caused by race conditions or corruption.
473 * If we ever need to handle concurrent deregistration, we have to move
474 * the NIL_RTGCPHYS check inside the PGM lock.
475 */
476 if (pRemoved)
477 RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pRemoved->Core);
478
479 pgmUnlock(pVM);
480
481 if (!pRemoved)
482 AssertMsgFailed(("Didn't find range starting at %RGp in the tree!\n", pPhysHandler->Core.Key));
483 else
484 AssertMsgFailed(("Found different handle at %RGp in the tree: got %p insteaded of %p\n",
485 pPhysHandler->Core.Key, pRemoved, pPhysHandler));
486 return VERR_PGM_HANDLER_IPE_1;
487}
488
489
490/**
491 * Destroys (frees) a physical handler.
492 *
493 * The caller must deregister it before destroying it!
494 *
495 * @returns VBox status code.
496 * @param pVM The cross context VM structure.
497 * @param pHandler The handler to free. NULL if ignored.
498 */
499int pgmHandlerPhysicalExDestroy(PVM pVM, PPGMPHYSHANDLER pHandler)
500{
501 if (pHandler)
502 {
503 AssertPtr(pHandler);
504 AssertReturn(pHandler->Core.Key == NIL_RTGCPHYS, VERR_WRONG_ORDER);
505 PGMHandlerPhysicalTypeRelease(pVM, pHandler->hType);
506 MMHyperFree(pVM, pHandler);
507 }
508 return VINF_SUCCESS;
509}
510
511
512/**
513 * Deregister a physical page access handler.
514 *
515 * @returns VBox status code.
516 * @param pVM The cross context VM structure.
517 * @param GCPhys Start physical address.
518 */
519VMMDECL(int) PGMHandlerPhysicalDeregister(PVMCC pVM, RTGCPHYS GCPhys)
520{
521 /*
522 * Find the handler.
523 */
524 pgmLock(pVM);
525 PPGMPHYSHANDLER pRemoved = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
526 if (pRemoved)
527 {
528 LogFlow(("PGMHandlerPhysicalDeregister: Removing Range %RGp-%RGp %s\n",
529 pRemoved->Core.Key, pRemoved->Core.KeyLast, R3STRING(pRemoved->pszDesc)));
530
531 /*
532 * Clear the page bits, notify the REM about this change and clear
533 * the cache.
534 */
535 pgmHandlerPhysicalResetRamFlags(pVM, pRemoved);
536 pgmHandlerPhysicalDeregisterNotifyREMAndNEM(pVM, pRemoved, -1);
537 pVM->pgm.s.pLastPhysHandlerR0 = 0;
538 pVM->pgm.s.pLastPhysHandlerR3 = 0;
539
540 pgmUnlock(pVM);
541
542 pRemoved->Core.Key = NIL_RTGCPHYS;
543 pgmHandlerPhysicalExDestroy(pVM, pRemoved);
544 return VINF_SUCCESS;
545 }
546
547 pgmUnlock(pVM);
548
549 AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys));
550 return VERR_PGM_HANDLER_NOT_FOUND;
551}
552
553
554/**
555 * Shared code with modify.
556 */
557static void pgmHandlerPhysicalDeregisterNotifyREMAndNEM(PVMCC pVM, PPGMPHYSHANDLER pCur, int fRestoreAsRAM)
558{
559 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
560 RTGCPHYS GCPhysStart = pCur->Core.Key;
561 RTGCPHYS GCPhysLast = pCur->Core.KeyLast;
562
563 /*
564 * Page align the range.
565 *
566 * Since we've reset (recalculated) the physical handler state of all pages
567 * we can make use of the page states to figure out whether a page should be
568 * included in the REM notification or not.
569 */
570 if ( (pCur->Core.Key & PAGE_OFFSET_MASK)
571 || ((pCur->Core.KeyLast + 1) & PAGE_OFFSET_MASK))
572 {
573 Assert(pCurType->enmKind != PGMPHYSHANDLERKIND_MMIO);
574
575 if (GCPhysStart & PAGE_OFFSET_MASK)
576 {
577 PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhysStart);
578 if ( pPage
579 && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_NONE)
580 {
581 RTGCPHYS GCPhys = (GCPhysStart + (PAGE_SIZE - 1)) & X86_PTE_PAE_PG_MASK;
582 if ( GCPhys > GCPhysLast
583 || GCPhys < GCPhysStart)
584 return;
585 GCPhysStart = GCPhys;
586 }
587 else
588 GCPhysStart &= X86_PTE_PAE_PG_MASK;
589 Assert(!pPage || PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO); /* these are page aligned atm! */
590 }
591
592 if (GCPhysLast & PAGE_OFFSET_MASK)
593 {
594 PPGMPAGE pPage = pgmPhysGetPage(pVM, GCPhysLast);
595 if ( pPage
596 && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_NONE)
597 {
598 RTGCPHYS GCPhys = (GCPhysLast & X86_PTE_PAE_PG_MASK) - 1;
599 if ( GCPhys < GCPhysStart
600 || GCPhys > GCPhysLast)
601 return;
602 GCPhysLast = GCPhys;
603 }
604 else
605 GCPhysLast |= PAGE_OFFSET_MASK;
606 Assert(!pPage || PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO); /* these are page aligned atm! */
607 }
608 }
609
610 /*
611 * Tell REM and NEM.
612 */
613 const bool fRestoreAsRAM2 = pCurType->pfnHandlerR3
614 && pCurType->enmKind != PGMPHYSHANDLERKIND_MMIO; /** @todo this isn't entirely correct. */
615#ifdef VBOX_WITH_REM
616# ifndef IN_RING3
617 REMNotifyHandlerPhysicalDeregister(pVM, pCurType->enmKind, GCPhysStart, GCPhysLast - GCPhysStart + 1,
618 !!pCurType->pfnHandlerR3, fRestoreAsRAM2);
619# else
620 REMR3NotifyHandlerPhysicalDeregister(pVM, pCurType->enmKind, GCPhysStart, GCPhysLast - GCPhysStart + 1,
621 !!pCurType->pfnHandlerR3, fRestoreAsRAM2);
622# endif
623#endif
624 /** @todo do we need this notification? */
625 NEMHCNotifyHandlerPhysicalDeregister(pVM, pCurType->enmKind, GCPhysStart, GCPhysLast - GCPhysStart + 1,
626 fRestoreAsRAM, fRestoreAsRAM2);
627}
628
629
630/**
631 * pgmHandlerPhysicalResetRamFlags helper that checks for other handlers on
632 * edge pages.
633 */
634DECLINLINE(void) pgmHandlerPhysicalRecalcPageState(PVMCC pVM, RTGCPHYS GCPhys, bool fAbove, PPGMRAMRANGE *ppRamHint)
635{
636 /*
637 * Look for other handlers.
638 */
639 unsigned uState = PGM_PAGE_HNDL_PHYS_STATE_NONE;
640 for (;;)
641 {
642 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys, fAbove);
643 if ( !pCur
644 || ((fAbove ? pCur->Core.Key : pCur->Core.KeyLast) >> PAGE_SHIFT) != (GCPhys >> PAGE_SHIFT))
645 break;
646 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
647 uState = RT_MAX(uState, pCurType->uState);
648
649 /* next? */
650 RTGCPHYS GCPhysNext = fAbove
651 ? pCur->Core.KeyLast + 1
652 : pCur->Core.Key - 1;
653 if ((GCPhysNext >> PAGE_SHIFT) != (GCPhys >> PAGE_SHIFT))
654 break;
655 GCPhys = GCPhysNext;
656 }
657
658 /*
659 * Update if we found something that is a higher priority
660 * state than the current.
661 */
662 if (uState != PGM_PAGE_HNDL_PHYS_STATE_NONE)
663 {
664 PPGMPAGE pPage;
665 int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, ppRamHint);
666 if ( RT_SUCCESS(rc)
667 && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) < uState)
668 {
669 /* This should normally not be necessary. */
670 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
671 bool fFlushTLBs ;
672 rc = pgmPoolTrackUpdateGCPhys(pVM, GCPhys, pPage, false /*fFlushPTEs*/, &fFlushTLBs);
673 if (RT_SUCCESS(rc) && fFlushTLBs)
674 PGM_INVL_ALL_VCPU_TLBS(pVM);
675 else
676 AssertRC(rc);
677
678 /* Tell NEM about the protection update. */
679 if (VM_IS_NEM_ENABLED(pVM))
680 {
681 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
682 PGMPAGETYPE enmType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
683 NEMHCNotifyPhysPageProtChanged(pVM, GCPhys, PGM_PAGE_GET_HCPHYS(pPage),
684 pgmPhysPageCalcNemProtection(pPage, enmType), enmType, &u2State);
685 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
686 }
687 }
688 else
689 AssertRC(rc);
690 }
691}
692
693
694/**
695 * Resets an aliased page.
696 *
697 * @param pVM The cross context VM structure.
698 * @param pPage The page.
699 * @param GCPhysPage The page address in case it comes in handy.
700 * @param fDoAccounting Whether to perform accounting. (Only set during
701 * reset where pgmR3PhysRamReset doesn't have the
702 * handler structure handy.)
703 */
704void pgmHandlerPhysicalResetAliasedPage(PVMCC pVM, PPGMPAGE pPage, RTGCPHYS GCPhysPage, bool fDoAccounting)
705{
706 Assert( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO
707 || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_SPECIAL_ALIAS_MMIO);
708 Assert(PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) == PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
709 RTHCPHYS const HCPhysPrev = PGM_PAGE_GET_HCPHYS(pPage);
710
711 /*
712 * Flush any shadow page table references *first*.
713 */
714 bool fFlushTLBs = false;
715 int rc = pgmPoolTrackUpdateGCPhys(pVM, GCPhysPage, pPage, true /*fFlushPTEs*/, &fFlushTLBs);
716 AssertLogRelRCReturnVoid(rc);
717 HMFlushTlbOnAllVCpus(pVM);
718
719 /*
720 * Make it an MMIO/Zero page.
721 */
722 PGM_PAGE_SET_HCPHYS(pVM, pPage, pVM->pgm.s.HCPhysZeroPg);
723 PGM_PAGE_SET_TYPE(pVM, pPage, PGMPAGETYPE_MMIO);
724 PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_ZERO);
725 PGM_PAGE_SET_PAGEID(pVM, pPage, NIL_GMM_PAGEID);
726 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_ALL);
727
728 /* Flush its TLB entry. */
729 pgmPhysInvalidatePageMapTLBEntry(pVM, GCPhysPage);
730
731 /*
732 * Do accounting for pgmR3PhysRamReset.
733 */
734 if (fDoAccounting)
735 {
736 PPGMPHYSHANDLER pHandler = pgmHandlerPhysicalLookup(pVM, GCPhysPage);
737 if (RT_LIKELY(pHandler))
738 {
739 Assert(pHandler->cAliasedPages > 0);
740 pHandler->cAliasedPages--;
741 }
742 else
743 AssertFailed();
744 }
745
746 /*
747 * Tell NEM about the protection change.
748 */
749 if (VM_IS_NEM_ENABLED(pVM))
750 {
751 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
752 NEMHCNotifyPhysPageChanged(pVM, GCPhysPage, HCPhysPrev, pVM->pgm.s.HCPhysZeroPg,
753 NEM_PAGE_PROT_NONE, PGMPAGETYPE_MMIO, &u2State);
754 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
755 }
756}
757
758
759/**
760 * Resets ram range flags.
761 *
762 * @returns VBox status code.
763 * @retval VINF_SUCCESS when shadow PTs was successfully updated.
764 * @param pVM The cross context VM structure.
765 * @param pCur The physical handler.
766 *
767 * @remark We don't start messing with the shadow page tables, as we've
768 * already got code in Trap0e which deals with out of sync handler
769 * flags (originally conceived for global pages).
770 */
771static void pgmHandlerPhysicalResetRamFlags(PVMCC pVM, PPGMPHYSHANDLER pCur)
772{
773 /*
774 * Iterate the guest ram pages updating the state.
775 */
776 RTUINT cPages = pCur->cPages;
777 RTGCPHYS GCPhys = pCur->Core.Key;
778 PPGMRAMRANGE pRamHint = NULL;
779 for (;;)
780 {
781 PPGMPAGE pPage;
782 int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
783 if (RT_SUCCESS(rc))
784 {
785 /* Reset aliased MMIO pages to MMIO, since this aliasing is our business.
786 (We don't flip MMIO to RAM though, that's PGMPhys.cpp's job.) */
787 bool fNemNotifiedAlready = false;
788 if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO
789 || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_SPECIAL_ALIAS_MMIO)
790 {
791 Assert(pCur->cAliasedPages > 0);
792 pgmHandlerPhysicalResetAliasedPage(pVM, pPage, GCPhys, false /*fDoAccounting*/);
793 pCur->cAliasedPages--;
794 fNemNotifiedAlready = true;
795 }
796#ifdef VBOX_STRICT
797 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
798 AssertMsg(pCurType->enmKind != PGMPHYSHANDLERKIND_MMIO || PGM_PAGE_IS_MMIO(pPage), ("%RGp %R[pgmpage]\n", GCPhys, pPage));
799#endif
800 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_NONE);
801
802 /* Tell NEM about the protection change. */
803 if (VM_IS_NEM_ENABLED(pVM) && !fNemNotifiedAlready)
804 {
805 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
806 PGMPAGETYPE enmType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
807 NEMHCNotifyPhysPageProtChanged(pVM, GCPhys, PGM_PAGE_GET_HCPHYS(pPage),
808 pgmPhysPageCalcNemProtection(pPage, enmType), enmType, &u2State);
809 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
810 }
811 }
812 else
813 AssertRC(rc);
814
815 /* next */
816 if (--cPages == 0)
817 break;
818 GCPhys += PAGE_SIZE;
819 }
820
821 pCur->cAliasedPages = 0;
822 pCur->cTmpOffPages = 0;
823
824 /*
825 * Check for partial start and end pages.
826 */
827 if (pCur->Core.Key & PAGE_OFFSET_MASK)
828 pgmHandlerPhysicalRecalcPageState(pVM, pCur->Core.Key - 1, false /* fAbove */, &pRamHint);
829 if ((pCur->Core.KeyLast & PAGE_OFFSET_MASK) != PAGE_OFFSET_MASK)
830 pgmHandlerPhysicalRecalcPageState(pVM, pCur->Core.KeyLast + 1, true /* fAbove */, &pRamHint);
831}
832
833
834/**
835 * Modify a physical page access handler.
836 *
837 * Modification can only be done to the range it self, not the type or anything else.
838 *
839 * @returns VBox status code.
840 * For all return codes other than VERR_PGM_HANDLER_NOT_FOUND and VINF_SUCCESS the range is deregistered
841 * and a new registration must be performed!
842 * @param pVM The cross context VM structure.
843 * @param GCPhysCurrent Current location.
844 * @param GCPhys New location.
845 * @param GCPhysLast New last location.
846 */
847VMMDECL(int) PGMHandlerPhysicalModify(PVMCC pVM, RTGCPHYS GCPhysCurrent, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast)
848{
849 /*
850 * Remove it.
851 */
852 int rc;
853 pgmLock(pVM);
854 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhysCurrent);
855 if (pCur)
856 {
857 /*
858 * Clear the ram flags. (We're gonna move or free it!)
859 */
860 pgmHandlerPhysicalResetRamFlags(pVM, pCur);
861 PPGMPHYSHANDLERTYPEINT const pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
862 bool const fRestoreAsRAM = pCurType->pfnHandlerR3 /** @todo this isn't entirely correct. */
863 && pCurType->enmKind != PGMPHYSHANDLERKIND_MMIO;
864
865 /*
866 * Validate the new range, modify and reinsert.
867 */
868 if (GCPhysLast >= GCPhys)
869 {
870 /*
871 * We require the range to be within registered ram.
872 * There is no apparent need to support ranges which cover more than one ram range.
873 */
874 PPGMRAMRANGE pRam = pgmPhysGetRange(pVM, GCPhys);
875 if ( pRam
876 && GCPhys <= pRam->GCPhysLast
877 && GCPhysLast >= pRam->GCPhys)
878 {
879 pCur->Core.Key = GCPhys;
880 pCur->Core.KeyLast = GCPhysLast;
881 pCur->cPages = (GCPhysLast - (GCPhys & X86_PTE_PAE_PG_MASK) + 1) >> PAGE_SHIFT;
882
883 if (RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pCur->Core))
884 {
885 RTGCPHYS const cb = GCPhysLast - GCPhys + 1;
886 PGMPHYSHANDLERKIND const enmKind = pCurType->enmKind;
887#ifdef VBOX_WITH_REM
888 bool const fHasHCHandler = !!pCurType->pfnHandlerR3;
889#endif
890
891 /*
892 * Set ram flags, flush shadow PT entries and finally tell REM about this.
893 */
894 rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pCur, pRam);
895
896 /** @todo NEM: not sure we need this notification... */
897 NEMHCNotifyHandlerPhysicalModify(pVM, enmKind, GCPhysCurrent, GCPhys, cb, fRestoreAsRAM);
898
899 pgmUnlock(pVM);
900
901#ifdef VBOX_WITH_REM
902# ifndef IN_RING3
903 REMNotifyHandlerPhysicalModify(pVM, enmKind, GCPhysCurrent, GCPhys, cb,
904 fHasHCHandler, fRestoreAsRAM);
905# else
906 REMR3NotifyHandlerPhysicalModify(pVM, enmKind, GCPhysCurrent, GCPhys, cb,
907 fHasHCHandler, fRestoreAsRAM);
908# endif
909#endif
910 PGM_INVL_ALL_VCPU_TLBS(pVM);
911 Log(("PGMHandlerPhysicalModify: GCPhysCurrent=%RGp -> GCPhys=%RGp GCPhysLast=%RGp\n",
912 GCPhysCurrent, GCPhys, GCPhysLast));
913 return VINF_SUCCESS;
914 }
915
916 AssertMsgFailed(("Conflict! GCPhys=%RGp GCPhysLast=%RGp\n", GCPhys, GCPhysLast));
917 rc = VERR_PGM_HANDLER_PHYSICAL_CONFLICT;
918 }
919 else
920 {
921 AssertMsgFailed(("No RAM range for %RGp-%RGp\n", GCPhys, GCPhysLast));
922 rc = VERR_PGM_HANDLER_PHYSICAL_NO_RAM_RANGE;
923 }
924 }
925 else
926 {
927 AssertMsgFailed(("Invalid range %RGp-%RGp\n", GCPhys, GCPhysLast));
928 rc = VERR_INVALID_PARAMETER;
929 }
930
931 /*
932 * Invalid new location, flush the cache and free it.
933 * We've only gotta notify REM and free the memory.
934 */
935 pgmHandlerPhysicalDeregisterNotifyREMAndNEM(pVM, pCur, -1);
936 pVM->pgm.s.pLastPhysHandlerR0 = 0;
937 pVM->pgm.s.pLastPhysHandlerR3 = 0;
938 PGMHandlerPhysicalTypeRelease(pVM, pCur->hType);
939 MMHyperFree(pVM, pCur);
940 }
941 else
942 {
943 AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhysCurrent));
944 rc = VERR_PGM_HANDLER_NOT_FOUND;
945 }
946
947 pgmUnlock(pVM);
948 return rc;
949}
950
951
952/**
953 * Changes the user callback arguments associated with a physical access handler.
954 *
955 * @returns VBox status code.
956 * @param pVM The cross context VM structure.
957 * @param GCPhys Start physical address of the handler.
958 * @param pvUserR3 User argument to the R3 handler.
959 * @param pvUserR0 User argument to the R0 handler.
960 */
961VMMDECL(int) PGMHandlerPhysicalChangeUserArgs(PVMCC pVM, RTGCPHYS GCPhys, RTR3PTR pvUserR3, RTR0PTR pvUserR0)
962{
963 /*
964 * Find the handler.
965 */
966 int rc = VINF_SUCCESS;
967 pgmLock(pVM);
968 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
969 if (pCur)
970 {
971 /*
972 * Change arguments.
973 */
974 pCur->pvUserR3 = pvUserR3;
975 pCur->pvUserR0 = pvUserR0;
976 }
977 else
978 {
979 AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys));
980 rc = VERR_PGM_HANDLER_NOT_FOUND;
981 }
982
983 pgmUnlock(pVM);
984 return rc;
985}
986
987
988/**
989 * Splits a physical access handler in two.
990 *
991 * @returns VBox status code.
992 * @param pVM The cross context VM structure.
993 * @param GCPhys Start physical address of the handler.
994 * @param GCPhysSplit The split address.
995 */
996VMMDECL(int) PGMHandlerPhysicalSplit(PVMCC pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysSplit)
997{
998 AssertReturn(GCPhys < GCPhysSplit, VERR_INVALID_PARAMETER);
999
1000 /*
1001 * Do the allocation without owning the lock.
1002 */
1003 PPGMPHYSHANDLER pNew;
1004 int rc = MMHyperAlloc(pVM, sizeof(*pNew), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew);
1005 if (RT_FAILURE(rc))
1006 return rc;
1007
1008 /*
1009 * Get the handler.
1010 */
1011 pgmLock(pVM);
1012 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1013 if (RT_LIKELY(pCur))
1014 {
1015 if (RT_LIKELY(GCPhysSplit <= pCur->Core.KeyLast))
1016 {
1017 /*
1018 * Create new handler node for the 2nd half.
1019 */
1020 *pNew = *pCur;
1021 pNew->Core.Key = GCPhysSplit;
1022 pNew->cPages = (pNew->Core.KeyLast - (pNew->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
1023
1024 pCur->Core.KeyLast = GCPhysSplit - 1;
1025 pCur->cPages = (pCur->Core.KeyLast - (pCur->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
1026
1027 if (RT_LIKELY(RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, &pNew->Core)))
1028 {
1029 LogFlow(("PGMHandlerPhysicalSplit: %RGp-%RGp and %RGp-%RGp\n",
1030 pCur->Core.Key, pCur->Core.KeyLast, pNew->Core.Key, pNew->Core.KeyLast));
1031 pgmUnlock(pVM);
1032 return VINF_SUCCESS;
1033 }
1034 AssertMsgFailed(("whu?\n"));
1035 rc = VERR_PGM_PHYS_HANDLER_IPE;
1036 }
1037 else
1038 {
1039 AssertMsgFailed(("outside range: %RGp-%RGp split %RGp\n", pCur->Core.Key, pCur->Core.KeyLast, GCPhysSplit));
1040 rc = VERR_INVALID_PARAMETER;
1041 }
1042 }
1043 else
1044 {
1045 AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys));
1046 rc = VERR_PGM_HANDLER_NOT_FOUND;
1047 }
1048 pgmUnlock(pVM);
1049 MMHyperFree(pVM, pNew);
1050 return rc;
1051}
1052
1053
1054/**
1055 * Joins up two adjacent physical access handlers which has the same callbacks.
1056 *
1057 * @returns VBox status code.
1058 * @param pVM The cross context VM structure.
1059 * @param GCPhys1 Start physical address of the first handler.
1060 * @param GCPhys2 Start physical address of the second handler.
1061 */
1062VMMDECL(int) PGMHandlerPhysicalJoin(PVMCC pVM, RTGCPHYS GCPhys1, RTGCPHYS GCPhys2)
1063{
1064 /*
1065 * Get the handlers.
1066 */
1067 int rc;
1068 pgmLock(pVM);
1069 PPGMPHYSHANDLER pCur1 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys1);
1070 if (RT_LIKELY(pCur1))
1071 {
1072 PPGMPHYSHANDLER pCur2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys2);
1073 if (RT_LIKELY(pCur2))
1074 {
1075 /*
1076 * Make sure that they are adjacent, and that they've got the same callbacks.
1077 */
1078 if (RT_LIKELY(pCur1->Core.KeyLast + 1 == pCur2->Core.Key))
1079 {
1080 if (RT_LIKELY(pCur1->hType == pCur2->hType))
1081 {
1082 PPGMPHYSHANDLER pCur3 = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys2);
1083 if (RT_LIKELY(pCur3 == pCur2))
1084 {
1085 pCur1->Core.KeyLast = pCur2->Core.KeyLast;
1086 pCur1->cPages = (pCur1->Core.KeyLast - (pCur1->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
1087 LogFlow(("PGMHandlerPhysicalJoin: %RGp-%RGp %RGp-%RGp\n",
1088 pCur1->Core.Key, pCur1->Core.KeyLast, pCur2->Core.Key, pCur2->Core.KeyLast));
1089 pVM->pgm.s.pLastPhysHandlerR0 = 0;
1090 pVM->pgm.s.pLastPhysHandlerR3 = 0;
1091 PGMHandlerPhysicalTypeRelease(pVM, pCur2->hType);
1092 MMHyperFree(pVM, pCur2);
1093 pgmUnlock(pVM);
1094 return VINF_SUCCESS;
1095 }
1096
1097 Assert(pCur3 == pCur2);
1098 rc = VERR_PGM_PHYS_HANDLER_IPE;
1099 }
1100 else
1101 {
1102 AssertMsgFailed(("mismatching handlers\n"));
1103 rc = VERR_ACCESS_DENIED;
1104 }
1105 }
1106 else
1107 {
1108 AssertMsgFailed(("not adjacent: %RGp-%RGp %RGp-%RGp\n",
1109 pCur1->Core.Key, pCur1->Core.KeyLast, pCur2->Core.Key, pCur2->Core.KeyLast));
1110 rc = VERR_INVALID_PARAMETER;
1111 }
1112 }
1113 else
1114 {
1115 AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys2));
1116 rc = VERR_PGM_HANDLER_NOT_FOUND;
1117 }
1118 }
1119 else
1120 {
1121 AssertMsgFailed(("Didn't find range starting at %RGp\n", GCPhys1));
1122 rc = VERR_PGM_HANDLER_NOT_FOUND;
1123 }
1124 pgmUnlock(pVM);
1125 return rc;
1126
1127}
1128
1129
1130/**
1131 * Resets any modifications to individual pages in a physical page access
1132 * handler region.
1133 *
1134 * This is used in pair with PGMHandlerPhysicalPageTempOff(),
1135 * PGMHandlerPhysicalPageAlias() or PGMHandlerPhysicalPageAliasHC().
1136 *
1137 * @returns VBox status code.
1138 * @param pVM The cross context VM structure.
1139 * @param GCPhys The start address of the handler regions, i.e. what you
1140 * passed to PGMR3HandlerPhysicalRegister(),
1141 * PGMHandlerPhysicalRegisterEx() or
1142 * PGMHandlerPhysicalModify().
1143 */
1144VMMDECL(int) PGMHandlerPhysicalReset(PVMCC pVM, RTGCPHYS GCPhys)
1145{
1146 LogFlow(("PGMHandlerPhysicalReset GCPhys=%RGp\n", GCPhys));
1147 pgmLock(pVM);
1148
1149 /*
1150 * Find the handler.
1151 */
1152 int rc;
1153 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1154 if (RT_LIKELY(pCur))
1155 {
1156 /*
1157 * Validate kind.
1158 */
1159 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
1160 switch (pCurType->enmKind)
1161 {
1162 case PGMPHYSHANDLERKIND_WRITE:
1163 case PGMPHYSHANDLERKIND_ALL:
1164 case PGMPHYSHANDLERKIND_MMIO: /* NOTE: Only use when clearing MMIO ranges with aliased MMIO2 pages! */
1165 {
1166 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysHandlerReset)); /** @todo move out of switch */
1167 PPGMRAMRANGE pRam = pgmPhysGetRange(pVM, GCPhys);
1168 Assert(pRam);
1169 Assert(pRam->GCPhys <= pCur->Core.Key);
1170 Assert(pRam->GCPhysLast >= pCur->Core.KeyLast);
1171
1172 if (pCurType->enmKind == PGMPHYSHANDLERKIND_MMIO)
1173 {
1174 /*
1175 * Reset all the PGMPAGETYPE_MMIO2_ALIAS_MMIO pages first and that's it.
1176 * This could probably be optimized a bit wrt to flushing, but I'm too lazy
1177 * to do that now...
1178 */
1179 if (pCur->cAliasedPages)
1180 {
1181 PPGMPAGE pPage = &pRam->aPages[(pCur->Core.Key - pRam->GCPhys) >> PAGE_SHIFT];
1182 uint32_t cLeft = pCur->cPages;
1183 while (cLeft-- > 0)
1184 {
1185 if ( PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO
1186 || PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_SPECIAL_ALIAS_MMIO)
1187 {
1188 Assert(pCur->cAliasedPages > 0);
1189 pgmHandlerPhysicalResetAliasedPage(pVM, pPage, pRam->GCPhys + ((RTGCPHYS)cLeft << PAGE_SHIFT),
1190 false /*fDoAccounting*/);
1191 --pCur->cAliasedPages;
1192#ifndef VBOX_STRICT
1193 if (pCur->cAliasedPages == 0)
1194 break;
1195#endif
1196 }
1197 Assert(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO);
1198 pPage++;
1199 }
1200 Assert(pCur->cAliasedPages == 0);
1201 }
1202 }
1203 else if (pCur->cTmpOffPages > 0)
1204 {
1205 /*
1206 * Set the flags and flush shadow PT entries.
1207 */
1208 rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pCur, pRam);
1209 }
1210
1211 pCur->cAliasedPages = 0;
1212 pCur->cTmpOffPages = 0;
1213
1214 rc = VINF_SUCCESS;
1215 break;
1216 }
1217
1218 /*
1219 * Invalid.
1220 */
1221 default:
1222 AssertMsgFailed(("Invalid type %d! Corruption!\n", pCurType->enmKind));
1223 rc = VERR_PGM_PHYS_HANDLER_IPE;
1224 break;
1225 }
1226 }
1227 else
1228 {
1229 AssertMsgFailed(("Didn't find MMIO Range starting at %#x\n", GCPhys));
1230 rc = VERR_PGM_HANDLER_NOT_FOUND;
1231 }
1232
1233 pgmUnlock(pVM);
1234 return rc;
1235}
1236
1237
1238/**
1239 * Temporarily turns off the access monitoring of a page within a monitored
1240 * physical write/all page access handler region.
1241 *
1242 * Use this when no further \#PFs are required for that page. Be aware that
1243 * a page directory sync might reset the flags, and turn on access monitoring
1244 * for the page.
1245 *
1246 * The caller must do required page table modifications.
1247 *
1248 * @returns VBox status code.
1249 * @param pVM The cross context VM structure.
1250 * @param GCPhys The start address of the access handler. This
1251 * must be a fully page aligned range or we risk
1252 * messing up other handlers installed for the
1253 * start and end pages.
1254 * @param GCPhysPage The physical address of the page to turn off
1255 * access monitoring for.
1256 */
1257VMMDECL(int) PGMHandlerPhysicalPageTempOff(PVMCC pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage)
1258{
1259 LogFlow(("PGMHandlerPhysicalPageTempOff GCPhysPage=%RGp\n", GCPhysPage));
1260
1261 pgmLock(pVM);
1262 /*
1263 * Validate the range.
1264 */
1265 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1266 if (RT_LIKELY(pCur))
1267 {
1268 if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
1269 && GCPhysPage <= pCur->Core.KeyLast))
1270 {
1271 Assert(!(pCur->Core.Key & PAGE_OFFSET_MASK));
1272 Assert((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK);
1273
1274 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
1275 AssertReturnStmt( pCurType->enmKind == PGMPHYSHANDLERKIND_WRITE
1276 || pCurType->enmKind == PGMPHYSHANDLERKIND_ALL,
1277 pgmUnlock(pVM), VERR_ACCESS_DENIED);
1278
1279 /*
1280 * Change the page status.
1281 */
1282 PPGMPAGE pPage;
1283 int rc = pgmPhysGetPageEx(pVM, GCPhysPage, &pPage);
1284 AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
1285 if (PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
1286 {
1287 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
1288 pCur->cTmpOffPages++;
1289
1290 /* Tell NEM about the protection change (VGA is using this to track dirty pages). */
1291 if (VM_IS_NEM_ENABLED(pVM))
1292 {
1293 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
1294 PGMPAGETYPE enmType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
1295 NEMHCNotifyPhysPageProtChanged(pVM, GCPhysPage, PGM_PAGE_GET_HCPHYS(pPage),
1296 pgmPhysPageCalcNemProtection(pPage, enmType), enmType, &u2State);
1297 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
1298 }
1299 }
1300 pgmUnlock(pVM);
1301 return VINF_SUCCESS;
1302 }
1303 pgmUnlock(pVM);
1304 AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
1305 GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
1306 return VERR_INVALID_PARAMETER;
1307 }
1308 pgmUnlock(pVM);
1309 AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
1310 return VERR_PGM_HANDLER_NOT_FOUND;
1311}
1312
1313
1314/**
1315 * Replaces an MMIO page with an MMIO2 page.
1316 *
1317 * This is a worker for IOMMMIOMapMMIO2Page that works in a similar way to
1318 * PGMHandlerPhysicalPageTempOff but for an MMIO page. Since an MMIO page has no
1319 * backing, the caller must provide a replacement page. For various reasons the
1320 * replacement page must be an MMIO2 page.
1321 *
1322 * The caller must do required page table modifications. You can get away
1323 * without making any modifications since it's an MMIO page, the cost is an extra
1324 * \#PF which will the resync the page.
1325 *
1326 * Call PGMHandlerPhysicalReset() to restore the MMIO page.
1327 *
1328 * The caller may still get handler callback even after this call and must be
1329 * able to deal correctly with such calls. The reason for these callbacks are
1330 * either that we're executing in the recompiler (which doesn't know about this
1331 * arrangement) or that we've been restored from saved state (where we won't
1332 * save the change).
1333 *
1334 * @returns VBox status code.
1335 * @param pVM The cross context VM structure.
1336 * @param GCPhys The start address of the access handler. This
1337 * must be a fully page aligned range or we risk
1338 * messing up other handlers installed for the
1339 * start and end pages.
1340 * @param GCPhysPage The physical address of the page to turn off
1341 * access monitoring for.
1342 * @param GCPhysPageRemap The physical address of the MMIO2 page that
1343 * serves as backing memory.
1344 *
1345 * @remark May cause a page pool flush if used on a page that is already
1346 * aliased.
1347 *
1348 * @note This trick does only work reliably if the two pages are never ever
1349 * mapped in the same page table. If they are the page pool code will
1350 * be confused should either of them be flushed. See the special case
1351 * of zero page aliasing mentioned in #3170.
1352 *
1353 */
1354VMMDECL(int) PGMHandlerPhysicalPageAlias(PVMCC pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage, RTGCPHYS GCPhysPageRemap)
1355{
1356/// Assert(!IOMIsLockOwner(pVM)); /* We mustn't own any other locks when calling this */
1357 pgmLock(pVM);
1358
1359 /*
1360 * Lookup and validate the range.
1361 */
1362 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1363 if (RT_LIKELY(pCur))
1364 {
1365 if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
1366 && GCPhysPage <= pCur->Core.KeyLast))
1367 {
1368 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
1369 AssertReturnStmt(pCurType->enmKind == PGMPHYSHANDLERKIND_MMIO, pgmUnlock(pVM), VERR_ACCESS_DENIED);
1370 AssertReturnStmt(!(pCur->Core.Key & PAGE_OFFSET_MASK), pgmUnlock(pVM), VERR_INVALID_PARAMETER);
1371 AssertReturnStmt((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK, pgmUnlock(pVM), VERR_INVALID_PARAMETER);
1372
1373 /*
1374 * Get and validate the two pages.
1375 */
1376 PPGMPAGE pPageRemap;
1377 int rc = pgmPhysGetPageEx(pVM, GCPhysPageRemap, &pPageRemap);
1378 AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
1379 AssertMsgReturnStmt(PGM_PAGE_GET_TYPE(pPageRemap) == PGMPAGETYPE_MMIO2,
1380 ("GCPhysPageRemap=%RGp %R[pgmpage]\n", GCPhysPageRemap, pPageRemap),
1381 pgmUnlock(pVM), VERR_PGM_PHYS_NOT_MMIO2);
1382
1383 PPGMPAGE pPage;
1384 rc = pgmPhysGetPageEx(pVM, GCPhysPage, &pPage);
1385 AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
1386 if (PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO)
1387 {
1388 AssertMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_MMIO2_ALIAS_MMIO,
1389 ("GCPhysPage=%RGp %R[pgmpage]\n", GCPhysPage, pPage),
1390 VERR_PGM_PHYS_NOT_MMIO2);
1391 if (PGM_PAGE_GET_HCPHYS(pPage) == PGM_PAGE_GET_HCPHYS(pPageRemap))
1392 {
1393 pgmUnlock(pVM);
1394 return VINF_PGM_HANDLER_ALREADY_ALIASED;
1395 }
1396
1397 /*
1398 * The page is already mapped as some other page, reset it
1399 * to an MMIO/ZERO page before doing the new mapping.
1400 */
1401 Log(("PGMHandlerPhysicalPageAlias: GCPhysPage=%RGp (%R[pgmpage]; %RHp -> %RHp\n",
1402 GCPhysPage, pPage, PGM_PAGE_GET_HCPHYS(pPage), PGM_PAGE_GET_HCPHYS(pPageRemap)));
1403 pgmHandlerPhysicalResetAliasedPage(pVM, pPage, GCPhysPage, false /*fDoAccounting*/);
1404 pCur->cAliasedPages--;
1405 }
1406 Assert(PGM_PAGE_IS_ZERO(pPage));
1407
1408 /*
1409 * Do the actual remapping here.
1410 * This page now serves as an alias for the backing memory specified.
1411 */
1412 LogFlow(("PGMHandlerPhysicalPageAlias: %RGp (%R[pgmpage]) alias for %RGp (%R[pgmpage])\n",
1413 GCPhysPage, pPage, GCPhysPageRemap, pPageRemap ));
1414 PGM_PAGE_SET_HCPHYS(pVM, pPage, PGM_PAGE_GET_HCPHYS(pPageRemap));
1415 PGM_PAGE_SET_TYPE(pVM, pPage, PGMPAGETYPE_MMIO2_ALIAS_MMIO);
1416 PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_ALLOCATED);
1417 PGM_PAGE_SET_PAGEID(pVM, pPage, PGM_PAGE_GET_PAGEID(pPageRemap));
1418 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
1419 pCur->cAliasedPages++;
1420 Assert(pCur->cAliasedPages <= pCur->cPages);
1421
1422 /* Flush its TLB entry. */
1423 pgmPhysInvalidatePageMapTLBEntry(pVM, GCPhysPage);
1424
1425 /* Tell NEM about the backing and protection change. */
1426 if (VM_IS_NEM_ENABLED(pVM))
1427 {
1428 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
1429 NEMHCNotifyPhysPageChanged(pVM, GCPhysPage, pVM->pgm.s.HCPhysZeroPg, PGM_PAGE_GET_HCPHYS(pPage),
1430 pgmPhysPageCalcNemProtection(pPage, PGMPAGETYPE_MMIO2_ALIAS_MMIO),
1431 PGMPAGETYPE_MMIO2_ALIAS_MMIO, &u2State);
1432 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
1433 }
1434 LogFlow(("PGMHandlerPhysicalPageAlias: => %R[pgmpage]\n", pPage));
1435 pgmUnlock(pVM);
1436 return VINF_SUCCESS;
1437 }
1438
1439 pgmUnlock(pVM);
1440 AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
1441 GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
1442 return VERR_INVALID_PARAMETER;
1443 }
1444
1445 pgmUnlock(pVM);
1446 AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
1447 return VERR_PGM_HANDLER_NOT_FOUND;
1448}
1449
1450
1451/**
1452 * Replaces an MMIO page with an arbitrary HC page in the shadow page tables.
1453 *
1454 * This differs from PGMHandlerPhysicalPageAlias in that the page doesn't need
1455 * to be a known MMIO2 page and that only shadow paging may access the page.
1456 * The latter distinction is important because the only use for this feature is
1457 * for mapping the special APIC access page that VT-x uses to detect APIC MMIO
1458 * operations, the page is shared between all guest CPUs and actually not
1459 * written to. At least at the moment.
1460 *
1461 * The caller must do required page table modifications. You can get away
1462 * without making any modifications since it's an MMIO page, the cost is an extra
1463 * \#PF which will the resync the page.
1464 *
1465 * Call PGMHandlerPhysicalReset() to restore the MMIO page.
1466 *
1467 *
1468 * @returns VBox status code.
1469 * @param pVM The cross context VM structure.
1470 * @param GCPhys The start address of the access handler. This
1471 * must be a fully page aligned range or we risk
1472 * messing up other handlers installed for the
1473 * start and end pages.
1474 * @param GCPhysPage The physical address of the page to turn off
1475 * access monitoring for.
1476 * @param HCPhysPageRemap The physical address of the HC page that
1477 * serves as backing memory.
1478 *
1479 * @remark May cause a page pool flush if used on a page that is already
1480 * aliased.
1481 */
1482VMMDECL(int) PGMHandlerPhysicalPageAliasHC(PVMCC pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysPage, RTHCPHYS HCPhysPageRemap)
1483{
1484/// Assert(!IOMIsLockOwner(pVM)); /* We mustn't own any other locks when calling this */
1485 pgmLock(pVM);
1486
1487 /*
1488 * Lookup and validate the range.
1489 */
1490 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1491 if (RT_LIKELY(pCur))
1492 {
1493 if (RT_LIKELY( GCPhysPage >= pCur->Core.Key
1494 && GCPhysPage <= pCur->Core.KeyLast))
1495 {
1496 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
1497 AssertReturnStmt(pCurType->enmKind == PGMPHYSHANDLERKIND_MMIO, pgmUnlock(pVM), VERR_ACCESS_DENIED);
1498 AssertReturnStmt(!(pCur->Core.Key & PAGE_OFFSET_MASK), pgmUnlock(pVM), VERR_INVALID_PARAMETER);
1499 AssertReturnStmt((pCur->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK, pgmUnlock(pVM), VERR_INVALID_PARAMETER);
1500
1501 /*
1502 * Get and validate the pages.
1503 */
1504 PPGMPAGE pPage;
1505 int rc = pgmPhysGetPageEx(pVM, GCPhysPage, &pPage);
1506 AssertReturnStmt(RT_SUCCESS_NP(rc), pgmUnlock(pVM), rc);
1507 if (PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_MMIO)
1508 {
1509 pgmUnlock(pVM);
1510 AssertMsgReturn(PGM_PAGE_GET_TYPE(pPage) == PGMPAGETYPE_SPECIAL_ALIAS_MMIO,
1511 ("GCPhysPage=%RGp %R[pgmpage]\n", GCPhysPage, pPage),
1512 VERR_PGM_PHYS_NOT_MMIO2);
1513 return VINF_PGM_HANDLER_ALREADY_ALIASED;
1514 }
1515 Assert(PGM_PAGE_IS_ZERO(pPage));
1516
1517 /*
1518 * Do the actual remapping here.
1519 * This page now serves as an alias for the backing memory
1520 * specified as far as shadow paging is concerned.
1521 */
1522 LogFlow(("PGMHandlerPhysicalPageAlias: %RGp (%R[pgmpage]) alias for %RHp\n",
1523 GCPhysPage, pPage, HCPhysPageRemap));
1524 PGM_PAGE_SET_HCPHYS(pVM, pPage, HCPhysPageRemap);
1525 PGM_PAGE_SET_TYPE(pVM, pPage, PGMPAGETYPE_SPECIAL_ALIAS_MMIO);
1526 PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_ALLOCATED);
1527 PGM_PAGE_SET_PAGEID(pVM, pPage, NIL_GMM_PAGEID);
1528 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_DISABLED);
1529 pCur->cAliasedPages++;
1530 Assert(pCur->cAliasedPages <= pCur->cPages);
1531
1532 /* Flush its TLB entry. */
1533 pgmPhysInvalidatePageMapTLBEntry(pVM, GCPhysPage);
1534
1535 /* Tell NEM about the backing and protection change. */
1536 if (VM_IS_NEM_ENABLED(pVM))
1537 {
1538 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
1539 NEMHCNotifyPhysPageChanged(pVM, GCPhysPage, pVM->pgm.s.HCPhysZeroPg, PGM_PAGE_GET_HCPHYS(pPage),
1540 pgmPhysPageCalcNemProtection(pPage, PGMPAGETYPE_SPECIAL_ALIAS_MMIO),
1541 PGMPAGETYPE_SPECIAL_ALIAS_MMIO, &u2State);
1542 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
1543 }
1544 LogFlow(("PGMHandlerPhysicalPageAliasHC: => %R[pgmpage]\n", pPage));
1545 pgmUnlock(pVM);
1546 return VINF_SUCCESS;
1547 }
1548 pgmUnlock(pVM);
1549 AssertMsgFailed(("The page %#x is outside the range %#x-%#x\n",
1550 GCPhysPage, pCur->Core.Key, pCur->Core.KeyLast));
1551 return VERR_INVALID_PARAMETER;
1552 }
1553 pgmUnlock(pVM);
1554
1555 AssertMsgFailed(("Specified physical handler start address %#x is invalid.\n", GCPhys));
1556 return VERR_PGM_HANDLER_NOT_FOUND;
1557}
1558
1559
1560/**
1561 * Checks if a physical range is handled
1562 *
1563 * @returns boolean
1564 * @param pVM The cross context VM structure.
1565 * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
1566 * @remarks Caller must take the PGM lock...
1567 * @thread EMT.
1568 */
1569VMMDECL(bool) PGMHandlerPhysicalIsRegistered(PVMCC pVM, RTGCPHYS GCPhys)
1570{
1571 /*
1572 * Find the handler.
1573 */
1574 pgmLock(pVM);
1575 PPGMPHYSHANDLER pCur = pgmHandlerPhysicalLookup(pVM, GCPhys);
1576 if (pCur)
1577 {
1578#ifdef VBOX_STRICT
1579 Assert(GCPhys >= pCur->Core.Key && GCPhys <= pCur->Core.KeyLast);
1580 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
1581 Assert( pCurType->enmKind == PGMPHYSHANDLERKIND_WRITE
1582 || pCurType->enmKind == PGMPHYSHANDLERKIND_ALL
1583 || pCurType->enmKind == PGMPHYSHANDLERKIND_MMIO);
1584#endif
1585 pgmUnlock(pVM);
1586 return true;
1587 }
1588 pgmUnlock(pVM);
1589 return false;
1590}
1591
1592
1593/**
1594 * Checks if it's an disabled all access handler or write access handler at the
1595 * given address.
1596 *
1597 * @returns true if it's an all access handler, false if it's a write access
1598 * handler.
1599 * @param pVM The cross context VM structure.
1600 * @param GCPhys The address of the page with a disabled handler.
1601 *
1602 * @remarks The caller, PGMR3PhysTlbGCPhys2Ptr, must hold the PGM lock.
1603 */
1604bool pgmHandlerPhysicalIsAll(PVMCC pVM, RTGCPHYS GCPhys)
1605{
1606 pgmLock(pVM);
1607 PPGMPHYSHANDLER pCur = pgmHandlerPhysicalLookup(pVM, GCPhys);
1608 if (!pCur)
1609 {
1610 pgmUnlock(pVM);
1611 AssertFailed();
1612 return true;
1613 }
1614 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
1615 Assert( pCurType->enmKind == PGMPHYSHANDLERKIND_WRITE
1616 || pCurType->enmKind == PGMPHYSHANDLERKIND_ALL
1617 || pCurType->enmKind == PGMPHYSHANDLERKIND_MMIO); /* sanity */
1618 /* Only whole pages can be disabled. */
1619 Assert( pCur->Core.Key <= (GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK)
1620 && pCur->Core.KeyLast >= (GCPhys | PAGE_OFFSET_MASK));
1621
1622 bool bRet = pCurType->enmKind != PGMPHYSHANDLERKIND_WRITE;
1623 pgmUnlock(pVM);
1624 return bRet;
1625}
1626
1627#ifdef VBOX_STRICT
1628
1629/**
1630 * State structure used by the PGMAssertHandlerAndFlagsInSync() function
1631 * and its AVL enumerators.
1632 */
1633typedef struct PGMAHAFIS
1634{
1635 /** The current physical address. */
1636 RTGCPHYS GCPhys;
1637 /** Number of errors. */
1638 unsigned cErrors;
1639 /** Pointer to the VM. */
1640 PVM pVM;
1641} PGMAHAFIS, *PPGMAHAFIS;
1642
1643
1644/**
1645 * Asserts that the handlers+guest-page-tables == ramrange-flags and
1646 * that the physical addresses associated with virtual handlers are correct.
1647 *
1648 * @returns Number of mismatches.
1649 * @param pVM The cross context VM structure.
1650 */
1651VMMDECL(unsigned) PGMAssertHandlerAndFlagsInSync(PVM pVM)
1652{
1653 PPGM pPGM = &pVM->pgm.s;
1654 PGMAHAFIS State;
1655 State.GCPhys = 0;
1656 State.cErrors = 0;
1657 State.pVM = pVM;
1658
1659 PGM_LOCK_ASSERT_OWNER(pVM);
1660
1661 /*
1662 * Check the RAM flags against the handlers.
1663 */
1664 for (PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRangesX); pRam; pRam = pRam->CTX_SUFF(pNext))
1665 {
1666 const uint32_t cPages = pRam->cb >> PAGE_SHIFT;
1667 for (uint32_t iPage = 0; iPage < cPages; iPage++)
1668 {
1669 PGMPAGE const *pPage = &pRam->aPages[iPage];
1670 if (PGM_PAGE_HAS_ANY_HANDLERS(pPage))
1671 {
1672 State.GCPhys = pRam->GCPhys + (iPage << PAGE_SHIFT);
1673
1674 /*
1675 * Physical first - calculate the state based on the handlers
1676 * active on the page, then compare.
1677 */
1678 if (PGM_PAGE_HAS_ANY_PHYSICAL_HANDLERS(pPage))
1679 {
1680 /* the first */
1681 PPGMPHYSHANDLER pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys);
1682 if (!pPhys)
1683 {
1684 pPhys = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers, State.GCPhys, true);
1685 if ( pPhys
1686 && pPhys->Core.Key > (State.GCPhys + PAGE_SIZE - 1))
1687 pPhys = NULL;
1688 Assert(!pPhys || pPhys->Core.Key >= State.GCPhys);
1689 }
1690 if (pPhys)
1691 {
1692 PPGMPHYSHANDLERTYPEINT pPhysType = (PPGMPHYSHANDLERTYPEINT)MMHyperHeapOffsetToPtr(pVM, pPhys->hType);
1693 unsigned uState = pPhysType->uState;
1694
1695 /* more? */
1696 while (pPhys->Core.KeyLast < (State.GCPhys | PAGE_OFFSET_MASK))
1697 {
1698 PPGMPHYSHANDLER pPhys2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pPGM->CTX_SUFF(pTrees)->PhysHandlers,
1699 pPhys->Core.KeyLast + 1, true);
1700 if ( !pPhys2
1701 || pPhys2->Core.Key > (State.GCPhys | PAGE_OFFSET_MASK))
1702 break;
1703 PPGMPHYSHANDLERTYPEINT pPhysType2 = (PPGMPHYSHANDLERTYPEINT)MMHyperHeapOffsetToPtr(pVM, pPhys2->hType);
1704 uState = RT_MAX(uState, pPhysType2->uState);
1705 pPhys = pPhys2;
1706 }
1707
1708 /* compare.*/
1709 if ( PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != uState
1710 && PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) != PGM_PAGE_HNDL_PHYS_STATE_DISABLED)
1711 {
1712 AssertMsgFailed(("ram range vs phys handler flags mismatch. GCPhys=%RGp state=%d expected=%d %s\n",
1713 State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), uState, pPhysType->pszDesc));
1714 State.cErrors++;
1715 }
1716
1717# ifdef VBOX_WITH_REM
1718# ifdef IN_RING3
1719 /* validate that REM is handling it. */
1720 if ( !REMR3IsPageAccessHandled(pVM, State.GCPhys)
1721 /* ignore shadowed ROM for the time being. */
1722 && PGM_PAGE_GET_TYPE(pPage) != PGMPAGETYPE_ROM_SHADOW)
1723 {
1724 AssertMsgFailed(("ram range vs phys handler REM mismatch. GCPhys=%RGp state=%d %s\n",
1725 State.GCPhys, PGM_PAGE_GET_HNDL_PHYS_STATE(pPage), pPhysType->pszDesc));
1726 State.cErrors++;
1727 }
1728# endif
1729# endif
1730 }
1731 else
1732 {
1733 AssertMsgFailed(("ram range vs phys handler mismatch. no handler for GCPhys=%RGp\n", State.GCPhys));
1734 State.cErrors++;
1735 }
1736 }
1737 }
1738 } /* foreach page in ram range. */
1739 } /* foreach ram range. */
1740
1741 /*
1742 * Do the reverse check for physical handlers.
1743 */
1744 /** @todo */
1745
1746 return State.cErrors;
1747}
1748
1749#endif /* VBOX_STRICT */
1750
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette