VirtualBox

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

Last change on this file since 92155 was 91848, checked in by vboxsync, 3 years ago

VMM/NEM,PGM: First bunch changes for watered down guest memory managment for NEM. (Gets stuck in bios.) bugref:10122

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 16.9 KB
Line 
1/* $Id: PGMHandler.cpp 91848 2021-10-19 23:18:13Z 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#ifdef VBOX_WITH_NATIVE_NEM
255 /* Tell NEM about the protection change. */
256 if (VM_IS_NEM_ENABLED(pVM))
257 {
258 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
259 PGMPAGETYPE enmType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
260 NEMHCNotifyPhysPageProtChanged(pVM, GCPhys, PGM_PAGE_GET_HCPHYS(pPage),
261 PGM_RAMRANGE_CALC_PAGE_R3PTR(pRamHint, GCPhys),
262 pgmPhysPageCalcNemProtection(pPage, enmType), enmType, &u2State);
263 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
264 }
265#endif
266 }
267 else
268 AssertRC(rc);
269
270 if (--cPages == 0)
271 return 0;
272 GCPhys += PAGE_SIZE;
273 }
274}
275
276
277/**
278 * Sets all the page level flags for one physical handler range.
279 *
280 * @returns 0
281 * @param pNode Pointer to a PGMPHYSHANDLER.
282 * @param pvUser Pointer to the VM.
283 */
284static DECLCALLBACK(int) pgmR3HandlerPhysicalOneSet(PAVLROGCPHYSNODECORE pNode, void *pvUser)
285{
286 PVM pVM = (PVM)pvUser;
287 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
288 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
289 unsigned uState = pCurType->uState;
290 PPGMRAMRANGE pRamHint = NULL;
291 RTGCPHYS GCPhys = pCur->Core.Key;
292 RTUINT cPages = pCur->cPages;
293 for (;;)
294 {
295 PPGMPAGE pPage;
296 int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
297 if (RT_SUCCESS(rc))
298 {
299 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
300
301#ifdef VBOX_WITH_NATIVE_NEM
302 /* Tell NEM about the protection change. */
303 if (VM_IS_NEM_ENABLED(pVM))
304 {
305 uint8_t u2State = PGM_PAGE_GET_NEM_STATE(pPage);
306 PGMPAGETYPE enmType = (PGMPAGETYPE)PGM_PAGE_GET_TYPE(pPage);
307 NEMHCNotifyPhysPageProtChanged(pVM, GCPhys, PGM_PAGE_GET_HCPHYS(pPage),
308 PGM_RAMRANGE_CALC_PAGE_R3PTR(pRamHint, GCPhys),
309 pgmPhysPageCalcNemProtection(pPage, enmType), enmType, &u2State);
310 PGM_PAGE_SET_NEM_STATE(pPage, u2State);
311 }
312#endif
313 }
314 else
315 AssertRC(rc);
316
317 if (--cPages == 0)
318 return 0;
319 GCPhys += PAGE_SIZE;
320 }
321}
322
323
324/**
325 * Arguments for pgmR3InfoHandlersPhysicalOne and pgmR3InfoHandlersVirtualOne.
326 */
327typedef struct PGMHANDLERINFOARG
328{
329 /** The output helpers.*/
330 PCDBGFINFOHLP pHlp;
331 /** Pointer to the cross context VM handle. */
332 PVM pVM;
333 /** Set if statistics should be dumped. */
334 bool fStats;
335} PGMHANDLERINFOARG, *PPGMHANDLERINFOARG;
336
337
338/**
339 * Info callback for 'pgmhandlers'.
340 *
341 * @param pVM The cross context VM structure.
342 * @param pHlp The output helpers.
343 * @param pszArgs The arguments. phys or virt.
344 */
345DECLCALLBACK(void) pgmR3InfoHandlers(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
346{
347 /*
348 * Parse options.
349 */
350 PGMHANDLERINFOARG Args = { pHlp, pVM, /* .fStats = */ true };
351 if (pszArgs)
352 Args.fStats = strstr(pszArgs, "nost") == NULL;
353
354 /*
355 * Dump the handlers.
356 */
357 pHlp->pfnPrintf(pHlp,
358 "Physical handlers: (PhysHandlers=%d (%#x))\n"
359 "%*s %*s %*s %*s HandlerGC UserGC Type Description\n",
360 pVM->pgm.s.pTreesR3->PhysHandlers, pVM->pgm.s.pTreesR3->PhysHandlers,
361 - (int)sizeof(RTGCPHYS) * 2, "From",
362 - (int)sizeof(RTGCPHYS) * 2 - 3, "- To (incl)",
363 - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
364 - (int)sizeof(RTHCPTR) * 2 - 1, "UserHC");
365 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.pTreesR3->PhysHandlers, true, pgmR3InfoHandlersPhysicalOne, &Args);
366}
367
368
369/**
370 * Displays one physical handler range.
371 *
372 * @returns 0
373 * @param pNode Pointer to a PGMPHYSHANDLER.
374 * @param pvUser Pointer to command helper functions.
375 */
376static DECLCALLBACK(int) pgmR3InfoHandlersPhysicalOne(PAVLROGCPHYSNODECORE pNode, void *pvUser)
377{
378 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
379 PPGMHANDLERINFOARG pArgs = (PPGMHANDLERINFOARG)pvUser;
380 PCDBGFINFOHLP pHlp = pArgs->pHlp;
381 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pArgs->pVM, pCur);
382 const char *pszType;
383 switch (pCurType->enmKind)
384 {
385 case PGMPHYSHANDLERKIND_MMIO: pszType = "MMIO "; break;
386 case PGMPHYSHANDLERKIND_WRITE: pszType = "Write "; break;
387 case PGMPHYSHANDLERKIND_ALL: pszType = "All "; break;
388 default: pszType = "????"; break;
389 }
390 pHlp->pfnPrintf(pHlp,
391 "%RGp - %RGp %RHv %RHv %RHv %RHv %s %s\n",
392 pCur->Core.Key, pCur->Core.KeyLast, pCurType->pfnHandlerR3, pCur->pvUserR3,
393 pCurType->pfnPfHandlerR0, pCur->pvUserR0, pszType, pCur->pszDesc);
394#ifdef VBOX_WITH_STATISTICS
395 if (pArgs->fStats)
396 pHlp->pfnPrintf(pHlp, " cPeriods: %9RU64 cTicks: %11RU64 Min: %11RU64 Avg: %11RU64 Max: %11RU64\n",
397 pCur->Stat.cPeriods, pCur->Stat.cTicks, pCur->Stat.cTicksMin,
398 pCur->Stat.cPeriods ? pCur->Stat.cTicks / pCur->Stat.cPeriods : 0, pCur->Stat.cTicksMax);
399#endif
400 return 0;
401}
402
Note: See TracBrowser for help on using the repository browser.

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