VirtualBox

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

Last change on this file since 10027 was 10027, checked in by vboxsync, 17 years ago

Corrected check for flushing shadow pml4. Extended check for nested paging case.

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