VirtualBox

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

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

Removed obsolete return codes

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