1 | /* $Id: PGMHandler.cpp 45808 2013-04-29 12:41:07Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PGM - Page Manager / Monitor, Access Handlers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2012 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 | *******************************************************************************/
|
---|
60 | static DECLCALLBACK(int) pgmR3HandlerPhysicalOneClear(PAVLROGCPHYSNODECORE pNode, void *pvUser);
|
---|
61 | static DECLCALLBACK(int) pgmR3HandlerPhysicalOneSet(PAVLROGCPHYSNODECORE pNode, void *pvUser);
|
---|
62 | static DECLCALLBACK(int) pgmR3InfoHandlersPhysicalOne(PAVLROGCPHYSNODECORE pNode, void *pvUser);
|
---|
63 | static DECLCALLBACK(int) pgmR3InfoHandlersVirtualOne(PAVLROGCPTRNODECORE pNode, void *pvUser);
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Register a access handler for a physical range.
|
---|
69 | *
|
---|
70 | * @returns VBox status code.
|
---|
71 | * @param pVM Pointer to the VM.
|
---|
72 | * @param enmType Handler type. Any of the PGMPHYSHANDLERTYPE_PHYSICAL* enums.
|
---|
73 | * @param GCPhys Start physical address.
|
---|
74 | * @param GCPhysLast Last physical address. (inclusive)
|
---|
75 | * @param pfnHandlerR3 The R3 handler.
|
---|
76 | * @param pvUserR3 User argument to the R3 handler.
|
---|
77 | * @param pszModR0 The R0 handler module. NULL means the default R0 module.
|
---|
78 | * @param pszHandlerR0 The R0 handler symbol name.
|
---|
79 | * @param pvUserR0 User argument to the R0 handler.
|
---|
80 | * @param pszModRC The RC handler module. NULL means the default RC
|
---|
81 | * module.
|
---|
82 | * @param pszHandlerRC The RC handler symbol name.
|
---|
83 | * @param pvUserRC User argument to the RC handler. Values less than
|
---|
84 | * 0x10000 will not be relocated.
|
---|
85 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
86 | */
|
---|
87 | VMMR3DECL(int) PGMR3HandlerPhysicalRegister(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast,
|
---|
88 | PFNPGMR3PHYSHANDLER pfnHandlerR3, void *pvUserR3,
|
---|
89 | const char *pszModR0, const char *pszHandlerR0, RTR0PTR pvUserR0,
|
---|
90 | const char *pszModRC, const char *pszHandlerRC, RTRCPTR pvUserRC, const char *pszDesc)
|
---|
91 | {
|
---|
92 | LogFlow(("PGMR3HandlerPhysicalRegister: enmType=%d GCPhys=%RGp GCPhysLast=%RGp pfnHandlerR3=%RHv pvUserHC=%RHv pszModR0=%s pszHandlerR0=%s pvUserR0=%RHv pszModRC=%s pszHandlerRC=%s pvUser=%RRv pszDesc=%s\n",
|
---|
93 | enmType, GCPhys, GCPhysLast, pfnHandlerR3, pvUserR3, pszModR0, pszHandlerR0, pvUserR0, pszHandlerRC, pszModRC, pvUserRC, pszDesc));
|
---|
94 |
|
---|
95 | /*
|
---|
96 | * Validate input.
|
---|
97 | */
|
---|
98 | if (!pszModRC)
|
---|
99 | pszModRC = VMMGC_MAIN_MODULE_NAME;
|
---|
100 | if (!pszModR0)
|
---|
101 | pszModR0 = VMMR0_MAIN_MODULE_NAME;
|
---|
102 | if (!pszHandlerR0)
|
---|
103 | pszHandlerR0 = "pgmPhysHandlerRedirectToHC";
|
---|
104 | if (!pszHandlerRC)
|
---|
105 | pszHandlerRC = "pgmPhysHandlerRedirectToHC";
|
---|
106 | AssertPtrReturn(pfnHandlerR3, VERR_INVALID_POINTER);
|
---|
107 | AssertPtrReturn(pszHandlerR0, VERR_INVALID_POINTER);
|
---|
108 | AssertPtrReturn(pszHandlerRC, VERR_INVALID_POINTER);
|
---|
109 |
|
---|
110 | /*
|
---|
111 | * Resolve the R0 handler.
|
---|
112 | */
|
---|
113 | R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0 = NIL_RTR0PTR;
|
---|
114 | int rc = VINF_SUCCESS;
|
---|
115 | rc = PDMR3LdrGetSymbolR0Lazy(pVM, pszModR0, NULL /*pszSearchPath*/, pszHandlerR0, &pfnHandlerR0);
|
---|
116 | if (RT_SUCCESS(rc))
|
---|
117 | {
|
---|
118 | /*
|
---|
119 | * Resolve the GC handler.
|
---|
120 | */
|
---|
121 | RTRCPTR pfnHandlerRC = NIL_RTRCPTR;
|
---|
122 | if (!HMIsEnabled(pVM))
|
---|
123 | rc = PDMR3LdrGetSymbolRCLazy(pVM, pszModRC, NULL /*pszSearchPath*/, pszHandlerRC, &pfnHandlerRC);
|
---|
124 | if (RT_SUCCESS(rc))
|
---|
125 | return PGMHandlerPhysicalRegisterEx(pVM, enmType, GCPhys, GCPhysLast, pfnHandlerR3, pvUserR3,
|
---|
126 | pfnHandlerR0, pvUserR0, pfnHandlerRC, pvUserRC, pszDesc);
|
---|
127 |
|
---|
128 | AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszModRC, pszHandlerRC, rc));
|
---|
129 | }
|
---|
130 | else
|
---|
131 | AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszModR0, pszHandlerR0, rc));
|
---|
132 |
|
---|
133 | return rc;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * Updates the physical page access handlers.
|
---|
139 | *
|
---|
140 | * @param pVM Pointer to the VM.
|
---|
141 | * @remark Only used when restoring a saved state.
|
---|
142 | */
|
---|
143 | void pgmR3HandlerPhysicalUpdateAll(PVM pVM)
|
---|
144 | {
|
---|
145 | LogFlow(("pgmHandlerPhysicalUpdateAll:\n"));
|
---|
146 |
|
---|
147 | /*
|
---|
148 | * Clear and set.
|
---|
149 | * (the right -> left on the setting pass is just bird speculating on cache hits)
|
---|
150 | */
|
---|
151 | pgmLock(pVM);
|
---|
152 | RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, true, pgmR3HandlerPhysicalOneClear, pVM);
|
---|
153 | RTAvlroGCPhysDoWithAll(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, false, pgmR3HandlerPhysicalOneSet, pVM);
|
---|
154 | pgmUnlock(pVM);
|
---|
155 | }
|
---|
156 |
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Clears all the page level flags for one physical handler range.
|
---|
160 | *
|
---|
161 | * @returns 0
|
---|
162 | * @param pNode Pointer to a PGMPHYSHANDLER.
|
---|
163 | * @param pvUser Pointer to the VM.
|
---|
164 | */
|
---|
165 | static DECLCALLBACK(int) pgmR3HandlerPhysicalOneClear(PAVLROGCPHYSNODECORE pNode, void *pvUser)
|
---|
166 | {
|
---|
167 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
|
---|
168 | PPGMRAMRANGE pRamHint = NULL;
|
---|
169 | RTGCPHYS GCPhys = pCur->Core.Key;
|
---|
170 | RTUINT cPages = pCur->cPages;
|
---|
171 | PVM pVM = (PVM)pvUser;
|
---|
172 | for (;;)
|
---|
173 | {
|
---|
174 | PPGMPAGE pPage;
|
---|
175 | int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
|
---|
176 | if (RT_SUCCESS(rc))
|
---|
177 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, PGM_PAGE_HNDL_PHYS_STATE_NONE);
|
---|
178 | else
|
---|
179 | AssertRC(rc);
|
---|
180 |
|
---|
181 | if (--cPages == 0)
|
---|
182 | return 0;
|
---|
183 | GCPhys += PAGE_SIZE;
|
---|
184 | }
|
---|
185 | }
|
---|
186 |
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Sets all the page level flags for one physical handler range.
|
---|
190 | *
|
---|
191 | * @returns 0
|
---|
192 | * @param pNode Pointer to a PGMPHYSHANDLER.
|
---|
193 | * @param pvUser Pointer to the VM.
|
---|
194 | */
|
---|
195 | static DECLCALLBACK(int) pgmR3HandlerPhysicalOneSet(PAVLROGCPHYSNODECORE pNode, void *pvUser)
|
---|
196 | {
|
---|
197 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
|
---|
198 | unsigned uState = pgmHandlerPhysicalCalcState(pCur);
|
---|
199 | PPGMRAMRANGE pRamHint = NULL;
|
---|
200 | RTGCPHYS GCPhys = pCur->Core.Key;
|
---|
201 | RTUINT cPages = pCur->cPages;
|
---|
202 | PVM pVM = (PVM)pvUser;
|
---|
203 | for (;;)
|
---|
204 | {
|
---|
205 | PPGMPAGE pPage;
|
---|
206 | int rc = pgmPhysGetPageWithHintEx(pVM, GCPhys, &pPage, &pRamHint);
|
---|
207 | if (RT_SUCCESS(rc))
|
---|
208 | PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, uState);
|
---|
209 | else
|
---|
210 | AssertRC(rc);
|
---|
211 |
|
---|
212 | if (--cPages == 0)
|
---|
213 | return 0;
|
---|
214 | GCPhys += PAGE_SIZE;
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Register a access handler for a virtual range.
|
---|
221 | *
|
---|
222 | * @returns VBox status code.
|
---|
223 | * @param pVM Pointer to the VM.
|
---|
224 | * @param enmType Handler type. Any of the PGMVIRTHANDLERTYPE_* enums.
|
---|
225 | * @param GCPtr Start address.
|
---|
226 | * @param GCPtrLast Last address (inclusive).
|
---|
227 | * @param pfnInvalidateR3 The R3 invalidate callback (can be 0)
|
---|
228 | * @param pfnHandlerR3 The R3 handler.
|
---|
229 | * @param pszHandlerRC The RC handler symbol name.
|
---|
230 | * @param pszModRC The RC handler module.
|
---|
231 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
232 | */
|
---|
233 | VMMR3DECL(int) PGMR3HandlerVirtualRegister(PVM pVM, PGMVIRTHANDLERTYPE enmType, RTGCPTR GCPtr, RTGCPTR GCPtrLast,
|
---|
234 | PFNPGMR3VIRTINVALIDATE pfnInvalidateR3,
|
---|
235 | PFNPGMR3VIRTHANDLER pfnHandlerR3,
|
---|
236 | const char *pszHandlerRC, const char *pszModRC,
|
---|
237 | const char *pszDesc)
|
---|
238 | {
|
---|
239 | LogFlow(("PGMR3HandlerVirtualRegisterEx: enmType=%d GCPtr=%RGv GCPtrLast=%RGv pszHandlerRC=%p:{%s} pszModRC=%p:{%s} pszDesc=%s\n",
|
---|
240 | enmType, GCPtr, GCPtrLast, pszHandlerRC, pszHandlerRC, pszModRC, pszModRC, pszDesc));
|
---|
241 |
|
---|
242 | /* Not supported/relevant for VT-x and AMD-V. */
|
---|
243 | if (HMIsEnabled(pVM))
|
---|
244 | return VERR_NOT_IMPLEMENTED;
|
---|
245 |
|
---|
246 | /*
|
---|
247 | * Validate input.
|
---|
248 | */
|
---|
249 | if (!pszModRC)
|
---|
250 | pszModRC = VMMGC_MAIN_MODULE_NAME;
|
---|
251 | if (!pszModRC || !*pszModRC || !pszHandlerRC || !*pszHandlerRC)
|
---|
252 | {
|
---|
253 | AssertMsgFailed(("pfnHandlerGC or/and pszModRC is missing\n"));
|
---|
254 | return VERR_INVALID_PARAMETER;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * Resolve the GC handler.
|
---|
259 | */
|
---|
260 | RTRCPTR pfnHandlerRC;
|
---|
261 | int rc = PDMR3LdrGetSymbolRCLazy(pVM, pszModRC, NULL /*pszSearchPath*/, pszHandlerRC, &pfnHandlerRC);
|
---|
262 | if (RT_SUCCESS(rc))
|
---|
263 | return PGMR3HandlerVirtualRegisterEx(pVM, enmType, GCPtr, GCPtrLast, pfnInvalidateR3,
|
---|
264 | pfnHandlerR3, pfnHandlerRC, pszDesc);
|
---|
265 |
|
---|
266 | AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszModRC, pszHandlerRC, rc));
|
---|
267 | return rc;
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Register an access handler for a virtual range.
|
---|
273 | *
|
---|
274 | * @returns VBox status code.
|
---|
275 | * @param pVM Pointer to the VM.
|
---|
276 | * @param enmType Handler type. Any of the PGMVIRTHANDLERTYPE_* enums.
|
---|
277 | * @param GCPtr Start address.
|
---|
278 | * @param GCPtrLast Last address (inclusive).
|
---|
279 | * @param pfnInvalidateR3 The R3 invalidate callback (can be 0)
|
---|
280 | * @param pfnHandlerR3 The R3 handler.
|
---|
281 | * @param pfnHandlerRC The RC handler.
|
---|
282 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
283 | * @thread EMT
|
---|
284 | */
|
---|
285 | /** @todo create a template for virtual handlers (see async i/o), we're wasting space
|
---|
286 | * duplicating the function pointers now. (Or we will once we add the missing callbacks.) */
|
---|
287 | VMMDECL(int) PGMR3HandlerVirtualRegisterEx(PVM pVM, PGMVIRTHANDLERTYPE enmType, RTGCPTR GCPtr, RTGCPTR GCPtrLast,
|
---|
288 | R3PTRTYPE(PFNPGMR3VIRTINVALIDATE) pfnInvalidateR3,
|
---|
289 | R3PTRTYPE(PFNPGMR3VIRTHANDLER) pfnHandlerR3,
|
---|
290 | RCPTRTYPE(PFNPGMRCVIRTHANDLER) pfnHandlerRC,
|
---|
291 | R3PTRTYPE(const char *) pszDesc)
|
---|
292 | {
|
---|
293 | Log(("PGMR3HandlerVirtualRegister: enmType=%d GCPtr=%RGv GCPtrLast=%RGv pfnInvalidateR3=%RHv pfnHandlerR3=%RHv pfnHandlerRC=%RRv pszDesc=%s\n",
|
---|
294 | enmType, GCPtr, GCPtrLast, pfnInvalidateR3, pfnHandlerR3, pfnHandlerRC, pszDesc));
|
---|
295 |
|
---|
296 | /* Not supported/relevant for VT-x and AMD-V. */
|
---|
297 | if (HMIsEnabled(pVM))
|
---|
298 | return VERR_NOT_IMPLEMENTED;
|
---|
299 |
|
---|
300 | /*
|
---|
301 | * Validate input.
|
---|
302 | */
|
---|
303 | switch (enmType)
|
---|
304 | {
|
---|
305 | case PGMVIRTHANDLERTYPE_ALL:
|
---|
306 | AssertReleaseMsgReturn( (GCPtr & PAGE_OFFSET_MASK) == 0
|
---|
307 | && (GCPtrLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK,
|
---|
308 | ("PGMVIRTHANDLERTYPE_ALL: GCPtr=%RGv GCPtrLast=%RGv\n", GCPtr, GCPtrLast),
|
---|
309 | VERR_NOT_IMPLEMENTED);
|
---|
310 | break;
|
---|
311 | case PGMVIRTHANDLERTYPE_WRITE:
|
---|
312 | if (!pfnHandlerR3)
|
---|
313 | {
|
---|
314 | AssertMsgFailed(("No HC handler specified!!\n"));
|
---|
315 | return VERR_INVALID_PARAMETER;
|
---|
316 | }
|
---|
317 | break;
|
---|
318 |
|
---|
319 | case PGMVIRTHANDLERTYPE_HYPERVISOR:
|
---|
320 | if (pfnHandlerR3)
|
---|
321 | {
|
---|
322 | AssertMsgFailed(("R3 handler specified for hypervisor range!?!\n"));
|
---|
323 | return VERR_INVALID_PARAMETER;
|
---|
324 | }
|
---|
325 | break;
|
---|
326 | default:
|
---|
327 | AssertMsgFailed(("Invalid enmType! enmType=%d\n", enmType));
|
---|
328 | return VERR_INVALID_PARAMETER;
|
---|
329 | }
|
---|
330 | if (GCPtrLast < GCPtr)
|
---|
331 | {
|
---|
332 | AssertMsgFailed(("GCPtrLast < GCPtr (%#x < %#x)\n", GCPtrLast, GCPtr));
|
---|
333 | return VERR_INVALID_PARAMETER;
|
---|
334 | }
|
---|
335 | if (!pfnHandlerRC)
|
---|
336 | {
|
---|
337 | AssertMsgFailed(("pfnHandlerRC is missing\n"));
|
---|
338 | return VERR_INVALID_PARAMETER;
|
---|
339 | }
|
---|
340 |
|
---|
341 | /*
|
---|
342 | * Allocate and initialize a new entry.
|
---|
343 | */
|
---|
344 | unsigned cPages = (RT_ALIGN(GCPtrLast + 1, PAGE_SIZE) - (GCPtr & PAGE_BASE_GC_MASK)) >> PAGE_SHIFT;
|
---|
345 | PPGMVIRTHANDLER pNew;
|
---|
346 | int rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMVIRTHANDLER, aPhysToVirt[cPages]), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew); /** @todo r=bird: incorrect member name PhysToVirt? */
|
---|
347 | if (RT_FAILURE(rc))
|
---|
348 | return rc;
|
---|
349 |
|
---|
350 | pNew->Core.Key = GCPtr;
|
---|
351 | pNew->Core.KeyLast = GCPtrLast;
|
---|
352 |
|
---|
353 | pNew->enmType = enmType;
|
---|
354 | pNew->pfnInvalidateR3 = pfnInvalidateR3;
|
---|
355 | pNew->pfnHandlerRC = pfnHandlerRC;
|
---|
356 | pNew->pfnHandlerR3 = pfnHandlerR3;
|
---|
357 | pNew->pszDesc = pszDesc;
|
---|
358 | pNew->cb = GCPtrLast - GCPtr + 1;
|
---|
359 | pNew->cPages = cPages;
|
---|
360 | /* Will be synced at next guest execution attempt. */
|
---|
361 | while (cPages-- > 0)
|
---|
362 | {
|
---|
363 | pNew->aPhysToVirt[cPages].Core.Key = NIL_RTGCPHYS;
|
---|
364 | pNew->aPhysToVirt[cPages].Core.KeyLast = NIL_RTGCPHYS;
|
---|
365 | pNew->aPhysToVirt[cPages].offVirtHandler = -RT_OFFSETOF(PGMVIRTHANDLER, aPhysToVirt[cPages]);
|
---|
366 | pNew->aPhysToVirt[cPages].offNextAlias = 0;
|
---|
367 | }
|
---|
368 |
|
---|
369 | /*
|
---|
370 | * Try to insert it into the tree.
|
---|
371 | *
|
---|
372 | * The current implementation doesn't allow multiple handlers for
|
---|
373 | * the same range this makes everything much simpler and faster.
|
---|
374 | */
|
---|
375 | AVLROGCPTRTREE *pRoot = enmType != PGMVIRTHANDLERTYPE_HYPERVISOR
|
---|
376 | ? &pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers
|
---|
377 | : &pVM->pgm.s.CTX_SUFF(pTrees)->HyperVirtHandlers;
|
---|
378 | pgmLock(pVM);
|
---|
379 | if (*pRoot != 0)
|
---|
380 | {
|
---|
381 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGetBestFit(pRoot, pNew->Core.Key, true);
|
---|
382 | if ( !pCur
|
---|
383 | || GCPtr > pCur->Core.KeyLast
|
---|
384 | || GCPtrLast < pCur->Core.Key)
|
---|
385 | pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGetBestFit(pRoot, pNew->Core.Key, false);
|
---|
386 | if ( pCur
|
---|
387 | && GCPtr <= pCur->Core.KeyLast
|
---|
388 | && GCPtrLast >= pCur->Core.Key)
|
---|
389 | {
|
---|
390 | /*
|
---|
391 | * The LDT sometimes conflicts with the IDT and LDT ranges while being
|
---|
392 | * updated on linux. So, we don't assert simply log it.
|
---|
393 | */
|
---|
394 | Log(("PGMR3HandlerVirtualRegister: Conflict with existing range %RGv-%RGv (%s), req. %RGv-%RGv (%s)\n",
|
---|
395 | pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc, GCPtr, GCPtrLast, pszDesc));
|
---|
396 | MMHyperFree(pVM, pNew);
|
---|
397 | pgmUnlock(pVM);
|
---|
398 | return VERR_PGM_HANDLER_VIRTUAL_CONFLICT;
|
---|
399 | }
|
---|
400 | }
|
---|
401 | if (RTAvlroGCPtrInsert(pRoot, &pNew->Core))
|
---|
402 | {
|
---|
403 | if (enmType != PGMVIRTHANDLERTYPE_HYPERVISOR)
|
---|
404 | {
|
---|
405 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
406 |
|
---|
407 | pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL | PGM_SYNC_CLEAR_PGM_POOL;
|
---|
408 | VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
|
---|
409 | }
|
---|
410 | pgmUnlock(pVM);
|
---|
411 |
|
---|
412 | #ifdef VBOX_WITH_STATISTICS
|
---|
413 | char szPath[256];
|
---|
414 | RTStrPrintf(szPath, sizeof(szPath), "/PGM/VirtHandler/Calls/%RGv-%RGv", pNew->Core.Key, pNew->Core.KeyLast);
|
---|
415 | rc = STAMR3Register(pVM, &pNew->Stat, STAMTYPE_PROFILE, STAMVISIBILITY_USED, szPath, STAMUNIT_TICKS_PER_CALL, pszDesc);
|
---|
416 | AssertRC(rc);
|
---|
417 | #endif
|
---|
418 | return VINF_SUCCESS;
|
---|
419 | }
|
---|
420 |
|
---|
421 | pgmUnlock(pVM);
|
---|
422 | AssertFailed();
|
---|
423 | MMHyperFree(pVM, pNew);
|
---|
424 | return VERR_PGM_HANDLER_VIRTUAL_CONFLICT;
|
---|
425 | }
|
---|
426 |
|
---|
427 |
|
---|
428 | /**
|
---|
429 | * Modify the page invalidation callback handler for a registered virtual range.
|
---|
430 | * (add more when needed)
|
---|
431 | *
|
---|
432 | * @returns VBox status code.
|
---|
433 | * @param pVM Pointer to the VM.
|
---|
434 | * @param GCPtr Start address.
|
---|
435 | * @param pfnInvalidateR3 The R3 invalidate callback (can be 0)
|
---|
436 | * @remarks Doesn't work with the hypervisor access handler type.
|
---|
437 | */
|
---|
438 | VMMDECL(int) PGMHandlerVirtualChangeInvalidateCallback(PVM pVM, RTGCPTR GCPtr, PFNPGMR3VIRTINVALIDATE pfnInvalidateR3)
|
---|
439 | {
|
---|
440 | pgmLock(pVM);
|
---|
441 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGet(&pVM->pgm.s.pTreesR3->VirtHandlers, GCPtr);
|
---|
442 | if (pCur)
|
---|
443 | {
|
---|
444 | pCur->pfnInvalidateR3 = pfnInvalidateR3;
|
---|
445 | pgmUnlock(pVM);
|
---|
446 | return VINF_SUCCESS;
|
---|
447 | }
|
---|
448 | pgmUnlock(pVM);
|
---|
449 | AssertMsgFailed(("Range %#x not found!\n", GCPtr));
|
---|
450 | return VERR_INVALID_PARAMETER;
|
---|
451 | }
|
---|
452 |
|
---|
453 | /**
|
---|
454 | * Deregister an access handler for a virtual range.
|
---|
455 | *
|
---|
456 | * @returns VBox status code.
|
---|
457 | * @param pVM Pointer to the VM.
|
---|
458 | * @param GCPtr Start address.
|
---|
459 | * @thread EMT
|
---|
460 | */
|
---|
461 | VMMDECL(int) PGMHandlerVirtualDeregister(PVM pVM, RTGCPTR GCPtr)
|
---|
462 | {
|
---|
463 | pgmLock(pVM);
|
---|
464 |
|
---|
465 | /*
|
---|
466 | * Find the handler.
|
---|
467 | * We naturally assume GCPtr is a unique specification.
|
---|
468 | */
|
---|
469 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, GCPtr);
|
---|
470 | if (RT_LIKELY(pCur))
|
---|
471 | {
|
---|
472 | Log(("PGMHandlerVirtualDeregister: Removing Virtual (%d) Range %RGv-%RGv %s\n", pCur->enmType,
|
---|
473 | pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc));
|
---|
474 | Assert(pCur->enmType != PGMVIRTHANDLERTYPE_HYPERVISOR);
|
---|
475 |
|
---|
476 | /*
|
---|
477 | * Reset the flags and remove phys2virt nodes.
|
---|
478 | */
|
---|
479 | for (uint32_t iPage = 0; iPage < pCur->cPages; iPage++)
|
---|
480 | if (pCur->aPhysToVirt[iPage].offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE)
|
---|
481 | pgmHandlerVirtualClearPage(pVM, pCur, iPage);
|
---|
482 |
|
---|
483 | /*
|
---|
484 | * Schedule CR3 sync.
|
---|
485 | */
|
---|
486 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
487 |
|
---|
488 | pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL | PGM_SYNC_CLEAR_PGM_POOL;
|
---|
489 | VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
|
---|
490 | }
|
---|
491 | else
|
---|
492 | {
|
---|
493 | /* must be a hypervisor one then. */
|
---|
494 | pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->HyperVirtHandlers, GCPtr);
|
---|
495 | if (RT_UNLIKELY(!pCur))
|
---|
496 | {
|
---|
497 | pgmUnlock(pVM);
|
---|
498 | #ifndef DEBUG_sander
|
---|
499 | AssertMsgFailed(("Range %#x not found!\n", GCPtr));
|
---|
500 | #endif
|
---|
501 | return VERR_INVALID_PARAMETER;
|
---|
502 | }
|
---|
503 |
|
---|
504 | Log(("PGMHandlerVirtualDeregister: Removing Hyper Virtual (%d) Range %RGv-%RGv %s\n", pCur->enmType,
|
---|
505 | pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc));
|
---|
506 | Assert(pCur->enmType == PGMVIRTHANDLERTYPE_HYPERVISOR);
|
---|
507 | }
|
---|
508 |
|
---|
509 | pgmUnlock(pVM);
|
---|
510 |
|
---|
511 | STAM_DEREG(pVM, &pCur->Stat);
|
---|
512 | MMHyperFree(pVM, pCur);
|
---|
513 |
|
---|
514 | return VINF_SUCCESS;
|
---|
515 | }
|
---|
516 |
|
---|
517 |
|
---|
518 | /**
|
---|
519 | * Arguments for pgmR3InfoHandlersPhysicalOne and pgmR3InfoHandlersVirtualOne.
|
---|
520 | */
|
---|
521 | typedef struct PGMHANDLERINFOARG
|
---|
522 | {
|
---|
523 | /** The output helpers.*/
|
---|
524 | PCDBGFINFOHLP pHlp;
|
---|
525 | /** Set if statistics should be dumped. */
|
---|
526 | bool fStats;
|
---|
527 | } PGMHANDLERINFOARG, *PPGMHANDLERINFOARG;
|
---|
528 |
|
---|
529 |
|
---|
530 | /**
|
---|
531 | * Info callback for 'pgmhandlers'.
|
---|
532 | *
|
---|
533 | * @param pHlp The output helpers.
|
---|
534 | * @param pszArgs The arguments. phys or virt.
|
---|
535 | */
|
---|
536 | DECLCALLBACK(void) pgmR3InfoHandlers(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
537 | {
|
---|
538 | /*
|
---|
539 | * Test input.
|
---|
540 | */
|
---|
541 | PGMHANDLERINFOARG Args = { pHlp, /* .fStats = */ true };
|
---|
542 | bool fPhysical = !pszArgs || !*pszArgs;
|
---|
543 | bool fVirtual = fPhysical;
|
---|
544 | bool fHyper = fPhysical;
|
---|
545 | if (!fPhysical)
|
---|
546 | {
|
---|
547 | bool fAll = strstr(pszArgs, "all") != NULL;
|
---|
548 | fPhysical = fAll || strstr(pszArgs, "phys") != NULL;
|
---|
549 | fVirtual = fAll || strstr(pszArgs, "virt") != NULL;
|
---|
550 | fHyper = fAll || strstr(pszArgs, "hyper")!= NULL;
|
---|
551 | Args.fStats = strstr(pszArgs, "nost") == NULL;
|
---|
552 | }
|
---|
553 |
|
---|
554 | /*
|
---|
555 | * Dump the handlers.
|
---|
556 | */
|
---|
557 | if (fPhysical)
|
---|
558 | {
|
---|
559 | pHlp->pfnPrintf(pHlp,
|
---|
560 | "Physical handlers: (PhysHandlers=%d (%#x))\n"
|
---|
561 | "%*s %*s %*s %*s HandlerGC UserGC Type Description\n",
|
---|
562 | pVM->pgm.s.pTreesR3->PhysHandlers, pVM->pgm.s.pTreesR3->PhysHandlers,
|
---|
563 | - (int)sizeof(RTGCPHYS) * 2, "From",
|
---|
564 | - (int)sizeof(RTGCPHYS) * 2 - 3, "- To (incl)",
|
---|
565 | - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
|
---|
566 | - (int)sizeof(RTHCPTR) * 2 - 1, "UserHC");
|
---|
567 | RTAvlroGCPhysDoWithAll(&pVM->pgm.s.pTreesR3->PhysHandlers, true, pgmR3InfoHandlersPhysicalOne, &Args);
|
---|
568 | }
|
---|
569 |
|
---|
570 | if (fVirtual)
|
---|
571 | {
|
---|
572 | pHlp->pfnPrintf(pHlp,
|
---|
573 | "Virtual handlers:\n"
|
---|
574 | "%*s %*s %*s %*s Type Description\n",
|
---|
575 | - (int)sizeof(RTGCPTR) * 2, "From",
|
---|
576 | - (int)sizeof(RTGCPTR) * 2 - 3, "- To (excl)",
|
---|
577 | - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
|
---|
578 | - (int)sizeof(RTRCPTR) * 2 - 1, "HandlerGC");
|
---|
579 | RTAvlroGCPtrDoWithAll(&pVM->pgm.s.pTreesR3->VirtHandlers, true, pgmR3InfoHandlersVirtualOne, &Args);
|
---|
580 | }
|
---|
581 |
|
---|
582 | if (fHyper)
|
---|
583 | {
|
---|
584 | pHlp->pfnPrintf(pHlp,
|
---|
585 | "Hypervisor Virtual handlers:\n"
|
---|
586 | "%*s %*s %*s %*s Type Description\n",
|
---|
587 | - (int)sizeof(RTGCPTR) * 2, "From",
|
---|
588 | - (int)sizeof(RTGCPTR) * 2 - 3, "- To (excl)",
|
---|
589 | - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
|
---|
590 | - (int)sizeof(RTRCPTR) * 2 - 1, "HandlerGC");
|
---|
591 | RTAvlroGCPtrDoWithAll(&pVM->pgm.s.pTreesR3->HyperVirtHandlers, true, pgmR3InfoHandlersVirtualOne, &Args);
|
---|
592 | }
|
---|
593 | }
|
---|
594 |
|
---|
595 |
|
---|
596 | /**
|
---|
597 | * Displays one physical handler range.
|
---|
598 | *
|
---|
599 | * @returns 0
|
---|
600 | * @param pNode Pointer to a PGMPHYSHANDLER.
|
---|
601 | * @param pvUser Pointer to command helper functions.
|
---|
602 | */
|
---|
603 | static DECLCALLBACK(int) pgmR3InfoHandlersPhysicalOne(PAVLROGCPHYSNODECORE pNode, void *pvUser)
|
---|
604 | {
|
---|
605 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
|
---|
606 | PPGMHANDLERINFOARG pArgs= (PPGMHANDLERINFOARG)pvUser;
|
---|
607 | PCDBGFINFOHLP pHlp = pArgs->pHlp;
|
---|
608 | const char *pszType;
|
---|
609 | switch (pCur->enmType)
|
---|
610 | {
|
---|
611 | case PGMPHYSHANDLERTYPE_MMIO: pszType = "MMIO "; break;
|
---|
612 | case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE: pszType = "Write "; break;
|
---|
613 | case PGMPHYSHANDLERTYPE_PHYSICAL_ALL: pszType = "All "; break;
|
---|
614 | default: pszType = "????"; break;
|
---|
615 | }
|
---|
616 | pHlp->pfnPrintf(pHlp,
|
---|
617 | "%RGp - %RGp %RHv %RHv %RRv %RRv %s %s\n",
|
---|
618 | pCur->Core.Key, pCur->Core.KeyLast, pCur->pfnHandlerR3, pCur->pvUserR3, pCur->pfnHandlerRC, pCur->pvUserRC, pszType, pCur->pszDesc);
|
---|
619 | #ifdef VBOX_WITH_STATISTICS
|
---|
620 | if (pArgs->fStats)
|
---|
621 | pHlp->pfnPrintf(pHlp, " cPeriods: %9RU64 cTicks: %11RU64 Min: %11RU64 Avg: %11RU64 Max: %11RU64\n",
|
---|
622 | pCur->Stat.cPeriods, pCur->Stat.cTicks, pCur->Stat.cTicksMin,
|
---|
623 | pCur->Stat.cPeriods ? pCur->Stat.cTicks / pCur->Stat.cPeriods : 0, pCur->Stat.cTicksMax);
|
---|
624 | #endif
|
---|
625 | return 0;
|
---|
626 | }
|
---|
627 |
|
---|
628 |
|
---|
629 | /**
|
---|
630 | * Displays one virtual handler range.
|
---|
631 | *
|
---|
632 | * @returns 0
|
---|
633 | * @param pNode Pointer to a PGMVIRTHANDLER.
|
---|
634 | * @param pvUser Pointer to command helper functions.
|
---|
635 | */
|
---|
636 | static DECLCALLBACK(int) pgmR3InfoHandlersVirtualOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
|
---|
637 | {
|
---|
638 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
|
---|
639 | PPGMHANDLERINFOARG pArgs= (PPGMHANDLERINFOARG)pvUser;
|
---|
640 | PCDBGFINFOHLP pHlp = pArgs->pHlp;
|
---|
641 | const char *pszType;
|
---|
642 | switch (pCur->enmType)
|
---|
643 | {
|
---|
644 | case PGMVIRTHANDLERTYPE_WRITE: pszType = "Write "; break;
|
---|
645 | case PGMVIRTHANDLERTYPE_ALL: pszType = "All "; break;
|
---|
646 | case PGMVIRTHANDLERTYPE_HYPERVISOR: pszType = "WriteHyp "; break;
|
---|
647 | default: pszType = "????"; break;
|
---|
648 | }
|
---|
649 | pHlp->pfnPrintf(pHlp, "%RGv - %RGv %RHv %RRv %s %s\n",
|
---|
650 | pCur->Core.Key, pCur->Core.KeyLast, pCur->pfnHandlerR3, pCur->pfnHandlerRC, pszType, pCur->pszDesc);
|
---|
651 | #ifdef VBOX_WITH_STATISTICS
|
---|
652 | if (pArgs->fStats)
|
---|
653 | pHlp->pfnPrintf(pHlp, " cPeriods: %9RU64 cTicks: %11RU64 Min: %11RU64 Avg: %11RU64 Max: %11RU64\n",
|
---|
654 | pCur->Stat.cPeriods, pCur->Stat.cTicks, pCur->Stat.cTicksMin,
|
---|
655 | pCur->Stat.cPeriods ? pCur->Stat.cTicks / pCur->Stat.cPeriods : 0, pCur->Stat.cTicksMax);
|
---|
656 | #endif
|
---|
657 | return 0;
|
---|
658 | }
|
---|
659 |
|
---|