VirtualBox

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

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

VMM: BEGIN_DECLS -> RT_BEGIN_DECLS; END_DECLS -> RT_END_DECLS.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 186.1 KB
Line 
1/* $Id: PGMInternal.h 20365 2009-06-08 00:19:18Z 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[11];
2225
2226 /*
2227 * This will be redefined at least two more times before we're done, I'm sure.
2228 * The current code is only to get on with the coding.
2229 * - 2004-06-10: initial version, bird.
2230 * - 2004-07-02: 1st time, bird.
2231 * - 2004-10-18: 2nd time, bird.
2232 * - 2005-07-xx: 3rd time, bird.
2233 */
2234
2235 /** Pointer to the page table entries for the dynamic page mapping area - GCPtr. */
2236 RCPTRTYPE(PX86PTE) paDynPageMap32BitPTEsGC;
2237 /** Pointer to the page table entries for the dynamic page mapping area - GCPtr. */
2238 RCPTRTYPE(PX86PTEPAE) paDynPageMapPaePTEsGC;
2239
2240 /** The host paging mode. (This is what SUPLib reports.) */
2241 SUPPAGINGMODE enmHostMode;
2242
2243 /** 4 MB page mask; 32 or 36 bits depending on PSE-36 (identical for all VCPUs) */
2244 RTGCPHYS GCPhys4MBPSEMask;
2245
2246 /** Pointer to the list of RAM ranges (Phys GC -> Phys HC conversion) - for R3.
2247 * This is sorted by physical address and contains no overlapping ranges. */
2248 R3PTRTYPE(PPGMRAMRANGE) pRamRangesR3;
2249 /** R0 pointer corresponding to PGM::pRamRangesR3. */
2250 R0PTRTYPE(PPGMRAMRANGE) pRamRangesR0;
2251 /** RC pointer corresponding to PGM::pRamRangesR3. */
2252 RCPTRTYPE(PPGMRAMRANGE) pRamRangesRC;
2253 RTRCPTR alignment4; /**< structure alignment. */
2254
2255 /** Pointer to the list of ROM ranges - for R3.
2256 * This is sorted by physical address and contains no overlapping ranges. */
2257 R3PTRTYPE(PPGMROMRANGE) pRomRangesR3;
2258 /** R0 pointer corresponding to PGM::pRomRangesR3. */
2259 R0PTRTYPE(PPGMROMRANGE) pRomRangesR0;
2260 /** RC pointer corresponding to PGM::pRomRangesR3. */
2261 RCPTRTYPE(PPGMROMRANGE) pRomRangesRC;
2262 /** Alignment padding. */
2263 RTRCPTR GCPtrPadding2;
2264
2265 /** Pointer to the list of MMIO2 ranges - for R3.
2266 * Registration order. */
2267 R3PTRTYPE(PPGMMMIO2RANGE) pMmio2RangesR3;
2268
2269 /** PGM offset based trees - R3 Ptr. */
2270 R3PTRTYPE(PPGMTREES) pTreesR3;
2271 /** PGM offset based trees - R0 Ptr. */
2272 R0PTRTYPE(PPGMTREES) pTreesR0;
2273 /** PGM offset based trees - RC Ptr. */
2274 RCPTRTYPE(PPGMTREES) pTreesRC;
2275
2276 /** Linked list of GC mappings - for RC.
2277 * The list is sorted ascending on address.
2278 */
2279 RCPTRTYPE(PPGMMAPPING) pMappingsRC;
2280 /** Linked list of GC mappings - for HC.
2281 * The list is sorted ascending on address.
2282 */
2283 R3PTRTYPE(PPGMMAPPING) pMappingsR3;
2284 /** Linked list of GC mappings - for R0.
2285 * The list is sorted ascending on address.
2286 */
2287 R0PTRTYPE(PPGMMAPPING) pMappingsR0;
2288
2289 /** Pointer to the 5 page CR3 content mapping.
2290 * The first page is always the CR3 (in some form) while the 4 other pages
2291 * are used of the PDs in PAE mode. */
2292 RTGCPTR GCPtrCR3Mapping;
2293#if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
2294 uint32_t u32Alignment;
2295#endif
2296
2297 /** Indicates that PGMR3FinalizeMappings has been called and that further
2298 * PGMR3MapIntermediate calls will be rejected. */
2299 bool fFinalizedMappings;
2300 /** If set no conflict checks are required. (boolean) */
2301 bool fMappingsFixed;
2302 /** If set, then no mappings are put into the shadow page table. (boolean) */
2303 bool fDisableMappings;
2304 /** Size of fixed mapping */
2305 uint32_t cbMappingFixed;
2306 /** Base address (GC) of fixed mapping */
2307 RTGCPTR GCPtrMappingFixed;
2308 /** The address of the previous RAM range mapping. */
2309 RTGCPTR GCPtrPrevRamRangeMapping;
2310
2311 /** @name Intermediate Context
2312 * @{ */
2313 /** Pointer to the intermediate page directory - Normal. */
2314 R3PTRTYPE(PX86PD) pInterPD;
2315 /** Pointer to the intermedate page tables - Normal.
2316 * There are two page tables, one for the identity mapping and one for
2317 * the host context mapping (of the core code). */
2318 R3PTRTYPE(PX86PT) apInterPTs[2];
2319 /** Pointer to the intermedate page tables - PAE. */
2320 R3PTRTYPE(PX86PTPAE) apInterPaePTs[2];
2321 /** Pointer to the intermedate page directory - PAE. */
2322 R3PTRTYPE(PX86PDPAE) apInterPaePDs[4];
2323 /** Pointer to the intermedate page directory - PAE. */
2324 R3PTRTYPE(PX86PDPT) pInterPaePDPT;
2325 /** Pointer to the intermedate page-map level 4 - AMD64. */
2326 R3PTRTYPE(PX86PML4) pInterPaePML4;
2327 /** Pointer to the intermedate page directory - AMD64. */
2328 R3PTRTYPE(PX86PDPT) pInterPaePDPT64;
2329 /** The Physical Address (HC) of the intermediate Page Directory - Normal. */
2330 RTHCPHYS HCPhysInterPD;
2331 /** The Physical Address (HC) of the intermediate Page Directory Pointer Table - PAE. */
2332 RTHCPHYS HCPhysInterPaePDPT;
2333 /** The Physical Address (HC) of the intermediate Page Map Level 4 table - AMD64. */
2334 RTHCPHYS HCPhysInterPaePML4;
2335 /** @} */
2336
2337 /** Base address of the dynamic page mapping area.
2338 * The array is MM_HYPER_DYNAMIC_SIZE bytes big.
2339 */
2340 RCPTRTYPE(uint8_t *) pbDynPageMapBaseGC;
2341 /** The index of the last entry used in the dynamic page mapping area. */
2342 RTUINT iDynPageMapLast;
2343 /** Cache containing the last entries in the dynamic page mapping area.
2344 * The cache size is covering half of the mapping area. */
2345 RTHCPHYS aHCPhysDynPageMapCache[MM_HYPER_DYNAMIC_SIZE >> (PAGE_SHIFT + 1)];
2346 /** Keep a lock counter for the full (!) mapping area. */
2347 uint32_t aLockedDynPageMapCache[MM_HYPER_DYNAMIC_SIZE >> (PAGE_SHIFT)];
2348
2349 /** The address of the ring-0 mapping cache if we're making use of it. */
2350 RTR0PTR pvR0DynMapUsed;
2351
2352 /** PGM critical section.
2353 * This protects the physical & virtual access handlers, ram ranges,
2354 * and the page flag updating (some of it anyway).
2355 */
2356 PDMCRITSECT CritSect;
2357
2358 /** Pointer to SHW+GST mode data (function pointers).
2359 * The index into this table is made up from */
2360 R3PTRTYPE(PPGMMODEDATA) paModeData;
2361
2362 /** Shadow Page Pool - R3 Ptr. */
2363 R3PTRTYPE(PPGMPOOL) pPoolR3;
2364 /** Shadow Page Pool - R0 Ptr. */
2365 R0PTRTYPE(PPGMPOOL) pPoolR0;
2366 /** Shadow Page Pool - RC Ptr. */
2367 RCPTRTYPE(PPGMPOOL) pPoolRC;
2368
2369 /** We're not in a state which permits writes to guest memory.
2370 * (Only used in strict builds.) */
2371 bool fNoMorePhysWrites;
2372
2373 /** Flush the cache on the next access. */
2374 bool fPhysCacheFlushPending;
2375/** @todo r=bird: Fix member names!*/
2376 /** PGMPhysRead cache */
2377 PGMPHYSCACHE pgmphysreadcache;
2378 /** PGMPhysWrite cache */
2379 PGMPHYSCACHE pgmphyswritecache;
2380
2381 /**
2382 * Data associated with managing the ring-3 mappings of the allocation chunks.
2383 */
2384 struct
2385 {
2386 /** The chunk tree, ordered by chunk id. */
2387#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
2388 R3PTRTYPE(PAVLU32NODECORE) pTree;
2389#else
2390 R3R0PTRTYPE(PAVLU32NODECORE) pTree;
2391#endif
2392 /** The chunk mapping TLB. */
2393 PGMCHUNKR3MAPTLB Tlb;
2394 /** The number of mapped chunks. */
2395 uint32_t c;
2396 /** The maximum number of mapped chunks.
2397 * @cfgm PGM/MaxRing3Chunks */
2398 uint32_t cMax;
2399 /** The chunk age tree, ordered by ageing sequence number. */
2400 R3PTRTYPE(PAVLLU32NODECORE) pAgeTree;
2401 /** The current time. */
2402 uint32_t iNow;
2403 /** Number of pgmR3PhysChunkFindUnmapCandidate calls left to the next ageing. */
2404 uint32_t AgeingCountdown;
2405 } ChunkR3Map;
2406
2407 /**
2408 * The page mapping TLB for ring-3 and (for the time being) ring-0.
2409 */
2410 PGMPAGER3MAPTLB PhysTlbHC;
2411
2412 /** @name The zero page.
2413 * @{ */
2414 /** The host physical address of the zero page. */
2415 RTHCPHYS HCPhysZeroPg;
2416 /** The ring-3 mapping of the zero page. */
2417 RTR3PTR pvZeroPgR3;
2418 /** The ring-0 mapping of the zero page. */
2419 RTR0PTR pvZeroPgR0;
2420 /** The GC mapping of the zero page. */
2421 RTGCPTR pvZeroPgRC;
2422#if GC_ARCH_BITS != 32
2423 uint32_t u32ZeroAlignment; /**< Alignment padding. */
2424#endif
2425 /** @}*/
2426
2427 /** The number of handy pages. */
2428 uint32_t cHandyPages;
2429 /**
2430 * Array of handy pages.
2431 *
2432 * This array is used in a two way communication between pgmPhysAllocPage
2433 * and GMMR0AllocateHandyPages, with PGMR3PhysAllocateHandyPages serving as
2434 * an intermediary.
2435 *
2436 * The size of this array is important, see pgmPhysEnsureHandyPage for details.
2437 * (The current size of 32 pages, means 128 KB of handy memory.)
2438 */
2439 GMMPAGEDESC aHandyPages[PGM_HANDY_PAGES];
2440
2441 /** @name Error injection.
2442 * @{ */
2443 /** Inject handy page allocation errors pretending we're completely out of
2444 * memory. */
2445 bool volatile fErrInjHandyPages;
2446 /** Padding. */
2447 bool afReserved[7];
2448 /** @} */
2449
2450 /** @name Release Statistics
2451 * @{ */
2452 uint32_t cAllPages; /**< The total number of pages. (Should be Private + Shared + Zero.) */
2453 uint32_t cPrivatePages; /**< The number of private pages. */
2454 uint32_t cSharedPages; /**< The number of shared pages. */
2455 uint32_t cZeroPages; /**< The number of zero backed pages. */
2456
2457 /** The number of times we were forced to change the hypervisor region location. */
2458 STAMCOUNTER cRelocations;
2459 /** @} */
2460
2461#ifdef VBOX_WITH_STATISTICS /** @todo move this chunk to the heap. */
2462 /* R3 only: */
2463 STAMCOUNTER StatR3DetectedConflicts; /**< R3: Number of times PGMR3MapHasConflicts() detected a conflict. */
2464 STAMPROFILE StatR3ResolveConflict; /**< R3: pgmR3SyncPTResolveConflict() profiling (includes the entire relocation). */
2465
2466 STAMCOUNTER StatRZChunkR3MapTlbHits; /**< RC/R0: Ring-3/0 chunk mapper TLB hits. */
2467 STAMCOUNTER StatRZChunkR3MapTlbMisses; /**< RC/R0: Ring-3/0 chunk mapper TLB misses. */
2468 STAMCOUNTER StatRZPageMapTlbHits; /**< RC/R0: Ring-3/0 page mapper TLB hits. */
2469 STAMCOUNTER StatRZPageMapTlbMisses; /**< RC/R0: Ring-3/0 page mapper TLB misses. */
2470 STAMCOUNTER StatR3ChunkR3MapTlbHits; /**< R3: Ring-3/0 chunk mapper TLB hits. */
2471 STAMCOUNTER StatR3ChunkR3MapTlbMisses; /**< R3: Ring-3/0 chunk mapper TLB misses. */
2472 STAMCOUNTER StatR3PageMapTlbHits; /**< R3: Ring-3/0 page mapper TLB hits. */
2473 STAMCOUNTER StatR3PageMapTlbMisses; /**< R3: Ring-3/0 page mapper TLB misses. */
2474 STAMPROFILE StatRZSyncCR3HandlerVirtualReset; /**< RC/R0: Profiling of the virtual handler resets. */
2475 STAMPROFILE StatRZSyncCR3HandlerVirtualUpdate; /**< RC/R0: Profiling of the virtual handler updates. */
2476 STAMPROFILE StatR3SyncCR3HandlerVirtualReset; /**< R3: Profiling of the virtual handler resets. */
2477 STAMPROFILE StatR3SyncCR3HandlerVirtualUpdate; /**< R3: Profiling of the virtual handler updates. */
2478 STAMCOUNTER StatR3PhysHandlerReset; /**< R3: The number of times PGMHandlerPhysicalReset is called. */
2479 STAMCOUNTER StatRZPhysHandlerReset; /**< RC/R0: The number of times PGMHandlerPhysicalReset is called. */
2480 STAMPROFILE StatRZVirtHandlerSearchByPhys; /**< RC/R0: Profiling of pgmHandlerVirtualFindByPhysAddr. */
2481 STAMPROFILE StatR3VirtHandlerSearchByPhys; /**< R3: Profiling of pgmHandlerVirtualFindByPhysAddr. */
2482 STAMCOUNTER StatRZPageReplaceShared; /**< RC/R0: Times a shared page has been replaced by a private one. */
2483 STAMCOUNTER StatRZPageReplaceZero; /**< RC/R0: Times the zero page has been replaced by a private one. */
2484/// @todo STAMCOUNTER StatRZPageHandyAllocs; /**< RC/R0: The number of times we've executed GMMR3AllocateHandyPages. */
2485 STAMCOUNTER StatR3PageReplaceShared; /**< R3: Times a shared page has been replaced by a private one. */
2486 STAMCOUNTER StatR3PageReplaceZero; /**< R3: Times the zero page has been replaced by a private one. */
2487/// @todo STAMCOUNTER StatR3PageHandyAllocs; /**< R3: The number of times we've executed GMMR3AllocateHandyPages. */
2488
2489 /* RC only: */
2490 STAMCOUNTER StatRCDynMapCacheMisses; /**< RC: The number of dynamic page mapping cache misses */
2491 STAMCOUNTER StatRCDynMapCacheHits; /**< RC: The number of dynamic page mapping cache hits */
2492 STAMCOUNTER StatRCInvlPgConflict; /**< RC: Number of times PGMInvalidatePage() detected a mapping conflict. */
2493 STAMCOUNTER StatRCInvlPgSyncMonCR3; /**< RC: Number of times PGMInvalidatePage() ran into PGM_SYNC_MONITOR_CR3. */
2494
2495# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2496 STAMCOUNTER StatTrackVirgin; /**< The number of first time shadowings. */
2497 STAMCOUNTER StatTrackAliased; /**< The number of times switching to cRef2, i.e. the page is being shadowed by two PTs. */
2498 STAMCOUNTER StatTrackAliasedMany; /**< The number of times we're tracking using cRef2. */
2499 STAMCOUNTER StatTrackAliasedLots; /**< The number of times we're hitting pages which has overflowed cRef2. */
2500 STAMCOUNTER StatTrackOverflows; /**< The number of times the extent list grows to long. */
2501 STAMPROFILE StatTrackDeref; /**< Profiling of SyncPageWorkerTrackDeref (expensive). */
2502# endif
2503#endif
2504} PGM;
2505/** Pointer to the PGM instance data. */
2506typedef PGM *PPGM;
2507
2508
2509/**
2510 * Converts a PGMCPU pointer into a VM pointer.
2511 * @returns Pointer to the VM structure the PGM is part of.
2512 * @param pPGM Pointer to PGMCPU instance data.
2513 */
2514#define PGMCPU2VM(pPGM) ( (PVM)((char*)pPGM - pPGM->offVM) )
2515
2516/**
2517 * Converts a PGMCPU pointer into a PGM pointer.
2518 * @returns Pointer to the VM structure the PGM is part of.
2519 * @param pPGM Pointer to PGMCPU instance data.
2520 */
2521#define PGMCPU2PGM(pPGMCpu) ( (PPGM)((char*)pPGMCpu - pPGMCpu->offPGM) )
2522
2523/**
2524 * PGMCPU Data (part of VMCPU).
2525 */
2526typedef struct PGMCPU
2527{
2528 /** Offset to the VM structure. */
2529 RTINT offVM;
2530 /** Offset to the VMCPU structure. */
2531 RTINT offVCpu;
2532 /** Offset of the PGM structure relative to VMCPU. */
2533 RTINT offPGM;
2534 RTINT uPadding0; /**< structure size alignment. */
2535
2536#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
2537 /** Automatically tracked physical memory mapping set.
2538 * Ring-0 and strict raw-mode builds. */
2539 PGMMAPSET AutoSet;
2540#endif
2541
2542 /** A20 gate mask.
2543 * Our current approach to A20 emulation is to let REM do it and don't bother
2544 * anywhere else. The interesting Guests will be operating with it enabled anyway.
2545 * But whould need arrise, we'll subject physical addresses to this mask. */
2546 RTGCPHYS GCPhysA20Mask;
2547 /** A20 gate state - boolean! */
2548 bool fA20Enabled;
2549
2550 /** What needs syncing (PGM_SYNC_*).
2551 * This is used to queue operations for PGMSyncCR3, PGMInvalidatePage,
2552 * PGMFlushTLB, and PGMR3Load. */
2553 RTUINT fSyncFlags;
2554
2555 /** The shadow paging mode. */
2556 PGMMODE enmShadowMode;
2557 /** The guest paging mode. */
2558 PGMMODE enmGuestMode;
2559
2560 /** The current physical address representing in the guest CR3 register. */
2561 RTGCPHYS GCPhysCR3;
2562
2563 /** @name 32-bit Guest Paging.
2564 * @{ */
2565 /** The guest's page directory, R3 pointer. */
2566 R3PTRTYPE(PX86PD) pGst32BitPdR3;
2567#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
2568 /** The guest's page directory, R0 pointer. */
2569 R0PTRTYPE(PX86PD) pGst32BitPdR0;
2570#endif
2571 /** The guest's page directory, static RC mapping. */
2572 RCPTRTYPE(PX86PD) pGst32BitPdRC;
2573 /** @} */
2574
2575 /** @name PAE Guest Paging.
2576 * @{ */
2577 /** The guest's page directory pointer table, static RC mapping. */
2578 RCPTRTYPE(PX86PDPT) pGstPaePdptRC;
2579 /** The guest's page directory pointer table, R3 pointer. */
2580 R3PTRTYPE(PX86PDPT) pGstPaePdptR3;
2581#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
2582 /** The guest's page directory pointer table, R0 pointer. */
2583 R0PTRTYPE(PX86PDPT) pGstPaePdptR0;
2584#endif
2585
2586 /** The guest's page directories, R3 pointers.
2587 * These are individual pointers and don't have to be adjecent.
2588 * These don't have to be up-to-date - use pgmGstGetPaePD() to access them. */
2589 R3PTRTYPE(PX86PDPAE) apGstPaePDsR3[4];
2590 /** The guest's page directories, R0 pointers.
2591 * Same restrictions as apGstPaePDsR3. */
2592#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
2593 R0PTRTYPE(PX86PDPAE) apGstPaePDsR0[4];
2594#endif
2595 /** The guest's page directories, static GC mapping.
2596 * Unlike the R3/R0 array the first entry can be accessed as a 2048 entry PD.
2597 * These don't have to be up-to-date - use pgmGstGetPaePD() to access them. */
2598 RCPTRTYPE(PX86PDPAE) apGstPaePDsRC[4];
2599 /** The physical addresses of the guest page directories (PAE) pointed to by apGstPagePDsHC/GC. */
2600 RTGCPHYS aGCPhysGstPaePDs[4];
2601 /** The physical addresses of the monitored guest page directories (PAE). */
2602 RTGCPHYS aGCPhysGstPaePDsMonitored[4];
2603 /** @} */
2604
2605 /** @name AMD64 Guest Paging.
2606 * @{ */
2607 /** The guest's page directory pointer table, R3 pointer. */
2608 R3PTRTYPE(PX86PML4) pGstAmd64Pml4R3;
2609#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
2610 /** The guest's page directory pointer table, R0 pointer. */
2611 R0PTRTYPE(PX86PML4) pGstAmd64Pml4R0;
2612#endif
2613 /** @} */
2614
2615 /** Pointer to the page of the current active CR3 - R3 Ptr. */
2616 R3PTRTYPE(PPGMPOOLPAGE) pShwPageCR3R3;
2617 /** Pointer to the page of the current active CR3 - R0 Ptr. */
2618 R0PTRTYPE(PPGMPOOLPAGE) pShwPageCR3R0;
2619 /** Pointer to the page of the current active CR3 - RC Ptr. */
2620 RCPTRTYPE(PPGMPOOLPAGE) pShwPageCR3RC;
2621 /* The shadow page pool index of the user table as specified during allocation; useful for freeing root pages */
2622 uint32_t iShwUser;
2623 /* The index into the user table (shadowed) as specified during allocation; useful for freeing root pages. */
2624 uint32_t iShwUserTable;
2625# if HC_ARCH_BITS == 64
2626 RTRCPTR alignment6; /**< structure size alignment. */
2627# endif
2628 /** @} */
2629
2630 /** @name Function pointers for Shadow paging.
2631 * @{
2632 */
2633 DECLR3CALLBACKMEMBER(int, pfnR3ShwRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
2634 DECLR3CALLBACKMEMBER(int, pfnR3ShwExit,(PVMCPU pVCpu));
2635 DECLR3CALLBACKMEMBER(int, pfnR3ShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
2636 DECLR3CALLBACKMEMBER(int, pfnR3ShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2637
2638 DECLRCCALLBACKMEMBER(int, pfnRCShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
2639 DECLRCCALLBACKMEMBER(int, pfnRCShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2640
2641 DECLR0CALLBACKMEMBER(int, pfnR0ShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
2642 DECLR0CALLBACKMEMBER(int, pfnR0ShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2643
2644 /** @} */
2645
2646 /** @name Function pointers for Guest paging.
2647 * @{
2648 */
2649 DECLR3CALLBACKMEMBER(int, pfnR3GstRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
2650 DECLR3CALLBACKMEMBER(int, pfnR3GstExit,(PVMCPU pVCpu));
2651 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
2652 DECLR3CALLBACKMEMBER(int, pfnR3GstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2653 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
2654 DECLRCCALLBACKMEMBER(int, pfnRCGstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
2655 DECLRCCALLBACKMEMBER(int, pfnRCGstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2656 DECLRCCALLBACKMEMBER(int, pfnRCGstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
2657#if HC_ARCH_BITS == 64
2658 RTRCPTR alignment3; /**< structure size alignment. */
2659#endif
2660
2661 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
2662 DECLR0CALLBACKMEMBER(int, pfnR0GstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
2663 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
2664 /** @} */
2665
2666 /** @name Function pointers for Both Shadow and Guest paging.
2667 * @{
2668 */
2669 DECLR3CALLBACKMEMBER(int, pfnR3BthRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
2670 /* no pfnR3BthTrap0eHandler */
2671 DECLR3CALLBACKMEMBER(int, pfnR3BthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2672 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
2673 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncPage,(PVMCPU pVCpu, X86PDE PdeSrc, RTGCPTR GCPtrPage, unsigned cPages, unsigned uError));
2674 DECLR3CALLBACKMEMBER(int, pfnR3BthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2675 DECLR3CALLBACKMEMBER(int, pfnR3BthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
2676 DECLR3CALLBACKMEMBER(unsigned, pfnR3BthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
2677 DECLR3CALLBACKMEMBER(int, pfnR3BthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
2678 DECLR3CALLBACKMEMBER(int, pfnR3BthUnmapCR3,(PVMCPU pVCpu));
2679
2680 DECLR0CALLBACKMEMBER(int, pfnR0BthTrap0eHandler,(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
2681 DECLR0CALLBACKMEMBER(int, pfnR0BthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2682 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
2683 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncPage,(PVMCPU pVCpu, X86PDE PdeSrc, RTGCPTR GCPtrPage, unsigned cPages, unsigned uError));
2684 DECLR0CALLBACKMEMBER(int, pfnR0BthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2685 DECLR0CALLBACKMEMBER(int, pfnR0BthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
2686 DECLR0CALLBACKMEMBER(unsigned, pfnR0BthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
2687 DECLR0CALLBACKMEMBER(int, pfnR0BthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
2688 DECLR0CALLBACKMEMBER(int, pfnR0BthUnmapCR3,(PVMCPU pVCpu));
2689
2690 DECLRCCALLBACKMEMBER(int, pfnRCBthTrap0eHandler,(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
2691 DECLRCCALLBACKMEMBER(int, pfnRCBthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2692 DECLRCCALLBACKMEMBER(int, pfnRCBthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
2693 DECLRCCALLBACKMEMBER(int, pfnRCBthSyncPage,(PVMCPU pVCpu, X86PDE PdeSrc, RTGCPTR GCPtrPage, unsigned cPages, unsigned uError));
2694 DECLRCCALLBACKMEMBER(int, pfnRCBthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
2695 DECLRCCALLBACKMEMBER(int, pfnRCBthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
2696 DECLRCCALLBACKMEMBER(unsigned, pfnRCBthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
2697 DECLRCCALLBACKMEMBER(int, pfnRCBthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
2698 DECLRCCALLBACKMEMBER(int, pfnRCBthUnmapCR3,(PVMCPU pVCpu));
2699#if HC_ARCH_BITS == 64
2700 RTRCPTR alignment2; /**< structure size alignment. */
2701#endif
2702 /** @} */
2703
2704 /** @name Release Statistics
2705 * @{ */
2706 /** The number of times the guest has switched mode since last reset or statistics reset. */
2707 STAMCOUNTER cGuestModeChanges;
2708 /** @} */
2709
2710#ifdef VBOX_WITH_STATISTICS /** @todo move this chunk to the heap. */
2711 /** @name Statistics
2712 * @{ */
2713 /** RC: Which statistic this \#PF should be attributed to. */
2714 RCPTRTYPE(PSTAMPROFILE) pStatTrap0eAttributionRC;
2715 RTRCPTR padding0;
2716 /** R0: Which statistic this \#PF should be attributed to. */
2717 R0PTRTYPE(PSTAMPROFILE) pStatTrap0eAttributionR0;
2718 RTR0PTR padding1;
2719
2720 /* Common */
2721 STAMCOUNTER StatSyncPtPD[X86_PG_ENTRIES]; /**< SyncPT - PD distribution. */
2722 STAMCOUNTER StatSyncPagePD[X86_PG_ENTRIES]; /**< SyncPage - PD distribution. */
2723
2724 /* R0 only: */
2725 STAMCOUNTER StatR0DynMapMigrateInvlPg; /**< R0: invlpg in PGMDynMapMigrateAutoSet. */
2726 STAMPROFILE StatR0DynMapGCPageInl; /**< R0: Calls to pgmR0DynMapGCPageInlined. */
2727 STAMCOUNTER StatR0DynMapGCPageInlHits; /**< R0: Hash table lookup hits. */
2728 STAMCOUNTER StatR0DynMapGCPageInlMisses; /**< R0: Misses that falls back to code common with PGMDynMapHCPage. */
2729 STAMCOUNTER StatR0DynMapGCPageInlRamHits; /**< R0: 1st ram range hits. */
2730 STAMCOUNTER StatR0DynMapGCPageInlRamMisses; /**< R0: 1st ram range misses, takes slow path. */
2731 STAMPROFILE StatR0DynMapHCPageInl; /**< R0: Calls to pgmR0DynMapHCPageInlined. */
2732 STAMCOUNTER StatR0DynMapHCPageInlHits; /**< R0: Hash table lookup hits. */
2733 STAMCOUNTER StatR0DynMapHCPageInlMisses; /**< R0: Misses that falls back to code common with PGMDynMapHCPage. */
2734 STAMPROFILE StatR0DynMapHCPage; /**< R0: Calls to PGMDynMapHCPage. */
2735 STAMCOUNTER StatR0DynMapSetOptimize; /**< R0: Calls to pgmDynMapOptimizeAutoSet. */
2736 STAMCOUNTER StatR0DynMapSetSearchFlushes; /**< R0: Set search restorting to subset flushes. */
2737 STAMCOUNTER StatR0DynMapSetSearchHits; /**< R0: Set search hits. */
2738 STAMCOUNTER StatR0DynMapSetSearchMisses; /**< R0: Set search misses. */
2739 STAMCOUNTER StatR0DynMapPage; /**< R0: Calls to pgmR0DynMapPage. */
2740 STAMCOUNTER StatR0DynMapPageHits0; /**< R0: Hits at iPage+0. */
2741 STAMCOUNTER StatR0DynMapPageHits1; /**< R0: Hits at iPage+1. */
2742 STAMCOUNTER StatR0DynMapPageHits2; /**< R0: Hits at iPage+2. */
2743 STAMCOUNTER StatR0DynMapPageInvlPg; /**< R0: invlpg. */
2744 STAMCOUNTER StatR0DynMapPageSlow; /**< R0: Calls to pgmR0DynMapPageSlow. */
2745 STAMCOUNTER StatR0DynMapPageSlowLoopHits; /**< R0: Hits in the pgmR0DynMapPageSlow search loop. */
2746 STAMCOUNTER StatR0DynMapPageSlowLoopMisses; /**< R0: Misses in the pgmR0DynMapPageSlow search loop. */
2747 //STAMCOUNTER StatR0DynMapPageSlowLostHits; /**< R0: Lost hits. */
2748 STAMCOUNTER StatR0DynMapSubsets; /**< R0: Times PGMDynMapPushAutoSubset was called. */
2749 STAMCOUNTER StatR0DynMapPopFlushes; /**< R0: Times PGMDynMapPopAutoSubset flushes the subset. */
2750 STAMCOUNTER aStatR0DynMapSetSize[11]; /**< R0: Set size distribution. */
2751
2752 /* RZ only: */
2753 STAMPROFILE StatRZTrap0e; /**< RC/R0: PGMTrap0eHandler() profiling. */
2754 STAMPROFILE StatRZTrap0eTimeCheckPageFault;
2755 STAMPROFILE StatRZTrap0eTimeSyncPT;
2756 STAMPROFILE StatRZTrap0eTimeMapping;
2757 STAMPROFILE StatRZTrap0eTimeOutOfSync;
2758 STAMPROFILE StatRZTrap0eTimeHandlers;
2759 STAMPROFILE StatRZTrap0eTime2CSAM; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is CSAM. */
2760 STAMPROFILE StatRZTrap0eTime2DirtyAndAccessed; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is dirty and/or accessed bit emulation. */
2761 STAMPROFILE StatRZTrap0eTime2GuestTrap; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a guest trap. */
2762 STAMPROFILE StatRZTrap0eTime2HndPhys; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a physical handler. */
2763 STAMPROFILE StatRZTrap0eTime2HndVirt; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a virtual handler. */
2764 STAMPROFILE StatRZTrap0eTime2HndUnhandled; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is access outside the monitored areas of a monitored page. */
2765 STAMPROFILE StatRZTrap0eTime2Misc; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is not known. */
2766 STAMPROFILE StatRZTrap0eTime2OutOfSync; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync page. */
2767 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndPhys; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync physical handler page. */
2768 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndVirt; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync virtual handler page. */
2769 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndObs; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an obsolete handler page. */
2770 STAMPROFILE StatRZTrap0eTime2SyncPT; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is lazy syncing of a PT. */
2771 STAMCOUNTER StatRZTrap0eConflicts; /**< RC/R0: The number of times \#PF was caused by an undetected conflict. */
2772 STAMCOUNTER StatRZTrap0eHandlersMapping; /**< RC/R0: Number of traps due to access handlers in mappings. */
2773 STAMCOUNTER StatRZTrap0eHandlersOutOfSync; /**< RC/R0: Number of out-of-sync handled pages. */
2774 STAMCOUNTER StatRZTrap0eHandlersPhysical; /**< RC/R0: Number of traps due to physical access handlers. */
2775 STAMCOUNTER StatRZTrap0eHandlersVirtual; /**< RC/R0: Number of traps due to virtual access handlers. */
2776 STAMCOUNTER StatRZTrap0eHandlersVirtualByPhys; /**< RC/R0: Number of traps due to virtual access handlers found by physical address. */
2777 STAMCOUNTER StatRZTrap0eHandlersVirtualUnmarked;/**< RC/R0: Number of traps due to virtual access handlers found by virtual address (without proper physical flags). */
2778 STAMCOUNTER StatRZTrap0eHandlersUnhandled; /**< RC/R0: Number of traps due to access outside range of monitored page(s). */
2779 STAMCOUNTER StatRZTrap0eHandlersInvalid; /**< RC/R0: Number of traps due to access to invalid physical memory. */
2780 STAMCOUNTER StatRZTrap0eUSNotPresentRead; /**< RC/R0: #PF err kind */
2781 STAMCOUNTER StatRZTrap0eUSNotPresentWrite; /**< RC/R0: #PF err kind */
2782 STAMCOUNTER StatRZTrap0eUSWrite; /**< RC/R0: #PF err kind */
2783 STAMCOUNTER StatRZTrap0eUSReserved; /**< RC/R0: #PF err kind */
2784 STAMCOUNTER StatRZTrap0eUSNXE; /**< RC/R0: #PF err kind */
2785 STAMCOUNTER StatRZTrap0eUSRead; /**< RC/R0: #PF err kind */
2786 STAMCOUNTER StatRZTrap0eSVNotPresentRead; /**< RC/R0: #PF err kind */
2787 STAMCOUNTER StatRZTrap0eSVNotPresentWrite; /**< RC/R0: #PF err kind */
2788 STAMCOUNTER StatRZTrap0eSVWrite; /**< RC/R0: #PF err kind */
2789 STAMCOUNTER StatRZTrap0eSVReserved; /**< RC/R0: #PF err kind */
2790 STAMCOUNTER StatRZTrap0eSNXE; /**< RC/R0: #PF err kind */
2791 STAMCOUNTER StatRZTrap0eGuestPF; /**< RC/R0: Real guest #PFs. */
2792 STAMCOUNTER StatRZTrap0eGuestPFUnh; /**< RC/R0: Real guest #PF ending up at the end of the #PF code. */
2793 STAMCOUNTER StatRZTrap0eGuestPFMapping; /**< RC/R0: Real guest #PF to HMA or other mapping. */
2794 STAMCOUNTER StatRZTrap0eWPEmulInRZ; /**< RC/R0: WP=0 virtualization trap, handled. */
2795 STAMCOUNTER StatRZTrap0eWPEmulToR3; /**< RC/R0: WP=0 virtualization trap, chickened out. */
2796 STAMCOUNTER StatRZTrap0ePD[X86_PG_ENTRIES]; /**< RC/R0: PD distribution of the #PFs. */
2797 STAMCOUNTER StatRZGuestCR3WriteHandled; /**< RC/R0: The number of times WriteHandlerCR3() was successfully called. */
2798 STAMCOUNTER StatRZGuestCR3WriteUnhandled; /**< RC/R0: The number of times WriteHandlerCR3() was called and we had to fall back to the recompiler. */
2799 STAMCOUNTER StatRZGuestCR3WriteConflict; /**< RC/R0: The number of times WriteHandlerCR3() was called and a conflict was detected. */
2800 STAMCOUNTER StatRZGuestROMWriteHandled; /**< RC/R0: The number of times pgmPhysRomWriteHandler() was successfully called. */
2801 STAMCOUNTER StatRZGuestROMWriteUnhandled; /**< RC/R0: The number of times pgmPhysRomWriteHandler() was called and we had to fall back to the recompiler */
2802
2803 /* HC - R3 and (maybe) R0: */
2804
2805 /* RZ & R3: */
2806 STAMPROFILE StatRZSyncCR3; /**< RC/R0: PGMSyncCR3() profiling. */
2807 STAMPROFILE StatRZSyncCR3Handlers; /**< RC/R0: Profiling of the PGMSyncCR3() update handler section. */
2808 STAMCOUNTER StatRZSyncCR3Global; /**< RC/R0: The number of global CR3 syncs. */
2809 STAMCOUNTER StatRZSyncCR3NotGlobal; /**< RC/R0: The number of non-global CR3 syncs. */
2810 STAMCOUNTER StatRZSyncCR3DstCacheHit; /**< RC/R0: The number of times we got some kind of cache hit on a page table. */
2811 STAMCOUNTER StatRZSyncCR3DstFreed; /**< RC/R0: The number of times we've had to free a shadow entry. */
2812 STAMCOUNTER StatRZSyncCR3DstFreedSrcNP; /**< RC/R0: The number of times we've had to free a shadow entry for which the source entry was not present. */
2813 STAMCOUNTER StatRZSyncCR3DstNotPresent; /**< RC/R0: The number of times we've encountered a not present shadow entry for a present guest entry. */
2814 STAMCOUNTER StatRZSyncCR3DstSkippedGlobalPD; /**< RC/R0: The number of times a global page directory wasn't flushed. */
2815 STAMCOUNTER StatRZSyncCR3DstSkippedGlobalPT; /**< RC/R0: The number of times a page table with only global entries wasn't flushed. */
2816 STAMPROFILE StatRZSyncPT; /**< RC/R0: PGMSyncPT() profiling. */
2817 STAMCOUNTER StatRZSyncPTFailed; /**< RC/R0: The number of times PGMSyncPT() failed. */
2818 STAMCOUNTER StatRZSyncPT4K; /**< RC/R0: Number of 4KB syncs. */
2819 STAMCOUNTER StatRZSyncPT4M; /**< RC/R0: Number of 4MB syncs. */
2820 STAMCOUNTER StatRZSyncPagePDNAs; /**< RC/R0: The number of time we've marked a PD not present from SyncPage to virtualize the accessed bit. */
2821 STAMCOUNTER StatRZSyncPagePDOutOfSync; /**< RC/R0: The number of time we've encountered an out-of-sync PD in SyncPage. */
2822 STAMCOUNTER StatRZAccessedPage; /**< RC/R0: The number of pages marked not present for accessed bit emulation. */
2823 STAMPROFILE StatRZDirtyBitTracking; /**< RC/R0: Profiling the dirty bit tracking in CheckPageFault().. */
2824 STAMCOUNTER StatRZDirtyPage; /**< RC/R0: The number of pages marked read-only for dirty bit tracking. */
2825 STAMCOUNTER StatRZDirtyPageBig; /**< RC/R0: The number of pages marked read-only for dirty bit tracking. */
2826 STAMCOUNTER StatRZDirtyPageSkipped; /**< RC/R0: The number of pages already dirty or readonly. */
2827 STAMCOUNTER StatRZDirtyPageTrap; /**< RC/R0: The number of traps generated for dirty bit tracking. */
2828 STAMCOUNTER StatRZDirtyPageStale; /**< RC/R0: The number of traps generated for dirty bit tracking. (stale tlb entries) */
2829 STAMCOUNTER StatRZDirtyTrackRealPF; /**< RC/R0: The number of real pages faults during dirty bit tracking. */
2830 STAMCOUNTER StatRZDirtiedPage; /**< RC/R0: The number of pages marked dirty because of write accesses. */
2831 STAMCOUNTER StatRZPageAlreadyDirty; /**< RC/R0: The number of pages already marked dirty because of write accesses. */
2832 STAMPROFILE StatRZInvalidatePage; /**< RC/R0: PGMInvalidatePage() profiling. */
2833 STAMCOUNTER StatRZInvalidatePage4KBPages; /**< RC/R0: The number of times PGMInvalidatePage() was called for a 4KB page. */
2834 STAMCOUNTER StatRZInvalidatePage4MBPages; /**< RC/R0: The number of times PGMInvalidatePage() was called for a 4MB page. */
2835 STAMCOUNTER StatRZInvalidatePage4MBPagesSkip; /**< RC/R0: The number of times PGMInvalidatePage() skipped a 4MB page. */
2836 STAMCOUNTER StatRZInvalidatePagePDMappings; /**< RC/R0: The number of times PGMInvalidatePage() was called for a page directory containing mappings (no conflict). */
2837 STAMCOUNTER StatRZInvalidatePagePDNAs; /**< RC/R0: The number of times PGMInvalidatePage() was called for a not accessed page directory. */
2838 STAMCOUNTER StatRZInvalidatePagePDNPs; /**< RC/R0: The number of times PGMInvalidatePage() was called for a not present page directory. */
2839 STAMCOUNTER StatRZInvalidatePagePDOutOfSync; /**< RC/R0: The number of times PGMInvalidatePage() was called for an out of sync page directory. */
2840 STAMCOUNTER StatRZInvalidatePageSkipped; /**< RC/R0: The number of times PGMInvalidatePage() was skipped due to not present shw or pending pending SyncCR3. */
2841 STAMCOUNTER StatRZPageOutOfSyncUser; /**< RC/R0: The number of times user page is out of sync was detected in #PF or VerifyAccessSyncPage. */
2842 STAMCOUNTER StatRZPageOutOfSyncSupervisor; /**< RC/R0: The number of times supervisor page is out of sync was detected in in #PF or VerifyAccessSyncPage. */
2843 STAMPROFILE StatRZPrefetch; /**< RC/R0: PGMPrefetchPage. */
2844 STAMPROFILE StatRZFlushTLB; /**< RC/R0: Profiling of the PGMFlushTLB() body. */
2845 STAMCOUNTER StatRZFlushTLBNewCR3; /**< RC/R0: The number of times PGMFlushTLB was called with a new CR3, non-global. (switch) */
2846 STAMCOUNTER StatRZFlushTLBNewCR3Global; /**< RC/R0: The number of times PGMFlushTLB was called with a new CR3, global. (switch) */
2847 STAMCOUNTER StatRZFlushTLBSameCR3; /**< RC/R0: The number of times PGMFlushTLB was called with the same CR3, non-global. (flush) */
2848 STAMCOUNTER StatRZFlushTLBSameCR3Global; /**< RC/R0: The number of times PGMFlushTLB was called with the same CR3, global. (flush) */
2849 STAMPROFILE StatRZGstModifyPage; /**< RC/R0: Profiling of the PGMGstModifyPage() body */
2850
2851 STAMPROFILE StatR3SyncCR3; /**< R3: PGMSyncCR3() profiling. */
2852 STAMPROFILE StatR3SyncCR3Handlers; /**< R3: Profiling of the PGMSyncCR3() update handler section. */
2853 STAMCOUNTER StatR3SyncCR3Global; /**< R3: The number of global CR3 syncs. */
2854 STAMCOUNTER StatR3SyncCR3NotGlobal; /**< R3: The number of non-global CR3 syncs. */
2855 STAMCOUNTER StatR3SyncCR3DstFreed; /**< R3: The number of times we've had to free a shadow entry. */
2856 STAMCOUNTER StatR3SyncCR3DstFreedSrcNP; /**< R3: The number of times we've had to free a shadow entry for which the source entry was not present. */
2857 STAMCOUNTER StatR3SyncCR3DstNotPresent; /**< R3: The number of times we've encountered a not present shadow entry for a present guest entry. */
2858 STAMCOUNTER StatR3SyncCR3DstSkippedGlobalPD; /**< R3: The number of times a global page directory wasn't flushed. */
2859 STAMCOUNTER StatR3SyncCR3DstSkippedGlobalPT; /**< R3: The number of times a page table with only global entries wasn't flushed. */
2860 STAMCOUNTER StatR3SyncCR3DstCacheHit; /**< R3: The number of times we got some kind of cache hit on a page table. */
2861 STAMPROFILE StatR3SyncPT; /**< R3: PGMSyncPT() profiling. */
2862 STAMCOUNTER StatR3SyncPTFailed; /**< R3: The number of times PGMSyncPT() failed. */
2863 STAMCOUNTER StatR3SyncPT4K; /**< R3: Number of 4KB syncs. */
2864 STAMCOUNTER StatR3SyncPT4M; /**< R3: Number of 4MB syncs. */
2865 STAMCOUNTER StatR3SyncPagePDNAs; /**< R3: The number of time we've marked a PD not present from SyncPage to virtualize the accessed bit. */
2866 STAMCOUNTER StatR3SyncPagePDOutOfSync; /**< R3: The number of time we've encountered an out-of-sync PD in SyncPage. */
2867 STAMCOUNTER StatR3AccessedPage; /**< R3: The number of pages marked not present for accessed bit emulation. */
2868 STAMPROFILE StatR3DirtyBitTracking; /**< R3: Profiling the dirty bit tracking in CheckPageFault(). */
2869 STAMCOUNTER StatR3DirtyPage; /**< R3: The number of pages marked read-only for dirty bit tracking. */
2870 STAMCOUNTER StatR3DirtyPageBig; /**< R3: The number of pages marked read-only for dirty bit tracking. */
2871 STAMCOUNTER StatR3DirtyPageSkipped; /**< R3: The number of pages already dirty or readonly. */
2872 STAMCOUNTER StatR3DirtyPageTrap; /**< R3: The number of traps generated for dirty bit tracking. */
2873 STAMCOUNTER StatR3DirtyTrackRealPF; /**< R3: The number of real pages faults during dirty bit tracking. */
2874 STAMCOUNTER StatR3DirtiedPage; /**< R3: The number of pages marked dirty because of write accesses. */
2875 STAMCOUNTER StatR3PageAlreadyDirty; /**< R3: The number of pages already marked dirty because of write accesses. */
2876 STAMPROFILE StatR3InvalidatePage; /**< R3: PGMInvalidatePage() profiling. */
2877 STAMCOUNTER StatR3InvalidatePage4KBPages; /**< R3: The number of times PGMInvalidatePage() was called for a 4KB page. */
2878 STAMCOUNTER StatR3InvalidatePage4MBPages; /**< R3: The number of times PGMInvalidatePage() was called for a 4MB page. */
2879 STAMCOUNTER StatR3InvalidatePage4MBPagesSkip; /**< R3: The number of times PGMInvalidatePage() skipped a 4MB page. */
2880 STAMCOUNTER StatR3InvalidatePagePDNAs; /**< R3: The number of times PGMInvalidatePage() was called for a not accessed page directory. */
2881 STAMCOUNTER StatR3InvalidatePagePDNPs; /**< R3: The number of times PGMInvalidatePage() was called for a not present page directory. */
2882 STAMCOUNTER StatR3InvalidatePagePDMappings; /**< R3: The number of times PGMInvalidatePage() was called for a page directory containing mappings (no conflict). */
2883 STAMCOUNTER StatR3InvalidatePagePDOutOfSync; /**< R3: The number of times PGMInvalidatePage() was called for an out of sync page directory. */
2884 STAMCOUNTER StatR3InvalidatePageSkipped; /**< R3: The number of times PGMInvalidatePage() was skipped due to not present shw or pending pending SyncCR3. */
2885 STAMCOUNTER StatR3PageOutOfSyncUser; /**< R3: The number of times user page is out of sync was detected in #PF or VerifyAccessSyncPage. */
2886 STAMCOUNTER StatR3PageOutOfSyncSupervisor; /**< R3: The number of times supervisor page is out of sync was detected in in #PF or VerifyAccessSyncPage. */
2887 STAMPROFILE StatR3Prefetch; /**< R3: PGMPrefetchPage. */
2888 STAMPROFILE StatR3FlushTLB; /**< R3: Profiling of the PGMFlushTLB() body. */
2889 STAMCOUNTER StatR3FlushTLBNewCR3; /**< R3: The number of times PGMFlushTLB was called with a new CR3, non-global. (switch) */
2890 STAMCOUNTER StatR3FlushTLBNewCR3Global; /**< R3: The number of times PGMFlushTLB was called with a new CR3, global. (switch) */
2891 STAMCOUNTER StatR3FlushTLBSameCR3; /**< R3: The number of times PGMFlushTLB was called with the same CR3, non-global. (flush) */
2892 STAMCOUNTER StatR3FlushTLBSameCR3Global; /**< R3: The number of times PGMFlushTLB was called with the same CR3, global. (flush) */
2893 STAMPROFILE StatR3GstModifyPage; /**< R3: Profiling of the PGMGstModifyPage() body */
2894 /** @} */
2895#endif /* VBOX_WITH_STATISTICS */
2896} PGMCPU;
2897/** Pointer to the per-cpu PGM data. */
2898typedef PGMCPU *PPGMCPU;
2899
2900
2901/** @name PGM::fSyncFlags Flags
2902 * @{
2903 */
2904/** Updates the virtual access handler state bit in PGMPAGE. */
2905#define PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL RT_BIT(0)
2906/** Always sync CR3. */
2907#define PGM_SYNC_ALWAYS RT_BIT(1)
2908/** Check monitoring on next CR3 (re)load and invalidate page.
2909 * @todo This is obsolete now. Remove after 2.2.0 is branched off. */
2910#define PGM_SYNC_MONITOR_CR3 RT_BIT(2)
2911/** Check guest mapping in SyncCR3. */
2912#define PGM_SYNC_MAP_CR3 RT_BIT(3)
2913/** Clear the page pool (a light weight flush). */
2914#define PGM_SYNC_CLEAR_PGM_POOL_BIT 8
2915#define PGM_SYNC_CLEAR_PGM_POOL RT_BIT(PGM_SYNC_CLEAR_PGM_POOL_BIT)
2916/** @} */
2917
2918
2919RT_BEGIN_DECLS
2920
2921int pgmLock(PVM pVM);
2922void pgmUnlock(PVM pVM);
2923
2924int pgmR3SyncPTResolveConflict(PVM pVM, PPGMMAPPING pMapping, PX86PD pPDSrc, RTGCPTR GCPtrOldMapping);
2925int pgmR3SyncPTResolveConflictPAE(PVM pVM, PPGMMAPPING pMapping, RTGCPTR GCPtrOldMapping);
2926PPGMMAPPING pgmGetMapping(PVM pVM, RTGCPTR GCPtr);
2927void pgmR3MapRelocate(PVM pVM, PPGMMAPPING pMapping, RTGCPTR GCPtrOldMapping, RTGCPTR GCPtrNewMapping);
2928DECLCALLBACK(void) pgmR3MapInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
2929
2930void pgmR3HandlerPhysicalUpdateAll(PVM pVM);
2931bool pgmHandlerPhysicalIsAll(PVM pVM, RTGCPHYS GCPhys);
2932void pgmHandlerPhysicalResetAliasedPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhysPage);
2933int pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage);
2934DECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser);
2935#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
2936void pgmHandlerVirtualDumpPhysPages(PVM pVM);
2937#else
2938# define pgmHandlerVirtualDumpPhysPages(a) do { } while (0)
2939#endif
2940DECLCALLBACK(void) pgmR3InfoHandlers(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
2941
2942
2943int pgmPhysAllocPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2944int pgmPhysPageLoadIntoTlb(PPGM pPGM, RTGCPHYS GCPhys);
2945int pgmPhysPageLoadIntoTlbWithPage(PPGM pPGM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2946int pgmPhysPageMakeWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2947int pgmPhysPageMakeWritableUnlocked(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2948int pgmPhysPageMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAP ppMap, void **ppv);
2949int pgmPhysPageMapByPageID(PVM pVM, uint32_t idPage, RTHCPHYS HCPhys, void **ppv);
2950int pgmPhysGCPhys2CCPtrInternal(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv);
2951int pgmPhysGCPhys2CCPtrInternalReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, const void **ppv);
2952VMMDECL(int) pgmPhysRomWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
2953#ifdef IN_RING3
2954void pgmR3PhysRelinkRamRanges(PVM pVM);
2955int pgmR3PhysRamPreAllocate(PVM pVM);
2956int pgmR3PhysRamReset(PVM pVM);
2957int pgmR3PhysRomReset(PVM pVM);
2958int pgmR3PhysChunkMap(PVM pVM, uint32_t idChunk, PPPGMCHUNKR3MAP ppChunk);
2959
2960int pgmR3PoolInit(PVM pVM);
2961void pgmR3PoolRelocate(PVM pVM);
2962void pgmR3PoolReset(PVM pVM);
2963
2964#endif /* IN_RING3 */
2965#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2966int pgmR0DynMapHCPageCommon(PVM pVM, PPGMMAPSET pSet, RTHCPHYS HCPhys, void **ppv);
2967#endif
2968int pgmPoolAllocEx(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, PGMPOOLACCESS enmAccess, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage);
2969
2970DECLINLINE(int) pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
2971{
2972 return pgmPoolAllocEx(pVM, GCPhys, enmKind, PGMPOOLACCESS_DONTCARE, iUser, iUserTable, ppPage);
2973}
2974
2975void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable);
2976void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
2977int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
2978void pgmPoolClearAll(PVM pVM);
2979PPGMPOOLPAGE pgmPoolGetPage(PPGMPOOL pPool, RTHCPHYS HCPhys);
2980int pgmPoolSyncCR3(PVMCPU pVCpu);
2981int pgmPoolTrackFlushGCPhys(PVM pVM, PPGMPAGE pPhysPage, bool *pfFlushTLBs);
2982uint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT);
2983void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPoolPage, PPGMPAGE pPhysPage);
2984#ifdef PGMPOOL_WITH_MONITORING
2985void pgmPoolMonitorChainChanging(PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvAddress, PDISCPUSTATE pCpu);
2986int pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
2987void pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
2988#endif
2989
2990int pgmR3ExitShadowModeBeforePoolFlush(PVM pVM, PVMCPU pVCpu);
2991int pgmR3ReEnterShadowModeAfterPoolFlush(PVM pVM, PVMCPU pVCpu);
2992
2993void pgmMapSetShadowPDEs(PVM pVM, PPGMMAPPING pMap, unsigned iNewPDE);
2994void pgmMapClearShadowPDEs(PVM pVM, PPGMPOOLPAGE pShwPageCR3, PPGMMAPPING pMap, unsigned iOldPDE, bool fDeactivateCR3);
2995int pgmMapActivateCR3(PVM pVM, PPGMPOOLPAGE pShwPageCR3);
2996int pgmMapDeactivateCR3(PVM pVM, PPGMPOOLPAGE pShwPageCR3);
2997
2998int pgmShwSyncPaePDPtr(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDPE pGstPdpe, PX86PDPAE *ppPD);
2999#ifndef IN_RC
3000int pgmShwSyncLongModePDPtr(PVMCPU pVCpu, RTGCPTR64 GCPtr, PX86PML4E pGstPml4e, PX86PDPE pGstPdpe, PX86PDPAE *ppPD);
3001#endif
3002int pgmShwGetEPTPDPtr(PVMCPU pVCpu, RTGCPTR64 GCPtr, PEPTPDPT *ppPdpt, PEPTPD *ppPD);
3003
3004PX86PD pgmGstLazyMap32BitPD(PPGMCPU pPGM);
3005PX86PDPT pgmGstLazyMapPaePDPT(PPGMCPU pPGM);
3006PX86PDPAE pgmGstLazyMapPaePD(PPGMCPU pPGM, uint32_t iPdpt);
3007PX86PML4 pgmGstLazyMapPml4(PPGMCPU pPGM);
3008
3009RT_END_DECLS
3010
3011
3012/**
3013 * Gets the PGMRAMRANGE structure for a guest page.
3014 *
3015 * @returns Pointer to the RAM range on success.
3016 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3017 *
3018 * @param pPGM PGM handle.
3019 * @param GCPhys The GC physical address.
3020 */
3021DECLINLINE(PPGMRAMRANGE) pgmPhysGetRange(PPGM pPGM, RTGCPHYS GCPhys)
3022{
3023 /*
3024 * Optimize for the first range.
3025 */
3026 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3027 RTGCPHYS off = GCPhys - pRam->GCPhys;
3028 if (RT_UNLIKELY(off >= pRam->cb))
3029 {
3030 do
3031 {
3032 pRam = pRam->CTX_SUFF(pNext);
3033 if (RT_UNLIKELY(!pRam))
3034 break;
3035 off = GCPhys - pRam->GCPhys;
3036 } while (off >= pRam->cb);
3037 }
3038 return pRam;
3039}
3040
3041
3042/**
3043 * Gets the PGMPAGE structure for a guest page.
3044 *
3045 * @returns Pointer to the page on success.
3046 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3047 *
3048 * @param pPGM PGM handle.
3049 * @param GCPhys The GC physical address.
3050 */
3051DECLINLINE(PPGMPAGE) pgmPhysGetPage(PPGM pPGM, RTGCPHYS GCPhys)
3052{
3053 /*
3054 * Optimize for the first range.
3055 */
3056 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3057 RTGCPHYS off = GCPhys - pRam->GCPhys;
3058 if (RT_UNLIKELY(off >= pRam->cb))
3059 {
3060 do
3061 {
3062 pRam = pRam->CTX_SUFF(pNext);
3063 if (RT_UNLIKELY(!pRam))
3064 return NULL;
3065 off = GCPhys - pRam->GCPhys;
3066 } while (off >= pRam->cb);
3067 }
3068 return &pRam->aPages[off >> PAGE_SHIFT];
3069}
3070
3071
3072/**
3073 * Gets the PGMPAGE structure for a guest page.
3074 *
3075 * Old Phys code: Will make sure the page is present.
3076 *
3077 * @returns VBox status code.
3078 * @retval VINF_SUCCESS and a valid *ppPage on success.
3079 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if the address isn't valid.
3080 *
3081 * @param pPGM PGM handle.
3082 * @param GCPhys The GC physical address.
3083 * @param ppPage Where to store the page poitner on success.
3084 */
3085DECLINLINE(int) pgmPhysGetPageEx(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGE ppPage)
3086{
3087 /*
3088 * Optimize for the first range.
3089 */
3090 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3091 RTGCPHYS off = GCPhys - pRam->GCPhys;
3092 if (RT_UNLIKELY(off >= pRam->cb))
3093 {
3094 do
3095 {
3096 pRam = pRam->CTX_SUFF(pNext);
3097 if (RT_UNLIKELY(!pRam))
3098 {
3099 *ppPage = NULL; /* avoid incorrect and very annoying GCC warnings */
3100 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
3101 }
3102 off = GCPhys - pRam->GCPhys;
3103 } while (off >= pRam->cb);
3104 }
3105 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
3106 return VINF_SUCCESS;
3107}
3108
3109
3110
3111
3112/**
3113 * Gets the PGMPAGE structure for a guest page.
3114 *
3115 * Old Phys code: Will make sure the page is present.
3116 *
3117 * @returns VBox status code.
3118 * @retval VINF_SUCCESS and a valid *ppPage on success.
3119 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if the address isn't valid.
3120 *
3121 * @param pPGM PGM handle.
3122 * @param GCPhys The GC physical address.
3123 * @param ppPage Where to store the page poitner on success.
3124 * @param ppRamHint Where to read and store the ram list hint.
3125 * The caller initializes this to NULL before the call.
3126 */
3127DECLINLINE(int) pgmPhysGetPageWithHintEx(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGE ppPage, PPGMRAMRANGE *ppRamHint)
3128{
3129 RTGCPHYS off;
3130 PPGMRAMRANGE pRam = *ppRamHint;
3131 if ( !pRam
3132 || RT_UNLIKELY((off = GCPhys - pRam->GCPhys) >= pRam->cb))
3133 {
3134 pRam = pPGM->CTX_SUFF(pRamRanges);
3135 off = GCPhys - pRam->GCPhys;
3136 if (RT_UNLIKELY(off >= pRam->cb))
3137 {
3138 do
3139 {
3140 pRam = pRam->CTX_SUFF(pNext);
3141 if (RT_UNLIKELY(!pRam))
3142 {
3143 *ppPage = NULL; /* Kill the incorrect and extremely annoying GCC warnings. */
3144 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
3145 }
3146 off = GCPhys - pRam->GCPhys;
3147 } while (off >= pRam->cb);
3148 }
3149 *ppRamHint = pRam;
3150 }
3151 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
3152 return VINF_SUCCESS;
3153}
3154
3155
3156/**
3157 * Gets the PGMPAGE structure for a guest page together with the PGMRAMRANGE.
3158 *
3159 * @returns Pointer to the page on success.
3160 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3161 *
3162 * @param pPGM PGM handle.
3163 * @param GCPhys The GC physical address.
3164 * @param ppRam Where to store the pointer to the PGMRAMRANGE.
3165 */
3166DECLINLINE(PPGMPAGE) pgmPhysGetPageAndRange(PPGM pPGM, RTGCPHYS GCPhys, PPGMRAMRANGE *ppRam)
3167{
3168 /*
3169 * Optimize for the first range.
3170 */
3171 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3172 RTGCPHYS off = GCPhys - pRam->GCPhys;
3173 if (RT_UNLIKELY(off >= pRam->cb))
3174 {
3175 do
3176 {
3177 pRam = pRam->CTX_SUFF(pNext);
3178 if (RT_UNLIKELY(!pRam))
3179 return NULL;
3180 off = GCPhys - pRam->GCPhys;
3181 } while (off >= pRam->cb);
3182 }
3183 *ppRam = pRam;
3184 return &pRam->aPages[off >> PAGE_SHIFT];
3185}
3186
3187
3188/**
3189 * Gets the PGMPAGE structure for a guest page together with the PGMRAMRANGE.
3190 *
3191 * @returns Pointer to the page on success.
3192 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3193 *
3194 * @param pPGM PGM handle.
3195 * @param GCPhys The GC physical address.
3196 * @param ppPage Where to store the pointer to the PGMPAGE structure.
3197 * @param ppRam Where to store the pointer to the PGMRAMRANGE structure.
3198 */
3199DECLINLINE(int) pgmPhysGetPageAndRangeEx(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGE ppPage, PPGMRAMRANGE *ppRam)
3200{
3201 /*
3202 * Optimize for the first range.
3203 */
3204 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3205 RTGCPHYS off = GCPhys - pRam->GCPhys;
3206 if (RT_UNLIKELY(off >= pRam->cb))
3207 {
3208 do
3209 {
3210 pRam = pRam->CTX_SUFF(pNext);
3211 if (RT_UNLIKELY(!pRam))
3212 {
3213 *ppRam = NULL; /* Shut up silly GCC warnings. */
3214 *ppPage = NULL; /* ditto */
3215 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
3216 }
3217 off = GCPhys - pRam->GCPhys;
3218 } while (off >= pRam->cb);
3219 }
3220 *ppRam = pRam;
3221 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
3222 return VINF_SUCCESS;
3223}
3224
3225
3226/**
3227 * Convert GC Phys to HC Phys.
3228 *
3229 * @returns VBox status.
3230 * @param pPGM PGM handle.
3231 * @param GCPhys The GC physical address.
3232 * @param pHCPhys Where to store the corresponding HC physical address.
3233 *
3234 * @deprecated Doesn't deal with zero, shared or write monitored pages.
3235 * Avoid when writing new code!
3236 */
3237DECLINLINE(int) pgmRamGCPhys2HCPhys(PPGM pPGM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
3238{
3239 PPGMPAGE pPage;
3240 int rc = pgmPhysGetPageEx(pPGM, GCPhys, &pPage);
3241 if (RT_FAILURE(rc))
3242 return rc;
3243 *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK);
3244 return VINF_SUCCESS;
3245}
3246
3247#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3248
3249/**
3250 * Inlined version of the ring-0 version of PGMDynMapHCPage that
3251 * optimizes access to pages already in the set.
3252 *
3253 * @returns VINF_SUCCESS. Will bail out to ring-3 on failure.
3254 * @param pPGM Pointer to the PVM instance data.
3255 * @param HCPhys The physical address of the page.
3256 * @param ppv Where to store the mapping address.
3257 */
3258DECLINLINE(int) pgmR0DynMapHCPageInlined(PPGM pPGM, RTHCPHYS HCPhys, void **ppv)
3259{
3260 PVM pVM = PGM2VM(pPGM);
3261 PPGMCPU pPGMCPU = (PPGMCPU)((uint8_t *)VMMGetCpu(pVM) + pPGM->offVCpuPGM); /* very pretty ;-) */
3262 PPGMMAPSET pSet = &pPGMCPU->AutoSet;
3263
3264 STAM_PROFILE_START(&pPGMCPU->StatR0DynMapHCPageInl, a);
3265 Assert(!(HCPhys & PAGE_OFFSET_MASK));
3266 Assert(pSet->cEntries <= RT_ELEMENTS(pSet->aEntries));
3267
3268 unsigned iHash = PGMMAPSET_HASH(HCPhys);
3269 unsigned iEntry = pSet->aiHashTable[iHash];
3270 if ( iEntry < pSet->cEntries
3271 && pSet->aEntries[iEntry].HCPhys == HCPhys)
3272 {
3273 *ppv = pSet->aEntries[iEntry].pvPage;
3274 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapHCPageInlHits);
3275 }
3276 else
3277 {
3278 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapHCPageInlMisses);
3279 pgmR0DynMapHCPageCommon(pVM, pSet, HCPhys, ppv);
3280 }
3281
3282 STAM_PROFILE_STOP(&pPGMCPU->StatR0DynMapHCPageInl, a);
3283 return VINF_SUCCESS;
3284}
3285
3286
3287/**
3288 * Inlined version of the ring-0 version of PGMDynMapGCPage that optimizes
3289 * access to pages already in the set.
3290 *
3291 * @returns See PGMDynMapGCPage.
3292 * @param pPGM Pointer to the PVM instance data.
3293 * @param HCPhys The physical address of the page.
3294 * @param ppv Where to store the mapping address.
3295 */
3296DECLINLINE(int) pgmR0DynMapGCPageInlined(PPGM pPGM, RTGCPHYS GCPhys, void **ppv)
3297{
3298 PVM pVM = PGM2VM(pPGM);
3299 PPGMCPU pPGMCPU = (PPGMCPU)((uint8_t *)VMMGetCpu(pVM) + pPGM->offVCpuPGM); /* very pretty ;-) */
3300
3301 STAM_PROFILE_START(&pPGMCPU->StatR0DynMapGCPageInl, a);
3302 Assert(!(GCPhys & PAGE_OFFSET_MASK));
3303
3304 /*
3305 * Get the ram range.
3306 */
3307 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3308 RTGCPHYS off = GCPhys - pRam->GCPhys;
3309 if (RT_UNLIKELY(off >= pRam->cb
3310 /** @todo || page state stuff */))
3311 {
3312 /* This case is not counted into StatR0DynMapGCPageInl. */
3313 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlRamMisses);
3314 return PGMDynMapGCPage(pVM, GCPhys, ppv);
3315 }
3316
3317 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[off >> PAGE_SHIFT]);
3318 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlRamHits);
3319
3320 /*
3321 * pgmR0DynMapHCPageInlined with out stats.
3322 */
3323 PPGMMAPSET pSet = &pPGMCPU->AutoSet;
3324 Assert(!(HCPhys & PAGE_OFFSET_MASK));
3325 Assert(pSet->cEntries <= RT_ELEMENTS(pSet->aEntries));
3326
3327 unsigned iHash = PGMMAPSET_HASH(HCPhys);
3328 unsigned iEntry = pSet->aiHashTable[iHash];
3329 if ( iEntry < pSet->cEntries
3330 && pSet->aEntries[iEntry].HCPhys == HCPhys)
3331 {
3332 *ppv = pSet->aEntries[iEntry].pvPage;
3333 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlHits);
3334 }
3335 else
3336 {
3337 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlMisses);
3338 pgmR0DynMapHCPageCommon(pVM, pSet, HCPhys, ppv);
3339 }
3340
3341 STAM_PROFILE_STOP(&pPGMCPU->StatR0DynMapGCPageInl, a);
3342 return VINF_SUCCESS;
3343}
3344
3345#endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
3346#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
3347
3348/**
3349 * Maps the page into current context (RC and maybe R0).
3350 *
3351 * @returns pointer to the mapping.
3352 * @param pVM Pointer to the PGM instance data.
3353 * @param pPage The page.
3354 */
3355DECLINLINE(void *) pgmPoolMapPageInlined(PPGM pPGM, PPGMPOOLPAGE pPage)
3356{
3357 if (pPage->idx >= PGMPOOL_IDX_FIRST)
3358 {
3359 Assert(pPage->idx < pPGM->CTX_SUFF(pPool)->cCurPages);
3360 void *pv;
3361# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3362 pgmR0DynMapHCPageInlined(pPGM, pPage->Core.Key, &pv);
3363# else
3364 PGMDynMapHCPage(PGM2VM(pPGM), pPage->Core.Key, &pv);
3365# endif
3366 return pv;
3367 }
3368 AssertFatalMsgFailed(("pgmPoolMapPageInlined invalid page index %x\n", pPage->idx));
3369}
3370
3371/**
3372 * Temporarily maps one host page specified by HC physical address, returning
3373 * pointer within the page.
3374 *
3375 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
3376 * reused after 8 mappings (or perhaps a few more if you score with the cache).
3377 *
3378 * @returns The address corresponding to HCPhys.
3379 * @param pPGM Pointer to the PVM instance data.
3380 * @param HCPhys HC Physical address of the page.
3381 */
3382DECLINLINE(void *) pgmDynMapHCPageOff(PPGM pPGM, RTHCPHYS HCPhys)
3383{
3384 void *pv;
3385# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3386 pgmR0DynMapHCPageInlined(pPGM, HCPhys & ~(RTHCPHYS)PAGE_OFFSET_MASK, &pv);
3387# else
3388 PGMDynMapHCPage(PGM2VM(pPGM), HCPhys & ~(RTHCPHYS)PAGE_OFFSET_MASK, &pv);
3389# endif
3390 pv = (void *)((uintptr_t)pv | (HCPhys & PAGE_OFFSET_MASK));
3391 return pv;
3392}
3393
3394#endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 || IN_RC */
3395#ifndef IN_RC
3396
3397/**
3398 * Queries the Physical TLB entry for a physical guest page,
3399 * attempting to load the TLB entry if necessary.
3400 *
3401 * @returns VBox status code.
3402 * @retval VINF_SUCCESS on success
3403 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
3404 *
3405 * @param pPGM The PGM instance handle.
3406 * @param GCPhys The address of the guest page.
3407 * @param ppTlbe Where to store the pointer to the TLB entry.
3408 */
3409DECLINLINE(int) pgmPhysPageQueryTlbe(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGEMAPTLBE ppTlbe)
3410{
3411 int rc;
3412 PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
3413 if (pTlbe->GCPhys == (GCPhys & X86_PTE_PAE_PG_MASK))
3414 {
3415 STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbHits));
3416 rc = VINF_SUCCESS;
3417 }
3418 else
3419 rc = pgmPhysPageLoadIntoTlb(pPGM, GCPhys);
3420 *ppTlbe = pTlbe;
3421 return rc;
3422}
3423
3424
3425/**
3426 * Queries the Physical TLB entry for a physical guest page,
3427 * attempting to load the TLB entry if necessary.
3428 *
3429 * @returns VBox status code.
3430 * @retval VINF_SUCCESS on success
3431 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
3432 *
3433 * @param pPGM The PGM instance handle.
3434 * @param pPage Pointer to the PGMPAGE structure corresponding to
3435 * GCPhys.
3436 * @param GCPhys The address of the guest page.
3437 * @param ppTlbe Where to store the pointer to the TLB entry.
3438 */
3439DECLINLINE(int) pgmPhysPageQueryTlbeWithPage(PPGM pPGM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAPTLBE ppTlbe)
3440{
3441 int rc;
3442 PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
3443 if (pTlbe->GCPhys == (GCPhys & X86_PTE_PAE_PG_MASK))
3444 {
3445 STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbHits));
3446 rc = VINF_SUCCESS;
3447 }
3448 else
3449 rc = pgmPhysPageLoadIntoTlbWithPage(pPGM, pPage, GCPhys);
3450 *ppTlbe = pTlbe;
3451 return rc;
3452}
3453
3454#endif /* !IN_RC */
3455
3456/**
3457 * Calculated the guest physical address of the large (4 MB) page in 32 bits paging mode.
3458 * Takes PSE-36 into account.
3459 *
3460 * @returns guest physical address
3461 * @param pPGM Pointer to the PGM instance data.
3462 * @param Pde Guest Pde
3463 */
3464DECLINLINE(RTGCPHYS) pgmGstGet4MBPhysPage(PPGM pPGM, X86PDE Pde)
3465{
3466 RTGCPHYS GCPhys = Pde.u & X86_PDE4M_PG_MASK;
3467 GCPhys |= (RTGCPHYS)Pde.b.u8PageNoHigh << 32;
3468
3469 return GCPhys & pPGM->GCPhys4MBPSEMask;
3470}
3471
3472
3473/**
3474 * Gets the page directory entry for the specified address (32-bit paging).
3475 *
3476 * @returns The page directory entry in question.
3477 * @param pPGM Pointer to the PGM instance data.
3478 * @param GCPtr The address.
3479 */
3480DECLINLINE(X86PDE) pgmGstGet32bitPDE(PPGMCPU pPGM, RTGCPTR GCPtr)
3481{
3482#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3483 PCX86PD pGuestPD = NULL;
3484 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPD);
3485 if (RT_FAILURE(rc))
3486 {
3487 X86PDE ZeroPde = {0};
3488 AssertMsgFailedReturn(("%Rrc\n", rc), ZeroPde);
3489 }
3490#else
3491 PX86PD pGuestPD = pPGM->CTX_SUFF(pGst32BitPd);
3492# ifdef IN_RING3
3493 if (!pGuestPD)
3494 pGuestPD = pgmGstLazyMap32BitPD(pPGM);
3495# endif
3496#endif
3497 return pGuestPD->a[GCPtr >> X86_PD_SHIFT];
3498}
3499
3500
3501/**
3502 * Gets the address of a specific page directory entry (32-bit paging).
3503 *
3504 * @returns Pointer the page directory entry in question.
3505 * @param pPGM Pointer to the PGM instance data.
3506 * @param GCPtr The address.
3507 */
3508DECLINLINE(PX86PDE) pgmGstGet32bitPDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
3509{
3510#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3511 PX86PD pGuestPD = NULL;
3512 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPD);
3513 AssertRCReturn(rc, NULL);
3514#else
3515 PX86PD pGuestPD = pPGM->CTX_SUFF(pGst32BitPd);
3516# ifdef IN_RING3
3517 if (!pGuestPD)
3518 pGuestPD = pgmGstLazyMap32BitPD(pPGM);
3519# endif
3520#endif
3521 return &pGuestPD->a[GCPtr >> X86_PD_SHIFT];
3522}
3523
3524
3525/**
3526 * Gets the address the guest page directory (32-bit paging).
3527 *
3528 * @returns Pointer the page directory entry in question.
3529 * @param pPGM Pointer to the PGM instance data.
3530 */
3531DECLINLINE(PX86PD) pgmGstGet32bitPDPtr(PPGMCPU pPGM)
3532{
3533#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3534 PX86PD pGuestPD = NULL;
3535 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPD);
3536 AssertRCReturn(rc, NULL);
3537#else
3538 PX86PD pGuestPD = pPGM->CTX_SUFF(pGst32BitPd);
3539# ifdef IN_RING3
3540 if (!pGuestPD)
3541 pGuestPD = pgmGstLazyMap32BitPD(pPGM);
3542# endif
3543#endif
3544 return pGuestPD;
3545}
3546
3547
3548/**
3549 * Gets the guest page directory pointer table.
3550 *
3551 * @returns Pointer to the page directory in question.
3552 * @returns NULL if the page directory is not present or on an invalid page.
3553 * @param pPGM Pointer to the PGM instance data.
3554 */
3555DECLINLINE(PX86PDPT) pgmGstGetPaePDPTPtr(PPGMCPU pPGM)
3556{
3557#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3558 PX86PDPT pGuestPDPT = NULL;
3559 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPDPT);
3560 AssertRCReturn(rc, NULL);
3561#else
3562 PX86PDPT pGuestPDPT = pPGM->CTX_SUFF(pGstPaePdpt);
3563# ifdef IN_RING3
3564 if (!pGuestPDPT)
3565 pGuestPDPT = pgmGstLazyMapPaePDPT(pPGM);
3566# endif
3567#endif
3568 return pGuestPDPT;
3569}
3570
3571
3572/**
3573 * Gets the guest page directory pointer table entry for the specified address.
3574 *
3575 * @returns Pointer to the page directory in question.
3576 * @returns NULL if the page directory is not present or on an invalid page.
3577 * @param pPGM Pointer to the PGM instance data.
3578 * @param GCPtr The address.
3579 */
3580DECLINLINE(PX86PDPE) pgmGstGetPaePDPEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
3581{
3582 AssertGCPtr32(GCPtr);
3583
3584#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3585 PX86PDPT pGuestPDPT = 0;
3586 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPDPT);
3587 AssertRCReturn(rc, 0);
3588#else
3589 PX86PDPT pGuestPDPT = pPGM->CTX_SUFF(pGstPaePdpt);
3590# ifdef IN_RING3
3591 if (!pGuestPDPT)
3592 pGuestPDPT = pgmGstLazyMapPaePDPT(pPGM);
3593# endif
3594#endif
3595 return &pGuestPDPT->a[(GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE];
3596}
3597
3598
3599/**
3600 * Gets the page directory for the specified address.
3601 *
3602 * @returns Pointer to the page directory in question.
3603 * @returns NULL if the page directory is not present or on an invalid page.
3604 * @param pPGM Pointer to the PGM instance data.
3605 * @param GCPtr The address.
3606 */
3607DECLINLINE(PX86PDPAE) pgmGstGetPaePD(PPGMCPU pPGM, RTGCPTR GCPtr)
3608{
3609 AssertGCPtr32(GCPtr);
3610
3611 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3612 AssertReturn(pGuestPDPT, NULL);
3613 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3614 if (pGuestPDPT->a[iPdpt].n.u1Present)
3615 {
3616#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3617 PX86PDPAE pGuestPD = NULL;
3618 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3619 AssertRCReturn(rc, NULL);
3620#else
3621 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3622 if ( !pGuestPD
3623 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3624 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3625#endif
3626 return pGuestPD;
3627 /* returning NULL is ok if we assume it's just an invalid page of some kind emulated as all 0s. (not quite true) */
3628 }
3629 return NULL;
3630}
3631
3632
3633/**
3634 * Gets the page directory entry for the specified address.
3635 *
3636 * @returns Pointer to the page directory entry in question.
3637 * @returns NULL if the page directory is not present or on an invalid page.
3638 * @param pPGM Pointer to the PGM instance data.
3639 * @param GCPtr The address.
3640 */
3641DECLINLINE(PX86PDEPAE) pgmGstGetPaePDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
3642{
3643 AssertGCPtr32(GCPtr);
3644
3645 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3646 AssertReturn(pGuestPDPT, NULL);
3647 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3648 if (pGuestPDPT->a[iPdpt].n.u1Present)
3649 {
3650 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3651#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3652 PX86PDPAE pGuestPD = NULL;
3653 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3654 AssertRCReturn(rc, NULL);
3655#else
3656 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3657 if ( !pGuestPD
3658 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3659 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3660#endif
3661 return &pGuestPD->a[iPD];
3662 /* 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) */
3663 }
3664 return NULL;
3665}
3666
3667
3668/**
3669 * Gets the page directory entry for the specified address.
3670 *
3671 * @returns The page directory entry in question.
3672 * @returns A non-present entry if the page directory is not present or on an invalid page.
3673 * @param pPGM Pointer to the PGM instance data.
3674 * @param GCPtr The address.
3675 */
3676DECLINLINE(X86PDEPAE) pgmGstGetPaePDE(PPGMCPU pPGM, RTGCPTR GCPtr)
3677{
3678 AssertGCPtr32(GCPtr);
3679 X86PDEPAE ZeroPde = {0};
3680 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3681 if (RT_LIKELY(pGuestPDPT))
3682 {
3683 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3684 if (pGuestPDPT->a[iPdpt].n.u1Present)
3685 {
3686 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3687#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3688 PX86PDPAE pGuestPD = NULL;
3689 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3690 AssertRCReturn(rc, ZeroPde);
3691#else
3692 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3693 if ( !pGuestPD
3694 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3695 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3696#endif
3697 return pGuestPD->a[iPD];
3698 }
3699 }
3700 return ZeroPde;
3701}
3702
3703
3704/**
3705 * Gets the page directory pointer table entry for the specified address
3706 * and returns the index into the page directory
3707 *
3708 * @returns Pointer to the page directory in question.
3709 * @returns NULL if the page directory is not present or on an invalid page.
3710 * @param pPGM Pointer to the PGM instance data.
3711 * @param GCPtr The address.
3712 * @param piPD Receives the index into the returned page directory
3713 * @param pPdpe Receives the page directory pointer entry. Optional.
3714 */
3715DECLINLINE(PX86PDPAE) pgmGstGetPaePDPtr(PPGMCPU pPGM, RTGCPTR GCPtr, unsigned *piPD, PX86PDPE pPdpe)
3716{
3717 AssertGCPtr32(GCPtr);
3718
3719 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3720 AssertReturn(pGuestPDPT, NULL);
3721 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3722 if (pPdpe)
3723 *pPdpe = pGuestPDPT->a[iPdpt];
3724 if (pGuestPDPT->a[iPdpt].n.u1Present)
3725 {
3726 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3727#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3728 PX86PDPAE pGuestPD = NULL;
3729 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3730 AssertRCReturn(rc, NULL);
3731#else
3732 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3733 if ( !pGuestPD
3734 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3735 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3736#endif
3737 *piPD = iPD;
3738 return pGuestPD;
3739 /* returning NIL_RTGCPHYS is ok if we assume it's just an invalid page of some kind emulated as all 0s. */
3740 }
3741 return NULL;
3742}
3743
3744#ifndef IN_RC
3745
3746/**
3747 * Gets the page map level-4 pointer for the guest.
3748 *
3749 * @returns Pointer to the PML4 page.
3750 * @param pPGM Pointer to the PGM instance data.
3751 */
3752DECLINLINE(PX86PML4) pgmGstGetLongModePML4Ptr(PPGMCPU pPGM)
3753{
3754#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3755 PX86PML4 pGuestPml4;
3756 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPml4);
3757 AssertRCReturn(rc, NULL);
3758#else
3759 PX86PML4 pGuestPml4 = pPGM->CTX_SUFF(pGstAmd64Pml4);
3760# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R3
3761 if (!pGuestPml4)
3762 pGuestPml4 = pgmGstLazyMapPml4(pPGM);
3763# endif
3764 Assert(pGuestPml4);
3765#endif
3766 return pGuestPml4;
3767}
3768
3769
3770/**
3771 * Gets the pointer to a page map level-4 entry.
3772 *
3773 * @returns Pointer to the PML4 entry.
3774 * @param pPGM Pointer to the PGM instance data.
3775 * @param iPml4 The index.
3776 */
3777DECLINLINE(PX86PML4E) pgmGstGetLongModePML4EPtr(PPGMCPU pPGM, unsigned int iPml4)
3778{
3779#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3780 PX86PML4 pGuestPml4;
3781 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPml4);
3782 AssertRCReturn(rc, NULL);
3783#else
3784 PX86PML4 pGuestPml4 = pPGM->CTX_SUFF(pGstAmd64Pml4);
3785# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R3
3786 if (!pGuestPml4)
3787 pGuestPml4 = pgmGstLazyMapPml4(pPGM);
3788# endif
3789 Assert(pGuestPml4);
3790#endif
3791 return &pGuestPml4->a[iPml4];
3792}
3793
3794
3795/**
3796 * Gets a page map level-4 entry.
3797 *
3798 * @returns The PML4 entry.
3799 * @param pPGM Pointer to the PGM instance data.
3800 * @param iPml4 The index.
3801 */
3802DECLINLINE(X86PML4E) pgmGstGetLongModePML4E(PPGMCPU pPGM, unsigned int iPml4)
3803{
3804#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3805 PX86PML4 pGuestPml4;
3806 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPml4);
3807 if (RT_FAILURE(rc))
3808 {
3809 X86PML4E ZeroPml4e = {0};
3810 AssertMsgFailedReturn(("%Rrc\n", rc), ZeroPml4e);
3811 }
3812#else
3813 PX86PML4 pGuestPml4 = pPGM->CTX_SUFF(pGstAmd64Pml4);
3814# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R3
3815 if (!pGuestPml4)
3816 pGuestPml4 = pgmGstLazyMapPml4(pPGM);
3817# endif
3818 Assert(pGuestPml4);
3819#endif
3820 return pGuestPml4->a[iPml4];
3821}
3822
3823
3824/**
3825 * Gets the page directory pointer entry for the specified address.
3826 *
3827 * @returns Pointer to the page directory pointer entry in question.
3828 * @returns NULL if the page directory is not present or on an invalid page.
3829 * @param pPGM Pointer to the PGM instance data.
3830 * @param GCPtr The address.
3831 * @param ppPml4e Page Map Level-4 Entry (out)
3832 */
3833DECLINLINE(PX86PDPE) pgmGstGetLongModePDPTPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e)
3834{
3835 PX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3836 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3837 PCX86PML4E pPml4e = *ppPml4e = &pGuestPml4->a[iPml4];
3838 if (pPml4e->n.u1Present)
3839 {
3840 PX86PDPT pPdpt;
3841 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPml4e->u & X86_PML4E_PG_MASK, &pPdpt);
3842 AssertRCReturn(rc, NULL);
3843
3844 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3845 return &pPdpt->a[iPdpt];
3846 }
3847 return NULL;
3848}
3849
3850
3851/**
3852 * Gets the page directory entry for the specified address.
3853 *
3854 * @returns The page directory entry in question.
3855 * @returns A non-present entry if the page directory is not present or on an invalid page.
3856 * @param pPGM Pointer to the PGM instance data.
3857 * @param GCPtr The address.
3858 * @param ppPml4e Page Map Level-4 Entry (out)
3859 * @param pPdpe Page directory pointer table entry (out)
3860 */
3861DECLINLINE(X86PDEPAE) pgmGstGetLongModePDEEx(PPGMCPU pPGM, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPE pPdpe)
3862{
3863 X86PDEPAE ZeroPde = {0};
3864 PX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3865 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3866 PCX86PML4E pPml4e = *ppPml4e = &pGuestPml4->a[iPml4];
3867 if (pPml4e->n.u1Present)
3868 {
3869 PCX86PDPT pPdptTemp;
3870 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPml4e->u & X86_PML4E_PG_MASK, &pPdptTemp);
3871 AssertRCReturn(rc, ZeroPde);
3872
3873 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3874 *pPdpe = pPdptTemp->a[iPdpt];
3875 if (pPdptTemp->a[iPdpt].n.u1Present)
3876 {
3877 PCX86PDPAE pPD;
3878 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3879 AssertRCReturn(rc, ZeroPde);
3880
3881 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3882 return pPD->a[iPD];
3883 }
3884 }
3885
3886 return ZeroPde;
3887}
3888
3889
3890/**
3891 * Gets the page directory entry for the specified address.
3892 *
3893 * @returns The page directory entry in question.
3894 * @returns A non-present entry if the page directory is not present or on an invalid page.
3895 * @param pPGM Pointer to the PGM instance data.
3896 * @param GCPtr The address.
3897 */
3898DECLINLINE(X86PDEPAE) pgmGstGetLongModePDE(PPGMCPU pPGM, RTGCPTR64 GCPtr)
3899{
3900 X86PDEPAE ZeroPde = {0};
3901 PCX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3902 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3903 if (pGuestPml4->a[iPml4].n.u1Present)
3904 {
3905 PCX86PDPT pPdptTemp;
3906 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pGuestPml4->a[iPml4].u & X86_PML4E_PG_MASK, &pPdptTemp);
3907 AssertRCReturn(rc, ZeroPde);
3908
3909 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3910 if (pPdptTemp->a[iPdpt].n.u1Present)
3911 {
3912 PCX86PDPAE pPD;
3913 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3914 AssertRCReturn(rc, ZeroPde);
3915
3916 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3917 return pPD->a[iPD];
3918 }
3919 }
3920 return ZeroPde;
3921}
3922
3923
3924/**
3925 * Gets the page directory entry for the specified address.
3926 *
3927 * @returns Pointer to the page directory entry in question.
3928 * @returns NULL if the page directory is not present or on an invalid page.
3929 * @param pPGM Pointer to the PGM instance data.
3930 * @param GCPtr The address.
3931 */
3932DECLINLINE(PX86PDEPAE) pgmGstGetLongModePDEPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr)
3933{
3934 PCX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3935 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3936 if (pGuestPml4->a[iPml4].n.u1Present)
3937 {
3938 PCX86PDPT pPdptTemp;
3939 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pGuestPml4->a[iPml4].u & X86_PML4E_PG_MASK, &pPdptTemp);
3940 AssertRCReturn(rc, NULL);
3941
3942 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3943 if (pPdptTemp->a[iPdpt].n.u1Present)
3944 {
3945 PX86PDPAE pPD;
3946 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3947 AssertRCReturn(rc, NULL);
3948
3949 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3950 return &pPD->a[iPD];
3951 }
3952 }
3953 return NULL;
3954}
3955
3956
3957/**
3958 * Gets the GUEST page directory pointer for the specified address.
3959 *
3960 * @returns The page directory in question.
3961 * @returns NULL if the page directory is not present or on an invalid page.
3962 * @param pPGM Pointer to the PGM instance data.
3963 * @param GCPtr The address.
3964 * @param ppPml4e Page Map Level-4 Entry (out)
3965 * @param pPdpe Page directory pointer table entry (out)
3966 * @param piPD Receives the index into the returned page directory
3967 */
3968DECLINLINE(PX86PDPAE) pgmGstGetLongModePDPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPE pPdpe, unsigned *piPD)
3969{
3970 PX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3971 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3972 PCX86PML4E pPml4e = *ppPml4e = &pGuestPml4->a[iPml4];
3973 if (pPml4e->n.u1Present)
3974 {
3975 PCX86PDPT pPdptTemp;
3976 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPml4e->u & X86_PML4E_PG_MASK, &pPdptTemp);
3977 AssertRCReturn(rc, NULL);
3978
3979 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3980 *pPdpe = pPdptTemp->a[iPdpt];
3981 if (pPdptTemp->a[iPdpt].n.u1Present)
3982 {
3983 PX86PDPAE pPD;
3984 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3985 AssertRCReturn(rc, NULL);
3986
3987 *piPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3988 return pPD;
3989 }
3990 }
3991 return 0;
3992}
3993
3994#endif /* !IN_RC */
3995
3996/**
3997 * Gets the shadow page directory, 32-bit.
3998 *
3999 * @returns Pointer to the shadow 32-bit PD.
4000 * @param pPGM Pointer to the PGM instance data.
4001 */
4002DECLINLINE(PX86PD) pgmShwGet32BitPDPtr(PPGMCPU pPGM)
4003{
4004 return (PX86PD)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPGM->CTX_SUFF(pShwPageCR3));
4005}
4006
4007
4008/**
4009 * Gets the shadow page directory entry for the specified address, 32-bit.
4010 *
4011 * @returns Shadow 32-bit PDE.
4012 * @param pPGM Pointer to the PGM instance data.
4013 * @param GCPtr The address.
4014 */
4015DECLINLINE(X86PDE) pgmShwGet32BitPDE(PPGMCPU pPGM, RTGCPTR GCPtr)
4016{
4017 const unsigned iPd = (GCPtr >> X86_PD_SHIFT) & X86_PD_MASK;
4018
4019 PX86PD pShwPde = pgmShwGet32BitPDPtr(pPGM);
4020 if (!pShwPde)
4021 {
4022 X86PDE ZeroPde = {0};
4023 return ZeroPde;
4024 }
4025 return pShwPde->a[iPd];
4026}
4027
4028
4029/**
4030 * Gets the pointer to the shadow page directory entry for the specified
4031 * address, 32-bit.
4032 *
4033 * @returns Pointer to the shadow 32-bit PDE.
4034 * @param pPGM Pointer to the PGM instance data.
4035 * @param GCPtr The address.
4036 */
4037DECLINLINE(PX86PDE) pgmShwGet32BitPDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
4038{
4039 const unsigned iPd = (GCPtr >> X86_PD_SHIFT) & X86_PD_MASK;
4040
4041 PX86PD pPde = pgmShwGet32BitPDPtr(pPGM);
4042 AssertReturn(pPde, NULL);
4043 return &pPde->a[iPd];
4044}
4045
4046
4047/**
4048 * Gets the shadow page pointer table, PAE.
4049 *
4050 * @returns Pointer to the shadow PAE PDPT.
4051 * @param pPGM Pointer to the PGM instance data.
4052 */
4053DECLINLINE(PX86PDPT) pgmShwGetPaePDPTPtr(PPGMCPU pPGM)
4054{
4055 return (PX86PDPT)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPGM->CTX_SUFF(pShwPageCR3));
4056}
4057
4058
4059/**
4060 * Gets the shadow page directory for the specified address, PAE.
4061 *
4062 * @returns Pointer to the shadow PD.
4063 * @param pPGM Pointer to the PGM instance data.
4064 * @param GCPtr The address.
4065 */
4066DECLINLINE(PX86PDPAE) pgmShwGetPaePDPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
4067{
4068 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
4069 PX86PDPT pPdpt = pgmShwGetPaePDPTPtr(pPGM);
4070
4071 if (!pPdpt->a[iPdpt].n.u1Present)
4072 return NULL;
4073
4074 /* Fetch the pgm pool shadow descriptor. */
4075 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(PGMCPU2PGM(pPGM)->CTX_SUFF(pPool), pPdpt->a[iPdpt].u & X86_PDPE_PG_MASK);
4076 AssertReturn(pShwPde, NULL);
4077
4078 return (PX86PDPAE)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pShwPde);
4079}
4080
4081
4082/**
4083 * Gets the shadow page directory for the specified address, PAE.
4084 *
4085 * @returns Pointer to the shadow PD.
4086 * @param pPGM Pointer to the PGM instance data.
4087 * @param GCPtr The address.
4088 */
4089DECLINLINE(PX86PDPAE) pgmShwGetPaePDPtr(PPGMCPU pPGM, PX86PDPT pPdpt, RTGCPTR GCPtr)
4090{
4091 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
4092
4093 if (!pPdpt->a[iPdpt].n.u1Present)
4094 return NULL;
4095
4096 /* Fetch the pgm pool shadow descriptor. */
4097 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(PGMCPU2PGM(pPGM)->CTX_SUFF(pPool), pPdpt->a[iPdpt].u & X86_PDPE_PG_MASK);
4098 AssertReturn(pShwPde, NULL);
4099
4100 return (PX86PDPAE)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pShwPde);
4101}
4102
4103
4104/**
4105 * Gets the shadow page directory entry, PAE.
4106 *
4107 * @returns PDE.
4108 * @param pPGM Pointer to the PGM instance data.
4109 * @param GCPtr The address.
4110 */
4111DECLINLINE(X86PDEPAE) pgmShwGetPaePDE(PPGMCPU pPGM, RTGCPTR GCPtr)
4112{
4113 const unsigned iPd = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
4114
4115 PX86PDPAE pShwPde = pgmShwGetPaePDPtr(pPGM, GCPtr);
4116 if (!pShwPde)
4117 {
4118 X86PDEPAE ZeroPde = {0};
4119 return ZeroPde;
4120 }
4121 return pShwPde->a[iPd];
4122}
4123
4124
4125/**
4126 * Gets the pointer to the shadow page directory entry for an address, PAE.
4127 *
4128 * @returns Pointer to the PDE.
4129 * @param pPGM Pointer to the PGM instance data.
4130 * @param GCPtr The address.
4131 */
4132DECLINLINE(PX86PDEPAE) pgmShwGetPaePDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
4133{
4134 const unsigned iPd = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
4135
4136 PX86PDPAE pPde = pgmShwGetPaePDPtr(pPGM, GCPtr);
4137 AssertReturn(pPde, NULL);
4138 return &pPde->a[iPd];
4139}
4140
4141#ifndef IN_RC
4142
4143/**
4144 * Gets the shadow page map level-4 pointer.
4145 *
4146 * @returns Pointer to the shadow PML4.
4147 * @param pPGM Pointer to the PGM instance data.
4148 */
4149DECLINLINE(PX86PML4) pgmShwGetLongModePML4Ptr(PPGMCPU pPGM)
4150{
4151 return (PX86PML4)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPGM->CTX_SUFF(pShwPageCR3));
4152}
4153
4154
4155/**
4156 * Gets the shadow page map level-4 entry for the specified address.
4157 *
4158 * @returns The entry.
4159 * @param pPGM Pointer to the PGM instance data.
4160 * @param GCPtr The address.
4161 */
4162DECLINLINE(X86PML4E) pgmShwGetLongModePML4E(PPGMCPU pPGM, RTGCPTR GCPtr)
4163{
4164 const unsigned iPml4 = ((RTGCUINTPTR64)GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
4165 PX86PML4 pShwPml4 = pgmShwGetLongModePML4Ptr(pPGM);
4166
4167 if (!pShwPml4)
4168 {
4169 X86PML4E ZeroPml4e = {0};
4170 return ZeroPml4e;
4171 }
4172 return pShwPml4->a[iPml4];
4173}
4174
4175
4176/**
4177 * Gets the pointer to the specified shadow page map level-4 entry.
4178 *
4179 * @returns The entry.
4180 * @param pPGM Pointer to the PGM instance data.
4181 * @param iPml4 The PML4 index.
4182 */
4183DECLINLINE(PX86PML4E) pgmShwGetLongModePML4EPtr(PPGMCPU pPGM, unsigned int iPml4)
4184{
4185 PX86PML4 pShwPml4 = pgmShwGetLongModePML4Ptr(pPGM);
4186 if (!pShwPml4)
4187 return NULL;
4188 return &pShwPml4->a[iPml4];
4189}
4190
4191
4192/**
4193 * Gets the GUEST page directory pointer for the specified address.
4194 *
4195 * @returns The page directory in question.
4196 * @returns NULL if the page directory is not present or on an invalid page.
4197 * @param pPGM Pointer to the PGM instance data.
4198 * @param GCPtr The address.
4199 * @param piPD Receives the index into the returned page directory
4200 */
4201DECLINLINE(PX86PDPAE) pgmGstGetLongModePDPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr, unsigned *piPD)
4202{
4203 PCX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
4204 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
4205 if (pGuestPml4->a[iPml4].n.u1Present)
4206 {
4207 PCX86PDPT pPdptTemp;
4208 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pGuestPml4->a[iPml4].u & X86_PML4E_PG_MASK, &pPdptTemp);
4209 AssertRCReturn(rc, NULL);
4210
4211 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
4212 if (pPdptTemp->a[iPdpt].n.u1Present)
4213 {
4214 PX86PDPAE pPD;
4215 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
4216 AssertRCReturn(rc, NULL);
4217
4218 *piPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
4219 return pPD;
4220 }
4221 }
4222 return NULL;
4223}
4224
4225#endif /* !IN_RC */
4226
4227/**
4228 * Gets the page state for a physical handler.
4229 *
4230 * @returns The physical handler page state.
4231 * @param pCur The physical handler in question.
4232 */
4233DECLINLINE(unsigned) pgmHandlerPhysicalCalcState(PPGMPHYSHANDLER pCur)
4234{
4235 switch (pCur->enmType)
4236 {
4237 case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE:
4238 return PGM_PAGE_HNDL_PHYS_STATE_WRITE;
4239
4240 case PGMPHYSHANDLERTYPE_MMIO:
4241 case PGMPHYSHANDLERTYPE_PHYSICAL_ALL:
4242 return PGM_PAGE_HNDL_PHYS_STATE_ALL;
4243
4244 default:
4245 AssertFatalMsgFailed(("Invalid type %d\n", pCur->enmType));
4246 }
4247}
4248
4249
4250/**
4251 * Gets the page state for a virtual handler.
4252 *
4253 * @returns The virtual handler page state.
4254 * @param pCur The virtual handler in question.
4255 * @remarks This should never be used on a hypervisor access handler.
4256 */
4257DECLINLINE(unsigned) pgmHandlerVirtualCalcState(PPGMVIRTHANDLER pCur)
4258{
4259 switch (pCur->enmType)
4260 {
4261 case PGMVIRTHANDLERTYPE_WRITE:
4262 return PGM_PAGE_HNDL_VIRT_STATE_WRITE;
4263 case PGMVIRTHANDLERTYPE_ALL:
4264 return PGM_PAGE_HNDL_VIRT_STATE_ALL;
4265 default:
4266 AssertFatalMsgFailed(("Invalid type %d\n", pCur->enmType));
4267 }
4268}
4269
4270
4271/**
4272 * Clears one physical page of a virtual handler
4273 *
4274 * @param pPGM Pointer to the PGM instance.
4275 * @param pCur Virtual handler structure
4276 * @param iPage Physical page index
4277 *
4278 * @remark Only used when PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL is being set, so no
4279 * need to care about other handlers in the same page.
4280 */
4281DECLINLINE(void) pgmHandlerVirtualClearPage(PPGM pPGM, PPGMVIRTHANDLER pCur, unsigned iPage)
4282{
4283 const PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
4284
4285 /*
4286 * Remove the node from the tree (it's supposed to be in the tree if we get here!).
4287 */
4288#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4289 AssertReleaseMsg(pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE,
4290 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4291 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias));
4292#endif
4293 if (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD)
4294 {
4295 /* We're the head of the alias chain. */
4296 PPGMPHYS2VIRTHANDLER pRemove = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRemove(&pPGM->CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key); NOREF(pRemove);
4297#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4298 AssertReleaseMsg(pRemove != NULL,
4299 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4300 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias));
4301 AssertReleaseMsg(pRemove == pPhys2Virt,
4302 ("wanted: pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n"
4303 " got: pRemove=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4304 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias,
4305 pRemove, pRemove->Core.Key, pRemove->Core.KeyLast, pRemove->offVirtHandler, pRemove->offNextAlias));
4306#endif
4307 if (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK)
4308 {
4309 /* Insert the next list in the alias chain into the tree. */
4310 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
4311#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4312 AssertReleaseMsg(pNext->offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE,
4313 ("pNext=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4314 pNext, pNext->Core.Key, pNext->Core.KeyLast, pNext->offVirtHandler, pNext->offNextAlias));
4315#endif
4316 pNext->offNextAlias |= PGMPHYS2VIRTHANDLER_IS_HEAD;
4317 bool fRc = RTAvlroGCPhysInsert(&pPGM->CTX_SUFF(pTrees)->PhysToVirtHandlers, &pNext->Core);
4318 AssertRelease(fRc);
4319 }
4320 }
4321 else
4322 {
4323 /* Locate the previous node in the alias chain. */
4324 PPGMPHYS2VIRTHANDLER pPrev = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pPGM->CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
4325#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4326 AssertReleaseMsg(pPrev != pPhys2Virt,
4327 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} pPrev=%p\n",
4328 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias, pPrev));
4329#endif
4330 for (;;)
4331 {
4332 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPrev + (pPrev->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
4333 if (pNext == pPhys2Virt)
4334 {
4335 /* unlink. */
4336 LogFlow(("pgmHandlerVirtualClearPage: removed %p:{.offNextAlias=%#RX32} from alias chain. prev %p:{.offNextAlias=%#RX32} [%RGp-%RGp]\n",
4337 pPhys2Virt, pPhys2Virt->offNextAlias, pPrev, pPrev->offNextAlias, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
4338 if (!(pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
4339 pPrev->offNextAlias &= ~PGMPHYS2VIRTHANDLER_OFF_MASK;
4340 else
4341 {
4342 PPGMPHYS2VIRTHANDLER pNewNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
4343 pPrev->offNextAlias = ((intptr_t)pNewNext - (intptr_t)pPrev)
4344 | (pPrev->offNextAlias & ~PGMPHYS2VIRTHANDLER_OFF_MASK);
4345 }
4346 break;
4347 }
4348
4349 /* next */
4350 if (pNext == pPrev)
4351 {
4352#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4353 AssertReleaseMsg(pNext != pPrev,
4354 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} pPrev=%p\n",
4355 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias, pPrev));
4356#endif
4357 break;
4358 }
4359 pPrev = pNext;
4360 }
4361 }
4362 Log2(("PHYS2VIRT: Removing %RGp-%RGp %#RX32 %s\n",
4363 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
4364 pPhys2Virt->offNextAlias = 0;
4365 pPhys2Virt->Core.KeyLast = NIL_RTGCPHYS; /* require reinsert */
4366
4367 /*
4368 * Clear the ram flags for this page.
4369 */
4370 PPGMPAGE pPage = pgmPhysGetPage(pPGM, pPhys2Virt->Core.Key);
4371 AssertReturnVoid(pPage);
4372 PGM_PAGE_SET_HNDL_VIRT_STATE(pPage, PGM_PAGE_HNDL_VIRT_STATE_NONE);
4373}
4374
4375
4376/**
4377 * Internal worker for finding a 'in-use' shadow page give by it's physical address.
4378 *
4379 * @returns Pointer to the shadow page structure.
4380 * @param pPool The pool.
4381 * @param idx The pool page index.
4382 */
4383DECLINLINE(PPGMPOOLPAGE) pgmPoolGetPageByIdx(PPGMPOOL pPool, unsigned idx)
4384{
4385 AssertFatalMsg(idx >= PGMPOOL_IDX_FIRST && idx < pPool->cCurPages, ("idx=%d\n", idx));
4386 return &pPool->aPages[idx];
4387}
4388
4389
4390#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
4391/**
4392 * Clear references to guest physical memory.
4393 *
4394 * @param pPool The pool.
4395 * @param pPoolPage The pool page.
4396 * @param pPhysPage The physical guest page tracking structure.
4397 */
4398DECLINLINE(void) pgmTrackDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPoolPage, PPGMPAGE pPhysPage)
4399{
4400 /*
4401 * Just deal with the simple case here.
4402 */
4403# ifdef LOG_ENABLED
4404 const unsigned uOrg = PGM_PAGE_GET_TRACKING(pPhysPage);
4405# endif
4406 const unsigned cRefs = PGM_PAGE_GET_TD_CREFS(pPhysPage);
4407 if (cRefs == 1)
4408 {
4409 Assert(pPoolPage->idx == PGM_PAGE_GET_TD_IDX(pPhysPage));
4410 PGM_PAGE_SET_TRACKING(pPhysPage, 0);
4411 }
4412 else
4413 pgmPoolTrackPhysExtDerefGCPhys(pPool, pPoolPage, pPhysPage);
4414 Log2(("pgmTrackDerefGCPhys: %x -> %x pPhysPage=%R[pgmpage]\n", uOrg, PGM_PAGE_GET_TRACKING(pPhysPage), pPhysPage ));
4415}
4416#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
4417
4418
4419#ifdef PGMPOOL_WITH_CACHE
4420/**
4421 * Moves the page to the head of the age list.
4422 *
4423 * This is done when the cached page is used in one way or another.
4424 *
4425 * @param pPool The pool.
4426 * @param pPage The cached page.
4427 */
4428DECLINLINE(void) pgmPoolCacheUsed(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4429{
4430 PVM pVM = pPool->CTX_SUFF(pVM);
4431 pgmLock(pVM);
4432
4433 /*
4434 * Move to the head of the age list.
4435 */
4436 if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
4437 {
4438 /* unlink */
4439 pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
4440 if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
4441 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
4442 else
4443 pPool->iAgeTail = pPage->iAgePrev;
4444
4445 /* insert at head */
4446 pPage->iAgePrev = NIL_PGMPOOL_IDX;
4447 pPage->iAgeNext = pPool->iAgeHead;
4448 Assert(pPage->iAgeNext != NIL_PGMPOOL_IDX); /* we would've already been head then */
4449 pPool->iAgeHead = pPage->idx;
4450 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->idx;
4451 }
4452 pgmUnlock(pVM);
4453}
4454#endif /* PGMPOOL_WITH_CACHE */
4455
4456/**
4457 * Locks a page to prevent flushing (important for cr3 root pages or shadow pae pd pages).
4458 *
4459 * @param pVM VM Handle.
4460 * @param pPage PGM pool page
4461 */
4462DECLINLINE(void) pgmPoolLockPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4463{
4464 ASMAtomicIncU32(&pPage->cLocked);
4465}
4466
4467
4468/**
4469 * Unlocks a page to allow flushing again
4470 *
4471 * @param pVM VM Handle.
4472 * @param pPage PGM pool page
4473 */
4474DECLINLINE(void) pgmPoolUnlockPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4475{
4476 Assert(pPage->cLocked);
4477 ASMAtomicDecU32(&pPage->cLocked);
4478}
4479
4480
4481/**
4482 * Checks if the page is locked (e.g. the active CR3 or one of the four PDs of a PAE PDPT)
4483 *
4484 * @returns VBox status code.
4485 * @param pPage PGM pool page
4486 */
4487DECLINLINE(bool) pgmPoolIsPageLocked(PPGM pPGM, PPGMPOOLPAGE pPage)
4488{
4489 if (pPage->cLocked)
4490 {
4491 LogFlow(("pgmPoolIsPageLocked found root page %d\n", pPage->enmKind));
4492 if (pPage->cModifications)
4493 pPage->cModifications = 1; /* reset counter (can't use 0, or else it will be reinserted in the modified list) */
4494 return true;
4495 }
4496 return false;
4497}
4498
4499/**
4500 * Tells if mappings are to be put into the shadow page table or not
4501 *
4502 * @returns boolean result
4503 * @param pVM VM handle.
4504 */
4505DECLINLINE(bool) pgmMapAreMappingsEnabled(PPGM pPGM)
4506{
4507#ifdef IN_RING0
4508 /* There are no mappings in VT-x and AMD-V mode. */
4509 Assert(pPGM->fDisableMappings);
4510 return false;
4511#else
4512 return !pPGM->fDisableMappings;
4513#endif
4514}
4515
4516/** @} */
4517
4518#endif
4519
4520
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