VirtualBox

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

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

pgmPoolMonitorFlush: details.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 158.9 KB
Line 
1/* $Id: PGMAllPool.cpp 18215 2009-03-24 18:39:39Z 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 uint16_t iToFree = pPool->iAgeTail;
1249 if (iToFree == iUser)
1250 iToFree = pPool->aPages[iToFree].iAgePrev;
1251/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
1252 if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
1253 {
1254 uint16_t i = pPool->aPages[iToFree].iAgePrev;
1255 for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
1256 {
1257 if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
1258 continue;
1259 iToFree = i;
1260 break;
1261 }
1262 }
1263*/
1264
1265 Assert(iToFree != iUser);
1266 AssertRelease(iToFree != NIL_PGMPOOL_IDX);
1267
1268 PPGMPOOLPAGE pPage = &pPool->aPages[iToFree];
1269
1270 /*
1271 * Reject any attempts at flushing the currently active shadow CR3 mapping
1272 */
1273 if (pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage))
1274 {
1275 /* Refresh the cr3 mapping by putting it at the head of the age list. */
1276 LogFlow(("pgmPoolCacheFreeOne refuse CR3 mapping\n"));
1277 pgmPoolCacheUsed(pPool, pPage);
1278 return pgmPoolCacheFreeOne(pPool, iUser);
1279 }
1280
1281 int rc = pgmPoolFlushPage(pPool, pPage);
1282 if (rc == VINF_SUCCESS)
1283 PGM_INVL_GUEST_TLBS(); /* see PT handler. */
1284 return rc;
1285}
1286
1287
1288/**
1289 * Checks if a kind mismatch is really a page being reused
1290 * or if it's just normal remappings.
1291 *
1292 * @returns true if reused and the cached page (enmKind1) should be flushed
1293 * @returns false if not reused.
1294 * @param enmKind1 The kind of the cached page.
1295 * @param enmKind2 The kind of the requested page.
1296 */
1297static bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
1298{
1299 switch (enmKind1)
1300 {
1301 /*
1302 * Never reuse them. There is no remapping in non-paging mode.
1303 */
1304 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1305 case PGMPOOLKIND_32BIT_PD_PHYS:
1306 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1307 case PGMPOOLKIND_PAE_PD_PHYS:
1308 case PGMPOOLKIND_PAE_PDPT_PHYS:
1309 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1310 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1311 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1312 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1313 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1314 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT: /* never reuse them for other types */
1315 return false;
1316
1317 /*
1318 * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
1319 */
1320 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1321 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1322 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1323 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1324 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
1325 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
1326 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
1327 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
1328 case PGMPOOLKIND_32BIT_PD:
1329 case PGMPOOLKIND_PAE_PDPT:
1330 switch (enmKind2)
1331 {
1332 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1333 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1334 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1335 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1336 case PGMPOOLKIND_64BIT_PML4:
1337 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1338 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1339 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1340 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1341 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1342 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1343 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1344 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1345 return true;
1346 default:
1347 return false;
1348 }
1349
1350 /*
1351 * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
1352 */
1353 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1354 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1355 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1356 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1357 case PGMPOOLKIND_64BIT_PML4:
1358 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1359 switch (enmKind2)
1360 {
1361 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1362 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1363 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1364 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1365 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
1366 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
1367 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
1368 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
1369 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1370 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1371 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1372 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1373 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1374 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1375 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1376 return true;
1377 default:
1378 return false;
1379 }
1380
1381 /*
1382 * These cannot be flushed, and it's common to reuse the PDs as PTs.
1383 */
1384 case PGMPOOLKIND_ROOT_NESTED:
1385 return false;
1386
1387 default:
1388 AssertFatalMsgFailed(("enmKind1=%d\n", enmKind1));
1389 }
1390}
1391
1392
1393/**
1394 * Attempts to satisfy a pgmPoolAlloc request from the cache.
1395 *
1396 * @returns VBox status code.
1397 * @retval VINF_PGM_CACHED_PAGE on success.
1398 * @retval VERR_FILE_NOT_FOUND if not found.
1399 * @param pPool The pool.
1400 * @param GCPhys The GC physical address of the page we're gonna shadow.
1401 * @param enmKind The kind of mapping.
1402 * @param iUser The shadow page pool index of the user table.
1403 * @param iUserTable The index into the user table (shadowed).
1404 * @param ppPage Where to store the pointer to the page.
1405 */
1406static int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
1407{
1408#ifndef IN_RC
1409 const PVM pVM = pPool->CTX_SUFF(pVM);
1410#endif
1411 /*
1412 * Look up the GCPhys in the hash.
1413 */
1414 unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
1415 Log3(("pgmPoolCacheAlloc: %RGp kind %s iUser=%x iUserTable=%x SLOT=%d\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable, i));
1416 if (i != NIL_PGMPOOL_IDX)
1417 {
1418 do
1419 {
1420 PPGMPOOLPAGE pPage = &pPool->aPages[i];
1421 Log4(("pgmPoolCacheAlloc: slot %d found page %RGp\n", i, pPage->GCPhys));
1422 if (pPage->GCPhys == GCPhys)
1423 {
1424 if ((PGMPOOLKIND)pPage->enmKind == enmKind)
1425 {
1426 /* Put it at the start of the use list to make sure pgmPoolTrackAddUser
1427 * doesn't flush it in case there are no more free use records.
1428 */
1429 pgmPoolCacheUsed(pPool, pPage);
1430
1431 int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
1432 if (RT_SUCCESS(rc))
1433 {
1434 Assert((PGMPOOLKIND)pPage->enmKind == enmKind);
1435 *ppPage = pPage;
1436 STAM_COUNTER_INC(&pPool->StatCacheHits);
1437 return VINF_PGM_CACHED_PAGE;
1438 }
1439 return rc;
1440 }
1441
1442 /*
1443 * The kind is different. In some cases we should now flush the page
1444 * as it has been reused, but in most cases this is normal remapping
1445 * of PDs as PT or big pages using the GCPhys field in a slightly
1446 * different way than the other kinds.
1447 */
1448 if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
1449 {
1450 STAM_COUNTER_INC(&pPool->StatCacheKindMismatches);
1451 pgmPoolFlushPage(pPool, pPage);
1452 PGM_INVL_GUEST_TLBS(); /* see PT handler. */
1453 break;
1454 }
1455 }
1456
1457 /* next */
1458 i = pPage->iNext;
1459 } while (i != NIL_PGMPOOL_IDX);
1460 }
1461
1462 Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%s\n", GCPhys, pgmPoolPoolKindToStr(enmKind)));
1463 STAM_COUNTER_INC(&pPool->StatCacheMisses);
1464 return VERR_FILE_NOT_FOUND;
1465}
1466
1467
1468/**
1469 * Inserts a page into the cache.
1470 *
1471 * @param pPool The pool.
1472 * @param pPage The cached page.
1473 * @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
1474 */
1475static void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
1476{
1477 /*
1478 * Insert into the GCPhys hash if the page is fit for that.
1479 */
1480 Assert(!pPage->fCached);
1481 if (fCanBeCached)
1482 {
1483 pPage->fCached = true;
1484 pgmPoolHashInsert(pPool, pPage);
1485 Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
1486 pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
1487 STAM_COUNTER_INC(&pPool->StatCacheCacheable);
1488 }
1489 else
1490 {
1491 Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
1492 pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
1493 STAM_COUNTER_INC(&pPool->StatCacheUncacheable);
1494 }
1495
1496 /*
1497 * Insert at the head of the age list.
1498 */
1499 pPage->iAgePrev = NIL_PGMPOOL_IDX;
1500 pPage->iAgeNext = pPool->iAgeHead;
1501 if (pPool->iAgeHead != NIL_PGMPOOL_IDX)
1502 pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
1503 else
1504 pPool->iAgeTail = pPage->idx;
1505 pPool->iAgeHead = pPage->idx;
1506}
1507
1508
1509/**
1510 * Flushes a cached page.
1511 *
1512 * @param pPool The pool.
1513 * @param pPage The cached page.
1514 */
1515static void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1516{
1517 Log3(("pgmPoolCacheFlushPage: %RGp\n", pPage->GCPhys));
1518
1519 /*
1520 * Remove the page from the hash.
1521 */
1522 if (pPage->fCached)
1523 {
1524 pPage->fCached = false;
1525 pgmPoolHashRemove(pPool, pPage);
1526 }
1527 else
1528 Assert(pPage->iNext == NIL_PGMPOOL_IDX);
1529
1530 /*
1531 * Remove it from the age list.
1532 */
1533 if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
1534 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
1535 else
1536 pPool->iAgeTail = pPage->iAgePrev;
1537 if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
1538 pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
1539 else
1540 pPool->iAgeHead = pPage->iAgeNext;
1541 pPage->iAgeNext = NIL_PGMPOOL_IDX;
1542 pPage->iAgePrev = NIL_PGMPOOL_IDX;
1543}
1544
1545#endif /* PGMPOOL_WITH_CACHE */
1546#ifdef PGMPOOL_WITH_MONITORING
1547
1548/**
1549 * Looks for pages sharing the monitor.
1550 *
1551 * @returns Pointer to the head page.
1552 * @returns NULL if not found.
1553 * @param pPool The Pool
1554 * @param pNewPage The page which is going to be monitored.
1555 */
1556static PPGMPOOLPAGE pgmPoolMonitorGetPageByGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pNewPage)
1557{
1558#ifdef PGMPOOL_WITH_CACHE
1559 /*
1560 * Look up the GCPhys in the hash.
1561 */
1562 RTGCPHYS GCPhys = pNewPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
1563 unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
1564 if (i == NIL_PGMPOOL_IDX)
1565 return NULL;
1566 do
1567 {
1568 PPGMPOOLPAGE pPage = &pPool->aPages[i];
1569 if ( pPage->GCPhys - GCPhys < PAGE_SIZE
1570 && pPage != pNewPage)
1571 {
1572 switch (pPage->enmKind)
1573 {
1574 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1575 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1576 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1577 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
1578 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
1579 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
1580 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
1581 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1582 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1583 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1584 case PGMPOOLKIND_64BIT_PML4:
1585 case PGMPOOLKIND_32BIT_PD:
1586 case PGMPOOLKIND_PAE_PDPT:
1587 {
1588 /* find the head */
1589 while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
1590 {
1591 Assert(pPage->iMonitoredPrev != pPage->idx);
1592 pPage = &pPool->aPages[pPage->iMonitoredPrev];
1593 }
1594 return pPage;
1595 }
1596
1597 /* ignore, no monitoring. */
1598 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1599 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1600 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1601 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1602 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1603 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1604 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1605 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1606 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1607 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1608 case PGMPOOLKIND_ROOT_NESTED:
1609 case PGMPOOLKIND_PAE_PD_PHYS:
1610 case PGMPOOLKIND_PAE_PDPT_PHYS:
1611 case PGMPOOLKIND_32BIT_PD_PHYS:
1612 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
1613 break;
1614 default:
1615 AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
1616 }
1617 }
1618
1619 /* next */
1620 i = pPage->iNext;
1621 } while (i != NIL_PGMPOOL_IDX);
1622#endif
1623 return NULL;
1624}
1625
1626
1627/**
1628 * Enabled write monitoring of a guest page.
1629 *
1630 * @returns VBox status code.
1631 * @retval VINF_SUCCESS on success.
1632 * @param pPool The pool.
1633 * @param pPage The cached page.
1634 */
1635static int pgmPoolMonitorInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1636{
1637 LogFlow(("pgmPoolMonitorInsert %RGp\n", pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1)));
1638
1639 /*
1640 * Filter out the relevant kinds.
1641 */
1642 switch (pPage->enmKind)
1643 {
1644 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1645 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1646 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1647 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1648 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1649 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1650 case PGMPOOLKIND_64BIT_PML4:
1651 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
1652 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
1653 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
1654 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
1655 case PGMPOOLKIND_32BIT_PD:
1656 case PGMPOOLKIND_PAE_PDPT:
1657 break;
1658
1659 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1660 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1661 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1662 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1663 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1664 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1665 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1666 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1667 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1668 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1669 case PGMPOOLKIND_ROOT_NESTED:
1670 /* Nothing to monitor here. */
1671 return VINF_SUCCESS;
1672
1673 case PGMPOOLKIND_32BIT_PD_PHYS:
1674 case PGMPOOLKIND_PAE_PDPT_PHYS:
1675 case PGMPOOLKIND_PAE_PD_PHYS:
1676 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
1677 /* Nothing to monitor here. */
1678 return VINF_SUCCESS;
1679#ifdef PGMPOOL_WITH_MIXED_PT_CR3
1680 break;
1681#else
1682 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
1683#endif
1684 default:
1685 AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
1686 }
1687
1688 /*
1689 * Install handler.
1690 */
1691 int rc;
1692 PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
1693 if (pPageHead)
1694 {
1695 Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
1696 Assert(pPageHead->iMonitoredPrev != pPage->idx);
1697 pPage->iMonitoredPrev = pPageHead->idx;
1698 pPage->iMonitoredNext = pPageHead->iMonitoredNext;
1699 if (pPageHead->iMonitoredNext != NIL_PGMPOOL_IDX)
1700 pPool->aPages[pPageHead->iMonitoredNext].iMonitoredPrev = pPage->idx;
1701 pPageHead->iMonitoredNext = pPage->idx;
1702 rc = VINF_SUCCESS;
1703 }
1704 else
1705 {
1706 Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX); Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
1707 PVM pVM = pPool->CTX_SUFF(pVM);
1708 const RTGCPHYS GCPhysPage = pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
1709 rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
1710 GCPhysPage, GCPhysPage + (PAGE_SIZE - 1),
1711 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
1712 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
1713 pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
1714 pPool->pszAccessHandler);
1715 /** @todo we should probably deal with out-of-memory conditions here, but for now increasing
1716 * the heap size should suffice. */
1717 AssertFatalRC(rc);
1718 Assert(!(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL) || VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3));
1719 }
1720 pPage->fMonitored = true;
1721 return rc;
1722}
1723
1724
1725/**
1726 * Disables write monitoring of a guest page.
1727 *
1728 * @returns VBox status code.
1729 * @retval VINF_SUCCESS on success.
1730 * @param pPool The pool.
1731 * @param pPage The cached page.
1732 */
1733static int pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1734{
1735 /*
1736 * Filter out the relevant kinds.
1737 */
1738 switch (pPage->enmKind)
1739 {
1740 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1741 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1742 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1743 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1744 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1745 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1746 case PGMPOOLKIND_64BIT_PML4:
1747 case PGMPOOLKIND_32BIT_PD:
1748 case PGMPOOLKIND_PAE_PDPT:
1749 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
1750 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
1751 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
1752 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
1753 break;
1754
1755 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1756 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1757 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1758 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1759 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1760 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
1761 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
1762 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
1763 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
1764 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
1765 case PGMPOOLKIND_ROOT_NESTED:
1766 case PGMPOOLKIND_PAE_PD_PHYS:
1767 case PGMPOOLKIND_PAE_PDPT_PHYS:
1768 case PGMPOOLKIND_32BIT_PD_PHYS:
1769 /* Nothing to monitor here. */
1770 return VINF_SUCCESS;
1771
1772#ifdef PGMPOOL_WITH_MIXED_PT_CR3
1773 break;
1774#endif
1775 default:
1776 AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
1777 }
1778
1779 /*
1780 * Remove the page from the monitored list or uninstall it if last.
1781 */
1782 const PVM pVM = pPool->CTX_SUFF(pVM);
1783 int rc;
1784 if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
1785 || pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
1786 {
1787 if (pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
1788 {
1789 PPGMPOOLPAGE pNewHead = &pPool->aPages[pPage->iMonitoredNext];
1790 pNewHead->iMonitoredPrev = NIL_PGMPOOL_IDX;
1791 rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
1792 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pNewHead),
1793 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pNewHead),
1794 pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pNewHead),
1795 pPool->pszAccessHandler);
1796 AssertFatalRCSuccess(rc);
1797 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
1798 }
1799 else
1800 {
1801 pPool->aPages[pPage->iMonitoredPrev].iMonitoredNext = pPage->iMonitoredNext;
1802 if (pPage->iMonitoredNext != NIL_PGMPOOL_IDX)
1803 {
1804 pPool->aPages[pPage->iMonitoredNext].iMonitoredPrev = pPage->iMonitoredPrev;
1805 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
1806 }
1807 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
1808 rc = VINF_SUCCESS;
1809 }
1810 }
1811 else
1812 {
1813 rc = PGMHandlerPhysicalDeregister(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1));
1814 AssertFatalRC(rc);
1815 AssertMsg(!(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL) || VM_FF_ISSET(pVM, VM_FF_PGM_SYNC_CR3),
1816 ("%#x %#x\n", pVM->pgm.s.fSyncFlags, pVM->fForcedActions));
1817 }
1818 pPage->fMonitored = false;
1819
1820 /*
1821 * Remove it from the list of modified pages (if in it).
1822 */
1823 pgmPoolMonitorModifiedRemove(pPool, pPage);
1824
1825 return rc;
1826}
1827
1828
1829/**
1830 * Inserts the page into the list of modified pages.
1831 *
1832 * @param pPool The pool.
1833 * @param pPage The page.
1834 */
1835void pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1836{
1837 Log3(("pgmPoolMonitorModifiedInsert: idx=%d\n", pPage->idx));
1838 AssertMsg( pPage->iModifiedNext == NIL_PGMPOOL_IDX
1839 && pPage->iModifiedPrev == NIL_PGMPOOL_IDX
1840 && pPool->iModifiedHead != pPage->idx,
1841 ("Next=%d Prev=%d idx=%d cModifications=%d Head=%d cModifiedPages=%d\n",
1842 pPage->iModifiedNext, pPage->iModifiedPrev, pPage->idx, pPage->cModifications,
1843 pPool->iModifiedHead, pPool->cModifiedPages));
1844
1845 pPage->iModifiedNext = pPool->iModifiedHead;
1846 if (pPool->iModifiedHead != NIL_PGMPOOL_IDX)
1847 pPool->aPages[pPool->iModifiedHead].iModifiedPrev = pPage->idx;
1848 pPool->iModifiedHead = pPage->idx;
1849 pPool->cModifiedPages++;
1850#ifdef VBOX_WITH_STATISTICS
1851 if (pPool->cModifiedPages > pPool->cModifiedPagesHigh)
1852 pPool->cModifiedPagesHigh = pPool->cModifiedPages;
1853#endif
1854}
1855
1856
1857/**
1858 * Removes the page from the list of modified pages and resets the
1859 * moficiation counter.
1860 *
1861 * @param pPool The pool.
1862 * @param pPage The page which is believed to be in the list of modified pages.
1863 */
1864static void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1865{
1866 Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
1867 if (pPool->iModifiedHead == pPage->idx)
1868 {
1869 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
1870 pPool->iModifiedHead = pPage->iModifiedNext;
1871 if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
1872 {
1873 pPool->aPages[pPage->iModifiedNext].iModifiedPrev = NIL_PGMPOOL_IDX;
1874 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1875 }
1876 pPool->cModifiedPages--;
1877 }
1878 else if (pPage->iModifiedPrev != NIL_PGMPOOL_IDX)
1879 {
1880 pPool->aPages[pPage->iModifiedPrev].iModifiedNext = pPage->iModifiedNext;
1881 if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
1882 {
1883 pPool->aPages[pPage->iModifiedNext].iModifiedPrev = pPage->iModifiedPrev;
1884 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1885 }
1886 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
1887 pPool->cModifiedPages--;
1888 }
1889 else
1890 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
1891 pPage->cModifications = 0;
1892}
1893
1894
1895/**
1896 * Zaps the list of modified pages, resetting their modification counters in the process.
1897 *
1898 * @param pVM The VM handle.
1899 */
1900void pgmPoolMonitorModifiedClearAll(PVM pVM)
1901{
1902 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1903 LogFlow(("pgmPoolMonitorModifiedClearAll: cModifiedPages=%d\n", pPool->cModifiedPages));
1904
1905 unsigned cPages = 0; NOREF(cPages);
1906 uint16_t idx = pPool->iModifiedHead;
1907 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
1908 while (idx != NIL_PGMPOOL_IDX)
1909 {
1910 PPGMPOOLPAGE pPage = &pPool->aPages[idx];
1911 idx = pPage->iModifiedNext;
1912 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1913 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
1914 pPage->cModifications = 0;
1915 Assert(++cPages);
1916 }
1917 AssertMsg(cPages == pPool->cModifiedPages, ("%d != %d\n", cPages, pPool->cModifiedPages));
1918 pPool->cModifiedPages = 0;
1919}
1920
1921
1922#ifdef IN_RING3
1923/**
1924 * Clear all shadow pages and clear all modification counters.
1925 *
1926 * @param pVM The VM handle.
1927 * @remark Should only be used when monitoring is available, thus placed in
1928 * the PGMPOOL_WITH_MONITORING #ifdef.
1929 */
1930void pgmPoolClearAll(PVM pVM)
1931{
1932 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
1933 STAM_PROFILE_START(&pPool->StatClearAll, c);
1934 LogFlow(("pgmPoolClearAll: cUsedPages=%d\n", pPool->cUsedPages));
1935
1936 /*
1937 * Iterate all the pages until we've encountered all that in use.
1938 * This is simple but not quite optimal solution.
1939 */
1940 unsigned cModifiedPages = 0; NOREF(cModifiedPages);
1941 unsigned cLeft = pPool->cUsedPages;
1942 unsigned iPage = pPool->cCurPages;
1943 while (--iPage >= PGMPOOL_IDX_FIRST)
1944 {
1945 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
1946 if (pPage->GCPhys != NIL_RTGCPHYS)
1947 {
1948 switch (pPage->enmKind)
1949 {
1950 /*
1951 * We only care about shadow page tables.
1952 */
1953 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1954 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1955 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1956 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1957 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1958 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1959 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1960 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1961 {
1962#ifdef PGMPOOL_WITH_USER_TRACKING
1963 if (pPage->cPresent)
1964#endif
1965 {
1966 void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
1967 STAM_PROFILE_START(&pPool->StatZeroPage, z);
1968 ASMMemZeroPage(pvShw);
1969 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
1970#ifdef PGMPOOL_WITH_USER_TRACKING
1971 pPage->cPresent = 0;
1972 pPage->iFirstPresent = ~0;
1973#endif
1974 }
1975 }
1976 /* fall thru */
1977
1978 default:
1979 Assert(!pPage->cModifications || ++cModifiedPages);
1980 Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
1981 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
1982 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1983 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
1984 pPage->cModifications = 0;
1985 break;
1986
1987 }
1988 if (!--cLeft)
1989 break;
1990 }
1991 }
1992
1993 /* swipe the special pages too. */
1994 for (iPage = PGMPOOL_IDX_FIRST_SPECIAL; iPage < PGMPOOL_IDX_FIRST; iPage++)
1995 {
1996 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
1997 if (pPage->GCPhys != NIL_RTGCPHYS)
1998 {
1999 Assert(!pPage->cModifications || ++cModifiedPages);
2000 Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
2001 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
2002 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
2003 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
2004 pPage->cModifications = 0;
2005 }
2006 }
2007
2008#ifndef DEBUG_michael
2009 AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
2010#endif
2011 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
2012 pPool->cModifiedPages = 0;
2013
2014#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2015 /*
2016 * Clear all the GCPhys links and rebuild the phys ext free list.
2017 */
2018 for (PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
2019 pRam;
2020 pRam = pRam->CTX_SUFF(pNext))
2021 {
2022 unsigned iPage = pRam->cb >> PAGE_SHIFT;
2023 while (iPage-- > 0)
2024 PGM_PAGE_SET_TRACKING(&pRam->aPages[iPage], 0);
2025 }
2026
2027 pPool->iPhysExtFreeHead = 0;
2028 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
2029 const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
2030 for (unsigned i = 0; i < cMaxPhysExts; i++)
2031 {
2032 paPhysExts[i].iNext = i + 1;
2033 paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
2034 paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
2035 paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
2036 }
2037 paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
2038#endif
2039
2040
2041 pPool->cPresent = 0;
2042 STAM_PROFILE_STOP(&pPool->StatClearAll, c);
2043}
2044#endif /* IN_RING3 */
2045
2046
2047/**
2048 * Handle SyncCR3 pool tasks
2049 *
2050 * @returns VBox status code.
2051 * @retval VINF_SUCCESS if successfully added.
2052 * @retval VINF_PGM_SYNC_CR3 is it needs to be deferred to ring 3 (GC only)
2053 * @param pVM The VM handle.
2054 * @remark Should only be used when monitoring is available, thus placed in
2055 * the PGMPOOL_WITH_MONITORING #ifdef.
2056 */
2057int pgmPoolSyncCR3(PVM pVM)
2058{
2059 LogFlow(("pgmPoolSyncCR3\n"));
2060 /*
2061 * When monitoring shadowed pages, we reset the modification counters on CR3 sync.
2062 * Occasionally we will have to clear all the shadow page tables because we wanted
2063 * to monitor a page which was mapped by too many shadowed page tables. This operation
2064 * sometimes refered to as a 'lightweight flush'.
2065 */
2066 if (!(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL))
2067 pgmPoolMonitorModifiedClearAll(pVM);
2068 else
2069 {
2070# ifdef IN_RING3 /* Don't flush in ring-0 or raw mode, it's taking too long. */
2071 pVM->pgm.s.fSyncFlags &= ~PGM_SYNC_CLEAR_PGM_POOL;
2072 pgmPoolClearAll(pVM);
2073# else /* !IN_RING3 */
2074 LogFlow(("SyncCR3: PGM_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
2075 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3); /** @todo no need to do global sync, right? */
2076 return VINF_PGM_SYNC_CR3;
2077# endif /* !IN_RING3 */
2078 }
2079 return VINF_SUCCESS;
2080}
2081
2082#endif /* PGMPOOL_WITH_MONITORING */
2083#ifdef PGMPOOL_WITH_USER_TRACKING
2084
2085/**
2086 * Frees up at least one user entry.
2087 *
2088 * @returns VBox status code.
2089 * @retval VINF_SUCCESS if successfully added.
2090 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
2091 * @param pPool The pool.
2092 * @param iUser The user index.
2093 */
2094static int pgmPoolTrackFreeOneUser(PPGMPOOL pPool, uint16_t iUser)
2095{
2096 STAM_COUNTER_INC(&pPool->StatTrackFreeUpOneUser);
2097#ifdef PGMPOOL_WITH_CACHE
2098 /*
2099 * Just free cached pages in a braindead fashion.
2100 */
2101 /** @todo walk the age list backwards and free the first with usage. */
2102 int rc = VINF_SUCCESS;
2103 do
2104 {
2105 int rc2 = pgmPoolCacheFreeOne(pPool, iUser);
2106 if (RT_FAILURE(rc2) && rc == VINF_SUCCESS)
2107 rc = rc2;
2108 } while (pPool->iUserFreeHead == NIL_PGMPOOL_USER_INDEX);
2109 return rc;
2110#else
2111 /*
2112 * Lazy approach.
2113 */
2114 /* @todo This path no longer works (CR3 root pages will be flushed)!! */
2115 AssertCompileFailed();
2116 Assert(!CPUMIsGuestInLongMode(pVM));
2117 pgmPoolFlushAllInt(pPool);
2118 return VERR_PGM_POOL_FLUSHED;
2119#endif
2120}
2121
2122
2123/**
2124 * Inserts a page into the cache.
2125 *
2126 * This will create user node for the page, insert it into the GCPhys
2127 * hash, and insert it into the age list.
2128 *
2129 * @returns VBox status code.
2130 * @retval VINF_SUCCESS if successfully added.
2131 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
2132 * @param pPool The pool.
2133 * @param pPage The cached page.
2134 * @param GCPhys The GC physical address of the page we're gonna shadow.
2135 * @param iUser The user index.
2136 * @param iUserTable The user table index.
2137 */
2138DECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
2139{
2140 int rc = VINF_SUCCESS;
2141 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
2142
2143 LogFlow(("pgmPoolTrackInsert GCPhys=%RGp iUser %x iUserTable %x\n", GCPhys, iUser, iUserTable));
2144
2145#ifdef VBOX_STRICT
2146 /*
2147 * Check that the entry doesn't already exists.
2148 */
2149 if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
2150 {
2151 uint16_t i = pPage->iUserHead;
2152 do
2153 {
2154 Assert(i < pPool->cMaxUsers);
2155 AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
2156 i = paUsers[i].iNext;
2157 } while (i != NIL_PGMPOOL_USER_INDEX);
2158 }
2159#endif
2160
2161 /*
2162 * Find free a user node.
2163 */
2164 uint16_t i = pPool->iUserFreeHead;
2165 if (i == NIL_PGMPOOL_USER_INDEX)
2166 {
2167 int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
2168 if (RT_FAILURE(rc))
2169 return rc;
2170 i = pPool->iUserFreeHead;
2171 }
2172
2173 /*
2174 * Unlink the user node from the free list,
2175 * initialize and insert it into the user list.
2176 */
2177 pPool->iUserFreeHead = paUsers[i].iNext;
2178 paUsers[i].iNext = NIL_PGMPOOL_USER_INDEX;
2179 paUsers[i].iUser = iUser;
2180 paUsers[i].iUserTable = iUserTable;
2181 pPage->iUserHead = i;
2182
2183 /*
2184 * Insert into cache and enable monitoring of the guest page if enabled.
2185 *
2186 * Until we implement caching of all levels, including the CR3 one, we'll
2187 * have to make sure we don't try monitor & cache any recursive reuse of
2188 * a monitored CR3 page. Because all windows versions are doing this we'll
2189 * have to be able to do combined access monitoring, CR3 + PT and
2190 * PD + PT (guest PAE).
2191 *
2192 * Update:
2193 * We're now cooperating with the CR3 monitor if an uncachable page is found.
2194 */
2195#if defined(PGMPOOL_WITH_MONITORING) || defined(PGMPOOL_WITH_CACHE)
2196# ifdef PGMPOOL_WITH_MIXED_PT_CR3
2197 const bool fCanBeMonitored = true;
2198# else
2199 bool fCanBeMonitored = pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored == NIL_RTGCPHYS
2200 || (GCPhys & X86_PTE_PAE_PG_MASK) != (pPool->CTX_SUFF(pVM)->pgm.s.GCPhysGstCR3Monitored & X86_PTE_PAE_PG_MASK)
2201 || pgmPoolIsBigPage((PGMPOOLKIND)pPage->enmKind);
2202# endif
2203# ifdef PGMPOOL_WITH_CACHE
2204 pgmPoolCacheInsert(pPool, pPage, fCanBeMonitored); /* This can be expanded. */
2205# endif
2206 if (fCanBeMonitored)
2207 {
2208# ifdef PGMPOOL_WITH_MONITORING
2209 rc = pgmPoolMonitorInsert(pPool, pPage);
2210 AssertRC(rc);
2211 }
2212# endif
2213#endif /* PGMPOOL_WITH_MONITORING */
2214 return rc;
2215}
2216
2217
2218# ifdef PGMPOOL_WITH_CACHE /* (only used when the cache is enabled.) */
2219/**
2220 * Adds a user reference to a page.
2221 *
2222 * This will move the page to the head of the
2223 *
2224 * @returns VBox status code.
2225 * @retval VINF_SUCCESS if successfully added.
2226 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
2227 * @param pPool The pool.
2228 * @param pPage The cached page.
2229 * @param iUser The user index.
2230 * @param iUserTable The user table.
2231 */
2232static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
2233{
2234 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
2235
2236 Log3(("pgmPoolTrackAddUser GCPhys = %RGp iUser %x iUserTable %x\n", pPage->GCPhys, iUser, iUserTable));
2237# ifdef VBOX_STRICT
2238 /*
2239 * Check that the entry doesn't already exists.
2240 */
2241 if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
2242 {
2243 uint16_t i = pPage->iUserHead;
2244 do
2245 {
2246 Assert(i < pPool->cMaxUsers);
2247 AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
2248 i = paUsers[i].iNext;
2249 } while (i != NIL_PGMPOOL_USER_INDEX);
2250 }
2251# endif
2252
2253 /*
2254 * Allocate a user node.
2255 */
2256 uint16_t i = pPool->iUserFreeHead;
2257 if (i == NIL_PGMPOOL_USER_INDEX)
2258 {
2259 int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
2260 if (RT_FAILURE(rc))
2261 return rc;
2262 i = pPool->iUserFreeHead;
2263 }
2264 pPool->iUserFreeHead = paUsers[i].iNext;
2265
2266 /*
2267 * Initialize the user node and insert it.
2268 */
2269 paUsers[i].iNext = pPage->iUserHead;
2270 paUsers[i].iUser = iUser;
2271 paUsers[i].iUserTable = iUserTable;
2272 pPage->iUserHead = i;
2273
2274# ifdef PGMPOOL_WITH_CACHE
2275 /*
2276 * Tell the cache to update its replacement stats for this page.
2277 */
2278 pgmPoolCacheUsed(pPool, pPage);
2279# endif
2280 return VINF_SUCCESS;
2281}
2282# endif /* PGMPOOL_WITH_CACHE */
2283
2284
2285/**
2286 * Frees a user record associated with a page.
2287 *
2288 * This does not clear the entry in the user table, it simply replaces the
2289 * user record to the chain of free records.
2290 *
2291 * @param pPool The pool.
2292 * @param HCPhys The HC physical address of the shadow page.
2293 * @param iUser The shadow page pool index of the user table.
2294 * @param iUserTable The index into the user table (shadowed).
2295 */
2296static void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
2297{
2298 /*
2299 * Unlink and free the specified user entry.
2300 */
2301 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
2302
2303 Log3(("pgmPoolTrackFreeUser %RGp %x %x\n", pPage->GCPhys, iUser, iUserTable));
2304 /* Special: For PAE and 32-bit paging, there is usually no more than one user. */
2305 uint16_t i = pPage->iUserHead;
2306 if ( i != NIL_PGMPOOL_USER_INDEX
2307 && paUsers[i].iUser == iUser
2308 && paUsers[i].iUserTable == iUserTable)
2309 {
2310 pPage->iUserHead = paUsers[i].iNext;
2311
2312 paUsers[i].iUser = NIL_PGMPOOL_IDX;
2313 paUsers[i].iNext = pPool->iUserFreeHead;
2314 pPool->iUserFreeHead = i;
2315 return;
2316 }
2317
2318 /* General: Linear search. */
2319 uint16_t iPrev = NIL_PGMPOOL_USER_INDEX;
2320 while (i != NIL_PGMPOOL_USER_INDEX)
2321 {
2322 if ( paUsers[i].iUser == iUser
2323 && paUsers[i].iUserTable == iUserTable)
2324 {
2325 if (iPrev != NIL_PGMPOOL_USER_INDEX)
2326 paUsers[iPrev].iNext = paUsers[i].iNext;
2327 else
2328 pPage->iUserHead = paUsers[i].iNext;
2329
2330 paUsers[i].iUser = NIL_PGMPOOL_IDX;
2331 paUsers[i].iNext = pPool->iUserFreeHead;
2332 pPool->iUserFreeHead = i;
2333 return;
2334 }
2335 iPrev = i;
2336 i = paUsers[i].iNext;
2337 }
2338
2339 /* Fatal: didn't find it */
2340 AssertFatalMsgFailed(("Didn't find the user entry! iUser=%#x iUserTable=%#x GCPhys=%RGp\n",
2341 iUser, iUserTable, pPage->GCPhys));
2342}
2343
2344
2345/**
2346 * Gets the entry size of a shadow table.
2347 *
2348 * @param enmKind The kind of page.
2349 *
2350 * @returns The size of the entry in bytes. That is, 4 or 8.
2351 * @returns If the kind is not for a table, an assertion is raised and 0 is
2352 * returned.
2353 */
2354DECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind)
2355{
2356 switch (enmKind)
2357 {
2358 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2359 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2360 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2361 case PGMPOOLKIND_32BIT_PD:
2362 case PGMPOOLKIND_32BIT_PD_PHYS:
2363 return 4;
2364
2365 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2366 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2367 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2368 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2369 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2370 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
2371 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
2372 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
2373 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
2374 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2375 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2376 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2377 case PGMPOOLKIND_64BIT_PML4:
2378 case PGMPOOLKIND_PAE_PDPT:
2379 case PGMPOOLKIND_ROOT_NESTED:
2380 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2381 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2382 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2383 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2384 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
2385 case PGMPOOLKIND_PAE_PD_PHYS:
2386 case PGMPOOLKIND_PAE_PDPT_PHYS:
2387 return 8;
2388
2389 default:
2390 AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
2391 }
2392}
2393
2394
2395/**
2396 * Gets the entry size of a guest table.
2397 *
2398 * @param enmKind The kind of page.
2399 *
2400 * @returns The size of the entry in bytes. That is, 0, 4 or 8.
2401 * @returns If the kind is not for a table, an assertion is raised and 0 is
2402 * returned.
2403 */
2404DECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind)
2405{
2406 switch (enmKind)
2407 {
2408 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2409 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2410 case PGMPOOLKIND_32BIT_PD:
2411 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2412 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2413 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
2414 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
2415 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
2416 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
2417 return 4;
2418
2419 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2420 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2421 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2422 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2423 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2424 case PGMPOOLKIND_64BIT_PML4:
2425 case PGMPOOLKIND_PAE_PDPT:
2426 return 8;
2427
2428 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2429 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2430 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2431 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2432 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2433 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2434 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
2435 case PGMPOOLKIND_ROOT_NESTED:
2436 case PGMPOOLKIND_PAE_PD_PHYS:
2437 case PGMPOOLKIND_PAE_PDPT_PHYS:
2438 case PGMPOOLKIND_32BIT_PD_PHYS:
2439 /** @todo can we return 0? (nobody is calling this...) */
2440 AssertFailed();
2441 return 0;
2442
2443 default:
2444 AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
2445 }
2446}
2447
2448#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2449
2450/**
2451 * Scans one shadow page table for mappings of a physical page.
2452 *
2453 * @param pVM The VM handle.
2454 * @param pPhysPage The guest page in question.
2455 * @param iShw The shadow page table.
2456 * @param cRefs The number of references made in that PT.
2457 */
2458static void pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
2459{
2460 LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
2461 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2462
2463 /*
2464 * Assert sanity.
2465 */
2466 Assert(cRefs == 1);
2467 AssertFatalMsg(iShw < pPool->cCurPages && iShw != NIL_PGMPOOL_IDX, ("iShw=%d\n", iShw));
2468 PPGMPOOLPAGE pPage = &pPool->aPages[iShw];
2469
2470 /*
2471 * Then, clear the actual mappings to the page in the shadow PT.
2472 */
2473 switch (pPage->enmKind)
2474 {
2475 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2476 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2477 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2478 {
2479 const uint32_t u32 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2480 PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2481 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2482 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2483 {
2484 Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX32 cRefs=%#x\n", i, pPT->a[i], cRefs));
2485 pPT->a[i].u = 0;
2486 cRefs--;
2487 if (!cRefs)
2488 return;
2489 }
2490#ifdef LOG_ENABLED
2491 RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
2492 for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
2493 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2494 {
2495 RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
2496 pPT->a[i].u = 0;
2497 }
2498#endif
2499 AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
2500 break;
2501 }
2502
2503 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2504 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2505 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2506 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2507 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2508 {
2509 const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2510 PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2511 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2512 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
2513 {
2514 Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
2515 pPT->a[i].u = 0;
2516 cRefs--;
2517 if (!cRefs)
2518 return;
2519 }
2520#ifdef LOG_ENABLED
2521 RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
2522 for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
2523 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
2524 {
2525 RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
2526 pPT->a[i].u = 0;
2527 }
2528#endif
2529 AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d u64=%RX64\n", cRefs, pPage->iFirstPresent, pPage->cPresent, u64));
2530 break;
2531 }
2532
2533 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
2534 {
2535 const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2536 PEPTPT pPT = (PEPTPT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2537 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2538 if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
2539 {
2540 Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
2541 pPT->a[i].u = 0;
2542 cRefs--;
2543 if (!cRefs)
2544 return;
2545 }
2546#ifdef LOG_ENABLED
2547 RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
2548 for (unsigned i = 0; i < RT_ELEMENTS(pPT->a); i++)
2549 if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
2550 {
2551 RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
2552 pPT->a[i].u = 0;
2553 }
2554#endif
2555 AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
2556 break;
2557 }
2558
2559 default:
2560 AssertFatalMsgFailed(("enmKind=%d iShw=%d\n", pPage->enmKind, iShw));
2561 }
2562}
2563
2564
2565/**
2566 * Scans one shadow page table for mappings of a physical page.
2567 *
2568 * @param pVM The VM handle.
2569 * @param pPhysPage The guest page in question.
2570 * @param iShw The shadow page table.
2571 * @param cRefs The number of references made in that PT.
2572 */
2573void pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
2574{
2575 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
2576 LogFlow(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%R[pgmpage] iShw=%d cRefs=%d\n", pPhysPage, iShw, cRefs));
2577 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPT, f);
2578 pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, iShw, cRefs);
2579 PGM_PAGE_SET_TRACKING(pPhysPage, 0);
2580 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPT, f);
2581}
2582
2583
2584/**
2585 * Flushes a list of shadow page tables mapping the same physical page.
2586 *
2587 * @param pVM The VM handle.
2588 * @param pPhysPage The guest page in question.
2589 * @param iPhysExt The physical cross reference extent list to flush.
2590 */
2591void pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt)
2592{
2593 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2594 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTs, f);
2595 LogFlow(("pgmPoolTrackFlushGCPhysPTs: pPhysPage=%R[pgmpage] iPhysExt\n", pPhysPage, iPhysExt));
2596
2597 const uint16_t iPhysExtStart = iPhysExt;
2598 PPGMPOOLPHYSEXT pPhysExt;
2599 do
2600 {
2601 Assert(iPhysExt < pPool->cMaxPhysExts);
2602 pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
2603 for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
2604 if (pPhysExt->aidx[i] != NIL_PGMPOOL_IDX)
2605 {
2606 pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, pPhysExt->aidx[i], 1);
2607 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
2608 }
2609
2610 /* next */
2611 iPhysExt = pPhysExt->iNext;
2612 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
2613
2614 /* insert the list into the free list and clear the ram range entry. */
2615 pPhysExt->iNext = pPool->iPhysExtFreeHead;
2616 pPool->iPhysExtFreeHead = iPhysExtStart;
2617 PGM_PAGE_SET_TRACKING(pPhysPage, 0);
2618
2619 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTs, f);
2620}
2621
2622#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
2623
2624/**
2625 * Flushes all shadow page table mappings of the given guest page.
2626 *
2627 * This is typically called when the host page backing the guest one has been
2628 * replaced or when the page protection was changed due to an access handler.
2629 *
2630 * @returns VBox status code.
2631 * @retval VINF_SUCCESS if all references has been successfully cleared.
2632 * @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
2633 * a page pool cleaning.
2634 *
2635 * @param pVM The VM handle.
2636 * @param pPhysPage The guest page in question.
2637 * @param pfFlushTLBs This is set to @a true if the shadow TLBs should be
2638 * flushed, it is NOT touched if this isn't necessary.
2639 * The caller MUST initialized this to @a false.
2640 */
2641int pgmPoolTrackFlushGCPhys(PVM pVM, PPGMPAGE pPhysPage, bool *pfFlushTLBs)
2642{
2643 int rc = VINF_SUCCESS;
2644#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2645 const uint16_t u16 = PGM_PAGE_GET_TRACKING(pPhysPage);
2646 if (u16)
2647 {
2648# ifdef VBOX_WITH_NEW_PHYS_CODE
2649 /*
2650 * The zero page is currently screwing up the tracking and we'll
2651 * have to flush the whole shebang. Unless VBOX_WITH_NEW_LAZY_PAGE_ALLOC
2652 * is defined, zero pages won't normally be mapped. Some kind of solution
2653 * will be needed for this problem of course, but it will have to wait...
2654 */
2655 if (PGM_PAGE_IS_ZERO(pPhysPage))
2656 rc = VINF_PGM_GCPHYS_ALIASED;
2657 else
2658# endif
2659 {
2660# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2661 /* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow and
2662 pgmPoolTrackFlushGCPhysPTs will/may kill the pool otherwise. */
2663 PVMCPU pVCpu = VMMGetCpu(pVM);
2664 uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
2665# endif
2666
2667 if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
2668 pgmPoolTrackFlushGCPhysPT(pVM,
2669 pPhysPage,
2670 PGMPOOL_TD_GET_IDX(u16),
2671 PGMPOOL_TD_GET_CREFS(u16));
2672 else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
2673 pgmPoolTrackFlushGCPhysPTs(pVM, pPhysPage, PGMPOOL_TD_GET_IDX(u16));
2674 else
2675 rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
2676 *pfFlushTLBs = true;
2677
2678# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2679 PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
2680# endif
2681 }
2682 }
2683
2684#elif defined(PGMPOOL_WITH_CACHE)
2685# ifdef VBOX_WITH_NEW_PHYS_CODE
2686 if (PGM_PAGE_IS_ZERO(pPhysPage))
2687 rc = VINF_PGM_GCPHYS_ALIASED;
2688 else
2689# endif
2690 {
2691# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2692 /* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow kill the pool otherwise. */
2693 PVMCPU pVCpu = VMMGetCpu(pVM);
2694 uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
2695# endif
2696 rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
2697 if (rc == VINF_SUCCESS)
2698 *pfFlushTLBs = true;
2699 }
2700
2701# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2702 PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
2703# endif
2704
2705#else
2706 rc = VINF_PGM_GCPHYS_ALIASED;
2707#endif
2708
2709 return rc;
2710}
2711
2712
2713/**
2714 * Scans all shadow page tables for mappings of a physical page.
2715 *
2716 * This may be slow, but it's most likely more efficient than cleaning
2717 * out the entire page pool / cache.
2718 *
2719 * @returns VBox status code.
2720 * @retval VINF_SUCCESS if all references has been successfully cleared.
2721 * @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
2722 * a page pool cleaning.
2723 *
2724 * @param pVM The VM handle.
2725 * @param pPhysPage The guest page in question.
2726 */
2727int pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage)
2728{
2729 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2730 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTsSlow, s);
2731 LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d pPhysPage=%R[pgmpage]\n",
2732 pPool->cUsedPages, pPool->cPresent, pPhysPage));
2733
2734#if 1
2735 /*
2736 * There is a limit to what makes sense.
2737 */
2738 if (pPool->cPresent > 1024)
2739 {
2740 LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: giving up... (cPresent=%d)\n", pPool->cPresent));
2741 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
2742 return VINF_PGM_GCPHYS_ALIASED;
2743 }
2744#endif
2745
2746 /*
2747 * Iterate all the pages until we've encountered all that in use.
2748 * This is simple but not quite optimal solution.
2749 */
2750 const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2751 const uint32_t u32 = u64;
2752 unsigned cLeft = pPool->cUsedPages;
2753 unsigned iPage = pPool->cCurPages;
2754 while (--iPage >= PGMPOOL_IDX_FIRST)
2755 {
2756 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
2757 if (pPage->GCPhys != NIL_RTGCPHYS)
2758 {
2759 switch (pPage->enmKind)
2760 {
2761 /*
2762 * We only care about shadow page tables.
2763 */
2764 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2765 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2766 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2767 {
2768 unsigned cPresent = pPage->cPresent;
2769 PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2770 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2771 if (pPT->a[i].n.u1Present)
2772 {
2773 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2774 {
2775 //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX32\n", iPage, i, pPT->a[i]));
2776 pPT->a[i].u = 0;
2777 }
2778 if (!--cPresent)
2779 break;
2780 }
2781 break;
2782 }
2783
2784 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2785 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2786 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2787 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2788 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2789 {
2790 unsigned cPresent = pPage->cPresent;
2791 PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2792 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
2793 if (pPT->a[i].n.u1Present)
2794 {
2795 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
2796 {
2797 //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
2798 pPT->a[i].u = 0;
2799 }
2800 if (!--cPresent)
2801 break;
2802 }
2803 break;
2804 }
2805 }
2806 if (!--cLeft)
2807 break;
2808 }
2809 }
2810
2811 PGM_PAGE_SET_TRACKING(pPhysPage, 0);
2812 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
2813 return VINF_SUCCESS;
2814}
2815
2816
2817/**
2818 * Clears the user entry in a user table.
2819 *
2820 * This is used to remove all references to a page when flushing it.
2821 */
2822static void pgmPoolTrackClearPageUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PCPGMPOOLUSER pUser)
2823{
2824 Assert(pUser->iUser != NIL_PGMPOOL_IDX);
2825 Assert(pUser->iUser < pPool->cCurPages);
2826 uint32_t iUserTable = pUser->iUserTable;
2827
2828 /*
2829 * Map the user page.
2830 */
2831 PPGMPOOLPAGE pUserPage = &pPool->aPages[pUser->iUser];
2832 union
2833 {
2834 uint64_t *pau64;
2835 uint32_t *pau32;
2836 } u;
2837 u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pUserPage);
2838
2839 LogFlow(("pgmPoolTrackClearPageUser: clear %x in %s (%RGp) (flushing %s)\n", iUserTable, pgmPoolPoolKindToStr(pUserPage->enmKind), pUserPage->Core.Key, pgmPoolPoolKindToStr(pPage->enmKind)));
2840
2841 /* Safety precaution in case we change the paging for other modes too in the future. */
2842 Assert(!pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage));
2843
2844#ifdef VBOX_STRICT
2845 /*
2846 * Some sanity checks.
2847 */
2848 switch (pUserPage->enmKind)
2849 {
2850 case PGMPOOLKIND_32BIT_PD:
2851 case PGMPOOLKIND_32BIT_PD_PHYS:
2852 Assert(iUserTable < X86_PG_ENTRIES);
2853 break;
2854 case PGMPOOLKIND_PAE_PDPT:
2855 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
2856 case PGMPOOLKIND_PAE_PDPT_PHYS:
2857 Assert(iUserTable < 4);
2858 Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
2859 break;
2860 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
2861 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
2862 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
2863 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
2864 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2865 case PGMPOOLKIND_PAE_PD_PHYS:
2866 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2867 break;
2868 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2869 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2870 Assert(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING));
2871 break;
2872 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2873 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2874 Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
2875 break;
2876 case PGMPOOLKIND_64BIT_PML4:
2877 Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
2878 /* GCPhys >> PAGE_SHIFT is the index here */
2879 break;
2880 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2881 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2882 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2883 break;
2884
2885 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2886 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2887 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2888 break;
2889
2890 case PGMPOOLKIND_ROOT_NESTED:
2891 Assert(iUserTable < X86_PG_PAE_ENTRIES);
2892 break;
2893
2894 default:
2895 AssertMsgFailed(("enmKind=%d\n", pUserPage->enmKind));
2896 break;
2897 }
2898#endif /* VBOX_STRICT */
2899
2900 /*
2901 * Clear the entry in the user page.
2902 */
2903 switch (pUserPage->enmKind)
2904 {
2905 /* 32-bit entries */
2906 case PGMPOOLKIND_32BIT_PD:
2907 case PGMPOOLKIND_32BIT_PD_PHYS:
2908 u.pau32[iUserTable] = 0;
2909 break;
2910
2911 /* 64-bit entries */
2912 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
2913 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
2914 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
2915 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
2916 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2917#if defined(IN_RC)
2918 /* 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
2919 * non-present PDPT will continue to cause page faults.
2920 */
2921 ASMReloadCR3();
2922#endif
2923 /* no break */
2924 case PGMPOOLKIND_PAE_PD_PHYS:
2925 case PGMPOOLKIND_PAE_PDPT_PHYS:
2926 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2927 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2928 case PGMPOOLKIND_64BIT_PML4:
2929 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
2930 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
2931 case PGMPOOLKIND_PAE_PDPT:
2932 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
2933 case PGMPOOLKIND_ROOT_NESTED:
2934 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
2935 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
2936 u.pau64[iUserTable] = 0;
2937 break;
2938
2939 default:
2940 AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
2941 }
2942}
2943
2944
2945/**
2946 * Clears all users of a page.
2947 */
2948static void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2949{
2950 /*
2951 * Free all the user records.
2952 */
2953 LogFlow(("pgmPoolTrackClearPageUsers %RGp\n", pPage->GCPhys));
2954
2955 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
2956 uint16_t i = pPage->iUserHead;
2957 while (i != NIL_PGMPOOL_USER_INDEX)
2958 {
2959 /* Clear enter in user table. */
2960 pgmPoolTrackClearPageUser(pPool, pPage, &paUsers[i]);
2961
2962 /* Free it. */
2963 const uint16_t iNext = paUsers[i].iNext;
2964 paUsers[i].iUser = NIL_PGMPOOL_IDX;
2965 paUsers[i].iNext = pPool->iUserFreeHead;
2966 pPool->iUserFreeHead = i;
2967
2968 /* Next. */
2969 i = iNext;
2970 }
2971 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
2972}
2973
2974#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2975
2976/**
2977 * Allocates a new physical cross reference extent.
2978 *
2979 * @returns Pointer to the allocated extent on success. NULL if we're out of them.
2980 * @param pVM The VM handle.
2981 * @param piPhysExt Where to store the phys ext index.
2982 */
2983PPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt)
2984{
2985 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
2986 uint16_t iPhysExt = pPool->iPhysExtFreeHead;
2987 if (iPhysExt == NIL_PGMPOOL_PHYSEXT_INDEX)
2988 {
2989 STAM_COUNTER_INC(&pPool->StamTrackPhysExtAllocFailures);
2990 return NULL;
2991 }
2992 PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
2993 pPool->iPhysExtFreeHead = pPhysExt->iNext;
2994 pPhysExt->iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
2995 *piPhysExt = iPhysExt;
2996 return pPhysExt;
2997}
2998
2999
3000/**
3001 * Frees a physical cross reference extent.
3002 *
3003 * @param pVM The VM handle.
3004 * @param iPhysExt The extent to free.
3005 */
3006void pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt)
3007{
3008 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3009 Assert(iPhysExt < pPool->cMaxPhysExts);
3010 PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
3011 for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
3012 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
3013 pPhysExt->iNext = pPool->iPhysExtFreeHead;
3014 pPool->iPhysExtFreeHead = iPhysExt;
3015}
3016
3017
3018/**
3019 * Frees a physical cross reference extent.
3020 *
3021 * @param pVM The VM handle.
3022 * @param iPhysExt The extent to free.
3023 */
3024void pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt)
3025{
3026 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3027
3028 const uint16_t iPhysExtStart = iPhysExt;
3029 PPGMPOOLPHYSEXT pPhysExt;
3030 do
3031 {
3032 Assert(iPhysExt < pPool->cMaxPhysExts);
3033 pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
3034 for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
3035 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
3036
3037 /* next */
3038 iPhysExt = pPhysExt->iNext;
3039 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
3040
3041 pPhysExt->iNext = pPool->iPhysExtFreeHead;
3042 pPool->iPhysExtFreeHead = iPhysExtStart;
3043}
3044
3045
3046/**
3047 * Insert a reference into a list of physical cross reference extents.
3048 *
3049 * @returns The new tracking data for PGMPAGE.
3050 *
3051 * @param pVM The VM handle.
3052 * @param iPhysExt The physical extent index of the list head.
3053 * @param iShwPT The shadow page table index.
3054 *
3055 */
3056static uint16_t pgmPoolTrackPhysExtInsert(PVM pVM, uint16_t iPhysExt, uint16_t iShwPT)
3057{
3058 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
3059 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
3060
3061 /* special common case. */
3062 if (paPhysExts[iPhysExt].aidx[2] == NIL_PGMPOOL_IDX)
3063 {
3064 paPhysExts[iPhysExt].aidx[2] = iShwPT;
3065 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
3066 LogFlow(("pgmPoolTrackPhysExtAddref: %d:{,,%d}\n", iPhysExt, iShwPT));
3067 return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
3068 }
3069
3070 /* general treatment. */
3071 const uint16_t iPhysExtStart = iPhysExt;
3072 unsigned cMax = 15;
3073 for (;;)
3074 {
3075 Assert(iPhysExt < pPool->cMaxPhysExts);
3076 for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
3077 if (paPhysExts[iPhysExt].aidx[i] == NIL_PGMPOOL_IDX)
3078 {
3079 paPhysExts[iPhysExt].aidx[i] = iShwPT;
3080 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
3081 LogFlow(("pgmPoolTrackPhysExtAddref: %d:{%d} i=%d cMax=%d\n", iPhysExt, iShwPT, i, cMax));
3082 return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtStart);
3083 }
3084 if (!--cMax)
3085 {
3086 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
3087 pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
3088 LogFlow(("pgmPoolTrackPhysExtAddref: overflow (1) iShwPT=%d\n", iShwPT));
3089 return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
3090 }
3091 }
3092
3093 /* add another extent to the list. */
3094 PPGMPOOLPHYSEXT pNew = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
3095 if (!pNew)
3096 {
3097 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
3098 pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
3099 return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
3100 }
3101 pNew->iNext = iPhysExtStart;
3102 pNew->aidx[0] = iShwPT;
3103 LogFlow(("pgmPoolTrackPhysExtAddref: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
3104 return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
3105}
3106
3107
3108/**
3109 * Add a reference to guest physical page where extents are in use.
3110 *
3111 * @returns The new tracking data for PGMPAGE.
3112 *
3113 * @param pVM The VM handle.
3114 * @param u16 The ram range flags (top 16-bits).
3115 * @param iShwPT The shadow page table index.
3116 */
3117uint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT)
3118{
3119 if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
3120 {
3121 /*
3122 * Convert to extent list.
3123 */
3124 Assert(PGMPOOL_TD_GET_CREFS(u16) == 1);
3125 uint16_t iPhysExt;
3126 PPGMPOOLPHYSEXT pPhysExt = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
3127 if (pPhysExt)
3128 {
3129 LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, PGMPOOL_TD_GET_IDX(u16), iShwPT));
3130 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliased);
3131 pPhysExt->aidx[0] = PGMPOOL_TD_GET_IDX(u16);
3132 pPhysExt->aidx[1] = iShwPT;
3133 u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
3134 }
3135 else
3136 u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
3137 }
3138 else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
3139 {
3140 /*
3141 * Insert into the extent list.
3142 */
3143 u16 = pgmPoolTrackPhysExtInsert(pVM, PGMPOOL_TD_GET_IDX(u16), iShwPT);
3144 }
3145 else
3146 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedLots);
3147 return u16;
3148}
3149
3150
3151/**
3152 * Clear references to guest physical memory.
3153 *
3154 * @param pPool The pool.
3155 * @param pPage The page.
3156 * @param pPhysPage Pointer to the aPages entry in the ram range.
3157 */
3158void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMPAGE pPhysPage)
3159{
3160 const unsigned cRefs = PGM_PAGE_GET_TD_CREFS(pPhysPage);
3161 AssertFatalMsg(cRefs == PGMPOOL_TD_CREFS_PHYSEXT, ("cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
3162
3163 uint16_t iPhysExt = PGM_PAGE_GET_TD_IDX(pPhysPage);
3164 if (iPhysExt != PGMPOOL_TD_IDX_OVERFLOWED)
3165 {
3166 uint16_t iPhysExtPrev = NIL_PGMPOOL_PHYSEXT_INDEX;
3167 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
3168 do
3169 {
3170 Assert(iPhysExt < pPool->cMaxPhysExts);
3171
3172 /*
3173 * Look for the shadow page and check if it's all freed.
3174 */
3175 for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
3176 {
3177 if (paPhysExts[iPhysExt].aidx[i] == pPage->idx)
3178 {
3179 paPhysExts[iPhysExt].aidx[i] = NIL_PGMPOOL_IDX;
3180
3181 for (i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
3182 if (paPhysExts[iPhysExt].aidx[i] != NIL_PGMPOOL_IDX)
3183 {
3184 Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
3185 return;
3186 }
3187
3188 /* we can free the node. */
3189 PVM pVM = pPool->CTX_SUFF(pVM);
3190 const uint16_t iPhysExtNext = paPhysExts[iPhysExt].iNext;
3191 if ( iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX
3192 && iPhysExtNext == NIL_PGMPOOL_PHYSEXT_INDEX)
3193 {
3194 /* lonely node */
3195 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
3196 Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d lonely\n", pPhysPage, pPage->idx));
3197 PGM_PAGE_SET_TRACKING(pPhysPage, 0);
3198 }
3199 else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
3200 {
3201 /* head */
3202 Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d head\n", pPhysPage, pPage->idx));
3203 PGM_PAGE_SET_TRACKING(pPhysPage, PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtNext));
3204 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
3205 }
3206 else
3207 {
3208 /* in list */
3209 Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
3210 paPhysExts[iPhysExtPrev].iNext = iPhysExtNext;
3211 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
3212 }
3213 iPhysExt = iPhysExtNext;
3214 return;
3215 }
3216 }
3217
3218 /* next */
3219 iPhysExtPrev = iPhysExt;
3220 iPhysExt = paPhysExts[iPhysExt].iNext;
3221 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
3222
3223 AssertFatalMsgFailed(("not-found! cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
3224 }
3225 else /* nothing to do */
3226 Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage]\n", pPhysPage));
3227}
3228
3229
3230/**
3231 * Clear references to guest physical memory.
3232 *
3233 * This is the same as pgmPoolTracDerefGCPhys except that the guest physical address
3234 * is assumed to be correct, so the linear search can be skipped and we can assert
3235 * at an earlier point.
3236 *
3237 * @param pPool The pool.
3238 * @param pPage The page.
3239 * @param HCPhys The host physical address corresponding to the guest page.
3240 * @param GCPhys The guest physical address corresponding to HCPhys.
3241 */
3242static void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
3243{
3244 /*
3245 * Walk range list.
3246 */
3247 PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
3248 while (pRam)
3249 {
3250 RTGCPHYS off = GCPhys - pRam->GCPhys;
3251 if (off < pRam->cb)
3252 {
3253 /* does it match? */
3254 const unsigned iPage = off >> PAGE_SHIFT;
3255 Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
3256#ifdef LOG_ENABLED
3257RTHCPHYS HCPhysPage = PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]);
3258Log2(("pgmPoolTracDerefGCPhys %RHp vs %RHp\n", HCPhysPage, HCPhys));
3259#endif
3260 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
3261 {
3262 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
3263 return;
3264 }
3265 break;
3266 }
3267 pRam = pRam->CTX_SUFF(pNext);
3268 }
3269 AssertFatalMsgFailed(("HCPhys=%RHp GCPhys=%RGp\n", HCPhys, GCPhys));
3270}
3271
3272
3273/**
3274 * Clear references to guest physical memory.
3275 *
3276 * @param pPool The pool.
3277 * @param pPage The page.
3278 * @param HCPhys The host physical address corresponding to the guest page.
3279 * @param GCPhysHint The guest physical address which may corresponding to HCPhys.
3280 */
3281static void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
3282{
3283 Log4(("pgmPoolTracDerefGCPhysHint %RHp %RGp\n", HCPhys, GCPhysHint));
3284
3285 /*
3286 * Walk range list.
3287 */
3288 PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
3289 while (pRam)
3290 {
3291 RTGCPHYS off = GCPhysHint - pRam->GCPhys;
3292 if (off < pRam->cb)
3293 {
3294 /* does it match? */
3295 const unsigned iPage = off >> PAGE_SHIFT;
3296 Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
3297 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
3298 {
3299 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
3300 return;
3301 }
3302 break;
3303 }
3304 pRam = pRam->CTX_SUFF(pNext);
3305 }
3306
3307 /*
3308 * Damn, the hint didn't work. We'll have to do an expensive linear search.
3309 */
3310 STAM_COUNTER_INC(&pPool->StatTrackLinearRamSearches);
3311 pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRanges);
3312 while (pRam)
3313 {
3314 unsigned iPage = pRam->cb >> PAGE_SHIFT;
3315 while (iPage-- > 0)
3316 {
3317 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
3318 {
3319 Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%RHp GCPhysHint=%RGp GCPhysReal=%RGp\n",
3320 HCPhys, GCPhysHint, pRam->GCPhys + (iPage << PAGE_SHIFT)));
3321 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
3322 return;
3323 }
3324 }
3325 pRam = pRam->CTX_SUFF(pNext);
3326 }
3327
3328 AssertFatalMsgFailed(("HCPhys=%RHp GCPhysHint=%RGp\n", HCPhys, GCPhysHint));
3329}
3330
3331
3332/**
3333 * Clear references to guest physical memory in a 32-bit / 32-bit page table.
3334 *
3335 * @param pPool The pool.
3336 * @param pPage The page.
3337 * @param pShwPT The shadow page table (mapping of the page).
3338 * @param pGstPT The guest page table.
3339 */
3340DECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
3341{
3342 for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
3343 if (pShwPT->a[i].n.u1Present)
3344 {
3345 Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
3346 i, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
3347 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
3348 if (!--pPage->cPresent)
3349 break;
3350 }
3351}
3352
3353
3354/**
3355 * Clear references to guest physical memory in a PAE / 32-bit page table.
3356 *
3357 * @param pPool The pool.
3358 * @param pPage The page.
3359 * @param pShwPT The shadow page table (mapping of the page).
3360 * @param pGstPT The guest page table (just a half one).
3361 */
3362DECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
3363{
3364 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
3365 if (pShwPT->a[i].n.u1Present)
3366 {
3367 Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX64 hint=%RX32\n",
3368 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
3369 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
3370 }
3371}
3372
3373
3374/**
3375 * Clear references to guest physical memory in a PAE / PAE page table.
3376 *
3377 * @param pPool The pool.
3378 * @param pPage The page.
3379 * @param pShwPT The shadow page table (mapping of the page).
3380 * @param pGstPT The guest page table.
3381 */
3382DECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
3383{
3384 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++)
3385 if (pShwPT->a[i].n.u1Present)
3386 {
3387 Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX32 hint=%RX32\n",
3388 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
3389 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
3390 }
3391}
3392
3393
3394/**
3395 * Clear references to guest physical memory in a 32-bit / 4MB page table.
3396 *
3397 * @param pPool The pool.
3398 * @param pPage The page.
3399 * @param pShwPT The shadow page table (mapping of the page).
3400 */
3401DECLINLINE(void) pgmPoolTrackDerefPT32Bit4MB(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT)
3402{
3403 RTGCPHYS GCPhys = pPage->GCPhys;
3404 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
3405 if (pShwPT->a[i].n.u1Present)
3406 {
3407 Log4(("pgmPoolTrackDerefPT32Bit4MB: i=%d pte=%RX32 GCPhys=%RGp\n",
3408 i, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys));
3409 pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys);
3410 }
3411}
3412
3413
3414/**
3415 * Clear references to guest physical memory in a PAE / 2/4MB page table.
3416 *
3417 * @param pPool The pool.
3418 * @param pPage The page.
3419 * @param pShwPT The shadow page table (mapping of the page).
3420 */
3421DECLINLINE(void) pgmPoolTrackDerefPTPaeBig(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT)
3422{
3423 RTGCPHYS GCPhys = pPage->GCPhys;
3424 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
3425 if (pShwPT->a[i].n.u1Present)
3426 {
3427 Log4(("pgmPoolTrackDerefPTPaeBig: i=%d pte=%RX64 hint=%RGp\n",
3428 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys));
3429 pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys);
3430 }
3431}
3432
3433#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
3434
3435
3436/**
3437 * Clear references to shadowed pages in a 32 bits page directory.
3438 *
3439 * @param pPool The pool.
3440 * @param pPage The page.
3441 * @param pShwPD The shadow page directory (mapping of the page).
3442 */
3443DECLINLINE(void) pgmPoolTrackDerefPD(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PD pShwPD)
3444{
3445 for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
3446 {
3447 if ( pShwPD->a[i].n.u1Present
3448 && !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
3449 )
3450 {
3451 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PG_MASK);
3452 if (pSubPage)
3453 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3454 else
3455 AssertFatalMsgFailed(("%x\n", pShwPD->a[i].u & X86_PDE_PG_MASK));
3456 }
3457 }
3458}
3459
3460/**
3461 * Clear references to shadowed pages in a PAE (legacy or 64 bits) page directory.
3462 *
3463 * @param pPool The pool.
3464 * @param pPage The page.
3465 * @param pShwPD The shadow page directory (mapping of the page).
3466 */
3467DECLINLINE(void) pgmPoolTrackDerefPDPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPAE pShwPD)
3468{
3469 for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
3470 {
3471 if ( pShwPD->a[i].n.u1Present
3472 && !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
3473 )
3474 {
3475 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
3476 if (pSubPage)
3477 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3478 else
3479 AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & X86_PDE_PAE_PG_MASK));
3480 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3481 }
3482 }
3483}
3484
3485/**
3486 * Clear references to shadowed pages in a PAE page directory pointer table.
3487 *
3488 * @param pPool The pool.
3489 * @param pPage The page.
3490 * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
3491 */
3492DECLINLINE(void) pgmPoolTrackDerefPDPTPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
3493{
3494 for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
3495 {
3496 if ( pShwPDPT->a[i].n.u1Present
3497 && !(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING)
3498 )
3499 {
3500 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
3501 if (pSubPage)
3502 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3503 else
3504 AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
3505 }
3506 }
3507}
3508
3509
3510/**
3511 * Clear references to shadowed pages in a 64-bit page directory pointer table.
3512 *
3513 * @param pPool The pool.
3514 * @param pPage The page.
3515 * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
3516 */
3517DECLINLINE(void) pgmPoolTrackDerefPDPT64Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
3518{
3519 for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
3520 {
3521 Assert(!(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING));
3522 if (pShwPDPT->a[i].n.u1Present)
3523 {
3524 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
3525 if (pSubPage)
3526 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3527 else
3528 AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
3529 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3530 }
3531 }
3532}
3533
3534
3535/**
3536 * Clear references to shadowed pages in a 64-bit level 4 page table.
3537 *
3538 * @param pPool The pool.
3539 * @param pPage The page.
3540 * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
3541 */
3542DECLINLINE(void) pgmPoolTrackDerefPML464Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PML4 pShwPML4)
3543{
3544 for (unsigned i = 0; i < RT_ELEMENTS(pShwPML4->a); i++)
3545 {
3546 if (pShwPML4->a[i].n.u1Present)
3547 {
3548 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
3549 if (pSubPage)
3550 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3551 else
3552 AssertFatalMsgFailed(("%RX64\n", pShwPML4->a[i].u & X86_PML4E_PG_MASK));
3553 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3554 }
3555 }
3556}
3557
3558
3559/**
3560 * Clear references to shadowed pages in an EPT page table.
3561 *
3562 * @param pPool The pool.
3563 * @param pPage The page.
3564 * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
3565 */
3566DECLINLINE(void) pgmPoolTrackDerefPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPT pShwPT)
3567{
3568 RTGCPHYS GCPhys = pPage->GCPhys;
3569 for (unsigned i = 0; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
3570 if (pShwPT->a[i].n.u1Present)
3571 {
3572 Log4(("pgmPoolTrackDerefPTEPT: i=%d pte=%RX64 GCPhys=%RX64\n",
3573 i, pShwPT->a[i].u & EPT_PTE_PG_MASK, pPage->GCPhys));
3574 pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & EPT_PTE_PG_MASK, GCPhys);
3575 }
3576}
3577
3578
3579/**
3580 * Clear references to shadowed pages in an EPT page directory.
3581 *
3582 * @param pPool The pool.
3583 * @param pPage The page.
3584 * @param pShwPD The shadow page directory (mapping of the page).
3585 */
3586DECLINLINE(void) pgmPoolTrackDerefPDEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPD pShwPD)
3587{
3588 for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
3589 {
3590 if (pShwPD->a[i].n.u1Present)
3591 {
3592 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & EPT_PDE_PG_MASK);
3593 if (pSubPage)
3594 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3595 else
3596 AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & EPT_PDE_PG_MASK));
3597 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3598 }
3599 }
3600}
3601
3602
3603/**
3604 * Clear references to shadowed pages in an EPT page directory pointer table.
3605 *
3606 * @param pPool The pool.
3607 * @param pPage The page.
3608 * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
3609 */
3610DECLINLINE(void) pgmPoolTrackDerefPDPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPDPT pShwPDPT)
3611{
3612 for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
3613 {
3614 if (pShwPDPT->a[i].n.u1Present)
3615 {
3616 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK);
3617 if (pSubPage)
3618 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3619 else
3620 AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK));
3621 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3622 }
3623 }
3624}
3625
3626
3627/**
3628 * Clears all references made by this page.
3629 *
3630 * This includes other shadow pages and GC physical addresses.
3631 *
3632 * @param pPool The pool.
3633 * @param pPage The page.
3634 */
3635static void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
3636{
3637 /*
3638 * Map the shadow page and take action according to the page kind.
3639 */
3640 void *pvShw = PGMPOOL_PAGE_2_LOCKED_PTR(pPool->CTX_SUFF(pVM), pPage);
3641 switch (pPage->enmKind)
3642 {
3643#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
3644 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3645 {
3646 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3647 void *pvGst;
3648 int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
3649 pgmPoolTrackDerefPT32Bit32Bit(pPool, pPage, (PX86PT)pvShw, (PCX86PT)pvGst);
3650 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3651 break;
3652 }
3653
3654 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3655 {
3656 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3657 void *pvGst;
3658 int rc = PGM_GCPHYS_2_PTR_EX(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
3659 pgmPoolTrackDerefPTPae32Bit(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PT)pvGst);
3660 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3661 break;
3662 }
3663
3664 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
3665 {
3666 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3667 void *pvGst;
3668 int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
3669 pgmPoolTrackDerefPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
3670 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3671 break;
3672 }
3673
3674 case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
3675 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3676 {
3677 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3678 pgmPoolTrackDerefPT32Bit4MB(pPool, pPage, (PX86PT)pvShw);
3679 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3680 break;
3681 }
3682
3683 case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 2 MB page */
3684 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
3685 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
3686 {
3687 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3688 pgmPoolTrackDerefPTPaeBig(pPool, pPage, (PX86PTPAE)pvShw);
3689 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3690 break;
3691 }
3692
3693#else /* !PGMPOOL_WITH_GCPHYS_TRACKING */
3694 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3695 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3696 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
3697 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3698 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
3699 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
3700 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
3701 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
3702 break;
3703#endif /* !PGMPOOL_WITH_GCPHYS_TRACKING */
3704
3705 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
3706 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
3707 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
3708 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
3709 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
3710 case PGMPOOLKIND_PAE_PD_PHYS:
3711 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
3712 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
3713 pgmPoolTrackDerefPDPae(pPool, pPage, (PX86PDPAE)pvShw);
3714 break;
3715
3716 case PGMPOOLKIND_32BIT_PD_PHYS:
3717 case PGMPOOLKIND_32BIT_PD:
3718 pgmPoolTrackDerefPD(pPool, pPage, (PX86PD)pvShw);
3719 break;
3720
3721 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
3722 case PGMPOOLKIND_PAE_PDPT:
3723 case PGMPOOLKIND_PAE_PDPT_PHYS:
3724 pgmPoolTrackDerefPDPTPae(pPool, pPage, (PX86PDPT)pvShw);
3725 break;
3726
3727 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
3728 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
3729 pgmPoolTrackDerefPDPT64Bit(pPool, pPage, (PX86PDPT)pvShw);
3730 break;
3731
3732 case PGMPOOLKIND_64BIT_PML4:
3733 pgmPoolTrackDerefPML464Bit(pPool, pPage, (PX86PML4)pvShw);
3734 break;
3735
3736 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
3737 pgmPoolTrackDerefPTEPT(pPool, pPage, (PEPTPT)pvShw);
3738 break;
3739
3740 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
3741 pgmPoolTrackDerefPDEPT(pPool, pPage, (PEPTPD)pvShw);
3742 break;
3743
3744 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
3745 pgmPoolTrackDerefPDPTEPT(pPool, pPage, (PEPTPDPT)pvShw);
3746 break;
3747
3748 default:
3749 AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
3750 }
3751
3752 /* paranoia, clear the shadow page. Remove this laser (i.e. let Alloc and ClearAll do it). */
3753 STAM_PROFILE_START(&pPool->StatZeroPage, z);
3754 ASMMemZeroPage(pvShw);
3755 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
3756 pPage->fZeroed = true;
3757 PGMPOOL_UNLOCK_PTR(pPool->CTX_SUFF(pVM), pvShw);
3758}
3759
3760#endif /* PGMPOOL_WITH_USER_TRACKING */
3761#ifdef IN_RING3
3762
3763/**
3764 * Flushes all the special root pages as part of a pgmPoolFlushAllInt operation.
3765 *
3766 * @param pPool The pool.
3767 */
3768static void pgmPoolFlushAllSpecialRoots(PPGMPOOL pPool)
3769{
3770 /*
3771 * These special pages are all mapped into the indexes 1..PGMPOOL_IDX_FIRST.
3772 */
3773 Assert(NIL_PGMPOOL_IDX == 0);
3774
3775 /*
3776 * Paranoia (to be removed), flag a global CR3 sync.
3777 */
3778 VM_FF_SET(pPool->CTX_SUFF(pVM), VM_FF_PGM_SYNC_CR3);
3779}
3780
3781
3782/**
3783 * Flushes the entire cache.
3784 *
3785 * It will assert a global CR3 flush (FF) and assumes the caller is aware of this
3786 * and execute this CR3 flush.
3787 *
3788 * @param pPool The pool.
3789 */
3790static void pgmPoolFlushAllInt(PPGMPOOL pPool)
3791{
3792 PVM pVM = pPool->CTX_SUFF(pVM);
3793
3794 STAM_PROFILE_START(&pPool->StatFlushAllInt, a);
3795 LogFlow(("pgmPoolFlushAllInt:\n"));
3796
3797 /*
3798 * If there are no pages in the pool, there is nothing to do.
3799 */
3800 if (pPool->cCurPages <= PGMPOOL_IDX_FIRST)
3801 {
3802 STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
3803 return;
3804 }
3805
3806 /* Unmap the old CR3 value before flushing everything. */
3807 int rc = PGM_BTH_PFN(UnmapCR3, pVM)(pVM);
3808 AssertRC(rc);
3809
3810 /* Exit the current shadow paging mode as well; nested paging and EPT use a root CR3 which will get flushed here. */
3811 rc = PGM_SHW_PFN(Exit, pVM)(pVM);
3812 AssertRC(rc);
3813
3814 /*
3815 * Nuke the free list and reinsert all pages into it.
3816 */
3817 for (unsigned i = pPool->cCurPages - 1; i >= PGMPOOL_IDX_FIRST; i--)
3818 {
3819 PPGMPOOLPAGE pPage = &pPool->aPages[i];
3820
3821#ifdef IN_RING3
3822 Assert(pPage->Core.Key == MMPage2Phys(pVM, pPage->pvPageR3));
3823#endif
3824#ifdef PGMPOOL_WITH_MONITORING
3825 if (pPage->fMonitored)
3826 pgmPoolMonitorFlush(pPool, pPage);
3827 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
3828 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
3829 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
3830 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
3831 pPage->cModifications = 0;
3832#endif
3833 pPage->GCPhys = NIL_RTGCPHYS;
3834 pPage->enmKind = PGMPOOLKIND_FREE;
3835 Assert(pPage->idx == i);
3836 pPage->iNext = i + 1;
3837 pPage->fZeroed = false; /* This could probably be optimized, but better safe than sorry. */
3838 pPage->fSeenNonGlobal = false;
3839 pPage->fMonitored= false;
3840 pPage->fCached = false;
3841 pPage->fReusedFlushPending = false;
3842#ifdef PGMPOOL_WITH_USER_TRACKING
3843 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
3844#else
3845 pPage->fCR3Mix = false;
3846#endif
3847#ifdef PGMPOOL_WITH_CACHE
3848 pPage->iAgeNext = NIL_PGMPOOL_IDX;
3849 pPage->iAgePrev = NIL_PGMPOOL_IDX;
3850#endif
3851 pPage->fLocked = false;
3852 }
3853 pPool->aPages[pPool->cCurPages - 1].iNext = NIL_PGMPOOL_IDX;
3854 pPool->iFreeHead = PGMPOOL_IDX_FIRST;
3855 pPool->cUsedPages = 0;
3856
3857#ifdef PGMPOOL_WITH_USER_TRACKING
3858 /*
3859 * Zap and reinitialize the user records.
3860 */
3861 pPool->cPresent = 0;
3862 pPool->iUserFreeHead = 0;
3863 PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
3864 const unsigned cMaxUsers = pPool->cMaxUsers;
3865 for (unsigned i = 0; i < cMaxUsers; i++)
3866 {
3867 paUsers[i].iNext = i + 1;
3868 paUsers[i].iUser = NIL_PGMPOOL_IDX;
3869 paUsers[i].iUserTable = 0xfffffffe;
3870 }
3871 paUsers[cMaxUsers - 1].iNext = NIL_PGMPOOL_USER_INDEX;
3872#endif
3873
3874#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
3875 /*
3876 * Clear all the GCPhys links and rebuild the phys ext free list.
3877 */
3878 for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRanges);
3879 pRam;
3880 pRam = pRam->CTX_SUFF(pNext))
3881 {
3882 unsigned iPage = pRam->cb >> PAGE_SHIFT;
3883 while (iPage-- > 0)
3884 PGM_PAGE_SET_TRACKING(&pRam->aPages[iPage], 0);
3885 }
3886
3887 pPool->iPhysExtFreeHead = 0;
3888 PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
3889 const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
3890 for (unsigned i = 0; i < cMaxPhysExts; i++)
3891 {
3892 paPhysExts[i].iNext = i + 1;
3893 paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
3894 paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
3895 paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
3896 }
3897 paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
3898#endif
3899
3900#ifdef PGMPOOL_WITH_MONITORING
3901 /*
3902 * Just zap the modified list.
3903 */
3904 pPool->cModifiedPages = 0;
3905 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
3906#endif
3907
3908#ifdef PGMPOOL_WITH_CACHE
3909 /*
3910 * Clear the GCPhys hash and the age list.
3911 */
3912 for (unsigned i = 0; i < RT_ELEMENTS(pPool->aiHash); i++)
3913 pPool->aiHash[i] = NIL_PGMPOOL_IDX;
3914 pPool->iAgeHead = NIL_PGMPOOL_IDX;
3915 pPool->iAgeTail = NIL_PGMPOOL_IDX;
3916#endif
3917
3918 /*
3919 * Flush all the special root pages.
3920 * Reinsert active pages into the hash and ensure monitoring chains are correct.
3921 */
3922 pgmPoolFlushAllSpecialRoots(pPool);
3923 for (unsigned i = PGMPOOL_IDX_FIRST_SPECIAL; i < PGMPOOL_IDX_FIRST; i++)
3924 {
3925 PPGMPOOLPAGE pPage = &pPool->aPages[i];
3926 pPage->iNext = NIL_PGMPOOL_IDX;
3927#ifdef PGMPOOL_WITH_MONITORING
3928 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
3929 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
3930 pPage->cModifications = 0;
3931 /* ASSUMES that we're not sharing with any of the other special pages (safe for now). */
3932 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
3933 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
3934 if (pPage->fMonitored)
3935 {
3936 int rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
3937 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
3938 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
3939 pPool->pfnAccessHandlerRC, MMHyperCCToRC(pVM, pPage),
3940 pPool->pszAccessHandler);
3941 AssertFatalRCSuccess(rc);
3942# ifdef PGMPOOL_WITH_CACHE
3943 pgmPoolHashInsert(pPool, pPage);
3944# endif
3945 }
3946#endif
3947#ifdef PGMPOOL_WITH_USER_TRACKING
3948 Assert(pPage->iUserHead == NIL_PGMPOOL_USER_INDEX); /* for now */
3949#endif
3950#ifdef PGMPOOL_WITH_CACHE
3951 Assert(pPage->iAgeNext == NIL_PGMPOOL_IDX);
3952 Assert(pPage->iAgePrev == NIL_PGMPOOL_IDX);
3953#endif
3954 }
3955
3956 /* Force a shadow mode reinit (necessary for nested paging and ept). */
3957 pVM->pgm.s.enmShadowMode = PGMMODE_INVALID;
3958
3959 /* Reinit the current shadow paging mode as well; nested paging and EPT use a root CR3 which will get flushed here. */
3960 rc = PGMR3ChangeMode(pVM, PGMGetGuestMode(pVM));
3961 AssertRC(rc);
3962
3963 /*
3964 * Finally, assert the FF.
3965 */
3966 VM_FF_SET(pVM, VM_FF_PGM_SYNC_CR3);
3967
3968 STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
3969}
3970
3971#endif /* IN_RING3 */
3972
3973/**
3974 * Flushes a pool page.
3975 *
3976 * This moves the page to the free list after removing all user references to it.
3977 * In GC this will cause a CR3 reload if the page is traced back to an active root page.
3978 *
3979 * @returns VBox status code.
3980 * @retval VINF_SUCCESS on success.
3981 * @param pPool The pool.
3982 * @param HCPhys The HC physical address of the shadow page.
3983 */
3984int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
3985{
3986 int rc = VINF_SUCCESS;
3987 STAM_PROFILE_START(&pPool->StatFlushPage, f);
3988 LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%RHp, .idx=%d, .enmKind=%s, .GCPhys=%RGp}\n",
3989 pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
3990
3991 /*
3992 * Quietly reject any attempts at flushing any of the special root pages.
3993 */
3994 if (pPage->idx < PGMPOOL_IDX_FIRST)
3995 {
3996 AssertFailed(); /* can no longer happen */
3997 Log(("pgmPoolFlushPage: special root page, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
3998 return VINF_SUCCESS;
3999 }
4000
4001 /*
4002 * Quietly reject any attempts at flushing the currently active shadow CR3 mapping
4003 */
4004 if (pgmPoolIsPageLocked(&pPool->CTX_SUFF(pVM)->pgm.s, pPage))
4005 {
4006 AssertMsg( pPage->enmKind == PGMPOOLKIND_64BIT_PML4
4007 || pPage->enmKind == PGMPOOLKIND_PAE_PDPT
4008 || pPage->enmKind == PGMPOOLKIND_PAE_PDPT_FOR_32BIT
4009 || pPage->enmKind == PGMPOOLKIND_32BIT_PD
4010 || pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD
4011 || pPage->enmKind == PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD
4012 || pPage->enmKind == PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD
4013 || pPage->enmKind == PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD
4014 || pPage->enmKind == PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD,
4015 ("Can't free the shadow CR3! (%RHp vs %RHp kind=%d\n", PGMGetHyperCR3(pPool->CTX_SUFF(pVM)), pPage->Core.Key, pPage->enmKind));
4016 Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
4017 return VINF_SUCCESS;
4018 }
4019
4020#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
4021 /* Start a subset so we won't run out of mapping space. */
4022 PVMCPU pVCpu = VMMGetCpu(pPool->CTX_SUFF(pVM));
4023 uint32_t iPrevSubset = PGMDynMapPushAutoSubset(pVCpu);
4024#endif
4025
4026 /*
4027 * Mark the page as being in need of a ASMMemZeroPage().
4028 */
4029 pPage->fZeroed = false;
4030
4031#ifdef PGMPOOL_WITH_USER_TRACKING
4032 /*
4033 * Clear the page.
4034 */
4035 pgmPoolTrackClearPageUsers(pPool, pPage);
4036 STAM_PROFILE_START(&pPool->StatTrackDeref,a);
4037 pgmPoolTrackDeref(pPool, pPage);
4038 STAM_PROFILE_STOP(&pPool->StatTrackDeref,a);
4039#endif
4040
4041#ifdef PGMPOOL_WITH_CACHE
4042 /*
4043 * Flush it from the cache.
4044 */
4045 pgmPoolCacheFlushPage(pPool, pPage);
4046#endif /* PGMPOOL_WITH_CACHE */
4047
4048#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
4049 /* Heavy stuff done. */
4050 PGMDynMapPopAutoSubset(pVCpu, iPrevSubset);
4051#endif
4052
4053#ifdef PGMPOOL_WITH_MONITORING
4054 /*
4055 * Deregistering the monitoring.
4056 */
4057 if (pPage->fMonitored)
4058 rc = pgmPoolMonitorFlush(pPool, pPage);
4059#endif
4060
4061 /*
4062 * Free the page.
4063 */
4064 Assert(pPage->iNext == NIL_PGMPOOL_IDX);
4065 pPage->iNext = pPool->iFreeHead;
4066 pPool->iFreeHead = pPage->idx;
4067 pPage->enmKind = PGMPOOLKIND_FREE;
4068 pPage->GCPhys = NIL_RTGCPHYS;
4069 pPage->fReusedFlushPending = false;
4070
4071 pPool->cUsedPages--;
4072 STAM_PROFILE_STOP(&pPool->StatFlushPage, f);
4073 return rc;
4074}
4075
4076
4077/**
4078 * Frees a usage of a pool page.
4079 *
4080 * The caller is responsible to updating the user table so that it no longer
4081 * references the shadow page.
4082 *
4083 * @param pPool The pool.
4084 * @param HCPhys The HC physical address of the shadow page.
4085 * @param iUser The shadow page pool index of the user table.
4086 * @param iUserTable The index into the user table (shadowed).
4087 */
4088void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
4089{
4090 STAM_PROFILE_START(&pPool->StatFree, a);
4091 LogFlow(("pgmPoolFreeByPage: pPage=%p:{.Key=%RHp, .idx=%d, enmKind=%s} iUser=%#x iUserTable=%#x\n",
4092 pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), iUser, iUserTable));
4093 Assert(pPage->idx >= PGMPOOL_IDX_FIRST);
4094#ifdef PGMPOOL_WITH_USER_TRACKING
4095 pgmPoolTrackFreeUser(pPool, pPage, iUser, iUserTable);
4096#endif
4097#ifdef PGMPOOL_WITH_CACHE
4098 if (!pPage->fCached)
4099#endif
4100 pgmPoolFlushPage(pPool, pPage);
4101 STAM_PROFILE_STOP(&pPool->StatFree, a);
4102}
4103
4104
4105/**
4106 * Makes one or more free page free.
4107 *
4108 * @returns VBox status code.
4109 * @retval VINF_SUCCESS on success.
4110 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
4111 *
4112 * @param pPool The pool.
4113 * @param enmKind Page table kind
4114 * @param iUser The user of the page.
4115 */
4116static int pgmPoolMakeMoreFreePages(PPGMPOOL pPool, PGMPOOLKIND enmKind, uint16_t iUser)
4117{
4118 LogFlow(("pgmPoolMakeMoreFreePages: iUser=%#x\n", iUser));
4119
4120 /*
4121 * If the pool isn't full grown yet, expand it.
4122 */
4123 if ( pPool->cCurPages < pPool->cMaxPages
4124#if defined(IN_RC)
4125 /* Hack alert: we can't deal with jumps to ring 3 when called from MapCR3 and allocating pages for PAE PDs. */
4126 && enmKind != PGMPOOLKIND_PAE_PD_FOR_PAE_PD
4127 && (enmKind < PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD || enmKind > PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD)
4128#endif
4129 )
4130 {
4131 STAM_PROFILE_ADV_SUSPEND(&pPool->StatAlloc, a);
4132#ifdef IN_RING3
4133 int rc = PGMR3PoolGrow(pPool->pVMR3);
4134#else
4135 int rc = CTXALLMID(VMM, CallHost)(pPool->CTX_SUFF(pVM), VMMCALLHOST_PGM_POOL_GROW, 0);
4136#endif
4137 if (RT_FAILURE(rc))
4138 return rc;
4139 STAM_PROFILE_ADV_RESUME(&pPool->StatAlloc, a);
4140 if (pPool->iFreeHead != NIL_PGMPOOL_IDX)
4141 return VINF_SUCCESS;
4142 }
4143
4144#ifdef PGMPOOL_WITH_CACHE
4145 /*
4146 * Free one cached page.
4147 */
4148 return pgmPoolCacheFreeOne(pPool, iUser);
4149#else
4150 /*
4151 * Flush the pool.
4152 *
4153 * If we have tracking enabled, it should be possible to come up with
4154 * a cheap replacement strategy...
4155 */
4156 /* @todo This path no longer works (CR3 root pages will be flushed)!! */
4157 AssertCompileFailed();
4158 Assert(!CPUMIsGuestInLongMode(pVM));
4159 pgmPoolFlushAllInt(pPool);
4160 return VERR_PGM_POOL_FLUSHED;
4161#endif
4162}
4163
4164
4165/**
4166 * Allocates a page from the pool.
4167 *
4168 * This page may actually be a cached page and not in need of any processing
4169 * on the callers part.
4170 *
4171 * @returns VBox status code.
4172 * @retval VINF_SUCCESS if a NEW page was allocated.
4173 * @retval VINF_PGM_CACHED_PAGE if a CACHED page was returned.
4174 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
4175 * @param pVM The VM handle.
4176 * @param GCPhys The GC physical address of the page we're gonna shadow.
4177 * For 4MB and 2MB PD entries, it's the first address the
4178 * shadow PT is covering.
4179 * @param enmKind The kind of mapping.
4180 * @param iUser The shadow page pool index of the user table.
4181 * @param iUserTable The index into the user table (shadowed).
4182 * @param ppPage Where to store the pointer to the page. NULL is stored here on failure.
4183 */
4184int pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
4185{
4186 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
4187 STAM_PROFILE_ADV_START(&pPool->StatAlloc, a);
4188 LogFlow(("pgmPoolAlloc: GCPhys=%RGp enmKind=%s iUser=%#x iUserTable=%#x\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable));
4189 *ppPage = NULL;
4190 /** @todo CSAM/PGMPrefetchPage messes up here during CSAMR3CheckGates
4191 * (TRPMR3SyncIDT) because of FF priority. Try fix that?
4192 * Assert(!(pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)); */
4193
4194#ifdef PGMPOOL_WITH_CACHE
4195 if (pPool->fCacheEnabled)
4196 {
4197 int rc2 = pgmPoolCacheAlloc(pPool, GCPhys, enmKind, iUser, iUserTable, ppPage);
4198 if (RT_SUCCESS(rc2))
4199 {
4200 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4201 LogFlow(("pgmPoolAlloc: cached returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
4202 return rc2;
4203 }
4204 }
4205#endif
4206
4207 /*
4208 * Allocate a new one.
4209 */
4210 int rc = VINF_SUCCESS;
4211 uint16_t iNew = pPool->iFreeHead;
4212 if (iNew == NIL_PGMPOOL_IDX)
4213 {
4214 rc = pgmPoolMakeMoreFreePages(pPool, enmKind, iUser);
4215 if (RT_FAILURE(rc))
4216 {
4217 Log(("pgmPoolAlloc: returns %Rrc (Free)\n", rc));
4218 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4219 return rc;
4220 }
4221 iNew = pPool->iFreeHead;
4222 AssertReleaseReturn(iNew != NIL_PGMPOOL_IDX, VERR_INTERNAL_ERROR);
4223 }
4224
4225 /* unlink the free head */
4226 PPGMPOOLPAGE pPage = &pPool->aPages[iNew];
4227 pPool->iFreeHead = pPage->iNext;
4228 pPage->iNext = NIL_PGMPOOL_IDX;
4229
4230 /*
4231 * Initialize it.
4232 */
4233 pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
4234 pPage->enmKind = enmKind;
4235 pPage->GCPhys = GCPhys;
4236 pPage->fSeenNonGlobal = false; /* Set this to 'true' to disable this feature. */
4237 pPage->fMonitored = false;
4238 pPage->fCached = false;
4239 pPage->fReusedFlushPending = false;
4240#ifdef PGMPOOL_WITH_MONITORING
4241 pPage->cModifications = 0;
4242 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
4243 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
4244#else
4245 pPage->fCR3Mix = false;
4246#endif
4247#ifdef PGMPOOL_WITH_USER_TRACKING
4248 pPage->cPresent = 0;
4249 pPage->iFirstPresent = ~0;
4250
4251 /*
4252 * Insert into the tracking and cache. If this fails, free the page.
4253 */
4254 int rc3 = pgmPoolTrackInsert(pPool, pPage, GCPhys, iUser, iUserTable);
4255 if (RT_FAILURE(rc3))
4256 {
4257 pPool->cUsedPages--;
4258 pPage->enmKind = PGMPOOLKIND_FREE;
4259 pPage->GCPhys = NIL_RTGCPHYS;
4260 pPage->iNext = pPool->iFreeHead;
4261 pPool->iFreeHead = pPage->idx;
4262 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4263 Log(("pgmPoolAlloc: returns %Rrc (Insert)\n", rc3));
4264 return rc3;
4265 }
4266#endif /* PGMPOOL_WITH_USER_TRACKING */
4267
4268 /*
4269 * Commit the allocation, clear the page and return.
4270 */
4271#ifdef VBOX_WITH_STATISTICS
4272 if (pPool->cUsedPages > pPool->cUsedPagesHigh)
4273 pPool->cUsedPagesHigh = pPool->cUsedPages;
4274#endif
4275
4276 if (!pPage->fZeroed)
4277 {
4278 STAM_PROFILE_START(&pPool->StatZeroPage, z);
4279 void *pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
4280 ASMMemZeroPage(pv);
4281 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
4282 }
4283
4284 *ppPage = pPage;
4285 LogFlow(("pgmPoolAlloc: returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
4286 rc, pPage, pPage->Core.Key, pPage->idx, pPage->fCached, pPage->fMonitored));
4287 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
4288 return rc;
4289}
4290
4291
4292/**
4293 * Frees a usage of a pool page.
4294 *
4295 * @param pVM The VM handle.
4296 * @param HCPhys The HC physical address of the shadow page.
4297 * @param iUser The shadow page pool index of the user table.
4298 * @param iUserTable The index into the user table (shadowed).
4299 */
4300void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable)
4301{
4302 LogFlow(("pgmPoolFree: HCPhys=%RHp iUser=%#x iUserTable=%#x\n", HCPhys, iUser, iUserTable));
4303 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
4304 pgmPoolFreeByPage(pPool, pgmPoolGetPage(pPool, HCPhys), iUser, iUserTable);
4305}
4306
4307
4308/**
4309 * Gets a in-use page in the pool by it's physical address.
4310 *
4311 * @returns Pointer to the page.
4312 * @param pVM The VM handle.
4313 * @param HCPhys The HC physical address of the shadow page.
4314 * @remark This function will NEVER return NULL. It will assert if HCPhys is invalid.
4315 */
4316PPGMPOOLPAGE pgmPoolGetPageByHCPhys(PVM pVM, RTHCPHYS HCPhys)
4317{
4318 /** @todo profile this! */
4319 PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
4320 PPGMPOOLPAGE pPage = pgmPoolGetPage(pPool, HCPhys);
4321 Log4(("pgmPoolGetPageByHCPhys: HCPhys=%RHp -> %p:{.idx=%d .GCPhys=%RGp .enmKind=%s}\n",
4322 HCPhys, pPage, pPage->idx, pPage->GCPhys, pgmPoolPoolKindToStr(pPage->enmKind)));
4323 return pPage;
4324}
4325
4326#ifdef IN_RING3
4327/**
4328 * Flushes the entire cache.
4329 *
4330 * It will assert a global CR3 flush (FF) and assumes the caller is aware of this
4331 * and execute this CR3 flush.
4332 *
4333 * @param pPool The pool.
4334 */
4335void pgmPoolFlushAll(PVM pVM)
4336{
4337 LogFlow(("pgmPoolFlushAll:\n"));
4338 pgmPoolFlushAllInt(pVM->pgm.s.CTX_SUFF(pPool));
4339}
4340#endif /* IN_RING3 */
4341
4342#ifdef LOG_ENABLED
4343static const char *pgmPoolPoolKindToStr(uint8_t enmKind)
4344{
4345 switch(enmKind)
4346 {
4347 case PGMPOOLKIND_INVALID:
4348 return "PGMPOOLKIND_INVALID";
4349 case PGMPOOLKIND_FREE:
4350 return "PGMPOOLKIND_FREE";
4351 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
4352 return "PGMPOOLKIND_32BIT_PT_FOR_PHYS";
4353 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
4354 return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT";
4355 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
4356 return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB";
4357 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
4358 return "PGMPOOLKIND_PAE_PT_FOR_PHYS";
4359 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
4360 return "PGMPOOLKIND_PAE_PT_FOR_32BIT_PT";
4361 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
4362 return "PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB";
4363 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
4364 return "PGMPOOLKIND_PAE_PT_FOR_PAE_PT";
4365 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
4366 return "PGMPOOLKIND_PAE_PT_FOR_PAE_2MB";
4367 case PGMPOOLKIND_32BIT_PD:
4368 return "PGMPOOLKIND_32BIT_PD";
4369 case PGMPOOLKIND_32BIT_PD_PHYS:
4370 return "PGMPOOLKIND_32BIT_PD_PHYS";
4371 case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
4372 return "PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD";
4373 case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
4374 return "PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD";
4375 case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
4376 return "PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD";
4377 case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
4378 return "PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD";
4379 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
4380 return "PGMPOOLKIND_PAE_PD_FOR_PAE_PD";
4381 case PGMPOOLKIND_PAE_PD_PHYS:
4382 return "PGMPOOLKIND_PAE_PD_PHYS";
4383 case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
4384 return "PGMPOOLKIND_PAE_PDPT_FOR_32BIT";
4385 case PGMPOOLKIND_PAE_PDPT:
4386 return "PGMPOOLKIND_PAE_PDPT";
4387 case PGMPOOLKIND_PAE_PDPT_PHYS:
4388 return "PGMPOOLKIND_PAE_PDPT_PHYS";
4389 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
4390 return "PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT";
4391 case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
4392 return "PGMPOOLKIND_64BIT_PDPT_FOR_PHYS";
4393 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
4394 return "PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD";
4395 case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
4396 return "PGMPOOLKIND_64BIT_PD_FOR_PHYS";
4397 case PGMPOOLKIND_64BIT_PML4:
4398 return "PGMPOOLKIND_64BIT_PML4";
4399 case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
4400 return "PGMPOOLKIND_EPT_PDPT_FOR_PHYS";
4401 case PGMPOOLKIND_EPT_PD_FOR_PHYS:
4402 return "PGMPOOLKIND_EPT_PD_FOR_PHYS";
4403 case PGMPOOLKIND_EPT_PT_FOR_PHYS:
4404 return "PGMPOOLKIND_EPT_PT_FOR_PHYS";
4405 case PGMPOOLKIND_ROOT_NESTED:
4406 return "PGMPOOLKIND_ROOT_NESTED";
4407 }
4408 return "Unknown kind!";
4409}
4410#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