1 | /* $Id: PGMAllHandler.cpp 6829 2008-02-06 14:06:30Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PGM - Page Manager / Monitor, Access Handlers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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/dbgf.h>
|
---|
24 | #include <VBox/pgm.h>
|
---|
25 | #include <VBox/iom.h>
|
---|
26 | #include <VBox/mm.h>
|
---|
27 | #include <VBox/em.h>
|
---|
28 | #include <VBox/stam.h>
|
---|
29 | #include <VBox/rem.h>
|
---|
30 | #include <VBox/dbgf.h>
|
---|
31 | #include <VBox/rem.h>
|
---|
32 | #include "PGMInternal.h"
|
---|
33 | #include <VBox/vm.h>
|
---|
34 |
|
---|
35 | #include <VBox/log.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/asm.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <VBox/param.h>
|
---|
40 | #include <VBox/err.h>
|
---|
41 | #include <VBox/selm.h>
|
---|
42 |
|
---|
43 |
|
---|
44 | /*******************************************************************************
|
---|
45 | * Internal Functions *
|
---|
46 | *******************************************************************************/
|
---|
47 | static int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVM pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam);
|
---|
48 | static void pgmHandlerPhysicalDeregisterNotifyREM(PVM pVM, PPGMPHYSHANDLER pCur);
|
---|
49 | static void pgmHandlerPhysicalResetRamFlags(PVM pVM, PPGMPHYSHANDLER pCur);
|
---|
50 |
|
---|
51 |
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Register a access handler for a physical range.
|
---|
55 | *
|
---|
56 | * @returns VBox status code.
|
---|
57 | * @retval VINF_SUCCESS when successfully installed.
|
---|
58 | * @retval VINF_PGM_GCPHYS_ALIASED when the shadow PTs could be updated because
|
---|
59 | * the guest page aliased or/and mapped by multiple PTs. A CR3 sync has been
|
---|
60 | * flagged together with a pool clearing.
|
---|
61 | * @retval VERR_PGM_HANDLER_PHYSICAL_CONFLICT if the range conflicts with an existing
|
---|
62 | * one. A debug assertion is raised.
|
---|
63 | *
|
---|
64 | * @param pVM VM Handle.
|
---|
65 | * @param enmType Handler type. Any of the PGMPHYSHANDLERTYPE_PHYSICAL* enums.
|
---|
66 | * @param GCPhys Start physical address.
|
---|
67 | * @param GCPhysLast Last physical address. (inclusive)
|
---|
68 | * @param pfnHandlerR3 The R3 handler.
|
---|
69 | * @param pvUserR3 User argument to the R3 handler.
|
---|
70 | * @param pfnHandlerR0 The R0 handler.
|
---|
71 | * @param pvUserR0 User argument to the R0 handler.
|
---|
72 | * @param pfnHandlerGC The GC handler.
|
---|
73 | * @param pvUserGC User argument to the GC handler.
|
---|
74 | * This must be a GC pointer because it will be relocated!
|
---|
75 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
76 | */
|
---|
77 | PGMDECL(int) PGMHandlerPhysicalRegisterEx(PVM pVM, PGMPHYSHANDLERTYPE enmType, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast,
|
---|
78 | R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3, RTR3PTR pvUserR3,
|
---|
79 | R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0, RTR0PTR pvUserR0,
|
---|
80 | GCPTRTYPE(PFNPGMGCPHYSHANDLER) pfnHandlerGC, RTGCPTR pvUserGC,
|
---|
81 | R3PTRTYPE(const char *) pszDesc)
|
---|
82 | {
|
---|
83 | Log(("PGMHandlerPhysicalRegisterEx: enmType=%d GCPhys=%VGp GCPhysLast=%VGp pfnHandlerR3=%VHv pvUserR3=%VHv pfnHandlerR0=%VHv pvUserR0=%VHv pfnHandlerGC=%VGv pvUserGC=%VGv pszDesc=%s\n",
|
---|
84 | enmType, GCPhys, GCPhysLast, pfnHandlerR3, pvUserR3, pfnHandlerR0, pvUserR0, pfnHandlerGC, pvUserGC, HCSTRING(pszDesc)));
|
---|
85 |
|
---|
86 | /*
|
---|
87 | * Validate input.
|
---|
88 | */
|
---|
89 | if (GCPhys >= GCPhysLast)
|
---|
90 | {
|
---|
91 | AssertMsgFailed(("GCPhys >= GCPhysLast (%#x >= %#x)\n", GCPhys, GCPhysLast));
|
---|
92 | return VERR_INVALID_PARAMETER;
|
---|
93 | }
|
---|
94 | switch (enmType)
|
---|
95 | {
|
---|
96 | case PGMPHYSHANDLERTYPE_MMIO:
|
---|
97 | case PGMPHYSHANDLERTYPE_PHYSICAL:
|
---|
98 | case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE:
|
---|
99 | case PGMPHYSHANDLERTYPE_PHYSICAL_ALL:
|
---|
100 | break;
|
---|
101 | default:
|
---|
102 | AssertMsgFailed(("Invalid input enmType=%d!\n", enmType));
|
---|
103 | return VERR_INVALID_PARAMETER;
|
---|
104 | }
|
---|
105 | if ( (RTGCUINTPTR)pvUserGC >= 0x10000
|
---|
106 | && MMHyperHC2GC(pVM, MMHyperGC2HC(pVM, pvUserGC)) != pvUserGC)
|
---|
107 | {
|
---|
108 | AssertMsgFailed(("Not GC pointer! pvUserGC=%VGv\n", pvUserGC));
|
---|
109 | return VERR_INVALID_PARAMETER;
|
---|
110 | }
|
---|
111 | AssertReturn(pfnHandlerR3 || pfnHandlerR0 || pfnHandlerGC, VERR_INVALID_PARAMETER);
|
---|
112 |
|
---|
113 | /*
|
---|
114 | * We require the range to be within registered ram.
|
---|
115 | * There is no apparent need to support ranges which cover more than one ram range.
|
---|
116 | */
|
---|
117 | PPGMRAMRANGE pRam = CTXALLSUFF(pVM->pgm.s.pRamRanges);
|
---|
118 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
119 | pRam = CTXALLSUFF(pRam->pNext);
|
---|
120 | if ( !pRam
|
---|
121 | || GCPhysLast < pRam->GCPhys
|
---|
122 | || GCPhys > pRam->GCPhysLast)
|
---|
123 | {
|
---|
124 | #ifdef IN_RING3
|
---|
125 | /*
|
---|
126 | * If this is an MMIO registration, we'll just add a range for it.
|
---|
127 | */
|
---|
128 | if ( enmType == PGMPHYSHANDLERTYPE_MMIO
|
---|
129 | && ( !pRam
|
---|
130 | || GCPhysLast < pRam->GCPhys)
|
---|
131 | )
|
---|
132 | {
|
---|
133 | size_t cb = GCPhysLast - GCPhys + 1;
|
---|
134 | Assert(cb == RT_ALIGN_Z(cb, PAGE_SIZE));
|
---|
135 | int rc = PGMR3PhysRegister(pVM, NULL, GCPhys, cb, MM_RAM_FLAGS_RESERVED | MM_RAM_FLAGS_MMIO, NULL, pszDesc);
|
---|
136 | if (VBOX_FAILURE(rc))
|
---|
137 | return rc;
|
---|
138 |
|
---|
139 | /* search again. */
|
---|
140 | pRam = CTXALLSUFF(pVM->pgm.s.pRamRanges);
|
---|
141 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
142 | pRam = CTXALLSUFF(pRam->pNext);
|
---|
143 | }
|
---|
144 |
|
---|
145 | if ( !pRam
|
---|
146 | || GCPhysLast < pRam->GCPhys
|
---|
147 | || GCPhys > pRam->GCPhysLast)
|
---|
148 | #endif /* IN_RING3 */
|
---|
149 | {
|
---|
150 | #ifdef IN_RING3
|
---|
151 | DBGFR3Info(pVM, "phys", NULL, NULL);
|
---|
152 | #endif
|
---|
153 | AssertMsgFailed(("No RAM range for %VGp-%VGp\n", GCPhys, GCPhysLast));
|
---|
154 | return VERR_PGM_HANDLER_PHYSICAL_NO_RAM_RANGE;
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * Allocate and initialize the new entry.
|
---|
160 | */
|
---|
161 | PPGMPHYSHANDLER pNew;
|
---|
162 | int rc = MMHyperAlloc(pVM, sizeof(*pNew), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew);
|
---|
163 | if (VBOX_FAILURE(rc))
|
---|
164 | return rc;
|
---|
165 |
|
---|
166 | pNew->Core.Key = GCPhys;
|
---|
167 | pNew->Core.KeyLast = GCPhysLast;
|
---|
168 | pNew->enmType = enmType;
|
---|
169 | pNew->cPages = (GCPhysLast - (GCPhys & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
170 | pNew->pfnHandlerR3 = pfnHandlerR3;
|
---|
171 | pNew->pvUserR3 = pvUserR3;
|
---|
172 | pNew->pfnHandlerR0 = pfnHandlerR0;
|
---|
173 | pNew->pvUserR0 = pvUserR0;
|
---|
174 | pNew->pfnHandlerGC = pfnHandlerGC;
|
---|
175 | pNew->pvUserGC = pvUserGC;
|
---|
176 | pNew->pszDesc = pszDesc;
|
---|
177 |
|
---|
178 | pgmLock(pVM);
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Try insert into list.
|
---|
182 | */
|
---|
183 | if (RTAvlroGCPhysInsert(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, &pNew->Core))
|
---|
184 | {
|
---|
185 | rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pNew, pRam);
|
---|
186 | if (rc == VINF_PGM_GCPHYS_ALIASED)
|
---|
187 | {
|
---|
188 | pVM->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
|
---|
189 | VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
|
---|
190 | }
|
---|
191 | pVM->pgm.s.fPhysCacheFlushPending = true;
|
---|
192 | #ifndef IN_RING3
|
---|
193 | REMNotifyHandlerPhysicalRegister(pVM, enmType, GCPhys, GCPhysLast - GCPhys + 1, !!pfnHandlerR3);
|
---|
194 | #else
|
---|
195 | REMR3NotifyHandlerPhysicalRegister(pVM, enmType, GCPhys, GCPhysLast - GCPhys + 1, !!pfnHandlerR3);
|
---|
196 | #endif
|
---|
197 | pgmUnlock(pVM);
|
---|
198 | if (rc != VINF_SUCCESS)
|
---|
199 | Log(("PGMHandlerPhysicalRegisterEx: returns %Vrc (%VGp-%VGp)\n", rc, GCPhys, GCPhysLast));
|
---|
200 | return rc;
|
---|
201 | }
|
---|
202 | pgmUnlock(pVM);
|
---|
203 |
|
---|
204 | #if defined(IN_RING3) && defined(VBOX_STRICT)
|
---|
205 | DBGFR3Info(pVM, "handlers", "phys nostats", NULL);
|
---|
206 | #endif
|
---|
207 | AssertMsgFailed(("Conflict! GCPhys=%VGp GCPhysLast=%VGp pszDesc=%s\n", GCPhys, GCPhysLast, pszDesc));
|
---|
208 | MMHyperFree(pVM, pNew);
|
---|
209 | return VERR_PGM_HANDLER_PHYSICAL_CONFLICT;
|
---|
210 | }
|
---|
211 |
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * Sets ram range flags and attempts updating shadow PTs.
|
---|
215 | *
|
---|
216 | * @returns VBox status code.
|
---|
217 | * @retval VINF_SUCCESS when shadow PTs was successfully updated.
|
---|
218 | * @retval VINF_PGM_GCPHYS_ALIASED when the shadow PTs could be updated because
|
---|
219 | * the guest page aliased or/and mapped by multiple PTs.
|
---|
220 | * @param pVM The VM handle.
|
---|
221 | * @param pCur The physical handler.
|
---|
222 | */
|
---|
223 | static int pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(PVM pVM, PPGMPHYSHANDLER pCur, PPGMRAMRANGE pRam)
|
---|
224 | {
|
---|
225 | /*
|
---|
226 | * Iterate the guest ram pages updating the flags and flushing PT entries
|
---|
227 | * mapping the page.
|
---|
228 | */
|
---|
229 | bool fFlushTLBs = false;
|
---|
230 | #if defined(PGMPOOL_WITH_GCPHYS_TRACKING) || defined(PGMPOOL_WITH_CACHE)
|
---|
231 | int rc = VINF_SUCCESS;
|
---|
232 | #else
|
---|
233 | const int rc = VINF_PGM_GCPHYS_ALIASED;
|
---|
234 | #endif
|
---|
235 | const unsigned fFlags = pgmHandlerPhysicalCalcFlags(pCur); Assert(!(fFlags & X86_PTE_PAE_PG_MASK));
|
---|
236 | RTUINT cPages = pCur->cPages;
|
---|
237 | RTUINT i = (pCur->Core.Key - pRam->GCPhys) >> PAGE_SHIFT;
|
---|
238 | for (;;)
|
---|
239 | {
|
---|
240 | /* Physical chunk in dynamically allocated range not present? */
|
---|
241 | if (RT_UNLIKELY(!PGM_PAGE_GET_HCPHYS(&pRam->aPages[i])))
|
---|
242 | {
|
---|
243 | RTGCPHYS GCPhys = pRam->GCPhys + (i << PAGE_SHIFT);
|
---|
244 | #ifdef IN_RING3
|
---|
245 | int rc2 = pgmr3PhysGrowRange(pVM, GCPhys);
|
---|
246 | #else
|
---|
247 | int rc2 = CTXALLMID(VMM, CallHost)(pVM, VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
|
---|
248 | #endif
|
---|
249 | if (rc2 != VINF_SUCCESS)
|
---|
250 | return rc2;
|
---|
251 | }
|
---|
252 |
|
---|
253 | if ((pRam->aPages[i].HCPhys & fFlags) != fFlags) /** @todo PAGE FLAGS */
|
---|
254 | {
|
---|
255 | pRam->aPages[i].HCPhys |= fFlags; /** @todo PAGE FLAGS */
|
---|
256 |
|
---|
257 | Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[i]));
|
---|
258 |
|
---|
259 | #ifdef PGMPOOL_WITH_GCPHYS_TRACKING
|
---|
260 | /* This code also makes ASSUMPTIONS about the cRefs and stuff. */
|
---|
261 | Assert(MM_RAM_FLAGS_IDX_SHIFT < MM_RAM_FLAGS_CREFS_SHIFT);
|
---|
262 | const uint16_t u16 = pRam->aPages[i].HCPhys >> MM_RAM_FLAGS_IDX_SHIFT; /** @todo PAGE FLAGS */
|
---|
263 | if (u16)
|
---|
264 | {
|
---|
265 | if ((u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) != MM_RAM_FLAGS_CREFS_PHYSEXT)
|
---|
266 | pgmPoolTrackFlushGCPhysPT(pVM,
|
---|
267 | &pRam->aPages[i],
|
---|
268 | u16 & MM_RAM_FLAGS_IDX_MASK,
|
---|
269 | u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
|
---|
270 | else if (u16 != ((MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) | MM_RAM_FLAGS_IDX_OVERFLOWED))
|
---|
271 | pgmPoolTrackFlushGCPhysPTs(pVM, &pRam->aPages[i], u16 & MM_RAM_FLAGS_IDX_MASK);
|
---|
272 | else
|
---|
273 | rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, &pRam->aPages[i]);
|
---|
274 | fFlushTLBs = true;
|
---|
275 | }
|
---|
276 | #elif defined(PGMPOOL_WITH_CACHE)
|
---|
277 | rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, &pRam->aPages[i]);
|
---|
278 | fFlushTLBs = true;
|
---|
279 | #endif
|
---|
280 | }
|
---|
281 |
|
---|
282 | /* next */
|
---|
283 | if (--cPages == 0)
|
---|
284 | break;
|
---|
285 | i++;
|
---|
286 | }
|
---|
287 |
|
---|
288 | if (fFlushTLBs && rc == VINF_SUCCESS)
|
---|
289 | {
|
---|
290 | PGM_INVL_GUEST_TLBS();
|
---|
291 | Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: flushing guest TLBs\n"));
|
---|
292 | }
|
---|
293 | else
|
---|
294 | Log(("pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs: doesn't flush guest TLBs. rc=%Vrc\n", rc));
|
---|
295 | return rc;
|
---|
296 | }
|
---|
297 |
|
---|
298 |
|
---|
299 | /**
|
---|
300 | * Register a physical page access handler.
|
---|
301 | *
|
---|
302 | * @returns VBox status code.
|
---|
303 | * @param pVM VM Handle.
|
---|
304 | * @param GCPhys Start physical address.
|
---|
305 | */
|
---|
306 | PGMDECL(int) PGMHandlerPhysicalDeregister(PVM pVM, RTGCPHYS GCPhys)
|
---|
307 | {
|
---|
308 | /*
|
---|
309 | * Find the handler.
|
---|
310 | */
|
---|
311 | pgmLock(pVM);
|
---|
312 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
313 | if (pCur)
|
---|
314 | {
|
---|
315 | LogFlow(("PGMHandlerPhysicalDeregister: Removing Range %#VGp-%#VGp %s\n",
|
---|
316 | pCur->Core.Key, pCur->Core.KeyLast, HCSTRING(pCur->pszDesc)));
|
---|
317 |
|
---|
318 | /*
|
---|
319 | * Clear the page bits and notify the REM about this change.
|
---|
320 | */
|
---|
321 | pgmHandlerPhysicalResetRamFlags(pVM, pCur);
|
---|
322 | pgmHandlerPhysicalDeregisterNotifyREM(pVM, pCur);
|
---|
323 | pgmUnlock(pVM);
|
---|
324 | MMHyperFree(pVM, pCur);
|
---|
325 | return VINF_SUCCESS;
|
---|
326 | }
|
---|
327 | pgmUnlock(pVM);
|
---|
328 |
|
---|
329 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhys));
|
---|
330 | return VERR_PGM_HANDLER_NOT_FOUND;
|
---|
331 | }
|
---|
332 |
|
---|
333 |
|
---|
334 | /**
|
---|
335 | * Shared code with modify.
|
---|
336 | */
|
---|
337 | static void pgmHandlerPhysicalDeregisterNotifyREM(PVM pVM, PPGMPHYSHANDLER pCur)
|
---|
338 | {
|
---|
339 | RTGCPHYS GCPhysStart = pCur->Core.Key;
|
---|
340 | RTGCPHYS GCPhysLast = pCur->Core.KeyLast;
|
---|
341 |
|
---|
342 | /*
|
---|
343 | * Page align the range.
|
---|
344 | */
|
---|
345 | if ( (pCur->Core.Key & PAGE_OFFSET_MASK)
|
---|
346 | || ((pCur->Core.KeyLast + 1) & PAGE_OFFSET_MASK))
|
---|
347 | {
|
---|
348 | if (GCPhysStart & PAGE_OFFSET_MASK)
|
---|
349 | {
|
---|
350 | if (pgmRamTestFlags(&pVM->pgm.s, GCPhysStart, MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_PHYSICAL_TEMP_OFF))
|
---|
351 | {
|
---|
352 | RTGCPHYS GCPhys = (GCPhysStart + (PAGE_SIZE - 1)) & X86_PTE_PAE_PG_MASK;
|
---|
353 | if ( GCPhys > GCPhysLast
|
---|
354 | || GCPhys < GCPhysStart)
|
---|
355 | return;
|
---|
356 | GCPhysStart = GCPhys;
|
---|
357 | }
|
---|
358 | else
|
---|
359 | GCPhysStart = GCPhysStart & X86_PTE_PAE_PG_MASK;
|
---|
360 | }
|
---|
361 | if (GCPhysLast & PAGE_OFFSET_MASK)
|
---|
362 | {
|
---|
363 | if (pgmRamTestFlags(&pVM->pgm.s, GCPhysLast, MM_RAM_FLAGS_MMIO | MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_ALL | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_PHYSICAL_TEMP_OFF))
|
---|
364 | {
|
---|
365 | RTGCPHYS GCPhys = (GCPhysStart & X86_PTE_PAE_PG_MASK) - 1;
|
---|
366 | if ( GCPhys < GCPhysStart
|
---|
367 | || GCPhys > GCPhysLast)
|
---|
368 | return;
|
---|
369 | GCPhysLast = GCPhys;
|
---|
370 | }
|
---|
371 | else
|
---|
372 | GCPhysLast += PAGE_SIZE - 1 - (GCPhysLast & PAGE_OFFSET_MASK);
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | /*
|
---|
377 | * Tell REM.
|
---|
378 | */
|
---|
379 | const bool fRestoreAsRAM = pCur->pfnHandlerR3
|
---|
380 | && pCur->enmType != PGMPHYSHANDLERTYPE_MMIO; /** @todo this isn't entirely correct. */
|
---|
381 | #ifndef IN_RING3
|
---|
382 | REMNotifyHandlerPhysicalDeregister(pVM, pCur->enmType, GCPhysStart, GCPhysLast - GCPhysStart + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
|
---|
383 | #else
|
---|
384 | REMR3NotifyHandlerPhysicalDeregister(pVM, pCur->enmType, GCPhysStart, GCPhysLast - GCPhysStart + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
|
---|
385 | #endif
|
---|
386 | }
|
---|
387 |
|
---|
388 |
|
---|
389 | /**
|
---|
390 | * Resets ram range flags.
|
---|
391 | *
|
---|
392 | * @returns VBox status code.
|
---|
393 | * @retval VINF_SUCCESS when shadow PTs was successfully updated.
|
---|
394 | * @param pVM The VM handle.
|
---|
395 | * @param pCur The physical handler.
|
---|
396 | *
|
---|
397 | * @remark We don't start messing with the shadow page tables, as we've already got code
|
---|
398 | * in Trap0e which deals with out of sync handler flags (originally conceived for
|
---|
399 | * global pages).
|
---|
400 | */
|
---|
401 | static void pgmHandlerPhysicalResetRamFlags(PVM pVM, PPGMPHYSHANDLER pCur)
|
---|
402 | {
|
---|
403 | /*
|
---|
404 | * Iterate the guest ram pages updating the flags and flushing PT entries
|
---|
405 | * mapping the page.
|
---|
406 | */
|
---|
407 | RTUINT cPages = pCur->cPages;
|
---|
408 | RTGCPHYS GCPhys = pCur->Core.Key;
|
---|
409 | PPGMRAMRANGE pRamHint = NULL;
|
---|
410 | PPGM pPGM = &pVM->pgm.s;
|
---|
411 | for (;;)
|
---|
412 | {
|
---|
413 | pgmRamFlagsClearByGCPhysWithHint(pPGM, GCPhys,
|
---|
414 | MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_WRITE | MM_RAM_FLAGS_PHYSICAL_ALL,
|
---|
415 | &pRamHint);
|
---|
416 | /* next */
|
---|
417 | if (--cPages == 0)
|
---|
418 | break;
|
---|
419 | GCPhys += PAGE_SIZE;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /*
|
---|
423 | * Check for partial start page.
|
---|
424 | */
|
---|
425 | if (pCur->Core.Key & PAGE_OFFSET_MASK)
|
---|
426 | {
|
---|
427 | RTGCPHYS GCPhys = pCur->Core.Key - 1;
|
---|
428 | for (;;)
|
---|
429 | {
|
---|
430 | PPGMPHYSHANDLER pBelow = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys, false);
|
---|
431 | if ( !pBelow
|
---|
432 | || (pBelow->Core.KeyLast >> PAGE_SHIFT) != (pCur->Core.Key >> PAGE_SHIFT))
|
---|
433 | break;
|
---|
434 | pgmRamFlagsSetByGCPhysWithHint(pPGM, GCPhys, pgmHandlerPhysicalCalcFlags(pCur), &pRamHint);
|
---|
435 |
|
---|
436 | /* next? */
|
---|
437 | if ( (pBelow->Core.Key >> PAGE_SHIFT) != (pCur->Core.Key >> PAGE_SHIFT)
|
---|
438 | || !(pBelow->Core.Key & PAGE_OFFSET_MASK))
|
---|
439 | break;
|
---|
440 | GCPhys = pBelow->Core.Key - 1;
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | /*
|
---|
445 | * Check for partial end page.
|
---|
446 | */
|
---|
447 | if ((pCur->Core.KeyLast & PAGE_OFFSET_MASK) != PAGE_SIZE - 1)
|
---|
448 | {
|
---|
449 | RTGCPHYS GCPhys = pCur->Core.KeyLast + 1;
|
---|
450 | for (;;)
|
---|
451 | {
|
---|
452 | PPGMPHYSHANDLER pAbove = (PPGMPHYSHANDLER)RTAvlroGCPhysGetBestFit(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys, true);
|
---|
453 | if ( !pAbove
|
---|
454 | || (pAbove->Core.Key >> PAGE_SHIFT) != (pCur->Core.KeyLast >> PAGE_SHIFT))
|
---|
455 | break;
|
---|
456 | pgmRamFlagsSetByGCPhysWithHint(pPGM, GCPhys, pgmHandlerPhysicalCalcFlags(pCur), &pRamHint);
|
---|
457 |
|
---|
458 | /* next? */
|
---|
459 | if ( (pAbove->Core.KeyLast >> PAGE_SHIFT) != (pCur->Core.KeyLast >> PAGE_SHIFT)
|
---|
460 | || (pAbove->Core.KeyLast & PAGE_OFFSET_MASK) == PAGE_SIZE - 1)
|
---|
461 | break;
|
---|
462 | GCPhys = pAbove->Core.KeyLast + 1;
|
---|
463 | }
|
---|
464 | }
|
---|
465 | }
|
---|
466 |
|
---|
467 |
|
---|
468 | /**
|
---|
469 | * Modify a physical page access handler.
|
---|
470 | *
|
---|
471 | * Modification can only be done to the range it self, not the type or anything else.
|
---|
472 | *
|
---|
473 | * @returns VBox status code.
|
---|
474 | * For all return codes other than VERR_PGM_HANDLER_NOT_FOUND and VINF_SUCCESS the range is deregistered
|
---|
475 | * and a new registration must be performed!
|
---|
476 | * @param pVM VM handle.
|
---|
477 | * @param GCPhysCurrent Current location.
|
---|
478 | * @param GCPhys New location.
|
---|
479 | * @param GCPhysLast New last location.
|
---|
480 | */
|
---|
481 | PGMDECL(int) PGMHandlerPhysicalModify(PVM pVM, RTGCPHYS GCPhysCurrent, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast)
|
---|
482 | {
|
---|
483 | /*
|
---|
484 | * Remove it.
|
---|
485 | */
|
---|
486 | int rc;
|
---|
487 | pgmLock(pVM);
|
---|
488 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhysCurrent);
|
---|
489 | if (pCur)
|
---|
490 | {
|
---|
491 | /*
|
---|
492 | * Clear the ram flags. (We're gonna move or free it!)
|
---|
493 | */
|
---|
494 | pgmHandlerPhysicalResetRamFlags(pVM, pCur);
|
---|
495 | const bool fRestoreAsRAM = pCur->pfnHandlerR3
|
---|
496 | && pCur->enmType != PGMPHYSHANDLERTYPE_MMIO; /** @todo this isn't entirely correct. */
|
---|
497 |
|
---|
498 | /*
|
---|
499 | * Validate the new range, modify and reinsert.
|
---|
500 | */
|
---|
501 | if (GCPhysLast >= GCPhys)
|
---|
502 | {
|
---|
503 | /*
|
---|
504 | * We require the range to be within registered ram.
|
---|
505 | * There is no apparent need to support ranges which cover more than one ram range.
|
---|
506 | */
|
---|
507 | PPGMRAMRANGE pRam = CTXALLSUFF(pVM->pgm.s.pRamRanges);
|
---|
508 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
509 | pRam = CTXALLSUFF(pRam->pNext);
|
---|
510 | if ( pRam
|
---|
511 | && GCPhys <= pRam->GCPhysLast
|
---|
512 | && GCPhysLast >= pRam->GCPhys)
|
---|
513 | {
|
---|
514 | pCur->Core.Key = GCPhys;
|
---|
515 | pCur->Core.KeyLast = GCPhysLast;
|
---|
516 | pCur->cPages = (GCPhysLast - (GCPhys & X86_PTE_PAE_PG_MASK) + 1) >> PAGE_SHIFT;
|
---|
517 |
|
---|
518 | if (RTAvlroGCPhysInsert(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, &pCur->Core))
|
---|
519 | {
|
---|
520 | /*
|
---|
521 | * Set ram flags, flush shadow PT entries and finally tell REM about this.
|
---|
522 | */
|
---|
523 | rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pCur, pRam);
|
---|
524 | if (rc == VINF_PGM_GCPHYS_ALIASED)
|
---|
525 | {
|
---|
526 | pVM->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
|
---|
527 | VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
|
---|
528 | }
|
---|
529 | pVM->pgm.s.fPhysCacheFlushPending = true;
|
---|
530 |
|
---|
531 | #ifndef IN_RING3
|
---|
532 | REMNotifyHandlerPhysicalModify(pVM, pCur->enmType, GCPhysCurrent, GCPhys,
|
---|
533 | pCur->Core.KeyLast - GCPhys + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
|
---|
534 | #else
|
---|
535 | REMR3NotifyHandlerPhysicalModify(pVM, pCur->enmType, GCPhysCurrent, GCPhys,
|
---|
536 | pCur->Core.KeyLast - GCPhys + 1, !!pCur->pfnHandlerR3, fRestoreAsRAM);
|
---|
537 | #endif
|
---|
538 | pgmUnlock(pVM);
|
---|
539 | Log(("PGMHandlerPhysicalModify: GCPhysCurrent=%VGp -> GCPhys=%VGp GCPhysLast=%VGp\n",
|
---|
540 | GCPhysCurrent, GCPhys, GCPhysLast));
|
---|
541 | return VINF_SUCCESS;
|
---|
542 | }
|
---|
543 |
|
---|
544 | AssertMsgFailed(("Conflict! GCPhys=%VGp GCPhysLast=%VGp\n", GCPhys, GCPhysLast));
|
---|
545 | rc = VERR_PGM_HANDLER_PHYSICAL_CONFLICT;
|
---|
546 | }
|
---|
547 | else
|
---|
548 | {
|
---|
549 | AssertMsgFailed(("No RAM range for %VGp-%VGp\n", GCPhys, GCPhysLast));
|
---|
550 | rc = VERR_PGM_HANDLER_PHYSICAL_NO_RAM_RANGE;
|
---|
551 | }
|
---|
552 | }
|
---|
553 | else
|
---|
554 | {
|
---|
555 | AssertMsgFailed(("Invalid range %VGp-%VGp\n", GCPhys, GCPhysLast));
|
---|
556 | rc = VERR_INVALID_PARAMETER;
|
---|
557 | }
|
---|
558 |
|
---|
559 | /*
|
---|
560 | * Invalid new location, free it.
|
---|
561 | * We've only gotta notify REM and free the memory.
|
---|
562 | */
|
---|
563 | pgmHandlerPhysicalDeregisterNotifyREM(pVM, pCur);
|
---|
564 | MMHyperFree(pVM, pCur);
|
---|
565 | }
|
---|
566 | else
|
---|
567 | {
|
---|
568 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhysCurrent));
|
---|
569 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
570 | }
|
---|
571 |
|
---|
572 | pgmUnlock(pVM);
|
---|
573 | return rc;
|
---|
574 | }
|
---|
575 |
|
---|
576 |
|
---|
577 | /**
|
---|
578 | * Changes the callbacks associated with a physical access handler.
|
---|
579 | *
|
---|
580 | * @returns VBox status code.
|
---|
581 | * @param pVM VM Handle.
|
---|
582 | * @param GCPhys Start physical address.
|
---|
583 | * @param pfnHandlerR3 The R3 handler.
|
---|
584 | * @param pvUserR3 User argument to the R3 handler.
|
---|
585 | * @param pfnHandlerR0 The R0 handler.
|
---|
586 | * @param pvUserR0 User argument to the R0 handler.
|
---|
587 | * @param pfnHandlerGC The GC handler.
|
---|
588 | * @param pvUserGC User argument to the GC handler.
|
---|
589 | * This must be a GC pointer because it will be relocated!
|
---|
590 | * @param pszDesc Pointer to description string. This must not be freed.
|
---|
591 | */
|
---|
592 | PGMDECL(int) PGMHandlerPhysicalChangeCallbacks(PVM pVM, RTGCPHYS GCPhys,
|
---|
593 | R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3, RTR3PTR pvUserR3,
|
---|
594 | R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0, RTR0PTR pvUserR0,
|
---|
595 | GCPTRTYPE(PFNPGMGCPHYSHANDLER) pfnHandlerGC, RTGCPTR pvUserGC,
|
---|
596 | R3PTRTYPE(const char *) pszDesc)
|
---|
597 | {
|
---|
598 | /*
|
---|
599 | * Get the handler.
|
---|
600 | */
|
---|
601 | int rc = VINF_SUCCESS;
|
---|
602 | pgmLock(pVM);
|
---|
603 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
604 | if (pCur)
|
---|
605 | {
|
---|
606 | /*
|
---|
607 | * Change callbacks.
|
---|
608 | */
|
---|
609 | pCur->pfnHandlerR3 = pfnHandlerR3;
|
---|
610 | pCur->pvUserR3 = pvUserR3;
|
---|
611 | pCur->pfnHandlerR0 = pfnHandlerR0;
|
---|
612 | pCur->pvUserR0 = pvUserR0;
|
---|
613 | pCur->pfnHandlerGC = pfnHandlerGC;
|
---|
614 | pCur->pvUserGC = pvUserGC;
|
---|
615 | pCur->pszDesc = pszDesc;
|
---|
616 | }
|
---|
617 | else
|
---|
618 | {
|
---|
619 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhys));
|
---|
620 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
621 | }
|
---|
622 |
|
---|
623 | pgmUnlock(pVM);
|
---|
624 | return rc;
|
---|
625 | }
|
---|
626 |
|
---|
627 |
|
---|
628 | /**
|
---|
629 | * Splitts a physical access handler in two.
|
---|
630 | *
|
---|
631 | * @returns VBox status code.
|
---|
632 | * @param pVM VM Handle.
|
---|
633 | * @param GCPhys Start physical address of the handler.
|
---|
634 | * @param GCPhysSplit The split address.
|
---|
635 | */
|
---|
636 | PGMDECL(int) PGMHandlerPhysicalSplit(PVM pVM, RTGCPHYS GCPhys, RTGCPHYS GCPhysSplit)
|
---|
637 | {
|
---|
638 | AssertReturn(GCPhys < GCPhysSplit, VERR_INVALID_PARAMETER);
|
---|
639 |
|
---|
640 | /*
|
---|
641 | * Do the allocation without owning the lock.
|
---|
642 | */
|
---|
643 | PPGMPHYSHANDLER pNew;
|
---|
644 | int rc = MMHyperAlloc(pVM, sizeof(*pNew), 0, MM_TAG_PGM_HANDLERS, (void **)&pNew);
|
---|
645 | if (VBOX_FAILURE(rc))
|
---|
646 | return rc;
|
---|
647 |
|
---|
648 | /*
|
---|
649 | * Get the handler.
|
---|
650 | */
|
---|
651 | pgmLock(pVM);
|
---|
652 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
653 | if (pCur)
|
---|
654 | {
|
---|
655 | if (GCPhysSplit <= pCur->Core.KeyLast)
|
---|
656 | {
|
---|
657 | /*
|
---|
658 | * Create new handler node for the 2nd half.
|
---|
659 | */
|
---|
660 | *pNew = *pCur;
|
---|
661 | pNew->Core.Key = GCPhysSplit;
|
---|
662 | pNew->cPages = (pNew->Core.KeyLast - (pNew->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
663 |
|
---|
664 | pCur->Core.KeyLast = GCPhysSplit - 1;
|
---|
665 | pCur->cPages = (pCur->Core.KeyLast - (pCur->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
666 |
|
---|
667 | if (RTAvlroGCPhysInsert(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, &pNew->Core))
|
---|
668 | {
|
---|
669 | LogFlow(("PGMHandlerPhysicalSplit: %VGp-%VGp and %VGp-%VGp\n",
|
---|
670 | pCur->Core.Key, pCur->Core.KeyLast, pNew->Core.Key, pNew->Core.KeyLast));
|
---|
671 | pgmUnlock(pVM);
|
---|
672 | return VINF_SUCCESS;
|
---|
673 | }
|
---|
674 | AssertMsgFailed(("whu?\n"));
|
---|
675 | rc = VERR_INTERNAL_ERROR;
|
---|
676 | }
|
---|
677 | else
|
---|
678 | {
|
---|
679 | AssertMsgFailed(("outside range: %VGp-%VGp split %VGp\n", pCur->Core.Key, pCur->Core.KeyLast, GCPhysSplit));
|
---|
680 | rc = VERR_INVALID_PARAMETER;
|
---|
681 | }
|
---|
682 | }
|
---|
683 | else
|
---|
684 | {
|
---|
685 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhys));
|
---|
686 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
687 | }
|
---|
688 | pgmUnlock(pVM);
|
---|
689 | MMHyperFree(pVM, pNew);
|
---|
690 | return rc;
|
---|
691 | }
|
---|
692 |
|
---|
693 |
|
---|
694 | /**
|
---|
695 | * Joins up two adjacent physical access handlers which has the same callbacks.
|
---|
696 | *
|
---|
697 | * @returns VBox status code.
|
---|
698 | * @param pVM VM Handle.
|
---|
699 | * @param GCPhys1 Start physical address of the first handler.
|
---|
700 | * @param GCPhys2 Start physical address of the second handler.
|
---|
701 | */
|
---|
702 | PGMDECL(int) PGMHandlerPhysicalJoin(PVM pVM, RTGCPHYS GCPhys1, RTGCPHYS GCPhys2)
|
---|
703 | {
|
---|
704 | /*
|
---|
705 | * Get the handlers.
|
---|
706 | */
|
---|
707 | int rc;
|
---|
708 | pgmLock(pVM);
|
---|
709 | PPGMPHYSHANDLER pCur1 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys1);
|
---|
710 | if (pCur1)
|
---|
711 | {
|
---|
712 | PPGMPHYSHANDLER pCur2 = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys2);
|
---|
713 | if (pCur2)
|
---|
714 | {
|
---|
715 | /*
|
---|
716 | * Make sure that they are adjacent, and that they've got the same callbacks.
|
---|
717 | */
|
---|
718 | if (pCur1->Core.KeyLast + 1 == pCur2->Core.Key)
|
---|
719 | {
|
---|
720 | if ( pCur1->pfnHandlerGC == pCur2->pfnHandlerGC
|
---|
721 | && pCur1->pfnHandlerR0 == pCur2->pfnHandlerR0
|
---|
722 | && pCur1->pfnHandlerR3 == pCur2->pfnHandlerR3)
|
---|
723 | {
|
---|
724 | PPGMPHYSHANDLER pCur3 = (PPGMPHYSHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys2);
|
---|
725 | if (pCur3 == pCur2)
|
---|
726 | {
|
---|
727 | pCur1->Core.KeyLast = pCur2->Core.KeyLast;
|
---|
728 | pCur1->cPages = (pCur1->Core.KeyLast - (pCur1->Core.Key & X86_PTE_PAE_PG_MASK) + PAGE_SIZE) >> PAGE_SHIFT;
|
---|
729 | LogFlow(("PGMHandlerPhysicalJoin: %VGp-%VGp %VGp-%VGp\n",
|
---|
730 | pCur1->Core.Key, pCur1->Core.KeyLast, pCur2->Core.Key, pCur2->Core.KeyLast));
|
---|
731 | pgmUnlock(pVM);
|
---|
732 | MMHyperFree(pVM, pCur2);
|
---|
733 | return VINF_SUCCESS;
|
---|
734 | }
|
---|
735 | Assert(pCur3 == pCur2);
|
---|
736 | rc = VERR_INTERNAL_ERROR;
|
---|
737 | }
|
---|
738 | else
|
---|
739 | {
|
---|
740 | AssertMsgFailed(("mismatching handlers\n"));
|
---|
741 | rc = VERR_ACCESS_DENIED;
|
---|
742 | }
|
---|
743 | }
|
---|
744 | else
|
---|
745 | {
|
---|
746 | AssertMsgFailed(("not adjacent: %VGp-%VGp %VGp-%VGp\n",
|
---|
747 | pCur1->Core.Key, pCur1->Core.KeyLast, pCur2->Core.Key, pCur2->Core.KeyLast));
|
---|
748 | rc = VERR_INVALID_PARAMETER;
|
---|
749 | }
|
---|
750 | }
|
---|
751 | else
|
---|
752 | {
|
---|
753 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhys2));
|
---|
754 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
755 | }
|
---|
756 | }
|
---|
757 | else
|
---|
758 | {
|
---|
759 | AssertMsgFailed(("Didn't find range starting at %VGp\n", GCPhys1));
|
---|
760 | rc = VERR_PGM_HANDLER_NOT_FOUND;
|
---|
761 | }
|
---|
762 | pgmUnlock(pVM);
|
---|
763 | return rc;
|
---|
764 |
|
---|
765 | }
|
---|
766 |
|
---|
767 |
|
---|
768 | /**
|
---|
769 | * Resets any modifications to individual pages in a physical
|
---|
770 | * page access handler region.
|
---|
771 | *
|
---|
772 | * This is used in pair with PGMHandlerPhysicalModify().
|
---|
773 | *
|
---|
774 | * @returns VBox status code.
|
---|
775 | * @param pVM VM Handle
|
---|
776 | * @param GCPhys Start physical address earlier passed to PGMR3HandlerPhysicalRegister().
|
---|
777 | */
|
---|
778 | PGMDECL(int) PGMHandlerPhysicalReset(PVM pVM, RTGCPHYS GCPhys)
|
---|
779 | {
|
---|
780 | /*
|
---|
781 | * Find the handler.
|
---|
782 | */
|
---|
783 | pgmLock(pVM);
|
---|
784 | PPGMPHYSHANDLER pCur = (PPGMPHYSHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysHandlers, GCPhys);
|
---|
785 | if (pCur)
|
---|
786 | {
|
---|
787 | /*
|
---|
788 | * Validate type.
|
---|
789 | */
|
---|
790 | switch (pCur->enmType)
|
---|
791 | {
|
---|
792 | case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE:
|
---|
793 | case PGMPHYSHANDLERTYPE_PHYSICAL_ALL:
|
---|
794 | {
|
---|
795 | /*
|
---|
796 | * Set the flags and flush shadow PT entries.
|
---|
797 | */
|
---|
798 | STAM_COUNTER_INC(&pVM->pgm.s.StatHandlePhysicalReset);
|
---|
799 | PPGMRAMRANGE pRam = CTXALLSUFF(pVM->pgm.s.pRamRanges);
|
---|
800 | while (pRam && GCPhys > pRam->GCPhysLast)
|
---|
801 | pRam = CTXALLSUFF(pRam->pNext);
|
---|
802 | int rc = pgmHandlerPhysicalSetRamFlagsAndFlushShadowPTs(pVM, pCur, pRam);
|
---|
803 | if (rc == VINF_PGM_GCPHYS_ALIASED)
|
---|
804 | {
|
---|
805 | pVM->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
|
---|
806 | VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
|
---|
807 | }
|
---|
808 | pVM->pgm.s.fPhysCacheFlushPending = true;
|
---|
809 | pgmUnlock(pVM);
|
---|
810 | return VINF_SUCCESS;
|
---|
811 | }
|
---|
812 |
|
---|
813 | /*
|
---|
814 | * Invalid.
|
---|
815 | */
|
---|
816 | case PGMPHYSHANDLERTYPE_PHYSICAL:
|
---|
817 | case PGMPHYSHANDLERTYPE_MMIO:
|
---|
818 | AssertMsgFailed(("Can't reset type %d!\n", pCur->enmType));
|
---|
819 | pgmUnlock(pVM);
|
---|
820 | return VERR_INTERNAL_ERROR;
|
---|
821 |
|
---|
822 | default:
|
---|
823 | AssertMsgFailed(("Invalid type %d! Corruption!\n", pCur->enmType));
|
---|
824 | pgmUnlock(pVM);
|
---|
825 | return VERR_INTERNAL_ERROR;
|
---|
826 | }
|
---|
827 | }
|
---|
828 | pgmUnlock(pVM);
|
---|
829 | AssertMsgFailed(("Didn't find MMIO Range starting at %#x\n", GCPhys));
|
---|
830 | return VERR_PGM_HANDLER_NOT_FOUND;
|
---|
831 | }
|
---|
832 |
|
---|
833 |
|
---|
834 | /**
|
---|
835 | * Search for virtual handler with matching physical address
|
---|
836 | *
|
---|
837 | * @returns VBox status code
|
---|
838 | * @param pVM The VM handle.
|
---|
839 | * @param GCPhys GC physical address to search for.
|
---|
840 | * @param ppVirt Where to store the pointer to the virtual handler structure.
|
---|
841 | * @param piPage Where to store the pointer to the index of the cached physical page.
|
---|
842 | */
|
---|
843 | int pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage)
|
---|
844 | {
|
---|
845 | STAM_PROFILE_START(CTXSUFF(&pVM->pgm.s.StatVirtHandleSearchByPhys), a);
|
---|
846 | Assert(ppVirt);
|
---|
847 |
|
---|
848 | PPGMPHYS2VIRTHANDLER pCur;
|
---|
849 | pCur = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRangeGet(&CTXSUFF(pVM->pgm.s.pTrees)->PhysToVirtHandlers, GCPhys);
|
---|
850 | if (pCur)
|
---|
851 | {
|
---|
852 | /* found a match! */
|
---|
853 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
854 | AssertRelease(pCur->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD);
|
---|
855 | #endif
|
---|
856 | *ppVirt = (PPGMVIRTHANDLER)((uintptr_t)pCur + pCur->offVirtHandler);
|
---|
857 | *piPage = pCur - &(*ppVirt)->aPhysToVirt[0];
|
---|
858 |
|
---|
859 | LogFlow(("PHYS2VIRT: found match for %VGp -> %VGv *piPage=%#x\n",
|
---|
860 | GCPhys, (*ppVirt)->GCPtr, *piPage));
|
---|
861 | STAM_PROFILE_STOP(CTXSUFF(&pVM->pgm.s.StatVirtHandleSearchByPhys), a);
|
---|
862 | return VINF_SUCCESS;
|
---|
863 | }
|
---|
864 |
|
---|
865 | *ppVirt = NULL;
|
---|
866 | STAM_PROFILE_STOP(CTXSUFF(&pVM->pgm.s.StatVirtHandleSearchByPhys), a);
|
---|
867 | return VERR_PGM_HANDLER_NOT_FOUND;
|
---|
868 | }
|
---|
869 |
|
---|
870 |
|
---|
871 | /**
|
---|
872 | * Deal with aliases in phys2virt.
|
---|
873 | *
|
---|
874 | * @param pVM The VM handle.
|
---|
875 | * @param pPhys2Virt The node we failed insert.
|
---|
876 | */
|
---|
877 | static void pgmHandlerVirtualInsertAliased(PVM pVM, PPGMPHYS2VIRTHANDLER pPhys2Virt)
|
---|
878 | {
|
---|
879 | /*
|
---|
880 | * First find the node which is conflicting with us.
|
---|
881 | */
|
---|
882 | /** @todo Deal with partial overlapping. (Unlikly situation, so I'm too lazy to do anything about it now.) */
|
---|
883 | PPGMPHYS2VIRTHANDLER pHead = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
|
---|
884 | if (!pHead)
|
---|
885 | {
|
---|
886 | /** @todo do something clever here... */
|
---|
887 | #ifdef IN_RING3
|
---|
888 | LogRel(("pgmHandlerVirtualInsertAliased: %VGp-%VGp\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
|
---|
889 | #endif
|
---|
890 | pPhys2Virt->offNextAlias = 0;
|
---|
891 | return;
|
---|
892 | }
|
---|
893 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
894 | AssertReleaseMsg(pHead != pPhys2Virt, ("%VGp-%VGp offVirtHandler=%#RX32\n",
|
---|
895 | pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler));
|
---|
896 | #endif
|
---|
897 |
|
---|
898 | /** @todo check if the current head node covers the ground we do. This is highly unlikely
|
---|
899 | * and I'm too lazy to implement this now as it will require sorting the list and stuff like that. */
|
---|
900 |
|
---|
901 | /*
|
---|
902 | * Insert ourselves as the next node.
|
---|
903 | */
|
---|
904 | if (!(pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
|
---|
905 | pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IN_TREE;
|
---|
906 | else
|
---|
907 | {
|
---|
908 | PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pHead + (pHead->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
|
---|
909 | pPhys2Virt->offNextAlias = ((intptr_t)pNext - (intptr_t)pPhys2Virt)
|
---|
910 | | PGMPHYS2VIRTHANDLER_IN_TREE;
|
---|
911 | }
|
---|
912 | pHead->offNextAlias = ((intptr_t)pPhys2Virt - (intptr_t)pHead)
|
---|
913 | | (pHead->offNextAlias & ~PGMPHYS2VIRTHANDLER_OFF_MASK);
|
---|
914 | Log(("pgmHandlerVirtualInsertAliased: %VGp-%VGp offNextAlias=%#RX32\n", pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
|
---|
915 | }
|
---|
916 |
|
---|
917 |
|
---|
918 | /**
|
---|
919 | * Resets one virtual handler range.
|
---|
920 | *
|
---|
921 | * @returns 0
|
---|
922 | * @param pNode Pointer to a PGMVIRTHANDLER.
|
---|
923 | * @param pvUser The VM handle.
|
---|
924 | */
|
---|
925 | DECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser)
|
---|
926 | {
|
---|
927 | PPGMVIRTHANDLER pCur = (PPGMVIRTHANDLER)pNode;
|
---|
928 | PVM pVM = (PVM)pvUser;
|
---|
929 |
|
---|
930 | /*
|
---|
931 | * Calc flags.
|
---|
932 | */
|
---|
933 | unsigned fFlags;
|
---|
934 | switch (pCur->enmType)
|
---|
935 | {
|
---|
936 | case PGMVIRTHANDLERTYPE_EIP:
|
---|
937 | case PGMVIRTHANDLERTYPE_NORMAL: fFlags = MM_RAM_FLAGS_VIRTUAL_HANDLER; break;
|
---|
938 | case PGMVIRTHANDLERTYPE_WRITE: fFlags = MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_WRITE; break;
|
---|
939 | case PGMVIRTHANDLERTYPE_ALL: fFlags = MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_ALL; break;
|
---|
940 | /* hypervisor handlers need no flags and wouldn't have nowhere to put them in any case. */
|
---|
941 | case PGMVIRTHANDLERTYPE_HYPERVISOR:
|
---|
942 | return 0;
|
---|
943 | default:
|
---|
944 | AssertMsgFailed(("Invalid type %d\n", pCur->enmType));
|
---|
945 | return 0;
|
---|
946 | }
|
---|
947 |
|
---|
948 | /*
|
---|
949 | * Iterate the pages and apply the flags.
|
---|
950 | */
|
---|
951 | PPGMRAMRANGE pRamHint = NULL;
|
---|
952 | RTGCUINTPTR offPage = ((RTGCUINTPTR)pCur->GCPtr & PAGE_OFFSET_MASK);
|
---|
953 | RTGCUINTPTR cbLeft = pCur->cb;
|
---|
954 | for (unsigned iPage = 0; iPage < pCur->cPages; iPage++)
|
---|
955 | {
|
---|
956 | PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
|
---|
957 | if (pPhys2Virt->Core.Key != NIL_RTGCPHYS)
|
---|
958 | {
|
---|
959 | /* Update the flags. */
|
---|
960 | int rc = pgmRamFlagsSetByGCPhysWithHint(&pVM->pgm.s, pPhys2Virt->Core.Key, fFlags, &pRamHint);
|
---|
961 | AssertRC(rc);
|
---|
962 |
|
---|
963 | /* Need to insert the page in the Phys2Virt lookup tree? */
|
---|
964 | if (pPhys2Virt->Core.KeyLast == NIL_RTGCPHYS)
|
---|
965 | {
|
---|
966 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
967 | AssertRelease(!pPhys2Virt->offNextAlias);
|
---|
968 | #endif
|
---|
969 | unsigned cbPhys = cbLeft;
|
---|
970 | if (cbPhys > PAGE_SIZE - offPage)
|
---|
971 | cbPhys = PAGE_SIZE - offPage;
|
---|
972 | else
|
---|
973 | Assert(iPage == pCur->cPages - 1);
|
---|
974 | pPhys2Virt->Core.KeyLast = pPhys2Virt->Core.Key + cbPhys - 1; /* inclusive */
|
---|
975 | pPhys2Virt->offNextAlias = PGMPHYS2VIRTHANDLER_IS_HEAD | PGMPHYS2VIRTHANDLER_IN_TREE;
|
---|
976 | if (!RTAvlroGCPhysInsert(&pVM->pgm.s.CTXSUFF(pTrees)->PhysToVirtHandlers, &pPhys2Virt->Core))
|
---|
977 | pgmHandlerVirtualInsertAliased(pVM, pPhys2Virt);
|
---|
978 | #ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
|
---|
979 | else
|
---|
980 | AssertReleaseMsg(RTAvlroGCPhysGet(&pVM->pgm.s.CTXSUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key) == &pPhys2Virt->Core,
|
---|
981 | ("%VGp-%VGp offNextAlias=%#RX32\n",
|
---|
982 | pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias));
|
---|
983 | #endif
|
---|
984 | Log2(("PHYS2VIRT: Insert physical range %VGp-%VGp offNextAlias=%#RX32 %s\n",
|
---|
985 | pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
|
---|
986 | }
|
---|
987 | }
|
---|
988 | cbLeft -= PAGE_SIZE - offPage;
|
---|
989 | offPage = 0;
|
---|
990 | }
|
---|
991 |
|
---|
992 | return 0;
|
---|
993 | }
|
---|
994 |
|
---|
995 |
|
---|
996 | #ifndef IN_RING3
|
---|
997 |
|
---|
998 | # ifdef IN_RING0
|
---|
999 | /** @todo try combine this with iom and em. */
|
---|
1000 |
|
---|
1001 | /**
|
---|
1002 | * Read callback for disassembly function; supports reading bytes that cross a page boundary
|
---|
1003 | *
|
---|
1004 | * @returns VBox status code.
|
---|
1005 | * @param pSrc GC source pointer
|
---|
1006 | * @param pDest HC destination pointer
|
---|
1007 | * @param size Number of bytes to read
|
---|
1008 | * @param pvUserdata Callback specific user data (pCpu)
|
---|
1009 | *
|
---|
1010 | */
|
---|
1011 | DECLCALLBACK(int) pgmReadBytes(RTHCUINTPTR pSrc, uint8_t *pDest, unsigned size, void *pvUserdata)
|
---|
1012 | {
|
---|
1013 | DISCPUSTATE *pCpu = (DISCPUSTATE *)pvUserdata;
|
---|
1014 | PVM pVM = (PVM)pCpu->apvUserData[0];
|
---|
1015 |
|
---|
1016 | int rc = PGMPhysReadGCPtr(pVM, pDest, pSrc, size);
|
---|
1017 | AssertRC(rc);
|
---|
1018 | return rc;
|
---|
1019 | }
|
---|
1020 |
|
---|
1021 | inline int pgmDisCoreOne(PVM pVM, DISCPUSTATE *pCpu, RTGCUINTPTR InstrGC, uint32_t *pOpsize)
|
---|
1022 | {
|
---|
1023 | return DISCoreOneEx(InstrGC, pCpu->mode, pgmReadBytes, pVM, pCpu, pOpsize);
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | # else /* !IN_RING0 (i.e. in IN_GC) */
|
---|
1027 | inline int pgmDisCoreOne(PVM pVM, DISCPUSTATE *pCpu, RTGCUINTPTR InstrGC, uint32_t *pOpsize)
|
---|
1028 | {
|
---|
1029 | return DISCoreOne(pCpu, InstrGC, pOpsize);
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | #endif /* !IN_RING0 (i.e. in IN_GC) */
|
---|
1033 |
|
---|
1034 |
|
---|
1035 | /**
|
---|
1036 | * \#PF Handler callback for Guest ROM range write access.
|
---|
1037 | * We simply ignore the writes or fall back to the recompiler if we don't support the instruction.
|
---|
1038 | *
|
---|
1039 | * @returns VBox status code (appropritate for trap handling and GC return).
|
---|
1040 | * @param pVM VM Handle.
|
---|
1041 | * @param uErrorCode CPU Error code.
|
---|
1042 | * @param pRegFrame Trap register frame.
|
---|
1043 | * @param pvFault The fault address (cr2).
|
---|
1044 | * @param GCPhysFault The GC physical address corresponding to pvFault.
|
---|
1045 | * @param pvUser User argument.
|
---|
1046 | */
|
---|
1047 | PGMDECL(int) pgmGuestROMWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, void *pvFault, RTGCPHYS GCPhysFault, void *pvUser)
|
---|
1048 | {
|
---|
1049 | DISCPUSTATE Cpu;
|
---|
1050 | Cpu.mode = SELMIsSelector32Bit(pVM, pRegFrame->eflags, pRegFrame->cs, &pRegFrame->csHid) ? CPUMODE_32BIT : CPUMODE_16BIT;
|
---|
1051 | if (Cpu.mode == CPUMODE_32BIT)
|
---|
1052 | {
|
---|
1053 | RTGCPTR GCPtrCode;
|
---|
1054 | int rc = SELMValidateAndConvertCSAddr(pVM, pRegFrame->eflags, pRegFrame->ss, pRegFrame->cs, &pRegFrame->csHid, (RTGCPTR)pRegFrame->eip, &GCPtrCode);
|
---|
1055 | if (VBOX_SUCCESS(rc))
|
---|
1056 | {
|
---|
1057 | uint32_t cbOp;
|
---|
1058 | rc = pgmDisCoreOne(pVM, &Cpu, (RTGCUINTPTR)GCPtrCode, &cbOp);
|
---|
1059 | if (VBOX_SUCCESS(rc))
|
---|
1060 | {
|
---|
1061 | /* ASSUMES simple instructions.
|
---|
1062 | * For instance 'pop [ROM_ADDRESS]' or 'and [ROM_ADDRESS], eax' better
|
---|
1063 | * not occure or we'll screw up the cpu state.
|
---|
1064 | */
|
---|
1065 | /** @todo We're assuming too much here I think. */
|
---|
1066 | if (!(Cpu.prefix & (PREFIX_REPNE | PREFIX_REP | PREFIX_SEG)))
|
---|
1067 | {
|
---|
1068 | /*
|
---|
1069 | * Move on to the next instruction.
|
---|
1070 | */
|
---|
1071 | pRegFrame->eip += cbOp;
|
---|
1072 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestROMWriteHandled);
|
---|
1073 | return VINF_SUCCESS;
|
---|
1074 | }
|
---|
1075 | LogFlow(("pgmGuestROMWriteHandler: wrong prefix!!\n"));
|
---|
1076 | }
|
---|
1077 | }
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 | STAM_COUNTER_INC(&pVM->pgm.s.StatGCGuestROMWriteUnhandled);
|
---|
1081 | return VINF_EM_RAW_EMULATE_INSTR;
|
---|
1082 | }
|
---|
1083 | #endif /* !IN_RING3 */
|
---|