VirtualBox

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

Last change on this file since 81150 was 81150, checked in by vboxsync, 5 years ago

VMM,/Makefile.kmk: Kicked out more recompiler related code. bugref:9576

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