VirtualBox

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

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

build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 30.9 KB
Line 
1/* $Id: PGMHandler.cpp 55890 2015-05-17 18:06:25Z 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 pfnHandlerR0 Pointer to the ring-0 handler callback.
76 * @param pfnHandlerRC Pointer to the raw-mode context handler callback.
77 * @param pszDesc The type description.
78 * @param phType Where to return the type handle (cross context
79 * safe).
80 */
81VMMR3_INT_DECL(int) PGMR3HandlerPhysicalTypeRegisterEx(PVM pVM, PGMPHYSHANDLERKIND enmKind,
82 PFNPGMR3PHYSHANDLER pfnHandlerR3,
83 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0,
84 RCPTRTYPE(PFNPGMRCPHYSHANDLER) pfnHandlerRC,
85 const char *pszDesc, PPGMPHYSHANDLERTYPE phType)
86{
87 AssertPtrReturn(pfnHandlerR3, VERR_INVALID_POINTER);
88 AssertReturn(pfnHandlerR0 != NIL_RTR0PTR, VERR_INVALID_POINTER);
89 AssertReturn(pfnHandlerRC != NIL_RTRCPTR || HMIsEnabled(pVM), VERR_INVALID_POINTER);
90 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
91 AssertReturn( enmKind == PGMPHYSHANDLERKIND_WRITE
92 || enmKind == PGMPHYSHANDLERKIND_ALL
93 || enmKind == PGMPHYSHANDLERKIND_MMIO,
94 VERR_INVALID_PARAMETER);
95
96 PPGMPHYSHANDLERTYPEINT pType;
97 int rc = MMHyperAlloc(pVM, sizeof(*pType), 0, MM_TAG_PGM_HANDLER_TYPES, (void **)&pType);
98 if (RT_SUCCESS(rc))
99 {
100 pType->u32Magic = PGMPHYSHANDLERTYPEINT_MAGIC;
101 pType->cRefs = 1;
102 pType->enmKind = enmKind;
103 pType->uState = enmKind == PGMPHYSHANDLERKIND_WRITE ? PGM_PAGE_HNDL_PHYS_STATE_WRITE : PGM_PAGE_HNDL_PHYS_STATE_ALL;
104 pType->pfnHandlerR3 = pfnHandlerR3;
105 pType->pfnHandlerR0 = pfnHandlerR0;
106 pType->pfnHandlerRC = pfnHandlerRC;
107 pType->pszDesc = pszDesc;
108
109 pgmLock(pVM);
110 RTListOff32Append(&pVM->pgm.s.CTX_SUFF(pTrees)->HeadPhysHandlerTypes, &pType->ListNode);
111 pgmUnlock(pVM);
112
113 *phType = MMHyperHeapPtrToOffset(pVM, pType);
114 LogFlow(("PGMR3HandlerPhysicalTypeRegisterEx: %p/%#x: enmKind=%d pfnHandlerR3=%RHv pfnHandlerR0=%RHv pfnHandlerRC=%RRv pszDesc=%s\n",
115 pType, *phType, enmKind, pfnHandlerR3, pfnHandlerR0, pfnHandlerRC, pszDesc));
116 return VINF_SUCCESS;
117 }
118 *phType = NIL_PGMPHYSHANDLERTYPE;
119 return rc;
120}
121
122
123/**
124 * Register a physical page access handler type.
125 *
126 * @returns VBox status code.
127 * @param pVM Pointer to the cross context VM structure.
128 * @param enmKind The kind of access handler.
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 pszModRC The name of the raw-mode context module, NULL is an
135 * alias for the main RC module.
136 * @param pszHandlerRC The name of the raw-mode context handler, NULL if
137 * the ring-3 handler should be called.
138 * @param pszDesc The type description.
139 * @param phType Where to return the type handle (cross context
140 * safe).
141 */
142VMMR3DECL(int) PGMR3HandlerPhysicalTypeRegister(PVM pVM, PGMPHYSHANDLERKIND enmKind,
143 R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3,
144 const char *pszModR0, const char *pszHandlerR0,
145 const char *pszModRC, const char *pszHandlerRC, const char *pszDesc,
146 PPGMPHYSHANDLERTYPE phType)
147{
148 LogFlow(("PGMR3HandlerPhysicalTypeRegister: enmKind=%d pfnHandlerR3=%RHv pszModR0=%s pszHandlerR0=%s pszModRC=%s pszHandlerRC=%s pszDesc=%s\n",
149 enmKind, pfnHandlerR3, pszModR0, pszHandlerR0, pszHandlerRC, pszModRC, pszDesc));
150
151 /*
152 * Validate input.
153 */
154 if (!pszModRC)
155 pszModRC = VMMGC_MAIN_MODULE_NAME;
156 if (!pszModR0)
157 pszModR0 = VMMR0_MAIN_MODULE_NAME;
158 if (!pszHandlerR0)
159 pszHandlerR0 = "pgmPhysHandlerRedirectToHC";
160 if (!pszHandlerRC)
161 pszHandlerRC = "pgmPhysHandlerRedirectToHC";
162 AssertPtrReturn(pfnHandlerR3, VERR_INVALID_POINTER);
163 AssertPtrReturn(pszHandlerR0, VERR_INVALID_POINTER);
164 AssertPtrReturn(pszHandlerRC, VERR_INVALID_POINTER);
165
166 /*
167 * Resolve the R0 handler.
168 */
169 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0 = NIL_RTR0PTR;
170 int rc = PDMR3LdrGetSymbolR0Lazy(pVM, pszModR0, NULL /*pszSearchPath*/, pszHandlerR0, &pfnHandlerR0);
171 if (RT_SUCCESS(rc))
172 {
173 /*
174 * Resolve the GC handler.
175 */
176 RTRCPTR pfnHandlerRC = NIL_RTRCPTR;
177 if (!HMIsEnabled(pVM))
178 rc = PDMR3LdrGetSymbolRCLazy(pVM, pszModRC, NULL /*pszSearchPath*/, pszHandlerRC, &pfnHandlerRC);
179 if (RT_SUCCESS(rc))
180 return PGMR3HandlerPhysicalTypeRegisterEx(pVM, enmKind, pfnHandlerR3, pfnHandlerR0, pfnHandlerRC, pszDesc, phType);
181
182 AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszModRC, pszHandlerRC, rc));
183 }
184 else
185 AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszModR0, pszHandlerR0, rc));
186
187 return rc;
188}
189
190
191/**
192 * Updates the physical page access handlers.
193 *
194 * @param pVM Pointer to the VM.
195 * @remark Only used when restoring a saved state.
196 */
197void pgmR3HandlerPhysicalUpdateAll(PVM pVM)
198{
199 LogFlow(("pgmHandlerPhysicalUpdateAll:\n"));
200
201 /*
202 * Clear and set.
203 * (the right -> left on the setting pass is just bird speculating on cache hits)
204 */
205 pgmLock(pVM);
206 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, true, pgmR3HandlerPhysicalOneClear, pVM);
207 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, false, pgmR3HandlerPhysicalOneSet, pVM);
208 pgmUnlock(pVM);
209}
210
211
212/**
213 * Clears all the page level flags for one physical handler range.
214 *
215 * @returns 0
216 * @param pNode Pointer to a PGMPHYSHANDLER.
217 * @param pvUser Pointer to the VM.
218 */
219static DECLCALLBACK(int) pgmR3HandlerPhysicalOneClear(PAVLROGCPHYSNODECORE pNode, void *pvUser)
220{
221 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
222 PPGMRAMRANGE pRamHint = NULL;
223 RTGCPHYS GCPhys = pCur->Core.Key;
224 RTUINT cPages = pCur->cPages;
225 PVM pVM = (PVM)pvUser;
226 for (;;)
227 {
228 PPGMPAGE pPage;
229 int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
230 if (RT_SUCCESS(rc))
231 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_NONE);
232 else
233 AssertRC(rc);
234
235 if (--cPages == 0)
236 return 0;
237 GCPhys += PAGE_SIZE;
238 }
239}
240
241
242/**
243 * Sets all the page level flags for one physical handler range.
244 *
245 * @returns 0
246 * @param pNode Pointer to a PGMPHYSHANDLER.
247 * @param pvUser Pointer to the VM.
248 */
249static DECLCALLBACK(int) pgmR3HandlerPhysicalOneSet(PAVLROGCPHYSNODECORE pNode, void *pvUser)
250{
251 PVM pVM = (PVM)pvUser;
252 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
253 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pVM, pCur);
254 unsigned uState = pCurType->uState;
255 PPGMRAMRANGE pRamHint = NULL;
256 RTGCPHYS GCPhys = pCur->Core.Key;
257 RTUINT cPages = pCur->cPages;
258 for (;;)
259 {
260 PPGMPAGE pPage;
261 int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
262 if (RT_SUCCESS(rc))
263 PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
264 else
265 AssertRC(rc);
266
267 if (--cPages == 0)
268 return 0;
269 GCPhys += PAGE_SIZE;
270 }
271}
272
273
274/**
275 * Register a virtual page access handler type, extended version.
276 *
277 * @returns VBox status code.
278 * @param pVM Pointer to the cross context VM structure.
279 * @param enmKind The kind of access handler.
280 * @param fRelocUserRC Whether the pvUserRC argument should be
281 * automatically relocated or not.
282 * @param pfnInvalidateR3 Pointer to the ring-3 invalidation handler callback.
283 * @param pfnHandlerR3 Pointer to the ring-3 handler callback.
284 * @param pfnHandlerRC Pointer to the raw-mode context handler callback.
285 * @param pszDesc The type description.
286 * @param phType Where to return the type handle (cross context
287 * safe).
288 * @remarks No virtual handlers when executing using HM (i.e. ring-0).
289 */
290VMMR3_INT_DECL(int) PGMR3HandlerVirtualTypeRegisterEx(PVM pVM, PGMVIRTHANDLERKIND enmKind, bool fRelocUserRC,
291 PFNPGMR3VIRTINVALIDATE pfnInvalidateR3,
292 PFNPGMR3VIRTHANDLER pfnHandlerR3,
293 RCPTRTYPE(PFNPGMRCVIRTHANDLER) pfnHandlerRC,
294 const char *pszDesc, PPGMVIRTHANDLERTYPE phType)
295{
296 AssertReturn(!HMIsEnabled(pVM), VERR_NOT_AVAILABLE); /* Not supported/relevant for VT-x and AMD-V. */
297 AssertReturn(RT_VALID_PTR(pfnHandlerR3) || enmKind == PGMVIRTHANDLERKIND_HYPERVISOR, VERR_INVALID_POINTER);
298 AssertPtrNullReturn(pfnInvalidateR3, VERR_INVALID_POINTER);
299 AssertReturn(pfnHandlerRC != NIL_RTRCPTR, VERR_INVALID_POINTER);
300 AssertPtrReturn(pszDesc, VERR_INVALID_POINTER);
301 AssertReturn( enmKind == PGMVIRTHANDLERKIND_WRITE
302 || enmKind == PGMVIRTHANDLERKIND_ALL
303 || enmKind == PGMVIRTHANDLERKIND_HYPERVISOR,
304 VERR_INVALID_PARAMETER);
305
306 PPGMVIRTHANDLERTYPEINT pType;
307 int rc = MMHyperAlloc(pVM, sizeof(*pType), 0, MM_TAG_PGM_HANDLER_TYPES, (void **)&pType);
308 if (RT_SUCCESS(rc))
309 {
310 pType->u32Magic = PGMVIRTHANDLERTYPEINT_MAGIC;
311 pType->cRefs = 1;
312 pType->enmKind = enmKind;
313 pType->fRelocUserRC = fRelocUserRC;
314 pType->uState = enmKind == PGMVIRTHANDLERKIND_ALL
315 ? PGM_PAGE_HNDL_VIRT_STATE_ALL : PGM_PAGE_HNDL_VIRT_STATE_WRITE;
316 pType->pfnInvalidateR3 = pfnInvalidateR3;
317 pType->pfnHandlerR3 = pfnHandlerR3;
318 pType->pfnHandlerRC = pfnHandlerRC;
319 pType->pszDesc = pszDesc;
320
321 pgmLock(pVM);
322 RTListOff32Append(&pVM->pgm.s.CTX_SUFF(pTrees)->HeadVirtHandlerTypes, &pType->ListNode);
323 pgmUnlock(pVM);
324
325 *phType = MMHyperHeapPtrToOffset(pVM, pType);
326 LogFlow(("PGMR3HandlerVirtualTypeRegisterEx: %p/%#x: enmKind=%d pfnInvalidateR3=%RHv pfnHandlerR3=%RHv pfnHandlerRC=%RRv pszDesc=%s\n",
327 pType, *phType, enmKind, pfnInvalidateR3, pfnHandlerR3, pfnHandlerRC, pszDesc));
328 return VINF_SUCCESS;
329 }
330 *phType = NIL_PGMVIRTHANDLERTYPE;
331 return rc;
332}
333
334
335/**
336 * Register a physical page access handler type.
337 *
338 * @returns VBox status code.
339 * @param pVM Pointer to the cross context VM structure.
340 * @param enmKind The kind of access handler.
341 * @param fRelocUserRC Whether the pvUserRC argument should be
342 * automatically relocated or not.
343 * @param pfnInvalidateR3 Pointer to the ring-3 invalidateion callback
344 * (optional, can be NULL).
345 * @param pfnHandlerR3 Pointer to the ring-3 handler callback.
346 * @param pszModRC The name of the raw-mode context module, NULL is an
347 * alias for the main RC module.
348 * @param pszHandlerRC The name of the raw-mode context handler, NULL if
349 * the ring-3 handler should be called.
350 * @param pszDesc The type description.
351 * @param phType Where to return the type handle (cross context
352 * safe).
353 * @remarks No virtual handlers when executing using HM (i.e. ring-0).
354 */
355VMMR3_INT_DECL(int) PGMR3HandlerVirtualTypeRegister(PVM pVM, PGMVIRTHANDLERKIND enmKind, bool fRelocUserRC,
356 PFNPGMR3VIRTINVALIDATE pfnInvalidateR3,
357 PFNPGMR3VIRTHANDLER pfnHandlerR3,
358 const char *pszHandlerRC, const char *pszModRC, const char *pszDesc,
359 PPGMVIRTHANDLERTYPE phType)
360{
361 LogFlow(("PGMR3HandlerVirtualTypeRegister: enmKind=%d pfnInvalidateR3=%RHv pfnHandlerR3=%RHv pszModRC=%s pszHandlerRC=%s pszDesc=%s\n",
362 enmKind, pfnInvalidateR3, pfnHandlerR3, pszHandlerRC, pszModRC, pszDesc));
363
364 /*
365 * Validate input.
366 */
367 if (!pszModRC)
368 pszModRC = VMMGC_MAIN_MODULE_NAME;
369 if (!pszHandlerRC)
370 pszHandlerRC = "pgmVirtHandlerRedirectToHC";
371 AssertPtrReturn(pszHandlerRC, VERR_INVALID_POINTER);
372
373 /*
374 * Resolve the GC handler.
375 */
376 RTRCPTR pfnHandlerRC = NIL_RTRCPTR;
377 int rc = PDMR3LdrGetSymbolRCLazy(pVM, pszModRC, NULL /*pszSearchPath*/, pszHandlerRC, &pfnHandlerRC);
378 if (RT_SUCCESS(rc))
379 return PGMR3HandlerVirtualTypeRegisterEx(pVM, enmKind, fRelocUserRC,
380 pfnInvalidateR3, pfnHandlerR3,
381 pfnHandlerRC,
382 pszDesc, phType);
383
384 AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszModRC, pszHandlerRC, 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->pfnHandlerRC, 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->pfnHandlerRC, 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