VirtualBox

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

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

More checks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 186.7 KB
Line 
1/* $Id: PGMInternal.h 20764 2009-06-22 11:13:45Z 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 RC virtual address of the 32-bit page table. */
452 RCPTRTYPE(PX86PT) pPTRC;
453 /** The RC virtual address of the two PAE page table. */
454 RCPTRTYPE(PX86PTPAE) paPaePTsRC;
455 /** The R0 virtual address of the 32-bit page table. */
456 R0PTRTYPE(PX86PT) pPTR0;
457 /** The R0 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 RC 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 /** For saving stack space, the disassembler state is allocated here instead of
2705 * on the stack.
2706 * @note The DISCPUSTATE structure is not R3/R0/RZ clean! */
2707 union
2708 {
2709 /** The disassembler scratch space. */
2710 DISCPUSTATE DisState;
2711 /** Padding. */
2712 uint8_t abDisStatePadding[DISCPUSTATE_PADDING_SIZE];
2713 };
2714
2715 /** @name Release Statistics
2716 * @{ */
2717 /** The number of times the guest has switched mode since last reset or statistics reset. */
2718 STAMCOUNTER cGuestModeChanges;
2719 /** @} */
2720
2721#ifdef VBOX_WITH_STATISTICS /** @todo move this chunk to the heap. */
2722 /** @name Statistics
2723 * @{ */
2724 /** RC: Which statistic this \#PF should be attributed to. */
2725 RCPTRTYPE(PSTAMPROFILE) pStatTrap0eAttributionRC;
2726 RTRCPTR padding0;
2727 /** R0: Which statistic this \#PF should be attributed to. */
2728 R0PTRTYPE(PSTAMPROFILE) pStatTrap0eAttributionR0;
2729 RTR0PTR padding1;
2730
2731 /* Common */
2732 STAMCOUNTER StatSyncPtPD[X86_PG_ENTRIES]; /**< SyncPT - PD distribution. */
2733 STAMCOUNTER StatSyncPagePD[X86_PG_ENTRIES]; /**< SyncPage - PD distribution. */
2734
2735 /* R0 only: */
2736 STAMCOUNTER StatR0DynMapMigrateInvlPg; /**< R0: invlpg in PGMDynMapMigrateAutoSet. */
2737 STAMPROFILE StatR0DynMapGCPageInl; /**< R0: Calls to pgmR0DynMapGCPageInlined. */
2738 STAMCOUNTER StatR0DynMapGCPageInlHits; /**< R0: Hash table lookup hits. */
2739 STAMCOUNTER StatR0DynMapGCPageInlMisses; /**< R0: Misses that falls back to code common with PGMDynMapHCPage. */
2740 STAMCOUNTER StatR0DynMapGCPageInlRamHits; /**< R0: 1st ram range hits. */
2741 STAMCOUNTER StatR0DynMapGCPageInlRamMisses; /**< R0: 1st ram range misses, takes slow path. */
2742 STAMPROFILE StatR0DynMapHCPageInl; /**< R0: Calls to pgmR0DynMapHCPageInlined. */
2743 STAMCOUNTER StatR0DynMapHCPageInlHits; /**< R0: Hash table lookup hits. */
2744 STAMCOUNTER StatR0DynMapHCPageInlMisses; /**< R0: Misses that falls back to code common with PGMDynMapHCPage. */
2745 STAMPROFILE StatR0DynMapHCPage; /**< R0: Calls to PGMDynMapHCPage. */
2746 STAMCOUNTER StatR0DynMapSetOptimize; /**< R0: Calls to pgmDynMapOptimizeAutoSet. */
2747 STAMCOUNTER StatR0DynMapSetSearchFlushes; /**< R0: Set search restorting to subset flushes. */
2748 STAMCOUNTER StatR0DynMapSetSearchHits; /**< R0: Set search hits. */
2749 STAMCOUNTER StatR0DynMapSetSearchMisses; /**< R0: Set search misses. */
2750 STAMCOUNTER StatR0DynMapPage; /**< R0: Calls to pgmR0DynMapPage. */
2751 STAMCOUNTER StatR0DynMapPageHits0; /**< R0: Hits at iPage+0. */
2752 STAMCOUNTER StatR0DynMapPageHits1; /**< R0: Hits at iPage+1. */
2753 STAMCOUNTER StatR0DynMapPageHits2; /**< R0: Hits at iPage+2. */
2754 STAMCOUNTER StatR0DynMapPageInvlPg; /**< R0: invlpg. */
2755 STAMCOUNTER StatR0DynMapPageSlow; /**< R0: Calls to pgmR0DynMapPageSlow. */
2756 STAMCOUNTER StatR0DynMapPageSlowLoopHits; /**< R0: Hits in the pgmR0DynMapPageSlow search loop. */
2757 STAMCOUNTER StatR0DynMapPageSlowLoopMisses; /**< R0: Misses in the pgmR0DynMapPageSlow search loop. */
2758 //STAMCOUNTER StatR0DynMapPageSlowLostHits; /**< R0: Lost hits. */
2759 STAMCOUNTER StatR0DynMapSubsets; /**< R0: Times PGMDynMapPushAutoSubset was called. */
2760 STAMCOUNTER StatR0DynMapPopFlushes; /**< R0: Times PGMDynMapPopAutoSubset flushes the subset. */
2761 STAMCOUNTER aStatR0DynMapSetSize[11]; /**< R0: Set size distribution. */
2762
2763 /* RZ only: */
2764 STAMPROFILE StatRZTrap0e; /**< RC/R0: PGMTrap0eHandler() profiling. */
2765 STAMPROFILE StatRZTrap0eTimeCheckPageFault;
2766 STAMPROFILE StatRZTrap0eTimeSyncPT;
2767 STAMPROFILE StatRZTrap0eTimeMapping;
2768 STAMPROFILE StatRZTrap0eTimeOutOfSync;
2769 STAMPROFILE StatRZTrap0eTimeHandlers;
2770 STAMPROFILE StatRZTrap0eTime2CSAM; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is CSAM. */
2771 STAMPROFILE StatRZTrap0eTime2DirtyAndAccessed; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is dirty and/or accessed bit emulation. */
2772 STAMPROFILE StatRZTrap0eTime2GuestTrap; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a guest trap. */
2773 STAMPROFILE StatRZTrap0eTime2HndPhys; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a physical handler. */
2774 STAMPROFILE StatRZTrap0eTime2HndVirt; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a virtual handler. */
2775 STAMPROFILE StatRZTrap0eTime2HndUnhandled; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is access outside the monitored areas of a monitored page. */
2776 STAMPROFILE StatRZTrap0eTime2Misc; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is not known. */
2777 STAMPROFILE StatRZTrap0eTime2OutOfSync; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync page. */
2778 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndPhys; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync physical handler page. */
2779 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndVirt; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync virtual handler page. */
2780 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndObs; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an obsolete handler page. */
2781 STAMPROFILE StatRZTrap0eTime2SyncPT; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is lazy syncing of a PT. */
2782 STAMCOUNTER StatRZTrap0eConflicts; /**< RC/R0: The number of times \#PF was caused by an undetected conflict. */
2783 STAMCOUNTER StatRZTrap0eHandlersMapping; /**< RC/R0: Number of traps due to access handlers in mappings. */
2784 STAMCOUNTER StatRZTrap0eHandlersOutOfSync; /**< RC/R0: Number of out-of-sync handled pages. */
2785 STAMCOUNTER StatRZTrap0eHandlersPhysical; /**< RC/R0: Number of traps due to physical access handlers. */
2786 STAMCOUNTER StatRZTrap0eHandlersVirtual; /**< RC/R0: Number of traps due to virtual access handlers. */
2787 STAMCOUNTER StatRZTrap0eHandlersVirtualByPhys; /**< RC/R0: Number of traps due to virtual access handlers found by physical address. */
2788 STAMCOUNTER StatRZTrap0eHandlersVirtualUnmarked;/**< RC/R0: Number of traps due to virtual access handlers found by virtual address (without proper physical flags). */
2789 STAMCOUNTER StatRZTrap0eHandlersUnhandled; /**< RC/R0: Number of traps due to access outside range of monitored page(s). */
2790 STAMCOUNTER StatRZTrap0eHandlersInvalid; /**< RC/R0: Number of traps due to access to invalid physical memory. */
2791 STAMCOUNTER StatRZTrap0eUSNotPresentRead; /**< RC/R0: #PF err kind */
2792 STAMCOUNTER StatRZTrap0eUSNotPresentWrite; /**< RC/R0: #PF err kind */
2793 STAMCOUNTER StatRZTrap0eUSWrite; /**< RC/R0: #PF err kind */
2794 STAMCOUNTER StatRZTrap0eUSReserved; /**< RC/R0: #PF err kind */
2795 STAMCOUNTER StatRZTrap0eUSNXE; /**< RC/R0: #PF err kind */
2796 STAMCOUNTER StatRZTrap0eUSRead; /**< RC/R0: #PF err kind */
2797 STAMCOUNTER StatRZTrap0eSVNotPresentRead; /**< RC/R0: #PF err kind */
2798 STAMCOUNTER StatRZTrap0eSVNotPresentWrite; /**< RC/R0: #PF err kind */
2799 STAMCOUNTER StatRZTrap0eSVWrite; /**< RC/R0: #PF err kind */
2800 STAMCOUNTER StatRZTrap0eSVReserved; /**< RC/R0: #PF err kind */
2801 STAMCOUNTER StatRZTrap0eSNXE; /**< RC/R0: #PF err kind */
2802 STAMCOUNTER StatRZTrap0eGuestPF; /**< RC/R0: Real guest #PFs. */
2803 STAMCOUNTER StatRZTrap0eGuestPFUnh; /**< RC/R0: Real guest #PF ending up at the end of the #PF code. */
2804 STAMCOUNTER StatRZTrap0eGuestPFMapping; /**< RC/R0: Real guest #PF to HMA or other mapping. */
2805 STAMCOUNTER StatRZTrap0eWPEmulInRZ; /**< RC/R0: WP=0 virtualization trap, handled. */
2806 STAMCOUNTER StatRZTrap0eWPEmulToR3; /**< RC/R0: WP=0 virtualization trap, chickened out. */
2807 STAMCOUNTER StatRZTrap0ePD[X86_PG_ENTRIES]; /**< RC/R0: PD distribution of the #PFs. */
2808 STAMCOUNTER StatRZGuestCR3WriteHandled; /**< RC/R0: The number of times WriteHandlerCR3() was successfully called. */
2809 STAMCOUNTER StatRZGuestCR3WriteUnhandled; /**< RC/R0: The number of times WriteHandlerCR3() was called and we had to fall back to the recompiler. */
2810 STAMCOUNTER StatRZGuestCR3WriteConflict; /**< RC/R0: The number of times WriteHandlerCR3() was called and a conflict was detected. */
2811 STAMCOUNTER StatRZGuestROMWriteHandled; /**< RC/R0: The number of times pgmPhysRomWriteHandler() was successfully called. */
2812 STAMCOUNTER StatRZGuestROMWriteUnhandled; /**< RC/R0: The number of times pgmPhysRomWriteHandler() was called and we had to fall back to the recompiler */
2813
2814 /* HC - R3 and (maybe) R0: */
2815
2816 /* RZ & R3: */
2817 STAMPROFILE StatRZSyncCR3; /**< RC/R0: PGMSyncCR3() profiling. */
2818 STAMPROFILE StatRZSyncCR3Handlers; /**< RC/R0: Profiling of the PGMSyncCR3() update handler section. */
2819 STAMCOUNTER StatRZSyncCR3Global; /**< RC/R0: The number of global CR3 syncs. */
2820 STAMCOUNTER StatRZSyncCR3NotGlobal; /**< RC/R0: The number of non-global CR3 syncs. */
2821 STAMCOUNTER StatRZSyncCR3DstCacheHit; /**< RC/R0: The number of times we got some kind of cache hit on a page table. */
2822 STAMCOUNTER StatRZSyncCR3DstFreed; /**< RC/R0: The number of times we've had to free a shadow entry. */
2823 STAMCOUNTER StatRZSyncCR3DstFreedSrcNP; /**< RC/R0: The number of times we've had to free a shadow entry for which the source entry was not present. */
2824 STAMCOUNTER StatRZSyncCR3DstNotPresent; /**< RC/R0: The number of times we've encountered a not present shadow entry for a present guest entry. */
2825 STAMCOUNTER StatRZSyncCR3DstSkippedGlobalPD; /**< RC/R0: The number of times a global page directory wasn't flushed. */
2826 STAMCOUNTER StatRZSyncCR3DstSkippedGlobalPT; /**< RC/R0: The number of times a page table with only global entries wasn't flushed. */
2827 STAMPROFILE StatRZSyncPT; /**< RC/R0: PGMSyncPT() profiling. */
2828 STAMCOUNTER StatRZSyncPTFailed; /**< RC/R0: The number of times PGMSyncPT() failed. */
2829 STAMCOUNTER StatRZSyncPT4K; /**< RC/R0: Number of 4KB syncs. */
2830 STAMCOUNTER StatRZSyncPT4M; /**< RC/R0: Number of 4MB syncs. */
2831 STAMCOUNTER StatRZSyncPagePDNAs; /**< RC/R0: The number of time we've marked a PD not present from SyncPage to virtualize the accessed bit. */
2832 STAMCOUNTER StatRZSyncPagePDOutOfSync; /**< RC/R0: The number of time we've encountered an out-of-sync PD in SyncPage. */
2833 STAMCOUNTER StatRZAccessedPage; /**< RC/R0: The number of pages marked not present for accessed bit emulation. */
2834 STAMPROFILE StatRZDirtyBitTracking; /**< RC/R0: Profiling the dirty bit tracking in CheckPageFault().. */
2835 STAMCOUNTER StatRZDirtyPage; /**< RC/R0: The number of pages marked read-only for dirty bit tracking. */
2836 STAMCOUNTER StatRZDirtyPageBig; /**< RC/R0: The number of pages marked read-only for dirty bit tracking. */
2837 STAMCOUNTER StatRZDirtyPageSkipped; /**< RC/R0: The number of pages already dirty or readonly. */
2838 STAMCOUNTER StatRZDirtyPageTrap; /**< RC/R0: The number of traps generated for dirty bit tracking. */
2839 STAMCOUNTER StatRZDirtyPageStale; /**< RC/R0: The number of traps generated for dirty bit tracking. (stale tlb entries) */
2840 STAMCOUNTER StatRZDirtyTrackRealPF; /**< RC/R0: The number of real pages faults during dirty bit tracking. */
2841 STAMCOUNTER StatRZDirtiedPage; /**< RC/R0: The number of pages marked dirty because of write accesses. */
2842 STAMCOUNTER StatRZPageAlreadyDirty; /**< RC/R0: The number of pages already marked dirty because of write accesses. */
2843 STAMPROFILE StatRZInvalidatePage; /**< RC/R0: PGMInvalidatePage() profiling. */
2844 STAMCOUNTER StatRZInvalidatePage4KBPages; /**< RC/R0: The number of times PGMInvalidatePage() was called for a 4KB page. */
2845 STAMCOUNTER StatRZInvalidatePage4MBPages; /**< RC/R0: The number of times PGMInvalidatePage() was called for a 4MB page. */
2846 STAMCOUNTER StatRZInvalidatePage4MBPagesSkip; /**< RC/R0: The number of times PGMInvalidatePage() skipped a 4MB page. */
2847 STAMCOUNTER StatRZInvalidatePagePDMappings; /**< RC/R0: The number of times PGMInvalidatePage() was called for a page directory containing mappings (no conflict). */
2848 STAMCOUNTER StatRZInvalidatePagePDNAs; /**< RC/R0: The number of times PGMInvalidatePage() was called for a not accessed page directory. */
2849 STAMCOUNTER StatRZInvalidatePagePDNPs; /**< RC/R0: The number of times PGMInvalidatePage() was called for a not present page directory. */
2850 STAMCOUNTER StatRZInvalidatePagePDOutOfSync; /**< RC/R0: The number of times PGMInvalidatePage() was called for an out of sync page directory. */
2851 STAMCOUNTER StatRZInvalidatePageSkipped; /**< RC/R0: The number of times PGMInvalidatePage() was skipped due to not present shw or pending pending SyncCR3. */
2852 STAMCOUNTER StatRZPageOutOfSyncUser; /**< RC/R0: The number of times user page is out of sync was detected in #PF or VerifyAccessSyncPage. */
2853 STAMCOUNTER StatRZPageOutOfSyncSupervisor; /**< RC/R0: The number of times supervisor page is out of sync was detected in in #PF or VerifyAccessSyncPage. */
2854 STAMPROFILE StatRZPrefetch; /**< RC/R0: PGMPrefetchPage. */
2855 STAMPROFILE StatRZFlushTLB; /**< RC/R0: Profiling of the PGMFlushTLB() body. */
2856 STAMCOUNTER StatRZFlushTLBNewCR3; /**< RC/R0: The number of times PGMFlushTLB was called with a new CR3, non-global. (switch) */
2857 STAMCOUNTER StatRZFlushTLBNewCR3Global; /**< RC/R0: The number of times PGMFlushTLB was called with a new CR3, global. (switch) */
2858 STAMCOUNTER StatRZFlushTLBSameCR3; /**< RC/R0: The number of times PGMFlushTLB was called with the same CR3, non-global. (flush) */
2859 STAMCOUNTER StatRZFlushTLBSameCR3Global; /**< RC/R0: The number of times PGMFlushTLB was called with the same CR3, global. (flush) */
2860 STAMPROFILE StatRZGstModifyPage; /**< RC/R0: Profiling of the PGMGstModifyPage() body */
2861
2862 STAMPROFILE StatR3SyncCR3; /**< R3: PGMSyncCR3() profiling. */
2863 STAMPROFILE StatR3SyncCR3Handlers; /**< R3: Profiling of the PGMSyncCR3() update handler section. */
2864 STAMCOUNTER StatR3SyncCR3Global; /**< R3: The number of global CR3 syncs. */
2865 STAMCOUNTER StatR3SyncCR3NotGlobal; /**< R3: The number of non-global CR3 syncs. */
2866 STAMCOUNTER StatR3SyncCR3DstFreed; /**< R3: The number of times we've had to free a shadow entry. */
2867 STAMCOUNTER StatR3SyncCR3DstFreedSrcNP; /**< R3: The number of times we've had to free a shadow entry for which the source entry was not present. */
2868 STAMCOUNTER StatR3SyncCR3DstNotPresent; /**< R3: The number of times we've encountered a not present shadow entry for a present guest entry. */
2869 STAMCOUNTER StatR3SyncCR3DstSkippedGlobalPD; /**< R3: The number of times a global page directory wasn't flushed. */
2870 STAMCOUNTER StatR3SyncCR3DstSkippedGlobalPT; /**< R3: The number of times a page table with only global entries wasn't flushed. */
2871 STAMCOUNTER StatR3SyncCR3DstCacheHit; /**< R3: The number of times we got some kind of cache hit on a page table. */
2872 STAMPROFILE StatR3SyncPT; /**< R3: PGMSyncPT() profiling. */
2873 STAMCOUNTER StatR3SyncPTFailed; /**< R3: The number of times PGMSyncPT() failed. */
2874 STAMCOUNTER StatR3SyncPT4K; /**< R3: Number of 4KB syncs. */
2875 STAMCOUNTER StatR3SyncPT4M; /**< R3: Number of 4MB syncs. */
2876 STAMCOUNTER StatR3SyncPagePDNAs; /**< R3: The number of time we've marked a PD not present from SyncPage to virtualize the accessed bit. */
2877 STAMCOUNTER StatR3SyncPagePDOutOfSync; /**< R3: The number of time we've encountered an out-of-sync PD in SyncPage. */
2878 STAMCOUNTER StatR3AccessedPage; /**< R3: The number of pages marked not present for accessed bit emulation. */
2879 STAMPROFILE StatR3DirtyBitTracking; /**< R3: Profiling the dirty bit tracking in CheckPageFault(). */
2880 STAMCOUNTER StatR3DirtyPage; /**< R3: The number of pages marked read-only for dirty bit tracking. */
2881 STAMCOUNTER StatR3DirtyPageBig; /**< R3: The number of pages marked read-only for dirty bit tracking. */
2882 STAMCOUNTER StatR3DirtyPageSkipped; /**< R3: The number of pages already dirty or readonly. */
2883 STAMCOUNTER StatR3DirtyPageTrap; /**< R3: The number of traps generated for dirty bit tracking. */
2884 STAMCOUNTER StatR3DirtyTrackRealPF; /**< R3: The number of real pages faults during dirty bit tracking. */
2885 STAMCOUNTER StatR3DirtiedPage; /**< R3: The number of pages marked dirty because of write accesses. */
2886 STAMCOUNTER StatR3PageAlreadyDirty; /**< R3: The number of pages already marked dirty because of write accesses. */
2887 STAMPROFILE StatR3InvalidatePage; /**< R3: PGMInvalidatePage() profiling. */
2888 STAMCOUNTER StatR3InvalidatePage4KBPages; /**< R3: The number of times PGMInvalidatePage() was called for a 4KB page. */
2889 STAMCOUNTER StatR3InvalidatePage4MBPages; /**< R3: The number of times PGMInvalidatePage() was called for a 4MB page. */
2890 STAMCOUNTER StatR3InvalidatePage4MBPagesSkip; /**< R3: The number of times PGMInvalidatePage() skipped a 4MB page. */
2891 STAMCOUNTER StatR3InvalidatePagePDNAs; /**< R3: The number of times PGMInvalidatePage() was called for a not accessed page directory. */
2892 STAMCOUNTER StatR3InvalidatePagePDNPs; /**< R3: The number of times PGMInvalidatePage() was called for a not present page directory. */
2893 STAMCOUNTER StatR3InvalidatePagePDMappings; /**< R3: The number of times PGMInvalidatePage() was called for a page directory containing mappings (no conflict). */
2894 STAMCOUNTER StatR3InvalidatePagePDOutOfSync; /**< R3: The number of times PGMInvalidatePage() was called for an out of sync page directory. */
2895 STAMCOUNTER StatR3InvalidatePageSkipped; /**< R3: The number of times PGMInvalidatePage() was skipped due to not present shw or pending pending SyncCR3. */
2896 STAMCOUNTER StatR3PageOutOfSyncUser; /**< R3: The number of times user page is out of sync was detected in #PF or VerifyAccessSyncPage. */
2897 STAMCOUNTER StatR3PageOutOfSyncSupervisor; /**< R3: The number of times supervisor page is out of sync was detected in in #PF or VerifyAccessSyncPage. */
2898 STAMPROFILE StatR3Prefetch; /**< R3: PGMPrefetchPage. */
2899 STAMPROFILE StatR3FlushTLB; /**< R3: Profiling of the PGMFlushTLB() body. */
2900 STAMCOUNTER StatR3FlushTLBNewCR3; /**< R3: The number of times PGMFlushTLB was called with a new CR3, non-global. (switch) */
2901 STAMCOUNTER StatR3FlushTLBNewCR3Global; /**< R3: The number of times PGMFlushTLB was called with a new CR3, global. (switch) */
2902 STAMCOUNTER StatR3FlushTLBSameCR3; /**< R3: The number of times PGMFlushTLB was called with the same CR3, non-global. (flush) */
2903 STAMCOUNTER StatR3FlushTLBSameCR3Global; /**< R3: The number of times PGMFlushTLB was called with the same CR3, global. (flush) */
2904 STAMPROFILE StatR3GstModifyPage; /**< R3: Profiling of the PGMGstModifyPage() body */
2905 /** @} */
2906#endif /* VBOX_WITH_STATISTICS */
2907} PGMCPU;
2908/** Pointer to the per-cpu PGM data. */
2909typedef PGMCPU *PPGMCPU;
2910
2911
2912/** @name PGM::fSyncFlags Flags
2913 * @{
2914 */
2915/** Updates the virtual access handler state bit in PGMPAGE. */
2916#define PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL RT_BIT(0)
2917/** Always sync CR3. */
2918#define PGM_SYNC_ALWAYS RT_BIT(1)
2919/** Check monitoring on next CR3 (re)load and invalidate page.
2920 * @todo This is obsolete now. Remove after 2.2.0 is branched off. */
2921#define PGM_SYNC_MONITOR_CR3 RT_BIT(2)
2922/** Check guest mapping in SyncCR3. */
2923#define PGM_SYNC_MAP_CR3 RT_BIT(3)
2924/** Clear the page pool (a light weight flush). */
2925#define PGM_SYNC_CLEAR_PGM_POOL_BIT 8
2926#define PGM_SYNC_CLEAR_PGM_POOL RT_BIT(PGM_SYNC_CLEAR_PGM_POOL_BIT)
2927/** @} */
2928
2929
2930RT_C_DECLS_BEGIN
2931
2932int pgmLock(PVM pVM);
2933void pgmUnlock(PVM pVM);
2934
2935int pgmR3SyncPTResolveConflict(PVM pVM, PPGMMAPPING pMapping, PX86PD pPDSrc, RTGCPTR GCPtrOldMapping);
2936int pgmR3SyncPTResolveConflictPAE(PVM pVM, PPGMMAPPING pMapping, RTGCPTR GCPtrOldMapping);
2937PPGMMAPPING pgmGetMapping(PVM pVM, RTGCPTR GCPtr);
2938void pgmR3MapRelocate(PVM pVM, PPGMMAPPING pMapping, RTGCPTR GCPtrOldMapping, RTGCPTR GCPtrNewMapping);
2939DECLCALLBACK(void) pgmR3MapInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
2940
2941void pgmR3HandlerPhysicalUpdateAll(PVM pVM);
2942bool pgmHandlerPhysicalIsAll(PVM pVM, RTGCPHYS GCPhys);
2943void pgmHandlerPhysicalResetAliasedPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhysPage);
2944int pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage);
2945DECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser);
2946#if defined(VBOX_STRICT) || defined(LOG_ENABLED)
2947void pgmHandlerVirtualDumpPhysPages(PVM pVM);
2948#else
2949# define pgmHandlerVirtualDumpPhysPages(a) do { } while (0)
2950#endif
2951DECLCALLBACK(void) pgmR3InfoHandlers(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
2952
2953
2954int pgmPhysAllocPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2955int pgmPhysPageLoadIntoTlb(PPGM pPGM, RTGCPHYS GCPhys);
2956int pgmPhysPageLoadIntoTlbWithPage(PPGM pPGM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2957int pgmPhysPageMakeWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2958int pgmPhysPageMakeWritableUnlocked(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
2959int pgmPhysPageMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAP ppMap, void **ppv);
2960int pgmPhysPageMapByPageID(PVM pVM, uint32_t idPage, RTHCPHYS HCPhys, void **ppv);
2961int pgmPhysGCPhys2CCPtrInternal(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv);
2962int pgmPhysGCPhys2CCPtrInternalReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, const void **ppv);
2963VMMDECL(int) pgmPhysRomWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
2964#ifdef IN_RING3
2965void pgmR3PhysRelinkRamRanges(PVM pVM);
2966int pgmR3PhysRamPreAllocate(PVM pVM);
2967int pgmR3PhysRamReset(PVM pVM);
2968int pgmR3PhysRomReset(PVM pVM);
2969int pgmR3PhysChunkMap(PVM pVM, uint32_t idChunk, PPPGMCHUNKR3MAP ppChunk);
2970
2971int pgmR3PoolInit(PVM pVM);
2972void pgmR3PoolRelocate(PVM pVM);
2973void pgmR3PoolReset(PVM pVM);
2974
2975#endif /* IN_RING3 */
2976#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
2977int pgmR0DynMapHCPageCommon(PVM pVM, PPGMMAPSET pSet, RTHCPHYS HCPhys, void **ppv);
2978#endif
2979int pgmPoolAllocEx(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, PGMPOOLACCESS enmAccess, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage, bool fLockPage = false);
2980
2981DECLINLINE(int) pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage, bool fLockPage = false)
2982{
2983 return pgmPoolAllocEx(pVM, GCPhys, enmKind, PGMPOOLACCESS_DONTCARE, iUser, iUserTable, ppPage, fLockPage);
2984}
2985
2986void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable);
2987void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
2988int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
2989void pgmPoolClearAll(PVM pVM);
2990PPGMPOOLPAGE pgmPoolGetPage(PPGMPOOL pPool, RTHCPHYS HCPhys);
2991int pgmPoolSyncCR3(PVMCPU pVCpu);
2992int pgmPoolTrackFlushGCPhys(PVM pVM, PPGMPAGE pPhysPage, bool *pfFlushTLBs);
2993uint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT);
2994void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPoolPage, PPGMPAGE pPhysPage);
2995#ifdef PGMPOOL_WITH_MONITORING
2996void pgmPoolMonitorChainChanging(PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, CTXTYPE(RTGCPTR, RTHCPTR, RTGCPTR) pvAddress, PDISCPUSTATE pCpu);
2997int pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
2998void pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
2999#endif
3000
3001int pgmR3ExitShadowModeBeforePoolFlush(PVM pVM, PVMCPU pVCpu);
3002int pgmR3ReEnterShadowModeAfterPoolFlush(PVM pVM, PVMCPU pVCpu);
3003
3004void pgmMapSetShadowPDEs(PVM pVM, PPGMMAPPING pMap, unsigned iNewPDE);
3005void pgmMapClearShadowPDEs(PVM pVM, PPGMPOOLPAGE pShwPageCR3, PPGMMAPPING pMap, unsigned iOldPDE, bool fDeactivateCR3);
3006int pgmMapActivateCR3(PVM pVM, PPGMPOOLPAGE pShwPageCR3);
3007int pgmMapDeactivateCR3(PVM pVM, PPGMPOOLPAGE pShwPageCR3);
3008
3009int pgmShwSyncPaePDPtr(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDPE pGstPdpe, PX86PDPAE *ppPD);
3010#ifndef IN_RC
3011int pgmShwSyncLongModePDPtr(PVMCPU pVCpu, RTGCPTR64 GCPtr, PX86PML4E pGstPml4e, PX86PDPE pGstPdpe, PX86PDPAE *ppPD);
3012#endif
3013int pgmShwGetEPTPDPtr(PVMCPU pVCpu, RTGCPTR64 GCPtr, PEPTPDPT *ppPdpt, PEPTPD *ppPD);
3014
3015PX86PD pgmGstLazyMap32BitPD(PPGMCPU pPGM);
3016PX86PDPT pgmGstLazyMapPaePDPT(PPGMCPU pPGM);
3017PX86PDPAE pgmGstLazyMapPaePD(PPGMCPU pPGM, uint32_t iPdpt);
3018PX86PML4 pgmGstLazyMapPml4(PPGMCPU pPGM);
3019
3020RT_C_DECLS_END
3021
3022
3023/**
3024 * Gets the PGMRAMRANGE structure for a guest page.
3025 *
3026 * @returns Pointer to the RAM range on success.
3027 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3028 *
3029 * @param pPGM PGM handle.
3030 * @param GCPhys The GC physical address.
3031 */
3032DECLINLINE(PPGMRAMRANGE) pgmPhysGetRange(PPGM pPGM, RTGCPHYS GCPhys)
3033{
3034 /*
3035 * Optimize for the first range.
3036 */
3037 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3038 RTGCPHYS off = GCPhys - pRam->GCPhys;
3039 if (RT_UNLIKELY(off >= pRam->cb))
3040 {
3041 do
3042 {
3043 pRam = pRam->CTX_SUFF(pNext);
3044 if (RT_UNLIKELY(!pRam))
3045 break;
3046 off = GCPhys - pRam->GCPhys;
3047 } while (off >= pRam->cb);
3048 }
3049 return pRam;
3050}
3051
3052
3053/**
3054 * Gets the PGMPAGE structure for a guest page.
3055 *
3056 * @returns Pointer to the page on success.
3057 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3058 *
3059 * @param pPGM PGM handle.
3060 * @param GCPhys The GC physical address.
3061 */
3062DECLINLINE(PPGMPAGE) pgmPhysGetPage(PPGM pPGM, RTGCPHYS GCPhys)
3063{
3064 /*
3065 * Optimize for the first range.
3066 */
3067 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3068 RTGCPHYS off = GCPhys - pRam->GCPhys;
3069 if (RT_UNLIKELY(off >= pRam->cb))
3070 {
3071 do
3072 {
3073 pRam = pRam->CTX_SUFF(pNext);
3074 if (RT_UNLIKELY(!pRam))
3075 return NULL;
3076 off = GCPhys - pRam->GCPhys;
3077 } while (off >= pRam->cb);
3078 }
3079 return &pRam->aPages[off >> PAGE_SHIFT];
3080}
3081
3082
3083/**
3084 * Gets the PGMPAGE structure for a guest page.
3085 *
3086 * Old Phys code: Will make sure the page is present.
3087 *
3088 * @returns VBox status code.
3089 * @retval VINF_SUCCESS and a valid *ppPage on success.
3090 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if the address isn't valid.
3091 *
3092 * @param pPGM PGM handle.
3093 * @param GCPhys The GC physical address.
3094 * @param ppPage Where to store the page poitner on success.
3095 */
3096DECLINLINE(int) pgmPhysGetPageEx(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGE ppPage)
3097{
3098 /*
3099 * Optimize for the first range.
3100 */
3101 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3102 RTGCPHYS off = GCPhys - pRam->GCPhys;
3103 if (RT_UNLIKELY(off >= pRam->cb))
3104 {
3105 do
3106 {
3107 pRam = pRam->CTX_SUFF(pNext);
3108 if (RT_UNLIKELY(!pRam))
3109 {
3110 *ppPage = NULL; /* avoid incorrect and very annoying GCC warnings */
3111 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
3112 }
3113 off = GCPhys - pRam->GCPhys;
3114 } while (off >= pRam->cb);
3115 }
3116 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
3117 return VINF_SUCCESS;
3118}
3119
3120
3121
3122
3123/**
3124 * Gets the PGMPAGE structure for a guest page.
3125 *
3126 * Old Phys code: Will make sure the page is present.
3127 *
3128 * @returns VBox status code.
3129 * @retval VINF_SUCCESS and a valid *ppPage on success.
3130 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if the address isn't valid.
3131 *
3132 * @param pPGM PGM handle.
3133 * @param GCPhys The GC physical address.
3134 * @param ppPage Where to store the page poitner on success.
3135 * @param ppRamHint Where to read and store the ram list hint.
3136 * The caller initializes this to NULL before the call.
3137 */
3138DECLINLINE(int) pgmPhysGetPageWithHintEx(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGE ppPage, PPGMRAMRANGE *ppRamHint)
3139{
3140 RTGCPHYS off;
3141 PPGMRAMRANGE pRam = *ppRamHint;
3142 if ( !pRam
3143 || RT_UNLIKELY((off = GCPhys - pRam->GCPhys) >= pRam->cb))
3144 {
3145 pRam = pPGM->CTX_SUFF(pRamRanges);
3146 off = GCPhys - pRam->GCPhys;
3147 if (RT_UNLIKELY(off >= pRam->cb))
3148 {
3149 do
3150 {
3151 pRam = pRam->CTX_SUFF(pNext);
3152 if (RT_UNLIKELY(!pRam))
3153 {
3154 *ppPage = NULL; /* Kill the incorrect and extremely annoying GCC warnings. */
3155 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
3156 }
3157 off = GCPhys - pRam->GCPhys;
3158 } while (off >= pRam->cb);
3159 }
3160 *ppRamHint = pRam;
3161 }
3162 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
3163 return VINF_SUCCESS;
3164}
3165
3166
3167/**
3168 * Gets the PGMPAGE structure for a guest page together with the PGMRAMRANGE.
3169 *
3170 * @returns Pointer to the page on success.
3171 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3172 *
3173 * @param pPGM PGM handle.
3174 * @param GCPhys The GC physical address.
3175 * @param ppRam Where to store the pointer to the PGMRAMRANGE.
3176 */
3177DECLINLINE(PPGMPAGE) pgmPhysGetPageAndRange(PPGM pPGM, RTGCPHYS GCPhys, PPGMRAMRANGE *ppRam)
3178{
3179 /*
3180 * Optimize for the first range.
3181 */
3182 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3183 RTGCPHYS off = GCPhys - pRam->GCPhys;
3184 if (RT_UNLIKELY(off >= pRam->cb))
3185 {
3186 do
3187 {
3188 pRam = pRam->CTX_SUFF(pNext);
3189 if (RT_UNLIKELY(!pRam))
3190 return NULL;
3191 off = GCPhys - pRam->GCPhys;
3192 } while (off >= pRam->cb);
3193 }
3194 *ppRam = pRam;
3195 return &pRam->aPages[off >> PAGE_SHIFT];
3196}
3197
3198
3199/**
3200 * Gets the PGMPAGE structure for a guest page together with the PGMRAMRANGE.
3201 *
3202 * @returns Pointer to the page on success.
3203 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
3204 *
3205 * @param pPGM PGM handle.
3206 * @param GCPhys The GC physical address.
3207 * @param ppPage Where to store the pointer to the PGMPAGE structure.
3208 * @param ppRam Where to store the pointer to the PGMRAMRANGE structure.
3209 */
3210DECLINLINE(int) pgmPhysGetPageAndRangeEx(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGE ppPage, PPGMRAMRANGE *ppRam)
3211{
3212 /*
3213 * Optimize for the first range.
3214 */
3215 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3216 RTGCPHYS off = GCPhys - pRam->GCPhys;
3217 if (RT_UNLIKELY(off >= pRam->cb))
3218 {
3219 do
3220 {
3221 pRam = pRam->CTX_SUFF(pNext);
3222 if (RT_UNLIKELY(!pRam))
3223 {
3224 *ppRam = NULL; /* Shut up silly GCC warnings. */
3225 *ppPage = NULL; /* ditto */
3226 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
3227 }
3228 off = GCPhys - pRam->GCPhys;
3229 } while (off >= pRam->cb);
3230 }
3231 *ppRam = pRam;
3232 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
3233 return VINF_SUCCESS;
3234}
3235
3236
3237/**
3238 * Convert GC Phys to HC Phys.
3239 *
3240 * @returns VBox status.
3241 * @param pPGM PGM handle.
3242 * @param GCPhys The GC physical address.
3243 * @param pHCPhys Where to store the corresponding HC physical address.
3244 *
3245 * @deprecated Doesn't deal with zero, shared or write monitored pages.
3246 * Avoid when writing new code!
3247 */
3248DECLINLINE(int) pgmRamGCPhys2HCPhys(PPGM pPGM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
3249{
3250 PPGMPAGE pPage;
3251 int rc = pgmPhysGetPageEx(pPGM, GCPhys, &pPage);
3252 if (RT_FAILURE(rc))
3253 return rc;
3254 *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK);
3255 return VINF_SUCCESS;
3256}
3257
3258#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3259
3260/**
3261 * Inlined version of the ring-0 version of PGMDynMapHCPage that
3262 * optimizes access to pages already in the set.
3263 *
3264 * @returns VINF_SUCCESS. Will bail out to ring-3 on failure.
3265 * @param pPGM Pointer to the PVM instance data.
3266 * @param HCPhys The physical address of the page.
3267 * @param ppv Where to store the mapping address.
3268 */
3269DECLINLINE(int) pgmR0DynMapHCPageInlined(PPGM pPGM, RTHCPHYS HCPhys, void **ppv)
3270{
3271 PVM pVM = PGM2VM(pPGM);
3272 PPGMCPU pPGMCPU = (PPGMCPU)((uint8_t *)VMMGetCpu(pVM) + pPGM->offVCpuPGM); /* very pretty ;-) */
3273 PPGMMAPSET pSet = &pPGMCPU->AutoSet;
3274
3275 STAM_PROFILE_START(&pPGMCPU->StatR0DynMapHCPageInl, a);
3276 Assert(!(HCPhys & PAGE_OFFSET_MASK));
3277 Assert(pSet->cEntries <= RT_ELEMENTS(pSet->aEntries));
3278
3279 unsigned iHash = PGMMAPSET_HASH(HCPhys);
3280 unsigned iEntry = pSet->aiHashTable[iHash];
3281 if ( iEntry < pSet->cEntries
3282 && pSet->aEntries[iEntry].HCPhys == HCPhys)
3283 {
3284 *ppv = pSet->aEntries[iEntry].pvPage;
3285 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapHCPageInlHits);
3286 }
3287 else
3288 {
3289 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapHCPageInlMisses);
3290 pgmR0DynMapHCPageCommon(pVM, pSet, HCPhys, ppv);
3291 }
3292
3293 STAM_PROFILE_STOP(&pPGMCPU->StatR0DynMapHCPageInl, a);
3294 return VINF_SUCCESS;
3295}
3296
3297
3298/**
3299 * Inlined version of the ring-0 version of PGMDynMapGCPage that optimizes
3300 * access to pages already in the set.
3301 *
3302 * @returns See PGMDynMapGCPage.
3303 * @param pPGM Pointer to the PVM instance data.
3304 * @param HCPhys The physical address of the page.
3305 * @param ppv Where to store the mapping address.
3306 */
3307DECLINLINE(int) pgmR0DynMapGCPageInlined(PPGM pPGM, RTGCPHYS GCPhys, void **ppv)
3308{
3309 PVM pVM = PGM2VM(pPGM);
3310 PPGMCPU pPGMCPU = (PPGMCPU)((uint8_t *)VMMGetCpu(pVM) + pPGM->offVCpuPGM); /* very pretty ;-) */
3311
3312 STAM_PROFILE_START(&pPGMCPU->StatR0DynMapGCPageInl, a);
3313 Assert(!(GCPhys & PAGE_OFFSET_MASK));
3314
3315 /*
3316 * Get the ram range.
3317 */
3318 PPGMRAMRANGE pRam = pPGM->CTX_SUFF(pRamRanges);
3319 RTGCPHYS off = GCPhys - pRam->GCPhys;
3320 if (RT_UNLIKELY(off >= pRam->cb
3321 /** @todo || page state stuff */))
3322 {
3323 /* This case is not counted into StatR0DynMapGCPageInl. */
3324 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlRamMisses);
3325 return PGMDynMapGCPage(pVM, GCPhys, ppv);
3326 }
3327
3328 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[off >> PAGE_SHIFT]);
3329 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlRamHits);
3330
3331 /*
3332 * pgmR0DynMapHCPageInlined with out stats.
3333 */
3334 PPGMMAPSET pSet = &pPGMCPU->AutoSet;
3335 Assert(!(HCPhys & PAGE_OFFSET_MASK));
3336 Assert(pSet->cEntries <= RT_ELEMENTS(pSet->aEntries));
3337
3338 unsigned iHash = PGMMAPSET_HASH(HCPhys);
3339 unsigned iEntry = pSet->aiHashTable[iHash];
3340 if ( iEntry < pSet->cEntries
3341 && pSet->aEntries[iEntry].HCPhys == HCPhys)
3342 {
3343 *ppv = pSet->aEntries[iEntry].pvPage;
3344 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlHits);
3345 }
3346 else
3347 {
3348 STAM_COUNTER_INC(&pPGMCPU->StatR0DynMapGCPageInlMisses);
3349 pgmR0DynMapHCPageCommon(pVM, pSet, HCPhys, ppv);
3350 }
3351
3352 STAM_PROFILE_STOP(&pPGMCPU->StatR0DynMapGCPageInl, a);
3353 return VINF_SUCCESS;
3354}
3355
3356#endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
3357#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
3358
3359/**
3360 * Maps the page into current context (RC and maybe R0).
3361 *
3362 * @returns pointer to the mapping.
3363 * @param pVM Pointer to the PGM instance data.
3364 * @param pPage The page.
3365 */
3366DECLINLINE(void *) pgmPoolMapPageInlined(PPGM pPGM, PPGMPOOLPAGE pPage)
3367{
3368 if (pPage->idx >= PGMPOOL_IDX_FIRST)
3369 {
3370 Assert(pPage->idx < pPGM->CTX_SUFF(pPool)->cCurPages);
3371 void *pv;
3372# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3373 pgmR0DynMapHCPageInlined(pPGM, pPage->Core.Key, &pv);
3374# else
3375 PGMDynMapHCPage(PGM2VM(pPGM), pPage->Core.Key, &pv);
3376# endif
3377 return pv;
3378 }
3379 AssertFatalMsgFailed(("pgmPoolMapPageInlined invalid page index %x\n", pPage->idx));
3380}
3381
3382/**
3383 * Temporarily maps one host page specified by HC physical address, returning
3384 * pointer within the page.
3385 *
3386 * Be WARNED that the dynamic page mapping area is small, 8 pages, thus the space is
3387 * reused after 8 mappings (or perhaps a few more if you score with the cache).
3388 *
3389 * @returns The address corresponding to HCPhys.
3390 * @param pPGM Pointer to the PVM instance data.
3391 * @param HCPhys HC Physical address of the page.
3392 */
3393DECLINLINE(void *) pgmDynMapHCPageOff(PPGM pPGM, RTHCPHYS HCPhys)
3394{
3395 void *pv;
3396# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3397 pgmR0DynMapHCPageInlined(pPGM, HCPhys & ~(RTHCPHYS)PAGE_OFFSET_MASK, &pv);
3398# else
3399 PGMDynMapHCPage(PGM2VM(pPGM), HCPhys & ~(RTHCPHYS)PAGE_OFFSET_MASK, &pv);
3400# endif
3401 pv = (void *)((uintptr_t)pv | (HCPhys & PAGE_OFFSET_MASK));
3402 return pv;
3403}
3404
3405#endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 || IN_RC */
3406#ifndef IN_RC
3407
3408/**
3409 * Queries the Physical TLB entry for a physical guest page,
3410 * attempting to load the TLB entry if necessary.
3411 *
3412 * @returns VBox status code.
3413 * @retval VINF_SUCCESS on success
3414 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
3415 *
3416 * @param pPGM The PGM instance handle.
3417 * @param GCPhys The address of the guest page.
3418 * @param ppTlbe Where to store the pointer to the TLB entry.
3419 */
3420DECLINLINE(int) pgmPhysPageQueryTlbe(PPGM pPGM, RTGCPHYS GCPhys, PPPGMPAGEMAPTLBE ppTlbe)
3421{
3422 int rc;
3423 PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
3424 if (pTlbe->GCPhys == (GCPhys & X86_PTE_PAE_PG_MASK))
3425 {
3426 STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbHits));
3427 rc = VINF_SUCCESS;
3428 }
3429 else
3430 rc = pgmPhysPageLoadIntoTlb(pPGM, GCPhys);
3431 *ppTlbe = pTlbe;
3432 return rc;
3433}
3434
3435
3436/**
3437 * Queries the Physical TLB entry for a physical guest page,
3438 * attempting to load the TLB entry if necessary.
3439 *
3440 * @returns VBox status code.
3441 * @retval VINF_SUCCESS on success
3442 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
3443 *
3444 * @param pPGM The PGM instance handle.
3445 * @param pPage Pointer to the PGMPAGE structure corresponding to
3446 * GCPhys.
3447 * @param GCPhys The address of the guest page.
3448 * @param ppTlbe Where to store the pointer to the TLB entry.
3449 */
3450DECLINLINE(int) pgmPhysPageQueryTlbeWithPage(PPGM pPGM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAPTLBE ppTlbe)
3451{
3452 int rc;
3453 PPGMPAGEMAPTLBE pTlbe = &pPGM->CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
3454 if (pTlbe->GCPhys == (GCPhys & X86_PTE_PAE_PG_MASK))
3455 {
3456 STAM_COUNTER_INC(&pPGM->CTX_MID_Z(Stat,PageMapTlbHits));
3457 rc = VINF_SUCCESS;
3458 }
3459 else
3460 rc = pgmPhysPageLoadIntoTlbWithPage(pPGM, pPage, GCPhys);
3461 *ppTlbe = pTlbe;
3462 return rc;
3463}
3464
3465#endif /* !IN_RC */
3466
3467/**
3468 * Calculated the guest physical address of the large (4 MB) page in 32 bits paging mode.
3469 * Takes PSE-36 into account.
3470 *
3471 * @returns guest physical address
3472 * @param pPGM Pointer to the PGM instance data.
3473 * @param Pde Guest Pde
3474 */
3475DECLINLINE(RTGCPHYS) pgmGstGet4MBPhysPage(PPGM pPGM, X86PDE Pde)
3476{
3477 RTGCPHYS GCPhys = Pde.u & X86_PDE4M_PG_MASK;
3478 GCPhys |= (RTGCPHYS)Pde.b.u8PageNoHigh << 32;
3479
3480 return GCPhys & pPGM->GCPhys4MBPSEMask;
3481}
3482
3483
3484/**
3485 * Gets the page directory entry for the specified address (32-bit paging).
3486 *
3487 * @returns The page directory entry in question.
3488 * @param pPGM Pointer to the PGM instance data.
3489 * @param GCPtr The address.
3490 */
3491DECLINLINE(X86PDE) pgmGstGet32bitPDE(PPGMCPU pPGM, RTGCPTR GCPtr)
3492{
3493#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3494 PCX86PD pGuestPD = NULL;
3495 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPD);
3496 if (RT_FAILURE(rc))
3497 {
3498 X86PDE ZeroPde = {0};
3499 AssertMsgFailedReturn(("%Rrc\n", rc), ZeroPde);
3500 }
3501#else
3502 PX86PD pGuestPD = pPGM->CTX_SUFF(pGst32BitPd);
3503# ifdef IN_RING3
3504 if (!pGuestPD)
3505 pGuestPD = pgmGstLazyMap32BitPD(pPGM);
3506# endif
3507#endif
3508 return pGuestPD->a[GCPtr >> X86_PD_SHIFT];
3509}
3510
3511
3512/**
3513 * Gets the address of a specific page directory entry (32-bit paging).
3514 *
3515 * @returns Pointer the page directory entry in question.
3516 * @param pPGM Pointer to the PGM instance data.
3517 * @param GCPtr The address.
3518 */
3519DECLINLINE(PX86PDE) pgmGstGet32bitPDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
3520{
3521#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3522 PX86PD pGuestPD = NULL;
3523 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPD);
3524 AssertRCReturn(rc, NULL);
3525#else
3526 PX86PD pGuestPD = pPGM->CTX_SUFF(pGst32BitPd);
3527# ifdef IN_RING3
3528 if (!pGuestPD)
3529 pGuestPD = pgmGstLazyMap32BitPD(pPGM);
3530# endif
3531#endif
3532 return &pGuestPD->a[GCPtr >> X86_PD_SHIFT];
3533}
3534
3535
3536/**
3537 * Gets the address the guest page directory (32-bit paging).
3538 *
3539 * @returns Pointer the page directory entry in question.
3540 * @param pPGM Pointer to the PGM instance data.
3541 */
3542DECLINLINE(PX86PD) pgmGstGet32bitPDPtr(PPGMCPU pPGM)
3543{
3544#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3545 PX86PD pGuestPD = NULL;
3546 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPD);
3547 AssertRCReturn(rc, NULL);
3548#else
3549 PX86PD pGuestPD = pPGM->CTX_SUFF(pGst32BitPd);
3550# ifdef IN_RING3
3551 if (!pGuestPD)
3552 pGuestPD = pgmGstLazyMap32BitPD(pPGM);
3553# endif
3554#endif
3555 return pGuestPD;
3556}
3557
3558
3559/**
3560 * Gets the guest page directory pointer table.
3561 *
3562 * @returns Pointer to the page directory in question.
3563 * @returns NULL if the page directory is not present or on an invalid page.
3564 * @param pPGM Pointer to the PGM instance data.
3565 */
3566DECLINLINE(PX86PDPT) pgmGstGetPaePDPTPtr(PPGMCPU pPGM)
3567{
3568#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3569 PX86PDPT pGuestPDPT = NULL;
3570 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPDPT);
3571 AssertRCReturn(rc, NULL);
3572#else
3573 PX86PDPT pGuestPDPT = pPGM->CTX_SUFF(pGstPaePdpt);
3574# ifdef IN_RING3
3575 if (!pGuestPDPT)
3576 pGuestPDPT = pgmGstLazyMapPaePDPT(pPGM);
3577# endif
3578#endif
3579 return pGuestPDPT;
3580}
3581
3582
3583/**
3584 * Gets the guest page directory pointer table entry for the specified address.
3585 *
3586 * @returns Pointer to the page directory in question.
3587 * @returns NULL if the page directory is not present or on an invalid page.
3588 * @param pPGM Pointer to the PGM instance data.
3589 * @param GCPtr The address.
3590 */
3591DECLINLINE(PX86PDPE) pgmGstGetPaePDPEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
3592{
3593 AssertGCPtr32(GCPtr);
3594
3595#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3596 PX86PDPT pGuestPDPT = 0;
3597 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPDPT);
3598 AssertRCReturn(rc, 0);
3599#else
3600 PX86PDPT pGuestPDPT = pPGM->CTX_SUFF(pGstPaePdpt);
3601# ifdef IN_RING3
3602 if (!pGuestPDPT)
3603 pGuestPDPT = pgmGstLazyMapPaePDPT(pPGM);
3604# endif
3605#endif
3606 return &pGuestPDPT->a[(GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE];
3607}
3608
3609
3610/**
3611 * Gets the page directory for the specified address.
3612 *
3613 * @returns Pointer to the page directory in question.
3614 * @returns NULL if the page directory is not present or on an invalid page.
3615 * @param pPGM Pointer to the PGM instance data.
3616 * @param GCPtr The address.
3617 */
3618DECLINLINE(PX86PDPAE) pgmGstGetPaePD(PPGMCPU pPGM, RTGCPTR GCPtr)
3619{
3620 AssertGCPtr32(GCPtr);
3621
3622 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3623 AssertReturn(pGuestPDPT, NULL);
3624 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3625 if (pGuestPDPT->a[iPdpt].n.u1Present)
3626 {
3627#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3628 PX86PDPAE pGuestPD = NULL;
3629 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3630 AssertRCReturn(rc, NULL);
3631#else
3632 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3633 if ( !pGuestPD
3634 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3635 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3636#endif
3637 return pGuestPD;
3638 /* returning NULL is ok if we assume it's just an invalid page of some kind emulated as all 0s. (not quite true) */
3639 }
3640 return NULL;
3641}
3642
3643
3644/**
3645 * Gets the page directory entry for the specified address.
3646 *
3647 * @returns Pointer to the page directory entry in question.
3648 * @returns NULL if the page directory is not present or on an invalid page.
3649 * @param pPGM Pointer to the PGM instance data.
3650 * @param GCPtr The address.
3651 */
3652DECLINLINE(PX86PDEPAE) pgmGstGetPaePDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
3653{
3654 AssertGCPtr32(GCPtr);
3655
3656 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3657 AssertReturn(pGuestPDPT, NULL);
3658 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3659 if (pGuestPDPT->a[iPdpt].n.u1Present)
3660 {
3661 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3662#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3663 PX86PDPAE pGuestPD = NULL;
3664 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3665 AssertRCReturn(rc, NULL);
3666#else
3667 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3668 if ( !pGuestPD
3669 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3670 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3671#endif
3672 return &pGuestPD->a[iPD];
3673 /* 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) */
3674 }
3675 return NULL;
3676}
3677
3678
3679/**
3680 * Gets the page directory entry for the specified address.
3681 *
3682 * @returns The page directory entry in question.
3683 * @returns A non-present entry if the page directory is not present or on an invalid page.
3684 * @param pPGM Pointer to the PGM instance data.
3685 * @param GCPtr The address.
3686 */
3687DECLINLINE(X86PDEPAE) pgmGstGetPaePDE(PPGMCPU pPGM, RTGCPTR GCPtr)
3688{
3689 AssertGCPtr32(GCPtr);
3690 X86PDEPAE ZeroPde = {0};
3691 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3692 if (RT_LIKELY(pGuestPDPT))
3693 {
3694 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3695 if (pGuestPDPT->a[iPdpt].n.u1Present)
3696 {
3697 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3698#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3699 PX86PDPAE pGuestPD = NULL;
3700 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3701 AssertRCReturn(rc, ZeroPde);
3702#else
3703 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3704 if ( !pGuestPD
3705 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3706 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3707#endif
3708 return pGuestPD->a[iPD];
3709 }
3710 }
3711 return ZeroPde;
3712}
3713
3714
3715/**
3716 * Gets the page directory pointer table entry for the specified address
3717 * and returns the index into the page directory
3718 *
3719 * @returns Pointer to the page directory in question.
3720 * @returns NULL if the page directory is not present or on an invalid page.
3721 * @param pPGM Pointer to the PGM instance data.
3722 * @param GCPtr The address.
3723 * @param piPD Receives the index into the returned page directory
3724 * @param pPdpe Receives the page directory pointer entry. Optional.
3725 */
3726DECLINLINE(PX86PDPAE) pgmGstGetPaePDPtr(PPGMCPU pPGM, RTGCPTR GCPtr, unsigned *piPD, PX86PDPE pPdpe)
3727{
3728 AssertGCPtr32(GCPtr);
3729
3730 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pPGM);
3731 AssertReturn(pGuestPDPT, NULL);
3732 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
3733 if (pPdpe)
3734 *pPdpe = pGuestPDPT->a[iPdpt];
3735 if (pGuestPDPT->a[iPdpt].n.u1Present)
3736 {
3737 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3738#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3739 PX86PDPAE pGuestPD = NULL;
3740 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK, (void **)&pGuestPD);
3741 AssertRCReturn(rc, NULL);
3742#else
3743 PX86PDPAE pGuestPD = pPGM->CTX_SUFF(apGstPaePDs)[iPdpt];
3744 if ( !pGuestPD
3745 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pPGM->aGCPhysGstPaePDs[iPdpt])
3746 pGuestPD = pgmGstLazyMapPaePD(pPGM, iPdpt);
3747#endif
3748 *piPD = iPD;
3749 return pGuestPD;
3750 /* returning NIL_RTGCPHYS is ok if we assume it's just an invalid page of some kind emulated as all 0s. */
3751 }
3752 return NULL;
3753}
3754
3755#ifndef IN_RC
3756
3757/**
3758 * Gets the page map level-4 pointer for the guest.
3759 *
3760 * @returns Pointer to the PML4 page.
3761 * @param pPGM Pointer to the PGM instance data.
3762 */
3763DECLINLINE(PX86PML4) pgmGstGetLongModePML4Ptr(PPGMCPU pPGM)
3764{
3765#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3766 PX86PML4 pGuestPml4;
3767 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPml4);
3768 AssertRCReturn(rc, NULL);
3769#else
3770 PX86PML4 pGuestPml4 = pPGM->CTX_SUFF(pGstAmd64Pml4);
3771# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R3
3772 if (!pGuestPml4)
3773 pGuestPml4 = pgmGstLazyMapPml4(pPGM);
3774# endif
3775 Assert(pGuestPml4);
3776#endif
3777 return pGuestPml4;
3778}
3779
3780
3781/**
3782 * Gets the pointer to a page map level-4 entry.
3783 *
3784 * @returns Pointer to the PML4 entry.
3785 * @param pPGM Pointer to the PGM instance data.
3786 * @param iPml4 The index.
3787 */
3788DECLINLINE(PX86PML4E) pgmGstGetLongModePML4EPtr(PPGMCPU pPGM, unsigned int iPml4)
3789{
3790#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3791 PX86PML4 pGuestPml4;
3792 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPml4);
3793 AssertRCReturn(rc, NULL);
3794#else
3795 PX86PML4 pGuestPml4 = pPGM->CTX_SUFF(pGstAmd64Pml4);
3796# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R3
3797 if (!pGuestPml4)
3798 pGuestPml4 = pgmGstLazyMapPml4(pPGM);
3799# endif
3800 Assert(pGuestPml4);
3801#endif
3802 return &pGuestPml4->a[iPml4];
3803}
3804
3805
3806/**
3807 * Gets a page map level-4 entry.
3808 *
3809 * @returns The PML4 entry.
3810 * @param pPGM Pointer to the PGM instance data.
3811 * @param iPml4 The index.
3812 */
3813DECLINLINE(X86PML4E) pgmGstGetLongModePML4E(PPGMCPU pPGM, unsigned int iPml4)
3814{
3815#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
3816 PX86PML4 pGuestPml4;
3817 int rc = pgmR0DynMapGCPageInlined(PGMCPU2PGM(pPGM), pPGM->GCPhysCR3, (void **)&pGuestPml4);
3818 if (RT_FAILURE(rc))
3819 {
3820 X86PML4E ZeroPml4e = {0};
3821 AssertMsgFailedReturn(("%Rrc\n", rc), ZeroPml4e);
3822 }
3823#else
3824 PX86PML4 pGuestPml4 = pPGM->CTX_SUFF(pGstAmd64Pml4);
3825# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R3
3826 if (!pGuestPml4)
3827 pGuestPml4 = pgmGstLazyMapPml4(pPGM);
3828# endif
3829 Assert(pGuestPml4);
3830#endif
3831 return pGuestPml4->a[iPml4];
3832}
3833
3834
3835/**
3836 * Gets the page directory pointer entry for the specified address.
3837 *
3838 * @returns Pointer to the page directory pointer entry in question.
3839 * @returns NULL if the page directory is not present or on an invalid page.
3840 * @param pPGM Pointer to the PGM instance data.
3841 * @param GCPtr The address.
3842 * @param ppPml4e Page Map Level-4 Entry (out)
3843 */
3844DECLINLINE(PX86PDPE) pgmGstGetLongModePDPTPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e)
3845{
3846 PX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3847 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3848 PCX86PML4E pPml4e = *ppPml4e = &pGuestPml4->a[iPml4];
3849 if (pPml4e->n.u1Present)
3850 {
3851 PX86PDPT pPdpt;
3852 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPml4e->u & X86_PML4E_PG_MASK, &pPdpt);
3853 AssertRCReturn(rc, NULL);
3854
3855 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3856 return &pPdpt->a[iPdpt];
3857 }
3858 return NULL;
3859}
3860
3861
3862/**
3863 * Gets the page directory entry for the specified address.
3864 *
3865 * @returns The page directory entry in question.
3866 * @returns A non-present entry if the page directory is not present or on an invalid page.
3867 * @param pPGM Pointer to the PGM instance data.
3868 * @param GCPtr The address.
3869 * @param ppPml4e Page Map Level-4 Entry (out)
3870 * @param pPdpe Page directory pointer table entry (out)
3871 */
3872DECLINLINE(X86PDEPAE) pgmGstGetLongModePDEEx(PPGMCPU pPGM, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPE pPdpe)
3873{
3874 X86PDEPAE ZeroPde = {0};
3875 PX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3876 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3877 PCX86PML4E pPml4e = *ppPml4e = &pGuestPml4->a[iPml4];
3878 if (pPml4e->n.u1Present)
3879 {
3880 PCX86PDPT pPdptTemp;
3881 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPml4e->u & X86_PML4E_PG_MASK, &pPdptTemp);
3882 AssertRCReturn(rc, ZeroPde);
3883
3884 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3885 *pPdpe = pPdptTemp->a[iPdpt];
3886 if (pPdptTemp->a[iPdpt].n.u1Present)
3887 {
3888 PCX86PDPAE pPD;
3889 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3890 AssertRCReturn(rc, ZeroPde);
3891
3892 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3893 return pPD->a[iPD];
3894 }
3895 }
3896
3897 return ZeroPde;
3898}
3899
3900
3901/**
3902 * Gets the page directory entry for the specified address.
3903 *
3904 * @returns The page directory entry in question.
3905 * @returns A non-present entry if the page directory is not present or on an invalid page.
3906 * @param pPGM Pointer to the PGM instance data.
3907 * @param GCPtr The address.
3908 */
3909DECLINLINE(X86PDEPAE) pgmGstGetLongModePDE(PPGMCPU pPGM, RTGCPTR64 GCPtr)
3910{
3911 X86PDEPAE ZeroPde = {0};
3912 PCX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3913 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3914 if (pGuestPml4->a[iPml4].n.u1Present)
3915 {
3916 PCX86PDPT pPdptTemp;
3917 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pGuestPml4->a[iPml4].u & X86_PML4E_PG_MASK, &pPdptTemp);
3918 AssertRCReturn(rc, ZeroPde);
3919
3920 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3921 if (pPdptTemp->a[iPdpt].n.u1Present)
3922 {
3923 PCX86PDPAE pPD;
3924 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3925 AssertRCReturn(rc, ZeroPde);
3926
3927 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3928 return pPD->a[iPD];
3929 }
3930 }
3931 return ZeroPde;
3932}
3933
3934
3935/**
3936 * Gets the page directory entry for the specified address.
3937 *
3938 * @returns Pointer to the page directory entry in question.
3939 * @returns NULL if the page directory is not present or on an invalid page.
3940 * @param pPGM Pointer to the PGM instance data.
3941 * @param GCPtr The address.
3942 */
3943DECLINLINE(PX86PDEPAE) pgmGstGetLongModePDEPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr)
3944{
3945 PCX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3946 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3947 if (pGuestPml4->a[iPml4].n.u1Present)
3948 {
3949 PCX86PDPT pPdptTemp;
3950 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pGuestPml4->a[iPml4].u & X86_PML4E_PG_MASK, &pPdptTemp);
3951 AssertRCReturn(rc, NULL);
3952
3953 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3954 if (pPdptTemp->a[iPdpt].n.u1Present)
3955 {
3956 PX86PDPAE pPD;
3957 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3958 AssertRCReturn(rc, NULL);
3959
3960 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3961 return &pPD->a[iPD];
3962 }
3963 }
3964 return NULL;
3965}
3966
3967
3968/**
3969 * Gets the GUEST page directory pointer for the specified address.
3970 *
3971 * @returns The page directory in question.
3972 * @returns NULL if the page directory is not present or on an invalid page.
3973 * @param pPGM Pointer to the PGM instance data.
3974 * @param GCPtr The address.
3975 * @param ppPml4e Page Map Level-4 Entry (out)
3976 * @param pPdpe Page directory pointer table entry (out)
3977 * @param piPD Receives the index into the returned page directory
3978 */
3979DECLINLINE(PX86PDPAE) pgmGstGetLongModePDPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPE pPdpe, unsigned *piPD)
3980{
3981 PX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
3982 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
3983 PCX86PML4E pPml4e = *ppPml4e = &pGuestPml4->a[iPml4];
3984 if (pPml4e->n.u1Present)
3985 {
3986 PCX86PDPT pPdptTemp;
3987 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPml4e->u & X86_PML4E_PG_MASK, &pPdptTemp);
3988 AssertRCReturn(rc, NULL);
3989
3990 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
3991 *pPdpe = pPdptTemp->a[iPdpt];
3992 if (pPdptTemp->a[iPdpt].n.u1Present)
3993 {
3994 PX86PDPAE pPD;
3995 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
3996 AssertRCReturn(rc, NULL);
3997
3998 *piPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
3999 return pPD;
4000 }
4001 }
4002 return 0;
4003}
4004
4005#endif /* !IN_RC */
4006
4007/**
4008 * Gets the shadow page directory, 32-bit.
4009 *
4010 * @returns Pointer to the shadow 32-bit PD.
4011 * @param pPGM Pointer to the PGM instance data.
4012 */
4013DECLINLINE(PX86PD) pgmShwGet32BitPDPtr(PPGMCPU pPGM)
4014{
4015 return (PX86PD)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPGM->CTX_SUFF(pShwPageCR3));
4016}
4017
4018
4019/**
4020 * Gets the shadow page directory entry for the specified address, 32-bit.
4021 *
4022 * @returns Shadow 32-bit PDE.
4023 * @param pPGM Pointer to the PGM instance data.
4024 * @param GCPtr The address.
4025 */
4026DECLINLINE(X86PDE) pgmShwGet32BitPDE(PPGMCPU pPGM, RTGCPTR GCPtr)
4027{
4028 const unsigned iPd = (GCPtr >> X86_PD_SHIFT) & X86_PD_MASK;
4029
4030 PX86PD pShwPde = pgmShwGet32BitPDPtr(pPGM);
4031 if (!pShwPde)
4032 {
4033 X86PDE ZeroPde = {0};
4034 return ZeroPde;
4035 }
4036 return pShwPde->a[iPd];
4037}
4038
4039
4040/**
4041 * Gets the pointer to the shadow page directory entry for the specified
4042 * address, 32-bit.
4043 *
4044 * @returns Pointer to the shadow 32-bit PDE.
4045 * @param pPGM Pointer to the PGM instance data.
4046 * @param GCPtr The address.
4047 */
4048DECLINLINE(PX86PDE) pgmShwGet32BitPDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
4049{
4050 const unsigned iPd = (GCPtr >> X86_PD_SHIFT) & X86_PD_MASK;
4051
4052 PX86PD pPde = pgmShwGet32BitPDPtr(pPGM);
4053 AssertReturn(pPde, NULL);
4054 return &pPde->a[iPd];
4055}
4056
4057
4058/**
4059 * Gets the shadow page pointer table, PAE.
4060 *
4061 * @returns Pointer to the shadow PAE PDPT.
4062 * @param pPGM Pointer to the PGM instance data.
4063 */
4064DECLINLINE(PX86PDPT) pgmShwGetPaePDPTPtr(PPGMCPU pPGM)
4065{
4066 return (PX86PDPT)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPGM->CTX_SUFF(pShwPageCR3));
4067}
4068
4069
4070/**
4071 * Gets the shadow page directory for the specified address, PAE.
4072 *
4073 * @returns Pointer to the shadow PD.
4074 * @param pPGM Pointer to the PGM instance data.
4075 * @param GCPtr The address.
4076 */
4077DECLINLINE(PX86PDPAE) pgmShwGetPaePDPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
4078{
4079 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
4080 PX86PDPT pPdpt = pgmShwGetPaePDPTPtr(pPGM);
4081
4082 if (!pPdpt->a[iPdpt].n.u1Present)
4083 return NULL;
4084
4085 /* Fetch the pgm pool shadow descriptor. */
4086 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(PGMCPU2PGM(pPGM)->CTX_SUFF(pPool), pPdpt->a[iPdpt].u & X86_PDPE_PG_MASK);
4087 AssertReturn(pShwPde, NULL);
4088
4089 return (PX86PDPAE)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pShwPde);
4090}
4091
4092
4093/**
4094 * Gets the shadow page directory for the specified address, PAE.
4095 *
4096 * @returns Pointer to the shadow PD.
4097 * @param pPGM Pointer to the PGM instance data.
4098 * @param GCPtr The address.
4099 */
4100DECLINLINE(PX86PDPAE) pgmShwGetPaePDPtr(PPGMCPU pPGM, PX86PDPT pPdpt, RTGCPTR GCPtr)
4101{
4102 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
4103
4104 if (!pPdpt->a[iPdpt].n.u1Present)
4105 return NULL;
4106
4107 /* Fetch the pgm pool shadow descriptor. */
4108 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(PGMCPU2PGM(pPGM)->CTX_SUFF(pPool), pPdpt->a[iPdpt].u & X86_PDPE_PG_MASK);
4109 AssertReturn(pShwPde, NULL);
4110
4111 return (PX86PDPAE)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pShwPde);
4112}
4113
4114
4115/**
4116 * Gets the shadow page directory entry, PAE.
4117 *
4118 * @returns PDE.
4119 * @param pPGM Pointer to the PGM instance data.
4120 * @param GCPtr The address.
4121 */
4122DECLINLINE(X86PDEPAE) pgmShwGetPaePDE(PPGMCPU pPGM, RTGCPTR GCPtr)
4123{
4124 const unsigned iPd = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
4125
4126 PX86PDPAE pShwPde = pgmShwGetPaePDPtr(pPGM, GCPtr);
4127 if (!pShwPde)
4128 {
4129 X86PDEPAE ZeroPde = {0};
4130 return ZeroPde;
4131 }
4132 return pShwPde->a[iPd];
4133}
4134
4135
4136/**
4137 * Gets the pointer to the shadow page directory entry for an address, PAE.
4138 *
4139 * @returns Pointer to the PDE.
4140 * @param pPGM Pointer to the PGM instance data.
4141 * @param GCPtr The address.
4142 */
4143DECLINLINE(PX86PDEPAE) pgmShwGetPaePDEPtr(PPGMCPU pPGM, RTGCPTR GCPtr)
4144{
4145 const unsigned iPd = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
4146
4147 PX86PDPAE pPde = pgmShwGetPaePDPtr(pPGM, GCPtr);
4148 AssertReturn(pPde, NULL);
4149 return &pPde->a[iPd];
4150}
4151
4152#ifndef IN_RC
4153
4154/**
4155 * Gets the shadow page map level-4 pointer.
4156 *
4157 * @returns Pointer to the shadow PML4.
4158 * @param pPGM Pointer to the PGM instance data.
4159 */
4160DECLINLINE(PX86PML4) pgmShwGetLongModePML4Ptr(PPGMCPU pPGM)
4161{
4162 return (PX86PML4)PGMPOOL_PAGE_2_PTR_BY_PGMCPU(pPGM, pPGM->CTX_SUFF(pShwPageCR3));
4163}
4164
4165
4166/**
4167 * Gets the shadow page map level-4 entry for the specified address.
4168 *
4169 * @returns The entry.
4170 * @param pPGM Pointer to the PGM instance data.
4171 * @param GCPtr The address.
4172 */
4173DECLINLINE(X86PML4E) pgmShwGetLongModePML4E(PPGMCPU pPGM, RTGCPTR GCPtr)
4174{
4175 const unsigned iPml4 = ((RTGCUINTPTR64)GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
4176 PX86PML4 pShwPml4 = pgmShwGetLongModePML4Ptr(pPGM);
4177
4178 if (!pShwPml4)
4179 {
4180 X86PML4E ZeroPml4e = {0};
4181 return ZeroPml4e;
4182 }
4183 return pShwPml4->a[iPml4];
4184}
4185
4186
4187/**
4188 * Gets the pointer to the specified shadow page map level-4 entry.
4189 *
4190 * @returns The entry.
4191 * @param pPGM Pointer to the PGM instance data.
4192 * @param iPml4 The PML4 index.
4193 */
4194DECLINLINE(PX86PML4E) pgmShwGetLongModePML4EPtr(PPGMCPU pPGM, unsigned int iPml4)
4195{
4196 PX86PML4 pShwPml4 = pgmShwGetLongModePML4Ptr(pPGM);
4197 if (!pShwPml4)
4198 return NULL;
4199 return &pShwPml4->a[iPml4];
4200}
4201
4202
4203/**
4204 * Gets the GUEST page directory pointer for the specified address.
4205 *
4206 * @returns The page directory in question.
4207 * @returns NULL if the page directory is not present or on an invalid page.
4208 * @param pPGM Pointer to the PGM instance data.
4209 * @param GCPtr The address.
4210 * @param piPD Receives the index into the returned page directory
4211 */
4212DECLINLINE(PX86PDPAE) pgmGstGetLongModePDPtr(PPGMCPU pPGM, RTGCPTR64 GCPtr, unsigned *piPD)
4213{
4214 PCX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pPGM);
4215 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
4216 if (pGuestPml4->a[iPml4].n.u1Present)
4217 {
4218 PCX86PDPT pPdptTemp;
4219 int rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pGuestPml4->a[iPml4].u & X86_PML4E_PG_MASK, &pPdptTemp);
4220 AssertRCReturn(rc, NULL);
4221
4222 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
4223 if (pPdptTemp->a[iPdpt].n.u1Present)
4224 {
4225 PX86PDPAE pPD;
4226 rc = PGM_GCPHYS_2_PTR_BY_PGMCPU(pPGM, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
4227 AssertRCReturn(rc, NULL);
4228
4229 *piPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
4230 return pPD;
4231 }
4232 }
4233 return NULL;
4234}
4235
4236#endif /* !IN_RC */
4237
4238/**
4239 * Gets the page state for a physical handler.
4240 *
4241 * @returns The physical handler page state.
4242 * @param pCur The physical handler in question.
4243 */
4244DECLINLINE(unsigned) pgmHandlerPhysicalCalcState(PPGMPHYSHANDLER pCur)
4245{
4246 switch (pCur->enmType)
4247 {
4248 case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE:
4249 return PGM_PAGE_HNDL_PHYS_STATE_WRITE;
4250
4251 case PGMPHYSHANDLERTYPE_MMIO:
4252 case PGMPHYSHANDLERTYPE_PHYSICAL_ALL:
4253 return PGM_PAGE_HNDL_PHYS_STATE_ALL;
4254
4255 default:
4256 AssertFatalMsgFailed(("Invalid type %d\n", pCur->enmType));
4257 }
4258}
4259
4260
4261/**
4262 * Gets the page state for a virtual handler.
4263 *
4264 * @returns The virtual handler page state.
4265 * @param pCur The virtual handler in question.
4266 * @remarks This should never be used on a hypervisor access handler.
4267 */
4268DECLINLINE(unsigned) pgmHandlerVirtualCalcState(PPGMVIRTHANDLER pCur)
4269{
4270 switch (pCur->enmType)
4271 {
4272 case PGMVIRTHANDLERTYPE_WRITE:
4273 return PGM_PAGE_HNDL_VIRT_STATE_WRITE;
4274 case PGMVIRTHANDLERTYPE_ALL:
4275 return PGM_PAGE_HNDL_VIRT_STATE_ALL;
4276 default:
4277 AssertFatalMsgFailed(("Invalid type %d\n", pCur->enmType));
4278 }
4279}
4280
4281
4282/**
4283 * Clears one physical page of a virtual handler
4284 *
4285 * @param pPGM Pointer to the PGM instance.
4286 * @param pCur Virtual handler structure
4287 * @param iPage Physical page index
4288 *
4289 * @remark Only used when PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL is being set, so no
4290 * need to care about other handlers in the same page.
4291 */
4292DECLINLINE(void) pgmHandlerVirtualClearPage(PPGM pPGM, PPGMVIRTHANDLER pCur, unsigned iPage)
4293{
4294 const PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
4295
4296 /*
4297 * Remove the node from the tree (it's supposed to be in the tree if we get here!).
4298 */
4299#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4300 AssertReleaseMsg(pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE,
4301 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4302 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias));
4303#endif
4304 if (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD)
4305 {
4306 /* We're the head of the alias chain. */
4307 PPGMPHYS2VIRTHANDLER pRemove = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRemove(&pPGM->CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key); NOREF(pRemove);
4308#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4309 AssertReleaseMsg(pRemove != NULL,
4310 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4311 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias));
4312 AssertReleaseMsg(pRemove == pPhys2Virt,
4313 ("wanted: pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n"
4314 " got: pRemove=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4315 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias,
4316 pRemove, pRemove->Core.Key, pRemove->Core.KeyLast, pRemove->offVirtHandler, pRemove->offNextAlias));
4317#endif
4318 if (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK)
4319 {
4320 /* Insert the next list in the alias chain into the tree. */
4321 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
4322#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4323 AssertReleaseMsg(pNext->offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE,
4324 ("pNext=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
4325 pNext, pNext->Core.Key, pNext->Core.KeyLast, pNext->offVirtHandler, pNext->offNextAlias));
4326#endif
4327 pNext->offNextAlias |= PGMPHYS2VIRTHANDLER_IS_HEAD;
4328 bool fRc = RTAvlroGCPhysInsert(&pPGM->CTX_SUFF(pTrees)->PhysToVirtHandlers, &pNext->Core);
4329 AssertRelease(fRc);
4330 }
4331 }
4332 else
4333 {
4334 /* Locate the previous node in the alias chain. */
4335 PPGMPHYS2VIRTHANDLER pPrev = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pPGM->CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
4336#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4337 AssertReleaseMsg(pPrev != pPhys2Virt,
4338 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} pPrev=%p\n",
4339 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias, pPrev));
4340#endif
4341 for (;;)
4342 {
4343 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPrev + (pPrev->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
4344 if (pNext == pPhys2Virt)
4345 {
4346 /* unlink. */
4347 LogFlow(("pgmHandlerVirtualClearPage: removed %p:{.offNextAlias=%#RX32} from alias chain. prev %p:{.offNextAlias=%#RX32} [%RGp-%RGp]\n",
4348 pPhys2Virt, pPhys2Virt->offNextAlias, pPrev, pPrev->offNextAlias, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
4349 if (!(pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
4350 pPrev->offNextAlias &= ~PGMPHYS2VIRTHANDLER_OFF_MASK;
4351 else
4352 {
4353 PPGMPHYS2VIRTHANDLER pNewNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
4354 pPrev->offNextAlias = ((intptr_t)pNewNext - (intptr_t)pPrev)
4355 | (pPrev->offNextAlias & ~PGMPHYS2VIRTHANDLER_OFF_MASK);
4356 }
4357 break;
4358 }
4359
4360 /* next */
4361 if (pNext == pPrev)
4362 {
4363#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
4364 AssertReleaseMsg(pNext != pPrev,
4365 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} pPrev=%p\n",
4366 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias, pPrev));
4367#endif
4368 break;
4369 }
4370 pPrev = pNext;
4371 }
4372 }
4373 Log2(("PHYS2VIRT: Removing %RGp-%RGp %#RX32 %s\n",
4374 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
4375 pPhys2Virt->offNextAlias = 0;
4376 pPhys2Virt->Core.KeyLast = NIL_RTGCPHYS; /* require reinsert */
4377
4378 /*
4379 * Clear the ram flags for this page.
4380 */
4381 PPGMPAGE pPage = pgmPhysGetPage(pPGM, pPhys2Virt->Core.Key);
4382 AssertReturnVoid(pPage);
4383 PGM_PAGE_SET_HNDL_VIRT_STATE(pPage, PGM_PAGE_HNDL_VIRT_STATE_NONE);
4384}
4385
4386
4387/**
4388 * Internal worker for finding a 'in-use' shadow page give by it's physical address.
4389 *
4390 * @returns Pointer to the shadow page structure.
4391 * @param pPool The pool.
4392 * @param idx The pool page index.
4393 */
4394DECLINLINE(PPGMPOOLPAGE) pgmPoolGetPageByIdx(PPGMPOOL pPool, unsigned idx)
4395{
4396 AssertFatalMsg(idx >= PGMPOOL_IDX_FIRST && idx < pPool->cCurPages, ("idx=%d\n", idx));
4397 return &pPool->aPages[idx];
4398}
4399
4400
4401#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
4402/**
4403 * Clear references to guest physical memory.
4404 *
4405 * @param pPool The pool.
4406 * @param pPoolPage The pool page.
4407 * @param pPhysPage The physical guest page tracking structure.
4408 */
4409DECLINLINE(void) pgmTrackDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPoolPage, PPGMPAGE pPhysPage)
4410{
4411 /*
4412 * Just deal with the simple case here.
4413 */
4414# ifdef LOG_ENABLED
4415 const unsigned uOrg = PGM_PAGE_GET_TRACKING(pPhysPage);
4416# endif
4417 const unsigned cRefs = PGM_PAGE_GET_TD_CREFS(pPhysPage);
4418 if (cRefs == 1)
4419 {
4420 Assert(pPoolPage->idx == PGM_PAGE_GET_TD_IDX(pPhysPage));
4421 PGM_PAGE_SET_TRACKING(pPhysPage, 0);
4422 }
4423 else
4424 pgmPoolTrackPhysExtDerefGCPhys(pPool, pPoolPage, pPhysPage);
4425 Log2(("pgmTrackDerefGCPhys: %x -> %x pPhysPage=%R[pgmpage]\n", uOrg, PGM_PAGE_GET_TRACKING(pPhysPage), pPhysPage ));
4426}
4427#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
4428
4429
4430#ifdef PGMPOOL_WITH_CACHE
4431/**
4432 * Moves the page to the head of the age list.
4433 *
4434 * This is done when the cached page is used in one way or another.
4435 *
4436 * @param pPool The pool.
4437 * @param pPage The cached page.
4438 */
4439DECLINLINE(void) pgmPoolCacheUsed(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4440{
4441 PVM pVM = pPool->CTX_SUFF(pVM);
4442 pgmLock(pVM);
4443
4444 /*
4445 * Move to the head of the age list.
4446 */
4447 if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
4448 {
4449 /* unlink */
4450 pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
4451 if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
4452 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
4453 else
4454 pPool->iAgeTail = pPage->iAgePrev;
4455
4456 /* insert at head */
4457 pPage->iAgePrev = NIL_PGMPOOL_IDX;
4458 pPage->iAgeNext = pPool->iAgeHead;
4459 Assert(pPage->iAgeNext != NIL_PGMPOOL_IDX); /* we would've already been head then */
4460 pPool->iAgeHead = pPage->idx;
4461 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->idx;
4462 }
4463 pgmUnlock(pVM);
4464}
4465#endif /* PGMPOOL_WITH_CACHE */
4466
4467/**
4468 * Locks a page to prevent flushing (important for cr3 root pages or shadow pae pd pages).
4469 *
4470 * @param pVM VM Handle.
4471 * @param pPage PGM pool page
4472 */
4473DECLINLINE(void) pgmPoolLockPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4474{
4475 Assert(PGMIsLockOwner(pPool->CTX_SUFF(pVM)));
4476 ASMAtomicIncU32(&pPage->cLocked);
4477}
4478
4479
4480/**
4481 * Unlocks a page to allow flushing again
4482 *
4483 * @param pVM VM Handle.
4484 * @param pPage PGM pool page
4485 */
4486DECLINLINE(void) pgmPoolUnlockPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
4487{
4488 Assert(PGMIsLockOwner(pPool->CTX_SUFF(pVM)));
4489 Assert(pPage->cLocked);
4490 ASMAtomicDecU32(&pPage->cLocked);
4491}
4492
4493
4494/**
4495 * Checks if the page is locked (e.g. the active CR3 or one of the four PDs of a PAE PDPT)
4496 *
4497 * @returns VBox status code.
4498 * @param pPage PGM pool page
4499 */
4500DECLINLINE(bool) pgmPoolIsPageLocked(PPGM pPGM, PPGMPOOLPAGE pPage)
4501{
4502 if (pPage->cLocked)
4503 {
4504 LogFlow(("pgmPoolIsPageLocked found root page %d\n", pPage->enmKind));
4505 if (pPage->cModifications)
4506 pPage->cModifications = 1; /* reset counter (can't use 0, or else it will be reinserted in the modified list) */
4507 return true;
4508 }
4509 return false;
4510}
4511
4512/**
4513 * Tells if mappings are to be put into the shadow page table or not
4514 *
4515 * @returns boolean result
4516 * @param pVM VM handle.
4517 */
4518DECLINLINE(bool) pgmMapAreMappingsEnabled(PPGM pPGM)
4519{
4520#ifdef IN_RING0
4521 /* There are no mappings in VT-x and AMD-V mode. */
4522 Assert(pPGM->fDisableMappings);
4523 return false;
4524#else
4525 return !pPGM->fDisableMappings;
4526#endif
4527}
4528
4529/** @} */
4530
4531#endif
4532
4533
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