VirtualBox

source: vbox/trunk/src/VBox/VMM/include/PGMInline.h@ 43303

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

Forward ported r78414 from 4.1: Fix VERR_NO_MEMORY loading VMMR0.r0 because of mt.lion executable memory fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 50.1 KB
Line 
1/* $Id: PGMInline.h 43303 2012-09-11 23:55:10Z vboxsync $ */
2/** @file
3 * PGM - Inlined functions.
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef ___PGMInline_h
19#define ___PGMInline_h
20
21#include <VBox/cdefs.h>
22#include <VBox/types.h>
23#include <VBox/err.h>
24#include <VBox/vmm/stam.h>
25#include <VBox/param.h>
26#include <VBox/vmm/vmm.h>
27#include <VBox/vmm/mm.h>
28#include <VBox/vmm/pdmcritsect.h>
29#include <VBox/vmm/pdmapi.h>
30#include <VBox/dis.h>
31#include <VBox/vmm/dbgf.h>
32#include <VBox/log.h>
33#include <VBox/vmm/gmm.h>
34#include <VBox/vmm/hwaccm.h>
35#include <iprt/asm.h>
36#include <iprt/assert.h>
37#include <iprt/avl.h>
38#include <iprt/critsect.h>
39#include <iprt/sha.h>
40
41
42
43/** @addtogroup grp_pgm_int Internals
44 * @internal
45 * @{
46 */
47
48/**
49 * Gets the PGMRAMRANGE structure for a guest page.
50 *
51 * @returns Pointer to the RAM range on success.
52 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
53 *
54 * @param pVM Pointer to the VM.
55 * @param GCPhys The GC physical address.
56 */
57DECLINLINE(PPGMRAMRANGE) pgmPhysGetRange(PVM pVM, RTGCPHYS GCPhys)
58{
59 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)];
60 if (!pRam || GCPhys - pRam->GCPhys >= pRam->cb)
61 pRam = pgmPhysGetRangeSlow(pVM, GCPhys);
62 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,RamRangeTlbHits));
63 return pRam;
64}
65
66
67/**
68 * Gets the PGMRAMRANGE structure for a guest page, if unassigned get the ram
69 * range above it.
70 *
71 * @returns Pointer to the RAM range on success.
72 * @returns NULL if the address is located after the last range.
73 *
74 * @param pVM Pointer to the VM.
75 * @param GCPhys The GC physical address.
76 */
77DECLINLINE(PPGMRAMRANGE) pgmPhysGetRangeAtOrAbove(PVM pVM, RTGCPHYS GCPhys)
78{
79 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)];
80 if ( !pRam
81 || (GCPhys - pRam->GCPhys) >= pRam->cb)
82 return pgmPhysGetRangeAtOrAboveSlow(pVM, GCPhys);
83 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,RamRangeTlbHits));
84 return pRam;
85}
86
87
88/**
89 * Gets the PGMPAGE structure for a guest page.
90 *
91 * @returns Pointer to the page on success.
92 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
93 *
94 * @param pVM Pointer to the VM.
95 * @param GCPhys The GC physical address.
96 */
97DECLINLINE(PPGMPAGE) pgmPhysGetPage(PVM pVM, RTGCPHYS GCPhys)
98{
99 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)];
100 RTGCPHYS off;
101 if ( !pRam
102 || (off = GCPhys - pRam->GCPhys) >= pRam->cb)
103 return pgmPhysGetPageSlow(pVM, GCPhys);
104 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,RamRangeTlbHits));
105 return &pRam->aPages[off >> PAGE_SHIFT];
106}
107
108
109/**
110 * Gets the PGMPAGE structure for a guest page.
111 *
112 * Old Phys code: Will make sure the page is present.
113 *
114 * @returns VBox status code.
115 * @retval VINF_SUCCESS and a valid *ppPage on success.
116 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if the address isn't valid.
117 *
118 * @param pVM Pointer to the VM.
119 * @param GCPhys The GC physical address.
120 * @param ppPage Where to store the page pointer on success.
121 */
122DECLINLINE(int) pgmPhysGetPageEx(PVM pVM, RTGCPHYS GCPhys, PPPGMPAGE ppPage)
123{
124 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)];
125 RTGCPHYS off;
126 if ( !pRam
127 || (off = GCPhys - pRam->GCPhys) >= pRam->cb)
128 return pgmPhysGetPageExSlow(pVM, GCPhys, ppPage);
129 *ppPage = &pRam->aPages[(GCPhys - pRam->GCPhys) >> PAGE_SHIFT];
130 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,RamRangeTlbHits));
131 return VINF_SUCCESS;
132}
133
134
135/**
136 * Gets the PGMPAGE structure for a guest page.
137 *
138 * Old Phys code: Will make sure the page is present.
139 *
140 * @returns VBox status code.
141 * @retval VINF_SUCCESS and a valid *ppPage on success.
142 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if the address isn't valid.
143 *
144 * @param pVM Pointer to the VM.
145 * @param GCPhys The GC physical address.
146 * @param ppPage Where to store the page pointer on success.
147 * @param ppRamHint Where to read and store the ram list hint.
148 * The caller initializes this to NULL before the call.
149 */
150DECLINLINE(int) pgmPhysGetPageWithHintEx(PVM pVM, RTGCPHYS GCPhys, PPPGMPAGE ppPage, PPGMRAMRANGE *ppRamHint)
151{
152 RTGCPHYS off;
153 PPGMRAMRANGE pRam = *ppRamHint;
154 if ( !pRam
155 || RT_UNLIKELY((off = GCPhys - pRam->GCPhys) >= pRam->cb))
156 {
157 pRam = pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)];
158 if ( !pRam
159 || (off = GCPhys - pRam->GCPhys) >= pRam->cb)
160 return pgmPhysGetPageAndRangeExSlow(pVM, GCPhys, ppPage, ppRamHint);
161
162 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,RamRangeTlbHits));
163 *ppRamHint = pRam;
164 }
165 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
166 return VINF_SUCCESS;
167}
168
169
170/**
171 * Gets the PGMPAGE structure for a guest page together with the PGMRAMRANGE.
172 *
173 * @returns Pointer to the page on success.
174 * @returns NULL on a VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS condition.
175 *
176 * @param pVM Pointer to the VM.
177 * @param GCPhys The GC physical address.
178 * @param ppPage Where to store the pointer to the PGMPAGE structure.
179 * @param ppRam Where to store the pointer to the PGMRAMRANGE structure.
180 */
181DECLINLINE(int) pgmPhysGetPageAndRangeEx(PVM pVM, RTGCPHYS GCPhys, PPPGMPAGE ppPage, PPGMRAMRANGE *ppRam)
182{
183 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)];
184 RTGCPHYS off;
185 if ( !pRam
186 || (off = GCPhys - pRam->GCPhys) >= pRam->cb)
187 return pgmPhysGetPageAndRangeExSlow(pVM, GCPhys, ppPage, ppRam);
188
189 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,RamRangeTlbHits));
190 *ppRam = pRam;
191 *ppPage = &pRam->aPages[off >> PAGE_SHIFT];
192 return VINF_SUCCESS;
193}
194
195
196/**
197 * Convert GC Phys to HC Phys.
198 *
199 * @returns VBox status.
200 * @param pVM Pointer to the VM.
201 * @param GCPhys The GC physical address.
202 * @param pHCPhys Where to store the corresponding HC physical address.
203 *
204 * @deprecated Doesn't deal with zero, shared or write monitored pages.
205 * Avoid when writing new code!
206 */
207DECLINLINE(int) pgmRamGCPhys2HCPhys(PVM pVM, RTGCPHYS GCPhys, PRTHCPHYS pHCPhys)
208{
209 PPGMPAGE pPage;
210 int rc = pgmPhysGetPageEx(pVM, GCPhys, &pPage);
211 if (RT_FAILURE(rc))
212 return rc;
213 *pHCPhys = PGM_PAGE_GET_HCPHYS(pPage) | (GCPhys & PAGE_OFFSET_MASK);
214 return VINF_SUCCESS;
215}
216
217#if defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0) || defined(IN_RC)
218
219/**
220 * Inlined version of the ring-0 version of the host page mapping code
221 * that optimizes access to pages already in the set.
222 *
223 * @returns VINF_SUCCESS. Will bail out to ring-3 on failure.
224 * @param pVCpu Pointer to the VMCPU.
225 * @param HCPhys The physical address of the page.
226 * @param ppv Where to store the mapping address.
227 */
228DECLINLINE(int) pgmRZDynMapHCPageInlined(PVMCPU pVCpu, RTHCPHYS HCPhys, void **ppv RTLOG_COMMA_SRC_POS_DECL)
229{
230 PPGMMAPSET pSet = &pVCpu->pgm.s.AutoSet;
231
232 STAM_PROFILE_START(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapHCPageInl, a);
233 Assert(!(HCPhys & PAGE_OFFSET_MASK));
234 Assert(pSet->cEntries <= RT_ELEMENTS(pSet->aEntries));
235
236 unsigned iHash = PGMMAPSET_HASH(HCPhys);
237 unsigned iEntry = pSet->aiHashTable[iHash];
238 if ( iEntry < pSet->cEntries
239 && pSet->aEntries[iEntry].HCPhys == HCPhys
240 && pSet->aEntries[iEntry].cInlinedRefs < UINT16_MAX - 1)
241 {
242 pSet->aEntries[iEntry].cInlinedRefs++;
243 *ppv = pSet->aEntries[iEntry].pvPage;
244 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapHCPageInlHits);
245 }
246 else
247 {
248 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapHCPageInlMisses);
249 pgmRZDynMapHCPageCommon(pSet, HCPhys, ppv RTLOG_COMMA_SRC_POS_ARGS);
250 }
251
252 STAM_PROFILE_STOP(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapHCPageInl, a);
253 return VINF_SUCCESS;
254}
255
256
257/**
258 * Inlined version of the guest page mapping code that optimizes access to pages
259 * already in the set.
260 *
261 * @returns VBox status code, see pgmRZDynMapGCPageCommon for details.
262 * @param pVM Pointer to the VM.
263 * @param pVCpu Pointer to the VMCPU.
264 * @param GCPhys The guest physical address of the page.
265 * @param ppv Where to store the mapping address.
266 */
267DECLINLINE(int) pgmRZDynMapGCPageV2Inlined(PVM pVM, PVMCPU pVCpu, RTGCPHYS GCPhys, void **ppv RTLOG_COMMA_SRC_POS_DECL)
268{
269 STAM_PROFILE_START(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapGCPageInl, a);
270 AssertMsg(!(GCPhys & PAGE_OFFSET_MASK), ("%RGp\n", GCPhys));
271
272 /*
273 * Get the ram range.
274 */
275 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)];
276 RTGCPHYS off;
277 if ( !pRam
278 || (off = GCPhys - pRam->GCPhys) >= pRam->cb
279 /** @todo || page state stuff */
280 )
281 {
282 /* This case is not counted into StatRZDynMapGCPageInl. */
283 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapGCPageInlRamMisses);
284 return pgmRZDynMapGCPageCommon(pVM, pVCpu, GCPhys, ppv RTLOG_COMMA_SRC_POS_ARGS);
285 }
286
287 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[off >> PAGE_SHIFT]);
288 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapGCPageInlRamHits);
289
290 /*
291 * pgmRZDynMapHCPageInlined with out stats.
292 */
293 PPGMMAPSET pSet = &pVCpu->pgm.s.AutoSet;
294 Assert(!(HCPhys & PAGE_OFFSET_MASK));
295 Assert(pSet->cEntries <= RT_ELEMENTS(pSet->aEntries));
296
297 unsigned iHash = PGMMAPSET_HASH(HCPhys);
298 unsigned iEntry = pSet->aiHashTable[iHash];
299 if ( iEntry < pSet->cEntries
300 && pSet->aEntries[iEntry].HCPhys == HCPhys
301 && pSet->aEntries[iEntry].cInlinedRefs < UINT16_MAX - 1)
302 {
303 pSet->aEntries[iEntry].cInlinedRefs++;
304 *ppv = pSet->aEntries[iEntry].pvPage;
305 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapGCPageInlHits);
306 }
307 else
308 {
309 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapGCPageInlMisses);
310 pgmRZDynMapHCPageCommon(pSet, HCPhys, ppv RTLOG_COMMA_SRC_POS_ARGS);
311 }
312
313 STAM_PROFILE_STOP(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapGCPageInl, a);
314 return VINF_SUCCESS;
315}
316
317
318/**
319 * Inlined version of the ring-0 version of guest page mapping that optimizes
320 * access to pages already in the set.
321 *
322 * @returns VBox status code, see pgmRZDynMapGCPageCommon for details.
323 * @param pVCpu Pointer to the VMCPU.
324 * @param GCPhys The guest physical address of the page.
325 * @param ppv Where to store the mapping address.
326 */
327DECLINLINE(int) pgmRZDynMapGCPageInlined(PVMCPU pVCpu, RTGCPHYS GCPhys, void **ppv RTLOG_COMMA_SRC_POS_DECL)
328{
329 return pgmRZDynMapGCPageV2Inlined(pVCpu->CTX_SUFF(pVM), pVCpu, GCPhys, ppv RTLOG_COMMA_SRC_POS_ARGS);
330}
331
332
333/**
334 * Inlined version of the ring-0 version of the guest byte mapping code
335 * that optimizes access to pages already in the set.
336 *
337 * @returns VBox status code, see pgmRZDynMapGCPageCommon for details.
338 * @param pVCpu Pointer to the VMCPU.
339 * @param HCPhys The physical address of the page.
340 * @param ppv Where to store the mapping address. The offset is
341 * preserved.
342 */
343DECLINLINE(int) pgmRZDynMapGCPageOffInlined(PVMCPU pVCpu, RTGCPHYS GCPhys, void **ppv RTLOG_COMMA_SRC_POS_DECL)
344{
345 STAM_PROFILE_START(&pVCpu->pgm.s.StatRZDynMapGCPageInl, a);
346
347 /*
348 * Get the ram range.
349 */
350 PVM pVM = pVCpu->CTX_SUFF(pVM);
351 PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(apRamRangesTlb)[PGM_RAMRANGE_TLB_IDX(GCPhys)];
352 RTGCPHYS off;
353 if ( !pRam
354 || (off = GCPhys - pRam->GCPhys) >= pRam->cb
355 /** @todo || page state stuff */
356 )
357 {
358 /* This case is not counted into StatRZDynMapGCPageInl. */
359 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapGCPageInlRamMisses);
360 return pgmRZDynMapGCPageCommon(pVM, pVCpu, GCPhys, ppv RTLOG_COMMA_SRC_POS_ARGS);
361 }
362
363 RTHCPHYS HCPhys = PGM_PAGE_GET_HCPHYS(&pRam->aPages[off >> PAGE_SHIFT]);
364 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapGCPageInlRamHits);
365
366 /*
367 * pgmRZDynMapHCPageInlined with out stats.
368 */
369 PPGMMAPSET pSet = &pVCpu->pgm.s.AutoSet;
370 Assert(!(HCPhys & PAGE_OFFSET_MASK));
371 Assert(pSet->cEntries <= RT_ELEMENTS(pSet->aEntries));
372
373 unsigned iHash = PGMMAPSET_HASH(HCPhys);
374 unsigned iEntry = pSet->aiHashTable[iHash];
375 if ( iEntry < pSet->cEntries
376 && pSet->aEntries[iEntry].HCPhys == HCPhys
377 && pSet->aEntries[iEntry].cInlinedRefs < UINT16_MAX - 1)
378 {
379 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapGCPageInlHits);
380 pSet->aEntries[iEntry].cInlinedRefs++;
381 *ppv = (void *)((uintptr_t)pSet->aEntries[iEntry].pvPage | (PAGE_OFFSET_MASK & (uintptr_t)GCPhys));
382 }
383 else
384 {
385 STAM_COUNTER_INC(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapGCPageInlMisses);
386 pgmRZDynMapHCPageCommon(pSet, HCPhys, ppv RTLOG_COMMA_SRC_POS_ARGS);
387 *ppv = (void *)((uintptr_t)*ppv | (PAGE_OFFSET_MASK & (uintptr_t)GCPhys));
388 }
389
390 STAM_PROFILE_STOP(&pVCpu->pgm.s.CTX_SUFF(pStats)->StatRZDynMapGCPageInl, a);
391 return VINF_SUCCESS;
392}
393
394#endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 */
395#if defined(IN_RC) || defined(VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0)
396
397/**
398 * Maps the page into current context (RC and maybe R0).
399 *
400 * @returns pointer to the mapping.
401 * @param pVM Pointer to the PGM instance data.
402 * @param pPage The page.
403 */
404DECLINLINE(void *) pgmPoolMapPageInlined(PVM pVM, PPGMPOOLPAGE pPage RTLOG_COMMA_SRC_POS_DECL)
405{
406 if (pPage->idx >= PGMPOOL_IDX_FIRST)
407 {
408 Assert(pPage->idx < pVM->pgm.s.CTX_SUFF(pPool)->cCurPages);
409 void *pv;
410 pgmRZDynMapHCPageInlined(VMMGetCpu(pVM), pPage->Core.Key, &pv RTLOG_COMMA_SRC_POS_ARGS);
411 return pv;
412 }
413 AssertFatalMsgFailed(("pgmPoolMapPageInlined invalid page index %x\n", pPage->idx));
414}
415
416
417/**
418 * Maps the page into current context (RC and maybe R0).
419 *
420 * @returns pointer to the mapping.
421 * @param pVM Pointer to the PGM instance data.
422 * @param pVCpu Pointer to the VMCPU.
423 * @param pPage The page.
424 */
425DECLINLINE(void *) pgmPoolMapPageV2Inlined(PVM pVM, PVMCPU pVCpu, PPGMPOOLPAGE pPage RTLOG_COMMA_SRC_POS_DECL)
426{
427 if (pPage->idx >= PGMPOOL_IDX_FIRST)
428 {
429 Assert(pPage->idx < pVM->pgm.s.CTX_SUFF(pPool)->cCurPages);
430 void *pv;
431 Assert(pVCpu == VMMGetCpu(pVM));
432 pgmRZDynMapHCPageInlined(pVCpu, pPage->Core.Key, &pv RTLOG_COMMA_SRC_POS_ARGS);
433 return pv;
434 }
435 AssertFatalMsgFailed(("pgmPoolMapPageV2Inlined invalid page index %x\n", pPage->idx));
436}
437
438#endif /* VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 || IN_RC */
439#ifndef IN_RC
440
441/**
442 * Queries the Physical TLB entry for a physical guest page,
443 * attempting to load the TLB entry if necessary.
444 *
445 * @returns VBox status code.
446 * @retval VINF_SUCCESS on success
447 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
448 *
449 * @param pVM Pointer to the VM.
450 * @param GCPhys The address of the guest page.
451 * @param ppTlbe Where to store the pointer to the TLB entry.
452 */
453DECLINLINE(int) pgmPhysPageQueryTlbe(PVM pVM, RTGCPHYS GCPhys, PPPGMPAGEMAPTLBE ppTlbe)
454{
455 int rc;
456 PPGMPAGEMAPTLBE pTlbe = &pVM->pgm.s.CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
457 if (pTlbe->GCPhys == (GCPhys & X86_PTE_PAE_PG_MASK))
458 {
459 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PageMapTlbHits));
460 rc = VINF_SUCCESS;
461 }
462 else
463 rc = pgmPhysPageLoadIntoTlb(pVM, GCPhys);
464 *ppTlbe = pTlbe;
465 return rc;
466}
467
468
469/**
470 * Queries the Physical TLB entry for a physical guest page,
471 * attempting to load the TLB entry if necessary.
472 *
473 * @returns VBox status code.
474 * @retval VINF_SUCCESS on success
475 * @retval VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS if it's not a valid physical address.
476 *
477 * @param pVM Pointer to the VM.
478 * @param pPage Pointer to the PGMPAGE structure corresponding to
479 * GCPhys.
480 * @param GCPhys The address of the guest page.
481 * @param ppTlbe Where to store the pointer to the TLB entry.
482 */
483DECLINLINE(int) pgmPhysPageQueryTlbeWithPage(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhys, PPPGMPAGEMAPTLBE ppTlbe)
484{
485 int rc;
486 PPGMPAGEMAPTLBE pTlbe = &pVM->pgm.s.CTXSUFF(PhysTlb).aEntries[PGM_PAGEMAPTLB_IDX(GCPhys)];
487 if (pTlbe->GCPhys == (GCPhys & X86_PTE_PAE_PG_MASK))
488 {
489 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PageMapTlbHits));
490 rc = VINF_SUCCESS;
491 AssertPtr(pTlbe->pv);
492# ifndef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
493 Assert(!pTlbe->pMap || RT_VALID_PTR(pTlbe->pMap->pv));
494# endif
495 }
496 else
497 rc = pgmPhysPageLoadIntoTlbWithPage(pVM, pPage, GCPhys);
498 *ppTlbe = pTlbe;
499 return rc;
500}
501
502#endif /* !IN_RC */
503
504/**
505 * Enables write monitoring for an allocated page.
506 *
507 * The caller is responsible for updating the shadow page tables.
508 *
509 * @param pVM Pointer to the VM.
510 * @param pPage The page to write monitor.
511 * @param GCPhysPage The address of the page.
512 */
513DECLINLINE(void) pgmPhysPageWriteMonitor(PVM pVM, PPGMPAGE pPage, RTGCPHYS GCPhysPage)
514{
515 Assert(PGM_PAGE_GET_STATE(pPage) == PGM_PAGE_STATE_ALLOCATED);
516 PGM_LOCK_ASSERT_OWNER(pVM);
517
518 PGM_PAGE_SET_STATE(pVM, pPage, PGM_PAGE_STATE_WRITE_MONITORED);
519 pVM->pgm.s.cMonitoredPages++;
520
521 /* Large pages must disabled. */
522 if (PGM_PAGE_GET_PDE_TYPE(pPage) == PGM_PAGE_PDE_TYPE_PDE)
523 {
524 PPGMPAGE pFirstPage = pgmPhysGetPage(pVM, GCPhysPage & X86_PDE2M_PAE_PG_MASK);
525 AssertFatal(pFirstPage);
526 if (PGM_PAGE_GET_PDE_TYPE(pFirstPage) == PGM_PAGE_PDE_TYPE_PDE)
527 {
528 PGM_PAGE_SET_PDE_TYPE(pVM, pFirstPage, PGM_PAGE_PDE_TYPE_PDE_DISABLED);
529 pVM->pgm.s.cLargePagesDisabled++;
530 }
531 else
532 Assert(PGM_PAGE_GET_PDE_TYPE(pFirstPage) == PGM_PAGE_PDE_TYPE_PDE_DISABLED);
533 }
534}
535
536
537/**
538 * Checks if the no-execute (NX) feature is active (EFER.NXE=1).
539 *
540 * Only used when the guest is in PAE or long mode. This is inlined so that we
541 * can perform consistency checks in debug builds.
542 *
543 * @returns true if it is, false if it isn't.
544 * @param pVCpu Pointer to the VMCPU.
545 */
546DECL_FORCE_INLINE(bool) pgmGstIsNoExecuteActive(PVMCPU pVCpu)
547{
548 Assert(pVCpu->pgm.s.fNoExecuteEnabled == CPUMIsGuestNXEnabled(pVCpu));
549 Assert(CPUMIsGuestInPAEMode(pVCpu) || CPUMIsGuestInLongMode(pVCpu));
550 return pVCpu->pgm.s.fNoExecuteEnabled;
551}
552
553
554/**
555 * Checks if the page size extension (PSE) is currently enabled (CR4.PSE=1).
556 *
557 * Only used when the guest is in paged 32-bit mode. This is inlined so that
558 * we can perform consistency checks in debug builds.
559 *
560 * @returns true if it is, false if it isn't.
561 * @param pVCpu Pointer to the VMCPU.
562 */
563DECL_FORCE_INLINE(bool) pgmGst32BitIsPageSizeExtActive(PVMCPU pVCpu)
564{
565 Assert(pVCpu->pgm.s.fGst32BitPageSizeExtension == CPUMIsGuestPageSizeExtEnabled(pVCpu));
566 Assert(!CPUMIsGuestInPAEMode(pVCpu));
567 Assert(!CPUMIsGuestInLongMode(pVCpu));
568 return pVCpu->pgm.s.fGst32BitPageSizeExtension;
569}
570
571
572/**
573 * Calculated the guest physical address of the large (4 MB) page in 32 bits paging mode.
574 * Takes PSE-36 into account.
575 *
576 * @returns guest physical address
577 * @param pVM Pointer to the VM.
578 * @param Pde Guest Pde
579 */
580DECLINLINE(RTGCPHYS) pgmGstGet4MBPhysPage(PVM pVM, X86PDE Pde)
581{
582 RTGCPHYS GCPhys = Pde.u & X86_PDE4M_PG_MASK;
583 GCPhys |= (RTGCPHYS)Pde.b.u8PageNoHigh << 32;
584
585 return GCPhys & pVM->pgm.s.GCPhys4MBPSEMask;
586}
587
588
589/**
590 * Gets the address the guest page directory (32-bit paging).
591 *
592 * @returns VBox status code.
593 * @param pVCpu Pointer to the VMCPU.
594 * @param ppPd Where to return the mapping. This is always set.
595 */
596DECLINLINE(int) pgmGstGet32bitPDPtrEx(PVMCPU pVCpu, PX86PD *ppPd)
597{
598#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
599 int rc = pgmRZDynMapGCPageInlined(pVCpu, pVCpu->pgm.s.GCPhysCR3, (void **)ppPd RTLOG_COMMA_SRC_POS);
600 if (RT_FAILURE(rc))
601 {
602 *ppPd = NULL;
603 return rc;
604 }
605#else
606 *ppPd = pVCpu->pgm.s.CTX_SUFF(pGst32BitPd);
607 if (RT_UNLIKELY(!*ppPd))
608 return pgmGstLazyMap32BitPD(pVCpu, ppPd);
609#endif
610 return VINF_SUCCESS;
611}
612
613
614/**
615 * Gets the address the guest page directory (32-bit paging).
616 *
617 * @returns Pointer to the page directory entry in question.
618 * @param pVCpu Pointer to the VMCPU.
619 */
620DECLINLINE(PX86PD) pgmGstGet32bitPDPtr(PVMCPU pVCpu)
621{
622#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
623 PX86PD pGuestPD = NULL;
624 int rc = pgmRZDynMapGCPageInlined(pVCpu, pVCpu->pgm.s.GCPhysCR3, (void **)&pGuestPD RTLOG_COMMA_SRC_POS);
625 if (RT_FAILURE(rc))
626 {
627 AssertMsg(rc == VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS, ("%Rrc\n", rc));
628 return NULL;
629 }
630#else
631 PX86PD pGuestPD = pVCpu->pgm.s.CTX_SUFF(pGst32BitPd);
632 if (RT_UNLIKELY(!pGuestPD))
633 {
634 int rc = pgmGstLazyMap32BitPD(pVCpu, &pGuestPD);
635 if (RT_FAILURE(rc))
636 return NULL;
637 }
638#endif
639 return pGuestPD;
640}
641
642
643/**
644 * Gets the guest page directory pointer table.
645 *
646 * @returns VBox status code.
647 * @param pVCpu Pointer to the VMCPU.
648 * @param ppPdpt Where to return the mapping. This is always set.
649 */
650DECLINLINE(int) pgmGstGetPaePDPTPtrEx(PVMCPU pVCpu, PX86PDPT *ppPdpt)
651{
652#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
653 int rc = pgmRZDynMapGCPageOffInlined(pVCpu, pVCpu->pgm.s.GCPhysCR3, (void **)ppPdpt RTLOG_COMMA_SRC_POS);
654 if (RT_FAILURE(rc))
655 {
656 *ppPdpt = NULL;
657 return rc;
658 }
659#else
660 *ppPdpt = pVCpu->pgm.s.CTX_SUFF(pGstPaePdpt);
661 if (RT_UNLIKELY(!*ppPdpt))
662 return pgmGstLazyMapPaePDPT(pVCpu, ppPdpt);
663#endif
664 return VINF_SUCCESS;
665}
666
667
668/**
669 * Gets the guest page directory pointer table.
670 *
671 * @returns Pointer to the page directory in question.
672 * @returns NULL if the page directory is not present or on an invalid page.
673 * @param pVCpu Pointer to the VMCPU.
674 */
675DECLINLINE(PX86PDPT) pgmGstGetPaePDPTPtr(PVMCPU pVCpu)
676{
677 PX86PDPT pGuestPdpt;
678 int rc = pgmGstGetPaePDPTPtrEx(pVCpu, &pGuestPdpt);
679 AssertMsg(RT_SUCCESS(rc) || rc == VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS, ("%Rrc\n", rc)); NOREF(rc);
680 return pGuestPdpt;
681}
682
683
684/**
685 * Gets the guest page directory pointer table entry for the specified address.
686 *
687 * @returns Pointer to the page directory in question.
688 * @returns NULL if the page directory is not present or on an invalid page.
689 * @param pVCpu Pointer to the VMCPU.
690 * @param GCPtr The address.
691 */
692DECLINLINE(PX86PDPE) pgmGstGetPaePDPEPtr(PVMCPU pVCpu, RTGCPTR GCPtr)
693{
694 AssertGCPtr32(GCPtr);
695
696#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
697 PX86PDPT pGuestPDPT = NULL;
698 int rc = pgmRZDynMapGCPageOffInlined(pVCpu, pVCpu->pgm.s.GCPhysCR3, (void **)&pGuestPDPT RTLOG_COMMA_SRC_POS);
699 AssertRCReturn(rc, NULL);
700#else
701 PX86PDPT pGuestPDPT = pVCpu->pgm.s.CTX_SUFF(pGstPaePdpt);
702 if (RT_UNLIKELY(!pGuestPDPT))
703 {
704 int rc = pgmGstLazyMapPaePDPT(pVCpu, &pGuestPDPT);
705 if (RT_FAILURE(rc))
706 return NULL;
707 }
708#endif
709 return &pGuestPDPT->a[(GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE];
710}
711
712
713/**
714 * Gets the page directory entry for the specified address.
715 *
716 * @returns The page directory entry in question.
717 * @returns A non-present entry if the page directory is not present or on an invalid page.
718 * @param pVCpu The handle of the virtual CPU.
719 * @param GCPtr The address.
720 */
721DECLINLINE(X86PDEPAE) pgmGstGetPaePDE(PVMCPU pVCpu, RTGCPTR GCPtr)
722{
723 AssertGCPtr32(GCPtr);
724 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pVCpu);
725 if (RT_LIKELY(pGuestPDPT))
726 {
727 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
728 if ( pGuestPDPT->a[iPdpt].n.u1Present
729 && !(pGuestPDPT->a[iPdpt].u & pVCpu->pgm.s.fGstPaeMbzPdpeMask) )
730 {
731 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
732#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
733 PX86PDPAE pGuestPD = NULL;
734 int rc = pgmRZDynMapGCPageInlined(pVCpu,
735 pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK,
736 (void **)&pGuestPD
737 RTLOG_COMMA_SRC_POS);
738 if (RT_SUCCESS(rc))
739 return pGuestPD->a[iPD];
740 AssertMsg(rc == VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS, ("%Rrc\n", rc));
741#else
742 PX86PDPAE pGuestPD = pVCpu->pgm.s.CTX_SUFF(apGstPaePDs)[iPdpt];
743 if ( !pGuestPD
744 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pVCpu->pgm.s.aGCPhysGstPaePDs[iPdpt])
745 pgmGstLazyMapPaePD(pVCpu, iPdpt, &pGuestPD);
746 if (pGuestPD)
747 return pGuestPD->a[iPD];
748#endif
749 }
750 }
751
752 X86PDEPAE ZeroPde = {0};
753 return ZeroPde;
754}
755
756
757/**
758 * Gets the page directory pointer table entry for the specified address
759 * and returns the index into the page directory
760 *
761 * @returns Pointer to the page directory in question.
762 * @returns NULL if the page directory is not present or on an invalid page.
763 * @param pVCpu Pointer to the VMCPU.
764 * @param GCPtr The address.
765 * @param piPD Receives the index into the returned page directory
766 * @param pPdpe Receives the page directory pointer entry. Optional.
767 */
768DECLINLINE(PX86PDPAE) pgmGstGetPaePDPtr(PVMCPU pVCpu, RTGCPTR GCPtr, unsigned *piPD, PX86PDPE pPdpe)
769{
770 AssertGCPtr32(GCPtr);
771
772 /* The PDPE. */
773 PX86PDPT pGuestPDPT = pgmGstGetPaePDPTPtr(pVCpu);
774 if (RT_UNLIKELY(!pGuestPDPT))
775 return NULL;
776 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
777 if (pPdpe)
778 *pPdpe = pGuestPDPT->a[iPdpt];
779 if (!pGuestPDPT->a[iPdpt].n.u1Present)
780 return NULL;
781 if (RT_UNLIKELY(pVCpu->pgm.s.fGstPaeMbzPdpeMask & pGuestPDPT->a[iPdpt].u))
782 return NULL;
783
784 /* The PDE. */
785#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
786 PX86PDPAE pGuestPD = NULL;
787 int rc = pgmRZDynMapGCPageInlined(pVCpu,
788 pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK,
789 (void **)&pGuestPD
790 RTLOG_COMMA_SRC_POS);
791 if (RT_FAILURE(rc))
792 {
793 AssertMsg(rc == VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS, ("%Rrc\n", rc));
794 return NULL;
795 }
796#else
797 PX86PDPAE pGuestPD = pVCpu->pgm.s.CTX_SUFF(apGstPaePDs)[iPdpt];
798 if ( !pGuestPD
799 || (pGuestPDPT->a[iPdpt].u & X86_PDPE_PG_MASK) != pVCpu->pgm.s.aGCPhysGstPaePDs[iPdpt])
800 pgmGstLazyMapPaePD(pVCpu, iPdpt, &pGuestPD);
801#endif
802
803 *piPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
804 return pGuestPD;
805}
806
807#ifndef IN_RC
808
809/**
810 * Gets the page map level-4 pointer for the guest.
811 *
812 * @returns VBox status code.
813 * @param pVCpu Pointer to the VMCPU.
814 * @param ppPml4 Where to return the mapping. Always set.
815 */
816DECLINLINE(int) pgmGstGetLongModePML4PtrEx(PVMCPU pVCpu, PX86PML4 *ppPml4)
817{
818#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
819 int rc = pgmRZDynMapGCPageInlined(pVCpu, pVCpu->pgm.s.GCPhysCR3, (void **)ppPml4 RTLOG_COMMA_SRC_POS);
820 if (RT_FAILURE(rc))
821 {
822 *ppPml4 = NULL;
823 return rc;
824 }
825#else
826 *ppPml4 = pVCpu->pgm.s.CTX_SUFF(pGstAmd64Pml4);
827 if (RT_UNLIKELY(!*ppPml4))
828 return pgmGstLazyMapPml4(pVCpu, ppPml4);
829#endif
830 return VINF_SUCCESS;
831}
832
833
834/**
835 * Gets the page map level-4 pointer for the guest.
836 *
837 * @returns Pointer to the PML4 page.
838 * @param pVCpu Pointer to the VMCPU.
839 */
840DECLINLINE(PX86PML4) pgmGstGetLongModePML4Ptr(PVMCPU pVCpu)
841{
842 PX86PML4 pGuestPml4;
843 int rc = pgmGstGetLongModePML4PtrEx(pVCpu, &pGuestPml4);
844 AssertMsg(RT_SUCCESS(rc) || rc == VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS, ("%Rrc\n", rc)); NOREF(rc);
845 return pGuestPml4;
846}
847
848
849/**
850 * Gets the pointer to a page map level-4 entry.
851 *
852 * @returns Pointer to the PML4 entry.
853 * @param pVCpu Pointer to the VMCPU.
854 * @param iPml4 The index.
855 * @remarks Only used by AssertCR3.
856 */
857DECLINLINE(PX86PML4E) pgmGstGetLongModePML4EPtr(PVMCPU pVCpu, unsigned int iPml4)
858{
859#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
860 PX86PML4 pGuestPml4;
861 int rc = pgmRZDynMapGCPageInlined(pVCpu, pVCpu->pgm.s.GCPhysCR3, (void **)&pGuestPml4 RTLOG_COMMA_SRC_POS);
862 AssertRCReturn(rc, NULL);
863#else
864 PX86PML4 pGuestPml4 = pVCpu->pgm.s.CTX_SUFF(pGstAmd64Pml4);
865 if (RT_UNLIKELY(!pGuestPml4))
866 {
867 int rc = pgmGstLazyMapPml4(pVCpu, &pGuestPml4);
868 AssertRCReturn(rc, NULL);
869 }
870#endif
871 return &pGuestPml4->a[iPml4];
872}
873
874
875/**
876 * Gets the page directory entry for the specified address.
877 *
878 * @returns The page directory entry in question.
879 * @returns A non-present entry if the page directory is not present or on an invalid page.
880 * @param pVCpu Pointer to the VMCPU.
881 * @param GCPtr The address.
882 */
883DECLINLINE(X86PDEPAE) pgmGstGetLongModePDE(PVMCPU pVCpu, RTGCPTR64 GCPtr)
884{
885 /*
886 * Note! To keep things simple, ASSUME invalid physical addresses will
887 * cause X86_TRAP_PF_RSVD. This isn't a problem until we start
888 * supporting 52-bit wide physical guest addresses.
889 */
890 PCX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pVCpu);
891 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
892 if ( RT_LIKELY(pGuestPml4)
893 && pGuestPml4->a[iPml4].n.u1Present
894 && !(pGuestPml4->a[iPml4].u & pVCpu->pgm.s.fGstAmd64MbzPml4eMask) )
895 {
896 PCX86PDPT pPdptTemp;
897 int rc = PGM_GCPHYS_2_PTR_BY_VMCPU(pVCpu, pGuestPml4->a[iPml4].u & X86_PML4E_PG_MASK, &pPdptTemp);
898 if (RT_SUCCESS(rc))
899 {
900 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
901 if ( pPdptTemp->a[iPdpt].n.u1Present
902 && !(pPdptTemp->a[iPdpt].u & pVCpu->pgm.s.fGstAmd64MbzPdpeMask) )
903 {
904 PCX86PDPAE pPD;
905 rc = PGM_GCPHYS_2_PTR_BY_VMCPU(pVCpu, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
906 if (RT_SUCCESS(rc))
907 {
908 const unsigned iPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
909 return pPD->a[iPD];
910 }
911 }
912 }
913 AssertMsg(RT_SUCCESS(rc) || rc == VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS, ("%Rrc\n", rc));
914 }
915
916 X86PDEPAE ZeroPde = {0};
917 return ZeroPde;
918}
919
920
921/**
922 * Gets the GUEST page directory pointer for the specified address.
923 *
924 * @returns The page directory in question.
925 * @returns NULL if the page directory is not present or on an invalid page.
926 * @param pVCpu Pointer to the VMCPU.
927 * @param GCPtr The address.
928 * @param ppPml4e Page Map Level-4 Entry (out)
929 * @param pPdpe Page directory pointer table entry (out)
930 * @param piPD Receives the index into the returned page directory
931 */
932DECLINLINE(PX86PDPAE) pgmGstGetLongModePDPtr(PVMCPU pVCpu, RTGCPTR64 GCPtr, PX86PML4E *ppPml4e, PX86PDPE pPdpe, unsigned *piPD)
933{
934 /* The PMLE4. */
935 PX86PML4 pGuestPml4 = pgmGstGetLongModePML4Ptr(pVCpu);
936 if (RT_UNLIKELY(!pGuestPml4))
937 return NULL;
938 const unsigned iPml4 = (GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
939 PCX86PML4E pPml4e = *ppPml4e = &pGuestPml4->a[iPml4];
940 if (!pPml4e->n.u1Present)
941 return NULL;
942 if (RT_UNLIKELY(pPml4e->u & pVCpu->pgm.s.fGstAmd64MbzPml4eMask))
943 return NULL;
944
945 /* The PDPE. */
946 PCX86PDPT pPdptTemp;
947 int rc = PGM_GCPHYS_2_PTR_BY_VMCPU(pVCpu, pPml4e->u & X86_PML4E_PG_MASK, &pPdptTemp);
948 if (RT_FAILURE(rc))
949 {
950 AssertMsg(rc == VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS, ("%Rrc\n", rc));
951 return NULL;
952 }
953 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_AMD64;
954 *pPdpe = pPdptTemp->a[iPdpt];
955 if (!pPdpe->n.u1Present)
956 return NULL;
957 if (RT_UNLIKELY(pPdpe->u & pVCpu->pgm.s.fGstAmd64MbzPdpeMask))
958 return NULL;
959
960 /* The PDE. */
961 PX86PDPAE pPD;
962 rc = PGM_GCPHYS_2_PTR_BY_VMCPU(pVCpu, pPdptTemp->a[iPdpt].u & X86_PDPE_PG_MASK, &pPD);
963 if (RT_FAILURE(rc))
964 {
965 AssertMsg(rc == VERR_PGM_INVALID_GC_PHYSICAL_ADDRESS, ("%Rrc\n", rc));
966 return NULL;
967 }
968
969 *piPD = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
970 return pPD;
971}
972
973#endif /* !IN_RC */
974
975/**
976 * Gets the shadow page directory, 32-bit.
977 *
978 * @returns Pointer to the shadow 32-bit PD.
979 * @param pVCpu Pointer to the VMCPU.
980 */
981DECLINLINE(PX86PD) pgmShwGet32BitPDPtr(PVMCPU pVCpu)
982{
983 return (PX86PD)PGMPOOL_PAGE_2_PTR_V2(pVCpu->CTX_SUFF(pVM), pVCpu, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
984}
985
986
987/**
988 * Gets the shadow page directory entry for the specified address, 32-bit.
989 *
990 * @returns Shadow 32-bit PDE.
991 * @param pVCpu Pointer to the VMCPU.
992 * @param GCPtr The address.
993 */
994DECLINLINE(X86PDE) pgmShwGet32BitPDE(PVMCPU pVCpu, RTGCPTR GCPtr)
995{
996 const unsigned iPd = (GCPtr >> X86_PD_SHIFT) & X86_PD_MASK;
997
998 PX86PD pShwPde = pgmShwGet32BitPDPtr(pVCpu);
999 if (!pShwPde)
1000 {
1001 X86PDE ZeroPde = {0};
1002 return ZeroPde;
1003 }
1004 return pShwPde->a[iPd];
1005}
1006
1007
1008/**
1009 * Gets the pointer to the shadow page directory entry for the specified
1010 * address, 32-bit.
1011 *
1012 * @returns Pointer to the shadow 32-bit PDE.
1013 * @param pVCpu Pointer to the VMCPU.
1014 * @param GCPtr The address.
1015 */
1016DECLINLINE(PX86PDE) pgmShwGet32BitPDEPtr(PVMCPU pVCpu, RTGCPTR GCPtr)
1017{
1018 const unsigned iPd = (GCPtr >> X86_PD_SHIFT) & X86_PD_MASK;
1019
1020 PX86PD pPde = pgmShwGet32BitPDPtr(pVCpu);
1021 AssertReturn(pPde, NULL);
1022 return &pPde->a[iPd];
1023}
1024
1025
1026/**
1027 * Gets the shadow page pointer table, PAE.
1028 *
1029 * @returns Pointer to the shadow PAE PDPT.
1030 * @param pVCpu Pointer to the VMCPU.
1031 */
1032DECLINLINE(PX86PDPT) pgmShwGetPaePDPTPtr(PVMCPU pVCpu)
1033{
1034 return (PX86PDPT)PGMPOOL_PAGE_2_PTR_V2(pVCpu->CTX_SUFF(pVM), pVCpu, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
1035}
1036
1037
1038/**
1039 * Gets the shadow page directory for the specified address, PAE.
1040 *
1041 * @returns Pointer to the shadow PD.
1042 * @param pVCpu Pointer to the VMCPU.
1043 * @param GCPtr The address.
1044 */
1045DECLINLINE(PX86PDPAE) pgmShwGetPaePDPtr(PVMCPU pVCpu, RTGCPTR GCPtr)
1046{
1047 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
1048 PX86PDPT pPdpt = pgmShwGetPaePDPTPtr(pVCpu);
1049
1050 if (!pPdpt->a[iPdpt].n.u1Present)
1051 return NULL;
1052
1053 /* Fetch the pgm pool shadow descriptor. */
1054 PVM pVM = pVCpu->CTX_SUFF(pVM);
1055 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(pVM->pgm.s.CTX_SUFF(pPool), pPdpt->a[iPdpt].u & X86_PDPE_PG_MASK);
1056 AssertReturn(pShwPde, NULL);
1057
1058 return (PX86PDPAE)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPde);
1059}
1060
1061
1062/**
1063 * Gets the shadow page directory for the specified address, PAE.
1064 *
1065 * @returns Pointer to the shadow PD.
1066 * @param pVCpu Pointer to the VMCPU.
1067 * @param GCPtr The address.
1068 */
1069DECLINLINE(PX86PDPAE) pgmShwGetPaePDPtr(PVMCPU pVCpu, PX86PDPT pPdpt, RTGCPTR GCPtr)
1070{
1071 const unsigned iPdpt = (GCPtr >> X86_PDPT_SHIFT) & X86_PDPT_MASK_PAE;
1072
1073 if (!pPdpt->a[iPdpt].n.u1Present)
1074 return NULL;
1075
1076 /* Fetch the pgm pool shadow descriptor. */
1077 PVM pVM = pVCpu->CTX_SUFF(pVM);
1078 PPGMPOOLPAGE pShwPde = pgmPoolGetPage(pVM->pgm.s.CTX_SUFF(pPool), pPdpt->a[iPdpt].u & X86_PDPE_PG_MASK);
1079 AssertReturn(pShwPde, NULL);
1080
1081 return (PX86PDPAE)PGMPOOL_PAGE_2_PTR_V2(pVM, pVCpu, pShwPde);
1082}
1083
1084
1085/**
1086 * Gets the shadow page directory entry, PAE.
1087 *
1088 * @returns PDE.
1089 * @param pVCpu Pointer to the VMCPU.
1090 * @param GCPtr The address.
1091 */
1092DECLINLINE(X86PDEPAE) pgmShwGetPaePDE(PVMCPU pVCpu, RTGCPTR GCPtr)
1093{
1094 const unsigned iPd = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
1095
1096 PX86PDPAE pShwPde = pgmShwGetPaePDPtr(pVCpu, GCPtr);
1097 if (!pShwPde)
1098 {
1099 X86PDEPAE ZeroPde = {0};
1100 return ZeroPde;
1101 }
1102 return pShwPde->a[iPd];
1103}
1104
1105
1106/**
1107 * Gets the pointer to the shadow page directory entry for an address, PAE.
1108 *
1109 * @returns Pointer to the PDE.
1110 * @param pVCpu Pointer to the VMCPU.
1111 * @param GCPtr The address.
1112 * @remarks Only used by AssertCR3.
1113 */
1114DECLINLINE(PX86PDEPAE) pgmShwGetPaePDEPtr(PVMCPU pVCpu, RTGCPTR GCPtr)
1115{
1116 const unsigned iPd = (GCPtr >> X86_PD_PAE_SHIFT) & X86_PD_PAE_MASK;
1117
1118 PX86PDPAE pPde = pgmShwGetPaePDPtr(pVCpu, GCPtr);
1119 AssertReturn(pPde, NULL);
1120 return &pPde->a[iPd];
1121}
1122
1123#ifndef IN_RC
1124
1125/**
1126 * Gets the shadow page map level-4 pointer.
1127 *
1128 * @returns Pointer to the shadow PML4.
1129 * @param pVCpu Pointer to the VMCPU.
1130 */
1131DECLINLINE(PX86PML4) pgmShwGetLongModePML4Ptr(PVMCPU pVCpu)
1132{
1133 return (PX86PML4)PGMPOOL_PAGE_2_PTR_V2(pVCpu->CTX_SUFF(pVM), pVCpu, pVCpu->pgm.s.CTX_SUFF(pShwPageCR3));
1134}
1135
1136
1137/**
1138 * Gets the shadow page map level-4 entry for the specified address.
1139 *
1140 * @returns The entry.
1141 * @param pVCpu Pointer to the VMCPU.
1142 * @param GCPtr The address.
1143 */
1144DECLINLINE(X86PML4E) pgmShwGetLongModePML4E(PVMCPU pVCpu, RTGCPTR GCPtr)
1145{
1146 const unsigned iPml4 = ((RTGCUINTPTR64)GCPtr >> X86_PML4_SHIFT) & X86_PML4_MASK;
1147 PX86PML4 pShwPml4 = pgmShwGetLongModePML4Ptr(pVCpu);
1148
1149 if (!pShwPml4)
1150 {
1151 X86PML4E ZeroPml4e = {0};
1152 return ZeroPml4e;
1153 }
1154 return pShwPml4->a[iPml4];
1155}
1156
1157
1158/**
1159 * Gets the pointer to the specified shadow page map level-4 entry.
1160 *
1161 * @returns The entry.
1162 * @param pVCpu Pointer to the VMCPU.
1163 * @param iPml4 The PML4 index.
1164 */
1165DECLINLINE(PX86PML4E) pgmShwGetLongModePML4EPtr(PVMCPU pVCpu, unsigned int iPml4)
1166{
1167 PX86PML4 pShwPml4 = pgmShwGetLongModePML4Ptr(pVCpu);
1168 if (!pShwPml4)
1169 return NULL;
1170 return &pShwPml4->a[iPml4];
1171}
1172
1173#endif /* !IN_RC */
1174
1175/**
1176 * Cached physical handler lookup.
1177 *
1178 * @returns Physical handler covering @a GCPhys.
1179 * @param pVM Pointer to the VM.
1180 * @param GCPhys The lookup address.
1181 */
1182DECLINLINE(PPGMPHYSHANDLER) pgmHandlerPhysicalLookup(PVM pVM, RTGCPHYS GCPhys)
1183{
1184 PPGMPHYSHANDLER pHandler = pVM->pgm.s.CTX_SUFF(pLastPhysHandler);
1185 if ( pHandler
1186 && GCPhys >= pHandler->Core.Key
1187 && GCPhys < pHandler->Core.KeyLast)
1188 {
1189 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysHandlerLookupHits));
1190 return pHandler;
1191 }
1192
1193 STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->CTX_MID_Z(Stat,PhysHandlerLookupMisses));
1194 pHandler = (PPGMPHYSHANDLER)RTAvlroGCPhysRangeGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysHandlers, GCPhys);
1195 if (pHandler)
1196 pVM->pgm.s.CTX_SUFF(pLastPhysHandler) = pHandler;
1197 return pHandler;
1198}
1199
1200
1201/**
1202 * Gets the page state for a physical handler.
1203 *
1204 * @returns The physical handler page state.
1205 * @param pCur The physical handler in question.
1206 */
1207DECLINLINE(unsigned) pgmHandlerPhysicalCalcState(PPGMPHYSHANDLER pCur)
1208{
1209 switch (pCur->enmType)
1210 {
1211 case PGMPHYSHANDLERTYPE_PHYSICAL_WRITE:
1212 return PGM_PAGE_HNDL_PHYS_STATE_WRITE;
1213
1214 case PGMPHYSHANDLERTYPE_MMIO:
1215 case PGMPHYSHANDLERTYPE_PHYSICAL_ALL:
1216 return PGM_PAGE_HNDL_PHYS_STATE_ALL;
1217
1218 default:
1219 AssertFatalMsgFailed(("Invalid type %d\n", pCur->enmType));
1220 }
1221}
1222
1223
1224/**
1225 * Gets the page state for a virtual handler.
1226 *
1227 * @returns The virtual handler page state.
1228 * @param pCur The virtual handler in question.
1229 * @remarks This should never be used on a hypervisor access handler.
1230 */
1231DECLINLINE(unsigned) pgmHandlerVirtualCalcState(PPGMVIRTHANDLER pCur)
1232{
1233 switch (pCur->enmType)
1234 {
1235 case PGMVIRTHANDLERTYPE_WRITE:
1236 return PGM_PAGE_HNDL_VIRT_STATE_WRITE;
1237 case PGMVIRTHANDLERTYPE_ALL:
1238 return PGM_PAGE_HNDL_VIRT_STATE_ALL;
1239 default:
1240 AssertFatalMsgFailed(("Invalid type %d\n", pCur->enmType));
1241 }
1242}
1243
1244
1245/**
1246 * Clears one physical page of a virtual handler.
1247 *
1248 * @param pVM Pointer to the VM.
1249 * @param pCur Virtual handler structure.
1250 * @param iPage Physical page index.
1251 *
1252 * @remark Only used when PGM_SYNC_UPDATE_PAGE_BIT_VIRTUAL is being set, so no
1253 * need to care about other handlers in the same page.
1254 */
1255DECLINLINE(void) pgmHandlerVirtualClearPage(PVM pVM, PPGMVIRTHANDLER pCur, unsigned iPage)
1256{
1257 const PPGMPHYS2VIRTHANDLER pPhys2Virt = &pCur->aPhysToVirt[iPage];
1258
1259 /*
1260 * Remove the node from the tree (it's supposed to be in the tree if we get here!).
1261 */
1262#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1263 AssertReleaseMsg(pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE,
1264 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
1265 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias));
1266#endif
1267 if (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_IS_HEAD)
1268 {
1269 /* We're the head of the alias chain. */
1270 PPGMPHYS2VIRTHANDLER pRemove = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysRemove(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key); NOREF(pRemove);
1271#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1272 AssertReleaseMsg(pRemove != NULL,
1273 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
1274 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias));
1275 AssertReleaseMsg(pRemove == pPhys2Virt,
1276 ("wanted: pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n"
1277 " got: pRemove=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
1278 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias,
1279 pRemove, pRemove->Core.Key, pRemove->Core.KeyLast, pRemove->offVirtHandler, pRemove->offNextAlias));
1280#endif
1281 if (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK)
1282 {
1283 /* Insert the next list in the alias chain into the tree. */
1284 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
1285#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1286 AssertReleaseMsg(pNext->offNextAlias & PGMPHYS2VIRTHANDLER_IN_TREE,
1287 ("pNext=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32}\n",
1288 pNext, pNext->Core.Key, pNext->Core.KeyLast, pNext->offVirtHandler, pNext->offNextAlias));
1289#endif
1290 pNext->offNextAlias |= PGMPHYS2VIRTHANDLER_IS_HEAD;
1291 bool fRc = RTAvlroGCPhysInsert(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, &pNext->Core);
1292 AssertRelease(fRc);
1293 }
1294 }
1295 else
1296 {
1297 /* Locate the previous node in the alias chain. */
1298 PPGMPHYS2VIRTHANDLER pPrev = (PPGMPHYS2VIRTHANDLER)RTAvlroGCPhysGet(&pVM->pgm.s.CTX_SUFF(pTrees)->PhysToVirtHandlers, pPhys2Virt->Core.Key);
1299#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1300 AssertReleaseMsg(pPrev != pPhys2Virt,
1301 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} pPrev=%p\n",
1302 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias, pPrev));
1303#endif
1304 for (;;)
1305 {
1306 PPGMPHYS2VIRTHANDLER pNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPrev + (pPrev->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
1307 if (pNext == pPhys2Virt)
1308 {
1309 /* unlink. */
1310 LogFlow(("pgmHandlerVirtualClearPage: removed %p:{.offNextAlias=%#RX32} from alias chain. prev %p:{.offNextAlias=%#RX32} [%RGp-%RGp]\n",
1311 pPhys2Virt, pPhys2Virt->offNextAlias, pPrev, pPrev->offNextAlias, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast));
1312 if (!(pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK))
1313 pPrev->offNextAlias &= ~PGMPHYS2VIRTHANDLER_OFF_MASK;
1314 else
1315 {
1316 PPGMPHYS2VIRTHANDLER pNewNext = (PPGMPHYS2VIRTHANDLER)((intptr_t)pPhys2Virt + (pPhys2Virt->offNextAlias & PGMPHYS2VIRTHANDLER_OFF_MASK));
1317 pPrev->offNextAlias = ((intptr_t)pNewNext - (intptr_t)pPrev)
1318 | (pPrev->offNextAlias & ~PGMPHYS2VIRTHANDLER_OFF_MASK);
1319 }
1320 break;
1321 }
1322
1323 /* next */
1324 if (pNext == pPrev)
1325 {
1326#ifdef VBOX_STRICT_PGM_HANDLER_VIRTUAL
1327 AssertReleaseMsg(pNext != pPrev,
1328 ("pPhys2Virt=%p:{.Core.Key=%RGp, .Core.KeyLast=%RGp, .offVirtHandler=%#RX32, .offNextAlias=%#RX32} pPrev=%p\n",
1329 pPhys2Virt, pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offVirtHandler, pPhys2Virt->offNextAlias, pPrev));
1330#endif
1331 break;
1332 }
1333 pPrev = pNext;
1334 }
1335 }
1336 Log2(("PHYS2VIRT: Removing %RGp-%RGp %#RX32 %s\n",
1337 pPhys2Virt->Core.Key, pPhys2Virt->Core.KeyLast, pPhys2Virt->offNextAlias, R3STRING(pCur->pszDesc)));
1338 pPhys2Virt->offNextAlias = 0;
1339 pPhys2Virt->Core.KeyLast = NIL_RTGCPHYS; /* require reinsert */
1340
1341 /*
1342 * Clear the ram flags for this page.
1343 */
1344 PPGMPAGE pPage = pgmPhysGetPage(pVM, pPhys2Virt->Core.Key);
1345 AssertReturnVoid(pPage);
1346 PGM_PAGE_SET_HNDL_VIRT_STATE(pPage, PGM_PAGE_HNDL_VIRT_STATE_NONE);
1347}
1348
1349
1350/**
1351 * Internal worker for finding a 'in-use' shadow page give by it's physical address.
1352 *
1353 * @returns Pointer to the shadow page structure.
1354 * @param pPool The pool.
1355 * @param idx The pool page index.
1356 */
1357DECLINLINE(PPGMPOOLPAGE) pgmPoolGetPageByIdx(PPGMPOOL pPool, unsigned idx)
1358{
1359 AssertFatalMsg(idx >= PGMPOOL_IDX_FIRST && idx < pPool->cCurPages, ("idx=%d\n", idx));
1360 return &pPool->aPages[idx];
1361}
1362
1363
1364/**
1365 * Clear references to guest physical memory.
1366 *
1367 * @param pPool The pool.
1368 * @param pPoolPage The pool page.
1369 * @param pPhysPage The physical guest page tracking structure.
1370 * @param iPte Shadow PTE index
1371 */
1372DECLINLINE(void) pgmTrackDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPoolPage, PPGMPAGE pPhysPage, uint16_t iPte)
1373{
1374 /*
1375 * Just deal with the simple case here.
1376 */
1377# ifdef VBOX_STRICT
1378 PVM pVM = pPool->CTX_SUFF(pVM); NOREF(pVM);
1379# endif
1380# ifdef LOG_ENABLED
1381 const unsigned uOrg = PGM_PAGE_GET_TRACKING(pPhysPage);
1382# endif
1383 const unsigned cRefs = PGM_PAGE_GET_TD_CREFS(pPhysPage);
1384 if (cRefs == 1)
1385 {
1386 Assert(pPoolPage->idx == PGM_PAGE_GET_TD_IDX(pPhysPage));
1387 Assert(iPte == PGM_PAGE_GET_PTE_INDEX(pPhysPage));
1388 /* Invalidate the tracking data. */
1389 PGM_PAGE_SET_TRACKING(pVM, pPhysPage, 0);
1390 }
1391 else
1392 pgmPoolTrackPhysExtDerefGCPhys(pPool, pPoolPage, pPhysPage, iPte);
1393 Log2(("pgmTrackDerefGCPhys: %x -> %x pPhysPage=%R[pgmpage]\n", uOrg, PGM_PAGE_GET_TRACKING(pPhysPage), pPhysPage ));
1394}
1395
1396
1397/**
1398 * Moves the page to the head of the age list.
1399 *
1400 * This is done when the cached page is used in one way or another.
1401 *
1402 * @param pPool The pool.
1403 * @param pPage The cached page.
1404 */
1405DECLINLINE(void) pgmPoolCacheUsed(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1406{
1407 PGM_LOCK_ASSERT_OWNER(pPool->CTX_SUFF(pVM));
1408
1409 /*
1410 * Move to the head of the age list.
1411 */
1412 if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
1413 {
1414 /* unlink */
1415 pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
1416 if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
1417 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
1418 else
1419 pPool->iAgeTail = pPage->iAgePrev;
1420
1421 /* insert at head */
1422 pPage->iAgePrev = NIL_PGMPOOL_IDX;
1423 pPage->iAgeNext = pPool->iAgeHead;
1424 Assert(pPage->iAgeNext != NIL_PGMPOOL_IDX); /* we would've already been head then */
1425 pPool->iAgeHead = pPage->idx;
1426 pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->idx;
1427 }
1428}
1429
1430
1431/**
1432 * Locks a page to prevent flushing (important for cr3 root pages or shadow pae pd pages).
1433 *
1434 * @param pVM Pointer to the VM.
1435 * @param pPage PGM pool page
1436 */
1437DECLINLINE(void) pgmPoolLockPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1438{
1439 PGM_LOCK_ASSERT_OWNER(pPool->CTX_SUFF(pVM));
1440 ASMAtomicIncU32(&pPage->cLocked);
1441}
1442
1443
1444/**
1445 * Unlocks a page to allow flushing again
1446 *
1447 * @param pVM Pointer to the VM.
1448 * @param pPage PGM pool page
1449 */
1450DECLINLINE(void) pgmPoolUnlockPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
1451{
1452 PGM_LOCK_ASSERT_OWNER(pPool->CTX_SUFF(pVM));
1453 Assert(pPage->cLocked);
1454 ASMAtomicDecU32(&pPage->cLocked);
1455}
1456
1457
1458/**
1459 * Checks if the page is locked (e.g. the active CR3 or one of the four PDs of a PAE PDPT)
1460 *
1461 * @returns VBox status code.
1462 * @param pPage PGM pool page
1463 */
1464DECLINLINE(bool) pgmPoolIsPageLocked(PPGMPOOLPAGE pPage)
1465{
1466 if (pPage->cLocked)
1467 {
1468 LogFlow(("pgmPoolIsPageLocked found root page %d\n", pPage->enmKind));
1469 if (pPage->cModifications)
1470 pPage->cModifications = 1; /* reset counter (can't use 0, or else it will be reinserted in the modified list) */
1471 return true;
1472 }
1473 return false;
1474}
1475
1476
1477/**
1478 * Tells if mappings are to be put into the shadow page table or not.
1479 *
1480 * @returns boolean result
1481 * @param pVM Pointer to the VM.
1482 */
1483DECL_FORCE_INLINE(bool) pgmMapAreMappingsEnabled(PVM pVM)
1484{
1485#ifdef PGM_WITHOUT_MAPPINGS
1486 /* There are no mappings in VT-x and AMD-V mode. */
1487 Assert(pVM->pgm.s.fMappingsDisabled);
1488 return false;
1489#else
1490 return !pVM->pgm.s.fMappingsDisabled;
1491#endif
1492}
1493
1494
1495/**
1496 * Checks if the mappings are floating and enabled.
1497 *
1498 * @returns true / false.
1499 * @param pVM Pointer to the VM.
1500 */
1501DECL_FORCE_INLINE(bool) pgmMapAreMappingsFloating(PVM pVM)
1502{
1503#ifdef PGM_WITHOUT_MAPPINGS
1504 /* There are no mappings in VT-x and AMD-V mode. */
1505 Assert(pVM->pgm.s.fMappingsDisabled);
1506 return false;
1507#else
1508 return !pVM->pgm.s.fMappingsDisabled
1509 && !pVM->pgm.s.fMappingsFixed;
1510#endif
1511}
1512
1513/** @} */
1514
1515#endif
1516
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