VirtualBox

source: vbox/trunk/src/VBox/VMM/VMMAll/PGMAllPool.cpp@ 14809

Last change on this file since 14809 was 14809, checked in by vboxsync, 16 years ago

PGMAllPool: RTLogPrintf exist in ring-0 now.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 149.7 KB
Line 
1/* $Id: PGMAllPool.cpp 14809 2008-11-29 23:22:01Z vboxsync $ */
2/** @file
3 * PGM Shadow Page Pool.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_PGM_POOL
27#include <VBox/pgm.h>
28#include <VBox/mm.h>
29#include <VBox/em.h>
30#include <VBox/cpum.h>
31#ifdef IN_RC
32# include <VBox/patm.h>
33#endif
34#include "PGMInternal.h"
35#include <VBox/vm.h>
36#include <VBox/disopcode.h>
37#include <VBox/hwacc_vmx.h>
38
39#include <VBox/log.h>
40#include <VBox/err.h>
41#include <iprt/asm.h>
42
43
44/*******************************************************************************
45* Internal Functions *
46*******************************************************************************/
47__BEGIN_DECLS
48static void pgmPoolFlushAllInt(PPGMPOOL pPool);
49#ifdef PGMPOOL_WITH_USER_TRACKING
50DECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind);
51DECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind);
52static void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
53#endif
54#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
55static void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint);
56#endif
57#ifdef PGMPOOL_WITH_CACHE
58static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
59#endif
60#ifdef PGMPOOL_WITH_MONITORING
61static void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
62#endif
63#ifndef IN_RING3
64DECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
65#endif
66__END_DECLS
67
68
69/**
70 * Checks if the specified page pool kind is for a 4MB or 2MB guest page.
71 *
72 * @returns true if it's the shadow of a 4MB or 2MB guest page, otherwise false.
73 * @param enmKind The page kind.
74 */
75DECLINLINE(bool) pgmPoolIsBigPage(PGMPOOLKIND enmKind)
76{
77 switch (enmKind)
78 {
79 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
80 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
81 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
82 return true;
83 default:
84 return false;
85 }
86}
87
88
89#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
90/**
91 * Maps a pool page into the current context.
92 *
93 * @returns Pointer to the mapping.
94 * @param pVM The VM handle.
95 * @param pPage The page to map.
96 */
97void *pgmPoolMapPage(PVM pVM, PPGMPOOLPAGE pPage)
98{
99 /* general pages. */
100 if (pPage->idx >= PGMPOOL_IDX_FIRST)
101 {
102 Assert(pPage->idx < pVM->pgm.s.CTX_SUFF(pPool)->cCurPages);
103 void *pv;
104 int rc = PGMDynMapHCPage(pVM, pPage->Core.Key, &pv);
105 AssertReleaseRC(rc);
106 return pv;
107 }
108
109 /* special pages. */
110# ifdef IN_RC
111 switch (pPage->idx)
112 {
113 case PGMPOOL_IDX_PD:
114 return pVM->pgm.s.pShw32BitPdRC;
115 case PGMPOOL_IDX_PAE_PD:
116 case PGMPOOL_IDX_PAE_PD_0:
117 return pVM->pgm.s.apShwPaePDsRC[0];
118 case PGMPOOL_IDX_PAE_PD_1:
119 return pVM->pgm.s.apShwPaePDsRC[1];
120 case PGMPOOL_IDX_PAE_PD_2:
121 return pVM->pgm.s.apShwPaePDsRC[2];
122 case PGMPOOL_IDX_PAE_PD_3:
123 return pVM->pgm.s.apShwPaePDsRC[3];
124 case PGMPOOL_IDX_PDPT:
125 return pVM->pgm.s.pShwPaePdptRC;
126 default:
127 AssertReleaseMsgFailed(("Invalid index %d\n", pPage->idx));
128 return NULL;
129 }
130
131# else /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
132 RTHCPHYS HCPhys;
133 switch (pPage->idx)
134 {
135 case PGMPOOL_IDX_PD:
136 HCPhys = pVM->pgm.s.HCPhysShw32BitPD;
137 break;
138 case PGMPOOL_IDX_PAE_PD_0:
139 HCPhys = pVM->pgm.s.aHCPhysPaePDs[0];
140 break;
141 case PGMPOOL_IDX_PAE_PD_1:
142 HCPhys = pVM->pgm.s.aHCPhysPaePDs[1];
143 break;
144 case PGMPOOL_IDX_PAE_PD_2:
145 HCPhys = pVM->pgm.s.aHCPhysPaePDs[2];
146 break;
147 case PGMPOOL_IDX_PAE_PD_3:
148 HCPhys = pVM->pgm.s.aHCPhysPaePDs[3];
149 break;
150 case PGMPOOL_IDX_PDPT:
151 HCPhys = pVM->pgm.s.HCPhysShwPaePdpt;
152 break;
153 case PGMPOOL_IDX_PAE_PD:
154 AssertReleaseMsgFailed(("PGMPOOL_IDX_PAE_PD is not usable in VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 context\n"));
155 return NULL;
156 default:
157 AssertReleaseMsgFailed(("Invalid index %d\n", pPage->idx));
158 return NULL;
159 }
160 void *pv;
161 int rc = PGMDynMapHCPage(pVM, HCPhys, &pv);
162 AssertReleaseRC(rc);
163 return pv;
164# endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
165}
166#endif /* IN_RC || VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
167
168
169#ifdef PGMPOOL_WITH_MONITORING
170/**
171 * Determin the size of a write instruction.
172 * @returns number of bytes written.
173 * @param pDis The disassembler state.
174 */
175static unsigned pgmPoolDisasWriteSize(PDISCPUSTATE pDis)
176{
177 /*
178 * This is very crude and possibly wrong for some opcodes,
179 * but since it's not really supposed to be called we can
180 * probably live with that.
181 */
182 return DISGetParamSize(pDis, &pDis->param1);
183}
184
185
186/**
187 * Flushes a chain of pages sharing the same access monitor.
188 *
189 * @returns VBox status code suitable for scheduling.
190 * @param pPool The pool.
191 * @param pPage A page in the chain.
192 */
193int pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
194{
195 LogFlow(("pgmPoolMonitorChainFlush: Flush page %RGp type=%d\n", pPage->GCPhys, pPage->enmKind));
196
197 /*
198 * Find the list head.
199 */
200 uint16_t idx = pPage->idx;
201 if (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
202 {
203 while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
204 {
205 idx = pPage->iMonitoredPrev;
206 Assert(idx != pPage->idx);
207 pPage = &pPool->aPages[idx];
208 }
209 }
210
211 /*
212 * Iterate the list flushing each shadow page.
213 */
214 int rc = VINF_SUCCESS;
215 for (;;)
216 {
217 idx = pPage->iMonitoredNext;
218 Assert(idx != pPage->idx);
219 if (pPage->idx >= PGMPOOL_IDX_FIRST)
220 {
221 int rc2 = pgmPoolFlushPage(pPool, pPage);
222 if (rc2 == VERR_PGM_POOL_CLEARED && rc == VINF_SUCCESS)
223 rc = VINF_PGM_SYNC_CR3;
224 }
225 /* next */
226 if (idx == NIL_PGMPOOL_IDX)
227 break;
228 pPage = &pPool->aPages[idx];
229 }
230 return rc;
231}
232
233
234/**
235 * Wrapper for getting the current context pointer to the entry being modified.
236 *
237 * @returns Pointer to the current context mapping of the entry.
238 * @param pPool The pool.
239 * @param pvFault The fault virtual address.
240 * @param GCPhysFault The fault physical address.
241 * @param cbEntry The entry size.
242 */
243#ifdef IN_RING3
244DECLINLINE(const void *) pgmPoolMonitorGCPtr2CCPtr(PPGMPOOL pPool, RTHCPTR pvFault, RTGCPHYS GCPhysFault, const unsigned cbEntry)
245#else
246DECLINLINE(const void *) pgmPoolMonitorGCPtr2CCPtr(PPGMPOOL pPool, RTGCPTR pvFault, RTGCPHYS GCPhysFault, const unsigned cbEntry)
247#endif
248{
249#ifdef IN_RC
250 return (const void *)((RTGCUINTPTR)pvFault & ~(RTGCUINTPTR)(cbEntry - 1));
251
252#elif defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
253 void *pvRet;
254 int rc = PGMDynMapGCPageOff(pPool->pVMR0, GCPhysFault & ~(RTGCPHYS)(cbEntry - 1), &pvRet);
255 AssertFatalRCSuccess(rc);
256 return pvRet;
257
258#elif defined(IN_RING0)
259 void *pvRet;
260 int rc = pgmRamGCPhys2HCPtr(&pPool->pVMR0->pgm.s, GCPhysFault & ~(RTGCPHYS)(cbEntry - 1), &pvRet);
261 AssertFatalRCSuccess(rc);
262 return pvRet;
263
264#elif defined(IN_RING3)
265 return (RTHCPTR)((uintptr_t)pvFault & ~(RTHCUINTPTR)(cbEntry - 1));
266#else
267# error "huh?"
268#endif
269}
270
271
272/**
273 * Process shadow entries before they are changed by the guest.
274 *
275 * For PT entries we will clear them. For PD entries, we'll simply check
276 * for mapping conflicts and set the SyncCR3 FF if found.
277 *
278 * @param pPool The pool.
279 * @param pPage The head page.
280 * @param GCPhysFault The guest physical fault address.
281 * @param uAddress In R0 and GC this is the guest context fault address (flat).
282 * In R3 this is the host context 'fault' address.
283 * @param pCpu The disassembler state for figuring out the write size.
284 * This need not be specified if the caller knows we won't do cross entry accesses.
285 */
286#ifdef IN_RING3
287void pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTHCPTR pvAddress, PDISCPUSTATE pCpu)
288#else
289void pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTGCPTR pvAddress, PDISCPUSTATE pCpu)
290#endif
291{
292 Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
293 const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
294 const unsigned cbWrite = (pCpu) ? pgmPoolDisasWriteSize(pCpu) : 0;
295
296 LogFlow(("pgmPoolMonitorChainChanging: %RGv phys=%RGp kind=%d cbWrite=%d\n", pvAddress, GCPhysFault, pPage->enmKind, cbWrite));
297
298 for (;;)
299 {
300 union
301 {
302 void *pv;
303 PX86PT pPT;
304 PX86PTPAE pPTPae;
305 PX86PD pPD;
306 PX86PDPAE pPDPae;
307 PX86PDPT pPDPT;
308 PX86PML4 pPML4;
309 } uShw;
310
311 switch (pPage->enmKind)
312 {
313 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
314 {
315 uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
316 const unsigned iShw = off / sizeof(X86PTE);
317 if (uShw.pPT->a[iShw].n.u1Present)
318 {
319# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
320 PCX86PTE pGstPte = (PCX86PTE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
321 Log4(("pgmPoolMonitorChainChanging 32_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PG_MASK));
322 pgmPoolTracDerefGCPhysHint(pPool, pPage,
323 uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK,
324 pGstPte->u & X86_PTE_PG_MASK);
325# endif
326 uShw.pPT->a[iShw].u = 0;
327 }
328 break;
329 }
330
331 /* page/2 sized */
332 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
333 uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
334 if (!((off ^ pPage->GCPhys) & (PAGE_SIZE / 2)))
335 {
336 const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
337 if (uShw.pPTPae->a[iShw].n.u1Present)
338 {
339# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
340 PCX86PTE pGstPte = (PCX86PTE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
341 Log4(("pgmPoolMonitorChainChanging pae_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PG_MASK));
342 pgmPoolTracDerefGCPhysHint(pPool, pPage,
343 uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
344 pGstPte->u & X86_PTE_PG_MASK);
345# endif
346 uShw.pPTPae->a[iShw].u = 0;
347 }
348 }
349 break;
350
351 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
352 {
353 uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
354 const unsigned iShw = off / sizeof(X86PTEPAE);
355 if (uShw.pPTPae->a[iShw].n.u1Present)
356 {
357# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
358 PCX86PTEPAE pGstPte = (PCX86PTEPAE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
359 Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PAE_PG_MASK));
360 pgmPoolTracDerefGCPhysHint(pPool, pPage,
361 uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
362 pGstPte->u & X86_PTE_PAE_PG_MASK);
363# endif
364 uShw.pPTPae->a[iShw].u = 0;
365 }
366
367 /* paranoia / a bit assumptive. */
368 if ( pCpu
369 && (off & 7)
370 && (off & 7) + cbWrite > sizeof(X86PTEPAE))
371 {
372 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTEPAE);
373 AssertReturnVoid(iShw2 < RT_ELEMENTS(uShw.pPTPae->a));
374
375 if (uShw.pPTPae->a[iShw2].n.u1Present)
376 {
377# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
378 PCX86PTEPAE pGstPte = (PCX86PTEPAE)pgmPoolMonitorGCPtr2CCPtr(pPool, pvAddress, GCPhysFault, sizeof(*pGstPte));
379 Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK, pGstPte->u & X86_PTE_PAE_PG_MASK));
380 pgmPoolTracDerefGCPhysHint(pPool, pPage,
381 uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK,
382 pGstPte->u & X86_PTE_PAE_PG_MASK);
383# endif
384 uShw.pPTPae->a[iShw2].u = 0;
385 }
386 }
387
388 break;
389 }
390
391 case PGMPOOLKIND_ROOT_32BIT_PD:
392 {
393 uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
394 const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
395 if (uShw.pPD->a[iShw].u & PGM_PDFLAGS_MAPPING)
396 {
397 Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
398 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
399 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
400 }
401 /* paranoia / a bit assumptive. */
402 else if ( pCpu
403 && (off & 3)
404 && (off & 3) + cbWrite > sizeof(X86PTE))
405 {
406 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTE);
407 if ( iShw2 != iShw
408 && iShw2 < RT_ELEMENTS(uShw.pPD->a)
409 && uShw.pPD->a[iShw2].u & PGM_PDFLAGS_MAPPING)
410 {
411 Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
412 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
413 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
414 }
415 }
416#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
417 if ( uShw.pPD->a[iShw].n.u1Present
418 && !VM_FF_ISSET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3))
419 {
420 LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
421# ifdef IN_RC /* TLB load - we're pushing things a bit... */
422 ASMProbeReadByte(pvAddress);
423# endif
424 pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
425 uShw.pPD->a[iShw].u = 0;
426 }
427#endif
428 break;
429 }
430
431 case PGMPOOLKIND_ROOT_PAE_PD:
432 {
433 unsigned iGst = off / sizeof(X86PDE); // ASSUMING 32-bit guest paging!
434 unsigned iShwPdpt = iGst / 256;
435 unsigned iShw = (iGst % 256) * 2;
436 Assert(pPage->idx == PGMPOOL_IDX_PAE_PD);
437 PPGMPOOLPAGE pPage2 = pPage + 1 + iShwPdpt;
438 Assert(pPage2->idx == PGMPOOL_IDX_PAE_PD_0 + iShwPdpt);
439 uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage2);
440 for (unsigned i = 0; i < 2; i++, iShw++)
441 {
442 if ((uShw.pPDPae->a[iShw].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
443 {
444 Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
445 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
446 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw=%#x!\n", iShwPdpt, iShw));
447 }
448 /* paranoia / a bit assumptive. */
449 else if ( pCpu
450 && (off & 3)
451 && (off & 3) + cbWrite > 4)
452 {
453 const unsigned iShw2 = iShw + 2;
454 if ( iShw2 < RT_ELEMENTS(uShw.pPDPae->a) /** @todo was completely wrong, it's better now after #1865 but still wrong from cross PD. */
455 && (uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
456 {
457 Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
458 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
459 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw2=%#x!\n", iShwPdpt, iShw2));
460 }
461 }
462#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
463 if ( uShw.pPDPae->a[iShw].n.u1Present
464 && !VM_FF_ISSET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3))
465 {
466 LogFlow(("pgmPoolMonitorChainChanging: iShwPdpt=%#x iShw=%#x: %RX64 -> freeing it!\n", iShwPdpt, iShw, uShw.pPDPae->a[iShw].u));
467# ifdef IN_RC /* TLB load - we're pushing things a bit... */
468 ASMProbeReadByte(pvAddress);
469# endif
470 pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK, pPage->idx, iShw + iShwPdpt * X86_PG_PAE_ENTRIES);
471 uShw.pPDPae->a[iShw].u = 0;
472 }
473#endif
474 }
475 break;
476 }
477
478 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
479 {
480 uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
481 const unsigned iShw = off / sizeof(X86PDEPAE);
482 if (uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING)
483 {
484 Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
485 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
486 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
487 }
488#ifdef PGMPOOL_INVALIDATE_UPPER_SHADOW_TABLE_ENTRIES
489 /*
490 * Causes trouble when the guest uses a PDE to refer to the whole page table level
491 * structure. (Invalidate here; faults later on when it tries to change the page
492 * table entries -> recheck; probably only applies to the RC case.)
493 */
494 else
495 {
496 if (uShw.pPDPae->a[iShw].n.u1Present)
497 {
498 LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
499 pgmPoolFree(pPool->CTX_SUFF(pVM),
500 uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
501 /* Note: hardcoded PAE implementation dependency */
502 (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? PGMPOOL_IDX_PAE_PD : pPage->idx,
503 (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? iShw + (pPage->idx - PGMPOOL_IDX_PAE_PD_0) * X86_PG_PAE_ENTRIES : iShw);
504 uShw.pPDPae->a[iShw].u = 0;
505 }
506 }
507#endif
508 /* paranoia / a bit assumptive. */
509 if ( pCpu
510 && (off & 7)
511 && (off & 7) + cbWrite > sizeof(X86PDEPAE))
512 {
513 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
514 AssertReturnVoid(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
515
516 if ( iShw2 != iShw
517 && uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING)
518 {
519 Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
520 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
521 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
522 }
523#ifdef PGMPOOL_INVALIDATE_UPPER_SHADOW_TABLE_ENTRIES
524 else if (uShw.pPDPae->a[iShw2].n.u1Present)
525 {
526 LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
527 pgmPoolFree(pPool->CTX_SUFF(pVM),
528 uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
529 /* Note: hardcoded PAE implementation dependency */
530 (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? PGMPOOL_IDX_PAE_PD : pPage->idx,
531 (pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD) ? iShw2 + (pPage->idx - PGMPOOL_IDX_PAE_PD_0) * X86_PG_PAE_ENTRIES : iShw2);
532 uShw.pPDPae->a[iShw2].u = 0;
533 }
534#endif
535 }
536 break;
537 }
538
539 case PGMPOOLKIND_ROOT_PDPT:
540 {
541 /*
542 * Hopefully this doesn't happen very often:
543 * - touching unused parts of the page
544 * - messing with the bits of pd pointers without changing the physical address
545 */
546 uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
547 const unsigned iShw = off / sizeof(X86PDPE);
548 if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use RT_ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
549 {
550 if (uShw.pPDPT->a[iShw].u & PGM_PLXFLAGS_MAPPING)
551 {
552 Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
553 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
554 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
555 }
556 /* paranoia / a bit assumptive. */
557 else if ( pCpu
558 && (off & 7)
559 && (off & 7) + cbWrite > sizeof(X86PDPE))
560 {
561 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
562 if ( iShw2 != iShw
563 && iShw2 < X86_PG_PAE_PDPE_ENTRIES
564 && uShw.pPDPT->a[iShw2].u & PGM_PLXFLAGS_MAPPING)
565 {
566 Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
567 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
568 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
569 }
570 }
571 }
572 break;
573 }
574
575#ifndef IN_RC
576 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
577 {
578 Assert(pPage->enmKind == PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD);
579
580 uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
581 const unsigned iShw = off / sizeof(X86PDEPAE);
582 if (uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING)
583 {
584 Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
585 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
586 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
587 }
588 else
589 {
590 if (uShw.pPDPae->a[iShw].n.u1Present)
591 {
592 LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
593 pgmPoolFree(pPool->CTX_SUFF(pVM),
594 uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
595 pPage->idx,
596 iShw);
597 uShw.pPDPae->a[iShw].u = 0;
598 }
599 }
600 /* paranoia / a bit assumptive. */
601 if ( pCpu
602 && (off & 7)
603 && (off & 7) + cbWrite > sizeof(X86PDEPAE))
604 {
605 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
606 AssertReturnVoid(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
607
608 if ( iShw2 != iShw
609 && uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING)
610 {
611 Assert(pgmMapAreMappingsEnabled(&pPool->CTX_SUFF(pVM)->pgm.s));
612 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
613 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
614 }
615 else
616 if (uShw.pPDPae->a[iShw2].n.u1Present)
617 {
618 LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
619 pgmPoolFree(pPool->CTX_SUFF(pVM),
620 uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
621 pPage->idx,
622 iShw2);
623 uShw.pPDPae->a[iShw2].u = 0;
624 }
625 }
626 break;
627 }
628
629 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
630 {
631 /*
632 * Hopefully this doesn't happen very often:
633 * - messing with the bits of pd pointers without changing the physical address
634 */
635 if (!VM_FF_ISSET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3))
636 {
637 uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
638 const unsigned iShw = off / sizeof(X86PDPE);
639 if (uShw.pPDPT->a[iShw].n.u1Present)
640 {
641 LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
642 pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK, pPage->idx, iShw);
643 uShw.pPDPT->a[iShw].u = 0;
644 }
645 /* paranoia / a bit assumptive. */
646 if ( pCpu
647 && (off & 7)
648 && (off & 7) + cbWrite > sizeof(X86PDPE))
649 {
650 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
651 if (uShw.pPDPT->a[iShw2].n.u1Present)
652 {
653 LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
654 pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK, pPage->idx, iShw2);
655 uShw.pPDPT->a[iShw2].u = 0;
656 }
657 }
658 }
659 break;
660 }
661
662 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
663 {
664 /*
665 * Hopefully this doesn't happen very often:
666 * - messing with the bits of pd pointers without changing the physical address
667 */
668 if (!VM_FF_ISSET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3))
669 {
670 uShw.pv = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
671 const unsigned iShw = off / sizeof(X86PDPE);
672 if (uShw.pPML4->a[iShw].n.u1Present)
673 {
674 LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
675 pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPML4->a[iShw].u & X86_PML4E_PG_MASK, pPage->idx, iShw);
676 uShw.pPML4->a[iShw].u = 0;
677 }
678 /* paranoia / a bit assumptive. */
679 if ( pCpu
680 && (off & 7)
681 && (off & 7) + cbWrite > sizeof(X86PDPE))
682 {
683 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PML4E);
684 if (uShw.pPML4->a[iShw2].n.u1Present)
685 {
686 LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
687 pgmPoolFree(pPool->CTX_SUFF(pVM), uShw.pPML4->a[iShw2].u & X86_PML4E_PG_MASK, pPage->idx, iShw2);
688 uShw.pPML4->a[iShw2].u = 0;
689 }
690 }
691 }
692 break;
693 }
694#endif /* IN_RING0 */
695
696 default:
697 AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
698 }
699
700 /* next */
701 if (pPage->iMonitoredNext == NIL_PGMPOOL_IDX)
702 return;
703 pPage = &pPool->aPages[pPage->iMonitoredNext];
704 }
705}
706
707
708# ifndef IN_RING3
709/**
710 * Checks if a access could be a fork operation in progress.
711 *
712 * Meaning, that the guest is setuping up the parent process for Copy-On-Write.
713 *
714 * @returns true if it's likly that we're forking, otherwise false.
715 * @param pPool The pool.
716 * @param pCpu The disassembled instruction.
717 * @param offFault The access offset.
718 */
719DECLINLINE(bool) pgmPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pCpu, unsigned offFault)
720{
721 /*
722 * i386 linux is using btr to clear X86_PTE_RW.
723 * The functions involved are (2.6.16 source inspection):
724 * clear_bit
725 * ptep_set_wrprotect
726 * copy_one_pte
727 * copy_pte_range
728 * copy_pmd_range
729 * copy_pud_range
730 * copy_page_range
731 * dup_mmap
732 * dup_mm
733 * copy_mm
734 * copy_process
735 * do_fork
736 */
737 if ( pCpu->pCurInstr->opcode == OP_BTR
738 && !(offFault & 4)
739 /** @todo Validate that the bit index is X86_PTE_RW. */
740 )
741 {
742 STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,Fork));
743 return true;
744 }
745 return false;
746}
747
748
749/**
750 * Determine whether the page is likely to have been reused.
751 *
752 * @returns true if we consider the page as being reused for a different purpose.
753 * @returns false if we consider it to still be a paging page.
754 * @param pVM VM Handle.
755 * @param pPage The page in question.
756 * @param pRegFrame Trap register frame.
757 * @param pCpu The disassembly info for the faulting instruction.
758 * @param pvFault The fault address.
759 *
760 * @remark The REP prefix check is left to the caller because of STOSD/W.
761 */
762DECLINLINE(bool) pgmPoolMonitorIsReused(PVM pVM, PPGMPOOLPAGE pPage, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pCpu, RTGCPTR pvFault)
763{
764#ifndef IN_RC
765 /** @todo could make this general, faulting close to rsp should be safe reuse heuristic. */
766 if ( HWACCMHasPendingIrq(pVM)
767 && (pRegFrame->rsp - pvFault) < 32)
768 {
769 /* Fault caused by stack writes while trying to inject an interrupt event. */
770 Log(("pgmPoolMonitorIsReused: reused %RGv for interrupt stack (rsp=%RGv).\n", pvFault, pRegFrame->rsp));
771 return true;
772 }
773#else
774 NOREF(pVM); NOREF(pvFault);
775#endif
776
777 switch (pCpu->pCurInstr->opcode)
778 {
779 /* call implies the actual push of the return address faulted */
780 case OP_CALL:
781 Log4(("pgmPoolMonitorIsReused: CALL\n"));
782 return true;
783 case OP_PUSH:
784 Log4(("pgmPoolMonitorIsReused: PUSH\n"));
785 return true;
786 case OP_PUSHF:
787 Log4(("pgmPoolMonitorIsReused: PUSHF\n"));
788 return true;
789 case OP_PUSHA:
790 Log4(("pgmPoolMonitorIsReused: PUSHA\n"));
791 return true;
792 case OP_FXSAVE:
793 Log4(("pgmPoolMonitorIsReused: FXSAVE\n"));
794 return true;
795 case OP_MOVNTI: /* solaris - block_zero_no_xmm */
796 Log4(("pgmPoolMonitorIsReused: MOVNTI\n"));
797 return true;
798 case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
799 Log4(("pgmPoolMonitorIsReused: MOVNTDQ\n"));
800 return true;
801 case OP_MOVSWD:
802 case OP_STOSWD:
803 if ( pCpu->prefix == (PREFIX_REP|PREFIX_REX)
804 && pRegFrame->rcx >= 0x40
805 )
806 {
807 Assert(pCpu->mode == CPUMODE_64BIT);
808
809 Log(("pgmPoolMonitorIsReused: OP_STOSQ\n"));
810 return true;
811 }
812 return false;
813 }
814 if ( (pCpu->param1.flags & USE_REG_GEN32)
815 && (pCpu->param1.base.reg_gen == USE_REG_ESP))
816 {
817 Log4(("pgmPoolMonitorIsReused: ESP\n"));
818 return true;
819 }
820
821 //if (pPage->fCR3Mix)
822 // return false;
823 return false;
824}
825
826
827/**
828 * Flushes the page being accessed.
829 *
830 * @returns VBox status code suitable for scheduling.
831 * @param pVM The VM handle.
832 * @param pPool The pool.
833 * @param pPage The pool page (head).
834 * @param pCpu The disassembly of the write instruction.
835 * @param pRegFrame The trap register frame.
836 * @param GCPhysFault The fault address as guest physical address.
837 * @param pvFault The fault address.
838 */
839static int pgmPoolAccessHandlerFlush(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
840 PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
841{
842 /*
843 * First, do the flushing.
844 */
845 int rc = pgmPoolMonitorChainFlush(pPool, pPage);
846
847 /*
848 * Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection).
849 */
850 uint32_t cbWritten;
851 int rc2 = EMInterpretInstructionCPU(pVM, pCpu, pRegFrame, pvFault, &cbWritten);
852 if (RT_SUCCESS(rc2))
853 pRegFrame->rip += pCpu->opsize;
854 else if (rc2 == VERR_EM_INTERPRETER)
855 {
856#ifdef IN_RC
857 if (PATMIsPatchGCAddr(pVM, (RTRCPTR)pRegFrame->eip))
858 {
859 LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
860 pRegFrame->cs, (RTGCPTR)pRegFrame->eip));
861 rc = VINF_SUCCESS;
862 STAM_COUNTER_INC(&pPool->StatMonitorRZIntrFailPatch2);
863 }
864 else
865#endif
866 {
867 rc = VINF_EM_RAW_EMULATE_INSTR;
868 STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
869 }
870 }
871 else
872 rc = rc2;
873
874 /* See use in pgmPoolAccessHandlerSimple(). */
875 PGM_INVL_GUEST_TLBS();
876
877 LogFlow(("pgmPoolAccessHandlerPT: returns %Rrc (flushed)\n", rc));
878 return rc;
879
880}
881
882
883/**
884 * Handles the STOSD write accesses.
885 *
886 * @returns VBox status code suitable for scheduling.
887 * @param pVM The VM handle.
888 * @param pPool The pool.
889 * @param pPage The pool page (head).
890 * @param pCpu The disassembly of the write instruction.
891 * @param pRegFrame The trap register frame.
892 * @param GCPhysFault The fault address as guest physical address.
893 * @param pvFault The fault address.
894 */
895DECLINLINE(int) pgmPoolAccessHandlerSTOSD(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
896 PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
897{
898 Assert(pCpu->mode == CPUMODE_32BIT);
899
900 /*
901 * Increment the modification counter and insert it into the list
902 * of modified pages the first time.
903 */
904 if (!pPage->cModifications++)
905 pgmPoolMonitorModifiedInsert(pPool, pPage);
906
907 /*
908 * Execute REP STOSD.
909 *
910 * This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
911 * write situation, meaning that it's safe to write here.
912 */
913 RTGCUINTPTR pu32 = (RTGCUINTPTR)pvFault;
914 while (pRegFrame->ecx)
915 {
916 pgmPoolMonitorChainChanging(pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
917#ifdef IN_RC
918 *(uint32_t *)pu32 = pRegFrame->eax;
919#else
920 PGMPhysSimpleWriteGCPhys(pVM, GCPhysFault, &pRegFrame->eax, 4);
921#endif
922 pu32 += 4;
923 GCPhysFault += 4;
924 pRegFrame->edi += 4;
925 pRegFrame->ecx--;
926 }
927 pRegFrame->rip += pCpu->opsize;
928
929 /* See use in pgmPoolAccessHandlerSimple(). */
930 PGM_INVL_GUEST_TLBS();
931
932 LogFlow(("pgmPoolAccessHandlerSTOSD: returns\n"));
933 return VINF_SUCCESS;
934}
935
936
937/**
938 * Handles the simple write accesses.
939 *
940 * @returns VBox status code suitable for scheduling.
941 * @param pVM The VM handle.
942 * @param pPool The pool.
943 * @param pPage The pool page (head).
944 * @param pCpu The disassembly of the write instruction.
945 * @param pRegFrame The trap register frame.
946 * @param GCPhysFault The fault address as guest physical address.
947 * @param pvFault The fault address.
948 */
949DECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pCpu,
950 PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
951{
952 /*
953 * Increment the modification counter and insert it into the list
954 * of modified pages the first time.
955 */
956 if (!pPage->cModifications++)
957 pgmPoolMonitorModifiedInsert(pPool, pPage);
958
959 /*
960 * Clear all the pages. ASSUMES that pvFault is readable.
961 */
962 pgmPoolMonitorChainChanging(pPool, pPage, GCPhysFault, pvFault, pCpu);
963
964 /*
965 * Interpret the instruction.
966 */
967 uint32_t cb;
968 int rc = EMInterpretInstructionCPU(pVM, pCpu, pRegFrame, pvFault, &cb);
969 if (RT_SUCCESS(rc))
970 pRegFrame->rip += pCpu->opsize;
971 else if (rc == VERR_EM_INTERPRETER)
972 {
973 LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for %04x:%RGv - opcode=%d\n",
974 pRegFrame->cs, (RTGCPTR)pRegFrame->rip, pCpu->pCurInstr->opcode));
975 rc = VINF_EM_RAW_EMULATE_INSTR;
976 STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
977 }
978
979 /*
980 * Quick hack, with logging enabled we're getting stale
981 * code TLBs but no data TLB for EIP and crash in EMInterpretDisasOne.
982 * Flushing here is BAD and expensive, I think EMInterpretDisasOne will
983 * have to be fixed to support this. But that'll have to wait till next week.
984 *
985 * An alternative is to keep track of the changed PTEs together with the
986 * GCPhys from the guest PT. This may proove expensive though.
987 *
988 * At the moment, it's VITAL that it's done AFTER the instruction interpreting
989 * because we need the stale TLBs in some cases (XP boot). This MUST be fixed properly!
990 */
991 PGM_INVL_GUEST_TLBS();
992
993 LogFlow(("pgmPoolAccessHandlerSimple: returns %Rrc cb=%d\n", rc, cb));
994 return rc;
995}
996
997
998/**
999 * \#PF Handler callback for PT write accesses.
1000 *
1001 * @returns VBox status code (appropriate for GC return).
1002 * @param pVM VM Handle.
1003 * @param uErrorCode CPU Error code.
1004 * @param pRegFrame Trap register frame.
1005 * NULL on DMA and other non CPU access.
1006 * @param pvFault The fault address (cr2).
1007 * @param GCPhysFault The GC physical address corresponding to pvFault.
1008 * @param pvUser User argument.
1009 */
1010DECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
1011{
1012 STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), a);
1013 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1014 PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)pvUser;
1015 LogFlow(("pgmPoolAccessHandler: pvFault=%RGv pPage=%p:{.idx=%d} GCPhysFault=%RGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
1016
1017 /*
1018 * We should ALWAYS have the list head as user parameter. This
1019 * is because we use that page to record the changes.
1020 */
1021 Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
1022
1023 /*
1024 * Disassemble the faulting instruction.
1025 */
1026 DISCPUSTATE Cpu;
1027 int rc = EMInterpretDisasOne(pVM, pRegFrame, &Cpu, NULL);
1028 AssertRCReturn(rc, rc);
1029
1030 /*
1031 * Check if it's worth dealing with.
1032 */
1033 bool fReused = false;
1034 if ( ( pPage->cModifications < 48 /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
1035 || pPage->fCR3Mix)
1036 && !(fReused = pgmPoolMonitorIsReused(pVM, pPage, pRegFrame, &Cpu, pvFault))
1037 && !pgmPoolMonitorIsForking(pPool, &Cpu, GCPhysFault & PAGE_OFFSET_MASK))
1038 {
1039 /*
1040 * Simple instructions, no REP prefix.
1041 */
1042 if (!(Cpu.prefix & (PREFIX_REP | PREFIX_REPNE)))
1043 {
1044 rc = pgmPoolAccessHandlerSimple(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
1045 STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,Handled), a);
1046 return rc;
1047 }
1048
1049 /*
1050 * Windows is frequently doing small memset() operations (netio test 4k+).
1051 * We have to deal with these or we'll kill the cache and performance.
1052 */
1053 if ( Cpu.pCurInstr->opcode == OP_STOSWD
1054 && CPUMGetGuestCPL(pVM, pRegFrame) == 0
1055 && pRegFrame->ecx <= 0x20
1056 && pRegFrame->ecx * 4 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
1057 && !((uintptr_t)pvFault & 3)
1058 && (pRegFrame->eax == 0 || pRegFrame->eax == 0x80) /* the two values observed. */
1059 && Cpu.mode == CPUMODE_32BIT
1060 && Cpu.opmode == CPUMODE_32BIT
1061 && Cpu.addrmode == CPUMODE_32BIT
1062 && Cpu.prefix == PREFIX_REP
1063 && !pRegFrame->eflags.Bits.u1DF
1064 )
1065 {
1066 rc = pgmPoolAccessHandlerSTOSD(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
1067 STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,RepStosd), a);
1068 return rc;
1069 }
1070
1071 /* REP prefix, don't bother. */
1072 STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,RepPrefix));
1073 Log4(("pgmPoolAccessHandler: eax=%#x ecx=%#x edi=%#x esi=%#x rip=%RGv opcode=%d prefix=%#x\n",
1074 pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, (RTGCPTR)pRegFrame->rip, Cpu.pCurInstr->opcode, Cpu.prefix));
1075 }
1076
1077 /*
1078 * Not worth it, so flush it.
1079 *
1080 * If we considered it to be reused, don't to back to ring-3
1081 * to emulate failed instructions since we usually cannot
1082 * interpret then. This may be a bit risky, in which case
1083 * the reuse detection must be fixed.
1084 */
1085 rc = pgmPoolAccessHandlerFlush(pVM, pPool, pPage, &Cpu, pRegFrame, GCPhysFault, pvFault);
1086 if (rc == VINF_EM_RAW_EMULATE_INSTR && fReused)
1087 rc = VINF_SUCCESS;
1088 STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,FlushPage), a);
1089 return rc;
1090}
1091
1092# endif /* !IN_RING3 */
1093#endif /* PGMPOOL_WITH_MONITORING */
1094
1095#ifdef PGMPOOL_WITH_CACHE
1096
1097/**
1098 * Inserts a page into the GCPhys hash table.
1099 *
1100 * @param pPool The pool.
1101 * @param pPage The page.
1102 */
1103DECLINLINE(void) pgmPoolHashInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1104{
1105 Log3(("pgmPoolHashInsert: %RGp\n", pPage->GCPhys));
1106 Assert(pPage->GCPhys != NIL_RTGCPHYS); Assert(pPage->iNext == NIL_PGMPOOL_IDX);
1107 uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
1108 pPage->iNext = pPool->aiHash[iHash];
1109 pPool->aiHash[iHash] = pPage->idx;
1110}
1111
1112
1113/**
1114 * Removes a page from the GCPhys hash table.
1115 *
1116 * @param pPool The pool.
1117 * @param pPage The page.
1118 */
1119DECLINLINE(void) pgmPoolHashRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1120{
1121 Log3(("pgmPoolHashRemove: %RGp\n", pPage->GCPhys));
1122 uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
1123 if (pPool->aiHash[iHash] == pPage->idx)
1124 pPool->aiHash[iHash] = pPage->iNext;
1125 else
1126 {
1127 uint16_t iPrev = pPool->aiHash[iHash];
1128 for (;;)
1129 {
1130 const int16_t i = pPool->aPages[iPrev].iNext;
1131 if (i == pPage->idx)
1132 {
1133 pPool->aPages[iPrev].iNext = pPage->iNext;
1134 break;
1135 }
1136 if (i == NIL_PGMPOOL_IDX)
1137 {
1138 AssertReleaseMsgFailed(("GCPhys=%RGp idx=%#x\n", pPage->GCPhys, pPage->idx));
1139 break;
1140 }
1141 iPrev = i;
1142 }
1143 }
1144 pPage->iNext = NIL_PGMPOOL_IDX;
1145}
1146
1147
1148/**
1149 * Frees up one cache page.
1150 *
1151 * @returns VBox status code.
1152 * @retval VINF_SUCCESS on success.
1153 * @retval VERR_PGM_POOL_CLEARED if the deregistration of a physical handler will cause a light weight pool flush.
1154 * @param pPool The pool.
1155 * @param iUser The user index.
1156 */
1157static int pgmPoolCacheFreeOne(PPGMPOOL pPool, uint16_t iUser)
1158{
1159#ifndef IN_RC
1160 const PVM pVM = pPool->CTX_SUFF(pVM);
1161#endif
1162 Assert(pPool->iAgeHead != pPool->iAgeTail); /* We shouldn't be here if there < 2 cached entries! */
1163 STAM_COUNTER_INC(&pPool->StatCacheFreeUpOne);
1164
1165 /*
1166 * Select one page from the tail of the age list.
1167 */
1168 uint16_t iToFree = pPool->iAgeTail;
1169 if (iToFree == iUser)
1170 iToFree = pPool->aPages[iToFree].iAgePrev;
1171/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
1172 if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
1173 {
1174 uint16_t i = pPool->aPages[iToFree].iAgePrev;
1175 for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
1176 {
1177 if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
1178 continue;
1179 iToFree = i;
1180 break;
1181 }
1182 }
1183*/
1184
1185 Assert(iToFree != iUser);
1186 AssertRelease(iToFree != NIL_PGMPOOL_IDX);
1187
1188 PPGMPOOLPAGE pPage = &pPool->aPages[iToFree];
1189
1190 /*
1191 * Reject any attempts at flushing the currently active shadow CR3 mapping
1192 */
1193 if (PGMGetHyperCR3(pPool->CTX_SUFF(pVM)) == pPage->Core.Key)
1194 {
1195 /* Refresh the cr3 mapping by putting it at the head of the age list. */
1196 pgmPoolCacheUsed(pPool, pPage);
1197 return pgmPoolCacheFreeOne(pPool, iUser);
1198 }
1199
1200 int rc = pgmPoolFlushPage(pPool, pPage);
1201 if (rc == VINF_SUCCESS)
1202 PGM_INVL_GUEST_TLBS(); /* see PT handler. */
1203 return rc;
1204}
1205
1206
1207/**
1208 * Checks if a kind mismatch is really a page being reused
1209 * or if it's just normal remappings.
1210 *
1211 * @returns true if reused and the cached page (enmKind1) should be flushed
1212 * @returns false if not reused.
1213 * @param enmKind1 The kind of the cached page.
1214 * @param enmKind2 The kind of the requested page.
1215 */
1216static bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
1217{
1218 switch (enmKind1)
1219 {
1220 /*
1221 * Never reuse them. There is no remapping in non-paging mode.
1222 */
1223 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1224 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1225 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1226 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1227 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1228 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1229 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1230 return true;
1231
1232 /*
1233 * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
1234 */
1235 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1236 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1237 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1238 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1239 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
1240 switch (enmKind2)
1241 {
1242 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1243 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1244 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1245 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1246 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
1247 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1248 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1249 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1250 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1251 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1252 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1253 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1254 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1255 return true;
1256 default:
1257 return false;
1258 }
1259
1260 /*
1261 * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
1262 */
1263 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1264 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1265 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1266 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1267 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
1268 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1269 switch (enmKind2)
1270 {
1271 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1272 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1273 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1274 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1275 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
1276 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1277 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1278 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1279 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1280 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1281 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1282 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1283 return true;
1284 default:
1285 return false;
1286 }
1287
1288 /*
1289 * These cannot be flushed, and it's common to reuse the PDs as PTs.
1290 */
1291 case PGMPOOLKIND_ROOT_32BIT_PD:
1292 case PGMPOOLKIND_ROOT_PAE_PD:
1293 case PGMPOOLKIND_ROOT_PDPT:
1294 case PGMPOOLKIND_ROOT_NESTED:
1295 return false;
1296
1297 default:
1298 AssertFatalMsgFailed(("enmKind1=%d\n", enmKind1));
1299 }
1300}
1301
1302
1303/**
1304 * Attempts to satisfy a pgmPoolAlloc request from the cache.
1305 *
1306 * @returns VBox status code.
1307 * @retval VINF_PGM_CACHED_PAGE on success.
1308 * @retval VERR_FILE_NOT_FOUND if not found.
1309 * @param pPool The pool.
1310 * @param GCPhys The GC physical address of the page we're gonna shadow.
1311 * @param enmKind The kind of mapping.
1312 * @param iUser The shadow page pool index of the user table.
1313 * @param iUserTable The index into the user table (shadowed).
1314 * @param ppPage Where to store the pointer to the page.
1315 */
1316static int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
1317{
1318#ifndef IN_RC
1319 const PVM pVM = pPool->CTX_SUFF(pVM);
1320#endif
1321 /*
1322 * Look up the GCPhys in the hash.
1323 */
1324 unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
1325 Log3(("pgmPoolCacheAlloc: %RGp kind %d iUser=%d iUserTable=%x SLOT=%d\n", GCPhys, enmKind, iUser, iUserTable, i));
1326 if (i != NIL_PGMPOOL_IDX)
1327 {
1328 do
1329 {
1330 PPGMPOOLPAGE pPage = &pPool->aPages[i];
1331 Log3(("pgmPoolCacheAlloc: slot %d found page %RGp\n", i, pPage->GCPhys));
1332 if (pPage->GCPhys == GCPhys)
1333 {
1334 if ((PGMPOOLKIND)pPage->enmKind == enmKind)
1335 {
1336 int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
1337 if (RT_SUCCESS(rc))
1338 {
1339 *ppPage = pPage;
1340 STAM_COUNTER_INC(&pPool->StatCacheHits);
1341 return VINF_PGM_CACHED_PAGE;
1342 }
1343 return rc;
1344 }
1345
1346 /*
1347 * The kind is different. In some cases we should now flush the page
1348 * as it has been reused, but in most cases this is normal remapping
1349 * of PDs as PT or big pages using the GCPhys field in a slightly
1350 * different way than the other kinds.
1351 */
1352 if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
1353 {
1354 STAM_COUNTER_INC(&pPool->StatCacheKindMismatches);
1355 pgmPoolFlushPage(pPool, pPage); /* ASSUMES that VERR_PGM_POOL_CLEARED will be returned by pgmPoolTracInsert. */
1356 PGM_INVL_GUEST_TLBS(); /* see PT handler. */
1357 break;
1358 }
1359 }
1360
1361 /* next */
1362 i = pPage->iNext;
1363 } while (i != NIL_PGMPOOL_IDX);
1364 }
1365
1366 Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%d\n", GCPhys, enmKind));
1367 STAM_COUNTER_INC(&pPool->StatCacheMisses);
1368 return VERR_FILE_NOT_FOUND;
1369}
1370
1371
1372/**
1373 * Inserts a page into the cache.
1374 *
1375 * @param pPool The pool.
1376 * @param pPage The cached page.
1377 * @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
1378 */
1379static void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
1380{
1381 /*
1382 * Insert into the GCPhys hash if the page is fit for that.
1383 */
1384 Assert(!pPage->fCached);
1385 if (fCanBeCached)
1386 {
1387 pPage->fCached = true;
1388 pgmPoolHashInsert(pPool, pPage);
1389 Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%d, GCPhys=%RGp}\n",
1390 pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
1391 STAM_COUNTER_INC(&pPool->StatCacheCacheable);
1392 }
1393 else
1394 {
1395 Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%d, GCPhys=%RGp}\n",
1396 pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
1397 STAM_COUNTER_INC(&pPool->StatCacheUncacheable);
1398 }
1399
1400 /*
1401 * Insert at the head of the age list.
1402 */
1403 pPage->iAgePrev = NIL_PGMPOOL_IDX;
1404 pPage->iAgeNext = pPool->iAgeHead;
1405 if (pPool->iAgeHead != NIL_PGMPOOL_IDX)
1406 pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
1407 else
1408 pPool->iAgeTail = pPage->idx;
1409 pPool->iAgeHead = pPage->idx;
1410}
1411
1412
1413/**
1414 * Flushes a cached page.
1415 *
1416 * @param pPool The pool.
1417 * @param pPage The cached page.
1418 */
1419static void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1420{
1421 Log3(("pgmPoolCacheFlushPage: %RGp\n", pPage->GCPhys));
1422
1423 /*
1424 * Remove the page from the hash.
1425 */
1426 if (pPage->fCached)
1427 {
1428 pPage->fCached = false;
1429 pgmPoolHashRemove(pPool, pPage);
1430 }
1431 else
1432 Assert(pPage->iNext == NIL_PGMPOOL_IDX);
1433
1434 /*
1435 * Remove it from the age list.
1436 */
1437 if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
1438 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
1439 else
1440 pPool->iAgeTail = pPage->iAgePrev;
1441 if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
1442 pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
1443 else
1444 pPool->iAgeHead = pPage->iAgeNext;
1445 pPage->iAgeNext = NIL_PGMPOOL_IDX;
1446 pPage->iAgePrev = NIL_PGMPOOL_IDX;
1447}
1448
1449#endif /* PGMPOOL_WITH_CACHE */
1450#ifdef PGMPOOL_WITH_MONITORING
1451
1452/**
1453 * Looks for pages sharing the monitor.
1454 *
1455 * @returns Pointer to the head page.
1456 * @returns NULL if not found.
1457 * @param pPool The Pool
1458 * @param pNewPage The page which is going to be monitored.
1459 */
1460static PPGMPOOLPAGE pgmPoolMonitorGetPageByGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pNewPage)
1461{
1462#ifdef PGMPOOL_WITH_CACHE
1463 /*
1464 * Look up the GCPhys in the hash.
1465 */
1466 RTGCPHYS GCPhys = pNewPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
1467 unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
1468 if (i == NIL_PGMPOOL_IDX)
1469 return NULL;
1470 do
1471 {
1472 PPGMPOOLPAGE pPage = &pPool->aPages[i];
1473 if ( pPage->GCPhys - GCPhys < PAGE_SIZE
1474 && pPage != pNewPage)
1475 {
1476 switch (pPage->enmKind)
1477 {
1478 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1479 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1480 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1481 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
1482 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1483 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1484 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1485 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
1486 case PGMPOOLKIND_ROOT_32BIT_PD:
1487 case PGMPOOLKIND_ROOT_PAE_PD:
1488 case PGMPOOLKIND_ROOT_PDPT:
1489 {
1490 /* find the head */
1491 while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
1492 {
1493 Assert(pPage->iMonitoredPrev != pPage->idx);
1494 pPage = &pPool->aPages[pPage->iMonitoredPrev];
1495 }
1496 return pPage;
1497 }
1498
1499 /* ignore, no monitoring. */
1500 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1501 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1502 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1503 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1504 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1505 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1506 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1507 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1508 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1509 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1510 case PGMPOOLKIND_ROOT_NESTED:
1511 break;
1512 default:
1513 AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
1514 }
1515 }
1516
1517 /* next */
1518 i = pPage->iNext;
1519 } while (i != NIL_PGMPOOL_IDX);
1520#endif
1521 return NULL;
1522}
1523
1524
1525/**
1526 * Enabled write monitoring of a guest page.
1527 *
1528 * @returns VBox status code.
1529 * @retval VINF_SUCCESS on success.
1530 * @retval VERR_PGM_POOL_CLEARED if the registration of the physical handler will cause a light weight pool flush.
1531 * @param pPool The pool.
1532 * @param pPage The cached page.
1533 */
1534static int pgmPoolMonitorInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1535{
1536 LogFlow(("pgmPoolMonitorInsert %RGp\n", pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1)));
1537
1538 /*
1539 * Filter out the relevant kinds.
1540 */
1541 switch (pPage->enmKind)
1542 {
1543 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1544 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1545 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1546 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1547 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1548 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1549 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
1550 case PGMPOOLKIND_ROOT_PDPT:
1551 break;
1552
1553 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1554 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1555 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1556 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1557 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1558 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1559 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1560 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1561 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1562 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1563 case PGMPOOLKIND_ROOT_NESTED:
1564 /* Nothing to monitor here. */
1565 return VINF_SUCCESS;
1566
1567 case PGMPOOLKIND_ROOT_32BIT_PD:
1568 case PGMPOOLKIND_ROOT_PAE_PD:
1569#ifdef PGMPOOL_WITH_MIXED_PT_CR3
1570 break;
1571#endif
1572 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
1573 default:
1574 AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
1575 }
1576
1577 /*
1578 * Install handler.
1579 */
1580 int rc;
1581 PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
1582 if (pPageHead)
1583 {
1584 Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
1585 Assert(pPageHead->iMonitoredPrev != pPage->idx);
1586 pPage->iMonitoredPrev = pPageHead->idx;
1587 pPage->iMonitoredNext = pPageHead->iMonitoredNext;
1588 if (pPageHead->iMonitoredNext != NIL_PGMPOOL_IDX)
1589 pPool->aPages[pPageHead->iMonitoredNext].iMonitoredPrev = pPage->idx;
1590 pPageHead->iMonitoredNext = pPage->idx;
1591 rc = VINF_SUCCESS;
1592 }
1593 else
1594 {
1595 Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX); Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
1596 PVM pVM = pPool->CTX_SUFF(pVM);
1597 const RTGCPHYS GCPhysPage = pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
1598 rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
1599 GCPhysPage, GCPhysPage + (PAGE_SIZE - 1),
1600 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
1601 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
1602 pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
1603 pPool->pszAccessHandler);
1604 /** @todo we should probably deal with out-of-memory conditions here, but for now increasing
1605 * the heap size should suffice. */
1606 AssertFatalRC(rc);
1607 if (pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
1608 rc = VERR_PGM_POOL_CLEARED;
1609 }
1610 pPage->fMonitored = true;
1611 return rc;
1612}
1613
1614
1615/**
1616 * Disables write monitoring of a guest page.
1617 *
1618 * @returns VBox status code.
1619 * @retval VINF_SUCCESS on success.
1620 * @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
1621 * @param pPool The pool.
1622 * @param pPage The cached page.
1623 */
1624static int pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1625{
1626 /*
1627 * Filter out the relevant kinds.
1628 */
1629 switch (pPage->enmKind)
1630 {
1631 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1632 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1633 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1634 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1635 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1636 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1637 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
1638 case PGMPOOLKIND_ROOT_PDPT:
1639 break;
1640
1641 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1642 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1643 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1644 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1645 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1646 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1647 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1648 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1649 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1650 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1651 case PGMPOOLKIND_ROOT_NESTED:
1652 /* Nothing to monitor here. */
1653 return VINF_SUCCESS;
1654
1655 case PGMPOOLKIND_ROOT_32BIT_PD:
1656 case PGMPOOLKIND_ROOT_PAE_PD:
1657#ifdef PGMPOOL_WITH_MIXED_PT_CR3
1658 break;
1659#endif
1660 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
1661 default:
1662 AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
1663 }
1664
1665 /*
1666 * Remove the page from the monitored list or uninstall it if last.
1667 */
1668 const PVM pVM = pPool->CTX_SUFF(pVM);
1669 int rc;
1670 if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
1671 || pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
1672 {
1673 if (pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
1674 {
1675 PPGMPOOLPAGE pNewHead = &pPool->aPages[pPage->iMonitoredNext];
1676 pNewHead->iMonitoredPrev = NIL_PGMPOOL_IDX;
1677 pNewHead->fCR3Mix = pPage->fCR3Mix;
1678 rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
1679 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pNewHead),
1680 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pNewHead),
1681 pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pNewHead),
1682 pPool->pszAccessHandler);
1683 AssertFatalRCSuccess(rc);
1684 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
1685 }
1686 else
1687 {
1688 pPool->aPages[pPage->iMonitoredPrev].iMonitoredNext = pPage->iMonitoredNext;
1689 if (pPage->iMonitoredNext != NIL_PGMPOOL_IDX)
1690 {
1691 pPool->aPages[pPage->iMonitoredNext].iMonitoredPrev = pPage->iMonitoredPrev;
1692 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
1693 }
1694 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
1695 rc = VINF_SUCCESS;
1696 }
1697 }
1698 else
1699 {
1700 rc = PGMHandlerPhysicalDeregister(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1));
1701 AssertFatalRC(rc);
1702 if (pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
1703 rc = VERR_PGM_POOL_CLEARED;
1704 }
1705 pPage->fMonitored = false;
1706
1707 /*
1708 * Remove it from the list of modified pages (if in it).
1709 */
1710 pgmPoolMonitorModifiedRemove(pPool, pPage);
1711
1712 return rc;
1713}
1714
1715# ifdef PGMPOOL_WITH_MIXED_PT_CR3
1716
1717/**
1718 * Set or clear the fCR3Mix attribute in a chain of monitored pages.
1719 *
1720 * @param pPool The Pool.
1721 * @param pPage A page in the chain.
1722 * @param fCR3Mix The new fCR3Mix value.
1723 */
1724static void pgmPoolMonitorChainChangeCR3Mix(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCR3Mix)
1725{
1726 /* current */
1727 pPage->fCR3Mix = fCR3Mix;
1728
1729 /* before */
1730 int16_t idx = pPage->iMonitoredPrev;
1731 while (idx != NIL_PGMPOOL_IDX)
1732 {
1733 pPool->aPages[idx].fCR3Mix = fCR3Mix;
1734 idx = pPool->aPages[idx].iMonitoredPrev;
1735 }
1736
1737 /* after */
1738 idx = pPage->iMonitoredNext;
1739 while (idx != NIL_PGMPOOL_IDX)
1740 {
1741 pPool->aPages[idx].fCR3Mix = fCR3Mix;
1742 idx = pPool->aPages[idx].iMonitoredNext;
1743 }
1744}
1745
1746
1747/**
1748 * Installs or modifies monitoring of a CR3 page (special).
1749 *
1750 * We're pretending the CR3 page is shadowed by the pool so we can use the
1751 * generic mechanisms in detecting chained monitoring. (This also gives us a
1752 * tast of what code changes are required to really pool CR3 shadow pages.)
1753 *
1754 * @returns VBox status code.
1755 * @param pPool The pool.
1756 * @param idxRoot The CR3 (root) page index.
1757 * @param GCPhysCR3 The (new) CR3 value.
1758 */
1759int pgmPoolMonitorMonitorCR3(PPGMPOOL pPool, uint16_t idxRoot, RTGCPHYS GCPhysCR3)
1760{
1761 Assert(idxRoot != NIL_PGMPOOL_IDX && idxRoot < PGMPOOL_IDX_FIRST);
1762 PPGMPOOLPAGE pPage = &pPool->aPages[idxRoot];
1763 LogFlow(("pgmPoolMonitorMonitorCR3: idxRoot=%d pPage=%p:{.GCPhys=%RGp, .fMonitored=%d} GCPhysCR3=%RGp\n",
1764 idxRoot, pPage, pPage->GCPhys, pPage->fMonitored, GCPhysCR3));
1765
1766 /*
1767 * The unlikely case where it already matches.
1768 */
1769 if (pPage->GCPhys == GCPhysCR3)
1770 {
1771 Assert(pPage->fMonitored);
1772 return VINF_SUCCESS;
1773 }
1774
1775 /*
1776 * Flush the current monitoring and remove it from the hash.
1777 */
1778 int rc = VINF_SUCCESS;
1779 if (pPage->fMonitored)
1780 {
1781 pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, false);
1782 rc = pgmPoolMonitorFlush(pPool, pPage);
1783 if (rc == VERR_PGM_POOL_CLEARED)
1784 rc = VINF_SUCCESS;
1785 else
1786 AssertFatalRC(rc);
1787 pgmPoolHashRemove(pPool, pPage);
1788 }
1789
1790 /*
1791 * Monitor the page at the new location and insert it into the hash.
1792 */
1793 pPage->GCPhys = GCPhysCR3;
1794 int rc2 = pgmPoolMonitorInsert(pPool, pPage);
1795 if (rc2 != VERR_PGM_POOL_CLEARED)
1796 {
1797 AssertFatalRC(rc2);
1798 if (rc2 != VINF_SUCCESS && rc == VINF_SUCCESS)
1799 rc = rc2;
1800 }
1801 pgmPoolHashInsert(pPool, pPage);
1802 pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, true);
1803 return rc;
1804}
1805
1806
1807/**
1808 * Removes the monitoring of a CR3 page (special).
1809 *
1810 * @returns VBox status code.
1811 * @param pPool The pool.
1812 * @param idxRoot The CR3 (root) page index.
1813 */
1814int pgmPoolMonitorUnmonitorCR3(PPGMPOOL pPool, uint16_t idxRoot)
1815{
1816 Assert(idxRoot != NIL_PGMPOOL_IDX && idxRoot < PGMPOOL_IDX_FIRST);
1817 PPGMPOOLPAGE pPage = &pPool->aPages[idxRoot];
1818 LogFlow(("pgmPoolMonitorUnmonitorCR3: idxRoot=%d pPage=%p:{.GCPhys=%RGp, .fMonitored=%d}\n",
1819 idxRoot, pPage, pPage->GCPhys, pPage->fMonitored));
1820
1821 if (!pPage->fMonitored)
1822 return VINF_SUCCESS;
1823
1824 pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, false);
1825 int rc = pgmPoolMonitorFlush(pPool, pPage);
1826 if (rc != VERR_PGM_POOL_CLEARED)
1827 AssertFatalRC(rc);
1828 else
1829 rc = VINF_SUCCESS;
1830 pgmPoolHashRemove(pPool, pPage);
1831 Assert(!pPage->fMonitored);
1832 pPage->GCPhys = NIL_RTGCPHYS;
1833 return rc;
1834}
1835
1836# endif /* PGMPOOL_WITH_MIXED_PT_CR3 */
1837
1838/**
1839 * Inserts the page into the list of modified pages.
1840 *
1841 * @param pPool The pool.
1842 * @param pPage The page.
1843 */
1844void pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1845{
1846 Log3(("pgmPoolMonitorModifiedInsert: idx=%d\n", pPage->idx));
1847 AssertMsg( pPage->iModifiedNext == NIL_PGMPOOL_IDX
1848 && pPage->iModifiedPrev == NIL_PGMPOOL_IDX
1849 && pPool->iModifiedHead != pPage->idx,
1850 ("Next=%d Prev=%d idx=%d cModifications=%d Head=%d cModifiedPages=%d\n",
1851 pPage->iModifiedNext, pPage->iModifiedPrev, pPage->idx, pPage->cModifications,
1852 pPool->iModifiedHead, pPool->cModifiedPages));
1853
1854 pPage->iModifiedNext = pPool->iModifiedHead;
1855 if (pPool->iModifiedHead != NIL_PGMPOOL_IDX)
1856 pPool->aPages[pPool->iModifiedHead].iModifiedPrev = pPage->idx;
1857 pPool->iModifiedHead = pPage->idx;
1858 pPool->cModifiedPages++;
1859#ifdef VBOX_WITH_STATISTICS
1860 if (pPool->cModifiedPages > pPool->cModifiedPagesHigh)
1861 pPool->cModifiedPagesHigh = pPool->cModifiedPages;
1862#endif
1863}
1864
1865
1866/**
1867 * Removes the page from the list of modified pages and resets the
1868 * moficiation counter.
1869 *
1870 * @param pPool The pool.
1871 * @param pPage The page which is believed to be in the list of modified pages.
1872 */
1873static void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1874{
1875 Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
1876 if (pPool->iModifiedHead == pPage->idx)
1877 {
1878 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
1879 pPool->iModifiedHead = pPage->iModifiedNext;
1880 if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
1881 {
1882 pPool->aPages[pPage->iModifiedNext].iModifiedPrev = NIL_PGMPOOL_IDX;
1883 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1884 }
1885 pPool->cModifiedPages--;
1886 }
1887 else if (pPage->iModifiedPrev != NIL_PGMPOOL_IDX)
1888 {
1889 pPool->aPages[pPage->iModifiedPrev].iModifiedNext = pPage->iModifiedNext;
1890 if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
1891 {
1892 pPool->aPages[pPage->iModifiedNext].iModifiedPrev = pPage->iModifiedPrev;
1893 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1894 }
1895 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
1896 pPool->cModifiedPages--;
1897 }
1898 else
1899 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
1900 pPage->cModifications = 0;
1901}
1902
1903
1904/**
1905 * Zaps the list of modified pages, resetting their modification counters in the process.
1906 *
1907 * @param pVM The VM handle.
1908 */
1909void pgmPoolMonitorModifiedClearAll(PVM pVM)
1910{
1911 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1912 LogFlow(("pgmPoolMonitorModifiedClearAll: cModifiedPages=%d\n", pPool->cModifiedPages));
1913
1914 unsigned cPages = 0; NOREF(cPages);
1915 uint16_t idx = pPool->iModifiedHead;
1916 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
1917 while (idx != NIL_PGMPOOL_IDX)
1918 {
1919 PPGMPOOLPAGE pPage = &pPool->aPages[idx];
1920 idx = pPage->iModifiedNext;
1921 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1922 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
1923 pPage->cModifications = 0;
1924 Assert(++cPages);
1925 }
1926 AssertMsg(cPages == pPool->cModifiedPages, ("%d != %d\n", cPages, pPool->cModifiedPages));
1927 pPool->cModifiedPages = 0;
1928}
1929
1930
1931/**
1932 * Clear all shadow pages and clear all modification counters.
1933 *
1934 * @param pVM The VM handle.
1935 * @remark Should only be used when monitoring is available, thus placed in
1936 * the PGMPOOL_WITH_MONITORING #ifdef.
1937 */
1938void pgmPoolClearAll(PVM pVM)
1939{
1940 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1941 STAM_PROFILE_START(&pPool->StatClearAll, c);
1942 LogFlow(("pgmPoolClearAll: cUsedPages=%d\n", pPool->cUsedPages));
1943
1944 /*
1945 * Iterate all the pages until we've encountered all that in use.
1946 * This is simple but not quite optimal solution.
1947 */
1948 unsigned cModifiedPages = 0; NOREF(cModifiedPages);
1949 unsigned cLeft = pPool->cUsedPages;
1950 unsigned iPage = pPool->cCurPages;
1951 while (--iPage >= PGMPOOL_IDX_FIRST)
1952 {
1953 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
1954 if (pPage->GCPhys != NIL_RTGCPHYS)
1955 {
1956 switch (pPage->enmKind)
1957 {
1958 /*
1959 * We only care about shadow page tables.
1960 */
1961 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1962 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1963 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1964 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1965 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1966 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1967 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1968 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1969 {
1970#ifdef PGMPOOL_WITH_USER_TRACKING
1971 if (pPage->cPresent)
1972#endif
1973 {
1974 void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
1975 STAM_PROFILE_START(&pPool->StatZeroPage, z);
1976 ASMMemZeroPage(pvShw);
1977 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
1978#ifdef PGMPOOL_WITH_USER_TRACKING
1979 pPage->cPresent = 0;
1980 pPage->iFirstPresent = ~0;
1981#endif
1982 }
1983 }
1984 /* fall thru */
1985
1986 default:
1987 Assert(!pPage->cModifications || ++cModifiedPages);
1988 Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
1989 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
1990 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1991 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
1992 pPage->cModifications = 0;
1993 break;
1994
1995 }
1996 if (!--cLeft)
1997 break;
1998 }
1999 }
2000
2001 /* swipe the special pages too. */
2002 for (iPage = PGMPOOL_IDX_FIRST_SPECIAL; iPage < PGMPOOL_IDX_FIRST; iPage++)
2003 {
2004 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
2005 if (pPage->GCPhys != NIL_RTGCPHYS)
2006 {
2007 Assert(!pPage->cModifications || ++cModifiedPages);
2008 Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
2009 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
2010 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
2011 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
2012 pPage->cModifications = 0;
2013 }
2014 }
2015
2016#ifndef DEBUG_michael
2017 AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
2018#endif
2019 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
2020 pPool->cModifiedPages = 0;
2021
2022#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2023 /*
2024 * Clear all the GCPhys links and rebuild the phys ext free list.
2025 */
2026 for (PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
2027 pRam;
2028 pRam = pRam->CTX_SUFF(pNext))
2029 {
2030 unsigned iPage = pRam->cb >> PAGE_SHIFT;
2031 while (iPage-- > 0)
2032 pRam->aPages[iPage].HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
2033 }
2034
2035 pPool->iPhysExtFreeHead = 0;
2036 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
2037 const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
2038 for (unsigned i = 0; i < cMaxPhysExts; i++)
2039 {
2040 paPhysExts[i].iNext = i + 1;
2041 paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
2042 paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
2043 paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
2044 }
2045 paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
2046#endif
2047
2048
2049 pPool->cPresent = 0;
2050 STAM_PROFILE_STOP(&pPool->StatClearAll, c);
2051}
2052
2053
2054/**
2055 * Handle SyncCR3 pool tasks
2056 *
2057 * @returns VBox status code.
2058 * @retval VINF_SUCCESS if successfully added.
2059 * @retval VINF_PGM_SYNC_CR3 is it needs to be deferred to ring 3 (GC only)
2060 * @param pVM The VM handle.
2061 * @remark Should only be used when monitoring is available, thus placed in
2062 * the PGMPOOL_WITH_MONITORING #ifdef.
2063 */
2064int pgmPoolSyncCR3(PVM pVM)
2065{
2066 /*
2067 * When monitoring shadowed pages, we reset the modification counters on CR3 sync.
2068 * Occasionally we will have to clear all the shadow page tables because we wanted
2069 * to monitor a page which was mapped by too many shadowed page tables. This operation
2070 * sometimes refered to as a 'lightweight flush'.
2071 */
2072 if (!(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL))
2073 pgmPoolMonitorModifiedClearAll(pVM);
2074 else
2075 {
2076# ifndef IN_RC
2077 pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_CLEAR_PGM_POOL;
2078 pgmPoolClearAll(pVM);
2079# else
2080 LogFlow(("SyncCR3: PGM_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
2081 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3); /** @todo no need to do global sync, right? */
2082 return VINF_PGM_SYNC_CR3;
2083# endif
2084 }
2085 return VINF_SUCCESS;
2086}
2087
2088#endif /* PGMPOOL_WITH_MONITORING */
2089#ifdef PGMPOOL_WITH_USER_TRACKING
2090
2091/**
2092 * Frees up at least one user entry.
2093 *
2094 * @returns VBox status code.
2095 * @retval VINF_SUCCESS if successfully added.
2096 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
2097 * @param pPool The pool.
2098 * @param iUser The user index.
2099 */
2100static int pgmPoolTrackFreeOneUser(PPGMPOOL pPool, uint16_t iUser)
2101{
2102 STAM_COUNTER_INC(&pPool->StatTrackFreeUpOneUser);
2103#ifdef PGMPOOL_WITH_CACHE
2104 /*
2105 * Just free cached pages in a braindead fashion.
2106 */
2107 /** @todo walk the age list backwards and free the first with usage. */
2108 int rc = VINF_SUCCESS;
2109 do
2110 {
2111 int rc2 = pgmPoolCacheFreeOne(pPool, iUser);
2112 if (RT_FAILURE(rc2) && rc == VINF_SUCCESS)
2113 rc = rc2;
2114 } while (pPool->iUserFreeHead == NIL_PGMPOOL_USER_INDEX);
2115 return rc;
2116#else
2117 /*
2118 * Lazy approach.
2119 */
2120 /* @todo incompatible with long mode paging (cr3 root will be flushed) */
2121 Assert(!CPUMIsGuestInLongMode(pVM));
2122 pgmPoolFlushAllInt(pPool);
2123 return VERR_PGM_POOL_FLUSHED;
2124#endif
2125}
2126
2127
2128/**
2129 * Inserts a page into the cache.
2130 *
2131 * This will create user node for the page, insert it into the GCPhys
2132 * hash, and insert it into the age list.
2133 *
2134 * @returns VBox status code.
2135 * @retval VINF_SUCCESS if successfully added.
2136 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
2137 * @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
2138 * @param pPool The pool.
2139 * @param pPage The cached page.
2140 * @param GCPhys The GC physical address of the page we're gonna shadow.
2141 * @param iUser The user index.
2142 * @param iUserTable The user table index.
2143 */
2144DECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
2145{
2146 int rc = VINF_SUCCESS;
2147 PPGMPOOLUSER pUser = pPool->CTX_SUFF(paUsers);
2148
2149 LogFlow(("pgmPoolTrackInsert iUser %d iUserTable %d\n", iUser, iUserTable));
2150
2151 /*
2152 * Find free a user node.
2153 */
2154 uint16_t i = pPool->iUserFreeHead;
2155 if (i == NIL_PGMPOOL_USER_INDEX)
2156 {
2157 int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
2158 if (RT_FAILURE(rc))
2159 return rc;
2160 i = pPool->iUserFreeHead;
2161 }
2162
2163 /*
2164 * Unlink the user node from the free list,
2165 * initialize and insert it into the user list.
2166 */
2167 pPool->iUserFreeHead = pUser[i].iNext;
2168 pUser[i].iNext = NIL_PGMPOOL_USER_INDEX;
2169 pUser[i].iUser = iUser;
2170 pUser[i].iUserTable = iUserTable;
2171 pPage->iUserHead = i;
2172
2173 /*
2174 * Insert into cache and enable monitoring of the guest page if enabled.
2175 *
2176 * Until we implement caching of all levels, including the CR3 one, we'll
2177 * have to make sure we don't try monitor & cache any recursive reuse of
2178 * a monitored CR3 page. Because all windows versions are doing this we'll
2179 * have to be able to do combined access monitoring, CR3 + PT and
2180 * PD + PT (guest PAE).
2181 *
2182 * Update:
2183 * We're now cooperating with the CR3 monitor if an uncachable page is found.
2184 */
2185#if defined(PGMPOOL_WITH_MONITORING) || defined(PGMPOOL_WITH_CACHE)
2186# ifdef PGMPOOL_WITH_MIXED_PT_CR3
2187 const bool fCanBeMonitored = true;
2188# else
2189 bool fCanBeMonitored = pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored == NIL_RTGCPHYS
2190 || (GCPhys & X86_PTE_PAE_PG_MASK) != (pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored & X86_PTE_PAE_PG_MASK)
2191 || pgmPoolIsBigPage((PGMPOOLKIND)pPage->enmKind);
2192# endif
2193# ifdef PGMPOOL_WITH_CACHE
2194 pgmPoolCacheInsert(pPool, pPage, fCanBeMonitored); /* This can be expanded. */
2195# endif
2196 if (fCanBeMonitored)
2197 {
2198# ifdef PGMPOOL_WITH_MONITORING
2199 rc = pgmPoolMonitorInsert(pPool, pPage);
2200 if (rc == VERR_PGM_POOL_CLEARED)
2201 {
2202 /* 'Failed' - free the usage, and keep it in the cache (if enabled). */
2203# ifndef PGMPOOL_WITH_CACHE
2204 pgmPoolMonitorFlush(pPool, pPage);
2205 rc = VERR_PGM_POOL_FLUSHED;
2206# endif
2207 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
2208 pUser[i].iNext = pPool->iUserFreeHead;
2209 pUser[i].iUser = NIL_PGMPOOL_IDX;
2210 pPool->iUserFreeHead = i;
2211 }
2212 }
2213# endif
2214#endif /* PGMPOOL_WITH_MONITORING */
2215 return rc;
2216}
2217
2218
2219# ifdef PGMPOOL_WITH_CACHE /* (only used when the cache is enabled.) */
2220/**
2221 * Adds a user reference to a page.
2222 *
2223 * This will
2224 * This will move the page to the head of the
2225 *
2226 * @returns VBox status code.
2227 * @retval VINF_SUCCESS if successfully added.
2228 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
2229 * @param pPool The pool.
2230 * @param pPage The cached page.
2231 * @param iUser The user index.
2232 * @param iUserTable The user table.
2233 */
2234static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
2235{
2236 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
2237
2238 LogFlow(("pgmPoolTrackAddUser iUser %d iUserTable %d\n", iUser, iUserTable));
2239# ifdef VBOX_STRICT
2240 /*
2241 * Check that the entry doesn't already exists.
2242 */
2243 if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
2244 {
2245 uint16_t i = pPage->iUserHead;
2246 do
2247 {
2248 Assert(i < pPool->cMaxUsers);
2249 AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
2250 i = paUsers[i].iNext;
2251 } while (i != NIL_PGMPOOL_USER_INDEX);
2252 }
2253# endif
2254
2255 /*
2256 * Allocate a user node.
2257 */
2258 uint16_t i = pPool->iUserFreeHead;
2259 if (i == NIL_PGMPOOL_USER_INDEX)
2260 {
2261 int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
2262 if (RT_FAILURE(rc))
2263 return rc;
2264 i = pPool->iUserFreeHead;
2265 }
2266 pPool->iUserFreeHead = paUsers[i].iNext;
2267
2268 /*
2269 * Initialize the user node and insert it.
2270 */
2271 paUsers[i].iNext = pPage->iUserHead;
2272 paUsers[i].iUser = iUser;
2273 paUsers[i].iUserTable = iUserTable;
2274 pPage->iUserHead = i;
2275
2276# ifdef PGMPOOL_WITH_CACHE
2277 /*
2278 * Tell the cache to update its replacement stats for this page.
2279 */
2280 pgmPoolCacheUsed(pPool, pPage);
2281# endif
2282 return VINF_SUCCESS;
2283}
2284# endif /* PGMPOOL_WITH_CACHE */
2285
2286
2287/**
2288 * Frees a user record associated with a page.
2289 *
2290 * This does not clear the entry in the user table, it simply replaces the
2291 * user record to the chain of free records.
2292 *
2293 * @param pPool The pool.
2294 * @param HCPhys The HC physical address of the shadow page.
2295 * @param iUser The shadow page pool index of the user table.
2296 * @param iUserTable The index into the user table (shadowed).
2297 */
2298static void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
2299{
2300 /*
2301 * Unlink and free the specified user entry.
2302 */
2303 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
2304
2305 /* Special: For PAE and 32-bit paging, there is usually no more than one user. */
2306 uint16_t i = pPage->iUserHead;
2307 if ( i != NIL_PGMPOOL_USER_INDEX
2308 && paUsers[i].iUser == iUser
2309 && paUsers[i].iUserTable == iUserTable)
2310 {
2311 pPage->iUserHead = paUsers[i].iNext;
2312
2313 paUsers[i].iUser = NIL_PGMPOOL_IDX;
2314 paUsers[i].iNext = pPool->iUserFreeHead;
2315 pPool->iUserFreeHead = i;
2316 return;
2317 }
2318
2319 /* General: Linear search. */
2320 uint16_t iPrev = NIL_PGMPOOL_USER_INDEX;
2321 while (i != NIL_PGMPOOL_USER_INDEX)
2322 {
2323 if ( paUsers[i].iUser == iUser
2324 && paUsers[i].iUserTable == iUserTable)
2325 {
2326 if (iPrev != NIL_PGMPOOL_USER_INDEX)
2327 paUsers[iPrev].iNext = paUsers[i].iNext;
2328 else
2329 pPage->iUserHead = paUsers[i].iNext;
2330
2331 paUsers[i].iUser = NIL_PGMPOOL_IDX;
2332 paUsers[i].iNext = pPool->iUserFreeHead;
2333 pPool->iUserFreeHead = i;
2334 return;
2335 }
2336 iPrev = i;
2337 i = paUsers[i].iNext;
2338 }
2339
2340 /* Fatal: didn't find it */
2341 AssertFatalMsgFailed(("Didn't find the user entry! iUser=%#x iUserTable=%#x GCPhys=%RGp\n",
2342 iUser, iUserTable, pPage->GCPhys));
2343}
2344
2345
2346/**
2347 * Gets the entry size of a shadow table.
2348 *
2349 * @param enmKind The kind of page.
2350 *
2351 * @returns The size of the entry in bytes. That is, 4 or 8.
2352 * @returns If the kind is not for a table, an assertion is raised and 0 is
2353 * returned.
2354 */
2355DECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind)
2356{
2357 switch (enmKind)
2358 {
2359 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2360 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2361 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2362 case PGMPOOLKIND_ROOT_32BIT_PD:
2363 return 4;
2364
2365 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2366 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2367 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2368 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2369 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2370 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
2371 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2372 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2373 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2374 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
2375 case PGMPOOLKIND_ROOT_PAE_PD:
2376 case PGMPOOLKIND_ROOT_PDPT:
2377 case PGMPOOLKIND_ROOT_NESTED:
2378 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2379 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2380 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2381 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2382 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
2383 return 8;
2384
2385 default:
2386 AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
2387 }
2388}
2389
2390
2391/**
2392 * Gets the entry size of a guest table.
2393 *
2394 * @param enmKind The kind of page.
2395 *
2396 * @returns The size of the entry in bytes. That is, 0, 4 or 8.
2397 * @returns If the kind is not for a table, an assertion is raised and 0 is
2398 * returned.
2399 */
2400DECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind)
2401{
2402 switch (enmKind)
2403 {
2404 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2405 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2406 case PGMPOOLKIND_ROOT_32BIT_PD:
2407 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2408 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2409 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
2410 return 4;
2411
2412 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2413 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2414 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2415 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2416 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2417 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
2418 case PGMPOOLKIND_ROOT_PAE_PD:
2419 case PGMPOOLKIND_ROOT_PDPT:
2420 return 8;
2421
2422 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2423 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2424 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2425 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2426 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2427 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2428 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
2429 case PGMPOOLKIND_ROOT_NESTED:
2430 /** @todo can we return 0? (nobody is calling this...) */
2431 AssertFailed();
2432 return 0;
2433
2434 default:
2435 AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
2436 }
2437}
2438
2439#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2440
2441/**
2442 * Scans one shadow page table for mappings of a physical page.
2443 *
2444 * @param pVM The VM handle.
2445 * @param pPhysPage The guest page in question.
2446 * @param iShw The shadow page table.
2447 * @param cRefs The number of references made in that PT.
2448 */
2449static void pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
2450{
2451 LogFlow(("pgmPoolTrackFlushGCPhysPT: HCPhys=%RHp iShw=%d cRefs=%d\n", pPhysPage->HCPhys, iShw, cRefs));
2452 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2453
2454 /*
2455 * Assert sanity.
2456 */
2457 Assert(cRefs == 1);
2458 AssertFatalMsg(iShw < pPool->cCurPages && iShw != NIL_PGMPOOL_IDX, ("iShw=%d\n", iShw));
2459 PPGMPOOLPAGE pPage = &pPool->aPages[iShw];
2460
2461 /*
2462 * Then, clear the actual mappings to the page in the shadow PT.
2463 */
2464 switch (pPage->enmKind)
2465 {
2466 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2467 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2468 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2469 {
2470 const uint32_t u32 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2471 PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2472 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2473 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2474 {
2475 Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX32 cRefs=%#x\n", i, pPT->a[i], cRefs));
2476 pPT->a[i].u = 0;
2477 cRefs--;
2478 if (!cRefs)
2479 return;
2480 }
2481#ifdef LOG_ENABLED
2482 RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
2483 for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
2484 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2485 {
2486 RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
2487 pPT->a[i].u = 0;
2488 }
2489#endif
2490 AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
2491 break;
2492 }
2493
2494 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2495 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2496 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2497 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2498 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2499 {
2500 const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2501 PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2502 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2503 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
2504 {
2505 Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
2506 pPT->a[i].u = 0;
2507 cRefs--;
2508 if (!cRefs)
2509 return;
2510 }
2511#ifdef LOG_ENABLED
2512 RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
2513 for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
2514 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
2515 {
2516 RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
2517 pPT->a[i].u = 0;
2518 }
2519#endif
2520 AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d u64=%RX64\n", cRefs, pPage->iFirstPresent, pPage->cPresent, u64));
2521 break;
2522 }
2523
2524 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
2525 {
2526 const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2527 PEPTPT pPT = (PEPTPT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2528 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2529 if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
2530 {
2531 Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
2532 pPT->a[i].u = 0;
2533 cRefs--;
2534 if (!cRefs)
2535 return;
2536 }
2537#ifdef LOG_ENABLED
2538 RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
2539 for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
2540 if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
2541 {
2542 RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
2543 pPT->a[i].u = 0;
2544 }
2545#endif
2546 AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
2547 break;
2548 }
2549
2550 default:
2551 AssertFatalMsgFailed(("enmKind=%d iShw=%d\n", pPage->enmKind, iShw));
2552 }
2553}
2554
2555
2556/**
2557 * Scans one shadow page table for mappings of a physical page.
2558 *
2559 * @param pVM The VM handle.
2560 * @param pPhysPage The guest page in question.
2561 * @param iShw The shadow page table.
2562 * @param cRefs The number of references made in that PT.
2563 */
2564void pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
2565{
2566 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
2567 LogFlow(("pgmPoolTrackFlushGCPhysPT: HCPhys=%RHp iShw=%d cRefs=%d\n", pPhysPage->HCPhys, iShw, cRefs));
2568 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPT, f);
2569 pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, iShw, cRefs);
2570 pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
2571 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPT, f);
2572}
2573
2574
2575/**
2576 * Flushes a list of shadow page tables mapping the same physical page.
2577 *
2578 * @param pVM The VM handle.
2579 * @param pPhysPage The guest page in question.
2580 * @param iPhysExt The physical cross reference extent list to flush.
2581 */
2582void pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt)
2583{
2584 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2585 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTs, f);
2586 LogFlow(("pgmPoolTrackFlushGCPhysPTs: HCPhys=%RHp iPhysExt\n", pPhysPage->HCPhys, iPhysExt));
2587
2588 const uint16_t iPhysExtStart = iPhysExt;
2589 PPGMPOOLPHYSEXT pPhysExt;
2590 do
2591 {
2592 Assert(iPhysExt < pPool->cMaxPhysExts);
2593 pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
2594 for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
2595 if (pPhysExt->aidx[i] != NIL_PGMPOOL_IDX)
2596 {
2597 pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, pPhysExt->aidx[i], 1);
2598 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
2599 }
2600
2601 /* next */
2602 iPhysExt = pPhysExt->iNext;
2603 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
2604
2605 /* insert the list into the free list and clear the ram range entry. */
2606 pPhysExt->iNext = pPool->iPhysExtFreeHead;
2607 pPool->iPhysExtFreeHead = iPhysExtStart;
2608 pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
2609
2610 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTs, f);
2611}
2612
2613#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
2614
2615/**
2616 * Scans all shadow page tables for mappings of a physical page.
2617 *
2618 * This may be slow, but it's most likely more efficient than cleaning
2619 * out the entire page pool / cache.
2620 *
2621 * @returns VBox status code.
2622 * @retval VINF_SUCCESS if all references has been successfully cleared.
2623 * @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
2624 * a page pool cleaning.
2625 *
2626 * @param pVM The VM handle.
2627 * @param pPhysPage The guest page in question.
2628 */
2629int pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage)
2630{
2631 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2632 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTsSlow, s);
2633 LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d HCPhys=%RHp\n",
2634 pPool->cUsedPages, pPool->cPresent, pPhysPage->HCPhys));
2635
2636#if 1
2637 /*
2638 * There is a limit to what makes sense.
2639 */
2640 if (pPool->cPresent > 1024)
2641 {
2642 LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: giving up... (cPresent=%d)\n", pPool->cPresent));
2643 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
2644 return VINF_PGM_GCPHYS_ALIASED;
2645 }
2646#endif
2647
2648 /*
2649 * Iterate all the pages until we've encountered all that in use.
2650 * This is simple but not quite optimal solution.
2651 */
2652 const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2653 const uint32_t u32 = u64;
2654 unsigned cLeft = pPool->cUsedPages;
2655 unsigned iPage = pPool->cCurPages;
2656 while (--iPage >= PGMPOOL_IDX_FIRST)
2657 {
2658 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
2659 if (pPage->GCPhys != NIL_RTGCPHYS)
2660 {
2661 switch (pPage->enmKind)
2662 {
2663 /*
2664 * We only care about shadow page tables.
2665 */
2666 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2667 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2668 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2669 {
2670 unsigned cPresent = pPage->cPresent;
2671 PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2672 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2673 if (pPT->a[i].n.u1Present)
2674 {
2675 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2676 {
2677 //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX32\n", iPage, i, pPT->a[i]));
2678 pPT->a[i].u = 0;
2679 }
2680 if (!--cPresent)
2681 break;
2682 }
2683 break;
2684 }
2685
2686 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2687 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2688 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2689 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2690 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2691 {
2692 unsigned cPresent = pPage->cPresent;
2693 PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2694 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2695 if (pPT->a[i].n.u1Present)
2696 {
2697 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
2698 {
2699 //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
2700 pPT->a[i].u = 0;
2701 }
2702 if (!--cPresent)
2703 break;
2704 }
2705 break;
2706 }
2707 }
2708 if (!--cLeft)
2709 break;
2710 }
2711 }
2712
2713 pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
2714 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
2715 return VINF_SUCCESS;
2716}
2717
2718
2719/**
2720 * Clears the user entry in a user table.
2721 *
2722 * This is used to remove all references to a page when flushing it.
2723 */
2724static void pgmPoolTrackClearPageUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PCPGMPOOLUSER pUser)
2725{
2726 Assert(pUser->iUser != NIL_PGMPOOL_IDX);
2727 Assert(pUser->iUser < pPool->cCurPages);
2728 uint32_t iUserTable = pUser->iUserTable;
2729
2730 /*
2731 * Map the user page.
2732 */
2733 PPGMPOOLPAGE pUserPage = &pPool->aPages[pUser->iUser];
2734#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2735 if (pUserPage->enmKind == PGMPOOLKIND_ROOT_PAE_PD)
2736 {
2737 /* Must translate the fake 2048 entry PD to a 512 PD one since the R0 mapping is not linear. */
2738 Assert(pUser->iUser == PGMPOOL_IDX_PAE_PD);
2739 uint32_t iPdpt = iUserTable / X86_PG_PAE_ENTRIES;
2740 iUserTable %= X86_PG_PAE_ENTRIES;
2741 pUserPage = &pPool->aPages[PGMPOOL_IDX_PAE_PD_0 + iPdpt];
2742 Assert(pUserPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD);
2743 }
2744#endif
2745 union
2746 {
2747 uint64_t *pau64;
2748 uint32_t *pau32;
2749 } u;
2750 u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pUserPage);
2751
2752 /* Safety precaution in case we change the paging for other modes too in the future. */
2753 Assert(PGMGetHyperCR3(pPool->CTX_SUFF(pVM)) != pPage->Core.Key);
2754
2755#ifdef VBOX_STRICT
2756 /*
2757 * Some sanity checks.
2758 */
2759 switch (pUserPage->enmKind)
2760 {
2761 case PGMPOOLKIND_ROOT_32BIT_PD:
2762 Assert(iUserTable < X86_PG_ENTRIES);
2763 Assert(!(u.pau32[iUserTable] & PGM_PDFLAGS_MAPPING));
2764 break;
2765# ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2766 case PGMPOOLKIND_ROOT_PAE_PD:
2767 Assert(iUserTable < 2048 && pUser->iUser == PGMPOOL_IDX_PAE_PD);
2768 AssertMsg(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING), ("%llx %d\n", u.pau64[iUserTable], iUserTable));
2769 break;
2770# endif
2771 case PGMPOOLKIND_ROOT_PDPT:
2772 Assert(iUserTable < 4);
2773 Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
2774 break;
2775 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
2776 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2777 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2778 break;
2779 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2780 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2781 Assert(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING));
2782 break;
2783 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2784 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2785 Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
2786 break;
2787 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
2788 Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
2789 /* GCPhys >> PAGE_SHIFT is the index here */
2790 break;
2791 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2792 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2793 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2794 break;
2795
2796 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2797 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2798 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2799 break;
2800
2801 case PGMPOOLKIND_ROOT_NESTED:
2802 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2803 break;
2804
2805 default:
2806 AssertMsgFailed(("enmKind=%d\n", pUserPage->enmKind));
2807 break;
2808 }
2809#endif /* VBOX_STRICT */
2810
2811 /*
2812 * Clear the entry in the user page.
2813 */
2814 switch (pUserPage->enmKind)
2815 {
2816 /* 32-bit entries */
2817 case PGMPOOLKIND_ROOT_32BIT_PD:
2818 u.pau32[iUserTable] = 0;
2819 break;
2820
2821 /* 64-bit entries */
2822 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
2823 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2824 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2825 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2826 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
2827 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2828 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2829#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2830 case PGMPOOLKIND_ROOT_PAE_PD:
2831#endif
2832 case PGMPOOLKIND_ROOT_PDPT:
2833 case PGMPOOLKIND_ROOT_NESTED:
2834 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2835 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2836 u.pau64[iUserTable] = 0;
2837 break;
2838
2839 default:
2840 AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
2841 }
2842}
2843
2844
2845/**
2846 * Clears all users of a page.
2847 */
2848static void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2849{
2850 /*
2851 * Free all the user records.
2852 */
2853 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
2854 uint16_t i = pPage->iUserHead;
2855 while (i != NIL_PGMPOOL_USER_INDEX)
2856 {
2857 /* Clear enter in user table. */
2858 pgmPoolTrackClearPageUser(pPool, pPage, &paUsers[i]);
2859
2860 /* Free it. */
2861 const uint16_t iNext = paUsers[i].iNext;
2862 paUsers[i].iUser = NIL_PGMPOOL_IDX;
2863 paUsers[i].iNext = pPool->iUserFreeHead;
2864 pPool->iUserFreeHead = i;
2865
2866 /* Next. */
2867 i = iNext;
2868 }
2869 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
2870}
2871
2872#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2873
2874/**
2875 * Allocates a new physical cross reference extent.
2876 *
2877 * @returns Pointer to the allocated extent on success. NULL if we're out of them.
2878 * @param pVM The VM handle.
2879 * @param piPhysExt Where to store the phys ext index.
2880 */
2881PPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt)
2882{
2883 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2884 uint16_t iPhysExt = pPool->iPhysExtFreeHead;
2885 if (iPhysExt == NIL_PGMPOOL_PHYSEXT_INDEX)
2886 {
2887 STAM_COUNTER_INC(&pPool->StamTrackPhysExtAllocFailures);
2888 return NULL;
2889 }
2890 PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
2891 pPool->iPhysExtFreeHead = pPhysExt->iNext;
2892 pPhysExt->iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
2893 *piPhysExt = iPhysExt;
2894 return pPhysExt;
2895}
2896
2897
2898/**
2899 * Frees a physical cross reference extent.
2900 *
2901 * @param pVM The VM handle.
2902 * @param iPhysExt The extent to free.
2903 */
2904void pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt)
2905{
2906 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2907 Assert(iPhysExt < pPool->cMaxPhysExts);
2908 PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
2909 for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
2910 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
2911 pPhysExt->iNext = pPool->iPhysExtFreeHead;
2912 pPool->iPhysExtFreeHead = iPhysExt;
2913}
2914
2915
2916/**
2917 * Frees a physical cross reference extent.
2918 *
2919 * @param pVM The VM handle.
2920 * @param iPhysExt The extent to free.
2921 */
2922void pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt)
2923{
2924 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2925
2926 const uint16_t iPhysExtStart = iPhysExt;
2927 PPGMPOOLPHYSEXT pPhysExt;
2928 do
2929 {
2930 Assert(iPhysExt < pPool->cMaxPhysExts);
2931 pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
2932 for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
2933 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
2934
2935 /* next */
2936 iPhysExt = pPhysExt->iNext;
2937 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
2938
2939 pPhysExt->iNext = pPool->iPhysExtFreeHead;
2940 pPool->iPhysExtFreeHead = iPhysExtStart;
2941}
2942
2943
2944/**
2945 * Insert a reference into a list of physical cross reference extents.
2946 *
2947 * @returns The new ram range flags (top 16-bits).
2948 *
2949 * @param pVM The VM handle.
2950 * @param iPhysExt The physical extent index of the list head.
2951 * @param iShwPT The shadow page table index.
2952 *
2953 */
2954static uint16_t pgmPoolTrackPhysExtInsert(PVM pVM, uint16_t iPhysExt, uint16_t iShwPT)
2955{
2956 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2957 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
2958
2959 /* special common case. */
2960 if (paPhysExts[iPhysExt].aidx[2] == NIL_PGMPOOL_IDX)
2961 {
2962 paPhysExts[iPhysExt].aidx[2] = iShwPT;
2963 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
2964 LogFlow(("pgmPoolTrackPhysExtAddref: %d:{,,%d}\n", iPhysExt, iShwPT));
2965 return iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2966 }
2967
2968 /* general treatment. */
2969 const uint16_t iPhysExtStart = iPhysExt;
2970 unsigned cMax = 15;
2971 for (;;)
2972 {
2973 Assert(iPhysExt < pPool->cMaxPhysExts);
2974 for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
2975 if (paPhysExts[iPhysExt].aidx[i] == NIL_PGMPOOL_IDX)
2976 {
2977 paPhysExts[iPhysExt].aidx[i] = iShwPT;
2978 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
2979 LogFlow(("pgmPoolTrackPhysExtAddref: %d:{%d} i=%d cMax=%d\n", iPhysExt, iShwPT, i, cMax));
2980 return iPhysExtStart | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2981 }
2982 if (!--cMax)
2983 {
2984 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
2985 pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
2986 LogFlow(("pgmPoolTrackPhysExtAddref: overflow (1) iShwPT=%d\n", iShwPT));
2987 return MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2988 }
2989 }
2990
2991 /* add another extent to the list. */
2992 PPGMPOOLPHYSEXT pNew = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
2993 if (!pNew)
2994 {
2995 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
2996 pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
2997 return MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2998 }
2999 pNew->iNext = iPhysExtStart;
3000 pNew->aidx[0] = iShwPT;
3001 LogFlow(("pgmPoolTrackPhysExtAddref: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
3002 return iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
3003}
3004
3005
3006/**
3007 * Add a reference to guest physical page where extents are in use.
3008 *
3009 * @returns The new ram range flags (top 16-bits).
3010 *
3011 * @param pVM The VM handle.
3012 * @param u16 The ram range flags (top 16-bits).
3013 * @param iShwPT The shadow page table index.
3014 */
3015uint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT)
3016{
3017 if ((u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) != MM_RAM_FLAGS_CREFS_PHYSEXT)
3018 {
3019 /*
3020 * Convert to extent list.
3021 */
3022 Assert((u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) == 1);
3023 uint16_t iPhysExt;
3024 PPGMPOOLPHYSEXT pPhysExt = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
3025 if (pPhysExt)
3026 {
3027 LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, u16 & MM_RAM_FLAGS_IDX_MASK, iShwPT));
3028 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliased);
3029 pPhysExt->aidx[0] = u16 & MM_RAM_FLAGS_IDX_MASK;
3030 pPhysExt->aidx[1] = iShwPT;
3031 u16 = iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
3032 }
3033 else
3034 u16 = MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
3035 }
3036 else if (u16 != (MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT))))
3037 {
3038 /*
3039 * Insert into the extent list.
3040 */
3041 u16 = pgmPoolTrackPhysExtInsert(pVM, u16 & MM_RAM_FLAGS_IDX_MASK, iShwPT);
3042 }
3043 else
3044 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedLots);
3045 return u16;
3046}
3047
3048
3049/**
3050 * Clear references to guest physical memory.
3051 *
3052 * @param pPool The pool.
3053 * @param pPage The page.
3054 * @param pPhysPage Pointer to the aPages entry in the ram range.
3055 */
3056void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMPAGE pPhysPage)
3057{
3058 const unsigned cRefs = pPhysPage->HCPhys >> MM_RAM_FLAGS_CREFS_SHIFT; /** @todo PAGE FLAGS */
3059 AssertFatalMsg(cRefs == MM_RAM_FLAGS_CREFS_PHYSEXT, ("cRefs=%d HCPhys=%RHp pPage=%p:{.idx=%d}\n", cRefs, pPhysPage->HCPhys, pPage, pPage->idx));
3060
3061 uint16_t iPhysExt = (pPhysPage->HCPhys >> MM_RAM_FLAGS_IDX_SHIFT) & MM_RAM_FLAGS_IDX_MASK;
3062 if (iPhysExt != MM_RAM_FLAGS_IDX_OVERFLOWED)
3063 {
3064 uint16_t iPhysExtPrev = NIL_PGMPOOL_PHYSEXT_INDEX;
3065 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
3066 do
3067 {
3068 Assert(iPhysExt < pPool->cMaxPhysExts);
3069
3070 /*
3071 * Look for the shadow page and check if it's all freed.
3072 */
3073 for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
3074 {
3075 if (paPhysExts[iPhysExt].aidx[i] == pPage->idx)
3076 {
3077 paPhysExts[iPhysExt].aidx[i] = NIL_PGMPOOL_IDX;
3078
3079 for (i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
3080 if (paPhysExts[iPhysExt].aidx[i] != NIL_PGMPOOL_IDX)
3081 {
3082 LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d\n", pPhysPage->HCPhys, pPage->idx));
3083 return;
3084 }
3085
3086 /* we can free the node. */
3087 PVM pVM = pPool->CTX_SUFF(pVM);
3088 const uint16_t iPhysExtNext = paPhysExts[iPhysExt].iNext;
3089 if ( iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX
3090 && iPhysExtNext == NIL_PGMPOOL_PHYSEXT_INDEX)
3091 {
3092 /* lonely node */
3093 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
3094 LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d lonely\n", pPhysPage->HCPhys, pPage->idx));
3095 pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
3096 }
3097 else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
3098 {
3099 /* head */
3100 LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d head\n", pPhysPage->HCPhys, pPage->idx));
3101 pPhysPage->HCPhys = (pPhysPage->HCPhys & MM_RAM_FLAGS_NO_REFS_MASK) /** @todo PAGE FLAGS */
3102 | ((uint64_t)MM_RAM_FLAGS_CREFS_PHYSEXT << MM_RAM_FLAGS_CREFS_SHIFT)
3103 | ((uint64_t)iPhysExtNext << MM_RAM_FLAGS_IDX_SHIFT);
3104 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
3105 }
3106 else
3107 {
3108 /* in list */
3109 LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d\n", pPhysPage->HCPhys, pPage->idx));
3110 paPhysExts[iPhysExtPrev].iNext = iPhysExtNext;
3111 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
3112 }
3113 iPhysExt = iPhysExtNext;
3114 return;
3115 }
3116 }
3117
3118 /* next */
3119 iPhysExtPrev = iPhysExt;
3120 iPhysExt = paPhysExts[iPhysExt].iNext;
3121 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
3122
3123 AssertFatalMsgFailed(("not-found! cRefs=%d HCPhys=%RHp pPage=%p:{.idx=%d}\n", cRefs, pPhysPage->HCPhys, pPage, pPage->idx));
3124 }
3125 else /* nothing to do */
3126 LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64\n", pPhysPage->HCPhys));
3127}
3128
3129
3130/**
3131 * Clear references to guest physical memory.
3132 *
3133 * This is the same as pgmPoolTracDerefGCPhys except that the guest physical address
3134 * is assumed to be correct, so the linear search can be skipped and we can assert
3135 * at an earlier point.
3136 *
3137 * @param pPool The pool.
3138 * @param pPage The page.
3139 * @param HCPhys The host physical address corresponding to the guest page.
3140 * @param GCPhys The guest physical address corresponding to HCPhys.
3141 */
3142static void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
3143{
3144 /*
3145 * Walk range list.
3146 */
3147 PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
3148 while (pRam)
3149 {
3150 RTGCPHYS off = GCPhys - pRam->GCPhys;
3151 if (off < pRam->cb)
3152 {
3153 /* does it match? */
3154 const unsigned iPage = off >> PAGE_SHIFT;
3155 Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
3156#ifdef LOG_ENABLED
3157RTHCPHYS HCPhysPage = PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]);
3158Log(("pgmPoolTracDerefGCPhys %RHp vs %RHp\n", HCPhysPage, HCPhys));
3159#endif
3160 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
3161 {
3162 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
3163 return;
3164 }
3165 break;
3166 }
3167 pRam = pRam->CTX_SUFF(pNext);
3168 }
3169 AssertFatalMsgFailed(("HCPhys=%RHp GCPhys=%RGp\n", HCPhys, GCPhys));
3170}
3171
3172
3173/**
3174 * Clear references to guest physical memory.
3175 *
3176 * @param pPool The pool.
3177 * @param pPage The page.
3178 * @param HCPhys The host physical address corresponding to the guest page.
3179 * @param GCPhysHint The guest physical address which may corresponding to HCPhys.
3180 */
3181static void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
3182{
3183 /*
3184 * Walk range list.
3185 */
3186 PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
3187 while (pRam)
3188 {
3189 RTGCPHYS off = GCPhysHint - pRam->GCPhys;
3190 if (off < pRam->cb)
3191 {
3192 /* does it match? */
3193 const unsigned iPage = off >> PAGE_SHIFT;
3194 Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
3195 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
3196 {
3197 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
3198 return;
3199 }
3200 break;
3201 }
3202 pRam = pRam->CTX_SUFF(pNext);
3203 }
3204
3205 /*
3206 * Damn, the hint didn't work. We'll have to do an expensive linear search.
3207 */
3208 STAM_COUNTER_INC(&pPool->StatTrackLinearRamSearches);
3209 pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
3210 while (pRam)
3211 {
3212 unsigned iPage = pRam->cb >> PAGE_SHIFT;
3213 while (iPage-- > 0)
3214 {
3215 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
3216 {
3217 Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%RHp GCPhysHint=%RGp GCPhysReal=%RGp\n",
3218 HCPhys, GCPhysHint, pRam->GCPhys + (iPage << PAGE_SHIFT)));
3219 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
3220 return;
3221 }
3222 }
3223 pRam = pRam->CTX_SUFF(pNext);
3224 }
3225
3226 AssertFatalMsgFailed(("HCPhys=%RHp GCPhysHint=%RGp\n", HCPhys, GCPhysHint));
3227}
3228
3229
3230/**
3231 * Clear references to guest physical memory in a 32-bit / 32-bit page table.
3232 *
3233 * @param pPool The pool.
3234 * @param pPage The page.
3235 * @param pShwPT The shadow page table (mapping of the page).
3236 * @param pGstPT The guest page table.
3237 */
3238DECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
3239{
3240 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
3241 if (pShwPT->a[i].n.u1Present)
3242 {
3243 Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
3244 i, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
3245 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
3246 if (!--pPage->cPresent)
3247 break;
3248 }
3249}
3250
3251
3252/**
3253 * Clear references to guest physical memory in a PAE / 32-bit page table.
3254 *
3255 * @param pPool The pool.
3256 * @param pPage The page.
3257 * @param pShwPT The shadow page table (mapping of the page).
3258 * @param pGstPT The guest page table (just a half one).
3259 */
3260DECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
3261{
3262 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
3263 if (pShwPT->a[i].n.u1Present)
3264 {
3265 Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX32 hint=%RX32\n",
3266 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
3267 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
3268 }
3269}
3270
3271
3272/**
3273 * Clear references to guest physical memory in a PAE / PAE page table.
3274 *
3275 * @param pPool The pool.
3276 * @param pPage The page.
3277 * @param pShwPT The shadow page table (mapping of the page).
3278 * @param pGstPT The guest page table.
3279 */
3280DECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
3281{
3282 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
3283 if (pShwPT->a[i].n.u1Present)
3284 {
3285 Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX32 hint=%RX32\n",
3286 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
3287 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
3288 }
3289}
3290
3291
3292/**
3293 * Clear references to guest physical memory in a 32-bit / 4MB page table.
3294 *
3295 * @param pPool The pool.
3296 * @param pPage The page.
3297 * @param pShwPT The shadow page table (mapping of the page).
3298 */
3299DECLINLINE(void) pgmPoolTrackDerefPT32Bit4MB(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT)
3300{
3301 RTGCPHYS GCPhys = pPage->GCPhys;
3302 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
3303 if (pShwPT->a[i].n.u1Present)
3304 {
3305 Log4(("pgmPoolTrackDerefPT32Bit4MB: i=%d pte=%RX32 GCPhys=%RGp\n",
3306 i, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys));
3307 pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys);
3308 }
3309}
3310
3311
3312/**
3313 * Clear references to guest physical memory in a PAE / 2/4MB page table.
3314 *
3315 * @param pPool The pool.
3316 * @param pPage The page.
3317 * @param pShwPT The shadow page table (mapping of the page).
3318 */
3319DECLINLINE(void) pgmPoolTrackDerefPTPaeBig(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT)
3320{
3321 RTGCPHYS GCPhys = pPage->GCPhys;
3322 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
3323 if (pShwPT->a[i].n.u1Present)
3324 {
3325 Log4(("pgmPoolTrackDerefPTPaeBig: i=%d pte=%RX64 hint=%RGp\n",
3326 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys));
3327 pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys);
3328 }
3329}
3330
3331#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
3332
3333/**
3334 * Clear references to shadowed pages in a PAE (legacy or 64 bits) page directory.
3335 *
3336 * @param pPool The pool.
3337 * @param pPage The page.
3338 * @param pShwPD The shadow page directory (mapping of the page).
3339 */
3340DECLINLINE(void) pgmPoolTrackDerefPDPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPAE pShwPD)
3341{
3342 for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
3343 {
3344 if (pShwPD->a[i].n.u1Present)
3345 {
3346 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
3347 if (pSubPage)
3348 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3349 else
3350 AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & X86_PDE_PAE_PG_MASK));
3351 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3352 }
3353 }
3354}
3355
3356
3357/**
3358 * Clear references to shadowed pages in a 64-bit page directory pointer table.
3359 *
3360 * @param pPool The pool.
3361 * @param pPage The page.
3362 * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
3363 */
3364DECLINLINE(void) pgmPoolTrackDerefPDPT64Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
3365{
3366 for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
3367 {
3368 if (pShwPDPT->a[i].n.u1Present)
3369 {
3370 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
3371 if (pSubPage)
3372 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3373 else
3374 AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
3375 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3376 }
3377 }
3378}
3379
3380
3381/**
3382 * Clear references to shadowed pages in a 64-bit level 4 page table.
3383 *
3384 * @param pPool The pool.
3385 * @param pPage The page.
3386 * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
3387 */
3388DECLINLINE(void) pgmPoolTrackDerefPML464Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PML4 pShwPML4)
3389{
3390 for (unsigned i = 0; i < RT_ELEMENTS(pShwPML4->a); i++)
3391 {
3392 if (pShwPML4->a[i].n.u1Present)
3393 {
3394 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
3395 if (pSubPage)
3396 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3397 else
3398 AssertFatalMsgFailed(("%RX64\n", pShwPML4->a[i].u & X86_PML4E_PG_MASK));
3399 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3400 }
3401 }
3402}
3403
3404
3405/**
3406 * Clear references to shadowed pages in an EPT page table.
3407 *
3408 * @param pPool The pool.
3409 * @param pPage The page.
3410 * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
3411 */
3412DECLINLINE(void) pgmPoolTrackDerefPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPT pShwPT)
3413{
3414 RTGCPHYS GCPhys = pPage->GCPhys;
3415 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
3416 if (pShwPT->a[i].n.u1Present)
3417 {
3418 Log4(("pgmPoolTrackDerefPTEPT: i=%d pte=%RX64 GCPhys=%RX64\n",
3419 i, pShwPT->a[i].u & EPT_PTE_PG_MASK, pPage->GCPhys));
3420 pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & EPT_PTE_PG_MASK, GCPhys);
3421 }
3422}
3423
3424
3425/**
3426 * Clear references to shadowed pages in an EPT page directory.
3427 *
3428 * @param pPool The pool.
3429 * @param pPage The page.
3430 * @param pShwPD The shadow page directory (mapping of the page).
3431 */
3432DECLINLINE(void) pgmPoolTrackDerefPDEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPD pShwPD)
3433{
3434 for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
3435 {
3436 if (pShwPD->a[i].n.u1Present)
3437 {
3438 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & EPT_PDE_PG_MASK);
3439 if (pSubPage)
3440 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3441 else
3442 AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & EPT_PDE_PG_MASK));
3443 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3444 }
3445 }
3446}
3447
3448
3449/**
3450 * Clear references to shadowed pages in an EPT page directory pointer table.
3451 *
3452 * @param pPool The pool.
3453 * @param pPage The page.
3454 * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
3455 */
3456DECLINLINE(void) pgmPoolTrackDerefPDPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPDPT pShwPDPT)
3457{
3458 for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
3459 {
3460 if (pShwPDPT->a[i].n.u1Present)
3461 {
3462 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK);
3463 if (pSubPage)
3464 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3465 else
3466 AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK));
3467 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3468 }
3469 }
3470}
3471
3472
3473/**
3474 * Clears all references made by this page.
3475 *
3476 * This includes other shadow pages and GC physical addresses.
3477 *
3478 * @param pPool The pool.
3479 * @param pPage The page.
3480 */
3481static void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
3482{
3483 /*
3484 * Map the shadow page and take action according to the page kind.
3485 */
3486 void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
3487 switch (pPage->enmKind)
3488 {
3489#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
3490 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3491 {
3492 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3493 void *pvGst;
3494 int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
3495 pgmPoolTrackDerefPT32Bit32Bit(pPool, pPage, (PX86PT)pvShw, (PCX86PT)pvGst);
3496 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3497 break;
3498 }
3499
3500 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3501 {
3502 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3503 void *pvGst;
3504 int rc = PGM_GCPHYS_2_PTR_EX(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
3505 pgmPoolTrackDerefPTPae32Bit(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PT)pvGst);
3506 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3507 break;
3508 }
3509
3510 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
3511 {
3512 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3513 void *pvGst;
3514 int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
3515 pgmPoolTrackDerefPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
3516 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3517 break;
3518 }
3519
3520 case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
3521 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3522 {
3523 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3524 pgmPoolTrackDerefPT32Bit4MB(pPool, pPage, (PX86PT)pvShw);
3525 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3526 break;
3527 }
3528
3529 case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 2 MB page */
3530 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
3531 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
3532 {
3533 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3534 pgmPoolTrackDerefPTPaeBig(pPool, pPage, (PX86PTPAE)pvShw);
3535 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3536 break;
3537 }
3538
3539#else /* !PGMPOOL_WITH_GCPHYS_TRACKING */
3540 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3541 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3542 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
3543 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3544 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
3545 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
3546 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
3547 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
3548 break;
3549#endif /* !PGMPOOL_WITH_GCPHYS_TRACKING */
3550
3551 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
3552 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
3553 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
3554 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
3555 pgmPoolTrackDerefPDPae(pPool, pPage, (PX86PDPAE)pvShw);
3556 break;
3557
3558 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
3559 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
3560 pgmPoolTrackDerefPDPT64Bit(pPool, pPage, (PX86PDPT)pvShw);
3561 break;
3562
3563 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
3564 pgmPoolTrackDerefPML464Bit(pPool, pPage, (PX86PML4)pvShw);
3565 break;
3566
3567 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
3568 pgmPoolTrackDerefPTEPT(pPool, pPage, (PEPTPT)pvShw);
3569 break;
3570
3571 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
3572 pgmPoolTrackDerefPDEPT(pPool, pPage, (PEPTPD)pvShw);
3573 break;
3574
3575 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
3576 pgmPoolTrackDerefPDPTEPT(pPool, pPage, (PEPTPDPT)pvShw);
3577 break;
3578
3579 default:
3580 AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
3581 }
3582
3583 /* paranoia, clear the shadow page. Remove this laser (i.e. let Alloc and ClearAll do it). */
3584 STAM_PROFILE_START(&pPool->StatZeroPage, z);
3585 ASMMemZeroPage(pvShw);
3586 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
3587 pPage->fZeroed = true;
3588}
3589
3590#endif /* PGMPOOL_WITH_USER_TRACKING */
3591
3592/**
3593 * Flushes all the special root pages as part of a pgmPoolFlushAllInt operation.
3594 *
3595 * @param pPool The pool.
3596 */
3597static void pgmPoolFlushAllSpecialRoots(PPGMPOOL pPool)
3598{
3599 /*
3600 * These special pages are all mapped into the indexes 1..PGMPOOL_IDX_FIRST.
3601 */
3602 Assert(NIL_PGMPOOL_IDX == 0);
3603 for (unsigned i = 1; i < PGMPOOL_IDX_FIRST; i++)
3604 {
3605 /*
3606 * Get the page address.
3607 */
3608 PPGMPOOLPAGE pPage = &pPool->aPages[i];
3609 union
3610 {
3611 uint64_t *pau64;
3612 uint32_t *pau32;
3613 } u;
3614
3615 /*
3616 * Mark stuff not present.
3617 */
3618 switch (pPage->enmKind)
3619 {
3620 case PGMPOOLKIND_ROOT_32BIT_PD:
3621 u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
3622 for (unsigned iPage = 0; iPage < X86_PG_ENTRIES; iPage++)
3623 if ((u.pau32[iPage] & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == X86_PDE_P)
3624 u.pau32[iPage] = 0;
3625 break;
3626
3627 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
3628 u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
3629 for (unsigned iPage = 0; iPage < X86_PG_PAE_ENTRIES; iPage++)
3630 if ((u.pau64[iPage] & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == X86_PDE_P)
3631 u.pau64[iPage] = 0;
3632 break;
3633
3634 case PGMPOOLKIND_ROOT_PDPT:
3635 /* Not root of shadowed pages currently, ignore it. */
3636 break;
3637
3638 case PGMPOOLKIND_ROOT_NESTED:
3639 u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
3640 ASMMemZero32(u.pau64, PAGE_SIZE);
3641 break;
3642 }
3643 }
3644
3645 /*
3646 * Paranoia (to be removed), flag a global CR3 sync.
3647 */
3648 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
3649}
3650
3651
3652/**
3653 * Flushes the entire cache.
3654 *
3655 * It will assert a global CR3 flush (FF) and assumes the caller is aware of this
3656 * and execute this CR3 flush.
3657 *
3658 * @param pPool The pool.
3659 */
3660static void pgmPoolFlushAllInt(PPGMPOOL pPool)
3661{
3662 STAM_PROFILE_START(&pPool->StatFlushAllInt, a);
3663 LogFlow(("pgmPoolFlushAllInt:\n"));
3664
3665 /*
3666 * If there are no pages in the pool, there is nothing to do.
3667 */
3668 if (pPool->cCurPages <= PGMPOOL_IDX_FIRST)
3669 {
3670 STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
3671 return;
3672 }
3673
3674 /*
3675 * Nuke the free list and reinsert all pages into it.
3676 */
3677 for (unsigned i = pPool->cCurPages - 1; i >= PGMPOOL_IDX_FIRST; i--)
3678 {
3679 PPGMPOOLPAGE pPage = &pPool->aPages[i];
3680
3681#ifdef IN_RING3
3682 Assert(pPage->Core.Key == MMPage2Phys(pPool->pVMR3, pPage->pvPageR3));
3683#endif
3684#ifdef PGMPOOL_WITH_MONITORING
3685 if (pPage->fMonitored)
3686 pgmPoolMonitorFlush(pPool, pPage);
3687 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
3688 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
3689 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
3690 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
3691 pPage->cModifications = 0;
3692#endif
3693 pPage->GCPhys = NIL_RTGCPHYS;
3694 pPage->enmKind = PGMPOOLKIND_FREE;
3695 Assert(pPage->idx == i);
3696 pPage->iNext = i + 1;
3697 pPage->fZeroed = false; /* This could probably be optimized, but better safe than sorry. */
3698 pPage->fSeenNonGlobal = false;
3699 pPage->fMonitored= false;
3700 pPage->fCached = false;
3701 pPage->fReusedFlushPending = false;
3702 pPage->fCR3Mix = false;
3703#ifdef PGMPOOL_WITH_USER_TRACKING
3704 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
3705#endif
3706#ifdef PGMPOOL_WITH_CACHE
3707 pPage->iAgeNext = NIL_PGMPOOL_IDX;
3708 pPage->iAgePrev = NIL_PGMPOOL_IDX;
3709#endif
3710 }
3711 pPool->aPages[pPool->cCurPages - 1].iNext = NIL_PGMPOOL_IDX;
3712 pPool->iFreeHead = PGMPOOL_IDX_FIRST;
3713 pPool->cUsedPages = 0;
3714
3715#ifdef PGMPOOL_WITH_USER_TRACKING
3716 /*
3717 * Zap and reinitialize the user records.
3718 */
3719 pPool->cPresent = 0;
3720 pPool->iUserFreeHead = 0;
3721 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
3722 const unsigned cMaxUsers = pPool->cMaxUsers;
3723 for (unsigned i = 0; i < cMaxUsers; i++)
3724 {
3725 paUsers[i].iNext = i + 1;
3726 paUsers[i].iUser = NIL_PGMPOOL_IDX;
3727 paUsers[i].iUserTable = 0xfffffffe;
3728 }
3729 paUsers[cMaxUsers - 1].iNext = NIL_PGMPOOL_USER_INDEX;
3730#endif
3731
3732#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
3733 /*
3734 * Clear all the GCPhys links and rebuild the phys ext free list.
3735 */
3736 for (PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
3737 pRam;
3738 pRam = pRam->CTX_SUFF(pNext))
3739 {
3740 unsigned iPage = pRam->cb >> PAGE_SHIFT;
3741 while (iPage-- > 0)
3742 pRam->aPages[iPage].HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
3743 }
3744
3745 pPool->iPhysExtFreeHead = 0;
3746 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
3747 const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
3748 for (unsigned i = 0; i < cMaxPhysExts; i++)
3749 {
3750 paPhysExts[i].iNext = i + 1;
3751 paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
3752 paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
3753 paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
3754 }
3755 paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
3756#endif
3757
3758#ifdef PGMPOOL_WITH_MONITORING
3759 /*
3760 * Just zap the modified list.
3761 */
3762 pPool->cModifiedPages = 0;
3763 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
3764#endif
3765
3766#ifdef PGMPOOL_WITH_CACHE
3767 /*
3768 * Clear the GCPhys hash and the age list.
3769 */
3770 for (unsigned i = 0; i < RT_ELEMENTS(pPool->aiHash); i++)
3771 pPool->aiHash[i] = NIL_PGMPOOL_IDX;
3772 pPool->iAgeHead = NIL_PGMPOOL_IDX;
3773 pPool->iAgeTail = NIL_PGMPOOL_IDX;
3774#endif
3775
3776 /*
3777 * Flush all the special root pages.
3778 * Reinsert active pages into the hash and ensure monitoring chains are correct.
3779 */
3780 pgmPoolFlushAllSpecialRoots(pPool);
3781 for (unsigned i = PGMPOOL_IDX_FIRST_SPECIAL; i < PGMPOOL_IDX_FIRST; i++)
3782 {
3783 PPGMPOOLPAGE pPage = &pPool->aPages[i];
3784 pPage->iNext = NIL_PGMPOOL_IDX;
3785#ifdef PGMPOOL_WITH_MONITORING
3786 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
3787 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
3788 pPage->cModifications = 0;
3789 /* ASSUMES that we're not sharing with any of the other special pages (safe for now). */
3790 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
3791 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
3792 if (pPage->fMonitored)
3793 {
3794 PVM pVM = pPool->CTX_SUFF(pVM);
3795 int rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
3796 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
3797 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
3798 pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
3799 pPool->pszAccessHandler);
3800 AssertFatalRCSuccess(rc);
3801# ifdef PGMPOOL_WITH_CACHE
3802 pgmPoolHashInsert(pPool, pPage);
3803# endif
3804 }
3805#endif
3806#ifdef PGMPOOL_WITH_USER_TRACKING
3807 Assert(pPage->iUserHead == NIL_PGMPOOL_USER_INDEX); /* for now */
3808#endif
3809#ifdef PGMPOOL_WITH_CACHE
3810 Assert(pPage->iAgeNext == NIL_PGMPOOL_IDX);
3811 Assert(pPage->iAgePrev == NIL_PGMPOOL_IDX);
3812#endif
3813 }
3814
3815 STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
3816}
3817
3818
3819/**
3820 * Flushes a pool page.
3821 *
3822 * This moves the page to the free list after removing all user references to it.
3823 * In GC this will cause a CR3 reload if the page is traced back to an active root page.
3824 *
3825 * @returns VBox status code.
3826 * @retval VINF_SUCCESS on success.
3827 * @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
3828 * @param pPool The pool.
3829 * @param HCPhys The HC physical address of the shadow page.
3830 */
3831int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
3832{
3833 int rc = VINF_SUCCESS;
3834 STAM_PROFILE_START(&pPool->StatFlushPage, f);
3835 LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%RHp, .idx=%d, .enmKind=%d, .GCPhys=%RGp}\n",
3836 pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
3837
3838 /*
3839 * Quietly reject any attempts at flushing any of the special root pages.
3840 */
3841 if (pPage->idx < PGMPOOL_IDX_FIRST)
3842 {
3843 Log(("pgmPoolFlushPage: special root page, rejected. enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
3844 return VINF_SUCCESS;
3845 }
3846
3847 /*
3848 * Quietly reject any attempts at flushing the currently active shadow CR3 mapping
3849 */
3850 if (PGMGetHyperCR3(pPool->CTX_SUFF(pVM)) == pPage->Core.Key)
3851 {
3852 AssertMsg(pPage->enmKind == PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4,
3853 ("Can't free the shadow CR3! (%RHp vs %RHp kind=%d\n", PGMGetHyperCR3(pPool->CTX_SUFF(pVM)), pPage->Core.Key, pPage->enmKind));
3854 Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
3855 return VINF_SUCCESS;
3856 }
3857
3858 /*
3859 * Mark the page as being in need of a ASMMemZeroPage().
3860 */
3861 pPage->fZeroed = false;
3862
3863#ifdef PGMPOOL_WITH_USER_TRACKING
3864 /*
3865 * Clear the page.
3866 */
3867 pgmPoolTrackClearPageUsers(pPool, pPage);
3868 STAM_PROFILE_START(&pPool->StatTrackDeref,a);
3869 pgmPoolTrackDeref(pPool, pPage);
3870 STAM_PROFILE_STOP(&pPool->StatTrackDeref,a);
3871#endif
3872
3873#ifdef PGMPOOL_WITH_CACHE
3874 /*
3875 * Flush it from the cache.
3876 */
3877 pgmPoolCacheFlushPage(pPool, pPage);
3878#endif /* PGMPOOL_WITH_CACHE */
3879
3880#ifdef PGMPOOL_WITH_MONITORING
3881 /*
3882 * Deregistering the monitoring.
3883 */
3884 if (pPage->fMonitored)
3885 rc = pgmPoolMonitorFlush(pPool, pPage);
3886#endif
3887
3888 /*
3889 * Free the page.
3890 */
3891 Assert(pPage->iNext == NIL_PGMPOOL_IDX);
3892 pPage->iNext = pPool->iFreeHead;
3893 pPool->iFreeHead = pPage->idx;
3894 pPage->enmKind = PGMPOOLKIND_FREE;
3895 pPage->GCPhys = NIL_RTGCPHYS;
3896 pPage->fReusedFlushPending = false;
3897
3898 pPool->cUsedPages--;
3899 STAM_PROFILE_STOP(&pPool->StatFlushPage, f);
3900 return rc;
3901}
3902
3903
3904/**
3905 * Frees a usage of a pool page.
3906 *
3907 * The caller is responsible to updating the user table so that it no longer
3908 * references the shadow page.
3909 *
3910 * @param pPool The pool.
3911 * @param HCPhys The HC physical address of the shadow page.
3912 * @param iUser The shadow page pool index of the user table.
3913 * @param iUserTable The index into the user table (shadowed).
3914 */
3915void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
3916{
3917 STAM_PROFILE_START(&pPool->StatFree, a);
3918 LogFlow(("pgmPoolFreeByPage: pPage=%p:{.Key=%RHp, .idx=%d, enmKind=%d} iUser=%#x iUserTable=%#x\n",
3919 pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, iUser, iUserTable));
3920 Assert(pPage->idx >= PGMPOOL_IDX_FIRST);
3921#ifdef PGMPOOL_WITH_USER_TRACKING
3922 pgmPoolTrackFreeUser(pPool, pPage, iUser, iUserTable);
3923#endif
3924#ifdef PGMPOOL_WITH_CACHE
3925 if (!pPage->fCached)
3926#endif
3927 pgmPoolFlushPage(pPool, pPage); /* ASSUMES that VERR_PGM_POOL_CLEARED can be ignored here. */
3928 STAM_PROFILE_STOP(&pPool->StatFree, a);
3929}
3930
3931
3932/**
3933 * Makes one or more free page free.
3934 *
3935 * @returns VBox status code.
3936 * @retval VINF_SUCCESS on success.
3937 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
3938 *
3939 * @param pPool The pool.
3940 * @param iUser The user of the page.
3941 */
3942static int pgmPoolMakeMoreFreePages(PPGMPOOL pPool, uint16_t iUser)
3943{
3944 LogFlow(("pgmPoolMakeMoreFreePages: iUser=%#x\n", iUser));
3945
3946 /*
3947 * If the pool isn't full grown yet, expand it.
3948 */
3949 if (pPool->cCurPages < pPool->cMaxPages)
3950 {
3951 STAM_PROFILE_ADV_SUSPEND(&pPool->StatAlloc, a);
3952#ifdef IN_RING3
3953 int rc = PGMR3PoolGrow(pPool->pVMR3);
3954#else
3955 int rc = CTXALLMID(VMM, CallHost)(pPool->CTX_SUFF(pVM), VMMCALLHOST_PGM_POOL_GROW, 0);
3956#endif
3957 if (RT_FAILURE(rc))
3958 return rc;
3959 STAM_PROFILE_ADV_RESUME(&pPool->StatAlloc, a);
3960 if (pPool->iFreeHead != NIL_PGMPOOL_IDX)
3961 return VINF_SUCCESS;
3962 }
3963
3964#ifdef PGMPOOL_WITH_CACHE
3965 /*
3966 * Free one cached page.
3967 */
3968 return pgmPoolCacheFreeOne(pPool, iUser);
3969#else
3970 /*
3971 * Flush the pool.
3972 * If we have tracking enabled, it should be possible to come up with
3973 * a cheap replacement strategy...
3974 */
3975 /* @todo incompatible with long mode paging (cr3 root will be flushed) */
3976 Assert(!CPUMIsGuestInLongMode(pVM));
3977 pgmPoolFlushAllInt(pPool);
3978 return VERR_PGM_POOL_FLUSHED;
3979#endif
3980}
3981
3982
3983/**
3984 * Allocates a page from the pool.
3985 *
3986 * This page may actually be a cached page and not in need of any processing
3987 * on the callers part.
3988 *
3989 * @returns VBox status code.
3990 * @retval VINF_SUCCESS if a NEW page was allocated.
3991 * @retval VINF_PGM_CACHED_PAGE if a CACHED page was returned.
3992 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
3993 * @param pVM The VM handle.
3994 * @param GCPhys The GC physical address of the page we're gonna shadow.
3995 * For 4MB and 2MB PD entries, it's the first address the
3996 * shadow PT is covering.
3997 * @param enmKind The kind of mapping.
3998 * @param iUser The shadow page pool index of the user table.
3999 * @param iUserTable The index into the user table (shadowed).
4000 * @param ppPage Where to store the pointer to the page. NULL is stored here on failure.
4001 */
4002int pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
4003{
4004 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
4005 STAM_PROFILE_ADV_START(&pPool->StatAlloc, a);
4006 LogFlow(("pgmPoolAlloc: GCPhys=%RGp enmKind=%d iUser=%#x iUserTable=%#x\n", GCPhys, enmKind, iUser, iUserTable));
4007 *ppPage = NULL;
4008
4009#ifdef PGMPOOL_WITH_CACHE
4010 if (pPool->fCacheEnabled)
4011 {
4012 int rc2 = pgmPoolCacheAlloc(pPool, GCPhys, enmKind, iUser, iUserTable, ppPage);
4013 if (RT_SUCCESS(rc2))
4014 {
4015 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4016 LogFlow(("pgmPoolAlloc: cached returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
4017 return rc2;
4018 }
4019 }
4020#endif
4021
4022 /*
4023 * Allocate a new one.
4024 */
4025 int rc = VINF_SUCCESS;
4026 uint16_t iNew = pPool->iFreeHead;
4027 if (iNew == NIL_PGMPOOL_IDX)
4028 {
4029 rc = pgmPoolMakeMoreFreePages(pPool, iUser);
4030 if (RT_FAILURE(rc))
4031 {
4032 if (rc != VERR_PGM_POOL_CLEARED)
4033 {
4034 Log(("pgmPoolAlloc: returns %Rrc (Free)\n", rc));
4035 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4036 return rc;
4037 }
4038 Log(("pgmPoolMakeMoreFreePages failed with %Rrc -> return VERR_PGM_POOL_FLUSHED\n", rc));
4039 rc = VERR_PGM_POOL_FLUSHED;
4040 }
4041 iNew = pPool->iFreeHead;
4042 AssertReleaseReturn(iNew != NIL_PGMPOOL_IDX, VERR_INTERNAL_ERROR);
4043 }
4044
4045 /* unlink the free head */
4046 PPGMPOOLPAGE pPage = &pPool->aPages[iNew];
4047 pPool->iFreeHead = pPage->iNext;
4048 pPage->iNext = NIL_PGMPOOL_IDX;
4049
4050 /*
4051 * Initialize it.
4052 */
4053 pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
4054 pPage->enmKind = enmKind;
4055 pPage->GCPhys = GCPhys;
4056 pPage->fSeenNonGlobal = false; /* Set this to 'true' to disable this feature. */
4057 pPage->fMonitored = false;
4058 pPage->fCached = false;
4059 pPage->fReusedFlushPending = false;
4060 pPage->fCR3Mix = false;
4061#ifdef PGMPOOL_WITH_MONITORING
4062 pPage->cModifications = 0;
4063 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
4064 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
4065#endif
4066#ifdef PGMPOOL_WITH_USER_TRACKING
4067 pPage->cPresent = 0;
4068 pPage->iFirstPresent = ~0;
4069
4070 /*
4071 * Insert into the tracking and cache. If this fails, free the page.
4072 */
4073 int rc3 = pgmPoolTrackInsert(pPool, pPage, GCPhys, iUser, iUserTable);
4074 if (RT_FAILURE(rc3))
4075 {
4076 if (rc3 != VERR_PGM_POOL_CLEARED)
4077 {
4078 pPool->cUsedPages--;
4079 pPage->enmKind = PGMPOOLKIND_FREE;
4080 pPage->GCPhys = NIL_RTGCPHYS;
4081 pPage->iNext = pPool->iFreeHead;
4082 pPool->iFreeHead = pPage->idx;
4083 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4084 Log(("pgmPoolAlloc: returns %Rrc (Insert)\n", rc3));
4085 return rc3;
4086 }
4087 Log(("pgmPoolTrackInsert failed with %Rrc -> return VERR_PGM_POOL_FLUSHED\n", rc3));
4088 rc = VERR_PGM_POOL_FLUSHED;
4089 }
4090#endif /* PGMPOOL_WITH_USER_TRACKING */
4091
4092 /*
4093 * Commit the allocation, clear the page and return.
4094 */
4095#ifdef VBOX_WITH_STATISTICS
4096 if (pPool->cUsedPages > pPool->cUsedPagesHigh)
4097 pPool->cUsedPagesHigh = pPool->cUsedPages;
4098#endif
4099
4100 if (!pPage->fZeroed)
4101 {
4102 STAM_PROFILE_START(&pPool->StatZeroPage, z);
4103 void *pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
4104 ASMMemZeroPage(pv);
4105 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
4106 }
4107
4108 *ppPage = pPage;
4109 LogFlow(("pgmPoolAlloc: returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
4110 rc, pPage, pPage->Core.Key, pPage->idx, pPage->fCached, pPage->fMonitored));
4111 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4112 return rc;
4113}
4114
4115
4116/**
4117 * Frees a usage of a pool page.
4118 *
4119 * @param pVM The VM handle.
4120 * @param HCPhys The HC physical address of the shadow page.
4121 * @param iUser The shadow page pool index of the user table.
4122 * @param iUserTable The index into the user table (shadowed).
4123 */
4124void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable)
4125{
4126 LogFlow(("pgmPoolFree: HCPhys=%RHp iUser=%#x iUserTable=%#x\n", HCPhys, iUser, iUserTable));
4127 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
4128 pgmPoolFreeByPage(pPool, pgmPoolGetPage(pPool, HCPhys), iUser, iUserTable);
4129}
4130
4131
4132/**
4133 * Gets a in-use page in the pool by it's physical address.
4134 *
4135 * @returns Pointer to the page.
4136 * @param pVM The VM handle.
4137 * @param HCPhys The HC physical address of the shadow page.
4138 * @remark This function will NEVER return NULL. It will assert if HCPhys is invalid.
4139 */
4140PPGMPOOLPAGE pgmPoolGetPageByHCPhys(PVM pVM, RTHCPHYS HCPhys)
4141{
4142 /** @todo profile this! */
4143 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
4144 PPGMPOOLPAGE pPage = pgmPoolGetPage(pPool, HCPhys);
4145 Log3(("pgmPoolGetPageByHCPhys: HCPhys=%RHp -> %p:{.idx=%d .GCPhys=%RGp .enmKind=%d}\n",
4146 HCPhys, pPage, pPage->idx, pPage->GCPhys, pPage->enmKind));
4147 return pPage;
4148}
4149
4150
4151/**
4152 * Flushes the entire cache.
4153 *
4154 * It will assert a global CR3 flush (FF) and assumes the caller is aware of this
4155 * and execute this CR3 flush.
4156 *
4157 * @param pPool The pool.
4158 */
4159void pgmPoolFlushAll(PVM pVM)
4160{
4161 LogFlow(("pgmPoolFlushAll:\n"));
4162 pgmPoolFlushAllInt(pVM->pgm.s.CTX_SUFF(pPool));
4163}
4164
Note: See TracBrowser for help on using the repository browser.

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