VirtualBox

source: vbox/trunk/src/VBox/VMM/PGMInternal.h@ 20060

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

Atomic updates of lock counters

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 185.7 KB
Line 
1/* $Id: PGMInternal.h 20060 2009-05-27 09:38:46Z vboxsync $ */
2/** @file
3 * PGM - Internal header file.
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#ifndef ___PGMInternal_h
23#define ___PGMInternal_h
24
25#include <VBox/cdefs.h>
26#include <VBox/types.h>
27#include <VBox/err.h>
28#include <VBox/stam.h>
29#include <VBox/param.h>
30#include <VBox/vmm.h>
31#include <VBox/mm.h>
32#include <VBox/pdmcritsect.h>
33#include <VBox/pdmapi.h>
34#include <VBox/dis.h>
35#include <VBox/dbgf.h>
36#include <VBox/log.h>
37#include <VBox/gmm.h>
38#include <VBox/hwaccm.h>
39#include <iprt/avl.h>
40#include <iprt/asm.h>
41#include <iprt/assert.h>
42#include <iprt/critsect.h>
43
44
45
46/** @defgroup grp_pgm_int Internals
47 * @ingroup grp_pgm
48 * @internal
49 * @{
50 */
51
52
53/** @name PGM Compile Time Config
54 * @{
55 */
56
57/**
58 * Solve page is out of sync issues inside Guest Context (in PGMGC.cpp).
59 * Comment it if it will break something.
60 */
61#define PGM_OUT_OF_SYNC_IN_GC
62
63/**
64 * Check and skip global PDEs for non-global flushes
65 */
66#define PGM_SKIP_GLOBAL_PAGEDIRS_ON_NONGLOBAL_FLUSH
67
68/**
69 * Sync N pages instead of a whole page table
70 */
71#define PGM_SYNC_N_PAGES
72
73/**
74 * Number of pages to sync during a page fault
75 *
76 * When PGMPOOL_WITH_GCPHYS_TRACKING is enabled using high values here
77 * causes a lot of unnecessary extents and also is slower than taking more \#PFs.
78 */
79#define PGM_SYNC_NR_PAGES 8
80
81/**
82 * Number of PGMPhysRead/Write cache entries (must be <= sizeof(uint64_t))
83 */
84#define PGM_MAX_PHYSCACHE_ENTRIES 64
85#define PGM_MAX_PHYSCACHE_ENTRIES_MASK (PGM_MAX_PHYSCACHE_ENTRIES-1)
86
87/**
88 * Enable caching of PGMR3PhysRead/WriteByte/Word/Dword
89 */
90#define PGM_PHYSMEMACCESS_CACHING
91
92/** @def PGMPOOL_WITH_CACHE
93 * Enable agressive caching using the page pool.
94 *
95 * This requires PGMPOOL_WITH_USER_TRACKING and PGMPOOL_WITH_MONITORING.
96 */
97#define PGMPOOL_WITH_CACHE
98
99/** @def PGMPOOL_WITH_MIXED_PT_CR3
100 * When defined, we'll deal with 'uncachable' pages.
101 */
102#ifdef PGMPOOL_WITH_CACHE
103# define PGMPOOL_WITH_MIXED_PT_CR3
104#endif
105
106/** @def PGMPOOL_WITH_MONITORING
107 * Monitor the guest pages which are shadowed.
108 * When this is enabled, PGMPOOL_WITH_CACHE or PGMPOOL_WITH_GCPHYS_TRACKING must
109 * be enabled as well.
110 * @remark doesn't really work without caching now. (Mixed PT/CR3 change.)
111 */
112#ifdef PGMPOOL_WITH_CACHE
113# define PGMPOOL_WITH_MONITORING
114#endif
115
116/** @def PGMPOOL_WITH_GCPHYS_TRACKING
117 * Tracking the of shadow pages mapping guest physical pages.
118 *
119 * This is very expensive, the current cache prototype is trying to figure out
120 * whether it will be acceptable with an agressive caching policy.
121 */
122#if defined(PGMPOOL_WITH_CACHE) || defined(PGMPOOL_WITH_MONITORING)
123# define PGMPOOL_WITH_GCPHYS_TRACKING
124#endif
125
126/** @def PGMPOOL_WITH_USER_TRACKING
127 * Tracking users of shadow pages. This is required for the linking of shadow page
128 * tables and physical guest addresses.
129 */
130#if defined(PGMPOOL_WITH_GCPHYS_TRACKING) || defined(PGMPOOL_WITH_CACHE) || defined(PGMPOOL_WITH_MONITORING)
131# define PGMPOOL_WITH_USER_TRACKING
132#endif
133
134/** @def PGMPOOL_CFG_MAX_GROW
135 * The maximum number of pages to add to the pool in one go.
136 */
137#define PGMPOOL_CFG_MAX_GROW (_256K >> PAGE_SHIFT)
138
139/** @def VBOX_STRICT_PGM_HANDLER_VIRTUAL
140 * Enables some extra assertions for virtual handlers (mainly phys2virt related).
141 */
142#ifdef VBOX_STRICT
143# define VBOX_STRICT_PGM_HANDLER_VIRTUAL
144#endif
145
146/** @def VBOX_WITH_NEW_LAZY_PAGE_ALLOC
147 * Enables the experimental lazy page allocation code. */
148/*# define VBOX_WITH_NEW_LAZY_PAGE_ALLOC */
149
150/** @} */
151
152
153/** @name PDPT and PML4 flags.
154 * These are placed in the three bits available for system programs in
155 * the PDPT and PML4 entries.
156 * @{ */
157/** The entry is a permanent one and it's must always be present.
158 * Never free such an entry. */
159#define PGM_PLXFLAGS_PERMANENT RT_BIT_64(10)
160/** Mapping (hypervisor allocated pagetable). */
161#define PGM_PLXFLAGS_MAPPING RT_BIT_64(11)
162/** @} */
163
164/** @name Page directory flags.
165 * These are placed in the three bits available for system programs in
166 * the page directory entries.
167 * @{ */
168/** Mapping (hypervisor allocated pagetable). */
169#define PGM_PDFLAGS_MAPPING RT_BIT_64(10)
170/** Made read-only to facilitate dirty bit tracking. */
171#define PGM_PDFLAGS_TRACK_DIRTY RT_BIT_64(11)
172/** @} */
173
174/** @name Page flags.
175 * These are placed in the three bits available for system programs in
176 * the page entries.
177 * @{ */
178/** Made read-only to facilitate dirty bit tracking. */
179#define PGM_PTFLAGS_TRACK_DIRTY RT_BIT_64(9)
180
181#ifndef PGM_PTFLAGS_CSAM_VALIDATED
182/** Scanned and approved by CSAM (tm).
183 * NOTE: Must be identical to the one defined in CSAMInternal.h!!
184 * @todo Move PGM_PTFLAGS_* and PGM_PDFLAGS_* to VBox/pgm.h. */
185#define PGM_PTFLAGS_CSAM_VALIDATED RT_BIT_64(11)
186#endif
187
188/** @} */
189
190/** @name Defines used to indicate the shadow and guest paging in the templates.
191 * @{ */
192#define PGM_TYPE_REAL 1
193#define PGM_TYPE_PROT 2
194#define PGM_TYPE_32BIT 3
195#define PGM_TYPE_PAE 4
196#define PGM_TYPE_AMD64 5
197#define PGM_TYPE_NESTED 6
198#define PGM_TYPE_EPT 7
199#define PGM_TYPE_MAX PGM_TYPE_EPT
200/** @} */
201
202/** Macro for checking if the guest is using paging.
203 * @param uGstType PGM_TYPE_*
204 * @param uShwType PGM_TYPE_*
205 * @remark ASSUMES certain order of the PGM_TYPE_* values.
206 */
207#define PGM_WITH_PAGING(uGstType, uShwType) \
208 ( (uGstType) >= PGM_TYPE_32BIT \
209 && (uShwType) != PGM_TYPE_NESTED \
210 && (uShwType) != PGM_TYPE_EPT)
211
212/** Macro for checking if the guest supports the NX bit.
213 * @param uGstType PGM_TYPE_*
214 * @param uShwType PGM_TYPE_*
215 * @remark ASSUMES certain order of the PGM_TYPE_* values.
216 */
217#define PGM_WITH_NX(uGstType, uShwType) \
218 ( (uGstType) >= PGM_TYPE_PAE \
219 && (uShwType) != PGM_TYPE_NESTED \
220 && (uShwType) != PGM_TYPE_EPT)
221
222
223/** @def PGM_HCPHYS_2_PTR
224 * Maps a HC physical page pool address to a virtual address.
225 *
226 * @returns VBox status code.
227 * @param pVM The VM handle.
228 * @param HCPhys The HC physical address to map to a virtual one.
229 * @param ppv Where to store the virtual address. No need to cast this.
230 *
231 * @remark In GC this uses PGMGCDynMapHCPage(), so it will consume of the
232 * small page window employeed by that function. Be careful.
233 * @remark There is no need to assert on the result.
234 */
235#ifdef IN_RC
236# define PGM_HCPHYS_2_PTR(pVM, HCPhys, ppv) \
237 PGMDynMapHCPage(pVM, HCPhys, (void **)(ppv))
238#elif defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
239# define PGM_HCPHYS_2_PTR(pVM, HCPhys, ppv) \
240 pgmR0DynMapHCPageInlined(&(pVM)->pgm.s, HCPhys, (void **)(ppv))
241#else
242# define PGM_HCPHYS_2_PTR(pVM, HCPhys, ppv) \
243 MMPagePhys2PageEx(pVM, HCPhys, (void **)(ppv))
244#endif
245
246/** @def PGM_HCPHYS_2_PTR_BY_PGM
247 * Maps a HC physical page pool address to a virtual address.
248 *
249 * @returns VBox status code.
250 * @param pPGM The PGM instance data.
251 * @param HCPhys The HC physical address to map to a virtual one.
252 * @param ppv Where to store the virtual address. No need to cast this.
253 *
254 * @remark In GC this uses PGMGCDynMapHCPage(), so it will consume of the
255 * small page window employeed by that function. Be careful.
256 * @remark There is no need to assert on the result.
257 */
258#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
259# define PGM_HCPHYS_2_PTR_BY_PGM(pPGM, HCPhys, ppv) \
260 pgmR0DynMapHCPageInlined(pPGM, HCPhys, (void **)(ppv))
261#else
262# define PGM_HCPHYS_2_PTR_BY_PGM(pPGM, HCPhys, ppv) \
263 PGM_HCPHYS_2_PTR(PGM2VM(pPGM), HCPhys, (void **)(ppv))
264#endif
265
266/** @def PGM_GCPHYS_2_PTR
267 * Maps a GC physical page address to a virtual address.
268 *
269 * @returns VBox status code.
270 * @param pVM The VM handle.
271 * @param GCPhys The GC physical address to map to a virtual one.
272 * @param ppv Where to store the virtual address. No need to cast this.
273 *
274 * @remark In GC this uses PGMGCDynMapGCPage(), so it will consume of the
275 * small page window employeed by that function. Be careful.
276 * @remark There is no need to assert on the result.
277 */
278#ifdef IN_RC
279# define PGM_GCPHYS_2_PTR(pVM, GCPhys, ppv) \
280 PGMDynMapGCPage(pVM, GCPhys, (void **)(ppv))
281#elif defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
282# define PGM_GCPHYS_2_PTR(pVM, GCPhys, ppv) \
283 pgmR0DynMapGCPageInlined(&(pVM)->pgm.s, GCPhys, (void **)(ppv))
284#else
285# define PGM_GCPHYS_2_PTR(pVM, GCPhys, ppv) \
286 PGMPhysGCPhys2R3Ptr(pVM, GCPhys, 1 /* one page only */, (PRTR3PTR)(ppv)) /** @todo this isn't asserting, use PGMRamGCPhys2HCPtr! */
287#endif
288
289/** @def PGM_GCPHYS_2_PTR_BY_PGMCPU
290 * Maps a GC physical page address to a virtual address.
291 *
292 * @returns VBox status code.
293 * @param pPGM Pointer to the PGM instance data.
294 * @param GCPhys The GC physical address to map to a virtual one.
295 * @param ppv Where to store the virtual address. No need to cast this.
296 *
297 * @remark In GC this uses PGMGCDynMapGCPage(), so it will consume of the
298 * small page window employeed by that function. Be careful.
299 * @remark There is no need to assert on the result.
300 */
301#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
302# define PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, GCPhys, ppv) \
303 pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), GCPhys, (void **)(ppv))
304#else
305# define PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, GCPhys, ppv) \
306 PGM_GCPHYS_2_PTR(PGMCPU2VM(pPGM), GCPhys, ppv)
307#endif
308
309/** @def PGM_GCPHYS_2_PTR_EX
310 * Maps a unaligned GC physical page address to a virtual address.
311 *
312 * @returns VBox status code.
313 * @param pVM The VM handle.
314 * @param GCPhys The GC physical address to map to a virtual one.
315 * @param ppv Where to store the virtual address. No need to cast this.
316 *
317 * @remark In GC this uses PGMGCDynMapGCPage(), so it will consume of the
318 * small page window employeed by that function. Be careful.
319 * @remark There is no need to assert on the result.
320 */
321#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
322# define PGM_GCPHYS_2_PTR_EX(pVM, GCPhys, ppv) \
323 PGMDynMapGCPageOff(pVM, GCPhys, (void **)(ppv))
324#else
325# define PGM_GCPHYS_2_PTR_EX(pVM, GCPhys, ppv) \
326 PGMPhysGCPhys2R3Ptr(pVM, GCPhys, 1 /* one page only */, (PRTR3PTR)(ppv)) /** @todo this isn't asserting, use PGMRamGCPhys2HCPtr! */
327#endif
328
329/** @def PGM_INVL_PG
330 * Invalidates a page.
331 *
332 * @param pVCpu The VMCPU handle.
333 * @param GCVirt The virtual address of the page to invalidate.
334 */
335#ifdef IN_RC
336# define PGM_INVL_PG(pVCpu, GCVirt) ASMInvalidatePage((void *)(GCVirt))
337#elif defined(IN_RING0)
338# define PGM_INVL_PG(pVCpu, GCVirt) HWACCMInvalidatePage(pVCpu, (RTGCPTR)(GCVirt))
339#else
340# define PGM_INVL_PG(pVCpu, GCVirt) HWACCMInvalidatePage(pVCpu, (RTGCPTR)(GCVirt))
341#endif
342
343/** @def PGM_INVL_PG
344 * Invalidates a page on all VCPUs
345 *
346 * @param pVM The VM handle.
347 * @param GCVirt The virtual address of the page to invalidate.
348 */
349#ifdef IN_RC
350# define PGM_INVL_ALL_VCPU_PG(pVM, GCVirt) ASMInvalidatePage((void *)(GCVirt))
351#elif defined(IN_RING0)
352# define PGM_INVL_ALL_VCPU_PG(pVM, GCVirt) HWACCMInvalidatePageOnAllVCpus(pVM, (RTGCPTR)(GCVirt))
353#else
354# define PGM_INVL_ALL_VCPU_PG(pVM, GCVirt) HWACCMInvalidatePageOnAllVCpus(pVM, (RTGCPTR)(GCVirt))
355#endif
356
357/** @def PGM_INVL_BIG_PG
358 * Invalidates a 4MB page directory entry.
359 *
360 * @param pVCpu The VMCPU handle.
361 * @param GCVirt The virtual address within the page directory to invalidate.
362 */
363#ifdef IN_RC
364# define PGM_INVL_BIG_PG(pVCpu, GCVirt) ASMReloadCR3()
365#elif defined(IN_RING0)
366# define PGM_INVL_BIG_PG(pVCpu, GCVirt) HWACCMFlushTLB(pVCpu)
367#else
368# define PGM_INVL_BIG_PG(pVCpu, GCVirt) HWACCMFlushTLB(pVCpu)
369#endif
370
371/** @def PGM_INVL_VCPU_TLBS()
372 * Invalidates the TLBs of the specified VCPU
373 *
374 * @param pVCpu The VMCPU handle.
375 */
376#ifdef IN_RC
377# define PGM_INVL_VCPU_TLBS(pVCpu) ASMReloadCR3()
378#elif defined(IN_RING0)
379# define PGM_INVL_VCPU_TLBS(pVCpu) HWACCMFlushTLB(pVCpu)
380#else
381# define PGM_INVL_VCPU_TLBS(pVCpu) HWACCMFlushTLB(pVCpu)
382#endif
383
384/** @def PGM_INVL_ALL_VCPU_TLBS()
385 * Invalidates the TLBs of all VCPUs
386 *
387 * @param pVM The VM handle.
388 */
389#ifdef IN_RC
390# define PGM_INVL_ALL_VCPU_TLBS(pVM) ASMReloadCR3()
391#elif defined(IN_RING0)
392# define PGM_INVL_ALL_VCPU_TLBS(pVM) HWACCMFlushTLBOnAllVCpus(pVM)
393#else
394# define PGM_INVL_ALL_VCPU_TLBS(pVM) HWACCMFlushTLBOnAllVCpus(pVM)
395#endif
396
397/** Size of the GCPtrConflict array in PGMMAPPING.
398 * @remarks Must be a power of two. */
399#define PGMMAPPING_CONFLICT_MAX 8
400
401/**
402 * Structure for tracking GC Mappings.
403 *
404 * This structure is used by linked list in both GC and HC.
405 */
406typedef struct PGMMAPPING
407{
408 /** Pointer to next entry. */
409 R3PTRTYPE(struct PGMMAPPING *) pNextR3;
410 /** Pointer to next entry. */
411 R0PTRTYPE(struct PGMMAPPING *) pNextR0;
412 /** Pointer to next entry. */
413 RCPTRTYPE(struct PGMMAPPING *) pNextRC;
414 /** Indicate whether this entry is finalized. */
415 bool fFinalized;
416 /** Start Virtual address. */
417 RTGCPTR GCPtr;
418 /** Last Virtual address (inclusive). */
419 RTGCPTR GCPtrLast;
420 /** Range size (bytes). */
421 RTGCPTR cb;
422 /** Pointer to relocation callback function. */
423 R3PTRTYPE(PFNPGMRELOCATE) pfnRelocate;
424 /** User argument to the callback. */
425 R3PTRTYPE(void *) pvUser;
426 /** Mapping description / name. For easing debugging. */
427 R3PTRTYPE(const char *) pszDesc;
428 /** Last 8 addresses that caused conflicts. */
429 RTGCPTR aGCPtrConflicts[PGMMAPPING_CONFLICT_MAX];
430 /** Number of conflicts for this hypervisor mapping. */
431 uint32_t cConflicts;
432 /** Number of page tables. */
433 uint32_t cPTs;
434
435 /** Array of page table mapping data. Each entry
436 * describes one page table. The array can be longer
437 * than the declared length.
438 */
439 struct
440 {
441 /** The HC physical address of the page table. */
442 RTHCPHYS HCPhysPT;
443 /** The HC physical address of the first PAE page table. */
444 RTHCPHYS HCPhysPaePT0;
445 /** The HC physical address of the second PAE page table. */
446 RTHCPHYS HCPhysPaePT1;
447 /** The HC virtual address of the 32-bit page table. */
448 R3PTRTYPE(PX86PT) pPTR3;
449 /** The HC virtual address of the two PAE page table. (i.e 1024 entries instead of 512) */
450 R3PTRTYPE(PX86PTPAE) paPaePTsR3;
451 /** The GC virtual address of the 32-bit page table. */
452 RCPTRTYPE(PX86PT) pPTRC;
453 /** The GC virtual address of the two PAE page table. */
454 RCPTRTYPE(PX86PTPAE) paPaePTsRC;
455 /** The GC virtual address of the 32-bit page table. */
456 R0PTRTYPE(PX86PT) pPTR0;
457 /** The GC virtual address of the two PAE page table. */
458 R0PTRTYPE(PX86PTPAE) paPaePTsR0;
459 } aPTs[1];
460} PGMMAPPING;
461/** Pointer to structure for tracking GC Mappings. */
462typedef struct PGMMAPPING *PPGMMAPPING;
463
464
465/**
466 * Physical page access handler structure.
467 *
468 * This is used to keep track of physical address ranges
469 * which are being monitored in some kind of way.
470 */
471typedef struct PGMPHYSHANDLER
472{
473 AVLROGCPHYSNODECORE Core;
474 /** Access type. */
475 PGMPHYSHANDLERTYPE enmType;
476 /** Number of pages to update. */
477 uint32_t cPages;
478 /** Pointer to R3 callback function. */
479 R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3;
480 /** User argument for R3 handlers. */
481 R3PTRTYPE(void *) pvUserR3;
482 /** Pointer to R0 callback function. */
483 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0;
484 /** User argument for R0 handlers. */
485 R0PTRTYPE(void *) pvUserR0;
486 /** Pointer to GC callback function. */
487 RCPTRTYPE(PFNPGMRCPHYSHANDLER) pfnHandlerRC;
488 /** User argument for RC handlers. */
489 RCPTRTYPE(void *) pvUserRC;
490 /** Description / Name. For easing debugging. */
491 R3PTRTYPE(const char *) pszDesc;
492#ifdef VBOX_WITH_STATISTICS
493 /** Profiling of this handler. */
494 STAMPROFILE Stat;
495#endif
496} PGMPHYSHANDLER;
497/** Pointer to a physical page access handler structure. */
498typedef PGMPHYSHANDLER *PPGMPHYSHANDLER;
499
500
501/**
502 * Cache node for the physical addresses covered by a virtual handler.
503 */
504typedef struct PGMPHYS2VIRTHANDLER
505{
506 /** Core node for the tree based on physical ranges. */
507 AVLROGCPHYSNODECORE Core;
508 /** Offset from this struct to the PGMVIRTHANDLER structure. */
509 int32_t offVirtHandler;
510 /** Offset of the next alias relative to this one.
511 * Bit 0 is used for indicating whether we're in the tree.
512 * Bit 1 is used for indicating that we're the head node.
513 */
514 int32_t offNextAlias;
515} PGMPHYS2VIRTHANDLER;
516/** Pointer to a phys to virtual handler structure. */
517typedef PGMPHYS2VIRTHANDLER *PPGMPHYS2VIRTHANDLER;
518
519/** The bit in PGMPHYS2VIRTHANDLER::offNextAlias used to indicate that the
520 * node is in the tree. */
521#define PGMPHYS2VIRTHANDLER_IN_TREE RT_BIT(0)
522/** The bit in PGMPHYS2VIRTHANDLER::offNextAlias used to indicate that the
523 * node is in the head of an alias chain.
524 * The PGMPHYS2VIRTHANDLER_IN_TREE is always set if this bit is set. */
525#define PGMPHYS2VIRTHANDLER_IS_HEAD RT_BIT(1)
526/** The mask to apply to PGMPHYS2VIRTHANDLER::offNextAlias to get the offset. */
527#define PGMPHYS2VIRTHANDLER_OFF_MASK (~(int32_t)3)
528
529
530/**
531 * Virtual page access handler structure.
532 *
533 * This is used to keep track of virtual address ranges
534 * which are being monitored in some kind of way.
535 */
536typedef struct PGMVIRTHANDLER
537{
538 /** Core node for the tree based on virtual ranges. */
539 AVLROGCPTRNODECORE Core;
540 /** Size of the range (in bytes). */
541 RTGCPTR cb;
542 /** Number of cache pages. */
543 uint32_t cPages;
544 /** Access type. */
545 PGMVIRTHANDLERTYPE enmType;
546 /** Pointer to the RC callback function. */
547 RCPTRTYPE(PFNPGMRCVIRTHANDLER) pfnHandlerRC;
548#if HC_ARCH_BITS == 64
549 RTRCPTR padding;
550#endif
551 /** Pointer to the R3 callback function for invalidation. */
552 R3PTRTYPE(PFNPGMR3VIRTINVALIDATE) pfnInvalidateR3;
553 /** Pointer to the R3 callback function. */
554 R3PTRTYPE(PFNPGMR3VIRTHANDLER) pfnHandlerR3;
555 /** Description / Name. For easing debugging. */
556 R3PTRTYPE(const char *) pszDesc;
557#ifdef VBOX_WITH_STATISTICS
558 /** Profiling of this handler. */
559 STAMPROFILE Stat;
560#endif
561 /** Array of cached physical addresses for the monitored ranged. */
562 PGMPHYS2VIRTHANDLER aPhysToVirt[HC_ARCH_BITS == 32 ? 1 : 2];
563} PGMVIRTHANDLER;
564/** Pointer to a virtual page access handler structure. */
565typedef PGMVIRTHANDLER *PPGMVIRTHANDLER;
566
567
568/**
569 * Page type.
570 *
571 * @remarks This enum has to fit in a 3-bit field (see PGMPAGE::u3Type).
572 * @remarks This is used in the saved state, so changes to it requires bumping
573 * the saved state version.
574 * @todo So, convert to \#defines!
575 */
576typedef enum PGMPAGETYPE
577{
578 /** The usual invalid zero entry. */
579 PGMPAGETYPE_INVALID = 0,
580 /** RAM page. (RWX) */
581 PGMPAGETYPE_RAM,
582 /** MMIO2 page. (RWX) */
583 PGMPAGETYPE_MMIO2,
584 /** MMIO2 page aliased over an MMIO page. (RWX)
585 * See PGMHandlerPhysicalPageAlias(). */
586 PGMPAGETYPE_MMIO2_ALIAS_MMIO,
587 /** Shadowed ROM. (RWX) */
588 PGMPAGETYPE_ROM_SHADOW,
589 /** ROM page. (R-X) */
590 PGMPAGETYPE_ROM,
591 /** MMIO page. (---) */
592 PGMPAGETYPE_MMIO,
593 /** End of valid entries. */
594 PGMPAGETYPE_END
595} PGMPAGETYPE;
596AssertCompile(PGMPAGETYPE_END <= 7);
597
598/** @name Page type predicates.
599 * @{ */
600#define PGMPAGETYPE_IS_READABLE(type) ( (type) <= PGMPAGETYPE_ROM )
601#define PGMPAGETYPE_IS_WRITEABLE(type) ( (type) <= PGMPAGETYPE_ROM_SHADOW )
602#define PGMPAGETYPE_IS_RWX(type) ( (type) <= PGMPAGETYPE_ROM_SHADOW )
603#define PGMPAGETYPE_IS_ROX(type) ( (type) == PGMPAGETYPE_ROM )
604#define PGMPAGETYPE_IS_NP(type) ( (type) == PGMPAGETYPE_MMIO )
605/** @} */
606
607
608/**
609 * A Physical Guest Page tracking structure.
610 *
611 * The format of this structure is complicated because we have to fit a lot
612 * of information into as few bits as possible. The format is also subject
613 * to change (there is one comming up soon). Which means that for we'll be
614 * using PGM_PAGE_GET_*, PGM_PAGE_IS_ and PGM_PAGE_SET_* macros for *all*
615 * accessess to the structure.
616 */
617typedef struct PGMPAGE
618{
619 /** The physical address and a whole lot of other stuff. All bits are used! */
620 RTHCPHYS HCPhysX;
621 /** The page state. */
622 uint32_t u2StateX : 2;
623 /** Flag indicating that a write monitored page was written to when set. */
624 uint32_t fWrittenToX : 1;
625 /** For later. */
626 uint32_t fSomethingElse : 1;
627 /** The Page ID.
628 * @todo Merge with HCPhysX once we've liberated HCPhysX of its stuff.
629 * The HCPhysX will then be 100% static. */
630 uint32_t idPageX : 28;
631 /** The page type (PGMPAGETYPE). */
632 uint32_t u3Type : 3;
633 /** The physical handler state (PGM_PAGE_HNDL_PHYS_STATE*) */
634 uint32_t u2HandlerPhysStateX : 2;
635 /** The virtual handler state (PGM_PAGE_HNDL_VIRT_STATE*) */
636 uint32_t u2HandlerVirtStateX : 2;
637 uint32_t u29B : 25;
638} PGMPAGE;
639AssertCompileSize(PGMPAGE, 16);
640/** Pointer to a physical guest page. */
641typedef PGMPAGE *PPGMPAGE;
642/** Pointer to a const physical guest page. */
643typedef const PGMPAGE *PCPGMPAGE;
644/** Pointer to a physical guest page pointer. */
645typedef PPGMPAGE *PPPGMPAGE;
646
647
648/**
649 * Clears the page structure.
650 * @param pPage Pointer to the physical guest page tracking structure.
651 */
652#define PGM_PAGE_CLEAR(pPage) \
653 do { \
654 (pPage)->HCPhysX = 0; \
655 (pPage)->u2StateX = 0; \
656 (pPage)->fWrittenToX = 0; \
657 (pPage)->fSomethingElse = 0; \
658 (pPage)->idPageX = 0; \
659 (pPage)->u3Type = 0; \
660 (pPage)->u29B = 0; \
661 } while (0)
662
663/**
664 * Initializes the page structure.
665 * @param pPage Pointer to the physical guest page tracking structure.
666 */
667#define PGM_PAGE_INIT(pPage, _HCPhys, _idPage, _uType, _uState) \
668 do { \
669 (pPage)->HCPhysX = (_HCPhys); \
670 (pPage)->u2StateX = (_uState); \
671 (pPage)->fWrittenToX = 0; \
672 (pPage)->fSomethingElse = 0; \
673 (pPage)->idPageX = (_idPage); \
674 /*(pPage)->u3Type = (_uType); - later */ \
675 PGM_PAGE_SET_TYPE(pPage, _uType); \
676 (pPage)->u29B = 0; \
677 } while (0)
678
679/**
680 * Initializes the page structure of a ZERO page.
681 * @param pPage Pointer to the physical guest page tracking structure.
682 */
683#define PGM_PAGE_INIT_ZERO(pPage, pVM, _uType) \
684 PGM_PAGE_INIT(pPage, (pVM)->pgm.s.HCPhysZeroPg, NIL_GMM_PAGEID, (_uType), PGM_PAGE_STATE_ZERO)
685/** Temporary hack. Replaced by PGM_PAGE_INIT_ZERO once the old code is kicked out. */
686# define PGM_PAGE_INIT_ZERO_REAL(pPage, pVM, _uType) \
687 PGM_PAGE_INIT(pPage, (pVM)->pgm.s.HCPhysZeroPg, NIL_GMM_PAGEID, (_uType), PGM_PAGE_STATE_ZERO)
688
689
690/** @name The Page state, PGMPAGE::u2StateX.
691 * @{ */
692/** The zero page.
693 * This is a per-VM page that's never ever mapped writable. */
694#define PGM_PAGE_STATE_ZERO 0
695/** A allocated page.
696 * This is a per-VM page allocated from the page pool (or wherever
697 * we get MMIO2 pages from if the type is MMIO2).
698 */
699#define PGM_PAGE_STATE_ALLOCATED 1
700/** A allocated page that's being monitored for writes.
701 * The shadow page table mappings are read-only. When a write occurs, the
702 * fWrittenTo member is set, the page remapped as read-write and the state
703 * moved back to allocated. */
704#define PGM_PAGE_STATE_WRITE_MONITORED 2
705/** The page is shared, aka. copy-on-write.
706 * This is a page that's shared with other VMs. */
707#define PGM_PAGE_STATE_SHARED 3
708/** @} */
709
710
711/**
712 * Gets the page state.
713 * @returns page state (PGM_PAGE_STATE_*).
714 * @param pPage Pointer to the physical guest page tracking structure.
715 */
716#define PGM_PAGE_GET_STATE(pPage) ( (pPage)->u2StateX )
717
718/**
719 * Sets the page state.
720 * @param pPage Pointer to the physical guest page tracking structure.
721 * @param _uState The new page state.
722 */
723#define PGM_PAGE_SET_STATE(pPage, _uState) \
724 do { (pPage)->u2StateX = (_uState); } while (0)
725
726
727/**
728 * Gets the host physical address of the guest page.
729 * @returns host physical address (RTHCPHYS).
730 * @param pPage Pointer to the physical guest page tracking structure.
731 */
732#define PGM_PAGE_GET_HCPHYS(pPage) ( (pPage)->HCPhysX & UINT64_C(0x0000fffffffff000) )
733
734/**
735 * Sets the host physical address of the guest page.
736 * @param pPage Pointer to the physical guest page tracking structure.
737 * @param _HCPhys The new host physical address.
738 */
739#define PGM_PAGE_SET_HCPHYS(pPage, _HCPhys) \
740 do { (pPage)->HCPhysX = (((pPage)->HCPhysX) & UINT64_C(0xffff000000000fff)) \
741 | ((_HCPhys) & UINT64_C(0x0000fffffffff000)); } while (0)
742
743/**
744 * Get the Page ID.
745 * @returns The Page ID; NIL_GMM_PAGEID if it's a ZERO page.
746 * @param pPage Pointer to the physical guest page tracking structure.
747 */
748#define PGM_PAGE_GET_PAGEID(pPage) ( (pPage)->idPageX )
749/* later:
750#define PGM_PAGE_GET_PAGEID(pPage) ( ((uint32_t)(pPage)->HCPhysX >> (48 - 12))
751 | ((uint32_t)(pPage)->HCPhysX & 0xfff) )
752*/
753/**
754 * Sets the Page ID.
755 * @param pPage Pointer to the physical guest page tracking structure.
756 */
757#define PGM_PAGE_SET_PAGEID(pPage, _idPage) do { (pPage)->idPageX = (_idPage); } while (0)
758/* later:
759#define PGM_PAGE_SET_PAGEID(pPage, _idPage) do { (pPage)->HCPhysX = (((pPage)->HCPhysX) & UINT64_C(0x0000fffffffff000)) \
760 | ((_idPage) & 0xfff) \
761 | (((_idPage) & 0x0ffff000) << (48-12)); } while (0)
762*/
763
764/**
765 * Get the Chunk ID.
766 * @returns The Chunk ID; NIL_GMM_CHUNKID if it's a ZERO page.
767 * @param pPage Pointer to the physical guest page tracking structure.
768 */
769#define PGM_PAGE_GET_CHUNKID(pPage) ( (pPage)->idPageX >> GMM_CHUNKID_SHIFT )
770/* later:
771#if GMM_CHUNKID_SHIFT == 12
772# define PGM_PAGE_GET_CHUNKID(pPage) ( (uint32_t)((pPage)->HCPhysX >> 48) )
773#elif GMM_CHUNKID_SHIFT > 12
774# define PGM_PAGE_GET_CHUNKID(pPage) ( (uint32_t)((pPage)->HCPhysX >> (48 + (GMM_CHUNKID_SHIFT - 12)) )
775#elif GMM_CHUNKID_SHIFT < 12
776# define PGM_PAGE_GET_CHUNKID(pPage) ( ( (uint32_t)((pPage)->HCPhysX >> 48) << (12 - GMM_CHUNKID_SHIFT) ) \
777 | ( (uint32_t)((pPage)->HCPhysX & 0xfff) >> GMM_CHUNKID_SHIFT ) )
778#else
779# error "GMM_CHUNKID_SHIFT isn't defined or something."
780#endif
781*/
782
783/**
784 * Get the index of the page within the allocaiton chunk.
785 * @returns The page index.
786 * @param pPage Pointer to the physical guest page tracking structure.
787 */
788#define PGM_PAGE_GET_PAGE_IN_CHUNK(pPage) ( (pPage)->idPageX & GMM_PAGEID_IDX_MASK )
789/* later:
790#if GMM_CHUNKID_SHIFT <= 12
791# define PGM_PAGE_GET_PAGE_IN_CHUNK(pPage) ( (uint32_t)((pPage)->HCPhysX & GMM_PAGEID_IDX_MASK) )
792#else
793# define PGM_PAGE_GET_PAGE_IN_CHUNK(pPage) ( (uint32_t)((pPage)->HCPhysX & 0xfff) \
794 | ( (uint32_t)((pPage)->HCPhysX >> 48) & (RT_BIT_32(GMM_CHUNKID_SHIFT - 12) - 1) ) )
795#endif
796*/
797
798
799/**
800 * Gets the page type.
801 * @returns The page type.
802 * @param pPage Pointer to the physical guest page tracking structure.
803 */
804#define PGM_PAGE_GET_TYPE(pPage) (pPage)->u3Type
805
806/**
807 * Sets the page type.
808 * @param pPage Pointer to the physical guest page tracking structure.
809 * @param _enmType The new page type (PGMPAGETYPE).
810 */
811#define PGM_PAGE_SET_TYPE(pPage, _enmType) \
812 do { (pPage)->u3Type = (_enmType); } while (0)
813
814/**
815 * Checks if the page is marked for MMIO.
816 * @returns true/false.
817 * @param pPage Pointer to the physical guest page tracking structure.
818 */
819#define PGM_PAGE_IS_MMIO(pPage) ( (pPage)->u3Type == PGMPAGETYPE_MMIO )
820
821/**
822 * Checks if the page is backed by the ZERO page.
823 * @returns true/false.
824 * @param pPage Pointer to the physical guest page tracking structure.
825 */
826#define PGM_PAGE_IS_ZERO(pPage) ( (pPage)->u2StateX == PGM_PAGE_STATE_ZERO )
827
828/**
829 * Checks if the page is backed by a SHARED page.
830 * @returns true/false.
831 * @param pPage Pointer to the physical guest page tracking structure.
832 */
833#define PGM_PAGE_IS_SHARED(pPage) ( (pPage)->u2StateX == PGM_PAGE_STATE_SHARED )
834
835
836/**
837 * Marks the paget as written to (for GMM change monitoring).
838 * @param pPage Pointer to the physical guest page tracking structure.
839 */
840#define PGM_PAGE_SET_WRITTEN_TO(pPage) do { (pPage)->fWrittenToX = 1; } while (0)
841
842/**
843 * Clears the written-to indicator.
844 * @param pPage Pointer to the physical guest page tracking structure.
845 */
846#define PGM_PAGE_CLEAR_WRITTEN_TO(pPage) do { (pPage)->fWrittenToX = 0; } while (0)
847
848/**
849 * Checks if the page was marked as written-to.
850 * @returns true/false.
851 * @param pPage Pointer to the physical guest page tracking structure.
852 */
853#define PGM_PAGE_IS_WRITTEN_TO(pPage) ( (pPage)->fWrittenToX )
854
855
856/** @name Physical Access Handler State values (PGMPAGE::u2HandlerPhysStateX).
857 *
858 * @remarks The values are assigned in order of priority, so we can calculate
859 * the correct state for a page with different handlers installed.
860 * @{ */
861/** No handler installed. */
862#define PGM_PAGE_HNDL_PHYS_STATE_NONE 0
863/** Monitoring is temporarily disabled. */
864#define PGM_PAGE_HNDL_PHYS_STATE_DISABLED 1
865/** Write access is monitored. */
866#define PGM_PAGE_HNDL_PHYS_STATE_WRITE 2
867/** All access is monitored. */
868#define PGM_PAGE_HNDL_PHYS_STATE_ALL 3
869/** @} */
870
871/**
872 * Gets the physical access handler state of a page.
873 * @returns PGM_PAGE_HNDL_PHYS_STATE_* value.
874 * @param pPage Pointer to the physical guest page tracking structure.
875 */
876#define PGM_PAGE_GET_HNDL_PHYS_STATE(pPage) ( (pPage)->u2HandlerPhysStateX )
877
878/**
879 * Sets the physical access handler state of a page.
880 * @param pPage Pointer to the physical guest page tracking structure.
881 * @param _uState The new state value.
882 */
883#define PGM_PAGE_SET_HNDL_PHYS_STATE(pPage, _uState) \
884 do { (pPage)->u2HandlerPhysStateX = (_uState); } while (0)
885
886/**
887 * Checks if the page has any physical access handlers, including temporariliy disabled ones.
888 * @returns true/false
889 * @param pPage Pointer to the physical guest page tracking structure.
890 */
891#define PGM_PAGE_HAS_ANY_PHYSICAL_HANDLERS(pPage) ( (pPage)->u2HandlerPhysStateX != PGM_PAGE_HNDL_PHYS_STATE_NONE )
892
893/**
894 * Checks if the page has any active physical access handlers.
895 * @returns true/false
896 * @param pPage Pointer to the physical guest page tracking structure.
897 */
898#define PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(pPage) ( (pPage)->u2HandlerPhysStateX >= PGM_PAGE_HNDL_PHYS_STATE_WRITE )
899
900
901/** @name Virtual Access Handler State values (PGMPAGE::u2HandlerVirtStateX).
902 *
903 * @remarks The values are assigned in order of priority, so we can calculate
904 * the correct state for a page with different handlers installed.
905 * @{ */
906/** No handler installed. */
907#define PGM_PAGE_HNDL_VIRT_STATE_NONE 0
908/* 1 is reserved so the lineup is identical with the physical ones. */
909/** Write access is monitored. */
910#define PGM_PAGE_HNDL_VIRT_STATE_WRITE 2
911/** All access is monitored. */
912#define PGM_PAGE_HNDL_VIRT_STATE_ALL 3
913/** @} */
914
915/**
916 * Gets the virtual access handler state of a page.
917 * @returns PGM_PAGE_HNDL_VIRT_STATE_* value.
918 * @param pPage Pointer to the physical guest page tracking structure.
919 */
920#define PGM_PAGE_GET_HNDL_VIRT_STATE(pPage) ( (pPage)->u2HandlerVirtStateX )
921
922/**
923 * Sets the virtual access handler state of a page.
924 * @param pPage Pointer to the physical guest page tracking structure.
925 * @param _uState The new state value.
926 */
927#define PGM_PAGE_SET_HNDL_VIRT_STATE(pPage, _uState) \
928 do { (pPage)->u2HandlerVirtStateX = (_uState); } while (0)
929
930/**
931 * Checks if the page has any virtual access handlers.
932 * @returns true/false
933 * @param pPage Pointer to the physical guest page tracking structure.
934 */
935#define PGM_PAGE_HAS_ANY_VIRTUAL_HANDLERS(pPage) ( (pPage)->u2HandlerVirtStateX != PGM_PAGE_HNDL_VIRT_STATE_NONE )
936
937/**
938 * Same as PGM_PAGE_HAS_ANY_VIRTUAL_HANDLERS - can't disable pages in
939 * virtual handlers.
940 * @returns true/false
941 * @param pPage Pointer to the physical guest page tracking structure.
942 */
943#define PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(pPage) PGM_PAGE_HAS_ANY_VIRTUAL_HANDLERS(pPage)
944
945
946
947/**
948 * Checks if the page has any access handlers, including temporarily disabled ones.
949 * @returns true/false
950 * @param pPage Pointer to the physical guest page tracking structure.
951 */
952#define PGM_PAGE_HAS_ANY_HANDLERS(pPage) \
953 ( (pPage)->u2HandlerPhysStateX != PGM_PAGE_HNDL_PHYS_STATE_NONE \
954 || (pPage)->u2HandlerVirtStateX != PGM_PAGE_HNDL_VIRT_STATE_NONE )
955
956/**
957 * Checks if the page has any active access handlers.
958 * @returns true/false
959 * @param pPage Pointer to the physical guest page tracking structure.
960 */
961#define PGM_PAGE_HAS_ACTIVE_HANDLERS(pPage) \
962 ( (pPage)->u2HandlerPhysStateX >= PGM_PAGE_HNDL_PHYS_STATE_WRITE \
963 || (pPage)->u2HandlerVirtStateX >= PGM_PAGE_HNDL_VIRT_STATE_WRITE )
964
965/**
966 * Checks if the page has any active access handlers catching all accesses.
967 * @returns true/false
968 * @param pPage Pointer to the physical guest page tracking structure.
969 */
970#define PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(pPage) \
971 ( (pPage)->u2HandlerPhysStateX == PGM_PAGE_HNDL_PHYS_STATE_ALL \
972 || (pPage)->u2HandlerVirtStateX == PGM_PAGE_HNDL_VIRT_STATE_ALL )
973
974
975
976
977/** @def PGM_PAGE_GET_TRACKING
978 * Gets the packed shadow page pool tracking data associated with a guest page.
979 * @returns uint16_t containing the data.
980 * @param pPage Pointer to the physical guest page tracking structure.
981 */
982#define PGM_PAGE_GET_TRACKING(pPage) \
983 ( *((uint16_t *)&(pPage)->HCPhysX + 3) )
984
985/** @def PGM_PAGE_SET_TRACKING
986 * Sets the packed shadow page pool tracking data associated with a guest page.
987 * @param pPage Pointer to the physical guest page tracking structure.
988 * @param u16TrackingData The tracking data to store.
989 */
990#define PGM_PAGE_SET_TRACKING(pPage, u16TrackingData) \
991 do { *((uint16_t *)&(pPage)->HCPhysX + 3) = (u16TrackingData); } while (0)
992
993/** @def PGM_PAGE_GET_TD_CREFS
994 * Gets the @a cRefs tracking data member.
995 * @returns cRefs.
996 * @param pPage Pointer to the physical guest page tracking structure.
997 */
998#define PGM_PAGE_GET_TD_CREFS(pPage) \
999 ((PGM_PAGE_GET_TRACKING(pPage) >> PGMPOOL_TD_CREFS_SHIFT) & PGMPOOL_TD_CREFS_MASK)
1000
1001#define PGM_PAGE_GET_TD_IDX(pPage) \
1002 ((PGM_PAGE_GET_TRACKING(pPage) >> PGMPOOL_TD_IDX_SHIFT) & PGMPOOL_TD_IDX_MASK)
1003
1004/**
1005 * Ram range for GC Phys to HC Phys conversion.
1006 *
1007 * Can be used for HC Virt to GC Phys and HC Virt to HC Phys
1008 * conversions too, but we'll let MM handle that for now.
1009 *
1010 * This structure is used by linked lists in both GC and HC.
1011 */
1012typedef struct PGMRAMRANGE
1013{
1014 /** Start of the range. Page aligned. */
1015 RTGCPHYS GCPhys;
1016 /** Size of the range. (Page aligned of course). */
1017 RTGCPHYS cb;
1018 /** Pointer to the next RAM range - for R3. */
1019 R3PTRTYPE(struct PGMRAMRANGE *) pNextR3;
1020 /** Pointer to the next RAM range - for R0. */
1021 R0PTRTYPE(struct PGMRAMRANGE *) pNextR0;
1022 /** Pointer to the next RAM range - for RC. */
1023 RCPTRTYPE(struct PGMRAMRANGE *) pNextRC;
1024 /** PGM_RAM_RANGE_FLAGS_* flags. */
1025 uint32_t fFlags;
1026 /** Last address in the range (inclusive). Page aligned (-1). */
1027 RTGCPHYS GCPhysLast;
1028 /** Start of the HC mapping of the range. This is only used for MMIO2. */
1029 R3PTRTYPE(void *) pvR3;
1030 /** The range description. */
1031 R3PTRTYPE(const char *) pszDesc;
1032 /** Pointer to self - R0 pointer. */
1033 R0PTRTYPE(struct PGMRAMRANGE *) pSelfR0;
1034 /** Pointer to self - RC pointer. */
1035 RCPTRTYPE(struct PGMRAMRANGE *) pSelfRC;
1036 /** Padding to make aPage aligned on sizeof(PGMPAGE). */
1037 uint32_t au32Alignment2[HC_ARCH_BITS == 32 ? 2 : 1];
1038 /** Array of physical guest page tracking structures. */
1039 PGMPAGE aPages[1];
1040} PGMRAMRANGE;
1041/** Pointer to Ram range for GC Phys to HC Phys conversion. */
1042typedef PGMRAMRANGE *PPGMRAMRANGE;
1043
1044/** @name PGMRAMRANGE::fFlags
1045 * @{ */
1046/** The RAM range is floating around as an independent guest mapping. */
1047#define PGM_RAM_RANGE_FLAGS_FLOATING RT_BIT(20)
1048/** @} */
1049
1050
1051/**
1052 * Per page tracking structure for ROM image.
1053 *
1054 * A ROM image may have a shadow page, in which case we may have
1055 * two pages backing it. This structure contains the PGMPAGE for
1056 * both while PGMRAMRANGE have a copy of the active one. It is
1057 * important that these aren't out of sync in any regard other
1058 * than page pool tracking data.
1059 */
1060typedef struct PGMROMPAGE
1061{
1062 /** The page structure for the virgin ROM page. */
1063 PGMPAGE Virgin;
1064 /** The page structure for the shadow RAM page. */
1065 PGMPAGE Shadow;
1066 /** The current protection setting. */
1067 PGMROMPROT enmProt;
1068 /** Pad the structure size to a multiple of 8. */
1069 uint32_t u32Padding;
1070} PGMROMPAGE;
1071/** Pointer to a ROM page tracking structure. */
1072typedef PGMROMPAGE *PPGMROMPAGE;
1073
1074
1075/**
1076 * A registered ROM image.
1077 *
1078 * This is needed to keep track of ROM image since they generally
1079 * intrude into a PGMRAMRANGE. It also keeps track of additional
1080 * info like the two page sets (read-only virgin and read-write shadow),
1081 * the current state of each page.
1082 *
1083 * Because access handlers cannot easily be executed in a different
1084 * context, the ROM ranges needs to be accessible and in all contexts.
1085 */
1086typedef struct PGMROMRANGE
1087{
1088 /** Pointer to the next range - R3. */
1089 R3PTRTYPE(struct PGMROMRANGE *) pNextR3;
1090 /** Pointer to the next range - R0. */
1091 R0PTRTYPE(struct PGMROMRANGE *) pNextR0;
1092 /** Pointer to the next range - RC. */
1093 RCPTRTYPE(struct PGMROMRANGE *) pNextRC;
1094 /** Pointer alignment */
1095 RTRCPTR GCPtrAlignment;
1096 /** Address of the range. */
1097 RTGCPHYS GCPhys;
1098 /** Address of the last byte in the range. */
1099 RTGCPHYS GCPhysLast;
1100 /** Size of the range. */
1101 RTGCPHYS cb;
1102 /** The flags (PGMPHYS_ROM_FLAG_*). */
1103 uint32_t fFlags;
1104 /** Alignment padding ensuring that aPages is sizeof(PGMROMPAGE) aligned. */
1105 uint32_t au32Alignemnt[HC_ARCH_BITS == 32 ? 7 : 3];
1106 /** Pointer to the original bits when PGMPHYS_ROM_FLAGS_PERMANENT_BINARY was specified.
1107 * This is used for strictness checks. */
1108 R3PTRTYPE(const void *) pvOriginal;
1109 /** The ROM description. */
1110 R3PTRTYPE(const char *) pszDesc;
1111 /** The per page tracking structures. */
1112 PGMROMPAGE aPages[1];
1113} PGMROMRANGE;
1114/** Pointer to a ROM range. */
1115typedef PGMROMRANGE *PPGMROMRANGE;
1116
1117
1118/**
1119 * A registered MMIO2 (= Device RAM) range.
1120 *
1121 * There are a few reason why we need to keep track of these
1122 * registrations. One of them is the deregistration & cleanup
1123 * stuff, while another is that the PGMRAMRANGE associated with
1124 * such a region may have to be removed from the ram range list.
1125 *
1126 * Overlapping with a RAM range has to be 100% or none at all. The
1127 * pages in the existing RAM range must not be ROM nor MMIO. A guru
1128 * meditation will be raised if a partial overlap or an overlap of
1129 * ROM pages is encountered. On an overlap we will free all the
1130 * existing RAM pages and put in the ram range pages instead.
1131 */
1132typedef struct PGMMMIO2RANGE
1133{
1134 /** The owner of the range. (a device) */
1135 PPDMDEVINSR3 pDevInsR3;
1136 /** Pointer to the ring-3 mapping of the allocation. */
1137 RTR3PTR pvR3;
1138 /** Pointer to the next range - R3. */
1139 R3PTRTYPE(struct PGMMMIO2RANGE *) pNextR3;
1140 /** Whether it's mapped or not. */
1141 bool fMapped;
1142 /** Whether it's overlapping or not. */
1143 bool fOverlapping;
1144 /** The PCI region number.
1145 * @remarks This ASSUMES that nobody will ever really need to have multiple
1146 * PCI devices with matching MMIO region numbers on a single device. */
1147 uint8_t iRegion;
1148 /** Alignment padding for putting the ram range on a PGMPAGE alignment boundrary. */
1149 uint8_t abAlignemnt[HC_ARCH_BITS == 32 ? 1 : 5];
1150 /** The associated RAM range. */
1151 PGMRAMRANGE RamRange;
1152} PGMMMIO2RANGE;
1153/** Pointer to a MMIO2 range. */
1154typedef PGMMMIO2RANGE *PPGMMMIO2RANGE;
1155
1156
1157
1158
1159/**
1160 * PGMPhysRead/Write cache entry
1161 */
1162typedef struct PGMPHYSCACHEENTRY
1163{
1164 /** R3 pointer to physical page. */
1165 R3PTRTYPE(uint8_t *) pbR3;
1166 /** GC Physical address for cache entry */
1167 RTGCPHYS GCPhys;
1168#if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
1169 RTGCPHYS u32Padding0; /**< alignment padding. */
1170#endif
1171} PGMPHYSCACHEENTRY;
1172
1173/**
1174 * PGMPhysRead/Write cache to reduce REM memory access overhead
1175 */
1176typedef struct PGMPHYSCACHE
1177{
1178 /** Bitmap of valid cache entries */
1179 uint64_t aEntries;
1180 /** Cache entries */
1181 PGMPHYSCACHEENTRY Entry[PGM_MAX_PHYSCACHE_ENTRIES];
1182} PGMPHYSCACHE;
1183
1184
1185/** Pointer to an allocation chunk ring-3 mapping. */
1186typedef struct PGMCHUNKR3MAP *PPGMCHUNKR3MAP;
1187/** Pointer to an allocation chunk ring-3 mapping pointer. */
1188typedef PPGMCHUNKR3MAP *PPPGMCHUNKR3MAP;
1189
1190/**
1191 * Ring-3 tracking structore for an allocation chunk ring-3 mapping.
1192 *
1193 * The primary tree (Core) uses the chunk id as key.
1194 * The secondary tree (AgeCore) is used for ageing and uses ageing sequence number as key.
1195 */
1196typedef struct PGMCHUNKR3MAP
1197{
1198 /** The key is the chunk id. */
1199 AVLU32NODECORE Core;
1200 /** The key is the ageing sequence number. */
1201 AVLLU32NODECORE AgeCore;
1202 /** The current age thingy. */
1203 uint32_t iAge;
1204 /** The current reference count. */
1205 uint32_t volatile cRefs;
1206 /** The current permanent reference count. */
1207 uint32_t volatile cPermRefs;
1208 /** The mapping address. */
1209 void *pv;
1210} PGMCHUNKR3MAP;
1211
1212/**
1213 * Allocation chunk ring-3 mapping TLB entry.
1214 */
1215typedef struct PGMCHUNKR3MAPTLBE
1216{
1217 /** The chunk id. */
1218 uint32_t volatile idChunk;
1219#if HC_ARCH_BITS == 64
1220 uint32_t u32Padding; /**< alignment padding. */
1221#endif
1222 /** The chunk map. */
1223#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1224 R3PTRTYPE(PPGMCHUNKR3MAP) volatile pChunk;
1225#else
1226 R3R0PTRTYPE(PPGMCHUNKR3MAP) volatile pChunk;
1227#endif
1228} PGMCHUNKR3MAPTLBE;
1229/** Pointer to the an allocation chunk ring-3 mapping TLB entry. */
1230typedef PGMCHUNKR3MAPTLBE *PPGMCHUNKR3MAPTLBE;
1231
1232/** The number of TLB entries in PGMCHUNKR3MAPTLB.
1233 * @remark Must be a power of two value. */
1234#define PGM_CHUNKR3MAPTLB_ENTRIES 32
1235
1236/**
1237 * Allocation chunk ring-3 mapping TLB.
1238 *
1239 * @remarks We use a TLB to speed up lookups by avoiding walking the AVL.
1240 * At first glance this might look kinda odd since AVL trees are
1241 * supposed to give the most optimial lookup times of all trees
1242 * due to their balancing. However, take a tree with 1023 nodes
1243 * in it, that's 10 levels, meaning that most searches has to go
1244 * down 9 levels before they find what they want. This isn't fast
1245 * compared to a TLB hit. There is the factor of cache misses,
1246 * and of course the problem with trees and branch prediction.
1247 * This is why we use TLBs in front of most of the trees.
1248 *
1249 * @todo Generalize this TLB + AVL stuff, shouldn't be all that
1250 * difficult when we switch to the new inlined AVL trees (from kStuff).
1251 */
1252typedef struct PGMCHUNKR3MAPTLB
1253{
1254 /** The TLB entries. */
1255 PGMCHUNKR3MAPTLBE aEntries[PGM_CHUNKR3MAPTLB_ENTRIES];
1256} PGMCHUNKR3MAPTLB;
1257
1258/**
1259 * Calculates the index of a guest page in the Ring-3 Chunk TLB.
1260 * @returns Chunk TLB index.
1261 * @param idChunk The Chunk ID.
1262 */
1263#define PGM_CHUNKR3MAPTLB_IDX(idChunk) ( (idChunk) & (PGM_CHUNKR3MAPTLB_ENTRIES - 1) )
1264
1265
1266/**
1267 * Ring-3 guest page mapping TLB entry.
1268 * @remarks used in ring-0 as well at the moment.
1269 */
1270typedef struct PGMPAGER3MAPTLBE
1271{
1272 /** Address of the page. */
1273 RTGCPHYS volatile GCPhys;
1274 /** The guest page. */
1275#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1276 R3PTRTYPE(PPGMPAGE) volatile pPage;
1277#else
1278 R3R0PTRTYPE(PPGMPAGE) volatile pPage;
1279#endif
1280 /** Pointer to the page mapping tracking structure, PGMCHUNKR3MAP. */
1281#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1282 R3PTRTYPE(PPGMCHUNKR3MAP) volatile pMap;
1283#else
1284 R3R0PTRTYPE(PPGMCHUNKR3MAP) volatile pMap;
1285#endif
1286 /** The address */
1287#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1288 R3PTRTYPE(void *) volatile pv;
1289#else
1290 R3R0PTRTYPE(void *) volatile pv;
1291#endif
1292#if HC_ARCH_BITS == 32
1293 uint32_t u32Padding; /**< alignment padding. */
1294#endif
1295} PGMPAGER3MAPTLBE;
1296/** Pointer to an entry in the HC physical TLB. */
1297typedef PGMPAGER3MAPTLBE *PPGMPAGER3MAPTLBE;
1298
1299
1300/** The number of entries in the ring-3 guest page mapping TLB.
1301 * @remarks The value must be a power of two. */
1302#define PGM_PAGER3MAPTLB_ENTRIES 64
1303
1304/**
1305 * Ring-3 guest page mapping TLB.
1306 * @remarks used in ring-0 as well at the moment.
1307 */
1308typedef struct PGMPAGER3MAPTLB
1309{
1310 /** The TLB entries. */
1311 PGMPAGER3MAPTLBE aEntries[PGM_PAGER3MAPTLB_ENTRIES];
1312} PGMPAGER3MAPTLB;
1313/** Pointer to the ring-3 guest page mapping TLB. */
1314typedef PGMPAGER3MAPTLB *PPGMPAGER3MAPTLB;
1315
1316/**
1317 * Calculates the index of the TLB entry for the specified guest page.
1318 * @returns Physical TLB index.
1319 * @param GCPhys The guest physical address.
1320 */
1321#define PGM_PAGER3MAPTLB_IDX(GCPhys) ( ((GCPhys) >> PAGE_SHIFT) & (PGM_PAGER3MAPTLB_ENTRIES - 1) )
1322
1323
1324/**
1325 * Mapping cache usage set entry.
1326 *
1327 * @remarks 16-bit ints was choosen as the set is not expected to be used beyond
1328 * the dynamic ring-0 and (to some extent) raw-mode context mapping
1329 * cache. If it's extended to include ring-3, well, then something will
1330 * have be changed here...
1331 */
1332typedef struct PGMMAPSETENTRY
1333{
1334 /** The mapping cache index. */
1335 uint16_t iPage;
1336 /** The number of references.
1337 * The max is UINT16_MAX - 1. */
1338 uint16_t cRefs;
1339#if HC_ARCH_BITS == 64
1340 uint32_t alignment;
1341#endif
1342 /** Pointer to the page. */
1343 RTR0PTR pvPage;
1344 /** The physical address for this entry. */
1345 RTHCPHYS HCPhys;
1346} PGMMAPSETENTRY;
1347/** Pointer to a mapping cache usage set entry. */
1348typedef PGMMAPSETENTRY *PPGMMAPSETENTRY;
1349
1350/**
1351 * Mapping cache usage set.
1352 *
1353 * This is used in ring-0 and the raw-mode context to track dynamic mappings
1354 * done during exits / traps. The set is
1355 */
1356typedef struct PGMMAPSET
1357{
1358 /** The number of occupied entries.
1359 * This is PGMMAPSET_CLOSED if the set is closed and we're not supposed to do
1360 * dynamic mappings. */
1361 uint32_t cEntries;
1362 /** The start of the current subset.
1363 * This is UINT32_MAX if no subset is currently open. */
1364 uint32_t iSubset;
1365 /** The index of the current CPU, only valid if the set is open. */
1366 int32_t iCpu;
1367#if HC_ARCH_BITS == 64
1368 uint32_t alignment;
1369#endif
1370 /** The entries. */
1371 PGMMAPSETENTRY aEntries[64];
1372 /** HCPhys -> iEntry fast lookup table.
1373 * Use PGMMAPSET_HASH for hashing.
1374 * The entries may or may not be valid, check against cEntries. */
1375 uint8_t aiHashTable[128];
1376} PGMMAPSET;
1377/** Pointer to the mapping cache set. */
1378typedef PGMMAPSET *PPGMMAPSET;
1379
1380/** PGMMAPSET::cEntries value for a closed set. */
1381#define PGMMAPSET_CLOSED UINT32_C(0xdeadc0fe)
1382
1383/** Hash function for aiHashTable. */
1384#define PGMMAPSET_HASH(HCPhys) (((HCPhys) >> PAGE_SHIFT) & 127)
1385
1386/** The max fill size (strict builds). */
1387#define PGMMAPSET_MAX_FILL (64U * 80U / 100U)
1388
1389
1390/** @name Context neutrual page mapper TLB.
1391 *
1392 * Hoping to avoid some code and bug duplication parts of the GCxxx->CCPtr
1393 * code is writting in a kind of context neutrual way. Time will show whether
1394 * this actually makes sense or not...
1395 *
1396 * @todo this needs to be reconsidered and dropped/redone since the ring-0
1397 * context ends up using a global mapping cache on some platforms
1398 * (darwin).
1399 *
1400 * @{ */
1401/** @typedef PPGMPAGEMAPTLB
1402 * The page mapper TLB pointer type for the current context. */
1403/** @typedef PPGMPAGEMAPTLB
1404 * The page mapper TLB entry pointer type for the current context. */
1405/** @typedef PPGMPAGEMAPTLB
1406 * The page mapper TLB entry pointer pointer type for the current context. */
1407/** @def PGM_PAGEMAPTLB_ENTRIES
1408 * The number of TLB entries in the page mapper TLB for the current context. */
1409/** @def PGM_PAGEMAPTLB_IDX
1410 * Calculate the TLB index for a guest physical address.
1411 * @returns The TLB index.
1412 * @param GCPhys The guest physical address. */
1413/** @typedef PPGMPAGEMAP
1414 * Pointer to a page mapper unit for current context. */
1415/** @typedef PPPGMPAGEMAP
1416 * Pointer to a page mapper unit pointer for current context. */
1417#ifdef IN_RC
1418// typedef PPGMPAGEGCMAPTLB PPGMPAGEMAPTLB;
1419// typedef PPGMPAGEGCMAPTLBE PPGMPAGEMAPTLBE;
1420// typedef PPGMPAGEGCMAPTLBE *PPPGMPAGEMAPTLBE;
1421# define PGM_PAGEMAPTLB_ENTRIES PGM_PAGEGCMAPTLB_ENTRIES
1422# define PGM_PAGEMAPTLB_IDX(GCPhys) PGM_PAGEGCMAPTLB_IDX(GCPhys)
1423 typedef void * PPGMPAGEMAP;
1424 typedef void ** PPPGMPAGEMAP;
1425//#elif IN_RING0
1426// typedef PPGMPAGER0MAPTLB PPGMPAGEMAPTLB;
1427// typedef PPGMPAGER0MAPTLBE PPGMPAGEMAPTLBE;
1428// typedef PPGMPAGER0MAPTLBE *PPPGMPAGEMAPTLBE;
1429//# define PGM_PAGEMAPTLB_ENTRIES PGM_PAGER0MAPTLB_ENTRIES
1430//# define PGM_PAGEMAPTLB_IDX(GCPhys) PGM_PAGER0MAPTLB_IDX(GCPhys)
1431// typedef PPGMCHUNKR0MAP PPGMPAGEMAP;
1432// typedef PPPGMCHUNKR0MAP PPPGMPAGEMAP;
1433#else
1434 typedef PPGMPAGER3MAPTLB PPGMPAGEMAPTLB;
1435 typedef PPGMPAGER3MAPTLBE PPGMPAGEMAPTLBE;
1436 typedef PPGMPAGER3MAPTLBE *PPPGMPAGEMAPTLBE;
1437# define PGM_PAGEMAPTLB_ENTRIES PGM_PAGER3MAPTLB_ENTRIES
1438# define PGM_PAGEMAPTLB_IDX(GCPhys) PGM_PAGER3MAPTLB_IDX(GCPhys)
1439 typedef PPGMCHUNKR3MAP PPGMPAGEMAP;
1440 typedef PPPGMCHUNKR3MAP PPPGMPAGEMAP;
1441#endif
1442/** @} */
1443
1444
1445/** @name PGM Pool Indexes.
1446 * Aka. the unique shadow page identifier.
1447 * @{ */
1448/** NIL page pool IDX. */
1449#define NIL_PGMPOOL_IDX 0
1450/** The first normal index. */
1451#define PGMPOOL_IDX_FIRST_SPECIAL 1
1452/** Page directory (32-bit root). */
1453#define PGMPOOL_IDX_PD 1
1454/** Page Directory Pointer Table (PAE root). */
1455#define PGMPOOL_IDX_PDPT 2
1456/** AMD64 CR3 level index.*/
1457#define PGMPOOL_IDX_AMD64_CR3 3
1458/** Nested paging root.*/
1459#define PGMPOOL_IDX_NESTED_ROOT 4
1460/** The first normal index. */
1461#define PGMPOOL_IDX_FIRST 5
1462/** The last valid index. (inclusive, 14 bits) */
1463#define PGMPOOL_IDX_LAST 0x3fff
1464/** @} */
1465
1466/** The NIL index for the parent chain. */
1467#define NIL_PGMPOOL_USER_INDEX ((uint16_t)0xffff)
1468
1469/**
1470 * Node in the chain linking a shadowed page to it's parent (user).
1471 */
1472#pragma pack(1)
1473typedef struct PGMPOOLUSER
1474{
1475 /** The index to the next item in the chain. NIL_PGMPOOL_USER_INDEX is no next. */
1476 uint16_t iNext;
1477 /** The user page index. */
1478 uint16_t iUser;
1479 /** Index into the user table. */
1480 uint32_t iUserTable;
1481} PGMPOOLUSER, *PPGMPOOLUSER;
1482typedef const PGMPOOLUSER *PCPGMPOOLUSER;
1483#pragma pack()
1484
1485
1486/** The NIL index for the phys ext chain. */
1487#define NIL_PGMPOOL_PHYSEXT_INDEX ((uint16_t)0xffff)
1488
1489/**
1490 * Node in the chain of physical cross reference extents.
1491 * @todo Calling this an 'extent' is not quite right, find a better name.
1492 */
1493#pragma pack(1)
1494typedef struct PGMPOOLPHYSEXT
1495{
1496 /** The index to the next item in the chain. NIL_PGMPOOL_PHYSEXT_INDEX is no next. */
1497 uint16_t iNext;
1498 /** The user page index. */
1499 uint16_t aidx[3];
1500} PGMPOOLPHYSEXT, *PPGMPOOLPHYSEXT;
1501typedef const PGMPOOLPHYSEXT *PCPGMPOOLPHYSEXT;
1502#pragma pack()
1503
1504
1505/**
1506 * The kind of page that's being shadowed.
1507 */
1508typedef enum PGMPOOLKIND
1509{
1510 /** The virtual invalid 0 entry. */
1511 PGMPOOLKIND_INVALID = 0,
1512 /** The entry is free (=unused). */
1513 PGMPOOLKIND_FREE,
1514
1515 /** Shw: 32-bit page table; Gst: no paging */
1516 PGMPOOLKIND_32BIT_PT_FOR_PHYS,
1517 /** Shw: 32-bit page table; Gst: 32-bit page table. */
1518 PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT,
1519 /** Shw: 32-bit page table; Gst: 4MB page. */
1520 PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB,
1521 /** Shw: PAE page table; Gst: no paging */
1522 PGMPOOLKIND_PAE_PT_FOR_PHYS,
1523 /** Shw: PAE page table; Gst: 32-bit page table. */
1524 PGMPOOLKIND_PAE_PT_FOR_32BIT_PT,
1525 /** Shw: PAE page table; Gst: Half of a 4MB page. */
1526 PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB,
1527 /** Shw: PAE page table; Gst: PAE page table. */
1528 PGMPOOLKIND_PAE_PT_FOR_PAE_PT,
1529 /** Shw: PAE page table; Gst: 2MB page. */
1530 PGMPOOLKIND_PAE_PT_FOR_PAE_2MB,
1531
1532 /** Shw: 32-bit page directory. Gst: 32-bit page directory. */
1533 PGMPOOLKIND_32BIT_PD,
1534 /** Shw: 32-bit page directory. Gst: no paging. */
1535 PGMPOOLKIND_32BIT_PD_PHYS,
1536 /** Shw: PAE page directory 0; Gst: 32-bit page directory. */
1537 PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD,
1538 /** Shw: PAE page directory 1; Gst: 32-bit page directory. */
1539 PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD,
1540 /** Shw: PAE page directory 2; Gst: 32-bit page directory. */
1541 PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD,
1542 /** Shw: PAE page directory 3; Gst: 32-bit page directory. */
1543 PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD,
1544 /** Shw: PAE page directory; Gst: PAE page directory. */
1545 PGMPOOLKIND_PAE_PD_FOR_PAE_PD,
1546 /** Shw: PAE page directory; Gst: no paging. */
1547 PGMPOOLKIND_PAE_PD_PHYS,
1548
1549 /** Shw: PAE page directory pointer table (legacy, 4 entries); Gst 32 bits paging. */
1550 PGMPOOLKIND_PAE_PDPT_FOR_32BIT,
1551 /** Shw: PAE page directory pointer table (legacy, 4 entries); Gst PAE PDPT. */
1552 PGMPOOLKIND_PAE_PDPT,
1553 /** Shw: PAE page directory pointer table (legacy, 4 entries); Gst: no paging. */
1554 PGMPOOLKIND_PAE_PDPT_PHYS,
1555
1556 /** Shw: 64-bit page directory pointer table; Gst: 64-bit page directory pointer table. */
1557 PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT,
1558 /** Shw: 64-bit page directory pointer table; Gst: no paging */
1559 PGMPOOLKIND_64BIT_PDPT_FOR_PHYS,
1560 /** Shw: 64-bit page directory table; Gst: 64-bit page directory table. */
1561 PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD,
1562 /** Shw: 64-bit page directory table; Gst: no paging */
1563 PGMPOOLKIND_64BIT_PD_FOR_PHYS, /* 22 */
1564
1565 /** Shw: 64-bit PML4; Gst: 64-bit PML4. */
1566 PGMPOOLKIND_64BIT_PML4,
1567
1568 /** Shw: EPT page directory pointer table; Gst: no paging */
1569 PGMPOOLKIND_EPT_PDPT_FOR_PHYS,
1570 /** Shw: EPT page directory table; Gst: no paging */
1571 PGMPOOLKIND_EPT_PD_FOR_PHYS,
1572 /** Shw: EPT page table; Gst: no paging */
1573 PGMPOOLKIND_EPT_PT_FOR_PHYS,
1574
1575 /** Shw: Root Nested paging table. */
1576 PGMPOOLKIND_ROOT_NESTED,
1577
1578 /** The last valid entry. */
1579 PGMPOOLKIND_LAST = PGMPOOLKIND_ROOT_NESTED
1580} PGMPOOLKIND;
1581
1582
1583/**
1584 * The tracking data for a page in the pool.
1585 */
1586typedef struct PGMPOOLPAGE
1587{
1588 /** AVL node code with the (R3) physical address of this page. */
1589 AVLOHCPHYSNODECORE Core;
1590 /** Pointer to the R3 mapping of the page. */
1591#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1592 R3PTRTYPE(void *) pvPageR3;
1593#else
1594 R3R0PTRTYPE(void *) pvPageR3;
1595#endif
1596 /** The guest physical address. */
1597#if HC_ARCH_BITS == 32 && GC_ARCH_BITS == 64
1598 uint32_t Alignment0;
1599#endif
1600 RTGCPHYS GCPhys;
1601 /** The kind of page we're shadowing. (This is really a PGMPOOLKIND enum.) */
1602 uint8_t enmKind;
1603 uint8_t bPadding;
1604 /** The index of this page. */
1605 uint16_t idx;
1606 /** The next entry in the list this page currently resides in.
1607 * It's either in the free list or in the GCPhys hash. */
1608 uint16_t iNext;
1609#ifdef PGMPOOL_WITH_USER_TRACKING
1610 /** Head of the user chain. NIL_PGMPOOL_USER_INDEX if not currently in use. */
1611 uint16_t iUserHead;
1612 /** The number of present entries. */
1613 uint16_t cPresent;
1614 /** The first entry in the table which is present. */
1615 uint16_t iFirstPresent;
1616#endif
1617#ifdef PGMPOOL_WITH_MONITORING
1618 /** The number of modifications to the monitored page. */
1619 uint16_t cModifications;
1620 /** The next modified page. NIL_PGMPOOL_IDX if tail. */
1621 uint16_t iModifiedNext;
1622 /** The previous modified page. NIL_PGMPOOL_IDX if head. */
1623 uint16_t iModifiedPrev;
1624 /** The next page sharing access handler. NIL_PGMPOOL_IDX if tail. */
1625 uint16_t iMonitoredNext;
1626 /** The previous page sharing access handler. NIL_PGMPOOL_IDX if head. */
1627 uint16_t iMonitoredPrev;
1628#endif
1629#ifdef PGMPOOL_WITH_CACHE
1630 /** The next page in the age list. */
1631 uint16_t iAgeNext;
1632 /** The previous page in the age list. */
1633 uint16_t iAgePrev;
1634#endif /* PGMPOOL_WITH_CACHE */
1635 /** Used to indicate that the page is zeroed. */
1636 bool fZeroed;
1637 /** Used to indicate that a PT has non-global entries. */
1638 bool fSeenNonGlobal;
1639 /** Used to indicate that we're monitoring writes to the guest page. */
1640 bool fMonitored;
1641 /** Used to indicate that the page is in the cache (e.g. in the GCPhys hash).
1642 * (All pages are in the age list.) */
1643 bool fCached;
1644 /** This is used by the R3 access handlers when invoked by an async thread.
1645 * It's a hack required because of REMR3NotifyHandlerPhysicalDeregister. */
1646 bool volatile fReusedFlushPending;
1647 bool bPadding1;
1648
1649 /** Used to indicate that this page can't be flushed. Important for cr3 root pages or shadow pae pd pages). */
1650 uint32_t cLocked;
1651} PGMPOOLPAGE, *PPGMPOOLPAGE, **PPPGMPOOLPAGE;
1652/** Pointer to a const pool page. */
1653typedef PGMPOOLPAGE const *PCPGMPOOLPAGE;
1654
1655
1656#ifdef PGMPOOL_WITH_CACHE
1657/** The hash table size. */
1658# define PGMPOOL_HASH_SIZE 0x40
1659/** The hash function. */
1660# define PGMPOOL_HASH(GCPhys) ( ((GCPhys) >> PAGE_SHIFT) & (PGMPOOL_HASH_SIZE - 1) )
1661#endif
1662
1663
1664/**
1665 * The shadow page pool instance data.
1666 *
1667 * It's all one big allocation made at init time, except for the
1668 * pages that is. The user nodes follows immediatly after the
1669 * page structures.
1670 */
1671typedef struct PGMPOOL
1672{
1673 /** The VM handle - R3 Ptr. */
1674 PVMR3 pVMR3;
1675 /** The VM handle - R0 Ptr. */
1676 PVMR0 pVMR0;
1677 /** The VM handle - RC Ptr. */
1678 PVMRC pVMRC;
1679 /** The max pool size. This includes the special IDs. */
1680 uint16_t cMaxPages;
1681 /** The current pool size. */
1682 uint16_t cCurPages;
1683 /** The head of the free page list. */
1684 uint16_t iFreeHead;
1685 /* Padding. */
1686 uint16_t u16Padding;
1687#ifdef PGMPOOL_WITH_USER_TRACKING
1688 /** Head of the chain of free user nodes. */
1689 uint16_t iUserFreeHead;
1690 /** The number of user nodes we've allocated. */
1691 uint16_t cMaxUsers;
1692 /** The number of present page table entries in the entire pool. */
1693 uint32_t cPresent;
1694 /** Pointer to the array of user nodes - RC pointer. */
1695 RCPTRTYPE(PPGMPOOLUSER) paUsersRC;
1696 /** Pointer to the array of user nodes - R3 pointer. */
1697 R3PTRTYPE(PPGMPOOLUSER) paUsersR3;
1698 /** Pointer to the array of user nodes - R0 pointer. */
1699 R0PTRTYPE(PPGMPOOLUSER) paUsersR0;
1700#endif /* PGMPOOL_WITH_USER_TRACKING */
1701#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
1702 /** Head of the chain of free phys ext nodes. */
1703 uint16_t iPhysExtFreeHead;
1704 /** The number of user nodes we've allocated. */
1705 uint16_t cMaxPhysExts;
1706 /** Pointer to the array of physical xref extent - RC pointer. */
1707 RCPTRTYPE(PPGMPOOLPHYSEXT) paPhysExtsRC;
1708 /** Pointer to the array of physical xref extent nodes - R3 pointer. */
1709 R3PTRTYPE(PPGMPOOLPHYSEXT) paPhysExtsR3;
1710 /** Pointer to the array of physical xref extent nodes - R0 pointer. */
1711 R0PTRTYPE(PPGMPOOLPHYSEXT) paPhysExtsR0;
1712#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
1713#ifdef PGMPOOL_WITH_CACHE
1714 /** Hash table for GCPhys addresses. */
1715 uint16_t aiHash[PGMPOOL_HASH_SIZE];
1716 /** The head of the age list. */
1717 uint16_t iAgeHead;
1718 /** The tail of the age list. */
1719 uint16_t iAgeTail;
1720 /** Set if the cache is enabled. */
1721 bool fCacheEnabled;
1722#endif /* PGMPOOL_WITH_CACHE */
1723#ifdef PGMPOOL_WITH_MONITORING
1724 /** Head of the list of modified pages. */
1725 uint16_t iModifiedHead;
1726 /** The current number of modified pages. */
1727 uint16_t cModifiedPages;
1728 /** Access handler, RC. */
1729 RCPTRTYPE(PFNPGMRCPHYSHANDLER) pfnAccessHandlerRC;
1730 /** Access handler, R0. */
1731 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnAccessHandlerR0;
1732 /** Access handler, R3. */
1733 R3PTRTYPE(PFNPGMR3PHYSHANDLER) pfnAccessHandlerR3;
1734 /** The access handler description (HC ptr). */
1735 R3PTRTYPE(const char *) pszAccessHandler;
1736#endif /* PGMPOOL_WITH_MONITORING */
1737 /** The number of pages currently in use. */
1738 uint16_t cUsedPages;
1739#ifdef VBOX_WITH_STATISTICS
1740 /** The high wather mark for cUsedPages. */
1741 uint16_t cUsedPagesHigh;
1742 uint32_t Alignment1; /**< Align the next member on a 64-bit boundrary. */
1743 /** Profiling pgmPoolAlloc(). */
1744 STAMPROFILEADV StatAlloc;
1745 /** Profiling pgmPoolClearAll(). */
1746 STAMPROFILE StatClearAll;
1747 /** Profiling pgmPoolFlushAllInt(). */
1748 STAMPROFILE StatFlushAllInt;
1749 /** Profiling pgmPoolFlushPage(). */
1750 STAMPROFILE StatFlushPage;
1751 /** Profiling pgmPoolFree(). */
1752 STAMPROFILE StatFree;
1753 /** Profiling time spent zeroing pages. */
1754 STAMPROFILE StatZeroPage;
1755# ifdef PGMPOOL_WITH_USER_TRACKING
1756 /** Profiling of pgmPoolTrackDeref. */
1757 STAMPROFILE StatTrackDeref;
1758 /** Profiling pgmTrackFlushGCPhysPT. */
1759 STAMPROFILE StatTrackFlushGCPhysPT;
1760 /** Profiling pgmTrackFlushGCPhysPTs. */
1761 STAMPROFILE StatTrackFlushGCPhysPTs;
1762 /** Profiling pgmTrackFlushGCPhysPTsSlow. */
1763 STAMPROFILE StatTrackFlushGCPhysPTsSlow;
1764 /** Number of times we've been out of user records. */
1765 STAMCOUNTER StatTrackFreeUpOneUser;
1766# endif
1767# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
1768 /** Profiling deref activity related tracking GC physical pages. */
1769 STAMPROFILE StatTrackDerefGCPhys;
1770 /** Number of linear searches for a HCPhys in the ram ranges. */
1771 STAMCOUNTER StatTrackLinearRamSearches;
1772 /** The number of failing pgmPoolTrackPhysExtAlloc calls. */
1773 STAMCOUNTER StamTrackPhysExtAllocFailures;
1774# endif
1775# ifdef PGMPOOL_WITH_MONITORING
1776 /** Profiling the RC/R0 access handler. */
1777 STAMPROFILE StatMonitorRZ;
1778 /** Times we've failed interpreting the instruction. */
1779 STAMCOUNTER StatMonitorRZEmulateInstr;
1780 /** Profiling the pgmPoolFlushPage calls made from the RC/R0 access handler. */
1781 STAMPROFILE StatMonitorRZFlushPage;
1782 /** Times we've detected fork(). */
1783 STAMCOUNTER StatMonitorRZFork;
1784 /** Profiling the RC/R0 access we've handled (except REP STOSD). */
1785 STAMPROFILE StatMonitorRZHandled;
1786 /** Times we've failed interpreting a patch code instruction. */
1787 STAMCOUNTER StatMonitorRZIntrFailPatch1;
1788 /** Times we've failed interpreting a patch code instruction during flushing. */
1789 STAMCOUNTER StatMonitorRZIntrFailPatch2;
1790 /** The number of times we've seen rep prefixes we can't handle. */
1791 STAMCOUNTER StatMonitorRZRepPrefix;
1792 /** Profiling the REP STOSD cases we've handled. */
1793 STAMPROFILE StatMonitorRZRepStosd;
1794
1795 /** Profiling the R3 access handler. */
1796 STAMPROFILE StatMonitorR3;
1797 /** Times we've failed interpreting the instruction. */
1798 STAMCOUNTER StatMonitorR3EmulateInstr;
1799 /** Profiling the pgmPoolFlushPage calls made from the R3 access handler. */
1800 STAMPROFILE StatMonitorR3FlushPage;
1801 /** Times we've detected fork(). */
1802 STAMCOUNTER StatMonitorR3Fork;
1803 /** Profiling the R3 access we've handled (except REP STOSD). */
1804 STAMPROFILE StatMonitorR3Handled;
1805 /** The number of times we've seen rep prefixes we can't handle. */
1806 STAMCOUNTER StatMonitorR3RepPrefix;
1807 /** Profiling the REP STOSD cases we've handled. */
1808 STAMPROFILE StatMonitorR3RepStosd;
1809 /** The number of times we're called in an async thread an need to flush. */
1810 STAMCOUNTER StatMonitorR3Async;
1811 /** The high wather mark for cModifiedPages. */
1812 uint16_t cModifiedPagesHigh;
1813 uint16_t Alignment2[3]; /**< Align the next member on a 64-bit boundrary. */
1814# endif
1815# ifdef PGMPOOL_WITH_CACHE
1816 /** The number of cache hits. */
1817 STAMCOUNTER StatCacheHits;
1818 /** The number of cache misses. */
1819 STAMCOUNTER StatCacheMisses;
1820 /** The number of times we've got a conflict of 'kind' in the cache. */
1821 STAMCOUNTER StatCacheKindMismatches;
1822 /** Number of times we've been out of pages. */
1823 STAMCOUNTER StatCacheFreeUpOne;
1824 /** The number of cacheable allocations. */
1825 STAMCOUNTER StatCacheCacheable;
1826 /** The number of uncacheable allocations. */
1827 STAMCOUNTER StatCacheUncacheable;
1828# endif
1829#elif HC_ARCH_BITS == 64
1830 uint32_t Alignment3; /**< Align the next member on a 64-bit boundrary. */
1831#endif
1832 /** The AVL tree for looking up a page by its HC physical address. */
1833 AVLOHCPHYSTREE HCPhysTree;
1834 uint32_t Alignment4; /**< Align the next member on a 64-bit boundrary. */
1835 /** Array of pages. (cMaxPages in length)
1836 * The Id is the index into thist array.
1837 */
1838 PGMPOOLPAGE aPages[PGMPOOL_IDX_FIRST];
1839} PGMPOOL, *PPGMPOOL, **PPPGMPOOL;
1840
1841
1842/** @def PGMPOOL_PAGE_2_PTR
1843 * Maps a pool page pool into the current context.
1844 *
1845 * @returns VBox status code.
1846 * @param pVM The VM handle.
1847 * @param pPage The pool page.
1848 *
1849 * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
1850 * small page window employeed by that function. Be careful.
1851 * @remark There is no need to assert on the result.
1852 */
1853#if defined(IN_RC)
1854# define PGMPOOL_PAGE_2_PTR(pVM, pPage) pgmPoolMapPageInlined(&(pVM)->pgm.s, (pPage))
1855#elif defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1856# define PGMPOOL_PAGE_2_PTR(pVM, pPage) pgmPoolMapPageInlined(&(pVM)->pgm.s, (pPage))
1857#elif defined(VBOX_STRICT)
1858# define PGMPOOL_PAGE_2_PTR(pVM, pPage) pgmPoolMapPageStrict(pPage)
1859DECLINLINE(void *) pgmPoolMapPageStrict(PPGMPOOLPAGE pPage)
1860{
1861 Assert(pPage && pPage->pvPageR3);
1862 return pPage->pvPageR3;
1863}
1864#else
1865# define PGMPOOL_PAGE_2_PTR(pVM, pPage) ((pPage)->pvPageR3)
1866#endif
1867
1868/** @def PGMPOOL_PAGE_2_PTR_BY_PGM
1869 * Maps a pool page pool into the current context.
1870 *
1871 * @returns VBox status code.
1872 * @param pPGM Pointer to the PGM instance data.
1873 * @param pPage The pool page.
1874 *
1875 * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
1876 * small page window employeed by that function. Be careful.
1877 * @remark There is no need to assert on the result.
1878 */
1879#if defined(IN_RC)
1880# define PGMPOOL_PAGE_2_PTR_BY_PGM(pPGM, pPage) pgmPoolMapPageInlined(pPGM, (pPage))
1881#elif defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1882# define PGMPOOL_PAGE_2_PTR_BY_PGM(pPGM, pPage) pgmPoolMapPageInlined(pPGM, (pPage))
1883#else
1884# define PGMPOOL_PAGE_2_PTR_BY_PGM(pPGM, pPage) PGMPOOL_PAGE_2_PTR(PGM2VM(pPGM), pPage)
1885#endif
1886
1887/** @def PGMPOOL_PAGE_2_PTR_BY_PGMCPU
1888 * Maps a pool page pool into the current context.
1889 *
1890 * @returns VBox status code.
1891 * @param pPGM Pointer to the PGMCPU instance data.
1892 * @param pPage The pool page.
1893 *
1894 * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
1895 * small page window employeed by that function. Be careful.
1896 * @remark There is no need to assert on the result.
1897 */
1898#if defined(IN_RC)
1899# define PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPage) pgmPoolMapPageInlined(PGMCPU2PGM(pPGM), (pPage))
1900#elif defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
1901# define PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPage) pgmPoolMapPageInlined(PGMCPU2PGM(pPGM), (pPage))
1902#else
1903# define PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPage) PGMPOOL_PAGE_2_PTR(PGMCPU2VM(pPGM), pPage)
1904#endif
1905
1906
1907/** @name Per guest page tracking data.
1908 * This is currently as a 16-bit word in the PGMPAGE structure, the idea though
1909 * is to use more bits for it and split it up later on. But for now we'll play
1910 * safe and change as little as possible.
1911 *
1912 * The 16-bit word has two parts:
1913 *
1914 * The first 14-bit forms the @a idx field. It is either the index of a page in
1915 * the shadow page pool, or and index into the extent list.
1916 *
1917 * The 2 topmost bits makes up the @a cRefs field, which counts the number of
1918 * shadow page pool references to the page. If cRefs equals
1919 * PGMPOOL_CREFS_PHYSEXT, then the @a idx field is an indext into the extent
1920 * (misnomer) table and not the shadow page pool.
1921 *
1922 * See PGM_PAGE_GET_TRACKING and PGM_PAGE_SET_TRACKING for how to get and set
1923 * the 16-bit word.
1924 *
1925 * @{ */
1926/** The shift count for getting to the cRefs part. */
1927#define PGMPOOL_TD_CREFS_SHIFT 14
1928/** The mask applied after shifting the tracking data down by
1929 * PGMPOOL_TD_CREFS_SHIFT. */
1930#define PGMPOOL_TD_CREFS_MASK 0x3
1931/** The cRef value used to indiciate that the idx is the head of a
1932 * physical cross reference list. */
1933#define PGMPOOL_TD_CREFS_PHYSEXT PGMPOOL_TD_CREFS_MASK
1934/** The shift used to get idx. */
1935#define PGMPOOL_TD_IDX_SHIFT 0
1936/** The mask applied to the idx after shifting down by PGMPOOL_TD_IDX_SHIFT. */
1937#define PGMPOOL_TD_IDX_MASK 0x3fff
1938/** The idx value when we're out of of PGMPOOLPHYSEXT entries or/and there are
1939 * simply too many mappings of this page. */
1940#define PGMPOOL_TD_IDX_OVERFLOWED PGMPOOL_TD_IDX_MASK
1941
1942/** @def PGMPOOL_TD_MAKE
1943 * Makes a 16-bit tracking data word.
1944 *
1945 * @returns tracking data.
1946 * @param cRefs The @a cRefs field. Must be within bounds!
1947 * @param idx The @a idx field. Must also be within bounds! */
1948#define PGMPOOL_TD_MAKE(cRefs, idx) ( ((cRefs) << PGMPOOL_TD_CREFS_SHIFT) | (idx) )
1949
1950/** @def PGMPOOL_TD_GET_CREFS
1951 * Get the @a cRefs field from a tracking data word.
1952 *
1953 * @returns The @a cRefs field
1954 * @param u16 The tracking data word. */
1955#define PGMPOOL_TD_GET_CREFS(u16) ( ((u16) >> PGMPOOL_TD_CREFS_SHIFT) & PGMPOOL_TD_CREFS_MASK )
1956
1957/** @def PGMPOOL_TD_GET_IDX
1958 * Get the @a idx field from a tracking data word.
1959 *
1960 * @returns The @a idx field
1961 * @param u16 The tracking data word. */
1962#define PGMPOOL_TD_GET_IDX(u16) ( ((u16) >> PGMPOOL_TD_IDX_SHIFT) & PGMPOOL_TD_IDX_MASK )
1963/** @} */
1964
1965
1966/**
1967 * Trees are using self relative offsets as pointers.
1968 * So, all its data, including the root pointer, must be in the heap for HC and GC
1969 * to have the same layout.
1970 */
1971typedef struct PGMTREES
1972{
1973 /** Physical access handlers (AVL range+offsetptr tree). */
1974 AVLROGCPHYSTREE PhysHandlers;
1975 /** Virtual access handlers (AVL range + GC ptr tree). */
1976 AVLROGCPTRTREE VirtHandlers;
1977 /** Virtual access handlers (Phys range AVL range + offsetptr tree). */
1978 AVLROGCPHYSTREE PhysToVirtHandlers;
1979 /** Virtual access handlers for the hypervisor (AVL range + GC ptr tree). */
1980 AVLROGCPTRTREE HyperVirtHandlers;
1981} PGMTREES;
1982/** Pointer to PGM trees. */
1983typedef PGMTREES *PPGMTREES;
1984
1985
1986/** @name Paging mode macros
1987 * @{ */
1988#ifdef IN_RC
1989# define PGM_CTX(a,b) a##RC##b
1990# define PGM_CTX_STR(a,b) a "GC" b
1991# define PGM_CTX_DECL(type) VMMRCDECL(type)
1992#else
1993# ifdef IN_RING3
1994# define PGM_CTX(a,b) a##R3##b
1995# define PGM_CTX_STR(a,b) a "R3" b
1996# define PGM_CTX_DECL(type) DECLCALLBACK(type)
1997# else
1998# define PGM_CTX(a,b) a##R0##b
1999# define PGM_CTX_STR(a,b) a "R0" b
2000# define PGM_CTX_DECL(type) VMMDECL(type)
2001# endif
2002#endif
2003
2004#define PGM_GST_NAME_REAL(name) PGM_CTX(pgm,GstReal##name)
2005#define PGM_GST_NAME_RC_REAL_STR(name) "pgmRCGstReal" #name
2006#define PGM_GST_NAME_R0_REAL_STR(name) "pgmR0GstReal" #name
2007#define PGM_GST_NAME_PROT(name) PGM_CTX(pgm,GstProt##name)
2008#define PGM_GST_NAME_RC_PROT_STR(name) "pgmRCGstProt" #name
2009#define PGM_GST_NAME_R0_PROT_STR(name) "pgmR0GstProt" #name
2010#define PGM_GST_NAME_32BIT(name) PGM_CTX(pgm,Gst32Bit##name)
2011#define PGM_GST_NAME_RC_32BIT_STR(name) "pgmRCGst32Bit" #name
2012#define PGM_GST_NAME_R0_32BIT_STR(name) "pgmR0Gst32Bit" #name
2013#define PGM_GST_NAME_PAE(name) PGM_CTX(pgm,GstPAE##name)
2014#define PGM_GST_NAME_RC_PAE_STR(name) "pgmRCGstPAE" #name
2015#define PGM_GST_NAME_R0_PAE_STR(name) "pgmR0GstPAE" #name
2016#define PGM_GST_NAME_AMD64(name) PGM_CTX(pgm,GstAMD64##name)
2017#define PGM_GST_NAME_RC_AMD64_STR(name) "pgmRCGstAMD64" #name
2018#define PGM_GST_NAME_R0_AMD64_STR(name) "pgmR0GstAMD64" #name
2019#define PGM_GST_PFN(name, pVCpu) ((pVCpu)->pgm.s.PGM_CTX(pfn,Gst##name))
2020#define PGM_GST_DECL(type, name) PGM_CTX_DECL(type) PGM_GST_NAME(name)
2021
2022#define PGM_SHW_NAME_32BIT(name) PGM_CTX(pgm,Shw32Bit##name)
2023#define PGM_SHW_NAME_RC_32BIT_STR(name) "pgmRCShw32Bit" #name
2024#define PGM_SHW_NAME_R0_32BIT_STR(name) "pgmR0Shw32Bit" #name
2025#define PGM_SHW_NAME_PAE(name) PGM_CTX(pgm,ShwPAE##name)
2026#define PGM_SHW_NAME_RC_PAE_STR(name) "pgmRCShwPAE" #name
2027#define PGM_SHW_NAME_R0_PAE_STR(name) "pgmR0ShwPAE" #name
2028#define PGM_SHW_NAME_AMD64(name) PGM_CTX(pgm,ShwAMD64##name)
2029#define PGM_SHW_NAME_RC_AMD64_STR(name) "pgmRCShwAMD64" #name
2030#define PGM_SHW_NAME_R0_AMD64_STR(name) "pgmR0ShwAMD64" #name
2031#define PGM_SHW_NAME_NESTED(name) PGM_CTX(pgm,ShwNested##name)
2032#define PGM_SHW_NAME_RC_NESTED_STR(name) "pgmRCShwNested" #name
2033#define PGM_SHW_NAME_R0_NESTED_STR(name) "pgmR0ShwNested" #name
2034#define PGM_SHW_NAME_EPT(name) PGM_CTX(pgm,ShwEPT##name)
2035#define PGM_SHW_NAME_RC_EPT_STR(name) "pgmRCShwEPT" #name
2036#define PGM_SHW_NAME_R0_EPT_STR(name) "pgmR0ShwEPT" #name
2037#define PGM_SHW_DECL(type, name) PGM_CTX_DECL(type) PGM_SHW_NAME(name)
2038#define PGM_SHW_PFN(name, pVCpu) ((pVCpu)->pgm.s.PGM_CTX(pfn,Shw##name))
2039
2040/* Shw_Gst */
2041#define PGM_BTH_NAME_32BIT_REAL(name) PGM_CTX(pgm,Bth32BitReal##name)
2042#define PGM_BTH_NAME_32BIT_PROT(name) PGM_CTX(pgm,Bth32BitProt##name)
2043#define PGM_BTH_NAME_32BIT_32BIT(name) PGM_CTX(pgm,Bth32Bit32Bit##name)
2044#define PGM_BTH_NAME_PAE_REAL(name) PGM_CTX(pgm,BthPAEReal##name)
2045#define PGM_BTH_NAME_PAE_PROT(name) PGM_CTX(pgm,BthPAEProt##name)
2046#define PGM_BTH_NAME_PAE_32BIT(name) PGM_CTX(pgm,BthPAE32Bit##name)
2047#define PGM_BTH_NAME_PAE_PAE(name) PGM_CTX(pgm,BthPAEPAE##name)
2048#define PGM_BTH_NAME_AMD64_PROT(name) PGM_CTX(pgm,BthAMD64Prot##name)
2049#define PGM_BTH_NAME_AMD64_AMD64(name) PGM_CTX(pgm,BthAMD64AMD64##name)
2050#define PGM_BTH_NAME_NESTED_REAL(name) PGM_CTX(pgm,BthNestedReal##name)
2051#define PGM_BTH_NAME_NESTED_PROT(name) PGM_CTX(pgm,BthNestedProt##name)
2052#define PGM_BTH_NAME_NESTED_32BIT(name) PGM_CTX(pgm,BthNested32Bit##name)
2053#define PGM_BTH_NAME_NESTED_PAE(name) PGM_CTX(pgm,BthNestedPAE##name)
2054#define PGM_BTH_NAME_NESTED_AMD64(name) PGM_CTX(pgm,BthNestedAMD64##name)
2055#define PGM_BTH_NAME_EPT_REAL(name) PGM_CTX(pgm,BthEPTReal##name)
2056#define PGM_BTH_NAME_EPT_PROT(name) PGM_CTX(pgm,BthEPTProt##name)
2057#define PGM_BTH_NAME_EPT_32BIT(name) PGM_CTX(pgm,BthEPT32Bit##name)
2058#define PGM_BTH_NAME_EPT_PAE(name) PGM_CTX(pgm,BthEPTPAE##name)
2059#define PGM_BTH_NAME_EPT_AMD64(name) PGM_CTX(pgm,BthEPTAMD64##name)
2060
2061#define PGM_BTH_NAME_RC_32BIT_REAL_STR(name) "pgmRCBth32BitReal" #name
2062#define PGM_BTH_NAME_RC_32BIT_PROT_STR(name) "pgmRCBth32BitProt" #name
2063#define PGM_BTH_NAME_RC_32BIT_32BIT_STR(name) "pgmRCBth32Bit32Bit" #name
2064#define PGM_BTH_NAME_RC_PAE_REAL_STR(name) "pgmRCBthPAEReal" #name
2065#define PGM_BTH_NAME_RC_PAE_PROT_STR(name) "pgmRCBthPAEProt" #name
2066#define PGM_BTH_NAME_RC_PAE_32BIT_STR(name) "pgmRCBthPAE32Bit" #name
2067#define PGM_BTH_NAME_RC_PAE_PAE_STR(name) "pgmRCBthPAEPAE" #name
2068#define PGM_BTH_NAME_RC_AMD64_AMD64_STR(name) "pgmRCBthAMD64AMD64" #name
2069#define PGM_BTH_NAME_RC_NESTED_REAL_STR(name) "pgmRCBthNestedReal" #name
2070#define PGM_BTH_NAME_RC_NESTED_PROT_STR(name) "pgmRCBthNestedProt" #name
2071#define PGM_BTH_NAME_RC_NESTED_32BIT_STR(name) "pgmRCBthNested32Bit" #name
2072#define PGM_BTH_NAME_RC_NESTED_PAE_STR(name) "pgmRCBthNestedPAE" #name
2073#define PGM_BTH_NAME_RC_NESTED_AMD64_STR(name) "pgmRCBthNestedAMD64" #name
2074#define PGM_BTH_NAME_RC_EPT_REAL_STR(name) "pgmRCBthEPTReal" #name
2075#define PGM_BTH_NAME_RC_EPT_PROT_STR(name) "pgmRCBthEPTProt" #name
2076#define PGM_BTH_NAME_RC_EPT_32BIT_STR(name) "pgmRCBthEPT32Bit" #name
2077#define PGM_BTH_NAME_RC_EPT_PAE_STR(name) "pgmRCBthEPTPAE" #name
2078#define PGM_BTH_NAME_RC_EPT_AMD64_STR(name) "pgmRCBthEPTAMD64" #name
2079#define PGM_BTH_NAME_R0_32BIT_REAL_STR(name) "pgmR0Bth32BitReal" #name
2080#define PGM_BTH_NAME_R0_32BIT_PROT_STR(name) "pgmR0Bth32BitProt" #name
2081#define PGM_BTH_NAME_R0_32BIT_32BIT_STR(name) "pgmR0Bth32Bit32Bit" #name
2082#define PGM_BTH_NAME_R0_PAE_REAL_STR(name) "pgmR0BthPAEReal" #name
2083#define PGM_BTH_NAME_R0_PAE_PROT_STR(name) "pgmR0BthPAEProt" #name
2084#define PGM_BTH_NAME_R0_PAE_32BIT_STR(name) "pgmR0BthPAE32Bit" #name
2085#define PGM_BTH_NAME_R0_PAE_PAE_STR(name) "pgmR0BthPAEPAE" #name
2086#define PGM_BTH_NAME_R0_AMD64_PROT_STR(name) "pgmR0BthAMD64Prot" #name
2087#define PGM_BTH_NAME_R0_AMD64_AMD64_STR(name) "pgmR0BthAMD64AMD64" #name
2088#define PGM_BTH_NAME_R0_NESTED_REAL_STR(name) "pgmR0BthNestedReal" #name
2089#define PGM_BTH_NAME_R0_NESTED_PROT_STR(name) "pgmR0BthNestedProt" #name
2090#define PGM_BTH_NAME_R0_NESTED_32BIT_STR(name) "pgmR0BthNested32Bit" #name
2091#define PGM_BTH_NAME_R0_NESTED_PAE_STR(name) "pgmR0BthNestedPAE" #name
2092#define PGM_BTH_NAME_R0_NESTED_AMD64_STR(name) "pgmR0BthNestedAMD64" #name
2093#define PGM_BTH_NAME_R0_EPT_REAL_STR(name) "pgmR0BthEPTReal" #name
2094#define PGM_BTH_NAME_R0_EPT_PROT_STR(name) "pgmR0BthEPTProt" #name
2095#define PGM_BTH_NAME_R0_EPT_32BIT_STR(name) "pgmR0BthEPT32Bit" #name
2096#define PGM_BTH_NAME_R0_EPT_PAE_STR(name) "pgmR0BthEPTPAE" #name
2097#define PGM_BTH_NAME_R0_EPT_AMD64_STR(name) "pgmR0BthEPTAMD64" #name
2098
2099#define PGM_BTH_DECL(type, name) PGM_CTX_DECL(type) PGM_BTH_NAME(name)
2100#define PGM_BTH_PFN(name, pVCpu) ((pVCpu)->pgm.s.PGM_CTX(pfn,Bth##name))
2101/** @} */
2102
2103/**
2104 * Data for each paging mode.
2105 */
2106typedef struct PGMMODEDATA
2107{
2108 /** The guest mode type. */
2109 uint32_t uGstType;
2110 /** The shadow mode type. */
2111 uint32_t uShwType;
2112
2113 /** @name Function pointers for Shadow paging.
2114 * @{
2115 */
2116 DECLR3CALLBACKMEMBER(int, pfnR3ShwRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
2117 DECLR3CALLBACKMEMBER(int, pfnR3ShwExit,(PVMCPU pVCpu));
2118 DECLR3CALLBACKMEMBER(int, pfnR3ShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
2119 DECLR3CALLBACKMEMBER(int, pfnR3ShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2120
2121 DECLRCCALLBACKMEMBER(int, pfnRCShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
2122 DECLRCCALLBACKMEMBER(int, pfnRCShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2123
2124 DECLR0CALLBACKMEMBER(int, pfnR0ShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
2125 DECLR0CALLBACKMEMBER(int, pfnR0ShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2126 /** @} */
2127
2128 /** @name Function pointers for Guest paging.
2129 * @{
2130 */
2131 DECLR3CALLBACKMEMBER(int, pfnR3GstRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
2132 DECLR3CALLBACKMEMBER(int, pfnR3GstExit,(PVMCPU pVCpu));
2133 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
2134 DECLR3CALLBACKMEMBER(int, pfnR3GstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2135 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
2136 DECLRCCALLBACKMEMBER(int, pfnRCGstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
2137 DECLRCCALLBACKMEMBER(int, pfnRCGstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2138 DECLRCCALLBACKMEMBER(int, pfnRCGstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
2139 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
2140 DECLR0CALLBACKMEMBER(int, pfnR0GstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2141 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
2142 /** @} */
2143
2144 /** @name Function pointers for Both Shadow and Guest paging.
2145 * @{
2146 */
2147 DECLR3CALLBACKMEMBER(int, pfnR3BthRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
2148 /* no pfnR3BthTrap0eHandler */
2149 DECLR3CALLBACKMEMBER(int, pfnR3BthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2150 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
2151 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncPage,(PVMCPU pVCpu, X86PDE PdeSrc, RTGCPTR GCPtrPage, unsigned cPages, unsigned uError));
2152 DECLR3CALLBACKMEMBER(int, pfnR3BthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2153 DECLR3CALLBACKMEMBER(int, pfnR3BthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
2154#ifdef VBOX_STRICT
2155 DECLR3CALLBACKMEMBER(unsigned, pfnR3BthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
2156#endif
2157 DECLR3CALLBACKMEMBER(int, pfnR3BthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
2158 DECLR3CALLBACKMEMBER(int, pfnR3BthUnmapCR3,(PVMCPU pVCpu));
2159
2160 DECLRCCALLBACKMEMBER(int, pfnRCBthTrap0eHandler,(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
2161 DECLRCCALLBACKMEMBER(int, pfnRCBthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2162 DECLRCCALLBACKMEMBER(int, pfnRCBthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
2163 DECLRCCALLBACKMEMBER(int, pfnRCBthSyncPage,(PVMCPU pVCpu, X86PDE PdeSrc, RTGCPTR GCPtrPage, unsigned cPages, unsigned uError));
2164 DECLRCCALLBACKMEMBER(int, pfnRCBthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2165 DECLRCCALLBACKMEMBER(int, pfnRCBthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
2166#ifdef VBOX_STRICT
2167 DECLRCCALLBACKMEMBER(unsigned, pfnRCBthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
2168#endif
2169 DECLRCCALLBACKMEMBER(int, pfnRCBthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
2170 DECLRCCALLBACKMEMBER(int, pfnRCBthUnmapCR3,(PVMCPU pVCpu));
2171
2172 DECLR0CALLBACKMEMBER(int, pfnR0BthTrap0eHandler,(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
2173 DECLR0CALLBACKMEMBER(int, pfnR0BthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2174 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
2175 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncPage,(PVMCPU pVCpu, X86PDE PdeSrc, RTGCPTR GCPtrPage, unsigned cPages, unsigned uError));
2176 DECLR0CALLBACKMEMBER(int, pfnR0BthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2177 DECLR0CALLBACKMEMBER(int, pfnR0BthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
2178#ifdef VBOX_STRICT
2179 DECLR0CALLBACKMEMBER(unsigned, pfnR0BthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
2180#endif
2181 DECLR0CALLBACKMEMBER(int, pfnR0BthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
2182 DECLR0CALLBACKMEMBER(int, pfnR0BthUnmapCR3,(PVMCPU pVCpu));
2183 /** @} */
2184} PGMMODEDATA, *PPGMMODEDATA;
2185
2186
2187
2188/**
2189 * Converts a PGM pointer into a VM pointer.
2190 * @returns Pointer to the VM structure the PGM is part of.
2191 * @param pPGM Pointer to PGM instance data.
2192 */
2193#define PGM2VM(pPGM) ( (PVM)((char*)pPGM - pPGM->offVM) )
2194
2195/**
2196 * PGM Data (part of VM)
2197 */
2198typedef struct PGM
2199{
2200 /** Offset to the VM structure. */
2201 RTINT offVM;
2202 /** Offset of the PGMCPU structure relative to VMCPU. */
2203 RTINT offVCpuPGM;
2204
2205 /** @cfgm{RamPreAlloc, boolean, false}
2206 * Indicates whether the base RAM should all be allocated before starting
2207 * the VM (default), or if it should be allocated when first written to.
2208 */
2209 bool fRamPreAlloc;
2210 /** Alignment padding. */
2211 bool afAlignment0[7];
2212
2213 /** What needs syncing (PGM_SYNC_*).
2214 * This is used to queue operations for PGMSyncCR3, PGMInvalidatePage,
2215 * PGMFlushTLB, and PGMR3Load. */
2216 RTUINT fGlobalSyncFlags;
2217
2218 /*
2219 * This will be redefined at least two more times before we're done, I'm sure.
2220 * The current code is only to get on with the coding.
2221 * - 2004-06-10: initial version, bird.
2222 * - 2004-07-02: 1st time, bird.
2223 * - 2004-10-18: 2nd time, bird.
2224 * - 2005-07-xx: 3rd time, bird.
2225 */
2226
2227 /** Pointer to the page table entries for the dynamic page mapping area - GCPtr. */
2228 RCPTRTYPE(PX86PTE) paDynPageMap32BitPTEsGC;
2229 /** Pointer to the page table entries for the dynamic page mapping area - GCPtr. */
2230 RCPTRTYPE(PX86PTEPAE) paDynPageMapPaePTEsGC;
2231
2232 /** The host paging mode. (This is what SUPLib reports.) */
2233 SUPPAGINGMODE enmHostMode;
2234
2235 /** 4 MB page mask; 32 or 36 bits depending on PSE-36 (identical for all VCPUs) */
2236 RTGCPHYS GCPhys4MBPSEMask;
2237
2238 /** Pointer to the list of RAM ranges (Phys GC -> Phys HC conversion) - for R3.
2239 * This is sorted by physical address and contains no overlapping ranges. */
2240 R3PTRTYPE(PPGMRAMRANGE) pRamRangesR3;
2241 /** R0 pointer corresponding to PGM::pRamRangesR3. */
2242 R0PTRTYPE(PPGMRAMRANGE) pRamRangesR0;
2243 /** RC pointer corresponding to PGM::pRamRangesR3. */
2244 RCPTRTYPE(PPGMRAMRANGE) pRamRangesRC;
2245 RTRCPTR alignment4; /**< structure alignment. */
2246
2247 /** Pointer to the list of ROM ranges - for R3.
2248 * This is sorted by physical address and contains no overlapping ranges. */
2249 R3PTRTYPE(PPGMROMRANGE) pRomRangesR3;
2250 /** R0 pointer corresponding to PGM::pRomRangesR3. */
2251 R0PTRTYPE(PPGMROMRANGE) pRomRangesR0;
2252 /** RC pointer corresponding to PGM::pRomRangesR3. */
2253 RCPTRTYPE(PPGMROMRANGE) pRomRangesRC;
2254 /** Alignment padding. */
2255 RTRCPTR GCPtrPadding2;
2256
2257 /** Pointer to the list of MMIO2 ranges - for R3.
2258 * Registration order. */
2259 R3PTRTYPE(PPGMMMIO2RANGE) pMmio2RangesR3;
2260
2261 /** PGM offset based trees - R3 Ptr. */
2262 R3PTRTYPE(PPGMTREES) pTreesR3;
2263 /** PGM offset based trees - R0 Ptr. */
2264 R0PTRTYPE(PPGMTREES) pTreesR0;
2265 /** PGM offset based trees - RC Ptr. */
2266 RCPTRTYPE(PPGMTREES) pTreesRC;
2267
2268 /** Linked list of GC mappings - for RC.
2269 * The list is sorted ascending on address.
2270 */
2271 RCPTRTYPE(PPGMMAPPING) pMappingsRC;
2272 /** Linked list of GC mappings - for HC.
2273 * The list is sorted ascending on address.
2274 */
2275 R3PTRTYPE(PPGMMAPPING) pMappingsR3;
2276 /** Linked list of GC mappings - for R0.
2277 * The list is sorted ascending on address.
2278 */
2279 R0PTRTYPE(PPGMMAPPING) pMappingsR0;
2280
2281 /** Pointer to the 5 page CR3 content mapping.
2282 * The first page is always the CR3 (in some form) while the 4 other pages
2283 * are used of the PDs in PAE mode. */
2284 RTGCPTR GCPtrCR3Mapping;
2285#if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
2286 uint32_t u32Alignment;
2287#endif
2288
2289 /** Indicates that PGMR3FinalizeMappings has been called and that further
2290 * PGMR3MapIntermediate calls will be rejected. */
2291 bool fFinalizedMappings;
2292 /** If set no conflict checks are required. (boolean) */
2293 bool fMappingsFixed;
2294 /** If set, then no mappings are put into the shadow page table. (boolean) */
2295 bool fDisableMappings;
2296 /** Size of fixed mapping */
2297 uint32_t cbMappingFixed;
2298 /** Base address (GC) of fixed mapping */
2299 RTGCPTR GCPtrMappingFixed;
2300 /** The address of the previous RAM range mapping. */
2301 RTGCPTR GCPtrPrevRamRangeMapping;
2302
2303 /** @name Intermediate Context
2304 * @{ */
2305 /** Pointer to the intermediate page directory - Normal. */
2306 R3PTRTYPE(PX86PD) pInterPD;
2307 /** Pointer to the intermedate page tables - Normal.
2308 * There are two page tables, one for the identity mapping and one for
2309 * the host context mapping (of the core code). */
2310 R3PTRTYPE(PX86PT) apInterPTs[2];
2311 /** Pointer to the intermedate page tables - PAE. */
2312 R3PTRTYPE(PX86PTPAE) apInterPaePTs[2];
2313 /** Pointer to the intermedate page directory - PAE. */
2314 R3PTRTYPE(PX86PDPAE) apInterPaePDs[4];
2315 /** Pointer to the intermedate page directory - PAE. */
2316 R3PTRTYPE(PX86PDPT) pInterPaePDPT;
2317 /** Pointer to the intermedate page-map level 4 - AMD64. */
2318 R3PTRTYPE(PX86PML4) pInterPaePML4;
2319 /** Pointer to the intermedate page directory - AMD64. */
2320 R3PTRTYPE(PX86PDPT) pInterPaePDPT64;
2321 /** The Physical Address (HC) of the intermediate Page Directory - Normal. */
2322 RTHCPHYS HCPhysInterPD;
2323 /** The Physical Address (HC) of the intermediate Page Directory Pointer Table - PAE. */
2324 RTHCPHYS HCPhysInterPaePDPT;
2325 /** The Physical Address (HC) of the intermediate Page Map Level 4 table - AMD64. */
2326 RTHCPHYS HCPhysInterPaePML4;
2327 /** @} */
2328
2329 /** Base address of the dynamic page mapping area.
2330 * The array is MM_HYPER_DYNAMIC_SIZE bytes big.
2331 */
2332 RCPTRTYPE(uint8_t *) pbDynPageMapBaseGC;
2333 /** The index of the last entry used in the dynamic page mapping area. */
2334 RTUINT iDynPageMapLast;
2335 /** Cache containing the last entries in the dynamic page mapping area.
2336 * The cache size is covering half of the mapping area. */
2337 RTHCPHYS aHCPhysDynPageMapCache[MM_HYPER_DYNAMIC_SIZE >> (PAGE_SHIFT + 1)];
2338 /** Keep a lock counter for the full (!) mapping area. */
2339 uint32_t aLockedDynPageMapCache[MM_HYPER_DYNAMIC_SIZE >> (PAGE_SHIFT)];
2340
2341 /** The address of the ring-0 mapping cache if we're making use of it. */
2342 RTR0PTR pvR0DynMapUsed;
2343
2344 /** PGM critical section.
2345 * This protects the physical & virtual access handlers, ram ranges,
2346 * and the page flag updating (some of it anyway).
2347 */
2348 PDMCRITSECT CritSect;
2349
2350 /** Pointer to SHW+GST mode data (function pointers).
2351 * The index into this table is made up from */
2352 R3PTRTYPE(PPGMMODEDATA) paModeData;
2353
2354 /** Shadow Page Pool - R3 Ptr. */
2355 R3PTRTYPE(PPGMPOOL) pPoolR3;
2356 /** Shadow Page Pool - R0 Ptr. */
2357 R0PTRTYPE(PPGMPOOL) pPoolR0;
2358 /** Shadow Page Pool - RC Ptr. */
2359 RCPTRTYPE(PPGMPOOL) pPoolRC;
2360
2361 /** We're not in a state which permits writes to guest memory.
2362 * (Only used in strict builds.) */
2363 bool fNoMorePhysWrites;
2364
2365 /** Flush the cache on the next access. */
2366 bool fPhysCacheFlushPending;
2367/** @todo r=bird: Fix member names!*/
2368 /** PGMPhysRead cache */
2369 PGMPHYSCACHE pgmphysreadcache;
2370 /** PGMPhysWrite cache */
2371 PGMPHYSCACHE pgmphyswritecache;
2372
2373 /**
2374 * Data associated with managing the ring-3 mappings of the allocation chunks.
2375 */
2376 struct
2377 {
2378 /** The chunk tree, ordered by chunk id. */
2379#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
2380 R3PTRTYPE(PAVLU32NODECORE) pTree;
2381#else
2382 R3R0PTRTYPE(PAVLU32NODECORE) pTree;
2383#endif
2384 /** The chunk mapping TLB. */
2385 PGMCHUNKR3MAPTLB Tlb;
2386 /** The number of mapped chunks. */
2387 uint32_t c;
2388 /** The maximum number of mapped chunks.
2389 * @cfgm PGM/MaxRing3Chunks */
2390 uint32_t cMax;
2391 /** The chunk age tree, ordered by ageing sequence number. */
2392 R3PTRTYPE(PAVLLU32NODECORE) pAgeTree;
2393 /** The current time. */
2394 uint32_t iNow;
2395 /** Number of pgmR3PhysChunkFindUnmapCandidate calls left to the next ageing. */
2396 uint32_t AgeingCountdown;
2397 } ChunkR3Map;
2398
2399 /**
2400 * The page mapping TLB for ring-3 and (for the time being) ring-0.
2401 */
2402 PGMPAGER3MAPTLB PhysTlbHC;
2403
2404 /** @name The zero page.
2405 * @{ */
2406 /** The host physical address of the zero page. */
2407 RTHCPHYS HCPhysZeroPg;
2408 /** The ring-3 mapping of the zero page. */
2409 RTR3PTR pvZeroPgR3;
2410 /** The ring-0 mapping of the zero page. */
2411 RTR0PTR pvZeroPgR0;
2412 /** The GC mapping of the zero page. */
2413 RTGCPTR pvZeroPgRC;
2414#if GC_ARCH_BITS != 32
2415 uint32_t u32ZeroAlignment; /**< Alignment padding. */
2416#endif
2417 /** @}*/
2418
2419 /** The number of handy pages. */
2420 uint32_t cHandyPages;
2421 /**
2422 * Array of handy pages.
2423 *
2424 * This array is used in a two way communication between pgmPhysAllocPage
2425 * and GMMR0AllocateHandyPages, with PGMR3PhysAllocateHandyPages serving as
2426 * an intermediary.
2427 *
2428 * The size of this array is important, see pgmPhysEnsureHandyPage for details.
2429 * (The current size of 32 pages, means 128 KB of handy memory.)
2430 */
2431 GMMPAGEDESC aHandyPages[PGM_HANDY_PAGES];
2432
2433 /** @name Error injection.
2434 * @{ */
2435 /** Inject handy page allocation errors pretending we're completely out of
2436 * memory. */
2437 bool volatile fErrInjHandyPages;
2438 /** Padding. */
2439 bool afReserved[7];
2440 /** @} */
2441
2442 /** @name Release Statistics
2443 * @{ */
2444 uint32_t cAllPages; /**< The total number of pages. (Should be Private + Shared + Zero.) */
2445 uint32_t cPrivatePages; /**< The number of private pages. */
2446 uint32_t cSharedPages; /**< The number of shared pages. */
2447 uint32_t cZeroPages; /**< The number of zero backed pages. */
2448
2449 /** The number of times we were forced to change the hypervisor region location. */
2450 STAMCOUNTER cRelocations;
2451 /** @} */
2452
2453#ifdef VBOX_WITH_STATISTICS /** @todo move this chunk to the heap. */
2454 /* R3 only: */
2455 STAMCOUNTER StatR3DetectedConflicts; /**< R3: Number of times PGMR3MapHasConflicts() detected a conflict. */
2456 STAMPROFILE StatR3ResolveConflict; /**< R3: pgmR3SyncPTResolveConflict() profiling (includes the entire relocation). */
2457
2458 STAMCOUNTER StatRZChunkR3MapTlbHits; /**< RC/R0: Ring-3/0 chunk mapper TLB hits. */
2459 STAMCOUNTER StatRZChunkR3MapTlbMisses; /**< RC/R0: Ring-3/0 chunk mapper TLB misses. */
2460 STAMCOUNTER StatRZPageMapTlbHits; /**< RC/R0: Ring-3/0 page mapper TLB hits. */
2461 STAMCOUNTER StatRZPageMapTlbMisses; /**< RC/R0: Ring-3/0 page mapper TLB misses. */
2462 STAMCOUNTER StatR3ChunkR3MapTlbHits; /**< R3: Ring-3/0 chunk mapper TLB hits. */
2463 STAMCOUNTER StatR3ChunkR3MapTlbMisses; /**< R3: Ring-3/0 chunk mapper TLB misses. */
2464 STAMCOUNTER StatR3PageMapTlbHits; /**< R3: Ring-3/0 page mapper TLB hits. */
2465 STAMCOUNTER StatR3PageMapTlbMisses; /**< R3: Ring-3/0 page mapper TLB misses. */
2466 STAMPROFILE StatRZSyncCR3HandlerVirtualReset; /**< RC/R0: Profiling of the virtual handler resets. */
2467 STAMPROFILE StatRZSyncCR3HandlerVirtualUpdate; /**< RC/R0: Profiling of the virtual handler updates. */
2468 STAMPROFILE StatR3SyncCR3HandlerVirtualReset; /**< R3: Profiling of the virtual handler resets. */
2469 STAMPROFILE StatR3SyncCR3HandlerVirtualUpdate; /**< R3: Profiling of the virtual handler updates. */
2470 STAMCOUNTER StatR3PhysHandlerReset; /**< R3: The number of times PGMHandlerPhysicalReset is called. */
2471 STAMCOUNTER StatRZPhysHandlerReset; /**< RC/R0: The number of times PGMHandlerPhysicalReset is called. */
2472 STAMPROFILE StatRZVirtHandlerSearchByPhys; /**< RC/R0: Profiling of pgmHandlerVirtualFindByPhysAddr. */
2473 STAMPROFILE StatR3VirtHandlerSearchByPhys; /**< R3: Profiling of pgmHandlerVirtualFindByPhysAddr. */
2474 STAMCOUNTER StatRZPageReplaceShared; /**< RC/R0: Times a shared page has been replaced by a private one. */
2475 STAMCOUNTER StatRZPageReplaceZero; /**< RC/R0: Times the zero page has been replaced by a private one. */
2476/// @todo STAMCOUNTER StatRZPageHandyAllocs; /**< RC/R0: The number of times we've executed GMMR3AllocateHandyPages. */
2477 STAMCOUNTER StatR3PageReplaceShared; /**< R3: Times a shared page has been replaced by a private one. */
2478 STAMCOUNTER StatR3PageReplaceZero; /**< R3: Times the zero page has been replaced by a private one. */
2479/// @todo STAMCOUNTER StatR3PageHandyAllocs; /**< R3: The number of times we've executed GMMR3AllocateHandyPages. */
2480
2481 /* RC only: */
2482 STAMCOUNTER StatRCDynMapCacheMisses; /**< RC: The number of dynamic page mapping cache misses */
2483 STAMCOUNTER StatRCDynMapCacheHits; /**< RC: The number of dynamic page mapping cache hits */
2484 STAMCOUNTER StatRCInvlPgConflict; /**< RC: Number of times PGMInvalidatePage() detected a mapping conflict. */
2485 STAMCOUNTER StatRCInvlPgSyncMonCR3; /**< RC: Number of times PGMInvalidatePage() ran into PGM_SYNC_MONITOR_CR3. */
2486
2487# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2488 STAMCOUNTER StatTrackVirgin; /**< The number of first time shadowings. */
2489 STAMCOUNTER StatTrackAliased; /**< The number of times switching to cRef2, i.e. the page is being shadowed by two PTs. */
2490 STAMCOUNTER StatTrackAliasedMany; /**< The number of times we're tracking using cRef2. */
2491 STAMCOUNTER StatTrackAliasedLots; /**< The number of times we're hitting pages which has overflowed cRef2. */
2492 STAMCOUNTER StatTrackOverflows; /**< The number of times the extent list grows to long. */
2493 STAMPROFILE StatTrackDeref; /**< Profiling of SyncPageWorkerTrackDeref (expensive). */
2494# endif
2495#endif
2496} PGM;
2497/** Pointer to the PGM instance data. */
2498typedef PGM *PPGM;
2499
2500
2501/**
2502 * Converts a PGMCPU pointer into a VM pointer.
2503 * @returns Pointer to the VM structure the PGM is part of.
2504 * @param pPGM Pointer to PGMCPU instance data.
2505 */
2506#define PGMCPU2VM(pPGM) ( (PVM)((char*)pPGM - pPGM->offVM) )
2507
2508/**
2509 * Converts a PGMCPU pointer into a PGM pointer.
2510 * @returns Pointer to the VM structure the PGM is part of.
2511 * @param pPGM Pointer to PGMCPU instance data.
2512 */
2513#define PGMCPU2PGM(pPGMCpu) ( (PPGM)((char*)pPGMCpu - pPGMCpu->offPGM) )
2514
2515/**
2516 * PGMCPU Data (part of VMCPU).
2517 */
2518typedef struct PGMCPU
2519{
2520 /** Offset to the VM structure. */
2521 RTINT offVM;
2522 /** Offset to the VMCPU structure. */
2523 RTINT offVCpu;
2524 /** Offset of the PGM structure relative to VMCPU. */
2525 RTINT offPGM;
2526 RTINT uPadding0; /**< structure size alignment. */
2527
2528#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
2529 /** Automatically tracked physical memory mapping set.
2530 * Ring-0 and strict raw-mode builds. */
2531 PGMMAPSET AutoSet;
2532#endif
2533
2534 /** A20 gate mask.
2535 * Our current approach to A20 emulation is to let REM do it and don't bother
2536 * anywhere else. The interesting Guests will be operating with it enabled anyway.
2537 * But whould need arrise, we'll subject physical addresses to this mask. */
2538 RTGCPHYS GCPhysA20Mask;
2539 /** A20 gate state - boolean! */
2540 bool fA20Enabled;
2541
2542 /** What needs syncing (PGM_SYNC_*).
2543 * This is used to queue operations for PGMSyncCR3, PGMInvalidatePage,
2544 * PGMFlushTLB, and PGMR3Load. */
2545 RTUINT fSyncFlags;
2546
2547 /** The shadow paging mode. */
2548 PGMMODE enmShadowMode;
2549 /** The guest paging mode. */
2550 PGMMODE enmGuestMode;
2551
2552 /** The current physical address representing in the guest CR3 register. */
2553 RTGCPHYS GCPhysCR3;
2554
2555 /** @name 32-bit Guest Paging.
2556 * @{ */
2557 /** The guest's page directory, R3 pointer. */
2558 R3PTRTYPE(PX86PD) pGst32BitPdR3;
2559#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
2560 /** The guest's page directory, R0 pointer. */
2561 R0PTRTYPE(PX86PD) pGst32BitPdR0;
2562#endif
2563 /** The guest's page directory, static RC mapping. */
2564 RCPTRTYPE(PX86PD) pGst32BitPdRC;
2565 /** @} */
2566
2567 /** @name PAE Guest Paging.
2568 * @{ */
2569 /** The guest's page directory pointer table, static RC mapping. */
2570 RCPTRTYPE(PX86PDPT) pGstPaePdptRC;
2571 /** The guest's page directory pointer table, R3 pointer. */
2572 R3PTRTYPE(PX86PDPT) pGstPaePdptR3;
2573#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
2574 /** The guest's page directory pointer table, R0 pointer. */
2575 R0PTRTYPE(PX86PDPT) pGstPaePdptR0;
2576#endif
2577
2578 /** The guest's page directories, R3 pointers.
2579 * These are individual pointers and don't have to be adjecent.
2580 * These don't have to be up-to-date - use pgmGstGetPaePD() to access them. */
2581 R3PTRTYPE(PX86PDPAE) apGstPaePDsR3[4];
2582 /** The guest's page directories, R0 pointers.
2583 * Same restrictions as apGstPaePDsR3. */
2584#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
2585 R0PTRTYPE(PX86PDPAE) apGstPaePDsR0[4];
2586#endif
2587 /** The guest's page directories, static GC mapping.
2588 * Unlike the R3/R0 array the first entry can be accessed as a 2048 entry PD.
2589 * These don't have to be up-to-date - use pgmGstGetPaePD() to access them. */
2590 RCPTRTYPE(PX86PDPAE) apGstPaePDsRC[4];
2591 /** The physical addresses of the guest page directories (PAE) pointed to by apGstPagePDsHC/GC. */
2592 RTGCPHYS aGCPhysGstPaePDs[4];
2593 /** The physical addresses of the monitored guest page directories (PAE). */
2594 RTGCPHYS aGCPhysGstPaePDsMonitored[4];
2595 /** @} */
2596
2597 /** @name AMD64 Guest Paging.
2598 * @{ */
2599 /** The guest's page directory pointer table, R3 pointer. */
2600 R3PTRTYPE(PX86PML4) pGstAmd64Pml4R3;
2601#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
2602 /** The guest's page directory pointer table, R0 pointer. */
2603 R0PTRTYPE(PX86PML4) pGstAmd64Pml4R0;
2604#endif
2605 /** @} */
2606
2607 /** Pointer to the page of the current active CR3 - R3 Ptr. */
2608 R3PTRTYPE(PPGMPOOLPAGE) pShwPageCR3R3;
2609 /** Pointer to the page of the current active CR3 - R0 Ptr. */
2610 R0PTRTYPE(PPGMPOOLPAGE) pShwPageCR3R0;
2611 /** Pointer to the page of the current active CR3 - RC Ptr. */
2612 RCPTRTYPE(PPGMPOOLPAGE) pShwPageCR3RC;
2613 /* The shadow page pool index of the user table as specified during allocation; useful for freeing root pages */
2614 uint32_t iShwUser;
2615 /* The index into the user table (shadowed) as specified during allocation; useful for freeing root pages. */
2616 uint32_t iShwUserTable;
2617# if HC_ARCH_BITS == 64
2618 RTRCPTR alignment6; /**< structure size alignment. */
2619# endif
2620 /** @} */
2621
2622 /** @name Function pointers for Shadow paging.
2623 * @{
2624 */
2625 DECLR3CALLBACKMEMBER(int, pfnR3ShwRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
2626 DECLR3CALLBACKMEMBER(int, pfnR3ShwExit,(PVMCPU pVCpu));
2627 DECLR3CALLBACKMEMBER(int, pfnR3ShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
2628 DECLR3CALLBACKMEMBER(int, pfnR3ShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2629
2630 DECLRCCALLBACKMEMBER(int, pfnRCShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
2631 DECLRCCALLBACKMEMBER(int, pfnRCShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2632
2633 DECLR0CALLBACKMEMBER(int, pfnR0ShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
2634 DECLR0CALLBACKMEMBER(int, pfnR0ShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2635
2636 /** @} */
2637
2638 /** @name Function pointers for Guest paging.
2639 * @{
2640 */
2641 DECLR3CALLBACKMEMBER(int, pfnR3GstRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
2642 DECLR3CALLBACKMEMBER(int, pfnR3GstExit,(PVMCPU pVCpu));
2643 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
2644 DECLR3CALLBACKMEMBER(int, pfnR3GstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2645 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
2646 DECLRCCALLBACKMEMBER(int, pfnRCGstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
2647 DECLRCCALLBACKMEMBER(int, pfnRCGstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2648 DECLRCCALLBACKMEMBER(int, pfnRCGstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
2649#if HC_ARCH_BITS == 64
2650 RTRCPTR alignment3; /**< structure size alignment. */
2651#endif
2652
2653 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
2654 DECLR0CALLBACKMEMBER(int, pfnR0GstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2655 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
2656 /** @} */
2657
2658 /** @name Function pointers for Both Shadow and Guest paging.
2659 * @{
2660 */
2661 DECLR3CALLBACKMEMBER(int, pfnR3BthRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
2662 /* no pfnR3BthTrap0eHandler */
2663 DECLR3CALLBACKMEMBER(int, pfnR3BthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2664 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
2665 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncPage,(PVMCPU pVCpu, X86PDE PdeSrc, RTGCPTR GCPtrPage, unsigned cPages, unsigned uError));
2666 DECLR3CALLBACKMEMBER(int, pfnR3BthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2667 DECLR3CALLBACKMEMBER(int, pfnR3BthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
2668 DECLR3CALLBACKMEMBER(unsigned, pfnR3BthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
2669 DECLR3CALLBACKMEMBER(int, pfnR3BthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
2670 DECLR3CALLBACKMEMBER(int, pfnR3BthUnmapCR3,(PVMCPU pVCpu));
2671
2672 DECLR0CALLBACKMEMBER(int, pfnR0BthTrap0eHandler,(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
2673 DECLR0CALLBACKMEMBER(int, pfnR0BthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2674 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
2675 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncPage,(PVMCPU pVCpu, X86PDE PdeSrc, RTGCPTR GCPtrPage, unsigned cPages, unsigned uError));
2676 DECLR0CALLBACKMEMBER(int, pfnR0BthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2677 DECLR0CALLBACKMEMBER(int, pfnR0BthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
2678 DECLR0CALLBACKMEMBER(unsigned, pfnR0BthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
2679 DECLR0CALLBACKMEMBER(int, pfnR0BthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
2680 DECLR0CALLBACKMEMBER(int, pfnR0BthUnmapCR3,(PVMCPU pVCpu));
2681
2682 DECLRCCALLBACKMEMBER(int, pfnRCBthTrap0eHandler,(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
2683 DECLRCCALLBACKMEMBER(int, pfnRCBthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2684 DECLRCCALLBACKMEMBER(int, pfnRCBthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
2685 DECLRCCALLBACKMEMBER(int, pfnRCBthSyncPage,(PVMCPU pVCpu, X86PDE PdeSrc, RTGCPTR GCPtrPage, unsigned cPages, unsigned uError));
2686 DECLRCCALLBACKMEMBER(int, pfnRCBthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2687 DECLRCCALLBACKMEMBER(int, pfnRCBthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
2688 DECLRCCALLBACKMEMBER(unsigned, pfnRCBthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
2689 DECLRCCALLBACKMEMBER(int, pfnRCBthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
2690 DECLRCCALLBACKMEMBER(int, pfnRCBthUnmapCR3,(PVMCPU pVCpu));
2691#if HC_ARCH_BITS == 64
2692 RTRCPTR alignment2; /**< structure size alignment. */
2693#endif
2694 /** @} */
2695
2696 /** @name Release Statistics
2697 * @{ */
2698 /** The number of times the guest has switched mode since last reset or statistics reset. */
2699 STAMCOUNTER cGuestModeChanges;
2700 /** @} */
2701
2702#ifdef VBOX_WITH_STATISTICS /** @todo move this chunk to the heap. */
2703 /** @name Statistics
2704 * @{ */
2705 /** RC: Which statistic this \#PF should be attributed to. */
2706 RCPTRTYPE(PSTAMPROFILE) pStatTrap0eAttributionRC;
2707 RTRCPTR padding0;
2708 /** R0: Which statistic this \#PF should be attributed to. */
2709 R0PTRTYPE(PSTAMPROFILE) pStatTrap0eAttributionR0;
2710 RTR0PTR padding1;
2711
2712 /* Common */
2713 STAMCOUNTER StatSyncPtPD[X86_PG_ENTRIES]; /**< SyncPT - PD distribution. */
2714 STAMCOUNTER StatSyncPagePD[X86_PG_ENTRIES]; /**< SyncPage - PD distribution. */
2715
2716 /* R0 only: */
2717 STAMCOUNTER StatR0DynMapMigrateInvlPg; /**< R0: invlpg in PGMDynMapMigrateAutoSet. */
2718 STAMPROFILE StatR0DynMapGCPageInl; /**< R0: Calls to pgmR0DynMapGCPageInlined. */
2719 STAMCOUNTER StatR0DynMapGCPageInlHits; /**< R0: Hash table lookup hits. */
2720 STAMCOUNTER StatR0DynMapGCPageInlMisses; /**< R0: Misses that falls back to code common with PGMDynMapHCPage. */
2721 STAMCOUNTER StatR0DynMapGCPageInlRamHits; /**< R0: 1st ram range hits. */
2722 STAMCOUNTER StatR0DynMapGCPageInlRamMisses; /**< R0: 1st ram range misses, takes slow path. */
2723 STAMPROFILE StatR0DynMapHCPageInl; /**< R0: Calls to pgmR0DynMapHCPageInlined. */
2724 STAMCOUNTER StatR0DynMapHCPageInlHits; /**< R0: Hash table lookup hits. */
2725 STAMCOUNTER StatR0DynMapHCPageInlMisses; /**< R0: Misses that falls back to code common with PGMDynMapHCPage. */
2726 STAMPROFILE StatR0DynMapHCPage; /**< R0: Calls to PGMDynMapHCPage. */
2727 STAMCOUNTER StatR0DynMapSetOptimize; /**< R0: Calls to pgmDynMapOptimizeAutoSet. */
2728 STAMCOUNTER StatR0DynMapSetSearchFlushes; /**< R0: Set search restorting to subset flushes. */
2729 STAMCOUNTER StatR0DynMapSetSearchHits; /**< R0: Set search hits. */
2730 STAMCOUNTER StatR0DynMapSetSearchMisses; /**< R0: Set search misses. */
2731 STAMCOUNTER StatR0DynMapPage; /**< R0: Calls to pgmR0DynMapPage. */
2732 STAMCOUNTER StatR0DynMapPageHits0; /**< R0: Hits at iPage+0. */
2733 STAMCOUNTER StatR0DynMapPageHits1; /**< R0: Hits at iPage+1. */
2734 STAMCOUNTER StatR0DynMapPageHits2; /**< R0: Hits at iPage+2. */
2735 STAMCOUNTER StatR0DynMapPageInvlPg; /**< R0: invlpg. */
2736 STAMCOUNTER StatR0DynMapPageSlow; /**< R0: Calls to pgmR0DynMapPageSlow. */
2737 STAMCOUNTER StatR0DynMapPageSlowLoopHits; /**< R0: Hits in the pgmR0DynMapPageSlow search loop. */
2738 STAMCOUNTER StatR0DynMapPageSlowLoopMisses; /**< R0: Misses in the pgmR0DynMapPageSlow search loop. */
2739 //STAMCOUNTER StatR0DynMapPageSlowLostHits; /**< R0: Lost hits. */
2740 STAMCOUNTER StatR0DynMapSubsets; /**< R0: Times PGMDynMapPushAutoSubset was called. */
2741 STAMCOUNTER StatR0DynMapPopFlushes; /**< R0: Times PGMDynMapPopAutoSubset flushes the subset. */
2742 STAMCOUNTER aStatR0DynMapSetSize[11]; /**< R0: Set size distribution. */
2743
2744 /* RZ only: */
2745 STAMPROFILE StatRZTrap0e; /**< RC/R0: PGMTrap0eHandler() profiling. */
2746 STAMPROFILE StatRZTrap0eTimeCheckPageFault;
2747 STAMPROFILE StatRZTrap0eTimeSyncPT;
2748 STAMPROFILE StatRZTrap0eTimeMapping;
2749 STAMPROFILE StatRZTrap0eTimeOutOfSync;
2750 STAMPROFILE StatRZTrap0eTimeHandlers;
2751 STAMPROFILE StatRZTrap0eTime2CSAM; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is CSAM. */
2752 STAMPROFILE StatRZTrap0eTime2DirtyAndAccessed; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is dirty and/or accessed bit emulation. */
2753 STAMPROFILE StatRZTrap0eTime2GuestTrap; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a guest trap. */
2754 STAMPROFILE StatRZTrap0eTime2HndPhys; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a physical handler. */
2755 STAMPROFILE StatRZTrap0eTime2HndVirt; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a virtual handler. */
2756 STAMPROFILE StatRZTrap0eTime2HndUnhandled; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is access outside the monitored areas of a monitored page. */
2757 STAMPROFILE StatRZTrap0eTime2Misc; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is not known. */
2758 STAMPROFILE StatRZTrap0eTime2OutOfSync; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync page. */
2759 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndPhys; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync physical handler page. */
2760 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndVirt; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync virtual handler page. */
2761 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndObs; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an obsolete handler page. */
2762 STAMPROFILE StatRZTrap0eTime2SyncPT; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is lazy syncing of a PT. */
2763 STAMCOUNTER StatRZTrap0eConflicts; /**< RC/R0: The number of times \#PF was caused by an undetected conflict. */
2764 STAMCOUNTER StatRZTrap0eHandlersMapping; /**< RC/R0: Number of traps due to access handlers in mappings. */
2765 STAMCOUNTER StatRZTrap0eHandlersOutOfSync; /**< RC/R0: Number of out-of-sync handled pages. */
2766 STAMCOUNTER StatRZTrap0eHandlersPhysical; /**< RC/R0: Number of traps due to physical access handlers. */
2767 STAMCOUNTER StatRZTrap0eHandlersVirtual; /**< RC/R0: Number of traps due to virtual access handlers. */
2768 STAMCOUNTER StatRZTrap0eHandlersVirtualByPhys; /**< RC/R0: Number of traps due to virtual access handlers found by physical address. */
2769 STAMCOUNTER StatRZTrap0eHandlersVirtualUnmarked;/**< RC/R0: Number of traps due to virtual access handlers found by virtual address (without proper physical flags). */
2770 STAMCOUNTER StatRZTrap0eHandlersUnhandled; /**< RC/R0: Number of traps due to access outside range of monitored page(s). */
2771 STAMCOUNTER StatRZTrap0eHandlersInvalid; /**< RC/R0: Number of traps due to access to invalid physical memory. */
2772 STAMCOUNTER StatRZTrap0eUSNotPresentRead; /**< RC/R0: #PF err kind */
2773 STAMCOUNTER StatRZTrap0eUSNotPresentWrite; /**< RC/R0: #PF err kind */
2774 STAMCOUNTER StatRZTrap0eUSWrite; /**< RC/R0: #PF err kind */
2775 STAMCOUNTER StatRZTrap0eUSReserved; /**< RC/R0: #PF err kind */
2776 STAMCOUNTER StatRZTrap0eUSNXE; /**< RC/R0: #PF err kind */
2777 STAMCOUNTER StatRZTrap0eUSRead; /**< RC/R0: #PF err kind */
2778 STAMCOUNTER StatRZTrap0eSVNotPresentRead; /**< RC/R0: #PF err kind */
2779 STAMCOUNTER StatRZTrap0eSVNotPresentWrite; /**< RC/R0: #PF err kind */
2780 STAMCOUNTER StatRZTrap0eSVWrite; /**< RC/R0: #PF err kind */
2781 STAMCOUNTER StatRZTrap0eSVReserved; /**< RC/R0: #PF err kind */
2782 STAMCOUNTER StatRZTrap0eSNXE; /**< RC/R0: #PF err kind */
2783 STAMCOUNTER StatRZTrap0eGuestPF; /**< RC/R0: Real guest #PFs. */
2784 STAMCOUNTER StatRZTrap0eGuestPFUnh; /**< RC/R0: Real guest #PF ending up at the end of the #PF code. */
2785 STAMCOUNTER StatRZTrap0eGuestPFMapping; /**< RC/R0: Real guest #PF to HMA or other mapping. */
2786 STAMCOUNTER StatRZTrap0eWPEmulInRZ; /**< RC/R0: WP=0 virtualization trap, handled. */
2787 STAMCOUNTER StatRZTrap0eWPEmulToR3; /**< RC/R0: WP=0 virtualization trap, chickened out. */
2788 STAMCOUNTER StatRZTrap0ePD[X86_PG_ENTRIES]; /**< RC/R0: PD distribution of the #PFs. */
2789 STAMCOUNTER StatRZGuestCR3WriteHandled; /**< RC/R0: The number of times WriteHandlerCR3() was successfully called. */
2790 STAMCOUNTER StatRZGuestCR3WriteUnhandled; /**< RC/R0: The number of times WriteHandlerCR3() was called and we had to fall back to the recompiler. */
2791 STAMCOUNTER StatRZGuestCR3WriteConflict; /**< RC/R0: The number of times WriteHandlerCR3() was called and a conflict was detected. */
2792 STAMCOUNTER StatRZGuestROMWriteHandled; /**< RC/R0: The number of times pgmPhysRomWriteHandler() was successfully called. */
2793 STAMCOUNTER StatRZGuestROMWriteUnhandled; /**< RC/R0: The number of times pgmPhysRomWriteHandler() was called and we had to fall back to the recompiler */
2794
2795 /* HC - R3 and (maybe) R0: */
2796
2797 /* RZ & R3: */
2798 STAMPROFILE StatRZSyncCR3; /**< RC/R0: PGMSyncCR3() profiling. */
2799 STAMPROFILE StatRZSyncCR3Handlers; /**< RC/R0: Profiling of the PGMSyncCR3() update handler section. */
2800 STAMCOUNTER StatRZSyncCR3Global; /**< RC/R0: The number of global CR3 syncs. */
2801 STAMCOUNTER StatRZSyncCR3NotGlobal; /**< RC/R0: The number of non-global CR3 syncs. */
2802 STAMCOUNTER StatRZSyncCR3DstCacheHit; /**< RC/R0: The number of times we got some kind of cache hit on a page table. */
2803 STAMCOUNTER StatRZSyncCR3DstFreed; /**< RC/R0: The number of times we've had to free a shadow entry. */
2804 STAMCOUNTER StatRZSyncCR3DstFreedSrcNP; /**< RC/R0: The number of times we've had to free a shadow entry for which the source entry was not present. */
2805 STAMCOUNTER StatRZSyncCR3DstNotPresent; /**< RC/R0: The number of times we've encountered a not present shadow entry for a present guest entry. */
2806 STAMCOUNTER StatRZSyncCR3DstSkippedGlobalPD; /**< RC/R0: The number of times a global page directory wasn't flushed. */
2807 STAMCOUNTER StatRZSyncCR3DstSkippedGlobalPT; /**< RC/R0: The number of times a page table with only global entries wasn't flushed. */
2808 STAMPROFILE StatRZSyncPT; /**< RC/R0: PGMSyncPT() profiling. */
2809 STAMCOUNTER StatRZSyncPTFailed; /**< RC/R0: The number of times PGMSyncPT() failed. */
2810 STAMCOUNTER StatRZSyncPT4K; /**< RC/R0: Number of 4KB syncs. */
2811 STAMCOUNTER StatRZSyncPT4M; /**< RC/R0: Number of 4MB syncs. */
2812 STAMCOUNTER StatRZSyncPagePDNAs; /**< RC/R0: The number of time we've marked a PD not present from SyncPage to virtualize the accessed bit. */
2813 STAMCOUNTER StatRZSyncPagePDOutOfSync; /**< RC/R0: The number of time we've encountered an out-of-sync PD in SyncPage. */
2814 STAMCOUNTER StatRZAccessedPage; /**< RC/R0: The number of pages marked not present for accessed bit emulation. */
2815 STAMPROFILE StatRZDirtyBitTracking; /**< RC/R0: Profiling the dirty bit tracking in CheckPageFault().. */
2816 STAMCOUNTER StatRZDirtyPage; /**< RC/R0: The number of pages marked read-only for dirty bit tracking. */
2817 STAMCOUNTER StatRZDirtyPageBig; /**< RC/R0: The number of pages marked read-only for dirty bit tracking. */
2818 STAMCOUNTER StatRZDirtyPageSkipped; /**< RC/R0: The number of pages already dirty or readonly. */
2819 STAMCOUNTER StatRZDirtyPageTrap; /**< RC/R0: The number of traps generated for dirty bit tracking. */
2820 STAMCOUNTER StatRZDirtyPageStale; /**< RC/R0: The number of traps generated for dirty bit tracking. (stale tlb entries) */
2821 STAMCOUNTER StatRZDirtyTrackRealPF; /**< RC/R0: The number of real pages faults during dirty bit tracking. */
2822 STAMCOUNTER StatRZDirtiedPage; /**< RC/R0: The number of pages marked dirty because of write accesses. */
2823 STAMCOUNTER StatRZPageAlreadyDirty; /**< RC/R0: The number of pages already marked dirty because of write accesses. */
2824 STAMPROFILE StatRZInvalidatePage; /**< RC/R0: PGMInvalidatePage() profiling. */
2825 STAMCOUNTER StatRZInvalidatePage4KBPages; /**< RC/R0: The number of times PGMInvalidatePage() was called for a 4KB page. */
2826 STAMCOUNTER StatRZInvalidatePage4MBPages; /**< RC/R0: The number of times PGMInvalidatePage() was called for a 4MB page. */
2827 STAMCOUNTER StatRZInvalidatePage4MBPagesSkip; /**< RC/R0: The number of times PGMInvalidatePage() skipped a 4MB page. */
2828 STAMCOUNTER StatRZInvalidatePagePDMappings; /**< RC/R0: The number of times PGMInvalidatePage() was called for a page directory containing mappings (no conflict). */
2829 STAMCOUNTER StatRZInvalidatePagePDNAs; /**< RC/R0: The number of times PGMInvalidatePage() was called for a not accessed page directory. */
2830 STAMCOUNTER StatRZInvalidatePagePDNPs; /**< RC/R0: The number of times PGMInvalidatePage() was called for a not present page directory. */
2831 STAMCOUNTER StatRZInvalidatePagePDOutOfSync; /**< RC/R0: The number of times PGMInvalidatePage() was called for an out of sync page directory. */
2832 STAMCOUNTER StatRZInvalidatePageSkipped; /**< RC/R0: The number of times PGMInvalidatePage() was skipped due to not present shw or pending pending SyncCR3. */
2833 STAMCOUNTER StatRZPageOutOfSyncUser; /**< RC/R0: The number of times user page is out of sync was detected in #PF or VerifyAccessSyncPage. */
2834 STAMCOUNTER StatRZPageOutOfSyncSupervisor; /**< RC/R0: The number of times supervisor page is out of sync was detected in in #PF or VerifyAccessSyncPage. */
2835 STAMPROFILE StatRZPrefetch; /**< RC/R0: PGMPrefetchPage. */
2836 STAMPROFILE StatRZFlushTLB; /**< RC/R0: Profiling of the PGMFlushTLB() body. */
2837 STAMCOUNTER StatRZFlushTLBNewCR3; /**< RC/R0: The number of times PGMFlushTLB was called with a new CR3, non-global. (switch) */
2838 STAMCOUNTER StatRZFlushTLBNewCR3Global; /**< RC/R0: The number of times PGMFlushTLB was called with a new CR3, global. (switch) */
2839 STAMCOUNTER StatRZFlushTLBSameCR3; /**< RC/R0: The number of times PGMFlushTLB was called with the same CR3, non-global. (flush) */
2840 STAMCOUNTER StatRZFlushTLBSameCR3Global; /**< RC/R0: The number of times PGMFlushTLB was called with the same CR3, global. (flush) */
2841 STAMPROFILE StatRZGstModifyPage; /**< RC/R0: Profiling of the PGMGstModifyPage() body */
2842
2843 STAMPROFILE StatR3SyncCR3; /**< R3: PGMSyncCR3() profiling. */
2844 STAMPROFILE StatR3SyncCR3Handlers; /**< R3: Profiling of the PGMSyncCR3() update handler section. */
2845 STAMCOUNTER StatR3SyncCR3Global; /**< R3: The number of global CR3 syncs. */
2846 STAMCOUNTER StatR3SyncCR3NotGlobal; /**< R3: The number of non-global CR3 syncs. */
2847 STAMCOUNTER StatR3SyncCR3DstFreed; /**< R3: The number of times we've had to free a shadow entry. */
2848 STAMCOUNTER StatR3SyncCR3DstFreedSrcNP; /**< R3: The number of times we've had to free a shadow entry for which the source entry was not present. */
2849 STAMCOUNTER StatR3SyncCR3DstNotPresent; /**< R3: The number of times we've encountered a not present shadow entry for a present guest entry. */
2850 STAMCOUNTER StatR3SyncCR3DstSkippedGlobalPD; /**< R3: The number of times a global page directory wasn't flushed. */
2851 STAMCOUNTER StatR3SyncCR3DstSkippedGlobalPT; /**< R3: The number of times a page table with only global entries wasn't flushed. */
2852 STAMCOUNTER StatR3SyncCR3DstCacheHit; /**< R3: The number of times we got some kind of cache hit on a page table. */
2853 STAMPROFILE StatR3SyncPT; /**< R3: PGMSyncPT() profiling. */
2854 STAMCOUNTER StatR3SyncPTFailed; /**< R3: The number of times PGMSyncPT() failed. */
2855 STAMCOUNTER StatR3SyncPT4K; /**< R3: Number of 4KB syncs. */
2856 STAMCOUNTER StatR3SyncPT4M; /**< R3: Number of 4MB syncs. */
2857 STAMCOUNTER StatR3SyncPagePDNAs; /**< R3: The number of time we've marked a PD not present from SyncPage to virtualize the accessed bit. */
2858 STAMCOUNTER StatR3SyncPagePDOutOfSync; /**< R3: The number of time we've encountered an out-of-sync PD in SyncPage. */
2859 STAMCOUNTER StatR3AccessedPage; /**< R3: The number of pages marked not present for accessed bit emulation. */
2860 STAMPROFILE StatR3DirtyBitTracking; /**< R3: Profiling the dirty bit tracking in CheckPageFault(). */
2861 STAMCOUNTER StatR3DirtyPage; /**< R3: The number of pages marked read-only for dirty bit tracking. */
2862 STAMCOUNTER StatR3DirtyPageBig; /**< R3: The number of pages marked read-only for dirty bit tracking. */
2863 STAMCOUNTER StatR3DirtyPageSkipped; /**< R3: The number of pages already dirty or readonly. */
2864 STAMCOUNTER StatR3DirtyPageTrap; /**< R3: The number of traps generated for dirty bit tracking. */
2865 STAMCOUNTER StatR3DirtyTrackRealPF; /**< R3: The number of real pages faults during dirty bit tracking. */
2866 STAMCOUNTER StatR3DirtiedPage; /**< R3: The number of pages marked dirty because of write accesses. */
2867 STAMCOUNTER StatR3PageAlreadyDirty; /**< R3: The number of pages already marked dirty because of write accesses. */
2868 STAMPROFILE StatR3InvalidatePage; /**< R3: PGMInvalidatePage() profiling. */
2869 STAMCOUNTER StatR3InvalidatePage4KBPages; /**< R3: The number of times PGMInvalidatePage() was called for a 4KB page. */
2870 STAMCOUNTER StatR3InvalidatePage4MBPages; /**< R3: The number of times PGMInvalidatePage() was called for a 4MB page. */
2871 STAMCOUNTER StatR3InvalidatePage4MBPagesSkip; /**< R3: The number of times PGMInvalidatePage() skipped a 4MB page. */
2872 STAMCOUNTER StatR3InvalidatePagePDNAs; /**< R3: The number of times PGMInvalidatePage() was called for a not accessed page directory. */
2873 STAMCOUNTER StatR3InvalidatePagePDNPs; /**< R3: The number of times PGMInvalidatePage() was called for a not present page directory. */
2874 STAMCOUNTER StatR3InvalidatePagePDMappings; /**< R3: The number of times PGMInvalidatePage() was called for a page directory containing mappings (no conflict). */
2875 STAMCOUNTER StatR3InvalidatePagePDOutOfSync; /**< R3: The number of times PGMInvalidatePage() was called for an out of sync page directory. */
2876 STAMCOUNTER StatR3InvalidatePageSkipped; /**< R3: The number of times PGMInvalidatePage() was skipped due to not present shw or pending pending SyncCR3. */
2877 STAMCOUNTER StatR3PageOutOfSyncUser; /**< R3: The number of times user page is out of sync was detected in #PF or VerifyAccessSyncPage. */
2878 STAMCOUNTER StatR3PageOutOfSyncSupervisor; /**< R3: The number of times supervisor page is out of sync was detected in in #PF or VerifyAccessSyncPage. */
2879 STAMPROFILE StatR3Prefetch; /**< R3: PGMPrefetchPage. */
2880 STAMPROFILE StatR3FlushTLB; /**< R3: Profiling of the PGMFlushTLB() body. */
2881 STAMCOUNTER StatR3FlushTLBNewCR3; /**< R3: The number of times PGMFlushTLB was called with a new CR3, non-global. (switch) */
2882 STAMCOUNTER StatR3FlushTLBNewCR3Global; /**< R3: The number of times PGMFlushTLB was called with a new CR3, global. (switch) */
2883 STAMCOUNTER StatR3FlushTLBSameCR3; /**< R3: The number of times PGMFlushTLB was called with the same CR3, non-global. (flush) */
2884 STAMCOUNTER StatR3FlushTLBSameCR3Global; /**< R3: The number of times PGMFlushTLB was called with the same CR3, global. (flush) */
2885 STAMPROFILE StatR3GstModifyPage; /**< R3: Profiling of the PGMGstModifyPage() body */
2886 /** @} */
2887#endif /* VBOX_WITH_STATISTICS */
2888} PGMCPU;
2889/** Pointer to the per-cpu PGM data. */
2890typedef PGMCPU *PPGMCPU;
2891
2892
2893/** @name PGM::fSyncFlags Flags
2894 * @{
2895 */
2896/** Updates the virtual access handler state bit in PGMPAGE. */
2897#define PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL RT_BIT(0)
2898/** Always sync CR3. */
2899#define PGM_SYNC_ALWAYS RT_BIT(1)
2900/** Check monitoring on next CR3 (re)load and invalidate page.
2901 * @todo This is obsolete now. Remove after 2.2.0 is branched off. */
2902#define PGM_SYNC_MONITOR_CR3 RT_BIT(2)
2903/** Check guest mapping in SyncCR3. */
2904#define PGM_SYNC_MAP_CR3 RT_BIT(3)
2905/** Clear the page pool (a light weight flush). */
2906#define PGM_GLOBAL_SYNC_CLEAR_PGM_POOL_BIT 8
2907#define PGM_GLOBAL_SYNC_CLEAR_PGM_POOL RT_BIT(PGM_GLOBAL_SYNC_CLEAR_PGM_POOL_BIT)
2908/** @} */
2909
2910
2911__BEGIN_DECLS
2912
2913int pgmLock(PVM pVM);
2914void pgmUnlock(PVM pVM);
2915
2916int pgmR3SyncPTResolveConflict(PVM pVM, PPGMMAPPING pMapping, PX86PD pPDSrc, RTGCPTR GCPtrOldMapping);
2917int pgmR3SyncPTResolveConflictPAE(PVM pVM, PPGMMAPPING pMapping, RTGCPTR GCPtrOldMapping);
2918PPGMMAPPING pgmGetMapping(PVM pVM, RTGCPTR GCPtr);
2919void pgmR3MapRelocate(PVM pVM, PPGMMAPPING pMapping, RTGCPTR GCPtrOldMapping, RTGCPTR GCPtrNewMapping);
2920DECLCALLBACK(void) pgmR3MapInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
2921
2922void pgmR3HandlerPhysicalUpdateAll(PVM pVM);
2923bool pgmHandlerPhysicalIsAll(PVM pVM, RTGCPHYS GCPhys);
2924void pgmHandlerPhysicalResetAliasedPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhysPage);
2925int pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage);
2926DECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser);
2927#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
2928void pgmHandlerVirtualDumpPhysPages(PVM pVM);
2929#else
2930# define pgmHandlerVirtualDumpPhysPages(a) do { } while (0)
2931#endif
2932DECLCALLBACK(void) pgmR3InfoHandlers(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
2933
2934
2935int pgmPhysAllocPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2936int pgmPhysPageLoadIntoTlb(PPGM pPGM, RTGCPHYS GCPhys);
2937int pgmPhysPageLoadIntoTlbWithPage(PPGM pPGM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2938int pgmPhysPageMakeWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2939int pgmPhysPageMakeWritableUnlocked(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2940int pgmPhysPageMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAP ppMap, void **ppv);
2941int pgmPhysPageMapByPageID(PVM pVM, uint32_t idPage, RTHCPHYS HCPhys, void **ppv);
2942int pgmPhysGCPhys2CCPtrInternal(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv);
2943int pgmPhysGCPhys2CCPtrInternalReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, const void **ppv);
2944VMMDECL(int) pgmPhysRomWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
2945#ifdef IN_RING3
2946void pgmR3PhysRelinkRamRanges(PVM pVM);
2947int pgmR3PhysRamPreAllocate(PVM pVM);
2948int pgmR3PhysRamReset(PVM pVM);
2949int pgmR3PhysRomReset(PVM pVM);
2950int pgmR3PhysChunkMap(PVM pVM, uint32_t idChunk, PPPGMCHUNKR3MAP ppChunk);
2951
2952int pgmR3PoolInit(PVM pVM);
2953void pgmR3PoolRelocate(PVM pVM);
2954void pgmR3PoolReset(PVM pVM);
2955
2956#endif /* IN_RING3 */
2957#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2958int pgmR0DynMapHCPageCommon(PVM pVM, PPGMMAPSET pSet, RTHCPHYS HCPhys, void **ppv);
2959#endif
2960int pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage);
2961void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable);
2962void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
2963int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
2964void pgmPoolClearAll(PVM pVM);
2965PPGMPOOLPAGE pgmPoolGetPage(PPGMPOOL pPool, RTHCPHYS HCPhys);
2966int pgmPoolSyncCR3(PVM pVM);
2967int pgmPoolTrackFlushGCPhys(PVM pVM, PPGMPAGE pPhysPage, bool *pfFlushTLBs);
2968uint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT);
2969void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPoolPage, PPGMPAGE pPhysPage);
2970#ifdef PGMPOOL_WITH_MONITORING
2971void pgmPoolMonitorChainChanging(PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvAddress, PDISCPUSTATE pCpu);
2972int pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
2973void pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
2974#endif
2975
2976int pgmR3ExitShadowModeBeforePoolFlush(PVM pVM, PVMCPU pVCpu);
2977int pgmR3ReEnterShadowModeAfterPoolFlush(PVM pVM, PVMCPU pVCpu);
2978
2979void pgmMapSetShadowPDEs(PVM pVM, PPGMMAPPING pMap, unsigned iNewPDE);
2980void pgmMapClearShadowPDEs(PVM pVM, PPGMPOOLPAGE pShwPageCR3, PPGMMAPPING pMap, unsigned iOldPDE, bool fDeactivateCR3);
2981int pgmMapActivateCR3(PVM pVM, PPGMPOOLPAGE pShwPageCR3);
2982int pgmMapDeactivateCR3(PVM pVM, PPGMPOOLPAGE pShwPageCR3);
2983
2984int pgmShwSyncPaePDPtr(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDPE pGstPdpe, PX86PDPAE *ppPD);
2985#ifndef IN_RC
2986int pgmShwSyncLongModePDPtr(PVMCPU pVCpu, RTGCPTR64 GCPtr, PX86PML4E pGstPml4e, PX86PDPE pGstPdpe, PX86PDPAE *ppPD);
2987#endif
2988int pgmShwGetEPTPDPtr(PVMCPU pVCpu, RTGCPTR64 GCPtr, PEPTPDPT *ppPdpt, PEPTPD *ppPD);
2989
2990PX86PD pgmGstLazyMap32BitPD(PPGMCPU pPGM);
2991PX86PDPT pgmGstLazyMapPaePDPT(PPGMCPU pPGM);
2992PX86PDPAE pgmGstLazyMapPaePD(PPGMCPU pPGM, uint32_t iPdpt);
2993PX86PML4 pgmGstLazyMapPml4(PPGMCPU pPGM);
2994
2995__END_DECLS
2996
2997
2998/**
2999 * Gets the PGMRAMRANGE structure for a guest page.
3000 *
3001 * @returns Pointer to the RAM range on success.
3002 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3003 *
3004 * @param pPGM PGM handle.
3005 * @param GCPhys The GC physical address.
3006 */
3007DECLINLINE(PPGMRAMRANGE) pgmPhysGetRange(PPGM pPGM, RTGCPHYS GCPhys)
3008{
3009 /*
3010 * Optimize for the first range.
3011 */
3012 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3013 RTGCPHYS off = GCPhys - pRam->GCPhys;
3014 if (RT_UNLIKELY(off >= pRam->cb))
3015 {
3016 do
3017 {
3018 pRam = pRam->CTX_SUFF(pNext);
3019 if (RT_UNLIKELY(!pRam))
3020 break;
3021 off = GCPhys - pRam->GCPhys;
3022 } while (off >= pRam->cb);
3023 }
3024 return pRam;
3025}
3026
3027
3028/**
3029 * Gets the PGMPAGE structure for a guest page.
3030 *
3031 * @returns Pointer to the page on success.
3032 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3033 *
3034 * @param pPGM PGM handle.
3035 * @param GCPhys The GC physical address.
3036 */
3037DECLINLINE(PPGMPAGE) pgmPhysGetPage(PPGM pPGM, RTGCPHYS GCPhys)
3038{
3039 /*
3040 * Optimize for the first range.
3041 */
3042 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3043 RTGCPHYS off = GCPhys - pRam->GCPhys;
3044 if (RT_UNLIKELY(off >= pRam->cb))
3045 {
3046 do
3047 {
3048 pRam = pRam->CTX_SUFF(pNext);
3049 if (RT_UNLIKELY(!pRam))
3050 return NULL;
3051 off = GCPhys - pRam->GCPhys;
3052 } while (off >= pRam->cb);
3053 }
3054 return &pRam->aPages[off >> PAGE_SHIFT];
3055}
3056
3057
3058/**
3059 * Gets the PGMPAGE structure for a guest page.
3060 *
3061 * Old Phys code: Will make sure the page is present.
3062 *
3063 * @returns VBox status code.
3064 * @retval VINF_SUCCESS and a valid *ppPage on success.
3065 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if the address isn't valid.
3066 *
3067 * @param pPGM PGM handle.
3068 * @param GCPhys The GC physical address.
3069 * @param ppPage Where to store the page poitner on success.
3070 */
3071DECLINLINE(int) pgmPhysGetPageEx(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGE ppPage)
3072{
3073 /*
3074 * Optimize for the first range.
3075 */
3076 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3077 RTGCPHYS off = GCPhys - pRam->GCPhys;
3078 if (RT_UNLIKELY(off >= pRam->cb))
3079 {
3080 do
3081 {
3082 pRam = pRam->CTX_SUFF(pNext);
3083 if (RT_UNLIKELY(!pRam))
3084 {
3085 *ppPage = NULL; /* avoid incorrect and very annoying GCC warnings */
3086 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
3087 }
3088 off = GCPhys - pRam->GCPhys;
3089 } while (off >= pRam->cb);
3090 }
3091 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
3092 return VINF_SUCCESS;
3093}
3094
3095
3096
3097
3098/**
3099 * Gets the PGMPAGE structure for a guest page.
3100 *
3101 * Old Phys code: Will make sure the page is present.
3102 *
3103 * @returns VBox status code.
3104 * @retval VINF_SUCCESS and a valid *ppPage on success.
3105 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if the address isn't valid.
3106 *
3107 * @param pPGM PGM handle.
3108 * @param GCPhys The GC physical address.
3109 * @param ppPage Where to store the page poitner on success.
3110 * @param ppRamHint Where to read and store the ram list hint.
3111 * The caller initializes this to NULL before the call.
3112 */
3113DECLINLINE(int) pgmPhysGetPageWithHintEx(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGE ppPage, PPGMRAMRANGE *ppRamHint)
3114{
3115 RTGCPHYS off;
3116 PPGMRAMRANGE pRam = *ppRamHint;
3117 if ( !pRam
3118 || RT_UNLIKELY((off = GCPhys - pRam->GCPhys) >= pRam->cb))
3119 {
3120 pRam = pPGM->CTX_SUFF(pRamRanges);
3121 off = GCPhys - pRam->GCPhys;
3122 if (RT_UNLIKELY(off >= pRam->cb))
3123 {
3124 do
3125 {
3126 pRam = pRam->CTX_SUFF(pNext);
3127 if (RT_UNLIKELY(!pRam))
3128 {
3129 *ppPage = NULL; /* Kill the incorrect and extremely annoying GCC warnings. */
3130 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
3131 }
3132 off = GCPhys - pRam->GCPhys;
3133 } while (off >= pRam->cb);
3134 }
3135 *ppRamHint = pRam;
3136 }
3137 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
3138 return VINF_SUCCESS;
3139}
3140
3141
3142/**
3143 * Gets the PGMPAGE structure for a guest page together with the PGMRAMRANGE.
3144 *
3145 * @returns Pointer to the page on success.
3146 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3147 *
3148 * @param pPGM PGM handle.
3149 * @param GCPhys The GC physical address.
3150 * @param ppRam Where to store the pointer to the PGMRAMRANGE.
3151 */
3152DECLINLINE(PPGMPAGE) pgmPhysGetPageAndRange(PPGM pPGM, RTGCPHYS GCPhys, PPGMRAMRANGE *ppRam)
3153{
3154 /*
3155 * Optimize for the first range.
3156 */
3157 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3158 RTGCPHYS off = GCPhys - pRam->GCPhys;
3159 if (RT_UNLIKELY(off >= pRam->cb))
3160 {
3161 do
3162 {
3163 pRam = pRam->CTX_SUFF(pNext);
3164 if (RT_UNLIKELY(!pRam))
3165 return NULL;
3166 off = GCPhys - pRam->GCPhys;
3167 } while (off >= pRam->cb);
3168 }
3169 *ppRam = pRam;
3170 return &pRam->aPages[off >> PAGE_SHIFT];
3171}
3172
3173
3174/**
3175 * Gets the PGMPAGE structure for a guest page together with the PGMRAMRANGE.
3176 *
3177 * @returns Pointer to the page on success.
3178 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3179 *
3180 * @param pPGM PGM handle.
3181 * @param GCPhys The GC physical address.
3182 * @param ppPage Where to store the pointer to the PGMPAGE structure.
3183 * @param ppRam Where to store the pointer to the PGMRAMRANGE structure.
3184 */
3185DECLINLINE(int) pgmPhysGetPageAndRangeEx(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGE ppPage, PPGMRAMRANGE *ppRam)
3186{
3187 /*
3188 * Optimize for the first range.
3189 */
3190 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3191 RTGCPHYS off = GCPhys - pRam->GCPhys;
3192 if (RT_UNLIKELY(off >= pRam->cb))
3193 {
3194 do
3195 {
3196 pRam = pRam->CTX_SUFF(pNext);
3197 if (RT_UNLIKELY(!pRam))
3198 {
3199 *ppRam = NULL; /* Shut up silly GCC warnings. */
3200 *ppPage = NULL; /* ditto */
3201 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
3202 }
3203 off = GCPhys - pRam->GCPhys;
3204 } while (off >= pRam->cb);
3205 }
3206 *ppRam = pRam;
3207 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
3208 return VINF_SUCCESS;
3209}
3210
3211
3212/**
3213 * Convert GC Phys to HC Phys.
3214 *
3215 * @returns VBox status.
3216 * @param pPGM PGM handle.
3217 * @param GCPhys The GC physical address.
3218 * @param pHCPhys Where to store the corresponding HC physical address.
3219 *
3220 * @deprecated Doesn't deal with zero, shared or write monitored pages.
3221 * Avoid when writing new code!
3222 */
3223DECLINLINE(int) pgmRamGCPhys2HCPhys(PPGM pPGM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
3224{
3225 PPGMPAGE pPage;
3226 int rc = pgmPhysGetPageEx(pPGM, GCPhys, &pPage);
3227 if (RT_FAILURE(rc))
3228 return rc;
3229 *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK);
3230 return VINF_SUCCESS;
3231}
3232
3233#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3234
3235/**
3236 * Inlined version of the ring-0 version of PGMDynMapHCPage that
3237 * optimizes access to pages already in the set.
3238 *
3239 * @returns VINF_SUCCESS. Will bail out to ring-3 on failure.
3240 * @param pPGM Pointer to the PVM instance data.
3241 * @param HCPhys The physical address of the page.
3242 * @param ppv Where to store the mapping address.
3243 */
3244DECLINLINE(int) pgmR0DynMapHCPageInlined(PPGM pPGM, RTHCPHYS HCPhys, void **ppv)
3245{
3246 PVM pVM = PGM2VM(pPGM);
3247 PPGMCPU pPGMCPU = (PPGMCPU)((uint8_t *)VMMGetCpu(pVM) + pPGM->offVCpuPGM); /* very pretty ;-) */
3248 PPGMMAPSET pSet = &pPGMCPU->AutoSet;
3249
3250 STAM_PROFILE_START(&pPGMCPU->StatR0DynMapHCPageInl, a);
3251 Assert(!(HCPhys & PAGE_OFFSET_MASK));
3252 Assert(pSet->cEntries <= RT_ELEMENTS(pSet->aEntries));
3253
3254 unsigned iHash = PGMMAPSET_HASH(HCPhys);
3255 unsigned iEntry = pSet->aiHashTable[iHash];
3256 if ( iEntry < pSet->cEntries
3257 && pSet->aEntries[iEntry].HCPhys == HCPhys)
3258 {
3259 *ppv = pSet->aEntries[iEntry].pvPage;
3260 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapHCPageInlHits);
3261 }
3262 else
3263 {
3264 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapHCPageInlMisses);
3265 pgmR0DynMapHCPageCommon(pVM, pSet, HCPhys, ppv);
3266 }
3267
3268 STAM_PROFILE_STOP(&pPGMCPU->StatR0DynMapHCPageInl, a);
3269 return VINF_SUCCESS;
3270}
3271
3272
3273/**
3274 * Inlined version of the ring-0 version of PGMDynMapGCPage that optimizes
3275 * access to pages already in the set.
3276 *
3277 * @returns See PGMDynMapGCPage.
3278 * @param pPGM Pointer to the PVM instance data.
3279 * @param HCPhys The physical address of the page.
3280 * @param ppv Where to store the mapping address.
3281 */
3282DECLINLINE(int) pgmR0DynMapGCPageInlined(PPGM pPGM, RTGCPHYS GCPhys, void **ppv)
3283{
3284 PVM pVM = PGM2VM(pPGM);
3285 PPGMCPU pPGMCPU = (PPGMCPU)((uint8_t *)VMMGetCpu(pVM) + pPGM->offVCpuPGM); /* very pretty ;-) */
3286
3287 STAM_PROFILE_START(&pPGMCPU->StatR0DynMapGCPageInl, a);
3288 Assert(!(GCPhys & PAGE_OFFSET_MASK));
3289
3290 /*
3291 * Get the ram range.
3292 */
3293 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3294 RTGCPHYS off = GCPhys - pRam->GCPhys;
3295 if (RT_UNLIKELY(off >= pRam->cb
3296 /** @todo || page state stuff */))
3297 {
3298 /* This case is not counted into StatR0DynMapGCPageInl. */
3299 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlRamMisses);
3300 return PGMDynMapGCPage(pVM, GCPhys, ppv);
3301 }
3302
3303 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[off >> PAGE_SHIFT]);
3304 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlRamHits);
3305
3306 /*
3307 * pgmR0DynMapHCPageInlined with out stats.
3308 */
3309 PPGMMAPSET pSet = &pPGMCPU->AutoSet;
3310 Assert(!(HCPhys & PAGE_OFFSET_MASK));
3311 Assert(pSet->cEntries <= RT_ELEMENTS(pSet->aEntries));
3312
3313 unsigned iHash = PGMMAPSET_HASH(HCPhys);
3314 unsigned iEntry = pSet->aiHashTable[iHash];
3315 if ( iEntry < pSet->cEntries
3316 && pSet->aEntries[iEntry].HCPhys == HCPhys)
3317 {
3318 *ppv = pSet->aEntries[iEntry].pvPage;
3319 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlHits);
3320 }
3321 else
3322 {
3323 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlMisses);
3324 pgmR0DynMapHCPageCommon(pVM, pSet, HCPhys, ppv);
3325 }
3326
3327 STAM_PROFILE_STOP(&pPGMCPU->StatR0DynMapGCPageInl, a);
3328 return VINF_SUCCESS;
3329}
3330
3331#endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
3332#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
3333
3334/**
3335 * Maps the page into current context (RC and maybe R0).
3336 *
3337 * @returns pointer to the mapping.
3338 * @param pVM Pointer to the PGM instance data.
3339 * @param pPage The page.
3340 */
3341DECLINLINE(void *) pgmPoolMapPageInlined(PPGM pPGM, PPGMPOOLPAGE pPage)
3342{
3343 if (pPage->idx >= PGMPOOL_IDX_FIRST)
3344 {
3345 Assert(pPage->idx < pPGM->CTX_SUFF(pPool)->cCurPages);
3346 void *pv;
3347# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3348 pgmR0DynMapHCPageInlined(pPGM, pPage->Core.Key, &pv);
3349# else
3350 PGMDynMapHCPage(PGM2VM(pPGM), pPage->Core.Key, &pv);
3351# endif
3352 return pv;
3353 }
3354 AssertFatalMsgFailed(("pgmPoolMapPageInlined invalid page index %x\n", pPage->idx));
3355}
3356
3357/**
3358 * Temporarily maps one host page specified by HC physical address, returning
3359 * pointer within the page.
3360 *
3361 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
3362 * reused after 8 mappings (or perhaps a few more if you score with the cache).
3363 *
3364 * @returns The address corresponding to HCPhys.
3365 * @param pPGM Pointer to the PVM instance data.
3366 * @param HCPhys HC Physical address of the page.
3367 */
3368DECLINLINE(void *) pgmDynMapHCPageOff(PPGM pPGM, RTHCPHYS HCPhys)
3369{
3370 void *pv;
3371# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3372 pgmR0DynMapHCPageInlined(pPGM, HCPhys & ~(RTHCPHYS)PAGE_OFFSET_MASK, &pv);
3373# else
3374 PGMDynMapHCPage(PGM2VM(pPGM), HCPhys & ~(RTHCPHYS)PAGE_OFFSET_MASK, &pv);
3375# endif
3376 pv = (void *)((uintptr_t)pv | (HCPhys & PAGE_OFFSET_MASK));
3377 return pv;
3378}
3379
3380#endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 || IN_RC */
3381#ifndef IN_RC
3382
3383/**
3384 * Queries the Physical TLB entry for a physical guest page,
3385 * attempting to load the TLB entry if necessary.
3386 *
3387 * @returns VBox status code.
3388 * @retval VINF_SUCCESS on success
3389 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
3390 *
3391 * @param pPGM The PGM instance handle.
3392 * @param GCPhys The address of the guest page.
3393 * @param ppTlbe Where to store the pointer to the TLB entry.
3394 */
3395DECLINLINE(int) pgmPhysPageQueryTlbe(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGEMAPTLBE ppTlbe)
3396{
3397 int rc;
3398 PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
3399 if (pTlbe->GCPhys == (GCPhys & X86_PTE_PAE_PG_MASK))
3400 {
3401 STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbHits));
3402 rc = VINF_SUCCESS;
3403 }
3404 else
3405 rc = pgmPhysPageLoadIntoTlb(pPGM, GCPhys);
3406 *ppTlbe = pTlbe;
3407 return rc;
3408}
3409
3410
3411/**
3412 * Queries the Physical TLB entry for a physical guest page,
3413 * attempting to load the TLB entry if necessary.
3414 *
3415 * @returns VBox status code.
3416 * @retval VINF_SUCCESS on success
3417 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
3418 *
3419 * @param pPGM The PGM instance handle.
3420 * @param pPage Pointer to the PGMPAGE structure corresponding to
3421 * GCPhys.
3422 * @param GCPhys The address of the guest page.
3423 * @param ppTlbe Where to store the pointer to the TLB entry.
3424 */
3425DECLINLINE(int) pgmPhysPageQueryTlbeWithPage(PPGM pPGM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAPTLBE ppTlbe)
3426{
3427 int rc;
3428 PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
3429 if (pTlbe->GCPhys == (GCPhys & X86_PTE_PAE_PG_MASK))
3430 {
3431 STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbHits));
3432 rc = VINF_SUCCESS;
3433 }
3434 else
3435 rc = pgmPhysPageLoadIntoTlbWithPage(pPGM, pPage, GCPhys);
3436 *ppTlbe = pTlbe;
3437 return rc;
3438}
3439
3440#endif /* !IN_RC */
3441
3442/**
3443 * Calculated the guest physical address of the large (4 MB) page in 32 bits paging mode.
3444 * Takes PSE-36 into account.
3445 *
3446 * @returns guest physical address
3447 * @param pPGM Pointer to the PGM instance data.
3448 * @param Pde Guest Pde
3449 */
3450DECLINLINE(RTGCPHYS) pgmGstGet4MBPhysPage(PPGM pPGM, X86PDE Pde)
3451{
3452 RTGCPHYS GCPhys = Pde.u & X86_PDE4M_PG_MASK;
3453 GCPhys |= (RTGCPHYS)Pde.b.u8PageNoHigh << 32;
3454
3455 return GCPhys & pPGM->GCPhys4MBPSEMask;
3456}
3457
3458
3459/**
3460 * Gets the page directory entry for the specified address (32-bit paging).
3461 *
3462 * @returns The page directory entry in question.
3463 * @param pPGM Pointer to the PGM instance data.
3464 * @param GCPtr The address.
3465 */
3466DECLINLINE(X86PDE) pgmGstGet32bitPDE(PPGMCPU pPGM, RTGCPTR GCPtr)
3467{
3468#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3469 PCX86PD pGuestPD = NULL;
3470 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPD);
3471 if (RT_FAILURE(rc))
3472 {
3473 X86PDE ZeroPde = {0};
3474 AssertMsgFailedReturn(("%Rrc\n", rc), ZeroPde);
3475 }
3476#else
3477 PX86PD pGuestPD = pPGM->CTX_SUFF(pGst32BitPd);
3478# ifdef IN_RING3
3479 if (!pGuestPD)
3480 pGuestPD = pgmGstLazyMap32BitPD(pPGM);
3481# endif
3482#endif
3483 return pGuestPD->a[GCPtr >> X86_PD_SHIFT];
3484}
3485
3486
3487/**
3488 * Gets the address of a specific page directory entry (32-bit paging).
3489 *
3490 * @returns Pointer the page directory entry in question.
3491 * @param pPGM Pointer to the PGM instance data.
3492 * @param GCPtr The address.
3493 */
3494DECLINLINE(PX86PDE) pgmGstGet32bitPDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
3495{
3496#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3497 PX86PD pGuestPD = NULL;
3498 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPD);
3499 AssertRCReturn(rc, NULL);
3500#else
3501 PX86PD pGuestPD = pPGM->CTX_SUFF(pGst32BitPd);
3502# ifdef IN_RING3
3503 if (!pGuestPD)
3504 pGuestPD = pgmGstLazyMap32BitPD(pPGM);
3505# endif
3506#endif
3507 return &pGuestPD->a[GCPtr >> X86_PD_SHIFT];
3508}
3509
3510
3511/**
3512 * Gets the address the guest page directory (32-bit paging).
3513 *
3514 * @returns Pointer the page directory entry in question.
3515 * @param pPGM Pointer to the PGM instance data.
3516 */
3517DECLINLINE(PX86PD) pgmGstGet32bitPDPtr(PPGMCPU pPGM)
3518{
3519#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3520 PX86PD pGuestPD = NULL;
3521 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPD);
3522 AssertRCReturn(rc, NULL);
3523#else
3524 PX86PD pGuestPD = pPGM->CTX_SUFF(pGst32BitPd);
3525# ifdef IN_RING3
3526 if (!pGuestPD)
3527 pGuestPD = pgmGstLazyMap32BitPD(pPGM);
3528# endif
3529#endif
3530 return pGuestPD;
3531}
3532
3533
3534/**
3535 * Gets the guest page directory pointer table.
3536 *
3537 * @returns Pointer to the page directory in question.
3538 * @returns NULL if the page directory is not present or on an invalid page.
3539 * @param pPGM Pointer to the PGM instance data.
3540 */
3541DECLINLINE(PX86PDPT) pgmGstGetPaePDPTPtr(PPGMCPU pPGM)
3542{
3543#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3544 PX86PDPT pGuestPDPT = NULL;
3545 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPDPT);
3546 AssertRCReturn(rc, NULL);
3547#else
3548 PX86PDPT pGuestPDPT = pPGM->CTX_SUFF(pGstPaePdpt);
3549# ifdef IN_RING3
3550 if (!pGuestPDPT)
3551 pGuestPDPT = pgmGstLazyMapPaePDPT(pPGM);
3552# endif
3553#endif
3554 return pGuestPDPT;
3555}
3556
3557
3558/**
3559 * Gets the guest page directory pointer table entry for the specified address.
3560 *
3561 * @returns Pointer to the page directory in question.
3562 * @returns NULL if the page directory is not present or on an invalid page.
3563 * @param pPGM Pointer to the PGM instance data.
3564 * @param GCPtr The address.
3565 */
3566DECLINLINE(PX86PDPE) pgmGstGetPaePDPEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
3567{
3568 AssertGCPtr32(GCPtr);
3569
3570#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3571 PX86PDPT pGuestPDPT = 0;
3572 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPDPT);
3573 AssertRCReturn(rc, 0);
3574#else
3575 PX86PDPT pGuestPDPT = pPGM->CTX_SUFF(pGstPaePdpt);
3576# ifdef IN_RING3
3577 if (!pGuestPDPT)
3578 pGuestPDPT = pgmGstLazyMapPaePDPT(pPGM);
3579# endif
3580#endif
3581 return &pGuestPDPT->a[(GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE];
3582}
3583
3584
3585/**
3586 * Gets the page directory for the specified address.
3587 *
3588 * @returns Pointer to the page directory in question.
3589 * @returns NULL if the page directory is not present or on an invalid page.
3590 * @param pPGM Pointer to the PGM instance data.
3591 * @param GCPtr The address.
3592 */
3593DECLINLINE(PX86PDPAE) pgmGstGetPaePD(PPGMCPU pPGM, RTGCPTR GCPtr)
3594{
3595 AssertGCPtr32(GCPtr);
3596
3597 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3598 AssertReturn(pGuestPDPT, NULL);
3599 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3600 if (pGuestPDPT->a[iPdpt].n.u1Present)
3601 {
3602#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3603 PX86PDPAE pGuestPD = NULL;
3604 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3605 AssertRCReturn(rc, NULL);
3606#else
3607 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3608 if ( !pGuestPD
3609 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3610 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3611#endif
3612 return pGuestPD;
3613 /* returning NULL is ok if we assume it's just an invalid page of some kind emulated as all 0s. (not quite true) */
3614 }
3615 return NULL;
3616}
3617
3618
3619/**
3620 * Gets the page directory entry for the specified address.
3621 *
3622 * @returns Pointer to the page directory entry in question.
3623 * @returns NULL if the page directory is not present or on an invalid page.
3624 * @param pPGM Pointer to the PGM instance data.
3625 * @param GCPtr The address.
3626 */
3627DECLINLINE(PX86PDEPAE) pgmGstGetPaePDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
3628{
3629 AssertGCPtr32(GCPtr);
3630
3631 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3632 AssertReturn(pGuestPDPT, NULL);
3633 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3634 if (pGuestPDPT->a[iPdpt].n.u1Present)
3635 {
3636 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3637#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3638 PX86PDPAE pGuestPD = NULL;
3639 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3640 AssertRCReturn(rc, NULL);
3641#else
3642 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3643 if ( !pGuestPD
3644 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3645 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3646#endif
3647 return &pGuestPD->a[iPD];
3648 /* returning NIL_RTGCPHYS is ok if we assume it's just an invalid page or something which we'll emulate as all 0s. (not quite true) */
3649 }
3650 return NULL;
3651}
3652
3653
3654/**
3655 * Gets the page directory entry for the specified address.
3656 *
3657 * @returns The page directory entry in question.
3658 * @returns A non-present entry if the page directory is not present or on an invalid page.
3659 * @param pPGM Pointer to the PGM instance data.
3660 * @param GCPtr The address.
3661 */
3662DECLINLINE(X86PDEPAE) pgmGstGetPaePDE(PPGMCPU pPGM, RTGCPTR GCPtr)
3663{
3664 AssertGCPtr32(GCPtr);
3665 X86PDEPAE ZeroPde = {0};
3666 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3667 if (RT_LIKELY(pGuestPDPT))
3668 {
3669 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3670 if (pGuestPDPT->a[iPdpt].n.u1Present)
3671 {
3672 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3673#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3674 PX86PDPAE pGuestPD = NULL;
3675 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3676 AssertRCReturn(rc, ZeroPde);
3677#else
3678 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3679 if ( !pGuestPD
3680 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3681 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3682#endif
3683 return pGuestPD->a[iPD];
3684 }
3685 }
3686 return ZeroPde;
3687}
3688
3689
3690/**
3691 * Gets the page directory pointer table entry for the specified address
3692 * and returns the index into the page directory
3693 *
3694 * @returns Pointer to the page directory in question.
3695 * @returns NULL if the page directory is not present or on an invalid page.
3696 * @param pPGM Pointer to the PGM instance data.
3697 * @param GCPtr The address.
3698 * @param piPD Receives the index into the returned page directory
3699 * @param pPdpe Receives the page directory pointer entry. Optional.
3700 */
3701DECLINLINE(PX86PDPAE) pgmGstGetPaePDPtr(PPGMCPU pPGM, RTGCPTR GCPtr, unsigned *piPD, PX86PDPE pPdpe)
3702{
3703 AssertGCPtr32(GCPtr);
3704
3705 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3706 AssertReturn(pGuestPDPT, NULL);
3707 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3708 if (pPdpe)
3709 *pPdpe = pGuestPDPT->a[iPdpt];
3710 if (pGuestPDPT->a[iPdpt].n.u1Present)
3711 {
3712 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3713#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3714 PX86PDPAE pGuestPD = NULL;
3715 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3716 AssertRCReturn(rc, NULL);
3717#else
3718 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3719 if ( !pGuestPD
3720 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3721 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3722#endif
3723 *piPD = iPD;
3724 return pGuestPD;
3725 /* returning NIL_RTGCPHYS is ok if we assume it's just an invalid page of some kind emulated as all 0s. */
3726 }
3727 return NULL;
3728}
3729
3730#ifndef IN_RC
3731
3732/**
3733 * Gets the page map level-4 pointer for the guest.
3734 *
3735 * @returns Pointer to the PML4 page.
3736 * @param pPGM Pointer to the PGM instance data.
3737 */
3738DECLINLINE(PX86PML4) pgmGstGetLongModePML4Ptr(PPGMCPU pPGM)
3739{
3740#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3741 PX86PML4 pGuestPml4;
3742 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPml4);
3743 AssertRCReturn(rc, NULL);
3744#else
3745 PX86PML4 pGuestPml4 = pPGM->CTX_SUFF(pGstAmd64Pml4);
3746# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R3
3747 if (!pGuestPml4)
3748 pGuestPml4 = pgmGstLazyMapPml4(pPGM);
3749# endif
3750 Assert(pGuestPml4);
3751#endif
3752 return pGuestPml4;
3753}
3754
3755
3756/**
3757 * Gets the pointer to a page map level-4 entry.
3758 *
3759 * @returns Pointer to the PML4 entry.
3760 * @param pPGM Pointer to the PGM instance data.
3761 * @param iPml4 The index.
3762 */
3763DECLINLINE(PX86PML4E) pgmGstGetLongModePML4EPtr(PPGMCPU pPGM, unsigned int iPml4)
3764{
3765#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3766 PX86PML4 pGuestPml4;
3767 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPml4);
3768 AssertRCReturn(rc, NULL);
3769#else
3770 PX86PML4 pGuestPml4 = pPGM->CTX_SUFF(pGstAmd64Pml4);
3771# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R3
3772 if (!pGuestPml4)
3773 pGuestPml4 = pgmGstLazyMapPml4(pPGM);
3774# endif
3775 Assert(pGuestPml4);
3776#endif
3777 return &pGuestPml4->a[iPml4];
3778}
3779
3780
3781/**
3782 * Gets a page map level-4 entry.
3783 *
3784 * @returns The PML4 entry.
3785 * @param pPGM Pointer to the PGM instance data.
3786 * @param iPml4 The index.
3787 */
3788DECLINLINE(X86PML4E) pgmGstGetLongModePML4E(PPGMCPU pPGM, unsigned int iPml4)
3789{
3790#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3791 PX86PML4 pGuestPml4;
3792 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPml4);
3793 if (RT_FAILURE(rc))
3794 {
3795 X86PML4E ZeroPml4e = {0};
3796 AssertMsgFailedReturn(("%Rrc\n", rc), ZeroPml4e);
3797 }
3798#else
3799 PX86PML4 pGuestPml4 = pPGM->CTX_SUFF(pGstAmd64Pml4);
3800# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R3
3801 if (!pGuestPml4)
3802 pGuestPml4 = pgmGstLazyMapPml4(pPGM);
3803# endif
3804 Assert(pGuestPml4);
3805#endif
3806 return pGuestPml4->a[iPml4];
3807}
3808
3809
3810/**
3811 * Gets the page directory pointer entry for the specified address.
3812 *
3813 * @returns Pointer to the page directory pointer entry in question.
3814 * @returns NULL if the page directory is not present or on an invalid page.
3815 * @param pPGM Pointer to the PGM instance data.
3816 * @param GCPtr The address.
3817 * @param ppPml4e Page Map Level-4 Entry (out)
3818 */
3819DECLINLINE(PX86PDPE) pgmGstGetLongModePDPTPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e)
3820{
3821 PX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3822 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3823 PCX86PML4E pPml4e = *ppPml4e = &pGuestPml4->a[iPml4];
3824 if (pPml4e->n.u1Present)
3825 {
3826 PX86PDPT pPdpt;
3827 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPml4e->u & X86_PML4E_PG_MASK, &pPdpt);
3828 AssertRCReturn(rc, NULL);
3829
3830 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3831 return &pPdpt->a[iPdpt];
3832 }
3833 return NULL;
3834}
3835
3836
3837/**
3838 * Gets the page directory entry for the specified address.
3839 *
3840 * @returns The page directory entry in question.
3841 * @returns A non-present entry if the page directory is not present or on an invalid page.
3842 * @param pPGM Pointer to the PGM instance data.
3843 * @param GCPtr The address.
3844 * @param ppPml4e Page Map Level-4 Entry (out)
3845 * @param pPdpe Page directory pointer table entry (out)
3846 */
3847DECLINLINE(X86PDEPAE) pgmGstGetLongModePDEEx(PPGMCPU pPGM, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPE pPdpe)
3848{
3849 X86PDEPAE ZeroPde = {0};
3850 PX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3851 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3852 PCX86PML4E pPml4e = *ppPml4e = &pGuestPml4->a[iPml4];
3853 if (pPml4e->n.u1Present)
3854 {
3855 PCX86PDPT pPdptTemp;
3856 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPml4e->u & X86_PML4E_PG_MASK, &pPdptTemp);
3857 AssertRCReturn(rc, ZeroPde);
3858
3859 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3860 *pPdpe = pPdptTemp->a[iPdpt];
3861 if (pPdptTemp->a[iPdpt].n.u1Present)
3862 {
3863 PCX86PDPAE pPD;
3864 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3865 AssertRCReturn(rc, ZeroPde);
3866
3867 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3868 return pPD->a[iPD];
3869 }
3870 }
3871
3872 return ZeroPde;
3873}
3874
3875
3876/**
3877 * Gets the page directory entry for the specified address.
3878 *
3879 * @returns The page directory entry in question.
3880 * @returns A non-present entry if the page directory is not present or on an invalid page.
3881 * @param pPGM Pointer to the PGM instance data.
3882 * @param GCPtr The address.
3883 */
3884DECLINLINE(X86PDEPAE) pgmGstGetLongModePDE(PPGMCPU pPGM, RTGCPTR64 GCPtr)
3885{
3886 X86PDEPAE ZeroPde = {0};
3887 PCX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3888 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3889 if (pGuestPml4->a[iPml4].n.u1Present)
3890 {
3891 PCX86PDPT pPdptTemp;
3892 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pGuestPml4->a[iPml4].u & X86_PML4E_PG_MASK, &pPdptTemp);
3893 AssertRCReturn(rc, ZeroPde);
3894
3895 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3896 if (pPdptTemp->a[iPdpt].n.u1Present)
3897 {
3898 PCX86PDPAE pPD;
3899 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3900 AssertRCReturn(rc, ZeroPde);
3901
3902 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3903 return pPD->a[iPD];
3904 }
3905 }
3906 return ZeroPde;
3907}
3908
3909
3910/**
3911 * Gets the page directory entry for the specified address.
3912 *
3913 * @returns Pointer to the page directory entry in question.
3914 * @returns NULL if the page directory is not present or on an invalid page.
3915 * @param pPGM Pointer to the PGM instance data.
3916 * @param GCPtr The address.
3917 */
3918DECLINLINE(PX86PDEPAE) pgmGstGetLongModePDEPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr)
3919{
3920 PCX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3921 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3922 if (pGuestPml4->a[iPml4].n.u1Present)
3923 {
3924 PCX86PDPT pPdptTemp;
3925 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pGuestPml4->a[iPml4].u & X86_PML4E_PG_MASK, &pPdptTemp);
3926 AssertRCReturn(rc, NULL);
3927
3928 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3929 if (pPdptTemp->a[iPdpt].n.u1Present)
3930 {
3931 PX86PDPAE pPD;
3932 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3933 AssertRCReturn(rc, NULL);
3934
3935 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3936 return &pPD->a[iPD];
3937 }
3938 }
3939 return NULL;
3940}
3941
3942
3943/**
3944 * Gets the GUEST page directory pointer for the specified address.
3945 *
3946 * @returns The page directory in question.
3947 * @returns NULL if the page directory is not present or on an invalid page.
3948 * @param pPGM Pointer to the PGM instance data.
3949 * @param GCPtr The address.
3950 * @param ppPml4e Page Map Level-4 Entry (out)
3951 * @param pPdpe Page directory pointer table entry (out)
3952 * @param piPD Receives the index into the returned page directory
3953 */
3954DECLINLINE(PX86PDPAE) pgmGstGetLongModePDPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPE pPdpe, unsigned *piPD)
3955{
3956 PX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3957 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3958 PCX86PML4E pPml4e = *ppPml4e = &pGuestPml4->a[iPml4];
3959 if (pPml4e->n.u1Present)
3960 {
3961 PCX86PDPT pPdptTemp;
3962 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPml4e->u & X86_PML4E_PG_MASK, &pPdptTemp);
3963 AssertRCReturn(rc, NULL);
3964
3965 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3966 *pPdpe = pPdptTemp->a[iPdpt];
3967 if (pPdptTemp->a[iPdpt].n.u1Present)
3968 {
3969 PX86PDPAE pPD;
3970 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3971 AssertRCReturn(rc, NULL);
3972
3973 *piPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3974 return pPD;
3975 }
3976 }
3977 return 0;
3978}
3979
3980#endif /* !IN_RC */
3981
3982/**
3983 * Gets the shadow page directory, 32-bit.
3984 *
3985 * @returns Pointer to the shadow 32-bit PD.
3986 * @param pPGM Pointer to the PGM instance data.
3987 */
3988DECLINLINE(PX86PD) pgmShwGet32BitPDPtr(PPGMCPU pPGM)
3989{
3990 return (PX86PD)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPGM->CTX_SUFF(pShwPageCR3));
3991}
3992
3993
3994/**
3995 * Gets the shadow page directory entry for the specified address, 32-bit.
3996 *
3997 * @returns Shadow 32-bit PDE.
3998 * @param pPGM Pointer to the PGM instance data.
3999 * @param GCPtr The address.
4000 */
4001DECLINLINE(X86PDE) pgmShwGet32BitPDE(PPGMCPU pPGM, RTGCPTR GCPtr)
4002{
4003 const unsigned iPd = (GCPtr >> X86_PD_SHIFT) & X86_PD_MASK;
4004
4005 PX86PD pShwPde = pgmShwGet32BitPDPtr(pPGM);
4006 if (!pShwPde)
4007 {
4008 X86PDE ZeroPde = {0};
4009 return ZeroPde;
4010 }
4011 return pShwPde->a[iPd];
4012}
4013
4014
4015/**
4016 * Gets the pointer to the shadow page directory entry for the specified
4017 * address, 32-bit.
4018 *
4019 * @returns Pointer to the shadow 32-bit PDE.
4020 * @param pPGM Pointer to the PGM instance data.
4021 * @param GCPtr The address.
4022 */
4023DECLINLINE(PX86PDE) pgmShwGet32BitPDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
4024{
4025 const unsigned iPd = (GCPtr >> X86_PD_SHIFT) & X86_PD_MASK;
4026
4027 PX86PD pPde = pgmShwGet32BitPDPtr(pPGM);
4028 AssertReturn(pPde, NULL);
4029 return &pPde->a[iPd];
4030}
4031
4032
4033/**
4034 * Gets the shadow page pointer table, PAE.
4035 *
4036 * @returns Pointer to the shadow PAE PDPT.
4037 * @param pPGM Pointer to the PGM instance data.
4038 */
4039DECLINLINE(PX86PDPT) pgmShwGetPaePDPTPtr(PPGMCPU pPGM)
4040{
4041 return (PX86PDPT)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPGM->CTX_SUFF(pShwPageCR3));
4042}
4043
4044
4045/**
4046 * Gets the shadow page directory for the specified address, PAE.
4047 *
4048 * @returns Pointer to the shadow PD.
4049 * @param pPGM Pointer to the PGM instance data.
4050 * @param GCPtr The address.
4051 */
4052DECLINLINE(PX86PDPAE) pgmShwGetPaePDPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
4053{
4054 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
4055 PX86PDPT pPdpt = pgmShwGetPaePDPTPtr(pPGM);
4056
4057 if (!pPdpt->a[iPdpt].n.u1Present)
4058 return NULL;
4059
4060 /* Fetch the pgm pool shadow descriptor. */
4061 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(PGMCPU2PGM(pPGM)->CTX_SUFF(pPool), pPdpt->a[iPdpt].u & X86_PDPE_PG_MASK);
4062 AssertReturn(pShwPde, NULL);
4063
4064 return (PX86PDPAE)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pShwPde);
4065}
4066
4067
4068/**
4069 * Gets the shadow page directory for the specified address, PAE.
4070 *
4071 * @returns Pointer to the shadow PD.
4072 * @param pPGM Pointer to the PGM instance data.
4073 * @param GCPtr The address.
4074 */
4075DECLINLINE(PX86PDPAE) pgmShwGetPaePDPtr(PPGMCPU pPGM, PX86PDPT pPdpt, RTGCPTR GCPtr)
4076{
4077 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
4078
4079 if (!pPdpt->a[iPdpt].n.u1Present)
4080 return NULL;
4081
4082 /* Fetch the pgm pool shadow descriptor. */
4083 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(PGMCPU2PGM(pPGM)->CTX_SUFF(pPool), pPdpt->a[iPdpt].u & X86_PDPE_PG_MASK);
4084 AssertReturn(pShwPde, NULL);
4085
4086 return (PX86PDPAE)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pShwPde);
4087}
4088
4089
4090/**
4091 * Gets the shadow page directory entry, PAE.
4092 *
4093 * @returns PDE.
4094 * @param pPGM Pointer to the PGM instance data.
4095 * @param GCPtr The address.
4096 */
4097DECLINLINE(X86PDEPAE) pgmShwGetPaePDE(PPGMCPU pPGM, RTGCPTR GCPtr)
4098{
4099 const unsigned iPd = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
4100
4101 PX86PDPAE pShwPde = pgmShwGetPaePDPtr(pPGM, GCPtr);
4102 if (!pShwPde)
4103 {
4104 X86PDEPAE ZeroPde = {0};
4105 return ZeroPde;
4106 }
4107 return pShwPde->a[iPd];
4108}
4109
4110
4111/**
4112 * Gets the pointer to the shadow page directory entry for an address, PAE.
4113 *
4114 * @returns Pointer to the PDE.
4115 * @param pPGM Pointer to the PGM instance data.
4116 * @param GCPtr The address.
4117 */
4118DECLINLINE(PX86PDEPAE) pgmShwGetPaePDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
4119{
4120 const unsigned iPd = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
4121
4122 PX86PDPAE pPde = pgmShwGetPaePDPtr(pPGM, GCPtr);
4123 AssertReturn(pPde, NULL);
4124 return &pPde->a[iPd];
4125}
4126
4127#ifndef IN_RC
4128
4129/**
4130 * Gets the shadow page map level-4 pointer.
4131 *
4132 * @returns Pointer to the shadow PML4.
4133 * @param pPGM Pointer to the PGM instance data.
4134 */
4135DECLINLINE(PX86PML4) pgmShwGetLongModePML4Ptr(PPGMCPU pPGM)
4136{
4137 return (PX86PML4)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPGM->CTX_SUFF(pShwPageCR3));
4138}
4139
4140
4141/**
4142 * Gets the shadow page map level-4 entry for the specified address.
4143 *
4144 * @returns The entry.
4145 * @param pPGM Pointer to the PGM instance data.
4146 * @param GCPtr The address.
4147 */
4148DECLINLINE(X86PML4E) pgmShwGetLongModePML4E(PPGMCPU pPGM, RTGCPTR GCPtr)
4149{
4150 const unsigned iPml4 = ((RTGCUINTPTR64)GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
4151 PX86PML4 pShwPml4 = pgmShwGetLongModePML4Ptr(pPGM);
4152
4153 if (!pShwPml4)
4154 {
4155 X86PML4E ZeroPml4e = {0};
4156 return ZeroPml4e;
4157 }
4158 return pShwPml4->a[iPml4];
4159}
4160
4161
4162/**
4163 * Gets the pointer to the specified shadow page map level-4 entry.
4164 *
4165 * @returns The entry.
4166 * @param pPGM Pointer to the PGM instance data.
4167 * @param iPml4 The PML4 index.
4168 */
4169DECLINLINE(PX86PML4E) pgmShwGetLongModePML4EPtr(PPGMCPU pPGM, unsigned int iPml4)
4170{
4171 PX86PML4 pShwPml4 = pgmShwGetLongModePML4Ptr(pPGM);
4172 if (!pShwPml4)
4173 return NULL;
4174 return &pShwPml4->a[iPml4];
4175}
4176
4177
4178/**
4179 * Gets the GUEST page directory pointer for the specified address.
4180 *
4181 * @returns The page directory in question.
4182 * @returns NULL if the page directory is not present or on an invalid page.
4183 * @param pPGM Pointer to the PGM instance data.
4184 * @param GCPtr The address.
4185 * @param piPD Receives the index into the returned page directory
4186 */
4187DECLINLINE(PX86PDPAE) pgmGstGetLongModePDPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr, unsigned *piPD)
4188{
4189 PCX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
4190 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
4191 if (pGuestPml4->a[iPml4].n.u1Present)
4192 {
4193 PCX86PDPT pPdptTemp;
4194 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pGuestPml4->a[iPml4].u & X86_PML4E_PG_MASK, &pPdptTemp);
4195 AssertRCReturn(rc, NULL);
4196
4197 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
4198 if (pPdptTemp->a[iPdpt].n.u1Present)
4199 {
4200 PX86PDPAE pPD;
4201 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
4202 AssertRCReturn(rc, NULL);
4203
4204 *piPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
4205 return pPD;
4206 }
4207 }
4208 return NULL;
4209}
4210
4211#endif /* !IN_RC */
4212
4213/**
4214 * Gets the page state for a physical handler.
4215 *
4216 * @returns The physical handler page state.
4217 * @param pCur The physical handler in question.
4218 */
4219DECLINLINE(unsigned) pgmHandlerPhysicalCalcState(PPGMPHYSHANDLER pCur)
4220{
4221 switch (pCur->enmType)
4222 {
4223 case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE:
4224 return PGM_PAGE_HNDL_PHYS_STATE_WRITE;
4225
4226 case PGMPHYSHANDLERTYPE_MMIO:
4227 case PGMPHYSHANDLERTYPE_PHYSICAL_ALL:
4228 return PGM_PAGE_HNDL_PHYS_STATE_ALL;
4229
4230 default:
4231 AssertFatalMsgFailed(("Invalid type %d\n", pCur->enmType));
4232 }
4233}
4234
4235
4236/**
4237 * Gets the page state for a virtual handler.
4238 *
4239 * @returns The virtual handler page state.
4240 * @param pCur The virtual handler in question.
4241 * @remarks This should never be used on a hypervisor access handler.
4242 */
4243DECLINLINE(unsigned) pgmHandlerVirtualCalcState(PPGMVIRTHANDLER pCur)
4244{
4245 switch (pCur->enmType)
4246 {
4247 case PGMVIRTHANDLERTYPE_WRITE:
4248 return PGM_PAGE_HNDL_VIRT_STATE_WRITE;
4249 case PGMVIRTHANDLERTYPE_ALL:
4250 return PGM_PAGE_HNDL_VIRT_STATE_ALL;
4251 default:
4252 AssertFatalMsgFailed(("Invalid type %d\n", pCur->enmType));
4253 }
4254}
4255
4256
4257/**
4258 * Clears one physical page of a virtual handler
4259 *
4260 * @param pPGM Pointer to the PGM instance.
4261 * @param pCur Virtual handler structure
4262 * @param iPage Physical page index
4263 *
4264 * @remark Only used when PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL is being set, so no
4265 * need to care about other handlers in the same page.
4266 */
4267DECLINLINE(void) pgmHandlerVirtualClearPage(PPGM pPGM, PPGMVIRTHANDLER pCur, unsigned iPage)
4268{
4269 const PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
4270
4271 /*
4272 * Remove the node from the tree (it's supposed to be in the tree if we get here!).
4273 */
4274#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4275 AssertReleaseMsg(pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE,
4276 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4277 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias));
4278#endif
4279 if (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD)
4280 {
4281 /* We're the head of the alias chain. */
4282 PPGMPHYS2VIRTHANDLER pRemove = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRemove(&pPGM->CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key); NOREF(pRemove);
4283#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4284 AssertReleaseMsg(pRemove != NULL,
4285 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4286 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias));
4287 AssertReleaseMsg(pRemove == pPhys2Virt,
4288 ("wanted: pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n"
4289 " got: pRemove=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4290 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias,
4291 pRemove, pRemove->Core.Key, pRemove->Core.KeyLast, pRemove->offVirtHandler, pRemove->offNextAlias));
4292#endif
4293 if (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK)
4294 {
4295 /* Insert the next list in the alias chain into the tree. */
4296 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
4297#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4298 AssertReleaseMsg(pNext->offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE,
4299 ("pNext=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4300 pNext, pNext->Core.Key, pNext->Core.KeyLast, pNext->offVirtHandler, pNext->offNextAlias));
4301#endif
4302 pNext->offNextAlias |= PGMPHYS2VIRTHANDLER_IS_HEAD;
4303 bool fRc = RTAvlroGCPhysInsert(&pPGM->CTX_SUFF(pTrees)->PhysToVirtHandlers, &pNext->Core);
4304 AssertRelease(fRc);
4305 }
4306 }
4307 else
4308 {
4309 /* Locate the previous node in the alias chain. */
4310 PPGMPHYS2VIRTHANDLER pPrev = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pPGM->CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
4311#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4312 AssertReleaseMsg(pPrev != pPhys2Virt,
4313 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} pPrev=%p\n",
4314 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias, pPrev));
4315#endif
4316 for (;;)
4317 {
4318 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPrev + (pPrev->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
4319 if (pNext == pPhys2Virt)
4320 {
4321 /* unlink. */
4322 LogFlow(("pgmHandlerVirtualClearPage: removed %p:{.offNextAlias=%#RX32} from alias chain. prev %p:{.offNextAlias=%#RX32} [%RGp-%RGp]\n",
4323 pPhys2Virt, pPhys2Virt->offNextAlias, pPrev, pPrev->offNextAlias, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
4324 if (!(pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
4325 pPrev->offNextAlias &= ~PGMPHYS2VIRTHANDLER_OFF_MASK;
4326 else
4327 {
4328 PPGMPHYS2VIRTHANDLER pNewNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
4329 pPrev->offNextAlias = ((intptr_t)pNewNext - (intptr_t)pPrev)
4330 | (pPrev->offNextAlias & ~PGMPHYS2VIRTHANDLER_OFF_MASK);
4331 }
4332 break;
4333 }
4334
4335 /* next */
4336 if (pNext == pPrev)
4337 {
4338#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4339 AssertReleaseMsg(pNext != pPrev,
4340 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} pPrev=%p\n",
4341 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias, pPrev));
4342#endif
4343 break;
4344 }
4345 pPrev = pNext;
4346 }
4347 }
4348 Log2(("PHYS2VIRT: Removing %RGp-%RGp %#RX32 %s\n",
4349 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
4350 pPhys2Virt->offNextAlias = 0;
4351 pPhys2Virt->Core.KeyLast = NIL_RTGCPHYS; /* require reinsert */
4352
4353 /*
4354 * Clear the ram flags for this page.
4355 */
4356 PPGMPAGE pPage = pgmPhysGetPage(pPGM, pPhys2Virt->Core.Key);
4357 AssertReturnVoid(pPage);
4358 PGM_PAGE_SET_HNDL_VIRT_STATE(pPage, PGM_PAGE_HNDL_VIRT_STATE_NONE);
4359}
4360
4361
4362/**
4363 * Internal worker for finding a 'in-use' shadow page give by it's physical address.
4364 *
4365 * @returns Pointer to the shadow page structure.
4366 * @param pPool The pool.
4367 * @param idx The pool page index.
4368 */
4369DECLINLINE(PPGMPOOLPAGE) pgmPoolGetPageByIdx(PPGMPOOL pPool, unsigned idx)
4370{
4371 AssertFatalMsg(idx >= PGMPOOL_IDX_FIRST && idx < pPool->cCurPages, ("idx=%d\n", idx));
4372 return &pPool->aPages[idx];
4373}
4374
4375
4376#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
4377/**
4378 * Clear references to guest physical memory.
4379 *
4380 * @param pPool The pool.
4381 * @param pPoolPage The pool page.
4382 * @param pPhysPage The physical guest page tracking structure.
4383 */
4384DECLINLINE(void) pgmTrackDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPoolPage, PPGMPAGE pPhysPage)
4385{
4386 /*
4387 * Just deal with the simple case here.
4388 */
4389# ifdef LOG_ENABLED
4390 const unsigned uOrg = PGM_PAGE_GET_TRACKING(pPhysPage);
4391# endif
4392 const unsigned cRefs = PGM_PAGE_GET_TD_CREFS(pPhysPage);
4393 if (cRefs == 1)
4394 {
4395 Assert(pPoolPage->idx == PGM_PAGE_GET_TD_IDX(pPhysPage));
4396 PGM_PAGE_SET_TRACKING(pPhysPage, 0);
4397 }
4398 else
4399 pgmPoolTrackPhysExtDerefGCPhys(pPool, pPoolPage, pPhysPage);
4400 Log2(("pgmTrackDerefGCPhys: %x -> %x pPhysPage=%R[pgmpage]\n", uOrg, PGM_PAGE_GET_TRACKING(pPhysPage), pPhysPage ));
4401}
4402#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
4403
4404
4405#ifdef PGMPOOL_WITH_CACHE
4406/**
4407 * Moves the page to the head of the age list.
4408 *
4409 * This is done when the cached page is used in one way or another.
4410 *
4411 * @param pPool The pool.
4412 * @param pPage The cached page.
4413 */
4414DECLINLINE(void) pgmPoolCacheUsed(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4415{
4416 PVM pVM = pPool->CTX_SUFF(pVM);
4417 pgmLock(pVM);
4418
4419 /*
4420 * Move to the head of the age list.
4421 */
4422 if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
4423 {
4424 /* unlink */
4425 pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
4426 if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
4427 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
4428 else
4429 pPool->iAgeTail = pPage->iAgePrev;
4430
4431 /* insert at head */
4432 pPage->iAgePrev = NIL_PGMPOOL_IDX;
4433 pPage->iAgeNext = pPool->iAgeHead;
4434 Assert(pPage->iAgeNext != NIL_PGMPOOL_IDX); /* we would've already been head then */
4435 pPool->iAgeHead = pPage->idx;
4436 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->idx;
4437 }
4438 pgmUnlock(pVM);
4439}
4440#endif /* PGMPOOL_WITH_CACHE */
4441
4442/**
4443 * Locks a page to prevent flushing (important for cr3 root pages or shadow pae pd pages).
4444 *
4445 * @param pVM VM Handle.
4446 * @param pPage PGM pool page
4447 */
4448DECLINLINE(void) pgmPoolLockPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4449{
4450 ASMAtomicIncU32(&pPage->cLocked);
4451}
4452
4453
4454/**
4455 * Unlocks a page to allow flushing again
4456 *
4457 * @param pVM VM Handle.
4458 * @param pPage PGM pool page
4459 */
4460DECLINLINE(void) pgmPoolUnlockPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4461{
4462 Assert(pPage->cLocked);
4463 ASMAtomicDecU32(&pPage->cLocked);
4464}
4465
4466
4467/**
4468 * Checks if the page is locked (e.g. the active CR3 or one of the four PDs of a PAE PDPT)
4469 *
4470 * @returns VBox status code.
4471 * @param pPage PGM pool page
4472 */
4473DECLINLINE(bool) pgmPoolIsPageLocked(PPGM pPGM, PPGMPOOLPAGE pPage)
4474{
4475 if (pPage->cLocked)
4476 {
4477 LogFlow(("pgmPoolIsPageLocked found root page %d\n", pPage->enmKind));
4478 if (pPage->cModifications)
4479 pPage->cModifications = 1; /* reset counter (can't use 0, or else it will be reinserted in the modified list) */
4480 return true;
4481 }
4482 return false;
4483}
4484
4485/**
4486 * Tells if mappings are to be put into the shadow page table or not
4487 *
4488 * @returns boolean result
4489 * @param pVM VM handle.
4490 */
4491DECLINLINE(bool) pgmMapAreMappingsEnabled(PPGM pPGM)
4492{
4493#ifdef IN_RING0
4494 /* There are no mappings in VT-x and AMD-V mode. */
4495 Assert(pPGM->fDisableMappings);
4496 return false;
4497#else
4498 return !pPGM->fDisableMappings;
4499#endif
4500}
4501
4502/** @} */
4503
4504#endif
4505
4506
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