VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMR3/PGMHandler.cpp@ 92363

Last change on this file since 92363 was 92162, checked in by vboxsync, 3 years ago

VMM/PGM,DevVGA: Baked MMIO2 dirty page tracking into PGM, moving it out of DevVGA. Using the handler state to record a page as dirty (PGM_PAGE_HNDL_PHYS_STATE_DISABLED). bugref:10122

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 17.3 KB
Line 
1/* $Id: PGMHandler.cpp 92162 2021-10-31 23:34:31Z vboxsync $ */
2/** @file
3 * PGM - Page Manager / Monitor, Access Handlers.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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#define VBOX_WITHOUT_PAGING_BIT_FIELDS /* 64-bit bitfields are just asking for trouble. See @bugref{9841} and others. */
24#include <VBox/vmm/dbgf.h>
25#include <VBox/vmm/pgm.h>
26#include <VBox/vmm/cpum.h>
27#include <VBox/vmm/iom.h>
28#include <VBox/sup.h>
29#include <VBox/vmm/mm.h>
30#include <VBox/vmm/em.h>
31#include <VBox/vmm/stam.h>
32#include <VBox/vmm/dbgf.h>
33#include <VBox/vmm/selm.h>
34#include <VBox/vmm/ssm.h>
35#include "PGMInternal.h"
36#include <VBox/vmm/vm.h>
37#include "PGMInline.h"
38#include <VBox/dbg.h>
39
40#include <VBox/log.h>
41#include <iprt/assert.h>
42#include <iprt/alloc.h>
43#include <iprt/asm.h>
44#include <iprt/errcore.h>
45#include <iprt/thread.h>
46#include <iprt/string.h>
47#include <VBox/param.h>
48#include <VBox/vmm/hm.h>
49
50
51/*********************************************************************************************************************************
52* Internal Functions *
53*********************************************************************************************************************************/
54static DECLCALLBACK(int) pgmR3HandlerPhysicalOneClear(PAVLROGCPHYSNODECORE pNode, void *pvUser);
55static DECLCALLBACK(int) pgmR3HandlerPhysicalOneSet(PAVLROGCPHYSNODECORE pNode, void *pvUser);
56static DECLCALLBACK(int) pgmR3InfoHandlersPhysicalOne(PAVLROGCPHYSNODECORE pNode, void *pvUser);
57
58
59
60
61/**
62 * Register a physical page access handler type, extended version.
63 *
64 * @returns VBox status code.
65 * @param pVM The cross context VM structure.
66 * @param enmKind The kind of access handler.
67 * @param fKeepPgmLock Whether to hold the PGM lock while calling the
68 * handler or not. Mainly for PGM callers.
69 * @param pfnHandlerR3 Pointer to the ring-3 handler callback.
70 * @param pfnHandlerR0 Pointer to the ring-0 handler callback.
71 * @param pfnPfHandlerR0 Pointer to the ring-0 \#PF handler callback.
72 * callback.
73 * @param pszDesc The type description.
74 * @param phType Where to return the type handle (cross context
75 * safe).
76 */
77VMMR3_INT_DECL(int) PGMR3HandlerPhysicalTypeRegisterEx(PVM pVM, PGMPHYSHANDLERKIND enmKind, bool fKeepPgmLock,
78 PFNPGMPHYSHANDLER pfnHandlerR3,
79 R0PTRTYPE(PFNPGMPHYSHANDLER) pfnHandlerR0,
80 R0PTRTYPE(PFNPGMRZPHYSPFHANDLER) pfnPfHandlerR0,
81 const char *pszDesc, PPGMPHYSHANDLERTYPE phType)
82{
83 AssertPtrReturn(pfnHandlerR3, VERR_INVALID_POINTER);
84 AssertReturn(pfnHandlerR0 != NIL_RTR0PTR, VERR_INVALID_POINTER);
85 AssertReturn(pfnPfHandlerR0 != NIL_RTR0PTR, VERR_INVALID_POINTER);
86 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
87 AssertReturn( enmKind == PGMPHYSHANDLERKIND_WRITE
88 || enmKind == PGMPHYSHANDLERKIND_ALL
89 || enmKind == PGMPHYSHANDLERKIND_MMIO,
90 VERR_INVALID_PARAMETER);
91
92 PPGMPHYSHANDLERTYPEINT pType;
93 int rc = MMHyperAlloc(pVM, sizeof(*pType), 0, MM_TAG_PGM_HANDLER_TYPES, (void **)&pType);
94 if (RT_SUCCESS(rc))
95 {
96 pType->u32Magic = PGMPHYSHANDLERTYPEINT_MAGIC;
97 pType->cRefs = 1;
98 pType->enmKind = enmKind;
99 pType->uState = enmKind == PGMPHYSHANDLERKIND_WRITE
100 ? PGM_PAGE_HNDL_PHYS_STATE_WRITE : PGM_PAGE_HNDL_PHYS_STATE_ALL;
101 pType->fKeepPgmLock = fKeepPgmLock;
102 pType->pfnHandlerR3 = pfnHandlerR3;
103 pType->pfnHandlerR0 = pfnHandlerR0;
104 pType->pfnPfHandlerR0 = pfnPfHandlerR0;
105 pType->pszDesc = pszDesc;
106
107 PGM_LOCK_VOID(pVM);
108 RTListOff32Append(&pVM->pgm.s.CTX_SUFF(pTrees)->HeadPhysHandlerTypes, &pType->ListNode);
109 PGM_UNLOCK(pVM);
110
111 *phType = MMHyperHeapPtrToOffset(pVM, pType);
112 LogFlow(("PGMR3HandlerPhysicalTypeRegisterEx: %p/%#x: enmKind=%d pfnHandlerR3=%RHv pfnHandlerR0=%RHv pszDesc=%s\n",
113 pType, *phType, enmKind, pfnHandlerR3, pfnPfHandlerR0, pszDesc));
114 return VINF_SUCCESS;
115 }
116 *phType = NIL_PGMPHYSHANDLERTYPE;
117 return rc;
118}
119
120
121/**
122 * Register a physical page access handler type.
123 *
124 * @returns VBox status code.
125 * @param pVM The cross context VM structure.
126 * @param enmKind The kind of access handler.
127 * @param fKeepPgmLock Whether to hold the PGM lock while calling the
128 * handler or not. Mainly for PGM callers.
129 * @param pfnHandlerR3 Pointer to the ring-3 handler callback.
130 * @param pszModR0 The name of the ring-0 module, NULL is an alias for
131 * the main ring-0 module.
132 * @param pszHandlerR0 The name of the ring-0 handler, NULL if the ring-3
133 * handler should be called.
134 * @param pszPfHandlerR0 The name of the ring-0 \#PF handler, NULL if the
135 * ring-3 handler should be called.
136 * @param pszModRC The name of the raw-mode context module, NULL is an
137 * alias for the main RC module.
138 * @param pszHandlerRC The name of the raw-mode context handler, NULL if
139 * the ring-3 handler should be called.
140 * @param pszPfHandlerRC The name of the raw-mode context \#PF handler, NULL
141 * if the ring-3 handler should be called.
142 * @param pszDesc The type description.
143 * @param phType Where to return the type handle (cross context
144 * safe).
145 */
146VMMR3DECL(int) PGMR3HandlerPhysicalTypeRegister(PVM pVM, PGMPHYSHANDLERKIND enmKind, bool fKeepPgmLock,
147 R3PTRTYPE(PFNPGMPHYSHANDLER) pfnHandlerR3,
148 const char *pszModR0, const char *pszHandlerR0, const char *pszPfHandlerR0,
149 const char *pszModRC, const char *pszHandlerRC, const char *pszPfHandlerRC,
150 const char *pszDesc, PPGMPHYSHANDLERTYPE phType)
151{
152 LogFlow(("PGMR3HandlerPhysicalTypeRegister: enmKind=%d pfnHandlerR3=%RHv pszModR0=%s pszHandlerR0=%s pszPfHandlerR0=%s pszModRC=%s pszHandlerRC=%s pszPfHandlerRC=%s pszDesc=%s\n",
153 enmKind, pfnHandlerR3, pszModR0, pszHandlerR0, pszPfHandlerR0, pszModRC, pszHandlerRC, pszPfHandlerRC, pszDesc));
154
155 /*
156 * Validate input.
157 */
158 AssertPtrReturn(pfnHandlerR3, VERR_INVALID_POINTER);
159 AssertPtrNullReturn(pszModR0, VERR_INVALID_POINTER);
160 AssertPtrNullReturn(pszHandlerR0, VERR_INVALID_POINTER);
161 AssertPtrNullReturn(pszPfHandlerR0, VERR_INVALID_POINTER);
162 AssertPtrNullReturn(pszModRC, VERR_INVALID_POINTER);
163 AssertPtrNullReturn(pszHandlerRC, VERR_INVALID_POINTER);
164 AssertPtrNullReturn(pszPfHandlerRC, VERR_INVALID_POINTER);
165
166 /*
167 * Resolve the R0 handlers.
168 */
169 R0PTRTYPE(PFNPGMPHYSHANDLER) pfnHandlerR0 = NIL_RTR0PTR;
170 int rc = PDMR3LdrGetSymbolR0Lazy(pVM, pszHandlerR0 ? pszModR0 : NULL, NULL /*pszSearchPath*/,
171 pszHandlerR0 ? pszHandlerR0 : "pgmPhysHandlerRedirectToHC", &pfnHandlerR0);
172 if (RT_SUCCESS(rc))
173 {
174 R0PTRTYPE(PFNPGMR0PHYSPFHANDLER) pfnPfHandlerR0 = NIL_RTR0PTR;
175 rc = PDMR3LdrGetSymbolR0Lazy(pVM, pszPfHandlerR0 ? pszModR0 : NULL, NULL /*pszSearchPath*/,
176 pszPfHandlerR0 ? pszPfHandlerR0 : "pgmPhysPfHandlerRedirectToHC", &pfnPfHandlerR0);
177 if (RT_SUCCESS(rc))
178 {
179 /*
180 * Resolve the GC handler.
181 */
182 RTRCPTR pfnHandlerRC = NIL_RTRCPTR;
183 RTRCPTR pfnPfHandlerRC = NIL_RTRCPTR;
184 if (VM_IS_RAW_MODE_ENABLED(pVM))
185 {
186 rc = PDMR3LdrGetSymbolRCLazy(pVM, pszHandlerRC ? pszModRC : NULL, NULL /*pszSearchPath*/,
187 pszHandlerRC ? pszHandlerRC : "pgmPhysHandlerRedirectToHC", &pfnHandlerRC);
188 if (RT_SUCCESS(rc))
189 {
190 rc = PDMR3LdrGetSymbolRCLazy(pVM, pszPfHandlerRC ? pszModRC : NULL, NULL /*pszSearchPath*/,
191 pszPfHandlerRC ? pszPfHandlerRC : "pgmPhysPfHandlerRedirectToHC", &pfnPfHandlerRC);
192 AssertMsgRC(rc, ("Failed to resolve %s.%s, rc=%Rrc.\n", pszPfHandlerRC ? pszModRC : VMMRC_MAIN_MODULE_NAME,
193 pszPfHandlerRC ? pszPfHandlerRC : "pgmPhysPfHandlerRedirectToHC", rc));
194 }
195 else
196 AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszHandlerRC ? pszModRC : VMMRC_MAIN_MODULE_NAME,
197 pszHandlerRC ? pszHandlerRC : "pgmPhysHandlerRedirectToHC", rc));
198
199 }
200 if (RT_SUCCESS(rc))
201 return PGMR3HandlerPhysicalTypeRegisterEx(pVM, enmKind, fKeepPgmLock, pfnHandlerR3,
202 pfnHandlerR0, pfnPfHandlerR0, pszDesc, phType);
203 }
204 else
205 AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszPfHandlerR0 ? pszModR0 : VMMR0_MAIN_MODULE_NAME,
206 pszPfHandlerR0 ? pszPfHandlerR0 : "pgmPhysHandlerRedirectToHC", rc));
207 }
208 else
209 AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszHandlerR0 ? pszModR0 : VMMR0_MAIN_MODULE_NAME,
210 pszHandlerR0 ? pszHandlerR0 : "pgmPhysHandlerRedirectToHC", rc));
211
212 return rc;
213}
214
215
216/**
217 * Updates the physical page access handlers.
218 *
219 * @param pVM The cross context VM structure.
220 * @remark Only used when restoring a saved state.
221 */
222void pgmR3HandlerPhysicalUpdateAll(PVM pVM)
223{
224 LogFlow(("pgmHandlerPhysicalUpdateAll:\n"));
225
226 /*
227 * Clear and set.
228 * (the right -> left on the setting pass is just bird speculating on cache hits)
229 */
230 PGM_LOCK_VOID(pVM);
231 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, true, pgmR3HandlerPhysicalOneClear, pVM);
232 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, false, pgmR3HandlerPhysicalOneSet, pVM);
233 PGM_UNLOCK(pVM);
234}
235
236
237/**
238 * Clears all the page level flags for one physical handler range.
239 *
240 * @returns 0
241 * @param pNode Pointer to a PGMPHYSHANDLER.
242 * @param pvUser Pointer to the VM.
243 */
244static DECLCALLBACK(int) pgmR3HandlerPhysicalOneClear(PAVLROGCPHYSNODECORE pNode, void *pvUser)
245{
246 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
247 PPGMRAMRANGE pRamHint = NULL;
248 RTGCPHYS GCPhys = pCur->Core.Key;
249 RTUINT cPages = pCur->cPages;
250 PVM pVM = (PVM)pvUser;
251 for (;;)
252 {
253 PPGMPAGE pPage;
254 int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
255 if (RT_SUCCESS(rc))
256 {
257 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_NONE);
258
259#ifdef VBOX_WITH_NATIVE_NEM
260 /* Tell NEM about the protection change. */
261 if (VM_IS_NEM_ENABLED(pVM))
262 {
263 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
264 PGMPAGETYPE enmType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
265 NEMHCNotifyPhysPageProtChanged(pVM, GCPhys, PGM_PAGE_GET_HCPHYS(pPage),
266 PGM_RAMRANGE_CALC_PAGE_R3PTR(pRamHint, GCPhys),
267 pgmPhysPageCalcNemProtection(pPage, enmType), enmType, &u2State);
268 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
269 }
270#endif
271 }
272 else
273 AssertRC(rc);
274
275 if (--cPages == 0)
276 return 0;
277 GCPhys += PAGE_SIZE;
278 }
279}
280
281
282/**
283 * Sets all the page level flags for one physical handler range.
284 *
285 * @returns 0
286 * @param pNode Pointer to a PGMPHYSHANDLER.
287 * @param pvUser Pointer to the VM.
288 */
289static DECLCALLBACK(int) pgmR3HandlerPhysicalOneSet(PAVLROGCPHYSNODECORE pNode, void *pvUser)
290{
291 PVM pVM = (PVM)pvUser;
292 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
293 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
294 unsigned uState = pCurType->uState;
295 PPGMRAMRANGE pRamHint = NULL;
296 RTGCPHYS GCPhys = pCur->Core.Key;
297 RTUINT cPages = pCur->cPages;
298 for (;;)
299 {
300 PPGMPAGE pPage;
301 int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
302 if (RT_SUCCESS(rc))
303 {
304 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
305
306#ifdef VBOX_WITH_NATIVE_NEM
307 /* Tell NEM about the protection change. */
308 if (VM_IS_NEM_ENABLED(pVM))
309 {
310 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
311 PGMPAGETYPE enmType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
312 NEMHCNotifyPhysPageProtChanged(pVM, GCPhys, PGM_PAGE_GET_HCPHYS(pPage),
313 PGM_RAMRANGE_CALC_PAGE_R3PTR(pRamHint, GCPhys),
314 pgmPhysPageCalcNemProtection(pPage, enmType), enmType, &u2State);
315 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
316 }
317#endif
318 }
319 else
320 AssertRC(rc);
321
322 if (--cPages == 0)
323 return 0;
324 GCPhys += PAGE_SIZE;
325 }
326}
327
328
329/**
330 * Arguments for pgmR3InfoHandlersPhysicalOne and pgmR3InfoHandlersVirtualOne.
331 */
332typedef struct PGMHANDLERINFOARG
333{
334 /** The output helpers.*/
335 PCDBGFINFOHLP pHlp;
336 /** Pointer to the cross context VM handle. */
337 PVM pVM;
338 /** Set if statistics should be dumped. */
339 bool fStats;
340} PGMHANDLERINFOARG, *PPGMHANDLERINFOARG;
341
342
343/**
344 * Info callback for 'pgmhandlers'.
345 *
346 * @param pVM The cross context VM structure.
347 * @param pHlp The output helpers.
348 * @param pszArgs The arguments. phys or virt.
349 */
350DECLCALLBACK(void) pgmR3InfoHandlers(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
351{
352 /*
353 * Parse options.
354 */
355 PGMHANDLERINFOARG Args = { pHlp, pVM, /* .fStats = */ true };
356 if (pszArgs)
357 Args.fStats = strstr(pszArgs, "nost") == NULL;
358
359 /*
360 * Dump the handlers.
361 */
362 pHlp->pfnPrintf(pHlp,
363 "Physical handlers: (PhysHandlers=%d (%#x))\n"
364 "%*s %*s %*s %*s HandlerGC UserGC Type Description\n",
365 pVM->pgm.s.pTreesR3->PhysHandlers, pVM->pgm.s.pTreesR3->PhysHandlers,
366 - (int)sizeof(RTGCPHYS) * 2, "From",
367 - (int)sizeof(RTGCPHYS) * 2 - 3, "- To (incl)",
368 - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
369 - (int)sizeof(RTHCPTR) * 2 - 1, "UserHC");
370 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.pTreesR3->PhysHandlers, true, pgmR3InfoHandlersPhysicalOne, &Args);
371}
372
373
374/**
375 * Displays one physical handler range.
376 *
377 * @returns 0
378 * @param pNode Pointer to a PGMPHYSHANDLER.
379 * @param pvUser Pointer to command helper functions.
380 */
381static DECLCALLBACK(int) pgmR3InfoHandlersPhysicalOne(PAVLROGCPHYSNODECORE pNode, void *pvUser)
382{
383 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
384 PPGMHANDLERINFOARG pArgs = (PPGMHANDLERINFOARG)pvUser;
385 PCDBGFINFOHLP pHlp = pArgs->pHlp;
386 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pArgs->pVM, pCur);
387 const char *pszType;
388 switch (pCurType->enmKind)
389 {
390 case PGMPHYSHANDLERKIND_MMIO: pszType = "MMIO "; break;
391 case PGMPHYSHANDLERKIND_WRITE: pszType = "Write "; break;
392 case PGMPHYSHANDLERKIND_ALL: pszType = "All "; break;
393 default: pszType = "????"; break;
394 }
395 pHlp->pfnPrintf(pHlp,
396 "%RGp - %RGp %RHv %RHv %RHv %RHv %s %s\n",
397 pCur->Core.Key, pCur->Core.KeyLast, pCurType->pfnHandlerR3, pCur->pvUserR3,
398 pCurType->pfnPfHandlerR0, pCur->pvUserR0, pszType, pCur->pszDesc);
399#ifdef VBOX_WITH_STATISTICS
400 if (pArgs->fStats)
401 pHlp->pfnPrintf(pHlp, " cPeriods: %9RU64 cTicks: %11RU64 Min: %11RU64 Avg: %11RU64 Max: %11RU64\n",
402 pCur->Stat.cPeriods, pCur->Stat.cTicks, pCur->Stat.cTicksMin,
403 pCur->Stat.cPeriods ? pCur->Stat.cTicks / pCur->Stat.cPeriods : 0, pCur->Stat.cTicksMax);
404#endif
405 return 0;
406}
407
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