VirtualBox

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

Last change on this file since 64115 was 64115, checked in by vboxsync, 8 years ago

PDM,IOM,PGM: Morphed the MMIO2 API into a mixed MMIO2 and pre-registered MMIO API that is able to deal with really large (<= 64GB) MMIO ranges. Limited testing, so back out at first sign of trouble.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 194.5 KB
Line 
1/* $Id: PGMInternal.h 64115 2016-09-30 20:14:27Z vboxsync $ */
2/** @file
3 * PGM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006-2016 Oracle Corporation
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
18#ifndef ___PGMInternal_h
19#define ___PGMInternal_h
20
21#include <VBox/cdefs.h>
22#include <VBox/types.h>
23#include <VBox/err.h>
24#include <VBox/dbg.h>
25#include <VBox/vmm/stam.h>
26#include <VBox/param.h>
27#include <VBox/vmm/vmm.h>
28#include <VBox/vmm/mm.h>
29#include <VBox/vmm/pdmcritsect.h>
30#include <VBox/vmm/pdmapi.h>
31#include <VBox/dis.h>
32#include <VBox/vmm/dbgf.h>
33#include <VBox/log.h>
34#include <VBox/vmm/gmm.h>
35#include <VBox/vmm/hm.h>
36#include <VBox/vmm/hm_vmx.h>
37#include <iprt/asm.h>
38#include <iprt/assert.h>
39#include <iprt/avl.h>
40#include <iprt/critsect.h>
41#include <iprt/list-off32.h>
42#include <iprt/sha.h>
43
44
45
46/** @defgroup grp_pgm_int Internals
47 * @ingroup grp_pgm
48 * @internal
49 * @{
50 */
51
52
53/** @name PGM Compile Time Config
54 * @{
55 */
56
57/**
58 * Indicates that there are no guest mappings in the shadow tables.
59 *
60 * Note! In ring-3 the macro is also used to exclude the managment of the
61 * intermediate context page tables. On 32-bit systems we use the intermediate
62 * context to support 64-bit guest execution. Thus, we cannot fully make it
63 * without mappings there even when VBOX_WITH_RAW_MODE is not defined.
64 *
65 * In raw-mode context there are by design always guest mappings (the code is
66 * executed from one), while in ring-0 there are none at all. Neither context
67 * manages the page tables for intermediate switcher context, that's all done in
68 * ring-3.
69 */
70#if defined(IN_RING0) \
71 || ( !defined(VBOX_WITH_RAW_MODE) \
72 && ( HC_ARCH_BITS != 32 \
73 || !defined(VBOX_WITH_64_BITS_GUESTS) \
74 ) \
75 )
76# define PGM_WITHOUT_MAPPINGS
77#endif
78
79/**
80 * Check and skip global PDEs for non-global flushes
81 */
82#define PGM_SKIP_GLOBAL_PAGEDIRS_ON_NONGLOBAL_FLUSH
83
84/**
85 * Optimization for PAE page tables that are modified often
86 */
87//#if 0 /* disabled again while debugging */
88#ifndef IN_RC
89# define PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
90#endif
91//#endif
92
93/**
94 * Large page support enabled only on 64 bits hosts; applies to nested paging only.
95 */
96#if (HC_ARCH_BITS == 64) && !defined(IN_RC)
97# define PGM_WITH_LARGE_PAGES
98#endif
99
100/**
101 * Enables optimizations for MMIO handlers that exploits X86_TRAP_PF_RSVD and
102 * VMX_EXIT_EPT_MISCONFIG.
103 */
104#if 1 /* testing */
105# define PGM_WITH_MMIO_OPTIMIZATIONS
106#endif
107
108/**
109 * Sync N pages instead of a whole page table
110 */
111#define PGM_SYNC_N_PAGES
112
113/**
114 * Number of pages to sync during a page fault
115 *
116 * When PGMPOOL_WITH_GCPHYS_TRACKING is enabled using high values here
117 * causes a lot of unnecessary extents and also is slower than taking more \#PFs.
118 *
119 * Note that \#PFs are much more expensive in the VT-x/AMD-V case due to
120 * world switch overhead, so let's sync more.
121 */
122# ifdef IN_RING0
123/* Chose 32 based on the compile test in @bugref{4219}; 64 shows worse stats.
124 * 32 again shows better results than 16; slightly more overhead in the \#PF handler,
125 * but ~5% fewer faults.
126 */
127# define PGM_SYNC_NR_PAGES 32
128#else
129# define PGM_SYNC_NR_PAGES 8
130#endif
131
132/**
133 * Number of PGMPhysRead/Write cache entries (must be <= sizeof(uint64_t))
134 */
135#define PGM_MAX_PHYSCACHE_ENTRIES 64
136#define PGM_MAX_PHYSCACHE_ENTRIES_MASK (PGM_MAX_PHYSCACHE_ENTRIES-1)
137
138
139/** @def PGMPOOL_CFG_MAX_GROW
140 * The maximum number of pages to add to the pool in one go.
141 */
142#define PGMPOOL_CFG_MAX_GROW (_256K >> PAGE_SHIFT)
143
144/** @def VBOX_STRICT_PGM_HANDLER_VIRTUAL
145 * Enables some extra assertions for virtual handlers (mainly phys2virt related).
146 */
147#ifdef VBOX_STRICT
148# define VBOX_STRICT_PGM_HANDLER_VIRTUAL
149#endif
150
151/** @def VBOX_WITH_NEW_LAZY_PAGE_ALLOC
152 * Enables the experimental lazy page allocation code. */
153#ifdef DOXYGEN_RUNNING
154# define VBOX_WITH_NEW_LAZY_PAGE_ALLOC
155#endif
156
157/** @def VBOX_WITH_REAL_WRITE_MONITORED_PAGES
158 * Enables real write monitoring of pages, i.e. mapping them read-only and
159 * only making them writable when getting a write access \#PF. */
160#define VBOX_WITH_REAL_WRITE_MONITORED_PAGES
161
162/** @} */
163
164
165/** @name PDPT and PML4 flags.
166 * These are placed in the three bits available for system programs in
167 * the PDPT and PML4 entries.
168 * @{ */
169/** The entry is a permanent one and it's must always be present.
170 * Never free such an entry. */
171#define PGM_PLXFLAGS_PERMANENT RT_BIT_64(10)
172/** Mapping (hypervisor allocated pagetable). */
173#define PGM_PLXFLAGS_MAPPING RT_BIT_64(11)
174/** @} */
175
176/** @name Page directory flags.
177 * These are placed in the three bits available for system programs in
178 * the page directory entries.
179 * @{ */
180/** Mapping (hypervisor allocated pagetable). */
181#define PGM_PDFLAGS_MAPPING RT_BIT_64(10)
182/** Made read-only to facilitate dirty bit tracking. */
183#define PGM_PDFLAGS_TRACK_DIRTY RT_BIT_64(11)
184/** @} */
185
186/** @name Page flags.
187 * These are placed in the three bits available for system programs in
188 * the page entries.
189 * @{ */
190/** Made read-only to facilitate dirty bit tracking. */
191#define PGM_PTFLAGS_TRACK_DIRTY RT_BIT_64(9)
192
193#ifndef PGM_PTFLAGS_CSAM_VALIDATED
194/** Scanned and approved by CSAM (tm).
195 * NOTE: Must be identical to the one defined in CSAMInternal.h!!
196 * @todo Move PGM_PTFLAGS_* and PGM_PDFLAGS_* to VBox/vmm/pgm.h. */
197#define PGM_PTFLAGS_CSAM_VALIDATED RT_BIT_64(11)
198#endif
199
200/** @} */
201
202/** @name Defines used to indicate the shadow and guest paging in the templates.
203 * @{ */
204#define PGM_TYPE_REAL 1
205#define PGM_TYPE_PROT 2
206#define PGM_TYPE_32BIT 3
207#define PGM_TYPE_PAE 4
208#define PGM_TYPE_AMD64 5
209#define PGM_TYPE_NESTED 6
210#define PGM_TYPE_EPT 7
211#define PGM_TYPE_MAX PGM_TYPE_EPT
212/** @} */
213
214/** Macro for checking if the guest is using paging.
215 * @param uGstType PGM_TYPE_*
216 * @param uShwType PGM_TYPE_*
217 * @remark ASSUMES certain order of the PGM_TYPE_* values.
218 */
219#define PGM_WITH_PAGING(uGstType, uShwType) \
220 ( (uGstType) >= PGM_TYPE_32BIT \
221 && (uShwType) != PGM_TYPE_NESTED \
222 && (uShwType) != PGM_TYPE_EPT)
223
224/** Macro for checking if the guest supports the NX bit.
225 * @param uGstType PGM_TYPE_*
226 * @param uShwType PGM_TYPE_*
227 * @remark ASSUMES certain order of the PGM_TYPE_* values.
228 */
229#define PGM_WITH_NX(uGstType, uShwType) \
230 ( (uGstType) >= PGM_TYPE_PAE \
231 && (uShwType) != PGM_TYPE_NESTED \
232 && (uShwType) != PGM_TYPE_EPT)
233
234
235/** @def PGM_HCPHYS_2_PTR
236 * Maps a HC physical page pool address to a virtual address.
237 *
238 * @returns VBox status code.
239 * @param pVM The cross context VM structure.
240 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
241 * @param HCPhys The HC physical address to map to a virtual one.
242 * @param ppv Where to store the virtual address. No need to cast
243 * this.
244 *
245 * @remark Use with care as we don't have so much dynamic mapping space in
246 * ring-0 on 32-bit darwin and in RC.
247 * @remark There is no need to assert on the result.
248 */
249#if defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) || defined(IN_RC)
250# define PGM_HCPHYS_2_PTR(pVM, pVCpu, HCPhys, ppv) \
251 pgmRZDynMapHCPageInlined(pVCpu, HCPhys, (void **)(ppv) RTLOG_COMMA_SRC_POS)
252#else
253# define PGM_HCPHYS_2_PTR(pVM, pVCpu, HCPhys, ppv) \
254 MMPagePhys2PageEx(pVM, HCPhys, (void **)(ppv))
255#endif
256
257/** @def PGM_GCPHYS_2_PTR_V2
258 * Maps a GC physical page address to a virtual address.
259 *
260 * @returns VBox status code.
261 * @param pVM The cross context VM structure.
262 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
263 * @param GCPhys The GC physical address to map to a virtual one.
264 * @param ppv Where to store the virtual address. No need to cast this.
265 *
266 * @remark Use with care as we don't have so much dynamic mapping space in
267 * ring-0 on 32-bit darwin and in RC.
268 * @remark There is no need to assert on the result.
269 */
270#if defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) || defined(IN_RC)
271# define PGM_GCPHYS_2_PTR_V2(pVM, pVCpu, GCPhys, ppv) \
272 pgmRZDynMapGCPageV2Inlined(pVM, pVCpu, GCPhys, (void **)(ppv) RTLOG_COMMA_SRC_POS)
273#else
274# define PGM_GCPHYS_2_PTR_V2(pVM, pVCpu, GCPhys, ppv) \
275 pgmPhysGCPhys2R3Ptr(pVM, GCPhys, (PRTR3PTR)(ppv)) /** @todo this isn't asserting! */
276#endif
277
278/** @def PGM_GCPHYS_2_PTR
279 * Maps a GC physical page address to a virtual address.
280 *
281 * @returns VBox status code.
282 * @param pVM The cross context VM structure.
283 * @param GCPhys The GC physical address to map to a virtual one.
284 * @param ppv Where to store the virtual address. No need to cast this.
285 *
286 * @remark Use with care as we don't have so much dynamic mapping space in
287 * ring-0 on 32-bit darwin and in RC.
288 * @remark There is no need to assert on the result.
289 */
290#define PGM_GCPHYS_2_PTR(pVM, GCPhys, ppv) PGM_GCPHYS_2_PTR_V2(pVM, VMMGetCpu(pVM), GCPhys, ppv)
291
292/** @def PGM_GCPHYS_2_PTR_BY_VMCPU
293 * Maps a GC physical page address to a virtual address.
294 *
295 * @returns VBox status code.
296 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
297 * @param GCPhys The GC physical address to map to a virtual one.
298 * @param ppv Where to store the virtual address. No need to cast this.
299 *
300 * @remark Use with care as we don't have so much dynamic mapping space in
301 * ring-0 on 32-bit darwin and in RC.
302 * @remark There is no need to assert on the result.
303 */
304#define PGM_GCPHYS_2_PTR_BY_VMCPU(pVCpu, GCPhys, ppv) PGM_GCPHYS_2_PTR_V2((pVCpu)->CTX_SUFF(pVM), pVCpu, GCPhys, ppv)
305
306/** @def PGM_GCPHYS_2_PTR_EX
307 * Maps a unaligned GC physical page address to a virtual address.
308 *
309 * @returns VBox status code.
310 * @param pVM The cross context VM structure.
311 * @param GCPhys The GC physical address to map to a virtual one.
312 * @param ppv Where to store the virtual address. No need to cast this.
313 *
314 * @remark Use with care as we don't have so much dynamic mapping space in
315 * ring-0 on 32-bit darwin and in RC.
316 * @remark There is no need to assert on the result.
317 */
318#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
319# define PGM_GCPHYS_2_PTR_EX(pVM, GCPhys, ppv) \
320 pgmRZDynMapGCPageOffInlined(VMMGetCpu(pVM), GCPhys, (void **)(ppv) RTLOG_COMMA_SRC_POS)
321#else
322# define PGM_GCPHYS_2_PTR_EX(pVM, GCPhys, ppv) \
323 pgmPhysGCPhys2R3Ptr(pVM, GCPhys, (PRTR3PTR)(ppv)) /** @todo this isn't asserting! */
324#endif
325
326/** @def PGM_DYNMAP_UNUSED_HINT
327 * Hints to the dynamic mapping code in RC and R0/darwin that the specified page
328 * is no longer used.
329 *
330 * For best effect only apply this to the page that was mapped most recently.
331 *
332 * @param pVCpu The cross context virtual CPU structure of the calling EMT.
333 * @param pvPage The pool page.
334 */
335#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
336# ifdef LOG_ENABLED
337# define PGM_DYNMAP_UNUSED_HINT(pVCpu, pvPage) pgmRZDynMapUnusedHint(pVCpu, pvPage, RT_SRC_POS)
338# else
339# define PGM_DYNMAP_UNUSED_HINT(pVCpu, pvPage) pgmRZDynMapUnusedHint(pVCpu, pvPage)
340# endif
341#else
342# define PGM_DYNMAP_UNUSED_HINT(pVCpu, pvPage) do {} while (0)
343#endif
344
345/** @def PGM_DYNMAP_UNUSED_HINT_VM
346 * Hints to the dynamic mapping code in RC and R0/darwin that the specified page
347 * is no longer used.
348 *
349 * For best effect only apply this to the page that was mapped most recently.
350 *
351 * @param pVM The cross context VM structure.
352 * @param pvPage The pool page.
353 */
354#define PGM_DYNMAP_UNUSED_HINT_VM(pVM, pvPage) PGM_DYNMAP_UNUSED_HINT(VMMGetCpu(pVM), pvPage)
355
356
357/** @def PGM_INVL_PG
358 * Invalidates a page.
359 *
360 * @param pVCpu The cross context virtual CPU structure.
361 * @param GCVirt The virtual address of the page to invalidate.
362 */
363#ifdef IN_RC
364# define PGM_INVL_PG(pVCpu, GCVirt) ASMInvalidatePage((uintptr_t)(GCVirt))
365#elif defined(IN_RING0)
366# define PGM_INVL_PG(pVCpu, GCVirt) HMInvalidatePage(pVCpu, (RTGCPTR)(GCVirt))
367#else
368# define PGM_INVL_PG(pVCpu, GCVirt) HMInvalidatePage(pVCpu, (RTGCPTR)(GCVirt))
369#endif
370
371/** @def PGM_INVL_PG_ALL_VCPU
372 * Invalidates a page on all VCPUs
373 *
374 * @param pVM The cross context VM structure.
375 * @param GCVirt The virtual address of the page to invalidate.
376 */
377#ifdef IN_RC
378# define PGM_INVL_PG_ALL_VCPU(pVM, GCVirt) ASMInvalidatePage((uintptr_t)(GCVirt))
379#elif defined(IN_RING0)
380# define PGM_INVL_PG_ALL_VCPU(pVM, GCVirt) HMInvalidatePageOnAllVCpus(pVM, (RTGCPTR)(GCVirt))
381#else
382# define PGM_INVL_PG_ALL_VCPU(pVM, GCVirt) HMInvalidatePageOnAllVCpus(pVM, (RTGCPTR)(GCVirt))
383#endif
384
385/** @def PGM_INVL_BIG_PG
386 * Invalidates a 4MB page directory entry.
387 *
388 * @param pVCpu The cross context virtual CPU structure.
389 * @param GCVirt The virtual address within the page directory to invalidate.
390 */
391#ifdef IN_RC
392# define PGM_INVL_BIG_PG(pVCpu, GCVirt) ASMReloadCR3()
393#elif defined(IN_RING0)
394# define PGM_INVL_BIG_PG(pVCpu, GCVirt) HMFlushTLB(pVCpu)
395#else
396# define PGM_INVL_BIG_PG(pVCpu, GCVirt) HMFlushTLB(pVCpu)
397#endif
398
399/** @def PGM_INVL_VCPU_TLBS()
400 * Invalidates the TLBs of the specified VCPU
401 *
402 * @param pVCpu The cross context virtual CPU structure.
403 */
404#ifdef IN_RC
405# define PGM_INVL_VCPU_TLBS(pVCpu) ASMReloadCR3()
406#elif defined(IN_RING0)
407# define PGM_INVL_VCPU_TLBS(pVCpu) HMFlushTLB(pVCpu)
408#else
409# define PGM_INVL_VCPU_TLBS(pVCpu) HMFlushTLB(pVCpu)
410#endif
411
412/** @def PGM_INVL_ALL_VCPU_TLBS()
413 * Invalidates the TLBs of all VCPUs
414 *
415 * @param pVM The cross context VM structure.
416 */
417#ifdef IN_RC
418# define PGM_INVL_ALL_VCPU_TLBS(pVM) ASMReloadCR3()
419#elif defined(IN_RING0)
420# define PGM_INVL_ALL_VCPU_TLBS(pVM) HMFlushTLBOnAllVCpus(pVM)
421#else
422# define PGM_INVL_ALL_VCPU_TLBS(pVM) HMFlushTLBOnAllVCpus(pVM)
423#endif
424
425
426/** @name Safer Shadow PAE PT/PTE
427 * For helping avoid misinterpreting invalid PAE/AMD64 page table entries as
428 * present.
429 *
430 * @{
431 */
432#if 1
433/**
434 * For making sure that u1Present and X86_PTE_P checks doesn't mistake
435 * invalid entries for present.
436 * @sa X86PTEPAE.
437 */
438typedef union PGMSHWPTEPAE
439{
440 /** Unsigned integer view */
441 X86PGPAEUINT uCareful;
442 /* Not other views. */
443} PGMSHWPTEPAE;
444
445# define PGMSHWPTEPAE_IS_P(Pte) ( ((Pte).uCareful & (X86_PTE_P | X86_PTE_PAE_MBZ_MASK_NX)) == X86_PTE_P )
446# define PGMSHWPTEPAE_IS_RW(Pte) ( !!((Pte).uCareful & X86_PTE_RW))
447# define PGMSHWPTEPAE_IS_US(Pte) ( !!((Pte).uCareful & X86_PTE_US))
448# define PGMSHWPTEPAE_IS_A(Pte) ( !!((Pte).uCareful & X86_PTE_A))
449# define PGMSHWPTEPAE_IS_D(Pte) ( !!((Pte).uCareful & X86_PTE_D))
450# define PGMSHWPTEPAE_IS_TRACK_DIRTY(Pte) ( !!((Pte).uCareful & PGM_PTFLAGS_TRACK_DIRTY) )
451# define PGMSHWPTEPAE_IS_P_RW(Pte) ( ((Pte).uCareful & (X86_PTE_P | X86_PTE_RW | X86_PTE_PAE_MBZ_MASK_NX)) == (X86_PTE_P | X86_PTE_RW) )
452# define PGMSHWPTEPAE_GET_LOG(Pte) ( (Pte).uCareful )
453# define PGMSHWPTEPAE_GET_HCPHYS(Pte) ( (Pte).uCareful & X86_PTE_PAE_PG_MASK )
454# define PGMSHWPTEPAE_GET_U(Pte) ( (Pte).uCareful ) /**< Use with care. */
455# define PGMSHWPTEPAE_SET(Pte, uVal) do { (Pte).uCareful = (uVal); } while (0)
456# define PGMSHWPTEPAE_SET2(Pte, Pte2) do { (Pte).uCareful = (Pte2).uCareful; } while (0)
457# define PGMSHWPTEPAE_ATOMIC_SET(Pte, uVal) do { ASMAtomicWriteU64(&(Pte).uCareful, (uVal)); } while (0)
458# define PGMSHWPTEPAE_ATOMIC_SET2(Pte, Pte2) do { ASMAtomicWriteU64(&(Pte).uCareful, (Pte2).uCareful); } while (0)
459# define PGMSHWPTEPAE_SET_RO(Pte) do { (Pte).uCareful &= ~(X86PGPAEUINT)X86_PTE_RW; } while (0)
460# define PGMSHWPTEPAE_SET_RW(Pte) do { (Pte).uCareful |= X86_PTE_RW; } while (0)
461
462/**
463 * For making sure that u1Present and X86_PTE_P checks doesn't mistake
464 * invalid entries for present.
465 * @sa X86PTPAE.
466 */
467typedef struct PGMSHWPTPAE
468{
469 PGMSHWPTEPAE a[X86_PG_PAE_ENTRIES];
470} PGMSHWPTPAE;
471
472#else
473typedef X86PTEPAE PGMSHWPTEPAE;
474typedef X86PTPAE PGMSHWPTPAE;
475# define PGMSHWPTEPAE_IS_P(Pte) ( (Pte).n.u1Present )
476# define PGMSHWPTEPAE_IS_RW(Pte) ( (Pte).n.u1Write )
477# define PGMSHWPTEPAE_IS_US(Pte) ( (Pte).n.u1User )
478# define PGMSHWPTEPAE_IS_A(Pte) ( (Pte).n.u1Accessed )
479# define PGMSHWPTEPAE_IS_D(Pte) ( (Pte).n.u1Dirty )
480# define PGMSHWPTEPAE_IS_TRACK_DIRTY(Pte) ( !!((Pte).u & PGM_PTFLAGS_TRACK_DIRTY) )
481# define PGMSHWPTEPAE_IS_P_RW(Pte) ( ((Pte).u & (X86_PTE_P | X86_PTE_RW)) == (X86_PTE_P | X86_PTE_RW) )
482# define PGMSHWPTEPAE_GET_LOG(Pte) ( (Pte).u )
483# define PGMSHWPTEPAE_GET_HCPHYS(Pte) ( (Pte).u & X86_PTE_PAE_PG_MASK )
484# define PGMSHWPTEPAE_GET_U(Pte) ( (Pte).u ) /**< Use with care. */
485# define PGMSHWPTEPAE_SET(Pte, uVal) do { (Pte).u = (uVal); } while (0)
486# define PGMSHWPTEPAE_SET2(Pte, Pte2) do { (Pte).u = (Pte2).u; } while (0)
487# define PGMSHWPTEPAE_ATOMIC_SET(Pte, uVal) do { ASMAtomicWriteU64(&(Pte).u, (uVal)); } while (0)
488# define PGMSHWPTEPAE_ATOMIC_SET2(Pte, Pte2) do { ASMAtomicWriteU64(&(Pte).u, (Pte2).u); } while (0)
489# define PGMSHWPTEPAE_SET_RO(Pte) do { (Pte).u &= ~(X86PGPAEUINT)X86_PTE_RW; } while (0)
490# define PGMSHWPTEPAE_SET_RW(Pte) do { (Pte).u |= X86_PTE_RW; } while (0)
491
492#endif
493
494/** Pointer to a shadow PAE PTE. */
495typedef PGMSHWPTEPAE *PPGMSHWPTEPAE;
496/** Pointer to a const shadow PAE PTE. */
497typedef PGMSHWPTEPAE const *PCPGMSHWPTEPAE;
498
499/** Pointer to a shadow PAE page table. */
500typedef PGMSHWPTPAE *PPGMSHWPTPAE;
501/** Pointer to a const shadow PAE page table. */
502typedef PGMSHWPTPAE const *PCPGMSHWPTPAE;
503/** @} */
504
505
506/** Size of the GCPtrConflict array in PGMMAPPING.
507 * @remarks Must be a power of two. */
508#define PGMMAPPING_CONFLICT_MAX 8
509
510/**
511 * Structure for tracking GC Mappings.
512 *
513 * This structure is used by linked list in both GC and HC.
514 */
515typedef struct PGMMAPPING
516{
517 /** Pointer to next entry. */
518 R3PTRTYPE(struct PGMMAPPING *) pNextR3;
519 /** Pointer to next entry. */
520 R0PTRTYPE(struct PGMMAPPING *) pNextR0;
521 /** Pointer to next entry. */
522 RCPTRTYPE(struct PGMMAPPING *) pNextRC;
523 /** Indicate whether this entry is finalized. */
524 bool fFinalized;
525 /** Start Virtual address. */
526 RTGCPTR GCPtr;
527 /** Last Virtual address (inclusive). */
528 RTGCPTR GCPtrLast;
529 /** Range size (bytes). */
530 RTGCPTR cb;
531 /** Pointer to relocation callback function. */
532 R3PTRTYPE(PFNPGMRELOCATE) pfnRelocate;
533 /** User argument to the callback. */
534 R3PTRTYPE(void *) pvUser;
535 /** Mapping description / name. For easing debugging. */
536 R3PTRTYPE(const char *) pszDesc;
537 /** Last 8 addresses that caused conflicts. */
538 RTGCPTR aGCPtrConflicts[PGMMAPPING_CONFLICT_MAX];
539 /** Number of conflicts for this hypervisor mapping. */
540 uint32_t cConflicts;
541 /** Number of page tables. */
542 uint32_t cPTs;
543
544 /** Array of page table mapping data. Each entry
545 * describes one page table. The array can be longer
546 * than the declared length.
547 */
548 struct
549 {
550 /** The HC physical address of the page table. */
551 RTHCPHYS HCPhysPT;
552 /** The HC physical address of the first PAE page table. */
553 RTHCPHYS HCPhysPaePT0;
554 /** The HC physical address of the second PAE page table. */
555 RTHCPHYS HCPhysPaePT1;
556 /** The HC virtual address of the 32-bit page table. */
557 R3PTRTYPE(PX86PT) pPTR3;
558 /** The HC virtual address of the two PAE page table. (i.e 1024 entries instead of 512) */
559 R3PTRTYPE(PPGMSHWPTPAE) paPaePTsR3;
560 /** The RC virtual address of the 32-bit page table. */
561 RCPTRTYPE(PX86PT) pPTRC;
562 /** The RC virtual address of the two PAE page table. */
563 RCPTRTYPE(PPGMSHWPTPAE) paPaePTsRC;
564 /** The R0 virtual address of the 32-bit page table. */
565 R0PTRTYPE(PX86PT) pPTR0;
566 /** The R0 virtual address of the two PAE page table. */
567 R0PTRTYPE(PPGMSHWPTPAE) paPaePTsR0;
568 } aPTs[1];
569} PGMMAPPING;
570/** Pointer to structure for tracking GC Mappings. */
571typedef struct PGMMAPPING *PPGMMAPPING;
572
573
574/**
575 * Physical page access handler type registration.
576 */
577typedef struct PGMPHYSHANDLERTYPEINT
578{
579 /** Number of references. */
580 uint32_t volatile cRefs;
581 /** Magic number (PGMPHYSHANDLERTYPEINT_MAGIC). */
582 uint32_t u32Magic;
583 /** Link of handler types anchored in PGMTREES::HeadPhysHandlerTypes. */
584 RTLISTOFF32NODE ListNode;
585 /** The kind of accesses we're handling. */
586 PGMPHYSHANDLERKIND enmKind;
587 /** The PGM_PAGE_HNDL_PHYS_STATE_XXX value corresponding to enmKind. */
588 uint32_t uState;
589 /** Pointer to RC callback function. */
590 RCPTRTYPE(PFNPGMPHYSHANDLER) pfnHandlerRC;
591 /** Pointer to RC callback function for \#PFs. */
592 RCPTRTYPE(PFNPGMRZPHYSPFHANDLER) pfnPfHandlerRC;
593 /** Pointer to R3 callback function. */
594 R3PTRTYPE(PFNPGMPHYSHANDLER) pfnHandlerR3;
595 /** Pointer to R0 callback function. */
596 R0PTRTYPE(PFNPGMPHYSHANDLER) pfnHandlerR0;
597 /** Pointer to R0 callback function for \#PFs. */
598 R0PTRTYPE(PFNPGMRZPHYSPFHANDLER) pfnPfHandlerR0;
599 /** Description / Name. For easing debugging. */
600 R3PTRTYPE(const char *) pszDesc;
601} PGMPHYSHANDLERTYPEINT;
602/** Pointer to a physical access handler type registration. */
603typedef PGMPHYSHANDLERTYPEINT *PPGMPHYSHANDLERTYPEINT;
604/** Magic value for the physical handler callbacks (Robert A. Heinlein). */
605#define PGMPHYSHANDLERTYPEINT_MAGIC UINT32_C(0x19070707)
606/** Magic value for the physical handler callbacks. */
607#define PGMPHYSHANDLERTYPEINT_MAGIC_DEAD UINT32_C(0x19880508)
608
609/**
610 * Converts a handle to a pointer.
611 * @returns PPGMPHYSHANDLERTYPEINT
612 * @param a_pVM The cross context VM structure.
613 * @param a_hType Physical access handler type handle.
614 */
615#define PGMPHYSHANDLERTYPEINT_FROM_HANDLE(a_pVM, a_hType) ((PPGMPHYSHANDLERTYPEINT)MMHyperHeapOffsetToPtr(a_pVM, a_hType))
616
617
618/**
619 * Physical page access handler structure.
620 *
621 * This is used to keep track of physical address ranges
622 * which are being monitored in some kind of way.
623 */
624typedef struct PGMPHYSHANDLER
625{
626 AVLROGCPHYSNODECORE Core;
627 /** Number of pages to update. */
628 uint32_t cPages;
629 /** Set if we have pages that have been aliased. */
630 uint32_t cAliasedPages;
631 /** Set if we have pages that have temporarily been disabled. */
632 uint32_t cTmpOffPages;
633 /** Registered handler type handle (heap offset). */
634 PGMPHYSHANDLERTYPE hType;
635 /** User argument for RC handlers. */
636 RCPTRTYPE(void *) pvUserRC;
637#if HC_ARCH_BITS == 64
638 RTRCPTR Padding0; /**< Explicit alignment padding. */
639#endif
640 /** User argument for R3 handlers. */
641 R3PTRTYPE(void *) pvUserR3;
642 /** User argument for R0 handlers. */
643 R0PTRTYPE(void *) pvUserR0;
644 /** Description / Name. For easing debugging. */
645 R3PTRTYPE(const char *) pszDesc;
646#ifdef VBOX_WITH_STATISTICS
647 /** Profiling of this handler. */
648 STAMPROFILE Stat;
649#endif
650} PGMPHYSHANDLER;
651/** Pointer to a physical page access handler structure. */
652typedef PGMPHYSHANDLER *PPGMPHYSHANDLER;
653
654/**
655 * Gets the type record for a physical handler (no reference added).
656 * @returns PPGMPHYSHANDLERTYPEINT
657 * @param a_pVM The cross context VM structure.
658 * @param a_pPhysHandler Pointer to the physical handler structure
659 * (PGMPHYSHANDLER).
660 */
661#define PGMPHYSHANDLER_GET_TYPE(a_pVM, a_pPhysHandler) PGMPHYSHANDLERTYPEINT_FROM_HANDLE(a_pVM, (a_pPhysHandler)->hType)
662
663
664#ifdef VBOX_WITH_RAW_MODE
665
666/**
667 * Cache node for the physical addresses covered by a virtual handler.
668 */
669typedef struct PGMPHYS2VIRTHANDLER
670{
671 /** Core node for the tree based on physical ranges. */
672 AVLROGCPHYSNODECORE Core;
673 /** Offset from this struct to the PGMVIRTHANDLER structure. */
674 int32_t offVirtHandler;
675 /** Offset of the next alias relative to this one.
676 * Bit 0 is used for indicating whether we're in the tree.
677 * Bit 1 is used for indicating that we're the head node.
678 */
679 int32_t offNextAlias;
680} PGMPHYS2VIRTHANDLER;
681/** Pointer to a phys to virtual handler structure. */
682typedef PGMPHYS2VIRTHANDLER *PPGMPHYS2VIRTHANDLER;
683
684/** The bit in PGMPHYS2VIRTHANDLER::offNextAlias used to indicate that the
685 * node is in the tree. */
686# define PGMPHYS2VIRTHANDLER_IN_TREE RT_BIT(0)
687/** The bit in PGMPHYS2VIRTHANDLER::offNextAlias used to indicate that the
688 * node is in the head of an alias chain.
689 * The PGMPHYS2VIRTHANDLER_IN_TREE is always set if this bit is set. */
690# define PGMPHYS2VIRTHANDLER_IS_HEAD RT_BIT(1)
691/** The mask to apply to PGMPHYS2VIRTHANDLER::offNextAlias to get the offset. */
692# define PGMPHYS2VIRTHANDLER_OFF_MASK (~(int32_t)3)
693
694
695/**
696 * Virtual page access handler type registration.
697 */
698typedef struct PGMVIRTANDLERTYPEINT
699{
700 /** Number of references. */
701 uint32_t volatile cRefs;
702 /** Magic number (PGMVIRTHANDLERTYPEINT_MAGIC). */
703 uint32_t u32Magic;
704 /** Link of handler types anchored in PGMTREES::HeadVirtHandlerTypes. */
705 RTLISTOFF32NODE ListNode;
706 /** The kind of accesses we're handling. */
707 PGMVIRTHANDLERKIND enmKind;
708 /** The PGM_PAGE_HNDL_PHYS_STATE_XXX value corresponding to enmKind. */
709 uint32_t uState;
710 /** Whether the pvUserRC argument should be automatically relocated or not. */
711 bool fRelocUserRC;
712 bool afPadding[HC_ARCH_BITS == 64 ? 7 : 3];
713 /** Pointer to RC callback function. */
714 RCPTRTYPE(PFNPGMVIRTHANDLER) pfnHandlerRC;
715 /** Pointer to RC callback function for \#PFs. */
716 RCPTRTYPE(PFNPGMRCVIRTPFHANDLER) pfnPfHandlerRC;
717 /** Pointer to the R3 callback function for invalidation. */
718 R3PTRTYPE(PFNPGMR3VIRTINVALIDATE) pfnInvalidateR3;
719 /** Pointer to R3 callback function. */
720 R3PTRTYPE(PFNPGMVIRTHANDLER) pfnHandlerR3;
721 /** Description / Name. For easing debugging. */
722 R3PTRTYPE(const char *) pszDesc;
723} PGMVIRTHANDLERTYPEINT;
724/** Pointer to a virtual access handler type registration. */
725typedef PGMVIRTHANDLERTYPEINT *PPGMVIRTHANDLERTYPEINT;
726/** Magic value for the virtual handler callbacks (Sir Arthur Charles Clarke). */
727# define PGMVIRTHANDLERTYPEINT_MAGIC UINT32_C(0x19171216)
728/** Magic value for the virtual handler callbacks. */
729# define PGMVIRTHANDLERTYPEINT_MAGIC_DEAD UINT32_C(0x20080319)
730
731/**
732 * Converts a handle to a pointer.
733 * @returns PPGMVIRTHANDLERTYPEINT
734 * @param a_pVM The cross context VM structure.
735 * @param a_hType Vitual access handler type handle.
736 */
737# define PGMVIRTHANDLERTYPEINT_FROM_HANDLE(a_pVM, a_hType) ((PPGMVIRTHANDLERTYPEINT)MMHyperHeapOffsetToPtr(a_pVM, a_hType))
738
739
740/**
741 * Virtual page access handler structure.
742 *
743 * This is used to keep track of virtual address ranges
744 * which are being monitored in some kind of way.
745 */
746typedef struct PGMVIRTHANDLER
747{
748 /** Core node for the tree based on virtual ranges. */
749 AVLROGCPTRNODECORE Core;
750 /** Size of the range (in bytes). */
751 uint32_t cb;
752 /** Number of cache pages. */
753 uint32_t cPages;
754 /** Registered handler type handle (heap offset). */
755 PGMVIRTHANDLERTYPE hType;
756 /** User argument for RC handlers. */
757 RCPTRTYPE(void *) pvUserRC;
758 /** User argument for R3 handlers. */
759 R3PTRTYPE(void *) pvUserR3;
760 /** Description / Name. For easing debugging. */
761 R3PTRTYPE(const char *) pszDesc;
762# ifdef VBOX_WITH_STATISTICS
763 /** Profiling of this handler. */
764 STAMPROFILE Stat;
765# endif
766 /** Array of cached physical addresses for the monitored ranged. */
767 PGMPHYS2VIRTHANDLER aPhysToVirt[HC_ARCH_BITS == 32 ? 1 : 2];
768} PGMVIRTHANDLER;
769/** Pointer to a virtual page access handler structure. */
770typedef PGMVIRTHANDLER *PPGMVIRTHANDLER;
771
772/**
773 * Gets the type record for a virtual handler (no reference added).
774 * @returns PPGMVIRTHANDLERTYPEINT
775 * @param a_pVM The cross context VM structure.
776 * @param a_pVirtHandler Pointer to the virtual handler structure
777 * (PGMVIRTHANDLER).
778 */
779# define PGMVIRTANDLER_GET_TYPE(a_pVM, a_pVirtHandler) PGMVIRTHANDLERTYPEINT_FROM_HANDLE(a_pVM, (a_pVirtHandler)->hType)
780
781#endif /* VBOX_WITH_RAW_MODE */
782
783
784/** @name Page type predicates.
785 * @{ */
786#define PGMPAGETYPE_IS_READABLE(type) ( (type) <= PGMPAGETYPE_ROM )
787#define PGMPAGETYPE_IS_WRITEABLE(type) ( (type) <= PGMPAGETYPE_ROM_SHADOW )
788#define PGMPAGETYPE_IS_RWX(type) ( (type) <= PGMPAGETYPE_ROM_SHADOW )
789#define PGMPAGETYPE_IS_ROX(type) ( (type) == PGMPAGETYPE_ROM )
790#define PGMPAGETYPE_IS_NP(type) ( (type) == PGMPAGETYPE_MMIO )
791/** @} */
792
793
794/**
795 * A Physical Guest Page tracking structure.
796 *
797 * The format of this structure is complicated because we have to fit a lot
798 * of information into as few bits as possible. The format is also subject
799 * to change (there is one coming up soon). Which means that for we'll be
800 * using PGM_PAGE_GET_*, PGM_PAGE_IS_ and PGM_PAGE_SET_* macros for *all*
801 * accesses to the structure.
802 */
803typedef union PGMPAGE
804{
805 /** Structured view. */
806 struct
807 {
808 /** 1:0 - The physical handler state (PGM_PAGE_HNDL_PHYS_STATE_*). */
809 uint64_t u2HandlerPhysStateY : 2;
810 /** 3:2 - Paging structure needed to map the page
811 * (PGM_PAGE_PDE_TYPE_*). */
812 uint64_t u2PDETypeY : 2;
813 /** 4 - Indicator of dirty page for fault tolerance tracking. */
814 uint64_t fFTDirtyY : 1;
815 /** 5 - Flag indicating that a write monitored page was written to
816 * when set. */
817 uint64_t fWrittenToY : 1;
818 /** 7:6 - Unused. */
819 uint64_t u2Unused0 : 2;
820 /** 9:8 - The physical handler state (PGM_PAGE_HNDL_VIRT_STATE_*). */
821 uint64_t u2HandlerVirtStateY : 2;
822 /** 11:10 - Unused. */
823 uint64_t u2Unused1 : 2;
824 /** 12:48 - The host physical frame number (shift left to get the
825 * address). */
826 uint64_t HCPhysFN : 36;
827 /** 50:48 - The page state. */
828 uint64_t uStateY : 3;
829 /** 51:53 - The page type (PGMPAGETYPE). */
830 uint64_t uTypeY : 3;
831 /** 63:54 - PTE index for usage tracking (page pool). */
832 uint64_t u10PteIdx : 10;
833
834 /** The GMM page ID.
835 * @remarks In the current implementation, MMIO2 and pages aliased to
836 * MMIO2 pages will be exploiting this field to calculate the
837 * ring-3 mapping address corresponding to the page.
838 * Later we may consider including MMIO2 management into GMM. */
839 uint32_t idPage;
840 /** Usage tracking (page pool). */
841 uint16_t u16TrackingY;
842 /** The number of read locks on this page. */
843 uint8_t cReadLocksY;
844 /** The number of write locks on this page. */
845 uint8_t cWriteLocksY;
846 } s;
847
848 /** 64-bit integer view. */
849 uint64_t au64[2];
850 /** 16-bit view. */
851 uint32_t au32[4];
852 /** 16-bit view. */
853 uint16_t au16[8];
854 /** 8-bit view. */
855 uint8_t au8[16];
856} PGMPAGE;
857AssertCompileSize(PGMPAGE, 16);
858/** Pointer to a physical guest page. */
859typedef PGMPAGE *PPGMPAGE;
860/** Pointer to a const physical guest page. */
861typedef const PGMPAGE *PCPGMPAGE;
862/** Pointer to a physical guest page pointer. */
863typedef PPGMPAGE *PPPGMPAGE;
864
865
866/**
867 * Clears the page structure.
868 * @param a_pPage Pointer to the physical guest page tracking structure.
869 */
870#define PGM_PAGE_CLEAR(a_pPage) \
871 do { \
872 (a_pPage)->au64[0] = 0; \
873 (a_pPage)->au64[1] = 0; \
874 } while (0)
875
876/**
877 * Initializes the page structure.
878 * @param a_pPage Pointer to the physical guest page tracking structure.
879 * @param a_HCPhys The host physical address of the page.
880 * @param a_idPage The (GMM) page ID of the page.
881 * @param a_uType The page type (PGMPAGETYPE).
882 * @param a_uState The page state (PGM_PAGE_STATE_XXX).
883 */
884#define PGM_PAGE_INIT(a_pPage, a_HCPhys, a_idPage, a_uType, a_uState) \
885 do { \
886 RTHCPHYS SetHCPhysTmp = (a_HCPhys); \
887 AssertFatal(!(SetHCPhysTmp & ~UINT64_C(0x0000fffffffff000))); \
888 (a_pPage)->au64[0] = SetHCPhysTmp; \
889 (a_pPage)->au64[1] = 0; \
890 (a_pPage)->s.idPage = (a_idPage); \
891 (a_pPage)->s.uStateY = (a_uState); \
892 (a_pPage)->s.uTypeY = (a_uType); \
893 } while (0)
894
895/**
896 * Initializes the page structure of a ZERO page.
897 * @param a_pPage Pointer to the physical guest page tracking structure.
898 * @param a_pVM The VM handle (for getting the zero page address).
899 * @param a_uType The page type (PGMPAGETYPE).
900 */
901#define PGM_PAGE_INIT_ZERO(a_pPage, a_pVM, a_uType) \
902 PGM_PAGE_INIT((a_pPage), (a_pVM)->pgm.s.HCPhysZeroPg, NIL_GMM_PAGEID, (a_uType), PGM_PAGE_STATE_ZERO)
903
904
905/** @name The Page state, PGMPAGE::uStateY.
906 * @{ */
907/** The zero page.
908 * This is a per-VM page that's never ever mapped writable. */
909#define PGM_PAGE_STATE_ZERO 0
910/** A allocated page.
911 * This is a per-VM page allocated from the page pool (or wherever
912 * we get MMIO2 pages from if the type is MMIO2).
913 */
914#define PGM_PAGE_STATE_ALLOCATED 1
915/** A allocated page that's being monitored for writes.
916 * The shadow page table mappings are read-only. When a write occurs, the
917 * fWrittenTo member is set, the page remapped as read-write and the state
918 * moved back to allocated. */
919#define PGM_PAGE_STATE_WRITE_MONITORED 2
920/** The page is shared, aka. copy-on-write.
921 * This is a page that's shared with other VMs. */
922#define PGM_PAGE_STATE_SHARED 3
923/** The page is ballooned, so no longer available for this VM. */
924#define PGM_PAGE_STATE_BALLOONED 4
925/** @} */
926
927
928/** Asserts lock ownership in some of the PGM_PAGE_XXX macros. */
929#if defined(VBOX_STRICT) && 0 /** @todo triggers in pgmRZDynMapGCPageV2Inlined */
930# define PGM_PAGE_ASSERT_LOCK(a_pVM) PGM_LOCK_ASSERT_OWNER(a_pVM)
931#else
932# define PGM_PAGE_ASSERT_LOCK(a_pVM) do { } while (0)
933#endif
934
935/**
936 * Gets the page state.
937 * @returns page state (PGM_PAGE_STATE_*).
938 * @param a_pPage Pointer to the physical guest page tracking structure.
939 *
940 * @remarks See PGM_PAGE_GET_HCPHYS_NA for remarks about GCC and strict
941 * builds.
942 */
943#define PGM_PAGE_GET_STATE_NA(a_pPage) ( (a_pPage)->s.uStateY )
944#if defined(__GNUC__) && defined(VBOX_STRICT)
945# define PGM_PAGE_GET_STATE(a_pPage) __extension__ ({ PGM_PAGE_ASSERT_LOCK(pVM); PGM_PAGE_GET_STATE_NA(a_pPage); })
946#else
947# define PGM_PAGE_GET_STATE PGM_PAGE_GET_STATE_NA
948#endif
949
950/**
951 * Sets the page state.
952 * @param a_pVM The VM handle, only used for lock ownership assertions.
953 * @param a_pPage Pointer to the physical guest page tracking structure.
954 * @param a_uState The new page state.
955 */
956#define PGM_PAGE_SET_STATE(a_pVM, a_pPage, a_uState) \
957 do { (a_pPage)->s.uStateY = (a_uState); PGM_PAGE_ASSERT_LOCK(a_pVM); } while (0)
958
959
960/**
961 * Gets the host physical address of the guest page.
962 * @returns host physical address (RTHCPHYS).
963 * @param a_pPage Pointer to the physical guest page tracking structure.
964 *
965 * @remarks In strict builds on gcc platforms, this macro will make some ugly
966 * assumption about a valid pVM variable/parameter being in the
967 * current context. It will use this pVM variable to assert that the
968 * PGM lock is held. Use the PGM_PAGE_GET_HCPHYS_NA in contexts where
969 * pVM is not around.
970 */
971#if 0
972# define PGM_PAGE_GET_HCPHYS_NA(a_pPage) ( (a_pPage)->s.HCPhysFN << 12 )
973# define PGM_PAGE_GET_HCPHYS PGM_PAGE_GET_HCPHYS_NA
974#else
975# define PGM_PAGE_GET_HCPHYS_NA(a_pPage) ( (a_pPage)->au64[0] & UINT64_C(0x0000fffffffff000) )
976# if defined(__GNUC__) && defined(VBOX_STRICT)
977# define PGM_PAGE_GET_HCPHYS(a_pPage) __extension__ ({ PGM_PAGE_ASSERT_LOCK(pVM); PGM_PAGE_GET_HCPHYS_NA(a_pPage); })
978# else
979# define PGM_PAGE_GET_HCPHYS PGM_PAGE_GET_HCPHYS_NA
980# endif
981#endif
982
983/**
984 * Sets the host physical address of the guest page.
985 *
986 * @param a_pVM The VM handle, only used for lock ownership assertions.
987 * @param a_pPage Pointer to the physical guest page tracking structure.
988 * @param a_HCPhys The new host physical address.
989 */
990#define PGM_PAGE_SET_HCPHYS(a_pVM, a_pPage, a_HCPhys) \
991 do { \
992 RTHCPHYS const SetHCPhysTmp = (a_HCPhys); \
993 AssertFatal(!(SetHCPhysTmp & ~UINT64_C(0x0000fffffffff000))); \
994 (a_pPage)->s.HCPhysFN = SetHCPhysTmp >> 12; \
995 PGM_PAGE_ASSERT_LOCK(a_pVM); \
996 } while (0)
997
998/**
999 * Get the Page ID.
1000 * @returns The Page ID; NIL_GMM_PAGEID if it's a ZERO page.
1001 * @param a_pPage Pointer to the physical guest page tracking structure.
1002 */
1003#define PGM_PAGE_GET_PAGEID(a_pPage) ( (uint32_t)(a_pPage)->s.idPage )
1004
1005/**
1006 * Sets the Page ID.
1007 * @param a_pVM The VM handle, only used for lock ownership assertions.
1008 * @param a_pPage Pointer to the physical guest page tracking structure.
1009 * @param a_idPage The new page ID.
1010 */
1011#define PGM_PAGE_SET_PAGEID(a_pVM, a_pPage, a_idPage) \
1012 do { \
1013 (a_pPage)->s.idPage = (a_idPage); \
1014 PGM_PAGE_ASSERT_LOCK(a_pVM); \
1015 } while (0)
1016
1017/**
1018 * Get the Chunk ID.
1019 * @returns The Chunk ID; NIL_GMM_CHUNKID if it's a ZERO page.
1020 * @param a_pPage Pointer to the physical guest page tracking structure.
1021 */
1022#define PGM_PAGE_GET_CHUNKID(a_pPage) ( PGM_PAGE_GET_PAGEID(a_pPage) >> GMM_CHUNKID_SHIFT )
1023
1024/**
1025 * Get the index of the page within the allocation chunk.
1026 * @returns The page index.
1027 * @param a_pPage Pointer to the physical guest page tracking structure.
1028 */
1029#define PGM_PAGE_GET_PAGE_IN_CHUNK(a_pPage) ( PGM_PAGE_GET_PAGEID(a_pPage) & GMM_PAGEID_IDX_MASK )
1030
1031/**
1032 * Gets the page type.
1033 * @returns The page type.
1034 * @param a_pPage Pointer to the physical guest page tracking structure.
1035 *
1036 * @remarks See PGM_PAGE_GET_HCPHYS_NA for remarks about GCC and strict
1037 * builds.
1038 */
1039#define PGM_PAGE_GET_TYPE_NA(a_pPage) ( (a_pPage)->s.uTypeY )
1040#if defined(__GNUC__) && defined(VBOX_STRICT)
1041# define PGM_PAGE_GET_TYPE(a_pPage) __extension__ ({ PGM_PAGE_ASSERT_LOCK(pVM); PGM_PAGE_GET_TYPE_NA(a_pPage); })
1042#else
1043# define PGM_PAGE_GET_TYPE PGM_PAGE_GET_TYPE_NA
1044#endif
1045
1046/**
1047 * Sets the page type.
1048 *
1049 * @param a_pVM The VM handle, only used for lock ownership assertions.
1050 * @param a_pPage Pointer to the physical guest page tracking structure.
1051 * @param a_enmType The new page type (PGMPAGETYPE).
1052 */
1053#define PGM_PAGE_SET_TYPE(a_pVM, a_pPage, a_enmType) \
1054 do { (a_pPage)->s.uTypeY = (a_enmType); PGM_PAGE_ASSERT_LOCK(a_pVM); } while (0)
1055
1056/**
1057 * Gets the page table index
1058 * @returns The page table index.
1059 * @param a_pPage Pointer to the physical guest page tracking structure.
1060 */
1061#define PGM_PAGE_GET_PTE_INDEX(a_pPage) ( (a_pPage)->s.u10PteIdx )
1062
1063/**
1064 * Sets the page table index.
1065 * @param a_pVM The VM handle, only used for lock ownership assertions.
1066 * @param a_pPage Pointer to the physical guest page tracking structure.
1067 * @param a_iPte New page table index.
1068 */
1069#define PGM_PAGE_SET_PTE_INDEX(a_pVM, a_pPage, a_iPte) \
1070 do { (a_pPage)->s.u10PteIdx = (a_iPte); PGM_PAGE_ASSERT_LOCK(a_pVM); } while (0)
1071
1072/**
1073 * Checks if the page is marked for MMIO, no MMIO2 aliasing.
1074 * @returns true/false.
1075 * @param a_pPage Pointer to the physical guest page tracking structure.
1076 */
1077#define PGM_PAGE_IS_MMIO(a_pPage) ( (a_pPage)->s.uTypeY == PGMPAGETYPE_MMIO )
1078
1079/**
1080 * Checks if the page is marked for MMIO, including both aliases.
1081 * @returns true/false.
1082 * @param a_pPage Pointer to the physical guest page tracking structure.
1083 */
1084#define PGM_PAGE_IS_MMIO_OR_ALIAS(a_pPage) ( (a_pPage)->s.uTypeY == PGMPAGETYPE_MMIO \
1085 || (a_pPage)->s.uTypeY == PGMPAGETYPE_MMIO2_ALIAS_MMIO \
1086 || (a_pPage)->s.uTypeY == PGMPAGETYPE_SPECIAL_ALIAS_MMIO \
1087 )
1088
1089/**
1090 * Checks if the page is marked for MMIO, including special aliases.
1091 * @returns true/false.
1092 * @param a_pPage Pointer to the physical guest page tracking structure.
1093 */
1094#define PGM_PAGE_IS_MMIO_OR_SPECIAL_ALIAS(a_pPage) ( (a_pPage)->s.uTypeY == PGMPAGETYPE_MMIO \
1095 || (a_pPage)->s.uTypeY == PGMPAGETYPE_SPECIAL_ALIAS_MMIO )
1096
1097/**
1098 * Checks if the page is a special aliased MMIO page.
1099 * @returns true/false.
1100 * @param a_pPage Pointer to the physical guest page tracking structure.
1101 */
1102#define PGM_PAGE_IS_SPECIAL_ALIAS_MMIO(a_pPage) ( (a_pPage)->s.uTypeY == PGMPAGETYPE_SPECIAL_ALIAS_MMIO )
1103
1104/**
1105 * Checks if the page is backed by the ZERO page.
1106 * @returns true/false.
1107 * @param a_pPage Pointer to the physical guest page tracking structure.
1108 */
1109#define PGM_PAGE_IS_ZERO(a_pPage) ( (a_pPage)->s.uStateY == PGM_PAGE_STATE_ZERO )
1110
1111/**
1112 * Checks if the page is backed by a SHARED page.
1113 * @returns true/false.
1114 * @param a_pPage Pointer to the physical guest page tracking structure.
1115 */
1116#define PGM_PAGE_IS_SHARED(a_pPage) ( (a_pPage)->s.uStateY == PGM_PAGE_STATE_SHARED )
1117
1118/**
1119 * Checks if the page is ballooned.
1120 * @returns true/false.
1121 * @param a_pPage Pointer to the physical guest page tracking structure.
1122 */
1123#define PGM_PAGE_IS_BALLOONED(a_pPage) ( (a_pPage)->s.uStateY == PGM_PAGE_STATE_BALLOONED )
1124
1125/**
1126 * Checks if the page is allocated.
1127 * @returns true/false.
1128 * @param a_pPage Pointer to the physical guest page tracking structure.
1129 */
1130#define PGM_PAGE_IS_ALLOCATED(a_pPage) ( (a_pPage)->s.uStateY == PGM_PAGE_STATE_ALLOCATED )
1131
1132/**
1133 * Marks the page as written to (for GMM change monitoring).
1134 * @param a_pVM The VM handle, only used for lock ownership assertions.
1135 * @param a_pPage Pointer to the physical guest page tracking structure.
1136 */
1137#define PGM_PAGE_SET_WRITTEN_TO(a_pVM, a_pPage) \
1138 do { (a_pPage)->s.fWrittenToY = 1; PGM_PAGE_ASSERT_LOCK(a_pVM); } while (0)
1139
1140/**
1141 * Clears the written-to indicator.
1142 * @param a_pVM The VM handle, only used for lock ownership assertions.
1143 * @param a_pPage Pointer to the physical guest page tracking structure.
1144 */
1145#define PGM_PAGE_CLEAR_WRITTEN_TO(a_pVM, a_pPage) \
1146 do { (a_pPage)->s.fWrittenToY = 0; PGM_PAGE_ASSERT_LOCK(a_pVM); } while (0)
1147
1148/**
1149 * Checks if the page was marked as written-to.
1150 * @returns true/false.
1151 * @param a_pPage Pointer to the physical guest page tracking structure.
1152 */
1153#define PGM_PAGE_IS_WRITTEN_TO(a_pPage) ( (a_pPage)->s.fWrittenToY )
1154
1155/**
1156 * Marks the page as dirty for FTM
1157 * @param a_pPage Pointer to the physical guest page tracking structure.
1158 */
1159#define PGM_PAGE_SET_FT_DIRTY(a_pPage) do { (a_pPage)->s.fFTDirtyY = 1; } while (0)
1160
1161/**
1162 * Clears the FTM dirty indicator
1163 * @param a_pPage Pointer to the physical guest page tracking structure.
1164 */
1165#define PGM_PAGE_CLEAR_FT_DIRTY(a_pPage) do { (a_pPage)->s.fFTDirtyY = 0; } while (0)
1166
1167/**
1168 * Checks if the page was marked as dirty for FTM
1169 * @returns true/false.
1170 * @param a_pPage Pointer to the physical guest page tracking structure.
1171 */
1172#define PGM_PAGE_IS_FT_DIRTY(a_pPage) ( (a_pPage)->s.fFTDirtyY )
1173
1174
1175/** @name PT usage values (PGMPAGE::u2PDEType).
1176 *
1177 * @{ */
1178/** Either as a PT or PDE. */
1179#define PGM_PAGE_PDE_TYPE_DONTCARE 0
1180/** Must use a page table to map the range. */
1181#define PGM_PAGE_PDE_TYPE_PT 1
1182/** Can use a page directory entry to map the continuous range. */
1183#define PGM_PAGE_PDE_TYPE_PDE 2
1184/** Can use a page directory entry to map the continuous range - temporarily disabled (by page monitoring). */
1185#define PGM_PAGE_PDE_TYPE_PDE_DISABLED 3
1186/** @} */
1187
1188/**
1189 * Set the PDE type of the page
1190 * @param a_pVM The VM handle, only used for lock ownership assertions.
1191 * @param a_pPage Pointer to the physical guest page tracking structure.
1192 * @param a_uType PGM_PAGE_PDE_TYPE_*.
1193 */
1194#define PGM_PAGE_SET_PDE_TYPE(a_pVM, a_pPage, a_uType) \
1195 do { (a_pPage)->s.u2PDETypeY = (a_uType); PGM_PAGE_ASSERT_LOCK(a_pVM); } while (0)
1196
1197/**
1198 * Checks if the page was marked being part of a large page
1199 * @returns true/false.
1200 * @param a_pPage Pointer to the physical guest page tracking structure.
1201 */
1202#define PGM_PAGE_GET_PDE_TYPE(a_pPage) ( (a_pPage)->s.u2PDETypeY )
1203
1204/** Enabled optimized access handler tests.
1205 * These optimizations makes ASSUMPTIONS about the state values and the s1
1206 * layout. When enabled, the compiler should normally generate more compact
1207 * code.
1208 */
1209#define PGM_PAGE_WITH_OPTIMIZED_HANDLER_ACCESS 1
1210
1211/** @name Physical Access Handler State values (PGMPAGE::u2HandlerPhysStateY).
1212 *
1213 * @remarks The values are assigned in order of priority, so we can calculate
1214 * the correct state for a page with different handlers installed.
1215 * @{ */
1216/** No handler installed. */
1217#define PGM_PAGE_HNDL_PHYS_STATE_NONE 0
1218/** Monitoring is temporarily disabled. */
1219#define PGM_PAGE_HNDL_PHYS_STATE_DISABLED 1
1220/** Write access is monitored. */
1221#define PGM_PAGE_HNDL_PHYS_STATE_WRITE 2
1222/** All access is monitored. */
1223#define PGM_PAGE_HNDL_PHYS_STATE_ALL 3
1224/** @} */
1225
1226/**
1227 * Gets the physical access handler state of a page.
1228 * @returns PGM_PAGE_HNDL_PHYS_STATE_* value.
1229 * @param a_pPage Pointer to the physical guest page tracking structure.
1230 */
1231#define PGM_PAGE_GET_HNDL_PHYS_STATE(a_pPage) ( (a_pPage)->s.u2HandlerPhysStateY )
1232
1233/**
1234 * Sets the physical access handler state of a page.
1235 * @param a_pPage Pointer to the physical guest page tracking structure.
1236 * @param a_uState The new state value.
1237 */
1238#define PGM_PAGE_SET_HNDL_PHYS_STATE(a_pPage, a_uState) \
1239 do { (a_pPage)->s.u2HandlerPhysStateY = (a_uState); } while (0)
1240
1241/**
1242 * Checks if the page has any physical access handlers, including temporarily disabled ones.
1243 * @returns true/false
1244 * @param a_pPage Pointer to the physical guest page tracking structure.
1245 */
1246#define PGM_PAGE_HAS_ANY_PHYSICAL_HANDLERS(a_pPage) \
1247 ( PGM_PAGE_GET_HNDL_PHYS_STATE(a_pPage) != PGM_PAGE_HNDL_PHYS_STATE_NONE )
1248
1249/**
1250 * Checks if the page has any active physical access handlers.
1251 * @returns true/false
1252 * @param a_pPage Pointer to the physical guest page tracking structure.
1253 */
1254#define PGM_PAGE_HAS_ACTIVE_PHYSICAL_HANDLERS(a_pPage) \
1255 ( PGM_PAGE_GET_HNDL_PHYS_STATE(a_pPage) >= PGM_PAGE_HNDL_PHYS_STATE_WRITE )
1256
1257
1258/** @name Virtual Access Handler State values (PGMPAGE::u2HandlerVirtStateY).
1259 *
1260 * @remarks The values are assigned in order of priority, so we can calculate
1261 * the correct state for a page with different handlers installed.
1262 * @{ */
1263/** No handler installed. */
1264#define PGM_PAGE_HNDL_VIRT_STATE_NONE 0
1265/* 1 is reserved so the lineup is identical with the physical ones. */
1266/** Write access is monitored. */
1267#define PGM_PAGE_HNDL_VIRT_STATE_WRITE 2
1268/** All access is monitored. */
1269#define PGM_PAGE_HNDL_VIRT_STATE_ALL 3
1270/** @} */
1271
1272/**
1273 * Gets the virtual access handler state of a page.
1274 * @returns PGM_PAGE_HNDL_VIRT_STATE_* value.
1275 * @param a_pPage Pointer to the physical guest page tracking structure.
1276 */
1277#define PGM_PAGE_GET_HNDL_VIRT_STATE(a_pPage) ( (a_pPage)->s.u2HandlerVirtStateY )
1278
1279/**
1280 * Sets the virtual access handler state of a page.
1281 * @param a_pPage Pointer to the physical guest page tracking structure.
1282 * @param a_uState The new state value.
1283 */
1284#define PGM_PAGE_SET_HNDL_VIRT_STATE(a_pPage, a_uState) \
1285 do { (a_pPage)->s.u2HandlerVirtStateY = (a_uState); } while (0)
1286
1287/**
1288 * Checks if the page has any virtual access handlers.
1289 * @returns true/false
1290 * @param a_pPage Pointer to the physical guest page tracking structure.
1291 */
1292#define PGM_PAGE_HAS_ANY_VIRTUAL_HANDLERS(a_pPage) \
1293 ( PGM_PAGE_GET_HNDL_VIRT_STATE(a_pPage) != PGM_PAGE_HNDL_VIRT_STATE_NONE )
1294
1295/**
1296 * Same as PGM_PAGE_HAS_ANY_VIRTUAL_HANDLERS - can't disable pages in
1297 * virtual handlers.
1298 * @returns true/false
1299 * @param a_pPage Pointer to the physical guest page tracking structure.
1300 */
1301#define PGM_PAGE_HAS_ACTIVE_VIRTUAL_HANDLERS(a_pPage) \
1302 PGM_PAGE_HAS_ANY_VIRTUAL_HANDLERS(a_pPage)
1303
1304
1305/**
1306 * Checks if the page has any access handlers, including temporarily disabled ones.
1307 * @returns true/false
1308 * @param a_pPage Pointer to the physical guest page tracking structure.
1309 */
1310#ifdef PGM_PAGE_WITH_OPTIMIZED_HANDLER_ACCESS
1311# define PGM_PAGE_HAS_ANY_HANDLERS(a_pPage) \
1312 ( ((a_pPage)->au32[0] & UINT16_C(0x0303)) != 0 )
1313#else
1314# define PGM_PAGE_HAS_ANY_HANDLERS(a_pPage) \
1315 ( PGM_PAGE_GET_HNDL_PHYS_STATE(a_pPage) != PGM_PAGE_HNDL_PHYS_STATE_NONE \
1316 || PGM_PAGE_GET_HNDL_VIRT_STATE(a_pPage) != PGM_PAGE_HNDL_VIRT_STATE_NONE )
1317#endif
1318
1319/**
1320 * Checks if the page has any active access handlers.
1321 * @returns true/false
1322 * @param a_pPage Pointer to the physical guest page tracking structure.
1323 */
1324#ifdef PGM_PAGE_WITH_OPTIMIZED_HANDLER_ACCESS
1325# define PGM_PAGE_HAS_ACTIVE_HANDLERS(a_pPage) \
1326 ( ((a_pPage)->au32[0] & UINT16_C(0x0202)) != 0 )
1327#else
1328# define PGM_PAGE_HAS_ACTIVE_HANDLERS(a_pPage) \
1329 ( PGM_PAGE_GET_HNDL_PHYS_STATE(a_pPage) >= PGM_PAGE_HNDL_PHYS_STATE_WRITE \
1330 || PGM_PAGE_GET_HNDL_VIRT_STATE(a_pPage) >= PGM_PAGE_HNDL_VIRT_STATE_WRITE )
1331#endif
1332
1333/**
1334 * Checks if the page has any active access handlers catching all accesses.
1335 * @returns true/false
1336 * @param a_pPage Pointer to the physical guest page tracking structure.
1337 */
1338#ifdef PGM_PAGE_WITH_OPTIMIZED_HANDLER_ACCESS
1339# define PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(a_pPage) \
1340 ( ( ((a_pPage)->au8[0] | (a_pPage)->au8[1]) & UINT8_C(0x3) ) \
1341 == PGM_PAGE_HNDL_PHYS_STATE_ALL )
1342#else
1343# define PGM_PAGE_HAS_ACTIVE_ALL_HANDLERS(a_pPage) \
1344 ( PGM_PAGE_GET_HNDL_PHYS_STATE(a_pPage) == PGM_PAGE_HNDL_PHYS_STATE_ALL \
1345 || PGM_PAGE_GET_HNDL_VIRT_STATE(a_pPage) == PGM_PAGE_HNDL_VIRT_STATE_ALL )
1346#endif
1347
1348
1349/** @def PGM_PAGE_GET_TRACKING
1350 * Gets the packed shadow page pool tracking data associated with a guest page.
1351 * @returns uint16_t containing the data.
1352 * @param a_pPage Pointer to the physical guest page tracking structure.
1353 */
1354#define PGM_PAGE_GET_TRACKING_NA(a_pPage) ( (a_pPage)->s.u16TrackingY )
1355#if defined(__GNUC__) && defined(VBOX_STRICT)
1356# define PGM_PAGE_GET_TRACKING(a_pPage) __extension__ ({ PGM_PAGE_ASSERT_LOCK(pVM); PGM_PAGE_GET_TRACKING_NA(a_pPage); })
1357#else
1358# define PGM_PAGE_GET_TRACKING PGM_PAGE_GET_TRACKING_NA
1359#endif
1360
1361/** @def PGM_PAGE_SET_TRACKING
1362 * Sets the packed shadow page pool tracking data associated with a guest page.
1363 * @param a_pVM The VM handle, only used for lock ownership assertions.
1364 * @param a_pPage Pointer to the physical guest page tracking structure.
1365 * @param a_u16TrackingData The tracking data to store.
1366 */
1367#define PGM_PAGE_SET_TRACKING(a_pVM, a_pPage, a_u16TrackingData) \
1368 do { (a_pPage)->s.u16TrackingY = (a_u16TrackingData); PGM_PAGE_ASSERT_LOCK(a_pVM); } while (0)
1369
1370/** @def PGM_PAGE_GET_TD_CREFS
1371 * Gets the @a cRefs tracking data member.
1372 * @returns cRefs.
1373 * @param a_pPage Pointer to the physical guest page tracking structure.
1374 */
1375#define PGM_PAGE_GET_TD_CREFS(a_pPage) \
1376 ((PGM_PAGE_GET_TRACKING(a_pPage) >> PGMPOOL_TD_CREFS_SHIFT) & PGMPOOL_TD_CREFS_MASK)
1377#define PGM_PAGE_GET_TD_CREFS_NA(a_pPage) \
1378 ((PGM_PAGE_GET_TRACKING_NA(a_pPage) >> PGMPOOL_TD_CREFS_SHIFT) & PGMPOOL_TD_CREFS_MASK)
1379
1380/** @def PGM_PAGE_GET_TD_IDX
1381 * Gets the @a idx tracking data member.
1382 * @returns idx.
1383 * @param a_pPage Pointer to the physical guest page tracking structure.
1384 */
1385#define PGM_PAGE_GET_TD_IDX(a_pPage) \
1386 ((PGM_PAGE_GET_TRACKING(a_pPage) >> PGMPOOL_TD_IDX_SHIFT) & PGMPOOL_TD_IDX_MASK)
1387#define PGM_PAGE_GET_TD_IDX_NA(a_pPage) \
1388 ((PGM_PAGE_GET_TRACKING_NA(a_pPage) >> PGMPOOL_TD_IDX_SHIFT) & PGMPOOL_TD_IDX_MASK)
1389
1390
1391/** Max number of locks on a page. */
1392#define PGM_PAGE_MAX_LOCKS UINT8_C(254)
1393
1394/** Get the read lock count.
1395 * @returns count.
1396 * @param a_pPage Pointer to the physical guest page tracking structure.
1397 */
1398#define PGM_PAGE_GET_READ_LOCKS(a_pPage) ( (a_pPage)->s.cReadLocksY )
1399
1400/** Get the write lock count.
1401 * @returns count.
1402 * @param a_pPage Pointer to the physical guest page tracking structure.
1403 */
1404#define PGM_PAGE_GET_WRITE_LOCKS(a_pPage) ( (a_pPage)->s.cWriteLocksY )
1405
1406/** Decrement the read lock counter.
1407 * @param a_pPage Pointer to the physical guest page tracking structure.
1408 */
1409#define PGM_PAGE_DEC_READ_LOCKS(a_pPage) do { --(a_pPage)->s.cReadLocksY; } while (0)
1410
1411/** Decrement the write lock counter.
1412 * @param a_pPage Pointer to the physical guest page tracking structure.
1413 */
1414#define PGM_PAGE_DEC_WRITE_LOCKS(a_pPage) do { --(a_pPage)->s.cWriteLocksY; } while (0)
1415
1416/** Increment the read lock counter.
1417 * @param a_pPage Pointer to the physical guest page tracking structure.
1418 */
1419#define PGM_PAGE_INC_READ_LOCKS(a_pPage) do { ++(a_pPage)->s.cReadLocksY; } while (0)
1420
1421/** Increment the write lock counter.
1422 * @param a_pPage Pointer to the physical guest page tracking structure.
1423 */
1424#define PGM_PAGE_INC_WRITE_LOCKS(a_pPage) do { ++(a_pPage)->s.cWriteLocksY; } while (0)
1425
1426
1427#if 0
1428/** Enables sanity checking of write monitoring using CRC-32. */
1429# define PGMLIVESAVERAMPAGE_WITH_CRC32
1430#endif
1431
1432/**
1433 * Per page live save tracking data.
1434 */
1435typedef struct PGMLIVESAVERAMPAGE
1436{
1437 /** Number of times it has been dirtied. */
1438 uint32_t cDirtied : 24;
1439 /** Whether it is currently dirty. */
1440 uint32_t fDirty : 1;
1441 /** Ignore the page.
1442 * This is used for pages that has been MMIO, MMIO2 or ROM pages once. We will
1443 * deal with these after pausing the VM and DevPCI have said it bit about
1444 * remappings. */
1445 uint32_t fIgnore : 1;
1446 /** Was a ZERO page last time around. */
1447 uint32_t fZero : 1;
1448 /** Was a SHARED page last time around. */
1449 uint32_t fShared : 1;
1450 /** Whether the page is/was write monitored in a previous pass. */
1451 uint32_t fWriteMonitored : 1;
1452 /** Whether the page is/was write monitored earlier in this pass. */
1453 uint32_t fWriteMonitoredJustNow : 1;
1454 /** Bits reserved for future use. */
1455 uint32_t u2Reserved : 2;
1456#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1457 /** CRC-32 for the page. This is for internal consistency checks. */
1458 uint32_t u32Crc;
1459#endif
1460} PGMLIVESAVERAMPAGE;
1461#ifdef PGMLIVESAVERAMPAGE_WITH_CRC32
1462AssertCompileSize(PGMLIVESAVERAMPAGE, 8);
1463#else
1464AssertCompileSize(PGMLIVESAVERAMPAGE, 4);
1465#endif
1466/** Pointer to the per page live save tracking data. */
1467typedef PGMLIVESAVERAMPAGE *PPGMLIVESAVERAMPAGE;
1468
1469/** The max value of PGMLIVESAVERAMPAGE::cDirtied. */
1470#define PGMLIVSAVEPAGE_MAX_DIRTIED 0x00fffff0
1471
1472
1473/**
1474 * RAM range for GC Phys to HC Phys conversion.
1475 *
1476 * Can be used for HC Virt to GC Phys and HC Virt to HC Phys
1477 * conversions too, but we'll let MM handle that for now.
1478 *
1479 * This structure is used by linked lists in both GC and HC.
1480 */
1481typedef struct PGMRAMRANGE
1482{
1483 /** Start of the range. Page aligned. */
1484 RTGCPHYS GCPhys;
1485 /** Size of the range. (Page aligned of course). */
1486 RTGCPHYS cb;
1487 /** Pointer to the next RAM range - for R3. */
1488 R3PTRTYPE(struct PGMRAMRANGE *) pNextR3;
1489 /** Pointer to the next RAM range - for R0. */
1490 R0PTRTYPE(struct PGMRAMRANGE *) pNextR0;
1491 /** Pointer to the next RAM range - for RC. */
1492 RCPTRTYPE(struct PGMRAMRANGE *) pNextRC;
1493 /** PGM_RAM_RANGE_FLAGS_* flags. */
1494 uint32_t fFlags;
1495 /** Last address in the range (inclusive). Page aligned (-1). */
1496 RTGCPHYS GCPhysLast;
1497 /** Start of the HC mapping of the range. This is only used for MMIO2. */
1498 R3PTRTYPE(void *) pvR3;
1499 /** Live save per page tracking data. */
1500 R3PTRTYPE(PPGMLIVESAVERAMPAGE) paLSPages;
1501 /** The range description. */
1502 R3PTRTYPE(const char *) pszDesc;
1503 /** Pointer to self - R0 pointer. */
1504 R0PTRTYPE(struct PGMRAMRANGE *) pSelfR0;
1505 /** Pointer to self - RC pointer. */
1506 RCPTRTYPE(struct PGMRAMRANGE *) pSelfRC;
1507
1508 /** Alignment padding. */
1509 RTRCPTR Alignment0;
1510 /** Pointer to the left search three node - ring-3 context. */
1511 R3PTRTYPE(struct PGMRAMRANGE *) pLeftR3;
1512 /** Pointer to the right search three node - ring-3 context. */
1513 R3PTRTYPE(struct PGMRAMRANGE *) pRightR3;
1514 /** Pointer to the left search three node - ring-0 context. */
1515 R0PTRTYPE(struct PGMRAMRANGE *) pLeftR0;
1516 /** Pointer to the right search three node - ring-0 context. */
1517 R0PTRTYPE(struct PGMRAMRANGE *) pRightR0;
1518 /** Pointer to the left search three node - raw-mode context. */
1519 RCPTRTYPE(struct PGMRAMRANGE *) pLeftRC;
1520 /** Pointer to the right search three node - raw-mode context. */
1521 RCPTRTYPE(struct PGMRAMRANGE *) pRightRC;
1522
1523 /** Padding to make aPage aligned on sizeof(PGMPAGE). */
1524#if HC_ARCH_BITS == 32
1525 uint32_t au32Alignment2[HC_ARCH_BITS == 32 ? 2 : 0];
1526#endif
1527 /** Array of physical guest page tracking structures. */
1528 PGMPAGE aPages[1];
1529} PGMRAMRANGE;
1530/** Pointer to RAM range for GC Phys to HC Phys conversion. */
1531typedef PGMRAMRANGE *PPGMRAMRANGE;
1532
1533/** @name PGMRAMRANGE::fFlags
1534 * @{ */
1535/** The RAM range is floating around as an independent guest mapping. */
1536#define PGM_RAM_RANGE_FLAGS_FLOATING RT_BIT(20)
1537/** Ad hoc RAM range for an ROM mapping. */
1538#define PGM_RAM_RANGE_FLAGS_AD_HOC_ROM RT_BIT(21)
1539/** Ad hoc RAM range for an MMIO mapping. */
1540#define PGM_RAM_RANGE_FLAGS_AD_HOC_MMIO RT_BIT(22)
1541/** Ad hoc RAM range for an MMIO2 or pre-registered MMIO mapping. */
1542#define PGM_RAM_RANGE_FLAGS_AD_HOC_MMIO_EX RT_BIT(23)
1543/** @} */
1544
1545/** Tests if a RAM range is an ad hoc one or not.
1546 * @returns true/false.
1547 * @param pRam The RAM range.
1548 */
1549#define PGM_RAM_RANGE_IS_AD_HOC(pRam) \
1550 (!!( (pRam)->fFlags & (PGM_RAM_RANGE_FLAGS_AD_HOC_ROM | PGM_RAM_RANGE_FLAGS_AD_HOC_MMIO | PGM_RAM_RANGE_FLAGS_AD_HOC_MMIO_EX) ) )
1551
1552/** The number of entries in the RAM range TLBs (there is one for each
1553 * context). Must be a power of two. */
1554#define PGM_RAMRANGE_TLB_ENTRIES 8
1555
1556/**
1557 * Calculates the RAM range TLB index for the physical address.
1558 *
1559 * @returns RAM range TLB index.
1560 * @param a_GCPhys The guest physical address.
1561 */
1562#define PGM_RAMRANGE_TLB_IDX(a_GCPhys) ( ((a_GCPhys) >> 20) & (PGM_RAMRANGE_TLB_ENTRIES - 1) )
1563
1564
1565
1566/**
1567 * Per page tracking structure for ROM image.
1568 *
1569 * A ROM image may have a shadow page, in which case we may have two pages
1570 * backing it. This structure contains the PGMPAGE for both while
1571 * PGMRAMRANGE have a copy of the active one. It is important that these
1572 * aren't out of sync in any regard other than page pool tracking data.
1573 */
1574typedef struct PGMROMPAGE
1575{
1576 /** The page structure for the virgin ROM page. */
1577 PGMPAGE Virgin;
1578 /** The page structure for the shadow RAM page. */
1579 PGMPAGE Shadow;
1580 /** The current protection setting. */
1581 PGMROMPROT enmProt;
1582 /** Live save status information. Makes use of unused alignment space. */
1583 struct
1584 {
1585 /** The previous protection value. */
1586 uint8_t u8Prot;
1587 /** Written to flag set by the handler. */
1588 bool fWrittenTo;
1589 /** Whether the shadow page is dirty or not. */
1590 bool fDirty;
1591 /** Whether it was dirtied in the recently. */
1592 bool fDirtiedRecently;
1593 } LiveSave;
1594} PGMROMPAGE;
1595AssertCompileSizeAlignment(PGMROMPAGE, 8);
1596/** Pointer to a ROM page tracking structure. */
1597typedef PGMROMPAGE *PPGMROMPAGE;
1598
1599
1600/**
1601 * A registered ROM image.
1602 *
1603 * This is needed to keep track of ROM image since they generally intrude
1604 * into a PGMRAMRANGE. It also keeps track of additional info like the
1605 * two page sets (read-only virgin and read-write shadow), the current
1606 * state of each page.
1607 *
1608 * Because access handlers cannot easily be executed in a different
1609 * context, the ROM ranges needs to be accessible and in all contexts.
1610 */
1611typedef struct PGMROMRANGE
1612{
1613 /** Pointer to the next range - R3. */
1614 R3PTRTYPE(struct PGMROMRANGE *) pNextR3;
1615 /** Pointer to the next range - R0. */
1616 R0PTRTYPE(struct PGMROMRANGE *) pNextR0;
1617 /** Pointer to the next range - RC. */
1618 RCPTRTYPE(struct PGMROMRANGE *) pNextRC;
1619 /** Pointer alignment */
1620 RTRCPTR RCPtrAlignment;
1621 /** Address of the range. */
1622 RTGCPHYS GCPhys;
1623 /** Address of the last byte in the range. */
1624 RTGCPHYS GCPhysLast;
1625 /** Size of the range. */
1626 RTGCPHYS cb;
1627 /** The flags (PGMPHYS_ROM_FLAGS_*). */
1628 uint32_t fFlags;
1629 /** The saved state range ID. */
1630 uint8_t idSavedState;
1631 /** Alignment padding. */
1632 uint8_t au8Alignment[3];
1633 /** Alignment padding ensuring that aPages is sizeof(PGMROMPAGE) aligned. */
1634 uint32_t au32Alignemnt[HC_ARCH_BITS == 32 ? 5 : 1];
1635 /** The size bits pvOriginal points to. */
1636 uint32_t cbOriginal;
1637 /** Pointer to the original bits when PGMPHYS_ROM_FLAGS_PERMANENT_BINARY was specified.
1638 * This is used for strictness checks. */
1639 R3PTRTYPE(const void *) pvOriginal;
1640 /** The ROM description. */
1641 R3PTRTYPE(const char *) pszDesc;
1642 /** The per page tracking structures. */
1643 PGMROMPAGE aPages[1];
1644} PGMROMRANGE;
1645/** Pointer to a ROM range. */
1646typedef PGMROMRANGE *PPGMROMRANGE;
1647
1648
1649/**
1650 * Live save per page data for an MMIO2 page.
1651 *
1652 * Not using PGMLIVESAVERAMPAGE here because we cannot use normal write monitoring
1653 * of MMIO2 pages. The current approach is using some optimistic SHA-1 +
1654 * CRC-32 for detecting changes as well as special handling of zero pages. This
1655 * is a TEMPORARY measure which isn't perfect, but hopefully it is good enough
1656 * for speeding things up. (We're using SHA-1 and not SHA-256 or SHA-512
1657 * because of speed (2.5x and 6x slower).)
1658 *
1659 * @todo Implement dirty MMIO2 page reporting that can be enabled during live
1660 * save but normally is disabled. Since we can write monitor guest
1661 * accesses on our own, we only need this for host accesses. Shouldn't be
1662 * too difficult for DevVGA, VMMDev might be doable, the planned
1663 * networking fun will be fun since it involves ring-0.
1664 */
1665typedef struct PGMLIVESAVEMMIO2PAGE
1666{
1667 /** Set if the page is considered dirty. */
1668 bool fDirty;
1669 /** The number of scans this page has remained unchanged for.
1670 * Only updated for dirty pages. */
1671 uint8_t cUnchangedScans;
1672 /** Whether this page was zero at the last scan. */
1673 bool fZero;
1674 /** Alignment padding. */
1675 bool fReserved;
1676 /** CRC-32 for the first half of the page.
1677 * This is used together with u32CrcH2 to quickly detect changes in the page
1678 * during the non-final passes. */
1679 uint32_t u32CrcH1;
1680 /** CRC-32 for the second half of the page. */
1681 uint32_t u32CrcH2;
1682 /** SHA-1 for the saved page.
1683 * This is used in the final pass to skip pages without changes. */
1684 uint8_t abSha1Saved[RTSHA1_HASH_SIZE];
1685} PGMLIVESAVEMMIO2PAGE;
1686/** Pointer to a live save status data for an MMIO2 page. */
1687typedef PGMLIVESAVEMMIO2PAGE *PPGMLIVESAVEMMIO2PAGE;
1688
1689/**
1690 * A registered MMIO2 (= Device RAM) or pre-registered MMIO range.
1691 *
1692 * There are a few reason why we need to keep track of these registrations. One
1693 * of them is the deregistration & cleanup stuff, while another is that the
1694 * PGMRAMRANGE associated with such a region may have to be removed from the ram
1695 * range list.
1696 *
1697 * Overlapping with a RAM range has to be 100% or none at all. The pages in the
1698 * existing RAM range must not be ROM nor MMIO. A guru meditation will be
1699 * raised if a partial overlap or an overlap of ROM pages is encountered. On an
1700 * overlap we will free all the existing RAM pages and put in the ram range
1701 * pages instead.
1702 */
1703typedef struct PGMREGMMIORANGE
1704{
1705 /** The owner of the range. (a device) */
1706 PPDMDEVINSR3 pDevInsR3;
1707 /** Pointer to the ring-3 mapping of the allocation, if MMIO2. */
1708 RTR3PTR pvR3;
1709 /** Pointer to the next range - R3. */
1710 R3PTRTYPE(struct PGMREGMMIORANGE *) pNextR3;
1711 /** Whether this is MMIO2 or plain MMIO. */
1712 bool fMmio2;
1713 /** Whether it's mapped or not. */
1714 bool fMapped;
1715 /** Whether it's overlapping or not. */
1716 bool fOverlapping;
1717 /** The PCI region number.
1718 * @remarks This ASSUMES that nobody will ever really need to have multiple
1719 * PCI devices with matching MMIO region numbers on a single device. */
1720 uint8_t iRegion;
1721 /** The saved state range ID. */
1722 uint8_t idSavedState;
1723 /** MMIO2 range identifier, for page IDs (PGMPAGE::s.idPage). */
1724 uint8_t idMmio2;
1725 /** Alignment padding for putting the ram range on a PGMPAGE alignment boundary. */
1726 uint8_t abAlignment[HC_ARCH_BITS == 32 ? 6 : 2];
1727 /** Pointer to the physical handler for MMIO. */
1728 R3PTRTYPE(PPGMPHYSHANDLER) pPhysHandlerR3;
1729 /** Live save per page tracking data for MMIO2. */
1730 R3PTRTYPE(PPGMLIVESAVEMMIO2PAGE) paLSPages;
1731 /** The associated RAM range. */
1732 PGMRAMRANGE RamRange;
1733} PGMREGMMIORANGE;
1734AssertCompileMemberAlignment(PGMREGMMIORANGE, RamRange, 16);
1735/** Pointer to a MMIO2 or pre-registered MMIO range. */
1736typedef PGMREGMMIORANGE *PPGMREGMMIORANGE;
1737
1738/** @name Internal MMIO2 constants.
1739 * @{ */
1740/** The maximum number of MMIO2 ranges. */
1741#define PGM_MMIO2_MAX_RANGES 8
1742/** The maximum number of pages in a MMIO2 range. */
1743#define PGM_MMIO2_MAX_PAGE_COUNT UINT32_C(0x01000000)
1744/** Makes a MMIO2 page ID out of a MMIO2 range ID and page index number. */
1745#define PGM_MMIO2_PAGEID_MAKE(a_idMmio2, a_iPage) ( ((uint32_t)(a_idMmio2) << 24) | (uint32_t)(a_iPage) )
1746/** Gets the MMIO2 range ID from an MMIO2 page ID. */
1747#define PGM_MMIO2_PAGEID_GET_MMIO2_ID(a_idPage) ( (uint8_t)((a_idPage) >> 24) )
1748/** Gets the MMIO2 page index from an MMIO2 page ID. */
1749#define PGM_MMIO2_PAGEID_GET_IDX(a_idPage) ( ((a_idPage) & UINT32_C(0x00ffffff)) )
1750/** @} */
1751
1752
1753
1754/**
1755 * PGMPhysRead/Write cache entry
1756 */
1757typedef struct PGMPHYSCACHEENTRY
1758{
1759 /** R3 pointer to physical page. */
1760 R3PTRTYPE(uint8_t *) pbR3;
1761 /** GC Physical address for cache entry */
1762 RTGCPHYS GCPhys;
1763#if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
1764 RTGCPHYS u32Padding0; /**< alignment padding. */
1765#endif
1766} PGMPHYSCACHEENTRY;
1767
1768/**
1769 * PGMPhysRead/Write cache to reduce REM memory access overhead
1770 */
1771typedef struct PGMPHYSCACHE
1772{
1773 /** Bitmap of valid cache entries */
1774 uint64_t aEntries;
1775 /** Cache entries */
1776 PGMPHYSCACHEENTRY Entry[PGM_MAX_PHYSCACHE_ENTRIES];
1777} PGMPHYSCACHE;
1778
1779
1780/** Pointer to an allocation chunk ring-3 mapping. */
1781typedef struct PGMCHUNKR3MAP *PPGMCHUNKR3MAP;
1782/** Pointer to an allocation chunk ring-3 mapping pointer. */
1783typedef PPGMCHUNKR3MAP *PPPGMCHUNKR3MAP;
1784
1785/**
1786 * Ring-3 tracking structure for an allocation chunk ring-3 mapping.
1787 *
1788 * The primary tree (Core) uses the chunk id as key.
1789 */
1790typedef struct PGMCHUNKR3MAP
1791{
1792 /** The key is the chunk id. */
1793 AVLU32NODECORE Core;
1794 /** The time (ChunkR3Map.iNow) this chunk was last used. Used for unmap
1795 * selection. */
1796 uint32_t iLastUsed;
1797 /** The current reference count. */
1798 uint32_t volatile cRefs;
1799 /** The current permanent reference count. */
1800 uint32_t volatile cPermRefs;
1801 /** The mapping address. */
1802 void *pv;
1803} PGMCHUNKR3MAP;
1804
1805/**
1806 * Allocation chunk ring-3 mapping TLB entry.
1807 */
1808typedef struct PGMCHUNKR3MAPTLBE
1809{
1810 /** The chunk id. */
1811 uint32_t volatile idChunk;
1812#if HC_ARCH_BITS == 64
1813 uint32_t u32Padding; /**< alignment padding. */
1814#endif
1815 /** The chunk map. */
1816#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1817 R3PTRTYPE(PPGMCHUNKR3MAP) volatile pChunk;
1818#else
1819 R3R0PTRTYPE(PPGMCHUNKR3MAP) volatile pChunk;
1820#endif
1821} PGMCHUNKR3MAPTLBE;
1822/** Pointer to the an allocation chunk ring-3 mapping TLB entry. */
1823typedef PGMCHUNKR3MAPTLBE *PPGMCHUNKR3MAPTLBE;
1824
1825/** The number of TLB entries in PGMCHUNKR3MAPTLB.
1826 * @remark Must be a power of two value. */
1827#define PGM_CHUNKR3MAPTLB_ENTRIES 64
1828
1829/**
1830 * Allocation chunk ring-3 mapping TLB.
1831 *
1832 * @remarks We use a TLB to speed up lookups by avoiding walking the AVL.
1833 * At first glance this might look kinda odd since AVL trees are
1834 * supposed to give the most optimal lookup times of all trees
1835 * due to their balancing. However, take a tree with 1023 nodes
1836 * in it, that's 10 levels, meaning that most searches has to go
1837 * down 9 levels before they find what they want. This isn't fast
1838 * compared to a TLB hit. There is the factor of cache misses,
1839 * and of course the problem with trees and branch prediction.
1840 * This is why we use TLBs in front of most of the trees.
1841 *
1842 * @todo Generalize this TLB + AVL stuff, shouldn't be all that
1843 * difficult when we switch to the new inlined AVL trees (from kStuff).
1844 */
1845typedef struct PGMCHUNKR3MAPTLB
1846{
1847 /** The TLB entries. */
1848 PGMCHUNKR3MAPTLBE aEntries[PGM_CHUNKR3MAPTLB_ENTRIES];
1849} PGMCHUNKR3MAPTLB;
1850
1851/**
1852 * Calculates the index of a guest page in the Ring-3 Chunk TLB.
1853 * @returns Chunk TLB index.
1854 * @param idChunk The Chunk ID.
1855 */
1856#define PGM_CHUNKR3MAPTLB_IDX(idChunk) ( (idChunk) & (PGM_CHUNKR3MAPTLB_ENTRIES - 1) )
1857
1858
1859/**
1860 * Ring-3 guest page mapping TLB entry.
1861 * @remarks used in ring-0 as well at the moment.
1862 */
1863typedef struct PGMPAGER3MAPTLBE
1864{
1865 /** Address of the page. */
1866 RTGCPHYS volatile GCPhys;
1867 /** The guest page. */
1868#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1869 R3PTRTYPE(PPGMPAGE) volatile pPage;
1870#else
1871 R3R0PTRTYPE(PPGMPAGE) volatile pPage;
1872#endif
1873 /** Pointer to the page mapping tracking structure, PGMCHUNKR3MAP. */
1874#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1875 R3PTRTYPE(PPGMCHUNKR3MAP) volatile pMap;
1876#else
1877 R3R0PTRTYPE(PPGMCHUNKR3MAP) volatile pMap;
1878#endif
1879 /** The address */
1880#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
1881 R3PTRTYPE(void *) volatile pv;
1882#else
1883 R3R0PTRTYPE(void *) volatile pv;
1884#endif
1885#if HC_ARCH_BITS == 32
1886 uint32_t u32Padding; /**< alignment padding. */
1887#endif
1888} PGMPAGER3MAPTLBE;
1889/** Pointer to an entry in the HC physical TLB. */
1890typedef PGMPAGER3MAPTLBE *PPGMPAGER3MAPTLBE;
1891
1892
1893/** The number of entries in the ring-3 guest page mapping TLB.
1894 * @remarks The value must be a power of two. */
1895#define PGM_PAGER3MAPTLB_ENTRIES 256
1896
1897/**
1898 * Ring-3 guest page mapping TLB.
1899 * @remarks used in ring-0 as well at the moment.
1900 */
1901typedef struct PGMPAGER3MAPTLB
1902{
1903 /** The TLB entries. */
1904 PGMPAGER3MAPTLBE aEntries[PGM_PAGER3MAPTLB_ENTRIES];
1905} PGMPAGER3MAPTLB;
1906/** Pointer to the ring-3 guest page mapping TLB. */
1907typedef PGMPAGER3MAPTLB *PPGMPAGER3MAPTLB;
1908
1909/**
1910 * Calculates the index of the TLB entry for the specified guest page.
1911 * @returns Physical TLB index.
1912 * @param GCPhys The guest physical address.
1913 */
1914#define PGM_PAGER3MAPTLB_IDX(GCPhys) ( ((GCPhys) >> PAGE_SHIFT) & (PGM_PAGER3MAPTLB_ENTRIES - 1) )
1915
1916
1917/**
1918 * Raw-mode context dynamic mapping cache entry.
1919 *
1920 * Because of raw-mode context being reloctable and all relocations are applied
1921 * in ring-3, this has to be defined here and be RC specific.
1922 *
1923 * @sa PGMRZDYNMAPENTRY, PGMR0DYNMAPENTRY.
1924 */
1925typedef struct PGMRCDYNMAPENTRY
1926{
1927 /** The physical address of the currently mapped page.
1928 * This is duplicate for three reasons: cache locality, cache policy of the PT
1929 * mappings and sanity checks. */
1930 RTHCPHYS HCPhys;
1931 /** Pointer to the page. */
1932 RTRCPTR pvPage;
1933 /** The number of references. */
1934 int32_t volatile cRefs;
1935 /** PTE pointer union. */
1936 struct PGMRCDYNMAPENTRY_PPTE
1937 {
1938 /** PTE pointer, 32-bit legacy version. */
1939 RCPTRTYPE(PX86PTE) pLegacy;
1940 /** PTE pointer, PAE version. */
1941 RCPTRTYPE(PX86PTEPAE) pPae;
1942 } uPte;
1943} PGMRCDYNMAPENTRY;
1944/** Pointer to a dynamic mapping cache entry for the raw-mode context. */
1945typedef PGMRCDYNMAPENTRY *PPGMRCDYNMAPENTRY;
1946
1947
1948/**
1949 * Dynamic mapping cache for the raw-mode context.
1950 *
1951 * This is initialized during VMMRC init based upon the pbDynPageMapBaseGC and
1952 * paDynPageMap* PGM members. However, it has to be defined in PGMInternal.h
1953 * so that we can perform relocations from PGMR3Relocate. This has the
1954 * consequence that we must have separate ring-0 and raw-mode context versions
1955 * of this struct even if they share the basic elements.
1956 *
1957 * @sa PPGMRZDYNMAP, PGMR0DYNMAP.
1958 */
1959typedef struct PGMRCDYNMAP
1960{
1961 /** The usual magic number / eye catcher (PGMRZDYNMAP_MAGIC). */
1962 uint32_t u32Magic;
1963 /** Array for tracking and managing the pages. */
1964 RCPTRTYPE(PPGMRCDYNMAPENTRY) paPages;
1965 /** The cache size given as a number of pages. */
1966 uint32_t cPages;
1967 /** The current load.
1968 * This does not include guard pages. */
1969 uint32_t cLoad;
1970 /** The max load ever.
1971 * This is maintained to get trigger adding of more mapping space. */
1972 uint32_t cMaxLoad;
1973 /** The number of guard pages. */
1974 uint32_t cGuardPages;
1975 /** The number of users (protected by hInitLock). */
1976 uint32_t cUsers;
1977} PGMRCDYNMAP;
1978/** Pointer to the dynamic cache for the raw-mode context. */
1979typedef PGMRCDYNMAP *PPGMRCDYNMAP;
1980
1981
1982/**
1983 * Mapping cache usage set entry.
1984 *
1985 * @remarks 16-bit ints was chosen as the set is not expected to be used beyond
1986 * the dynamic ring-0 and (to some extent) raw-mode context mapping
1987 * cache. If it's extended to include ring-3, well, then something
1988 * will have be changed here...
1989 */
1990typedef struct PGMMAPSETENTRY
1991{
1992 /** Pointer to the page. */
1993#ifndef IN_RC
1994 RTR0PTR pvPage;
1995#else
1996 RTRCPTR pvPage;
1997# if HC_ARCH_BITS == 64
1998 uint32_t u32Alignment2;
1999# endif
2000#endif
2001 /** The mapping cache index. */
2002 uint16_t iPage;
2003 /** The number of references.
2004 * The max is UINT16_MAX - 1. */
2005 uint16_t cRefs;
2006 /** The number inlined references.
2007 * The max is UINT16_MAX - 1. */
2008 uint16_t cInlinedRefs;
2009 /** Unreferences. */
2010 uint16_t cUnrefs;
2011
2012#if HC_ARCH_BITS == 32
2013 uint32_t u32Alignment1;
2014#endif
2015 /** The physical address for this entry. */
2016 RTHCPHYS HCPhys;
2017} PGMMAPSETENTRY;
2018AssertCompileMemberOffset(PGMMAPSETENTRY, iPage, RT_MAX(sizeof(RTR0PTR), sizeof(RTRCPTR)));
2019AssertCompileMemberAlignment(PGMMAPSETENTRY, HCPhys, sizeof(RTHCPHYS));
2020/** Pointer to a mapping cache usage set entry. */
2021typedef PGMMAPSETENTRY *PPGMMAPSETENTRY;
2022
2023/**
2024 * Mapping cache usage set.
2025 *
2026 * This is used in ring-0 and the raw-mode context to track dynamic mappings
2027 * done during exits / traps. The set is
2028 */
2029typedef struct PGMMAPSET
2030{
2031 /** The number of occupied entries.
2032 * This is PGMMAPSET_CLOSED if the set is closed and we're not supposed to do
2033 * dynamic mappings. */
2034 uint32_t cEntries;
2035 /** The start of the current subset.
2036 * This is UINT32_MAX if no subset is currently open. */
2037 uint32_t iSubset;
2038 /** The index of the current CPU, only valid if the set is open. */
2039 int32_t iCpu;
2040 uint32_t alignment;
2041 /** The entries. */
2042 PGMMAPSETENTRY aEntries[64];
2043 /** HCPhys -> iEntry fast lookup table.
2044 * Use PGMMAPSET_HASH for hashing.
2045 * The entries may or may not be valid, check against cEntries. */
2046 uint8_t aiHashTable[128];
2047} PGMMAPSET;
2048AssertCompileSizeAlignment(PGMMAPSET, 8);
2049/** Pointer to the mapping cache set. */
2050typedef PGMMAPSET *PPGMMAPSET;
2051
2052/** PGMMAPSET::cEntries value for a closed set. */
2053#define PGMMAPSET_CLOSED UINT32_C(0xdeadc0fe)
2054
2055/** Hash function for aiHashTable. */
2056#define PGMMAPSET_HASH(HCPhys) (((HCPhys) >> PAGE_SHIFT) & 127)
2057
2058
2059/** @name Context neutral page mapper TLB.
2060 *
2061 * Hoping to avoid some code and bug duplication parts of the GCxxx->CCPtr
2062 * code is writting in a kind of context neutral way. Time will show whether
2063 * this actually makes sense or not...
2064 *
2065 * @todo this needs to be reconsidered and dropped/redone since the ring-0
2066 * context ends up using a global mapping cache on some platforms
2067 * (darwin).
2068 *
2069 * @{ */
2070/** @typedef PPGMPAGEMAPTLB
2071 * The page mapper TLB pointer type for the current context. */
2072/** @typedef PPGMPAGEMAPTLB
2073 * The page mapper TLB entry pointer type for the current context. */
2074/** @typedef PPGMPAGEMAPTLB
2075 * The page mapper TLB entry pointer pointer type for the current context. */
2076/** @def PGM_PAGEMAPTLB_ENTRIES
2077 * The number of TLB entries in the page mapper TLB for the current context. */
2078/** @def PGM_PAGEMAPTLB_IDX
2079 * Calculate the TLB index for a guest physical address.
2080 * @returns The TLB index.
2081 * @param GCPhys The guest physical address. */
2082/** @typedef PPGMPAGEMAP
2083 * Pointer to a page mapper unit for current context. */
2084/** @typedef PPPGMPAGEMAP
2085 * Pointer to a page mapper unit pointer for current context. */
2086#if defined(IN_RC) && !defined(DOXYGEN_RUNNING)
2087// typedef PPGMPAGEGCMAPTLB PPGMPAGEMAPTLB;
2088// typedef PPGMPAGEGCMAPTLBE PPGMPAGEMAPTLBE;
2089// typedef PPGMPAGEGCMAPTLBE *PPPGMPAGEMAPTLBE;
2090# define PGM_PAGEMAPTLB_ENTRIES PGM_PAGEGCMAPTLB_ENTRIES
2091# define PGM_PAGEMAPTLB_IDX(GCPhys) PGM_PAGEGCMAPTLB_IDX(GCPhys)
2092 typedef void * PPGMPAGEMAP;
2093 typedef void ** PPPGMPAGEMAP;
2094//#elif IN_RING0
2095// typedef PPGMPAGER0MAPTLB PPGMPAGEMAPTLB;
2096// typedef PPGMPAGER0MAPTLBE PPGMPAGEMAPTLBE;
2097// typedef PPGMPAGER0MAPTLBE *PPPGMPAGEMAPTLBE;
2098//# define PGM_PAGEMAPTLB_ENTRIES PGM_PAGER0MAPTLB_ENTRIES
2099//# define PGM_PAGEMAPTLB_IDX(GCPhys) PGM_PAGER0MAPTLB_IDX(GCPhys)
2100// typedef PPGMCHUNKR0MAP PPGMPAGEMAP;
2101// typedef PPPGMCHUNKR0MAP PPPGMPAGEMAP;
2102#else
2103 typedef PPGMPAGER3MAPTLB PPGMPAGEMAPTLB;
2104 typedef PPGMPAGER3MAPTLBE PPGMPAGEMAPTLBE;
2105 typedef PPGMPAGER3MAPTLBE *PPPGMPAGEMAPTLBE;
2106# define PGM_PAGEMAPTLB_ENTRIES PGM_PAGER3MAPTLB_ENTRIES
2107# define PGM_PAGEMAPTLB_IDX(GCPhys) PGM_PAGER3MAPTLB_IDX(GCPhys)
2108 typedef PPGMCHUNKR3MAP PPGMPAGEMAP;
2109 typedef PPPGMCHUNKR3MAP PPPGMPAGEMAP;
2110#endif
2111/** @} */
2112
2113
2114/** @name PGM Pool Indexes.
2115 * Aka. the unique shadow page identifier.
2116 * @{ */
2117/** NIL page pool IDX. */
2118#define NIL_PGMPOOL_IDX 0
2119/** The first normal index. There used to be 5 fictive pages up front, now
2120 * there is only the NIL page. */
2121#define PGMPOOL_IDX_FIRST 1
2122/** The last valid index. (inclusive, 14 bits) */
2123#define PGMPOOL_IDX_LAST 0x3fff
2124/** @} */
2125
2126/** The NIL index for the parent chain. */
2127#define NIL_PGMPOOL_USER_INDEX ((uint16_t)0xffff)
2128#define NIL_PGMPOOL_PRESENT_INDEX ((uint16_t)0xffff)
2129
2130/**
2131 * Node in the chain linking a shadowed page to it's parent (user).
2132 */
2133#pragma pack(1)
2134typedef struct PGMPOOLUSER
2135{
2136 /** The index to the next item in the chain. NIL_PGMPOOL_USER_INDEX is no next. */
2137 uint16_t iNext;
2138 /** The user page index. */
2139 uint16_t iUser;
2140 /** Index into the user table. */
2141 uint32_t iUserTable;
2142} PGMPOOLUSER, *PPGMPOOLUSER;
2143typedef const PGMPOOLUSER *PCPGMPOOLUSER;
2144#pragma pack()
2145
2146
2147/** The NIL index for the phys ext chain. */
2148#define NIL_PGMPOOL_PHYSEXT_INDEX ((uint16_t)0xffff)
2149/** The NIL pte index for a phys ext chain slot. */
2150#define NIL_PGMPOOL_PHYSEXT_IDX_PTE ((uint16_t)0xffff)
2151
2152/**
2153 * Node in the chain of physical cross reference extents.
2154 * @todo Calling this an 'extent' is not quite right, find a better name.
2155 * @todo find out the optimal size of the aidx array
2156 */
2157#pragma pack(1)
2158typedef struct PGMPOOLPHYSEXT
2159{
2160 /** The index to the next item in the chain. NIL_PGMPOOL_PHYSEXT_INDEX is no next. */
2161 uint16_t iNext;
2162 /** Alignment. */
2163 uint16_t u16Align;
2164 /** The user page index. */
2165 uint16_t aidx[3];
2166 /** The page table index or NIL_PGMPOOL_PHYSEXT_IDX_PTE if unknown. */
2167 uint16_t apte[3];
2168} PGMPOOLPHYSEXT, *PPGMPOOLPHYSEXT;
2169typedef const PGMPOOLPHYSEXT *PCPGMPOOLPHYSEXT;
2170#pragma pack()
2171
2172
2173/**
2174 * The kind of page that's being shadowed.
2175 */
2176typedef enum PGMPOOLKIND
2177{
2178 /** The virtual invalid 0 entry. */
2179 PGMPOOLKIND_INVALID = 0,
2180 /** The entry is free (=unused). */
2181 PGMPOOLKIND_FREE,
2182
2183 /** Shw: 32-bit page table; Gst: no paging. */
2184 PGMPOOLKIND_32BIT_PT_FOR_PHYS,
2185 /** Shw: 32-bit page table; Gst: 32-bit page table. */
2186 PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT,
2187 /** Shw: 32-bit page table; Gst: 4MB page. */
2188 PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB,
2189 /** Shw: PAE page table; Gst: no paging. */
2190 PGMPOOLKIND_PAE_PT_FOR_PHYS,
2191 /** Shw: PAE page table; Gst: 32-bit page table. */
2192 PGMPOOLKIND_PAE_PT_FOR_32BIT_PT,
2193 /** Shw: PAE page table; Gst: Half of a 4MB page. */
2194 PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB,
2195 /** Shw: PAE page table; Gst: PAE page table. */
2196 PGMPOOLKIND_PAE_PT_FOR_PAE_PT,
2197 /** Shw: PAE page table; Gst: 2MB page. */
2198 PGMPOOLKIND_PAE_PT_FOR_PAE_2MB,
2199
2200 /** Shw: 32-bit page directory. Gst: 32-bit page directory. */
2201 PGMPOOLKIND_32BIT_PD,
2202 /** Shw: 32-bit page directory. Gst: no paging. */
2203 PGMPOOLKIND_32BIT_PD_PHYS,
2204 /** Shw: PAE page directory 0; Gst: 32-bit page directory. */
2205 PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD,
2206 /** Shw: PAE page directory 1; Gst: 32-bit page directory. */
2207 PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD,
2208 /** Shw: PAE page directory 2; Gst: 32-bit page directory. */
2209 PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD,
2210 /** Shw: PAE page directory 3; Gst: 32-bit page directory. */
2211 PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD,
2212 /** Shw: PAE page directory; Gst: PAE page directory. */
2213 PGMPOOLKIND_PAE_PD_FOR_PAE_PD,
2214 /** Shw: PAE page directory; Gst: no paging. Note: +NP. */
2215 PGMPOOLKIND_PAE_PD_PHYS,
2216
2217 /** Shw: PAE page directory pointer table (legacy, 4 entries); Gst 32 bits paging. */
2218 PGMPOOLKIND_PAE_PDPT_FOR_32BIT,
2219 /** Shw: PAE page directory pointer table (legacy, 4 entries); Gst PAE PDPT. */
2220 PGMPOOLKIND_PAE_PDPT,
2221 /** Shw: PAE page directory pointer table (legacy, 4 entries); Gst: no paging. */
2222 PGMPOOLKIND_PAE_PDPT_PHYS,
2223
2224 /** Shw: 64-bit page directory pointer table; Gst: 64-bit page directory pointer table. */
2225 PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT,
2226 /** Shw: 64-bit page directory pointer table; Gst: no paging. */
2227 PGMPOOLKIND_64BIT_PDPT_FOR_PHYS,
2228 /** Shw: 64-bit page directory table; Gst: 64-bit page directory table. */
2229 PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD,
2230 /** Shw: 64-bit page directory table; Gst: no paging. */
2231 PGMPOOLKIND_64BIT_PD_FOR_PHYS, /* 24 */
2232
2233 /** Shw: 64-bit PML4; Gst: 64-bit PML4. */
2234 PGMPOOLKIND_64BIT_PML4,
2235
2236 /** Shw: EPT page directory pointer table; Gst: no paging. */
2237 PGMPOOLKIND_EPT_PDPT_FOR_PHYS,
2238 /** Shw: EPT page directory table; Gst: no paging. */
2239 PGMPOOLKIND_EPT_PD_FOR_PHYS,
2240 /** Shw: EPT page table; Gst: no paging. */
2241 PGMPOOLKIND_EPT_PT_FOR_PHYS,
2242
2243 /** Shw: Root Nested paging table. */
2244 PGMPOOLKIND_ROOT_NESTED,
2245
2246 /** The last valid entry. */
2247 PGMPOOLKIND_LAST = PGMPOOLKIND_ROOT_NESTED
2248} PGMPOOLKIND;
2249
2250/**
2251 * The access attributes of the page; only applies to big pages.
2252 */
2253typedef enum
2254{
2255 PGMPOOLACCESS_DONTCARE = 0,
2256 PGMPOOLACCESS_USER_RW,
2257 PGMPOOLACCESS_USER_R,
2258 PGMPOOLACCESS_USER_RW_NX,
2259 PGMPOOLACCESS_USER_R_NX,
2260 PGMPOOLACCESS_SUPERVISOR_RW,
2261 PGMPOOLACCESS_SUPERVISOR_R,
2262 PGMPOOLACCESS_SUPERVISOR_RW_NX,
2263 PGMPOOLACCESS_SUPERVISOR_R_NX
2264} PGMPOOLACCESS;
2265
2266/**
2267 * The tracking data for a page in the pool.
2268 */
2269typedef struct PGMPOOLPAGE
2270{
2271 /** AVL node code with the (HC) physical address of this page. */
2272 AVLOHCPHYSNODECORE Core;
2273 /** Pointer to the R3 mapping of the page. */
2274#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
2275 R3PTRTYPE(void *) pvPageR3;
2276#else
2277 R3R0PTRTYPE(void *) pvPageR3;
2278#endif
2279#if HC_ARCH_BITS == 32 && GC_ARCH_BITS == 64
2280 uint32_t Alignment0;
2281#endif
2282 /** The guest physical address. */
2283 RTGCPHYS GCPhys;
2284 /** The kind of page we're shadowing. (This is really a PGMPOOLKIND enum.) */
2285 uint8_t enmKind;
2286 /** The subkind of page we're shadowing. (This is really a PGMPOOLACCESS enum.) */
2287 uint8_t enmAccess;
2288 /** This supplements enmKind and enmAccess */
2289 bool fA20Enabled : 1;
2290
2291 /** Used to indicate that the page is zeroed. */
2292 bool fZeroed : 1;
2293 /** Used to indicate that a PT has non-global entries. */
2294 bool fSeenNonGlobal : 1;
2295 /** Used to indicate that we're monitoring writes to the guest page. */
2296 bool fMonitored : 1;
2297 /** Used to indicate that the page is in the cache (e.g. in the GCPhys hash).
2298 * (All pages are in the age list.) */
2299 bool fCached : 1;
2300 /** This is used by the R3 access handlers when invoked by an async thread.
2301 * It's a hack required because of REMR3NotifyHandlerPhysicalDeregister. */
2302 bool volatile fReusedFlushPending : 1;
2303 /** Used to mark the page as dirty (write monitoring is temporarily
2304 * off). */
2305 bool fDirty : 1;
2306 bool fPadding1 : 1;
2307 bool fPadding2;
2308
2309 /** The index of this page. */
2310 uint16_t idx;
2311 /** The next entry in the list this page currently resides in.
2312 * It's either in the free list or in the GCPhys hash. */
2313 uint16_t iNext;
2314 /** Head of the user chain. NIL_PGMPOOL_USER_INDEX if not currently in use. */
2315 uint16_t iUserHead;
2316 /** The number of present entries. */
2317 uint16_t cPresent;
2318 /** The first entry in the table which is present. */
2319 uint16_t iFirstPresent;
2320 /** The number of modifications to the monitored page. */
2321 uint16_t cModifications;
2322 /** The next modified page. NIL_PGMPOOL_IDX if tail. */
2323 uint16_t iModifiedNext;
2324 /** The previous modified page. NIL_PGMPOOL_IDX if head. */
2325 uint16_t iModifiedPrev;
2326 /** The next page sharing access handler. NIL_PGMPOOL_IDX if tail. */
2327 uint16_t iMonitoredNext;
2328 /** The previous page sharing access handler. NIL_PGMPOOL_IDX if head. */
2329 uint16_t iMonitoredPrev;
2330 /** The next page in the age list. */
2331 uint16_t iAgeNext;
2332 /** The previous page in the age list. */
2333 uint16_t iAgePrev;
2334 /** Index into PGMPOOL::aDirtyPages if fDirty is set. */
2335 uint8_t idxDirtyEntry;
2336
2337 /** @name Access handler statistics to determine whether the guest is
2338 * (re)initializing a page table.
2339 * @{ */
2340 RTGCPTR GCPtrLastAccessHandlerRip;
2341 RTGCPTR GCPtrLastAccessHandlerFault;
2342 uint64_t cLastAccessHandler;
2343 /** @} */
2344 /** Used to indicate that this page can't be flushed. Important for cr3 root pages or shadow pae pd pages. */
2345 uint32_t volatile cLocked;
2346#if GC_ARCH_BITS == 64
2347 uint32_t u32Alignment3;
2348#endif
2349# ifdef VBOX_STRICT
2350 RTGCPTR GCPtrDirtyFault;
2351# endif
2352} PGMPOOLPAGE;
2353/** Pointer to a pool page. */
2354typedef PGMPOOLPAGE *PPGMPOOLPAGE;
2355/** Pointer to a const pool page. */
2356typedef PGMPOOLPAGE const *PCPGMPOOLPAGE;
2357/** Pointer to a pool page pointer. */
2358typedef PGMPOOLPAGE **PPPGMPOOLPAGE;
2359
2360
2361/** The hash table size. */
2362# define PGMPOOL_HASH_SIZE 0x40
2363/** The hash function. */
2364# define PGMPOOL_HASH(GCPhys) ( ((GCPhys) >> PAGE_SHIFT) & (PGMPOOL_HASH_SIZE - 1) )
2365
2366
2367/**
2368 * The shadow page pool instance data.
2369 *
2370 * It's all one big allocation made at init time, except for the
2371 * pages that is. The user nodes follows immediately after the
2372 * page structures.
2373 */
2374typedef struct PGMPOOL
2375{
2376 /** The VM handle - R3 Ptr. */
2377 PVMR3 pVMR3;
2378 /** The VM handle - R0 Ptr. */
2379 PVMR0 pVMR0;
2380 /** The VM handle - RC Ptr. */
2381 PVMRC pVMRC;
2382 /** The max pool size. This includes the special IDs. */
2383 uint16_t cMaxPages;
2384 /** The current pool size. */
2385 uint16_t cCurPages;
2386 /** The head of the free page list. */
2387 uint16_t iFreeHead;
2388 /* Padding. */
2389 uint16_t u16Padding;
2390 /** Head of the chain of free user nodes. */
2391 uint16_t iUserFreeHead;
2392 /** The number of user nodes we've allocated. */
2393 uint16_t cMaxUsers;
2394 /** The number of present page table entries in the entire pool. */
2395 uint32_t cPresent;
2396 /** Pointer to the array of user nodes - RC pointer. */
2397 RCPTRTYPE(PPGMPOOLUSER) paUsersRC;
2398 /** Pointer to the array of user nodes - R3 pointer. */
2399 R3PTRTYPE(PPGMPOOLUSER) paUsersR3;
2400 /** Pointer to the array of user nodes - R0 pointer. */
2401 R0PTRTYPE(PPGMPOOLUSER) paUsersR0;
2402 /** Head of the chain of free phys ext nodes. */
2403 uint16_t iPhysExtFreeHead;
2404 /** The number of user nodes we've allocated. */
2405 uint16_t cMaxPhysExts;
2406 /** Pointer to the array of physical xref extent - RC pointer. */
2407 RCPTRTYPE(PPGMPOOLPHYSEXT) paPhysExtsRC;
2408 /** Pointer to the array of physical xref extent nodes - R3 pointer. */
2409 R3PTRTYPE(PPGMPOOLPHYSEXT) paPhysExtsR3;
2410 /** Pointer to the array of physical xref extent nodes - R0 pointer. */
2411 R0PTRTYPE(PPGMPOOLPHYSEXT) paPhysExtsR0;
2412 /** Hash table for GCPhys addresses. */
2413 uint16_t aiHash[PGMPOOL_HASH_SIZE];
2414 /** The head of the age list. */
2415 uint16_t iAgeHead;
2416 /** The tail of the age list. */
2417 uint16_t iAgeTail;
2418 /** Set if the cache is enabled. */
2419 bool fCacheEnabled;
2420 /** Alignment padding. */
2421 bool afPadding1[3];
2422 /** Head of the list of modified pages. */
2423 uint16_t iModifiedHead;
2424 /** The current number of modified pages. */
2425 uint16_t cModifiedPages;
2426 /** Physical access handler type registration handle. */
2427 PGMPHYSHANDLERTYPE hAccessHandlerType;
2428 /** Next available slot (in aDirtyPages). */
2429 uint32_t idxFreeDirtyPage;
2430 /** Number of active dirty pages. */
2431 uint32_t cDirtyPages;
2432 /** Array of current dirty pgm pool page indices. */
2433 struct
2434 {
2435 uint16_t uIdx;
2436 uint16_t Alignment[3];
2437 uint64_t aPage[512];
2438 } aDirtyPages[16];
2439 /** The number of pages currently in use. */
2440 uint16_t cUsedPages;
2441#ifdef VBOX_WITH_STATISTICS
2442 /** The high water mark for cUsedPages. */
2443 uint16_t cUsedPagesHigh;
2444 uint32_t Alignment1; /**< Align the next member on a 64-bit boundary. */
2445 /** Profiling pgmPoolAlloc(). */
2446 STAMPROFILEADV StatAlloc;
2447 /** Profiling pgmR3PoolClearDoIt(). */
2448 STAMPROFILE StatClearAll;
2449 /** Profiling pgmR3PoolReset(). */
2450 STAMPROFILE StatR3Reset;
2451 /** Profiling pgmPoolFlushPage(). */
2452 STAMPROFILE StatFlushPage;
2453 /** Profiling pgmPoolFree(). */
2454 STAMPROFILE StatFree;
2455 /** Counting explicit flushes by PGMPoolFlushPage(). */
2456 STAMCOUNTER StatForceFlushPage;
2457 /** Counting explicit flushes of dirty pages by PGMPoolFlushPage(). */
2458 STAMCOUNTER StatForceFlushDirtyPage;
2459 /** Counting flushes for reused pages. */
2460 STAMCOUNTER StatForceFlushReused;
2461 /** Profiling time spent zeroing pages. */
2462 STAMPROFILE StatZeroPage;
2463 /** Profiling of pgmPoolTrackDeref. */
2464 STAMPROFILE StatTrackDeref;
2465 /** Profiling pgmTrackFlushGCPhysPT. */
2466 STAMPROFILE StatTrackFlushGCPhysPT;
2467 /** Profiling pgmTrackFlushGCPhysPTs. */
2468 STAMPROFILE StatTrackFlushGCPhysPTs;
2469 /** Profiling pgmTrackFlushGCPhysPTsSlow. */
2470 STAMPROFILE StatTrackFlushGCPhysPTsSlow;
2471 /** Number of times we've been out of user records. */
2472 STAMCOUNTER StatTrackFreeUpOneUser;
2473 /** Nr of flushed entries. */
2474 STAMCOUNTER StatTrackFlushEntry;
2475 /** Nr of updated entries. */
2476 STAMCOUNTER StatTrackFlushEntryKeep;
2477 /** Profiling deref activity related tracking GC physical pages. */
2478 STAMPROFILE StatTrackDerefGCPhys;
2479 /** Number of linear searches for a HCPhys in the ram ranges. */
2480 STAMCOUNTER StatTrackLinearRamSearches;
2481 /** The number of failing pgmPoolTrackPhysExtAlloc calls. */
2482 STAMCOUNTER StamTrackPhysExtAllocFailures;
2483 /** Profiling the RC/R0 access handler. */
2484 STAMPROFILE StatMonitorRZ;
2485 /** Times we've failed interpreting the instruction. */
2486 STAMCOUNTER StatMonitorRZEmulateInstr;
2487 /** Profiling the pgmPoolFlushPage calls made from the RC/R0 access handler. */
2488 STAMPROFILE StatMonitorRZFlushPage;
2489 /* Times we've detected a page table reinit. */
2490 STAMCOUNTER StatMonitorRZFlushReinit;
2491 /** Counting flushes for pages that are modified too often. */
2492 STAMCOUNTER StatMonitorRZFlushModOverflow;
2493 /** Times we've detected fork(). */
2494 STAMCOUNTER StatMonitorRZFork;
2495 /** Profiling the RC/R0 access we've handled (except REP STOSD). */
2496 STAMPROFILE StatMonitorRZHandled;
2497 /** Times we've failed interpreting a patch code instruction. */
2498 STAMCOUNTER StatMonitorRZIntrFailPatch1;
2499 /** Times we've failed interpreting a patch code instruction during flushing. */
2500 STAMCOUNTER StatMonitorRZIntrFailPatch2;
2501 /** The number of times we've seen rep prefixes we can't handle. */
2502 STAMCOUNTER StatMonitorRZRepPrefix;
2503 /** Profiling the REP STOSD cases we've handled. */
2504 STAMPROFILE StatMonitorRZRepStosd;
2505 /** Nr of handled PT faults. */
2506 STAMCOUNTER StatMonitorRZFaultPT;
2507 /** Nr of handled PD faults. */
2508 STAMCOUNTER StatMonitorRZFaultPD;
2509 /** Nr of handled PDPT faults. */
2510 STAMCOUNTER StatMonitorRZFaultPDPT;
2511 /** Nr of handled PML4 faults. */
2512 STAMCOUNTER StatMonitorRZFaultPML4;
2513
2514 /** Profiling the R3 access handler. */
2515 STAMPROFILE StatMonitorR3;
2516 /** Times we've failed interpreting the instruction. */
2517 STAMCOUNTER StatMonitorR3EmulateInstr;
2518 /** Profiling the pgmPoolFlushPage calls made from the R3 access handler. */
2519 STAMPROFILE StatMonitorR3FlushPage;
2520 /* Times we've detected a page table reinit. */
2521 STAMCOUNTER StatMonitorR3FlushReinit;
2522 /** Counting flushes for pages that are modified too often. */
2523 STAMCOUNTER StatMonitorR3FlushModOverflow;
2524 /** Times we've detected fork(). */
2525 STAMCOUNTER StatMonitorR3Fork;
2526 /** Profiling the R3 access we've handled (except REP STOSD). */
2527 STAMPROFILE StatMonitorR3Handled;
2528 /** The number of times we've seen rep prefixes we can't handle. */
2529 STAMCOUNTER StatMonitorR3RepPrefix;
2530 /** Profiling the REP STOSD cases we've handled. */
2531 STAMPROFILE StatMonitorR3RepStosd;
2532 /** Nr of handled PT faults. */
2533 STAMCOUNTER StatMonitorR3FaultPT;
2534 /** Nr of handled PD faults. */
2535 STAMCOUNTER StatMonitorR3FaultPD;
2536 /** Nr of handled PDPT faults. */
2537 STAMCOUNTER StatMonitorR3FaultPDPT;
2538 /** Nr of handled PML4 faults. */
2539 STAMCOUNTER StatMonitorR3FaultPML4;
2540 /** The number of times we're called in an async thread an need to flush. */
2541 STAMCOUNTER StatMonitorR3Async;
2542 /** Times we've called pgmPoolResetDirtyPages (and there were dirty page). */
2543 STAMCOUNTER StatResetDirtyPages;
2544 /** Times we've called pgmPoolAddDirtyPage. */
2545 STAMCOUNTER StatDirtyPage;
2546 /** Times we've had to flush duplicates for dirty page management. */
2547 STAMCOUNTER StatDirtyPageDupFlush;
2548 /** Times we've had to flush because of overflow. */
2549 STAMCOUNTER StatDirtyPageOverFlowFlush;
2550
2551 /** The high water mark for cModifiedPages. */
2552 uint16_t cModifiedPagesHigh;
2553 uint16_t Alignment2[3]; /**< Align the next member on a 64-bit boundary. */
2554
2555 /** The number of cache hits. */
2556 STAMCOUNTER StatCacheHits;
2557 /** The number of cache misses. */
2558 STAMCOUNTER StatCacheMisses;
2559 /** The number of times we've got a conflict of 'kind' in the cache. */
2560 STAMCOUNTER StatCacheKindMismatches;
2561 /** Number of times we've been out of pages. */
2562 STAMCOUNTER StatCacheFreeUpOne;
2563 /** The number of cacheable allocations. */
2564 STAMCOUNTER StatCacheCacheable;
2565 /** The number of uncacheable allocations. */
2566 STAMCOUNTER StatCacheUncacheable;
2567#else
2568 uint32_t Alignment3; /**< Align the next member on a 64-bit boundary. */
2569#endif
2570 /** The AVL tree for looking up a page by its HC physical address. */
2571 AVLOHCPHYSTREE HCPhysTree;
2572 uint32_t Alignment4; /**< Align the next member on a 64-bit boundary. */
2573 /** Array of pages. (cMaxPages in length)
2574 * The Id is the index into thist array.
2575 */
2576 PGMPOOLPAGE aPages[PGMPOOL_IDX_FIRST];
2577} PGMPOOL, *PPGMPOOL, **PPPGMPOOL;
2578AssertCompileMemberAlignment(PGMPOOL, iModifiedHead, 8);
2579AssertCompileMemberAlignment(PGMPOOL, aDirtyPages, 8);
2580AssertCompileMemberAlignment(PGMPOOL, cUsedPages, 8);
2581#ifdef VBOX_WITH_STATISTICS
2582AssertCompileMemberAlignment(PGMPOOL, StatAlloc, 8);
2583#endif
2584AssertCompileMemberAlignment(PGMPOOL, aPages, 8);
2585
2586
2587/** @def PGMPOOL_PAGE_2_PTR
2588 * Maps a pool page pool into the current context.
2589 *
2590 * @returns VBox status code.
2591 * @param a_pVM Pointer to the VM.
2592 * @param a_pPage The pool page.
2593 *
2594 * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
2595 * small page window employeed by that function. Be careful.
2596 * @remark There is no need to assert on the result.
2597 */
2598#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
2599# define PGMPOOL_PAGE_2_PTR(a_pVM, a_pPage) pgmPoolMapPageInlined((a_pVM), (a_pPage) RTLOG_COMMA_SRC_POS)
2600#elif defined(VBOX_STRICT) || 1 /* temporarily going strict here */
2601# define PGMPOOL_PAGE_2_PTR(a_pVM, a_pPage) pgmPoolMapPageStrict(a_pPage, __FUNCTION__)
2602DECLINLINE(void *) pgmPoolMapPageStrict(PPGMPOOLPAGE a_pPage, const char *pszCaller)
2603{
2604 AssertPtr(a_pPage);
2605 AssertReleaseMsg(RT_VALID_PTR(a_pPage->pvPageR3), ("enmKind=%d idx=%#x HCPhys=%RHp GCPhys=%RGp caller=%s\n", a_pPage->enmKind, a_pPage->idx, a_pPage->Core.Key, a_pPage->GCPhys, pszCaller));
2606 return a_pPage->pvPageR3;
2607}
2608#else
2609# define PGMPOOL_PAGE_2_PTR(pVM, a_pPage) ((a_pPage)->pvPageR3)
2610#endif
2611
2612
2613/** @def PGMPOOL_PAGE_2_PTR_V2
2614 * Maps a pool page pool into the current context, taking both VM and VMCPU.
2615 *
2616 * @returns VBox status code.
2617 * @param a_pVM Pointer to the VM.
2618 * @param a_pVCpu The current CPU.
2619 * @param a_pPage The pool page.
2620 *
2621 * @remark In RC this uses PGMGCDynMapHCPage(), so it will consume of the
2622 * small page window employeed by that function. Be careful.
2623 * @remark There is no need to assert on the result.
2624 */
2625#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
2626# define PGMPOOL_PAGE_2_PTR_V2(a_pVM, a_pVCpu, a_pPage) pgmPoolMapPageV2Inlined((a_pVM), (a_pVCpu), (a_pPage) RTLOG_COMMA_SRC_POS)
2627#else
2628# define PGMPOOL_PAGE_2_PTR_V2(a_pVM, a_pVCpu, a_pPage) PGMPOOL_PAGE_2_PTR((a_pVM), (a_pPage))
2629#endif
2630
2631
2632/** @name Per guest page tracking data.
2633 * This is currently as a 16-bit word in the PGMPAGE structure, the idea though
2634 * is to use more bits for it and split it up later on. But for now we'll play
2635 * safe and change as little as possible.
2636 *
2637 * The 16-bit word has two parts:
2638 *
2639 * The first 14-bit forms the @a idx field. It is either the index of a page in
2640 * the shadow page pool, or and index into the extent list.
2641 *
2642 * The 2 topmost bits makes up the @a cRefs field, which counts the number of
2643 * shadow page pool references to the page. If cRefs equals
2644 * PGMPOOL_CREFS_PHYSEXT, then the @a idx field is an indext into the extent
2645 * (misnomer) table and not the shadow page pool.
2646 *
2647 * See PGM_PAGE_GET_TRACKING and PGM_PAGE_SET_TRACKING for how to get and set
2648 * the 16-bit word.
2649 *
2650 * @{ */
2651/** The shift count for getting to the cRefs part. */
2652#define PGMPOOL_TD_CREFS_SHIFT 14
2653/** The mask applied after shifting the tracking data down by
2654 * PGMPOOL_TD_CREFS_SHIFT. */
2655#define PGMPOOL_TD_CREFS_MASK 0x3
2656/** The cRefs value used to indicate that the idx is the head of a
2657 * physical cross reference list. */
2658#define PGMPOOL_TD_CREFS_PHYSEXT PGMPOOL_TD_CREFS_MASK
2659/** The shift used to get idx. */
2660#define PGMPOOL_TD_IDX_SHIFT 0
2661/** The mask applied to the idx after shifting down by PGMPOOL_TD_IDX_SHIFT. */
2662#define PGMPOOL_TD_IDX_MASK 0x3fff
2663/** The idx value when we're out of of PGMPOOLPHYSEXT entries or/and there are
2664 * simply too many mappings of this page. */
2665#define PGMPOOL_TD_IDX_OVERFLOWED PGMPOOL_TD_IDX_MASK
2666
2667/** @def PGMPOOL_TD_MAKE
2668 * Makes a 16-bit tracking data word.
2669 *
2670 * @returns tracking data.
2671 * @param cRefs The @a cRefs field. Must be within bounds!
2672 * @param idx The @a idx field. Must also be within bounds! */
2673#define PGMPOOL_TD_MAKE(cRefs, idx) ( ((cRefs) << PGMPOOL_TD_CREFS_SHIFT) | (idx) )
2674
2675/** @def PGMPOOL_TD_GET_CREFS
2676 * Get the @a cRefs field from a tracking data word.
2677 *
2678 * @returns The @a cRefs field
2679 * @param u16 The tracking data word.
2680 * @remarks This will only return 1 or PGMPOOL_TD_CREFS_PHYSEXT for a
2681 * non-zero @a u16. */
2682#define PGMPOOL_TD_GET_CREFS(u16) ( ((u16) >> PGMPOOL_TD_CREFS_SHIFT) & PGMPOOL_TD_CREFS_MASK )
2683
2684/** @def PGMPOOL_TD_GET_IDX
2685 * Get the @a idx field from a tracking data word.
2686 *
2687 * @returns The @a idx field
2688 * @param u16 The tracking data word. */
2689#define PGMPOOL_TD_GET_IDX(u16) ( ((u16) >> PGMPOOL_TD_IDX_SHIFT) & PGMPOOL_TD_IDX_MASK )
2690/** @} */
2691
2692
2693
2694/** @name A20 gate macros
2695 * @{ */
2696#define PGM_WITH_A20
2697#ifdef PGM_WITH_A20
2698# define PGM_A20_IS_ENABLED(a_pVCpu) ((a_pVCpu)->pgm.s.fA20Enabled)
2699# define PGM_A20_APPLY(a_pVCpu, a_GCPhys) ((a_GCPhys) & (a_pVCpu)->pgm.s.GCPhysA20Mask)
2700# define PGM_A20_APPLY_TO_VAR(a_pVCpu, a_GCPhysVar) \
2701 do { a_GCPhysVar &= (a_pVCpu)->pgm.s.GCPhysA20Mask; } while (0)
2702# define PGM_A20_ASSERT_MASKED(pVCpu, a_GCPhys) Assert(PGM_A20_APPLY(pVCpu, a_GCPhys) == (a_GCPhys))
2703#else
2704# define PGM_A20_IS_ENABLED(a_pVCpu) (true)
2705# define PGM_A20_APPLY(a_pVCpu, a_GCPhys) (a_GCPhys)
2706# define PGM_A20_APPLY_TO_VAR(a_pVCpu, a_GCPhysVar) do { } while (0)
2707# define PGM_A20_ASSERT_MASKED(pVCpu, a_GCPhys) do { } while (0)
2708#endif
2709/** @} */
2710
2711
2712/**
2713 * Roots and anchors for trees and list employing self relative offsets as
2714 * pointers.
2715 *
2716 * When using self-relative offsets instead of pointers, the offsets needs to be
2717 * the same in all offsets. Thus the roots and anchors needs to live on the
2718 * hyper heap just like the nodes.
2719 */
2720typedef struct PGMTREES
2721{
2722 /** List of physical access handler types (offset pointers) of type
2723 * PGMPHYSHANDLERTYPEINT. This is needed for relocations. */
2724 RTLISTOFF32ANCHOR HeadPhysHandlerTypes;
2725 /** Physical access handlers (AVL range+offsetptr tree). */
2726 AVLROGCPHYSTREE PhysHandlers;
2727#ifdef VBOX_WITH_RAW_MODE
2728 /** Virtual access handlers (AVL range + GC ptr tree). */
2729 AVLROGCPTRTREE VirtHandlers;
2730 /** Virtual access handlers (Phys range AVL range + offsetptr tree).
2731 * @remarks Handler of the hypervisor kind are of course not present. */
2732 AVLROGCPHYSTREE PhysToVirtHandlers;
2733 /** Virtual access handlers for the hypervisor (AVL range + GC ptr tree). */
2734 AVLROGCPTRTREE HyperVirtHandlers;
2735 /** List of virtual access handler types (offset pointers) of type
2736 * PGMVIRTHANDLERTYPEINT. This is needed for relocations. */
2737 RTLISTOFF32ANCHOR HeadVirtHandlerTypes;
2738#endif
2739} PGMTREES;
2740/** Pointer to PGM trees. */
2741typedef PGMTREES *PPGMTREES;
2742
2743
2744/**
2745 * Page fault guest state for the AMD64 paging mode.
2746 */
2747typedef struct PGMPTWALKCORE
2748{
2749 /** The guest virtual address that is being resolved by the walk
2750 * (input). */
2751 RTGCPTR GCPtr;
2752
2753 /** The guest physical address that is the result of the walk.
2754 * @remarks only valid if fSucceeded is set. */
2755 RTGCPHYS GCPhys;
2756
2757 /** Set if the walk succeeded, i.d. GCPhys is valid. */
2758 bool fSucceeded;
2759 /** The level problem arrised at.
2760 * PTE is level 1, PDE is level 2, PDPE is level 3, PML4 is level 4, CR3 is
2761 * level 8. This is 0 on success. */
2762 uint8_t uLevel;
2763 /** Set if the page isn't present. */
2764 bool fNotPresent;
2765 /** Encountered a bad physical address. */
2766 bool fBadPhysAddr;
2767 /** Set if there was reserved bit violations. */
2768 bool fRsvdError;
2769 /** Set if it involves a big page (2/4 MB). */
2770 bool fBigPage;
2771 /** Set if it involves a gigantic page (1 GB). */
2772 bool fGigantPage;
2773 /** The effect X86_PTE_US flag for the address. */
2774 bool fEffectiveUS;
2775 /** The effect X86_PTE_RW flag for the address. */
2776 bool fEffectiveRW;
2777 /** The effect X86_PTE_NX flag for the address. */
2778 bool fEffectiveNX;
2779} PGMPTWALKCORE;
2780
2781
2782/**
2783 * Guest page table walk for the AMD64 mode.
2784 */
2785typedef struct PGMPTWALKGSTAMD64
2786{
2787 /** The common core. */
2788 PGMPTWALKCORE Core;
2789
2790 PX86PML4 pPml4;
2791 PX86PML4E pPml4e;
2792 X86PML4E Pml4e;
2793
2794 PX86PDPT pPdpt;
2795 PX86PDPE pPdpe;
2796 X86PDPE Pdpe;
2797
2798 PX86PDPAE pPd;
2799 PX86PDEPAE pPde;
2800 X86PDEPAE Pde;
2801
2802 PX86PTPAE pPt;
2803 PX86PTEPAE pPte;
2804 X86PTEPAE Pte;
2805} PGMPTWALKGSTAMD64;
2806/** Pointer to a AMD64 guest page table walk. */
2807typedef PGMPTWALKGSTAMD64 *PPGMPTWALKGSTAMD64;
2808/** Pointer to a const AMD64 guest page table walk. */
2809typedef PGMPTWALKGSTAMD64 const *PCPGMPTWALKGSTAMD64;
2810
2811/**
2812 * Guest page table walk for the PAE mode.
2813 */
2814typedef struct PGMPTWALKGSTPAE
2815{
2816 /** The common core. */
2817 PGMPTWALKCORE Core;
2818
2819 PX86PDPT pPdpt;
2820 PX86PDPE pPdpe;
2821 X86PDPE Pdpe;
2822
2823 PX86PDPAE pPd;
2824 PX86PDEPAE pPde;
2825 X86PDEPAE Pde;
2826
2827 PX86PTPAE pPt;
2828 PX86PTEPAE pPte;
2829 X86PTEPAE Pte;
2830} PGMPTWALKGSTPAE;
2831/** Pointer to a PAE guest page table walk. */
2832typedef PGMPTWALKGSTPAE *PPGMPTWALKGSTPAE;
2833/** Pointer to a const AMD64 guest page table walk. */
2834typedef PGMPTWALKGSTPAE const *PCPGMPTWALKGSTPAE;
2835
2836/**
2837 * Guest page table walk for the 32-bit mode.
2838 */
2839typedef struct PGMPTWALKGST32BIT
2840{
2841 /** The common core. */
2842 PGMPTWALKCORE Core;
2843
2844 PX86PD pPd;
2845 PX86PDE pPde;
2846 X86PDE Pde;
2847
2848 PX86PT pPt;
2849 PX86PTE pPte;
2850 X86PTE Pte;
2851} PGMPTWALKGST32BIT;
2852/** Pointer to a 32-bit guest page table walk. */
2853typedef PGMPTWALKGST32BIT *PPGMPTWALKGST32BIT;
2854/** Pointer to a const 32-bit guest page table walk. */
2855typedef PGMPTWALKGST32BIT const *PCPGMPTWALKGST32BIT;
2856
2857/**
2858 * Which part of PGMPTWALKGST that is valid.
2859 */
2860typedef enum PGMPTWALKGSTTYPE
2861{
2862 /** Customary invalid 0 value. */
2863 PGMPTWALKGSTTYPE_INVALID = 0,
2864 /** PGMPTWALKGST::u.Amd64 is valid. */
2865 PGMPTWALKGSTTYPE_AMD64,
2866 /** PGMPTWALKGST::u.Pae is valid. */
2867 PGMPTWALKGSTTYPE_PAE,
2868 /** PGMPTWALKGST::u.Legacy is valid. */
2869 PGMPTWALKGSTTYPE_32BIT,
2870 /** Customary 32-bit type hack. */
2871 PGMPTWALKGSTTYPE_32BIT_HACK = 0x7fff0000
2872} PGMPTWALKGSTTYPE;
2873
2874/**
2875 * Combined guest page table walk result.
2876 */
2877typedef struct PGMPTWALKGST
2878{
2879 union
2880 {
2881 /** The page walker core - always valid. */
2882 PGMPTWALKCORE Core;
2883 /** The page walker for AMD64. */
2884 PGMPTWALKGSTAMD64 Amd64;
2885 /** The page walker for PAE (32-bit). */
2886 PGMPTWALKGSTPAE Pae;
2887 /** The page walker for 32-bit paging (called legacy due to C naming
2888 * convension). */
2889 PGMPTWALKGST32BIT Legacy;
2890 } u;
2891 /** Indicates which part of the union is valid. */
2892 PGMPTWALKGSTTYPE enmType;
2893} PGMPTWALKGST;
2894/** Pointer to a combined guest page table walk result. */
2895typedef PGMPTWALKGST *PPGMPTWALKGST;
2896/** Pointer to a read-only combined guest page table walk result. */
2897typedef PGMPTWALKGST const *PCPGMPTWALKGST;
2898
2899
2900/** @name Paging mode macros
2901 * @{
2902 */
2903#ifdef IN_RC
2904# define PGM_CTX(a,b) a##RC##b
2905# define PGM_CTX_STR(a,b) a "GC" b
2906# define PGM_CTX_DECL(type) VMMRCDECL(type)
2907#else
2908# ifdef IN_RING3
2909# define PGM_CTX(a,b) a##R3##b
2910# define PGM_CTX_STR(a,b) a "R3" b
2911# define PGM_CTX_DECL(type) DECLCALLBACK(type)
2912# else
2913# define PGM_CTX(a,b) a##R0##b
2914# define PGM_CTX_STR(a,b) a "R0" b
2915# define PGM_CTX_DECL(type) VMMDECL(type)
2916# endif
2917#endif
2918
2919#define PGM_GST_NAME_REAL(name) PGM_CTX(pgm,GstReal##name)
2920#define PGM_GST_NAME_RC_REAL_STR(name) "pgmRCGstReal" #name
2921#define PGM_GST_NAME_R0_REAL_STR(name) "pgmR0GstReal" #name
2922#define PGM_GST_NAME_PROT(name) PGM_CTX(pgm,GstProt##name)
2923#define PGM_GST_NAME_RC_PROT_STR(name) "pgmRCGstProt" #name
2924#define PGM_GST_NAME_R0_PROT_STR(name) "pgmR0GstProt" #name
2925#define PGM_GST_NAME_32BIT(name) PGM_CTX(pgm,Gst32Bit##name)
2926#define PGM_GST_NAME_RC_32BIT_STR(name) "pgmRCGst32Bit" #name
2927#define PGM_GST_NAME_R0_32BIT_STR(name) "pgmR0Gst32Bit" #name
2928#define PGM_GST_NAME_PAE(name) PGM_CTX(pgm,GstPAE##name)
2929#define PGM_GST_NAME_RC_PAE_STR(name) "pgmRCGstPAE" #name
2930#define PGM_GST_NAME_R0_PAE_STR(name) "pgmR0GstPAE" #name
2931#define PGM_GST_NAME_AMD64(name) PGM_CTX(pgm,GstAMD64##name)
2932#define PGM_GST_NAME_RC_AMD64_STR(name) "pgmRCGstAMD64" #name
2933#define PGM_GST_NAME_R0_AMD64_STR(name) "pgmR0GstAMD64" #name
2934#define PGM_GST_PFN(name, pVCpu) ((pVCpu)->pgm.s.PGM_CTX(pfn,Gst##name))
2935#define PGM_GST_DECL(type, name) PGM_CTX_DECL(type) PGM_GST_NAME(name)
2936
2937#define PGM_SHW_NAME_32BIT(name) PGM_CTX(pgm,Shw32Bit##name)
2938#define PGM_SHW_NAME_RC_32BIT_STR(name) "pgmRCShw32Bit" #name
2939#define PGM_SHW_NAME_R0_32BIT_STR(name) "pgmR0Shw32Bit" #name
2940#define PGM_SHW_NAME_PAE(name) PGM_CTX(pgm,ShwPAE##name)
2941#define PGM_SHW_NAME_RC_PAE_STR(name) "pgmRCShwPAE" #name
2942#define PGM_SHW_NAME_R0_PAE_STR(name) "pgmR0ShwPAE" #name
2943#define PGM_SHW_NAME_AMD64(name) PGM_CTX(pgm,ShwAMD64##name)
2944#define PGM_SHW_NAME_RC_AMD64_STR(name) "pgmRCShwAMD64" #name
2945#define PGM_SHW_NAME_R0_AMD64_STR(name) "pgmR0ShwAMD64" #name
2946#define PGM_SHW_NAME_NESTED(name) PGM_CTX(pgm,ShwNested##name)
2947#define PGM_SHW_NAME_RC_NESTED_STR(name) "pgmRCShwNested" #name
2948#define PGM_SHW_NAME_R0_NESTED_STR(name) "pgmR0ShwNested" #name
2949#define PGM_SHW_NAME_EPT(name) PGM_CTX(pgm,ShwEPT##name)
2950#define PGM_SHW_NAME_RC_EPT_STR(name) "pgmRCShwEPT" #name
2951#define PGM_SHW_NAME_R0_EPT_STR(name) "pgmR0ShwEPT" #name
2952#define PGM_SHW_DECL(type, name) PGM_CTX_DECL(type) PGM_SHW_NAME(name)
2953#define PGM_SHW_PFN(name, pVCpu) ((pVCpu)->pgm.s.PGM_CTX(pfn,Shw##name))
2954
2955/* Shw_Gst */
2956#define PGM_BTH_NAME_32BIT_REAL(name) PGM_CTX(pgm,Bth32BitReal##name)
2957#define PGM_BTH_NAME_32BIT_PROT(name) PGM_CTX(pgm,Bth32BitProt##name)
2958#define PGM_BTH_NAME_32BIT_32BIT(name) PGM_CTX(pgm,Bth32Bit32Bit##name)
2959#define PGM_BTH_NAME_PAE_REAL(name) PGM_CTX(pgm,BthPAEReal##name)
2960#define PGM_BTH_NAME_PAE_PROT(name) PGM_CTX(pgm,BthPAEProt##name)
2961#define PGM_BTH_NAME_PAE_32BIT(name) PGM_CTX(pgm,BthPAE32Bit##name)
2962#define PGM_BTH_NAME_PAE_PAE(name) PGM_CTX(pgm,BthPAEPAE##name)
2963#define PGM_BTH_NAME_AMD64_PROT(name) PGM_CTX(pgm,BthAMD64Prot##name)
2964#define PGM_BTH_NAME_AMD64_AMD64(name) PGM_CTX(pgm,BthAMD64AMD64##name)
2965#define PGM_BTH_NAME_NESTED_REAL(name) PGM_CTX(pgm,BthNestedReal##name)
2966#define PGM_BTH_NAME_NESTED_PROT(name) PGM_CTX(pgm,BthNestedProt##name)
2967#define PGM_BTH_NAME_NESTED_32BIT(name) PGM_CTX(pgm,BthNested32Bit##name)
2968#define PGM_BTH_NAME_NESTED_PAE(name) PGM_CTX(pgm,BthNestedPAE##name)
2969#define PGM_BTH_NAME_NESTED_AMD64(name) PGM_CTX(pgm,BthNestedAMD64##name)
2970#define PGM_BTH_NAME_EPT_REAL(name) PGM_CTX(pgm,BthEPTReal##name)
2971#define PGM_BTH_NAME_EPT_PROT(name) PGM_CTX(pgm,BthEPTProt##name)
2972#define PGM_BTH_NAME_EPT_32BIT(name) PGM_CTX(pgm,BthEPT32Bit##name)
2973#define PGM_BTH_NAME_EPT_PAE(name) PGM_CTX(pgm,BthEPTPAE##name)
2974#define PGM_BTH_NAME_EPT_AMD64(name) PGM_CTX(pgm,BthEPTAMD64##name)
2975
2976#define PGM_BTH_NAME_RC_32BIT_REAL_STR(name) "pgmRCBth32BitReal" #name
2977#define PGM_BTH_NAME_RC_32BIT_PROT_STR(name) "pgmRCBth32BitProt" #name
2978#define PGM_BTH_NAME_RC_32BIT_32BIT_STR(name) "pgmRCBth32Bit32Bit" #name
2979#define PGM_BTH_NAME_RC_PAE_REAL_STR(name) "pgmRCBthPAEReal" #name
2980#define PGM_BTH_NAME_RC_PAE_PROT_STR(name) "pgmRCBthPAEProt" #name
2981#define PGM_BTH_NAME_RC_PAE_32BIT_STR(name) "pgmRCBthPAE32Bit" #name
2982#define PGM_BTH_NAME_RC_PAE_PAE_STR(name) "pgmRCBthPAEPAE" #name
2983#define PGM_BTH_NAME_RC_AMD64_AMD64_STR(name) "pgmRCBthAMD64AMD64" #name
2984#define PGM_BTH_NAME_RC_NESTED_REAL_STR(name) "pgmRCBthNestedReal" #name
2985#define PGM_BTH_NAME_RC_NESTED_PROT_STR(name) "pgmRCBthNestedProt" #name
2986#define PGM_BTH_NAME_RC_NESTED_32BIT_STR(name) "pgmRCBthNested32Bit" #name
2987#define PGM_BTH_NAME_RC_NESTED_PAE_STR(name) "pgmRCBthNestedPAE" #name
2988#define PGM_BTH_NAME_RC_NESTED_AMD64_STR(name) "pgmRCBthNestedAMD64" #name
2989#define PGM_BTH_NAME_RC_EPT_REAL_STR(name) "pgmRCBthEPTReal" #name
2990#define PGM_BTH_NAME_RC_EPT_PROT_STR(name) "pgmRCBthEPTProt" #name
2991#define PGM_BTH_NAME_RC_EPT_32BIT_STR(name) "pgmRCBthEPT32Bit" #name
2992#define PGM_BTH_NAME_RC_EPT_PAE_STR(name) "pgmRCBthEPTPAE" #name
2993#define PGM_BTH_NAME_RC_EPT_AMD64_STR(name) "pgmRCBthEPTAMD64" #name
2994#define PGM_BTH_NAME_R0_32BIT_REAL_STR(name) "pgmR0Bth32BitReal" #name
2995#define PGM_BTH_NAME_R0_32BIT_PROT_STR(name) "pgmR0Bth32BitProt" #name
2996#define PGM_BTH_NAME_R0_32BIT_32BIT_STR(name) "pgmR0Bth32Bit32Bit" #name
2997#define PGM_BTH_NAME_R0_PAE_REAL_STR(name) "pgmR0BthPAEReal" #name
2998#define PGM_BTH_NAME_R0_PAE_PROT_STR(name) "pgmR0BthPAEProt" #name
2999#define PGM_BTH_NAME_R0_PAE_32BIT_STR(name) "pgmR0BthPAE32Bit" #name
3000#define PGM_BTH_NAME_R0_PAE_PAE_STR(name) "pgmR0BthPAEPAE" #name
3001#define PGM_BTH_NAME_R0_AMD64_PROT_STR(name) "pgmR0BthAMD64Prot" #name
3002#define PGM_BTH_NAME_R0_AMD64_AMD64_STR(name) "pgmR0BthAMD64AMD64" #name
3003#define PGM_BTH_NAME_R0_NESTED_REAL_STR(name) "pgmR0BthNestedReal" #name
3004#define PGM_BTH_NAME_R0_NESTED_PROT_STR(name) "pgmR0BthNestedProt" #name
3005#define PGM_BTH_NAME_R0_NESTED_32BIT_STR(name) "pgmR0BthNested32Bit" #name
3006#define PGM_BTH_NAME_R0_NESTED_PAE_STR(name) "pgmR0BthNestedPAE" #name
3007#define PGM_BTH_NAME_R0_NESTED_AMD64_STR(name) "pgmR0BthNestedAMD64" #name
3008#define PGM_BTH_NAME_R0_EPT_REAL_STR(name) "pgmR0BthEPTReal" #name
3009#define PGM_BTH_NAME_R0_EPT_PROT_STR(name) "pgmR0BthEPTProt" #name
3010#define PGM_BTH_NAME_R0_EPT_32BIT_STR(name) "pgmR0BthEPT32Bit" #name
3011#define PGM_BTH_NAME_R0_EPT_PAE_STR(name) "pgmR0BthEPTPAE" #name
3012#define PGM_BTH_NAME_R0_EPT_AMD64_STR(name) "pgmR0BthEPTAMD64" #name
3013
3014#define PGM_BTH_DECL(type, name) PGM_CTX_DECL(type) PGM_BTH_NAME(name)
3015#define PGM_BTH_PFN(name, pVCpu) ((pVCpu)->pgm.s.PGM_CTX(pfn,Bth##name))
3016/** @} */
3017
3018/**
3019 * Data for each paging mode.
3020 */
3021typedef struct PGMMODEDATA
3022{
3023 /** The guest mode type. */
3024 uint32_t uGstType;
3025 /** The shadow mode type. */
3026 uint32_t uShwType;
3027
3028 /** @name Function pointers for Shadow paging.
3029 * @{
3030 */
3031 DECLR3CALLBACKMEMBER(int, pfnR3ShwRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
3032 DECLR3CALLBACKMEMBER(int, pfnR3ShwExit,(PVMCPU pVCpu));
3033 DECLR3CALLBACKMEMBER(int, pfnR3ShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
3034 DECLR3CALLBACKMEMBER(int, pfnR3ShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask, uint32_t fOpFlags));
3035
3036 DECLRCCALLBACKMEMBER(int, pfnRCShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
3037 DECLRCCALLBACKMEMBER(int, pfnRCShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask, uint32_t fOpFlags));
3038
3039 DECLR0CALLBACKMEMBER(int, pfnR0ShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
3040 DECLR0CALLBACKMEMBER(int, pfnR0ShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask, uint32_t fOpFlags));
3041 /** @} */
3042
3043 /** @name Function pointers for Guest paging.
3044 * @{
3045 */
3046 DECLR3CALLBACKMEMBER(int, pfnR3GstRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
3047 DECLR3CALLBACKMEMBER(int, pfnR3GstExit,(PVMCPU pVCpu));
3048 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
3049 DECLR3CALLBACKMEMBER(int, pfnR3GstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
3050 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
3051 DECLRCCALLBACKMEMBER(int, pfnRCGstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
3052 DECLRCCALLBACKMEMBER(int, pfnRCGstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
3053 DECLRCCALLBACKMEMBER(int, pfnRCGstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
3054 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
3055 DECLR0CALLBACKMEMBER(int, pfnR0GstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
3056 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
3057 /** @} */
3058
3059 /** @name Function pointers for Both Shadow and Guest paging.
3060 * @{
3061 */
3062 DECLR3CALLBACKMEMBER(int, pfnR3BthRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
3063 /* no pfnR3BthTrap0eHandler */
3064 DECLR3CALLBACKMEMBER(int, pfnR3BthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
3065 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
3066 DECLR3CALLBACKMEMBER(int, pfnR3BthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
3067 DECLR3CALLBACKMEMBER(int, pfnR3BthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
3068#ifdef VBOX_STRICT
3069 DECLR3CALLBACKMEMBER(unsigned, pfnR3BthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
3070#endif
3071 DECLR3CALLBACKMEMBER(int, pfnR3BthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
3072 DECLR3CALLBACKMEMBER(int, pfnR3BthUnmapCR3,(PVMCPU pVCpu));
3073
3074 DECLRCCALLBACKMEMBER(int, pfnRCBthTrap0eHandler,(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, bool *pfLockTaken));
3075 DECLRCCALLBACKMEMBER(int, pfnRCBthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
3076 DECLRCCALLBACKMEMBER(int, pfnRCBthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
3077 DECLRCCALLBACKMEMBER(int, pfnRCBthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
3078 DECLRCCALLBACKMEMBER(int, pfnRCBthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
3079#ifdef VBOX_STRICT
3080 DECLRCCALLBACKMEMBER(unsigned, pfnRCBthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
3081#endif
3082 DECLRCCALLBACKMEMBER(int, pfnRCBthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
3083 DECLRCCALLBACKMEMBER(int, pfnRCBthUnmapCR3,(PVMCPU pVCpu));
3084
3085 DECLR0CALLBACKMEMBER(int, pfnR0BthTrap0eHandler,(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, bool *pfLockTaken));
3086 DECLR0CALLBACKMEMBER(int, pfnR0BthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
3087 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
3088 DECLR0CALLBACKMEMBER(int, pfnR0BthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
3089 DECLR0CALLBACKMEMBER(int, pfnR0BthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
3090#ifdef VBOX_STRICT
3091 DECLR0CALLBACKMEMBER(unsigned, pfnR0BthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
3092#endif
3093 DECLR0CALLBACKMEMBER(int, pfnR0BthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
3094 DECLR0CALLBACKMEMBER(int, pfnR0BthUnmapCR3,(PVMCPU pVCpu));
3095 /** @} */
3096} PGMMODEDATA, *PPGMMODEDATA;
3097
3098
3099#ifdef VBOX_WITH_STATISTICS
3100/**
3101 * PGM statistics.
3102 *
3103 * These lives on the heap when compiled in as they would otherwise waste
3104 * unnecessary space in release builds.
3105 */
3106typedef struct PGMSTATS
3107{
3108 /* R3 only: */
3109 STAMCOUNTER StatR3DetectedConflicts; /**< R3: Number of times PGMR3MapHasConflicts() detected a conflict. */
3110 STAMPROFILE StatR3ResolveConflict; /**< R3: pgmR3SyncPTResolveConflict() profiling (includes the entire relocation). */
3111
3112 /* R3+RZ */
3113 STAMCOUNTER StatRZChunkR3MapTlbHits; /**< RC/R0: Ring-3/0 chunk mapper TLB hits. */
3114 STAMCOUNTER StatRZChunkR3MapTlbMisses; /**< RC/R0: Ring-3/0 chunk mapper TLB misses. */
3115 STAMCOUNTER StatRZPageMapTlbHits; /**< RC/R0: Ring-3/0 page mapper TLB hits. */
3116 STAMCOUNTER StatRZPageMapTlbMisses; /**< RC/R0: Ring-3/0 page mapper TLB misses. */
3117 STAMCOUNTER StatPageMapTlbFlushes; /**< ALL: Ring-3/0 page mapper TLB flushes. */
3118 STAMCOUNTER StatPageMapTlbFlushEntry; /**< ALL: Ring-3/0 page mapper TLB flushes. */
3119 STAMCOUNTER StatR3ChunkR3MapTlbHits; /**< R3: Ring-3/0 chunk mapper TLB hits. */
3120 STAMCOUNTER StatR3ChunkR3MapTlbMisses; /**< R3: Ring-3/0 chunk mapper TLB misses. */
3121 STAMCOUNTER StatR3PageMapTlbHits; /**< R3: Ring-3/0 page mapper TLB hits. */
3122 STAMCOUNTER StatR3PageMapTlbMisses; /**< R3: Ring-3/0 page mapper TLB misses. */
3123 STAMCOUNTER StatRZRamRangeTlbHits; /**< RC/R0: RAM range TLB hits. */
3124 STAMCOUNTER StatRZRamRangeTlbMisses; /**< RC/R0: RAM range TLB misses. */
3125 STAMCOUNTER StatR3RamRangeTlbHits; /**< R3: RAM range TLB hits. */
3126 STAMCOUNTER StatR3RamRangeTlbMisses; /**< R3: RAM range TLB misses. */
3127 STAMPROFILE StatRZSyncCR3HandlerVirtualReset; /**< RC/R0: Profiling of the virtual handler resets. */
3128 STAMPROFILE StatRZSyncCR3HandlerVirtualUpdate; /**< RC/R0: Profiling of the virtual handler updates. */
3129 STAMPROFILE StatR3SyncCR3HandlerVirtualReset; /**< R3: Profiling of the virtual handler resets. */
3130 STAMPROFILE StatR3SyncCR3HandlerVirtualUpdate; /**< R3: Profiling of the virtual handler updates. */
3131 STAMCOUNTER StatR3PhysHandlerReset; /**< R3: The number of times PGMHandlerPhysicalReset is called. */
3132 STAMCOUNTER StatRZPhysHandlerReset; /**< RC/R0: The number of times PGMHandlerPhysicalReset is called. */
3133 STAMCOUNTER StatR3PhysHandlerLookupHits; /**< R3: Number of cache hits when looking up physical handlers. */
3134 STAMCOUNTER StatR3PhysHandlerLookupMisses; /**< R3: Number of cache misses when looking up physical handlers. */
3135 STAMCOUNTER StatRZPhysHandlerLookupHits; /**< RC/R0: Number of cache hits when lookup up physical handlers. */
3136 STAMCOUNTER StatRZPhysHandlerLookupMisses; /**< RC/R0: Number of cache misses when looking up physical handlers */
3137 STAMPROFILE StatRZVirtHandlerSearchByPhys; /**< RC/R0: Profiling of pgmHandlerVirtualFindByPhysAddr. */
3138 STAMPROFILE StatR3VirtHandlerSearchByPhys; /**< R3: Profiling of pgmHandlerVirtualFindByPhysAddr. */
3139 STAMCOUNTER StatRZPageReplaceShared; /**< RC/R0: Times a shared page has been replaced by a private one. */
3140 STAMCOUNTER StatRZPageReplaceZero; /**< RC/R0: Times the zero page has been replaced by a private one. */
3141/// @todo STAMCOUNTER StatRZPageHandyAllocs; /**< RC/R0: The number of times we've executed GMMR3AllocateHandyPages. */
3142 STAMCOUNTER StatR3PageReplaceShared; /**< R3: Times a shared page has been replaced by a private one. */
3143 STAMCOUNTER StatR3PageReplaceZero; /**< R3: Times the zero page has been replaced by a private one. */
3144/// @todo STAMCOUNTER StatR3PageHandyAllocs; /**< R3: The number of times we've executed GMMR3AllocateHandyPages. */
3145
3146 /* RC only: */
3147 STAMCOUNTER StatRCInvlPgConflict; /**< RC: Number of times PGMInvalidatePage() detected a mapping conflict. */
3148 STAMCOUNTER StatRCInvlPgSyncMonCR3; /**< RC: Number of times PGMInvalidatePage() ran into PGM_SYNC_MONITOR_CR3. */
3149
3150 STAMCOUNTER StatRZPhysRead;
3151 STAMCOUNTER StatRZPhysReadBytes;
3152 STAMCOUNTER StatRZPhysWrite;
3153 STAMCOUNTER StatRZPhysWriteBytes;
3154 STAMCOUNTER StatR3PhysRead;
3155 STAMCOUNTER StatR3PhysReadBytes;
3156 STAMCOUNTER StatR3PhysWrite;
3157 STAMCOUNTER StatR3PhysWriteBytes;
3158 STAMCOUNTER StatRCPhysRead;
3159 STAMCOUNTER StatRCPhysReadBytes;
3160 STAMCOUNTER StatRCPhysWrite;
3161 STAMCOUNTER StatRCPhysWriteBytes;
3162
3163 STAMCOUNTER StatRZPhysSimpleRead;
3164 STAMCOUNTER StatRZPhysSimpleReadBytes;
3165 STAMCOUNTER StatRZPhysSimpleWrite;
3166 STAMCOUNTER StatRZPhysSimpleWriteBytes;
3167 STAMCOUNTER StatR3PhysSimpleRead;
3168 STAMCOUNTER StatR3PhysSimpleReadBytes;
3169 STAMCOUNTER StatR3PhysSimpleWrite;
3170 STAMCOUNTER StatR3PhysSimpleWriteBytes;
3171 STAMCOUNTER StatRCPhysSimpleRead;
3172 STAMCOUNTER StatRCPhysSimpleReadBytes;
3173 STAMCOUNTER StatRCPhysSimpleWrite;
3174 STAMCOUNTER StatRCPhysSimpleWriteBytes;
3175
3176 STAMCOUNTER StatTrackVirgin; /**< The number of first time shadowings. */
3177 STAMCOUNTER StatTrackAliased; /**< The number of times switching to cRef2, i.e. the page is being shadowed by two PTs. */
3178 STAMCOUNTER StatTrackAliasedMany; /**< The number of times we're tracking using cRef2. */
3179 STAMCOUNTER StatTrackAliasedLots; /**< The number of times we're hitting pages which has overflowed cRef2. */
3180 STAMCOUNTER StatTrackNoExtentsLeft; /**< The number of times the extent list was exhausted. */
3181 STAMCOUNTER StatTrackOverflows; /**< The number of times the extent list grows to long. */
3182 STAMPROFILE StatTrackDeref; /**< Profiling of SyncPageWorkerTrackDeref (expensive). */
3183
3184 /** Time spent by the host OS for large page allocation. */
3185 STAMPROFILE StatAllocLargePage;
3186 /** Time spent clearing the newly allocated large pages. */
3187 STAMPROFILE StatClearLargePage;
3188 /** The number of times allocating a large pages takes more than the allowed period. */
3189 STAMCOUNTER StatLargePageOverflow;
3190 /** pgmPhysIsValidLargePage profiling - R3 */
3191 STAMPROFILE StatR3IsValidLargePage;
3192 /** pgmPhysIsValidLargePage profiling - RZ*/
3193 STAMPROFILE StatRZIsValidLargePage;
3194
3195 STAMPROFILE StatChunkAging;
3196 STAMPROFILE StatChunkFindCandidate;
3197 STAMPROFILE StatChunkUnmap;
3198 STAMPROFILE StatChunkMap;
3199} PGMSTATS;
3200#endif /* VBOX_WITH_STATISTICS */
3201
3202
3203/**
3204 * Converts a PGM pointer into a VM pointer.
3205 * @returns Pointer to the VM structure the PGM is part of.
3206 * @param pPGM Pointer to PGM instance data.
3207 */
3208#define PGM2VM(pPGM) ( (PVM)((char*)pPGM - pPGM->offVM) )
3209
3210/**
3211 * PGM Data (part of VM)
3212 */
3213typedef struct PGM
3214{
3215 /** Offset to the VM structure. */
3216 int32_t offVM;
3217 /** Offset of the PGMCPU structure relative to VMCPU. */
3218 int32_t offVCpuPGM;
3219
3220 /** @cfgm{/RamPreAlloc, boolean, false}
3221 * Indicates whether the base RAM should all be allocated before starting
3222 * the VM (default), or if it should be allocated when first written to.
3223 */
3224 bool fRamPreAlloc;
3225 /** Indicates whether write monitoring is currently in use.
3226 * This is used to prevent conflicts between live saving and page sharing
3227 * detection. */
3228 bool fPhysWriteMonitoringEngaged;
3229 /** Set if the CPU has less than 52-bit physical address width.
3230 * This is used */
3231 bool fLessThan52PhysicalAddressBits;
3232 /** Set when nested paging is active.
3233 * This is meant to save calls to HMIsNestedPagingActive and let the
3234 * compilers optimize the code better. Whether we use nested paging or
3235 * not is something we find out during VMM initialization and we won't
3236 * change this later on. */
3237 bool fNestedPaging;
3238 /** The host paging mode. (This is what SUPLib reports.) */
3239 SUPPAGINGMODE enmHostMode;
3240 /** We're not in a state which permits writes to guest memory.
3241 * (Only used in strict builds.) */
3242 bool fNoMorePhysWrites;
3243 /** @cfgm{/PageFusionAllowed, boolean, false}
3244 * Whether page fusion is allowed. */
3245 bool fPageFusionAllowed;
3246 /** @cfgm{/PGM/PciPassThrough, boolean, false}
3247 * Whether PCI passthrough is enabled. */
3248 bool fPciPassthrough;
3249 /** The number of MMIO2 regions (serves as the next MMIO2 ID). */
3250 uint8_t cMmio2Regions;
3251 /** Restore original ROM page content when resetting after loading state.
3252 * The flag is set by pgmR3LoadRomRanges and cleared at reset. This
3253 * enables the VM to start using an updated ROM without requiring powering
3254 * down the VM, just rebooting or resetting it. */
3255 bool fRestoreRomPagesOnReset;
3256 /** Whether to automatically clear all RAM pages on reset. */
3257 bool fZeroRamPagesOnReset;
3258 /** Alignment padding. */
3259 bool afAlignment3[7];
3260
3261 /** Indicates that PGMR3FinalizeMappings has been called and that further
3262 * PGMR3MapIntermediate calls will be rejected. */
3263 bool fFinalizedMappings;
3264 /** If set no conflict checks are required. */
3265 bool fMappingsFixed;
3266 /** If set if restored as fixed but we were unable to re-fixate at the old
3267 * location because of room or address incompatibilities. */
3268 bool fMappingsFixedRestored;
3269 /** Size of fixed mapping.
3270 * This is valid if either fMappingsFixed or fMappingsFixedRestored is set. */
3271 uint32_t cbMappingFixed;
3272 /** Generation ID for the RAM ranges. This member is incremented everytime
3273 * a RAM range is linked or unlinked. */
3274 uint32_t volatile idRamRangesGen;
3275
3276 /** Base address (GC) of fixed mapping.
3277 * This is valid if either fMappingsFixed or fMappingsFixedRestored is set. */
3278 RTGCPTR GCPtrMappingFixed;
3279 /** The address of the previous RAM range mapping. */
3280 RTGCPTR GCPtrPrevRamRangeMapping;
3281
3282 /** Physical access handler type for ROM protection. */
3283 PGMPHYSHANDLERTYPE hRomPhysHandlerType;
3284 /** Alignment padding. */
3285 uint32_t u32Padding;
3286
3287 /** 4 MB page mask; 32 or 36 bits depending on PSE-36 (identical for all VCPUs) */
3288 RTGCPHYS GCPhys4MBPSEMask;
3289 /** Mask containing the invalid bits of a guest physical address.
3290 * @remarks this does not stop at bit 52. */
3291 RTGCPHYS GCPhysInvAddrMask;
3292
3293
3294 /** RAM range TLB for R3. */
3295 R3PTRTYPE(PPGMRAMRANGE) apRamRangesTlbR3[PGM_RAMRANGE_TLB_ENTRIES];
3296 /** Pointer to the list of RAM ranges (Phys GC -> Phys HC conversion) - for R3.
3297 * This is sorted by physical address and contains no overlapping ranges. */
3298 R3PTRTYPE(PPGMRAMRANGE) pRamRangesXR3;
3299 /** Root of the RAM range search tree for ring-3. */
3300 R3PTRTYPE(PPGMRAMRANGE) pRamRangeTreeR3;
3301 /** PGM offset based trees - R3 Ptr. */
3302 R3PTRTYPE(PPGMTREES) pTreesR3;
3303 /** Caching the last physical handler we looked up in R3. */
3304 R3PTRTYPE(PPGMPHYSHANDLER) pLastPhysHandlerR3;
3305 /** Shadow Page Pool - R3 Ptr. */
3306 R3PTRTYPE(PPGMPOOL) pPoolR3;
3307 /** Linked list of GC mappings - for HC.
3308 * The list is sorted ascending on address. */
3309 R3PTRTYPE(PPGMMAPPING) pMappingsR3;
3310 /** Pointer to the list of ROM ranges - for R3.
3311 * This is sorted by physical address and contains no overlapping ranges. */
3312 R3PTRTYPE(PPGMROMRANGE) pRomRangesR3;
3313 /** Pointer to the list of MMIO2 ranges - for R3.
3314 * Registration order. */
3315 R3PTRTYPE(PPGMREGMMIORANGE) pRegMmioRangesR3;
3316 /** Pointer to SHW+GST mode data (function pointers).
3317 * The index into this table is made up from */
3318 R3PTRTYPE(PPGMMODEDATA) paModeData;
3319 RTR3PTR R3PtrAlignment0;
3320 /** MMIO2 lookup array for ring-3. Indexed by idMmio2 minus 1. */
3321 R3PTRTYPE(PPGMREGMMIORANGE) apMmio2RangesR3[PGM_MMIO2_MAX_RANGES];
3322
3323 /** RAM range TLB for R0. */
3324 R0PTRTYPE(PPGMRAMRANGE) apRamRangesTlbR0[PGM_RAMRANGE_TLB_ENTRIES];
3325 /** R0 pointer corresponding to PGM::pRamRangesXR3. */
3326 R0PTRTYPE(PPGMRAMRANGE) pRamRangesXR0;
3327 /** Root of the RAM range search tree for ring-0. */
3328 R0PTRTYPE(PPGMRAMRANGE) pRamRangeTreeR0;
3329 /** PGM offset based trees - R0 Ptr. */
3330 R0PTRTYPE(PPGMTREES) pTreesR0;
3331 /** Caching the last physical handler we looked up in R0. */
3332 R0PTRTYPE(PPGMPHYSHANDLER) pLastPhysHandlerR0;
3333 /** Shadow Page Pool - R0 Ptr. */
3334 R0PTRTYPE(PPGMPOOL) pPoolR0;
3335 /** Linked list of GC mappings - for R0.
3336 * The list is sorted ascending on address. */
3337 R0PTRTYPE(PPGMMAPPING) pMappingsR0;
3338 /** R0 pointer corresponding to PGM::pRomRangesR3. */
3339 R0PTRTYPE(PPGMROMRANGE) pRomRangesR0;
3340 RTR0PTR R0PtrAlignment0;
3341 /** MMIO2 lookup array for ring-0. Indexed by idMmio2 minus 1. */
3342 R0PTRTYPE(PPGMREGMMIORANGE) apMmio2RangesR0[PGM_MMIO2_MAX_RANGES];
3343
3344 /** RAM range TLB for RC. */
3345 RCPTRTYPE(PPGMRAMRANGE) apRamRangesTlbRC[PGM_RAMRANGE_TLB_ENTRIES];
3346 /** RC pointer corresponding to PGM::pRamRangesXR3. */
3347 RCPTRTYPE(PPGMRAMRANGE) pRamRangesXRC;
3348 /** Root of the RAM range search tree for raw-mode context. */
3349 RCPTRTYPE(PPGMRAMRANGE) pRamRangeTreeRC;
3350 /** PGM offset based trees - RC Ptr. */
3351 RCPTRTYPE(PPGMTREES) pTreesRC;
3352 /** Caching the last physical handler we looked up in RC. */
3353 RCPTRTYPE(PPGMPHYSHANDLER) pLastPhysHandlerRC;
3354 /** Shadow Page Pool - RC Ptr. */
3355 RCPTRTYPE(PPGMPOOL) pPoolRC;
3356 /** Linked list of GC mappings - for RC.
3357 * The list is sorted ascending on address. */
3358 RCPTRTYPE(PPGMMAPPING) pMappingsRC;
3359 /** RC pointer corresponding to PGM::pRomRangesR3. */
3360 RCPTRTYPE(PPGMROMRANGE) pRomRangesRC;
3361 RTRCPTR RCPtrAlignment0;
3362 /** Pointer to the page table entries for the dynamic page mapping area - GCPtr. */
3363 RCPTRTYPE(PX86PTE) paDynPageMap32BitPTEsGC;
3364 /** Pointer to the page table entries for the dynamic page mapping area - GCPtr. */
3365 RCPTRTYPE(PPGMSHWPTEPAE) paDynPageMapPaePTEsGC;
3366
3367
3368 /** Pointer to the 5 page CR3 content mapping.
3369 * The first page is always the CR3 (in some form) while the 4 other pages
3370 * are used of the PDs in PAE mode. */
3371 RTGCPTR GCPtrCR3Mapping;
3372
3373 /** @name Intermediate Context
3374 * @{ */
3375 /** Pointer to the intermediate page directory - Normal. */
3376 R3PTRTYPE(PX86PD) pInterPD;
3377 /** Pointer to the intermediate page tables - Normal.
3378 * There are two page tables, one for the identity mapping and one for
3379 * the host context mapping (of the core code). */
3380 R3PTRTYPE(PX86PT) apInterPTs[2];
3381 /** Pointer to the intermediate page tables - PAE. */
3382 R3PTRTYPE(PX86PTPAE) apInterPaePTs[2];
3383 /** Pointer to the intermediate page directory - PAE. */
3384 R3PTRTYPE(PX86PDPAE) apInterPaePDs[4];
3385 /** Pointer to the intermediate page directory - PAE. */
3386 R3PTRTYPE(PX86PDPT) pInterPaePDPT;
3387 /** Pointer to the intermediate page-map level 4 - AMD64. */
3388 R3PTRTYPE(PX86PML4) pInterPaePML4;
3389 /** Pointer to the intermediate page directory - AMD64. */
3390 R3PTRTYPE(PX86PDPT) pInterPaePDPT64;
3391 /** The Physical Address (HC) of the intermediate Page Directory - Normal. */
3392 RTHCPHYS HCPhysInterPD;
3393 /** The Physical Address (HC) of the intermediate Page Directory Pointer Table - PAE. */
3394 RTHCPHYS HCPhysInterPaePDPT;
3395 /** The Physical Address (HC) of the intermediate Page Map Level 4 table - AMD64. */
3396 RTHCPHYS HCPhysInterPaePML4;
3397 /** @} */
3398
3399 /** Base address of the dynamic page mapping area.
3400 * The array is MM_HYPER_DYNAMIC_SIZE bytes big.
3401 *
3402 * @todo The plan of keeping PGMRCDYNMAP private to PGMRZDynMap.cpp didn't
3403 * work out. Some cleaning up of the initialization that would
3404 * remove this memory is yet to be done...
3405 */
3406 RCPTRTYPE(uint8_t *) pbDynPageMapBaseGC;
3407 /** The address of the raw-mode context mapping cache. */
3408 RCPTRTYPE(PPGMRCDYNMAP) pRCDynMap;
3409 /** The address of the ring-0 mapping cache if we're making use of it. */
3410 RTR0PTR pvR0DynMapUsed;
3411
3412 /** Hack: Number of deprecated page mapping locks taken by the current lock
3413 * owner via pgmPhysGCPhys2CCPtrInternalDepr. */
3414 uint32_t cDeprecatedPageLocks;
3415#if HC_ARCH_BITS == 64
3416 /** Alignment padding. */
3417 uint32_t u32Alignment2;
3418#endif
3419
3420
3421 /** PGM critical section.
3422 * This protects the physical & virtual access handlers, ram ranges,
3423 * and the page flag updating (some of it anyway).
3424 */
3425 PDMCRITSECT CritSectX;
3426
3427 /**
3428 * Data associated with managing the ring-3 mappings of the allocation chunks.
3429 */
3430 struct
3431 {
3432 /** The chunk tree, ordered by chunk id. */
3433#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE
3434 R3PTRTYPE(PAVLU32NODECORE) pTree;
3435#else
3436 R3R0PTRTYPE(PAVLU32NODECORE) pTree;
3437#endif
3438#if HC_ARCH_BITS == 32
3439 uint32_t u32Alignment0;
3440#endif
3441 /** The chunk mapping TLB. */
3442 PGMCHUNKR3MAPTLB Tlb;
3443 /** The number of mapped chunks. */
3444 uint32_t c;
3445 /** @cfgm{/PGM/MaxRing3Chunks, uint32_t, host dependent}
3446 * The maximum number of mapped chunks. On 64-bit this is unlimited by default,
3447 * on 32-bit it defaults to 1 or 3 GB depending on the host. */
3448 uint32_t cMax;
3449 /** The current time. This is incremented whenever a chunk is inserted. */
3450 uint32_t iNow;
3451 /** Alignment padding. */
3452 uint32_t u32Alignment1;
3453 } ChunkR3Map;
3454
3455 /**
3456 * The page mapping TLB for ring-3 and (for the time being) ring-0.
3457 */
3458 PGMPAGER3MAPTLB PhysTlbHC;
3459
3460 /** @name The zero page.
3461 * @{ */
3462 /** The host physical address of the zero page. */
3463 RTHCPHYS HCPhysZeroPg;
3464 /** The ring-3 mapping of the zero page. */
3465 RTR3PTR pvZeroPgR3;
3466 /** The ring-0 mapping of the zero page. */
3467 RTR0PTR pvZeroPgR0;
3468 /** The GC mapping of the zero page. */
3469 RTRCPTR pvZeroPgRC;
3470 RTRCPTR RCPtrAlignment3;
3471 /** @}*/
3472
3473 /** @name The Invalid MMIO page.
3474 * This page is filled with 0xfeedface.
3475 * @{ */
3476 /** The host physical address of the invalid MMIO page. */
3477 RTHCPHYS HCPhysMmioPg;
3478 /** The host pysical address of the invalid MMIO page plus all invalid
3479 * physical address bits set. This is used to trigger X86_TRAP_PF_RSVD.
3480 * @remarks Check fLessThan52PhysicalAddressBits before use. */
3481 RTHCPHYS HCPhysInvMmioPg;
3482 /** The ring-3 mapping of the invalid MMIO page. */
3483 RTR3PTR pvMmioPgR3;
3484#if HC_ARCH_BITS == 32
3485 RTR3PTR R3PtrAlignment4;
3486#endif
3487 /** @} */
3488
3489
3490 /** The number of handy pages. */
3491 uint32_t cHandyPages;
3492
3493 /** The number of large handy pages. */
3494 uint32_t cLargeHandyPages;
3495
3496 /**
3497 * Array of handy pages.
3498 *
3499 * This array is used in a two way communication between pgmPhysAllocPage
3500 * and GMMR0AllocateHandyPages, with PGMR3PhysAllocateHandyPages serving as
3501 * an intermediary.
3502 *
3503 * The size of this array is important, see pgmPhysEnsureHandyPage for details.
3504 * (The current size of 32 pages, means 128 KB of handy memory.)
3505 */
3506 GMMPAGEDESC aHandyPages[PGM_HANDY_PAGES];
3507
3508 /**
3509 * Array of large handy pages. (currently size 1)
3510 *
3511 * This array is used in a two way communication between pgmPhysAllocLargePage
3512 * and GMMR0AllocateLargePage, with PGMR3PhysAllocateLargePage serving as
3513 * an intermediary.
3514 */
3515 GMMPAGEDESC aLargeHandyPage[1];
3516
3517 /**
3518 * Live save data.
3519 */
3520 struct
3521 {
3522 /** Per type statistics. */
3523 struct
3524 {
3525 /** The number of ready pages. */
3526 uint32_t cReadyPages;
3527 /** The number of dirty pages. */
3528 uint32_t cDirtyPages;
3529 /** The number of ready zero pages. */
3530 uint32_t cZeroPages;
3531 /** The number of write monitored pages. */
3532 uint32_t cMonitoredPages;
3533 } Rom,
3534 Mmio2,
3535 Ram;
3536 /** The number of ignored pages in the RAM ranges (i.e. MMIO, MMIO2 and ROM). */
3537 uint32_t cIgnoredPages;
3538 /** Indicates that a live save operation is active. */
3539 bool fActive;
3540 /** Padding. */
3541 bool afReserved[2];
3542 /** The next history index. */
3543 uint8_t iDirtyPagesHistory;
3544 /** History of the total amount of dirty pages. */
3545 uint32_t acDirtyPagesHistory[64];
3546 /** Short term dirty page average. */
3547 uint32_t cDirtyPagesShort;
3548 /** Long term dirty page average. */
3549 uint32_t cDirtyPagesLong;
3550 /** The number of saved pages. This is used to get some kind of estimate of the
3551 * link speed so we can decide when we're done. It is reset after the first
3552 * 7 passes so the speed estimate doesn't get inflated by the initial set of
3553 * zero pages. */
3554 uint64_t cSavedPages;
3555 /** The nanosecond timestamp when cSavedPages was 0. */
3556 uint64_t uSaveStartNS;
3557 /** Pages per second (for statistics). */
3558 uint32_t cPagesPerSecond;
3559 uint32_t cAlignment;
3560 } LiveSave;
3561
3562 /** @name Error injection.
3563 * @{ */
3564 /** Inject handy page allocation errors pretending we're completely out of
3565 * memory. */
3566 bool volatile fErrInjHandyPages;
3567 /** Padding. */
3568 bool afReserved[3];
3569 /** @} */
3570
3571 /** @name Release Statistics
3572 * @{ */
3573 uint32_t cAllPages; /**< The total number of pages. (Should be Private + Shared + Zero + Pure MMIO.) */
3574 uint32_t cPrivatePages; /**< The number of private pages. */
3575 uint32_t cSharedPages; /**< The number of shared pages. */
3576 uint32_t cReusedSharedPages; /**< The number of reused shared pages. */
3577 uint32_t cZeroPages; /**< The number of zero backed pages. */
3578 uint32_t cPureMmioPages; /**< The number of pure MMIO pages. */
3579 uint32_t cMonitoredPages; /**< The number of write monitored pages. */
3580 uint32_t cWrittenToPages; /**< The number of previously write monitored pages. */
3581 uint32_t cWriteLockedPages; /**< The number of write locked pages. */
3582 uint32_t cReadLockedPages; /**< The number of read locked pages. */
3583 uint32_t cBalloonedPages; /**< The number of ballooned pages. */
3584 uint32_t cMappedChunks; /**< Number of times we mapped a chunk. */
3585 uint32_t cUnmappedChunks; /**< Number of times we unmapped a chunk. */
3586 uint32_t cLargePages; /**< The number of large pages. */
3587 uint32_t cLargePagesDisabled; /**< The number of disabled large pages. */
3588/* uint32_t aAlignment4[1]; */
3589
3590 /** The number of times we were forced to change the hypervisor region location. */
3591 STAMCOUNTER cRelocations;
3592
3593 STAMCOUNTER StatLargePageReused; /**< The number of large pages we've reused.*/
3594 STAMCOUNTER StatLargePageRefused; /**< The number of times we couldn't use a large page.*/
3595 STAMCOUNTER StatLargePageRecheck; /**< The number of times we rechecked a disabled large page.*/
3596
3597 STAMPROFILE StatShModCheck; /**< Profiles shared module checks. */
3598 /** @} */
3599
3600#ifdef VBOX_WITH_STATISTICS
3601 /** @name Statistics on the heap.
3602 * @{ */
3603 R3PTRTYPE(PGMSTATS *) pStatsR3;
3604 R0PTRTYPE(PGMSTATS *) pStatsR0;
3605 RCPTRTYPE(PGMSTATS *) pStatsRC;
3606 RTRCPTR RCPtrAlignment;
3607 /** @} */
3608#endif
3609} PGM;
3610#ifndef IN_TSTVMSTRUCTGC /* HACK */
3611AssertCompileMemberAlignment(PGM, paDynPageMap32BitPTEsGC, 8);
3612AssertCompileMemberAlignment(PGM, GCPtrMappingFixed, sizeof(RTGCPTR));
3613AssertCompileMemberAlignment(PGM, HCPhysInterPD, 8);
3614AssertCompileMemberAlignment(PGM, CritSectX, 8);
3615AssertCompileMemberAlignment(PGM, ChunkR3Map, 8);
3616AssertCompileMemberAlignment(PGM, PhysTlbHC, 8);
3617AssertCompileMemberAlignment(PGM, HCPhysZeroPg, 8);
3618AssertCompileMemberAlignment(PGM, aHandyPages, 8);
3619AssertCompileMemberAlignment(PGM, cRelocations, 8);
3620#endif /* !IN_TSTVMSTRUCTGC */
3621/** Pointer to the PGM instance data. */
3622typedef PGM *PPGM;
3623
3624
3625
3626typedef struct PGMCPUSTATS
3627{
3628 /* Common */
3629 STAMCOUNTER StatSyncPtPD[X86_PG_ENTRIES]; /**< SyncPT - PD distribution. */
3630 STAMCOUNTER StatSyncPagePD[X86_PG_ENTRIES]; /**< SyncPage - PD distribution. */
3631
3632 /* R0 only: */
3633 STAMPROFILE StatR0NpMiscfg; /**< R0: PGMR0Trap0eHandlerNPMisconfig() profiling. */
3634 STAMCOUNTER StatR0NpMiscfgSyncPage; /**< R0: SyncPage calls from PGMR0Trap0eHandlerNPMisconfig(). */
3635
3636 /* RZ only: */
3637 STAMPROFILE StatRZTrap0e; /**< RC/R0: PGMTrap0eHandler() profiling. */
3638 STAMPROFILE StatRZTrap0eTime2Ballooned; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is read access to a ballooned page. */
3639 STAMPROFILE StatRZTrap0eTime2CSAM; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is CSAM. */
3640 STAMPROFILE StatRZTrap0eTime2DirtyAndAccessed; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is dirty and/or accessed bit emulation. */
3641 STAMPROFILE StatRZTrap0eTime2GuestTrap; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a guest trap. */
3642 STAMPROFILE StatRZTrap0eTime2HndPhys; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a physical handler. */
3643 STAMPROFILE StatRZTrap0eTime2HndVirt; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a virtual handler. */
3644 STAMPROFILE StatRZTrap0eTime2HndUnhandled; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is access outside the monitored areas of a monitored page. */
3645 STAMPROFILE StatRZTrap0eTime2InvalidPhys; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is access to an invalid physical guest address. */
3646 STAMPROFILE StatRZTrap0eTime2MakeWritable; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is a page that needed to be made writable. */
3647 STAMPROFILE StatRZTrap0eTime2Mapping; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is the guest mappings. */
3648 STAMPROFILE StatRZTrap0eTime2Misc; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is not known. */
3649 STAMPROFILE StatRZTrap0eTime2OutOfSync; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync page. */
3650 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndPhys; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync physical handler page. */
3651 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndVirt; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an out-of-sync virtual handler page. */
3652 STAMPROFILE StatRZTrap0eTime2OutOfSyncHndObs; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is an obsolete handler page. */
3653 STAMPROFILE StatRZTrap0eTime2SyncPT; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is lazy syncing of a PT. */
3654 STAMPROFILE StatRZTrap0eTime2WPEmulation; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is CR0.WP emulation. */
3655 STAMPROFILE StatRZTrap0eTime2Wp0RoUsHack; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is CR0.WP and netware hack to be enabled. */
3656 STAMPROFILE StatRZTrap0eTime2Wp0RoUsUnhack; /**< RC/R0: Profiling of the Trap0eHandler body when the cause is CR0.WP and netware hack to be disabled. */
3657 STAMCOUNTER StatRZTrap0eConflicts; /**< RC/R0: The number of times \#PF was caused by an undetected conflict. */
3658 STAMCOUNTER StatRZTrap0eHandlersMapping; /**< RC/R0: Number of traps due to access handlers in mappings. */
3659 STAMCOUNTER StatRZTrap0eHandlersOutOfSync; /**< RC/R0: Number of out-of-sync handled pages. */
3660 STAMCOUNTER StatRZTrap0eHandlersPhysAll; /**< RC/R0: Number of traps due to physical all-access handlers. */
3661 STAMCOUNTER StatRZTrap0eHandlersPhysAllOpt; /**< RC/R0: Number of the physical all-access handler traps using the optimization. */
3662 STAMCOUNTER StatRZTrap0eHandlersPhysWrite; /**< RC/R0: Number of traps due to write-physical access handlers. */
3663 STAMCOUNTER StatRZTrap0eHandlersVirtual; /**< RC/R0: Number of traps due to virtual access handlers. */
3664 STAMCOUNTER StatRZTrap0eHandlersVirtualByPhys; /**< RC/R0: Number of traps due to virtual access handlers found by physical address. */
3665 STAMCOUNTER StatRZTrap0eHandlersVirtualUnmarked;/**< RC/R0: Number of traps due to virtual access handlers found by virtual address (without proper physical flags). */
3666 STAMCOUNTER StatRZTrap0eHandlersUnhandled; /**< RC/R0: Number of traps due to access outside range of monitored page(s). */
3667 STAMCOUNTER StatRZTrap0eHandlersInvalid; /**< RC/R0: Number of traps due to access to invalid physical memory. */
3668 STAMCOUNTER StatRZTrap0eUSNotPresentRead; /**< RC/R0: \#PF err kind */
3669 STAMCOUNTER StatRZTrap0eUSNotPresentWrite; /**< RC/R0: \#PF err kind */
3670 STAMCOUNTER StatRZTrap0eUSWrite; /**< RC/R0: \#PF err kind */
3671 STAMCOUNTER StatRZTrap0eUSReserved; /**< RC/R0: \#PF err kind */
3672 STAMCOUNTER StatRZTrap0eUSNXE; /**< RC/R0: \#PF err kind */
3673 STAMCOUNTER StatRZTrap0eUSRead; /**< RC/R0: \#PF err kind */
3674 STAMCOUNTER StatRZTrap0eSVNotPresentRead; /**< RC/R0: \#PF err kind */
3675 STAMCOUNTER StatRZTrap0eSVNotPresentWrite; /**< RC/R0: \#PF err kind */
3676 STAMCOUNTER StatRZTrap0eSVWrite; /**< RC/R0: \#PF err kind */
3677 STAMCOUNTER StatRZTrap0eSVReserved; /**< RC/R0: \#PF err kind */
3678 STAMCOUNTER StatRZTrap0eSNXE; /**< RC/R0: \#PF err kind */
3679 STAMCOUNTER StatRZTrap0eGuestPF; /**< RC/R0: Real guest \#PFs. */
3680 STAMCOUNTER StatRZTrap0eGuestPFMapping; /**< RC/R0: Real guest \#PF to HMA or other mapping. */
3681 STAMCOUNTER StatRZTrap0eWPEmulInRZ; /**< RC/R0: WP=0 virtualization trap, handled. */
3682 STAMCOUNTER StatRZTrap0eWPEmulToR3; /**< RC/R0: WP=0 virtualization trap, chickened out. */
3683 STAMCOUNTER StatRZTrap0ePD[X86_PG_ENTRIES]; /**< RC/R0: PD distribution of the \#PFs. */
3684 STAMCOUNTER StatRZGuestCR3WriteHandled; /**< RC/R0: The number of times WriteHandlerCR3() was successfully called. */
3685 STAMCOUNTER StatRZGuestCR3WriteUnhandled; /**< RC/R0: The number of times WriteHandlerCR3() was called and we had to fall back to the recompiler. */
3686 STAMCOUNTER StatRZGuestCR3WriteConflict; /**< RC/R0: The number of times WriteHandlerCR3() was called and a conflict was detected. */
3687 STAMCOUNTER StatRZGuestROMWriteHandled; /**< RC/R0: The number of times pgmPhysRomWriteHandler() was successfully called. */
3688 STAMCOUNTER StatRZGuestROMWriteUnhandled; /**< RC/R0: The number of times pgmPhysRomWriteHandler() was called and we had to fall back to the recompiler */
3689 STAMCOUNTER StatRZDynMapMigrateInvlPg; /**< RZ: invlpg in PGMR0DynMapMigrateAutoSet. */
3690 STAMPROFILE StatRZDynMapGCPageInl; /**< RZ: Calls to pgmRZDynMapGCPageInlined. */
3691 STAMCOUNTER StatRZDynMapGCPageInlHits; /**< RZ: Hash table lookup hits. */
3692 STAMCOUNTER StatRZDynMapGCPageInlMisses; /**< RZ: Misses that falls back to the code common. */
3693 STAMCOUNTER StatRZDynMapGCPageInlRamHits; /**< RZ: 1st ram range hits. */
3694 STAMCOUNTER StatRZDynMapGCPageInlRamMisses; /**< RZ: 1st ram range misses, takes slow path. */
3695 STAMPROFILE StatRZDynMapHCPageInl; /**< RZ: Calls to pgmRZDynMapHCPageInlined. */
3696 STAMCOUNTER StatRZDynMapHCPageInlHits; /**< RZ: Hash table lookup hits. */
3697 STAMCOUNTER StatRZDynMapHCPageInlMisses; /**< RZ: Misses that falls back to the code common. */
3698 STAMPROFILE StatRZDynMapHCPage; /**< RZ: Calls to pgmRZDynMapHCPageCommon. */
3699 STAMCOUNTER StatRZDynMapSetOptimize; /**< RZ: Calls to pgmRZDynMapOptimizeAutoSet. */
3700 STAMCOUNTER StatRZDynMapSetSearchFlushes; /**< RZ: Set search restoring to subset flushes. */
3701 STAMCOUNTER StatRZDynMapSetSearchHits; /**< RZ: Set search hits. */
3702 STAMCOUNTER StatRZDynMapSetSearchMisses; /**< RZ: Set search misses. */
3703 STAMCOUNTER StatRZDynMapPage; /**< RZ: Calls to pgmR0DynMapPage. */
3704 STAMCOUNTER StatRZDynMapPageHits0; /**< RZ: Hits at iPage+0. */
3705 STAMCOUNTER StatRZDynMapPageHits1; /**< RZ: Hits at iPage+1. */
3706 STAMCOUNTER StatRZDynMapPageHits2; /**< RZ: Hits at iPage+2. */
3707 STAMCOUNTER StatRZDynMapPageInvlPg; /**< RZ: invlpg. */
3708 STAMCOUNTER StatRZDynMapPageSlow; /**< RZ: Calls to pgmR0DynMapPageSlow. */
3709 STAMCOUNTER StatRZDynMapPageSlowLoopHits; /**< RZ: Hits in the pgmR0DynMapPageSlow search loop. */
3710 STAMCOUNTER StatRZDynMapPageSlowLoopMisses; /**< RZ: Misses in the pgmR0DynMapPageSlow search loop. */
3711 //STAMCOUNTER StatRZDynMapPageSlowLostHits; /**< RZ: Lost hits. */
3712 STAMCOUNTER StatRZDynMapSubsets; /**< RZ: Times PGMDynMapPushAutoSubset was called. */
3713 STAMCOUNTER StatRZDynMapPopFlushes; /**< RZ: Times PGMDynMapPopAutoSubset flushes the subset. */
3714 STAMCOUNTER aStatRZDynMapSetFilledPct[11]; /**< RZ: Set fill distribution, percent. */
3715
3716 /* HC - R3 and (maybe) R0: */
3717
3718 /* RZ & R3: */
3719 STAMPROFILE StatRZSyncCR3; /**< RC/R0: PGMSyncCR3() profiling. */
3720 STAMPROFILE StatRZSyncCR3Handlers; /**< RC/R0: Profiling of the PGMSyncCR3() update handler section. */
3721 STAMCOUNTER StatRZSyncCR3Global; /**< RC/R0: The number of global CR3 syncs. */
3722 STAMCOUNTER StatRZSyncCR3NotGlobal; /**< RC/R0: The number of non-global CR3 syncs. */
3723 STAMCOUNTER StatRZSyncCR3DstCacheHit; /**< RC/R0: The number of times we got some kind of cache hit on a page table. */
3724 STAMCOUNTER StatRZSyncCR3DstFreed; /**< RC/R0: The number of times we've had to free a shadow entry. */
3725 STAMCOUNTER StatRZSyncCR3DstFreedSrcNP; /**< RC/R0: The number of times we've had to free a shadow entry for which the source entry was not present. */
3726 STAMCOUNTER StatRZSyncCR3DstNotPresent; /**< RC/R0: The number of times we've encountered a not present shadow entry for a present guest entry. */
3727 STAMCOUNTER StatRZSyncCR3DstSkippedGlobalPD; /**< RC/R0: The number of times a global page directory wasn't flushed. */
3728 STAMCOUNTER StatRZSyncCR3DstSkippedGlobalPT; /**< RC/R0: The number of times a page table with only global entries wasn't flushed. */
3729 STAMPROFILE StatRZSyncPT; /**< RC/R0: PGMSyncPT() profiling. */
3730 STAMCOUNTER StatRZSyncPTFailed; /**< RC/R0: The number of times PGMSyncPT() failed. */
3731 STAMCOUNTER StatRZSyncPT4K; /**< RC/R0: Number of 4KB syncs. */
3732 STAMCOUNTER StatRZSyncPT4M; /**< RC/R0: Number of 4MB syncs. */
3733 STAMCOUNTER StatRZSyncPagePDNAs; /**< RC/R0: The number of time we've marked a PD not present from SyncPage to virtualize the accessed bit. */
3734 STAMCOUNTER StatRZSyncPagePDOutOfSync; /**< RC/R0: The number of time we've encountered an out-of-sync PD in SyncPage. */
3735 STAMCOUNTER StatRZAccessedPage; /**< RC/R0: The number of pages marked not present for accessed bit emulation. */
3736 STAMPROFILE StatRZDirtyBitTracking; /**< RC/R0: Profiling the dirty bit tracking in CheckPageFault(). */
3737 STAMCOUNTER StatRZDirtyPage; /**< RC/R0: The number of pages marked read-only for dirty bit tracking. */
3738 STAMCOUNTER StatRZDirtyPageBig; /**< RC/R0: The number of pages marked read-only for dirty bit tracking. */
3739 STAMCOUNTER StatRZDirtyPageSkipped; /**< RC/R0: The number of pages already dirty or readonly. */
3740 STAMCOUNTER StatRZDirtyPageTrap; /**< RC/R0: The number of traps generated for dirty bit tracking. */
3741 STAMCOUNTER StatRZDirtyPageStale; /**< RC/R0: The number of traps generated for dirty bit tracking. (stale tlb entries) */
3742 STAMCOUNTER StatRZDirtyTrackRealPF; /**< RC/R0: The number of real pages faults during dirty bit tracking. */
3743 STAMCOUNTER StatRZDirtiedPage; /**< RC/R0: The number of pages marked dirty because of write accesses. */
3744 STAMCOUNTER StatRZPageAlreadyDirty; /**< RC/R0: The number of pages already marked dirty because of write accesses. */
3745 STAMPROFILE StatRZInvalidatePage; /**< RC/R0: PGMInvalidatePage() profiling. */
3746 STAMCOUNTER StatRZInvalidatePage4KBPages; /**< RC/R0: The number of times PGMInvalidatePage() was called for a 4KB page. */
3747 STAMCOUNTER StatRZInvalidatePage4MBPages; /**< RC/R0: The number of times PGMInvalidatePage() was called for a 4MB page. */
3748 STAMCOUNTER StatRZInvalidatePage4MBPagesSkip; /**< RC/R0: The number of times PGMInvalidatePage() skipped a 4MB page. */
3749 STAMCOUNTER StatRZInvalidatePagePDMappings; /**< RC/R0: The number of times PGMInvalidatePage() was called for a page directory containing mappings (no conflict). */
3750 STAMCOUNTER StatRZInvalidatePagePDNAs; /**< RC/R0: The number of times PGMInvalidatePage() was called for a not accessed page directory. */
3751 STAMCOUNTER StatRZInvalidatePagePDNPs; /**< RC/R0: The number of times PGMInvalidatePage() was called for a not present page directory. */
3752 STAMCOUNTER StatRZInvalidatePagePDOutOfSync; /**< RC/R0: The number of times PGMInvalidatePage() was called for an out of sync page directory. */
3753 STAMCOUNTER StatRZInvalidatePageSkipped; /**< RC/R0: The number of times PGMInvalidatePage() was skipped due to not present shw or pending pending SyncCR3. */
3754 STAMCOUNTER StatRZPageOutOfSyncUser; /**< RC/R0: The number of times user page is out of sync was detected in \#PF or VerifyAccessSyncPage. */
3755 STAMCOUNTER StatRZPageOutOfSyncSupervisor; /**< RC/R0: The number of times supervisor page is out of sync was detected in in \#PF or VerifyAccessSyncPage. */
3756 STAMCOUNTER StatRZPageOutOfSyncUserWrite; /**< RC/R0: The number of times user page is out of sync was detected in \#PF. */
3757 STAMCOUNTER StatRZPageOutOfSyncSupervisorWrite; /**< RC/R0: The number of times supervisor page is out of sync was detected in in \#PF. */
3758 STAMCOUNTER StatRZPageOutOfSyncBallloon; /**< RC/R0: The number of times a ballooned page was accessed (read). */
3759 STAMPROFILE StatRZPrefetch; /**< RC/R0: PGMPrefetchPage. */
3760 STAMPROFILE StatRZFlushTLB; /**< RC/R0: Profiling of the PGMFlushTLB() body. */
3761 STAMCOUNTER StatRZFlushTLBNewCR3; /**< RC/R0: The number of times PGMFlushTLB was called with a new CR3, non-global. (switch) */
3762 STAMCOUNTER StatRZFlushTLBNewCR3Global; /**< RC/R0: The number of times PGMFlushTLB was called with a new CR3, global. (switch) */
3763 STAMCOUNTER StatRZFlushTLBSameCR3; /**< RC/R0: The number of times PGMFlushTLB was called with the same CR3, non-global. (flush) */
3764 STAMCOUNTER StatRZFlushTLBSameCR3Global; /**< RC/R0: The number of times PGMFlushTLB was called with the same CR3, global. (flush) */
3765 STAMPROFILE StatRZGstModifyPage; /**< RC/R0: Profiling of the PGMGstModifyPage() body */
3766
3767 STAMPROFILE StatR3SyncCR3; /**< R3: PGMSyncCR3() profiling. */
3768 STAMPROFILE StatR3SyncCR3Handlers; /**< R3: Profiling of the PGMSyncCR3() update handler section. */
3769 STAMCOUNTER StatR3SyncCR3Global; /**< R3: The number of global CR3 syncs. */
3770 STAMCOUNTER StatR3SyncCR3NotGlobal; /**< R3: The number of non-global CR3 syncs. */
3771 STAMCOUNTER StatR3SyncCR3DstFreed; /**< R3: The number of times we've had to free a shadow entry. */
3772 STAMCOUNTER StatR3SyncCR3DstFreedSrcNP; /**< R3: The number of times we've had to free a shadow entry for which the source entry was not present. */
3773 STAMCOUNTER StatR3SyncCR3DstNotPresent; /**< R3: The number of times we've encountered a not present shadow entry for a present guest entry. */
3774 STAMCOUNTER StatR3SyncCR3DstSkippedGlobalPD; /**< R3: The number of times a global page directory wasn't flushed. */
3775 STAMCOUNTER StatR3SyncCR3DstSkippedGlobalPT; /**< R3: The number of times a page table with only global entries wasn't flushed. */
3776 STAMCOUNTER StatR3SyncCR3DstCacheHit; /**< R3: The number of times we got some kind of cache hit on a page table. */
3777 STAMPROFILE StatR3SyncPT; /**< R3: PGMSyncPT() profiling. */
3778 STAMCOUNTER StatR3SyncPTFailed; /**< R3: The number of times PGMSyncPT() failed. */
3779 STAMCOUNTER StatR3SyncPT4K; /**< R3: Number of 4KB syncs. */
3780 STAMCOUNTER StatR3SyncPT4M; /**< R3: Number of 4MB syncs. */
3781 STAMCOUNTER StatR3SyncPagePDNAs; /**< R3: The number of time we've marked a PD not present from SyncPage to virtualize the accessed bit. */
3782 STAMCOUNTER StatR3SyncPagePDOutOfSync; /**< R3: The number of time we've encountered an out-of-sync PD in SyncPage. */
3783 STAMCOUNTER StatR3AccessedPage; /**< R3: The number of pages marked not present for accessed bit emulation. */
3784 STAMPROFILE StatR3DirtyBitTracking; /**< R3: Profiling the dirty bit tracking in CheckPageFault(). */
3785 STAMCOUNTER StatR3DirtyPage; /**< R3: The number of pages marked read-only for dirty bit tracking. */
3786 STAMCOUNTER StatR3DirtyPageBig; /**< R3: The number of pages marked read-only for dirty bit tracking. */
3787 STAMCOUNTER StatR3DirtyPageSkipped; /**< R3: The number of pages already dirty or readonly. */
3788 STAMCOUNTER StatR3DirtyPageTrap; /**< R3: The number of traps generated for dirty bit tracking. */
3789 STAMCOUNTER StatR3DirtyTrackRealPF; /**< R3: The number of real pages faults during dirty bit tracking. */
3790 STAMCOUNTER StatR3DirtiedPage; /**< R3: The number of pages marked dirty because of write accesses. */
3791 STAMCOUNTER StatR3PageAlreadyDirty; /**< R3: The number of pages already marked dirty because of write accesses. */
3792 STAMPROFILE StatR3InvalidatePage; /**< R3: PGMInvalidatePage() profiling. */
3793 STAMCOUNTER StatR3InvalidatePage4KBPages; /**< R3: The number of times PGMInvalidatePage() was called for a 4KB page. */
3794 STAMCOUNTER StatR3InvalidatePage4MBPages; /**< R3: The number of times PGMInvalidatePage() was called for a 4MB page. */
3795 STAMCOUNTER StatR3InvalidatePage4MBPagesSkip; /**< R3: The number of times PGMInvalidatePage() skipped a 4MB page. */
3796 STAMCOUNTER StatR3InvalidatePagePDNAs; /**< R3: The number of times PGMInvalidatePage() was called for a not accessed page directory. */
3797 STAMCOUNTER StatR3InvalidatePagePDNPs; /**< R3: The number of times PGMInvalidatePage() was called for a not present page directory. */
3798 STAMCOUNTER StatR3InvalidatePagePDMappings; /**< R3: The number of times PGMInvalidatePage() was called for a page directory containing mappings (no conflict). */
3799 STAMCOUNTER StatR3InvalidatePagePDOutOfSync; /**< R3: The number of times PGMInvalidatePage() was called for an out of sync page directory. */
3800 STAMCOUNTER StatR3InvalidatePageSkipped; /**< R3: The number of times PGMInvalidatePage() was skipped due to not present shw or pending pending SyncCR3. */
3801 STAMCOUNTER StatR3PageOutOfSyncUser; /**< R3: The number of times user page is out of sync was detected in \#PF or VerifyAccessSyncPage. */
3802 STAMCOUNTER StatR3PageOutOfSyncSupervisor; /**< R3: The number of times supervisor page is out of sync was detected in in \#PF or VerifyAccessSyncPage. */
3803 STAMCOUNTER StatR3PageOutOfSyncUserWrite; /**< R3: The number of times user page is out of sync was detected in \#PF. */
3804 STAMCOUNTER StatR3PageOutOfSyncSupervisorWrite; /**< R3: The number of times supervisor page is out of sync was detected in in \#PF. */
3805 STAMCOUNTER StatR3PageOutOfSyncBallloon; /**< R3: The number of times a ballooned page was accessed (read). */
3806 STAMPROFILE StatR3Prefetch; /**< R3: PGMPrefetchPage. */
3807 STAMPROFILE StatR3FlushTLB; /**< R3: Profiling of the PGMFlushTLB() body. */
3808 STAMCOUNTER StatR3FlushTLBNewCR3; /**< R3: The number of times PGMFlushTLB was called with a new CR3, non-global. (switch) */
3809 STAMCOUNTER StatR3FlushTLBNewCR3Global; /**< R3: The number of times PGMFlushTLB was called with a new CR3, global. (switch) */
3810 STAMCOUNTER StatR3FlushTLBSameCR3; /**< R3: The number of times PGMFlushTLB was called with the same CR3, non-global. (flush) */
3811 STAMCOUNTER StatR3FlushTLBSameCR3Global; /**< R3: The number of times PGMFlushTLB was called with the same CR3, global. (flush) */
3812 STAMPROFILE StatR3GstModifyPage; /**< R3: Profiling of the PGMGstModifyPage() body */
3813 /** @} */
3814} PGMCPUSTATS;
3815
3816
3817/**
3818 * Converts a PGMCPU pointer into a VM pointer.
3819 * @returns Pointer to the VM structure the PGM is part of.
3820 * @param pPGM Pointer to PGMCPU instance data.
3821 */
3822#define PGMCPU2VM(pPGM) ( (PVM)((char*)(pPGM) - (pPGM)->offVM) )
3823
3824/**
3825 * Converts a PGMCPU pointer into a PGM pointer.
3826 * @returns Pointer to the VM structure the PGM is part of.
3827 * @param pPGMCpu Pointer to PGMCPU instance data.
3828 */
3829#define PGMCPU2PGM(pPGMCpu) ( (PPGM)((char *)(pPGMCpu) - (pPGMCpu)->offPGM) )
3830
3831/**
3832 * PGMCPU Data (part of VMCPU).
3833 */
3834typedef struct PGMCPU
3835{
3836 /** Offset to the VM structure. */
3837 int32_t offVM;
3838 /** Offset to the VMCPU structure. */
3839 int32_t offVCpu;
3840 /** Offset of the PGM structure relative to VMCPU. */
3841 int32_t offPGM;
3842 uint32_t uPadding0; /**< structure size alignment. */
3843
3844#if defined(VBOX_WITH_2X_4GB_ADDR_SPACE) || defined(VBOX_WITH_RAW_MODE)
3845 /** Automatically tracked physical memory mapping set.
3846 * Ring-0 and strict raw-mode builds. */
3847 PGMMAPSET AutoSet;
3848#endif
3849
3850 /** A20 gate mask.
3851 * Our current approach to A20 emulation is to let REM do it and don't bother
3852 * anywhere else. The interesting Guests will be operating with it enabled anyway.
3853 * But whould need arrise, we'll subject physical addresses to this mask. */
3854 RTGCPHYS GCPhysA20Mask;
3855 /** A20 gate state - boolean! */
3856 bool fA20Enabled;
3857 /** Mirror of the EFER.NXE bit. Managed by PGMNotifyNxeChanged. */
3858 bool fNoExecuteEnabled;
3859 /** Unused bits. */
3860 bool afUnused[2];
3861
3862 /** What needs syncing (PGM_SYNC_*).
3863 * This is used to queue operations for PGMSyncCR3, PGMInvalidatePage,
3864 * PGMFlushTLB, and PGMR3Load. */
3865 uint32_t fSyncFlags;
3866
3867 /** The shadow paging mode. */
3868 PGMMODE enmShadowMode;
3869 /** The guest paging mode. */
3870 PGMMODE enmGuestMode;
3871
3872 /** The current physical address representing in the guest CR3 register. */
3873 RTGCPHYS GCPhysCR3;
3874
3875 /** @name 32-bit Guest Paging.
3876 * @{ */
3877 /** The guest's page directory, R3 pointer. */
3878 R3PTRTYPE(PX86PD) pGst32BitPdR3;
3879#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
3880 /** The guest's page directory, R0 pointer. */
3881 R0PTRTYPE(PX86PD) pGst32BitPdR0;
3882#endif
3883 /** The guest's page directory, static RC mapping. */
3884 RCPTRTYPE(PX86PD) pGst32BitPdRC;
3885 /** Mask containing the MBZ bits of a big page PDE. */
3886 uint32_t fGst32BitMbzBigPdeMask;
3887 /** Set if the page size extension (PSE) is enabled. */
3888 bool fGst32BitPageSizeExtension;
3889 /** Alignment padding. */
3890 bool afAlignment2[3];
3891 /** @} */
3892
3893 /** @name PAE Guest Paging.
3894 * @{ */
3895 /** The guest's page directory pointer table, static RC mapping. */
3896 RCPTRTYPE(PX86PDPT) pGstPaePdptRC;
3897 /** The guest's page directory pointer table, R3 pointer. */
3898 R3PTRTYPE(PX86PDPT) pGstPaePdptR3;
3899#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
3900 /** The guest's page directory pointer table, R0 pointer. */
3901 R0PTRTYPE(PX86PDPT) pGstPaePdptR0;
3902#endif
3903
3904 /** The guest's page directories, R3 pointers.
3905 * These are individual pointers and don't have to be adjacent.
3906 * These don't have to be up-to-date - use pgmGstGetPaePD() to access them. */
3907 R3PTRTYPE(PX86PDPAE) apGstPaePDsR3[4];
3908 /** The guest's page directories, R0 pointers.
3909 * Same restrictions as apGstPaePDsR3. */
3910#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
3911 R0PTRTYPE(PX86PDPAE) apGstPaePDsR0[4];
3912#endif
3913 /** The guest's page directories, static GC mapping.
3914 * Unlike the R3/R0 array the first entry can be accessed as a 2048 entry PD.
3915 * These don't have to be up-to-date - use pgmGstGetPaePD() to access them. */
3916 RCPTRTYPE(PX86PDPAE) apGstPaePDsRC[4];
3917 /** The physical addresses of the guest page directories (PAE) pointed to by apGstPagePDsHC/GC.
3918 * @todo Remove this and use aGstPaePdpeRegs instead? */
3919 RTGCPHYS aGCPhysGstPaePDs[4];
3920 /** The values of the 4 PDPE CPU registers (PAE). */
3921 X86PDPE aGstPaePdpeRegs[4];
3922 /** The physical addresses of the monitored guest page directories (PAE). */
3923 RTGCPHYS aGCPhysGstPaePDsMonitored[4];
3924 /** Mask containing the MBZ PTE bits. */
3925 uint64_t fGstPaeMbzPteMask;
3926 /** Mask containing the MBZ PDE bits. */
3927 uint64_t fGstPaeMbzPdeMask;
3928 /** Mask containing the MBZ big page PDE bits. */
3929 uint64_t fGstPaeMbzBigPdeMask;
3930 /** Mask containing the MBZ PDPE bits. */
3931 uint64_t fGstPaeMbzPdpeMask;
3932 /** @} */
3933
3934 /** @name AMD64 Guest Paging.
3935 * @{ */
3936 /** The guest's page directory pointer table, R3 pointer. */
3937 R3PTRTYPE(PX86PML4) pGstAmd64Pml4R3;
3938#ifndef VBOX_WITH_2X_4GB_ADDR_SPACE
3939 /** The guest's page directory pointer table, R0 pointer. */
3940 R0PTRTYPE(PX86PML4) pGstAmd64Pml4R0;
3941#else
3942 RTR0PTR alignment6b; /**< alignment equalizer. */
3943#endif
3944 /** Mask containing the MBZ PTE bits. */
3945 uint64_t fGstAmd64MbzPteMask;
3946 /** Mask containing the MBZ PDE bits. */
3947 uint64_t fGstAmd64MbzPdeMask;
3948 /** Mask containing the MBZ big page PDE bits. */
3949 uint64_t fGstAmd64MbzBigPdeMask;
3950 /** Mask containing the MBZ PDPE bits. */
3951 uint64_t fGstAmd64MbzPdpeMask;
3952 /** Mask containing the MBZ big page PDPE bits. */
3953 uint64_t fGstAmd64MbzBigPdpeMask;
3954 /** Mask containing the MBZ PML4E bits. */
3955 uint64_t fGstAmd64MbzPml4eMask;
3956 /** Mask containing the PDPE bits that we shadow. */
3957 uint64_t fGstAmd64ShadowedPdpeMask;
3958 /** Mask containing the PML4E bits that we shadow. */
3959 uint64_t fGstAmd64ShadowedPml4eMask;
3960 /** @} */
3961
3962 /** @name PAE and AMD64 Guest Paging.
3963 * @{ */
3964 /** Mask containing the PTE bits that we shadow. */
3965 uint64_t fGst64ShadowedPteMask;
3966 /** Mask containing the PDE bits that we shadow. */
3967 uint64_t fGst64ShadowedPdeMask;
3968 /** Mask containing the big page PDE bits that we shadow in the PDE. */
3969 uint64_t fGst64ShadowedBigPdeMask;
3970 /** Mask containing the big page PDE bits that we shadow in the PTE. */
3971 uint64_t fGst64ShadowedBigPde4PteMask;
3972 /** @} */
3973
3974 /** Pointer to the page of the current active CR3 - R3 Ptr. */
3975 R3PTRTYPE(PPGMPOOLPAGE) pShwPageCR3R3;
3976 /** Pointer to the page of the current active CR3 - R0 Ptr. */
3977 R0PTRTYPE(PPGMPOOLPAGE) pShwPageCR3R0;
3978 /** Pointer to the page of the current active CR3 - RC Ptr. */
3979 RCPTRTYPE(PPGMPOOLPAGE) pShwPageCR3RC;
3980# if HC_ARCH_BITS == 64
3981 RTRCPTR alignment6; /**< structure size alignment. */
3982# endif
3983 /** @} */
3984
3985 /** @name Function pointers for Shadow paging.
3986 * @{
3987 */
3988 DECLR3CALLBACKMEMBER(int, pfnR3ShwRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
3989 DECLR3CALLBACKMEMBER(int, pfnR3ShwExit,(PVMCPU pVCpu));
3990 DECLR3CALLBACKMEMBER(int, pfnR3ShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
3991 DECLR3CALLBACKMEMBER(int, pfnR3ShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask, uint32_t fOpFlags));
3992
3993 DECLRCCALLBACKMEMBER(int, pfnRCShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
3994 DECLRCCALLBACKMEMBER(int, pfnRCShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask, uint32_t fOpFlags));
3995
3996 DECLR0CALLBACKMEMBER(int, pfnR0ShwGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
3997 DECLR0CALLBACKMEMBER(int, pfnR0ShwModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask, uint32_t fOpFlags));
3998
3999 /** @} */
4000
4001 /** @name Function pointers for Guest paging.
4002 * @{
4003 */
4004 DECLR3CALLBACKMEMBER(int, pfnR3GstRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
4005 DECLR3CALLBACKMEMBER(int, pfnR3GstExit,(PVMCPU pVCpu));
4006 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
4007 DECLR3CALLBACKMEMBER(int, pfnR3GstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
4008 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
4009 DECLRCCALLBACKMEMBER(int, pfnRCGstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
4010 DECLRCCALLBACKMEMBER(int, pfnRCGstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
4011 DECLRCCALLBACKMEMBER(int, pfnRCGstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
4012#if HC_ARCH_BITS == 64
4013 RTRCPTR alignment3; /**< structure size alignment. */
4014#endif
4015
4016 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPage,(PVMCPU pVCpu, RTGCPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
4017 DECLR0CALLBACKMEMBER(int, pfnR0GstModifyPage,(PVMCPU pVCpu, RTGCPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
4018 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPDE,(PVMCPU pVCpu, RTGCPTR GCPtr, PX86PDEPAE pPde));
4019 /** @} */
4020
4021 /** @name Function pointers for Both Shadow and Guest paging.
4022 * @{
4023 */
4024 DECLR3CALLBACKMEMBER(int, pfnR3BthRelocate,(PVMCPU pVCpu, RTGCPTR offDelta));
4025 /* no pfnR3BthTrap0eHandler */
4026 DECLR3CALLBACKMEMBER(int, pfnR3BthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
4027 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
4028 DECLR3CALLBACKMEMBER(int, pfnR3BthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
4029 DECLR3CALLBACKMEMBER(int, pfnR3BthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
4030 DECLR3CALLBACKMEMBER(unsigned, pfnR3BthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
4031 DECLR3CALLBACKMEMBER(int, pfnR3BthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
4032 DECLR3CALLBACKMEMBER(int, pfnR3BthUnmapCR3,(PVMCPU pVCpu));
4033
4034 DECLR0CALLBACKMEMBER(int, pfnR0BthTrap0eHandler,(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, bool *pfLockTaken));
4035 DECLR0CALLBACKMEMBER(int, pfnR0BthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
4036 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
4037 DECLR0CALLBACKMEMBER(int, pfnR0BthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
4038 DECLR0CALLBACKMEMBER(int, pfnR0BthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
4039 DECLR0CALLBACKMEMBER(unsigned, pfnR0BthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
4040 DECLR0CALLBACKMEMBER(int, pfnR0BthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
4041 DECLR0CALLBACKMEMBER(int, pfnR0BthUnmapCR3,(PVMCPU pVCpu));
4042
4043 DECLRCCALLBACKMEMBER(int, pfnRCBthTrap0eHandler,(PVMCPU pVCpu, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, bool *pfLockTaken));
4044 DECLRCCALLBACKMEMBER(int, pfnRCBthInvalidatePage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
4045 DECLRCCALLBACKMEMBER(int, pfnRCBthSyncCR3,(PVMCPU pVCpu, uint64_t cr0, uint64_t cr3, uint64_t cr4, bool fGlobal));
4046 DECLRCCALLBACKMEMBER(int, pfnRCBthPrefetchPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage));
4047 DECLRCCALLBACKMEMBER(int, pfnRCBthVerifyAccessSyncPage,(PVMCPU pVCpu, RTGCPTR GCPtrPage, unsigned fFlags, unsigned uError));
4048 DECLRCCALLBACKMEMBER(unsigned, pfnRCBthAssertCR3,(PVMCPU pVCpu, uint64_t cr3, uint64_t cr4, RTGCPTR GCPtr, RTGCPTR cb));
4049 DECLRCCALLBACKMEMBER(int, pfnRCBthMapCR3,(PVMCPU pVCpu, RTGCPHYS GCPhysCR3));
4050 DECLRCCALLBACKMEMBER(int, pfnRCBthUnmapCR3,(PVMCPU pVCpu));
4051#if 0
4052 RTRCPTR alignment2; /**< structure size alignment. */
4053#endif
4054 /** @} */
4055
4056 /** For saving stack space, the disassembler state is allocated here instead of
4057 * on the stack. */
4058 DISCPUSTATE DisState;
4059
4060 /** Counts the number of times the netware WP0+RO+US hack has been applied. */
4061 uint64_t cNetwareWp0Hacks;
4062
4063 /** Count the number of pgm pool access handler calls. */
4064 uint64_t cPoolAccessHandler;
4065
4066 /** @name Release Statistics
4067 * @{ */
4068 /** The number of times the guest has switched mode since last reset or statistics reset. */
4069 STAMCOUNTER cGuestModeChanges;
4070 /** The number of times the guest has switched mode since last reset or statistics reset. */
4071 STAMCOUNTER cA20Changes;
4072 /** @} */
4073
4074#ifdef VBOX_WITH_STATISTICS /** @todo move this chunk to the heap. */
4075 /** @name Statistics
4076 * @{ */
4077 /** RC: Pointer to the statistics. */
4078 RCPTRTYPE(PGMCPUSTATS *) pStatsRC;
4079 /** RC: Which statistic this \#PF should be attributed to. */
4080 RCPTRTYPE(PSTAMPROFILE) pStatTrap0eAttributionRC;
4081 /** R0: Pointer to the statistics. */
4082 R0PTRTYPE(PGMCPUSTATS *) pStatsR0;
4083 /** R0: Which statistic this \#PF should be attributed to. */
4084 R0PTRTYPE(PSTAMPROFILE) pStatTrap0eAttributionR0;
4085 /** R3: Pointer to the statistics. */
4086 R3PTRTYPE(PGMCPUSTATS *) pStatsR3;
4087 /** Alignment padding. */
4088 RTR3PTR pPaddingR3;
4089 /** @} */
4090#endif /* VBOX_WITH_STATISTICS */
4091} PGMCPU;
4092/** Pointer to the per-cpu PGM data. */
4093typedef PGMCPU *PPGMCPU;
4094
4095
4096/** @name PGM::fSyncFlags Flags
4097 * @{
4098 */
4099/** Updates the virtual access handler state bit in PGMPAGE. */
4100#define PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL RT_BIT(0)
4101/** Always sync CR3. */
4102#define PGM_SYNC_ALWAYS RT_BIT(1)
4103/** Check monitoring on next CR3 (re)load and invalidate page.
4104 * @todo This is obsolete now. Remove after 2.2.0 is branched off. */
4105#define PGM_SYNC_MONITOR_CR3 RT_BIT(2)
4106/** Check guest mapping in SyncCR3. */
4107#define PGM_SYNC_MAP_CR3 RT_BIT(3)
4108/** Clear the page pool (a light weight flush). */
4109#define PGM_SYNC_CLEAR_PGM_POOL_BIT 8
4110#define PGM_SYNC_CLEAR_PGM_POOL RT_BIT(PGM_SYNC_CLEAR_PGM_POOL_BIT)
4111/** @} */
4112
4113
4114RT_C_DECLS_BEGIN
4115
4116#if defined(VBOX_STRICT) && defined(IN_RING3)
4117int pgmLockDebug(PVM pVM, RT_SRC_POS_DECL);
4118# define pgmLock(a_pVM) pgmLockDebug(a_pVM, RT_SRC_POS)
4119#else
4120int pgmLock(PVM pVM);
4121#endif
4122void pgmUnlock(PVM pVM);
4123/**
4124 * Asserts that the caller owns the PDM lock.
4125 * This is the internal variant of PGMIsLockOwner.
4126 * @param a_pVM Pointer to the VM.
4127 */
4128#define PGM_LOCK_ASSERT_OWNER(a_pVM) Assert(PDMCritSectIsOwner(&(a_pVM)->pgm.s.CritSectX))
4129/**
4130 * Asserts that the caller owns the PDM lock.
4131 * This is the internal variant of PGMIsLockOwner.
4132 * @param a_pVM Pointer to the VM.
4133 * @param a_pVCpu The current CPU handle.
4134 */
4135#define PGM_LOCK_ASSERT_OWNER_EX(a_pVM, a_pVCpu) Assert(PDMCritSectIsOwnerEx(&(a_pVM)->pgm.s.CritSectX, pVCpu))
4136
4137#ifndef PGM_WITHOUT_MAPPINGS
4138int pgmR3MappingsFixInternal(PVM pVM, RTGCPTR GCPtrBase, uint32_t cb);
4139int pgmR3SyncPTResolveConflict(PVM pVM, PPGMMAPPING pMapping, PX86PD pPDSrc, RTGCPTR GCPtrOldMapping);
4140int pgmR3SyncPTResolveConflictPAE(PVM pVM, PPGMMAPPING pMapping, RTGCPTR GCPtrOldMapping);
4141int pgmMapResolveConflicts(PVM pVM);
4142#endif /* !PGM_WITHOUT_MAPPINGS */
4143PPGMMAPPING pgmGetMapping(PVM pVM, RTGCPTR GCPtr);
4144DECLCALLBACK(void) pgmR3MapInfo(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
4145
4146int pgmHandlerPhysicalExCreate(PVM pVM, PGMPHYSHANDLERTYPE hType, RTR3PTR pvUserR3, RTR0PTR pvUserR0,
4147 RTRCPTR pvUserRC, R3PTRTYPE(const char *) pszDesc, PPGMPHYSHANDLER *ppPhysHandler);
4148int pgmHandlerPhysicalExRegister(PVM pVM, PPGMPHYSHANDLER pPhysHandler, RTGCPHYS GCPhys, RTGCPHYS GCPhysLast);
4149int pgmHandlerPhysicalExDeregister(PVM pVM, PPGMPHYSHANDLER pPhysHandler);
4150int pgmHandlerPhysicalExDestroy(PVM pVM, PPGMPHYSHANDLER pHandler);
4151void pgmR3HandlerPhysicalUpdateAll(PVM pVM);
4152bool pgmHandlerPhysicalIsAll(PVM pVM, RTGCPHYS GCPhys);
4153void pgmHandlerPhysicalResetAliasedPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhysPage, bool fDoAccounting);
4154#ifdef VBOX_WITH_RAW_MODE
4155PPGMVIRTHANDLER pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, unsigned *piPage);
4156DECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser);
4157# if defined(VBOX_STRICT) || defined(LOG_ENABLED)
4158void pgmHandlerVirtualDumpPhysPages(PVM pVM);
4159# else
4160# define pgmHandlerVirtualDumpPhysPages(a) do { } while (0)
4161# endif
4162#endif /* VBOX_WITH_RAW_MODE */
4163DECLCALLBACK(void) pgmR3InfoHandlers(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
4164int pgmR3InitSavedState(PVM pVM, uint64_t cbRam);
4165
4166int pgmPhysAllocPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
4167int pgmPhysAllocLargePage(PVM pVM, RTGCPHYS GCPhys);
4168int pgmPhysRecheckLargePage(PVM pVM, RTGCPHYS GCPhys, PPGMPAGE pLargePage);
4169int pgmPhysPageLoadIntoTlb(PVM pVM, RTGCPHYS GCPhys);
4170int pgmPhysPageLoadIntoTlbWithPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
4171void pgmPhysPageMakeWriteMonitoredWritable(PVM pVM, PPGMPAGE pPage);
4172int pgmPhysPageMakeWritable(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys);
4173int pgmPhysPageMakeWritableAndMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv);
4174int pgmPhysPageMap(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv);
4175int pgmPhysPageMapReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void const **ppv);
4176int pgmPhysPageMapByPageID(PVM pVM, uint32_t idPage, RTHCPHYS HCPhys, void **ppv);
4177int pgmPhysGCPhys2R3Ptr(PVM pVM, RTGCPHYS GCPhys, PRTR3PTR pR3Ptr);
4178int pgmPhysCr3ToHCPtr(PVM pVM, RTGCPHYS GCPhys, PRTR3PTR pR3Ptr);
4179int pgmPhysGCPhys2CCPtrInternalDepr(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv);
4180int pgmPhysGCPhys2CCPtrInternal(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, void **ppv, PPGMPAGEMAPLOCK pLock);
4181int pgmPhysGCPhys2CCPtrInternalReadOnly(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, const void **ppv, PPGMPAGEMAPLOCK pLock);
4182void pgmPhysReleaseInternalPageMappingLock(PVM pVM, PPGMPAGEMAPLOCK pLock);
4183PGM_ALL_CB2_PROTO(FNPGMPHYSHANDLER) pgmPhysRomWriteHandler;
4184#ifndef IN_RING3
4185DECLEXPORT(FNPGMPHYSHANDLER) pgmPhysHandlerRedirectToHC;
4186DECLEXPORT(FNPGMRZPHYSPFHANDLER) pgmPhysPfHandlerRedirectToHC;
4187DECLEXPORT(FNPGMRZPHYSPFHANDLER) pgmPhysRomWritePfHandler;
4188#endif
4189int pgmPhysFreePage(PVM pVM, PGMMFREEPAGESREQ pReq, uint32_t *pcPendingPages, PPGMPAGE pPage, RTGCPHYS GCPhys);
4190void pgmPhysInvalidRamRangeTlbs(PVM pVM);
4191void pgmPhysInvalidatePageMapTLB(PVM pVM);
4192void pgmPhysInvalidatePageMapTLBEntry(PVM pVM, RTGCPHYS GCPhys);
4193PPGMRAMRANGE pgmPhysGetRangeSlow(PVM pVM, RTGCPHYS GCPhys);
4194PPGMRAMRANGE pgmPhysGetRangeAtOrAboveSlow(PVM pVM, RTGCPHYS GCPhys);
4195PPGMPAGE pgmPhysGetPageSlow(PVM pVM, RTGCPHYS GCPhys);
4196int pgmPhysGetPageExSlow(PVM pVM, RTGCPHYS GCPhys, PPPGMPAGE ppPage);
4197int pgmPhysGetPageAndRangeExSlow(PVM pVM, RTGCPHYS GCPhys, PPPGMPAGE ppPage, PPGMRAMRANGE *ppRam);
4198
4199#ifdef IN_RING3
4200void pgmR3PhysRelinkRamRanges(PVM pVM);
4201int pgmR3PhysRamPreAllocate(PVM pVM);
4202int pgmR3PhysRamReset(PVM pVM);
4203int pgmR3PhysRomReset(PVM pVM);
4204int pgmR3PhysRamZeroAll(PVM pVM);
4205int pgmR3PhysChunkMap(PVM pVM, uint32_t idChunk, PPPGMCHUNKR3MAP ppChunk);
4206int pgmR3PhysRamTerm(PVM pVM);
4207void pgmR3PhysRomTerm(PVM pVM);
4208void pgmR3PhysAssertSharedPageChecksums(PVM pVM);
4209
4210int pgmR3PoolInit(PVM pVM);
4211void pgmR3PoolRelocate(PVM pVM);
4212void pgmR3PoolResetUnpluggedCpu(PVM pVM, PVMCPU pVCpu);
4213void pgmR3PoolReset(PVM pVM);
4214void pgmR3PoolClearAll(PVM pVM, bool fFlushRemTlb);
4215DECLCALLBACK(VBOXSTRICTRC) pgmR3PoolClearAllRendezvous(PVM pVM, PVMCPU pVCpu, void *fpvFlushRemTbl);
4216void pgmR3PoolWriteProtectPages(PVM pVM);
4217
4218#endif /* IN_RING3 */
4219#if defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) || defined(IN_RC)
4220int pgmRZDynMapHCPageCommon(PPGMMAPSET pSet, RTHCPHYS HCPhys, void **ppv RTLOG_COMMA_SRC_POS_DECL);
4221int pgmRZDynMapGCPageCommon(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void **ppv RTLOG_COMMA_SRC_POS_DECL);
4222# ifdef LOG_ENABLED
4223void pgmRZDynMapUnusedHint(PVMCPU pVCpu, void *pvHint, RT_SRC_POS_DECL);
4224# else
4225void pgmRZDynMapUnusedHint(PVMCPU pVCpu, void *pvHint);
4226# endif
4227#endif
4228int pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, PGMPOOLACCESS enmAccess, bool fA20Enabled,
4229 uint16_t iUser, uint32_t iUserTable, bool fLockPage, PPPGMPOOLPAGE ppPage);
4230void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable);
4231void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
4232int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fFlush = true /* DO NOT USE false UNLESS YOU KNOWN WHAT YOU'RE DOING!! */);
4233void pgmPoolFlushPageByGCPhys(PVM pVM, RTGCPHYS GCPhys);
4234PPGMPOOLPAGE pgmPoolGetPage(PPGMPOOL pPool, RTHCPHYS HCPhys);
4235PPGMPOOLPAGE pgmPoolQueryPageForDbg(PPGMPOOL pPool, RTHCPHYS HCPhys);
4236int pgmPoolSyncCR3(PVMCPU pVCpu);
4237bool pgmPoolIsDirtyPage(PVM pVM, RTGCPHYS GCPhys);
4238void pgmPoolInvalidateDirtyPage(PVM pVM, RTGCPHYS GCPhysPT);
4239int pgmPoolTrackUpdateGCPhys(PVM pVM, RTGCPHYS GCPhysPage, PPGMPAGE pPhysPage, bool fFlushPTEs, bool *pfFlushTLBs);
4240void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint, uint16_t iPte);
4241uint16_t pgmPoolTrackPhysExtAddref(PVM pVM, PPGMPAGE pPhysPage, uint16_t u16, uint16_t iShwPT, uint16_t iPte);
4242void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPoolPage, PPGMPAGE pPhysPage, uint16_t iPte);
4243int pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
4244void pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
4245PGM_ALL_CB2_PROTO(FNPGMPHYSHANDLER) pgmPoolAccessHandler;
4246#ifndef IN_RING3
4247DECLEXPORT(FNPGMRZPHYSPFHANDLER) pgmPoolAccessPfHandler;
4248#endif
4249
4250void pgmPoolAddDirtyPage(PVM pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage);
4251void pgmPoolResetDirtyPages(PVM pVM);
4252void pgmPoolResetDirtyPage(PVM pVM, RTGCPTR GCPtrPage);
4253
4254int pgmR3ExitShadowModeBeforePoolFlush(PVMCPU pVCpu);
4255int pgmR3ReEnterShadowModeAfterPoolFlush(PVM pVM, PVMCPU pVCpu);
4256void pgmR3RefreshShadowModeAfterA20Change(PVMCPU pVCpu);
4257
4258void pgmMapSetShadowPDEs(PVM pVM, PPGMMAPPING pMap, unsigned iNewPDE);
4259void pgmMapClearShadowPDEs(PVM pVM, PPGMPOOLPAGE pShwPageCR3, PPGMMAPPING pMap, unsigned iOldPDE, bool fDeactivateCR3);
4260int pgmMapActivateCR3(PVM pVM, PPGMPOOLPAGE pShwPageCR3);
4261int pgmMapDeactivateCR3(PVM pVM, PPGMPOOLPAGE pShwPageCR3);
4262
4263int pgmShwMakePageSupervisorAndWritable(PVMCPU pVCpu, RTGCPTR GCPtr, bool fBigPage, uint32_t fOpFlags);
4264int pgmShwSyncPaePDPtr(PVMCPU pVCpu, RTGCPTR GCPtr, X86PGPAEUINT uGstPdpe, PX86PDPAE *ppPD);
4265int pgmShwSyncNestedPageLocked(PVMCPU pVCpu, RTGCPHYS GCPhysFault, uint32_t cPages, PGMMODE enmShwPagingMode);
4266
4267int pgmGstLazyMap32BitPD(PVMCPU pVCpu, PX86PD *ppPd);
4268int pgmGstLazyMapPaePDPT(PVMCPU pVCpu, PX86PDPT *ppPdpt);
4269int pgmGstLazyMapPaePD(PVMCPU pVCpu, uint32_t iPdpt, PX86PDPAE *ppPd);
4270int pgmGstLazyMapPml4(PVMCPU pVCpu, PX86PML4 *ppPml4);
4271int pgmGstPtWalk(PVMCPU pVCpu, RTGCPTR GCPtr, PPGMPTWALKGST pWalk);
4272
4273# if defined(VBOX_STRICT) && HC_ARCH_BITS == 64 && defined(IN_RING3)
4274FNDBGCCMD pgmR3CmdCheckDuplicatePages;
4275FNDBGCCMD pgmR3CmdShowSharedModules;
4276# endif
4277
4278RT_C_DECLS_END
4279
4280/** @} */
4281
4282#endif
4283
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