VirtualBox

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

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

PGM: Saved state hacking, fA20Enabled is now bool, shadow ROM bug fix, and mapping chunk limit correction for 32-bit hosts.

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