VirtualBox

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

Last change on this file since 22774 was 22774, checked in by vboxsync, 15 years ago

Disabled experimental code that caused problems with XP

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 188.3 KB
Line 
1/* $Id: PGMAllPool.cpp 22774 2009-09-04 10:20:22Z 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#include <iprt/string.h>
43
44
45/*******************************************************************************
46* Internal Functions *
47*******************************************************************************/
48RT_C_DECLS_BEGIN
49static void pgmPoolFlushAllInt(PPGMPOOL pPool);
50#ifdef PGMPOOL_WITH_USER_TRACKING
51DECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind);
52DECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind);
53static void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
54#endif
55#ifdef PGMPOOL_WITH_CACHE
56static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
57#endif
58#ifdef PGMPOOL_WITH_MONITORING
59static void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
60#endif
61#ifndef IN_RING3
62DECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
63#endif
64#ifdef LOG_ENABLED
65static const char *pgmPoolPoolKindToStr(uint8_t enmKind);
66#endif
67
68void pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs);
69void pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt);
70int pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage);
71PPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt);
72void pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt);
73void pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt);
74
75RT_C_DECLS_END
76
77
78/**
79 * Checks if the specified page pool kind is for a 4MB or 2MB guest page.
80 *
81 * @returns true if it's the shadow of a 4MB or 2MB guest page, otherwise false.
82 * @param enmKind The page kind.
83 */
84DECLINLINE(bool) pgmPoolIsBigPage(PGMPOOLKIND enmKind)
85{
86 switch (enmKind)
87 {
88 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
89 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
90 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
91 return true;
92 default:
93 return false;
94 }
95}
96
97/** @def PGMPOOL_PAGE_2_LOCKED_PTR
98 * Maps a pool page pool into the current context and lock it (RC only).
99 *
100 * @returns VBox status code.
101 * @param pVM The VM handle.
102 * @param pPage The pool page.
103 *
104 * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
105 * small page window employeed by that function. Be careful.
106 * @remark There is no need to assert on the result.
107 */
108#if defined(IN_RC)
109DECLINLINE(void *) PGMPOOL_PAGE_2_LOCKED_PTR(PVM pVM, PPGMPOOLPAGE pPage)
110{
111 void *pv = pgmPoolMapPageInlined(&pVM->pgm.s, pPage);
112
113 /* Make sure the dynamic mapping will not be reused. */
114 if (pv)
115 PGMDynLockHCPage(pVM, (uint8_t *)pv);
116
117 return pv;
118}
119#else
120# define PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage) PGMPOOL_PAGE_2_PTR(pVM, pPage)
121#endif
122
123/** @def PGMPOOL_UNLOCK_PTR
124 * Unlock a previously locked dynamic caching (RC only).
125 *
126 * @returns VBox status code.
127 * @param pVM The VM handle.
128 * @param pPage The pool page.
129 *
130 * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
131 * small page window employeed by that function. Be careful.
132 * @remark There is no need to assert on the result.
133 */
134#if defined(IN_RC)
135DECLINLINE(void) PGMPOOL_UNLOCK_PTR(PVM pVM, void *pvPage)
136{
137 if (pvPage)
138 PGMDynUnlockHCPage(pVM, (uint8_t *)pvPage);
139}
140#else
141# define PGMPOOL_UNLOCK_PTR(pVM, pPage) do {} while (0)
142#endif
143
144
145#ifdef PGMPOOL_WITH_MONITORING
146/**
147 * Determin the size of a write instruction.
148 * @returns number of bytes written.
149 * @param pDis The disassembler state.
150 */
151static unsigned pgmPoolDisasWriteSize(PDISCPUSTATE pDis)
152{
153 /*
154 * This is very crude and possibly wrong for some opcodes,
155 * but since it's not really supposed to be called we can
156 * probably live with that.
157 */
158 return DISGetParamSize(pDis, &pDis->param1);
159}
160
161
162/**
163 * Flushes a chain of pages sharing the same access monitor.
164 *
165 * @returns VBox status code suitable for scheduling.
166 * @param pPool The pool.
167 * @param pPage A page in the chain.
168 */
169int pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
170{
171 LogFlow(("pgmPoolMonitorChainFlush: Flush page %RGp type=%d\n", pPage->GCPhys, pPage->enmKind));
172
173 /*
174 * Find the list head.
175 */
176 uint16_t idx = pPage->idx;
177 if (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
178 {
179 while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
180 {
181 idx = pPage->iMonitoredPrev;
182 Assert(idx != pPage->idx);
183 pPage = &pPool->aPages[idx];
184 }
185 }
186
187 /*
188 * Iterate the list flushing each shadow page.
189 */
190 int rc = VINF_SUCCESS;
191 for (;;)
192 {
193 idx = pPage->iMonitoredNext;
194 Assert(idx != pPage->idx);
195 if (pPage->idx >= PGMPOOL_IDX_FIRST)
196 {
197 int rc2 = pgmPoolFlushPage(pPool, pPage);
198 AssertRC(rc2);
199 }
200 /* next */
201 if (idx == NIL_PGMPOOL_IDX)
202 break;
203 pPage = &pPool->aPages[idx];
204 }
205 return rc;
206}
207
208
209/**
210 * Wrapper for getting the current context pointer to the entry being modified.
211 *
212 * @returns VBox status code suitable for scheduling.
213 * @param pVM VM Handle.
214 * @param pvDst Destination address
215 * @param pvSrc Source guest virtual address.
216 * @param GCPhysSrc The source guest physical address.
217 * @param cb Size of data to read
218 */
219DECLINLINE(int) pgmPoolPhysSimpleReadGCPhys(PVM pVM, void *pvDst, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvSrc, RTGCPHYS GCPhysSrc, size_t cb)
220{
221#if defined(IN_RING3)
222 memcpy(pvDst, (RTHCPTR)((uintptr_t)pvSrc & ~(RTHCUINTPTR)(cb - 1)), cb);
223 return VINF_SUCCESS;
224#else
225 /* @todo in RC we could attempt to use the virtual address, although this can cause many faults (PAE Windows XP guest). */
226 return PGMPhysSimpleReadGCPhys(pVM, pvDst, GCPhysSrc & ~(RTGCPHYS)(cb - 1), cb);
227#endif
228}
229
230/**
231 * Process shadow entries before they are changed by the guest.
232 *
233 * For PT entries we will clear them. For PD entries, we'll simply check
234 * for mapping conflicts and set the SyncCR3 FF if found.
235 *
236 * @param pVCpu VMCPU handle
237 * @param pPool The pool.
238 * @param pPage The head page.
239 * @param GCPhysFault The guest physical fault address.
240 * @param uAddress In R0 and GC this is the guest context fault address (flat).
241 * In R3 this is the host context 'fault' address.
242 * @param pDis The disassembler state for figuring out the write size.
243 * This need not be specified if the caller knows we won't do cross entry accesses.
244 */
245void pgmPoolMonitorChainChanging(PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvAddress, PDISCPUSTATE pDis)
246{
247 AssertMsg(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX, ("%#x (idx=%#x)\n", pPage->iMonitoredPrev, pPage->idx));
248 const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
249 const unsigned cbWrite = pDis ? pgmPoolDisasWriteSize(pDis) : 0;
250 PVM pVM = pPool->CTX_SUFF(pVM);
251
252 LogFlow(("pgmPoolMonitorChainChanging: %RGv phys=%RGp cbWrite=%d\n", (RTGCPTR)pvAddress, GCPhysFault, cbWrite));
253
254 for (;;)
255 {
256 union
257 {
258 void *pv;
259 PX86PT pPT;
260 PX86PTPAE pPTPae;
261 PX86PD pPD;
262 PX86PDPAE pPDPae;
263 PX86PDPT pPDPT;
264 PX86PML4 pPML4;
265 } uShw;
266
267 LogFlow(("pgmPoolMonitorChainChanging: page idx=%d phys=%RGp (next=%d) kind=%s\n", pPage->idx, pPage->GCPhys, pPage->iMonitoredNext, pgmPoolPoolKindToStr(pPage->enmKind), cbWrite));
268
269 uShw.pv = NULL;
270 switch (pPage->enmKind)
271 {
272 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
273 {
274 uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
275 const unsigned iShw = off / sizeof(X86PTE);
276 LogFlow(("PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT iShw=%x\n", iShw));
277 if (uShw.pPT->a[iShw].n.u1Present)
278 {
279# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
280 X86PTE GstPte;
281
282 int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
283 AssertRC(rc);
284 Log4(("pgmPoolMonitorChainChanging 32_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PG_MASK));
285 pgmPoolTracDerefGCPhysHint(pPool, pPage,
286 uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK,
287 GstPte.u & X86_PTE_PG_MASK);
288# endif
289 ASMAtomicWriteSize(&uShw.pPT->a[iShw], 0);
290 }
291 break;
292 }
293
294 /* page/2 sized */
295 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
296 {
297 uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
298 if (!((off ^ pPage->GCPhys) & (PAGE_SIZE / 2)))
299 {
300 const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
301 LogFlow(("PGMPOOLKIND_PAE_PT_FOR_32BIT_PT iShw=%x\n", iShw));
302 if (uShw.pPTPae->a[iShw].n.u1Present)
303 {
304# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
305 X86PTE GstPte;
306 int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
307 AssertRC(rc);
308
309 Log4(("pgmPoolMonitorChainChanging pae_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PG_MASK));
310 pgmPoolTracDerefGCPhysHint(pPool, pPage,
311 uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
312 GstPte.u & X86_PTE_PG_MASK);
313# endif
314 ASMAtomicWriteSize(&uShw.pPTPae->a[iShw], 0);
315 }
316 }
317 break;
318 }
319
320 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
321 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
322 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
323 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
324 {
325 unsigned iGst = off / sizeof(X86PDE);
326 unsigned iShwPdpt = iGst / 256;
327 unsigned iShw = (iGst % 256) * 2;
328 uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
329
330 LogFlow(("pgmPoolMonitorChainChanging PAE for 32 bits: iGst=%x iShw=%x idx = %d page idx=%d\n", iGst, iShw, iShwPdpt, pPage->enmKind - PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD));
331 if (iShwPdpt == pPage->enmKind - (unsigned)PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD)
332 {
333 for (unsigned i = 0; i < 2; i++)
334 {
335# ifndef IN_RING0
336 if ((uShw.pPDPae->a[iShw + i].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
337 {
338 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
339 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
340 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw=%#x!\n", iShwPdpt, iShw+i));
341 break;
342 }
343 else
344# endif /* !IN_RING0 */
345 if (uShw.pPDPae->a[iShw+i].n.u1Present)
346 {
347 LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw+i, uShw.pPDPae->a[iShw+i].u));
348 pgmPoolFree(pVM,
349 uShw.pPDPae->a[iShw+i].u & X86_PDE_PAE_PG_MASK,
350 pPage->idx,
351 iShw + i);
352 ASMAtomicWriteSize(&uShw.pPDPae->a[iShw+i], 0);
353 }
354
355 /* paranoia / a bit assumptive. */
356 if ( pDis
357 && (off & 3)
358 && (off & 3) + cbWrite > 4)
359 {
360 const unsigned iShw2 = iShw + 2 + i;
361 if (iShw2 < RT_ELEMENTS(uShw.pPDPae->a))
362 {
363# ifndef IN_RING0
364 if ((uShw.pPDPae->a[iShw2].u & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == (PGM_PDFLAGS_MAPPING | X86_PDE_P))
365 {
366 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
367 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
368 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShwPdpt=%#x iShw2=%#x!\n", iShwPdpt, iShw2));
369 break;
370 }
371 else
372# endif /* !IN_RING0 */
373 if (uShw.pPDPae->a[iShw2].n.u1Present)
374 {
375 LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
376 pgmPoolFree(pVM,
377 uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
378 pPage->idx,
379 iShw2);
380 ASMAtomicWriteSize(&uShw.pPDPae->a[iShw2].u, 0);
381 }
382 }
383 }
384 }
385 }
386 break;
387 }
388
389 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
390 {
391 uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
392 const unsigned iShw = off / sizeof(X86PTEPAE);
393 if (uShw.pPTPae->a[iShw].n.u1Present)
394 {
395# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
396 X86PTEPAE GstPte;
397 int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
398 AssertRC(rc);
399
400 Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PAE_PG_MASK));
401 pgmPoolTracDerefGCPhysHint(pPool, pPage,
402 uShw.pPTPae->a[iShw].u & X86_PTE_PAE_PG_MASK,
403 GstPte.u & X86_PTE_PAE_PG_MASK);
404# endif
405 ASMAtomicWriteSize(&uShw.pPTPae->a[iShw].u, 0);
406 }
407
408 /* paranoia / a bit assumptive. */
409 if ( pDis
410 && (off & 7)
411 && (off & 7) + cbWrite > sizeof(X86PTEPAE))
412 {
413 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTEPAE);
414 AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPTPae->a));
415
416 if (uShw.pPTPae->a[iShw2].n.u1Present)
417 {
418# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
419 X86PTEPAE GstPte;
420# ifdef IN_RING3
421 int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, (RTHCPTR)((RTHCUINTPTR)pvAddress + sizeof(GstPte)), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
422# else
423 int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress + sizeof(GstPte), GCPhysFault + sizeof(GstPte), sizeof(GstPte));
424# endif
425 AssertRC(rc);
426 Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PAE_PG_MASK));
427 pgmPoolTracDerefGCPhysHint(pPool, pPage,
428 uShw.pPTPae->a[iShw2].u & X86_PTE_PAE_PG_MASK,
429 GstPte.u & X86_PTE_PAE_PG_MASK);
430# endif
431 ASMAtomicWriteSize(&uShw.pPTPae->a[iShw2].u ,0);
432 }
433 }
434 break;
435 }
436
437 case PGMPOOLKIND_32BIT_PD:
438 {
439 uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
440 const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
441
442 LogFlow(("pgmPoolMonitorChainChanging: PGMPOOLKIND_32BIT_PD %x\n", iShw));
443# ifndef IN_RING0
444 if (uShw.pPD->a[iShw].u & PGM_PDFLAGS_MAPPING)
445 {
446 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
447 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
448 STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
449 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
450 break;
451 }
452# endif /* !IN_RING0 */
453# ifndef IN_RING0
454 else
455# endif /* !IN_RING0 */
456 {
457 if (uShw.pPD->a[iShw].n.u1Present)
458 {
459 LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
460 pgmPoolFree(pVM,
461 uShw.pPD->a[iShw].u & X86_PDE_PAE_PG_MASK,
462 pPage->idx,
463 iShw);
464 ASMAtomicWriteSize(&uShw.pPD->a[iShw].u, 0);
465 }
466 }
467 /* paranoia / a bit assumptive. */
468 if ( pDis
469 && (off & 3)
470 && (off & 3) + cbWrite > sizeof(X86PTE))
471 {
472 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTE);
473 if ( iShw2 != iShw
474 && iShw2 < RT_ELEMENTS(uShw.pPD->a))
475 {
476# ifndef IN_RING0
477 if (uShw.pPD->a[iShw2].u & PGM_PDFLAGS_MAPPING)
478 {
479 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
480 STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
481 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
482 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
483 break;
484 }
485# endif /* !IN_RING0 */
486# ifndef IN_RING0
487 else
488# endif /* !IN_RING0 */
489 {
490 if (uShw.pPD->a[iShw2].n.u1Present)
491 {
492 LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPD->a[iShw2].u));
493 pgmPoolFree(pVM,
494 uShw.pPD->a[iShw2].u & X86_PDE_PAE_PG_MASK,
495 pPage->idx,
496 iShw2);
497 ASMAtomicWriteSize(&uShw.pPD->a[iShw2].u, 0);
498 }
499 }
500 }
501 }
502#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). */
503 if ( uShw.pPD->a[iShw].n.u1Present
504 && !VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3))
505 {
506 LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
507# ifdef IN_RC /* TLB load - we're pushing things a bit... */
508 ASMProbeReadByte(pvAddress);
509# endif
510 pgmPoolFree(pVM, uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
511 ASMAtomicWriteSize(&uShw.pPD->a[iShw].u, 0);
512 }
513#endif
514 break;
515 }
516
517 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
518 {
519 uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
520 const unsigned iShw = off / sizeof(X86PDEPAE);
521#ifndef IN_RING0
522 if (uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING)
523 {
524 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
525 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
526 STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
527 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw=%#x!\n", iShw));
528 break;
529 }
530#endif /* !IN_RING0 */
531 /*
532 * Causes trouble when the guest uses a PDE to refer to the whole page table level
533 * structure. (Invalidate here; faults later on when it tries to change the page
534 * table entries -> recheck; probably only applies to the RC case.)
535 */
536# ifndef IN_RING0
537 else
538# endif /* !IN_RING0 */
539 {
540 if (uShw.pPDPae->a[iShw].n.u1Present)
541 {
542 LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
543 pgmPoolFree(pVM,
544 uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
545 pPage->idx,
546 iShw);
547 ASMAtomicWriteSize(&uShw.pPDPae->a[iShw].u, 0);
548 }
549 }
550 /* paranoia / a bit assumptive. */
551 if ( pDis
552 && (off & 7)
553 && (off & 7) + cbWrite > sizeof(X86PDEPAE))
554 {
555 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
556 AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
557
558#ifndef IN_RING0
559 if ( iShw2 != iShw
560 && uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING)
561 {
562 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
563 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
564 STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
565 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
566 break;
567 }
568#endif /* !IN_RING0 */
569# ifndef IN_RING0
570 else
571# endif /* !IN_RING0 */
572 if (uShw.pPDPae->a[iShw2].n.u1Present)
573 {
574 LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
575 pgmPoolFree(pVM,
576 uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
577 pPage->idx,
578 iShw2);
579 ASMAtomicWriteSize(&uShw.pPDPae->a[iShw2].u, 0);
580 }
581 }
582 break;
583 }
584
585 case PGMPOOLKIND_PAE_PDPT:
586 {
587 /*
588 * Hopefully this doesn't happen very often:
589 * - touching unused parts of the page
590 * - messing with the bits of pd pointers without changing the physical address
591 */
592 /* PDPT roots are not page aligned; 32 byte only! */
593 const unsigned offPdpt = GCPhysFault - pPage->GCPhys;
594
595 uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
596 const unsigned iShw = offPdpt / sizeof(X86PDPE);
597 if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use RT_ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
598 {
599# ifndef IN_RING0
600 if (uShw.pPDPT->a[iShw].u & PGM_PLXFLAGS_MAPPING)
601 {
602 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
603 STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
604 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
605 LogFlow(("pgmPoolMonitorChainChanging: Detected pdpt conflict at iShw=%#x!\n", iShw));
606 break;
607 }
608# endif /* !IN_RING0 */
609# ifndef IN_RING0
610 else
611# endif /* !IN_RING0 */
612 if (uShw.pPDPT->a[iShw].n.u1Present)
613 {
614 LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
615 pgmPoolFree(pVM,
616 uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK,
617 pPage->idx,
618 iShw);
619 ASMAtomicWriteSize(&uShw.pPDPT->a[iShw].u, 0);
620 }
621
622 /* paranoia / a bit assumptive. */
623 if ( pDis
624 && (offPdpt & 7)
625 && (offPdpt & 7) + cbWrite > sizeof(X86PDPE))
626 {
627 const unsigned iShw2 = (offPdpt + cbWrite - 1) / sizeof(X86PDPE);
628 if ( iShw2 != iShw
629 && iShw2 < X86_PG_PAE_PDPE_ENTRIES)
630 {
631# ifndef IN_RING0
632 if (uShw.pPDPT->a[iShw2].u & PGM_PLXFLAGS_MAPPING)
633 {
634 Assert(pgmMapAreMappingsEnabled(&pVM->pgm.s));
635 STAM_COUNTER_INC(&(pVCpu->pgm.s.StatRZGuestCR3WriteConflict));
636 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
637 LogFlow(("pgmPoolMonitorChainChanging: Detected conflict at iShw2=%#x!\n", iShw2));
638 break;
639 }
640# endif /* !IN_RING0 */
641# ifndef IN_RING0
642 else
643# endif /* !IN_RING0 */
644 if (uShw.pPDPT->a[iShw2].n.u1Present)
645 {
646 LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
647 pgmPoolFree(pVM,
648 uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK,
649 pPage->idx,
650 iShw2);
651 ASMAtomicWriteSize(&uShw.pPDPT->a[iShw2].u, 0);
652 }
653 }
654 }
655 }
656 break;
657 }
658
659#ifndef IN_RC
660 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
661 {
662 uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
663 const unsigned iShw = off / sizeof(X86PDEPAE);
664 Assert(!(uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING));
665 if (uShw.pPDPae->a[iShw].n.u1Present)
666 {
667 LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
668 pgmPoolFree(pVM,
669 uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
670 pPage->idx,
671 iShw);
672 ASMAtomicWriteSize(&uShw.pPDPae->a[iShw].u, 0);
673 }
674 /* paranoia / a bit assumptive. */
675 if ( pDis
676 && (off & 7)
677 && (off & 7) + cbWrite > sizeof(X86PDEPAE))
678 {
679 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
680 AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
681
682 Assert(!(uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING));
683 if (uShw.pPDPae->a[iShw2].n.u1Present)
684 {
685 LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
686 pgmPoolFree(pVM,
687 uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
688 pPage->idx,
689 iShw2);
690 ASMAtomicWriteSize(&uShw.pPDPae->a[iShw2].u, 0);
691 }
692 }
693 break;
694 }
695
696 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
697 {
698 /*
699 * Hopefully this doesn't happen very often:
700 * - messing with the bits of pd pointers without changing the physical address
701 */
702 if (!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3))
703 {
704 uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
705 const unsigned iShw = off / sizeof(X86PDPE);
706 if (uShw.pPDPT->a[iShw].n.u1Present)
707 {
708 LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
709 pgmPoolFree(pVM, uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK, pPage->idx, iShw);
710 ASMAtomicWriteSize(&uShw.pPDPT->a[iShw].u, 0);
711 }
712 /* paranoia / a bit assumptive. */
713 if ( pDis
714 && (off & 7)
715 && (off & 7) + cbWrite > sizeof(X86PDPE))
716 {
717 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
718 if (uShw.pPDPT->a[iShw2].n.u1Present)
719 {
720 LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
721 pgmPoolFree(pVM, uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK, pPage->idx, iShw2);
722 ASMAtomicWriteSize(&uShw.pPDPT->a[iShw2].u, 0);
723 }
724 }
725 }
726 break;
727 }
728
729 case PGMPOOLKIND_64BIT_PML4:
730 {
731 /*
732 * Hopefully this doesn't happen very often:
733 * - messing with the bits of pd pointers without changing the physical address
734 */
735 if (!VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3))
736 {
737 uShw.pv = PGMPOOL_PAGE_2_LOCKED_PTR(pVM, pPage);
738 const unsigned iShw = off / sizeof(X86PDPE);
739 if (uShw.pPML4->a[iShw].n.u1Present)
740 {
741 LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
742 pgmPoolFree(pVM, uShw.pPML4->a[iShw].u & X86_PML4E_PG_MASK, pPage->idx, iShw);
743 ASMAtomicWriteSize(&uShw.pPML4->a[iShw].u, 0);
744 }
745 /* paranoia / a bit assumptive. */
746 if ( pDis
747 && (off & 7)
748 && (off & 7) + cbWrite > sizeof(X86PDPE))
749 {
750 const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PML4E);
751 if (uShw.pPML4->a[iShw2].n.u1Present)
752 {
753 LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
754 pgmPoolFree(pVM, uShw.pPML4->a[iShw2].u & X86_PML4E_PG_MASK, pPage->idx, iShw2);
755 ASMAtomicWriteSize(&uShw.pPML4->a[iShw2].u, 0);
756 }
757 }
758 }
759 break;
760 }
761#endif /* IN_RING0 */
762
763 default:
764 AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
765 }
766 PGMPOOL_UNLOCK_PTR(pVM, uShw.pv);
767
768 /* next */
769 if (pPage->iMonitoredNext == NIL_PGMPOOL_IDX)
770 return;
771 pPage = &pPool->aPages[pPage->iMonitoredNext];
772 }
773}
774
775# ifndef IN_RING3
776/**
777 * Checks if a access could be a fork operation in progress.
778 *
779 * Meaning, that the guest is setting up the parent process for Copy-On-Write.
780 *
781 * @returns true if it's likly that we're forking, otherwise false.
782 * @param pPool The pool.
783 * @param pDis The disassembled instruction.
784 * @param offFault The access offset.
785 */
786DECLINLINE(bool) pgmPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pDis, unsigned offFault)
787{
788 /*
789 * i386 linux is using btr to clear X86_PTE_RW.
790 * The functions involved are (2.6.16 source inspection):
791 * clear_bit
792 * ptep_set_wrprotect
793 * copy_one_pte
794 * copy_pte_range
795 * copy_pmd_range
796 * copy_pud_range
797 * copy_page_range
798 * dup_mmap
799 * dup_mm
800 * copy_mm
801 * copy_process
802 * do_fork
803 */
804 if ( pDis->pCurInstr->opcode == OP_BTR
805 && !(offFault & 4)
806 /** @todo Validate that the bit index is X86_PTE_RW. */
807 )
808 {
809 STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,Fork));
810 return true;
811 }
812 return false;
813}
814
815
816/**
817 * Determine whether the page is likely to have been reused.
818 *
819 * @returns true if we consider the page as being reused for a different purpose.
820 * @returns false if we consider it to still be a paging page.
821 * @param pVM VM Handle.
822 * @param pVCpu VMCPU Handle.
823 * @param pRegFrame Trap register frame.
824 * @param pDis The disassembly info for the faulting instruction.
825 * @param pvFault The fault address.
826 *
827 * @remark The REP prefix check is left to the caller because of STOSD/W.
828 */
829DECLINLINE(bool) pgmPoolMonitorIsReused(PVM pVM, PVMCPU pVCpu, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pDis, RTGCPTR pvFault)
830{
831#ifndef IN_RC
832 /** @todo could make this general, faulting close to rsp should be a safe reuse heuristic. */
833 if ( HWACCMHasPendingIrq(pVM)
834 && (pRegFrame->rsp - pvFault) < 32)
835 {
836 /* Fault caused by stack writes while trying to inject an interrupt event. */
837 Log(("pgmPoolMonitorIsReused: reused %RGv for interrupt stack (rsp=%RGv).\n", pvFault, pRegFrame->rsp));
838 return true;
839 }
840#else
841 NOREF(pVM); NOREF(pvFault);
842#endif
843
844 LogFlow(("Reused instr %RGv %d at %RGv param1.flags=%x param1.reg=%d\n", pRegFrame->rip, pDis->pCurInstr->opcode, pvFault, pDis->param1.flags, pDis->param1.base.reg_gen));
845
846 /* Non-supervisor mode write means it's used for something else. */
847 if (CPUMGetGuestCPL(pVCpu, pRegFrame) != 0)
848 return true;
849
850 switch (pDis->pCurInstr->opcode)
851 {
852 /* call implies the actual push of the return address faulted */
853 case OP_CALL:
854 Log4(("pgmPoolMonitorIsReused: CALL\n"));
855 return true;
856 case OP_PUSH:
857 Log4(("pgmPoolMonitorIsReused: PUSH\n"));
858 return true;
859 case OP_PUSHF:
860 Log4(("pgmPoolMonitorIsReused: PUSHF\n"));
861 return true;
862 case OP_PUSHA:
863 Log4(("pgmPoolMonitorIsReused: PUSHA\n"));
864 return true;
865 case OP_FXSAVE:
866 Log4(("pgmPoolMonitorIsReused: FXSAVE\n"));
867 return true;
868 case OP_MOVNTI: /* solaris - block_zero_no_xmm */
869 Log4(("pgmPoolMonitorIsReused: MOVNTI\n"));
870 return true;
871 case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
872 Log4(("pgmPoolMonitorIsReused: MOVNTDQ\n"));
873 return true;
874 case OP_MOVSWD:
875 case OP_STOSWD:
876 if ( pDis->prefix == (PREFIX_REP|PREFIX_REX)
877 && pRegFrame->rcx >= 0x40
878 )
879 {
880 Assert(pDis->mode == CPUMODE_64BIT);
881
882 Log(("pgmPoolMonitorIsReused: OP_STOSQ\n"));
883 return true;
884 }
885 return false;
886 }
887 if ( ( (pDis->param1.flags & USE_REG_GEN32)
888 || (pDis->param1.flags & USE_REG_GEN64))
889 && (pDis->param1.base.reg_gen == USE_REG_ESP))
890 {
891 Log4(("pgmPoolMonitorIsReused: ESP\n"));
892 return true;
893 }
894
895 return false;
896}
897
898/**
899 * Flushes the page being accessed.
900 *
901 * @returns VBox status code suitable for scheduling.
902 * @param pVM The VM handle.
903 * @param pVCpu The VMCPU handle.
904 * @param pPool The pool.
905 * @param pPage The pool page (head).
906 * @param pDis The disassembly of the write instruction.
907 * @param pRegFrame The trap register frame.
908 * @param GCPhysFault The fault address as guest physical address.
909 * @param pvFault The fault address.
910 */
911static int pgmPoolAccessHandlerFlush(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
912 PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
913{
914 /*
915 * First, do the flushing.
916 */
917 int rc = pgmPoolMonitorChainFlush(pPool, pPage);
918
919 /*
920 * Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection). Must do this in raw mode (!); XP boot will fail otherwise
921 */
922 uint32_t cbWritten;
923 int rc2 = EMInterpretInstructionCPU(pVM, pVCpu, pDis, pRegFrame, pvFault, &cbWritten);
924 if (RT_SUCCESS(rc2))
925 pRegFrame->rip += pDis->opsize;
926 else if (rc2 == VERR_EM_INTERPRETER)
927 {
928#ifdef IN_RC
929 if (PATMIsPatchGCAddr(pVM, (RTRCPTR)pRegFrame->eip))
930 {
931 LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for patch code %04x:%RGv, ignoring.\n",
932 pRegFrame->cs, (RTGCPTR)pRegFrame->eip));
933 rc = VINF_SUCCESS;
934 STAM_COUNTER_INC(&pPool->StatMonitorRZIntrFailPatch2);
935 }
936 else
937#endif
938 {
939 rc = VINF_EM_RAW_EMULATE_INSTR;
940 STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
941 }
942 }
943 else
944 rc = rc2;
945
946 /* See use in pgmPoolAccessHandlerSimple(). */
947 PGM_INVL_VCPU_TLBS(pVCpu);
948 LogFlow(("pgmPoolAccessHandlerPT: returns %Rrc (flushed)\n", rc));
949 return rc;
950}
951
952/**
953 * Handles the STOSD write accesses.
954 *
955 * @returns VBox status code suitable for scheduling.
956 * @param pVM The VM handle.
957 * @param pPool The pool.
958 * @param pPage The pool page (head).
959 * @param pDis The disassembly of the write instruction.
960 * @param pRegFrame The trap register frame.
961 * @param GCPhysFault The fault address as guest physical address.
962 * @param pvFault The fault address.
963 */
964DECLINLINE(int) pgmPoolAccessHandlerSTOSD(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
965 PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
966{
967 unsigned uIncrement = pDis->param1.size;
968
969 Assert(pDis->mode == CPUMODE_32BIT || pDis->mode == CPUMODE_64BIT);
970 Assert(pRegFrame->rcx <= 0x20);
971
972#ifdef VBOX_STRICT
973 if (pDis->opmode == CPUMODE_32BIT)
974 Assert(uIncrement == 4);
975 else
976 Assert(uIncrement == 8);
977#endif
978
979 Log3(("pgmPoolAccessHandlerSTOSD\n"));
980
981 /*
982 * Increment the modification counter and insert it into the list
983 * of modified pages the first time.
984 */
985 if (!pPage->cModifications++)
986 pgmPoolMonitorModifiedInsert(pPool, pPage);
987
988 /*
989 * Execute REP STOSD.
990 *
991 * This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
992 * write situation, meaning that it's safe to write here.
993 */
994 PVMCPU pVCpu = VMMGetCpu(pPool->CTX_SUFF(pVM));
995 RTGCUINTPTR pu32 = (RTGCUINTPTR)pvFault;
996 while (pRegFrame->rcx)
997 {
998#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
999 uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
1000 pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
1001 PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
1002#else
1003 pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, (RTGCPTR)pu32, NULL);
1004#endif
1005#ifdef IN_RC
1006 *(uint32_t *)pu32 = pRegFrame->eax;
1007#else
1008 PGMPhysSimpleWriteGCPhys(pVM, GCPhysFault, &pRegFrame->rax, uIncrement);
1009#endif
1010 pu32 += uIncrement;
1011 GCPhysFault += uIncrement;
1012 pRegFrame->rdi += uIncrement;
1013 pRegFrame->rcx--;
1014 }
1015 pRegFrame->rip += pDis->opsize;
1016
1017#ifdef IN_RC
1018 /* See use in pgmPoolAccessHandlerSimple(). */
1019 PGM_INVL_VCPU_TLBS(pVCpu);
1020#endif
1021
1022 LogFlow(("pgmPoolAccessHandlerSTOSD: returns\n"));
1023 return VINF_SUCCESS;
1024}
1025
1026
1027/**
1028 * Handles the simple write accesses.
1029 *
1030 * @returns VBox status code suitable for scheduling.
1031 * @param pVM The VM handle.
1032 * @param pVCpu The VMCPU handle.
1033 * @param pPool The pool.
1034 * @param pPage The pool page (head).
1035 * @param pDis The disassembly of the write instruction.
1036 * @param pRegFrame The trap register frame.
1037 * @param GCPhysFault The fault address as guest physical address.
1038 * @param pvFault The fault address.
1039 * @param pfReused Reused state (out)
1040 */
1041DECLINLINE(int) pgmPoolAccessHandlerSimple(PVM pVM, PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
1042 PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault, bool *pfReused)
1043{
1044 Log3(("pgmPoolAccessHandlerSimple\n"));
1045 /*
1046 * Increment the modification counter and insert it into the list
1047 * of modified pages the first time.
1048 */
1049 if (!pPage->cModifications++)
1050 pgmPoolMonitorModifiedInsert(pPool, pPage);
1051
1052 /*
1053 * Clear all the pages. ASSUMES that pvFault is readable.
1054 */
1055#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
1056 uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
1057 pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, pDis);
1058 PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
1059#else
1060 pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, pvFault, pDis);
1061#endif
1062
1063 /*
1064 * Interpret the instruction.
1065 */
1066 uint32_t cb;
1067 int rc = EMInterpretInstructionCPU(pVM, pVCpu, pDis, pRegFrame, pvFault, &cb);
1068 if (RT_SUCCESS(rc))
1069 pRegFrame->rip += pDis->opsize;
1070 else if (rc == VERR_EM_INTERPRETER)
1071 {
1072 LogFlow(("pgmPoolAccessHandlerPTWorker: Interpretation failed for %04x:%RGv - opcode=%d\n",
1073 pRegFrame->cs, (RTGCPTR)pRegFrame->rip, pDis->pCurInstr->opcode));
1074 rc = VINF_EM_RAW_EMULATE_INSTR;
1075 STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,EmulateInstr));
1076 }
1077
1078#if 0 /* experimental code */
1079 if (rc == VINF_SUCCESS)
1080 {
1081 switch (pPage->enmKind)
1082 {
1083 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1084 {
1085 X86PTEPAE GstPte;
1086 int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvFault, GCPhysFault, sizeof(GstPte));
1087 AssertRC(rc);
1088
1089 /* Check the new value written by the guest. If present and with a bogus physical address, then
1090 * it's fairly safe to assume the guest is reusing the PT.
1091 */
1092 if (GstPte.n.u1Present)
1093 {
1094 RTHCPHYS HCPhys = -1;
1095 int rc = PGMPhysGCPhys2HCPhys(pVM, GstPte.u & X86_PTE_PAE_PG_MASK, &HCPhys);
1096 if (rc != VINF_SUCCESS)
1097 {
1098 *pfReused = true;
1099 STAM_COUNTER_INC(&pPool->StatForceFlushReused);
1100 }
1101 }
1102 break;
1103 }
1104 }
1105 }
1106#endif
1107
1108#ifdef IN_RC
1109 /*
1110 * Quick hack, with logging enabled we're getting stale
1111 * code TLBs but no data TLB for EIP and crash in EMInterpretDisasOne.
1112 * Flushing here is BAD and expensive, I think EMInterpretDisasOne will
1113 * have to be fixed to support this. But that'll have to wait till next week.
1114 *
1115 * An alternative is to keep track of the changed PTEs together with the
1116 * GCPhys from the guest PT. This may proove expensive though.
1117 *
1118 * At the moment, it's VITAL that it's done AFTER the instruction interpreting
1119 * because we need the stale TLBs in some cases (XP boot). This MUST be fixed properly!
1120 */
1121 PGM_INVL_VCPU_TLBS(pVCpu);
1122#endif
1123
1124 LogFlow(("pgmPoolAccessHandlerSimple: returns %Rrc cb=%d\n", rc, cb));
1125 return rc;
1126}
1127
1128/**
1129 * \#PF Handler callback for PT write accesses.
1130 *
1131 * @returns VBox status code (appropriate for GC return).
1132 * @param pVM VM Handle.
1133 * @param uErrorCode CPU Error code.
1134 * @param pRegFrame Trap register frame.
1135 * NULL on DMA and other non CPU access.
1136 * @param pvFault The fault address (cr2).
1137 * @param GCPhysFault The GC physical address corresponding to pvFault.
1138 * @param pvUser User argument.
1139 */
1140DECLEXPORT(int) pgmPoolAccessHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
1141{
1142 STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), a);
1143 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1144 PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)pvUser;
1145 PVMCPU pVCpu = VMMGetCpu(pVM);
1146 unsigned cMaxModifications;
1147 bool fForcedFlush = false;
1148
1149 LogFlow(("pgmPoolAccessHandler: pvFault=%RGv pPage=%p:{.idx=%d} GCPhysFault=%RGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
1150
1151 pgmLock(pVM);
1152 if (PHYS_PAGE_ADDRESS(GCPhysFault) != PHYS_PAGE_ADDRESS(pPage->GCPhys))
1153 {
1154 /* Pool page changed while we were waiting for the lock; ignore. */
1155 Log(("CPU%d: pgmPoolAccessHandler pgm pool page for %RGp changed (to %RGp) while waiting!\n", pVCpu->idCpu, PHYS_PAGE_ADDRESS(GCPhysFault), PHYS_PAGE_ADDRESS(pPage->GCPhys)));
1156 STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,Handled), a);
1157 pgmUnlock(pVM);
1158 return VINF_SUCCESS;
1159 }
1160
1161 /*
1162 * Disassemble the faulting instruction.
1163 */
1164 PDISCPUSTATE pDis = &pVCpu->pgm.s.DisState;
1165 int rc = EMInterpretDisasOne(pVM, pVCpu, pRegFrame, pDis, NULL);
1166 AssertReturnStmt(rc == VINF_SUCCESS, pgmUnlock(pVM), rc);
1167
1168 Assert(pPage->enmKind != PGMPOOLKIND_FREE);
1169
1170 /*
1171 * We should ALWAYS have the list head as user parameter. This
1172 * is because we use that page to record the changes.
1173 */
1174 Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
1175#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
1176 Assert(!pPage->fDirty);
1177#endif
1178
1179 /* Maximum nr of modifications depends on the guest mode. */
1180 if (pDis->mode == CPUMODE_32BIT)
1181 cMaxModifications = 48;
1182 else
1183 cMaxModifications = 24;
1184
1185 /*
1186 * Incremental page table updates should weight more than random ones.
1187 * (Only applies when started from offset 0)
1188 */
1189 pVCpu->pgm.s.cPoolAccessHandler++;
1190 if ( pPage->pvLastAccessHandlerRip >= pRegFrame->rip - 0x40 /* observed loops in Windows 7 x64 */
1191 && pPage->pvLastAccessHandlerRip < pRegFrame->rip + 0x40
1192 && pvFault == (pPage->pvLastAccessHandlerFault + pDis->param1.size)
1193 && pVCpu->pgm.s.cPoolAccessHandler == (pPage->cLastAccessHandlerCount + 1))
1194 {
1195 Log(("Possible page reuse cMods=%d -> %d (locked=%d type=%s)\n", pPage->cModifications, pPage->cModifications * 2, pgmPoolIsPageLocked(&pVM->pgm.s, pPage), pgmPoolPoolKindToStr(pPage->enmKind)));
1196 pPage->cModifications = pPage->cModifications * 2;
1197 pPage->pvLastAccessHandlerFault = pvFault;
1198 pPage->cLastAccessHandlerCount = pVCpu->pgm.s.cPoolAccessHandler;
1199 if (pPage->cModifications >= cMaxModifications)
1200 {
1201 STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FlushReinit));
1202 fForcedFlush = true;
1203 }
1204 }
1205
1206 if (pPage->cModifications >= cMaxModifications)
1207 Log(("Mod overflow %VGv cMods=%d (locked=%d type=%s)\n", pvFault, pPage->cModifications, pgmPoolIsPageLocked(&pVM->pgm.s, pPage), pgmPoolPoolKindToStr(pPage->enmKind)));
1208
1209 /*
1210 * Check if it's worth dealing with.
1211 */
1212 bool fReused = false;
1213 bool fNotReusedNotForking = false;
1214 if ( ( pPage->cModifications < cMaxModifications /** @todo #define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
1215 || pgmPoolIsPageLocked(&pVM->pgm.s, pPage)
1216 )
1217 && !(fReused = pgmPoolMonitorIsReused(pVM, pVCpu, pRegFrame, pDis, pvFault))
1218 && !pgmPoolMonitorIsForking(pPool, pDis, GCPhysFault & PAGE_OFFSET_MASK))
1219 {
1220 /*
1221 * Simple instructions, no REP prefix.
1222 */
1223 if (!(pDis->prefix & (PREFIX_REP | PREFIX_REPNE)))
1224 {
1225 rc = pgmPoolAccessHandlerSimple(pVM, pVCpu, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault, &fReused);
1226 if (fReused)
1227 goto flushPage;
1228
1229 /* A mov instruction to change the first page table entry will be remembered so we can detect
1230 * full page table changes early on. This will reduce the amount of unnecessary traps we'll take.
1231 */
1232 if ( rc == VINF_SUCCESS
1233 && pDis->pCurInstr->opcode == OP_MOV
1234 && (pvFault & PAGE_OFFSET_MASK) == 0)
1235 {
1236 pPage->pvLastAccessHandlerFault = pvFault;
1237 pPage->cLastAccessHandlerCount = pVCpu->pgm.s.cPoolAccessHandler;
1238 pPage->pvLastAccessHandlerRip = pRegFrame->rip;
1239 /* Make sure we don't kick out a page too quickly. */
1240 if (pPage->cModifications > 8)
1241 pPage->cModifications = 2;
1242 }
1243 else
1244 if (pPage->pvLastAccessHandlerFault == pvFault)
1245 {
1246 /* ignore the 2nd write to this page table entry. */
1247 pPage->cLastAccessHandlerCount = pVCpu->pgm.s.cPoolAccessHandler;
1248 }
1249 else
1250 {
1251 pPage->pvLastAccessHandlerFault = 0;
1252 pPage->pvLastAccessHandlerRip = 0;
1253 }
1254
1255 STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,Handled), a);
1256 pgmUnlock(pVM);
1257 return rc;
1258 }
1259
1260 /*
1261 * Windows is frequently doing small memset() operations (netio test 4k+).
1262 * We have to deal with these or we'll kill the cache and performance.
1263 */
1264 if ( pDis->pCurInstr->opcode == OP_STOSWD
1265 && !pRegFrame->eflags.Bits.u1DF
1266 && pDis->opmode == pDis->mode
1267 && pDis->addrmode == pDis->mode)
1268 {
1269 bool fValidStosd = false;
1270
1271 if ( pDis->mode == CPUMODE_32BIT
1272 && pDis->prefix == PREFIX_REP
1273 && pRegFrame->ecx <= 0x20
1274 && pRegFrame->ecx * 4 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
1275 && !((uintptr_t)pvFault & 3)
1276 && (pRegFrame->eax == 0 || pRegFrame->eax == 0x80) /* the two values observed. */
1277 )
1278 {
1279 fValidStosd = true;
1280 pRegFrame->rcx &= 0xffffffff; /* paranoia */
1281 }
1282 else
1283 if ( pDis->mode == CPUMODE_64BIT
1284 && pDis->prefix == (PREFIX_REP | PREFIX_REX)
1285 && pRegFrame->rcx <= 0x20
1286 && pRegFrame->rcx * 8 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
1287 && !((uintptr_t)pvFault & 7)
1288 && (pRegFrame->rax == 0 || pRegFrame->rax == 0x80) /* the two values observed. */
1289 )
1290 {
1291 fValidStosd = true;
1292 }
1293
1294 if (fValidStosd)
1295 {
1296 rc = pgmPoolAccessHandlerSTOSD(pVM, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault);
1297 STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,RepStosd), a);
1298 pgmUnlock(pVM);
1299 return rc;
1300 }
1301 }
1302
1303 /* REP prefix, don't bother. */
1304 STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,RepPrefix));
1305 Log4(("pgmPoolAccessHandler: eax=%#x ecx=%#x edi=%#x esi=%#x rip=%RGv opcode=%d prefix=%#x\n",
1306 pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, (RTGCPTR)pRegFrame->rip, pDis->pCurInstr->opcode, pDis->prefix));
1307 fNotReusedNotForking = true;
1308 }
1309
1310#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
1311 /* E.g. Windows 7 x64 initializes page tables and touches some pages in the table during the process. This
1312 * leads to pgm pool trashing and an excessive amount of write faults due to page monitoring.
1313 */
1314 if ( pPage->cModifications >= cMaxModifications
1315 && !fForcedFlush
1316 && pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT
1317 && ( fNotReusedNotForking
1318 || ( !pgmPoolMonitorIsReused(pVM, pVCpu, pRegFrame, pDis, pvFault)
1319 && !pgmPoolMonitorIsForking(pPool, pDis, GCPhysFault & PAGE_OFFSET_MASK))
1320 )
1321 )
1322 {
1323 Assert(!pgmPoolIsPageLocked(&pVM->pgm.s, pPage));
1324 Assert(pPage->fDirty == false);
1325
1326 /* Flush any monitored duplicates as we will disable write protection. */
1327 if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
1328 || pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
1329 {
1330 PPGMPOOLPAGE pPageHead = pPage;
1331
1332 /* Find the monitor head. */
1333 while (pPageHead->iMonitoredPrev != NIL_PGMPOOL_IDX)
1334 pPageHead = &pPool->aPages[pPageHead->iMonitoredPrev];
1335
1336 while (pPageHead)
1337 {
1338 unsigned idxNext = pPageHead->iMonitoredNext;
1339
1340 if (pPageHead != pPage)
1341 {
1342 STAM_COUNTER_INC(&pPool->StatDirtyPageDupFlush);
1343 Log(("Flush duplicate page idx=%d GCPhys=%RGp type=%s\n", pPageHead->idx, pPageHead->GCPhys, pgmPoolPoolKindToStr(pPageHead->enmKind)));
1344 int rc2 = pgmPoolFlushPage(pPool, pPageHead);
1345 AssertRC(rc2);
1346 }
1347
1348 if (idxNext == NIL_PGMPOOL_IDX)
1349 break;
1350
1351 pPageHead = &pPool->aPages[idxNext];
1352 }
1353 }
1354
1355 /* The flushing above might fail for locked pages, so double check. */
1356 if ( pPage->iMonitoredNext == NIL_PGMPOOL_IDX
1357 && pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
1358 {
1359 /* Temporarily allow write access to the page table again. */
1360 rc = PGMHandlerPhysicalPageTempOff(pVM, pPage->GCPhys, pPage->GCPhys);
1361 if (rc == VINF_SUCCESS)
1362 {
1363 rc = PGMShwModifyPage(pVCpu, pvFault, 1, X86_PTE_RW, ~(uint64_t)X86_PTE_RW);
1364 AssertMsg(rc == VINF_SUCCESS
1365 /* In the SMP case the page table might be removed while we wait for the PGM lock in the trap handler. */
1366 || rc == VERR_PAGE_TABLE_NOT_PRESENT
1367 || rc == VERR_PAGE_NOT_PRESENT,
1368 ("PGMShwModifyPage -> GCPtr=%RGv rc=%d\n", pvFault, rc));
1369
1370 pgmPoolAddDirtyPage(pVM, pPool, pPage);
1371 pPage->pvDirtyFault = pvFault;
1372
1373 STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), a);
1374 pgmUnlock(pVM);
1375 return rc;
1376 }
1377 }
1378 }
1379#endif /* PGMPOOL_WITH_OPTIMIZED_DIRTY_PT */
1380
1381flushPage:
1382 /*
1383 * Not worth it, so flush it.
1384 *
1385 * If we considered it to be reused, don't go back to ring-3
1386 * to emulate failed instructions since we usually cannot
1387 * interpret then. This may be a bit risky, in which case
1388 * the reuse detection must be fixed.
1389 */
1390 rc = pgmPoolAccessHandlerFlush(pVM, pVCpu, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault);
1391 if (rc == VINF_EM_RAW_EMULATE_INSTR && fReused)
1392 rc = VINF_SUCCESS;
1393 STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,FlushPage), a);
1394 pgmUnlock(pVM);
1395 return rc;
1396}
1397
1398# endif /* !IN_RING3 */
1399
1400# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
1401
1402# ifdef VBOX_STRICT
1403/**
1404 * Check references to guest physical memory in a PAE / PAE page table.
1405 *
1406 * @param pPool The pool.
1407 * @param pPage The page.
1408 * @param pShwPT The shadow page table (mapping of the page).
1409 * @param pGstPT The guest page table.
1410 */
1411DECLINLINE(void) pgmPoolTrackCheckPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
1412{
1413 unsigned cErrors = 0;
1414#ifdef VBOX_STRICT
1415 for (unsigned i = 0; i < RT_MIN(RT_ELEMENTS(pShwPT->a), pPage->iFirstPresent); i++)
1416 AssertMsg(!pShwPT->a[i].n.u1Present, ("Unexpected PTE: idx=%d %RX64 (first=%d)\n", i, pShwPT->a[i].u, pPage->iFirstPresent));
1417#endif
1418 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
1419 {
1420 if (pShwPT->a[i].n.u1Present)
1421 {
1422 RTHCPHYS HCPhys = -1;
1423 int rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PAE_PG_MASK, &HCPhys);
1424 if ( rc != VINF_SUCCESS
1425 || (pShwPT->a[i].u & X86_PTE_PAE_PG_MASK) != HCPhys)
1426 {
1427 RTHCPHYS HCPhysPT = -1;
1428 Log(("rc=%d idx=%d guest %RX64 shw=%RX64 vs %RHp\n", rc, i, pGstPT->a[i].u, pShwPT->a[i].u, HCPhys));
1429 cErrors++;
1430
1431 int rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pPage->GCPhys, &HCPhysPT);
1432 AssertRC(rc);
1433
1434 for (unsigned i = 0; i < pPool->cCurPages; i++)
1435 {
1436 PPGMPOOLPAGE pTempPage = &pPool->aPages[i];
1437
1438 if (pTempPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
1439 {
1440 PX86PTPAE pShwPT2 = (PX86PTPAE)PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pTempPage);
1441
1442 for (unsigned j = 0; j < RT_ELEMENTS(pShwPT->a); j++)
1443 {
1444 if ( pShwPT2->a[j].n.u1Present
1445 && pShwPT2->a[j].n.u1Write
1446 && ((pShwPT2->a[j].u & X86_PTE_PAE_PG_MASK) == HCPhysPT))
1447 {
1448 Log(("GCPhys=%RGp idx=%d %RX64 vs %RX64\n", pTempPage->GCPhys, j, pShwPT->a[j].u, pShwPT2->a[j].u));
1449 }
1450 }
1451 }
1452 }
1453 }
1454 }
1455 }
1456 Assert(!cErrors);
1457}
1458# endif /* VBOX_STRICT */
1459
1460/**
1461 * Clear references to guest physical memory in a PAE / PAE page table.
1462 *
1463 * @returns nr of changed PTEs
1464 * @param pPool The pool.
1465 * @param pPage The page.
1466 * @param pShwPT The shadow page table (mapping of the page).
1467 * @param pGstPT The guest page table.
1468 * @param pOldGstPT The old cached guest page table.
1469 */
1470DECLINLINE(unsigned) pgmPoolTrackFlushPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT, PCX86PTPAE pOldGstPT)
1471{
1472 unsigned cChanged = 0;
1473
1474#ifdef VBOX_STRICT
1475 for (unsigned i = 0; i < RT_MIN(RT_ELEMENTS(pShwPT->a), pPage->iFirstPresent); i++)
1476 AssertMsg(!pShwPT->a[i].n.u1Present, ("Unexpected PTE: idx=%d %RX64 (first=%d)\n", i, pShwPT->a[i].u, pPage->iFirstPresent));
1477#endif
1478 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
1479 {
1480 if (pShwPT->a[i].n.u1Present)
1481 {
1482 /* The the old cached PTE is identical, then there's no need to flush the shadow copy. */
1483 if ((pGstPT->a[i].u & X86_PTE_PAE_PG_MASK) == (pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK))
1484 {
1485#ifdef VBOX_STRICT
1486 RTHCPHYS HCPhys = -1;
1487 int rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PAE_PG_MASK, &HCPhys);
1488 AssertMsg(rc == VINF_SUCCESS && (pShwPT->a[i].u & X86_PTE_PAE_PG_MASK) == HCPhys, ("rc=%d guest %RX64 old %RX64 shw=%RX64 vs %RHp\n", rc, pGstPT->a[i].u, pOldGstPT->a[i].u, pShwPT->a[i].u, HCPhys));
1489#endif
1490 uint64_t uHostAttr = pShwPT->a[i].u & (X86_PTE_P | X86_PTE_US | X86_PTE_A | X86_PTE_D | X86_PTE_G | X86_PTE_PAE_NX);
1491 bool fHostRW = !!(pShwPT->a[i].u & X86_PTE_RW);
1492 uint64_t uGuestAttr = pGstPT->a[i].u & (X86_PTE_P | X86_PTE_US | X86_PTE_A | X86_PTE_D | X86_PTE_G | X86_PTE_PAE_NX);
1493 bool fGuestRW = !!(pGstPT->a[i].u & X86_PTE_RW);
1494
1495 if ( uHostAttr == uGuestAttr
1496 && fHostRW <= fGuestRW)
1497 continue;
1498 }
1499 cChanged++;
1500 /* Something was changed, so flush it. */
1501 Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX64 hint=%RX64\n",
1502 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
1503 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
1504 ASMAtomicWriteSize(&pShwPT->a[i].u, 0);
1505 }
1506 }
1507 return cChanged;
1508}
1509
1510
1511/**
1512 * Flush a dirty page
1513 *
1514 * @param pVM VM Handle.
1515 * @param pPool The pool.
1516 * @param idxSlot Dirty array slot index
1517 * @param fForceRemoval Force removal from the dirty page list
1518 */
1519static void pgmPoolFlushDirtyPage(PVM pVM, PPGMPOOL pPool, unsigned idxSlot, bool fForceRemoval = false)
1520{
1521 PPGMPOOLPAGE pPage;
1522 unsigned idxPage;
1523
1524 Assert(idxSlot < RT_ELEMENTS(pPool->aIdxDirtyPages));
1525 if (pPool->aIdxDirtyPages[idxSlot] == NIL_PGMPOOL_IDX)
1526 return;
1527
1528 idxPage = pPool->aIdxDirtyPages[idxSlot];
1529 AssertRelease(idxPage != NIL_PGMPOOL_IDX);
1530 pPage = &pPool->aPages[idxPage];
1531 Assert(pPage->idx == idxPage);
1532 Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX && pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
1533
1534 AssertMsg(pPage->fDirty, ("Page %RGp (slot=%d) not marked dirty!", pPage->GCPhys, idxSlot));
1535 Log(("Flush dirty page %RGp cMods=%d\n", pPage->GCPhys, pPage->cModifications));
1536
1537 /* Flush those PTEs that have changed. */
1538 STAM_PROFILE_START(&pPool->StatTrackDeref,a);
1539 void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
1540 void *pvGst;
1541 int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
1542 unsigned cChanges = pgmPoolTrackFlushPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst, (PCX86PTPAE)&pPool->aDirtyPages[idxSlot][0]);
1543 STAM_PROFILE_STOP(&pPool->StatTrackDeref,a);
1544
1545 /** Note: we might want to consider keeping the dirty page active in case there were many changes. */
1546
1547 /* Write protect the page again to catch all write accesses. */
1548 rc = PGMHandlerPhysicalReset(pVM, pPage->GCPhys);
1549 Assert(rc == VINF_SUCCESS);
1550 pPage->fDirty = false;
1551
1552#ifdef VBOX_STRICT
1553 uint64_t fFlags = 0;
1554 RTHCPHYS HCPhys;
1555 rc = PGMShwGetPage(VMMGetCpu(pVM), pPage->pvDirtyFault, &fFlags, &HCPhys);
1556 AssertMsg( ( rc == VINF_SUCCESS
1557 && (!(fFlags & X86_PTE_RW) || HCPhys != pPage->Core.Key))
1558 /* In the SMP case the page table might be removed while we wait for the PGM lock in the trap handler. */
1559 || rc == VERR_PAGE_TABLE_NOT_PRESENT
1560 || rc == VERR_PAGE_NOT_PRESENT,
1561 ("PGMShwGetPage -> GCPtr=%RGv rc=%d flags=%RX64\n", pPage->pvDirtyFault, rc, fFlags));
1562#endif
1563
1564 /* This page is likely to be modified again, so reduce the nr of modifications just a bit here. */
1565 Assert(pPage->cModifications);
1566 if (cChanges < 4)
1567 pPage->cModifications = 1; /* must use > 0 here */
1568 else
1569 pPage->cModifications = RT_MAX(1, pPage->cModifications / 2);
1570
1571 STAM_COUNTER_INC(&pPool->StatResetDirtyPages);
1572 if (pPool->cDirtyPages == RT_ELEMENTS(pPool->aIdxDirtyPages))
1573 pPool->idxFreeDirtyPage = idxSlot;
1574
1575 pPool->cDirtyPages--;
1576 pPool->aIdxDirtyPages[idxSlot] = NIL_PGMPOOL_IDX;
1577 Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aIdxDirtyPages));
1578 Log(("Removed dirty page %RGp cMods=%d\n", pPage->GCPhys, pPage->cModifications));
1579}
1580
1581# ifndef IN_RING3
1582/**
1583 * Add a new dirty page
1584 *
1585 * @param pVM VM Handle.
1586 * @param pPool The pool.
1587 * @param pPage The page.
1588 */
1589void pgmPoolAddDirtyPage(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1590{
1591 unsigned idxFree;
1592
1593 Assert(PGMIsLocked(pVM));
1594 AssertCompile(RT_ELEMENTS(pPool->aIdxDirtyPages) == 8 || RT_ELEMENTS(pPool->aIdxDirtyPages) == 16);
1595 Assert(!pPage->fDirty);
1596
1597 idxFree = pPool->idxFreeDirtyPage;
1598 Assert(idxFree < RT_ELEMENTS(pPool->aIdxDirtyPages));
1599 Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX && pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
1600
1601 if (pPool->cDirtyPages >= RT_ELEMENTS(pPool->aIdxDirtyPages))
1602 pgmPoolFlushDirtyPage(pVM, pPool, idxFree, true /* force removal */);
1603 Assert(pPool->cDirtyPages < RT_ELEMENTS(pPool->aIdxDirtyPages));
1604 AssertMsg(pPool->aIdxDirtyPages[idxFree] == NIL_PGMPOOL_IDX, ("idxFree=%d cDirtyPages=%d\n", idxFree, pPool->cDirtyPages));
1605
1606 Log(("Add dirty page %RGp (slot=%d)\n", pPage->GCPhys, idxFree));
1607
1608 /* Make a copy of the guest page table as we require valid GCPhys addresses when removing
1609 * references to physical pages. (the HCPhys linear lookup is *extremely* expensive!)
1610 */
1611 void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
1612 void *pvGst;
1613 int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
1614 memcpy(&pPool->aDirtyPages[idxFree][0], pvGst, PAGE_SIZE);
1615#ifdef VBOX_STRICT
1616 pgmPoolTrackCheckPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
1617#endif
1618
1619 STAM_COUNTER_INC(&pPool->StatDirtyPage);
1620 pPage->fDirty = true;
1621 pPage->idxDirty = idxFree;
1622 pPool->aIdxDirtyPages[idxFree] = pPage->idx;
1623 pPool->cDirtyPages++;
1624
1625 pPool->idxFreeDirtyPage = (pPool->idxFreeDirtyPage + 1) & (RT_ELEMENTS(pPool->aIdxDirtyPages) - 1);
1626 if ( pPool->cDirtyPages < RT_ELEMENTS(pPool->aIdxDirtyPages)
1627 && pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] != NIL_PGMPOOL_IDX)
1628 {
1629 unsigned i;
1630 for (i = 1; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
1631 {
1632 idxFree = (pPool->idxFreeDirtyPage + i) & (RT_ELEMENTS(pPool->aIdxDirtyPages) - 1);
1633 if (pPool->aIdxDirtyPages[idxFree] == NIL_PGMPOOL_IDX)
1634 {
1635 pPool->idxFreeDirtyPage = idxFree;
1636 break;
1637 }
1638 }
1639 Assert(i != RT_ELEMENTS(pPool->aIdxDirtyPages));
1640 }
1641
1642 Assert(pPool->cDirtyPages == RT_ELEMENTS(pPool->aIdxDirtyPages) || pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] == NIL_PGMPOOL_IDX);
1643 return;
1644}
1645# endif /* !IN_RING3 */
1646
1647/**
1648 * Check if the specified page is dirty (not write monitored)
1649 *
1650 * @return dirty or not
1651 * @param pVM VM Handle.
1652 * @param GCPhys Guest physical address
1653 */
1654bool pgmPoolIsDirtyPage(PVM pVM, RTGCPHYS GCPhys)
1655{
1656 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1657 Assert(PGMIsLocked(pVM));
1658 if (!pPool->cDirtyPages)
1659 return false;
1660
1661 GCPhys = GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
1662
1663 for (unsigned i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
1664 {
1665 if (pPool->aIdxDirtyPages[i] != NIL_PGMPOOL_IDX)
1666 {
1667 PPGMPOOLPAGE pPage;
1668 unsigned idxPage = pPool->aIdxDirtyPages[i];
1669
1670 pPage = &pPool->aPages[idxPage];
1671 if (pPage->GCPhys == GCPhys)
1672 return true;
1673 }
1674 }
1675 return false;
1676}
1677
1678/**
1679 * Reset all dirty pages by reinstating page monitoring.
1680 *
1681 * @param pVM VM Handle.
1682 * @param fForceRemoval Force removal of all dirty pages
1683 */
1684void pgmPoolResetDirtyPages(PVM pVM, bool fForceRemoval)
1685{
1686 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1687 Assert(PGMIsLocked(pVM));
1688 Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aIdxDirtyPages));
1689
1690 if (!pPool->cDirtyPages)
1691 return;
1692
1693 Log(("pgmPoolResetDirtyPages\n"));
1694 for (unsigned i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
1695 pgmPoolFlushDirtyPage(pVM, pPool, i, fForceRemoval);
1696
1697 pPool->idxFreeDirtyPage = 0;
1698 if ( pPool->cDirtyPages != RT_ELEMENTS(pPool->aIdxDirtyPages)
1699 && pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] != NIL_PGMPOOL_IDX)
1700 {
1701 unsigned i;
1702 for (i = 1; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
1703 {
1704 if (pPool->aIdxDirtyPages[i] == NIL_PGMPOOL_IDX)
1705 {
1706 pPool->idxFreeDirtyPage = i;
1707 break;
1708 }
1709 }
1710 AssertMsg(i != RT_ELEMENTS(pPool->aIdxDirtyPages), ("cDirtyPages %d", pPool->cDirtyPages));
1711 }
1712
1713 Assert(pPool->aIdxDirtyPages[pPool->idxFreeDirtyPage] == NIL_PGMPOOL_IDX || pPool->cDirtyPages == RT_ELEMENTS(pPool->aIdxDirtyPages));
1714 return;
1715}
1716# endif /* PGMPOOL_WITH_OPTIMIZED_DIRTY_PT */
1717#endif /* PGMPOOL_WITH_MONITORING */
1718
1719#ifdef PGMPOOL_WITH_CACHE
1720
1721/**
1722 * Inserts a page into the GCPhys hash table.
1723 *
1724 * @param pPool The pool.
1725 * @param pPage The page.
1726 */
1727DECLINLINE(void) pgmPoolHashInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1728{
1729 Log3(("pgmPoolHashInsert: %RGp\n", pPage->GCPhys));
1730 Assert(pPage->GCPhys != NIL_RTGCPHYS); Assert(pPage->iNext == NIL_PGMPOOL_IDX);
1731 uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
1732 pPage->iNext = pPool->aiHash[iHash];
1733 pPool->aiHash[iHash] = pPage->idx;
1734}
1735
1736
1737/**
1738 * Removes a page from the GCPhys hash table.
1739 *
1740 * @param pPool The pool.
1741 * @param pPage The page.
1742 */
1743DECLINLINE(void) pgmPoolHashRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1744{
1745 Log3(("pgmPoolHashRemove: %RGp\n", pPage->GCPhys));
1746 uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
1747 if (pPool->aiHash[iHash] == pPage->idx)
1748 pPool->aiHash[iHash] = pPage->iNext;
1749 else
1750 {
1751 uint16_t iPrev = pPool->aiHash[iHash];
1752 for (;;)
1753 {
1754 const int16_t i = pPool->aPages[iPrev].iNext;
1755 if (i == pPage->idx)
1756 {
1757 pPool->aPages[iPrev].iNext = pPage->iNext;
1758 break;
1759 }
1760 if (i == NIL_PGMPOOL_IDX)
1761 {
1762 AssertReleaseMsgFailed(("GCPhys=%RGp idx=%#x\n", pPage->GCPhys, pPage->idx));
1763 break;
1764 }
1765 iPrev = i;
1766 }
1767 }
1768 pPage->iNext = NIL_PGMPOOL_IDX;
1769}
1770
1771
1772/**
1773 * Frees up one cache page.
1774 *
1775 * @returns VBox status code.
1776 * @retval VINF_SUCCESS on success.
1777 * @param pPool The pool.
1778 * @param iUser The user index.
1779 */
1780static int pgmPoolCacheFreeOne(PPGMPOOL pPool, uint16_t iUser)
1781{
1782#ifndef IN_RC
1783 const PVM pVM = pPool->CTX_SUFF(pVM);
1784#endif
1785 Assert(pPool->iAgeHead != pPool->iAgeTail); /* We shouldn't be here if there < 2 cached entries! */
1786 STAM_COUNTER_INC(&pPool->StatCacheFreeUpOne);
1787
1788 /*
1789 * Select one page from the tail of the age list.
1790 */
1791 PPGMPOOLPAGE pPage;
1792 for (unsigned iLoop = 0; ; iLoop++)
1793 {
1794 uint16_t iToFree = pPool->iAgeTail;
1795 if (iToFree == iUser)
1796 iToFree = pPool->aPages[iToFree].iAgePrev;
1797/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
1798 if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
1799 {
1800 uint16_t i = pPool->aPages[iToFree].iAgePrev;
1801 for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
1802 {
1803 if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
1804 continue;
1805 iToFree = i;
1806 break;
1807 }
1808 }
1809*/
1810 Assert(iToFree != iUser);
1811 AssertRelease(iToFree != NIL_PGMPOOL_IDX);
1812 pPage = &pPool->aPages[iToFree];
1813
1814 /*
1815 * Reject any attempts at flushing the currently active shadow CR3 mapping.
1816 * Call pgmPoolCacheUsed to move the page to the head of the age list.
1817 */
1818 if (!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage))
1819 break;
1820 LogFlow(("pgmPoolCacheFreeOne: refuse CR3 mapping\n"));
1821 pgmPoolCacheUsed(pPool, pPage);
1822 AssertLogRelReturn(iLoop < 8192, VERR_INTERNAL_ERROR);
1823 }
1824
1825 /*
1826 * Found a usable page, flush it and return.
1827 */
1828 return pgmPoolFlushPage(pPool, pPage);
1829}
1830
1831
1832/**
1833 * Checks if a kind mismatch is really a page being reused
1834 * or if it's just normal remappings.
1835 *
1836 * @returns true if reused and the cached page (enmKind1) should be flushed
1837 * @returns false if not reused.
1838 * @param enmKind1 The kind of the cached page.
1839 * @param enmKind2 The kind of the requested page.
1840 */
1841static bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
1842{
1843 switch (enmKind1)
1844 {
1845 /*
1846 * Never reuse them. There is no remapping in non-paging mode.
1847 */
1848 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1849 case PGMPOOLKIND_32BIT_PD_PHYS:
1850 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1851 case PGMPOOLKIND_PAE_PD_PHYS:
1852 case PGMPOOLKIND_PAE_PDPT_PHYS:
1853 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1854 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1855 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1856 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1857 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1858 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT: /* never reuse them for other types */
1859 return false;
1860
1861 /*
1862 * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
1863 */
1864 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1865 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1866 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1867 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1868 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
1869 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
1870 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
1871 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
1872 case PGMPOOLKIND_32BIT_PD:
1873 case PGMPOOLKIND_PAE_PDPT:
1874 switch (enmKind2)
1875 {
1876 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1877 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1878 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1879 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1880 case PGMPOOLKIND_64BIT_PML4:
1881 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1882 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1883 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1884 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1885 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1886 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1887 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1888 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1889 return true;
1890 default:
1891 return false;
1892 }
1893
1894 /*
1895 * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
1896 */
1897 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1898 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1899 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1900 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1901 case PGMPOOLKIND_64BIT_PML4:
1902 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1903 switch (enmKind2)
1904 {
1905 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1906 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1907 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1908 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1909 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
1910 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
1911 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
1912 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
1913 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1914 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1915 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1916 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1917 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1918 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1919 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1920 return true;
1921 default:
1922 return false;
1923 }
1924
1925 /*
1926 * These cannot be flushed, and it's common to reuse the PDs as PTs.
1927 */
1928 case PGMPOOLKIND_ROOT_NESTED:
1929 return false;
1930
1931 default:
1932 AssertFatalMsgFailed(("enmKind1=%d\n", enmKind1));
1933 }
1934}
1935
1936
1937/**
1938 * Attempts to satisfy a pgmPoolAlloc request from the cache.
1939 *
1940 * @returns VBox status code.
1941 * @retval VINF_PGM_CACHED_PAGE on success.
1942 * @retval VERR_FILE_NOT_FOUND if not found.
1943 * @param pPool The pool.
1944 * @param GCPhys The GC physical address of the page we're gonna shadow.
1945 * @param enmKind The kind of mapping.
1946 * @param enmAccess Access type for the mapping (only relevant for big pages)
1947 * @param iUser The shadow page pool index of the user table.
1948 * @param iUserTable The index into the user table (shadowed).
1949 * @param ppPage Where to store the pointer to the page.
1950 */
1951static int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, PGMPOOLACCESS enmAccess, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
1952{
1953#ifndef IN_RC
1954 const PVM pVM = pPool->CTX_SUFF(pVM);
1955#endif
1956 /*
1957 * Look up the GCPhys in the hash.
1958 */
1959 unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
1960 Log3(("pgmPoolCacheAlloc: %RGp kind %s iUser=%x iUserTable=%x SLOT=%d\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable, i));
1961 if (i != NIL_PGMPOOL_IDX)
1962 {
1963 do
1964 {
1965 PPGMPOOLPAGE pPage = &pPool->aPages[i];
1966 Log4(("pgmPoolCacheAlloc: slot %d found page %RGp\n", i, pPage->GCPhys));
1967 if (pPage->GCPhys == GCPhys)
1968 {
1969 if ( (PGMPOOLKIND)pPage->enmKind == enmKind
1970 && (PGMPOOLACCESS)pPage->enmAccess == enmAccess)
1971 {
1972 /* Put it at the start of the use list to make sure pgmPoolTrackAddUser
1973 * doesn't flush it in case there are no more free use records.
1974 */
1975 pgmPoolCacheUsed(pPool, pPage);
1976
1977 int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
1978 if (RT_SUCCESS(rc))
1979 {
1980 Assert((PGMPOOLKIND)pPage->enmKind == enmKind);
1981 *ppPage = pPage;
1982 if (pPage->cModifications)
1983 pPage->cModifications = 1; /* reset counter (can't use 0, or else it will be reinserted in the modified list) */
1984 STAM_COUNTER_INC(&pPool->StatCacheHits);
1985 return VINF_PGM_CACHED_PAGE;
1986 }
1987 return rc;
1988 }
1989
1990 if ((PGMPOOLKIND)pPage->enmKind != enmKind)
1991 {
1992 /*
1993 * The kind is different. In some cases we should now flush the page
1994 * as it has been reused, but in most cases this is normal remapping
1995 * of PDs as PT or big pages using the GCPhys field in a slightly
1996 * different way than the other kinds.
1997 */
1998 if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
1999 {
2000 STAM_COUNTER_INC(&pPool->StatCacheKindMismatches);
2001 pgmPoolFlushPage(pPool, pPage);
2002 break;
2003 }
2004 }
2005 }
2006
2007 /* next */
2008 i = pPage->iNext;
2009 } while (i != NIL_PGMPOOL_IDX);
2010 }
2011
2012 Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%s\n", GCPhys, pgmPoolPoolKindToStr(enmKind)));
2013 STAM_COUNTER_INC(&pPool->StatCacheMisses);
2014 return VERR_FILE_NOT_FOUND;
2015}
2016
2017
2018/**
2019 * Inserts a page into the cache.
2020 *
2021 * @param pPool The pool.
2022 * @param pPage The cached page.
2023 * @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
2024 */
2025static void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
2026{
2027 /*
2028 * Insert into the GCPhys hash if the page is fit for that.
2029 */
2030 Assert(!pPage->fCached);
2031 if (fCanBeCached)
2032 {
2033 pPage->fCached = true;
2034 pgmPoolHashInsert(pPool, pPage);
2035 Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
2036 pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
2037 STAM_COUNTER_INC(&pPool->StatCacheCacheable);
2038 }
2039 else
2040 {
2041 Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
2042 pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
2043 STAM_COUNTER_INC(&pPool->StatCacheUncacheable);
2044 }
2045
2046 /*
2047 * Insert at the head of the age list.
2048 */
2049 pPage->iAgePrev = NIL_PGMPOOL_IDX;
2050 pPage->iAgeNext = pPool->iAgeHead;
2051 if (pPool->iAgeHead != NIL_PGMPOOL_IDX)
2052 pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
2053 else
2054 pPool->iAgeTail = pPage->idx;
2055 pPool->iAgeHead = pPage->idx;
2056}
2057
2058
2059/**
2060 * Flushes a cached page.
2061 *
2062 * @param pPool The pool.
2063 * @param pPage The cached page.
2064 */
2065static void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2066{
2067 Log3(("pgmPoolCacheFlushPage: %RGp\n", pPage->GCPhys));
2068
2069 /*
2070 * Remove the page from the hash.
2071 */
2072 if (pPage->fCached)
2073 {
2074 pPage->fCached = false;
2075 pgmPoolHashRemove(pPool, pPage);
2076 }
2077 else
2078 Assert(pPage->iNext == NIL_PGMPOOL_IDX);
2079
2080 /*
2081 * Remove it from the age list.
2082 */
2083 if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
2084 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
2085 else
2086 pPool->iAgeTail = pPage->iAgePrev;
2087 if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
2088 pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
2089 else
2090 pPool->iAgeHead = pPage->iAgeNext;
2091 pPage->iAgeNext = NIL_PGMPOOL_IDX;
2092 pPage->iAgePrev = NIL_PGMPOOL_IDX;
2093}
2094
2095#endif /* PGMPOOL_WITH_CACHE */
2096#ifdef PGMPOOL_WITH_MONITORING
2097
2098/**
2099 * Looks for pages sharing the monitor.
2100 *
2101 * @returns Pointer to the head page.
2102 * @returns NULL if not found.
2103 * @param pPool The Pool
2104 * @param pNewPage The page which is going to be monitored.
2105 */
2106static PPGMPOOLPAGE pgmPoolMonitorGetPageByGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pNewPage)
2107{
2108#ifdef PGMPOOL_WITH_CACHE
2109 /*
2110 * Look up the GCPhys in the hash.
2111 */
2112 RTGCPHYS GCPhys = pNewPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
2113 unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
2114 if (i == NIL_PGMPOOL_IDX)
2115 return NULL;
2116 do
2117 {
2118 PPGMPOOLPAGE pPage = &pPool->aPages[i];
2119 if ( pPage->GCPhys - GCPhys < PAGE_SIZE
2120 && pPage != pNewPage)
2121 {
2122 switch (pPage->enmKind)
2123 {
2124 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2125 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2126 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2127 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
2128 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
2129 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
2130 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
2131 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2132 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2133 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2134 case PGMPOOLKIND_64BIT_PML4:
2135 case PGMPOOLKIND_32BIT_PD:
2136 case PGMPOOLKIND_PAE_PDPT:
2137 {
2138 /* find the head */
2139 while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
2140 {
2141 Assert(pPage->iMonitoredPrev != pPage->idx);
2142 pPage = &pPool->aPages[pPage->iMonitoredPrev];
2143 }
2144 return pPage;
2145 }
2146
2147 /* ignore, no monitoring. */
2148 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2149 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2150 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2151 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2152 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2153 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2154 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2155 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2156 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2157 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
2158 case PGMPOOLKIND_ROOT_NESTED:
2159 case PGMPOOLKIND_PAE_PD_PHYS:
2160 case PGMPOOLKIND_PAE_PDPT_PHYS:
2161 case PGMPOOLKIND_32BIT_PD_PHYS:
2162 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
2163 break;
2164 default:
2165 AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
2166 }
2167 }
2168
2169 /* next */
2170 i = pPage->iNext;
2171 } while (i != NIL_PGMPOOL_IDX);
2172#endif
2173 return NULL;
2174}
2175
2176
2177/**
2178 * Enabled write monitoring of a guest page.
2179 *
2180 * @returns VBox status code.
2181 * @retval VINF_SUCCESS on success.
2182 * @param pPool The pool.
2183 * @param pPage The cached page.
2184 */
2185static int pgmPoolMonitorInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2186{
2187 LogFlow(("pgmPoolMonitorInsert %RGp\n", pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1)));
2188
2189 /*
2190 * Filter out the relevant kinds.
2191 */
2192 switch (pPage->enmKind)
2193 {
2194 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2195 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2196 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2197 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2198 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2199 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2200 case PGMPOOLKIND_64BIT_PML4:
2201 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
2202 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
2203 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
2204 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
2205 case PGMPOOLKIND_32BIT_PD:
2206 case PGMPOOLKIND_PAE_PDPT:
2207 break;
2208
2209 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2210 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2211 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2212 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2213 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2214 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2215 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2216 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2217 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2218 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
2219 case PGMPOOLKIND_ROOT_NESTED:
2220 /* Nothing to monitor here. */
2221 return VINF_SUCCESS;
2222
2223 case PGMPOOLKIND_32BIT_PD_PHYS:
2224 case PGMPOOLKIND_PAE_PDPT_PHYS:
2225 case PGMPOOLKIND_PAE_PD_PHYS:
2226 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
2227 /* Nothing to monitor here. */
2228 return VINF_SUCCESS;
2229#ifdef PGMPOOL_WITH_MIXED_PT_CR3
2230 break;
2231#else
2232 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
2233#endif
2234 default:
2235 AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
2236 }
2237
2238 /*
2239 * Install handler.
2240 */
2241 int rc;
2242 PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
2243 if (pPageHead)
2244 {
2245 Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
2246 Assert(pPageHead->iMonitoredPrev != pPage->idx);
2247
2248#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
2249 if (pPageHead->fDirty)
2250 pgmPoolFlushDirtyPage(pPool->CTX_SUFF(pVM), pPool, pPageHead->idxDirty, true /* force removal */);
2251#endif
2252
2253 pPage->iMonitoredPrev = pPageHead->idx;
2254 pPage->iMonitoredNext = pPageHead->iMonitoredNext;
2255 if (pPageHead->iMonitoredNext != NIL_PGMPOOL_IDX)
2256 pPool->aPages[pPageHead->iMonitoredNext].iMonitoredPrev = pPage->idx;
2257 pPageHead->iMonitoredNext = pPage->idx;
2258 rc = VINF_SUCCESS;
2259 }
2260 else
2261 {
2262 Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX); Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
2263 PVM pVM = pPool->CTX_SUFF(pVM);
2264 const RTGCPHYS GCPhysPage = pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
2265 rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
2266 GCPhysPage, GCPhysPage + (PAGE_SIZE - 1),
2267 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
2268 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
2269 pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
2270 pPool->pszAccessHandler);
2271 /** @todo we should probably deal with out-of-memory conditions here, but for now increasing
2272 * the heap size should suffice. */
2273 AssertFatalMsgRC(rc, ("PGMHandlerPhysicalRegisterEx %RGp failed with %Rrc\n", GCPhysPage, rc));
2274 Assert(!(VMMGetCpu(pVM)->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(VMMGetCpu(pVM), VMCPU_FF_PGM_SYNC_CR3));
2275 }
2276 pPage->fMonitored = true;
2277 return rc;
2278}
2279
2280
2281/**
2282 * Disables write monitoring of a guest page.
2283 *
2284 * @returns VBox status code.
2285 * @retval VINF_SUCCESS on success.
2286 * @param pPool The pool.
2287 * @param pPage The cached page.
2288 */
2289static int pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2290{
2291 /*
2292 * Filter out the relevant kinds.
2293 */
2294 switch (pPage->enmKind)
2295 {
2296 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2297 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2298 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2299 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2300 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2301 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2302 case PGMPOOLKIND_64BIT_PML4:
2303 case PGMPOOLKIND_32BIT_PD:
2304 case PGMPOOLKIND_PAE_PDPT:
2305 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
2306 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
2307 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
2308 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
2309 break;
2310
2311 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2312 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2313 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2314 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2315 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2316 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2317 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2318 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2319 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2320 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
2321 case PGMPOOLKIND_ROOT_NESTED:
2322 case PGMPOOLKIND_PAE_PD_PHYS:
2323 case PGMPOOLKIND_PAE_PDPT_PHYS:
2324 case PGMPOOLKIND_32BIT_PD_PHYS:
2325 /* Nothing to monitor here. */
2326 return VINF_SUCCESS;
2327
2328#ifdef PGMPOOL_WITH_MIXED_PT_CR3
2329 break;
2330#endif
2331 default:
2332 AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
2333 }
2334
2335 /*
2336 * Remove the page from the monitored list or uninstall it if last.
2337 */
2338 const PVM pVM = pPool->CTX_SUFF(pVM);
2339 int rc;
2340 if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
2341 || pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
2342 {
2343 if (pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
2344 {
2345 PPGMPOOLPAGE pNewHead = &pPool->aPages[pPage->iMonitoredNext];
2346 pNewHead->iMonitoredPrev = NIL_PGMPOOL_IDX;
2347 rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
2348 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pNewHead),
2349 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pNewHead),
2350 pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pNewHead),
2351 pPool->pszAccessHandler);
2352 AssertFatalRCSuccess(rc);
2353 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
2354 }
2355 else
2356 {
2357 pPool->aPages[pPage->iMonitoredPrev].iMonitoredNext = pPage->iMonitoredNext;
2358 if (pPage->iMonitoredNext != NIL_PGMPOOL_IDX)
2359 {
2360 pPool->aPages[pPage->iMonitoredNext].iMonitoredPrev = pPage->iMonitoredPrev;
2361 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
2362 }
2363 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
2364 rc = VINF_SUCCESS;
2365 }
2366 }
2367 else
2368 {
2369 rc = PGMHandlerPhysicalDeregister(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1));
2370 AssertFatalRC(rc);
2371#ifdef VBOX_STRICT
2372 PVMCPU pVCpu = VMMGetCpu(pVM);
2373#endif
2374 AssertMsg(!(pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_ISSET(pVCpu, VMCPU_FF_PGM_SYNC_CR3),
2375 ("%#x %#x\n", pVCpu->pgm.s.fSyncFlags, pVM->fGlobalForcedActions));
2376 }
2377 pPage->fMonitored = false;
2378
2379 /*
2380 * Remove it from the list of modified pages (if in it).
2381 */
2382 pgmPoolMonitorModifiedRemove(pPool, pPage);
2383
2384 return rc;
2385}
2386
2387
2388/**
2389 * Inserts the page into the list of modified pages.
2390 *
2391 * @param pPool The pool.
2392 * @param pPage The page.
2393 */
2394void pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2395{
2396 Log3(("pgmPoolMonitorModifiedInsert: idx=%d\n", pPage->idx));
2397 AssertMsg( pPage->iModifiedNext == NIL_PGMPOOL_IDX
2398 && pPage->iModifiedPrev == NIL_PGMPOOL_IDX
2399 && pPool->iModifiedHead != pPage->idx,
2400 ("Next=%d Prev=%d idx=%d cModifications=%d Head=%d cModifiedPages=%d\n",
2401 pPage->iModifiedNext, pPage->iModifiedPrev, pPage->idx, pPage->cModifications,
2402 pPool->iModifiedHead, pPool->cModifiedPages));
2403
2404 pPage->iModifiedNext = pPool->iModifiedHead;
2405 if (pPool->iModifiedHead != NIL_PGMPOOL_IDX)
2406 pPool->aPages[pPool->iModifiedHead].iModifiedPrev = pPage->idx;
2407 pPool->iModifiedHead = pPage->idx;
2408 pPool->cModifiedPages++;
2409#ifdef VBOX_WITH_STATISTICS
2410 if (pPool->cModifiedPages > pPool->cModifiedPagesHigh)
2411 pPool->cModifiedPagesHigh = pPool->cModifiedPages;
2412#endif
2413}
2414
2415
2416/**
2417 * Removes the page from the list of modified pages and resets the
2418 * moficiation counter.
2419 *
2420 * @param pPool The pool.
2421 * @param pPage The page which is believed to be in the list of modified pages.
2422 */
2423static void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2424{
2425 Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
2426 if (pPool->iModifiedHead == pPage->idx)
2427 {
2428 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
2429 pPool->iModifiedHead = pPage->iModifiedNext;
2430 if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
2431 {
2432 pPool->aPages[pPage->iModifiedNext].iModifiedPrev = NIL_PGMPOOL_IDX;
2433 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
2434 }
2435 pPool->cModifiedPages--;
2436 }
2437 else if (pPage->iModifiedPrev != NIL_PGMPOOL_IDX)
2438 {
2439 pPool->aPages[pPage->iModifiedPrev].iModifiedNext = pPage->iModifiedNext;
2440 if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
2441 {
2442 pPool->aPages[pPage->iModifiedNext].iModifiedPrev = pPage->iModifiedPrev;
2443 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
2444 }
2445 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
2446 pPool->cModifiedPages--;
2447 }
2448 else
2449 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
2450 pPage->cModifications = 0;
2451}
2452
2453
2454/**
2455 * Zaps the list of modified pages, resetting their modification counters in the process.
2456 *
2457 * @param pVM The VM handle.
2458 */
2459static void pgmPoolMonitorModifiedClearAll(PVM pVM)
2460{
2461 pgmLock(pVM);
2462 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2463 LogFlow(("pgmPoolMonitorModifiedClearAll: cModifiedPages=%d\n", pPool->cModifiedPages));
2464
2465 unsigned cPages = 0; NOREF(cPages);
2466
2467#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
2468 pgmPoolResetDirtyPages(pVM, true /* force removal. */);
2469#endif
2470
2471 uint16_t idx = pPool->iModifiedHead;
2472 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
2473 while (idx != NIL_PGMPOOL_IDX)
2474 {
2475 PPGMPOOLPAGE pPage = &pPool->aPages[idx];
2476 idx = pPage->iModifiedNext;
2477 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
2478 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
2479 pPage->cModifications = 0;
2480 Assert(++cPages);
2481 }
2482 AssertMsg(cPages == pPool->cModifiedPages, ("%d != %d\n", cPages, pPool->cModifiedPages));
2483 pPool->cModifiedPages = 0;
2484 pgmUnlock(pVM);
2485}
2486
2487
2488#ifdef IN_RING3
2489/**
2490 * Callback to clear all shadow pages and clear all modification counters.
2491 *
2492 * @returns VBox status code.
2493 * @param pVM The VM handle.
2494 * @param pVCpu The VMCPU for the EMT we're being called on. Unused.
2495 * @param pvUser Unused parameter.
2496 *
2497 * @remark Should only be used when monitoring is available, thus placed in
2498 * the PGMPOOL_WITH_MONITORING \#ifdef.
2499 */
2500DECLCALLBACK(int) pgmPoolClearAll(PVM pVM, PVMCPU pVCpu, void *pvUser)
2501{
2502 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2503 STAM_PROFILE_START(&pPool->StatClearAll, c);
2504 LogFlow(("pgmPoolClearAll: cUsedPages=%d\n", pPool->cUsedPages));
2505 NOREF(pvUser); NOREF(pVCpu);
2506
2507 pgmLock(pVM);
2508
2509#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
2510 pgmPoolResetDirtyPages(pVM, true /* force removal. */);
2511#endif
2512
2513 /*
2514 * Iterate all the pages until we've encountered all that in use.
2515 * This is simple but not quite optimal solution.
2516 */
2517 unsigned cModifiedPages = 0; NOREF(cModifiedPages);
2518 unsigned cLeft = pPool->cUsedPages;
2519 unsigned iPage = pPool->cCurPages;
2520 while (--iPage >= PGMPOOL_IDX_FIRST)
2521 {
2522 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
2523 if (pPage->GCPhys != NIL_RTGCPHYS)
2524 {
2525 switch (pPage->enmKind)
2526 {
2527 /*
2528 * We only care about shadow page tables.
2529 */
2530 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2531 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2532 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2533 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2534 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2535 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2536 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2537 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2538 {
2539#ifdef PGMPOOL_WITH_USER_TRACKING
2540 if (pPage->cPresent)
2541#endif
2542 {
2543 void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
2544 STAM_PROFILE_START(&pPool->StatZeroPage, z);
2545 ASMMemZeroPage(pvShw);
2546 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
2547#ifdef PGMPOOL_WITH_USER_TRACKING
2548 pPage->cPresent = 0;
2549 pPage->iFirstPresent = NIL_PGMPOOL_PRESENT_INDEX;
2550#endif
2551 }
2552#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
2553 else
2554 Assert(!pPage->fDirty);
2555#endif
2556 }
2557 /* fall thru */
2558
2559 default:
2560#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
2561 Assert(!pPage->fDirty);
2562#endif
2563 Assert(!pPage->cModifications || ++cModifiedPages);
2564 Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
2565 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
2566 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
2567 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
2568 pPage->cModifications = 0;
2569 break;
2570
2571 }
2572 if (!--cLeft)
2573 break;
2574 }
2575 }
2576
2577 /* swipe the special pages too. */
2578 for (iPage = PGMPOOL_IDX_FIRST_SPECIAL; iPage < PGMPOOL_IDX_FIRST; iPage++)
2579 {
2580 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
2581 if (pPage->GCPhys != NIL_RTGCPHYS)
2582 {
2583 Assert(!pPage->cModifications || ++cModifiedPages);
2584 Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
2585 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
2586 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
2587 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
2588 pPage->cModifications = 0;
2589 }
2590 }
2591
2592#ifndef DEBUG_michael
2593 AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
2594#endif
2595 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
2596 pPool->cModifiedPages = 0;
2597
2598#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2599 /*
2600 * Clear all the GCPhys links and rebuild the phys ext free list.
2601 */
2602 for (PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
2603 pRam;
2604 pRam = pRam->CTX_SUFF(pNext))
2605 {
2606 unsigned iPage = pRam->cb >> PAGE_SHIFT;
2607 while (iPage-- > 0)
2608 PGM_PAGE_SET_TRACKING(&pRam->aPages[iPage], 0);
2609 }
2610
2611 pPool->iPhysExtFreeHead = 0;
2612 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
2613 const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
2614 for (unsigned i = 0; i < cMaxPhysExts; i++)
2615 {
2616 paPhysExts[i].iNext = i + 1;
2617 paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
2618 paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
2619 paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
2620 }
2621 paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
2622#endif
2623
2624#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
2625 /* Clear all dirty pages. */
2626 pPool->idxFreeDirtyPage = 0;
2627 pPool->cDirtyPages = 0;
2628 for (unsigned i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
2629 pPool->aIdxDirtyPages[i] = NIL_PGMPOOL_IDX;
2630#endif
2631
2632 /* Clear the PGM_SYNC_CLEAR_PGM_POOL flag on all VCPUs to prevent redundant flushes. */
2633 for (unsigned idCpu = 0; idCpu < pVM->cCPUs; idCpu++)
2634 {
2635 PVMCPU pVCpu = &pVM->aCpus[idCpu];
2636
2637 pVCpu->pgm.s.fSyncFlags &= ~PGM_SYNC_CLEAR_PGM_POOL;
2638 }
2639
2640 pPool->cPresent = 0;
2641 pgmUnlock(pVM);
2642 PGM_INVL_ALL_VCPU_TLBS(pVM);
2643 STAM_PROFILE_STOP(&pPool->StatClearAll, c);
2644 return VINF_SUCCESS;
2645}
2646#endif /* IN_RING3 */
2647
2648
2649/**
2650 * Handle SyncCR3 pool tasks
2651 *
2652 * @returns VBox status code.
2653 * @retval VINF_SUCCESS if successfully added.
2654 * @retval VINF_PGM_SYNC_CR3 is it needs to be deferred to ring 3 (GC only)
2655 * @param pVCpu The VMCPU handle.
2656 * @remark Should only be used when monitoring is available, thus placed in
2657 * the PGMPOOL_WITH_MONITORING #ifdef.
2658 */
2659int pgmPoolSyncCR3(PVMCPU pVCpu)
2660{
2661 PVM pVM = pVCpu->CTX_SUFF(pVM);
2662 LogFlow(("pgmPoolSyncCR3\n"));
2663
2664 /*
2665 * When monitoring shadowed pages, we reset the modification counters on CR3 sync.
2666 * Occasionally we will have to clear all the shadow page tables because we wanted
2667 * to monitor a page which was mapped by too many shadowed page tables. This operation
2668 * sometimes refered to as a 'lightweight flush'.
2669 */
2670# ifdef IN_RING3 /* Don't flush in ring-0 or raw mode, it's taking too long. */
2671 if (ASMBitTestAndClear(&pVCpu->pgm.s.fSyncFlags, PGM_SYNC_CLEAR_PGM_POOL_BIT))
2672 {
2673 int rc = VMMR3EmtRendezvous(pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE, pgmPoolClearAll, NULL);
2674 AssertRC(rc);
2675 }
2676# else /* !IN_RING3 */
2677 if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
2678 {
2679 LogFlow(("SyncCR3: PGM_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
2680 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3); /** @todo no need to do global sync, right? */
2681 return VINF_PGM_SYNC_CR3;
2682 }
2683# endif /* !IN_RING3 */
2684 else
2685 pgmPoolMonitorModifiedClearAll(pVM);
2686
2687 return VINF_SUCCESS;
2688}
2689
2690#endif /* PGMPOOL_WITH_MONITORING */
2691#ifdef PGMPOOL_WITH_USER_TRACKING
2692
2693/**
2694 * Frees up at least one user entry.
2695 *
2696 * @returns VBox status code.
2697 * @retval VINF_SUCCESS if successfully added.
2698 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
2699 * @param pPool The pool.
2700 * @param iUser The user index.
2701 */
2702static int pgmPoolTrackFreeOneUser(PPGMPOOL pPool, uint16_t iUser)
2703{
2704 STAM_COUNTER_INC(&pPool->StatTrackFreeUpOneUser);
2705#ifdef PGMPOOL_WITH_CACHE
2706 /*
2707 * Just free cached pages in a braindead fashion.
2708 */
2709 /** @todo walk the age list backwards and free the first with usage. */
2710 int rc = VINF_SUCCESS;
2711 do
2712 {
2713 int rc2 = pgmPoolCacheFreeOne(pPool, iUser);
2714 if (RT_FAILURE(rc2) && rc == VINF_SUCCESS)
2715 rc = rc2;
2716 } while (pPool->iUserFreeHead == NIL_PGMPOOL_USER_INDEX);
2717 return rc;
2718#else
2719 /*
2720 * Lazy approach.
2721 */
2722 /* @todo This path no longer works (CR3 root pages will be flushed)!! */
2723 AssertCompileFailed();
2724 Assert(!CPUMIsGuestInLongMode(pVM));
2725 pgmPoolFlushAllInt(pPool);
2726 return VERR_PGM_POOL_FLUSHED;
2727#endif
2728}
2729
2730
2731/**
2732 * Inserts a page into the cache.
2733 *
2734 * This will create user node for the page, insert it into the GCPhys
2735 * hash, and insert it into the age list.
2736 *
2737 * @returns VBox status code.
2738 * @retval VINF_SUCCESS if successfully added.
2739 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
2740 * @param pPool The pool.
2741 * @param pPage The cached page.
2742 * @param GCPhys The GC physical address of the page we're gonna shadow.
2743 * @param iUser The user index.
2744 * @param iUserTable The user table index.
2745 */
2746DECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
2747{
2748 int rc = VINF_SUCCESS;
2749 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
2750
2751 LogFlow(("pgmPoolTrackInsert GCPhys=%RGp iUser %x iUserTable %x\n", GCPhys, iUser, iUserTable));
2752
2753#ifdef VBOX_STRICT
2754 /*
2755 * Check that the entry doesn't already exists.
2756 */
2757 if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
2758 {
2759 uint16_t i = pPage->iUserHead;
2760 do
2761 {
2762 Assert(i < pPool->cMaxUsers);
2763 AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
2764 i = paUsers[i].iNext;
2765 } while (i != NIL_PGMPOOL_USER_INDEX);
2766 }
2767#endif
2768
2769 /*
2770 * Find free a user node.
2771 */
2772 uint16_t i = pPool->iUserFreeHead;
2773 if (i == NIL_PGMPOOL_USER_INDEX)
2774 {
2775 int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
2776 if (RT_FAILURE(rc))
2777 return rc;
2778 i = pPool->iUserFreeHead;
2779 }
2780
2781 /*
2782 * Unlink the user node from the free list,
2783 * initialize and insert it into the user list.
2784 */
2785 pPool->iUserFreeHead = paUsers[i].iNext;
2786 paUsers[i].iNext = NIL_PGMPOOL_USER_INDEX;
2787 paUsers[i].iUser = iUser;
2788 paUsers[i].iUserTable = iUserTable;
2789 pPage->iUserHead = i;
2790
2791 /*
2792 * Insert into cache and enable monitoring of the guest page if enabled.
2793 *
2794 * Until we implement caching of all levels, including the CR3 one, we'll
2795 * have to make sure we don't try monitor & cache any recursive reuse of
2796 * a monitored CR3 page. Because all windows versions are doing this we'll
2797 * have to be able to do combined access monitoring, CR3 + PT and
2798 * PD + PT (guest PAE).
2799 *
2800 * Update:
2801 * We're now cooperating with the CR3 monitor if an uncachable page is found.
2802 */
2803#if defined(PGMPOOL_WITH_MONITORING) || defined(PGMPOOL_WITH_CACHE)
2804# ifdef PGMPOOL_WITH_MIXED_PT_CR3
2805 const bool fCanBeMonitored = true;
2806# else
2807 bool fCanBeMonitored = pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored == NIL_RTGCPHYS
2808 || (GCPhys & X86_PTE_PAE_PG_MASK) != (pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored & X86_PTE_PAE_PG_MASK)
2809 || pgmPoolIsBigPage((PGMPOOLKIND)pPage->enmKind);
2810# endif
2811# ifdef PGMPOOL_WITH_CACHE
2812 pgmPoolCacheInsert(pPool, pPage, fCanBeMonitored); /* This can be expanded. */
2813# endif
2814 if (fCanBeMonitored)
2815 {
2816# ifdef PGMPOOL_WITH_MONITORING
2817 rc = pgmPoolMonitorInsert(pPool, pPage);
2818 AssertRC(rc);
2819 }
2820# endif
2821#endif /* PGMPOOL_WITH_MONITORING */
2822 return rc;
2823}
2824
2825
2826# ifdef PGMPOOL_WITH_CACHE /* (only used when the cache is enabled.) */
2827/**
2828 * Adds a user reference to a page.
2829 *
2830 * This will move the page to the head of the
2831 *
2832 * @returns VBox status code.
2833 * @retval VINF_SUCCESS if successfully added.
2834 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
2835 * @param pPool The pool.
2836 * @param pPage The cached page.
2837 * @param iUser The user index.
2838 * @param iUserTable The user table.
2839 */
2840static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
2841{
2842 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
2843
2844 Log3(("pgmPoolTrackAddUser GCPhys = %RGp iUser %x iUserTable %x\n", pPage->GCPhys, iUser, iUserTable));
2845
2846# ifdef VBOX_STRICT
2847 /*
2848 * Check that the entry doesn't already exists. We only allow multiple users of top-level paging structures (SHW_POOL_ROOT_IDX).
2849 */
2850 if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
2851 {
2852 uint16_t i = pPage->iUserHead;
2853 do
2854 {
2855 Assert(i < pPool->cMaxUsers);
2856 AssertMsg(iUser != PGMPOOL_IDX_PD || iUser != PGMPOOL_IDX_PDPT || iUser != PGMPOOL_IDX_NESTED_ROOT || iUser != PGMPOOL_IDX_AMD64_CR3 ||
2857 paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
2858 i = paUsers[i].iNext;
2859 } while (i != NIL_PGMPOOL_USER_INDEX);
2860 }
2861# endif
2862
2863 /*
2864 * Allocate a user node.
2865 */
2866 uint16_t i = pPool->iUserFreeHead;
2867 if (i == NIL_PGMPOOL_USER_INDEX)
2868 {
2869 int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
2870 if (RT_FAILURE(rc))
2871 return rc;
2872 i = pPool->iUserFreeHead;
2873 }
2874 pPool->iUserFreeHead = paUsers[i].iNext;
2875
2876 /*
2877 * Initialize the user node and insert it.
2878 */
2879 paUsers[i].iNext = pPage->iUserHead;
2880 paUsers[i].iUser = iUser;
2881 paUsers[i].iUserTable = iUserTable;
2882 pPage->iUserHead = i;
2883
2884# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
2885 if (pPage->fDirty)
2886 pgmPoolFlushDirtyPage(pPool->CTX_SUFF(pVM), pPool, pPage->idxDirty, true /* force removal */);
2887# endif
2888
2889# ifdef PGMPOOL_WITH_CACHE
2890 /*
2891 * Tell the cache to update its replacement stats for this page.
2892 */
2893 pgmPoolCacheUsed(pPool, pPage);
2894# endif
2895 return VINF_SUCCESS;
2896}
2897# endif /* PGMPOOL_WITH_CACHE */
2898
2899
2900/**
2901 * Frees a user record associated with a page.
2902 *
2903 * This does not clear the entry in the user table, it simply replaces the
2904 * user record to the chain of free records.
2905 *
2906 * @param pPool The pool.
2907 * @param HCPhys The HC physical address of the shadow page.
2908 * @param iUser The shadow page pool index of the user table.
2909 * @param iUserTable The index into the user table (shadowed).
2910 */
2911static void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
2912{
2913 /*
2914 * Unlink and free the specified user entry.
2915 */
2916 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
2917
2918 Log3(("pgmPoolTrackFreeUser %RGp %x %x\n", pPage->GCPhys, iUser, iUserTable));
2919 /* Special: For PAE and 32-bit paging, there is usually no more than one user. */
2920 uint16_t i = pPage->iUserHead;
2921 if ( i != NIL_PGMPOOL_USER_INDEX
2922 && paUsers[i].iUser == iUser
2923 && paUsers[i].iUserTable == iUserTable)
2924 {
2925 pPage->iUserHead = paUsers[i].iNext;
2926
2927 paUsers[i].iUser = NIL_PGMPOOL_IDX;
2928 paUsers[i].iNext = pPool->iUserFreeHead;
2929 pPool->iUserFreeHead = i;
2930 return;
2931 }
2932
2933 /* General: Linear search. */
2934 uint16_t iPrev = NIL_PGMPOOL_USER_INDEX;
2935 while (i != NIL_PGMPOOL_USER_INDEX)
2936 {
2937 if ( paUsers[i].iUser == iUser
2938 && paUsers[i].iUserTable == iUserTable)
2939 {
2940 if (iPrev != NIL_PGMPOOL_USER_INDEX)
2941 paUsers[iPrev].iNext = paUsers[i].iNext;
2942 else
2943 pPage->iUserHead = paUsers[i].iNext;
2944
2945 paUsers[i].iUser = NIL_PGMPOOL_IDX;
2946 paUsers[i].iNext = pPool->iUserFreeHead;
2947 pPool->iUserFreeHead = i;
2948 return;
2949 }
2950 iPrev = i;
2951 i = paUsers[i].iNext;
2952 }
2953
2954 /* Fatal: didn't find it */
2955 AssertFatalMsgFailed(("Didn't find the user entry! iUser=%#x iUserTable=%#x GCPhys=%RGp\n",
2956 iUser, iUserTable, pPage->GCPhys));
2957}
2958
2959
2960/**
2961 * Gets the entry size of a shadow table.
2962 *
2963 * @param enmKind The kind of page.
2964 *
2965 * @returns The size of the entry in bytes. That is, 4 or 8.
2966 * @returns If the kind is not for a table, an assertion is raised and 0 is
2967 * returned.
2968 */
2969DECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind)
2970{
2971 switch (enmKind)
2972 {
2973 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2974 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2975 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2976 case PGMPOOLKIND_32BIT_PD:
2977 case PGMPOOLKIND_32BIT_PD_PHYS:
2978 return 4;
2979
2980 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2981 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2982 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2983 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2984 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2985 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
2986 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
2987 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
2988 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
2989 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2990 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2991 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2992 case PGMPOOLKIND_64BIT_PML4:
2993 case PGMPOOLKIND_PAE_PDPT:
2994 case PGMPOOLKIND_ROOT_NESTED:
2995 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2996 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2997 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2998 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2999 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
3000 case PGMPOOLKIND_PAE_PD_PHYS:
3001 case PGMPOOLKIND_PAE_PDPT_PHYS:
3002 return 8;
3003
3004 default:
3005 AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
3006 }
3007}
3008
3009
3010/**
3011 * Gets the entry size of a guest table.
3012 *
3013 * @param enmKind The kind of page.
3014 *
3015 * @returns The size of the entry in bytes. That is, 0, 4 or 8.
3016 * @returns If the kind is not for a table, an assertion is raised and 0 is
3017 * returned.
3018 */
3019DECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind)
3020{
3021 switch (enmKind)
3022 {
3023 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3024 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3025 case PGMPOOLKIND_32BIT_PD:
3026 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3027 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
3028 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
3029 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
3030 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
3031 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
3032 return 4;
3033
3034 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
3035 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
3036 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
3037 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
3038 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
3039 case PGMPOOLKIND_64BIT_PML4:
3040 case PGMPOOLKIND_PAE_PDPT:
3041 return 8;
3042
3043 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
3044 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
3045 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
3046 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
3047 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
3048 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
3049 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
3050 case PGMPOOLKIND_ROOT_NESTED:
3051 case PGMPOOLKIND_PAE_PD_PHYS:
3052 case PGMPOOLKIND_PAE_PDPT_PHYS:
3053 case PGMPOOLKIND_32BIT_PD_PHYS:
3054 /** @todo can we return 0? (nobody is calling this...) */
3055 AssertFailed();
3056 return 0;
3057
3058 default:
3059 AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
3060 }
3061}
3062
3063#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
3064
3065/**
3066 * Scans one shadow page table for mappings of a physical page.
3067 *
3068 * @param pVM The VM handle.
3069 * @param pPhysPage The guest page in question.
3070 * @param iShw The shadow page table.
3071 * @param cRefs The number of references made in that PT.
3072 */
3073static void pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
3074{
3075 LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
3076 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3077
3078 /*
3079 * Assert sanity.
3080 */
3081 Assert(cRefs == 1);
3082 AssertFatalMsg(iShw < pPool->cCurPages && iShw != NIL_PGMPOOL_IDX, ("iShw=%d\n", iShw));
3083 PPGMPOOLPAGE pPage = &pPool->aPages[iShw];
3084
3085 /*
3086 * Then, clear the actual mappings to the page in the shadow PT.
3087 */
3088 switch (pPage->enmKind)
3089 {
3090 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3091 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3092 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
3093 {
3094 const uint32_t u32 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
3095 PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
3096 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
3097 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
3098 {
3099 Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX32 cRefs=%#x\n", i, pPT->a[i], cRefs));
3100 pPT->a[i].u = 0;
3101 cRefs--;
3102 if (!cRefs)
3103 return;
3104 }
3105#ifdef LOG_ENABLED
3106 Log(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
3107 for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
3108 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
3109 {
3110 Log(("i=%d cRefs=%d\n", i, cRefs--));
3111 }
3112#endif
3113 AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
3114 break;
3115 }
3116
3117 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3118 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
3119 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
3120 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
3121 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
3122 {
3123 const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
3124 PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
3125 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
3126 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
3127 {
3128 Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
3129 pPT->a[i].u = 0;
3130 cRefs--;
3131 if (!cRefs)
3132 return;
3133 }
3134#ifdef LOG_ENABLED
3135 Log(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
3136 for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
3137 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
3138 {
3139 Log(("i=%d cRefs=%d\n", i, cRefs--));
3140 }
3141#endif
3142 AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d u64=%RX64\n", cRefs, pPage->iFirstPresent, pPage->cPresent, u64));
3143 break;
3144 }
3145
3146 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
3147 {
3148 const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
3149 PEPTPT pPT = (PEPTPT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
3150 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
3151 if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
3152 {
3153 Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
3154 pPT->a[i].u = 0;
3155 cRefs--;
3156 if (!cRefs)
3157 return;
3158 }
3159#ifdef LOG_ENABLED
3160 Log(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
3161 for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
3162 if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
3163 {
3164 Log(("i=%d cRefs=%d\n", i, cRefs--));
3165 }
3166#endif
3167 AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
3168 break;
3169 }
3170
3171 default:
3172 AssertFatalMsgFailed(("enmKind=%d iShw=%d\n", pPage->enmKind, iShw));
3173 }
3174}
3175
3176
3177/**
3178 * Scans one shadow page table for mappings of a physical page.
3179 *
3180 * @param pVM The VM handle.
3181 * @param pPhysPage The guest page in question.
3182 * @param iShw The shadow page table.
3183 * @param cRefs The number of references made in that PT.
3184 */
3185void pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
3186{
3187 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
3188 LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
3189 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPT, f);
3190 pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, iShw, cRefs);
3191 PGM_PAGE_SET_TRACKING(pPhysPage, 0);
3192 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPT, f);
3193}
3194
3195
3196/**
3197 * Flushes a list of shadow page tables mapping the same physical page.
3198 *
3199 * @param pVM The VM handle.
3200 * @param pPhysPage The guest page in question.
3201 * @param iPhysExt The physical cross reference extent list to flush.
3202 */
3203void pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt)
3204{
3205 Assert(PGMIsLockOwner(pVM));
3206 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3207 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTs, f);
3208 LogFlow(("pgmPoolTrackFlushGCPhysPTs: pPhysPage=%R[pgmpage] iPhysExt\n", pPhysPage, iPhysExt));
3209
3210 const uint16_t iPhysExtStart = iPhysExt;
3211 PPGMPOOLPHYSEXT pPhysExt;
3212 do
3213 {
3214 Assert(iPhysExt < pPool->cMaxPhysExts);
3215 pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
3216 for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
3217 if (pPhysExt->aidx[i] != NIL_PGMPOOL_IDX)
3218 {
3219 pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, pPhysExt->aidx[i], 1);
3220 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
3221 }
3222
3223 /* next */
3224 iPhysExt = pPhysExt->iNext;
3225 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
3226
3227 /* insert the list into the free list and clear the ram range entry. */
3228 pPhysExt->iNext = pPool->iPhysExtFreeHead;
3229 pPool->iPhysExtFreeHead = iPhysExtStart;
3230 PGM_PAGE_SET_TRACKING(pPhysPage, 0);
3231
3232 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTs, f);
3233}
3234
3235#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
3236
3237/**
3238 * Flushes all shadow page table mappings of the given guest page.
3239 *
3240 * This is typically called when the host page backing the guest one has been
3241 * replaced or when the page protection was changed due to an access handler.
3242 *
3243 * @returns VBox status code.
3244 * @retval VINF_SUCCESS if all references has been successfully cleared.
3245 * @retval VINF_PGM_SYNC_CR3 if we're better off with a CR3 sync and a page
3246 * pool cleaning. FF and sync flags are set.
3247 *
3248 * @param pVM The VM handle.
3249 * @param pPhysPage The guest page in question.
3250 * @param pfFlushTLBs This is set to @a true if the shadow TLBs should be
3251 * flushed, it is NOT touched if this isn't necessary.
3252 * The caller MUST initialized this to @a false.
3253 */
3254int pgmPoolTrackFlushGCPhys(PVM pVM, PPGMPAGE pPhysPage, bool *pfFlushTLBs)
3255{
3256 PVMCPU pVCpu = VMMGetCpu(pVM);
3257 pgmLock(pVM);
3258 int rc = VINF_SUCCESS;
3259#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
3260 const uint16_t u16 = PGM_PAGE_GET_TRACKING(pPhysPage);
3261 if (u16)
3262 {
3263 /*
3264 * The zero page is currently screwing up the tracking and we'll
3265 * have to flush the whole shebang. Unless VBOX_WITH_NEW_LAZY_PAGE_ALLOC
3266 * is defined, zero pages won't normally be mapped. Some kind of solution
3267 * will be needed for this problem of course, but it will have to wait...
3268 */
3269 if (PGM_PAGE_IS_ZERO(pPhysPage))
3270 rc = VINF_PGM_GCPHYS_ALIASED;
3271 else
3272 {
3273# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3274 /* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow and
3275 pgmPoolTrackFlushGCPhysPTs will/may kill the pool otherwise. */
3276 uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
3277# endif
3278
3279 if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
3280 pgmPoolTrackFlushGCPhysPT(pVM,
3281 pPhysPage,
3282 PGMPOOL_TD_GET_IDX(u16),
3283 PGMPOOL_TD_GET_CREFS(u16));
3284 else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
3285 pgmPoolTrackFlushGCPhysPTs(pVM, pPhysPage, PGMPOOL_TD_GET_IDX(u16));
3286 else
3287 rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
3288 *pfFlushTLBs = true;
3289
3290# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3291 PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
3292# endif
3293 }
3294 }
3295
3296#elif defined(PGMPOOL_WITH_CACHE)
3297 if (PGM_PAGE_IS_ZERO(pPhysPage))
3298 rc = VINF_PGM_GCPHYS_ALIASED;
3299 else
3300 {
3301# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3302 /* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow kills the pool otherwise. */
3303 uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
3304# endif
3305 rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
3306 if (rc == VINF_SUCCESS)
3307 *pfFlushTLBs = true;
3308 }
3309
3310# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3311 PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
3312# endif
3313
3314#else
3315 rc = VINF_PGM_GCPHYS_ALIASED;
3316#endif
3317
3318 if (rc == VINF_PGM_GCPHYS_ALIASED)
3319 {
3320 pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
3321 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
3322 rc = VINF_PGM_SYNC_CR3;
3323 }
3324 pgmUnlock(pVM);
3325 return rc;
3326}
3327
3328
3329/**
3330 * Scans all shadow page tables for mappings of a physical page.
3331 *
3332 * This may be slow, but it's most likely more efficient than cleaning
3333 * out the entire page pool / cache.
3334 *
3335 * @returns VBox status code.
3336 * @retval VINF_SUCCESS if all references has been successfully cleared.
3337 * @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
3338 * a page pool cleaning.
3339 *
3340 * @param pVM The VM handle.
3341 * @param pPhysPage The guest page in question.
3342 */
3343int pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage)
3344{
3345 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3346 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTsSlow, s);
3347 LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d pPhysPage=%R[pgmpage]\n",
3348 pPool->cUsedPages, pPool->cPresent, pPhysPage));
3349
3350#if 1
3351 /*
3352 * There is a limit to what makes sense.
3353 */
3354 if (pPool->cPresent > 1024)
3355 {
3356 LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: giving up... (cPresent=%d)\n", pPool->cPresent));
3357 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
3358 return VINF_PGM_GCPHYS_ALIASED;
3359 }
3360#endif
3361
3362 /*
3363 * Iterate all the pages until we've encountered all that in use.
3364 * This is simple but not quite optimal solution.
3365 */
3366 const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
3367 const uint32_t u32 = u64;
3368 unsigned cLeft = pPool->cUsedPages;
3369 unsigned iPage = pPool->cCurPages;
3370 while (--iPage >= PGMPOOL_IDX_FIRST)
3371 {
3372 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
3373 if (pPage->GCPhys != NIL_RTGCPHYS)
3374 {
3375 switch (pPage->enmKind)
3376 {
3377 /*
3378 * We only care about shadow page tables.
3379 */
3380 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3381 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3382 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
3383 {
3384 unsigned cPresent = pPage->cPresent;
3385 PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
3386 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
3387 if (pPT->a[i].n.u1Present)
3388 {
3389 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
3390 {
3391 //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX32\n", iPage, i, pPT->a[i]));
3392 pPT->a[i].u = 0;
3393 }
3394 if (!--cPresent)
3395 break;
3396 }
3397 break;
3398 }
3399
3400 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3401 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
3402 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
3403 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
3404 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
3405 {
3406 unsigned cPresent = pPage->cPresent;
3407 PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
3408 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
3409 if (pPT->a[i].n.u1Present)
3410 {
3411 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
3412 {
3413 //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
3414 pPT->a[i].u = 0;
3415 }
3416 if (!--cPresent)
3417 break;
3418 }
3419 break;
3420 }
3421 }
3422 if (!--cLeft)
3423 break;
3424 }
3425 }
3426
3427 PGM_PAGE_SET_TRACKING(pPhysPage, 0);
3428 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
3429 return VINF_SUCCESS;
3430}
3431
3432
3433/**
3434 * Clears the user entry in a user table.
3435 *
3436 * This is used to remove all references to a page when flushing it.
3437 */
3438static void pgmPoolTrackClearPageUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PCPGMPOOLUSER pUser)
3439{
3440 Assert(pUser->iUser != NIL_PGMPOOL_IDX);
3441 Assert(pUser->iUser < pPool->cCurPages);
3442 uint32_t iUserTable = pUser->iUserTable;
3443
3444 /*
3445 * Map the user page.
3446 */
3447 PPGMPOOLPAGE pUserPage = &pPool->aPages[pUser->iUser];
3448 union
3449 {
3450 uint64_t *pau64;
3451 uint32_t *pau32;
3452 } u;
3453 u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pUserPage);
3454
3455 LogFlow(("pgmPoolTrackClearPageUser: clear %x in %s (%RGp) (flushing %s)\n", iUserTable, pgmPoolPoolKindToStr(pUserPage->enmKind), pUserPage->Core.Key, pgmPoolPoolKindToStr(pPage->enmKind)));
3456
3457 /* Safety precaution in case we change the paging for other modes too in the future. */
3458 Assert(!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage));
3459
3460#ifdef VBOX_STRICT
3461 /*
3462 * Some sanity checks.
3463 */
3464 switch (pUserPage->enmKind)
3465 {
3466 case PGMPOOLKIND_32BIT_PD:
3467 case PGMPOOLKIND_32BIT_PD_PHYS:
3468 Assert(iUserTable < X86_PG_ENTRIES);
3469 break;
3470 case PGMPOOLKIND_PAE_PDPT:
3471 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
3472 case PGMPOOLKIND_PAE_PDPT_PHYS:
3473 Assert(iUserTable < 4);
3474 Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
3475 break;
3476 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
3477 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
3478 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
3479 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
3480 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
3481 case PGMPOOLKIND_PAE_PD_PHYS:
3482 Assert(iUserTable < X86_PG_PAE_ENTRIES);
3483 break;
3484 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
3485 Assert(iUserTable < X86_PG_PAE_ENTRIES);
3486 Assert(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING));
3487 break;
3488 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
3489 Assert(iUserTable < X86_PG_PAE_ENTRIES);
3490 Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
3491 break;
3492 case PGMPOOLKIND_64BIT_PML4:
3493 Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
3494 /* GCPhys >> PAGE_SHIFT is the index here */
3495 break;
3496 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
3497 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
3498 Assert(iUserTable < X86_PG_PAE_ENTRIES);
3499 break;
3500
3501 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
3502 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
3503 Assert(iUserTable < X86_PG_PAE_ENTRIES);
3504 break;
3505
3506 case PGMPOOLKIND_ROOT_NESTED:
3507 Assert(iUserTable < X86_PG_PAE_ENTRIES);
3508 break;
3509
3510 default:
3511 AssertMsgFailed(("enmKind=%d\n", pUserPage->enmKind));
3512 break;
3513 }
3514#endif /* VBOX_STRICT */
3515
3516 /*
3517 * Clear the entry in the user page.
3518 */
3519 switch (pUserPage->enmKind)
3520 {
3521 /* 32-bit entries */
3522 case PGMPOOLKIND_32BIT_PD:
3523 case PGMPOOLKIND_32BIT_PD_PHYS:
3524 u.pau32[iUserTable] = 0;
3525 break;
3526
3527 /* 64-bit entries */
3528 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
3529 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
3530 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
3531 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
3532 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
3533#if defined(IN_RC)
3534 /* In 32 bits PAE mode we *must* invalidate the TLB when changing a PDPT entry; the CPU fetches them only during cr3 load, so any
3535 * non-present PDPT will continue to cause page faults.
3536 */
3537 ASMReloadCR3();
3538#endif
3539 /* no break */
3540 case PGMPOOLKIND_PAE_PD_PHYS:
3541 case PGMPOOLKIND_PAE_PDPT_PHYS:
3542 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
3543 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
3544 case PGMPOOLKIND_64BIT_PML4:
3545 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
3546 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
3547 case PGMPOOLKIND_PAE_PDPT:
3548 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
3549 case PGMPOOLKIND_ROOT_NESTED:
3550 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
3551 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
3552 u.pau64[iUserTable] = 0;
3553 break;
3554
3555 default:
3556 AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
3557 }
3558}
3559
3560
3561/**
3562 * Clears all users of a page.
3563 */
3564static void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
3565{
3566 /*
3567 * Free all the user records.
3568 */
3569 LogFlow(("pgmPoolTrackClearPageUsers %RGp\n", pPage->GCPhys));
3570
3571 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
3572 uint16_t i = pPage->iUserHead;
3573 while (i != NIL_PGMPOOL_USER_INDEX)
3574 {
3575 /* Clear enter in user table. */
3576 pgmPoolTrackClearPageUser(pPool, pPage, &paUsers[i]);
3577
3578 /* Free it. */
3579 const uint16_t iNext = paUsers[i].iNext;
3580 paUsers[i].iUser = NIL_PGMPOOL_IDX;
3581 paUsers[i].iNext = pPool->iUserFreeHead;
3582 pPool->iUserFreeHead = i;
3583
3584 /* Next. */
3585 i = iNext;
3586 }
3587 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
3588}
3589
3590#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
3591
3592/**
3593 * Allocates a new physical cross reference extent.
3594 *
3595 * @returns Pointer to the allocated extent on success. NULL if we're out of them.
3596 * @param pVM The VM handle.
3597 * @param piPhysExt Where to store the phys ext index.
3598 */
3599PPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt)
3600{
3601 Assert(PGMIsLockOwner(pVM));
3602 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3603 uint16_t iPhysExt = pPool->iPhysExtFreeHead;
3604 if (iPhysExt == NIL_PGMPOOL_PHYSEXT_INDEX)
3605 {
3606 STAM_COUNTER_INC(&pPool->StamTrackPhysExtAllocFailures);
3607 return NULL;
3608 }
3609 PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
3610 pPool->iPhysExtFreeHead = pPhysExt->iNext;
3611 pPhysExt->iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
3612 *piPhysExt = iPhysExt;
3613 return pPhysExt;
3614}
3615
3616
3617/**
3618 * Frees a physical cross reference extent.
3619 *
3620 * @param pVM The VM handle.
3621 * @param iPhysExt The extent to free.
3622 */
3623void pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt)
3624{
3625 Assert(PGMIsLockOwner(pVM));
3626 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3627 Assert(iPhysExt < pPool->cMaxPhysExts);
3628 PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
3629 for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
3630 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
3631 pPhysExt->iNext = pPool->iPhysExtFreeHead;
3632 pPool->iPhysExtFreeHead = iPhysExt;
3633}
3634
3635
3636/**
3637 * Frees a physical cross reference extent.
3638 *
3639 * @param pVM The VM handle.
3640 * @param iPhysExt The extent to free.
3641 */
3642void pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt)
3643{
3644 Assert(PGMIsLockOwner(pVM));
3645 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3646
3647 const uint16_t iPhysExtStart = iPhysExt;
3648 PPGMPOOLPHYSEXT pPhysExt;
3649 do
3650 {
3651 Assert(iPhysExt < pPool->cMaxPhysExts);
3652 pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
3653 for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
3654 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
3655
3656 /* next */
3657 iPhysExt = pPhysExt->iNext;
3658 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
3659
3660 pPhysExt->iNext = pPool->iPhysExtFreeHead;
3661 pPool->iPhysExtFreeHead = iPhysExtStart;
3662}
3663
3664
3665/**
3666 * Insert a reference into a list of physical cross reference extents.
3667 *
3668 * @returns The new tracking data for PGMPAGE.
3669 *
3670 * @param pVM The VM handle.
3671 * @param iPhysExt The physical extent index of the list head.
3672 * @param iShwPT The shadow page table index.
3673 *
3674 */
3675static uint16_t pgmPoolTrackPhysExtInsert(PVM pVM, uint16_t iPhysExt, uint16_t iShwPT)
3676{
3677 Assert(PGMIsLockOwner(pVM));
3678 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3679 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
3680
3681 /* special common case. */
3682 if (paPhysExts[iPhysExt].aidx[2] == NIL_PGMPOOL_IDX)
3683 {
3684 paPhysExts[iPhysExt].aidx[2] = iShwPT;
3685 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
3686 LogFlow(("pgmPoolTrackPhysExtInsert: %d:{,,%d}\n", iPhysExt, iShwPT));
3687 return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
3688 }
3689
3690 /* general treatment. */
3691 const uint16_t iPhysExtStart = iPhysExt;
3692 unsigned cMax = 15;
3693 for (;;)
3694 {
3695 Assert(iPhysExt < pPool->cMaxPhysExts);
3696 for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
3697 if (paPhysExts[iPhysExt].aidx[i] == NIL_PGMPOOL_IDX)
3698 {
3699 paPhysExts[iPhysExt].aidx[i] = iShwPT;
3700 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
3701 LogFlow(("pgmPoolTrackPhysExtInsert: %d:{%d} i=%d cMax=%d\n", iPhysExt, iShwPT, i, cMax));
3702 return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtStart);
3703 }
3704 if (!--cMax)
3705 {
3706 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
3707 pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
3708 LogFlow(("pgmPoolTrackPhysExtInsert: overflow (1) iShwPT=%d\n", iShwPT));
3709 return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
3710 }
3711 }
3712
3713 /* add another extent to the list. */
3714 PPGMPOOLPHYSEXT pNew = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
3715 if (!pNew)
3716 {
3717 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
3718 pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
3719 LogFlow(("pgmPoolTrackPhysExtInsert: pgmPoolTrackPhysExtAlloc failed iShwPT=%d\n", iShwPT));
3720 return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
3721 }
3722 pNew->iNext = iPhysExtStart;
3723 pNew->aidx[0] = iShwPT;
3724 LogFlow(("pgmPoolTrackPhysExtInsert: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
3725 return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
3726}
3727
3728
3729/**
3730 * Add a reference to guest physical page where extents are in use.
3731 *
3732 * @returns The new tracking data for PGMPAGE.
3733 *
3734 * @param pVM The VM handle.
3735 * @param u16 The ram range flags (top 16-bits).
3736 * @param iShwPT The shadow page table index.
3737 */
3738uint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT)
3739{
3740 pgmLock(pVM);
3741 if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
3742 {
3743 /*
3744 * Convert to extent list.
3745 */
3746 Assert(PGMPOOL_TD_GET_CREFS(u16) == 1);
3747 uint16_t iPhysExt;
3748 PPGMPOOLPHYSEXT pPhysExt = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
3749 if (pPhysExt)
3750 {
3751 LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, PGMPOOL_TD_GET_IDX(u16), iShwPT));
3752 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliased);
3753 pPhysExt->aidx[0] = PGMPOOL_TD_GET_IDX(u16);
3754 pPhysExt->aidx[1] = iShwPT;
3755 u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
3756 }
3757 else
3758 u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
3759 }
3760 else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
3761 {
3762 /*
3763 * Insert into the extent list.
3764 */
3765 u16 = pgmPoolTrackPhysExtInsert(pVM, PGMPOOL_TD_GET_IDX(u16), iShwPT);
3766 }
3767 else
3768 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedLots);
3769 pgmUnlock(pVM);
3770 return u16;
3771}
3772
3773
3774/**
3775 * Clear references to guest physical memory.
3776 *
3777 * @param pPool The pool.
3778 * @param pPage The page.
3779 * @param pPhysPage Pointer to the aPages entry in the ram range.
3780 */
3781void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMPAGE pPhysPage)
3782{
3783 const unsigned cRefs = PGM_PAGE_GET_TD_CREFS(pPhysPage);
3784 AssertFatalMsg(cRefs == PGMPOOL_TD_CREFS_PHYSEXT, ("cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
3785
3786 uint16_t iPhysExt = PGM_PAGE_GET_TD_IDX(pPhysPage);
3787 if (iPhysExt != PGMPOOL_TD_IDX_OVERFLOWED)
3788 {
3789 PVM pVM = pPool->CTX_SUFF(pVM);
3790 pgmLock(pVM);
3791
3792 uint16_t iPhysExtPrev = NIL_PGMPOOL_PHYSEXT_INDEX;
3793 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
3794 do
3795 {
3796 Assert(iPhysExt < pPool->cMaxPhysExts);
3797
3798 /*
3799 * Look for the shadow page and check if it's all freed.
3800 */
3801 for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
3802 {
3803 if (paPhysExts[iPhysExt].aidx[i] == pPage->idx)
3804 {
3805 paPhysExts[iPhysExt].aidx[i] = NIL_PGMPOOL_IDX;
3806
3807 for (i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
3808 if (paPhysExts[iPhysExt].aidx[i] != NIL_PGMPOOL_IDX)
3809 {
3810 Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
3811 pgmUnlock(pVM);
3812 return;
3813 }
3814
3815 /* we can free the node. */
3816 const uint16_t iPhysExtNext = paPhysExts[iPhysExt].iNext;
3817 if ( iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX
3818 && iPhysExtNext == NIL_PGMPOOL_PHYSEXT_INDEX)
3819 {
3820 /* lonely node */
3821 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
3822 Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d lonely\n", pPhysPage, pPage->idx));
3823 PGM_PAGE_SET_TRACKING(pPhysPage, 0);
3824 }
3825 else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
3826 {
3827 /* head */
3828 Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d head\n", pPhysPage, pPage->idx));
3829 PGM_PAGE_SET_TRACKING(pPhysPage, PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtNext));
3830 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
3831 }
3832 else
3833 {
3834 /* in list */
3835 Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
3836 paPhysExts[iPhysExtPrev].iNext = iPhysExtNext;
3837 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
3838 }
3839 iPhysExt = iPhysExtNext;
3840 pgmUnlock(pVM);
3841 return;
3842 }
3843 }
3844
3845 /* next */
3846 iPhysExtPrev = iPhysExt;
3847 iPhysExt = paPhysExts[iPhysExt].iNext;
3848 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
3849
3850 pgmUnlock(pVM);
3851 AssertFatalMsgFailed(("not-found! cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
3852 }
3853 else /* nothing to do */
3854 Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage]\n", pPhysPage));
3855}
3856
3857
3858/**
3859 * Clear references to guest physical memory.
3860 *
3861 * This is the same as pgmPoolTracDerefGCPhys except that the guest physical address
3862 * is assumed to be correct, so the linear search can be skipped and we can assert
3863 * at an earlier point.
3864 *
3865 * @param pPool The pool.
3866 * @param pPage The page.
3867 * @param HCPhys The host physical address corresponding to the guest page.
3868 * @param GCPhys The guest physical address corresponding to HCPhys.
3869 */
3870static void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
3871{
3872 /*
3873 * Walk range list.
3874 */
3875 PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
3876 while (pRam)
3877 {
3878 RTGCPHYS off = GCPhys - pRam->GCPhys;
3879 if (off < pRam->cb)
3880 {
3881 /* does it match? */
3882 const unsigned iPage = off >> PAGE_SHIFT;
3883 Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
3884#ifdef LOG_ENABLED
3885RTHCPHYS HCPhysPage = PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]);
3886Log2(("pgmPoolTracDerefGCPhys %RHp vs %RHp\n", HCPhysPage, HCPhys));
3887#endif
3888 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
3889 {
3890 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
3891 return;
3892 }
3893 break;
3894 }
3895 pRam = pRam->CTX_SUFF(pNext);
3896 }
3897 AssertFatalMsgFailed(("HCPhys=%RHp GCPhys=%RGp\n", HCPhys, GCPhys));
3898}
3899
3900
3901/**
3902 * Clear references to guest physical memory.
3903 *
3904 * @param pPool The pool.
3905 * @param pPage The page.
3906 * @param HCPhys The host physical address corresponding to the guest page.
3907 * @param GCPhysHint The guest physical address which may corresponding to HCPhys.
3908 */
3909void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
3910{
3911 Log4(("pgmPoolTracDerefGCPhysHint %RHp %RGp\n", HCPhys, GCPhysHint));
3912
3913 /*
3914 * Walk range list.
3915 */
3916 PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
3917 while (pRam)
3918 {
3919 RTGCPHYS off = GCPhysHint - pRam->GCPhys;
3920 if (off < pRam->cb)
3921 {
3922 /* does it match? */
3923 const unsigned iPage = off >> PAGE_SHIFT;
3924 Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
3925 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
3926 {
3927 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
3928 return;
3929 }
3930 break;
3931 }
3932 pRam = pRam->CTX_SUFF(pNext);
3933 }
3934
3935 /*
3936 * Damn, the hint didn't work. We'll have to do an expensive linear search.
3937 */
3938 STAM_COUNTER_INC(&pPool->StatTrackLinearRamSearches);
3939 pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
3940 while (pRam)
3941 {
3942 unsigned iPage = pRam->cb >> PAGE_SHIFT;
3943 while (iPage-- > 0)
3944 {
3945 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
3946 {
3947 Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%RHp GCPhysHint=%RGp GCPhysReal=%RGp\n",
3948 HCPhys, GCPhysHint, pRam->GCPhys + (iPage << PAGE_SHIFT)));
3949 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
3950 return;
3951 }
3952 }
3953 pRam = pRam->CTX_SUFF(pNext);
3954 }
3955
3956 AssertFatalMsgFailed(("HCPhys=%RHp GCPhysHint=%RGp\n", HCPhys, GCPhysHint));
3957}
3958
3959
3960/**
3961 * Clear references to guest physical memory in a 32-bit / 32-bit page table.
3962 *
3963 * @param pPool The pool.
3964 * @param pPage The page.
3965 * @param pShwPT The shadow page table (mapping of the page).
3966 * @param pGstPT The guest page table.
3967 */
3968DECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
3969{
3970 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
3971 if (pShwPT->a[i].n.u1Present)
3972 {
3973 Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
3974 i, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
3975 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
3976 if (!--pPage->cPresent)
3977 break;
3978 }
3979}
3980
3981
3982/**
3983 * Clear references to guest physical memory in a PAE / 32-bit page table.
3984 *
3985 * @param pPool The pool.
3986 * @param pPage The page.
3987 * @param pShwPT The shadow page table (mapping of the page).
3988 * @param pGstPT The guest page table (just a half one).
3989 */
3990DECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
3991{
3992 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
3993 if (pShwPT->a[i].n.u1Present)
3994 {
3995 Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX64 hint=%RX32\n",
3996 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
3997 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
3998 }
3999}
4000
4001
4002/**
4003 * Clear references to guest physical memory in a PAE / PAE page table.
4004 *
4005 * @param pPool The pool.
4006 * @param pPage The page.
4007 * @param pShwPT The shadow page table (mapping of the page).
4008 * @param pGstPT The guest page table.
4009 */
4010DECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
4011{
4012 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
4013 if (pShwPT->a[i].n.u1Present)
4014 {
4015 Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX32 hint=%RX32\n",
4016 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
4017 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
4018 }
4019}
4020
4021
4022/**
4023 * Clear references to guest physical memory in a 32-bit / 4MB page table.
4024 *
4025 * @param pPool The pool.
4026 * @param pPage The page.
4027 * @param pShwPT The shadow page table (mapping of the page).
4028 */
4029DECLINLINE(void) pgmPoolTrackDerefPT32Bit4MB(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT)
4030{
4031 RTGCPHYS GCPhys = pPage->GCPhys;
4032 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
4033 if (pShwPT->a[i].n.u1Present)
4034 {
4035 Log4(("pgmPoolTrackDerefPT32Bit4MB: i=%d pte=%RX32 GCPhys=%RGp\n",
4036 i, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys));
4037 pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys);
4038 }
4039}
4040
4041
4042/**
4043 * Clear references to guest physical memory in a PAE / 2/4MB page table.
4044 *
4045 * @param pPool The pool.
4046 * @param pPage The page.
4047 * @param pShwPT The shadow page table (mapping of the page).
4048 */
4049DECLINLINE(void) pgmPoolTrackDerefPTPaeBig(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT)
4050{
4051 RTGCPHYS GCPhys = pPage->GCPhys;
4052 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
4053 if (pShwPT->a[i].n.u1Present)
4054 {
4055 Log4(("pgmPoolTrackDerefPTPaeBig: i=%d pte=%RX64 hint=%RGp\n",
4056 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys));
4057 pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys);
4058 }
4059}
4060
4061#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
4062
4063
4064/**
4065 * Clear references to shadowed pages in a 32 bits page directory.
4066 *
4067 * @param pPool The pool.
4068 * @param pPage The page.
4069 * @param pShwPD The shadow page directory (mapping of the page).
4070 */
4071DECLINLINE(void) pgmPoolTrackDerefPD(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PD pShwPD)
4072{
4073 for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
4074 {
4075 if ( pShwPD->a[i].n.u1Present
4076 && !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
4077 )
4078 {
4079 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PG_MASK);
4080 if (pSubPage)
4081 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
4082 else
4083 AssertFatalMsgFailed(("%x\n", pShwPD->a[i].u & X86_PDE_PG_MASK));
4084 }
4085 }
4086}
4087
4088/**
4089 * Clear references to shadowed pages in a PAE (legacy or 64 bits) page directory.
4090 *
4091 * @param pPool The pool.
4092 * @param pPage The page.
4093 * @param pShwPD The shadow page directory (mapping of the page).
4094 */
4095DECLINLINE(void) pgmPoolTrackDerefPDPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPAE pShwPD)
4096{
4097 for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
4098 {
4099 if ( pShwPD->a[i].n.u1Present
4100 && !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
4101 )
4102 {
4103 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
4104 if (pSubPage)
4105 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
4106 else
4107 AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & X86_PDE_PAE_PG_MASK));
4108 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
4109 }
4110 }
4111}
4112
4113/**
4114 * Clear references to shadowed pages in a PAE page directory pointer table.
4115 *
4116 * @param pPool The pool.
4117 * @param pPage The page.
4118 * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
4119 */
4120DECLINLINE(void) pgmPoolTrackDerefPDPTPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
4121{
4122 for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
4123 {
4124 if ( pShwPDPT->a[i].n.u1Present
4125 && !(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING)
4126 )
4127 {
4128 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
4129 if (pSubPage)
4130 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
4131 else
4132 AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
4133 }
4134 }
4135}
4136
4137
4138/**
4139 * Clear references to shadowed pages in a 64-bit page directory pointer table.
4140 *
4141 * @param pPool The pool.
4142 * @param pPage The page.
4143 * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
4144 */
4145DECLINLINE(void) pgmPoolTrackDerefPDPT64Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
4146{
4147 for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
4148 {
4149 Assert(!(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING));
4150 if (pShwPDPT->a[i].n.u1Present)
4151 {
4152 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
4153 if (pSubPage)
4154 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
4155 else
4156 AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
4157 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
4158 }
4159 }
4160}
4161
4162
4163/**
4164 * Clear references to shadowed pages in a 64-bit level 4 page table.
4165 *
4166 * @param pPool The pool.
4167 * @param pPage The page.
4168 * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
4169 */
4170DECLINLINE(void) pgmPoolTrackDerefPML464Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PML4 pShwPML4)
4171{
4172 for (unsigned i = 0; i < RT_ELEMENTS(pShwPML4->a); i++)
4173 {
4174 if (pShwPML4->a[i].n.u1Present)
4175 {
4176 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
4177 if (pSubPage)
4178 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
4179 else
4180 AssertFatalMsgFailed(("%RX64\n", pShwPML4->a[i].u & X86_PML4E_PG_MASK));
4181 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
4182 }
4183 }
4184}
4185
4186
4187/**
4188 * Clear references to shadowed pages in an EPT page table.
4189 *
4190 * @param pPool The pool.
4191 * @param pPage The page.
4192 * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
4193 */
4194DECLINLINE(void) pgmPoolTrackDerefPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPT pShwPT)
4195{
4196 RTGCPHYS GCPhys = pPage->GCPhys;
4197 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
4198 if (pShwPT->a[i].n.u1Present)
4199 {
4200 Log4(("pgmPoolTrackDerefPTEPT: i=%d pte=%RX64 GCPhys=%RX64\n",
4201 i, pShwPT->a[i].u & EPT_PTE_PG_MASK, pPage->GCPhys));
4202 pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & EPT_PTE_PG_MASK, GCPhys);
4203 }
4204}
4205
4206
4207/**
4208 * Clear references to shadowed pages in an EPT page directory.
4209 *
4210 * @param pPool The pool.
4211 * @param pPage The page.
4212 * @param pShwPD The shadow page directory (mapping of the page).
4213 */
4214DECLINLINE(void) pgmPoolTrackDerefPDEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPD pShwPD)
4215{
4216 for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
4217 {
4218 if (pShwPD->a[i].n.u1Present)
4219 {
4220 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & EPT_PDE_PG_MASK);
4221 if (pSubPage)
4222 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
4223 else
4224 AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & EPT_PDE_PG_MASK));
4225 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
4226 }
4227 }
4228}
4229
4230
4231/**
4232 * Clear references to shadowed pages in an EPT page directory pointer table.
4233 *
4234 * @param pPool The pool.
4235 * @param pPage The page.
4236 * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
4237 */
4238DECLINLINE(void) pgmPoolTrackDerefPDPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPDPT pShwPDPT)
4239{
4240 for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
4241 {
4242 if (pShwPDPT->a[i].n.u1Present)
4243 {
4244 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK);
4245 if (pSubPage)
4246 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
4247 else
4248 AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK));
4249 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
4250 }
4251 }
4252}
4253
4254
4255/**
4256 * Clears all references made by this page.
4257 *
4258 * This includes other shadow pages and GC physical addresses.
4259 *
4260 * @param pPool The pool.
4261 * @param pPage The page.
4262 */
4263static void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4264{
4265 /*
4266 * Map the shadow page and take action according to the page kind.
4267 */
4268 void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
4269 switch (pPage->enmKind)
4270 {
4271#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
4272 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
4273 {
4274 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
4275 void *pvGst;
4276 int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
4277 pgmPoolTrackDerefPT32Bit32Bit(pPool, pPage, (PX86PT)pvShw, (PCX86PT)pvGst);
4278 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
4279 break;
4280 }
4281
4282 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
4283 {
4284 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
4285 void *pvGst;
4286 int rc = PGM_GCPHYS_2_PTR_EX(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
4287 pgmPoolTrackDerefPTPae32Bit(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PT)pvGst);
4288 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
4289 break;
4290 }
4291
4292 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
4293 {
4294 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
4295 void *pvGst;
4296 int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
4297 pgmPoolTrackDerefPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
4298 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
4299 break;
4300 }
4301
4302 case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
4303 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
4304 {
4305 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
4306 pgmPoolTrackDerefPT32Bit4MB(pPool, pPage, (PX86PT)pvShw);
4307 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
4308 break;
4309 }
4310
4311 case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 2 MB page */
4312 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
4313 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
4314 {
4315 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
4316 pgmPoolTrackDerefPTPaeBig(pPool, pPage, (PX86PTPAE)pvShw);
4317 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
4318 break;
4319 }
4320
4321#else /* !PGMPOOL_WITH_GCPHYS_TRACKING */
4322 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
4323 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
4324 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
4325 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
4326 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
4327 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
4328 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
4329 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
4330 break;
4331#endif /* !PGMPOOL_WITH_GCPHYS_TRACKING */
4332
4333 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
4334 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
4335 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
4336 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
4337 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
4338 case PGMPOOLKIND_PAE_PD_PHYS:
4339 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
4340 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
4341 pgmPoolTrackDerefPDPae(pPool, pPage, (PX86PDPAE)pvShw);
4342 break;
4343
4344 case PGMPOOLKIND_32BIT_PD_PHYS:
4345 case PGMPOOLKIND_32BIT_PD:
4346 pgmPoolTrackDerefPD(pPool, pPage, (PX86PD)pvShw);
4347 break;
4348
4349 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
4350 case PGMPOOLKIND_PAE_PDPT:
4351 case PGMPOOLKIND_PAE_PDPT_PHYS:
4352 pgmPoolTrackDerefPDPTPae(pPool, pPage, (PX86PDPT)pvShw);
4353 break;
4354
4355 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
4356 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
4357 pgmPoolTrackDerefPDPT64Bit(pPool, pPage, (PX86PDPT)pvShw);
4358 break;
4359
4360 case PGMPOOLKIND_64BIT_PML4:
4361 pgmPoolTrackDerefPML464Bit(pPool, pPage, (PX86PML4)pvShw);
4362 break;
4363
4364 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
4365 pgmPoolTrackDerefPTEPT(pPool, pPage, (PEPTPT)pvShw);
4366 break;
4367
4368 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
4369 pgmPoolTrackDerefPDEPT(pPool, pPage, (PEPTPD)pvShw);
4370 break;
4371
4372 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
4373 pgmPoolTrackDerefPDPTEPT(pPool, pPage, (PEPTPDPT)pvShw);
4374 break;
4375
4376 default:
4377 AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
4378 }
4379
4380 /* paranoia, clear the shadow page. Remove this laser (i.e. let Alloc and ClearAll do it). */
4381 STAM_PROFILE_START(&pPool->StatZeroPage, z);
4382 ASMMemZeroPage(pvShw);
4383 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
4384 pPage->fZeroed = true;
4385 PGMPOOL_UNLOCK_PTR(pPool->CTX_SUFF(pVM), pvShw);
4386}
4387#endif /* PGMPOOL_WITH_USER_TRACKING */
4388
4389/**
4390 * Flushes a pool page.
4391 *
4392 * This moves the page to the free list after removing all user references to it.
4393 *
4394 * @returns VBox status code.
4395 * @retval VINF_SUCCESS on success.
4396 * @param pPool The pool.
4397 * @param HCPhys The HC physical address of the shadow page.
4398 */
4399int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4400{
4401 PVM pVM = pPool->CTX_SUFF(pVM);
4402
4403 int rc = VINF_SUCCESS;
4404 STAM_PROFILE_START(&pPool->StatFlushPage, f);
4405 LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%RHp, .idx=%d, .enmKind=%s, .GCPhys=%RGp}\n",
4406 pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
4407
4408 /*
4409 * Quietly reject any attempts at flushing any of the special root pages.
4410 */
4411 if (pPage->idx < PGMPOOL_IDX_FIRST)
4412 {
4413 AssertFailed(); /* can no longer happen */
4414 Log(("pgmPoolFlushPage: special root page, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
4415 return VINF_SUCCESS;
4416 }
4417
4418 pgmLock(pVM);
4419
4420 /*
4421 * Quietly reject any attempts at flushing the currently active shadow CR3 mapping
4422 */
4423 if (pgmPoolIsPageLocked(&pVM->pgm.s, pPage))
4424 {
4425 AssertMsg( pPage->enmKind == PGMPOOLKIND_64BIT_PML4
4426 || pPage->enmKind == PGMPOOLKIND_PAE_PDPT
4427 || pPage->enmKind == PGMPOOLKIND_PAE_PDPT_FOR_32BIT
4428 || pPage->enmKind == PGMPOOLKIND_32BIT_PD
4429 || pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD
4430 || pPage->enmKind == PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD
4431 || pPage->enmKind == PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD
4432 || pPage->enmKind == PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD
4433 || pPage->enmKind == PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD,
4434 ("Can't free the shadow CR3! (%RHp vs %RHp kind=%d\n", PGMGetHyperCR3(VMMGetCpu(pVM)), pPage->Core.Key, pPage->enmKind));
4435 Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
4436 pgmUnlock(pVM);
4437 return VINF_SUCCESS;
4438 }
4439
4440#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
4441 /* Start a subset so we won't run out of mapping space. */
4442 PVMCPU pVCpu = VMMGetCpu(pVM);
4443 uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
4444#endif
4445
4446 /*
4447 * Mark the page as being in need of an ASMMemZeroPage().
4448 */
4449 pPage->fZeroed = false;
4450
4451#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
4452 if (pPage->fDirty)
4453 pgmPoolFlushDirtyPage(pVM, pPool, pPage->idxDirty, true /* force removal */);
4454#endif
4455
4456#ifdef PGMPOOL_WITH_USER_TRACKING
4457 /*
4458 * Clear the page.
4459 */
4460 pgmPoolTrackClearPageUsers(pPool, pPage);
4461 STAM_PROFILE_START(&pPool->StatTrackDeref,a);
4462 pgmPoolTrackDeref(pPool, pPage);
4463 STAM_PROFILE_STOP(&pPool->StatTrackDeref,a);
4464#endif
4465
4466#ifdef PGMPOOL_WITH_CACHE
4467 /*
4468 * Flush it from the cache.
4469 */
4470 pgmPoolCacheFlushPage(pPool, pPage);
4471#endif /* PGMPOOL_WITH_CACHE */
4472
4473#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
4474 /* Heavy stuff done. */
4475 PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
4476#endif
4477
4478#ifdef PGMPOOL_WITH_MONITORING
4479 /*
4480 * Deregistering the monitoring.
4481 */
4482 if (pPage->fMonitored)
4483 rc = pgmPoolMonitorFlush(pPool, pPage);
4484#endif
4485
4486 /*
4487 * Free the page.
4488 */
4489 Assert(pPage->iNext == NIL_PGMPOOL_IDX);
4490 pPage->iNext = pPool->iFreeHead;
4491 pPool->iFreeHead = pPage->idx;
4492 pPage->enmKind = PGMPOOLKIND_FREE;
4493 pPage->enmAccess = PGMPOOLACCESS_DONTCARE;
4494 pPage->GCPhys = NIL_RTGCPHYS;
4495 pPage->fReusedFlushPending = false;
4496
4497 pPool->cUsedPages--;
4498 pgmUnlock(pVM);
4499 STAM_PROFILE_STOP(&pPool->StatFlushPage, f);
4500 return rc;
4501}
4502
4503
4504/**
4505 * Frees a usage of a pool page.
4506 *
4507 * The caller is responsible to updating the user table so that it no longer
4508 * references the shadow page.
4509 *
4510 * @param pPool The pool.
4511 * @param HCPhys The HC physical address of the shadow page.
4512 * @param iUser The shadow page pool index of the user table.
4513 * @param iUserTable The index into the user table (shadowed).
4514 */
4515void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
4516{
4517 PVM pVM = pPool->CTX_SUFF(pVM);
4518
4519 STAM_PROFILE_START(&pPool->StatFree, a);
4520 LogFlow(("pgmPoolFreeByPage: pPage=%p:{.Key=%RHp, .idx=%d, enmKind=%s} iUser=%#x iUserTable=%#x\n",
4521 pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), iUser, iUserTable));
4522 Assert(pPage->idx >= PGMPOOL_IDX_FIRST);
4523 pgmLock(pVM);
4524#ifdef PGMPOOL_WITH_USER_TRACKING
4525 pgmPoolTrackFreeUser(pPool, pPage, iUser, iUserTable);
4526#endif
4527#ifdef PGMPOOL_WITH_CACHE
4528 if (!pPage->fCached)
4529#endif
4530 pgmPoolFlushPage(pPool, pPage);
4531 pgmUnlock(pVM);
4532 STAM_PROFILE_STOP(&pPool->StatFree, a);
4533}
4534
4535
4536/**
4537 * Makes one or more free page free.
4538 *
4539 * @returns VBox status code.
4540 * @retval VINF_SUCCESS on success.
4541 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
4542 *
4543 * @param pPool The pool.
4544 * @param enmKind Page table kind
4545 * @param iUser The user of the page.
4546 */
4547static int pgmPoolMakeMoreFreePages(PPGMPOOL pPool, PGMPOOLKIND enmKind, uint16_t iUser)
4548{
4549 PVM pVM = pPool->CTX_SUFF(pVM);
4550
4551 LogFlow(("pgmPoolMakeMoreFreePages: iUser=%#x\n", iUser));
4552
4553 /*
4554 * If the pool isn't full grown yet, expand it.
4555 */
4556 if ( pPool->cCurPages < pPool->cMaxPages
4557#if defined(IN_RC)
4558 /* Hack alert: we can't deal with jumps to ring 3 when called from MapCR3 and allocating pages for PAE PDs. */
4559 && enmKind != PGMPOOLKIND_PAE_PD_FOR_PAE_PD
4560 && (enmKind < PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD || enmKind > PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD)
4561#endif
4562 )
4563 {
4564 STAM_PROFILE_ADV_SUSPEND(&pPool->StatAlloc, a);
4565#ifdef IN_RING3
4566 int rc = PGMR3PoolGrow(pVM);
4567#else
4568 int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_POOL_GROW, 0);
4569#endif
4570 if (RT_FAILURE(rc))
4571 return rc;
4572 STAM_PROFILE_ADV_RESUME(&pPool->StatAlloc, a);
4573 if (pPool->iFreeHead != NIL_PGMPOOL_IDX)
4574 return VINF_SUCCESS;
4575 }
4576
4577#ifdef PGMPOOL_WITH_CACHE
4578 /*
4579 * Free one cached page.
4580 */
4581 return pgmPoolCacheFreeOne(pPool, iUser);
4582#else
4583 /*
4584 * Flush the pool.
4585 *
4586 * If we have tracking enabled, it should be possible to come up with
4587 * a cheap replacement strategy...
4588 */
4589 /* @todo This path no longer works (CR3 root pages will be flushed)!! */
4590 AssertCompileFailed();
4591 Assert(!CPUMIsGuestInLongMode(pVM));
4592 pgmPoolFlushAllInt(pPool);
4593 return VERR_PGM_POOL_FLUSHED;
4594#endif
4595}
4596
4597/**
4598 * Allocates a page from the pool.
4599 *
4600 * This page may actually be a cached page and not in need of any processing
4601 * on the callers part.
4602 *
4603 * @returns VBox status code.
4604 * @retval VINF_SUCCESS if a NEW page was allocated.
4605 * @retval VINF_PGM_CACHED_PAGE if a CACHED page was returned.
4606 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
4607 * @param pVM The VM handle.
4608 * @param GCPhys The GC physical address of the page we're gonna shadow.
4609 * For 4MB and 2MB PD entries, it's the first address the
4610 * shadow PT is covering.
4611 * @param enmKind The kind of mapping.
4612 * @param enmAccess Access type for the mapping (only relevant for big pages)
4613 * @param iUser The shadow page pool index of the user table.
4614 * @param iUserTable The index into the user table (shadowed).
4615 * @param ppPage Where to store the pointer to the page. NULL is stored here on failure.
4616 * @param fLockPage Lock the page
4617 */
4618int pgmPoolAllocEx(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, PGMPOOLACCESS enmAccess, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage, bool fLockPage)
4619{
4620 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
4621 STAM_PROFILE_ADV_START(&pPool->StatAlloc, a);
4622 LogFlow(("pgmPoolAlloc: GCPhys=%RGp enmKind=%s iUser=%#x iUserTable=%#x\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable));
4623 *ppPage = NULL;
4624 /** @todo CSAM/PGMPrefetchPage messes up here during CSAMR3CheckGates
4625 * (TRPMR3SyncIDT) because of FF priority. Try fix that?
4626 * Assert(!(pVM->pgm.s.fGlobalSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)); */
4627
4628 pgmLock(pVM);
4629
4630#ifdef PGMPOOL_WITH_CACHE
4631 if (pPool->fCacheEnabled)
4632 {
4633 int rc2 = pgmPoolCacheAlloc(pPool, GCPhys, enmKind, enmAccess, iUser, iUserTable, ppPage);
4634 if (RT_SUCCESS(rc2))
4635 {
4636 if (fLockPage)
4637 pgmPoolLockPage(pPool, *ppPage);
4638 pgmUnlock(pVM);
4639 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4640 LogFlow(("pgmPoolAlloc: cached returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
4641 return rc2;
4642 }
4643 }
4644#endif
4645
4646 /*
4647 * Allocate a new one.
4648 */
4649 int rc = VINF_SUCCESS;
4650 uint16_t iNew = pPool->iFreeHead;
4651 if (iNew == NIL_PGMPOOL_IDX)
4652 {
4653 rc = pgmPoolMakeMoreFreePages(pPool, enmKind, iUser);
4654 if (RT_FAILURE(rc))
4655 {
4656 pgmUnlock(pVM);
4657 Log(("pgmPoolAlloc: returns %Rrc (Free)\n", rc));
4658 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4659 return rc;
4660 }
4661 iNew = pPool->iFreeHead;
4662 AssertReleaseReturn(iNew != NIL_PGMPOOL_IDX, VERR_INTERNAL_ERROR);
4663 }
4664
4665 /* unlink the free head */
4666 PPGMPOOLPAGE pPage = &pPool->aPages[iNew];
4667 pPool->iFreeHead = pPage->iNext;
4668 pPage->iNext = NIL_PGMPOOL_IDX;
4669
4670 /*
4671 * Initialize it.
4672 */
4673 pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
4674 pPage->enmKind = enmKind;
4675 pPage->enmAccess = enmAccess;
4676 pPage->GCPhys = GCPhys;
4677 pPage->fSeenNonGlobal = false; /* Set this to 'true' to disable this feature. */
4678 pPage->fMonitored = false;
4679 pPage->fCached = false;
4680#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
4681 pPage->fDirty = false;
4682#endif
4683 pPage->fReusedFlushPending = false;
4684#ifdef PGMPOOL_WITH_MONITORING
4685 pPage->cModifications = 0;
4686 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
4687 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
4688#else
4689 pPage->fCR3Mix = false;
4690#endif
4691#ifdef PGMPOOL_WITH_USER_TRACKING
4692 pPage->cPresent = 0;
4693 pPage->iFirstPresent = NIL_PGMPOOL_PRESENT_INDEX;
4694 pPage->pvLastAccessHandlerFault = 0;
4695 pPage->cLastAccessHandlerCount = 0;
4696 pPage->pvLastAccessHandlerRip = 0;
4697
4698 /*
4699 * Insert into the tracking and cache. If this fails, free the page.
4700 */
4701 int rc3 = pgmPoolTrackInsert(pPool, pPage, GCPhys, iUser, iUserTable);
4702 if (RT_FAILURE(rc3))
4703 {
4704 pPool->cUsedPages--;
4705 pPage->enmKind = PGMPOOLKIND_FREE;
4706 pPage->enmAccess = PGMPOOLACCESS_DONTCARE;
4707 pPage->GCPhys = NIL_RTGCPHYS;
4708 pPage->iNext = pPool->iFreeHead;
4709 pPool->iFreeHead = pPage->idx;
4710 pgmUnlock(pVM);
4711 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4712 Log(("pgmPoolAlloc: returns %Rrc (Insert)\n", rc3));
4713 return rc3;
4714 }
4715#endif /* PGMPOOL_WITH_USER_TRACKING */
4716
4717 /*
4718 * Commit the allocation, clear the page and return.
4719 */
4720#ifdef VBOX_WITH_STATISTICS
4721 if (pPool->cUsedPages > pPool->cUsedPagesHigh)
4722 pPool->cUsedPagesHigh = pPool->cUsedPages;
4723#endif
4724
4725 if (!pPage->fZeroed)
4726 {
4727 STAM_PROFILE_START(&pPool->StatZeroPage, z);
4728 void *pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
4729 ASMMemZeroPage(pv);
4730 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
4731 }
4732
4733 *ppPage = pPage;
4734 if (fLockPage)
4735 pgmPoolLockPage(pPool, pPage);
4736 pgmUnlock(pVM);
4737 LogFlow(("pgmPoolAlloc: returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
4738 rc, pPage, pPage->Core.Key, pPage->idx, pPage->fCached, pPage->fMonitored));
4739 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4740 return rc;
4741}
4742
4743
4744/**
4745 * Frees a usage of a pool page.
4746 *
4747 * @param pVM The VM handle.
4748 * @param HCPhys The HC physical address of the shadow page.
4749 * @param iUser The shadow page pool index of the user table.
4750 * @param iUserTable The index into the user table (shadowed).
4751 */
4752void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable)
4753{
4754 LogFlow(("pgmPoolFree: HCPhys=%RHp iUser=%#x iUserTable=%#x\n", HCPhys, iUser, iUserTable));
4755 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
4756 pgmPoolFreeByPage(pPool, pgmPoolGetPage(pPool, HCPhys), iUser, iUserTable);
4757}
4758
4759/**
4760 * Internal worker for finding a 'in-use' shadow page give by it's physical address.
4761 *
4762 * @returns Pointer to the shadow page structure.
4763 * @param pPool The pool.
4764 * @param HCPhys The HC physical address of the shadow page.
4765 */
4766PPGMPOOLPAGE pgmPoolGetPage(PPGMPOOL pPool, RTHCPHYS HCPhys)
4767{
4768 PVM pVM = pPool->CTX_SUFF(pVM);
4769
4770 Assert(PGMIsLockOwner(pVM));
4771
4772 /*
4773 * Look up the page.
4774 */
4775 pgmLock(pVM);
4776 PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, HCPhys & X86_PTE_PAE_PG_MASK);
4777 pgmUnlock(pVM);
4778
4779 AssertFatalMsg(pPage && pPage->enmKind != PGMPOOLKIND_FREE, ("HCPhys=%RHp pPage=%p idx=%d\n", HCPhys, pPage, (pPage) ? pPage->idx : 0));
4780 return pPage;
4781}
4782
4783#ifdef IN_RING3 /* currently only used in ring 3; save some space in the R0 & GC modules (left it here as we might need it elsewhere later on) */
4784/**
4785 * Flush the specified page if present
4786 *
4787 * @param pVM The VM handle.
4788 * @param GCPhys Guest physical address of the page to flush
4789 */
4790void pgmPoolFlushPageByGCPhys(PVM pVM, RTGCPHYS GCPhys)
4791{
4792#ifdef PGMPOOL_WITH_CACHE
4793 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
4794
4795 VM_ASSERT_EMT(pVM);
4796
4797 /*
4798 * Look up the GCPhys in the hash.
4799 */
4800 GCPhys = GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
4801 unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
4802 if (i == NIL_PGMPOOL_IDX)
4803 return;
4804
4805 do
4806 {
4807 PPGMPOOLPAGE pPage = &pPool->aPages[i];
4808 if (pPage->GCPhys - GCPhys < PAGE_SIZE)
4809 {
4810 switch (pPage->enmKind)
4811 {
4812 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
4813 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
4814 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
4815 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
4816 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
4817 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
4818 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
4819 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
4820 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
4821 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
4822 case PGMPOOLKIND_64BIT_PML4:
4823 case PGMPOOLKIND_32BIT_PD:
4824 case PGMPOOLKIND_PAE_PDPT:
4825 {
4826 Log(("PGMPoolFlushPage: found pgm pool pages for %RGp\n", GCPhys));
4827#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
4828 if (pPage->fDirty)
4829 STAM_COUNTER_INC(&pPool->StatForceFlushDirtyPage);
4830 else
4831#endif
4832 STAM_COUNTER_INC(&pPool->StatForceFlushPage);
4833 Assert(!pgmPoolIsPageLocked(&pVM->pgm.s, pPage));
4834 pgmPoolMonitorChainFlush(pPool, pPage);
4835 return;
4836 }
4837
4838 /* ignore, no monitoring. */
4839 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
4840 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
4841 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
4842 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
4843 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
4844 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
4845 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
4846 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
4847 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
4848 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
4849 case PGMPOOLKIND_ROOT_NESTED:
4850 case PGMPOOLKIND_PAE_PD_PHYS:
4851 case PGMPOOLKIND_PAE_PDPT_PHYS:
4852 case PGMPOOLKIND_32BIT_PD_PHYS:
4853 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
4854 break;
4855
4856 default:
4857 AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
4858 }
4859 }
4860
4861 /* next */
4862 i = pPage->iNext;
4863 } while (i != NIL_PGMPOOL_IDX);
4864#endif
4865 return;
4866}
4867#endif /* IN_RING3 */
4868
4869#ifdef IN_RING3
4870/**
4871 * Flushes the entire cache.
4872 *
4873 * It will assert a global CR3 flush (FF) and assumes the caller is aware of this
4874 * and execute this CR3 flush.
4875 *
4876 * @param pPool The pool.
4877 */
4878void pgmR3PoolReset(PVM pVM)
4879{
4880 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
4881
4882 Assert(PGMIsLockOwner(pVM));
4883 STAM_PROFILE_START(&pPool->StatFlushAllInt, a);
4884 LogFlow(("pgmPoolFlushAllInt:\n"));
4885
4886 /*
4887 * If there are no pages in the pool, there is nothing to do.
4888 */
4889 if (pPool->cCurPages <= PGMPOOL_IDX_FIRST)
4890 {
4891 STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
4892 return;
4893 }
4894
4895 /*
4896 * Exit the shadow mode since we're going to clear everything,
4897 * including the root page.
4898 */
4899 for (unsigned i=0;i<pVM->cCPUs;i++)
4900 {
4901 PVMCPU pVCpu = &pVM->aCpus[i];
4902 pgmR3ExitShadowModeBeforePoolFlush(pVM, pVCpu);
4903 }
4904
4905 /*
4906 * Nuke the free list and reinsert all pages into it.
4907 */
4908 for (unsigned i = pPool->cCurPages - 1; i >= PGMPOOL_IDX_FIRST; i--)
4909 {
4910 PPGMPOOLPAGE pPage = &pPool->aPages[i];
4911
4912 Assert(pPage->Core.Key == MMPage2Phys(pVM, pPage->pvPageR3));
4913#ifdef PGMPOOL_WITH_MONITORING
4914 if (pPage->fMonitored)
4915 pgmPoolMonitorFlush(pPool, pPage);
4916 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
4917 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
4918 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
4919 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
4920 pPage->cModifications = 0;
4921#endif
4922 pPage->GCPhys = NIL_RTGCPHYS;
4923 pPage->enmKind = PGMPOOLKIND_FREE;
4924 pPage->enmAccess = PGMPOOLACCESS_DONTCARE;
4925 Assert(pPage->idx == i);
4926 pPage->iNext = i + 1;
4927 pPage->fZeroed = false; /* This could probably be optimized, but better safe than sorry. */
4928 pPage->fSeenNonGlobal = false;
4929 pPage->fMonitored = false;
4930#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
4931 pPage->fDirty = false;
4932#endif
4933 pPage->fCached = false;
4934 pPage->fReusedFlushPending = false;
4935#ifdef PGMPOOL_WITH_USER_TRACKING
4936 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
4937#else
4938 pPage->fCR3Mix = false;
4939#endif
4940#ifdef PGMPOOL_WITH_CACHE
4941 pPage->iAgeNext = NIL_PGMPOOL_IDX;
4942 pPage->iAgePrev = NIL_PGMPOOL_IDX;
4943#endif
4944 pPage->cLocked = 0;
4945 }
4946 pPool->aPages[pPool->cCurPages - 1].iNext = NIL_PGMPOOL_IDX;
4947 pPool->iFreeHead = PGMPOOL_IDX_FIRST;
4948 pPool->cUsedPages = 0;
4949
4950#ifdef PGMPOOL_WITH_USER_TRACKING
4951 /*
4952 * Zap and reinitialize the user records.
4953 */
4954 pPool->cPresent = 0;
4955 pPool->iUserFreeHead = 0;
4956 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
4957 const unsigned cMaxUsers = pPool->cMaxUsers;
4958 for (unsigned i = 0; i < cMaxUsers; i++)
4959 {
4960 paUsers[i].iNext = i + 1;
4961 paUsers[i].iUser = NIL_PGMPOOL_IDX;
4962 paUsers[i].iUserTable = 0xfffffffe;
4963 }
4964 paUsers[cMaxUsers - 1].iNext = NIL_PGMPOOL_USER_INDEX;
4965#endif
4966
4967#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
4968 /*
4969 * Clear all the GCPhys links and rebuild the phys ext free list.
4970 */
4971 for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
4972 pRam;
4973 pRam = pRam->CTX_SUFF(pNext))
4974 {
4975 unsigned iPage = pRam->cb >> PAGE_SHIFT;
4976 while (iPage-- > 0)
4977 PGM_PAGE_SET_TRACKING(&pRam->aPages[iPage], 0);
4978 }
4979
4980 pPool->iPhysExtFreeHead = 0;
4981 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
4982 const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
4983 for (unsigned i = 0; i < cMaxPhysExts; i++)
4984 {
4985 paPhysExts[i].iNext = i + 1;
4986 paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
4987 paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
4988 paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
4989 }
4990 paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
4991#endif
4992
4993#ifdef PGMPOOL_WITH_MONITORING
4994 /*
4995 * Just zap the modified list.
4996 */
4997 pPool->cModifiedPages = 0;
4998 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
4999#endif
5000
5001#ifdef PGMPOOL_WITH_CACHE
5002 /*
5003 * Clear the GCPhys hash and the age list.
5004 */
5005 for (unsigned i = 0; i < RT_ELEMENTS(pPool->aiHash); i++)
5006 pPool->aiHash[i] = NIL_PGMPOOL_IDX;
5007 pPool->iAgeHead = NIL_PGMPOOL_IDX;
5008 pPool->iAgeTail = NIL_PGMPOOL_IDX;
5009#endif
5010
5011#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
5012 /* Clear all dirty pages. */
5013 pPool->idxFreeDirtyPage = 0;
5014 pPool->cDirtyPages = 0;
5015 for (unsigned i = 0; i < RT_ELEMENTS(pPool->aIdxDirtyPages); i++)
5016 pPool->aIdxDirtyPages[i] = NIL_PGMPOOL_IDX;
5017#endif
5018
5019 /*
5020 * Reinsert active pages into the hash and ensure monitoring chains are correct.
5021 */
5022 for (unsigned i = PGMPOOL_IDX_FIRST_SPECIAL; i < PGMPOOL_IDX_FIRST; i++)
5023 {
5024 PPGMPOOLPAGE pPage = &pPool->aPages[i];
5025 pPage->iNext = NIL_PGMPOOL_IDX;
5026#ifdef PGMPOOL_WITH_MONITORING
5027 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
5028 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
5029 pPage->cModifications = 0;
5030 /* ASSUMES that we're not sharing with any of the other special pages (safe for now). */
5031 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
5032 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
5033 if (pPage->fMonitored)
5034 {
5035 int rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
5036 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
5037 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
5038 pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
5039 pPool->pszAccessHandler);
5040 AssertFatalRCSuccess(rc);
5041# ifdef PGMPOOL_WITH_CACHE
5042 pgmPoolHashInsert(pPool, pPage);
5043# endif
5044 }
5045#endif
5046#ifdef PGMPOOL_WITH_USER_TRACKING
5047 Assert(pPage->iUserHead == NIL_PGMPOOL_USER_INDEX); /* for now */
5048#endif
5049#ifdef PGMPOOL_WITH_CACHE
5050 Assert(pPage->iAgeNext == NIL_PGMPOOL_IDX);
5051 Assert(pPage->iAgePrev == NIL_PGMPOOL_IDX);
5052#endif
5053 }
5054
5055 for (unsigned i=0;i<pVM->cCPUs;i++)
5056 {
5057 PVMCPU pVCpu = &pVM->aCpus[i];
5058 /*
5059 * Re-enter the shadowing mode and assert Sync CR3 FF.
5060 */
5061 pgmR3ReEnterShadowModeAfterPoolFlush(pVM, pVCpu);
5062 VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
5063 }
5064
5065 STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
5066}
5067#endif /* IN_RING3 */
5068
5069#ifdef LOG_ENABLED
5070static const char *pgmPoolPoolKindToStr(uint8_t enmKind)
5071{
5072 switch(enmKind)
5073 {
5074 case PGMPOOLKIND_INVALID:
5075 return "PGMPOOLKIND_INVALID";
5076 case PGMPOOLKIND_FREE:
5077 return "PGMPOOLKIND_FREE";
5078 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
5079 return "PGMPOOLKIND_32BIT_PT_FOR_PHYS";
5080 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
5081 return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT";
5082 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
5083 return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB";
5084 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
5085 return "PGMPOOLKIND_PAE_PT_FOR_PHYS";
5086 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
5087 return "PGMPOOLKIND_PAE_PT_FOR_32BIT_PT";
5088 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
5089 return "PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB";
5090 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
5091 return "PGMPOOLKIND_PAE_PT_FOR_PAE_PT";
5092 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
5093 return "PGMPOOLKIND_PAE_PT_FOR_PAE_2MB";
5094 case PGMPOOLKIND_32BIT_PD:
5095 return "PGMPOOLKIND_32BIT_PD";
5096 case PGMPOOLKIND_32BIT_PD_PHYS:
5097 return "PGMPOOLKIND_32BIT_PD_PHYS";
5098 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
5099 return "PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD";
5100 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
5101 return "PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD";
5102 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
5103 return "PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD";
5104 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
5105 return "PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD";
5106 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
5107 return "PGMPOOLKIND_PAE_PD_FOR_PAE_PD";
5108 case PGMPOOLKIND_PAE_PD_PHYS:
5109 return "PGMPOOLKIND_PAE_PD_PHYS";
5110 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
5111 return "PGMPOOLKIND_PAE_PDPT_FOR_32BIT";
5112 case PGMPOOLKIND_PAE_PDPT:
5113 return "PGMPOOLKIND_PAE_PDPT";
5114 case PGMPOOLKIND_PAE_PDPT_PHYS:
5115 return "PGMPOOLKIND_PAE_PDPT_PHYS";
5116 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
5117 return "PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT";
5118 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
5119 return "PGMPOOLKIND_64BIT_PDPT_FOR_PHYS";
5120 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
5121 return "PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD";
5122 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
5123 return "PGMPOOLKIND_64BIT_PD_FOR_PHYS";
5124 case PGMPOOLKIND_64BIT_PML4:
5125 return "PGMPOOLKIND_64BIT_PML4";
5126 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
5127 return "PGMPOOLKIND_EPT_PDPT_FOR_PHYS";
5128 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
5129 return "PGMPOOLKIND_EPT_PD_FOR_PHYS";
5130 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
5131 return "PGMPOOLKIND_EPT_PT_FOR_PHYS";
5132 case PGMPOOLKIND_ROOT_NESTED:
5133 return "PGMPOOLKIND_ROOT_NESTED";
5134 }
5135 return "Unknown kind!";
5136}
5137#endif /* LOG_ENABLED*/
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