VirtualBox

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

Last change on this file since 91688 was 90439, checked in by vboxsync, 3 years ago

VMM/PGM: Check PGMCritSectEnter status code when we don't return it. bugref:6695

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