VirtualBox

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

Last change on this file since 840 was 838, checked in by vboxsync, 18 years ago

GC Phys to HC virt conversion changes for dynamic RAM allocation.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 110.3 KB
Line 
1/* $Id: PGMInternal.h 838 2007-02-12 12:05:52Z vboxsync $ */
2/** @file
3 * PGM - Internal header file.
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#ifndef __PGMInternal_h__
23#define __PGMInternal_h__
24
25#include <VBox/cdefs.h>
26#include <VBox/types.h>
27#include <VBox/err.h>
28#include <VBox/stam.h>
29#include <VBox/param.h>
30#include <VBox/vmm.h>
31#include <VBox/mm.h>
32#include <VBox/pdm.h>
33#include <iprt/avl.h>
34#include <iprt/assert.h>
35#include <iprt/critsect.h>
36
37#if !defined(IN_PGM_R3) && !defined(IN_PGM_R0) && !defined(IN_PGM_GC)
38# error "Not in PGM! This is an internal header!"
39#endif
40
41
42/** @defgroup grp_pgm_int Internals
43 * @ingroup grp_pgm
44 * @internal
45 * @{
46 */
47
48
49/** @name PGM Compile Time Config
50 * @{
51 */
52
53/**
54 * Solve page is out of sync issues inside Guest Context (in PGMGC.cpp).
55 * Comment it if it will break something.
56 */
57#define PGM_OUT_OF_SYNC_IN_GC
58
59/**
60 * Virtualize the dirty bit
61 * This also makes a half-hearted attempt at the accessed bit. For full
62 * accessed bit virtualization define PGM_SYNC_ACCESSED_BIT.
63 */
64#define PGM_SYNC_DIRTY_BIT
65
66/**
67 * Fully virtualize the accessed bit.
68 * @remark This requires SYNC_DIRTY_ACCESSED_BITS to be defined!
69 */
70#define PGM_SYNC_ACCESSED_BIT
71
72/**
73 * Check and skip global PDEs for non-global flushes
74 */
75#define PGM_SKIP_GLOBAL_PAGEDIRS_ON_NONGLOBAL_FLUSH
76
77/**
78 * Sync N pages instead of a whole page table
79 */
80#define PGM_SYNC_N_PAGES
81
82/**
83 * Number of pages to sync during a page fault
84 *
85 * When PGMPOOL_WITH_GCPHYS_TRACKING is enabled using high values here
86 * causes a lot of unnecessary extents and also is slower than taking more \#PFs.
87 */
88#define PGM_SYNC_NR_PAGES 8
89
90/**
91 * Number of PGMPhysRead/Write cache entries (must be <= sizeof(uint64_t))
92 */
93#define PGM_MAX_PHYSCACHE_ENTRIES 64
94#define PGM_MAX_PHYSCACHE_ENTRIES_MASK (PGM_MAX_PHYSCACHE_ENTRIES-1)
95
96/**
97 * Enable caching of PGMR3PhysRead/WriteByte/Word/Dword
98 */
99#define PGM_PHYSMEMACCESS_CACHING
100
101/*
102 * Assert Sanity.
103 */
104#if defined(PGM_SYNC_ACCESSED_BIT) && !defined(PGM_SYNC_DIRTY_BIT)
105# error "PGM_SYNC_ACCESSED_BIT requires PGM_SYNC_DIRTY_BIT!"
106#endif
107
108/** @def PGMPOOL_WITH_CACHE
109 * Enable agressive caching using the page pool.
110 *
111 * This requires PGMPOOL_WITH_USER_TRACKING and PGMPOOL_WITH_MONITORING.
112 */
113#define PGMPOOL_WITH_CACHE
114
115/** @def PGMPOOL_WITH_MIXED_PT_CR3
116 * When defined, we'll deal with 'uncachable' pages.
117 */
118#ifdef PGMPOOL_WITH_CACHE
119# define PGMPOOL_WITH_MIXED_PT_CR3
120#endif
121
122/** @def PGMPOOL_WITH_MONITORING
123 * Monitor the guest pages which are shadowed.
124 * When this is enabled, PGMPOOL_WITH_CACHE or PGMPOOL_WITH_GCPHYS_TRACKING must
125 * be enabled as well.
126 * @remark doesn't really work without caching now. (Mixed PT/CR3 change.)
127 */
128#ifdef PGMPOOL_WITH_CACHE
129# define PGMPOOL_WITH_MONITORING
130#endif
131
132/** @def PGMPOOL_WITH_GCPHYS_TRACKING
133 * Tracking the of shadow pages mapping guest physical pages.
134 *
135 * This is very expensive, the current cache prototype is trying to figure out
136 * whether it will be acceptable with an agressive caching policy.
137 */
138#if defined(PGMPOOL_WITH_CACHE) || defined(PGMPOOL_WITH_MONITORING)
139# define PGMPOOL_WITH_GCPHYS_TRACKING
140#endif
141
142/** @def PGMPOOL_WITH_USER_TRACKING
143 * Tracking users of shadow pages. This is required for the linking of shadow page
144 * tables and physical guest addresses.
145 */
146#if defined(PGMPOOL_WITH_GCPHYS_TRACKING) || defined(PGMPOOL_WITH_CACHE) || defined(PGMPOOL_WITH_MONITORING)
147# define PGMPOOL_WITH_USER_TRACKING
148#endif
149
150/** @def PGMPOOL_CFG_MAX_GROW
151 * The maximum number of pages to add to the pool in one go.
152 */
153#define PGMPOOL_CFG_MAX_GROW (_256K >> PAGE_SHIFT)
154
155/** @def VBOX_STRICT_PGM_HANDLER_VIRTUAL
156 * Enables some extra assertions for virtual handlers (mainly phys2virt related).
157 */
158#ifdef VBOX_STRICT
159# define VBOX_STRICT_PGM_HANDLER_VIRTUAL
160#endif
161/** @} */
162
163
164/** @name PDPTR and PML4 flags.
165 * These are placed in the three bits available for system programs in
166 * the PDPTR and PML4 entries.
167 * @{ */
168/** The entry is a permanent one and it's must always be present.
169 * Never free such an entry. */
170#define PGM_PLXFLAGS_PERMANENT BIT64(10)
171/** @} */
172
173/** @name Page directory flags.
174 * These are placed in the three bits available for system programs in
175 * the page directory entries.
176 * @{ */
177/** Mapping (hypervisor allocated pagetable). */
178#define PGM_PDFLAGS_MAPPING BIT64(10)
179/** Made read-only to facilitate dirty bit tracking. */
180#define PGM_PDFLAGS_TRACK_DIRTY BIT64(11)
181/** @} */
182
183/** @name Page flags.
184 * These are placed in the three bits available for system programs in
185 * the page entries.
186 * @{ */
187/** Made read-only to facilitate dirty bit tracking. */
188#define PGM_PTFLAGS_TRACK_DIRTY BIT64(9)
189
190#ifndef PGM_PTFLAGS_CSAM_VALIDATED
191/** Scanned and approved by CSAM (tm).
192 * NOTE: Must be identical to the one defined in CSAMInternal.h!!
193 * @todo Move PGM_PTFLAGS_* and PGM_PDFLAGS_* to VBox/pgm.h. */
194#define PGM_PTFLAGS_CSAM_VALIDATED BIT64(11)
195#endif
196/** @} */
197
198/** @name Defines used to indicate the shadow and guest paging in the templates.
199 * @{ */
200#define PGM_TYPE_REAL 1
201#define PGM_TYPE_PROT 2
202#define PGM_TYPE_32BIT 3
203#define PGM_TYPE_PAE 4
204#define PGM_TYPE_AMD64 5
205/** @} */
206
207/** @def PGM_HCPHYS_2_PTR
208 * Maps a HC physical page pool address to a virtual address.
209 *
210 * @returns VBox status code.
211 * @param pVM The VM handle.
212 * @param HCPhys The HC physical address to map to a virtual one.
213 * @param ppv Where to store the virtual address. No need to cast this.
214 *
215 * @remark In GC this uses PGMGCDynMapHCPage(), so it will consume of the
216 * small page window employeed by that function. Be careful.
217 * @remark There is no need to assert on the result.
218 */
219#ifdef IN_GC
220# define PGM_HCPHYS_2_PTR(pVM, HCPhys, ppv) PGMGCDynMapHCPage(pVM, HCPhys, (void **)(ppv))
221#else
222# define PGM_HCPHYS_2_PTR(pVM, HCPhys, ppv) MMPagePhys2PageEx(pVM, HCPhys, (void **)(ppv))
223#endif
224
225/** @def PGM_GCPHYS_2_PTR
226 * Maps a GC physical page address to a virtual address.
227 *
228 * @returns VBox status code.
229 * @param pVM The VM handle.
230 * @param GCPhys The GC physical address to map to a virtual one.
231 * @param ppv Where to store the virtual address. No need to cast this.
232 *
233 * @remark In GC this uses PGMGCDynMapGCPage(), so it will consume of the
234 * small page window employeed by that function. Be careful.
235 * @remark There is no need to assert on the result.
236 */
237#ifdef IN_GC
238# define PGM_GCPHYS_2_PTR(pVM, GCPhys, ppv) PGMGCDynMapGCPage(pVM, GCPhys, (void **)(ppv))
239#else
240# define PGM_GCPHYS_2_PTR(pVM, GCPhys, ppv) PGMPhysGCPhys2HCPtr(pVM, GCPhys, 1 /* one page only */, (void **)(ppv)) /** @todo this isn't asserting, use PGMRamGCPhys2HCPtr! */
241#endif
242
243/** @def PGM_GCPHYS_2_PTR_EX
244 * Maps a unaligned GC physical page address to a virtual address.
245 *
246 * @returns VBox status code.
247 * @param pVM The VM handle.
248 * @param GCPhys The GC physical address to map to a virtual one.
249 * @param ppv Where to store the virtual address. No need to cast this.
250 *
251 * @remark In GC this uses PGMGCDynMapGCPage(), so it will consume of the
252 * small page window employeed by that function. Be careful.
253 * @remark There is no need to assert on the result.
254 */
255#ifdef IN_GC
256# define PGM_GCPHYS_2_PTR_EX(pVM, GCPhys, ppv) PGMGCDynMapGCPageEx(pVM, GCPhys, (void **)(ppv))
257#else
258# define PGM_GCPHYS_2_PTR_EX(pVM, GCPhys, ppv) PGMPhysGCPhys2HCPtr(pVM, GCPhys, 1 /* one page only */, (void **)(ppv)) /** @todo this isn't asserting, use PGMRamGCPhys2HCPtr! */
259#endif
260
261/** @def PGM_INVL_PG
262 * Invalidates a page when in GC does nothing in HC.
263 *
264 * @param GCVirt The virtual address of the page to invalidate.
265 */
266#ifdef IN_GC
267# define PGM_INVL_PG(GCVirt) ASMInvalidatePage((void *)(GCVirt))
268#else
269# define PGM_INVL_PG(GCVirt) ((void)0)
270#endif
271
272/** @def PGM_INVL_BIG_PG
273 * Invalidates a 4MB page directory entry when in GC does nothing in HC.
274 *
275 * @param GCVirt The virtual address within the page directory to invalidate.
276 */
277#ifdef IN_GC
278# define PGM_INVL_BIG_PG(GCVirt) ASMReloadCR3()
279#else
280# define PGM_INVL_BIG_PG(GCVirt) ((void)0)
281#endif
282
283/** @def PGM_INVL_GUEST_TLBS()
284 * Invalidates all guest TLBs.
285 */
286#ifdef IN_GC
287# define PGM_INVL_GUEST_TLBS() ASMReloadCR3()
288#else
289# define PGM_INVL_GUEST_TLBS() ((void)0)
290#endif
291
292
293/**
294 * Structure for tracking GC Mappings.
295 *
296 * This structure is used by linked list in both GC and HC.
297 */
298typedef struct PGMMAPPING
299{
300 /** Pointer to next entry. */
301 HCPTRTYPE(struct PGMMAPPING *) pNextHC;
302 /** Pointer to next entry. */
303 GCPTRTYPE(struct PGMMAPPING *) pNextGC;
304 /** Start Virtual address. */
305 RTGCUINTPTR GCPtr;
306 /** Last Virtual address (inclusive). */
307 RTGCUINTPTR GCPtrLast;
308 /** Range size (bytes). */
309 RTGCUINTPTR cb;
310 /** Pointer to relocation callback function. */
311 HCPTRTYPE(PFNPGMRELOCATE) pfnRelocate;
312 /** User argument to the callback. */
313 HCPTRTYPE(void *) pvUser;
314 /** Mapping description / name. For easing debugging. */
315 HCPTRTYPE(const char *) pszDesc;
316 /** Number of page tables. */
317 RTUINT cPTs;
318#if HC_ARCH_BITS != GC_ARCH_BITS
319 RTUINT uPadding0; /**< Alignment padding. */
320#endif
321 /** Array of page table mapping data. Each entry
322 * describes one page table. The array can be longer
323 * than the declared length.
324 */
325 struct
326 {
327 /** The HC physical address of the page table. */
328 RTHCPHYS HCPhysPT;
329 /** The HC physical address of the first PAE page table. */
330 RTHCPHYS HCPhysPaePT0;
331 /** The HC physical address of the second PAE page table. */
332 RTHCPHYS HCPhysPaePT1;
333 /** The HC virtual address of the 32-bit page table. */
334 HCPTRTYPE(PVBOXPT) pPTHC;
335 /** The HC virtual address of the two PAE page table. (i.e 1024 entries instead of 512) */
336 HCPTRTYPE(PX86PTPAE) paPaePTsHC;
337 /** The GC virtual address of the 32-bit page table. */
338 GCPTRTYPE(PVBOXPT) pPTGC;
339 /** The GC virtual address of the two PAE page table. */
340 GCPTRTYPE(PX86PTPAE) paPaePTsGC;
341 } aPTs[1];
342} PGMMAPPING;
343/** Pointer to structure for tracking GC Mappings. */
344typedef struct PGMMAPPING *PPGMMAPPING;
345
346
347/**
348 * Physical page access handler structure.
349 *
350 * This is used to keep track of physical address ranges
351 * which are being monitored in some kind of way.
352 */
353typedef struct PGMPHYSHANDLER
354{
355 AVLROGCPHYSNODECORE Core;
356 /** Alignment padding. */
357 uint32_t u32Padding;
358 /** Access type. */
359 PGMPHYSHANDLERTYPE enmType;
360 /** Number of pages to update. */
361 uint32_t cPages;
362 /** Pointer to R3 callback function. */
363 HCPTRTYPE(PFNPGMR3PHYSHANDLER) pfnHandlerR3;
364 /** User argument for R3 handlers. */
365 HCPTRTYPE(void *) pvUserR3;
366 /** Pointer to R0 callback function. */
367 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnHandlerR0;
368 /** User argument for R0 handlers. */
369 HCPTRTYPE(void *) pvUserR0;
370 /** Pointer to GC callback function. */
371 GCPTRTYPE(PFNPGMGCPHYSHANDLER) pfnHandlerGC;
372 /** User argument for GC handlers. */
373 GCPTRTYPE(void *) pvUserGC;
374 /** Description / Name. For easing debugging. */
375 HCPTRTYPE(const char *) pszDesc;
376#ifdef VBOX_WITH_STATISTICS
377 /** Profiling of this handler. */
378 STAMPROFILE Stat;
379#endif
380} PGMPHYSHANDLER;
381/** Pointer to a physical page access handler structure. */
382typedef PGMPHYSHANDLER *PPGMPHYSHANDLER;
383
384
385/**
386 * Cache node for the physical addresses covered by a virtual handler.
387 */
388typedef struct PGMPHYS2VIRTHANDLER
389{
390 /** Core node for the tree based on physical ranges. */
391 AVLROGCPHYSNODECORE Core;
392 /** Offset from this struct to the PGMVIRTHANDLER structure. */
393 RTGCINTPTR offVirtHandler;
394 /** Offset of the next alias relativer to this one.
395 * Bit 0 is used for indicating whether we're in the tree.
396 * Bit 1 is used for indicating that we're the head node.
397 */
398 int32_t offNextAlias;
399} PGMPHYS2VIRTHANDLER;
400/** Pointer to a phys to virtual handler structure. */
401typedef PGMPHYS2VIRTHANDLER *PPGMPHYS2VIRTHANDLER;
402
403/** The bit in PGMPHYS2VIRTHANDLER::offNextAlias used to indicate that the
404 * node is in the tree. */
405#define PGMPHYS2VIRTHANDLER_IN_TREE BIT(0)
406/** The bit in PGMPHYS2VIRTHANDLER::offNextAlias used to indicate that the
407 * node is in the head of an alias chain.
408 * The PGMPHYS2VIRTHANDLER_IN_TREE is always set if this bit is set. */
409#define PGMPHYS2VIRTHANDLER_IS_HEAD BIT(1)
410/** The mask to apply to PGMPHYS2VIRTHANDLER::offNextAlias to get the offset. */
411#define PGMPHYS2VIRTHANDLER_OFF_MASK (~(int32_t)3)
412
413
414/**
415 * Virtual page access handler structure.
416 *
417 * This is used to keep track of virtual address ranges
418 * which are being monitored in some kind of way.
419 */
420typedef struct PGMVIRTHANDLER
421{
422 /** Core node for the tree based on virtual ranges. */
423 AVLROGCPTRNODECORE Core;
424 /** Number of cache pages. */
425 uint32_t u32Padding;
426 /** Access type. */
427 PGMVIRTHANDLERTYPE enmType;
428 /** Number of cache pages. */
429 uint32_t cPages;
430
431/** @todo The next two members are redundant. It adds some readability though. */
432 /** Start of the range. */
433 RTGCPTR GCPtr;
434 /** End of the range (exclusive). */
435 RTGCPTR GCPtrLast;
436 /** Size of the range (in bytes). */
437 RTGCUINTPTR cb;
438 /** Pointer to the GC callback function. */
439 GCPTRTYPE(PFNPGMGCVIRTHANDLER) pfnHandlerGC;
440 /** Pointer to the HC callback function for invalidation. */
441 HCPTRTYPE(PFNPGMHCVIRTINVALIDATE) pfnInvalidateHC;
442 /** Pointer to the HC callback function. */
443 HCPTRTYPE(PFNPGMHCVIRTHANDLER) pfnHandlerHC;
444 /** Description / Name. For easing debugging. */
445 HCPTRTYPE(const char *) pszDesc;
446#ifdef VBOX_WITH_STATISTICS
447 /** Profiling of this handler. */
448 STAMPROFILE Stat;
449#endif
450 /** Array of cached physical addresses for the monitored ranged. */
451 PGMPHYS2VIRTHANDLER aPhysToVirt[HC_ARCH_BITS == 32 ? 1 : 2];
452} PGMVIRTHANDLER;
453/** Pointer to a virtual page access handler structure. */
454typedef PGMVIRTHANDLER *PPGMVIRTHANDLER;
455
456
457/**
458 * Ram range for GC Phys to HC Phys conversion.
459 *
460 * Can be used for HC Virt to GC Phys and HC Virt to HC Phys
461 * conversions too, but we'll let MM handle that for now.
462 *
463 * This structure is used by linked lists in both GC and HC.
464 */
465typedef struct PGMRAMRANGE
466{
467 /** Pointer to the next RAM range - for HC. */
468 HCPTRTYPE(struct PGMRAMRANGE *) pNextHC;
469 /** Pointer to the next RAM range - for GC. */
470 GCPTRTYPE(struct PGMRAMRANGE *) pNextGC;
471 /** Start of the range. Page aligned. */
472 RTGCPHYS GCPhys;
473 /** Last address in the range (inclusive). Page aligned (-1). */
474 RTGCPHYS GCPhysLast;
475 /** Size of the range. (Page aligned of course). */
476 RTGCPHYS cb;
477 /** MM_RAM_* flags */
478 uint32_t fFlags;
479
480 /** HC virtual lookup ranges for chunks. Currently only used with MM_RAM_FLAGS_DYNAMIC_ALLOC ranges. */
481 GCPTRTYPE(void **) pavHCChunkGC;
482 /** HC virtual lookup ranges for chunks. Currently only used with MM_RAM_FLAGS_DYNAMIC_ALLOC ranges. */
483 HCPTRTYPE(void **) pavHCChunkHC;
484
485 /** Start of the HC mapping of the range.
486 * For pure MMIO and dynamically allocated ranges this is NULL, while for all ranges this is a valid pointer. */
487 HCPTRTYPE(void *) pvHC;
488
489 /** Array of the flags and HC physical addresses corresponding to the range.
490 * The index is the page number in the range. The size is cb >> PAGE_SHIFT.
491 *
492 * The 12 lower bits of the physical address are flags and must be masked
493 * off to get the correct physical address.
494 *
495 * For pure MMIO ranges only the flags are valid.
496 */
497 RTHCPHYS aHCPhys[1];
498} PGMRAMRANGE;
499/** Pointer to Ram range for GC Phys to HC Phys conversion. */
500typedef PGMRAMRANGE *PPGMRAMRANGE;
501
502/** Return hc ptr corresponding to the ram range and physical offset */
503#define PGMRAMRANGE_GETHCPTR(pRam, off) \
504 (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC) ? (RTHCPTR)((RTHCUINTPTR)CTXSUFF(pRam->pavHCChunk)[(off >> PGM_DYNAMIC_CHUNK_SHIFT)] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK)) \
505 : (RTHCPTR)((RTHCUINTPTR)pRam->pvHC + off);
506
507/** @todo r=bird: fix typename. */
508/**
509 * PGMPhysRead/Write cache entry
510 */
511typedef struct PGMPHYSCACHE_ENTRY
512{
513 /** HC pointer to physical page */
514 HCPTRTYPE(uint8_t *) pbHC;
515 /** GC Physical address for cache entry */
516 RTGCPHYS GCPhys;
517#if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
518 RTGCPHYS u32Padding0; /**< alignment padding. */
519#endif
520} PGMPHYSCACHE_ENTRY;
521
522/**
523 * PGMPhysRead/Write cache to reduce REM memory access overhead
524 */
525typedef struct PGMPHYSCACHE
526{
527 /** Bitmap of valid cache entries */
528 uint64_t aEntries;
529 /** Cache entries */
530 PGMPHYSCACHE_ENTRY Entry[PGM_MAX_PHYSCACHE_ENTRIES];
531} PGMPHYSCACHE;
532
533
534/** @name PGM Pool Indexes.
535 * Aka. the unique shadow page identifier.
536 * @{ */
537/** NIL page pool IDX. */
538#define NIL_PGMPOOL_IDX 0
539/** The first normal index. */
540#define PGMPOOL_IDX_FIRST_SPECIAL 1
541/** Page directory (32-bit root). */
542#define PGMPOOL_IDX_PD 1
543/** The extended PAE page directory (2048 entries, works as root currently). */
544#define PGMPOOL_IDX_PAE_PD 2
545/** Page Directory Pointer Table (PAE root, not currently used). */
546#define PGMPOOL_IDX_PDPTR 3
547/** Page Map Level-4 (64-bit root). */
548#define PGMPOOL_IDX_PML4 4
549/** The first normal index. */
550#define PGMPOOL_IDX_FIRST 5
551/** The last valid index. (inclusive, 14 bits) */
552#define PGMPOOL_IDX_LAST 0x3fff
553/** @} */
554
555/** The NIL index for the parent chain. */
556#define NIL_PGMPOOL_USER_INDEX ((uint16_t)0xffff)
557
558/**
559 * Node in the chain linking a shadowed page to it's parent (user).
560 */
561#pragma pack(1)
562typedef struct PGMPOOLUSER
563{
564 /** The index to the next item in the chain. NIL_PGMPOOL_USER_INDEX is no next. */
565 uint16_t iNext;
566 /** The user page index. */
567 uint16_t iUser;
568 /** Index into the user table. */
569 uint16_t iUserTable;
570} PGMPOOLUSER, *PPGMPOOLUSER;
571typedef const PGMPOOLUSER *PCPGMPOOLUSER;
572#pragma pack()
573
574
575/** The NIL index for the phys ext chain. */
576#define NIL_PGMPOOL_PHYSEXT_INDEX ((uint16_t)0xffff)
577
578/**
579 * Node in the chain of physical cross reference extents.
580 */
581#pragma pack(1)
582typedef struct PGMPOOLPHYSEXT
583{
584 /** The index to the next item in the chain. NIL_PGMPOOL_PHYSEXT_INDEX is no next. */
585 uint16_t iNext;
586 /** The user page index. */
587 uint16_t aidx[3];
588} PGMPOOLPHYSEXT, *PPGMPOOLPHYSEXT;
589typedef const PGMPOOLPHYSEXT *PCPGMPOOLPHYSEXT;
590#pragma pack()
591
592
593/**
594 * The kind of page that's being shadowed.
595 */
596typedef enum PGMPOOLKIND
597{
598 /** The ritual invalid 0 entry. */
599 PGMPOOLKIND_INVALID = 0,
600 /** The entry is free (=unused). */
601 PGMPOOLKIND_FREE,
602
603 /** Shw: 32-bit page table; Gst: 32-bit page table. */
604 PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT,
605 /** Shw: 32-bit page table; Gst: 4MB page. */
606 PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB,
607 /** Shw: PAE page table; Gst: 32-bit page table. */
608 PGMPOOLKIND_PAE_PT_FOR_32BIT_PT,
609 /** Shw: PAE page table; Gst: Half of a 4MB page. */
610 PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB,
611 /** Shw: PAE page table; Gst: PAE page table. */
612 PGMPOOLKIND_PAE_PT_FOR_PAE_PT,
613 /** Shw: PAE page table; Gst: 2MB page. */
614 PGMPOOLKIND_PAE_PT_FOR_PAE_2MB,
615
616 /** Shw: PAE page directory; Gst: 32-bit page directory. */
617 PGMPOOLKIND_PAE_PD_FOR_32BIT_PD,
618 /** Shw: PAE page directory; Gst: PAE page directory. */
619 PGMPOOLKIND_PAE_PD_FOR_PAE_PD,
620
621 /** Shw: 64-bit page directory pointer table; Gst: 64-bit page directory pointer table. */
622 PGMPOOLKIND_64BIT_PDPTR_FOR_64BIT_PDPTR,
623
624 /** Shw: Root 32-bit page directory. */
625 PGMPOOLKIND_ROOT_32BIT_PD,
626 /** Shw: Root PAE page directory */
627 PGMPOOLKIND_ROOT_PAE_PD,
628 /** Shw: Root PAE page directory pointer table (legacy, 4 entries). */
629 PGMPOOLKIND_ROOT_PDPTR,
630 /** Shw: Root page map level-4 table. */
631 PGMPOOLKIND_ROOT_PML4,
632
633 /** The last valid entry. */
634 PGMPOOLKIND_LAST = PGMPOOLKIND_ROOT_PML4
635} PGMPOOLKIND;
636
637
638/**
639 * The tracking data for a page in the pool.
640 */
641typedef struct PGMPOOLPAGE
642{
643 /** AVL node code with the (HC) physical address of this page. */
644 AVLOHCPHYSNODECORE Core;
645 /** Pointer to the HC mapping of the page. */
646 HCPTRTYPE(void *) pvPageHC;
647 /** The guest physical address. */
648 RTGCPHYS GCPhys;
649 /** The kind of page we're shadowing. (This is really a PGMPOOLKIND enum.) */
650 uint8_t enmKind;
651 uint8_t bPadding;
652 /** The index of this page. */
653 uint16_t idx;
654 /** The next entry in the list this page currently resides in.
655 * It's either in the free list or in the GCPhys hash. */
656 uint16_t iNext;
657#ifdef PGMPOOL_WITH_USER_TRACKING
658 /** Head of the user chain. NIL_PGMPOOL_USER_INDEX if not currently in use. */
659 uint16_t iUserHead;
660 /** The number of present entries. */
661 uint16_t cPresent;
662 /** The first entry in the table which is present. */
663 uint16_t iFirstPresent;
664#endif
665#ifdef PGMPOOL_WITH_MONITORING
666 /** The number of modifications to the monitored page. */
667 uint16_t cModifications;
668 /** The next modified page. NIL_PGMPOOL_IDX if tail. */
669 uint16_t iModifiedNext;
670 /** The previous modified page. NIL_PGMPOOL_IDX if head. */
671 uint16_t iModifiedPrev;
672 /** The next page sharing access handler. NIL_PGMPOOL_IDX if tail. */
673 uint16_t iMonitoredNext;
674 /** The previous page sharing access handler. NIL_PGMPOOL_IDX if head. */
675 uint16_t iMonitoredPrev;
676#endif
677#ifdef PGMPOOL_WITH_CACHE
678 /** The next page in the age list. */
679 uint16_t iAgeNext;
680 /** The previous page in the age list. */
681 uint16_t iAgePrev;
682/** @todo add more from PGMCache.h when merging with it. */
683#endif /* PGMPOOL_WITH_CACHE */
684 /** Used to indicate that the page is zeroed. */
685 bool fZeroed;
686 /** Used to indicate that a PT has non-global entries. */
687 bool fSeenNonGlobal;
688 /** Used to indicate that we're monitoring writes to the guest page. */
689 bool fMonitored;
690 /** Used to indicate that the page is in the cache (e.g. in the GCPhys hash).
691 * (All pages are in the age list.) */
692 bool fCached;
693 /** This is used by the R3 access handlers when invoked by an async thread.
694 * It's a hack required because of REMR3NotifyHandlerPhysicalDeregister. */
695 bool volatile fReusedFlushPending;
696 /** Used to indicate that the guest is mapping the page is also used as a CR3.
697 * In these cases the access handler acts differently and will check
698 * for mapping conflicts like the normal CR3 handler.
699 * @todo When we change the CR3 shadowing to use pool pages, this flag can be
700 * replaced by a list of pages which share access handler.
701 */
702 bool fCR3Mix;
703#if HC_ARCH_BITS == 64 || GC_ARCH_BITS == 64
704 bool Alignment[4]; /**< Align the structure size on a 64-bit boundrary. */
705#endif
706} PGMPOOLPAGE, *PPGMPOOLPAGE, **PPPGMPOOLPAGE;
707
708
709#ifdef PGMPOOL_WITH_CACHE
710/** The hash table size. */
711# define PGMPOOL_HASH_SIZE 0x40
712/** The hash function. */
713# define PGMPOOL_HASH(GCPhys) ( ((GCPhys) >> PAGE_SHIFT) & (PGMPOOL_HASH_SIZE - 1) )
714#endif
715
716
717/**
718 * The shadow page pool instance data.
719 *
720 * It's all one big allocation made at init time, except for the
721 * pages that is. The user nodes follows immediatly after the
722 * page structures.
723 */
724typedef struct PGMPOOL
725{
726 /** The VM handle - HC Ptr. */
727 HCPTRTYPE(PVM) pVMHC;
728 /** The VM handle - GC Ptr. */
729 GCPTRTYPE(PVM) pVMGC;
730 /** The max pool size. This includes the special IDs. */
731 uint16_t cMaxPages;
732 /** The current pool size. */
733 uint16_t cCurPages;
734 /** The head of the free page list. */
735 uint16_t iFreeHead;
736 /* Padding. */
737 uint16_t u16Padding;
738#ifdef PGMPOOL_WITH_USER_TRACKING
739 /** Head of the chain of free user nodes. */
740 uint16_t iUserFreeHead;
741 /** The number of user nodes we've allocated. */
742 uint16_t cMaxUsers;
743 /** The number of present page table entries in the entire pool. */
744 uint32_t cPresent;
745 /** Pointer to the array of user nodes - GC pointer. */
746 GCPTRTYPE(PPGMPOOLUSER) paUsersGC;
747 /** Pointer to the array of user nodes - HC pointer. */
748 HCPTRTYPE(PPGMPOOLUSER) paUsersHC;
749#endif /* PGMPOOL_WITH_USER_TRACKING */
750#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
751 /** Head of the chain of free phys ext nodes. */
752 uint16_t iPhysExtFreeHead;
753 /** The number of user nodes we've allocated. */
754 uint16_t cMaxPhysExts;
755 /** Pointer to the array of physical xref extent - GC pointer. */
756 GCPTRTYPE(PPGMPOOLPHYSEXT) paPhysExtsGC;
757 /** Pointer to the array of physical xref extent nodes - HC pointer. */
758 HCPTRTYPE(PPGMPOOLPHYSEXT) paPhysExtsHC;
759#endif /* PGMPOOL_WITH_GCPHYS_TRACKING */
760#ifdef PGMPOOL_WITH_CACHE
761 /** Hash table for GCPhys addresses. */
762 uint16_t aiHash[PGMPOOL_HASH_SIZE];
763 /** The head of the age list. */
764 uint16_t iAgeHead;
765 /** The tail of the age list. */
766 uint16_t iAgeTail;
767 /** Set if the cache is enabled. */
768 bool fCacheEnabled;
769#endif /* PGMPOOL_WITH_CACHE */
770#ifdef PGMPOOL_WITH_MONITORING
771 /** Head of the list of modified pages. */
772 uint16_t iModifiedHead;
773 /** The current number of modified pages. */
774 uint16_t cModifiedPages;
775 /** Access handler, GC. */
776 GCPTRTYPE(PFNPGMGCPHYSHANDLER) pfnAccessHandlerGC;
777 /** Access handler, R0. */
778 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnAccessHandlerR0;
779 /** Access handler, R3. */
780 HCPTRTYPE(PFNPGMR3PHYSHANDLER) pfnAccessHandlerR3;
781 /** The access handler description (HC ptr). */
782 HCPTRTYPE(const char *) pszAccessHandler;
783#endif /* PGMPOOL_WITH_MONITORING */
784 /** The number of pages currently in use. */
785 uint16_t cUsedPages;
786#ifdef VBOX_WITH_STATISTICS
787 /** The high wather mark for cUsedPages. */
788 uint16_t cUsedPagesHigh;
789 uint32_t Alignment1; /**< Align the next member on a 64-bit boundrary. */
790 /** Profiling pgmPoolAlloc(). */
791 STAMPROFILEADV StatAlloc;
792 /** Profiling pgmPoolClearAll(). */
793 STAMPROFILE StatClearAll;
794 /** Profiling pgmPoolFlushAllInt(). */
795 STAMPROFILE StatFlushAllInt;
796 /** Profiling pgmPoolFlushPage(). */
797 STAMPROFILE StatFlushPage;
798 /** Profiling pgmPoolFree(). */
799 STAMPROFILE StatFree;
800 /** Profiling time spent zeroing pages. */
801 STAMPROFILE StatZeroPage;
802# ifdef PGMPOOL_WITH_USER_TRACKING
803 /** Profiling of pgmPoolTrackDeref. */
804 STAMPROFILE StatTrackDeref;
805 /** Profiling pgmTrackFlushGCPhysPT. */
806 STAMPROFILE StatTrackFlushGCPhysPT;
807 /** Profiling pgmTrackFlushGCPhysPTs. */
808 STAMPROFILE StatTrackFlushGCPhysPTs;
809 /** Profiling pgmTrackFlushGCPhysPTsSlow. */
810 STAMPROFILE StatTrackFlushGCPhysPTsSlow;
811 /** Number of times we've been out of user records. */
812 STAMCOUNTER StatTrackFreeUpOneUser;
813# endif
814# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
815 /** Profiling deref activity related tracking GC physical pages. */
816 STAMPROFILE StatTrackDerefGCPhys;
817 /** Number of linear searches for a HCPhys in the ram ranges. */
818 STAMCOUNTER StatTrackLinearRamSearches;
819 /** The number of failing pgmPoolTrackPhysExtAlloc calls. */
820 STAMCOUNTER StamTrackPhysExtAllocFailures;
821# endif
822# ifdef PGMPOOL_WITH_MONITORING
823 /** Profiling the GC PT access handler. */
824 STAMPROFILE StatMonitorGC;
825 /** Times we've failed interpreting the instruction. */
826 STAMCOUNTER StatMonitorGCEmulateInstr;
827 /** Profiling the pgmPoolFlushPage calls made from the GC PT access handler. */
828 STAMPROFILE StatMonitorGCFlushPage;
829 /** Times we've detected fork(). */
830 STAMCOUNTER StatMonitorGCFork;
831 /** Profiling the GC access we've handled (except REP STOSD). */
832 STAMPROFILE StatMonitorGCHandled;
833 /** Times we've failed interpreting a patch code instruction. */
834 STAMCOUNTER StatMonitorGCIntrFailPatch1;
835 /** Times we've failed interpreting a patch code instruction during flushing. */
836 STAMCOUNTER StatMonitorGCIntrFailPatch2;
837 /** The number of times we've seen rep prefixes we can't handle. */
838 STAMCOUNTER StatMonitorGCRepPrefix;
839 /** Profiling the REP STOSD cases we've handled. */
840 STAMPROFILE StatMonitorGCRepStosd;
841
842 /** Profiling the HC PT access handler. */
843 STAMPROFILE StatMonitorHC;
844 /** Times we've failed interpreting the instruction. */
845 STAMCOUNTER StatMonitorHCEmulateInstr;
846 /** Profiling the pgmPoolFlushPage calls made from the HC PT access handler. */
847 STAMPROFILE StatMonitorHCFlushPage;
848 /** Times we've detected fork(). */
849 STAMCOUNTER StatMonitorHCFork;
850 /** Profiling the HC access we've handled (except REP STOSD). */
851 STAMPROFILE StatMonitorHCHandled;
852 /** The number of times we've seen rep prefixes we can't handle. */
853 STAMCOUNTER StatMonitorHCRepPrefix;
854 /** Profiling the REP STOSD cases we've handled. */
855 STAMPROFILE StatMonitorHCRepStosd;
856 /** The number of times we're called in an async thread an need to flush. */
857 STAMCOUNTER StatMonitorHCAsync;
858 /** The high wather mark for cModifiedPages. */
859 uint16_t cModifiedPagesHigh;
860 uint16_t Alignment2[3]; /**< Align the next member on a 64-bit boundrary. */
861# endif
862# ifdef PGMPOOL_WITH_CACHE
863 /** The number of cache hits. */
864 STAMCOUNTER StatCacheHits;
865 /** The number of cache misses. */
866 STAMCOUNTER StatCacheMisses;
867 /** The number of times we've got a conflict of 'kind' in the cache. */
868 STAMCOUNTER StatCacheKindMismatches;
869 /** Number of times we've been out of pages. */
870 STAMCOUNTER StatCacheFreeUpOne;
871 /** The number of cacheable allocations. */
872 STAMCOUNTER StatCacheCacheable;
873 /** The number of uncacheable allocations. */
874 STAMCOUNTER StatCacheUncacheable;
875# endif
876#elif HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
877 uint32_t Alignment1; /**< Align the next member on a 64-bit boundrary. */
878#endif
879 /** The AVL tree for looking up a page by its HC physical address. */
880 AVLOHCPHYSTREE HCPhysTree;
881 uint32_t Alignment3; /**< Align the next member on a 64-bit boundrary. */
882 /** Array of pages. (cMaxPages in length)
883 * The Id is the index into thist array.
884 */
885 PGMPOOLPAGE aPages[PGMPOOL_IDX_FIRST];
886} PGMPOOL, *PPGMPOOL, **PPPGMPOOL;
887
888
889/** @def PGMPOOL_PAGE_2_PTR
890 * Maps a pool page pool into the current context.
891 *
892 * @returns VBox status code.
893 * @param pVM The VM handle.
894 * @param pPage The pool page.
895 *
896 * @remark In HC this uses PGMGCDynMapHCPage(), so it will consume of the
897 * small page window employeed by that function. Be careful.
898 * @remark There is no need to assert on the result.
899 */
900#ifdef IN_GC
901# define PGMPOOL_PAGE_2_PTR(pVM, pPage) pgmGCPoolMapPage((pVM), (pPage))
902#else
903# define PGMPOOL_PAGE_2_PTR(pVM, pPage) ((pPage)->pvPageHC)
904#endif
905
906
907/**
908 * Trees are using self relative offsets as pointers.
909 * So, all its data, including the root pointer, must be in the heap for HC and GC
910 * to have the same layout.
911 */
912typedef struct PGMTREES
913{
914 /** Physical access handlers (AVL range+offsetptr tree). */
915 AVLROGCPHYSTREE PhysHandlers;
916 /** Virtual access handlers (AVL range + GC ptr tree). */
917 AVLROGCPTRTREE VirtHandlers;
918 /** Virtual access handlers (Phys range AVL range + offsetptr tree). */
919 AVLROGCPHYSTREE PhysToVirtHandlers;
920 uint32_t auPadding[1];
921} PGMTREES;
922/** Pointer to PGM trees. */
923typedef PGMTREES *PPGMTREES;
924
925
926/** @name Paging mode macros
927 * @{ */
928#ifdef IN_GC
929# define PGM_CTX(a,b) a##GC##b
930# define PGM_CTX_STR(a,b) a "GC" b
931# define PGM_CTX_DECL(type) PGMGCDECL(type)
932#else
933# ifdef IN_RING3
934# define PGM_CTX(a,b) a##R3##b
935# define PGM_CTX_STR(a,b) a "R3" b
936# define PGM_CTX_DECL(type) DECLCALLBACK(type)
937# else
938# define PGM_CTX(a,b) a##R0##b
939# define PGM_CTX_STR(a,b) a "R0" b
940# define PGM_CTX_DECL(type) PGMDECL(type)
941# endif
942#endif
943
944#define PGM_GST_NAME_REAL(name) PGM_CTX(pgm,GstReal##name)
945#define PGM_GST_NAME_GC_REAL_STR(name) "pgmGCGstReal" #name
946#define PGM_GST_NAME_R0_REAL_STR(name) "pgmR0GstReal" #name
947#define PGM_GST_NAME_PROT(name) PGM_CTX(pgm,GstProt##name)
948#define PGM_GST_NAME_GC_PROT_STR(name) "pgmGCGstProt" #name
949#define PGM_GST_NAME_R0_PROT_STR(name) "pgmR0GstProt" #name
950#define PGM_GST_NAME_32BIT(name) PGM_CTX(pgm,Gst32Bit##name)
951#define PGM_GST_NAME_GC_32BIT_STR(name) "pgmGCGst32Bit" #name
952#define PGM_GST_NAME_R0_32BIT_STR(name) "pgmR0Gst32Bit" #name
953#define PGM_GST_NAME_PAE(name) PGM_CTX(pgm,GstPAE##name)
954#define PGM_GST_NAME_GC_PAE_STR(name) "pgmGCGstPAE" #name
955#define PGM_GST_NAME_R0_PAE_STR(name) "pgmR0GstPAE" #name
956#define PGM_GST_NAME_AMD64(name) PGM_CTX(pgm,GstAMD64##name)
957#define PGM_GST_NAME_GC_AMD64_STR(name) "pgmGCGstAMD64" #name
958#define PGM_GST_NAME_R0_AMD64_STR(name) "pgmR0GstAMD64" #name
959#define PGM_GST_PFN(name, pVM) ((pVM)->pgm.s.PGM_CTX(pfn,Gst##name))
960#define PGM_GST_DECL(type, name) PGM_CTX_DECL(type) PGM_GST_NAME(name)
961
962#define PGM_SHW_NAME_32BIT(name) PGM_CTX(pgm,Shw32Bit##name)
963#define PGM_SHW_NAME_GC_32BIT_STR(name) "pgmGCShw32Bit" #name
964#define PGM_SHW_NAME_R0_32BIT_STR(name) "pgmR0Shw32Bit" #name
965#define PGM_SHW_NAME_PAE(name) PGM_CTX(pgm,ShwPAE##name)
966#define PGM_SHW_NAME_GC_PAE_STR(name) "pgmGCShwPAE" #name
967#define PGM_SHW_NAME_R0_PAE_STR(name) "pgmR0ShwPAE" #name
968#define PGM_SHW_NAME_AMD64(name) PGM_CTX(pgm,ShwAMD64##name)
969#define PGM_SHW_NAME_GC_AMD64_STR(name) "pgmGCShwAMD64" #name
970#define PGM_SHW_NAME_R0_AMD64_STR(name) "pgmR0ShwAMD64" #name
971#define PGM_SHW_DECL(type, name) PGM_CTX_DECL(type) PGM_SHW_NAME(name)
972#define PGM_SHW_PFN(name, pVM) ((pVM)->pgm.s.PGM_CTX(pfn,Shw##name))
973
974/* Shw_Gst */
975#define PGM_BTH_NAME_32BIT_REAL(name) PGM_CTX(pgm,Bth32BitReal##name)
976#define PGM_BTH_NAME_32BIT_PROT(name) PGM_CTX(pgm,Bth32BitProt##name)
977#define PGM_BTH_NAME_32BIT_32BIT(name) PGM_CTX(pgm,Bth32Bit32Bit##name)
978#define PGM_BTH_NAME_PAE_REAL(name) PGM_CTX(pgm,BthPAEReal##name)
979#define PGM_BTH_NAME_PAE_PROT(name) PGM_CTX(pgm,BthPAEProt##name)
980#define PGM_BTH_NAME_PAE_32BIT(name) PGM_CTX(pgm,BthPAE32Bit##name)
981#define PGM_BTH_NAME_PAE_PAE(name) PGM_CTX(pgm,BthPAEPAE##name)
982#define PGM_BTH_NAME_AMD64_REAL(name) PGM_CTX(pgm,BthAMD64Real##name)
983#define PGM_BTH_NAME_AMD64_PROT(name) PGM_CTX(pgm,BthAMD64Prot##name)
984#define PGM_BTH_NAME_AMD64_AMD64(name) PGM_CTX(pgm,BthAMD64AMD64##name)
985#define PGM_BTH_NAME_GC_32BIT_REAL_STR(name) "pgmGCBth32BitReal" #name
986#define PGM_BTH_NAME_GC_32BIT_PROT_STR(name) "pgmGCBth32BitProt" #name
987#define PGM_BTH_NAME_GC_32BIT_32BIT_STR(name) "pgmGCBth32Bit32Bit" #name
988#define PGM_BTH_NAME_GC_PAE_REAL_STR(name) "pgmGCBthPAEReal" #name
989#define PGM_BTH_NAME_GC_PAE_PROT_STR(name) "pgmGCBthPAEProt" #name
990#define PGM_BTH_NAME_GC_PAE_32BIT_STR(name) "pgmGCBthPAE32Bit" #name
991#define PGM_BTH_NAME_GC_PAE_PAE_STR(name) "pgmGCBthPAEPAE" #name
992#define PGM_BTH_NAME_GC_AMD64_REAL_STR(name) "pgmGCBthAMD64Real" #name
993#define PGM_BTH_NAME_GC_AMD64_PROT_STR(name) "pgmGCBthAMD64Prot" #name
994#define PGM_BTH_NAME_GC_AMD64_AMD64_STR(name) "pgmGCBthAMD64AMD64" #name
995#define PGM_BTH_NAME_R0_32BIT_REAL_STR(name) "pgmR0Bth32BitReal" #name
996#define PGM_BTH_NAME_R0_32BIT_PROT_STR(name) "pgmR0Bth32BitProt" #name
997#define PGM_BTH_NAME_R0_32BIT_32BIT_STR(name) "pgmR0Bth32Bit32Bit" #name
998#define PGM_BTH_NAME_R0_PAE_REAL_STR(name) "pgmR0BthPAEReal" #name
999#define PGM_BTH_NAME_R0_PAE_PROT_STR(name) "pgmR0BthPAEProt" #name
1000#define PGM_BTH_NAME_R0_PAE_32BIT_STR(name) "pgmR0BthPAE32Bit" #name
1001#define PGM_BTH_NAME_R0_PAE_PAE_STR(name) "pgmR0BthPAEPAE" #name
1002#define PGM_BTH_NAME_R0_AMD64_REAL_STR(name) "pgmR0BthAMD64Real" #name
1003#define PGM_BTH_NAME_R0_AMD64_PROT_STR(name) "pgmR0BthAMD64Prot" #name
1004#define PGM_BTH_NAME_R0_AMD64_AMD64_STR(name) "pgmR0BthAMD64AMD64" #name
1005#define PGM_BTH_DECL(type, name) PGM_CTX_DECL(type) PGM_BTH_NAME(name)
1006#define PGM_BTH_PFN(name, pVM) ((pVM)->pgm.s.PGM_CTX(pfn,Bth##name))
1007/** @} */
1008
1009/**
1010 * Data for each paging mode.
1011 */
1012typedef struct PGMMODEDATA
1013{
1014 /** The guest mode type. */
1015 uint32_t uGstType;
1016 /** The shadow mode type. */
1017 uint32_t uShwType;
1018
1019 /** @name Function pointers for Shadow paging.
1020 * @{
1021 */
1022 DECLR3CALLBACKMEMBER(int, pfnR3ShwRelocate,(PVM pVM, RTGCUINTPTR offDelta));
1023 DECLR3CALLBACKMEMBER(int, pfnR3ShwExit,(PVM pVM));
1024 DECLR3CALLBACKMEMBER(int, pfnR3ShwGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
1025 DECLR3CALLBACKMEMBER(int, pfnR3ShwModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1026 DECLR3CALLBACKMEMBER(int, pfnR3ShwGetPDEByIndex,(PVM pVM, uint32_t iPD, PX86PDEPAE pPde));
1027 DECLR3CALLBACKMEMBER(int, pfnR3ShwSetPDEByIndex,(PVM pVM, uint32_t iPD, X86PDEPAE Pde));
1028 DECLR3CALLBACKMEMBER(int, pfnR3ShwModifyPDEByIndex,(PVM pVM, uint32_t iPD, uint64_t fFlags, uint64_t fMask));
1029
1030 DECLGCCALLBACKMEMBER(int, pfnGCShwGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
1031 DECLGCCALLBACKMEMBER(int, pfnGCShwModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1032 DECLGCCALLBACKMEMBER(int, pfnGCShwGetPDEByIndex,(PVM pVM, uint32_t iPD, PX86PDEPAE pPde));
1033 DECLGCCALLBACKMEMBER(int, pfnGCShwSetPDEByIndex,(PVM pVM, uint32_t iPD, X86PDEPAE Pde));
1034 DECLGCCALLBACKMEMBER(int, pfnGCShwModifyPDEByIndex,(PVM pVM, uint32_t iPD, uint64_t fFlags, uint64_t fMask));
1035
1036 DECLR0CALLBACKMEMBER(int, pfnR0ShwGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
1037 DECLR0CALLBACKMEMBER(int, pfnR0ShwModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1038 DECLR0CALLBACKMEMBER(int, pfnR0ShwGetPDEByIndex,(PVM pVM, uint32_t iPD, PX86PDEPAE pPde));
1039 DECLR0CALLBACKMEMBER(int, pfnR0ShwSetPDEByIndex,(PVM pVM, uint32_t iPD, X86PDEPAE Pde));
1040 DECLR0CALLBACKMEMBER(int, pfnR0ShwModifyPDEByIndex,(PVM pVM, uint32_t iPD, uint64_t fFlags, uint64_t fMask));
1041 /** @} */
1042
1043 /** @name Function pointers for Guest paging.
1044 * @{
1045 */
1046 DECLR3CALLBACKMEMBER(int, pfnR3GstRelocate,(PVM pVM, RTGCUINTPTR offDelta));
1047 DECLR3CALLBACKMEMBER(int, pfnR3GstExit,(PVM pVM));
1048 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
1049 DECLR3CALLBACKMEMBER(int, pfnR3GstModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1050 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPDE,(PVM pVM, RTGCUINTPTR GCPtr, PX86PDEPAE pPde));
1051 DECLR3CALLBACKMEMBER(int, pfnR3GstMonitorCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1052 DECLR3CALLBACKMEMBER(int, pfnR3GstUnmonitorCR3,(PVM pVM));
1053 DECLR3CALLBACKMEMBER(int, pfnR3GstMapCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1054 DECLR3CALLBACKMEMBER(int, pfnR3GstUnmapCR3,(PVM pVM));
1055 HCPTRTYPE(PFNPGMR3PHYSHANDLER) pfnHCGstWriteHandlerCR3;
1056 HCPTRTYPE(const char *) pszHCGstWriteHandlerCR3;
1057
1058 DECLGCCALLBACKMEMBER(int, pfnGCGstGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
1059 DECLGCCALLBACKMEMBER(int, pfnGCGstModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1060 DECLGCCALLBACKMEMBER(int, pfnGCGstGetPDE,(PVM pVM, RTGCUINTPTR GCPtr, PX86PDEPAE pPde));
1061 DECLGCCALLBACKMEMBER(int, pfnGCGstMonitorCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1062 DECLGCCALLBACKMEMBER(int, pfnGCGstUnmonitorCR3,(PVM pVM));
1063 DECLGCCALLBACKMEMBER(int, pfnGCGstMapCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1064 DECLGCCALLBACKMEMBER(int, pfnGCGstUnmapCR3,(PVM pVM));
1065 GCPTRTYPE(PFNPGMGCPHYSHANDLER) pfnGCGstWriteHandlerCR3;
1066
1067 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
1068 DECLR0CALLBACKMEMBER(int, pfnR0GstModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1069 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPDE,(PVM pVM, RTGCUINTPTR GCPtr, PX86PDEPAE pPde));
1070 DECLR0CALLBACKMEMBER(int, pfnR0GstMonitorCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1071 DECLR0CALLBACKMEMBER(int, pfnR0GstUnmonitorCR3,(PVM pVM));
1072 DECLR0CALLBACKMEMBER(int, pfnR0GstMapCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1073 DECLR0CALLBACKMEMBER(int, pfnR0GstUnmapCR3,(PVM pVM));
1074 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnR0GstWriteHandlerCR3;
1075 /** @} */
1076
1077 /** @name Function pointers for Both Shadow and Guest paging.
1078 * @{
1079 */
1080 DECLR3CALLBACKMEMBER(int, pfnR3BthRelocate,(PVM pVM, RTGCUINTPTR offDelta));
1081 DECLR3CALLBACKMEMBER(int, pfnR3BthTrap0eHandler,(PVM pVM, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
1082 DECLR3CALLBACKMEMBER(int, pfnR3BthInvalidatePage,(PVM pVM, RTGCPTR GCPtrPage));
1083 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncCR3,(PVM pVM, uint32_t cr0, uint32_t cr3, uint32_t cr4, bool fGlobal));
1084 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncPage,(PVM pVM, VBOXPDE PdeSrc, RTGCUINTPTR GCPtrPage, unsigned cPages, unsigned uError));
1085 DECLR3CALLBACKMEMBER(int, pfnR3BthPrefetchPage,(PVM pVM, RTGCUINTPTR GCPtrPage));
1086 DECLR3CALLBACKMEMBER(int, pfnR3BthVerifyAccessSyncPage,(PVM pVM, RTGCUINTPTR GCPtrPage, unsigned fFlags, unsigned uError));
1087#ifdef VBOX_STRICT
1088 DECLR3CALLBACKMEMBER(unsigned, pfnR3BthAssertCR3,(PVM pVM, uint32_t cr3, uint32_t cr4, RTGCUINTPTR GCPtr, RTGCUINTPTR cb));
1089#endif
1090
1091 DECLGCCALLBACKMEMBER(int, pfnGCBthTrap0eHandler,(PVM pVM, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
1092 DECLGCCALLBACKMEMBER(int, pfnGCBthInvalidatePage,(PVM pVM, RTGCPTR GCPtrPage));
1093 DECLGCCALLBACKMEMBER(int, pfnGCBthSyncCR3,(PVM pVM, uint32_t cr0, uint32_t cr3, uint32_t cr4, bool fGlobal));
1094 DECLGCCALLBACKMEMBER(int, pfnGCBthSyncPage,(PVM pVM, VBOXPDE PdeSrc, RTGCUINTPTR GCPtrPage, unsigned cPages, unsigned uError));
1095 DECLGCCALLBACKMEMBER(int, pfnGCBthPrefetchPage,(PVM pVM, RTGCUINTPTR GCPtrPage));
1096 DECLGCCALLBACKMEMBER(int, pfnGCBthVerifyAccessSyncPage,(PVM pVM, RTGCUINTPTR GCPtrPage, unsigned fFlags, unsigned uError));
1097#ifdef VBOX_STRICT
1098 DECLGCCALLBACKMEMBER(unsigned, pfnGCBthAssertCR3,(PVM pVM, uint32_t cr3, uint32_t cr4, RTGCUINTPTR GCPtr, RTGCUINTPTR cb));
1099#endif
1100
1101 DECLR0CALLBACKMEMBER(int, pfnR0BthTrap0eHandler,(PVM pVM, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
1102 DECLR0CALLBACKMEMBER(int, pfnR0BthInvalidatePage,(PVM pVM, RTGCPTR GCPtrPage));
1103 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncCR3,(PVM pVM, uint32_t cr0, uint32_t cr3, uint32_t cr4, bool fGlobal));
1104 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncPage,(PVM pVM, VBOXPDE PdeSrc, RTGCUINTPTR GCPtrPage, unsigned cPages, unsigned uError));
1105 DECLR0CALLBACKMEMBER(int, pfnR0BthPrefetchPage,(PVM pVM, RTGCUINTPTR GCPtrPage));
1106 DECLR0CALLBACKMEMBER(int, pfnR0BthVerifyAccessSyncPage,(PVM pVM, RTGCUINTPTR GCPtrPage, unsigned fFlags, unsigned uError));
1107#ifdef VBOX_STRICT
1108 DECLR0CALLBACKMEMBER(unsigned, pfnR0BthAssertCR3,(PVM pVM, uint32_t cr3, uint32_t cr4, RTGCUINTPTR GCPtr, RTGCUINTPTR cb));
1109#endif
1110 /** @} */
1111} PGMMODEDATA, *PPGMMODEDATA;
1112
1113
1114
1115/**
1116 * Converts a PGM pointer into a VM pointer.
1117 * @returns Pointer to the VM structure the PGM is part of.
1118 * @param pPGM Pointer to PGM instance data.
1119 */
1120#define PGM2VM(pPGM) ( (PVM)((char*)pPGM - pPGM->offVM) )
1121
1122/**
1123 * PGM Data (part of VM)
1124 */
1125typedef struct PGM
1126{
1127 /** Offset to the VM structure. */
1128 RTINT offVM;
1129
1130 /*
1131 * This will be redefined at least two more times before we're done, I'm sure.
1132 * The current code is only to get on with the coding.
1133 * - 2004-06-10: initial version, bird.
1134 * - 2004-07-02: 1st time, bird.
1135 * - 2004-10-18: 2nd time, bird.
1136 * - 2005-07-xx: 3rd time, bird.
1137 */
1138
1139 /** Pointer to the page table entries for the dynamic page mapping area - GCPtr. */
1140 GCPTRTYPE(PX86PTE) paDynPageMap32BitPTEsGC;
1141 /** Pointer to the page table entries for the dynamic page mapping area - GCPtr. */
1142 GCPTRTYPE(PX86PTEPAE) paDynPageMapPaePTEsGC;
1143
1144 /** The host paging mode. (This is what SUPLib reports.) */
1145 SUPPAGINGMODE enmHostMode;
1146 /** The shadow paging mode. */
1147 PGMMODE enmShadowMode;
1148 /** The guest paging mode. */
1149 PGMMODE enmGuestMode;
1150
1151 /** The current physical address representing in the guest CR3 register. */
1152 RTGCPHYS GCPhysCR3;
1153 /** Pointer to the 5 page CR3 content mapping.
1154 * The first page is always the CR3 (in some form) while the 4 other pages
1155 * are used of the PDs in PAE mode. */
1156 RTGCPTR GCPtrCR3Mapping;
1157 /** The physical address of the currently monitored guest CR3 page.
1158 * When this value is NIL_RTGCPHYS no page is being monitored. */
1159 RTGCPHYS GCPhysGstCR3Monitored;
1160#if HC_ARCH_BITS == 64 || GC_ARCH_BITS == 64
1161 RTGCPHYS GCPhysPadding0; /**< alignment padding. */
1162#endif
1163
1164 /** @name 32-bit Guest Paging.
1165 * @{ */
1166 /** The guest's page directory, HC pointer. */
1167 HCPTRTYPE(PVBOXPD) pGuestPDHC;
1168 /** The guest's page directory, static GC mapping. */
1169 GCPTRTYPE(PVBOXPD) pGuestPDGC;
1170 /** @} */
1171
1172 /** @name PAE Guest Paging.
1173 * @{ */
1174 /** The guest's page directory pointer table, static GC mapping. */
1175 GCPTRTYPE(PX86PDPTR) pGstPaePDPTRGC;
1176 /** The guest's page directory pointer table, HC pointer. */
1177 HCPTRTYPE(PX86PDPTR) pGstPaePDPTRHC;
1178 /** The guest's page directories, HC pointers.
1179 * These are individual pointers and doesn't have to be adjecent.
1180 * These doesn't have to be update to date - use pgmGstGetPaePD() to access them. */
1181 HCPTRTYPE(PX86PDPAE) apGstPaePDsHC[4];
1182 /** The guest's page directories, static GC mapping.
1183 * Unlike the HC array the first entry can be accessed as a 2048 entry PD.
1184 * These doesn't have to be update to date - use pgmGstGetPaePD() to access them. */
1185 GCPTRTYPE(PX86PDPAE) apGstPaePDsGC[4];
1186 /** The physical addresses of the guest page directories (PAE) pointed to by apGstPagePDsHC/GC. */
1187 RTGCPHYS aGCPhysGstPaePDs[4];
1188 /** The physical addresses of the monitored guest page directories (PAE). */
1189 RTGCPHYS aGCPhysGstPaePDsMonitored[4];
1190 /** @} */
1191
1192
1193 /** @name 32-bit Shadow Paging
1194 * @{ */
1195 /** The 32-Bit PD - HC Ptr. */
1196 HCPTRTYPE(PX86PD) pHC32BitPD;
1197 /** The 32-Bit PD - GC Ptr. */
1198 GCPTRTYPE(PX86PD) pGC32BitPD;
1199#if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
1200 uint32_t u32Padding1; /**< alignment padding. */
1201#endif
1202 /** The Physical Address (HC) of the 32-Bit PD. */
1203 RTHCPHYS HCPhys32BitPD;
1204 /** @} */
1205
1206 /** @name PAE Shadow Paging
1207 * @{ */
1208 /** The four PDs for the low 4GB - HC Ptr.
1209 * Even though these are 4 pointers, what they point at is a single table.
1210 * Thus, it's possible to walk the 2048 entries starting where apHCPaePDs[0] points. */
1211 HCPTRTYPE(PX86PDPAE) apHCPaePDs[4];
1212 /** The four PDs for the low 4GB - GC Ptr.
1213 * Same kind of mapping as apHCPaePDs. */
1214 GCPTRTYPE(PX86PDPAE) apGCPaePDs[4];
1215 /** The Physical Address (HC) of the four PDs for the low 4GB.
1216 * These are *NOT* 4 contiguous pages. */
1217 RTHCPHYS aHCPhysPaePDs[4];
1218 /** The PAE PDPTR - HC Ptr. */
1219 HCPTRTYPE(PX86PDPTR) pHCPaePDPTR;
1220 /** The Physical Address (HC) of the PAE PDPTR. */
1221 RTHCPHYS HCPhysPaePDPTR;
1222 /** The PAE PDPTR - GC Ptr. */
1223 GCPTRTYPE(PX86PDPTR) pGCPaePDPTR;
1224 /** @} */
1225
1226 /** @name AMD64 Shadow Paging
1227 * Extends PAE Paging.
1228 * @{ */
1229 /** The Page Map Level 4 table - HC Ptr. */
1230 GCPTRTYPE(PX86PML4) pGCPaePML4;
1231 /** The Page Map Level 4 table - GC Ptr. */
1232 HCPTRTYPE(PX86PML4) pHCPaePML4;
1233 /** The Physical Address (HC) of the Page Map Level 4 table. */
1234 RTHCPHYS HCPhysPaePML4;
1235 /** @}*/
1236
1237 /** @name Function pointers for Shadow paging.
1238 * @{
1239 */
1240 DECLR3CALLBACKMEMBER(int, pfnR3ShwRelocate,(PVM pVM, RTGCUINTPTR offDelta));
1241 DECLR3CALLBACKMEMBER(int, pfnR3ShwExit,(PVM pVM));
1242 DECLR3CALLBACKMEMBER(int, pfnR3ShwGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
1243 DECLR3CALLBACKMEMBER(int, pfnR3ShwModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1244 DECLR3CALLBACKMEMBER(int, pfnR3ShwGetPDEByIndex,(PVM pVM, uint32_t iPD, PX86PDEPAE pPde));
1245 DECLR3CALLBACKMEMBER(int, pfnR3ShwSetPDEByIndex,(PVM pVM, uint32_t iPD, X86PDEPAE Pde));
1246 DECLR3CALLBACKMEMBER(int, pfnR3ShwModifyPDEByIndex,(PVM pVM, uint32_t iPD, uint64_t fFlags, uint64_t fMask));
1247
1248 DECLGCCALLBACKMEMBER(int, pfnGCShwGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
1249 DECLGCCALLBACKMEMBER(int, pfnGCShwModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1250 DECLGCCALLBACKMEMBER(int, pfnGCShwGetPDEByIndex,(PVM pVM, uint32_t iPD, PX86PDEPAE pPde));
1251 DECLGCCALLBACKMEMBER(int, pfnGCShwSetPDEByIndex,(PVM pVM, uint32_t iPD, X86PDEPAE Pde));
1252 DECLGCCALLBACKMEMBER(int, pfnGCShwModifyPDEByIndex,(PVM pVM, uint32_t iPD, uint64_t fFlags, uint64_t fMask));
1253#if GC_ARCH_BITS == 32 && HC_ARCH_BITS == 64
1254 RTGCPTR alignment0; /**< structure size alignment. */
1255#endif
1256
1257 DECLR0CALLBACKMEMBER(int, pfnR0ShwGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTHCPHYS pHCPhys));
1258 DECLR0CALLBACKMEMBER(int, pfnR0ShwModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1259 DECLR0CALLBACKMEMBER(int, pfnR0ShwGetPDEByIndex,(PVM pVM, uint32_t iPD, PX86PDEPAE pPde));
1260 DECLR0CALLBACKMEMBER(int, pfnR0ShwSetPDEByIndex,(PVM pVM, uint32_t iPD, X86PDEPAE Pde));
1261 DECLR0CALLBACKMEMBER(int, pfnR0ShwModifyPDEByIndex,(PVM pVM, uint32_t iPD, uint64_t fFlags, uint64_t fMask));
1262
1263 /** @} */
1264
1265 /** @name Function pointers for Guest paging.
1266 * @{
1267 */
1268 DECLR3CALLBACKMEMBER(int, pfnR3GstRelocate,(PVM pVM, RTGCUINTPTR offDelta));
1269 DECLR3CALLBACKMEMBER(int, pfnR3GstExit,(PVM pVM));
1270 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
1271 DECLR3CALLBACKMEMBER(int, pfnR3GstModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1272 DECLR3CALLBACKMEMBER(int, pfnR3GstGetPDE,(PVM pVM, RTGCUINTPTR GCPtr, PX86PDEPAE pPde));
1273 DECLR3CALLBACKMEMBER(int, pfnR3GstMonitorCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1274 DECLR3CALLBACKMEMBER(int, pfnR3GstUnmonitorCR3,(PVM pVM));
1275 DECLR3CALLBACKMEMBER(int, pfnR3GstMapCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1276 DECLR3CALLBACKMEMBER(int, pfnR3GstUnmapCR3,(PVM pVM));
1277 HCPTRTYPE(PFNPGMR3PHYSHANDLER) pfnHCGstWriteHandlerCR3;
1278 HCPTRTYPE(const char *) pszHCGstWriteHandlerCR3;
1279
1280 DECLGCCALLBACKMEMBER(int, pfnGCGstGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
1281 DECLGCCALLBACKMEMBER(int, pfnGCGstModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1282 DECLGCCALLBACKMEMBER(int, pfnGCGstGetPDE,(PVM pVM, RTGCUINTPTR GCPtr, PX86PDEPAE pPde));
1283 DECLGCCALLBACKMEMBER(int, pfnGCGstMonitorCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1284 DECLGCCALLBACKMEMBER(int, pfnGCGstUnmonitorCR3,(PVM pVM));
1285 DECLGCCALLBACKMEMBER(int, pfnGCGstMapCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1286 DECLGCCALLBACKMEMBER(int, pfnGCGstUnmapCR3,(PVM pVM));
1287 GCPTRTYPE(PFNPGMGCPHYSHANDLER) pfnGCGstWriteHandlerCR3;
1288
1289 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPage,(PVM pVM, RTGCUINTPTR GCPtr, uint64_t *pfFlags, PRTGCPHYS pGCPhys));
1290 DECLR0CALLBACKMEMBER(int, pfnR0GstModifyPage,(PVM pVM, RTGCUINTPTR GCPtr, size_t cbPages, uint64_t fFlags, uint64_t fMask));
1291 DECLR0CALLBACKMEMBER(int, pfnR0GstGetPDE,(PVM pVM, RTGCUINTPTR GCPtr, PX86PDEPAE pPde));
1292 DECLR0CALLBACKMEMBER(int, pfnR0GstMonitorCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1293 DECLR0CALLBACKMEMBER(int, pfnR0GstUnmonitorCR3,(PVM pVM));
1294 DECLR0CALLBACKMEMBER(int, pfnR0GstMapCR3,(PVM pVM, RTGCPHYS GCPhysCR3));
1295 DECLR0CALLBACKMEMBER(int, pfnR0GstUnmapCR3,(PVM pVM));
1296 R0PTRTYPE(PFNPGMR0PHYSHANDLER) pfnR0GstWriteHandlerCR3;
1297 /** @} */
1298
1299 /** @name Function pointers for Both Shadow and Guest paging.
1300 * @{
1301 */
1302 DECLR3CALLBACKMEMBER(int, pfnR3BthRelocate,(PVM pVM, RTGCUINTPTR offDelta));
1303 DECLR3CALLBACKMEMBER(int, pfnR3BthTrap0eHandler,(PVM pVM, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
1304 DECLR3CALLBACKMEMBER(int, pfnR3BthInvalidatePage,(PVM pVM, RTGCPTR GCPtrPage));
1305 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncCR3,(PVM pVM, uint32_t cr0, uint32_t cr3, uint32_t cr4, bool fGlobal));
1306 DECLR3CALLBACKMEMBER(int, pfnR3BthSyncPage,(PVM pVM, VBOXPDE PdeSrc, RTGCUINTPTR GCPtrPage, unsigned cPages, unsigned uError));
1307 DECLR3CALLBACKMEMBER(int, pfnR3BthPrefetchPage,(PVM pVM, RTGCUINTPTR GCPtrPage));
1308 DECLR3CALLBACKMEMBER(int, pfnR3BthVerifyAccessSyncPage,(PVM pVM, RTGCUINTPTR GCPtrPage, unsigned fFlags, unsigned uError));
1309 DECLR3CALLBACKMEMBER(unsigned, pfnR3BthAssertCR3,(PVM pVM, uint32_t cr3, uint32_t cr4, RTGCUINTPTR GCPtr, RTGCUINTPTR cb));
1310
1311 DECLR0CALLBACKMEMBER(int, pfnR0BthTrap0eHandler,(PVM pVM, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
1312 DECLR0CALLBACKMEMBER(int, pfnR0BthInvalidatePage,(PVM pVM, RTGCPTR GCPtrPage));
1313 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncCR3,(PVM pVM, uint32_t cr0, uint32_t cr3, uint32_t cr4, bool fGlobal));
1314 DECLR0CALLBACKMEMBER(int, pfnR0BthSyncPage,(PVM pVM, VBOXPDE PdeSrc, RTGCUINTPTR GCPtrPage, unsigned cPages, unsigned uError));
1315 DECLR0CALLBACKMEMBER(int, pfnR0BthPrefetchPage,(PVM pVM, RTGCUINTPTR GCPtrPage));
1316 DECLR0CALLBACKMEMBER(int, pfnR0BthVerifyAccessSyncPage,(PVM pVM, RTGCUINTPTR GCPtrPage, unsigned fFlags, unsigned uError));
1317 DECLR0CALLBACKMEMBER(unsigned, pfnR0BthAssertCR3,(PVM pVM, uint32_t cr3, uint32_t cr4, RTGCUINTPTR GCPtr, RTGCUINTPTR cb));
1318
1319 DECLGCCALLBACKMEMBER(int, pfnGCBthTrap0eHandler,(PVM pVM, RTGCUINT uErr, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault));
1320 DECLGCCALLBACKMEMBER(int, pfnGCBthInvalidatePage,(PVM pVM, RTGCPTR GCPtrPage));
1321 DECLGCCALLBACKMEMBER(int, pfnGCBthSyncCR3,(PVM pVM, uint32_t cr0, uint32_t cr3, uint32_t cr4, bool fGlobal));
1322 DECLGCCALLBACKMEMBER(int, pfnGCBthSyncPage,(PVM pVM, VBOXPDE PdeSrc, RTGCUINTPTR GCPtrPage, unsigned cPages, unsigned uError));
1323 DECLGCCALLBACKMEMBER(int, pfnGCBthPrefetchPage,(PVM pVM, RTGCUINTPTR GCPtrPage));
1324 DECLGCCALLBACKMEMBER(int, pfnGCBthVerifyAccessSyncPage,(PVM pVM, RTGCUINTPTR GCPtrPage, unsigned fFlags, unsigned uError));
1325 DECLGCCALLBACKMEMBER(unsigned, pfnGCBthAssertCR3,(PVM pVM, uint32_t cr3, uint32_t cr4, RTGCUINTPTR GCPtr, RTGCUINTPTR cb));
1326#if GC_ARCH_BITS == 32 && HC_ARCH_BITS == 64
1327 RTGCPTR alignment2; /**< structure size alignment. */
1328#endif
1329 /** @} */
1330
1331 /** Pointer to SHW+GST mode data (function pointers).
1332 * The index into this table is made up from */
1333 R3PTRTYPE(PPGMMODEDATA) paModeData;
1334
1335
1336 /** Pointer to the list of RAM ranges (Phys GC -> Phys HC conversion) - for HC.
1337 * This is sorted by physical address and contains no overlaps.
1338 * The memory locks and other conversions are managed by MM at the moment.
1339 */
1340 HCPTRTYPE(PPGMRAMRANGE) pRamRangesHC;
1341 /** Pointer to the list of RAM ranges (Phys GC -> Phys HC conversion) - for GC.
1342 * This is sorted by physical address and contains no overlaps.
1343 * The memory locks and other conversions are managed by MM at the moment.
1344 */
1345 GCPTRTYPE(PPGMRAMRANGE) pRamRangesGC;
1346 /** The configured RAM size. */
1347 RTUINT cbRamSize;
1348
1349 /** PGM offset based trees - HC Ptr. */
1350 HCPTRTYPE(PPGMTREES) pTreesHC;
1351 /** PGM offset based trees - GC Ptr. */
1352 GCPTRTYPE(PPGMTREES) pTreesGC;
1353
1354 /** Linked list of GC mappings - for GC.
1355 * The list is sorted ascending on address.
1356 */
1357 GCPTRTYPE(PPGMMAPPING) pMappingsGC;
1358 /** Linked list of GC mappings - for HC.
1359 * The list is sorted ascending on address.
1360 */
1361 HCPTRTYPE(PPGMMAPPING) pMappingsHC;
1362
1363 /** If set no conflict checks are required. (boolean) */
1364 bool fMappingsFixed;
1365 /** If set, then no mappings are put into the shadow page table. (boolean) */
1366 bool fDisableMappings;
1367 /** Size of fixed mapping */
1368 uint32_t cbMappingFixed;
1369 /** Base address (GC) of fixed mapping */
1370 RTGCPTR GCPtrMappingFixed;
1371#if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
1372 uint32_t u32Padding0; /**< alignment padding. */
1373#endif
1374
1375
1376 /** @name Intermediate Context
1377 * @{ */
1378 /** Pointer to the intermediate page directory - Normal. */
1379 HCPTRTYPE(PX86PD) pInterPD;
1380 /** Pointer to the intermedate page tables - Normal.
1381 * There are two page tables, one for the identity mapping and one for
1382 * the host context mapping (of the core code). */
1383 HCPTRTYPE(PX86PT) apInterPTs[2];
1384 /** Pointer to the intermedate page tables - PAE. */
1385 HCPTRTYPE(PX86PTPAE) apInterPaePTs[2];
1386 /** Pointer to the intermedate page directory - PAE. */
1387 HCPTRTYPE(PX86PDPAE) apInterPaePDs[4];
1388 /** Pointer to the intermedate page directory - PAE. */
1389 HCPTRTYPE(PX86PDPTR) pInterPaePDPTR;
1390 /** Pointer to the intermedate page-map level 4 - AMD64. */
1391 HCPTRTYPE(PX86PML4) pInterPaePML4;
1392 /** Pointer to the intermedate page directory - AMD64. */
1393 HCPTRTYPE(PX86PDPTR) pInterPaePDPTR64;
1394 /** The Physical Address (HC) of the intermediate Page Directory - Normal. */
1395 RTHCPHYS HCPhysInterPD;
1396 /** The Physical Address (HC) of the intermediate Page Directory Pointer Table - PAE. */
1397 RTHCPHYS HCPhysInterPaePDPTR;
1398 /** The Physical Address (HC) of the intermediate Page Map Level 4 table - AMD64. */
1399 RTHCPHYS HCPhysInterPaePML4;
1400 /** @} */
1401
1402 /** Base address of the dynamic page mapping area.
1403 * The array is MM_HYPER_DYNAMIC_SIZE bytes big.
1404 */
1405 GCPTRTYPE(uint8_t *) pbDynPageMapBaseGC;
1406 /** The index of the last entry used in the dynamic page mapping area. */
1407 RTUINT iDynPageMapLast;
1408 /** Cache containing the last entries in the dynamic page mapping area.
1409 * The cache size is covering half of the mapping area. */
1410 RTHCPHYS aHCPhysDynPageMapCache[MM_HYPER_DYNAMIC_SIZE >> (PAGE_SHIFT + 1)];
1411
1412 /** A20 gate mask.
1413 * Our current approach to A20 emulation is to let REM do it and don't bother
1414 * anywhere else. The interesting Guests will be operating with it enabled anyway.
1415 * But whould need arrise, we'll subject physical addresses to this mask. */
1416 RTGCPHYS GCPhysA20Mask;
1417 /** A20 gate state - boolean! */
1418 RTUINT fA20Enabled;
1419
1420 /** What needs syncing (PGM_SYNC_*).
1421 * This is used to queue operations for PGMSyncCR3, PGMInvalidatePage,
1422 * PGMFlushTLB, and PGMR3Load. */
1423 RTUINT fSyncFlags;
1424
1425#if HC_ARCH_BITS == 64 && GC_ARCH_BITS == 32
1426 RTUINT uPadding3; /**< alignment padding. */
1427#endif
1428 /** PGM critical section.
1429 * This protects the physical & virtual access handlers, ram ranges,
1430 * and the page flag updating (some of it anyway).
1431 */
1432 PDMCRITSECT CritSect;
1433
1434 /** Shadow Page Pool - HC Ptr. */
1435 HCPTRTYPE(PPGMPOOL) pPoolHC;
1436 /** Shadow Page Pool - GC Ptr. */
1437 GCPTRTYPE(PPGMPOOL) pPoolGC;
1438
1439 /** Flush the cache on the next access. */
1440 bool fPhysCacheFlushPending;
1441/** @todo r=bird: Fix member names!*/
1442 /** PGMPhysRead cache */
1443 PGMPHYSCACHE pgmphysreadcache;
1444 /** PGMPhysWrite cache */
1445 PGMPHYSCACHE pgmphyswritecache;
1446
1447 /** @name Release Statistics
1448 * @{ */
1449 /** The number of times the guest has switched mode since last reset or statistics reset. */
1450 STAMCOUNTER cGuestModeChanges;
1451 /** @} */
1452
1453#ifdef VBOX_WITH_STATISTICS
1454 /** GC: Which statistic this \#PF should be attributed to. */
1455 GCPTRTYPE(PSTAMPROFILE) pStatTrap0eAttributionGC;
1456 RTGCPTR padding0;
1457 /** HC: Which statistic this \#PF should be attributed to. */
1458 HCPTRTYPE(PSTAMPROFILE) pStatTrap0eAttributionHC;
1459 RTHCPTR padding1;
1460 STAMPROFILE StatGCTrap0e; /**< GC: PGMGCTrap0eHandler() profiling. */
1461 STAMPROFILE StatTrap0eCSAM; /**< Profiling of the Trap0eHandler body when the cause is CSAM. */
1462 STAMPROFILE StatTrap0eDirtyAndAccessedBits; /**< Profiling of the Trap0eHandler body when the cause is dirty and/or accessed bit emulation. */
1463 STAMPROFILE StatTrap0eGuestTrap; /**< Profiling of the Trap0eHandler body when the cause is a guest trap. */
1464 STAMPROFILE StatTrap0eHndPhys; /**< Profiling of the Trap0eHandler body when the cause is a physical handler. */
1465 STAMPROFILE StatTrap0eHndVirt; /**< Profiling of the Trap0eHandler body when the cause is a virtual handler. */
1466 STAMPROFILE StatTrap0eHndUnhandled; /**< Profiling of the Trap0eHandler body when the cause is access outside the monitored areas of a monitored page. */
1467 STAMPROFILE StatTrap0eMisc; /**< Profiling of the Trap0eHandler body when the cause is not known. */
1468 STAMPROFILE StatTrap0eOutOfSync; /**< Profiling of the Trap0eHandler body when the cause is an out-of-sync page. */
1469 STAMPROFILE StatTrap0eOutOfSyncHndPhys; /**< Profiling of the Trap0eHandler body when the cause is an out-of-sync physical handler page. */
1470 STAMPROFILE StatTrap0eOutOfSyncHndVirt; /**< Profiling of the Trap0eHandler body when the cause is an out-of-sync virtual handler page. */
1471 STAMPROFILE StatTrap0eOutOfSyncObsHnd; /**< Profiling of the Trap0eHandler body when the cause is an obsolete handler page. */
1472 STAMPROFILE StatTrap0eSyncPT; /**< Profiling of the Trap0eHandler body when the cause is lazy syncing of a PT. */
1473
1474 STAMCOUNTER StatTrap0eMapHandler; /**< Number of traps due to access handlers in mappings. */
1475 STAMCOUNTER StatGCTrap0eConflicts; /**< GC: The number of times \#PF was caused by an undetected conflict. */
1476
1477 STAMCOUNTER StatGCTrap0eUSNotPresentRead;
1478 STAMCOUNTER StatGCTrap0eUSNotPresentWrite;
1479 STAMCOUNTER StatGCTrap0eUSWrite;
1480 STAMCOUNTER StatGCTrap0eUSReserved;
1481 STAMCOUNTER StatGCTrap0eUSRead;
1482
1483 STAMCOUNTER StatGCTrap0eSVNotPresentRead;
1484 STAMCOUNTER StatGCTrap0eSVNotPresentWrite;
1485 STAMCOUNTER StatGCTrap0eSVWrite;
1486 STAMCOUNTER StatGCTrap0eSVReserved;
1487
1488 STAMCOUNTER StatGCTrap0eUnhandled;
1489 STAMCOUNTER StatGCTrap0eMap;
1490
1491 /** GC: PGMSyncPT() profiling. */
1492 STAMPROFILE StatGCSyncPT;
1493 /** GC: The number of times PGMSyncPT() needed to allocate page tables. */
1494 STAMCOUNTER StatGCSyncPTAlloc;
1495 /** GC: The number of times PGMSyncPT() detected conflicts. */
1496 STAMCOUNTER StatGCSyncPTConflict;
1497 /** GC: The number of times PGMSyncPT() failed. */
1498 STAMCOUNTER StatGCSyncPTFailed;
1499 /** GC: PGMGCInvalidatePage() profiling. */
1500 STAMPROFILE StatGCInvalidatePage;
1501 /** GC: The number of times PGMGCInvalidatePage() was called for a 4KB page. */
1502 STAMCOUNTER StatGCInvalidatePage4KBPages;
1503 /** GC: The number of times PGMGCInvalidatePage() was called for a 4MB page. */
1504 STAMCOUNTER StatGCInvalidatePage4MBPages;
1505 /** GC: The number of times PGMGCInvalidatePage() skipped a 4MB page. */
1506 STAMCOUNTER StatGCInvalidatePage4MBPagesSkip;
1507 /** GC: The number of times PGMGCInvalidatePage() was called for a not accessed page directory. */
1508 STAMCOUNTER StatGCInvalidatePagePDNAs;
1509 /** GC: The number of times PGMGCInvalidatePage() was called for a not present page directory. */
1510 STAMCOUNTER StatGCInvalidatePagePDNPs;
1511 /** GC: The number of times PGMGCInvalidatePage() was called for a page directory containing mappings (no conflict). */
1512 STAMCOUNTER StatGCInvalidatePagePDMappings;
1513 /** GC: The number of times PGMGCInvalidatePage() was called for an out of sync page directory. */
1514 STAMCOUNTER StatGCInvalidatePagePDOutOfSync;
1515 /** HC: The number of times PGMGCInvalidatePage() was skipped due to not present shw or pending pending SyncCR3. */
1516 STAMCOUNTER StatGCInvalidatePageSkipped;
1517 /** GC: The number of times user page is out of sync was detected in GC. */
1518 STAMCOUNTER StatGCPageOutOfSyncUser;
1519 /** GC: The number of times supervisor page is out of sync was detected in GC. */
1520 STAMCOUNTER StatGCPageOutOfSyncSupervisor;
1521 /** GC: The number of dynamic page mapping cache hits */
1522 STAMCOUNTER StatDynMapCacheMisses;
1523 /** GC: The number of dynamic page mapping cache misses */
1524 STAMCOUNTER StatDynMapCacheHits;
1525 /** GC: The number of times pgmGCGuestPDWriteHandler() was successfully called. */
1526 STAMCOUNTER StatGCGuestCR3WriteHandled;
1527 /** GC: The number of times pgmGCGuestPDWriteHandler() was called and we had to fall back to the recompiler. */
1528 STAMCOUNTER StatGCGuestCR3WriteUnhandled;
1529 /** GC: The number of times pgmGCGuestPDWriteHandler() was called and a conflict was detected. */
1530 STAMCOUNTER StatGCGuestCR3WriteConflict;
1531 /** GC: Number of out-of-sync handled pages. */
1532 STAMCOUNTER StatHandlersOutOfSync;
1533 /** GC: Number of traps due to physical access handlers. */
1534 STAMCOUNTER StatHandlersPhysical;
1535 /** GC: Number of traps due to virtual access handlers. */
1536 STAMCOUNTER StatHandlersVirtual;
1537 /** GC: Number of traps due to virtual access handlers found by physical address. */
1538 STAMCOUNTER StatHandlersVirtualByPhys;
1539 /** GC: Number of traps due to virtual access handlers found by virtual address (without proper physical flags). */
1540 STAMCOUNTER StatHandlersVirtualUnmarked;
1541 /** GC: Number of traps due to access outside range of monitored page(s). */
1542 STAMCOUNTER StatHandlersUnhandled;
1543
1544 /** GC: The number of times pgmGCGuestROMWriteHandler() was successfully called. */
1545 STAMCOUNTER StatGCGuestROMWriteHandled;
1546 /** GC: The number of times pgmGCGuestROMWriteHandler() was called and we had to fall back to the recompiler */
1547 STAMCOUNTER StatGCGuestROMWriteUnhandled;
1548
1549 /** HC: PGMR3InvalidatePage() profiling. */
1550 STAMPROFILE StatHCInvalidatePage;
1551 /** HC: The number of times PGMR3InvalidatePage() was called for a 4KB page. */
1552 STAMCOUNTER StatHCInvalidatePage4KBPages;
1553 /** HC: The number of times PGMR3InvalidatePage() was called for a 4MB page. */
1554 STAMCOUNTER StatHCInvalidatePage4MBPages;
1555 /** HC: The number of times PGMR3InvalidatePage() skipped a 4MB page. */
1556 STAMCOUNTER StatHCInvalidatePage4MBPagesSkip;
1557 /** HC: The number of times PGMR3InvalidatePage() was called for a not accessed page directory. */
1558 STAMCOUNTER StatHCInvalidatePagePDNAs;
1559 /** HC: The number of times PGMR3InvalidatePage() was called for a not present page directory. */
1560 STAMCOUNTER StatHCInvalidatePagePDNPs;
1561 /** HC: The number of times PGMR3InvalidatePage() was called for a page directory containing mappings (no conflict). */
1562 STAMCOUNTER StatHCInvalidatePagePDMappings;
1563 /** HC: The number of times PGMGCInvalidatePage() was called for an out of sync page directory. */
1564 STAMCOUNTER StatHCInvalidatePagePDOutOfSync;
1565 /** HC: The number of times PGMR3InvalidatePage() was skipped due to not present shw or pending pending SyncCR3. */
1566 STAMCOUNTER StatHCInvalidatePageSkipped;
1567 /** HC: PGMR3SyncPT() profiling. */
1568 STAMPROFILE StatHCSyncPT;
1569 /** HC: pgmr3SyncPTResolveConflict() profiling (includes the entire relocation). */
1570 STAMPROFILE StatHCResolveConflict;
1571 /** HC: Number of times PGMR3CheckMappingConflicts() detected a conflict. */
1572 STAMCOUNTER StatHCDetectedConflicts;
1573 /** HC: The total number of times pgmHCGuestPDWriteHandler() was called. */
1574 STAMCOUNTER StatHCGuestPDWrite;
1575 /** HC: The number of times pgmHCGuestPDWriteHandler() detected a conflict */
1576 STAMCOUNTER StatHCGuestPDWriteConflict;
1577
1578 /** HC: The number of pages marked not present for accessed bit emulation. */
1579 STAMCOUNTER StatHCAccessedPage;
1580 /** HC: The number of pages marked read-only for dirty bit tracking. */
1581 STAMCOUNTER StatHCDirtyPage;
1582 /** HC: The number of pages marked read-only for dirty bit tracking. */
1583 STAMCOUNTER StatHCDirtyPageBig;
1584 /** HC: The number of traps generated for dirty bit tracking. */
1585 STAMCOUNTER StatHCDirtyPageTrap;
1586 /** HC: The number of pages already dirty or readonly. */
1587 STAMCOUNTER StatHCDirtyPageSkipped;
1588
1589 /** GC: The number of pages marked not present for accessed bit emulation. */
1590 STAMCOUNTER StatGCAccessedPage;
1591 /** GC: The number of pages marked read-only for dirty bit tracking. */
1592 STAMCOUNTER StatGCDirtyPage;
1593 /** GC: The number of pages marked read-only for dirty bit tracking. */
1594 STAMCOUNTER StatGCDirtyPageBig;
1595 /** GC: The number of traps generated for dirty bit tracking. */
1596 STAMCOUNTER StatGCDirtyPageTrap;
1597 /** GC: The number of pages already dirty or readonly. */
1598 STAMCOUNTER StatGCDirtyPageSkipped;
1599 /** GC: The number of pages marked dirty because of write accesses. */
1600 STAMCOUNTER StatGCDirtiedPage;
1601 /** GC: The number of pages already marked dirty because of write accesses. */
1602 STAMCOUNTER StatGCPageAlreadyDirty;
1603 /** GC: The number of real pages faults during dirty bit tracking. */
1604 STAMCOUNTER StatGCDirtyTrackRealPF;
1605
1606 /** GC: Profiling of the PGMTrackDirtyBit() body */
1607 STAMPROFILE StatGCDirtyBitTracking;
1608 /** HC: Profiling of the PGMTrackDirtyBit() body */
1609 STAMPROFILE StatHCDirtyBitTracking;
1610
1611 /** GC: Profiling of the PGMGstModifyPage() body */
1612 STAMPROFILE StatGCGstModifyPage;
1613 /** HC: Profiling of the PGMGstModifyPage() body */
1614 STAMPROFILE StatHCGstModifyPage;
1615
1616 /** GC: The number of time we've marked a PD not present from SyncPage to virtualize the accessed bit. */
1617 STAMCOUNTER StatGCSyncPagePDNAs;
1618 /** GC: The number of time we've encountered an out-of-sync PD in SyncPage. */
1619 STAMCOUNTER StatGCSyncPagePDOutOfSync;
1620 /** HC: The number of time we've marked a PD not present from SyncPage to virtualize the accessed bit. */
1621 STAMCOUNTER StatHCSyncPagePDNAs;
1622 /** HC: The number of time we've encountered an out-of-sync PD in SyncPage. */
1623 STAMCOUNTER StatHCSyncPagePDOutOfSync;
1624
1625 STAMCOUNTER StatSynPT4kGC;
1626 STAMCOUNTER StatSynPT4kHC;
1627 STAMCOUNTER StatSynPT4MGC;
1628 STAMCOUNTER StatSynPT4MHC;
1629
1630 /** Profiling of the PGMFlushTLB() body. */
1631 STAMPROFILE StatFlushTLB;
1632 /** The number of times PGMFlushTLB was called with a new CR3, non-global. (switch) */
1633 STAMCOUNTER StatFlushTLBNewCR3;
1634 /** The number of times PGMFlushTLB was called with a new CR3, global. (switch) */
1635 STAMCOUNTER StatFlushTLBNewCR3Global;
1636 /** The number of times PGMFlushTLB was called with the same CR3, non-global. (flush) */
1637 STAMCOUNTER StatFlushTLBSameCR3;
1638 /** The number of times PGMFlushTLB was called with the same CR3, global. (flush) */
1639 STAMCOUNTER StatFlushTLBSameCR3Global;
1640
1641 STAMPROFILE StatGCSyncCR3; /**< GC: PGMSyncCR3() profiling. */
1642 STAMPROFILE StatGCSyncCR3Handlers; /**< GC: Profiling of the PGMSyncCR3() update handler section. */
1643 STAMPROFILE StatGCSyncCR3HandlerVirtualReset; /**< GC: Profiling of the virtual handler resets. */
1644 STAMPROFILE StatGCSyncCR3HandlerVirtualUpdate; /**< GC: Profiling of the virtual handler updates. */
1645 STAMCOUNTER StatGCSyncCR3Global; /**< GC: The number of global CR3 syncs. */
1646 STAMCOUNTER StatGCSyncCR3NotGlobal; /**< GC: The number of non-global CR3 syncs. */
1647 STAMCOUNTER StatGCSyncCR3DstFreed; /**< GC: The number of times we've had to free a shadow entry. */
1648 STAMCOUNTER StatGCSyncCR3DstFreedSrcNP; /**< GC: The number of times we've had to free a shadow entry for which the source entry was not present. */
1649 STAMCOUNTER StatGCSyncCR3DstNotPresent; /**< GC: The number of times we've encountered a not present shadow entry for a present guest entry. */
1650 STAMCOUNTER StatGCSyncCR3DstSkippedGlobalPD; /**< GC: The number of times a global page directory wasn't flushed. */
1651 STAMCOUNTER StatGCSyncCR3DstSkippedGlobalPT; /**< GC: The number of times a page table with only global entries wasn't flushed. */
1652 STAMCOUNTER StatGCSyncCR3DstCacheHit; /**< GC: The number of times we got some kind of cache hit on a page table. */
1653
1654 STAMPROFILE StatHCSyncCR3; /**< HC: PGMSyncCR3() profiling. */
1655 STAMPROFILE StatHCSyncCR3Handlers; /**< HC: Profiling of the PGMSyncCR3() update handler section. */
1656 STAMPROFILE StatHCSyncCR3HandlerVirtualReset; /**< HC: Profiling of the virtual handler resets. */
1657 STAMPROFILE StatHCSyncCR3HandlerVirtualUpdate; /**< HC: Profiling of the virtual handler updates. */
1658 STAMCOUNTER StatHCSyncCR3Global; /**< HC: The number of global CR3 syncs. */
1659 STAMCOUNTER StatHCSyncCR3NotGlobal; /**< HC: The number of non-global CR3 syncs. */
1660 STAMCOUNTER StatHCSyncCR3DstFreed; /**< HC: The number of times we've had to free a shadow entry. */
1661 STAMCOUNTER StatHCSyncCR3DstFreedSrcNP; /**< HC: The number of times we've had to free a shadow entry for which the source entry was not present. */
1662 STAMCOUNTER StatHCSyncCR3DstNotPresent; /**< HC: The number of times we've encountered a not present shadow entry for a present guest entry. */
1663 STAMCOUNTER StatHCSyncCR3DstSkippedGlobalPD; /**< HC: The number of times a global page directory wasn't flushed. */
1664 STAMCOUNTER StatHCSyncCR3DstSkippedGlobalPT; /**< HC: The number of times a page table with only global entries wasn't flushed. */
1665 STAMCOUNTER StatHCSyncCR3DstCacheHit; /**< HC: The number of times we got some kind of cache hit on a page table. */
1666
1667 /** GC: Profiling of pgmHandlerVirtualFindByPhysAddr. */
1668 STAMPROFILE StatVirtHandleSearchByPhysGC;
1669 /** HC: Profiling of pgmHandlerVirtualFindByPhysAddr. */
1670 STAMPROFILE StatVirtHandleSearchByPhysHC;
1671 /** HC: The number of times PGMR3HandlerPhysicalReset is called. */
1672 STAMCOUNTER StatHandlePhysicalReset;
1673
1674 STAMPROFILE StatCheckPageFault;
1675 STAMPROFILE StatLazySyncPT;
1676 STAMPROFILE StatMapping;
1677 STAMPROFILE StatOutOfSync;
1678 STAMPROFILE StatHandlers;
1679 STAMPROFILE StatEIPHandlers;
1680 STAMPROFILE StatHCPrefetch;
1681
1682# ifdef PGMPOOL_WITH_GCPHYS_TRACKING
1683 /** The number of first time shadowings. */
1684 STAMCOUNTER StatTrackVirgin;
1685 /** The number of times switching to cRef2, i.e. the page is being shadowed by two PTs. */
1686 STAMCOUNTER StatTrackAliased;
1687 /** The number of times we're tracking using cRef2. */
1688 STAMCOUNTER StatTrackAliasedMany;
1689 /** The number of times we're hitting pages which has overflowed cRef2. */
1690 STAMCOUNTER StatTrackAliasedLots;
1691 /** The number of times the extent list grows to long. */
1692 STAMCOUNTER StatTrackOverflows;
1693 /** Profiling of SyncPageWorkerTrackDeref (expensive). */
1694 STAMPROFILE StatTrackDeref;
1695# endif
1696
1697 /** Allocated mbs of guest ram */
1698 STAMCOUNTER StatDynRamTotal;
1699 /** Nr of pgmr3PhysGrowRange calls. */
1700 STAMCOUNTER StatDynRamGrow;
1701
1702 STAMCOUNTER StatGCTrap0ePD[X86_PG_ENTRIES];
1703 STAMCOUNTER StatGCSyncPtPD[X86_PG_ENTRIES];
1704 STAMCOUNTER StatGCSyncPagePD[X86_PG_ENTRIES];
1705#endif
1706} PGM, *PPGM;
1707
1708
1709/** @name PGM::fSyncFlags Flags
1710 * @{
1711 */
1712/** Updates the MM_RAM_FLAGS_VIRTUAL_HANDLER page bit. */
1713#define PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL BIT(0)
1714/** Always sync CR3. */
1715#define PGM_SYNC_ALWAYS BIT(1)
1716/** Check monitoring on next CR3 (re)load and invalidate page. */
1717#define PGM_SYNC_MONITOR_CR3 BIT(2)
1718/** Clear the page pool (a light weight flush). */
1719#define PGM_SYNC_CLEAR_PGM_POOL BIT(8)
1720/** @} */
1721
1722
1723__BEGIN_DECLS
1724
1725PGMGCDECL(int) pgmGCGuestPDWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, void *pvFault, RTGCPHYS GCPhysFault, void *pvUser);
1726PGMDECL(int) pgmGuestROMWriteHandler(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, void *pvFault, RTGCPHYS GCPhysFault, void *pvUser);
1727PGMGCDECL(int) pgmCachePTWriteGC(PVM pVM, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame, RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser);
1728int pgmR3SyncPTResolveConflict(PVM pVM, PPGMMAPPING pMapping, PVBOXPD pPDSrc, int iPDOld);
1729PPGMMAPPING pgmGetMapping(PVM pVM, RTGCPTR GCPtr);
1730void pgmR3MapRelocate(PVM pVM, PPGMMAPPING pMapping, int iPDOld, int iPDNew);
1731int pgmR3ChangeMode(PVM pVM, PGMMODE enmGuestMode);
1732int pgmLock(PVM pVM);
1733void pgmUnlock(PVM pVM);
1734
1735void pgmR3HandlerPhysicalUpdateAll(PVM pVM);
1736int pgmHandlerVirtualFindByPhysAddr(PVM pVM, RTGCPHYS GCPhys, PPGMVIRTHANDLER *ppVirt, unsigned *piPage);
1737DECLCALLBACK(int) pgmHandlerVirtualResetOne(PAVLROGCPTRNODECORE pNode, void *pvUser);
1738#ifdef VBOX_STRICT
1739void pgmHandlerVirtualDumpPhysPages(PVM pVM);
1740#else
1741# define pgmHandlerVirtualDumpPhysPages(a) do { } while (0)
1742#endif
1743DECLCALLBACK(void) pgmR3InfoHandlers(PVM pVM, PCDBGFINFOHLP pHlp, const char *pszArgs);
1744
1745
1746#ifdef IN_RING3
1747int pgmr3PhysGrowRange(PVM pVM, RTGCPHYS GCPhys);
1748
1749int pgmR3PoolInit(PVM pVM);
1750void pgmR3PoolRelocate(PVM pVM);
1751void pgmR3PoolReset(PVM pVM);
1752
1753#endif
1754#ifdef IN_GC
1755void *pgmGCPoolMapPage(PVM pVM, PPGMPOOLPAGE pPage);
1756#endif
1757int pgmPoolAlloc(PVM pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, uint16_t iUser, uint16_t iUserTable, PPPGMPOOLPAGE ppPage);
1758PPGMPOOLPAGE pgmPoolGetPageByHCPhys(PVM pVM, RTHCPHYS HCPhys);
1759void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint16_t iUserTable);
1760void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint16_t iUserTable);
1761int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
1762void pgmPoolFlushAll(PVM pVM);
1763void pgmPoolClearAll(PVM pVM);
1764void pgmPoolTrackFlushGCPhysPT(PVM pVM, PRTHCPHYS pHCPhys, uint16_t iShw, uint16_t cRefs);
1765void pgmPoolTrackFlushGCPhysPTs(PVM pVM, PRTHCPHYS pHCPhys, uint16_t iPhysExt);
1766int pgmPoolTrackFlushGCPhysPTsSlow(PVM pVM, PRTHCPHYS pHCPhys);
1767PPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt);
1768void pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt);
1769void pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt);
1770uint16_t pgmPoolTrackPhysExtAddref(PVM pVM, uint16_t u16, uint16_t iShwPT);
1771void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PRTHCPHYS pHCPhys);
1772#ifdef PGMPOOL_WITH_MONITORING
1773# ifdef IN_RING3
1774void pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTHCPTR pvAddress, PDISCPUSTATE pCpu);
1775# else
1776void pgmPoolMonitorChainChanging(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault, RTGCPTR pvAddress, PDISCPUSTATE pCpu);
1777# endif
1778int pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
1779void pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
1780void pgmPoolMonitorModifiedClearAll(PVM pVM);
1781int pgmPoolMonitorMonitorCR3(PPGMPOOL pPool, uint16_t idxRoot, RTGCPHYS GCPhysCR3);
1782int pgmPoolMonitorUnmonitorCR3(PPGMPOOL pPool, uint16_t idxRoot);
1783#endif
1784
1785__END_DECLS
1786
1787
1788/**
1789 * Convert GC Phys to HC Phys.
1790 *
1791 * @returns VBox status.
1792 * @param pPGM PGM handle.
1793 * @param GCPhys The GC physical address.
1794 * @param pHCPhys Where to store the corresponding HC physical address.
1795 */
1796DECLINLINE(int) PGMRamGCPhys2HCPhys(PPGM pPGM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
1797{
1798 /*
1799 * Walk range list.
1800 */
1801 PPGMRAMRANGE pRam = CTXSUFF(pPGM->pRamRanges);
1802 while (pRam)
1803 {
1804 RTGCPHYS off = GCPhys - pRam->GCPhys;
1805 if (off < pRam->cb)
1806 {
1807 unsigned iPage = off >> PAGE_SHIFT;
1808 /* Physical chunk in dynamically allocated range not present? */
1809 if (RT_UNLIKELY(!(pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK)))
1810 {
1811#ifdef IN_RING3
1812 int rc = pgmr3PhysGrowRange(PGM2VM(pPGM), GCPhys);
1813#else
1814 int rc = CTXALLMID(VMM, CallHost)(PGM2VM(pPGM), VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
1815#endif
1816 if (rc != VINF_SUCCESS)
1817 return rc;
1818 }
1819 *pHCPhys = (pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK) | (off & PAGE_OFFSET_MASK);
1820 return VINF_SUCCESS;
1821 }
1822
1823 pRam = CTXSUFF(pRam->pNext);
1824 }
1825 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
1826}
1827
1828
1829/**
1830 * Convert GC Phys to HC Virt.
1831 *
1832 * @returns VBox status.
1833 * @param pPGM PGM handle.
1834 * @param GCPhys The GC physical address.
1835 * @param pHCPtr Where to store the corresponding HC virtual address.
1836 */
1837DECLINLINE(int) PGMRamGCPhys2HCPtr(PPGM pPGM, RTGCPHYS GCPhys, PRTHCPTR pHCPtr)
1838{
1839 /*
1840 * Walk range list.
1841 */
1842 PPGMRAMRANGE pRam = CTXSUFF(pPGM->pRamRanges);
1843 while (pRam)
1844 {
1845 RTGCPHYS off = GCPhys - pRam->GCPhys;
1846 if (off < pRam->cb)
1847 {
1848 if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
1849 {
1850 unsigned idx = (off >> PGM_DYNAMIC_CHUNK_SHIFT);
1851 /* Physical chunk in dynamically allocated range not present? */
1852 if (RT_UNLIKELY(!CTXSUFF(pRam->pavHCChunk)[idx]))
1853 {
1854#ifdef IN_RING3
1855 int rc = pgmr3PhysGrowRange(PGM2VM(pPGM), GCPhys);
1856#else
1857 int rc = CTXALLMID(VMM, CallHost)(PGM2VM(pPGM), VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
1858#endif
1859 if (rc != VINF_SUCCESS)
1860 return rc;
1861 }
1862 *pHCPtr = (RTHCPTR)((RTHCUINTPTR)CTXSUFF(pRam->pavHCChunk)[idx] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
1863 return VINF_SUCCESS;
1864 }
1865 if (pRam->pvHC)
1866 {
1867 *pHCPtr = (RTHCPTR)((RTHCUINTPTR)pRam->pvHC + off);
1868 return VINF_SUCCESS;
1869 }
1870 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
1871 }
1872
1873 pRam = CTXSUFF(pRam->pNext);
1874 }
1875 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
1876}
1877
1878
1879/**
1880 * Convert GC Phys to HC Virt.
1881 *
1882 * @returns VBox status.
1883 * @param PVM VM handle.
1884 * @param pRam Ram range
1885 * @param GCPhys The GC physical address.
1886 * @param pHCPtr Where to store the corresponding HC virtual address.
1887 */
1888DECLINLINE(int) PGMRamGCPhys2HCPtr(PVM pVM, PPGMRAMRANGE pRam, RTGCPHYS GCPhys, PRTHCPTR pHCPtr)
1889{
1890 RTGCPHYS off = GCPhys - pRam->GCPhys;
1891 Assert(off < pRam->cb);
1892
1893 if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
1894 {
1895 unsigned idx = (off >> PGM_DYNAMIC_CHUNK_SHIFT);
1896 /* Physical chunk in dynamically allocated range not present? */
1897 if (RT_UNLIKELY(!CTXSUFF(pRam->pavHCChunk)[idx]))
1898 {
1899#ifdef IN_RING3
1900 int rc = pgmr3PhysGrowRange(pVM, GCPhys);
1901#else
1902 int rc = CTXALLMID(VMM, CallHost)(pVM, VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
1903#endif
1904 if (rc != VINF_SUCCESS)
1905 return rc;
1906 }
1907 *pHCPtr = (RTHCPTR)((RTHCUINTPTR)CTXSUFF(pRam->pavHCChunk)[idx] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
1908 return VINF_SUCCESS;
1909 }
1910 if (pRam->pvHC)
1911 {
1912 *pHCPtr = (RTHCPTR)((RTHCUINTPTR)pRam->pvHC + off);
1913 return VINF_SUCCESS;
1914 }
1915 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
1916}
1917
1918
1919/**
1920 * Convert GC Phys to HC Virt and HC Phys.
1921 *
1922 * @returns VBox status.
1923 * @param pPGM PGM handle.
1924 * @param GCPhys The GC physical address.
1925 * @param pHCPtr Where to store the corresponding HC virtual address.
1926 * @param pHCPhys Where to store the HC Physical address and its flags.
1927 */
1928DECLINLINE(int) PGMRamGCPhys2HCPtrAndHCPhysWithFlags(PPGM pPGM, RTGCPHYS GCPhys, PRTHCPTR pHCPtr, PRTHCPHYS pHCPhys)
1929{
1930 /*
1931 * Walk range list.
1932 */
1933 PPGMRAMRANGE pRam = CTXSUFF(pPGM->pRamRanges);
1934 while (pRam)
1935 {
1936 RTGCPHYS off = GCPhys - pRam->GCPhys;
1937 if (off < pRam->cb)
1938 {
1939 unsigned iPage = off >> PAGE_SHIFT;
1940 /* Physical chunk in dynamically allocated range not present? */
1941 if (RT_UNLIKELY(!(pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK)))
1942 {
1943#ifdef IN_RING3
1944 int rc = pgmr3PhysGrowRange(PGM2VM(pPGM), GCPhys);
1945#else
1946 int rc = CTXALLMID(VMM, CallHost)(PGM2VM(pPGM), VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
1947#endif
1948 if (rc != VINF_SUCCESS)
1949 return rc;
1950 }
1951 *pHCPhys = pRam->aHCPhys[iPage];
1952
1953 if (pRam->fFlags & MM_RAM_FLAGS_DYNAMIC_ALLOC)
1954 {
1955 unsigned idx = (off >> PGM_DYNAMIC_CHUNK_SHIFT);
1956 *pHCPtr = (RTHCPTR)((RTHCUINTPTR)CTXSUFF(pRam->pavHCChunk)[idx] + (off & PGM_DYNAMIC_CHUNK_OFFSET_MASK));
1957 return VINF_SUCCESS;
1958 }
1959 if (pRam->pvHC)
1960 {
1961 *pHCPtr = (RTHCPTR)((RTHCUINTPTR)pRam->pvHC + off);
1962 return VINF_SUCCESS;
1963 }
1964 *pHCPtr = 0;
1965 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
1966 }
1967
1968 pRam = CTXSUFF(pRam->pNext);
1969 }
1970 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
1971}
1972
1973
1974/**
1975 * Convert GC Phys page to a page entry pointer.
1976 *
1977 * This is used by code which may have to update the flags.
1978 *
1979 * @returns VBox status.
1980 * @param pPGM PGM handle.
1981 * @param GCPhys The GC physical address.
1982 * @param ppHCPhys Where to store the pointer to the page entry.
1983 */
1984DECLINLINE(int) PGMRamGCPhys2PagePtr(PPGM pPGM, RTGCPHYS GCPhys, PRTHCPHYS *ppHCPhys)
1985{
1986 /*
1987 * Walk range list.
1988 */
1989 PPGMRAMRANGE pRam = CTXSUFF(pPGM->pRamRanges);
1990 while (pRam)
1991 {
1992 RTGCPHYS off = GCPhys - pRam->GCPhys;
1993 if (off < pRam->cb)
1994 {
1995 unsigned iPage = off >> PAGE_SHIFT;
1996 /* Physical chunk in dynamically allocated range not present? */
1997 if (RT_UNLIKELY(!(pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK)))
1998 {
1999#ifdef IN_RING3
2000 int rc = pgmr3PhysGrowRange(PGM2VM(pPGM), GCPhys);
2001#else
2002 int rc = CTXALLMID(VMM, CallHost)(PGM2VM(pPGM), VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
2003#endif
2004 if (rc != VINF_SUCCESS)
2005 return rc;
2006 }
2007 *ppHCPhys = &pRam->aHCPhys[iPage];
2008 return VINF_SUCCESS;
2009 }
2010
2011 pRam = CTXSUFF(pRam->pNext);
2012 }
2013 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
2014}
2015
2016
2017/**
2018 * Convert GC Phys page to HC Phys page and flags.
2019 *
2020 * @returns VBox status.
2021 * @param pPGM PGM handle.
2022 * @param GCPhys The GC physical address.
2023 * @param pHCPhys Where to store the corresponding HC physical address of the page
2024 * and the page flags.
2025 */
2026DECLINLINE(int) PGMRamGCPhys2HCPhysWithFlags(PPGM pPGM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
2027{
2028 /*
2029 * Walk range list.
2030 */
2031 PPGMRAMRANGE pRam = CTXSUFF(pPGM->pRamRanges);
2032 while (pRam)
2033 {
2034 RTGCPHYS off = GCPhys - pRam->GCPhys;
2035 if (off < pRam->cb)
2036 {
2037 unsigned iPage = off >> PAGE_SHIFT;
2038 /* Physical chunk in dynamically allocated range not present? */
2039 if (RT_UNLIKELY(!(pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK)))
2040 {
2041#ifdef IN_RING3
2042 int rc = pgmr3PhysGrowRange(PGM2VM(pPGM), GCPhys);
2043#else
2044 int rc = CTXALLMID(VMM, CallHost)(PGM2VM(pPGM), VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
2045#endif
2046 if (rc != VINF_SUCCESS)
2047 return rc;
2048 }
2049 *pHCPhys = pRam->aHCPhys[iPage];
2050 return VINF_SUCCESS;
2051 }
2052
2053 pRam = CTXSUFF(pRam->pNext);
2054 }
2055 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
2056}
2057
2058
2059/**
2060 * Clears flags associated with a RAM address.
2061 *
2062 * @returns VBox status code.
2063 * @param pPGM PGM handle.
2064 * @param GCPhys Guest context physical address.
2065 * @param fFlags fFlags to clear. (Bits 0-11.)
2066 */
2067DECLINLINE(int) PGMRamFlagsClearByGCPhys(PPGM pPGM, RTGCPHYS GCPhys, unsigned fFlags)
2068{
2069 /*
2070 * Walk range list.
2071 */
2072 PPGMRAMRANGE pRam = CTXSUFF(pPGM->pRamRanges);
2073 while (pRam)
2074 {
2075 RTGCPHYS off = GCPhys - pRam->GCPhys;
2076 if (off < pRam->cb)
2077 {
2078 unsigned iPage = off >> PAGE_SHIFT;
2079 /* Physical chunk in dynamically allocated range not present? */
2080 if (RT_UNLIKELY(!(pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK)))
2081 {
2082#ifdef IN_RING3
2083 int rc = pgmr3PhysGrowRange(PGM2VM(pPGM), GCPhys);
2084#else
2085 int rc = CTXALLMID(VMM, CallHost)(PGM2VM(pPGM), VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
2086#endif
2087 if (rc != VINF_SUCCESS)
2088 return rc;
2089 }
2090 fFlags &= ~X86_PTE_PAE_PG_MASK;
2091 pRam->aHCPhys[iPage] &= ~(RTHCPHYS)fFlags;
2092 return VINF_SUCCESS;
2093 }
2094
2095 pRam = CTXSUFF(pRam->pNext);
2096 }
2097 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
2098}
2099
2100
2101/**
2102 * Clears flags associated with a RAM address.
2103 *
2104 * @returns VBox status code.
2105 * @param pPGM PGM handle.
2106 * @param GCPhys Guest context physical address.
2107 * @param fFlags fFlags to clear. (Bits 0-11.)
2108 * @param ppRamHint Where to read and store the ram list hint.
2109 * The caller initializes this to NULL before the call.
2110 */
2111DECLINLINE(int) PGMRamFlagsClearByGCPhysWithHint(PPGM pPGM, RTGCPHYS GCPhys, unsigned fFlags, PPGMRAMRANGE *ppRamHint)
2112{
2113 /*
2114 * Check the hint.
2115 */
2116 PPGMRAMRANGE pRam = *ppRamHint;
2117 if (pRam)
2118 {
2119 RTGCPHYS off = GCPhys - pRam->GCPhys;
2120 if (off < pRam->cb)
2121 {
2122 unsigned iPage = off >> PAGE_SHIFT;
2123 /* Physical chunk in dynamically allocated range not present? */
2124 if (RT_UNLIKELY(!(pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK)))
2125 {
2126#ifdef IN_RING3
2127 int rc = pgmr3PhysGrowRange(PGM2VM(pPGM), GCPhys);
2128#else
2129 int rc = CTXALLMID(VMM, CallHost)(PGM2VM(pPGM), VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
2130#endif
2131 if (rc != VINF_SUCCESS)
2132 return rc;
2133 }
2134 fFlags &= ~X86_PTE_PAE_PG_MASK;
2135 pRam->aHCPhys[iPage] &= ~(RTHCPHYS)fFlags;
2136 return VINF_SUCCESS;
2137 }
2138 }
2139
2140 /*
2141 * Walk range list.
2142 */
2143 pRam = CTXSUFF(pPGM->pRamRanges);
2144 while (pRam)
2145 {
2146 RTGCPHYS off = GCPhys - pRam->GCPhys;
2147 if (off < pRam->cb)
2148 {
2149 unsigned iPage = off >> PAGE_SHIFT;
2150 /* Physical chunk in dynamically allocated range not present? */
2151 if (RT_UNLIKELY(!(pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK)))
2152 {
2153#ifdef IN_RING3
2154 int rc = pgmr3PhysGrowRange(PGM2VM(pPGM), GCPhys);
2155#else
2156 int rc = CTXALLMID(VMM, CallHost)(PGM2VM(pPGM), VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
2157#endif
2158 if (rc != VINF_SUCCESS)
2159 return rc;
2160 }
2161 fFlags &= ~X86_PTE_PAE_PG_MASK;
2162 pRam->aHCPhys[iPage] &= ~(RTHCPHYS)fFlags;
2163 *ppRamHint = pRam;
2164 return VINF_SUCCESS;
2165 }
2166
2167 pRam = CTXSUFF(pRam->pNext);
2168 }
2169 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
2170}
2171
2172/**
2173 * Sets (bitwise OR) flags associated with a RAM address.
2174 *
2175 * @returns VBox status code.
2176 * @param pPGM PGM handle.
2177 * @param GCPhys Guest context physical address.
2178 * @param fFlags fFlags to set clear. (Bits 0-11.)
2179 */
2180DECLINLINE(int) PGMRamFlagsSetByGCPhys(PPGM pPGM, RTGCPHYS GCPhys, unsigned fFlags)
2181{
2182 /*
2183 * Walk range list.
2184 */
2185 PPGMRAMRANGE pRam = CTXSUFF(pPGM->pRamRanges);
2186 while (pRam)
2187 {
2188 RTGCPHYS off = GCPhys - pRam->GCPhys;
2189 if (off < pRam->cb)
2190 {
2191 unsigned iPage = off >> PAGE_SHIFT;
2192 /* Physical chunk in dynamically allocated range not present? */
2193 if (RT_UNLIKELY(!(pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK)))
2194 {
2195#ifdef IN_RING3
2196 int rc = pgmr3PhysGrowRange(PGM2VM(pPGM), GCPhys);
2197#else
2198 int rc = CTXALLMID(VMM, CallHost)(PGM2VM(pPGM), VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
2199#endif
2200 if (rc != VINF_SUCCESS)
2201 return rc;
2202 }
2203 fFlags &= ~X86_PTE_PAE_PG_MASK;
2204 pRam->aHCPhys[iPage] |= fFlags;
2205 return VINF_SUCCESS;
2206 }
2207
2208 pRam = CTXSUFF(pRam->pNext);
2209 }
2210 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
2211}
2212
2213/**
2214 * Sets (bitwise OR) flags associated with a RAM address.
2215 *
2216 * @returns VBox status code.
2217 * @param pPGM PGM handle.
2218 * @param GCPhys Guest context physical address.
2219 * @param fFlags fFlags to set clear. (Bits 0-11.)
2220 * @param ppRamHint Where to read and store the ram list hint.
2221 * The caller initializes this to NULL before the call.
2222 */
2223DECLINLINE(int) PGMRamFlagsSetByGCPhysWithHint(PPGM pPGM, RTGCPHYS GCPhys, unsigned fFlags, PPGMRAMRANGE *ppRamHint)
2224{
2225 /*
2226 * Check the hint.
2227 */
2228 PPGMRAMRANGE pRam = *ppRamHint;
2229 if (pRam)
2230 {
2231 RTGCPHYS off = GCPhys - pRam->GCPhys;
2232 if (off < pRam->cb)
2233 {
2234 unsigned iPage = off >> PAGE_SHIFT;
2235 /* Physical chunk in dynamically allocated range not present? */
2236 if (RT_UNLIKELY(!(pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK)))
2237 {
2238#ifdef IN_RING3
2239 int rc = pgmr3PhysGrowRange(PGM2VM(pPGM), GCPhys);
2240#else
2241 int rc = CTXALLMID(VMM, CallHost)(PGM2VM(pPGM), VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
2242#endif
2243 if (rc != VINF_SUCCESS)
2244 return rc;
2245 }
2246 fFlags &= ~X86_PTE_PAE_PG_MASK;
2247 pRam->aHCPhys[iPage] |= fFlags;
2248 return VINF_SUCCESS;
2249 }
2250 }
2251
2252 /*
2253 * Walk range list.
2254 */
2255 pRam = CTXSUFF(pPGM->pRamRanges);
2256 while (pRam)
2257 {
2258 RTGCPHYS off = GCPhys - pRam->GCPhys;
2259 if (off < pRam->cb)
2260 {
2261 unsigned iPage = off >> PAGE_SHIFT;
2262 /* Physical chunk in dynamically allocated range not present? */
2263 if (RT_UNLIKELY(!(pRam->aHCPhys[iPage] & X86_PTE_PAE_PG_MASK)))
2264 {
2265#ifdef IN_RING3
2266 int rc = pgmr3PhysGrowRange(PGM2VM(pPGM), GCPhys);
2267#else
2268 int rc = CTXALLMID(VMM, CallHost)(PGM2VM(pPGM), VMMCALLHOST_PGM_RAM_GROW_RANGE, GCPhys);
2269#endif
2270 if (rc != VINF_SUCCESS)
2271 return rc;
2272 }
2273 fFlags &= ~X86_PTE_PAE_PG_MASK;
2274 pRam->aHCPhys[iPage] |= fFlags;
2275 *ppRamHint = pRam;
2276 return VINF_SUCCESS;
2277 }
2278
2279 pRam = CTXSUFF(pRam->pNext);
2280 }
2281 return VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS;
2282}
2283
2284
2285/**
2286 * Gets the page directory for the specified address.
2287 *
2288 * @returns Pointer to the page directory in question.
2289 * @returns NULL if the page directory is not present or on an invalid page.
2290 * @param pPGM Pointer to the PGM instance data.
2291 * @param GCPtr The address.
2292 */
2293DECLINLINE(PX86PDPAE) pgmGstGetPaePD(PPGM pPGM, RTGCUINTPTR GCPtr)
2294{
2295 const unsigned iPdPtr = GCPtr >> X86_PDPTR_SHIFT;
2296 if (CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].n.u1Present)
2297 {
2298 if ((CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].u & X86_PDPE_PG_MASK) == pPGM->aGCPhysGstPaePDs[iPdPtr])
2299 return CTXSUFF(pPGM->apGstPaePDs)[iPdPtr];
2300
2301 /* cache is out-of-sync. */
2302 PX86PDPAE pPD;
2303 int rc = PGM_GCPHYS_2_PTR(PGM2VM(pPGM), CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].u & X86_PDPE_PG_MASK, &pPD);
2304 if (VBOX_SUCCESS(rc))
2305 return pPD;
2306 AssertMsgFailed(("Impossible! rc=%d PDPE=%#llx\n", rc, CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].u));
2307 /* returning NIL_RTGCPHYS is ok if we assume it's just an invalid page of some kind emualted as all 0s. */
2308 }
2309 return NULL;
2310}
2311
2312
2313/**
2314 * Gets the page directory entry for the specified address.
2315 *
2316 * @returns Pointer to the page directory entry in question.
2317 * @returns NULL if the page directory is not present or on an invalid page.
2318 * @param pPGM Pointer to the PGM instance data.
2319 * @param GCPtr The address.
2320 */
2321DECLINLINE(PX86PDEPAE) pgmGstGetPaePDEPtr(PPGM pPGM, RTGCUINTPTR GCPtr)
2322{
2323 const unsigned iPdPtr = GCPtr >> X86_PDPTR_SHIFT;
2324 if (CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].n.u1Present)
2325 {
2326 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
2327 if ((CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].u & X86_PDPE_PG_MASK) == pPGM->aGCPhysGstPaePDs[iPdPtr])
2328 return &CTXSUFF(pPGM->apGstPaePDs)[iPdPtr]->a[iPD];
2329
2330 /* cache is out-of-sync. */
2331 PX86PDPAE pPD;
2332 int rc = PGM_GCPHYS_2_PTR(PGM2VM(pPGM), CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].u & X86_PDPE_PG_MASK, &pPD);
2333 if (VBOX_SUCCESS(rc))
2334 return &pPD->a[iPD];
2335 AssertMsgFailed(("Impossible! rc=%d PDPE=%#llx\n", rc, CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].u));
2336 /* returning NIL_RTGCPHYS is ok if we assume it's just an invalid page or something which we'll emulate as all 0s. */
2337 }
2338 return NULL;
2339}
2340
2341
2342/**
2343 * Gets the page directory entry for the specified address.
2344 *
2345 * @returns The page directory entry in question.
2346 * @returns A non-present entry if the page directory is not present or on an invalid page.
2347 * @param pPGM Pointer to the PGM instance data.
2348 * @param GCPtr The address.
2349 */
2350DECLINLINE(uint64_t) pgmGstGetPaePDE(PPGM pPGM, RTGCUINTPTR GCPtr)
2351{
2352 const unsigned iPdPtr = GCPtr >> X86_PDPTR_SHIFT;
2353 if (CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].n.u1Present)
2354 {
2355 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
2356 if ((CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].u & X86_PDPE_PG_MASK) == pPGM->aGCPhysGstPaePDs[iPdPtr])
2357 return CTXSUFF(pPGM->apGstPaePDs)[iPdPtr]->a[iPD].u;
2358
2359 /* cache is out-of-sync. */
2360 PX86PDPAE pPD;
2361 int rc = PGM_GCPHYS_2_PTR(PGM2VM(pPGM), CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].u & X86_PDPE_PG_MASK, &pPD);
2362 if (VBOX_SUCCESS(rc))
2363 return pPD->a[iPD].u;
2364 AssertMsgFailed(("Impossible! rc=%d PDPE=%#llx\n", rc, CTXSUFF(pPGM->pGstPaePDPTR)->a[iPdPtr].u));
2365 }
2366 return 0;
2367}
2368
2369
2370/**
2371 * Checks if any of the specified page flags are set for the given page.
2372 *
2373 * @returns true if any of the flags are set.
2374 * @returns false if all the flags are clear.
2375 * @param pPGM PGM handle.
2376 * @param GCPhys The GC physical address.
2377 * @param fFlags The flags to check for.
2378 */
2379DECLINLINE(bool) PGMRamTestFlags(PPGM pPGM, RTGCPHYS GCPhys, uint64_t fFlags)
2380{
2381 /*
2382 * Walk range list.
2383 */
2384 for (PPGMRAMRANGE pRam = CTXSUFF(pPGM->pRamRanges);
2385 pRam;
2386 pRam = CTXSUFF(pRam->pNext))
2387 {
2388 RTGCPHYS off = GCPhys - pRam->GCPhys;
2389 if (off < pRam->cb)
2390 return (pRam->aHCPhys[off >> PAGE_SHIFT] & fFlags) != 0;
2391 }
2392 return false;
2393}
2394
2395
2396/**
2397 * Gets the ram flags for a handler.
2398 *
2399 * @returns The ram flags.
2400 * @param pCur The physical handler in question.
2401 */
2402DECLINLINE(unsigned) pgmHandlerPhysicalCalcFlags(PPGMPHYSHANDLER pCur)
2403{
2404 switch (pCur->enmType)
2405 {
2406 case PGMPHYSHANDLERTYPE_PHYSICAL:
2407 return MM_RAM_FLAGS_PHYSICAL_HANDLER;
2408
2409 case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE:
2410 return MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_WRITE;
2411
2412 case PGMPHYSHANDLERTYPE_MMIO:
2413 case PGMPHYSHANDLERTYPE_PHYSICAL_ALL:
2414 return MM_RAM_FLAGS_PHYSICAL_HANDLER | MM_RAM_FLAGS_PHYSICAL_ALL;
2415
2416 default:
2417 AssertFatalMsgFailed(("Invalid type %d\n", pCur->enmType));
2418 }
2419}
2420
2421
2422/**
2423 * Clears one physical page of a virtual handler
2424 *
2425 * @param pPGM Pointer to the PGM instance.
2426 * @param pCur Virtual handler structure
2427 * @param iPage Physical page index
2428 */
2429DECLINLINE(void) pgmHandlerVirtualClearPage(PPGM pPGM, PPGMVIRTHANDLER pCur, unsigned iPage)
2430{
2431 const PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
2432
2433 /*
2434 * Remove the node from the tree (it's supposed to be in the tree if we get here!).
2435 */
2436#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
2437 AssertReleaseMsg(pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE,
2438 ("pPhys2Virt=%p:{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
2439 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias));
2440#endif
2441 if (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD)
2442 {
2443 /* We're the head of the alias chain. */
2444 PPGMPHYS2VIRTHANDLER pRemove = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRemove(&pPGM->CTXSUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key); NOREF(pRemove);
2445#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
2446 AssertReleaseMsg(pRemove != NULL,
2447 ("pPhys2Virt=%p:{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
2448 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias));
2449 AssertReleaseMsg(pRemove == pPhys2Virt,
2450 ("wanted: pPhys2Virt=%p:{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n"
2451 " got: pRemove=%p:{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
2452 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias,
2453 pRemove, pRemove->Core.Key, pRemove->Core.KeyLast, pRemove->offVirtHandler, pRemove->offNextAlias));
2454#endif
2455 if (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK)
2456 {
2457 /* Insert the next list in the alias chain into the tree. */
2458 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
2459#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
2460 AssertReleaseMsg(pNext->offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE,
2461 ("pNext=%p:{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
2462 pNext, pNext->Core.Key, pNext->Core.KeyLast, pNext->offVirtHandler, pNext->offNextAlias));
2463#endif
2464 pNext->offNextAlias |= PGMPHYS2VIRTHANDLER_IS_HEAD;
2465 bool fRc = RTAvlroGCPhysInsert(&pPGM->CTXSUFF(pTrees)->PhysToVirtHandlers, &pNext->Core);
2466 AssertRelease(fRc);
2467 }
2468 }
2469 else
2470 {
2471 /* Locate the previous node in the alias chain. */
2472 PPGMPHYS2VIRTHANDLER pPrev = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pPGM->CTXSUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
2473#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
2474 AssertReleaseMsg(pPrev != pPhys2Virt,
2475 ("pPhys2Virt=%p:{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} pPrev=%p\n",
2476 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias, pPrev));
2477#endif
2478 for (;;)
2479 {
2480 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPrev + (pPrev->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
2481 if (pNext == pPhys2Virt)
2482 {
2483 /* unlink. */
2484 LogFlow(("pgmHandlerVirtualClearPage: removed %p:{.offNextAlias=%#RX32} from alias chain. prev %p:{.offNextAlias=%#RX32} [%VGp-%VGp]\n",
2485 pPhys2Virt, pPhys2Virt->offNextAlias, pPrev, pPrev->offNextAlias, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
2486 if (!(pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
2487 pPrev->offNextAlias &= ~PGMPHYS2VIRTHANDLER_OFF_MASK;
2488 else
2489 {
2490 PPGMPHYS2VIRTHANDLER pNewNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
2491 pPrev->offNextAlias = ((intptr_t)pNewNext - (intptr_t)pPrev)
2492 | (pPrev->offNextAlias & ~PGMPHYS2VIRTHANDLER_OFF_MASK);
2493 }
2494 break;
2495 }
2496
2497 /* next */
2498 if (pNext == pPrev)
2499 {
2500#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
2501 AssertReleaseMsg(pNext != pPrev,
2502 ("pPhys2Virt=%p:{.Core.Key=%VGp, .Core.KeyLast=%VGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} pPrev=%p\n",
2503 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias, pPrev));
2504#endif
2505 break;
2506 }
2507 pPrev = pNext;
2508 }
2509 }
2510 Log2(("PHYS2VIRT: Removing %VGp-%VGp %#RX32 %s\n",
2511 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, HCSTRING(pCur->pszDesc)));
2512 pPhys2Virt->offNextAlias = 0;
2513 pPhys2Virt->Core.KeyLast = NIL_RTGCPHYS; /* require reinsert */
2514
2515 /*
2516 * Clear the ram flags for this page.
2517 */
2518 int rc = PGMRamFlagsClearByGCPhys(pPGM, pPhys2Virt->Core.Key,
2519 MM_RAM_FLAGS_VIRTUAL_HANDLER | MM_RAM_FLAGS_VIRTUAL_ALL | MM_RAM_FLAGS_VIRTUAL_WRITE);
2520 AssertRC(rc);
2521}
2522
2523
2524/**
2525 * Internal worker for finding a 'in-use' shadow page give by it's physical address.
2526 *
2527 * @returns Pointer to the shadow page structure.
2528 * @param pPool The pool.
2529 * @param HCPhys The HC physical address of the shadow page.
2530 */
2531DECLINLINE(PPGMPOOLPAGE) pgmPoolGetPage(PPGMPOOL pPool, RTHCPHYS HCPhys)
2532{
2533 /*
2534 * Look up the page.
2535 */
2536 PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, HCPhys & X86_PTE_PAE_PG_MASK);
2537 AssertFatalMsg(pPage && pPage->enmKind != PGMPOOLKIND_FREE, ("HCPhys=%VHp pPage=%p type=%d\n", HCPhys, pPage, (pPage) ? pPage->enmKind : 0));
2538 return pPage;
2539}
2540
2541
2542/**
2543 * Internal worker for finding a 'in-use' shadow page give by it's physical address.
2544 *
2545 * @returns Pointer to the shadow page structure.
2546 * @param pPool The pool.
2547 * @param idx The pool page index.
2548 */
2549DECLINLINE(PPGMPOOLPAGE) pgmPoolGetPageByIdx(PPGMPOOL pPool, unsigned idx)
2550{
2551 AssertFatalMsg(idx >= PGMPOOL_IDX_FIRST && idx < pPool->cCurPages, ("idx=%d\n", idx));
2552 return &pPool->aPages[idx];
2553}
2554
2555
2556#ifdef PGMPOOL_WITH_GCPHYS_TRACKING
2557/**
2558 * Clear references to guest physical memory.
2559 *
2560 * @param pPool The pool.
2561 * @param pPage The page.
2562 * @param pHCPhys Pointer to the aHCPhys entry in the ram range.
2563 */
2564DECLINLINE(void) pgmTrackDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PRTHCPHYS pHCPhys)
2565{
2566 /*
2567 * Just deal with the simple case here.
2568 */
2569#ifdef LOG_ENABLED
2570 const RTHCPHYS HCPhysOrg = *pHCPhys;
2571#endif
2572 const unsigned cRefs = *pHCPhys >> MM_RAM_FLAGS_CREFS_SHIFT;
2573 if (cRefs == 1)
2574 {
2575 Assert(pPage->idx == ((*pHCPhys >> MM_RAM_FLAGS_IDX_SHIFT) & MM_RAM_FLAGS_IDX_MASK));
2576 *pHCPhys = *pHCPhys & MM_RAM_FLAGS_NO_REFS_MASK;
2577 }
2578 else
2579 pgmPoolTrackPhysExtDerefGCPhys(pPool, pPage, pHCPhys);
2580 LogFlow(("pgmTrackDerefGCPhys: *pHCPhys=%RHp -> %RHp\n", HCPhysOrg, *pHCPhys));
2581}
2582#endif
2583
2584
2585#ifdef PGMPOOL_WITH_CACHE
2586/**
2587 * Moves the page to the head of the age list.
2588 *
2589 * This is done when the cached page is used in one way or another.
2590 *
2591 * @param pPool The pool.
2592 * @param pPage The cached page.
2593 * @todo inline in PGMInternal.h!
2594 */
2595DECLINLINE(void) pgmPoolCacheUsed(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
2596{
2597 /*
2598 * Move to the head of the age list.
2599 */
2600 if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
2601 {
2602 /* unlink */
2603 pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
2604 if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
2605 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
2606 else
2607 pPool->iAgeTail = pPage->iAgePrev;
2608
2609 /* insert at head */
2610 pPage->iAgePrev = NIL_PGMPOOL_IDX;
2611 pPage->iAgeNext = pPool->iAgeHead;
2612 Assert(pPage->iAgeNext != NIL_PGMPOOL_IDX); /* we would've already been head then */
2613 pPool->iAgeHead = pPage->idx;
2614 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->idx;
2615 }
2616}
2617#endif /* PGMPOOL_WITH_CACHE */
2618
2619/**
2620 * Tells if mappings are to be put into the shadow page table or not
2621 *
2622 * @returns boolean result
2623 * @param pVM VM handle.
2624 */
2625
2626DECLINLINE(bool) pgmMapAreMappingsEnabled(PPGM pPGM)
2627{
2628 return !pPGM->fDisableMappings;
2629}
2630
2631/** @} */
2632
2633#endif
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