1 | /* $Id: PGMHandler.cpp 55493 2015-04-28 16:51:35Z 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 | /**
|
---|
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 | */
|
---|
81 | VMM_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 | */
|
---|
142 | VMMR3DECL(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 | */
|
---|
197 | void 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 | */
|
---|
219 | static 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 | */
|
---|
249 | static 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 access handler for a virtual range.
|
---|
276 | *
|
---|
277 | * @returns VBox status code.
|
---|
278 | * @param pVM Pointer to the VM.
|
---|
279 | * @param enmType Handler type. Any of the PGMVIRTHANDLERTYPE_* enums.
|
---|
280 | * @param GCPtr Start address.
|
---|
281 | * @param GCPtrLast Last address (inclusive).
|
---|
282 | * @param pfnInvalidateR3 The R3 invalidate callback (can be 0)
|
---|
283 | * @param pfnHandlerR3 The R3 handler.
|
---|
284 | * @param pszHandlerRC The RC handler symbol name.
|
---|
285 | * @param pszModRC The RC handler module.
|
---|
286 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
287 | */
|
---|
288 | VMMR3DECL(int) PGMR3HandlerVirtualRegister(PVM pVM, PGMVIRTHANDLERTYPE enmType, RTGCPTR GCPtr, RTGCPTR GCPtrLast,
|
---|
289 | PFNPGMR3VIRTINVALIDATE pfnInvalidateR3,
|
---|
290 | PFNPGMR3VIRTHANDLER pfnHandlerR3,
|
---|
291 | const char *pszHandlerRC, const char *pszModRC,
|
---|
292 | const char *pszDesc)
|
---|
293 | {
|
---|
294 | LogFlow(("PGMR3HandlerVirtualRegisterEx: enmType=%d GCPtr=%RGv GCPtrLast=%RGv pszHandlerRC=%p:{%s} pszModRC=%p:{%s} pszDesc=%s\n",
|
---|
295 | enmType, GCPtr, GCPtrLast, pszHandlerRC, pszHandlerRC, pszModRC, pszModRC, pszDesc));
|
---|
296 |
|
---|
297 | /* Not supported/relevant for VT-x and AMD-V. */
|
---|
298 | if (HMIsEnabled(pVM))
|
---|
299 | return VERR_NOT_IMPLEMENTED;
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Validate input.
|
---|
303 | */
|
---|
304 | if (!pszModRC)
|
---|
305 | pszModRC = VMMGC_MAIN_MODULE_NAME;
|
---|
306 | if (!pszModRC || !*pszModRC || !pszHandlerRC || !*pszHandlerRC)
|
---|
307 | {
|
---|
308 | AssertMsgFailed(("pfnHandlerGC or/and pszModRC is missing\n"));
|
---|
309 | return VERR_INVALID_PARAMETER;
|
---|
310 | }
|
---|
311 |
|
---|
312 | /*
|
---|
313 | * Resolve the GC handler.
|
---|
314 | */
|
---|
315 | RTRCPTR pfnHandlerRC;
|
---|
316 | int rc = PDMR3LdrGetSymbolRCLazy(pVM, pszModRC, NULL /*pszSearchPath*/, pszHandlerRC, &pfnHandlerRC);
|
---|
317 | if (RT_SUCCESS(rc))
|
---|
318 | return PGMR3HandlerVirtualRegisterEx(pVM, enmType, GCPtr, GCPtrLast, pfnInvalidateR3,
|
---|
319 | pfnHandlerR3, pfnHandlerRC, pszDesc);
|
---|
320 |
|
---|
321 | AssertMsgFailed(("Failed to resolve %s.%s, rc=%Rrc.\n", pszModRC, pszHandlerRC, rc));
|
---|
322 | return rc;
|
---|
323 | }
|
---|
324 |
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Register an access handler for a virtual range.
|
---|
328 | *
|
---|
329 | * @returns VBox status code.
|
---|
330 | * @param pVM Pointer to the VM.
|
---|
331 | * @param enmType Handler type. Any of the PGMVIRTHANDLERTYPE_* enums.
|
---|
332 | * @param GCPtr Start address.
|
---|
333 | * @param GCPtrLast Last address (inclusive).
|
---|
334 | * @param pfnInvalidateR3 The R3 invalidate callback (can be 0)
|
---|
335 | * @param pfnHandlerR3 The R3 handler.
|
---|
336 | * @param pfnHandlerRC The RC handler.
|
---|
337 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
338 | * @thread EMT
|
---|
339 | */
|
---|
340 | /** @todo create a template for virtual handlers (see async i/o), we're wasting space
|
---|
341 | * duplicating the function pointers now. (Or we will once we add the missing callbacks.) */
|
---|
342 | VMMDECL(int) PGMR3HandlerVirtualRegisterEx(PVM pVM, PGMVIRTHANDLERTYPE enmType, RTGCPTR GCPtr, RTGCPTR GCPtrLast,
|
---|
343 | R3PTRTYPE(PFNPGMR3VIRTINVALIDATE) pfnInvalidateR3,
|
---|
344 | R3PTRTYPE(PFNPGMR3VIRTHANDLER) pfnHandlerR3,
|
---|
345 | RCPTRTYPE(PFNPGMRCVIRTHANDLER) pfnHandlerRC,
|
---|
346 | R3PTRTYPE(const char *) pszDesc)
|
---|
347 | {
|
---|
348 | Log(("PGMR3HandlerVirtualRegister: enmType=%d GCPtr=%RGv GCPtrLast=%RGv pfnInvalidateR3=%RHv pfnHandlerR3=%RHv pfnHandlerRC=%RRv pszDesc=%s\n",
|
---|
349 | enmType, GCPtr, GCPtrLast, pfnInvalidateR3, pfnHandlerR3, pfnHandlerRC, pszDesc));
|
---|
350 |
|
---|
351 | /* Not supported/relevant for VT-x and AMD-V. */
|
---|
352 | if (HMIsEnabled(pVM))
|
---|
353 | return VERR_NOT_IMPLEMENTED;
|
---|
354 |
|
---|
355 | /*
|
---|
356 | * Validate input.
|
---|
357 | */
|
---|
358 | switch (enmType)
|
---|
359 | {
|
---|
360 | case PGMVIRTHANDLERTYPE_ALL:
|
---|
361 | AssertReleaseMsgReturn( (GCPtr & PAGE_OFFSET_MASK) == 0
|
---|
362 | && (GCPtrLast & PAGE_OFFSET_MASK) == PAGE_OFFSET_MASK,
|
---|
363 | ("PGMVIRTHANDLERTYPE_ALL: GCPtr=%RGv GCPtrLast=%RGv\n", GCPtr, GCPtrLast),
|
---|
364 | VERR_NOT_IMPLEMENTED);
|
---|
365 | break;
|
---|
366 | case PGMVIRTHANDLERTYPE_WRITE:
|
---|
367 | if (!pfnHandlerR3)
|
---|
368 | {
|
---|
369 | AssertMsgFailed(("No HC handler specified!!\n"));
|
---|
370 | return VERR_INVALID_PARAMETER;
|
---|
371 | }
|
---|
372 | break;
|
---|
373 |
|
---|
374 | case PGMVIRTHANDLERTYPE_HYPERVISOR:
|
---|
375 | if (pfnHandlerR3)
|
---|
376 | {
|
---|
377 | AssertMsgFailed(("R3 handler specified for hypervisor range!?!\n"));
|
---|
378 | return VERR_INVALID_PARAMETER;
|
---|
379 | }
|
---|
380 | break;
|
---|
381 | default:
|
---|
382 | AssertMsgFailed(("Invalid enmType! enmType=%d\n", enmType));
|
---|
383 | return VERR_INVALID_PARAMETER;
|
---|
384 | }
|
---|
385 | if (GCPtrLast < GCPtr)
|
---|
386 | {
|
---|
387 | AssertMsgFailed(("GCPtrLast < GCPtr (%#x < %#x)\n", GCPtrLast, GCPtr));
|
---|
388 | return VERR_INVALID_PARAMETER;
|
---|
389 | }
|
---|
390 | if (!pfnHandlerRC)
|
---|
391 | {
|
---|
392 | AssertMsgFailed(("pfnHandlerRC is missing\n"));
|
---|
393 | return VERR_INVALID_PARAMETER;
|
---|
394 | }
|
---|
395 |
|
---|
396 | /*
|
---|
397 | * Allocate and initialize a new entry.
|
---|
398 | */
|
---|
399 | unsigned cPages = (RT_ALIGN(GCPtrLast + 1, PAGE_SIZE) - (GCPtr & PAGE_BASE_GC_MASK)) >> PAGE_SHIFT;
|
---|
400 | PPGMVIRTHANDLER pNew;
|
---|
401 | int rc = MMHyperAlloc(pVM, RT_OFFSETOF(PGMVIRTHANDLER, aPhysToVirt[cPages]), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew); /** @todo r=bird: incorrect member name PhysToVirt? */
|
---|
402 | if (RT_FAILURE(rc))
|
---|
403 | return rc;
|
---|
404 |
|
---|
405 | pNew->Core.Key = GCPtr;
|
---|
406 | pNew->Core.KeyLast = GCPtrLast;
|
---|
407 |
|
---|
408 | pNew->enmType = enmType;
|
---|
409 | pNew->pfnInvalidateR3 = pfnInvalidateR3;
|
---|
410 | pNew->pfnHandlerRC = pfnHandlerRC;
|
---|
411 | pNew->pfnHandlerR3 = pfnHandlerR3;
|
---|
412 | pNew->pszDesc = pszDesc;
|
---|
413 | pNew->cb = GCPtrLast - GCPtr + 1;
|
---|
414 | pNew->cPages = cPages;
|
---|
415 | /* Will be synced at next guest execution attempt. */
|
---|
416 | while (cPages-- > 0)
|
---|
417 | {
|
---|
418 | pNew->aPhysToVirt[cPages].Core.Key = NIL_RTGCPHYS;
|
---|
419 | pNew->aPhysToVirt[cPages].Core.KeyLast = NIL_RTGCPHYS;
|
---|
420 | pNew->aPhysToVirt[cPages].offVirtHandler = -RT_OFFSETOF(PGMVIRTHANDLER, aPhysToVirt[cPages]);
|
---|
421 | pNew->aPhysToVirt[cPages].offNextAlias = 0;
|
---|
422 | }
|
---|
423 |
|
---|
424 | /*
|
---|
425 | * Try to insert it into the tree.
|
---|
426 | *
|
---|
427 | * The current implementation doesn't allow multiple handlers for
|
---|
428 | * the same range this makes everything much simpler and faster.
|
---|
429 | */
|
---|
430 | AVLROGCPTRTREE *pRoot = enmType != PGMVIRTHANDLERTYPE_HYPERVISOR
|
---|
431 | ? &pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers
|
---|
432 | : &pVM->pgm.s.CTX_SUFF(pTrees)->HyperVirtHandlers;
|
---|
433 | pgmLock(pVM);
|
---|
434 | if (*pRoot != 0)
|
---|
435 | {
|
---|
436 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGetBestFit(pRoot, pNew->Core.Key, true);
|
---|
437 | if ( !pCur
|
---|
438 | || GCPtr > pCur->Core.KeyLast
|
---|
439 | || GCPtrLast < pCur->Core.Key)
|
---|
440 | pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGetBestFit(pRoot, pNew->Core.Key, false);
|
---|
441 | if ( pCur
|
---|
442 | && GCPtr <= pCur->Core.KeyLast
|
---|
443 | && GCPtrLast >= pCur->Core.Key)
|
---|
444 | {
|
---|
445 | /*
|
---|
446 | * The LDT sometimes conflicts with the IDT and LDT ranges while being
|
---|
447 | * updated on linux. So, we don't assert simply log it.
|
---|
448 | */
|
---|
449 | Log(("PGMR3HandlerVirtualRegister: Conflict with existing range %RGv-%RGv (%s), req. %RGv-%RGv (%s)\n",
|
---|
450 | pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc, GCPtr, GCPtrLast, pszDesc));
|
---|
451 | MMHyperFree(pVM, pNew);
|
---|
452 | pgmUnlock(pVM);
|
---|
453 | return VERR_PGM_HANDLER_VIRTUAL_CONFLICT;
|
---|
454 | }
|
---|
455 | }
|
---|
456 | if (RTAvlroGCPtrInsert(pRoot, &pNew->Core))
|
---|
457 | {
|
---|
458 | if (enmType != PGMVIRTHANDLERTYPE_HYPERVISOR)
|
---|
459 | {
|
---|
460 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
461 |
|
---|
462 | pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL | PGM_SYNC_CLEAR_PGM_POOL;
|
---|
463 | VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
|
---|
464 | }
|
---|
465 | pgmUnlock(pVM);
|
---|
466 |
|
---|
467 | #ifdef VBOX_WITH_STATISTICS
|
---|
468 | rc = STAMR3RegisterF(pVM, &pNew->Stat, STAMTYPE_PROFILE, STAMVISIBILITY_USED, STAMUNIT_TICKS_PER_CALL, pszDesc,
|
---|
469 | "/PGM/VirtHandler/Calls/%RGv-%RGv", pNew->Core.Key, pNew->Core.KeyLast);
|
---|
470 | AssertRC(rc);
|
---|
471 | #endif
|
---|
472 | return VINF_SUCCESS;
|
---|
473 | }
|
---|
474 |
|
---|
475 | pgmUnlock(pVM);
|
---|
476 | AssertFailed();
|
---|
477 | MMHyperFree(pVM, pNew);
|
---|
478 | return VERR_PGM_HANDLER_VIRTUAL_CONFLICT;
|
---|
479 | }
|
---|
480 |
|
---|
481 |
|
---|
482 | /**
|
---|
483 | * Modify the page invalidation callback handler for a registered virtual range.
|
---|
484 | * (add more when needed)
|
---|
485 | *
|
---|
486 | * @returns VBox status code.
|
---|
487 | * @param pVM Pointer to the VM.
|
---|
488 | * @param GCPtr Start address.
|
---|
489 | * @param pfnInvalidateR3 The R3 invalidate callback (can be 0)
|
---|
490 | * @remarks Doesn't work with the hypervisor access handler type.
|
---|
491 | */
|
---|
492 | VMMDECL(int) PGMHandlerVirtualChangeInvalidateCallback(PVM pVM, RTGCPTR GCPtr, PFNPGMR3VIRTINVALIDATE pfnInvalidateR3)
|
---|
493 | {
|
---|
494 | pgmLock(pVM);
|
---|
495 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrGet(&pVM->pgm.s.pTreesR3->VirtHandlers, GCPtr);
|
---|
496 | if (pCur)
|
---|
497 | {
|
---|
498 | pCur->pfnInvalidateR3 = pfnInvalidateR3;
|
---|
499 | pgmUnlock(pVM);
|
---|
500 | return VINF_SUCCESS;
|
---|
501 | }
|
---|
502 | pgmUnlock(pVM);
|
---|
503 | AssertMsgFailed(("Range %#x not found!\n", GCPtr));
|
---|
504 | return VERR_INVALID_PARAMETER;
|
---|
505 | }
|
---|
506 |
|
---|
507 | /**
|
---|
508 | * Deregister an access handler for a virtual range.
|
---|
509 | *
|
---|
510 | * @returns VBox status code.
|
---|
511 | * @param pVM Pointer to the VM.
|
---|
512 | * @param GCPtr Start address.
|
---|
513 | * @thread EMT
|
---|
514 | */
|
---|
515 | VMMDECL(int) PGMHandlerVirtualDeregister(PVM pVM, RTGCPTR GCPtr)
|
---|
516 | {
|
---|
517 | pgmLock(pVM);
|
---|
518 |
|
---|
519 | /*
|
---|
520 | * Find the handler.
|
---|
521 | * We naturally assume GCPtr is a unique specification.
|
---|
522 | */
|
---|
523 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->VirtHandlers, GCPtr);
|
---|
524 | if (RT_LIKELY(pCur))
|
---|
525 | {
|
---|
526 | Log(("PGMHandlerVirtualDeregister: Removing Virtual (%d) Range %RGv-%RGv %s\n", pCur->enmType,
|
---|
527 | pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc));
|
---|
528 | Assert(pCur->enmType != PGMVIRTHANDLERTYPE_HYPERVISOR);
|
---|
529 |
|
---|
530 | /*
|
---|
531 | * Reset the flags and remove phys2virt nodes.
|
---|
532 | */
|
---|
533 | for (uint32_t iPage = 0; iPage < pCur->cPages; iPage++)
|
---|
534 | if (pCur->aPhysToVirt[iPage].offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE)
|
---|
535 | pgmHandlerVirtualClearPage(pVM, pCur, iPage);
|
---|
536 |
|
---|
537 | /*
|
---|
538 | * Schedule CR3 sync.
|
---|
539 | */
|
---|
540 | PVMCPU pVCpu = VMMGetCpu(pVM);
|
---|
541 |
|
---|
542 | pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL | PGM_SYNC_CLEAR_PGM_POOL;
|
---|
543 | VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
|
---|
544 | }
|
---|
545 | else
|
---|
546 | {
|
---|
547 | /* must be a hypervisor one then. */
|
---|
548 | pCur = (PPGMVIRTHANDLER)RTAvlroGCPtrRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->HyperVirtHandlers, GCPtr);
|
---|
549 | if (RT_UNLIKELY(!pCur))
|
---|
550 | {
|
---|
551 | pgmUnlock(pVM);
|
---|
552 | #ifndef DEBUG_sander
|
---|
553 | AssertMsgFailed(("Range %#x not found!\n", GCPtr));
|
---|
554 | #endif
|
---|
555 | return VERR_INVALID_PARAMETER;
|
---|
556 | }
|
---|
557 |
|
---|
558 | Log(("PGMHandlerVirtualDeregister: Removing Hyper Virtual (%d) Range %RGv-%RGv %s\n", pCur->enmType,
|
---|
559 | pCur->Core.Key, pCur->Core.KeyLast, pCur->pszDesc));
|
---|
560 | Assert(pCur->enmType == PGMVIRTHANDLERTYPE_HYPERVISOR);
|
---|
561 | }
|
---|
562 |
|
---|
563 | pgmUnlock(pVM);
|
---|
564 |
|
---|
565 | #ifdef VBOX_WITH_STATISTICS
|
---|
566 | STAMR3DeregisterF(pVM->pUVM, "/PGM/VirtHandler/Calls/%RGv-%RGv", pCur->Core.Key, pCur->Core.KeyLast);
|
---|
567 | #endif
|
---|
568 | MMHyperFree(pVM, pCur);
|
---|
569 |
|
---|
570 | return VINF_SUCCESS;
|
---|
571 | }
|
---|
572 |
|
---|
573 |
|
---|
574 | /**
|
---|
575 | * Arguments for pgmR3InfoHandlersPhysicalOne and pgmR3InfoHandlersVirtualOne.
|
---|
576 | */
|
---|
577 | typedef struct PGMHANDLERINFOARG
|
---|
578 | {
|
---|
579 | /** The output helpers.*/
|
---|
580 | PCDBGFINFOHLP pHlp;
|
---|
581 | /** Pointer to the cross context VM handle. */
|
---|
582 | PVM pVM;
|
---|
583 | /** Set if statistics should be dumped. */
|
---|
584 | bool fStats;
|
---|
585 | } PGMHANDLERINFOARG, *PPGMHANDLERINFOARG;
|
---|
586 |
|
---|
587 |
|
---|
588 | /**
|
---|
589 | * Info callback for 'pgmhandlers'.
|
---|
590 | *
|
---|
591 | * @param pHlp The output helpers.
|
---|
592 | * @param pszArgs The arguments. phys or virt.
|
---|
593 | */
|
---|
594 | DECLCALLBACK(void) pgmR3InfoHandlers(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs)
|
---|
595 | {
|
---|
596 | /*
|
---|
597 | * Test input.
|
---|
598 | */
|
---|
599 | PGMHANDLERINFOARG Args = { pHlp, pVM, /* .fStats = */ true };
|
---|
600 | bool fPhysical = !pszArgs || !*pszArgs;
|
---|
601 | bool fVirtual = fPhysical;
|
---|
602 | bool fHyper = fPhysical;
|
---|
603 | if (!fPhysical)
|
---|
604 | {
|
---|
605 | bool fAll = strstr(pszArgs, "all") != NULL;
|
---|
606 | fPhysical = fAll || strstr(pszArgs, "phys") != NULL;
|
---|
607 | fVirtual = fAll || strstr(pszArgs, "virt") != NULL;
|
---|
608 | fHyper = fAll || strstr(pszArgs, "hyper")!= NULL;
|
---|
609 | Args.fStats = strstr(pszArgs, "nost") == NULL;
|
---|
610 | }
|
---|
611 |
|
---|
612 | /*
|
---|
613 | * Dump the handlers.
|
---|
614 | */
|
---|
615 | if (fPhysical)
|
---|
616 | {
|
---|
617 | pHlp->pfnPrintf(pHlp,
|
---|
618 | "Physical handlers: (PhysHandlers=%d (%#x))\n"
|
---|
619 | "%*s %*s %*s %*s HandlerGC UserGC Type Description\n",
|
---|
620 | pVM->pgm.s.pTreesR3->PhysHandlers, pVM->pgm.s.pTreesR3->PhysHandlers,
|
---|
621 | - (int)sizeof(RTGCPHYS) * 2, "From",
|
---|
622 | - (int)sizeof(RTGCPHYS) * 2 - 3, "- To (incl)",
|
---|
623 | - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
|
---|
624 | - (int)sizeof(RTHCPTR) * 2 - 1, "UserHC");
|
---|
625 | RTAvlroGCPhysDoWithAll(&pVM->pgm.s.pTreesR3->PhysHandlers, true, pgmR3InfoHandlersPhysicalOne, &Args);
|
---|
626 | }
|
---|
627 |
|
---|
628 | if (fVirtual)
|
---|
629 | {
|
---|
630 | pHlp->pfnPrintf(pHlp,
|
---|
631 | "Virtual handlers:\n"
|
---|
632 | "%*s %*s %*s %*s Type Description\n",
|
---|
633 | - (int)sizeof(RTGCPTR) * 2, "From",
|
---|
634 | - (int)sizeof(RTGCPTR) * 2 - 3, "- To (excl)",
|
---|
635 | - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
|
---|
636 | - (int)sizeof(RTRCPTR) * 2 - 1, "HandlerGC");
|
---|
637 | RTAvlroGCPtrDoWithAll(&pVM->pgm.s.pTreesR3->VirtHandlers, true, pgmR3InfoHandlersVirtualOne, &Args);
|
---|
638 | }
|
---|
639 |
|
---|
640 | if (fHyper)
|
---|
641 | {
|
---|
642 | pHlp->pfnPrintf(pHlp,
|
---|
643 | "Hypervisor Virtual handlers:\n"
|
---|
644 | "%*s %*s %*s %*s Type Description\n",
|
---|
645 | - (int)sizeof(RTGCPTR) * 2, "From",
|
---|
646 | - (int)sizeof(RTGCPTR) * 2 - 3, "- To (excl)",
|
---|
647 | - (int)sizeof(RTHCPTR) * 2 - 1, "HandlerHC",
|
---|
648 | - (int)sizeof(RTRCPTR) * 2 - 1, "HandlerGC");
|
---|
649 | RTAvlroGCPtrDoWithAll(&pVM->pgm.s.pTreesR3->HyperVirtHandlers, true, pgmR3InfoHandlersVirtualOne, &Args);
|
---|
650 | }
|
---|
651 | }
|
---|
652 |
|
---|
653 |
|
---|
654 | /**
|
---|
655 | * Displays one physical handler range.
|
---|
656 | *
|
---|
657 | * @returns 0
|
---|
658 | * @param pNode Pointer to a PGMPHYSHANDLER.
|
---|
659 | * @param pvUser Pointer to command helper functions.
|
---|
660 | */
|
---|
661 | static DECLCALLBACK(int) pgmR3InfoHandlersPhysicalOne(PAVLROGCPHYSNODECORE pNode, void *pvUser)
|
---|
662 | {
|
---|
663 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)pNode;
|
---|
664 | PPGMHANDLERINFOARG pArgs = (PPGMHANDLERINFOARG)pvUser;
|
---|
665 | PCDBGFINFOHLP pHlp = pArgs->pHlp;
|
---|
666 | PPGMPHYSHANDLERTYPEINT pCurType = PGMPHYSHANDLER_GET_TYPE(pArgs->pVM, pCur);
|
---|
667 | const char *pszType;
|
---|
668 | switch (pCurType->enmKind)
|
---|
669 | {
|
---|
670 | case PGMPHYSHANDLERKIND_MMIO: pszType = "MMIO "; break;
|
---|
671 | case PGMPHYSHANDLERKIND_WRITE: pszType = "Write "; break;
|
---|
672 | case PGMPHYSHANDLERKIND_ALL: pszType = "All "; break;
|
---|
673 | default: pszType = "????"; break;
|
---|
674 | }
|
---|
675 | pHlp->pfnPrintf(pHlp,
|
---|
676 | "%RGp - %RGp %RHv %RHv %RRv %RRv %s %s\n",
|
---|
677 | pCur->Core.Key, pCur->Core.KeyLast, pCurType->pfnHandlerR3, pCur->pvUserR3, pCurType->pfnHandlerRC, pCur->pvUserRC,
|
---|
678 | pszType, pCur->pszDesc);
|
---|
679 | #ifdef VBOX_WITH_STATISTICS
|
---|
680 | if (pArgs->fStats)
|
---|
681 | pHlp->pfnPrintf(pHlp, " cPeriods: %9RU64 cTicks: %11RU64 Min: %11RU64 Avg: %11RU64 Max: %11RU64\n",
|
---|
682 | pCur->Stat.cPeriods, pCur->Stat.cTicks, pCur->Stat.cTicksMin,
|
---|
683 | pCur->Stat.cPeriods ? pCur->Stat.cTicks / pCur->Stat.cPeriods : 0, pCur->Stat.cTicksMax);
|
---|
684 | #endif
|
---|
685 | return 0;
|
---|
686 | }
|
---|
687 |
|
---|
688 |
|
---|
689 | /**
|
---|
690 | * Displays one virtual handler range.
|
---|
691 | *
|
---|
692 | * @returns 0
|
---|
693 | * @param pNode Pointer to a PGMVIRTHANDLER.
|
---|
694 | * @param pvUser Pointer to command helper functions.
|
---|
695 | */
|
---|
696 | static DECLCALLBACK(int) pgmR3InfoHandlersVirtualOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
|
---|
697 | {
|
---|
698 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
|
---|
699 | PPGMHANDLERINFOARG pArgs= (PPGMHANDLERINFOARG)pvUser;
|
---|
700 | PCDBGFINFOHLP pHlp = pArgs->pHlp;
|
---|
701 | const char *pszType;
|
---|
702 | switch (pCur->enmType)
|
---|
703 | {
|
---|
704 | case PGMVIRTHANDLERTYPE_WRITE: pszType = "Write "; break;
|
---|
705 | case PGMVIRTHANDLERTYPE_ALL: pszType = "All "; break;
|
---|
706 | case PGMVIRTHANDLERTYPE_HYPERVISOR: pszType = "WriteHyp "; break;
|
---|
707 | default: pszType = "????"; break;
|
---|
708 | }
|
---|
709 | pHlp->pfnPrintf(pHlp, "%RGv - %RGv %RHv %RRv %s %s\n",
|
---|
710 | pCur->Core.Key, pCur->Core.KeyLast, pCur->pfnHandlerR3, pCur->pfnHandlerRC, pszType, pCur->pszDesc);
|
---|
711 | #ifdef VBOX_WITH_STATISTICS
|
---|
712 | if (pArgs->fStats)
|
---|
713 | pHlp->pfnPrintf(pHlp, " cPeriods: %9RU64 cTicks: %11RU64 Min: %11RU64 Avg: %11RU64 Max: %11RU64\n",
|
---|
714 | pCur->Stat.cPeriods, pCur->Stat.cTicks, pCur->Stat.cTicksMin,
|
---|
715 | pCur->Stat.cPeriods ? pCur->Stat.cTicks / pCur->Stat.cPeriods : 0, pCur->Stat.cTicksMax);
|
---|
716 | #endif
|
---|
717 | return 0;
|
---|
718 | }
|
---|
719 |
|
---|