VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PGMAllHandler.cpp@ 4680

Last change on this file since 4680 was 4620, checked in by vboxsync, 18 years ago

The initial PGMRAMRANGE::aHCPhys -> PGMRAMRANGE::aPages (PGMPAGE) conversion.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette