VirtualBox

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

Last change on this file since 103776 was 103374, checked in by vboxsync, 10 months ago

VMM/PGM,DBGF,GIC: Parfait pointed out some potential NULL pointer use here and there. bugref:3409

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