VirtualBox

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

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

Put back incorrectly removed tlb flushes (3.1 regression; #4485)

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

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