VirtualBox

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

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

Correction for PML4E clearing.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 132.8 KB
Line 
1/* $Id: PGMAllPool.cpp 9993 2008-06-27 12:40:45Z 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 return true;
1057
1058 /*
1059 * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
1060 */
1061 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1062 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1063 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1064 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1065 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
1066 switch (enmKind2)
1067 {
1068 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1069 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1070 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1071 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1072 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
1073 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1074 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1075 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1076 return true;
1077 default:
1078 return false;
1079 }
1080
1081 /*
1082 * It's perfectly fine to reuse these, except for PAE and non-paging stuff.
1083 */
1084 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1085 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1086 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1087 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1088 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
1089 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1090 switch (enmKind2)
1091 {
1092 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1093 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1094 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1095 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1096 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
1097 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1098 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1099 return true;
1100 default:
1101 return false;
1102 }
1103
1104 /*
1105 * These cannot be flushed, and it's common to reuse the PDs as PTs.
1106 */
1107 case PGMPOOLKIND_ROOT_32BIT_PD:
1108 case PGMPOOLKIND_ROOT_PAE_PD:
1109 case PGMPOOLKIND_ROOT_PDPT:
1110 return false;
1111
1112 default:
1113 AssertFatalMsgFailed(("enmKind1=%d\n", enmKind1));
1114 }
1115}
1116
1117
1118/**
1119 * Attempts to satisfy a pgmPoolAlloc request from the cache.
1120 *
1121 * @returns VBox status code.
1122 * @retval VINF_PGM_CACHED_PAGE on success.
1123 * @retval VERR_FILE_NOT_FOUND if not found.
1124 * @param pPool The pool.
1125 * @param GCPhys The GC physical address of the page we're gonna shadow.
1126 * @param enmKind The kind of mapping.
1127 * @param iUser The shadow page pool index of the user table.
1128 * @param iUserTable The index into the user table (shadowed).
1129 * @param ppPage Where to store the pointer to the page.
1130 */
1131static int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
1132{
1133#ifndef IN_GC
1134 const PVM pVM = pPool->CTXSUFF(pVM);
1135#endif
1136 /*
1137 * Look up the GCPhys in the hash.
1138 */
1139 unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
1140 Log3(("pgmPoolCacheAlloc: %VGp kind %d iUser=%d iUserTable=%x SLOT=%d\n", GCPhys, enmKind, iUser, iUserTable, i));
1141 if (i != NIL_PGMPOOL_IDX)
1142 {
1143 do
1144 {
1145 PPGMPOOLPAGE pPage = &pPool->aPages[i];
1146 Log3(("pgmPoolCacheAlloc: slot %d found page %VGp\n", i, pPage->GCPhys));
1147 if (pPage->GCPhys == GCPhys)
1148 {
1149 if ((PGMPOOLKIND)pPage->enmKind == enmKind)
1150 {
1151 int rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
1152 if (VBOX_SUCCESS(rc))
1153 {
1154 *ppPage = pPage;
1155 STAM_COUNTER_INC(&pPool->StatCacheHits);
1156 return VINF_PGM_CACHED_PAGE;
1157 }
1158 return rc;
1159 }
1160
1161 /*
1162 * The kind is different. In some cases we should now flush the page
1163 * as it has been reused, but in most cases this is normal remapping
1164 * of PDs as PT or big pages using the GCPhys field in a slightly
1165 * different way than the other kinds.
1166 */
1167 if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
1168 {
1169 STAM_COUNTER_INC(&pPool->StatCacheKindMismatches);
1170 pgmPoolFlushPage(pPool, pPage); /* ASSUMES that VERR_PGM_POOL_CLEARED will be returned by pgmPoolTracInsert. */
1171 PGM_INVL_GUEST_TLBS(); /* see PT handler. */
1172 break;
1173 }
1174 }
1175
1176 /* next */
1177 i = pPage->iNext;
1178 } while (i != NIL_PGMPOOL_IDX);
1179 }
1180
1181 Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%d\n", GCPhys, enmKind));
1182 STAM_COUNTER_INC(&pPool->StatCacheMisses);
1183 return VERR_FILE_NOT_FOUND;
1184}
1185
1186
1187/**
1188 * Inserts a page into the cache.
1189 *
1190 * @param pPool The pool.
1191 * @param pPage The cached page.
1192 * @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
1193 */
1194static void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
1195{
1196 /*
1197 * Insert into the GCPhys hash if the page is fit for that.
1198 */
1199 Assert(!pPage->fCached);
1200 if (fCanBeCached)
1201 {
1202 pPage->fCached = true;
1203 pgmPoolHashInsert(pPool, pPage);
1204 Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%d, GCPhys=%RGp}\n",
1205 pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
1206 STAM_COUNTER_INC(&pPool->StatCacheCacheable);
1207 }
1208 else
1209 {
1210 Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%d, GCPhys=%RGp}\n",
1211 pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
1212 STAM_COUNTER_INC(&pPool->StatCacheUncacheable);
1213 }
1214
1215 /*
1216 * Insert at the head of the age list.
1217 */
1218 pPage->iAgePrev = NIL_PGMPOOL_IDX;
1219 pPage->iAgeNext = pPool->iAgeHead;
1220 if (pPool->iAgeHead != NIL_PGMPOOL_IDX)
1221 pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
1222 else
1223 pPool->iAgeTail = pPage->idx;
1224 pPool->iAgeHead = pPage->idx;
1225}
1226
1227
1228/**
1229 * Flushes a cached page.
1230 *
1231 * @param pPool The pool.
1232 * @param pPage The cached page.
1233 */
1234static void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1235{
1236 Log3(("pgmPoolCacheFlushPage: %VGp\n", pPage->GCPhys));
1237
1238 /*
1239 * Remove the page from the hash.
1240 */
1241 if (pPage->fCached)
1242 {
1243 pPage->fCached = false;
1244 pgmPoolHashRemove(pPool, pPage);
1245 }
1246 else
1247 Assert(pPage->iNext == NIL_PGMPOOL_IDX);
1248
1249 /*
1250 * Remove it from the age list.
1251 */
1252 if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
1253 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
1254 else
1255 pPool->iAgeTail = pPage->iAgePrev;
1256 if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
1257 pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
1258 else
1259 pPool->iAgeHead = pPage->iAgeNext;
1260 pPage->iAgeNext = NIL_PGMPOOL_IDX;
1261 pPage->iAgePrev = NIL_PGMPOOL_IDX;
1262}
1263#endif /* PGMPOOL_WITH_CACHE */
1264
1265
1266#ifdef PGMPOOL_WITH_MONITORING
1267/**
1268 * Looks for pages sharing the monitor.
1269 *
1270 * @returns Pointer to the head page.
1271 * @returns NULL if not found.
1272 * @param pPool The Pool
1273 * @param pNewPage The page which is going to be monitored.
1274 */
1275static PPGMPOOLPAGE pgmPoolMonitorGetPageByGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pNewPage)
1276{
1277#ifdef PGMPOOL_WITH_CACHE
1278 /*
1279 * Look up the GCPhys in the hash.
1280 */
1281 RTGCPHYS GCPhys = pNewPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
1282 unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
1283 if (i == NIL_PGMPOOL_IDX)
1284 return NULL;
1285 do
1286 {
1287 PPGMPOOLPAGE pPage = &pPool->aPages[i];
1288 if ( pPage->GCPhys - GCPhys < PAGE_SIZE
1289 && pPage != pNewPage)
1290 {
1291 switch (pPage->enmKind)
1292 {
1293 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1294 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1295 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1296 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
1297 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1298 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1299 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1300 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
1301 case PGMPOOLKIND_ROOT_32BIT_PD:
1302 case PGMPOOLKIND_ROOT_PAE_PD:
1303 case PGMPOOLKIND_ROOT_PDPT:
1304 {
1305 /* find the head */
1306 while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
1307 {
1308 Assert(pPage->iMonitoredPrev != pPage->idx);
1309 pPage = &pPool->aPages[pPage->iMonitoredPrev];
1310 }
1311 return pPage;
1312 }
1313
1314 /* ignore, no monitoring. */
1315 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1316 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1317 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1318 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1319 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1320 break;
1321 default:
1322 AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
1323 }
1324 }
1325
1326 /* next */
1327 i = pPage->iNext;
1328 } while (i != NIL_PGMPOOL_IDX);
1329#endif
1330 return NULL;
1331}
1332
1333/**
1334 * Enabled write monitoring of a guest page.
1335 *
1336 * @returns VBox status code.
1337 * @retval VINF_SUCCESS on success.
1338 * @retval VERR_PGM_POOL_CLEARED if the registration of the physical handler will cause a light weight pool flush.
1339 * @param pPool The pool.
1340 * @param pPage The cached page.
1341 */
1342static int pgmPoolMonitorInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1343{
1344 LogFlow(("pgmPoolMonitorInsert %VGp\n", pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1)));
1345
1346 /*
1347 * Filter out the relevant kinds.
1348 */
1349 switch (pPage->enmKind)
1350 {
1351 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1352 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1353 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1354 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1355 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1356 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1357 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
1358 case PGMPOOLKIND_ROOT_PDPT:
1359 break;
1360
1361 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1362 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1363 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1364 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1365 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1366 /* Nothing to monitor here. */
1367 return VINF_SUCCESS;
1368
1369 case PGMPOOLKIND_ROOT_32BIT_PD:
1370 case PGMPOOLKIND_ROOT_PAE_PD:
1371#ifdef PGMPOOL_WITH_MIXED_PT_CR3
1372 break;
1373#endif
1374 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
1375 default:
1376 AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
1377 }
1378
1379 /*
1380 * Install handler.
1381 */
1382 int rc;
1383 PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
1384 if (pPageHead)
1385 {
1386 Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
1387 Assert(pPageHead->iMonitoredPrev != pPage->idx);
1388 pPage->iMonitoredPrev = pPageHead->idx;
1389 pPage->iMonitoredNext = pPageHead->iMonitoredNext;
1390 if (pPageHead->iMonitoredNext != NIL_PGMPOOL_IDX)
1391 pPool->aPages[pPageHead->iMonitoredNext].iMonitoredPrev = pPage->idx;
1392 pPageHead->iMonitoredNext = pPage->idx;
1393 rc = VINF_SUCCESS;
1394 }
1395 else
1396 {
1397 Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX); Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
1398 PVM pVM = pPool->CTXSUFF(pVM);
1399 const RTGCPHYS GCPhysPage = pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1);
1400 rc = PGMHandlerPhysicalRegisterEx(pVM, PGMPHYSHANDLERTYPE_PHYSICAL_WRITE,
1401 GCPhysPage, GCPhysPage + (PAGE_SIZE - 1),
1402 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
1403 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
1404 pPool->pfnAccessHandlerGC, MMHyperCCToGC(pVM, pPage),
1405 pPool->pszAccessHandler);
1406 /** @todo we should probably deal with out-of-memory conditions here, but for now increasing
1407 * the heap size should suffice. */
1408 AssertFatalRC(rc);
1409 if (pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
1410 rc = VERR_PGM_POOL_CLEARED;
1411 }
1412 pPage->fMonitored = true;
1413 return rc;
1414}
1415
1416
1417/**
1418 * Disables write monitoring of a guest page.
1419 *
1420 * @returns VBox status code.
1421 * @retval VINF_SUCCESS on success.
1422 * @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
1423 * @param pPool The pool.
1424 * @param pPage The cached page.
1425 */
1426static int pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1427{
1428 /*
1429 * Filter out the relevant kinds.
1430 */
1431 switch (pPage->enmKind)
1432 {
1433 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1434 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1435 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
1436 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1437 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
1438 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
1439 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
1440 case PGMPOOLKIND_ROOT_PDPT:
1441 break;
1442
1443 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1444 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1445 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1446 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1447 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1448 /* Nothing to monitor here. */
1449 return VINF_SUCCESS;
1450
1451 case PGMPOOLKIND_ROOT_32BIT_PD:
1452 case PGMPOOLKIND_ROOT_PAE_PD:
1453#ifdef PGMPOOL_WITH_MIXED_PT_CR3
1454 break;
1455#endif
1456 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
1457 default:
1458 AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
1459 }
1460
1461 /*
1462 * Remove the page from the monitored list or uninstall it if last.
1463 */
1464 const PVM pVM = pPool->CTXSUFF(pVM);
1465 int rc;
1466 if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
1467 || pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
1468 {
1469 if (pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
1470 {
1471 PPGMPOOLPAGE pNewHead = &pPool->aPages[pPage->iMonitoredNext];
1472 pNewHead->iMonitoredPrev = NIL_PGMPOOL_IDX;
1473 pNewHead->fCR3Mix = pPage->fCR3Mix;
1474 rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
1475 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pNewHead),
1476 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pNewHead),
1477 pPool->pfnAccessHandlerGC, MMHyperCCToGC(pVM, pNewHead),
1478 pPool->pszAccessHandler);
1479 AssertFatalRCSuccess(rc);
1480 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
1481 }
1482 else
1483 {
1484 pPool->aPages[pPage->iMonitoredPrev].iMonitoredNext = pPage->iMonitoredNext;
1485 if (pPage->iMonitoredNext != NIL_PGMPOOL_IDX)
1486 {
1487 pPool->aPages[pPage->iMonitoredNext].iMonitoredPrev = pPage->iMonitoredPrev;
1488 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
1489 }
1490 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
1491 rc = VINF_SUCCESS;
1492 }
1493 }
1494 else
1495 {
1496 rc = PGMHandlerPhysicalDeregister(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1));
1497 AssertFatalRC(rc);
1498 if (pVM->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
1499 rc = VERR_PGM_POOL_CLEARED;
1500 }
1501 pPage->fMonitored = false;
1502
1503 /*
1504 * Remove it from the list of modified pages (if in it).
1505 */
1506 pgmPoolMonitorModifiedRemove(pPool, pPage);
1507
1508 return rc;
1509}
1510
1511
1512#ifdef PGMPOOL_WITH_MIXED_PT_CR3
1513/**
1514 * Set or clear the fCR3Mix attribute in a chain of monitored pages.
1515 *
1516 * @param pPool The Pool.
1517 * @param pPage A page in the chain.
1518 * @param fCR3Mix The new fCR3Mix value.
1519 */
1520static void pgmPoolMonitorChainChangeCR3Mix(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCR3Mix)
1521{
1522 /* current */
1523 pPage->fCR3Mix = fCR3Mix;
1524
1525 /* before */
1526 int16_t idx = pPage->iMonitoredPrev;
1527 while (idx != NIL_PGMPOOL_IDX)
1528 {
1529 pPool->aPages[idx].fCR3Mix = fCR3Mix;
1530 idx = pPool->aPages[idx].iMonitoredPrev;
1531 }
1532
1533 /* after */
1534 idx = pPage->iMonitoredNext;
1535 while (idx != NIL_PGMPOOL_IDX)
1536 {
1537 pPool->aPages[idx].fCR3Mix = fCR3Mix;
1538 idx = pPool->aPages[idx].iMonitoredNext;
1539 }
1540}
1541
1542
1543/**
1544 * Installs or modifies monitoring of a CR3 page (special).
1545 *
1546 * We're pretending the CR3 page is shadowed by the pool so we can use the
1547 * generic mechanisms in detecting chained monitoring. (This also gives us a
1548 * tast of what code changes are required to really pool CR3 shadow pages.)
1549 *
1550 * @returns VBox status code.
1551 * @param pPool The pool.
1552 * @param idxRoot The CR3 (root) page index.
1553 * @param GCPhysCR3 The (new) CR3 value.
1554 */
1555int pgmPoolMonitorMonitorCR3(PPGMPOOL pPool, uint16_t idxRoot, RTGCPHYS GCPhysCR3)
1556{
1557 Assert(idxRoot != NIL_PGMPOOL_IDX && idxRoot < PGMPOOL_IDX_FIRST);
1558 PPGMPOOLPAGE pPage = &pPool->aPages[idxRoot];
1559 LogFlow(("pgmPoolMonitorMonitorCR3: idxRoot=%d pPage=%p:{.GCPhys=%VGp, .fMonitored=%d} GCPhysCR3=%VGp\n",
1560 idxRoot, pPage, pPage->GCPhys, pPage->fMonitored, GCPhysCR3));
1561
1562 /*
1563 * The unlikely case where it already matches.
1564 */
1565 if (pPage->GCPhys == GCPhysCR3)
1566 {
1567 Assert(pPage->fMonitored);
1568 return VINF_SUCCESS;
1569 }
1570
1571 /*
1572 * Flush the current monitoring and remove it from the hash.
1573 */
1574 int rc = VINF_SUCCESS;
1575 if (pPage->fMonitored)
1576 {
1577 pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, false);
1578 rc = pgmPoolMonitorFlush(pPool, pPage);
1579 if (rc == VERR_PGM_POOL_CLEARED)
1580 rc = VINF_SUCCESS;
1581 else
1582 AssertFatalRC(rc);
1583 pgmPoolHashRemove(pPool, pPage);
1584 }
1585
1586 /*
1587 * Monitor the page at the new location and insert it into the hash.
1588 */
1589 pPage->GCPhys = GCPhysCR3;
1590 int rc2 = pgmPoolMonitorInsert(pPool, pPage);
1591 if (rc2 != VERR_PGM_POOL_CLEARED)
1592 {
1593 AssertFatalRC(rc2);
1594 if (rc2 != VINF_SUCCESS && rc == VINF_SUCCESS)
1595 rc = rc2;
1596 }
1597 pgmPoolHashInsert(pPool, pPage);
1598 pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, true);
1599 return rc;
1600}
1601
1602
1603/**
1604 * Removes the monitoring of a CR3 page (special).
1605 *
1606 * @returns VBox status code.
1607 * @param pPool The pool.
1608 * @param idxRoot The CR3 (root) page index.
1609 */
1610int pgmPoolMonitorUnmonitorCR3(PPGMPOOL pPool, uint16_t idxRoot)
1611{
1612 Assert(idxRoot != NIL_PGMPOOL_IDX && idxRoot < PGMPOOL_IDX_FIRST);
1613 PPGMPOOLPAGE pPage = &pPool->aPages[idxRoot];
1614 LogFlow(("pgmPoolMonitorUnmonitorCR3: idxRoot=%d pPage=%p:{.GCPhys=%VGp, .fMonitored=%d}\n",
1615 idxRoot, pPage, pPage->GCPhys, pPage->fMonitored));
1616
1617 if (!pPage->fMonitored)
1618 return VINF_SUCCESS;
1619
1620 pgmPoolMonitorChainChangeCR3Mix(pPool, pPage, false);
1621 int rc = pgmPoolMonitorFlush(pPool, pPage);
1622 if (rc != VERR_PGM_POOL_CLEARED)
1623 AssertFatalRC(rc);
1624 else
1625 rc = VINF_SUCCESS;
1626 pgmPoolHashRemove(pPool, pPage);
1627 Assert(!pPage->fMonitored);
1628 pPage->GCPhys = NIL_RTGCPHYS;
1629 return rc;
1630}
1631#endif /* PGMPOOL_WITH_MIXED_PT_CR3 */
1632
1633
1634/**
1635 * Inserts the page into the list of modified pages.
1636 *
1637 * @param pPool The pool.
1638 * @param pPage The page.
1639 */
1640void pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1641{
1642 Log3(("pgmPoolMonitorModifiedInsert: idx=%d\n", pPage->idx));
1643 AssertMsg( pPage->iModifiedNext == NIL_PGMPOOL_IDX
1644 && pPage->iModifiedPrev == NIL_PGMPOOL_IDX
1645 && pPool->iModifiedHead != pPage->idx,
1646 ("Next=%d Prev=%d idx=%d cModifications=%d Head=%d cModifiedPages=%d\n",
1647 pPage->iModifiedNext, pPage->iModifiedPrev, pPage->idx, pPage->cModifications,
1648 pPool->iModifiedHead, pPool->cModifiedPages));
1649
1650 pPage->iModifiedNext = pPool->iModifiedHead;
1651 if (pPool->iModifiedHead != NIL_PGMPOOL_IDX)
1652 pPool->aPages[pPool->iModifiedHead].iModifiedPrev = pPage->idx;
1653 pPool->iModifiedHead = pPage->idx;
1654 pPool->cModifiedPages++;
1655#ifdef VBOX_WITH_STATISTICS
1656 if (pPool->cModifiedPages > pPool->cModifiedPagesHigh)
1657 pPool->cModifiedPagesHigh = pPool->cModifiedPages;
1658#endif
1659}
1660
1661
1662/**
1663 * Removes the page from the list of modified pages and resets the
1664 * moficiation counter.
1665 *
1666 * @param pPool The pool.
1667 * @param pPage The page which is believed to be in the list of modified pages.
1668 */
1669static void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1670{
1671 Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
1672 if (pPool->iModifiedHead == pPage->idx)
1673 {
1674 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
1675 pPool->iModifiedHead = pPage->iModifiedNext;
1676 if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
1677 {
1678 pPool->aPages[pPage->iModifiedNext].iModifiedPrev = NIL_PGMPOOL_IDX;
1679 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1680 }
1681 pPool->cModifiedPages--;
1682 }
1683 else if (pPage->iModifiedPrev != NIL_PGMPOOL_IDX)
1684 {
1685 pPool->aPages[pPage->iModifiedPrev].iModifiedNext = pPage->iModifiedNext;
1686 if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
1687 {
1688 pPool->aPages[pPage->iModifiedNext].iModifiedPrev = pPage->iModifiedPrev;
1689 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1690 }
1691 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
1692 pPool->cModifiedPages--;
1693 }
1694 else
1695 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
1696 pPage->cModifications = 0;
1697}
1698
1699
1700/**
1701 * Zaps the list of modified pages, resetting their modification counters in the process.
1702 *
1703 * @param pVM The VM handle.
1704 */
1705void pgmPoolMonitorModifiedClearAll(PVM pVM)
1706{
1707 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
1708 LogFlow(("pgmPoolMonitorModifiedClearAll: cModifiedPages=%d\n", pPool->cModifiedPages));
1709
1710 unsigned cPages = 0; NOREF(cPages);
1711 uint16_t idx = pPool->iModifiedHead;
1712 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
1713 while (idx != NIL_PGMPOOL_IDX)
1714 {
1715 PPGMPOOLPAGE pPage = &pPool->aPages[idx];
1716 idx = pPage->iModifiedNext;
1717 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1718 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
1719 pPage->cModifications = 0;
1720 Assert(++cPages);
1721 }
1722 AssertMsg(cPages == pPool->cModifiedPages, ("%d != %d\n", cPages, pPool->cModifiedPages));
1723 pPool->cModifiedPages = 0;
1724}
1725
1726
1727/**
1728 * Clear all shadow pages and clear all modification counters.
1729 *
1730 * @param pVM The VM handle.
1731 * @remark Should only be used when monitoring is available, thus placed in
1732 * the PGMPOOL_WITH_MONITORING #ifdef.
1733 */
1734void pgmPoolClearAll(PVM pVM)
1735{
1736 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
1737 STAM_PROFILE_START(&pPool->StatClearAll, c);
1738 LogFlow(("pgmPoolClearAll: cUsedPages=%d\n", pPool->cUsedPages));
1739
1740 /*
1741 * Iterate all the pages until we've encountered all that in use.
1742 * This is simple but not quite optimal solution.
1743 */
1744 unsigned cModifiedPages = 0; NOREF(cModifiedPages);
1745 unsigned cLeft = pPool->cUsedPages;
1746 unsigned iPage = pPool->cCurPages;
1747 while (--iPage >= PGMPOOL_IDX_FIRST)
1748 {
1749 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
1750 if (pPage->GCPhys != NIL_RTGCPHYS)
1751 {
1752 switch (pPage->enmKind)
1753 {
1754 /*
1755 * We only care about shadow page tables.
1756 */
1757 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
1758 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
1759 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
1760 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
1761 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
1762 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
1763 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
1764 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
1765 {
1766#ifdef PGMPOOL_WITH_USER_TRACKING
1767 if (pPage->cPresent)
1768#endif
1769 {
1770 void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pPage);
1771 STAM_PROFILE_START(&pPool->StatZeroPage, z);
1772 ASMMemZeroPage(pvShw);
1773 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
1774#ifdef PGMPOOL_WITH_USER_TRACKING
1775 pPage->cPresent = 0;
1776 pPage->iFirstPresent = ~0;
1777#endif
1778 }
1779 }
1780 /* fall thru */
1781
1782 default:
1783 Assert(!pPage->cModifications || ++cModifiedPages);
1784 Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
1785 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
1786 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1787 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
1788 pPage->cModifications = 0;
1789 break;
1790
1791 }
1792 if (!--cLeft)
1793 break;
1794 }
1795 }
1796
1797 /* swipe the special pages too. */
1798 for (iPage = PGMPOOL_IDX_FIRST_SPECIAL; iPage < PGMPOOL_IDX_FIRST; iPage++)
1799 {
1800 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
1801 if (pPage->GCPhys != NIL_RTGCPHYS)
1802 {
1803 Assert(!pPage->cModifications || ++cModifiedPages);
1804 Assert(pPage->iModifiedNext == NIL_PGMPOOL_IDX || pPage->cModifications);
1805 Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX || pPage->cModifications);
1806 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
1807 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
1808 pPage->cModifications = 0;
1809 }
1810 }
1811
1812#ifndef DEBUG_michael
1813 AssertMsg(cModifiedPages == pPool->cModifiedPages, ("%d != %d\n", cModifiedPages, pPool->cModifiedPages));
1814#endif
1815 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
1816 pPool->cModifiedPages = 0;
1817
1818#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
1819 /*
1820 * Clear all the GCPhys links and rebuild the phys ext free list.
1821 */
1822 for (PPGMRAMRANGE pRam = pPool->CTXSUFF(pVM)->pgm.s.CTXALLSUFF(pRamRanges);
1823 pRam;
1824 pRam = CTXALLSUFF(pRam->pNext))
1825 {
1826 unsigned iPage = pRam->cb >> PAGE_SHIFT;
1827 while (iPage-- > 0)
1828 pRam->aPages[iPage].HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
1829 }
1830
1831 pPool->iPhysExtFreeHead = 0;
1832 PPGMPOOLPHYSEXT paPhysExts = pPool->CTXSUFF(paPhysExts);
1833 const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
1834 for (unsigned i = 0; i < cMaxPhysExts; i++)
1835 {
1836 paPhysExts[i].iNext = i + 1;
1837 paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
1838 paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
1839 paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
1840 }
1841 paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
1842#endif
1843
1844
1845 pPool->cPresent = 0;
1846 STAM_PROFILE_STOP(&pPool->StatClearAll, c);
1847}
1848#endif /* PGMPOOL_WITH_MONITORING */
1849
1850
1851#ifdef PGMPOOL_WITH_USER_TRACKING
1852/**
1853 * Frees up at least one user entry.
1854 *
1855 * @returns VBox status code.
1856 * @retval VINF_SUCCESS if successfully added.
1857 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
1858 * @param pPool The pool.
1859 * @param iUser The user index.
1860 */
1861static int pgmPoolTrackFreeOneUser(PPGMPOOL pPool, uint16_t iUser)
1862{
1863 STAM_COUNTER_INC(&pPool->StatTrackFreeUpOneUser);
1864#ifdef PGMPOOL_WITH_CACHE
1865 /*
1866 * Just free cached pages in a braindead fashion.
1867 */
1868 /** @todo walk the age list backwards and free the first with usage. */
1869 int rc = VINF_SUCCESS;
1870 do
1871 {
1872 int rc2 = pgmPoolCacheFreeOne(pPool, iUser);
1873 if (VBOX_FAILURE(rc2) && rc == VINF_SUCCESS)
1874 rc = rc2;
1875 } while (pPool->iUserFreeHead == NIL_PGMPOOL_USER_INDEX);
1876 return rc;
1877#else
1878 /*
1879 * Lazy approach.
1880 */
1881 pgmPoolFlushAllInt(pPool);
1882 return VERR_PGM_POOL_FLUSHED;
1883#endif
1884}
1885
1886
1887/**
1888 * Inserts a page into the cache.
1889 *
1890 * This will create user node for the page, insert it into the GCPhys
1891 * hash, and insert it into the age list.
1892 *
1893 * @returns VBox status code.
1894 * @retval VINF_SUCCESS if successfully added.
1895 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
1896 * @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
1897 * @param pPool The pool.
1898 * @param pPage The cached page.
1899 * @param GCPhys The GC physical address of the page we're gonna shadow.
1900 * @param iUser The user index.
1901 * @param iUserTable The user table index.
1902 */
1903DECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
1904{
1905 int rc = VINF_SUCCESS;
1906 PPGMPOOLUSER pUser = pPool->CTXSUFF(paUsers);
1907
1908 LogFlow(("pgmPoolTrackInsert iUser %d iUserTable %d\n", iUser, iUserTable));
1909
1910 /*
1911 * Find free a user node.
1912 */
1913 uint16_t i = pPool->iUserFreeHead;
1914 if (i == NIL_PGMPOOL_USER_INDEX)
1915 {
1916 int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
1917 if (VBOX_FAILURE(rc))
1918 return rc;
1919 i = pPool->iUserFreeHead;
1920 }
1921
1922 /*
1923 * Unlink the user node from the free list,
1924 * initialize and insert it into the user list.
1925 */
1926 pPool->iUserFreeHead = pUser[i].iNext;
1927 pUser[i].iNext = NIL_PGMPOOL_USER_INDEX;
1928 pUser[i].iUser = iUser;
1929 pUser[i].iUserTable = iUserTable;
1930 pPage->iUserHead = i;
1931
1932 /*
1933 * Insert into cache and enable monitoring of the guest page if enabled.
1934 *
1935 * Until we implement caching of all levels, including the CR3 one, we'll
1936 * have to make sure we don't try monitor & cache any recursive reuse of
1937 * a monitored CR3 page. Because all windows versions are doing this we'll
1938 * have to be able to do combined access monitoring, CR3 + PT and
1939 * PD + PT (guest PAE).
1940 *
1941 * Update:
1942 * We're now cooperating with the CR3 monitor if an uncachable page is found.
1943 */
1944#if defined(PGMPOOL_WITH_MONITORING) || defined(PGMPOOL_WITH_CACHE)
1945# ifdef PGMPOOL_WITH_MIXED_PT_CR3
1946 const bool fCanBeMonitored = true;
1947# else
1948 bool fCanBeMonitored = pPool->CTXSUFF(pVM)->pgm.s.GCPhysGstCR3Monitored == NIL_RTGCPHYS
1949 || (GCPhys & X86_PTE_PAE_PG_MASK) != (pPool->CTXSUFF(pVM)->pgm.s.GCPhysGstCR3Monitored & X86_PTE_PAE_PG_MASK)
1950 || pgmPoolIsBigPage((PGMPOOLKIND)pPage->enmKind);
1951# endif
1952# ifdef PGMPOOL_WITH_CACHE
1953 pgmPoolCacheInsert(pPool, pPage, fCanBeMonitored); /* This can be expanded. */
1954# endif
1955 if (fCanBeMonitored)
1956 {
1957# ifdef PGMPOOL_WITH_MONITORING
1958 rc = pgmPoolMonitorInsert(pPool, pPage);
1959 if (rc == VERR_PGM_POOL_CLEARED)
1960 {
1961 /* 'Failed' - free the usage, and keep it in the cache (if enabled). */
1962# ifndef PGMPOOL_WITH_CACHE
1963 pgmPoolMonitorFlush(pPool, pPage);
1964 rc = VERR_PGM_POOL_FLUSHED;
1965# endif
1966 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
1967 pUser[i].iNext = pPool->iUserFreeHead;
1968 pUser[i].iUser = NIL_PGMPOOL_IDX;
1969 pPool->iUserFreeHead = i;
1970 }
1971 }
1972# endif
1973#endif /* PGMPOOL_WITH_MONITORING */
1974 return rc;
1975}
1976
1977
1978# ifdef PGMPOOL_WITH_CACHE /* (only used when the cache is enabled.) */
1979/**
1980 * Adds a user reference to a page.
1981 *
1982 * This will
1983 * This will move the page to the head of the
1984 *
1985 * @returns VBox status code.
1986 * @retval VINF_SUCCESS if successfully added.
1987 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
1988 * @param pPool The pool.
1989 * @param pPage The cached page.
1990 * @param iUser The user index.
1991 * @param iUserTable The user table.
1992 */
1993static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
1994{
1995 PPGMPOOLUSER paUsers = pPool->CTXSUFF(paUsers);
1996
1997 LogFlow(("pgmPoolTrackAddUser iUser %d iUserTable %d\n", iUser, iUserTable));
1998# ifdef VBOX_STRICT
1999 /*
2000 * Check that the entry doesn't already exists.
2001 */
2002 if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
2003 {
2004 uint16_t i = pPage->iUserHead;
2005 do
2006 {
2007 Assert(i < pPool->cMaxUsers);
2008 AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
2009 i = paUsers[i].iNext;
2010 } while (i != NIL_PGMPOOL_USER_INDEX);
2011 }
2012# endif
2013
2014 /*
2015 * Allocate a user node.
2016 */
2017 uint16_t i = pPool->iUserFreeHead;
2018 if (i == NIL_PGMPOOL_USER_INDEX)
2019 {
2020 int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
2021 if (VBOX_FAILURE(rc))
2022 return rc;
2023 i = pPool->iUserFreeHead;
2024 }
2025 pPool->iUserFreeHead = paUsers[i].iNext;
2026
2027 /*
2028 * Initialize the user node and insert it.
2029 */
2030 paUsers[i].iNext = pPage->iUserHead;
2031 paUsers[i].iUser = iUser;
2032 paUsers[i].iUserTable = iUserTable;
2033 pPage->iUserHead = i;
2034
2035# ifdef PGMPOOL_WITH_CACHE
2036 /*
2037 * Tell the cache to update its replacement stats for this page.
2038 */
2039 pgmPoolCacheUsed(pPool, pPage);
2040# endif
2041 return VINF_SUCCESS;
2042}
2043# endif /* PGMPOOL_WITH_CACHE */
2044
2045
2046/**
2047 * Frees a user record associated with a page.
2048 *
2049 * This does not clear the entry in the user table, it simply replaces the
2050 * user record to the chain of free records.
2051 *
2052 * @param pPool The pool.
2053 * @param HCPhys The HC physical address of the shadow page.
2054 * @param iUser The shadow page pool index of the user table.
2055 * @param iUserTable The index into the user table (shadowed).
2056 */
2057static void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
2058{
2059 /*
2060 * Unlink and free the specified user entry.
2061 */
2062 PPGMPOOLUSER paUsers = pPool->CTXSUFF(paUsers);
2063
2064 /* Special: For PAE and 32-bit paging, there is usually no more than one user. */
2065 uint16_t i = pPage->iUserHead;
2066 if ( i != NIL_PGMPOOL_USER_INDEX
2067 && paUsers[i].iUser == iUser
2068 && paUsers[i].iUserTable == iUserTable)
2069 {
2070 pPage->iUserHead = paUsers[i].iNext;
2071
2072 paUsers[i].iUser = NIL_PGMPOOL_IDX;
2073 paUsers[i].iNext = pPool->iUserFreeHead;
2074 pPool->iUserFreeHead = i;
2075 return;
2076 }
2077
2078 /* General: Linear search. */
2079 uint16_t iPrev = NIL_PGMPOOL_USER_INDEX;
2080 while (i != NIL_PGMPOOL_USER_INDEX)
2081 {
2082 if ( paUsers[i].iUser == iUser
2083 && paUsers[i].iUserTable == iUserTable)
2084 {
2085 if (iPrev != NIL_PGMPOOL_USER_INDEX)
2086 paUsers[iPrev].iNext = paUsers[i].iNext;
2087 else
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 iPrev = i;
2096 i = paUsers[i].iNext;
2097 }
2098
2099 /* Fatal: didn't find it */
2100 AssertFatalMsgFailed(("Didn't find the user entry! iUser=%#x iUserTable=%#x GCPhys=%VGp\n",
2101 iUser, iUserTable, pPage->GCPhys));
2102}
2103
2104
2105/**
2106 * Gets the entry size of a shadow table.
2107 *
2108 * @param enmKind The kind of page.
2109 *
2110 * @returns The size of the entry in bytes. That is, 4 or 8.
2111 * @returns If the kind is not for a table, an assertion is raised and 0 is
2112 * returned.
2113 */
2114DECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind)
2115{
2116 switch (enmKind)
2117 {
2118 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2119 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2120 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2121 case PGMPOOLKIND_ROOT_32BIT_PD:
2122 return 4;
2123
2124 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2125 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2126 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2127 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2128 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2129 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
2130 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2131 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2132 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2133 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
2134 case PGMPOOLKIND_ROOT_PAE_PD:
2135 case PGMPOOLKIND_ROOT_PDPT:
2136 return 8;
2137
2138 default:
2139 AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
2140 }
2141}
2142
2143
2144/**
2145 * Gets the entry size of a guest table.
2146 *
2147 * @param enmKind The kind of page.
2148 *
2149 * @returns The size of the entry in bytes. That is, 0, 4 or 8.
2150 * @returns If the kind is not for a table, an assertion is raised and 0 is
2151 * returned.
2152 */
2153DECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind)
2154{
2155 switch (enmKind)
2156 {
2157 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2158 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2159 case PGMPOOLKIND_ROOT_32BIT_PD:
2160 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2161 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2162 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
2163 return 4;
2164
2165 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2166 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2167 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2168 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2169 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2170 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
2171 case PGMPOOLKIND_ROOT_PAE_PD:
2172 case PGMPOOLKIND_ROOT_PDPT:
2173 return 8;
2174
2175 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2176 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2177 /** @todo can we return 0? (nobody is calling this...) */
2178 return 0;
2179
2180 default:
2181 AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
2182 }
2183}
2184
2185
2186#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2187/**
2188 * Scans one shadow page table for mappings of a physical page.
2189 *
2190 * @param pVM The VM handle.
2191 * @param pPhysPage The guest page in question.
2192 * @param iShw The shadow page table.
2193 * @param cRefs The number of references made in that PT.
2194 */
2195static void pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
2196{
2197 LogFlow(("pgmPoolTrackFlushGCPhysPT: HCPhys=%RHp iShw=%d cRefs=%d\n", pPhysPage->HCPhys, iShw, cRefs));
2198 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
2199
2200 /*
2201 * Assert sanity.
2202 */
2203 Assert(cRefs == 1);
2204 AssertFatalMsg(iShw < pPool->cCurPages && iShw != NIL_PGMPOOL_IDX, ("iShw=%d\n", iShw));
2205 PPGMPOOLPAGE pPage = &pPool->aPages[iShw];
2206
2207 /*
2208 * Then, clear the actual mappings to the page in the shadow PT.
2209 */
2210 switch (pPage->enmKind)
2211 {
2212 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2213 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2214 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2215 {
2216 const uint32_t u32 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2217 PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2218 for (unsigned i = pPage->iFirstPresent; i < ELEMENTS(pPT->a); i++)
2219 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2220 {
2221 Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX32 cRefs=%#x\n", i, pPT->a[i], cRefs));
2222 pPT->a[i].u = 0;
2223 cRefs--;
2224 if (!cRefs)
2225 return;
2226 }
2227#if defined(DEBUG) && !defined(IN_RING0) ///@todo RTLogPrintf is missing in R0.
2228 RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
2229 for (unsigned i = 0; i < ELEMENTS(pPT->a); i++)
2230 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2231 {
2232 RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
2233 pPT->a[i].u = 0;
2234 }
2235#endif
2236 AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
2237 break;
2238 }
2239
2240 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2241 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2242 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2243 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2244 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2245 {
2246 const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2247 PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2248 for (unsigned i = pPage->iFirstPresent; i < ELEMENTS(pPT->a); i++)
2249 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
2250 {
2251 Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64 cRefs=%#x\n", i, pPT->a[i], cRefs));
2252 pPT->a[i].u = 0;
2253 cRefs--;
2254 if (!cRefs)
2255 return;
2256 }
2257#if defined(DEBUG) && !defined(IN_RING0) ///@todo RTLogPrintf is missing in R0.
2258 RTLogPrintf("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent);
2259 for (unsigned i = 0; i < ELEMENTS(pPT->a); i++)
2260 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
2261 {
2262 RTLogPrintf("i=%d cRefs=%d\n", i, cRefs--);
2263 pPT->a[i].u = 0;
2264 }
2265#endif
2266 AssertFatalMsgFailed(("cRefs=%d iFirstPresent=%d cPresent=%d\n", cRefs, pPage->iFirstPresent, pPage->cPresent));
2267 break;
2268 }
2269
2270 default:
2271 AssertFatalMsgFailed(("enmKind=%d iShw=%d\n", pPage->enmKind, iShw));
2272 }
2273}
2274
2275
2276/**
2277 * Scans one shadow page table for mappings of a physical page.
2278 *
2279 * @param pVM The VM handle.
2280 * @param pPhysPage The guest page in question.
2281 * @param iShw The shadow page table.
2282 * @param cRefs The number of references made in that PT.
2283 */
2284void pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, uint16_t iShw, uint16_t cRefs)
2285{
2286 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool); NOREF(pPool);
2287 LogFlow(("pgmPoolTrackFlushGCPhysPT: HCPhys=%RHp iShw=%d cRefs=%d\n", pPhysPage->HCPhys, iShw, cRefs));
2288 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPT, f);
2289 pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, iShw, cRefs);
2290 pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
2291 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPT, f);
2292}
2293
2294
2295/**
2296 * Flushes a list of shadow page tables mapping the same physical page.
2297 *
2298 * @param pVM The VM handle.
2299 * @param pPhysPage The guest page in question.
2300 * @param iPhysExt The physical cross reference extent list to flush.
2301 */
2302void pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, uint16_t iPhysExt)
2303{
2304 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
2305 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTs, f);
2306 LogFlow(("pgmPoolTrackFlushGCPhysPTs: HCPhys=%RHp iPhysExt\n", pPhysPage->HCPhys, iPhysExt));
2307
2308 const uint16_t iPhysExtStart = iPhysExt;
2309 PPGMPOOLPHYSEXT pPhysExt;
2310 do
2311 {
2312 Assert(iPhysExt < pPool->cMaxPhysExts);
2313 pPhysExt = &pPool->CTXSUFF(paPhysExts)[iPhysExt];
2314 for (unsigned i = 0; i < ELEMENTS(pPhysExt->aidx); i++)
2315 if (pPhysExt->aidx[i] != NIL_PGMPOOL_IDX)
2316 {
2317 pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, pPhysExt->aidx[i], 1);
2318 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
2319 }
2320
2321 /* next */
2322 iPhysExt = pPhysExt->iNext;
2323 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
2324
2325 /* insert the list into the free list and clear the ram range entry. */
2326 pPhysExt->iNext = pPool->iPhysExtFreeHead;
2327 pPool->iPhysExtFreeHead = iPhysExtStart;
2328 pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
2329
2330 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTs, f);
2331}
2332#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
2333
2334
2335/**
2336 * Scans all shadow page tables for mappings of a physical page.
2337 *
2338 * This may be slow, but it's most likely more efficient than cleaning
2339 * out the entire page pool / cache.
2340 *
2341 * @returns VBox status code.
2342 * @retval VINF_SUCCESS if all references has been successfully cleared.
2343 * @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
2344 * a page pool cleaning.
2345 *
2346 * @param pVM The VM handle.
2347 * @param pPhysPage The guest page in question.
2348 */
2349int pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PPGMPAGE pPhysPage)
2350{
2351 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
2352 STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTsSlow, s);
2353 LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d HCPhys=%RHp\n",
2354 pPool->cUsedPages, pPool->cPresent, pPhysPage->HCPhys));
2355
2356#if 1
2357 /*
2358 * There is a limit to what makes sense.
2359 */
2360 if (pPool->cPresent > 1024)
2361 {
2362 LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: giving up... (cPresent=%d)\n", pPool->cPresent));
2363 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
2364 return VINF_PGM_GCPHYS_ALIASED;
2365 }
2366#endif
2367
2368 /*
2369 * Iterate all the pages until we've encountered all that in use.
2370 * This is simple but not quite optimal solution.
2371 */
2372 const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
2373 const uint32_t u32 = u64;
2374 unsigned cLeft = pPool->cUsedPages;
2375 unsigned iPage = pPool->cCurPages;
2376 while (--iPage >= PGMPOOL_IDX_FIRST)
2377 {
2378 PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
2379 if (pPage->GCPhys != NIL_RTGCPHYS)
2380 {
2381 switch (pPage->enmKind)
2382 {
2383 /*
2384 * We only care about shadow page tables.
2385 */
2386 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
2387 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
2388 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
2389 {
2390 unsigned cPresent = pPage->cPresent;
2391 PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2392 for (unsigned i = pPage->iFirstPresent; i < ELEMENTS(pPT->a); i++)
2393 if (pPT->a[i].n.u1Present)
2394 {
2395 if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
2396 {
2397 //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX32\n", iPage, i, pPT->a[i]));
2398 pPT->a[i].u = 0;
2399 }
2400 if (!--cPresent)
2401 break;
2402 }
2403 break;
2404 }
2405
2406 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
2407 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
2408 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
2409 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
2410 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
2411 {
2412 unsigned cPresent = pPage->cPresent;
2413 PX86PTPAE pPT = (PX86PTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
2414 for (unsigned i = pPage->iFirstPresent; i < ELEMENTS(pPT->a); i++)
2415 if (pPT->a[i].n.u1Present)
2416 {
2417 if ((pPT->a[i].u & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
2418 {
2419 //Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
2420 pPT->a[i].u = 0;
2421 }
2422 if (!--cPresent)
2423 break;
2424 }
2425 break;
2426 }
2427 }
2428 if (!--cLeft)
2429 break;
2430 }
2431 }
2432
2433 pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
2434 STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
2435 return VINF_SUCCESS;
2436}
2437
2438
2439/**
2440 * Clears the user entry in a user table.
2441 *
2442 * This is used to remove all references to a page when flushing it.
2443 */
2444static void pgmPoolTrackClearPageUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PCPGMPOOLUSER pUser)
2445{
2446 Assert(pUser->iUser != NIL_PGMPOOL_IDX);
2447 Assert(pUser->iUser < pPool->cCurPages);
2448
2449 /*
2450 * Map the user page.
2451 */
2452 PPGMPOOLPAGE pUserPage = &pPool->aPages[pUser->iUser];
2453 union
2454 {
2455 uint64_t *pau64;
2456 uint32_t *pau32;
2457 } u;
2458 u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pUserPage);
2459
2460 /* Safety precaution in case we change the paging for other modes too in the future. */
2461 Assert(PGMGetHyperCR3(CTXSUFF(pPool->pVM)) != pPage->Core.Key);
2462
2463#ifdef VBOX_STRICT
2464 /*
2465 * Some sanity checks.
2466 */
2467 switch (pUserPage->enmKind)
2468 {
2469 case PGMPOOLKIND_ROOT_32BIT_PD:
2470 Assert(!(u.pau32[pUser->iUser] & PGM_PDFLAGS_MAPPING));
2471 Assert(pUser->iUserTable < X86_PG_ENTRIES);
2472 break;
2473 case PGMPOOLKIND_ROOT_PAE_PD:
2474 Assert(!(u.pau64[pUser->iUser] & PGM_PDFLAGS_MAPPING));
2475 Assert(pUser->iUserTable < 2048 && pUser->iUser == PGMPOOL_IDX_PAE_PD);
2476 break;
2477 case PGMPOOLKIND_ROOT_PDPT:
2478 Assert(!(u.pau64[pUser->iUserTable] & PGM_PLXFLAGS_PERMANENT));
2479 Assert(pUser->iUserTable < 4);
2480 break;
2481 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
2482 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2483 Assert(pUser->iUserTable < X86_PG_PAE_ENTRIES);
2484 break;
2485 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2486 Assert(!(u.pau64[pUser->iUser] & PGM_PDFLAGS_MAPPING));
2487 Assert(pUser->iUserTable < X86_PG_PAE_ENTRIES);
2488 break;
2489 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2490 Assert(!(u.pau64[pUser->iUserTable] & PGM_PLXFLAGS_PERMANENT));
2491 Assert(pUser->iUserTable < X86_PG_PAE_ENTRIES);
2492 break;
2493 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
2494 Assert(!(u.pau64[pUser->iUserTable] & PGM_PLXFLAGS_PERMANENT));
2495 /* GCPhys >> PAGE_SHIFT is the index here */
2496 break;
2497 default:
2498 AssertMsgFailed(("enmKind=%d\n", pUserPage->enmKind));
2499 break;
2500 }
2501#endif /* VBOX_STRICT */
2502
2503 /*
2504 * Clear the entry in the user page.
2505 */
2506 switch (pUserPage->enmKind)
2507 {
2508 /* 32-bit entries */
2509 case PGMPOOLKIND_ROOT_32BIT_PD:
2510 u.pau32[pUser->iUserTable] = 0;
2511 break;
2512
2513 /* 64-bit entries */
2514 case PGMPOOLKIND_ROOT_PAE_PD:
2515 case PGMPOOLKIND_ROOT_PDPT:
2516 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
2517 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
2518 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
2519 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
2520 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
2521 u.pau64[pUser->iUserTable] = 0;
2522 break;
2523
2524 default:
2525 AssertFatalMsgFailed(("enmKind=%d iUser=%#x iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
2526 }
2527}
2528
2529
2530/**
2531 * Clears all users of a page.
2532 */
2533static void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2534{
2535 /*
2536 * Free all the user records.
2537 */
2538 PPGMPOOLUSER paUsers = pPool->CTXSUFF(paUsers);
2539 uint16_t i = pPage->iUserHead;
2540 while (i != NIL_PGMPOOL_USER_INDEX)
2541 {
2542 /* Clear enter in user table. */
2543 pgmPoolTrackClearPageUser(pPool, pPage, &paUsers[i]);
2544
2545 /* Free it. */
2546 const uint16_t iNext = paUsers[i].iNext;
2547 paUsers[i].iUser = NIL_PGMPOOL_IDX;
2548 paUsers[i].iNext = pPool->iUserFreeHead;
2549 pPool->iUserFreeHead = i;
2550
2551 /* Next. */
2552 i = iNext;
2553 }
2554 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
2555}
2556
2557
2558#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2559/**
2560 * Allocates a new physical cross reference extent.
2561 *
2562 * @returns Pointer to the allocated extent on success. NULL if we're out of them.
2563 * @param pVM The VM handle.
2564 * @param piPhysExt Where to store the phys ext index.
2565 */
2566PPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt)
2567{
2568 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
2569 uint16_t iPhysExt = pPool->iPhysExtFreeHead;
2570 if (iPhysExt == NIL_PGMPOOL_PHYSEXT_INDEX)
2571 {
2572 STAM_COUNTER_INC(&pPool->StamTrackPhysExtAllocFailures);
2573 return NULL;
2574 }
2575 PPGMPOOLPHYSEXT pPhysExt = &pPool->CTXSUFF(paPhysExts)[iPhysExt];
2576 pPool->iPhysExtFreeHead = pPhysExt->iNext;
2577 pPhysExt->iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
2578 *piPhysExt = iPhysExt;
2579 return pPhysExt;
2580}
2581
2582
2583/**
2584 * Frees a physical cross reference extent.
2585 *
2586 * @param pVM The VM handle.
2587 * @param iPhysExt The extent to free.
2588 */
2589void pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt)
2590{
2591 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
2592 Assert(iPhysExt < pPool->cMaxPhysExts);
2593 PPGMPOOLPHYSEXT pPhysExt = &pPool->CTXSUFF(paPhysExts)[iPhysExt];
2594 for (unsigned i = 0; i < ELEMENTS(pPhysExt->aidx); i++)
2595 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
2596 pPhysExt->iNext = pPool->iPhysExtFreeHead;
2597 pPool->iPhysExtFreeHead = iPhysExt;
2598}
2599
2600
2601/**
2602 * Frees a physical cross reference extent.
2603 *
2604 * @param pVM The VM handle.
2605 * @param iPhysExt The extent to free.
2606 */
2607void pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt)
2608{
2609 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
2610
2611 const uint16_t iPhysExtStart = iPhysExt;
2612 PPGMPOOLPHYSEXT pPhysExt;
2613 do
2614 {
2615 Assert(iPhysExt < pPool->cMaxPhysExts);
2616 pPhysExt = &pPool->CTXSUFF(paPhysExts)[iPhysExt];
2617 for (unsigned i = 0; i < ELEMENTS(pPhysExt->aidx); i++)
2618 pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
2619
2620 /* next */
2621 iPhysExt = pPhysExt->iNext;
2622 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
2623
2624 pPhysExt->iNext = pPool->iPhysExtFreeHead;
2625 pPool->iPhysExtFreeHead = iPhysExtStart;
2626}
2627
2628/**
2629 * Insert a reference into a list of physical cross reference extents.
2630 *
2631 * @returns The new ram range flags (top 16-bits).
2632 *
2633 * @param pVM The VM handle.
2634 * @param iPhysExt The physical extent index of the list head.
2635 * @param iShwPT The shadow page table index.
2636 *
2637 */
2638static uint16_t pgmPoolTrackPhysExtInsert(PVM pVM, uint16_t iPhysExt, uint16_t iShwPT)
2639{
2640 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
2641 PPGMPOOLPHYSEXT paPhysExts = pPool->CTXSUFF(paPhysExts);
2642
2643 /* special common case. */
2644 if (paPhysExts[iPhysExt].aidx[2] == NIL_PGMPOOL_IDX)
2645 {
2646 paPhysExts[iPhysExt].aidx[2] = iShwPT;
2647 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
2648 LogFlow(("pgmPoolTrackPhysExtAddref: %d:{,,%d}\n", iPhysExt, iShwPT));
2649 return iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2650 }
2651
2652 /* general treatment. */
2653 const uint16_t iPhysExtStart = iPhysExt;
2654 unsigned cMax = 15;
2655 for (;;)
2656 {
2657 Assert(iPhysExt < pPool->cMaxPhysExts);
2658 for (unsigned i = 0; i < ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
2659 if (paPhysExts[iPhysExt].aidx[i] == NIL_PGMPOOL_IDX)
2660 {
2661 paPhysExts[iPhysExt].aidx[i] = iShwPT;
2662 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedMany);
2663 LogFlow(("pgmPoolTrackPhysExtAddref: %d:{%d} i=%d cMax=%d\n", iPhysExt, iShwPT, i, cMax));
2664 return iPhysExtStart | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2665 }
2666 if (!--cMax)
2667 {
2668 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
2669 pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
2670 LogFlow(("pgmPoolTrackPhysExtAddref: overflow (1) iShwPT=%d\n", iShwPT));
2671 return MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2672 }
2673 }
2674
2675 /* add another extent to the list. */
2676 PPGMPOOLPHYSEXT pNew = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
2677 if (!pNew)
2678 {
2679 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackOverflows);
2680 pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
2681 return MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2682 }
2683 pNew->iNext = iPhysExtStart;
2684 pNew->aidx[0] = iShwPT;
2685 LogFlow(("pgmPoolTrackPhysExtAddref: added new extent %d:{%d}->%d\n", iPhysExt, iShwPT, iPhysExtStart));
2686 return iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2687}
2688
2689
2690/**
2691 * Add a reference to guest physical page where extents are in use.
2692 *
2693 * @returns The new ram range flags (top 16-bits).
2694 *
2695 * @param pVM The VM handle.
2696 * @param u16 The ram range flags (top 16-bits).
2697 * @param iShwPT The shadow page table index.
2698 */
2699uint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT)
2700{
2701 if ((u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) != MM_RAM_FLAGS_CREFS_PHYSEXT)
2702 {
2703 /*
2704 * Convert to extent list.
2705 */
2706 Assert((u16 >> (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT)) == 1);
2707 uint16_t iPhysExt;
2708 PPGMPOOLPHYSEXT pPhysExt = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
2709 if (pPhysExt)
2710 {
2711 LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, u16 & MM_RAM_FLAGS_IDX_MASK, iShwPT));
2712 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliased);
2713 pPhysExt->aidx[0] = u16 & MM_RAM_FLAGS_IDX_MASK;
2714 pPhysExt->aidx[1] = iShwPT;
2715 u16 = iPhysExt | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2716 }
2717 else
2718 u16 = MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT));
2719 }
2720 else if (u16 != (MM_RAM_FLAGS_IDX_OVERFLOWED | (MM_RAM_FLAGS_CREFS_PHYSEXT << (MM_RAM_FLAGS_CREFS_SHIFT - MM_RAM_FLAGS_IDX_SHIFT))))
2721 {
2722 /*
2723 * Insert into the extent list.
2724 */
2725 u16 = pgmPoolTrackPhysExtInsert(pVM, u16 & MM_RAM_FLAGS_IDX_MASK, iShwPT);
2726 }
2727 else
2728 STAM_COUNTER_INC(&pVM->pgm.s.StatTrackAliasedLots);
2729 return u16;
2730}
2731
2732
2733/**
2734 * Clear references to guest physical memory.
2735 *
2736 * @param pPool The pool.
2737 * @param pPage The page.
2738 * @param pPhysPage Pointer to the aPages entry in the ram range.
2739 */
2740void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMPAGE pPhysPage)
2741{
2742 const unsigned cRefs = pPhysPage->HCPhys >> MM_RAM_FLAGS_CREFS_SHIFT; /** @todo PAGE FLAGS */
2743 AssertFatalMsg(cRefs == MM_RAM_FLAGS_CREFS_PHYSEXT, ("cRefs=%d HCPhys=%RHp pPage=%p:{.idx=%d}\n", cRefs, pPhysPage->HCPhys, pPage, pPage->idx));
2744
2745 uint16_t iPhysExt = (pPhysPage->HCPhys >> MM_RAM_FLAGS_IDX_SHIFT) & MM_RAM_FLAGS_IDX_MASK;
2746 if (iPhysExt != MM_RAM_FLAGS_IDX_OVERFLOWED)
2747 {
2748 uint16_t iPhysExtPrev = NIL_PGMPOOL_PHYSEXT_INDEX;
2749 PPGMPOOLPHYSEXT paPhysExts = pPool->CTXSUFF(paPhysExts);
2750 do
2751 {
2752 Assert(iPhysExt < pPool->cMaxPhysExts);
2753
2754 /*
2755 * Look for the shadow page and check if it's all freed.
2756 */
2757 for (unsigned i = 0; i < ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
2758 {
2759 if (paPhysExts[iPhysExt].aidx[i] == pPage->idx)
2760 {
2761 paPhysExts[iPhysExt].aidx[i] = NIL_PGMPOOL_IDX;
2762
2763 for (i = 0; i < ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
2764 if (paPhysExts[iPhysExt].aidx[i] != NIL_PGMPOOL_IDX)
2765 {
2766 LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d\n", pPhysPage->HCPhys, pPage->idx));
2767 return;
2768 }
2769
2770 /* we can free the node. */
2771 PVM pVM = pPool->CTXSUFF(pVM);
2772 const uint16_t iPhysExtNext = paPhysExts[iPhysExt].iNext;
2773 if ( iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX
2774 && iPhysExtNext == NIL_PGMPOOL_PHYSEXT_INDEX)
2775 {
2776 /* lonely node */
2777 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
2778 LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d lonely\n", pPhysPage->HCPhys, pPage->idx));
2779 pPhysPage->HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
2780 }
2781 else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
2782 {
2783 /* head */
2784 LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d head\n", pPhysPage->HCPhys, pPage->idx));
2785 pPhysPage->HCPhys = (pPhysPage->HCPhys & MM_RAM_FLAGS_NO_REFS_MASK) /** @todo PAGE FLAGS */
2786 | ((uint64_t)MM_RAM_FLAGS_CREFS_PHYSEXT << MM_RAM_FLAGS_CREFS_SHIFT)
2787 | ((uint64_t)iPhysExtNext << MM_RAM_FLAGS_IDX_SHIFT);
2788 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
2789 }
2790 else
2791 {
2792 /* in list */
2793 LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64 idx=%d\n", pPhysPage->HCPhys, pPage->idx));
2794 paPhysExts[iPhysExtPrev].iNext = iPhysExtNext;
2795 pgmPoolTrackPhysExtFree(pVM, iPhysExt);
2796 }
2797 iPhysExt = iPhysExtNext;
2798 return;
2799 }
2800 }
2801
2802 /* next */
2803 iPhysExtPrev = iPhysExt;
2804 iPhysExt = paPhysExts[iPhysExt].iNext;
2805 } while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
2806
2807 AssertFatalMsgFailed(("not-found! cRefs=%d HCPhys=%RHp pPage=%p:{.idx=%d}\n", cRefs, pPhysPage->HCPhys, pPage, pPage->idx));
2808 }
2809 else /* nothing to do */
2810 LogFlow(("pgmPoolTrackPhysExtDerefGCPhys: HCPhys=%RX64\n", pPhysPage->HCPhys));
2811}
2812
2813
2814
2815/**
2816 * Clear references to guest physical memory.
2817 *
2818 * This is the same as pgmPoolTracDerefGCPhys except that the guest physical address
2819 * is assumed to be correct, so the linear search can be skipped and we can assert
2820 * at an earlier point.
2821 *
2822 * @param pPool The pool.
2823 * @param pPage The page.
2824 * @param HCPhys The host physical address corresponding to the guest page.
2825 * @param GCPhys The guest physical address corresponding to HCPhys.
2826 */
2827static void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys)
2828{
2829 /*
2830 * Walk range list.
2831 */
2832 PPGMRAMRANGE pRam = pPool->CTXSUFF(pVM)->pgm.s.CTXALLSUFF(pRamRanges);
2833 while (pRam)
2834 {
2835 RTGCPHYS off = GCPhys - pRam->GCPhys;
2836 if (off < pRam->cb)
2837 {
2838 /* does it match? */
2839 const unsigned iPage = off >> PAGE_SHIFT;
2840 Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
2841 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
2842 {
2843 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
2844 return;
2845 }
2846 break;
2847 }
2848 pRam = CTXALLSUFF(pRam->pNext);
2849 }
2850 AssertFatalMsgFailed(("HCPhys=%VHp GCPhys=%VGp\n", HCPhys, GCPhys));
2851}
2852
2853
2854/**
2855 * Clear references to guest physical memory.
2856 *
2857 * @param pPool The pool.
2858 * @param pPage The page.
2859 * @param HCPhys The host physical address corresponding to the guest page.
2860 * @param GCPhysHint The guest physical address which may corresponding to HCPhys.
2861 */
2862static void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint)
2863{
2864 /*
2865 * Walk range list.
2866 */
2867 PPGMRAMRANGE pRam = pPool->CTXSUFF(pVM)->pgm.s.CTXALLSUFF(pRamRanges);
2868 while (pRam)
2869 {
2870 RTGCPHYS off = GCPhysHint - pRam->GCPhys;
2871 if (off < pRam->cb)
2872 {
2873 /* does it match? */
2874 const unsigned iPage = off >> PAGE_SHIFT;
2875 Assert(PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]));
2876 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
2877 {
2878 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
2879 return;
2880 }
2881 break;
2882 }
2883 pRam = CTXALLSUFF(pRam->pNext);
2884 }
2885
2886 /*
2887 * Damn, the hint didn't work. We'll have to do an expensive linear search.
2888 */
2889 STAM_COUNTER_INC(&pPool->StatTrackLinearRamSearches);
2890 pRam = pPool->CTXSUFF(pVM)->pgm.s.CTXALLSUFF(pRamRanges);
2891 while (pRam)
2892 {
2893 unsigned iPage = pRam->cb >> PAGE_SHIFT;
2894 while (iPage-- > 0)
2895 {
2896 if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
2897 {
2898 Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%VHp GCPhysHint=%VGp GCPhysReal=%VGp\n",
2899 HCPhys, GCPhysHint, pRam->GCPhys + (iPage << PAGE_SHIFT)));
2900 pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage]);
2901 return;
2902 }
2903 }
2904 pRam = CTXALLSUFF(pRam->pNext);
2905 }
2906
2907 AssertFatalMsgFailed(("HCPhys=%VHp GCPhysHint=%VGp\n", HCPhys, GCPhysHint));
2908}
2909
2910
2911/**
2912 * Clear references to guest physical memory in a 32-bit / 32-bit page table.
2913 *
2914 * @param pPool The pool.
2915 * @param pPage The page.
2916 * @param pShwPT The shadow page table (mapping of the page).
2917 * @param pGstPT The guest page table.
2918 */
2919DECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
2920{
2921 for (unsigned i = pPage->iFirstPresent; i < ELEMENTS(pShwPT->a); i++)
2922 if (pShwPT->a[i].n.u1Present)
2923 {
2924 Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
2925 i, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
2926 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
2927 if (!--pPage->cPresent)
2928 break;
2929 }
2930}
2931
2932
2933/**
2934 * Clear references to guest physical memory in a PAE / 32-bit page table.
2935 *
2936 * @param pPool The pool.
2937 * @param pPage The page.
2938 * @param pShwPT The shadow page table (mapping of the page).
2939 * @param pGstPT The guest page table (just a half one).
2940 */
2941DECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PT pGstPT)
2942{
2943 for (unsigned i = 0; i < ELEMENTS(pShwPT->a); i++)
2944 if (pShwPT->a[i].n.u1Present)
2945 {
2946 Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX32 hint=%RX32\n",
2947 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
2948 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK);
2949 }
2950}
2951
2952
2953/**
2954 * Clear references to guest physical memory in a PAE / PAE page table.
2955 *
2956 * @param pPool The pool.
2957 * @param pPage The page.
2958 * @param pShwPT The shadow page table (mapping of the page).
2959 * @param pGstPT The guest page table.
2960 */
2961DECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT, PCX86PTPAE pGstPT)
2962{
2963 for (unsigned i = 0; i < ELEMENTS(pShwPT->a); i++)
2964 if (pShwPT->a[i].n.u1Present)
2965 {
2966 Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX32 hint=%RX32\n",
2967 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
2968 pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK);
2969 }
2970}
2971
2972
2973/**
2974 * Clear references to guest physical memory in a 32-bit / 4MB page table.
2975 *
2976 * @param pPool The pool.
2977 * @param pPage The page.
2978 * @param pShwPT The shadow page table (mapping of the page).
2979 */
2980DECLINLINE(void) pgmPoolTrackDerefPT32Bit4MB(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT)
2981{
2982 RTGCPHYS GCPhys = pPage->GCPhys;
2983 for (unsigned i = 0; i < ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
2984 if (pShwPT->a[i].n.u1Present)
2985 {
2986 Log4(("pgmPoolTrackDerefPT32Bit4MB: i=%d pte=%RX32 GCPhys=%RGp\n",
2987 i, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys));
2988 pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys);
2989 }
2990}
2991
2992
2993/**
2994 * Clear references to guest physical memory in a PAE / 2/4MB page table.
2995 *
2996 * @param pPool The pool.
2997 * @param pPage The page.
2998 * @param pShwPT The shadow page table (mapping of the page).
2999 */
3000DECLINLINE(void) pgmPoolTrackDerefPTPaeBig(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PTPAE pShwPT)
3001{
3002 RTGCPHYS GCPhys = pPage->GCPhys;
3003 for (unsigned i = 0; i < ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
3004 if (pShwPT->a[i].n.u1Present)
3005 {
3006 Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX32 hint=%RX32\n",
3007 i, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys));
3008 pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PAE_PG_MASK, GCPhys);
3009 }
3010}
3011#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
3012
3013
3014/**
3015 * Clear references to shadowed pages in a PAE (legacy or 64 bits) page directory.
3016 *
3017 * @param pPool The pool.
3018 * @param pPage The page.
3019 * @param pShwPD The shadow page directory (mapping of the page).
3020 */
3021DECLINLINE(void) pgmPoolTrackDerefPDPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPAE pShwPD)
3022{
3023 for (unsigned i = 0; i < ELEMENTS(pShwPD->a); i++)
3024 {
3025 if (pShwPD->a[i].n.u1Present)
3026 {
3027 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
3028 if (pSubPage)
3029 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3030 else
3031 AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & X86_PDE_PAE_PG_MASK));
3032 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3033 }
3034 }
3035}
3036
3037
3038/**
3039 * Clear references to shadowed pages in a 64-bit page directory pointer table.
3040 *
3041 * @param pPool The pool.
3042 * @param pPage The page.
3043 * @param pShwPDPT The shadow page directory pointer table (mapping of the page).
3044 */
3045DECLINLINE(void) pgmPoolTrackDerefPDPT64Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
3046{
3047 for (unsigned i = 0; i < ELEMENTS(pShwPDPT->a); i++)
3048 {
3049 if (pShwPDPT->a[i].n.u1Present)
3050 {
3051 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
3052 if (pSubPage)
3053 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3054 else
3055 AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
3056 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3057 }
3058 }
3059}
3060
3061/**
3062 * Clear references to shadowed pages in a 64-bit level 4 page table.
3063 *
3064 * @param pPool The pool.
3065 * @param pPage The page.
3066 * @param pShwPML4 The shadow page directory pointer table (mapping of the page).
3067 */
3068DECLINLINE(void) pgmPoolTrackDerefPML464Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PML4 pShwPML4)
3069{
3070 for (unsigned i = 0; i < ELEMENTS(pShwPML4->a); i++)
3071 {
3072 if (pShwPML4->a[i].n.u1Present)
3073 {
3074 PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
3075 if (pSubPage)
3076 pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
3077 else
3078 AssertFatalMsgFailed(("%RX64\n", pShwPML4->a[i].u & X86_PML4E_PG_MASK));
3079 /** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
3080 }
3081 }
3082}
3083
3084
3085/**
3086 * Clears all references made by this page.
3087 *
3088 * This includes other shadow pages and GC physical addresses.
3089 *
3090 * @param pPool The pool.
3091 * @param pPage The page.
3092 */
3093static void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
3094{
3095 /*
3096 * Map the shadow page and take action according to the page kind.
3097 */
3098 void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pPage);
3099 switch (pPage->enmKind)
3100 {
3101#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
3102 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3103 {
3104 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3105 void *pvGst;
3106 int rc = PGM_GCPHYS_2_PTR(pPool->CTXSUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
3107 pgmPoolTrackDerefPT32Bit32Bit(pPool, pPage, (PX86PT)pvShw, (PCX86PT)pvGst);
3108 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3109 break;
3110 }
3111
3112 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3113 {
3114 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3115 void *pvGst;
3116 int rc = PGM_GCPHYS_2_PTR_EX(pPool->CTXSUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
3117 pgmPoolTrackDerefPTPae32Bit(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PT)pvGst);
3118 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3119 break;
3120 }
3121
3122 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
3123 {
3124 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3125 void *pvGst;
3126 int rc = PGM_GCPHYS_2_PTR(pPool->CTXSUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
3127 pgmPoolTrackDerefPTPaePae(pPool, pPage, (PX86PTPAE)pvShw, (PCX86PTPAE)pvGst);
3128 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3129 break;
3130 }
3131
3132 case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
3133 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3134 {
3135 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3136 pgmPoolTrackDerefPT32Bit4MB(pPool, pPage, (PX86PT)pvShw);
3137 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3138 break;
3139 }
3140
3141 case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 4 MB page */
3142 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
3143 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
3144 {
3145 STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
3146 pgmPoolTrackDerefPTPaeBig(pPool, pPage, (PX86PTPAE)pvShw);
3147 STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
3148 break;
3149 }
3150
3151#else /* !PGMPOOL_WITH_GCPHYS_TRACKING */
3152 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
3153 case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
3154 case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
3155 case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
3156 case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
3157 case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
3158 case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
3159 case PGMPOOLKIND_PAE_PT_FOR_PHYS:
3160 break;
3161#endif /* !PGMPOOL_WITH_GCPHYS_TRACKING */
3162
3163 case PGMPOOLKIND_PAE_PD_FOR_32BIT_PD:
3164 case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
3165 case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
3166 pgmPoolTrackDerefPDPae(pPool, pPage, (PX86PDPAE)pvShw);
3167 break;
3168
3169 case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
3170 pgmPoolTrackDerefPDPT64Bit(pPool, pPage, (PX86PDPT)pvShw);
3171 break;
3172
3173 case PGMPOOLKIND_64BIT_PML4_FOR_64BIT_PML4:
3174 pgmPoolTrackDerefPML464Bit(pPool, pPage, (PX86PML4)pvShw);
3175 break;
3176
3177 default:
3178 AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
3179 }
3180
3181 /* paranoia, clear the shadow page. Remove this laser (i.e. let Alloc and ClearAll do it). */
3182 STAM_PROFILE_START(&pPool->StatZeroPage, z);
3183 ASMMemZeroPage(pvShw);
3184 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
3185 pPage->fZeroed = true;
3186}
3187#endif /* PGMPOOL_WITH_USER_TRACKING */
3188
3189
3190/**
3191 * Flushes all the special root pages as part of a pgmPoolFlushAllInt operation.
3192 *
3193 * @param pPool The pool.
3194 */
3195static void pgmPoolFlushAllSpecialRoots(PPGMPOOL pPool)
3196{
3197 /*
3198 * These special pages are all mapped into the indexes 1..PGMPOOL_IDX_FIRST.
3199 */
3200 Assert(NIL_PGMPOOL_IDX == 0);
3201 for (unsigned i = 1; i < PGMPOOL_IDX_FIRST; i++)
3202 {
3203 /*
3204 * Get the page address.
3205 */
3206 PPGMPOOLPAGE pPage = &pPool->aPages[i];
3207 union
3208 {
3209 uint64_t *pau64;
3210 uint32_t *pau32;
3211 } u;
3212 u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTXSUFF(pVM), pPage);
3213
3214 /*
3215 * Mark stuff not present.
3216 */
3217 switch (pPage->enmKind)
3218 {
3219 case PGMPOOLKIND_ROOT_32BIT_PD:
3220 for (unsigned iPage = 0; iPage < X86_PG_ENTRIES; iPage++)
3221 if ((u.pau32[iPage] & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == X86_PDE_P)
3222 u.pau32[iPage] = 0;
3223 break;
3224
3225 case PGMPOOLKIND_ROOT_PAE_PD:
3226 for (unsigned iPage = 0; iPage < X86_PG_PAE_ENTRIES * X86_PG_PAE_PDPE_ENTRIES; iPage++)
3227 if ((u.pau64[iPage] & (PGM_PDFLAGS_MAPPING | X86_PDE_P)) == X86_PDE_P)
3228 u.pau64[iPage] = 0;
3229 break;
3230
3231 case PGMPOOLKIND_ROOT_PDPT:
3232 /* Not root of shadowed pages currently, ignore it. */
3233 break;
3234 }
3235 }
3236
3237 /*
3238 * Paranoia (to be removed), flag a global CR3 sync.
3239 */
3240 VM_FF_SET(pPool->CTXSUFF(pVM), VM_FF_PGM_SYNC_CR3);
3241}
3242
3243
3244/**
3245 * Flushes the entire cache.
3246 *
3247 * It will assert a global CR3 flush (FF) and assumes the caller is aware of this
3248 * and execute this CR3 flush.
3249 *
3250 * @param pPool The pool.
3251 */
3252static void pgmPoolFlushAllInt(PPGMPOOL pPool)
3253{
3254 STAM_PROFILE_START(&pPool->StatFlushAllInt, a);
3255 LogFlow(("pgmPoolFlushAllInt:\n"));
3256
3257 /*
3258 * If there are no pages in the pool, there is nothing to do.
3259 */
3260 if (pPool->cCurPages <= PGMPOOL_IDX_FIRST)
3261 {
3262 STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
3263 return;
3264 }
3265
3266 /*
3267 * Nuke the free list and reinsert all pages into it.
3268 */
3269 for (unsigned i = pPool->cCurPages - 1; i >= PGMPOOL_IDX_FIRST; i--)
3270 {
3271 PPGMPOOLPAGE pPage = &pPool->aPages[i];
3272
3273#ifdef IN_RING3
3274 Assert(pPage->Core.Key == MMPage2Phys(pPool->pVMHC, pPage->pvPageHC));
3275#endif
3276#ifdef PGMPOOL_WITH_MONITORING
3277 if (pPage->fMonitored)
3278 pgmPoolMonitorFlush(pPool, pPage);
3279 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
3280 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
3281 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
3282 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
3283 pPage->cModifications = 0;
3284#endif
3285 pPage->GCPhys = NIL_RTGCPHYS;
3286 pPage->enmKind = PGMPOOLKIND_FREE;
3287 Assert(pPage->idx == i);
3288 pPage->iNext = i + 1;
3289 pPage->fZeroed = false; /* This could probably be optimized, but better safe than sorry. */
3290 pPage->fSeenNonGlobal = false;
3291 pPage->fMonitored= false;
3292 pPage->fCached = false;
3293 pPage->fReusedFlushPending = false;
3294 pPage->fCR3Mix = false;
3295#ifdef PGMPOOL_WITH_USER_TRACKING
3296 pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
3297#endif
3298#ifdef PGMPOOL_WITH_CACHE
3299 pPage->iAgeNext = NIL_PGMPOOL_IDX;
3300 pPage->iAgePrev = NIL_PGMPOOL_IDX;
3301#endif
3302 }
3303 pPool->aPages[pPool->cCurPages - 1].iNext = NIL_PGMPOOL_IDX;
3304 pPool->iFreeHead = PGMPOOL_IDX_FIRST;
3305 pPool->cUsedPages = 0;
3306
3307#ifdef PGMPOOL_WITH_USER_TRACKING
3308 /*
3309 * Zap and reinitialize the user records.
3310 */
3311 pPool->cPresent = 0;
3312 pPool->iUserFreeHead = 0;
3313 PPGMPOOLUSER paUsers = pPool->CTXSUFF(paUsers);
3314 const unsigned cMaxUsers = pPool->cMaxUsers;
3315 for (unsigned i = 0; i < cMaxUsers; i++)
3316 {
3317 paUsers[i].iNext = i + 1;
3318 paUsers[i].iUser = NIL_PGMPOOL_IDX;
3319 paUsers[i].iUserTable = 0xfffffffe;
3320 }
3321 paUsers[cMaxUsers - 1].iNext = NIL_PGMPOOL_USER_INDEX;
3322#endif
3323
3324#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
3325 /*
3326 * Clear all the GCPhys links and rebuild the phys ext free list.
3327 */
3328 for (PPGMRAMRANGE pRam = pPool->CTXSUFF(pVM)->pgm.s.CTXALLSUFF(pRamRanges);
3329 pRam;
3330 pRam = CTXALLSUFF(pRam->pNext))
3331 {
3332 unsigned iPage = pRam->cb >> PAGE_SHIFT;
3333 while (iPage-- > 0)
3334 pRam->aPages[iPage].HCPhys &= MM_RAM_FLAGS_NO_REFS_MASK; /** @todo PAGE FLAGS */
3335 }
3336
3337 pPool->iPhysExtFreeHead = 0;
3338 PPGMPOOLPHYSEXT paPhysExts = pPool->CTXSUFF(paPhysExts);
3339 const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
3340 for (unsigned i = 0; i < cMaxPhysExts; i++)
3341 {
3342 paPhysExts[i].iNext = i + 1;
3343 paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
3344 paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
3345 paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
3346 }
3347 paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
3348#endif
3349
3350#ifdef PGMPOOL_WITH_MONITORING
3351 /*
3352 * Just zap the modified list.
3353 */
3354 pPool->cModifiedPages = 0;
3355 pPool->iModifiedHead = NIL_PGMPOOL_IDX;
3356#endif
3357
3358#ifdef PGMPOOL_WITH_CACHE
3359 /*
3360 * Clear the GCPhys hash and the age list.
3361 */
3362 for (unsigned i = 0; i < ELEMENTS(pPool->aiHash); i++)
3363 pPool->aiHash[i] = NIL_PGMPOOL_IDX;
3364 pPool->iAgeHead = NIL_PGMPOOL_IDX;
3365 pPool->iAgeTail = NIL_PGMPOOL_IDX;
3366#endif
3367
3368 /*
3369 * Flush all the special root pages.
3370 * Reinsert active pages into the hash and ensure monitoring chains are correct.
3371 */
3372 pgmPoolFlushAllSpecialRoots(pPool);
3373 for (unsigned i = PGMPOOL_IDX_FIRST_SPECIAL; i < PGMPOOL_IDX_FIRST; i++)
3374 {
3375 PPGMPOOLPAGE pPage = &pPool->aPages[i];
3376 pPage->iNext = NIL_PGMPOOL_IDX;
3377#ifdef PGMPOOL_WITH_MONITORING
3378 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
3379 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
3380 pPage->cModifications = 0;
3381 /* ASSUMES that we're not sharing with any of the other special pages (safe for now). */
3382 pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
3383 pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
3384 if (pPage->fMonitored)
3385 {
3386 PVM pVM = pPool->CTXSUFF(pVM);
3387 int rc = PGMHandlerPhysicalChangeCallbacks(pVM, pPage->GCPhys & ~(RTGCPHYS)(PAGE_SIZE - 1),
3388 pPool->pfnAccessHandlerR3, MMHyperCCToR3(pVM, pPage),
3389 pPool->pfnAccessHandlerR0, MMHyperCCToR0(pVM, pPage),
3390 pPool->pfnAccessHandlerGC, MMHyperCCToGC(pVM, pPage),
3391 pPool->pszAccessHandler);
3392 AssertFatalRCSuccess(rc);
3393# ifdef PGMPOOL_WITH_CACHE
3394 pgmPoolHashInsert(pPool, pPage);
3395# endif
3396 }
3397#endif
3398#ifdef PGMPOOL_WITH_USER_TRACKING
3399 Assert(pPage->iUserHead == NIL_PGMPOOL_USER_INDEX); /* for now */
3400#endif
3401#ifdef PGMPOOL_WITH_CACHE
3402 Assert(pPage->iAgeNext == NIL_PGMPOOL_IDX);
3403 Assert(pPage->iAgePrev == NIL_PGMPOOL_IDX);
3404#endif
3405 }
3406
3407 STAM_PROFILE_STOP(&pPool->StatFlushAllInt, a);
3408}
3409
3410
3411/**
3412 * Flushes a pool page.
3413 *
3414 * This moves the page to the free list after removing all user references to it.
3415 * In GC this will cause a CR3 reload if the page is traced back to an active root page.
3416 *
3417 * @returns VBox status code.
3418 * @retval VINF_SUCCESS on success.
3419 * @retval VERR_PGM_POOL_CLEARED if the deregistration of the physical handler will cause a light weight pool flush.
3420 * @param pPool The pool.
3421 * @param HCPhys The HC physical address of the shadow page.
3422 */
3423int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
3424{
3425 int rc = VINF_SUCCESS;
3426 STAM_PROFILE_START(&pPool->StatFlushPage, f);
3427 LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%VHp, .idx=%d, .enmKind=%d, .GCPhys=%VGp}\n",
3428 pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, pPage->GCPhys));
3429
3430 /*
3431 * Quietly reject any attempts at flushing any of the special root pages.
3432 */
3433 if (pPage->idx < PGMPOOL_IDX_FIRST)
3434 {
3435 Log(("pgmPoolFlushPage: special root page, rejected. enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
3436 return VINF_SUCCESS;
3437 }
3438
3439 /*
3440 * Quietly reject any attempts at flushing the currently active shadow CR3 mapping
3441 */
3442 if ( pPage->idx == PGMPOOL_IDX_AMD64_CR3
3443 && PGMGetHyperCR3(CTXSUFF(pPool->pVM)) == pPage->Core.Key)
3444 {
3445 Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
3446 return VINF_SUCCESS;
3447 }
3448 /* Safety precaution in case we change the paging for other modes too in the future. */
3449 AssertFatal(PGMGetHyperCR3(CTXSUFF(pPool->pVM)) != pPage->Core.Key);
3450
3451 /*
3452 * Mark the page as being in need of a ASMMemZeroPage().
3453 */
3454 pPage->fZeroed = false;
3455
3456#ifdef PGMPOOL_WITH_USER_TRACKING
3457 /*
3458 * Clear the page.
3459 */
3460 pgmPoolTrackClearPageUsers(pPool, pPage);
3461 STAM_PROFILE_START(&pPool->StatTrackDeref,a);
3462 pgmPoolTrackDeref(pPool, pPage);
3463 STAM_PROFILE_STOP(&pPool->StatTrackDeref,a);
3464#endif
3465
3466#ifdef PGMPOOL_WITH_CACHE
3467 /*
3468 * Flush it from the cache.
3469 */
3470 pgmPoolCacheFlushPage(pPool, pPage);
3471#endif /* PGMPOOL_WITH_CACHE */
3472
3473#ifdef PGMPOOL_WITH_MONITORING
3474 /*
3475 * Deregistering the monitoring.
3476 */
3477 if (pPage->fMonitored)
3478 rc = pgmPoolMonitorFlush(pPool, pPage);
3479#endif
3480
3481 /*
3482 * Free the page.
3483 */
3484 Assert(pPage->iNext == NIL_PGMPOOL_IDX);
3485 pPage->iNext = pPool->iFreeHead;
3486 pPool->iFreeHead = pPage->idx;
3487 pPage->enmKind = PGMPOOLKIND_FREE;
3488 pPage->GCPhys = NIL_RTGCPHYS;
3489 pPage->fReusedFlushPending = false;
3490
3491 pPool->cUsedPages--;
3492 STAM_PROFILE_STOP(&pPool->StatFlushPage, f);
3493 return rc;
3494}
3495
3496
3497/**
3498 * Frees a usage of a pool page.
3499 *
3500 * The caller is responsible to updating the user table so that it no longer
3501 * references the shadow page.
3502 *
3503 * @param pPool The pool.
3504 * @param HCPhys The HC physical address of the shadow page.
3505 * @param iUser The shadow page pool index of the user table.
3506 * @param iUserTable The index into the user table (shadowed).
3507 */
3508void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
3509{
3510 STAM_PROFILE_START(&pPool->StatFree, a);
3511 LogFlow(("pgmPoolFreeByPage: pPage=%p:{.Key=%VHp, .idx=%d, enmKind=%d} iUser=%#x iUserTable=%#x\n",
3512 pPage, pPage->Core.Key, pPage->idx, pPage->enmKind, iUser, iUserTable));
3513 Assert(pPage->idx >= PGMPOOL_IDX_FIRST);
3514#ifdef PGMPOOL_WITH_USER_TRACKING
3515 pgmPoolTrackFreeUser(pPool, pPage, iUser, iUserTable);
3516#endif
3517#ifdef PGMPOOL_WITH_CACHE
3518 if (!pPage->fCached)
3519#endif
3520 pgmPoolFlushPage(pPool, pPage); /* ASSUMES that VERR_PGM_POOL_CLEARED can be ignored here. */
3521 STAM_PROFILE_STOP(&pPool->StatFree, a);
3522}
3523
3524
3525/**
3526 * Makes one or more free page free.
3527 *
3528 * @returns VBox status code.
3529 * @retval VINF_SUCCESS on success.
3530 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
3531 *
3532 * @param pPool The pool.
3533 * @param iUser The user of the page.
3534 */
3535static int pgmPoolMakeMoreFreePages(PPGMPOOL pPool, uint16_t iUser)
3536{
3537 LogFlow(("pgmPoolMakeMoreFreePages: iUser=%#x\n", iUser));
3538
3539 /*
3540 * If the pool isn't full grown yet, expand it.
3541 */
3542 if (pPool->cCurPages < pPool->cMaxPages)
3543 {
3544 STAM_PROFILE_ADV_SUSPEND(&pPool->StatAlloc, a);
3545#ifdef IN_RING3
3546 int rc = PGMR3PoolGrow(pPool->pVMHC);
3547#else
3548 int rc = CTXALLMID(VMM, CallHost)(pPool->CTXSUFF(pVM), VMMCALLHOST_PGM_POOL_GROW, 0);
3549#endif
3550 if (VBOX_FAILURE(rc))
3551 return rc;
3552 STAM_PROFILE_ADV_RESUME(&pPool->StatAlloc, a);
3553 if (pPool->iFreeHead != NIL_PGMPOOL_IDX)
3554 return VINF_SUCCESS;
3555 }
3556
3557#ifdef PGMPOOL_WITH_CACHE
3558 /*
3559 * Free one cached page.
3560 */
3561 return pgmPoolCacheFreeOne(pPool, iUser);
3562#else
3563 /*
3564 * Flush the pool.
3565 * If we have tracking enabled, it should be possible to come up with
3566 * a cheap replacement strategy...
3567 */
3568 pgmPoolFlushAllInt(pPool);
3569 return VERR_PGM_POOL_FLUSHED;
3570#endif
3571}
3572
3573
3574/**
3575 * Allocates a page from the pool.
3576 *
3577 * This page may actually be a cached page and not in need of any processing
3578 * on the callers part.
3579 *
3580 * @returns VBox status code.
3581 * @retval VINF_SUCCESS if a NEW page was allocated.
3582 * @retval VINF_PGM_CACHED_PAGE if a CACHED page was returned.
3583 * @retval VERR_PGM_POOL_FLUSHED if the pool was flushed.
3584 * @param pVM The VM handle.
3585 * @param GCPhys The GC physical address of the page we're gonna shadow.
3586 * For 4MB and 2MB PD entries, it's the first address the
3587 * shadow PT is covering.
3588 * @param enmKind The kind of mapping.
3589 * @param iUser The shadow page pool index of the user table.
3590 * @param iUserTable The index into the user table (shadowed).
3591 * @param ppPage Where to store the pointer to the page. NULL is stored here on failure.
3592 */
3593int pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
3594{
3595 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
3596 STAM_PROFILE_ADV_START(&pPool->StatAlloc, a);
3597 LogFlow(("pgmPoolAlloc: GCPhys=%VGp enmKind=%d iUser=%#x iUserTable=%#x\n", GCPhys, enmKind, iUser, iUserTable));
3598 *ppPage = NULL;
3599
3600#ifdef PGMPOOL_WITH_CACHE
3601 if (pPool->fCacheEnabled)
3602 {
3603 int rc2 = pgmPoolCacheAlloc(pPool, GCPhys, enmKind, iUser, iUserTable, ppPage);
3604 if (VBOX_SUCCESS(rc2))
3605 {
3606 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
3607 LogFlow(("pgmPoolAlloc: cached returns %Vrc *ppPage=%p:{.Key=%VHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
3608 return rc2;
3609 }
3610 }
3611#endif
3612
3613 /*
3614 * Allocate a new one.
3615 */
3616 int rc = VINF_SUCCESS;
3617 uint16_t iNew = pPool->iFreeHead;
3618 if (iNew == NIL_PGMPOOL_IDX)
3619 {
3620 rc = pgmPoolMakeMoreFreePages(pPool, iUser);
3621 if (VBOX_FAILURE(rc))
3622 {
3623 if (rc != VERR_PGM_POOL_CLEARED)
3624 {
3625 Log(("pgmPoolAlloc: returns %Vrc (Free)\n", rc));
3626 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
3627 return rc;
3628 }
3629 rc = VERR_PGM_POOL_FLUSHED;
3630 }
3631 iNew = pPool->iFreeHead;
3632 AssertReleaseReturn(iNew != NIL_PGMPOOL_IDX, VERR_INTERNAL_ERROR);
3633 }
3634
3635 /* unlink the free head */
3636 PPGMPOOLPAGE pPage = &pPool->aPages[iNew];
3637 pPool->iFreeHead = pPage->iNext;
3638 pPage->iNext = NIL_PGMPOOL_IDX;
3639
3640 /*
3641 * Initialize it.
3642 */
3643 pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
3644 pPage->enmKind = enmKind;
3645 pPage->GCPhys = GCPhys;
3646 pPage->fSeenNonGlobal = false; /* Set this to 'true' to disable this feature. */
3647 pPage->fMonitored = false;
3648 pPage->fCached = false;
3649 pPage->fReusedFlushPending = false;
3650 pPage->fCR3Mix = false;
3651#ifdef PGMPOOL_WITH_MONITORING
3652 pPage->cModifications = 0;
3653 pPage->iModifiedNext = NIL_PGMPOOL_IDX;
3654 pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
3655#endif
3656#ifdef PGMPOOL_WITH_USER_TRACKING
3657 pPage->cPresent = 0;
3658 pPage->iFirstPresent = ~0;
3659
3660 /*
3661 * Insert into the tracking and cache. If this fails, free the page.
3662 */
3663 int rc3 = pgmPoolTrackInsert(pPool, pPage, GCPhys, iUser, iUserTable);
3664 if (VBOX_FAILURE(rc3))
3665 {
3666 if (rc3 != VERR_PGM_POOL_CLEARED)
3667 {
3668 pPool->cUsedPages--;
3669 pPage->enmKind = PGMPOOLKIND_FREE;
3670 pPage->GCPhys = NIL_RTGCPHYS;
3671 pPage->iNext = pPool->iFreeHead;
3672 pPool->iFreeHead = pPage->idx;
3673 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
3674 Log(("pgmPoolAlloc: returns %Vrc (Insert)\n", rc3));
3675 return rc3;
3676 }
3677 rc = VERR_PGM_POOL_FLUSHED;
3678 }
3679#endif /* PGMPOOL_WITH_USER_TRACKING */
3680
3681 /*
3682 * Commit the allocation, clear the page and return.
3683 */
3684#ifdef VBOX_WITH_STATISTICS
3685 if (pPool->cUsedPages > pPool->cUsedPagesHigh)
3686 pPool->cUsedPagesHigh = pPool->cUsedPages;
3687#endif
3688
3689 if (!pPage->fZeroed)
3690 {
3691 STAM_PROFILE_START(&pPool->StatZeroPage, z);
3692 void *pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
3693 ASMMemZeroPage(pv);
3694 STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
3695 }
3696
3697 *ppPage = pPage;
3698 LogFlow(("pgmPoolAlloc: returns %Vrc *ppPage=%p:{.Key=%VHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
3699 rc, pPage, pPage->Core.Key, pPage->idx, pPage->fCached, pPage->fMonitored));
3700 STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
3701 return rc;
3702}
3703
3704
3705/**
3706 * Frees a usage of a pool page.
3707 *
3708 * @param pVM The VM handle.
3709 * @param HCPhys The HC physical address of the shadow page.
3710 * @param iUser The shadow page pool index of the user table.
3711 * @param iUserTable The index into the user table (shadowed).
3712 */
3713void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable)
3714{
3715 LogFlow(("pgmPoolFree: HCPhys=%VHp iUser=%#x iUserTable=%#x\n", HCPhys, iUser, iUserTable));
3716 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
3717 pgmPoolFreeByPage(pPool, pgmPoolGetPage(pPool, HCPhys), iUser, iUserTable);
3718}
3719
3720
3721/**
3722 * Gets a in-use page in the pool by it's physical address.
3723 *
3724 * @returns Pointer to the page.
3725 * @param pVM The VM handle.
3726 * @param HCPhys The HC physical address of the shadow page.
3727 * @remark This function will NEVER return NULL. It will assert if HCPhys is invalid.
3728 */
3729PPGMPOOLPAGE pgmPoolGetPageByHCPhys(PVM pVM, RTHCPHYS HCPhys)
3730{
3731 /** @todo profile this! */
3732 PPGMPOOL pPool = pVM->pgm.s.CTXSUFF(pPool);
3733 PPGMPOOLPAGE pPage = pgmPoolGetPage(pPool, HCPhys);
3734 Log3(("pgmPoolGetPageByHCPhys: HCPhys=%VHp -> %p:{.idx=%d .GCPhys=%VGp .enmKind=%d}\n",
3735 HCPhys, pPage, pPage->idx, pPage->GCPhys, pPage->enmKind));
3736 return pPage;
3737}
3738
3739
3740/**
3741 * Flushes the entire cache.
3742 *
3743 * It will assert a global CR3 flush (FF) and assumes the caller is aware of this
3744 * and execute this CR3 flush.
3745 *
3746 * @param pPool The pool.
3747 */
3748void pgmPoolFlushAll(PVM pVM)
3749{
3750 LogFlow(("pgmPoolFlushAll:\n"));
3751 pgmPoolFlushAllInt(pVM->pgm.s.CTXSUFF(pPool));
3752}
3753
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