VirtualBox

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

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

PGM,++: Made the ring-3 physical access handler callbacks present in all contexts, where applicable. They are not yet registered or used. Taking things slowly.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 30.7 KB
Line 
1/* $Id: PGMHandler.cpp 55909 2015-05-18 13:09:16Z 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.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#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 PFNPGMPHYSHANDLER pfnHandlerR3,
84 R0PTRTYPE(PFNPGMRZPHYSPFHANDLER) pfnPfHandlerR0,
85 RCPTRTYPE(PFNPGMRZPHYSPFHANDLER) 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(PFNPGMPHYSHANDLER) 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 pszPfHandlerRC The name of the raw-mode context handler.
349 * @param pszDesc The type description.
350 * @param phType Where to return the type handle (cross context
351 * safe).
352 * @remarks No virtual handlers when executing using HM (i.e. ring-0).
353 */
354VMMR3_INT_DECL(int) PGMR3HandlerVirtualTypeRegister(PVM pVM, PGMVIRTHANDLERKIND enmKind, bool fRelocUserRC,
355 PFNPGMR3VIRTINVALIDATE pfnInvalidateR3,
356 PFNPGMR3VIRTHANDLER pfnHandlerR3,
357 const char *pszPfHandlerRC, const char *pszDesc,
358 PPGMVIRTHANDLERTYPE phType)
359{
360 LogFlow(("PGMR3HandlerVirtualTypeRegister: enmKind=%d pfnInvalidateR3=%RHv pfnHandlerR3=%RHv pszPfHandlerRC=%s pszDesc=%s\n",
361 enmKind, pfnInvalidateR3, pfnHandlerR3, pszPfHandlerRC, pszDesc));
362
363 /*
364 * Validate input.
365 */
366 AssertPtrReturn(pszPfHandlerRC, VERR_INVALID_POINTER);
367
368 /*
369 * Resolve the GC handler.
370 */
371 RTRCPTR pfnPfHandlerRC = NIL_RTRCPTR;
372 int rc = PDMR3LdrGetSymbolRCLazy(pVM, VMMGC_MAIN_MODULE_NAME, NULL /*pszSearchPath*/, pszPfHandlerRC, &pfnPfHandlerRC);
373 if (RT_SUCCESS(rc))
374 return PGMR3HandlerVirtualTypeRegisterEx(pVM, enmKind, fRelocUserRC,
375 pfnInvalidateR3, pfnHandlerR3,
376 pfnPfHandlerRC,
377 pszDesc, phType);
378
379 AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", VMMGC_MAIN_MODULE_NAME, pszPfHandlerRC, rc));
380 return rc;
381}
382
383
384/**
385 * Register a access handler for a virtual range.
386 *
387 * @returns VBox status code.
388 * @param pVM Pointer to the VM.
389 * @param hType The handler type.
390 * @param GCPtr Start address.
391 * @param GCPtrLast Last address (inclusive).
392 * @param pvUserR3 The ring-3 context user argument.
393 * @param pvUserRC The raw-mode context user argument. Whether this is
394 * automatically relocated or not depends on the type.
395 * @param pszDesc Pointer to description string. This must not be freed.
396 */
397VMMR3_INT_DECL(int) PGMR3HandlerVirtualRegister(PVM pVM, PVMCPU pVCpu, PGMVIRTHANDLERTYPE hType, RTGCPTR GCPtr, RTGCPTR GCPtrLast,
398 void *pvUserR3, RTRCPTR pvUserRC, const char *pszDesc)
399{
400 AssertReturn(!HMIsEnabled(pVM), VERR_NOT_AVAILABLE); /* Not supported/relevant for VT-x and AMD-V. */
401 PPGMVIRTHANDLERTYPEINT pType = PGMVIRTHANDLERTYPEINT_FROM_HANDLE(pVM, hType);
402 Log(("PGMR3HandlerVirtualRegister: GCPhys=%RGp GCPhysLast=%RGp pvUserR3=%RHv pvUserGC=%RRv hType=%#x (%d, %s) pszDesc=%RHv:%s\n",
403 GCPtr, GCPtrLast, pvUserR3, pvUserRC, hType, pType->enmKind, R3STRING(pType->pszDesc), pszDesc, R3STRING(pszDesc)));
404
405 /*
406 * Validate input.
407 */
408 AssertReturn(pType->u32Magic == PGMVIRTHANDLERTYPEINT_MAGIC, VERR_INVALID_HANDLE);
409 AssertMsgReturn(GCPtr < GCPtrLast, ("GCPtr >= GCPtrLast (%RGp >= %RGp)\n", GCPtr, GCPtrLast), VERR_INVALID_PARAMETER);
410 switch (pType->enmKind)
411 {
412 case PGMVIRTHANDLERKIND_ALL:
413 AssertReleaseMsgReturn( (GCPtr & PAGE_OFFSET_MASK) == 0
414 && (GCPtrLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK,
415 ("PGMVIRTHANDLERKIND_ALL: GCPtr=%RGv GCPtrLast=%RGv\n", GCPtr, GCPtrLast),
416 VERR_NOT_IMPLEMENTED);
417 break;
418 case PGMVIRTHANDLERKIND_WRITE:
419 case PGMVIRTHANDLERKIND_HYPERVISOR:
420 break;
421 default:
422 AssertMsgFailedReturn(("Invalid enmKind=%d!\n", pType->enmKind), VERR_INVALID_PARAMETER);
423 }
424 AssertMsgReturn( (RTRCUINTPTR)pvUserRC < 0x10000
425 || MMHyperR3ToRC(pVM, MMHyperRCToR3(pVM, pvUserRC)) == pvUserRC,
426 ("Not RC pointer! pvUserRC=%RRv\n", pvUserRC),
427 VERR_INVALID_PARAMETER);
428
429 /*
430 * Allocate and initialize a new entry.
431 */
432 unsigned cPages = (RT_ALIGN(GCPtrLast + 1, PAGE_SIZE) - (GCPtr & PAGE_BASE_GC_MASK)) >> PAGE_SHIFT;
433 PPGMVIRTHANDLER pNew;
434 int rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMVIRTHANDLER, aPhysToVirt[cPages]), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew); /** @todo r=bird: incorrect member name PhysToVirt? */
435 if (RT_FAILURE(rc))
436 return rc;
437
438 pNew->Core.Key = GCPtr;
439 pNew->Core.KeyLast = GCPtrLast;
440
441 pNew->hType = hType;
442 pNew->pvUserRC = pvUserRC;
443 pNew->pvUserR3 = pvUserR3;
444 pNew->pszDesc = pszDesc ? pszDesc : pType->pszDesc;
445 pNew->cb = GCPtrLast - GCPtr + 1;
446 pNew->cPages = cPages;
447 /* Will be synced at next guest execution attempt. */
448 while (cPages-- > 0)
449 {
450 pNew->aPhysToVirt[cPages].Core.Key = NIL_RTGCPHYS;
451 pNew->aPhysToVirt[cPages].Core.KeyLast = NIL_RTGCPHYS;
452 pNew->aPhysToVirt[cPages].offVirtHandler = -RT_OFFSETOF(PGMVIRTHANDLER, aPhysToVirt[cPages]);
453 pNew->aPhysToVirt[cPages].offNextAlias = 0;
454 }
455
456 /*
457 * Try to insert it into the tree.
458 *
459 * The current implementation doesn't allow multiple handlers for
460 * the same range this makes everything much simpler and faster.
461 */
462 AVLROGCPTRTREE *pRoot = pType->enmKind != PGMVIRTHANDLERKIND_HYPERVISOR
463 ? &pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers
464 : &pVM->pgm.s.CTX_SUFF(pTrees)->HyperVirtHandlers;
465 pgmLock(pVM);
466 if (*pRoot != 0)
467 {
468 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGetBestFit(pRoot, pNew->Core.Key, true);
469 if ( !pCur
470 || GCPtr > pCur->Core.KeyLast
471 || GCPtrLast < pCur->Core.Key)
472 pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGetBestFit(pRoot, pNew->Core.Key, false);
473 if ( pCur
474 && GCPtr <= pCur->Core.KeyLast
475 && GCPtrLast >= pCur->Core.Key)
476 {
477 /*
478 * The LDT sometimes conflicts with the IDT and LDT ranges while being
479 * updated on linux. So, we don't assert simply log it.
480 */
481 Log(("PGMR3HandlerVirtualRegister: Conflict with existing range %RGv-%RGv (%s), req. %RGv-%RGv (%s)\n",
482 pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc, GCPtr, GCPtrLast, pszDesc));
483 MMHyperFree(pVM, pNew);
484 pgmUnlock(pVM);
485 return VERR_PGM_HANDLER_VIRTUAL_CONFLICT;
486 }
487 }
488 if (RTAvlroGCPtrInsert(pRoot, &pNew->Core))
489 {
490 if (pType->enmKind != PGMVIRTHANDLERKIND_HYPERVISOR)
491 {
492 pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL | PGM_SYNC_CLEAR_PGM_POOL;
493 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
494 }
495 PGMHandlerVirtualTypeRetain(pVM, hType);
496 pgmUnlock(pVM);
497
498#ifdef VBOX_WITH_STATISTICS
499 rc = STAMR3RegisterF(pVM, &pNew->Stat, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, pNew->pszDesc,
500 "/PGM/VirtHandler/Calls/%RGv-%RGv", pNew->Core.Key, pNew->Core.KeyLast);
501 AssertRC(rc);
502#endif
503 return VINF_SUCCESS;
504 }
505
506 pgmUnlock(pVM);
507 AssertFailed();
508 MMHyperFree(pVM, pNew);
509 return VERR_PGM_HANDLER_VIRTUAL_CONFLICT;
510
511}
512
513
514/**
515 * Changes the type of a virtual handler.
516 *
517 * The new and old type must have the same access kind.
518 *
519 * @returns VBox status code.
520 * @param pVM Pointer to the VM.
521 * @param GCPtr Start address of the virtual handler.
522 * @param hNewType The new handler type.
523 */
524VMMR3_INT_DECL(int) PGMHandlerVirtualChangeType(PVM pVM, RTGCPTR GCPtr, PGMVIRTHANDLERTYPE hNewType)
525{
526 PPGMVIRTHANDLERTYPEINT pNewType = PGMVIRTHANDLERTYPEINT_FROM_HANDLE(pVM, hNewType);
527 AssertReturn(pNewType->u32Magic == PGMVIRTHANDLERTYPEINT_MAGIC, VERR_INVALID_HANDLE);
528
529 pgmLock(pVM);
530 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGet(&pVM->pgm.s.pTreesR3->VirtHandlers, GCPtr);
531 if (pCur)
532 {
533 PGMVIRTHANDLERTYPE hOldType = pCur->hType;
534 PPGMVIRTHANDLERTYPEINT pOldType = PGMVIRTHANDLERTYPEINT_FROM_HANDLE(pVM, hOldType);
535 if (pOldType != pNewType)
536 {
537 AssertReturnStmt(pNewType->enmKind == pOldType->enmKind, pgmUnlock(pVM), VERR_ACCESS_DENIED);
538 PGMHandlerVirtualTypeRetain(pVM, hNewType);
539 pCur->hType = hNewType;
540 PGMHandlerVirtualTypeRelease(pVM, hOldType);
541 }
542 pgmUnlock(pVM);
543 return VINF_SUCCESS;
544 }
545 pgmUnlock(pVM);
546 AssertMsgFailed(("Range %#x not found!\n", GCPtr));
547 return VERR_INVALID_PARAMETER;
548}
549
550
551/**
552 * Deregister an access handler for a virtual range.
553 *
554 * @returns VBox status code.
555 * @param pVM Pointer to the VM.
556 * @param pVCpu Pointer to the cross context CPU structure for the
557 * calling EMT.
558 * @param GCPtr Start address.
559 * @param fHypervisor Set if PGMVIRTHANDLERKIND_HYPERVISOR, false if not.
560 * @thread EMT(pVCpu)
561 */
562VMMR3_INT_DECL(int) PGMHandlerVirtualDeregister(PVM pVM, PVMCPU pVCpu, RTGCPTR GCPtr, bool fHypervisor)
563{
564 pgmLock(pVM);
565
566 PPGMVIRTHANDLER pCur;
567 if (!fHypervisor)
568 {
569 /*
570 * Normal guest handler.
571 */
572 pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, GCPtr);
573 AssertMsgReturnStmt(pCur, ("GCPtr=%RGv\n", GCPtr), pgmUnlock(pVM), VERR_INVALID_PARAMETER);
574 Assert(PGMVIRTANDLER_GET_TYPE(pVM, pCur)->enmKind != PGMVIRTHANDLERKIND_HYPERVISOR);
575
576 Log(("PGMHandlerVirtualDeregister: Removing Virtual (%d) Range %RGv-%RGv %s\n",
577 PGMVIRTANDLER_GET_TYPE(pVM, pCur)->enmKind, pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc));
578
579 /* Reset the flags and remove phys2virt nodes. */
580 for (uint32_t iPage = 0; iPage < pCur->cPages; iPage++)
581 if (pCur->aPhysToVirt[iPage].offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE)
582 pgmHandlerVirtualClearPage(pVM, pCur, iPage);
583
584 /* Schedule CR3 sync. */
585 pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL | PGM_SYNC_CLEAR_PGM_POOL;
586 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
587 }
588 else
589 {
590 /*
591 * Hypervisor one (hypervisor relocation or termination only).
592 */
593 pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->HyperVirtHandlers, GCPtr);
594 AssertMsgReturnStmt(pCur, ("GCPtr=%RGv\n", GCPtr), pgmUnlock(pVM), VERR_INVALID_PARAMETER);
595 Assert(PGMVIRTANDLER_GET_TYPE(pVM, pCur)->enmKind == PGMVIRTHANDLERKIND_HYPERVISOR);
596
597 Log(("PGMHandlerVirtualDeregister: Removing Hyper Virtual Range %RGv-%RGv %s\n",
598 pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc));
599 }
600
601 pgmUnlock(pVM);
602
603 /*
604 * Free it.
605 */
606#ifdef VBOX_WITH_STATISTICS
607 STAMR3DeregisterF(pVM->pUVM, "/PGM/VirtHandler/Calls/%RGv-%RGv", pCur->Core.Key, pCur->Core.KeyLast);
608#endif
609 PGMHandlerVirtualTypeRelease(pVM, pCur->hType);
610 MMHyperFree(pVM, pCur);
611
612 return VINF_SUCCESS;
613}
614
615
616/**
617 * Arguments for pgmR3InfoHandlersPhysicalOne and pgmR3InfoHandlersVirtualOne.
618 */
619typedef struct PGMHANDLERINFOARG
620{
621 /** The output helpers.*/
622 PCDBGFINFOHLP pHlp;
623 /** Pointer to the cross context VM handle. */
624 PVM pVM;
625 /** Set if statistics should be dumped. */
626 bool fStats;
627} PGMHANDLERINFOARG, *PPGMHANDLERINFOARG;
628
629
630/**
631 * Info callback for 'pgmhandlers'.
632 *
633 * @param pHlp The output helpers.
634 * @param pszArgs The arguments. phys or virt.
635 */
636DECLCALLBACK(void) pgmR3InfoHandlers(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
637{
638 /*
639 * Test input.
640 */
641 PGMHANDLERINFOARG Args = { pHlp, pVM, /* .fStats = */ true };
642 bool fPhysical = !pszArgs || !*pszArgs;
643 bool fVirtual = fPhysical;
644 bool fHyper = fPhysical;
645 if (!fPhysical)
646 {
647 bool fAll = strstr(pszArgs, "all") != NULL;
648 fPhysical = fAll || strstr(pszArgs, "phys") != NULL;
649 fVirtual = fAll || strstr(pszArgs, "virt") != NULL;
650 fHyper = fAll || strstr(pszArgs, "hyper")!= NULL;
651 Args.fStats = strstr(pszArgs, "nost") == NULL;
652 }
653
654 /*
655 * Dump the handlers.
656 */
657 if (fPhysical)
658 {
659 pHlp->pfnPrintf(pHlp,
660 "Physical handlers: (PhysHandlers=%d (%#x))\n"
661 "%*s %*s %*s %*s HandlerGC UserGC Type Description\n",
662 pVM->pgm.s.pTreesR3->PhysHandlers, pVM->pgm.s.pTreesR3->PhysHandlers,
663 - (int)sizeof(RTGCPHYS) * 2, "From",
664 - (int)sizeof(RTGCPHYS) * 2 - 3, "- To (incl)",
665 - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
666 - (int)sizeof(RTHCPTR) * 2 - 1, "UserHC");
667 RTAvlroGCPhysDoWithAll(&pVM->pgm.s.pTreesR3->PhysHandlers, true, pgmR3InfoHandlersPhysicalOne, &Args);
668 }
669
670 if (fVirtual)
671 {
672 pHlp->pfnPrintf(pHlp,
673 "Virtual handlers:\n"
674 "%*s %*s %*s %*s Type Description\n",
675 - (int)sizeof(RTGCPTR) * 2, "From",
676 - (int)sizeof(RTGCPTR) * 2 - 3, "- To (excl)",
677 - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
678 - (int)sizeof(RTRCPTR) * 2 - 1, "HandlerGC");
679 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.pTreesR3->VirtHandlers, true, pgmR3InfoHandlersVirtualOne, &Args);
680 }
681
682 if (fHyper)
683 {
684 pHlp->pfnPrintf(pHlp,
685 "Hypervisor Virtual handlers:\n"
686 "%*s %*s %*s %*s Type Description\n",
687 - (int)sizeof(RTGCPTR) * 2, "From",
688 - (int)sizeof(RTGCPTR) * 2 - 3, "- To (excl)",
689 - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
690 - (int)sizeof(RTRCPTR) * 2 - 1, "HandlerGC");
691 RTAvlroGCPtrDoWithAll(&pVM->pgm.s.pTreesR3->HyperVirtHandlers, true, pgmR3InfoHandlersVirtualOne, &Args);
692 }
693}
694
695
696/**
697 * Displays one physical handler range.
698 *
699 * @returns 0
700 * @param pNode Pointer to a PGMPHYSHANDLER.
701 * @param pvUser Pointer to command helper functions.
702 */
703static DECLCALLBACK(int) pgmR3InfoHandlersPhysicalOne(PAVLROGCPHYSNODECORE pNode, void *pvUser)
704{
705 PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
706 PPGMHANDLERINFOARG pArgs = (PPGMHANDLERINFOARG)pvUser;
707 PCDBGFINFOHLP pHlp = pArgs->pHlp;
708 PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pArgs->pVM, pCur);
709 const char *pszType;
710 switch (pCurType->enmKind)
711 {
712 case PGMPHYSHANDLERKIND_MMIO: pszType = "MMIO "; break;
713 case PGMPHYSHANDLERKIND_WRITE: pszType = "Write "; break;
714 case PGMPHYSHANDLERKIND_ALL: pszType = "All "; break;
715 default: pszType = "????"; break;
716 }
717 pHlp->pfnPrintf(pHlp,
718 "%RGp - %RGp %RHv %RHv %RRv %RRv %s %s\n",
719 pCur->Core.Key, pCur->Core.KeyLast, pCurType->pfnHandlerR3, pCur->pvUserR3, pCurType->pfnPfHandlerRC, pCur->pvUserRC,
720 pszType, pCur->pszDesc);
721#ifdef VBOX_WITH_STATISTICS
722 if (pArgs->fStats)
723 pHlp->pfnPrintf(pHlp, " cPeriods: %9RU64 cTicks: %11RU64 Min: %11RU64 Avg: %11RU64 Max: %11RU64\n",
724 pCur->Stat.cPeriods, pCur->Stat.cTicks, pCur->Stat.cTicksMin,
725 pCur->Stat.cPeriods ? pCur->Stat.cTicks / pCur->Stat.cPeriods : 0, pCur->Stat.cTicksMax);
726#endif
727 return 0;
728}
729
730
731/**
732 * Displays one virtual handler range.
733 *
734 * @returns 0
735 * @param pNode Pointer to a PGMVIRTHANDLER.
736 * @param pvUser Pointer to command helper functions.
737 */
738static DECLCALLBACK(int) pgmR3InfoHandlersVirtualOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
739{
740 PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
741 PPGMHANDLERINFOARG pArgs = (PPGMHANDLERINFOARG)pvUser;
742 PCDBGFINFOHLP pHlp = pArgs->pHlp;
743 PPGMVIRTHANDLERTYPEINT pCurType = PGMVIRTANDLER_GET_TYPE(pArgs->pVM, pCur);
744 const char *pszType;
745 switch (pCurType->enmKind)
746 {
747 case PGMVIRTHANDLERKIND_WRITE: pszType = "Write "; break;
748 case PGMVIRTHANDLERKIND_ALL: pszType = "All "; break;
749 case PGMVIRTHANDLERKIND_HYPERVISOR: pszType = "WriteHyp "; break;
750 default: pszType = "????"; break;
751 }
752 pHlp->pfnPrintf(pHlp, "%RGv - %RGv %RHv %RRv %s %s\n",
753 pCur->Core.Key, pCur->Core.KeyLast, pCurType->pfnHandlerR3, pCurType->pfnPfHandlerRC, pszType, pCur->pszDesc);
754#ifdef VBOX_WITH_STATISTICS
755 if (pArgs->fStats)
756 pHlp->pfnPrintf(pHlp, " cPeriods: %9RU64 cTicks: %11RU64 Min: %11RU64 Avg: %11RU64 Max: %11RU64\n",
757 pCur->Stat.cPeriods, pCur->Stat.cTicks, pCur->Stat.cTicksMin,
758 pCur->Stat.cPeriods ? pCur->Stat.cTicks / pCur->Stat.cPeriods : 0, pCur->Stat.cTicksMax);
759#endif
760 return 0;
761}
762
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