VirtualBox

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

Last change on this file since 45808 was 45808, checked in by vboxsync, 12 years ago

VMM,DevVGA: Don't resolve RC symbols when HM is enabled (part 1).

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