VirtualBox

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

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

pgmPoolCacheFreeOne: Rewrote recursion to a finit loop; overflowing the stack is bad both in RC (#DF) and R0 (panic/corruption).

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

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