VirtualBox

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

Last change on this file since 55896 was 55896, checked in by vboxsync, 10 years ago

PGM: Renamed the ring-0 and raw-mode context physical page access handler callbacks to 'PfHandler' to indicate that these are for page-fault callbacks. Will add non-PF handlers similar to the ring-3 one later.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 31.0 KB
Line 
1/* $Id: PGMHandler.cpp 55896 2015-05-17 20:20:30Z vboxsync $ */
2/** @file
3 * PGM - Page Manager / Monitor, Access Handlers.
4 */
5
6/*
7 * Copyright (C) 2006-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#define LOG_GROUP LOG_GROUP_PGM
23#include <VBox/vmm/dbgf.h>
24#include <VBox/vmm/pgm.h>
25#include <VBox/vmm/cpum.h>
26#include <VBox/vmm/iom.h>
27#include <VBox/sup.h>
28#include <VBox/vmm/mm.h>
29#include <VBox/vmm/em.h>
30#include <VBox/vmm/stam.h>
31#include <VBox/vmm/csam.h>
32#ifdef VBOX_WITH_REM
33# include <VBox/vmm/rem.h>
34#endif
35#include <VBox/vmm/dbgf.h>
36#ifdef VBOX_WITH_REM
37# include <VBox/vmm/rem.h>
38#endif
39#include <VBox/vmm/selm.h>
40#include <VBox/vmm/ssm.h>
41#include "PGMInternal.h"
42#include <VBox/vmm/vm.h>
43#include "PGMInline.h"
44#include <VBox/dbg.h>
45
46#include <VBox/log.h>
47#include <iprt/assert.h>
48#include <iprt/alloc.h>
49#include <iprt/asm.h>
50#include <iprt/thread.h>
51#include <iprt/string.h>
52#include <VBox/param.h>
53#include <VBox/err.h>
54#include <VBox/vmm/hm.h>
55
56
57/*******************************************************************************
58* Internal Functions *
59*******************************************************************************/
60static DECLCALLBACK(int) pgmR3HandlerPhysicalOneClear(PAVLROGCPHYSNODECORE pNode, void *pvUser);
61static DECLCALLBACK(int) pgmR3HandlerPhysicalOneSet(PAVLROGCPHYSNODECORE pNode, void *pvUser);
62static DECLCALLBACK(int) pgmR3InfoHandlersPhysicalOne(PAVLROGCPHYSNODECORE pNode, void *pvUser);
63static DECLCALLBACK(int) pgmR3InfoHandlersVirtualOne(PAVLROGCPTRNODECORE pNode, void *pvUser);
64
65
66
67
68/**
69 * Register a physical page access handler type, extended version.
70 *
71 * @returns VBox status code.
72 * @param pVM Pointer to the cross context VM structure.
73 * @param enmKind The kind of access handler.
74 * @param pfnHandlerR3 Pointer to the ring-3 handler callback.
75 * @param pfnPfHandlerR0 Pointer to the ring-0 \#PF handler callback.
76 * @param pfnPfHandlerRC Pointer to the raw-mode context \#PF handler
77 * callback.
78 * @param pszDesc The type description.
79 * @param phType Where to return the type handle (cross context
80 * safe).
81 */
82VMMR3_INT_DECL(int) PGMR3HandlerPhysicalTypeRegisterEx(PVM pVM, PGMPHYSHANDLERKIND enmKind,
83 PFNPGMR3PHYSHANDLER pfnHandlerR3,
84 R0PTRTYPE(PFNPGMR0PHYSPFHANDLER) pfnPfHandlerR0,
85 RCPTRTYPE(PFNPGMRCPHYSPFHANDLER) pfnPfHandlerRC,
86 const char *pszDesc, PPGMPHYSHANDLERTYPE phType)
87{
88 AssertPtrReturn(pfnHandlerR3, VERR_INVALID_POINTER);
89 AssertReturn(pfnPfHandlerR0 != NIL_RTR0PTR, VERR_INVALID_POINTER);
90 AssertReturn(pfnPfHandlerRC != NIL_RTRCPTR || HMIsEnabled(pVM), VERR_INVALID_POINTER);
91 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
92 AssertReturn( enmKind == PGMPHYSHANDLERKIND_WRITE
93 || enmKind == PGMPHYSHANDLERKIND_ALL
94 || enmKind == PGMPHYSHANDLERKIND_MMIO,
95 VERR_INVALID_PARAMETER);
96
97 PPGMPHYSHANDLERTYPEINT pType;
98 int rc = MMHyperAlloc(pVM, sizeof(*pType), 0, MM_TAG_PGM_HANDLER_TYPES, (void **)&pType);
99 if (RT_SUCCESS(rc))
100 {
101 pType->u32Magic = PGMPHYSHANDLERTYPEINT_MAGIC;
102 pType->cRefs = 1;
103 pType->enmKind = enmKind;
104 pType->uState = enmKind == PGMPHYSHANDLERKIND_WRITE
105 ? PGM_PAGE_HNDL_PHYS_STATE_WRITE : PGM_PAGE_HNDL_PHYS_STATE_ALL;
106 pType->pfnHandlerR3 = pfnHandlerR3;
107 pType->pfnPfHandlerR0 = pfnPfHandlerR0;
108 pType->pfnPfHandlerRC = pfnPfHandlerRC;
109 pType->pszDesc = pszDesc;
110
111 pgmLock(pVM);
112 RTListOff32Append(&pVM->pgm.s.CTX_SUFF(pTrees)->HeadPhysHandlerTypes, &pType->ListNode);
113 pgmUnlock(pVM);
114
115 *phType = MMHyperHeapPtrToOffset(pVM, pType);
116 LogFlow(("PGMR3HandlerPhysicalTypeRegisterEx: %p/%#x: enmKind=%d pfnHandlerR3=%RHv pfnHandlerR0=%RHv pfnHandlerRC=%RRv pszDesc=%s\n",
117 pType, *phType, enmKind, pfnHandlerR3, pfnPfHandlerR0, pfnPfHandlerRC, pszDesc));
118 return VINF_SUCCESS;
119 }
120 *phType = NIL_PGMPHYSHANDLERTYPE;
121 return rc;
122}
123
124
125/**
126 * Register a physical page access handler type.
127 *
128 * @returns VBox status code.
129 * @param pVM Pointer to the cross context VM structure.
130 * @param enmKind The kind of access handler.
131 * @param pfnHandlerR3 Pointer to the ring-3 handler callback.
132 * @param pszModR0 The name of the ring-0 module, NULL is an alias for
133 * the main ring-0 module.
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 pszPfHandlerRC The name of the raw-mode context \#PF handler, NULL
139 * if the ring-3 handler should be called.
140 * @param pszDesc The type description.
141 * @param phType Where to return the type handle (cross context
142 * safe).
143 */
144VMMR3DECL(int) PGMR3HandlerPhysicalTypeRegister(PVM pVM, PGMPHYSHANDLERKIND enmKind,
145 R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3,
146 const char *pszModR0, const char *pszPfHandlerR0,
147 const char *pszModRC, const char *pszPfHandlerRC, const char *pszDesc,
148 PPGMPHYSHANDLERTYPE phType)
149{
150 LogFlow(("PGMR3HandlerPhysicalTypeRegister: enmKind=%d pfnHandlerR3=%RHv pszModR0=%s pszPfHandlerR0=%s pszModRC=%s pszPfHandlerRC=%s pszDesc=%s\n",
151 enmKind, pfnHandlerR3, pszModR0, pszPfHandlerR0, pszModRC, pszPfHandlerRC, pszDesc));
152
153 /*
154 * Validate input.
155 */
156 if (!pszModRC)
157 pszModRC = VMMGC_MAIN_MODULE_NAME;
158 if (!pszModR0)
159 pszModR0 = VMMR0_MAIN_MODULE_NAME;
160 if (!pszPfHandlerR0)
161 pszPfHandlerR0 = "pgmPhysPfHandlerRedirectToHC";
162 if (!pszPfHandlerRC)
163 pszPfHandlerRC = "pgmPhysPfHandlerRedirectToHC";
164 AssertPtrReturn(pfnHandlerR3, VERR_INVALID_POINTER);
165 AssertPtrReturn(pszPfHandlerR0, VERR_INVALID_POINTER);
166 AssertPtrReturn(pszPfHandlerRC, VERR_INVALID_POINTER);
167
168 /*
169 * Resolve the R0 handler.
170 */
171 R0PTRTYPE(PFNPGMR0PHYSPFHANDLER) pfnPfHandlerR0 = NIL_RTR0PTR;
172 int rc = PDMR3LdrGetSymbolR0Lazy(pVM, pszModR0, NULL /*pszSearchPath*/, pszPfHandlerR0, &pfnPfHandlerR0);
173 if (RT_SUCCESS(rc))
174 {
175 /*
176 * Resolve the GC handler.
177 */
178 RTRCPTR pfnPfHandlerRC = NIL_RTRCPTR;
179 if (!HMIsEnabled(pVM))
180 rc = PDMR3LdrGetSymbolRCLazy(pVM, pszModRC, NULL /*pszSearchPath*/, pszPfHandlerRC, &pfnPfHandlerRC);
181 if (RT_SUCCESS(rc))
182 return PGMR3HandlerPhysicalTypeRegisterEx(pVM, enmKind, pfnHandlerR3, pfnPfHandlerR0, pfnPfHandlerRC, pszDesc, phType);
183
184 AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszModRC, pszPfHandlerRC, rc));
185 }
186 else
187 AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszModR0, pszPfHandlerR0, rc));
188
189 return rc;
190}
191
192
193/**
194 * Updates the physical page access handlers.
195 *
196 * @param pVM Pointer to the VM.
197 * @remark Only used when restoring a saved state.
198 */
199void pgmR3HandlerPhysicalUpdateAll(PVM pVM)
200{
201 LogFlow(("pgmHandlerPhysicalUpdateAll:\n"));
202
203 /*
204 * Clear and set.
205 * (the right -> left on the setting pass is just bird speculating on cache hits)
206 */
207 pgmLock(pVM);
208 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, true, pgmR3HandlerPhysicalOneClear, pVM);
209 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, false, pgmR3HandlerPhysicalOneSet, pVM);
210 pgmUnlock(pVM);
211}
212
213
214/**
215 * Clears all the page level flags for one physical handler range.
216 *
217 * @returns 0
218 * @param pNode Pointer to a PGMPHYSHANDLER.
219 * @param pvUser Pointer to the VM.
220 */
221static DECLCALLBACK(int) pgmR3HandlerPhysicalOneClear(PAVLROGCPHYSNODECORE pNode, void *pvUser)
222{
223 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
224 PPGMRAMRANGE pRamHint = NULL;
225 RTGCPHYS GCPhys = pCur->Core.Key;
226 RTUINT cPages = pCur->cPages;
227 PVM pVM = (PVM)pvUser;
228 for (;;)
229 {
230 PPGMPAGE pPage;
231 int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
232 if (RT_SUCCESS(rc))
233 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_NONE);
234 else
235 AssertRC(rc);
236
237 if (--cPages == 0)
238 return 0;
239 GCPhys += PAGE_SIZE;
240 }
241}
242
243
244/**
245 * Sets all the page level flags for one physical handler range.
246 *
247 * @returns 0
248 * @param pNode Pointer to a PGMPHYSHANDLER.
249 * @param pvUser Pointer to the VM.
250 */
251static DECLCALLBACK(int) pgmR3HandlerPhysicalOneSet(PAVLROGCPHYSNODECORE pNode, void *pvUser)
252{
253 PVM pVM = (PVM)pvUser;
254 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
255 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
256 unsigned uState = pCurType->uState;
257 PPGMRAMRANGE pRamHint = NULL;
258 RTGCPHYS GCPhys = pCur->Core.Key;
259 RTUINT cPages = pCur->cPages;
260 for (;;)
261 {
262 PPGMPAGE pPage;
263 int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
264 if (RT_SUCCESS(rc))
265 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
266 else
267 AssertRC(rc);
268
269 if (--cPages == 0)
270 return 0;
271 GCPhys += PAGE_SIZE;
272 }
273}
274
275
276/**
277 * Register a virtual page access handler type, extended version.
278 *
279 * @returns VBox status code.
280 * @param pVM Pointer to the cross context VM structure.
281 * @param enmKind The kind of access handler.
282 * @param fRelocUserRC Whether the pvUserRC argument should be
283 * automatically relocated or not.
284 * @param pfnInvalidateR3 Pointer to the ring-3 invalidation handler callback.
285 * @param pfnHandlerR3 Pointer to the ring-3 handler callback.
286 * @param pfnPfHandlerRC Pointer to the raw-mode context handler callback.
287 * @param pszDesc The type description.
288 * @param phType Where to return the type handle (cross context
289 * safe).
290 * @remarks No virtual handlers when executing using HM (i.e. ring-0).
291 */
292VMMR3_INT_DECL(int) PGMR3HandlerVirtualTypeRegisterEx(PVM pVM, PGMVIRTHANDLERKIND enmKind, bool fRelocUserRC,
293 PFNPGMR3VIRTINVALIDATE pfnInvalidateR3,
294 PFNPGMR3VIRTHANDLER pfnHandlerR3,
295 RCPTRTYPE(FNPGMRCVIRTPFHANDLER) pfnPfHandlerRC,
296 const char *pszDesc, PPGMVIRTHANDLERTYPE phType)
297{
298 AssertReturn(!HMIsEnabled(pVM), VERR_NOT_AVAILABLE); /* Not supported/relevant for VT-x and AMD-V. */
299 AssertReturn(RT_VALID_PTR(pfnHandlerR3) || enmKind == PGMVIRTHANDLERKIND_HYPERVISOR, VERR_INVALID_POINTER);
300 AssertPtrNullReturn(pfnInvalidateR3, VERR_INVALID_POINTER);
301 AssertReturn(pfnPfHandlerRC != NIL_RTRCPTR, VERR_INVALID_POINTER);
302 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
303 AssertReturn( enmKind == PGMVIRTHANDLERKIND_WRITE
304 || enmKind == PGMVIRTHANDLERKIND_ALL
305 || enmKind == PGMVIRTHANDLERKIND_HYPERVISOR,
306 VERR_INVALID_PARAMETER);
307
308 PPGMVIRTHANDLERTYPEINT pType;
309 int rc = MMHyperAlloc(pVM, sizeof(*pType), 0, MM_TAG_PGM_HANDLER_TYPES, (void **)&pType);
310 if (RT_SUCCESS(rc))
311 {
312 pType->u32Magic = PGMVIRTHANDLERTYPEINT_MAGIC;
313 pType->cRefs = 1;
314 pType->enmKind = enmKind;
315 pType->fRelocUserRC = fRelocUserRC;
316 pType->uState = enmKind == PGMVIRTHANDLERKIND_ALL
317 ? PGM_PAGE_HNDL_VIRT_STATE_ALL : PGM_PAGE_HNDL_VIRT_STATE_WRITE;
318 pType->pfnInvalidateR3 = pfnInvalidateR3;
319 pType->pfnHandlerR3 = pfnHandlerR3;
320 pType->pfnPfHandlerRC = pfnPfHandlerRC;
321 pType->pszDesc = pszDesc;
322
323 pgmLock(pVM);
324 RTListOff32Append(&pVM->pgm.s.CTX_SUFF(pTrees)->HeadVirtHandlerTypes, &pType->ListNode);
325 pgmUnlock(pVM);
326
327 *phType = MMHyperHeapPtrToOffset(pVM, pType);
328 LogFlow(("PGMR3HandlerVirtualTypeRegisterEx: %p/%#x: enmKind=%d pfnInvalidateR3=%RHv pfnHandlerR3=%RHv pfnPfHandlerRC=%RRv pszDesc=%s\n",
329 pType, *phType, enmKind, pfnInvalidateR3, pfnHandlerR3, pfnPfHandlerRC, pszDesc));
330 return VINF_SUCCESS;
331 }
332 *phType = NIL_PGMVIRTHANDLERTYPE;
333 return rc;
334}
335
336
337/**
338 * Register a physical page access handler type.
339 *
340 * @returns VBox status code.
341 * @param pVM Pointer to the cross context VM structure.
342 * @param enmKind The kind of access handler.
343 * @param fRelocUserRC Whether the pvUserRC argument should be
344 * automatically relocated or not.
345 * @param pfnInvalidateR3 Pointer to the ring-3 invalidateion callback
346 * (optional, can be NULL).
347 * @param pfnHandlerR3 Pointer to the ring-3 handler callback.
348 * @param pszModRC The name of the raw-mode context module, NULL is an
349 * alias for the main RC module.
350 * @param pszPfHandlerRC The name of the raw-mode context handler, NULL if
351 * the ring-3 handler should be called.
352 * @param pszDesc The type description.
353 * @param phType Where to return the type handle (cross context
354 * safe).
355 * @remarks No virtual handlers when executing using HM (i.e. ring-0).
356 */
357VMMR3_INT_DECL(int) PGMR3HandlerVirtualTypeRegister(PVM pVM, PGMVIRTHANDLERKIND enmKind, bool fRelocUserRC,
358 PFNPGMR3VIRTINVALIDATE pfnInvalidateR3,
359 PFNPGMR3VIRTHANDLER pfnHandlerR3,
360 const char *pszPfHandlerRC, const char *pszModRC, const char *pszDesc,
361 PPGMVIRTHANDLERTYPE phType)
362{
363 LogFlow(("PGMR3HandlerVirtualTypeRegister: enmKind=%d pfnInvalidateR3=%RHv pfnHandlerR3=%RHv pszModRC=%s pszPfHandlerRC=%s pszDesc=%s\n",
364 enmKind, pfnInvalidateR3, pfnHandlerR3, pszPfHandlerRC, pszModRC, pszDesc));
365
366 /*
367 * Validate input.
368 */
369 if (!pszModRC)
370 pszModRC = VMMGC_MAIN_MODULE_NAME;
371 AssertPtrReturn(pszPfHandlerRC, VERR_INVALID_POINTER);
372
373 /*
374 * Resolve the GC handler.
375 */
376 RTRCPTR pfnPfHandlerRC = NIL_RTRCPTR;
377 int rc = PDMR3LdrGetSymbolRCLazy(pVM, pszModRC, NULL /*pszSearchPath*/, pszPfHandlerRC, &pfnPfHandlerRC);
378 if (RT_SUCCESS(rc))
379 return PGMR3HandlerVirtualTypeRegisterEx(pVM, enmKind, fRelocUserRC,
380 pfnInvalidateR3, pfnHandlerR3,
381 pfnPfHandlerRC,
382 pszDesc, phType);
383
384 AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszModRC, pszPfHandlerRC, rc));
385 return rc;
386}
387
388
389/**
390 * Register a access handler for a virtual range.
391 *
392 * @returns VBox status code.
393 * @param pVM Pointer to the VM.
394 * @param hType The handler type.
395 * @param GCPtr Start address.
396 * @param GCPtrLast Last address (inclusive).
397 * @param pvUserR3 The ring-3 context user argument.
398 * @param pvUserRC The raw-mode context user argument. Whether this is
399 * automatically relocated or not depends on the type.
400 * @param pszDesc Pointer to description string. This must not be freed.
401 */
402VMMR3_INT_DECL(int) PGMR3HandlerVirtualRegister(PVM pVM, PVMCPU pVCpu, PGMVIRTHANDLERTYPE hType, RTGCPTR GCPtr, RTGCPTR GCPtrLast,
403 void *pvUserR3, RTRCPTR pvUserRC, const char *pszDesc)
404{
405 AssertReturn(!HMIsEnabled(pVM), VERR_NOT_AVAILABLE); /* Not supported/relevant for VT-x and AMD-V. */
406 PPGMVIRTHANDLERTYPEINT pType = PGMVIRTHANDLERTYPEINT_FROM_HANDLE(pVM, hType);
407 Log(("PGMR3HandlerVirtualRegister: GCPhys=%RGp GCPhysLast=%RGp pvUserR3=%RHv pvUserGC=%RRv hType=%#x (%d, %s) pszDesc=%RHv:%s\n",
408 GCPtr, GCPtrLast, pvUserR3, pvUserRC, hType, pType->enmKind, R3STRING(pType->pszDesc), pszDesc, R3STRING(pszDesc)));
409
410 /*
411 * Validate input.
412 */
413 AssertReturn(pType->u32Magic == PGMVIRTHANDLERTYPEINT_MAGIC, VERR_INVALID_HANDLE);
414 AssertMsgReturn(GCPtr < GCPtrLast, ("GCPtr >= GCPtrLast (%RGp >= %RGp)\n", GCPtr, GCPtrLast), VERR_INVALID_PARAMETER);
415 switch (pType->enmKind)
416 {
417 case PGMVIRTHANDLERKIND_ALL:
418 AssertReleaseMsgReturn( (GCPtr & PAGE_OFFSET_MASK) == 0
419 && (GCPtrLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK,
420 ("PGMVIRTHANDLERKIND_ALL: GCPtr=%RGv GCPtrLast=%RGv\n", GCPtr, GCPtrLast),
421 VERR_NOT_IMPLEMENTED);
422 break;
423 case PGMVIRTHANDLERKIND_WRITE:
424 case PGMVIRTHANDLERKIND_HYPERVISOR:
425 break;
426 default:
427 AssertMsgFailedReturn(("Invalid enmKind=%d!\n", pType->enmKind), VERR_INVALID_PARAMETER);
428 }
429 AssertMsgReturn( (RTRCUINTPTR)pvUserRC < 0x10000
430 || MMHyperR3ToRC(pVM, MMHyperRCToR3(pVM, pvUserRC)) == pvUserRC,
431 ("Not RC pointer! pvUserRC=%RRv\n", pvUserRC),
432 VERR_INVALID_PARAMETER);
433
434 /*
435 * Allocate and initialize a new entry.
436 */
437 unsigned cPages = (RT_ALIGN(GCPtrLast + 1, PAGE_SIZE) - (GCPtr & PAGE_BASE_GC_MASK)) >> PAGE_SHIFT;
438 PPGMVIRTHANDLER pNew;
439 int rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMVIRTHANDLER, aPhysToVirt[cPages]), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew); /** @todo r=bird: incorrect member name PhysToVirt? */
440 if (RT_FAILURE(rc))
441 return rc;
442
443 pNew->Core.Key = GCPtr;
444 pNew->Core.KeyLast = GCPtrLast;
445
446 pNew->hType = hType;
447 pNew->pvUserRC = pvUserRC;
448 pNew->pvUserR3 = pvUserR3;
449 pNew->pszDesc = pszDesc ? pszDesc : pType->pszDesc;
450 pNew->cb = GCPtrLast - GCPtr + 1;
451 pNew->cPages = cPages;
452 /* Will be synced at next guest execution attempt. */
453 while (cPages-- > 0)
454 {
455 pNew->aPhysToVirt[cPages].Core.Key = NIL_RTGCPHYS;
456 pNew->aPhysToVirt[cPages].Core.KeyLast = NIL_RTGCPHYS;
457 pNew->aPhysToVirt[cPages].offVirtHandler = -RT_OFFSETOF(PGMVIRTHANDLER, aPhysToVirt[cPages]);
458 pNew->aPhysToVirt[cPages].offNextAlias = 0;
459 }
460
461 /*
462 * Try to insert it into the tree.
463 *
464 * The current implementation doesn't allow multiple handlers for
465 * the same range this makes everything much simpler and faster.
466 */
467 AVLROGCPTRTREE *pRoot = pType->enmKind != PGMVIRTHANDLERKIND_HYPERVISOR
468 ? &pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers
469 : &pVM->pgm.s.CTX_SUFF(pTrees)->HyperVirtHandlers;
470 pgmLock(pVM);
471 if (*pRoot != 0)
472 {
473 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGetBestFit(pRoot, pNew->Core.Key, true);
474 if ( !pCur
475 || GCPtr > pCur->Core.KeyLast
476 || GCPtrLast < pCur->Core.Key)
477 pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGetBestFit(pRoot, pNew->Core.Key, false);
478 if ( pCur
479 && GCPtr <= pCur->Core.KeyLast
480 && GCPtrLast >= pCur->Core.Key)
481 {
482 /*
483 * The LDT sometimes conflicts with the IDT and LDT ranges while being
484 * updated on linux. So, we don't assert simply log it.
485 */
486 Log(("PGMR3HandlerVirtualRegister: Conflict with existing range %RGv-%RGv (%s), req. %RGv-%RGv (%s)\n",
487 pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc, GCPtr, GCPtrLast, pszDesc));
488 MMHyperFree(pVM, pNew);
489 pgmUnlock(pVM);
490 return VERR_PGM_HANDLER_VIRTUAL_CONFLICT;
491 }
492 }
493 if (RTAvlroGCPtrInsert(pRoot, &pNew->Core))
494 {
495 if (pType->enmKind != PGMVIRTHANDLERKIND_HYPERVISOR)
496 {
497 pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL | PGM_SYNC_CLEAR_PGM_POOL;
498 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
499 }
500 PGMHandlerVirtualTypeRetain(pVM, hType);
501 pgmUnlock(pVM);
502
503#ifdef VBOX_WITH_STATISTICS
504 rc = STAMR3RegisterF(pVM, &pNew->Stat, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, pNew->pszDesc,
505 "/PGM/VirtHandler/Calls/%RGv-%RGv", pNew->Core.Key, pNew->Core.KeyLast);
506 AssertRC(rc);
507#endif
508 return VINF_SUCCESS;
509 }
510
511 pgmUnlock(pVM);
512 AssertFailed();
513 MMHyperFree(pVM, pNew);
514 return VERR_PGM_HANDLER_VIRTUAL_CONFLICT;
515
516}
517
518
519/**
520 * Changes the type of a virtual handler.
521 *
522 * The new and old type must have the same access kind.
523 *
524 * @returns VBox status code.
525 * @param pVM Pointer to the VM.
526 * @param GCPtr Start address of the virtual handler.
527 * @param hNewType The new handler type.
528 */
529VMMR3_INT_DECL(int) PGMHandlerVirtualChangeType(PVM pVM, RTGCPTR GCPtr, PGMVIRTHANDLERTYPE hNewType)
530{
531 PPGMVIRTHANDLERTYPEINT pNewType = PGMVIRTHANDLERTYPEINT_FROM_HANDLE(pVM, hNewType);
532 AssertReturn(pNewType->u32Magic == PGMVIRTHANDLERTYPEINT_MAGIC, VERR_INVALID_HANDLE);
533
534 pgmLock(pVM);
535 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGet(&pVM->pgm.s.pTreesR3->VirtHandlers, GCPtr);
536 if (pCur)
537 {
538 PGMVIRTHANDLERTYPE hOldType = pCur->hType;
539 PPGMVIRTHANDLERTYPEINT pOldType = PGMVIRTHANDLERTYPEINT_FROM_HANDLE(pVM, hOldType);
540 if (pOldType != pNewType)
541 {
542 AssertReturnStmt(pNewType->enmKind == pOldType->enmKind, pgmUnlock(pVM), VERR_ACCESS_DENIED);
543 PGMHandlerVirtualTypeRetain(pVM, hNewType);
544 pCur->hType = hNewType;
545 PGMHandlerVirtualTypeRelease(pVM, hOldType);
546 }
547 pgmUnlock(pVM);
548 return VINF_SUCCESS;
549 }
550 pgmUnlock(pVM);
551 AssertMsgFailed(("Range %#x not found!\n", GCPtr));
552 return VERR_INVALID_PARAMETER;
553}
554
555
556/**
557 * Deregister an access handler for a virtual range.
558 *
559 * @returns VBox status code.
560 * @param pVM Pointer to the VM.
561 * @param pVCpu Pointer to the cross context CPU structure for the
562 * calling EMT.
563 * @param GCPtr Start address.
564 * @param fHypervisor Set if PGMVIRTHANDLERKIND_HYPERVISOR, false if not.
565 * @thread EMT(pVCpu)
566 */
567VMMR3_INT_DECL(int) PGMHandlerVirtualDeregister(PVM pVM, PVMCPU pVCpu, RTGCPTR GCPtr, bool fHypervisor)
568{
569 pgmLock(pVM);
570
571 PPGMVIRTHANDLER pCur;
572 if (!fHypervisor)
573 {
574 /*
575 * Normal guest handler.
576 */
577 pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, GCPtr);
578 AssertMsgReturnStmt(pCur, ("GCPtr=%RGv\n", GCPtr), pgmUnlock(pVM), VERR_INVALID_PARAMETER);
579 Assert(PGMVIRTANDLER_GET_TYPE(pVM, pCur)->enmKind != PGMVIRTHANDLERKIND_HYPERVISOR);
580
581 Log(("PGMHandlerVirtualDeregister: Removing Virtual (%d) Range %RGv-%RGv %s\n",
582 PGMVIRTANDLER_GET_TYPE(pVM, pCur)->enmKind, pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc));
583
584 /* Reset the flags and remove phys2virt nodes. */
585 for (uint32_t iPage = 0; iPage < pCur->cPages; iPage++)
586 if (pCur->aPhysToVirt[iPage].offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE)
587 pgmHandlerVirtualClearPage(pVM, pCur, iPage);
588
589 /* Schedule CR3 sync. */
590 pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL | PGM_SYNC_CLEAR_PGM_POOL;
591 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
592 }
593 else
594 {
595 /*
596 * Hypervisor one (hypervisor relocation or termination only).
597 */
598 pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->HyperVirtHandlers, GCPtr);
599 AssertMsgReturnStmt(pCur, ("GCPtr=%RGv\n", GCPtr), pgmUnlock(pVM), VERR_INVALID_PARAMETER);
600 Assert(PGMVIRTANDLER_GET_TYPE(pVM, pCur)->enmKind == PGMVIRTHANDLERKIND_HYPERVISOR);
601
602 Log(("PGMHandlerVirtualDeregister: Removing Hyper Virtual Range %RGv-%RGv %s\n",
603 pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc));
604 }
605
606 pgmUnlock(pVM);
607
608 /*
609 * Free it.
610 */
611#ifdef VBOX_WITH_STATISTICS
612 STAMR3DeregisterF(pVM->pUVM, "/PGM/VirtHandler/Calls/%RGv-%RGv", pCur->Core.Key, pCur->Core.KeyLast);
613#endif
614 PGMHandlerVirtualTypeRelease(pVM, pCur->hType);
615 MMHyperFree(pVM, pCur);
616
617 return VINF_SUCCESS;
618}
619
620
621/**
622 * Arguments for pgmR3InfoHandlersPhysicalOne and pgmR3InfoHandlersVirtualOne.
623 */
624typedef struct PGMHANDLERINFOARG
625{
626 /** The output helpers.*/
627 PCDBGFINFOHLP pHlp;
628 /** Pointer to the cross context VM handle. */
629 PVM pVM;
630 /** Set if statistics should be dumped. */
631 bool fStats;
632} PGMHANDLERINFOARG, *PPGMHANDLERINFOARG;
633
634
635/**
636 * Info callback for 'pgmhandlers'.
637 *
638 * @param pHlp The output helpers.
639 * @param pszArgs The arguments. phys or virt.
640 */
641DECLCALLBACK(void) pgmR3InfoHandlers(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
642{
643 /*
644 * Test input.
645 */
646 PGMHANDLERINFOARG Args = { pHlp, pVM, /* .fStats = */ true };
647 bool fPhysical = !pszArgs || !*pszArgs;
648 bool fVirtual = fPhysical;
649 bool fHyper = fPhysical;
650 if (!fPhysical)
651 {
652 bool fAll = strstr(pszArgs, "all") != NULL;
653 fPhysical = fAll || strstr(pszArgs, "phys") != NULL;
654 fVirtual = fAll || strstr(pszArgs, "virt") != NULL;
655 fHyper = fAll || strstr(pszArgs, "hyper")!= NULL;
656 Args.fStats = strstr(pszArgs, "nost") == NULL;
657 }
658
659 /*
660 * Dump the handlers.
661 */
662 if (fPhysical)
663 {
664 pHlp->pfnPrintf(pHlp,
665 "Physical handlers: (PhysHandlers=%d (%#x))\n"
666 "%*s %*s %*s %*s HandlerGC UserGC Type Description\n",
667 pVM->pgm.s.pTreesR3->PhysHandlers, pVM->pgm.s.pTreesR3->PhysHandlers,
668 - (int)sizeof(RTGCPHYS) * 2, "From",
669 - (int)sizeof(RTGCPHYS) * 2 - 3, "- To (incl)",
670 - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
671 - (int)sizeof(RTHCPTR) * 2 - 1, "UserHC");
672 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.pTreesR3->PhysHandlers, true, pgmR3InfoHandlersPhysicalOne, &Args);
673 }
674
675 if (fVirtual)
676 {
677 pHlp->pfnPrintf(pHlp,
678 "Virtual handlers:\n"
679 "%*s %*s %*s %*s Type Description\n",
680 - (int)sizeof(RTGCPTR) * 2, "From",
681 - (int)sizeof(RTGCPTR) * 2 - 3, "- To (excl)",
682 - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
683 - (int)sizeof(RTRCPTR) * 2 - 1, "HandlerGC");
684 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.pTreesR3->VirtHandlers, true, pgmR3InfoHandlersVirtualOne, &Args);
685 }
686
687 if (fHyper)
688 {
689 pHlp->pfnPrintf(pHlp,
690 "Hypervisor Virtual handlers:\n"
691 "%*s %*s %*s %*s Type Description\n",
692 - (int)sizeof(RTGCPTR) * 2, "From",
693 - (int)sizeof(RTGCPTR) * 2 - 3, "- To (excl)",
694 - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
695 - (int)sizeof(RTRCPTR) * 2 - 1, "HandlerGC");
696 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.pTreesR3->HyperVirtHandlers, true, pgmR3InfoHandlersVirtualOne, &Args);
697 }
698}
699
700
701/**
702 * Displays one physical handler range.
703 *
704 * @returns 0
705 * @param pNode Pointer to a PGMPHYSHANDLER.
706 * @param pvUser Pointer to command helper functions.
707 */
708static DECLCALLBACK(int) pgmR3InfoHandlersPhysicalOne(PAVLROGCPHYSNODECORE pNode, void *pvUser)
709{
710 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
711 PPGMHANDLERINFOARG pArgs = (PPGMHANDLERINFOARG)pvUser;
712 PCDBGFINFOHLP pHlp = pArgs->pHlp;
713 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pArgs->pVM, pCur);
714 const char *pszType;
715 switch (pCurType->enmKind)
716 {
717 case PGMPHYSHANDLERKIND_MMIO: pszType = "MMIO "; break;
718 case PGMPHYSHANDLERKIND_WRITE: pszType = "Write "; break;
719 case PGMPHYSHANDLERKIND_ALL: pszType = "All "; break;
720 default: pszType = "????"; break;
721 }
722 pHlp->pfnPrintf(pHlp,
723 "%RGp - %RGp %RHv %RHv %RRv %RRv %s %s\n",
724 pCur->Core.Key, pCur->Core.KeyLast, pCurType->pfnHandlerR3, pCur->pvUserR3, pCurType->pfnPfHandlerRC, pCur->pvUserRC,
725 pszType, pCur->pszDesc);
726#ifdef VBOX_WITH_STATISTICS
727 if (pArgs->fStats)
728 pHlp->pfnPrintf(pHlp, " cPeriods: %9RU64 cTicks: %11RU64 Min: %11RU64 Avg: %11RU64 Max: %11RU64\n",
729 pCur->Stat.cPeriods, pCur->Stat.cTicks, pCur->Stat.cTicksMin,
730 pCur->Stat.cPeriods ? pCur->Stat.cTicks / pCur->Stat.cPeriods : 0, pCur->Stat.cTicksMax);
731#endif
732 return 0;
733}
734
735
736/**
737 * Displays one virtual handler range.
738 *
739 * @returns 0
740 * @param pNode Pointer to a PGMVIRTHANDLER.
741 * @param pvUser Pointer to command helper functions.
742 */
743static DECLCALLBACK(int) pgmR3InfoHandlersVirtualOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
744{
745 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
746 PPGMHANDLERINFOARG pArgs = (PPGMHANDLERINFOARG)pvUser;
747 PCDBGFINFOHLP pHlp = pArgs->pHlp;
748 PPGMVIRTHANDLERTYPEINT pCurType = PGMVIRTANDLER_GET_TYPE(pArgs->pVM, pCur);
749 const char *pszType;
750 switch (pCurType->enmKind)
751 {
752 case PGMVIRTHANDLERKIND_WRITE: pszType = "Write "; break;
753 case PGMVIRTHANDLERKIND_ALL: pszType = "All "; break;
754 case PGMVIRTHANDLERKIND_HYPERVISOR: pszType = "WriteHyp "; break;
755 default: pszType = "????"; break;
756 }
757 pHlp->pfnPrintf(pHlp, "%RGv - %RGv %RHv %RRv %s %s\n",
758 pCur->Core.Key, pCur->Core.KeyLast, pCurType->pfnHandlerR3, pCurType->pfnPfHandlerRC, pszType, pCur->pszDesc);
759#ifdef VBOX_WITH_STATISTICS
760 if (pArgs->fStats)
761 pHlp->pfnPrintf(pHlp, " cPeriods: %9RU64 cTicks: %11RU64 Min: %11RU64 Avg: %11RU64 Max: %11RU64\n",
762 pCur->Stat.cPeriods, pCur->Stat.cTicks, pCur->Stat.cTicksMin,
763 pCur->Stat.cPeriods ? pCur->Stat.cTicks / pCur->Stat.cPeriods : 0, pCur->Stat.cTicksMax);
764#endif
765 return 0;
766}
767
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