VirtualBox

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

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

Missing descriptions

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