VirtualBox

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

Last change on this file since 69288 was 69111, checked in by vboxsync, 7 years ago

(C) year

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